Browse Source

Add os.cpus() support for sunos

v0.7.4-release
Scott McWhirter 14 years ago
committed by Ryan Dahl
parent
commit
752bbd6b42
  1. 10
      src/platform.h
  2. 98
      src/platform_sunos.cc
  3. 4
      wscript

10
src/platform.h

@ -23,6 +23,9 @@
#define NODE_PLATFORM_H_
#include <v8.h>
#ifdef __sun
#include <kstat.h>
#endif
namespace node {
@ -39,6 +42,13 @@ class Platform {
static double GetTotalMemory();
static double GetUptime();
static int GetLoadAvg(v8::Local<v8::Array> *loads);
static v8::Handle<v8::Value> GetInterfaceAddresses();
private:
static double GetUptimeImpl();
static double prog_start_time;
#ifdef __sun
static v8::Handle<v8::Value> data_named(kstat_named_t *);
#endif
};

98
src/platform_sunos.cc

@ -27,6 +27,10 @@
#include <stdlib.h> /* getexecname() */
#include <strings.h> /* strncpy() */
#include <kstat.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>
#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<Array> *cpus) {
// http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/psrinfo/psrinfo.pl
HandleScope scope;
Local<Object> cpuinfo;
Local<Object> 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<Value> Platform::data_named(kstat_named_t *knp) {
Handle<Value> 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;

4
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}'

Loading…
Cancel
Save