From 1efac74958c03d6edcab7e1880fc841db7a8eb30 Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 18 Feb 2011 19:02:16 -0500 Subject: [PATCH] Fix os.cpus() on cygwin --- src/platform_cygwin.cc | 12 ++++++++---- src/platform_linux.cc | 9 +++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/platform_cygwin.cc b/src/platform_cygwin.cc index 416cb17..9338d4f 100644 --- a/src/platform_cygwin.cc +++ b/src/platform_cygwin.cc @@ -99,8 +99,7 @@ static inline char* _getProcessTitle() { if (GetLastError()) { _winapi_perror("GetConsoleTitleW"); return NULL; - } - else { + } else { // The title is empty, so return empty string process_title = strdup("\0"); return process_title; @@ -252,6 +251,7 @@ int Platform::GetExecutablePath(char* buffer, size_t* size) { } int Platform::GetCPUInfo(Local *cpus) { + HandleScope scope; Local cpuinfo; Local cputimes; unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK), @@ -285,14 +285,17 @@ int Platform::GetCPUInfo(Local *cpus) { if (fpStat) { while (fgets(line, 511, fpStat) != NULL) { - if (strncmp(line, "cpu ", 4) == 0) + if (strncmp(line, "cpu ", 4) == 0) { continue; - else if (strncmp(line, "intr ", 5) == 0) + } else if (strncmp(line, "cpu", 3) != 0) { break; + } + sscanf(line, "%*s %llu %llu %llu %llu", &ticks_user, &ticks_nice, &ticks_sys, &ticks_idle); snprintf(speedPath, sizeof(speedPath), "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", i); + fpSpeed = fopen(speedPath, "r"); if (fpSpeed) { if (fgets(line, 511, fpSpeed) != NULL) { @@ -355,4 +358,5 @@ int Platform::GetLoadAvg(Local *loads) { return -1; } + } // namespace node diff --git a/src/platform_linux.cc b/src/platform_linux.cc index 00732a0..4739977 100644 --- a/src/platform_linux.cc +++ b/src/platform_linux.cc @@ -183,15 +183,19 @@ int Platform::GetCPUInfo(Local *cpus) { if (fpStat) { while (fgets(line, 511, fpStat) != NULL) { - if (strncmp(line, "cpu ", 4) == 0) + if (strncmp(line, "cpu ", 4) == 0) { continue; - else if (strncmp(line, "intr ", 5) == 0) + } else if (strncmp(line, "cpu", 3) != 0) { break; + } + sscanf(line, "%*s %llu %llu %llu %llu %*llu %llu", &ticks_user, &ticks_nice, &ticks_sys, &ticks_idle, &ticks_intr); snprintf(speedPath, sizeof(speedPath), "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", i); + fpSpeed = fopen(speedPath, "r"); + if (fpSpeed) { if (fgets(line, 511, fpSpeed) != NULL) { sscanf(line, "%u", &cpuspeed); @@ -199,6 +203,7 @@ int Platform::GetCPUInfo(Local *cpus) { } fclose(fpSpeed); } + cpuinfo = Object::New(); cputimes = Object::New(); cputimes->Set(String::New("user"), Number::New(ticks_user * multiplier)); -- 2.7.4