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

标签 c++ winapi x86 cpu-architecture cpu-cache

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

#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关于我的特定处理器,以下是它报告的有关我的系统架构及其缓存的内容:

<表类="s-表"> <头> 缓存级别 缓存属性 <正文> 一级缓存大小 4 x 32 KB 8 路组相联指令缓存 4 x 32 KB 8 路组关联数据缓存 二级缓存大小 2 x 6 MB 24 路组关联缓存(每个 L2 缓存在 2 个内核之间共享)

在页面下方,它有这些 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

<表类="s-表"> <头> 缓存: 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 和您传递给它的数据中。当您应该从循环中传递 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 vector )。

关于c++ - 使用 GetLogicalProcessorInformation() 查询系统缓存信息的结果无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65373394/

相关文章:

c++ - Qt 图表和数据可视化小部件

c++ - 如何同时等待 I/O 完成端口和事件?

c - Win32 API CopyFile() 无法发送多个文件

x86 - 处理器如何处理条件?

类中的 C++ 字符串

c++ - 为什么 C++ 并发在 action listing_6.1 中不使用 std::recursive_mutex

delphi - 如何将指向句柄列表的指针传递给 UpdateProcThreadAttribute 函数

c++ - 为什么在 32 位 x86 架构上默认对齐 `int64_t` 8 字节?

assembly - 具有自己的操作码的EAX寄存器上的操作有什么意义?

c++ - 如何在函数/方法中使用 sf::window