From 9557020dc1bd2960313e1db52fbccb2bf3005ff8 Mon Sep 17 00:00:00 2001 From: Karl Skomski Date: Tue, 6 Sep 2011 21:28:15 +0200 Subject: [PATCH] New win32 platform function: GetCPUInfo Fixes #1644 --- src/platform_win32.cc | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/platform_win32.cc b/src/platform_win32.cc index e840bfee14..699868c012 100644 --- a/src/platform_win32.cc +++ b/src/platform_win32.cc @@ -215,7 +215,66 @@ int Platform::GetMemory(size_t *rss, size_t *vsize) { } int Platform::GetCPUInfo(Local *cpus) { - return -1; + + HandleScope scope; + *cpus = Array::New(); + + for (int i = 0; i < 32; i++) { + + char key[128] = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\"; + char processor_number[32]; + itoa(i, processor_number, 10); + strncat(key, processor_number, 2); + + HKEY processor_key = NULL; + + DWORD cpu_speed = 0; + DWORD cpu_speed_length = sizeof(cpu_speed); + + char cpu_brand[256]; + DWORD cpu_brand_length = sizeof(cpu_brand); + + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE, + &processor_key) != ERROR_SUCCESS) { + if (i == 0) { + winapi_perror("RegOpenKeyEx"); + return -1; + } + + continue; + } + + if (RegQueryValueEx(processor_key, "~MHz", NULL, NULL, + (LPBYTE)&cpu_speed, &cpu_speed_length) + != ERROR_SUCCESS) { + winapi_perror("RegQueryValueEx"); + return -1; + } + + if (RegQueryValueEx(processor_key, "ProcessorNameString", NULL, NULL, + (LPBYTE)&cpu_brand, &cpu_brand_length) + != ERROR_SUCCESS) { + winapi_perror("RegQueryValueEx"); + return -1; + } + + RegCloseKey(processor_key); + + Local times_info = Object::New(); // FIXME - find times on windows + times_info->Set(String::New("user"), Integer::New(0)); + times_info->Set(String::New("nice"), Integer::New(0)); + times_info->Set(String::New("sys"), Integer::New(0)); + times_info->Set(String::New("idle"), Integer::New(0)); + times_info->Set(String::New("irq"), Integer::New(0)); + + Local cpu_info = Object::New(); + cpu_info->Set(String::New("model"), String::New(cpu_brand)); + cpu_info->Set(String::New("speed"), Integer::New(cpu_speed)); + cpu_info->Set(String::New("times"), times_info); + (*cpus)->Set(i,cpu_info); + } + + return 0; }