c++ - 使用 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 与我的特定处理器有关的数据表,这是它报告的关于我的系统架构及其缓存的内容:
缓存级别 | 缓存属性 |
---|---|
一级缓存大小 | 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 架构的框图。
但是,这与我的程序的打印结果不匹配。根据保存的数据结构,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
向量)。
推荐阅读
- windows - 带有 Powershell Send-MailMessage 的 Windows 批处理 - 一半的换行符未显示
- php - 从数组中获取重复的下一个值的计数并将其存储到其他数组
- csv - 如何在 Neo4j 中从外部网站导入 csv?
- amazon-web-services - 如何从 EventBridge 定位 http api 网关或 ALB
- ios - 目标 C 显示最近的问题未定义的符号:_OBJC_CLASS_$_FIRApp
- excel - 如何在excel vba中将范围分配给命名范围?
- java - 普通类和注入类的区别
- python-3.x - 当我尝试在 Google Collab 中克隆 github 存储库时,为什么会收到“错误:命令错误,退出状态为 1”?
- reactjs - 为什么 react-image-magnify 在 swiper js 中不起作用?
- javascript - 多个动态输入 onchange 和 onblur 函数 javascript