forked from TritonDataCenter/node-kstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kstat.cc
522 lines (418 loc) · 12.5 KB
/
kstat.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <v8.h>
#include <node.h>
#include <string.h>
#include <unistd.h>
#include <node_object_wrap.h>
#include <kstat.h>
#include <errno.h>
#include <string>
#include <vector>
#include <sys/varargs.h>
using namespace v8;
using std::string;
using std::vector;
class KStatReader : node::ObjectWrap {
public:
static void Initialize(Handle<Object> target);
protected:
static Persistent<FunctionTemplate> templ;
typedef struct {
int data_type;
uint32_t value_ui32;
uint64_t value_ui64;
string value_str;
} write_value_t;
KStatReader(string *module, string *classname,
string *name, int instance);
Handle<Value> error(const char *fmt, ...);
Handle<Value> read(kstat_t *);
kstat_named_t* write(char *, write_value_t *);
int update();
~KStatReader();
static Handle<Value> New(const Arguments& args);
static Handle<Value> Read(const Arguments& args);
static Handle<Value> Write(const Arguments& args);
static Handle<Value> Update(const Arguments& args);
static void EIO_Write(uv_work_t *req);
static void EIO_AfterWrite(uv_work_t *req);
static Handle<Value> WriteAsync(const Arguments& args);
typedef struct {
KStatReader *k;
Persistent<Function> cb;
string name;
write_value_t val;
string error;
} write_baton_t;
private:
static string *stringMember(Local<Value>, char *, char *);
static int64_t intMember(Local<Value>, char *, int64_t);
Handle<Object> data_named(kstat_t *);
Handle<Object> data_io(kstat_t *);
string *ksr_module;
string *ksr_class;
string *ksr_name;
int ksr_instance;
kid_t ksr_kid;
kstat_ctl_t *ksr_ctl;
vector<kstat_t *> ksr_kstats;
};
Persistent<FunctionTemplate> KStatReader::templ;
KStatReader::KStatReader(string *module, string *classname,
string *name, int instance)
: node::ObjectWrap(), ksr_module(module), ksr_class(classname),
ksr_name(name), ksr_instance(instance), ksr_kid(-1)
{
if ((ksr_ctl = kstat_open()) == NULL)
throw "could not open kstat";
};
KStatReader::~KStatReader()
{
delete ksr_module;
delete ksr_class;
delete ksr_name;
kstat_close(ksr_ctl);
}
int
KStatReader::update()
{
kstat_t *ksp;
kid_t kid;
if ((kid = kstat_chain_update(ksr_ctl)) == 0 && ksr_kid != -1)
return (0);
if (kid == -1)
return (-1);
ksr_kid = kid;
ksr_kstats.clear();
for (ksp = ksr_ctl->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
if (!ksr_module->empty() &&
ksr_module->compare(ksp->ks_module) != 0)
continue;
if (!ksr_class->empty() &&
ksr_class->compare(ksp->ks_class) != 0)
continue;
if (!ksr_name->empty() && ksr_name->compare(ksp->ks_name) != 0)
continue;
if (ksr_instance != -1 && ksp->ks_instance != ksr_instance)
continue;
ksr_kstats.push_back(ksp);
}
return (0);
}
void
KStatReader::Initialize(Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> k = FunctionTemplate::New(KStatReader::New);
templ = Persistent<FunctionTemplate>::New(k);
templ->InstanceTemplate()->SetInternalFieldCount(1);
templ->SetClassName(String::NewSymbol("Reader"));
NODE_SET_PROTOTYPE_METHOD(templ, "read", KStatReader::Read);
NODE_SET_PROTOTYPE_METHOD(templ, "write", KStatReader::Write);
NODE_SET_PROTOTYPE_METHOD(templ, "writeAsync", KStatReader::WriteAsync);
target->Set(String::NewSymbol("Reader"), templ->GetFunction());
}
string *
KStatReader::stringMember(Local<Value> value, char *member, char *deflt)
{
if (!value->IsObject())
return (new string (deflt));
Local<Object> o = Local<Object>::Cast(value);
Local<Value> v = o->Get(String::New(member));
if (!v->IsString())
return (new string (deflt));
String::AsciiValue val(v);
return (new string(*val));
}
int64_t
KStatReader::intMember(Local<Value> value, char *member, int64_t deflt)
{
int64_t rval = deflt;
if (!value->IsObject())
return (rval);
Local<Object> o = Local<Object>::Cast(value);
value = o->Get(String::New(member));
if (!value->IsNumber())
return (rval);
Local<Integer> i = Local<Integer>::Cast(value);
return (i->Value());
}
Handle<Value>
KStatReader::New(const Arguments& args)
{
HandleScope scope;
KStatReader *k = new KStatReader(stringMember(args[0], "module", ""),
stringMember(args[0], "class", ""),
stringMember(args[0], "name", ""),
intMember(args[0], "instance", -1));
k->Wrap(args.Holder());
return (args.This());
}
Handle<Value>
KStatReader::error(const char *fmt, ...)
{
char buf[1024], buf2[1024];
char *err = buf;
va_list ap;
va_start(ap, fmt);
(void) vsnprintf(buf, sizeof (buf), fmt, ap);
if (buf[strlen(buf) - 1] != '\n') {
/*
* If our error doesn't end in a new-line, we'll append the
* strerror of errno.
*/
(void) snprintf(err = buf2, sizeof (buf2),
"%s: %s", buf, strerror(errno));
} else {
buf[strlen(buf) - 1] = '\0';
}
return (ThrowException(Exception::Error(String::New(err))));
}
Handle<Object>
KStatReader::data_named(kstat_t *ksp)
{
Handle<Object> data = Object::New();
kstat_named_t *nm = KSTAT_NAMED_PTR(ksp);
int i;
assert(ksp->ks_type == KSTAT_TYPE_NAMED);
for (i = 0; i < ksp->ks_ndata; i++, nm++) {
Handle<Value> val;
switch (nm->data_type) {
case KSTAT_DATA_CHAR:
/* must protect string ending */
nm->value.c[15] = '\0';
val = String::New(nm->value.c);
break;
case KSTAT_DATA_INT32:
val = Number::New(nm->value.i32);
break;
case KSTAT_DATA_UINT32:
val = Number::New(nm->value.ui32);
break;
case KSTAT_DATA_INT64:
val = Number::New(nm->value.i64);
break;
case KSTAT_DATA_UINT64:
val = Number::New(nm->value.ui64);
break;
case KSTAT_DATA_STRING:
/* actually STR_PTR can be NULL and this is normal */
val = String::New(KSTAT_NAMED_STR_PTR(nm) ?
KSTAT_NAMED_STR_PTR(nm) : "");
break;
default:
throw (error("unrecognized data type %d for member "
"\"%s\" in instance %d of stat \"%s\" (module "
"\"%s\", class \"%s\")\n", nm->data_type,
nm->name, ksp->ks_instance, ksp->ks_name,
ksp->ks_module, ksp->ks_class));
}
data->Set(String::New(nm->name), val);
}
return (data);
}
Handle<Object>
KStatReader::data_io(kstat_t *ksp)
{
Handle<Object> data = Object::New();
kstat_io_t *io = KSTAT_IO_PTR(ksp);
assert(ksp->ks_type == KSTAT_TYPE_IO);
data->Set(String::New("nread"), Number::New(io->nread));
data->Set(String::New("nwritten"), Number::New(io->nwritten));
data->Set(String::New("reads"), Integer::New(io->reads));
data->Set(String::New("writes"), Integer::New(io->writes));
data->Set(String::New("wtime"), Number::New(io->wtime));
data->Set(String::New("wlentime"), Number::New(io->wlentime));
data->Set(String::New("wlastupdate"), Number::New(io->wlastupdate));
data->Set(String::New("rtime"), Number::New(io->rtime));
data->Set(String::New("rlentime"), Number::New(io->rlentime));
data->Set(String::New("rlastupdate"), Number::New(io->rlastupdate));
data->Set(String::New("wcnt"), Integer::New(io->wcnt));
data->Set(String::New("rcnt"), Integer::New(io->rcnt));
return (data);
}
Handle<Value>
KStatReader::read(kstat_t *ksp)
{
Handle<Object> rval = Object::New();
Handle<Object> data;
rval->Set(String::New("class"), String::New(ksp->ks_class));
rval->Set(String::New("module"), String::New(ksp->ks_module));
rval->Set(String::New("name"), String::New(ksp->ks_name));
rval->Set(String::New("instance"), Integer::New(ksp->ks_instance));
if (kstat_read(ksr_ctl, 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().
*/
rval->Set(String::New("error"), String::New(strerror(errno)));
return (rval);
}
rval->Set(String::New("instance"), Integer::New(ksp->ks_instance));
rval->Set(String::New("snaptime"), Number::New(ksp->ks_snaptime));
rval->Set(String::New("crtime"), Number::New(ksp->ks_crtime));
if (ksp->ks_type == KSTAT_TYPE_NAMED) {
data = data_named(ksp);
} else if (ksp->ks_type == KSTAT_TYPE_IO) {
data = data_io(ksp);
} else {
return (rval);
}
rval->Set(String::New("data"), data);
return (rval);
}
Handle<Value>
KStatReader::Read(const Arguments& args)
{
KStatReader *k = ObjectWrap::Unwrap<KStatReader>(args.Holder());
Handle<Object> rval = Object::New();
HandleScope scope;
int i;
if (k->update() == -1)
return (k->error("failed to update kstat chain"));
rval = Array::New(k->ksr_kstats.size());
try {
for (i = 0; i < k->ksr_kstats.size(); i++)
rval->Set(i, k->read(k->ksr_kstats[i]));
} catch (Handle<Value> err) {
return (err);
}
return (rval);
}
kstat_named_t*
KStatReader::write(char *name, write_value_t *val)
{
kstat_t *ksp;
kstat_named_t *knp;
ksp = kstat_lookup(ksr_ctl, (char*)ksr_module->c_str(), ksr_instance,
(char*)ksr_name->c_str());
if (!ksp)
return NULL;
if (kstat_read(ksr_ctl, ksp, NULL) == -1)
return NULL;
knp = (kstat_named_t *) kstat_data_lookup(ksp, name);
if (!knp)
return NULL;
if (val->data_type == KSTAT_DATA_UINT32) {
knp->value.ui32 = val->value_ui32;
} else if (val->data_type == KSTAT_DATA_UINT64) {
knp->value.ui64 = val->value_ui64;
} else if (val->data_type == KSTAT_DATA_STRING) {
KSTAT_NAMED_STR_PTR(knp) = (char*)KSTAT_NAMED_PTR(ksp) +
ksp->ks_ndata * sizeof(kstat_named_t);
KSTAT_NAMED_STR_BUFLEN(knp) = val->value_str.length() + 1;
strcpy(KSTAT_NAMED_STR_PTR(knp),
(char *)val->value_str.c_str());
}
if (kstat_write(ksr_ctl, ksp, NULL) == -1) {
return NULL;
}
return knp;
}
Handle<Value>
KStatReader::Write(const Arguments& args)
{
KStatReader *k = ObjectWrap::Unwrap<KStatReader>(args.Holder());
Handle<Object> rval = Object::New();
HandleScope scope;
kstat_named_t *knp;
if (args.Length() < 2 || !args[0]->IsString())
return (k->error("first argument must be string"));
String::Utf8Value name(args[0]->ToString());
if (args.Length() < 2)
return (k->error("second argument is not specified"));
write_value_t val;
Local<Value> value = args[1];
if (value->IsUint32()) {
val.data_type = KSTAT_DATA_UINT32;
val.value_ui32 = value->Uint32Value();
} else if (value->IsNumber()) {
val.data_type = KSTAT_DATA_UINT64;
val.value_ui64 = value->NumberValue();
} else if (value->IsString()) {
String::Utf8Value str(value->ToString());
val.data_type = KSTAT_DATA_STRING;
val.value_str = *str;
}
knp = k->write(*name, &val);
if (knp == NULL)
return (k->error("can't write kstat"));
return (rval);
}
void
KStatReader::EIO_Write(uv_work_t *req)
{
write_baton_t *baton = static_cast<write_baton_t *>(req->data);
kstat_named_t *knp;
knp = baton->k->write((char*)baton->name.c_str(), &baton->val);
if (knp == NULL)
baton->error = strerror(errno);
}
void
KStatReader::EIO_AfterWrite(uv_work_t *req)
{
HandleScope scope;
write_baton_t *baton = static_cast<write_baton_t *>(req->data);
uv_unref((uv_handle_t *) req);
baton->k->Unref();
Local<Value> argv[1];
if (!baton->error.empty()) {
Local<Value> err =
Exception::Error(String::New(baton->error.c_str()));
argv[0] = err;
} else {
argv[0] = Local<Value>::New(Null());
}
TryCatch try_catch;
baton->cb->Call(Context::GetCurrent()->Global(), 1, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
baton->cb.Dispose();
delete baton;
delete req;
}
Handle<Value>
KStatReader::WriteAsync(const Arguments& args)
{
KStatReader *k = ObjectWrap::Unwrap<KStatReader>(args.Holder());
HandleScope scope;
if (args.Length() < 3)
return (k->error("expecting 3 arguments"));
if (!args[0]->IsString())
return (k->error("first argument must be string"));
String::Utf8Value name(args[0]->ToString());
Local<Value> value = args[1];
if (!args[2]->IsFunction())
return (k->error("third argument is not function"));
Local<Function> cb = Local<Function>::Cast(args[2]);
write_baton_t *baton = new write_baton_t();
baton->k = k;
baton->cb = Persistent<Function>::New(cb);
baton->name = *name;
if (value->IsUint32()) {
baton->val.data_type = KSTAT_DATA_UINT32;
baton->val.value_ui32 = value->Uint32Value();
} else if (value->IsNumber()) {
baton->val.data_type = KSTAT_DATA_UINT64;
baton->val.value_ui64 = value->NumberValue();
} else if (value->IsString()) {
String::Utf8Value str(value->ToString());
baton->val.data_type = KSTAT_DATA_STRING;
baton->val.value_str = *str;
}
k->Ref();
uv_work_t *req = new uv_work_t;
req->data = baton;
uv_queue_work(uv_default_loop(), req, EIO_Write, EIO_AfterWrite);
uv_ref((uv_handle_t*) &req);
return Undefined();
}
extern "C" void
init (Handle<Object> target)
{
KStatReader::Initialize(target);
}