如何获得正确数量的逻辑处理器

How to get correct number of logical processors

在Delphi中,我们需要知道并行化的CPU数量。到目前为止,我们已经使用了 GetNativeSystemInfo() 函数,它运行良好,也适用于具有超线程的服务器。

但是现在,我们有一个服务器(Intel Xeon Gold 6230)有 40 个物理处理器和 80 个带超线程的逻辑处理器,GetNativeSystemInfo() 只显示 40 个 CPU。

我们制作了一个使用 3 次调用的小测试程序:

  1. GetNativeSystemInfo()

  2. GetLogicalProcessorInformation()(代码来自 How to detect number of logical and physical processors efficiently?

  3. 并查看注册表中的 CPU 数量:

    Computer\HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor

对于我们所有的服务器,这 3 个调用提供相同数量的 CPU:

但对于 Intel Xeon,只有注册表为我们提供了 80 个 CPU:

有谁知道为什么它不适用于 Intel 服务器,或者知道确保获得最大 CPU 数量的方法吗?

GetLogicalProcessorInformation 文档中我找到了这部分:

On systems with more than 64 logical processors, the GetLogicalProcessorInformation function retrieves logical processor information about processors in the processor group to which the calling thread is currently assigned. Use the GetLogicalProcessorInformationEx function to retrieve information about processors in all processor groups on the system.

所以尝试使用 GetLogicalProcessorInformationEx

要查询大于 64 的逻辑处理器数,您必须使用更新的 GetLogicalProcessorInformationEx API,NumCPULib4Pascal 库以易于使用的方式包装它。

很遗憾,我无法在此处粘贴完整代码,因为它不符合 Whosebug 的字数限制。

下面的示例用法:

uses
  NumCPULib;

var
  lcc, pcc: Int32;
begin
  // count logical cpus
 lcc := TNumCPULib.GetLogicalCPUCount();
  // count physical cpus
 pcc := TNumCPULib.GetPhysicalCPUCount();
end;