【问题标题】:Invalid results querying my system’s cache information with GetLogicalProcessorInformation()使用 GetLogicalProcessorInformation() 查询系统缓存信息的结果无效
【发布时间】:2021-03-30 01:45:17
【问题描述】:

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

#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/缓存详情:

  • 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 个条目

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 的新手,所以任何和所有帮助、提示和建议都会很有用。

【问题讨论】:

  • 你不应该在使用Cache 字段之前检查info.Relationship == RelationCache 吗?
  • 您找到的数据表对于这样的四核 Core2 系统是正确的。一对核心(带有私有 L1i/d)共享一个 L2 缓存。不幸的是,共享 L2 的内核之间的通信与通过 DRAM 一样慢,尽管 IIRC 实际上有一个互连可以避免实际存储和重新加载 (akkadia.org/drepper/cpumemory.pdf)。 (后来的 CPU,从 Nehalem 开始,尤其是 Sandybridge,具有更多的可扩展设计。realworldtech.com/nehalem/2 将 Nehalem 与 Harpertown 进行比较,与 Yorkfield 的核心/缓存安排相同。)
  • (英特尔的四核 Core2 CPU 基本上只是将 2 个双核芯片粘贴在同一个封装中;这也是它们仅通过 FSB(前端总线)进行通信的原因之一。)
  • info.Cache.Level 根据docs 只能是 1、2 或 3。您应该删除 case 0 并将默认情况下的打印字符串更改为“确实有问题,这不是我的错!”。
  • 我强烈建议按照文档中的说明检查错误

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


【解决方案1】:

问题在于您的 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 向量)。

【讨论】:

  • 在阅读并应用它之后,它现在变得很有意义......起初,我无法轻易看到这个错误,因为它正在编译和运行,没有任何错误和警告。我什至多次通过我的调试器,但仍然无法弄清楚。像这样的时候,你需要第二双眼睛,另一个视角!!!感谢您解决我的错误!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多