Skip to content

Commit

Permalink
(MSVC, C++11) get CPU cache by "Win32_CacheMemory" and bug fix of GPU…
Browse files Browse the repository at this point in the history
… ram (#62)

* Update cpu.cpp

use "Win32_CacheMemory" to get CPU cache size, because "Win32_Processor" can not get L1 cache

* Update gpu.cpp

Data type of "AdapterRAM " is "uint32", 
using "int" will cause overflow
  • Loading branch information
MRsoymilk authored Oct 26, 2023
1 parent b04b26b commit cc6f08d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/windows/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,27 @@ std::vector<CPU> getAllCPUs() {
cpu._maxClockSpeed_MHz = vt_prop.uintVal;
cpu._regularClockSpeed_MHz = vt_prop.uintVal;
}
hr = obj->Get(L"L2CacheSize", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
cpu._L2CacheSize_Bytes = vt_prop.uintVal;
}
hr = obj->Get(L"L3CacheSize", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
cpu._L3CacheSize_Bytes = vt_prop.uintVal;
}
VariantClear(&vt_prop);
obj->Release();
auto cache{[&]() -> void {
auto data = utils::WMI::query<std::uint32_t>(L"Win32_CacheMemory", L"MaxCacheSize");
if (data.empty()) {
cpu._L1CacheSize_Bytes = cpu._L2CacheSize_Bytes = cpu._L3CacheSize_Bytes = -1;
}
try {
cpu._L1CacheSize_Bytes = data.at(0);
cpu._L2CacheSize_Bytes = data.at(1);
cpu._L3CacheSize_Bytes = data.at(2);
} catch (const std::exception& e) {
std::cout << "Exception in CPU Cache: " << e.what() << std::endl;
}
}};
cache();
cpus.push_back(std::move(cpu));
}
return cpus;
}

} // namespace hwinfo

#endif // HWINFO_WINDOWS
#endif // HWINFO_WINDOWS
2 changes: 1 addition & 1 deletion src/windows/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ std::vector<GPU> getAllGPUs() {
}
hr = obj->Get(L"AdapterRam", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
gpu._memory_Bytes = vt_prop.intVal;
gpu._memory_Bytes = vt_prop.uintVal;
}
VariantClear(&vt_prop);
obj->Release();
Expand Down

0 comments on commit cc6f08d

Please sign in to comment.