我编写了这个小程序,以查询和显示有关系统信息的缓存信息。
#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 QuerryCacheInformation() {
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() {
QuerryCacheInformation();
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®CoreTM2 Quad CPU Q9650上运行此程序。我在Windows 7 64b上运行此程序,并使用MS Visual Studio 2017(语言标记设置为)对此进行了编译ISO C++ Latest Draft Standard (/std:c++latest)
。
根据与我的特定处理器有关的CPU-World数据表,以下是它报告的有关我的系统的体系结构及其缓存的信息:
快取等级 | 缓存属性 |
---|---|
1级缓存大小 | 4个32 KB 8路集关联指令缓存 |
4 x 32 KB 8路集关联数据缓存 | |
2级缓存大小 | 2个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);
(替换buff[i]
用info
在lambda,并通过在info
代替的buffer
向量)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句