查找服务器计算机中的物理 CPU 插槽数
Find number of Physical CPU Sockets in server machine
我有一个带有 4 个物理处理器插槽的系统。运行Windows 2003,我想使用C++以编程方式查找套接字的数量。这是否可能,如果可能,如何?
对于 Windows 7 和 2008 服务器,有 GetActiveProcessorGroupCount 函数。但是你有 2003 服务器,所以这不是一个选项。
C++这需要编写WMI消费者代码,这是一个笨拙而无聊的(D)COM东西。
一个不错的解决方案是运行systeminfo
命令并解析输出,但要小心,因为输出的列标题已本地化为系统的语言环境。
编辑
刚刚找到了一个更好的解决方案,它利用了 WMI 的命令行界面。
运行以下命令并解析输出,每个套接字有一行
> 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
运行 exe 并在C++中解析输出应该是简单的部分。您还可以使用 POCO 库或 Boost.Process 来获得更干净的代码。
(这是未经测试的代码)
//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
您将
使用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 failuren"));
return (2);
}
}
else
{
_tprintf(TEXT("nError %dn"), 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: %dn"),
numaNodeCount);
_tprintf(TEXT("Number of physical processor sockets: %dn"),
processorPackageCount);
_tprintf(TEXT("Number of processor cores: %dn"),
processorCoreCount);
_tprintf(TEXT("Number of logical processors: %dn"),
logicalProcessorCount);
_tprintf(TEXT("Number of processor L1/L2/L3 caches: %d/%d/%dn"),
processorL1CacheCount,
processorL2CacheCount,
processorL3CacheCount);
free(buffer);
return 0;
}
相关文章:
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 从Q_INVOKABLE功能或插槽中更改QQuick图像源
- 升压信号2将插槽传递到成员功能以断开连接
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- C++ 信号和插槽不工作:插槽不响应事件
- NS3 插槽混淆(需要帮助理解)
- QObject::连接无法将信号连接到*this*对象的插槽
- QT 插槽未在主线程上调用
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 控制带有信号/插槽的Qt QML滑动视图
- Qt 信号/插槽问题
- QT5 信号不会激活插槽内的功能
- Qt5:连接:如果插槽的参数少于信号,如何使用"连接"
- 需要实例化不同类/对象并在启动时确定的硬件插槽的设计模式
- Qt信号和插槽如果从QRunnable或其他线程调用,则不起作用
- Qt插槽调用了两次
- C++Qt信号和插槽
- 我有 12 个 CPU,1 个插槽,每个插槽 6 个内核,每个内核 2 个线程 - 这些信息如何对应于 MPI 和 Op
- 查找服务器计算机中的物理 CPU 插槽数