使用 GetLogicalProcessorInformation()

问题描述

我编写了这个小程序来查询和显示有关我系统缓存的信息。

#include <Windows.h>
#include <iostream>
#include <string>
#include <array>
#include <vector>

template<typename T>
auto msg = [](std::string_view label,T value,std::string descriptor = std::string()) {
    std::cout << label.data() << ": " << value << descriptor << '\n' ;
};

const static std::array<std::string_view,4> CacheTypes{
    "Unified","Instruction","Data","Trace"
};

void QueryCacheInformation() {
    DWORD bufferSize = 0;
    GetLogicalProcessorInformation(0,&bufferSize);
    std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> buffer(bufferSize / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
    GetLogicalProcessorInformation(buffer.data(),&bufferSize);

    auto getCacheType = [](_PROCESSOR_CACHE_TYPE type) {
        return std::string(CacheTypes[type]); 
    };
   
    auto showAll = [&](int i,std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> &buff) {
        msg<DWORD>(std::string_view("CPU L" + std::to_string(i) + " cache type"),static_cast<int>(buff[i].Cache.Type),std::string(" " + getCacheType(buff[i].Cache.Type)) );
        msg<DWORD>(std::string_view("CPU L" + std::to_string(i) + " cache size"),buff[i].Cache.Size," bytes");
        msg<DWORD>(std::string_view("CPU L" + std::to_string(i) + " cache line size"),buff[i].Cache.LineSize," bytes");
        std::cout << '\n';
    };

    for (auto& info : buffer) {
        switch (info.Cache.Level) {
        case 0:
            break;
        case 1:           
        case 2:
        case 3:
            showAll(info.Cache.Level,buffer);
            break;
        default:
            std::cout << "System has no cache!\n";
        }
    }
}

int main() {
    QueryCacheInformation();    
    system("pause");
    return 0;
}

这是我运行这个程序时的输出:

CPU L1 cache type: 2 Data
CPU L1 cache size: 32768 bytes
CPU L1 cache line size: 64 bytes

CPU L1 cache type: 2 Data
CPU L1 cache size: 32768 bytes
CPU L1 cache line size: 64 bytes

CPU L1 cache type: 2 Data
CPU L1 cache size: 32768 bytes
CPU L1 cache line size: 64 bytes

CPU L1 cache type: 2 Data
CPU L1 cache size: 32768 bytes
CPU L1 cache line size: 64 bytes

CPU L2 cache type: 1 Instruction
CPU L2 cache size: 32768 bytes
CPU L2 cache line size: 64 bytes

CPU L1 cache type: 2 Data
CPU L1 cache size: 32768 bytes
CPU L1 cache line size: 64 bytes

CPU L1 cache type: 2 Data
CPU L1 cache size: 32768 bytes
CPU L1 cache line size: 64 bytes

CPU L1 cache type: 2 Data
CPU L1 cache size: 32768 bytes
CPU L1 cache line size: 64 bytes

CPU L1 cache type: 2 Data
CPU L1 cache size: 32768 bytes
CPU L1 cache line size: 64 bytes

CPU L2 cache type: 1 Instruction
CPU L2 cache size: 32768 bytes
CPU L2 cache line size: 64 bytes

Press any key to continue . . .

该程序似乎正在通过处理器节点进行查询并检索有关其缓存的信息并显示结果。但是,我在我的 Intel(R) Core(TM)2 Quad CPU Q9650 上运行这个程序。我在 Windows 7 64b 上运行它,我正在使用 MS Visual Studio 2017 编译它,语言标志设置为 ISO C++ Latest Draft Standard (/std:c++latest)

根据 CPU-World 与我的特定处理器相关的 datasheet,以下是关于我的系统架构及其缓存的报告:

缓存级别 缓存属性
一级缓存大小 4 x 32 KB 8 路组关联指令缓存
4 x 32 KB 8 路组关联数据缓存
二级缓存大小 2 x 6 MB 24 路组关联缓存(每个 L2 缓存在 2 个内核之间共享)

在页面下方,它有这些 CPU ID 表,其中包含有关其缓存的更多信息:

TLB/缓存详情:

  • 64 字节预取
  • 数据 TLB:4 KB 页面,4 路组关联,256 个条目
  • 数据 TLB:4 MB 页,4 路组关联,32 个条目
  • 指令 TLB:2-MB 页,4 路,8 个条目或 4M 页,4 路,4 个条目
  • 指令 TLB:4 KB 页面,4 路组关联,128 个条目
  • L1 数据 TLB:4 KB 页,4 路组关联,16 个条目
  • L1 数据 TLB:4 MB 页,4 路组关联,16 个条目

缓存: L1 数据 L1 指令 L2
尺寸: 4 x 32 KB 4 x 32 KB 2 x 6 MB
关联性: 8路组联 8路组联 24 路组关联
行大小: 64 字节 64 字节 64 字节
评论: 直接映射 直接映射 非包容性直接映射每 2 个内核 1 个缓存

根据数据表,这应该是我的 CPU 架构的框图。

Q9650 Block Diagram

然而,这与我的程序打印的结果不符。根据 GetLogicalProcessorInformation() 保存的数据结构,它声称我的 CPU 有 8 个 L1 数据缓存和 2 个 L2 指令缓存,它们都具有相同的确切大小,但事实并非如此。现在至于行大小,它们都是相同的,并且此信息似乎是正确的。只是“类型”和一些“尺寸”不是。我的 CPU 应该总共有 128KB 的 L1 数据缓存、128KB 的 L1 指令缓存和 12MB 的 L2 缓存。我不确定我哪里出错了,为什么我没有得到匹配的值和类型......

我是否正确查询和提取信息?它是在 for 循环、switch 语句还是我正在使用的 lambda 中?或者我完全忽略的其他东西?

我是这个 API 的新手,所以任何和所有帮助、技巧和建议都会很有用。

解决方法

问题出在您的 lambda 和您传递给它的数据中。当您应该从循环中传入 i 值时,您使用 buff(缓存级别)访问 info 的元素。

将 lambda 和调用站点更改为如下所示:

    auto showAll = [&](int i,SYSTEM_LOGICAL_PROCESSOR_INFORMATION &info) {
        msg<DWORD>(std::string_view("CPU L" + std::to_string(i) + " cache type"),static_cast<int>(info.Cache.Type),std::string(" " + getCacheType(info.Cache.Type)) );
        msg<DWORD>(std::string_view("CPU L" + std::to_string(i) + " cache size"),info.Cache.Size," bytes");
        msg<DWORD>(std::string_view("CPU L" + std::to_string(i) + " cache line size"),info.Cache.LineSize," bytes");
        std::cout << '\n';
    };
// ...
    showAll(info.Cache.Level,info);

(在 lambda 中用 buff[i] 替换 info,并传入 info 而不是 buffer 向量)。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...