From e8cf98c841c8aa80975fa2bd96187f49a6cbb760 Mon Sep 17 00:00:00 2001 From: Scott McWhirter Date: Sun, 27 Mar 2011 03:14:34 -0700 Subject: [PATCH] Add os.cpus() and os.uptime() support for sunos --- src/platform.h | 6 --- src/platform_sunos.cc | 99 +++++++++++++++++++++++++++--------------- test/simple/test-os.js | 34 +++++++++++---- 3 files changed, 90 insertions(+), 49 deletions(-) diff --git a/src/platform.h b/src/platform.h index 3227a3d666..5aad8e5e2c 100644 --- a/src/platform.h +++ b/src/platform.h @@ -43,12 +43,6 @@ class Platform { static double GetUptime(); static int GetLoadAvg(v8::Local *loads); static v8::Handle GetInterfaceAddresses(); - private: - static double GetUptimeImpl(); - static double prog_start_time; -#ifdef __sun - static v8::Handle data_named(kstat_named_t *); -#endif }; diff --git a/src/platform_sunos.cc b/src/platform_sunos.cc index ab5de74aa7..236d912a79 100644 --- a/src/platform_sunos.cc +++ b/src/platform_sunos.cc @@ -50,6 +50,7 @@ namespace node { using namespace v8; + char** Platform::SetupArgs(int argc, char *argv[]) { return argv; } @@ -112,7 +113,34 @@ int Platform::GetExecutablePath(char* buffer, size_t* size) { } -// TODO: libkstat provides all this info. Need to link it though. +static Handle data_named(kstat_named_t *knp) { + Handle val; + + switch (knp->data_type) { + case KSTAT_DATA_CHAR: + val = Number::New(knp->value.c[0]); + break; + case KSTAT_DATA_INT32: + val = Number::New(knp->value.i32); + break; + case KSTAT_DATA_UINT32: + val = Number::New(knp->value.ui32); + break; + case KSTAT_DATA_INT64: + val = Number::New(knp->value.i64); + break; + case KSTAT_DATA_UINT64: + val = Number::New(knp->value.ui64); + break; + case KSTAT_DATA_STRING: + val = String::New(KSTAT_NAMED_STR_PTR(knp)); + break; + default: + throw (String::New("unrecognized data type")); + } + + return (val); +} int Platform::GetCPUInfo(Local *cpus) { @@ -120,6 +148,7 @@ int Platform::GetCPUInfo(Local *cpus) { Local cpuinfo; Local cputimes; + int lookup_instance; kstat_ctl_t *kc; kstat_t *ksp; kstat_named_t *knp; @@ -129,10 +158,9 @@ int Platform::GetCPUInfo(Local *cpus) { *cpus = Array::New(); - int lookup_instance = 0; - while (ksp = kstat_lookup(kc, "cpu_info", lookup_instance, NULL)){ + lookup_instance = 0; + while (ksp = kstat_lookup(kc, "cpu_info", lookup_instance, NULL)) { cpuinfo = Object::New(); - cputimes = Object::New(); if (kstat_read(kc, ksp, NULL) == -1) { /* @@ -144,6 +172,7 @@ int Platform::GetCPUInfo(Local *cpus) { * the strerror(). */ cpuinfo->Set(String::New("error"), String::New(strerror(errno))); + (*cpus)->Set(lookup_instance, cpuinfo); } else { knp = (kstat_named_t *) kstat_data_lookup(ksp, "clock_MHz"); cpuinfo->Set(String::New("speed"), data_named(knp)); @@ -162,6 +191,7 @@ int Platform::GetCPUInfo(Local *cpus) { if (kstat_read(kc, ksp, NULL) == -1) { cputimes->Set(String::New("error"), String::New(strerror(errno))); + cpuinfo->Set(String::New("times"), cpuinfo); } else { knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_kernel"); cputimes->Set(String::New("system"), data_named(knp)); @@ -170,7 +200,7 @@ int Platform::GetCPUInfo(Local *cpus) { knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_idle"); cputimes->Set(String::New("idle"), data_named(knp)); knp = (kstat_named_t *) kstat_data_lookup(ksp, "intr"); - cputimes->Set(String::New("intr"), data_named(knp)); + cputimes->Set(String::New("irq"), data_named(knp)); cpuinfo->Set(String::New("times"), cputimes); } @@ -183,34 +213,6 @@ int Platform::GetCPUInfo(Local *cpus) { return 0; } -Handle Platform::data_named(kstat_named_t *knp) { - Handle val; - - switch (knp->data_type) { - case KSTAT_DATA_CHAR: - val = Number::New(knp->value.c[0]); - break; - case KSTAT_DATA_INT32: - val = Number::New(knp->value.i32); - break; - case KSTAT_DATA_UINT32: - val = Number::New(knp->value.ui32); - break; - case KSTAT_DATA_INT64: - val = Number::New(knp->value.i64); - break; - case KSTAT_DATA_UINT64: - val = Number::New(knp->value.ui64); - break; - case KSTAT_DATA_STRING: - val = String::New(KSTAT_NAMED_STR_PTR(knp)); - break; - default: - throw (String::New("unrecognized data type")); - } - - return (val); -} double Platform::GetFreeMemory() { return 0.0; @@ -223,8 +225,29 @@ double Platform::GetTotalMemory() { double Platform::GetUptime() { - // http://munin-monitoring.org/attachment/ticket/419/uptime - return 0.0; + kstat_ctl_t *kc; + kstat_t *ksp; + kstat_named_t *knp; + + long hz = sysconf(_SC_CLK_TCK); + ulong_t clk_intr; + + if ((kc = kstat_open()) == NULL) { + throw "could not open kstat"; + } + + ksp = kstat_lookup(kc, "unix", 0, "system_misc"); + + if (kstat_read(kc, ksp, NULL) == -1) { + throw "unable to read kstat"; + } else { + knp = (kstat_named_t *) kstat_data_lookup(ksp, "clk_intr"); + clk_intr = knp->value.ul; + } + + kstat_close(kc); + + return static_cast(clk_intr / hz); } @@ -233,5 +256,11 @@ int Platform::GetLoadAvg(Local *loads) { } +Handle Platform::GetInterfaceAddresses() { + HandleScope scope; + return scope.Close(Object::New()); +} + + } // namespace node diff --git a/test/simple/test-os.js b/test/simple/test-os.js index 5903f8f6b1..54961fb58e 100644 --- a/test/simple/test-os.js +++ b/test/simple/test-os.js @@ -23,11 +23,29 @@ var common = require('../common'); var assert = require('assert'); var os = require('os'); -assert.ok(os.hostname().length > 0); -assert.ok(os.loadavg().length > 0); -assert.ok(os.uptime() > 0); -assert.ok(os.freemem() > 0); -assert.ok(os.totalmem() > 0); -assert.ok(os.cpus().length > 0); -assert.ok(os.type().length > 0); -assert.ok(os.release().length > 0); \ No newline at end of file +var hostname = os.hostname() +console.log("hostname = %s", hostname); +assert.ok(hostname.length > 0); + +var uptime = os.uptime(); +console.log("uptime = %d", uptime); +assert.ok(uptime > 0); + +var cpus = os.cpus(); +console.log("cpus = ", cpus); +assert.ok(cpus.length > 0); + +var type = os.type(); +console.log("type = ", type); +assert.ok(type.length > 0); + +var release = os.release(); +console.log("release = ", release); +assert.ok(release > 0); + +if (process.platform != 'sunos') { + // not implemeneted yet + assert.ok(os.loadavg().length > 0); + assert.ok(os.freemem() > 0); + assert.ok(os.totalmem() > 0); +}