查找服务器计算机中的物理CPU套接字数

Bas*_*wer 6 c++ cpu window windows-server-2003

我有一个带有4个物理处理器插槽的系统.运行Windows 2003,我想以编程方式查找使用C++的套接字数量.这是可能的,如果是的话,怎么样?

Cha*_*esB 2

对于 Windows 7 和 2008 服务器,有GetActiveProcessorGroupCount 函数。但你有 2003 年的服务器,所以这不是一个选择。

\n\n

在 C++ 中,这需要编写 WMI\xc2\xa0consumer 代码,这是一个笨拙且无聊的 (D)COM 东西。

\n\n

一种不错的解决方案是运行systeminfo命令并解析输出,但要小心,因为输出的列标题已本地化为系统的区域设置。

\n\n

编辑

\n\n

刚刚找到了一个更好的解决方案,它利用 WMI 的命令行界面。

\n\n

运行以下命令并解析输出,每个套接字有一行

\n\n
> wmic.exe cpu get\nAddressWidth  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  \n64            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            \n
Run Code Online (Sandbox Code Playgroud)\n\n

在 C++ 中运行 exe 并解析输出应该是简单的部分。您还可以使用 POCO 库或 Boost.Process 来获得更清晰的代码。

\n\n

(这是未经测试的代码)

\n\n
//get wmic program output \nFILE* pipe = _popen("wmic.exe cpu get", "r");\nif (!pipe) throw std::exception("error");\nchar buffer[128];\nstd::string output;\nwhile(!feof(pipe)) {\n  if(fgets(buffer, 128, pipe) != NULL)\n      output += buffer;\n}\n_pclose(pipe);\n\n//split lines to a vector<string>\nstd::stringstream oss(output);\nstd::vector<std::string> processor_description; std::string buffer;\nwhile (std::getline(oss, buffer))\n  processor_description.push_back(buffer);\n//processor_description has n+1 elements, n being nb of sockets, +1 is the header of columns\n
Run Code Online (Sandbox Code Playgroud)\n