Skip to content

Commit e8cf98c

Browse files
Scott McWhirterry
Scott McWhirter
authored andcommitted
Add os.cpus() and os.uptime() support for sunos
1 parent 296ff04 commit e8cf98c

File tree

3 files changed

+90
-49
lines changed

3 files changed

+90
-49
lines changed

src/platform.h

-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ class Platform {
4343
static double GetUptime();
4444
static int GetLoadAvg(v8::Local<v8::Array> *loads);
4545
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
5246
};
5347

5448

src/platform_sunos.cc

+64-35
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ namespace node {
5050

5151
using namespace v8;
5252

53+
5354
char** Platform::SetupArgs(int argc, char *argv[]) {
5455
return argv;
5556
}
@@ -112,14 +113,42 @@ int Platform::GetExecutablePath(char* buffer, size_t* size) {
112113
}
113114

114115

115-
// TODO: libkstat provides all this info. Need to link it though.
116+
static Handle<Value> data_named(kstat_named_t *knp) {
117+
Handle<Value> val;
118+
119+
switch (knp->data_type) {
120+
case KSTAT_DATA_CHAR:
121+
val = Number::New(knp->value.c[0]);
122+
break;
123+
case KSTAT_DATA_INT32:
124+
val = Number::New(knp->value.i32);
125+
break;
126+
case KSTAT_DATA_UINT32:
127+
val = Number::New(knp->value.ui32);
128+
break;
129+
case KSTAT_DATA_INT64:
130+
val = Number::New(knp->value.i64);
131+
break;
132+
case KSTAT_DATA_UINT64:
133+
val = Number::New(knp->value.ui64);
134+
break;
135+
case KSTAT_DATA_STRING:
136+
val = String::New(KSTAT_NAMED_STR_PTR(knp));
137+
break;
138+
default:
139+
throw (String::New("unrecognized data type"));
140+
}
141+
142+
return (val);
143+
}
116144

117145

118146
int Platform::GetCPUInfo(Local<Array> *cpus) {
119147
HandleScope scope;
120148
Local<Object> cpuinfo;
121149
Local<Object> cputimes;
122150

151+
int lookup_instance;
123152
kstat_ctl_t *kc;
124153
kstat_t *ksp;
125154
kstat_named_t *knp;
@@ -129,10 +158,9 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
129158

130159
*cpus = Array::New();
131160

132-
int lookup_instance = 0;
133-
while (ksp = kstat_lookup(kc, "cpu_info", lookup_instance, NULL)){
161+
lookup_instance = 0;
162+
while (ksp = kstat_lookup(kc, "cpu_info", lookup_instance, NULL)) {
134163
cpuinfo = Object::New();
135-
cputimes = Object::New();
136164

137165
if (kstat_read(kc, ksp, NULL) == -1) {
138166
/*
@@ -144,6 +172,7 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
144172
* the strerror().
145173
*/
146174
cpuinfo->Set(String::New("error"), String::New(strerror(errno)));
175+
(*cpus)->Set(lookup_instance, cpuinfo);
147176
} else {
148177
knp = (kstat_named_t *) kstat_data_lookup(ksp, "clock_MHz");
149178
cpuinfo->Set(String::New("speed"), data_named(knp));
@@ -162,6 +191,7 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
162191

163192
if (kstat_read(kc, ksp, NULL) == -1) {
164193
cputimes->Set(String::New("error"), String::New(strerror(errno)));
194+
cpuinfo->Set(String::New("times"), cpuinfo);
165195
} else {
166196
knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_kernel");
167197
cputimes->Set(String::New("system"), data_named(knp));
@@ -170,7 +200,7 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
170200
knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_idle");
171201
cputimes->Set(String::New("idle"), data_named(knp));
172202
knp = (kstat_named_t *) kstat_data_lookup(ksp, "intr");
173-
cputimes->Set(String::New("intr"), data_named(knp));
203+
cputimes->Set(String::New("irq"), data_named(knp));
174204

175205
cpuinfo->Set(String::New("times"), cputimes);
176206
}
@@ -183,34 +213,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
183213
return 0;
184214
}
185215

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

215217
double Platform::GetFreeMemory() {
216218
return 0.0;
@@ -223,8 +225,29 @@ double Platform::GetTotalMemory() {
223225

224226

225227
double Platform::GetUptime() {
226-
// http://munin-monitoring.org/attachment/ticket/419/uptime
227-
return 0.0;
228+
kstat_ctl_t *kc;
229+
kstat_t *ksp;
230+
kstat_named_t *knp;
231+
232+
long hz = sysconf(_SC_CLK_TCK);
233+
ulong_t clk_intr;
234+
235+
if ((kc = kstat_open()) == NULL) {
236+
throw "could not open kstat";
237+
}
238+
239+
ksp = kstat_lookup(kc, "unix", 0, "system_misc");
240+
241+
if (kstat_read(kc, ksp, NULL) == -1) {
242+
throw "unable to read kstat";
243+
} else {
244+
knp = (kstat_named_t *) kstat_data_lookup(ksp, "clk_intr");
245+
clk_intr = knp->value.ul;
246+
}
247+
248+
kstat_close(kc);
249+
250+
return static_cast<double>(clk_intr / hz);
228251
}
229252

230253

@@ -233,5 +256,11 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
233256
}
234257

235258

259+
Handle<Value> Platform::GetInterfaceAddresses() {
260+
HandleScope scope;
261+
return scope.Close(Object::New());
262+
}
263+
264+
236265
} // namespace node
237266

test/simple/test-os.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,29 @@ var common = require('../common');
2323
var assert = require('assert');
2424
var os = require('os');
2525

26-
assert.ok(os.hostname().length > 0);
27-
assert.ok(os.loadavg().length > 0);
28-
assert.ok(os.uptime() > 0);
29-
assert.ok(os.freemem() > 0);
30-
assert.ok(os.totalmem() > 0);
31-
assert.ok(os.cpus().length > 0);
32-
assert.ok(os.type().length > 0);
33-
assert.ok(os.release().length > 0);
26+
var hostname = os.hostname()
27+
console.log("hostname = %s", hostname);
28+
assert.ok(hostname.length > 0);
29+
30+
var uptime = os.uptime();
31+
console.log("uptime = %d", uptime);
32+
assert.ok(uptime > 0);
33+
34+
var cpus = os.cpus();
35+
console.log("cpus = ", cpus);
36+
assert.ok(cpus.length > 0);
37+
38+
var type = os.type();
39+
console.log("type = ", type);
40+
assert.ok(type.length > 0);
41+
42+
var release = os.release();
43+
console.log("release = ", release);
44+
assert.ok(release > 0);
45+
46+
if (process.platform != 'sunos') {
47+
// not implemeneted yet
48+
assert.ok(os.loadavg().length > 0);
49+
assert.ok(os.freemem() > 0);
50+
assert.ok(os.totalmem() > 0);
51+
}

0 commit comments

Comments
 (0)