-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathkstat.c
558 lines (448 loc) · 11.7 KB
/
kstat.c
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* Copyright 2016 Joyent, Inc.
* Copyright 2024 MNX Cloud, Inc.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <thread.h>
#include <errno.h>
#include <kstat.h>
#include "addon.h"
static char kstatjs_error_buf[1024];
static char kstatjs_panic_buf[1024];
static void
kstatjs_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(kstatjs_error_buf, sizeof (kstatjs_error_buf),
fmt, ap);
va_end(ap);
}
const char *
kstatjs_errmsg(void)
{
return ((const char *)kstatjs_error_buf);
}
static void __NORETURN
kstatjs_vpanic(const char *fmt, va_list ap)
{
(void) fprintf(stderr, "node-kstat: ");
(void) vsnprintf(kstatjs_panic_buf, sizeof (kstatjs_panic_buf),
fmt, ap);
(void) fprintf(stderr, kstatjs_panic_buf);
(void) fflush(stderr);
abort();
}
void
kstatjs_panic(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
kstatjs_vpanic(fmt, ap);
va_end(ap);
}
#define KSTATJS_ASSERT(cond) \
if (!(cond)) { \
kstatjs_panic("kstat assertion failed in %s at line %d: %s\n", \
__FILE__, __LINE__, #cond); \
}
static void
kstatjs_finalize(kstatjs_t *ksj)
{
kstatjs_stat_t *ksjs, *next;
free(ksj->ksj_module);
ksj->ksj_module = NULL;
free(ksj->ksj_class);
ksj->ksj_class = NULL;
free(ksj->ksj_name);
ksj->ksj_name = NULL;
ksj->ksj_instance = -1;
if (ksj->ksj_ctl != NULL) {
kstat_close(ksj->ksj_ctl);
ksj->ksj_ctl = NULL;
}
for (ksjs = ksj->ksj_stats; ksjs != NULL; ksjs = next) {
next = ksjs->ksjs_next;
free(ksjs);
}
ksj->ksj_stats = NULL;
}
static int
kstatjs_lookup_nvpair(const nvlist_t *arg, const char *str, nvpair_t **nvpp)
{
int err;
if ((err = nvlist_lookup_nvpair((nvlist_t *)arg, str, nvpp)) != 0) {
/*
* Disappointingly, pre-April-2024 revisions of illumos have
* nvlist_lookup_nvpair() conflates the EINVAL and ENOENT
* case -- so we must check for EINVAL in addition to ENOENT
* and assume that it was actually ENOENT that was intended.
*/
if (err == EINVAL || err == ENOENT) {
*nvpp = NULL;
return (0);
}
kstatjs_err("nvlist manipulation error on member %s: %s",
str, strerror(err));
return (-1);
}
return (0);
}
static int
kstatjs_parsearg_string(const nvlist_t *arg, const char *str, char **p)
{
nvpair_t *nvp;
char *val;
if (kstatjs_lookup_nvpair(arg, str, &nvp) != 0)
return (-1);
if (nvp == NULL)
return (0);
/* The C++ front-end already validates incoming arguments. */
KSTATJS_ASSERT(nvpair_type(nvp) == DATA_TYPE_STRING);
(void) nvpair_value_string(nvp, &val);
if ((*p = strdup(val)) == NULL)
kstatjs_panic("kstats_parsearg_string(): %s", strerror(errno));
return (0);
}
static int
kstatjs_parsearg_int(const nvlist_t *arg, const char *str, int *p)
{
nvpair_t *nvp;
double val;
if (kstatjs_lookup_nvpair(arg, str, &nvp) != 0)
return (-1);
if (nvp == NULL)
return (0);
/* The C++ front-end already validates incoming arguments. */
KSTATJS_ASSERT(nvpair_type(nvp) == DATA_TYPE_DOUBLE);
(void) nvpair_value_double(nvp, &val);
*p = (int)val;
return (0);
}
/*
* This function consumes its "args" nvlist.
*/
static int
kstatjs_parsearg(nvlist_t *args, char **modulep, char **classp, char **namep,
int *instancep)
{
int rc = 0;
*modulep = *classp = *namep = NULL;
*instancep = -1;
if (kstatjs_parsearg_string(args, "module", modulep) != 0 ||
kstatjs_parsearg_string(args, "class", classp) != 0 ||
kstatjs_parsearg_string(args, "name", namep) != 0 ||
kstatjs_parsearg_int(args, "instance", instancep) != 0) {
rc = -1;
}
/* No caller uses "args" after it's done here. */
nvlist_free(args);
return (rc);
}
kstatjs_t *
kstatjs_init(nvlist_t *args)
{
kstatjs_t *ksj;
if ((ksj = malloc(sizeof (kstatjs_t))) == NULL)
kstatjs_panic("malloc(): %s", strerror(errno));
bzero(ksj, sizeof (kstatjs_t));
ksj->ksj_kid = -1;
if (kstatjs_parsearg(args, &ksj->ksj_module, &ksj->ksj_class,
&ksj->ksj_name, &ksj->ksj_instance) != 0) {
kstatjs_finalize(ksj);
free(ksj);
return (NULL);
}
if ((ksj->ksj_ctl = kstat_open()) == NULL) {
kstatjs_err("kstat_open(): %s", strerror(errno));
kstatjs_finalize(ksj);
free(ksj);
return (NULL);
}
return (ksj);
}
#define KSTATJS_READSTAT_IO_ADD(width, field) \
if ((err = nvlist_add_uint##width(nvl, #field, io->field)) != 0) { \
kstatjs_err("nvlist manipulation error on member %s: %s", \
#field, strerror(err)); \
nvlist_free(nvl); \
return (NULL); \
}
static nvlist_t *
kstatjs_readstat_io(kstat_t *ksp)
{
nvlist_t *nvl;
int err;
kstat_io_t *io = KSTAT_IO_PTR(ksp);
KSTATJS_ASSERT(ksp->ks_type == KSTAT_TYPE_IO);
if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0)) != 0) {
kstatjs_err("nvlist_alloc(): %s", strerror(err));
return (NULL);
}
KSTATJS_READSTAT_IO_ADD(64, nread);
KSTATJS_READSTAT_IO_ADD(64, nwritten);
KSTATJS_READSTAT_IO_ADD(32, reads);
KSTATJS_READSTAT_IO_ADD(32, writes);
KSTATJS_READSTAT_IO_ADD(64, wtime);
KSTATJS_READSTAT_IO_ADD(64, wlentime);
KSTATJS_READSTAT_IO_ADD(64, wlastupdate);
KSTATJS_READSTAT_IO_ADD(64, rtime);
KSTATJS_READSTAT_IO_ADD(64, rlentime);
KSTATJS_READSTAT_IO_ADD(64, rlastupdate);
KSTATJS_READSTAT_IO_ADD(64, wcnt);
KSTATJS_READSTAT_IO_ADD(64, rcnt);
return (nvl);
}
#undef KSTATJS_READSTAT_IO_ADD
static nvlist_t *
kstatjs_readstat_named(kstat_t *ksp)
{
nvlist_t *nvl;
kstat_named_t *nm = KSTAT_NAMED_PTR(ksp);
int err;
unsigned i;
if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0)) != 0) {
kstatjs_err("nvlist_alloc(): %s", strerror(err));
return (NULL);
}
for (i = 0; i < ksp->ks_ndata; i++, nm++) {
switch (nm->data_type) {
case KSTAT_DATA_CHAR:
err = nvlist_add_uint8(nvl, nm->name, nm->value.c[0]);
break;
case KSTAT_DATA_INT32:
err = nvlist_add_int32(nvl, nm->name, nm->value.i32);
break;
case KSTAT_DATA_UINT32:
err = nvlist_add_uint32(nvl, nm->name, nm->value.ui32);
break;
case KSTAT_DATA_INT64:
err = nvlist_add_int64(nvl, nm->name, nm->value.i64);
break;
case KSTAT_DATA_UINT64:
err = nvlist_add_uint64(nvl, nm->name, nm->value.ui64);
break;
case KSTAT_DATA_STRING:
err = nvlist_add_string(nvl, nm->name,
KSTAT_NAMED_STR_PTR(nm));
break;
default:
kstatjs_err("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);
nvlist_free(nvl);
return (NULL);
}
if (err != 0) {
kstatjs_err(
"nvlist manipulation error on member %s: %s",
nm->name, strerror(err));
nvlist_free(nvl);
return (NULL);
}
}
return (nvl);
}
static nvlist_t *
kstatjs_readstat(kstatjs_t *ksj, kstatjs_stat_t *ksjs)
{
kstat_t *ksp = ksjs->ksjs_ksp;
nvlist_t *nvl, *data;
int err;
if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0)) != 0) {
kstatjs_err("nvlist_alloc(): %s", strerror(err));
return (NULL);
}
if ((err = nvlist_add_string(nvl, "class", ksp->ks_class)) != 0 ||
(err = nvlist_add_string(nvl, "module", ksp->ks_module)) != 0 ||
(err = nvlist_add_string(nvl, "name", ksp->ks_name)) != 0 ||
(err = nvlist_add_int32(nvl, "instance", ksp->ks_instance))) {
goto err;
}
if (kstat_read(ksj->ksj_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().
*/
if ((err = nvlist_add_string(nvl,
"error", strerror(errno))) != 0) {
goto err;
}
return (nvl);
}
if ((err = nvlist_add_int32(nvl, "instance", ksp->ks_instance)) != 0 ||
(err = nvlist_add_uint64(nvl, "snaptime", ksp->ks_snaptime)) != 0 ||
(err = nvlist_add_uint64(nvl, "crtime", ksp->ks_crtime)) != 0) {
goto err;
}
if (ksp->ks_type == KSTAT_TYPE_NAMED) {
data = kstatjs_readstat_named(ksp);
} else if (ksp->ks_type == KSTAT_TYPE_IO) {
data = kstatjs_readstat_io(ksp);
} else {
return (nvl);
}
if (data == NULL) {
nvlist_free(nvl);
return (NULL);
}
if ((err = nvlist_add_nvlist(nvl, "data", data)) != 0) {
nvlist_free(data);
goto err;
}
nvlist_free(data);
return (nvl);
err:
kstatjs_err("nvlist manipulation error: %s", strerror(err));
nvlist_free(nvl);
return (NULL);
}
static boolean_t
kstatjs_match(kstat_t *ksp, char *module, char *class,
char *name, int instance)
{
if (module != NULL && strcmp(ksp->ks_module, module) != 0)
return (B_FALSE);
if (class != NULL && strcmp(ksp->ks_class, class) != 0)
return (B_FALSE);
if (name != NULL && strcmp(ksp->ks_name, name) != 0)
return (B_FALSE);
if (instance != -1 && ksp->ks_instance != instance)
return (B_FALSE);
return (B_TRUE);
}
static int
kstatjs_update(kstatjs_t *ksj)
{
kstat_ctl_t *ctl = ksj->ksj_ctl;
kstatjs_stat_t *freelist, *ksjs, *tail = NULL;
kstat_t *ksp;
kid_t kid;
if ((kid = kstat_chain_update(ctl)) == 0 && ksj->ksj_kid != -1)
return (0);
if (kid == -1) {
kstatjs_err("kstat_chain_update(): %s", strerror(errno));
return (-1);
}
ksj->ksj_kid = kid;
/*
* After a chain update, we need to update all of our kstat headers.
*/
freelist = ksj->ksj_stats;
ksj->ksj_stats = NULL;
for (ksp = ctl->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
if (!kstatjs_match(ksp, ksj->ksj_module,
ksj->ksj_class, ksj->ksj_name, ksj->ksj_instance)) {
continue;
}
if (freelist == NULL) {
if ((ksjs = malloc(sizeof (kstatjs_stat_t))) == NULL) {
/*
* To increase the odds of actually surviving
* memory allocation failure enough to be able
* throw an exception, we'll free all of our
* kstats and set our kid to -1 to force any
* subsequent read to re-process the kstat
* header chain.
*/
ksjs = ksj->ksj_stats;
while ((ksjs = ksj->ksj_stats) != NULL) {
ksj->ksj_stats = ksjs->ksjs_next;
free(ksjs);
}
kstatjs_err("malloc(): %s", strerror(errno));
return (-1);
}
} else {
ksjs = freelist;
freelist = freelist->ksjs_next;
}
ksjs->ksjs_ksp = ksp;
ksjs->ksjs_next = NULL;
if (tail != NULL) {
tail->ksjs_next = ksjs;
} else {
ksj->ksj_stats = ksjs;
}
tail = ksjs;
}
/*
* If there's anything left on the freelist, we need to free it.
*/
while ((ksjs = freelist) != NULL) {
freelist = ksjs->ksjs_next;
free(ksjs);
}
return (0);
}
nvlist_t *
kstatjs_read(kstatjs_t *ksj, nvlist_t *args)
{
kstatjs_stat_t *ksjs;
nvlist_t *nvl, *data;
int nstats = 0, err;
char *module = NULL, *class = NULL, *name = NULL;
int instance = -1;
if (kstatjs_parsearg(args, &module, &class, &name, &instance) != 0)
return (NULL);
if (ksj->ksj_ctl == NULL) {
kstatjs_err("kstat reader has already been closed");
return (NULL);
}
if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0)) != 0) {
kstatjs_err("nvlist_alloc(): %s", strerror(err));
goto out;
}
if (kstatjs_update(ksj) != 0) {
nvlist_free(nvl);
goto out;
}
for (ksjs = ksj->ksj_stats; ksjs != NULL; ksjs = ksjs->ksjs_next) {
char buf[12];
if (!kstatjs_match(ksjs->ksjs_ksp, module,
class, name, instance)) {
continue;
}
if ((data = kstatjs_readstat(ksj, ksjs)) == NULL) {
nvlist_free(nvl);
goto out;
}
(void) snprintf(buf, sizeof (buf), "%d", nstats++);
KSTATJS_ASSERT(nstats > 0);
if (nvlist_add_nvlist(nvl, buf, data) != 0) {
kstatjs_err("nvlist_add_nvlist(): %s", strerror(err));
nvlist_free(nvl);
goto out;
}
nvlist_free(data);
}
out:
free(module);
free(class);
free(name);
return (nvl);
}
void
kstatjs_free(nvlist_t *nvl)
{
nvlist_free(nvl);
}
int
kstatjs_close(kstatjs_t *ksj)
{
if (ksj->ksj_ctl == NULL) {
kstatjs_err("kstat reader has already been closed");
return (-1);
}
kstatjs_finalize(ksj);
return (0);
}