From 32e7e0b2b76358c00f43a3c188cd9e8b776f6a8c Mon Sep 17 00:00:00 2001 From: zhenweijin Date: Fri, 2 Aug 2024 17:11:15 +0800 Subject: [PATCH] Add support for getting cpu info on Windows for llama_bench --- examples/llama-bench/llama-bench.cpp | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/examples/llama-bench/llama-bench.cpp b/examples/llama-bench/llama-bench.cpp index 521fa8880..e33d73e52 100644 --- a/examples/llama-bench/llama-bench.cpp +++ b/examples/llama-bench/llama-bench.cpp @@ -27,6 +27,14 @@ #include "ggml-cann.h" #endif +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#ifndef NOMINMAX +# define NOMINMAX +#endif +#include +#endif + // utils static uint64_t get_time_ns() { using clock = std::chrono::high_resolution_clock; @@ -96,6 +104,38 @@ static std::string get_cpu_info() { } fclose(f); } +#elif defined(_WIN32) + HKEY hKey; + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, + TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), + 0, + KEY_READ, + &hKey) != ERROR_SUCCESS) { + // fail to open registry key + return ""; + } + char cpu_brand[256]; + DWORD cpu_brand_size = sizeof(cpu_brand); + if (RegQueryValueEx(hKey, + TEXT("ProcessorNameString"), + NULL, + NULL, + (LPBYTE)cpu_brand, + &cpu_brand_size) != ERROR_SUCCESS) { + RegCloseKey(hKey); + // fail to read processor information + return ""; + } + RegCloseKey(hKey); + + // ensure the string is null-terminated + if (cpu_brand_size >= sizeof(cpu_brand)) { + cpu_brand[sizeof(cpu_brand) - 1] = '\0'; + } else { + cpu_brand[cpu_brand_size] = '\0'; + } + + id = std::string(cpu_brand); #endif // TODO: other platforms return id;