现在多核处理器已经很普及了,市场主流是双核处理器,还有4核、8核等高端产品。而且Intel推广了超线程技术(Hyper-Threading
Technology,
HTT),可以将一个物理核心模拟为两个逻辑处理器。这一切使得“CPU数量”这一概念变得复杂起来,对于软件开发人员来说,希望能获得物理CPU数、
CPU核心数、逻辑CPU数等详细信息。
GetLogicalProcessorInformation函数用起来是有一定复杂性的。因为它返回的是SYSTEM_LOGICAL_PROCESSOR_INFORMATION数组,数组中的每一项分别描述了不同的信息,学习曲线较陡峭。
虽然MSDN上有该函数的范例代码,但是它屏蔽了很多细节,对我们的帮助有限。于是我将该范例程序作了改进,显示了SYSTEM_LOGICAL_PROCESSOR_INFORMATION数组中每一项的详细信息。
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[0
]
.ProcessorMask: 0x0000000000000005 //5
.Relationship: 0 //
RelationProcessorCore
.Reserved[0]: //0x0000000000000001 1
.Reserved[1]: //0x0000000000000000 0
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[1
]
.ProcessorMask: 0x0000000000000005 //5
.Relationship: 2 //
RelationCache
.Reserved[0]: //0x0000800000400801 140737492551681
.Reserved[1]: //0x0000000000000002 2
.
Cache.Level: 1
.
Cache.Associativity: 0x08 //8
.
Cache.LineSize: 0x0040 //64
.
Cache.Size: 0x00008000 //32768
.
Cache.
Type: 2 //
CacheData
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[2
]
.ProcessorMask: 0x0000000000000005 //5
.Relationship: 2 //
RelationCache
.Reserved[0]: //0x0000800000400801 140737492551681
.Reserved[1]: //0x0000000000000001 1
.
Cache.Level: 1
.
Cache.Associativity: 0x08 //8
.
Cache.LineSize: 0x0040 //64
.
Cache.Size: 0x00008000 //32768
.
Cache.
Type: 1 //
CacheInstruction
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[3
]
.ProcessorMask: 0x0000000000000005 //5
.Relationship: 2 //
RelationCache
.Reserved[0]: //0x0004000000400802 1125899911038978
.Reserved[1]: //0x0000000000000000 0
.
Cache.Level: 2
.
Cache.Associativity: 0x08 //8
.
Cache.LineSize: 0x0040 //64
.
Cache.Size: 0x00040000 //262144
.
Cache.
Type: 0 //
CacheUnified
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[4
]
.ProcessorMask: 0x000000000000000F //15
.Relationship: 3 //
RelationProcessorPackage
.Reserved[0]: //0x0000000000000000 0
.Reserved[1]: //0x0000000000000000 0
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[5
]
.ProcessorMask: 0x000000000000000A //10
.Relationship: 0 //
RelationProcessorCore
.Reserved[0]: //0x0000000000000001 1
.Reserved[1]: //0x0000000000000000 0
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[6
]
.ProcessorMask: 0x000000000000000A //10
.Relationship: 2 //
RelationCache
.Reserved[0]: //0x0000800000400801 140737492551681
.Reserved[1]: //0x0000000000000002 2
.
Cache.Level: 1
.
Cache.Associativity: 0x08 //8
.
Cache.LineSize: 0x0040 //64
.
Cache.Size: 0x00008000 //32768
.
Cache.
Type: 2 //
CacheData
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[7
]
.ProcessorMask: 0x000000000000000A //10
.Relationship: 2 //
RelationCache
.Reserved[0]: //0x0000800000400801 140737492551681
.Reserved[1]: //0x0000000000000001 1
.
Cache.Level: 1
.
Cache.Associativity: 0x08 //8
.
Cache.LineSize: 0x0040 //64
.
Cache.Size: 0x00008000 //32768
.
Cache.
Type: 1 //
CacheInstruction
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[8
]
.ProcessorMask: 0x000000000000000A //10
.Relationship: 2 //
RelationCache
.Reserved[0]: //0x0004000000400802 1125899911038978
.Reserved[1]: //0x0000000000000000 0
.
Cache.Level: 2
.
Cache.Associativity: 0x08 //8
.
Cache.LineSize: 0x0040 //64
.
Cache.Size: 0x00040000 //262144
.
Cache.
Type: 0 //
CacheUnified
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[9
]
.ProcessorMask: 0x000000000000000F //15
.Relationship: 2 //
RelationCache
.Reserved[0]: //0x0030000000400C03 13510798886308867
.Reserved[1]: //0x0000000000000000 0
.
Cache.Level: 3
.
Cache.Associativity: 0x0C //12
.
Cache.LineSize: 0x0040 //64
.
Cache.Size: 0x00300000 //3145728
.
Cache.
Type: 0 //
CacheUnified
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[10
]
.ProcessorMask: 0x000000000000000F //15
.Relationship: 1 //
RelationNumaNode
.Reserved[0]: //0x0000000000000000 0
.Reserved[1]: //0x0000000000000000 0
GetLogicalProcessorInformation results:
Number of NUMA nodes: 1
Number of physical processor packages: 1
Number of processor cores: 2
Number of logical processors: 4
Number of processor L1/L2/L3 caches: 4/2/1