使用 GetLogicalProcessorInformation() 查询系统缓存信息的结果无效

Invalid results querying my system’s cache information with 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 与我的特定处理器有关,这是它报告的有关我的系统架构及其缓存的内容:

Cache Level Cache Properties
Level 1 cache size 4 x 32 KB 8-way set associative instruction caches
4 x 32 KB 8-way set associative data caches
Level 2 cache size 2 x 6 MB 24-way set associative caches (each L2 cache is shared between 2 cores)

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

TLB/Cache details:

  • 64-byte Prefetching
  • Data TLB: 4-KB Pages, 4-way set associative, 256 entries
  • Data TLB: 4-MB Pages, 4-way set associative, 32 entries
  • Instruction TLB: 2-MB pages, 4-way, 8 entries or 4M pages, 4-way, 4 entries
  • Instruction TLB: 4-KB Pages, 4-way set associative, 128 entries
  • L1 Data TLB: 4-KB pages, 4-way set associative, 16 entries
  • L1 Data TLB: 4-MB pages, 4-way set associative, 16 entries

Cache: L1 Data L1 Instruction L2
Size: 4 x 32 KB 4 x 32 KB 2 x 6 MB
Associativity: 8-way set associative 8-way set associative 24-way set associative
Line Size: 64 bytes 64 bytes 64 bytes
Comments: Direct-mapped Direct-mapped Non-inclusive Direct-Mapped 1 cache per 2 cores

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

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

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

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

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

将 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 向量)。