标签:
CPU个数:这里指物理上的CPU个数,即在dos界面中,输入systeminfo时,显示信息中的处理器个数
CPU核数:这里指CPU核心数,即在dos界面中,输入wmic后,在键入CPU get * 时所显示的NumberOfCores
CPU线程数:这里指在dos界面中,输入wmic后,在键入CPU get * 时所显示的NumberOfLogicalProcessors
通常情况下,我们的CPU说的是几核,如双核,四核等,指的就是核心,而现在有种技术是超线程技术,此技术即为一个核心,两个线程;而正常情况下,一个核心只有一个线程;
除了在dos界面下可以看到CPU核数(像上述方法那样),还可以在Windows界面的任务管理器中看,性能下的CPU显示的栏数,即可以从分的栏数看到CPU数目。
在C++编写中查看CPU相关的信息,选择GetLogicalProcessorInformation对CPU的一些信息进行获取,代码如下:
1 #include <windows.h> 2 #include <malloc.h> 3 #include <stdio.h> 4 #include <tchar.h> 5 6 #if (_WIN32_WINNT < 0x0600) // [zyl910] 低版本的Windows SDK没有定义 RelationProcessorPackage 等常量 7 #define RelationProcessorPackage 3 8 #define RelationGroup 4 9 #endif 10 // [zyl910] LOGICAL_PROCESSOR_RELATIONSHIP枚举的名称 11 const LPTSTR Names_LOGICAL_PROCESSOR_RELATIONSHIP[] = { 12 _T("RelationProcessorCore") 13 ,_T("RelationProcessorPackage") 14 }; 15 16 typedef BOOL (WINAPI *LPFN_GLPI)( 17 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, 18 PDWORD); 19 20 21 // Helper function to count set bits in the processor mask. 22 DWORD CountSetBits(ULONG_PTR bitMask) 23 { 24 DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1; 25 DWORD bitSetCount = 0; 26 ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT; 27 DWORD i; 28 29 for (i = 0; i <= LSHIFT; ++i) 30 { 31 bitSetCount += ((bitMask & bitTest)?1:0); 32 bitTest/=2; 33 } 34 35 return bitSetCount; 36 } 37 38 int main () 39 { 40 LPFN_GLPI glpi; 41 BOOL done = FALSE; 42 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; 43 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; 44 DWORD returnLength = 0; 45 DWORD logicalProcessorCount = 0; 46 DWORD processorCoreCount = 0; 47 DWORD processorPackageCount = 0; 48 DWORD byteOffset = 0; 49 50 glpi = (LPFN_GLPI) GetProcAddress( 51 GetModuleHandle(TEXT("kernel32")), 52 "GetLogicalProcessorInformation"); 53 if (NULL == glpi) 54 { 55 printf("\nGetLogicalProcessorInformation is not supported.\n"); 56 return (1); 57 } 58 59 while (!done) 60 { 61 DWORD rc = glpi(buffer, &returnLength); 62 63 if (FALSE == rc) 64 { 65 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) 66 { 67 if (buffer) 68 free(buffer); 69 70 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc( 71 returnLength); 72 73 if (NULL == buffer) 74 { 75 printf("\nError: Allocation failure\n"); 76 return (2); 77 } 78 } 79 else 80 { 81 printf("\nError %d\n", GetLastError()); 82 return (3); 83 } 84 } 85 else 86 { 87 done = TRUE; 88 } 89 } 90 91 ptr = buffer; 92 93 if (true) // [zyl910] 显示SYSTEM_LOGICAL_PROCESSOR_INFORMATION结构体的详细信息 94 { 95 DWORD cnt = returnLength / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); // 计算SYSTEM_LOGICAL_PROCESSOR_INFORMATION结构体的数目 96 for(DWORD i=0; i<cnt; ++i) 97 { 98 printf("SYSTEM_LOGICAL_PROCESSOR_INFORMATION[%d]\n", i); printf("\t.ProcessorMask:\t0x%.16I64X\t//%I64d\n", (UINT64)ptr[i].ProcessorMask, (UINT64)ptr[i].ProcessorMask); 99 printf("\t.Relationship:\t%d\t//%s\n", ptr[i].Relationship, Names_LOGICAL_PROCESSOR_RELATIONSHIP[max(0,min(ptr[i].Relationship, RelationGroup))]); 100 for(int j=0; j<2; ++j) printf("\t.Reserved[%d]:\t//0x%.16I64X\t%I64d\n", j, (UINT64)ptr[i].Reserved[j], (UINT64)ptr[i].Reserved[j]); 101 } 102 } 103 104 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) 105 { 106 switch (ptr->Relationship) 107 { 108 case RelationProcessorCore: 109 processorCoreCount++; 110 111 // A hyperthreaded core supplies more than one logical processor. 112 logicalProcessorCount += CountSetBits(ptr->ProcessorMask); 113 break; 114 case RelationProcessorPackage: 115 // Logical processors share a physical package. 116 processorPackageCount++; 117 break; 118 } 119 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); 120 ptr++; 121 } 122 123 printf("\nGetLogicalProcessorInformation results:\n"); 124 printf("Number of physical processor packages: %d\n", 125 processorPackageCount); 126 printf("Number of processor cores: %d\n",processorCoreCount); 127 printf("Number of logical processors : %d\n" , logicalProcessorCount); 128 129 free(buffer); 130 131 return 0; 132 }
C++编写代码查看CPU个数,单个CPU核数,单个CPU线程数
标签:
原文地址:http://www.cnblogs.com/zhangshuang0909/p/5452753.html