From cc6f08d5afaf2ca97a1529242ba94a1a90d2713d Mon Sep 17 00:00:00 2001 From: milk Date: Fri, 27 Oct 2023 02:41:56 +0800 Subject: [PATCH] (MSVC, C++11) get CPU cache by "Win32_CacheMemory" and bug fix of GPU 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 --- src/windows/cpu.cpp | 24 +++++++++++++++--------- src/windows/gpu.cpp | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/windows/cpu.cpp b/src/windows/cpu.cpp index f2fa121e..dad3e332 100644 --- a/src/windows/cpu.cpp +++ b/src/windows/cpu.cpp @@ -132,16 +132,22 @@ std::vector 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(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; @@ -149,4 +155,4 @@ std::vector getAllCPUs() { } // namespace hwinfo -#endif // HWINFO_WINDOWS \ No newline at end of file +#endif // HWINFO_WINDOWS diff --git a/src/windows/gpu.cpp b/src/windows/gpu.cpp index 3dd06c49..518fc84a 100644 --- a/src/windows/gpu.cpp +++ b/src/windows/gpu.cpp @@ -58,7 +58,7 @@ std::vector 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();