显示CPU信息的程序
2012-11-07 10:51
411 查看
//程序可以直接编译运行,是我在MSDN的例子基础上加了超线程判断的函数。">#include <windows.h> #include <malloc.h> #include <stdio.h> #include <tchar.h> typedef BOOL (WINAPI *LPFN_GLPI)( PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); BOOL hyperThreadingOn() { DWORD rEbx, rEdx; __asm { push eax // save registers used push ebx push ecx push edx xor eax,eax // cpuid(1) add al, 0x01 _emit 0x0F _emit 0xA2 mov rEdx, edx // Features Flags, bit 28 indicates if HTT (Hyper-Thread Technology) is // available, but not if it is on; if on, Count of logical processors > 1. mov rEbx, ebx // Bits 23-16: Count of logical processors. // Valid only if Hyper-Threading Technology flag is set. pop edx // restore registers used pop ecx pop ebx pop eax } return (rEdx & (1<<28)) && (((rEbx & 0x00FF0000) >> 16) > 1); } // Helper function to count set bits in the processor mask. DWORD CountSetBits(ULONG_PTR bitMask) { DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1; DWORD bitSetCount = 0; ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT; DWORD i; for (i = 0; i <= LSHIFT; ++i) { bitSetCount += ((bitMask & bitTest)?1:0); bitTest/=2; } return bitSetCount; } int _cdecl _tmain () { LPFN_GLPI glpi; BOOL done = FALSE; PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; DWORD returnLength = 0; DWORD logicalProcessorCount = 0; DWORD numaNodeCount = 0; DWORD processorCoreCount = 0; DWORD processorL1CacheCount = 0; DWORD processorL2CacheCount = 0; DWORD processorL3CacheCount = 0; DWORD processorPackageCount = 0; DWORD byteOffset = 0; PCACHE_DESCRIPTOR Cache; glpi = (LPFN_GLPI) GetProcAddress( GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation"); if (NULL == glpi) { _tprintf(TEXT("\nGetLogicalProcessorInformation is not supported.\n")); return (1); } while (!done) { DWORD rc = glpi(buffer, &returnLength); if (FALSE == rc) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { if (buffer) free(buffer); buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc( returnLength); if (NULL == buffer) { _tprintf(TEXT("\nError: Allocation failure\n")); return (2); } } else { _tprintf(TEXT("\nError %d\n"), GetLastError()); return (3); } } else { done = TRUE; } } ptr = buffer; while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) { switch (ptr->Relationship) { case RelationNumaNode: // Non-NUMA systems report a single record of this type. numaNodeCount++; break; case RelationProcessorCore: processorCoreCount++; // A hyperthreaded core supplies more than one logical processor. logicalProcessorCount += CountSetBits(ptr->ProcessorMask); break; case RelationCache: // Cache data is in ptr->Cache, one CACHE_DESCRIPTOR structure for each cache. Cache = &ptr->Cache; if (Cache->Level == 1) { processorL1CacheCount++; } else if (Cache->Level == 2) { processorL2CacheCount++; } else if (Cache->Level == 3) { processorL3CacheCount++; } break; case RelationProcessorPackage: // Logical processors share a physical package. processorPackageCount++; break; default: _tprintf(TEXT("\nError: Unsupported LOGICAL_PROCESSOR_RELATIONSHIP value.\n")); break; } byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ptr++; } BOOL bHyperThreading = hyperThreadingOn(); _tprintf(TEXT("\nGetLogicalProcessorInformation results:\n")); _tprintf(TEXT("Number of NUMA nodes: %d\n"), numaNodeCount); _tprintf(TEXT("Number of physical processor packages: %d\n"), processorPackageCount); _tprintf(TEXT("Number of processor cores: %d\n"), processorCoreCount); _tprintf(TEXT("Number of logical processors: %d\n"), logicalProcessorCount); _tprintf(TEXT("Number of processor L1/L2/L3 caches: %d/%d/%d\n"), processorL1CacheCount, processorL2CacheCount, processorL3CacheCount); _tprintf(TEXT("HyperThreading is %s\n"),bHyperThreading?TEXT("ON"):TEXT("OFF")); free(buffer); return 0; }
相关文章推荐
- node中"require"不是内部或外部命令,也不是可运行的程序 或批处理文件;express -V无法显示express版本信息等问题。
- cpu显示详细信息
- 设置程序中版本显示信息
- 华为手机调试程序不显示log信息
- 你不一定懂的cpu显示信息
- MyEclipse/Eclipse运行程序后console显示信息不全问题
- 一个遍历文件夹内容,显示文件名,大小等信息的MFC程序
- LINUX的TOP信息里显示的CPU内容的含义
- MyEclipse/Eclipse运行程序后console显示信息不全问题
- Windows核心编程<读书笔记四之程序>显示系统中进程、线程、模块详细信息
- gdb调试程序不能显示线程创建信息问题
- android程序一个注册,然后到设置兴趣爱好,出生日期,然后把所有信息显示在另一个页面的程序
- 关于show the Debug navigation不显示CPU Memory等信息
- 你不一定懂的cpu显示信息
- VC++设计图形显示CPU内存利用率程序
- 你不一定懂的cpu显示信息
- MyEclipse/Eclipse运行程序后console显示信息不全问题
- MFC程序 调用控制台 显示调试信息
- 你不一定懂的cpu显示信息
- 如何让Delphi的程序在SQL Server控制台中的进程信息显示应用程序的名字?