#include #include using namespace std; // This API is only on Windows 2003 or later // extern "C" { typedef WINADVAPI BOOL ( WINAPI *PGetLogicalProcessorInformation)( PSYSTEM_LOGICAL_PROCESSOR_INFORMATION Buffer, PDWORD ReturnLength ); }; int __cdecl main (int argc, char* rgArg []) { BOOL status; UCHAR nodeNumber; PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pInfo; DWORD structureLength; ULONG_PTR processorMask; DWORD cpuId; ULONG_PTR cpuAffinity; ULONG numElements; ULONG elementId;; PGetLogicalProcessorInformation pGetLogicalProcInfo; // Check if API to retrieve core information is available // pGetLogicalProcInfo = reinterpret_cast ( GetProcAddress ( GetModuleHandle ("kernel32.dll"), "GetLogicalProcessorInformation") ); if (pGetLogicalProcInfo != NULL) { cout << "API GetLogicalProcessorInformation is available\n"; structureLength = 0; // Retrieve length information required to store the data // pGetLogicalProcInfo(NULL, &structureLength); if (structureLength != 0) { // Allocate buffer // pInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION) new char[structureLength]; if (pInfo != NULL) { // Retrieve actual data // if (pGetLogicalProcInfo(pInfo, &structureLength) == TRUE) { // Calculate number of elements in the processor information array // numElements = structureLength / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); for (elementId = 0; elementId < numElements; elementId++) { processorMask = pInfo [elementId].ProcessorMask; cpuId = 0; cpuAffinity = 1; cout << "CPUs:\n"; // Output related cpu Ids and their affinities // while (processorMask != 0) { if (processorMask & 1) { // Output CPU id // cout << cpuId << " (Affinity = " << cpuAffinity << ")\n"; } cpuId ++; cpuAffinity <<= 1; processorMask >>= 1; } // Output relationship information // cout << "\t\tShare "; switch (pInfo[elementId].Relationship) { case RelationProcessorCore : { if (pInfo[elementId].ProcessorCore.Flags == 1) { cout << "a single processor core (HT).\n"; } else { cout <<"a single processor socket (Multi Core).\n"; } } break; case RelationCache: { cout << "a cache \n"; } break; case RelationNumaNode: { cout << "the same NUMA node.\n"; cout << "\t\tThe node Id is " << pInfo[elementId].NumaNode.NodeNumber << "\n"; } break; default: { cout << "Unknown relationship: " << pInfo[elementId].Relationship << "\n"; } } } } // Don't forget to clean up memory // delete [] (char*) pInfo; } else { cout << "Failed to allocate a buffer to process " << "GetLogicalProcessorInformation output "; } } else { cout << "New API GetLogicalProcessorInformation FAILED!\n"; } } else { cout << "New API GetLogicalProcessorInformation is _not_ available\n\n"; } return 0; }