From 752bbd6b426d4be9fc8a352c7bec0f89839828d7 Mon Sep 17 00:00:00 2001 From: Scott McWhirter Date: Sun, 27 Mar 2011 03:14:34 -0700 Subject: [PATCH] Add os.cpus() support for sunos --- src/platform.h | 10 +++++ src/platform_sunos.cc | 98 ++++++++++++++++++++++++++++++++++++++++++- wscript | 4 +- 3 files changed, 110 insertions(+), 2 deletions(-) diff --git a/src/platform.h b/src/platform.h index db2c32cad..3227a3d66 100644 --- a/src/platform.h +++ b/src/platform.h @@ -23,6 +23,9 @@ #define NODE_PLATFORM_H_ #include +#ifdef __sun +#include +#endif namespace node { @@ -39,6 +42,13 @@ class Platform { static double GetTotalMemory(); 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 dc9617e04..d2020662d 100644 --- a/src/platform_sunos.cc +++ b/src/platform_sunos.cc @@ -27,6 +27,10 @@ #include /* getexecname() */ #include /* strncpy() */ +#include +#include +#include +#include #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64) #define PROCFS_FILE_OFFSET_BITS_HACK 1 @@ -112,10 +116,102 @@ int Platform::GetExecutablePath(char* buffer, size_t* size) { int Platform::GetCPUInfo(Local *cpus) { - // http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/psrinfo/psrinfo.pl + HandleScope scope; + Local cpuinfo; + Local cputimes; + + kstat_ctl_t *kc; + kstat_t *ksp; + kstat_named_t *knp; + + kc = kstat_open(); + if ((kc = kstat_open()) == NULL) + throw "could not open kstat"; + + *cpus = Array::New(); + + int 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) { + /* + * It is deeply annoying, but some kstats can return errors + * under otherwise routine conditions. (ACPI is one + * offender; there are surely others.) To prevent these + * fouled kstats from completely ruining our day, we assign + * an "error" member to the return value that consists of + * the strerror(). + */ + cpuinfo->Set(String::New("error"), String::New(strerror(errno))); + } else { + knp = (kstat_named_t *) kstat_data_lookup(ksp, "clock_MHz"); + cpuinfo->Set(String::New("speed"), data_named(knp)); + knp = (kstat_named_t *) kstat_data_lookup(ksp, "brand"); + cpuinfo->Set(String::New("model"), data_named(knp)); + (*cpus)->Set(lookup_instance, cpuinfo); + } + + lookup_instance++; + } + + lookup_instance = 0; + while (ksp = kstat_lookup(kc, "cpu", lookup_instance, "sys")){ + cpuinfo = (*cpus)->Get(lookup_instance)->ToObject(); + cputimes = Object::New(); + + if (kstat_read(kc, ksp, NULL) == -1) { + cputimes->Set(String::New("error"), String::New(strerror(errno))); + } else { + knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_kernel"); + cputimes->Set(String::New("system"), data_named(knp)); + knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_user"); + cputimes->Set(String::New("user"), data_named(knp)); + 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)); + + cpuinfo->Set(String::New("times"), cputimes); + } + + lookup_instance++; + } + + kstat_close(kc); + 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; diff --git a/wscript b/wscript index 7692e83f4..756b530cf 100644 --- a/wscript +++ b/wscript @@ -359,6 +359,8 @@ def configure(conf): conf.fatal("Cannot find socket library") if not conf.check(lib='nsl', uselib_store="NSL"): conf.fatal("Cannot find nsl library") + if not conf.check(lib='kstat', uselib_store="KSTAT"): + conf.fatal("Cannot find kstat library") conf.sub_config('deps/libeio') @@ -802,7 +804,7 @@ def build(bld): node = bld.new_task_gen("cxx", product_type) node.name = "node" node.target = "node" - node.uselib = 'RT EV OPENSSL CARES EXECINFO DL KVM SOCKET NSL UTIL OPROFILE' + node.uselib = 'RT EV OPENSSL CARES EXECINFO DL KVM SOCKET NSL KSTAT UTIL OPROFILE' node.add_objects = 'eio http_parser' if product_type_is_lib: node.install_path = '${LIBDIR}' -- 2.34.1