Skip to content

Commit 752bbd6

Browse files
Scott McWhirterry
authored andcommitted
Add os.cpus() support for sunos
1 parent d2298d2 commit 752bbd6

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

src/platform.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#define NODE_PLATFORM_H_
2424

2525
#include <v8.h>
26+
#ifdef __sun
27+
#include <kstat.h>
28+
#endif
2629

2730
namespace node {
2831

@@ -39,6 +42,13 @@ class Platform {
3942
static double GetTotalMemory();
4043
static double GetUptime();
4144
static int GetLoadAvg(v8::Local<v8::Array> *loads);
45+
static v8::Handle<v8::Value> GetInterfaceAddresses();
46+
private:
47+
static double GetUptimeImpl();
48+
static double prog_start_time;
49+
#ifdef __sun
50+
static v8::Handle<v8::Value> data_named(kstat_named_t *);
51+
#endif
4252
};
4353

4454

src/platform_sunos.cc

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include <stdlib.h> /* getexecname() */
2828
#include <strings.h> /* strncpy() */
2929

30+
#include <kstat.h>
31+
#include <errno.h>
32+
#include <inttypes.h>
33+
#include <sys/types.h>
3034

3135
#if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
3236
#define PROCFS_FILE_OFFSET_BITS_HACK 1
@@ -112,10 +116,102 @@ int Platform::GetExecutablePath(char* buffer, size_t* size) {
112116

113117

114118
int Platform::GetCPUInfo(Local<Array> *cpus) {
115-
// http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/psrinfo/psrinfo.pl
119+
HandleScope scope;
120+
Local<Object> cpuinfo;
121+
Local<Object> cputimes;
122+
123+
kstat_ctl_t *kc;
124+
kstat_t *ksp;
125+
kstat_named_t *knp;
126+
127+
kc = kstat_open();
128+
if ((kc = kstat_open()) == NULL)
129+
throw "could not open kstat";
130+
131+
*cpus = Array::New();
132+
133+
int lookup_instance = 0;
134+
while (ksp = kstat_lookup(kc, "cpu_info", lookup_instance, NULL)){
135+
cpuinfo = Object::New();
136+
cputimes = Object::New();
137+
138+
if (kstat_read(kc, ksp, NULL) == -1) {
139+
/*
140+
* It is deeply annoying, but some kstats can return errors
141+
* under otherwise routine conditions. (ACPI is one
142+
* offender; there are surely others.) To prevent these
143+
* fouled kstats from completely ruining our day, we assign
144+
* an "error" member to the return value that consists of
145+
* the strerror().
146+
*/
147+
cpuinfo->Set(String::New("error"), String::New(strerror(errno)));
148+
} else {
149+
knp = (kstat_named_t *) kstat_data_lookup(ksp, "clock_MHz");
150+
cpuinfo->Set(String::New("speed"), data_named(knp));
151+
knp = (kstat_named_t *) kstat_data_lookup(ksp, "brand");
152+
cpuinfo->Set(String::New("model"), data_named(knp));
153+
(*cpus)->Set(lookup_instance, cpuinfo);
154+
}
155+
156+
lookup_instance++;
157+
}
158+
159+
lookup_instance = 0;
160+
while (ksp = kstat_lookup(kc, "cpu", lookup_instance, "sys")){
161+
cpuinfo = (*cpus)->Get(lookup_instance)->ToObject();
162+
cputimes = Object::New();
163+
164+
if (kstat_read(kc, ksp, NULL) == -1) {
165+
cputimes->Set(String::New("error"), String::New(strerror(errno)));
166+
} else {
167+
knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_kernel");
168+
cputimes->Set(String::New("system"), data_named(knp));
169+
knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_user");
170+
cputimes->Set(String::New("user"), data_named(knp));
171+
knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_idle");
172+
cputimes->Set(String::New("idle"), data_named(knp));
173+
knp = (kstat_named_t *) kstat_data_lookup(ksp, "intr");
174+
cputimes->Set(String::New("intr"), data_named(knp));
175+
176+
cpuinfo->Set(String::New("times"), cputimes);
177+
}
178+
179+
lookup_instance++;
180+
}
181+
182+
kstat_close(kc);
183+
116184
return 0;
117185
}
118186

187+
Handle<Value> Platform::data_named(kstat_named_t *knp) {
188+
Handle<Value> val;
189+
190+
switch (knp->data_type) {
191+
case KSTAT_DATA_CHAR:
192+
val = Number::New(knp->value.c[0]);
193+
break;
194+
case KSTAT_DATA_INT32:
195+
val = Number::New(knp->value.i32);
196+
break;
197+
case KSTAT_DATA_UINT32:
198+
val = Number::New(knp->value.ui32);
199+
break;
200+
case KSTAT_DATA_INT64:
201+
val = Number::New(knp->value.i64);
202+
break;
203+
case KSTAT_DATA_UINT64:
204+
val = Number::New(knp->value.ui64);
205+
break;
206+
case KSTAT_DATA_STRING:
207+
val = String::New(KSTAT_NAMED_STR_PTR(knp));
208+
break;
209+
default:
210+
throw (String::New("unrecognized data type"));
211+
}
212+
213+
return (val);
214+
}
119215

120216
double Platform::GetFreeMemory() {
121217
return 0.0;

wscript

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ def configure(conf):
359359
conf.fatal("Cannot find socket library")
360360
if not conf.check(lib='nsl', uselib_store="NSL"):
361361
conf.fatal("Cannot find nsl library")
362+
if not conf.check(lib='kstat', uselib_store="KSTAT"):
363+
conf.fatal("Cannot find kstat library")
362364

363365
conf.sub_config('deps/libeio')
364366

@@ -802,7 +804,7 @@ def build(bld):
802804
node = bld.new_task_gen("cxx", product_type)
803805
node.name = "node"
804806
node.target = "node"
805-
node.uselib = 'RT EV OPENSSL CARES EXECINFO DL KVM SOCKET NSL UTIL OPROFILE'
807+
node.uselib = 'RT EV OPENSSL CARES EXECINFO DL KVM SOCKET NSL KSTAT UTIL OPROFILE'
806808
node.add_objects = 'eio http_parser'
807809
if product_type_is_lib:
808810
node.install_path = '${LIBDIR}'

0 commit comments

Comments
 (0)