Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
New win32 platform function: GetCPUInfo
Browse files Browse the repository at this point in the history
Fixes #1644
  • Loading branch information
skomski authored and ry committed Oct 12, 2011
1 parent 59be975 commit 9557020
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/platform_win32.cc
Expand Up @@ -215,7 +215,66 @@ int Platform::GetMemory(size_t *rss, size_t *vsize) {
}

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;
}


Expand Down

0 comments on commit 9557020

Please sign in to comment.