Browse Source

New win32 platform function: GetCPUInfo

Fixes #1644
v0.7.4-release
Karl Skomski 13 years ago
committed by Ryan Dahl
parent
commit
9557020dc1
  1. 61
      src/platform_win32.cc

61
src/platform_win32.cc

@ -215,7 +215,66 @@ int Platform::GetMemory(size_t *rss, size_t *vsize) {
} }
int Platform::GetCPUInfo(Local<Array> *cpus) { int Platform::GetCPUInfo(Local<Array> *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<Object> 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<Object> 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;
} }

Loading…
Cancel
Save