I have a system with 4 Physical Processor sockets. Running Windows 2003, I would like to programmatically find the number of sockets using C++. Is this possible and if so, how?
-
Do you want total nb of sockets or nb of cpu? it matters if not all sockets have a cpu plugged in... – CharlesB Jun 18 '12 at 12:37
-
Number of CPUs, physical sockets that are in use – Basit Anwer Jun 18 '12 at 12:50
-
1possible duplicate of [How to find the Number of CPU Cores via .NET/C#?](http://stackoverflow.com/questions/1542213/how-to-find-the-number-of-cpu-cores-via-net-c). Answer can be easily translated to C++, since it uses WMI. – CharlesB Jun 18 '12 at 13:03
-
Oh, and also [Find number of Physical CPU Sockets in server machine](http://stackoverflow.com/q/150355/11343) – CharlesB Jun 18 '12 at 13:06
-
1@CharlesB your second link That provides you with the number of logical cores. Your first link, how can i convert it to c++ ? – Basit Anwer Jun 18 '12 at 13:10
-
WMI is pretty tough to use in C++, I haven't done it myself but you should check stuff like http://www.codeproject.com/Articles/10539/Making-WMI-Queries-In-C – CharlesB Jun 18 '12 at 13:45
2 Answers
For Windows 7 and 2008 server there is the GetActiveProcessorGroupCount function. But you have 2003 server, so it's not an option.
In C++ this requires to write WMI consumer code, which is a clumsy and boring (D)COM stuff.
One nice solution would be to run systeminfo
command and parse the output, but be careful as the output's column header is localized to system's locale.
EDIT
Just found a much nicer solution, which makes use of the command-line interface to WMI.
Run the following command and parse output, there is one line per socket
> wmic.exe cpu get
AddressWidth Architecture Availability Caption ConfigManagerErrorCode ConfigManagerUserConfig CpuStatus CreationClassName CurrentClockSpeed CurrentVoltage DataWidth Description DeviceID ErrorCleared ErrorDescription ExtClock Family InstallDate L2CacheSize L2CacheSpeed L3CacheSize L3CacheSpeed LastErrorCode Level LoadPercentage Manufacturer MaxClockSpeed Name NumberOfCores NumberOfLogicalProcessors OtherFamilyDescription PNPDeviceID PowerManagementCapabilities PowerManagementSupported ProcessorId ProcessorType Revision Role SocketDesignation Status StatusInfo Stepping SystemCreationClassName SystemName UniqueId UpgradeMethod Version VoltageCaps
64 9 3 Intel64 Family 6 Model 23 Stepping 6 1 Win32_Processor 2532 33 64 Intel64 Family 6 Model 23 Stepping 6 CPU0 421 2 0 0 6 1 GenuineIntel 2532 Intel(R) Core(TM)2 Duo CPU T9400 @ 2.53GHz 2 2 FALSE BFEBFBFF00010676 3 5894 CPU CPU Socket #0 OK 3 Win32_ComputerSystem CHBROSSO-WIN7VM 1 2
Running an exe and parsing output in C++ should the easy part. You can also use POCO library or Boost.Process to have cleaner code.
(this is untested code)
//get wmic program output
FILE* pipe = _popen("wmic.exe cpu get", "r");
if (!pipe) throw std::exception("error");
char buffer[128];
std::string output;
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
output += buffer;
}
_pclose(pipe);
//split lines to a vector<string>
std::stringstream oss(output);
std::vector<std::string> processor_description; std::string buffer;
while (std::getline(oss, buffer))
processor_description.push_back(buffer);
//processor_description has n+1 elements, n being nb of sockets, +1 is the header of columns

- 86,532
- 28
- 194
- 218
-
umm, can you also help me out with the running the exe and getting the returned value part ? – Basit Anwer Jun 19 '12 at 07:05
you will find the number of sockets using C++. https://msdn.microsoft.com/en-us/library/windows/desktop/ms683194(v=vs.85).aspx
#include <windows.h>
#include <malloc.h>
#include <stdio.h>
#include <tchar.h>
typedef BOOL (WINAPI *LPFN_GLPI)(
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION,
PDWORD);
// 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++;
}
_tprintf(TEXT("\nGetLogicalProcessorInformation results:\n"));
_tprintf(TEXT("Number of NUMA nodes: %d\n"),
numaNodeCount);
_tprintf(TEXT("Number of physical processor sockets: %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);
free(buffer);
return 0;
}

- 442
- 5
- 21