Print this page
4740 kstat(1M) prints wrong snaptime
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/stat/kstat/kstat.c
+++ new/usr/src/cmd/stat/kstat/kstat.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2013 David Hoeppner. All rights reserved.
25 25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
26 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 27 */
28 28
29 29 /*
30 30 * Display kernel statistics
31 31 *
32 32 * This is a reimplementation of the perl kstat command originally found
33 33 * under usr/src/cmd/kstat/kstat.pl
34 34 *
35 35 * Incompatibilities:
36 36 * - perl regular expressions replaced with extended REs bracketed by '/'
37 37 *
38 38 * Flags added:
39 39 * -C similar to the -p option but value is separated by a colon
40 40 * -h display help
41 41 * -j json format
42 42 */
43 43
44 44 #include <assert.h>
45 45 #include <ctype.h>
46 46 #include <errno.h>
47 47 #include <kstat.h>
48 48 #include <langinfo.h>
49 49 #include <libgen.h>
50 50 #include <limits.h>
51 51 #include <locale.h>
52 52 #include <signal.h>
53 53 #include <stddef.h>
54 54 #include <stdio.h>
55 55 #include <stdlib.h>
56 56 #include <string.h>
57 57 #include <strings.h>
58 58 #include <time.h>
59 59 #include <unistd.h>
60 60 #include <sys/list.h>
61 61 #include <sys/time.h>
62 62 #include <sys/types.h>
63 63
64 64 #include "kstat.h"
65 65 #include "statcommon.h"
66 66
67 67 char *cmdname = "kstat"; /* Name of this command */
68 68 int caught_cont = 0; /* Have caught a SIGCONT */
69 69
70 70 static uint_t g_timestamp_fmt = NODATE;
71 71
72 72 /* Helper flag - header was printed already? */
73 73 static boolean_t g_headerflg;
74 74
75 75 /* Saved command line options */
76 76 static boolean_t g_cflg = B_FALSE;
77 77 static boolean_t g_jflg = B_FALSE;
78 78 static boolean_t g_lflg = B_FALSE;
79 79 static boolean_t g_pflg = B_FALSE;
80 80 static boolean_t g_qflg = B_FALSE;
81 81 static ks_pattern_t g_ks_class = {"*", 0};
82 82
83 83 /* Return zero if a selector did match */
84 84 static int g_matched = 1;
85 85
86 86 /* Sorted list of kstat instances */
87 87 static list_t instances_list;
88 88 static list_t selector_list;
89 89
90 90 int
91 91 main(int argc, char **argv)
92 92 {
93 93 ks_selector_t *nselector;
94 94 ks_selector_t *uselector;
95 95 kstat_ctl_t *kc;
96 96 hrtime_t start_n;
97 97 hrtime_t period_n;
98 98 boolean_t errflg = B_FALSE;
99 99 boolean_t nselflg = B_FALSE;
100 100 boolean_t uselflg = B_FALSE;
101 101 char *q;
102 102 int count = 1;
103 103 int infinite_cycles = 0;
104 104 int interval = 0;
105 105 int n = 0;
106 106 int c, m, tmp;
107 107
108 108 (void) setlocale(LC_ALL, "");
109 109 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
110 110 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
111 111 #endif
112 112 (void) textdomain(TEXT_DOMAIN);
113 113
114 114 /*
115 115 * Create the selector list and a dummy default selector to match
116 116 * everything. While we process the cmdline options we will add
117 117 * selectors to this list.
118 118 */
119 119 list_create(&selector_list, sizeof (ks_selector_t),
120 120 offsetof(ks_selector_t, ks_next));
121 121
122 122 nselector = new_selector();
123 123
124 124 /*
125 125 * Parse named command line arguments.
126 126 */
127 127 while ((c = getopt(argc, argv, "h?CqjlpT:m:i:n:s:c:")) != EOF)
128 128 switch (c) {
129 129 case 'h':
130 130 case '?':
131 131 usage();
132 132 exit(0);
133 133 break;
134 134 case 'C':
135 135 g_pflg = g_cflg = B_TRUE;
136 136 break;
137 137 case 'q':
138 138 g_qflg = B_TRUE;
139 139 break;
140 140 case 'j':
141 141 g_jflg = B_TRUE;
142 142 break;
143 143 case 'l':
144 144 g_pflg = g_lflg = B_TRUE;
145 145 break;
146 146 case 'p':
147 147 g_pflg = B_TRUE;
148 148 break;
149 149 case 'T':
150 150 switch (*optarg) {
151 151 case 'd':
152 152 g_timestamp_fmt = DDATE;
153 153 break;
154 154 case 'u':
155 155 g_timestamp_fmt = UDATE;
156 156 break;
157 157 default:
158 158 errflg = B_TRUE;
159 159 }
160 160 break;
161 161 case 'm':
162 162 nselflg = B_TRUE;
163 163 nselector->ks_module.pstr =
164 164 (char *)ks_safe_strdup(optarg);
165 165 break;
166 166 case 'i':
167 167 nselflg = B_TRUE;
168 168 nselector->ks_instance.pstr =
169 169 (char *)ks_safe_strdup(optarg);
170 170 break;
171 171 case 'n':
172 172 nselflg = B_TRUE;
173 173 nselector->ks_name.pstr =
174 174 (char *)ks_safe_strdup(optarg);
175 175 break;
176 176 case 's':
177 177 nselflg = B_TRUE;
178 178 nselector->ks_statistic.pstr =
179 179 (char *)ks_safe_strdup(optarg);
180 180 break;
181 181 case 'c':
182 182 g_ks_class.pstr =
183 183 (char *)ks_safe_strdup(optarg);
184 184 break;
185 185 default:
186 186 errflg = B_TRUE;
187 187 break;
188 188 }
189 189
190 190 if (g_qflg && (g_jflg || g_pflg)) {
191 191 (void) fprintf(stderr, gettext(
192 192 "-q and -lpj are mutually exclusive\n"));
193 193 errflg = B_TRUE;
194 194 }
195 195
196 196 if (errflg) {
197 197 usage();
198 198 exit(2);
199 199 }
200 200
201 201 argc -= optind;
202 202 argv += optind;
203 203
204 204 /*
205 205 * Consume the rest of the command line. Parsing the
206 206 * unnamed command line arguments.
207 207 */
208 208 while (argc--) {
209 209 errno = 0;
210 210 tmp = strtoul(*argv, &q, 10);
211 211 if (tmp == ULONG_MAX && errno == ERANGE) {
212 212 if (n == 0) {
213 213 (void) fprintf(stderr, gettext(
214 214 "Interval is too large\n"));
215 215 } else if (n == 1) {
216 216 (void) fprintf(stderr, gettext(
217 217 "Count is too large\n"));
218 218 }
219 219 usage();
220 220 exit(2);
221 221 }
222 222
223 223 if (errno != 0 || *q != '\0') {
224 224 m = 0;
225 225 uselector = new_selector();
226 226 while ((q = (char *)strsep(argv, ":")) != NULL) {
227 227 m++;
228 228 if (m > 4) {
229 229 free(uselector);
230 230 usage();
231 231 exit(2);
232 232 }
233 233
234 234 if (*q != '\0') {
235 235 switch (m) {
236 236 case 1:
237 237 uselector->ks_module.pstr =
238 238 (char *)ks_safe_strdup(q);
239 239 break;
240 240 case 2:
241 241 uselector->ks_instance.pstr =
242 242 (char *)ks_safe_strdup(q);
243 243 break;
244 244 case 3:
245 245 uselector->ks_name.pstr =
246 246 (char *)ks_safe_strdup(q);
247 247 break;
248 248 case 4:
249 249 uselector->ks_statistic.pstr =
250 250 (char *)ks_safe_strdup(q);
251 251 break;
252 252 default:
253 253 assert(B_FALSE);
254 254 }
255 255 }
256 256 }
257 257
258 258 uselflg = B_TRUE;
259 259 list_insert_tail(&selector_list, uselector);
260 260 } else {
261 261 if (tmp < 1) {
262 262 if (n == 0) {
263 263 (void) fprintf(stderr, gettext(
264 264 "Interval must be an "
265 265 "integer >= 1"));
266 266 } else if (n == 1) {
267 267 (void) fprintf(stderr, gettext(
268 268 "Count must be an integer >= 1"));
269 269 }
270 270 usage();
271 271 exit(2);
272 272 } else {
273 273 if (n == 0) {
274 274 interval = tmp;
275 275 count = -1;
276 276 } else if (n == 1) {
277 277 count = tmp;
278 278 } else {
279 279 usage();
280 280 exit(2);
281 281 }
282 282 }
283 283 n++;
284 284 }
285 285 argv++;
286 286 }
287 287
288 288 /*
289 289 * Check if we founded a named selector on the cmdline.
290 290 */
291 291 if (uselflg) {
292 292 if (nselflg) {
293 293 (void) fprintf(stderr, gettext(
294 294 "[module[:instance[:name[:statistic]]]] and "
295 295 "-m -i -n -s are mutually exclusive"));
296 296 usage();
297 297 exit(2);
298 298 } else {
299 299 free(nselector);
300 300 }
301 301 } else {
302 302 list_insert_tail(&selector_list, nselector);
303 303 }
304 304
305 305 assert(!list_is_empty(&selector_list));
306 306
307 307 list_create(&instances_list, sizeof (ks_instance_t),
308 308 offsetof(ks_instance_t, ks_next));
309 309
310 310 while ((kc = kstat_open()) == NULL) {
311 311 if (errno == EAGAIN) {
312 312 (void) poll(NULL, 0, 200);
313 313 } else {
314 314 perror("kstat_open");
315 315 exit(3);
316 316 }
317 317 }
318 318
319 319 if (count > 1) {
320 320 if (signal(SIGCONT, cont_handler) == SIG_ERR) {
321 321 (void) fprintf(stderr, gettext(
322 322 "signal failed"));
323 323 exit(3);
324 324 }
325 325 }
326 326
327 327 period_n = (hrtime_t)interval * NANOSEC;
328 328 start_n = gethrtime();
329 329
330 330 while (count == -1 || count-- > 0) {
331 331 ks_instances_read(kc);
332 332 ks_instances_print();
333 333
334 334 if (interval && count) {
335 335 ks_sleep_until(&start_n, period_n, infinite_cycles,
336 336 &caught_cont);
337 337 (void) kstat_chain_update(kc);
338 338 (void) putchar('\n');
339 339 }
340 340 }
341 341
342 342 (void) kstat_close(kc);
343 343
344 344 return (g_matched);
345 345 }
346 346
347 347 /*
348 348 * Print usage.
349 349 */
350 350 static void
351 351 usage(void)
352 352 {
353 353 (void) fprintf(stderr, gettext(
354 354 "Usage:\n"
355 355 "kstat [ -Cjlpq ] [ -T d|u ] [ -c class ]\n"
356 356 " [ -m module ] [ -i instance ] [ -n name ] [ -s statistic ]\n"
357 357 " [ interval [ count ] ]\n"
358 358 "kstat [ -Cjlpq ] [ -T d|u ] [ -c class ]\n"
359 359 " [ module[:instance[:name[:statistic]]] ... ]\n"
360 360 " [ interval [ count ] ]\n"));
361 361 }
362 362
363 363 /*
364 364 * Sort compare function.
365 365 */
366 366 static int
367 367 compare_instances(ks_instance_t *l_arg, ks_instance_t *r_arg)
368 368 {
369 369 int rval;
370 370
371 371 rval = strcasecmp(l_arg->ks_module, r_arg->ks_module);
372 372 if (rval == 0) {
373 373 if (l_arg->ks_instance == r_arg->ks_instance) {
374 374 return (strcasecmp(l_arg->ks_name, r_arg->ks_name));
375 375 } else if (l_arg->ks_instance < r_arg->ks_instance) {
376 376 return (-1);
377 377 } else {
378 378 return (1);
379 379 }
380 380 } else {
381 381 return (rval);
382 382 }
383 383 }
384 384
385 385 static char *
386 386 ks_safe_strdup(char *str)
387 387 {
388 388 char *ret;
389 389
390 390 if (str == NULL) {
391 391 return (NULL);
392 392 }
393 393
394 394 while ((ret = strdup(str)) == NULL) {
395 395 if (errno == EAGAIN) {
396 396 (void) poll(NULL, 0, 200);
397 397 } else {
398 398 perror("strdup");
399 399 exit(3);
400 400 }
401 401 }
402 402
403 403 return (ret);
404 404 }
405 405
406 406 static void
407 407 ks_sleep_until(hrtime_t *wakeup, hrtime_t interval, int forever,
408 408 int *caught_cont)
409 409 {
410 410 hrtime_t now, pause, pause_left;
411 411 struct timespec pause_tv;
412 412 int status;
413 413
414 414 now = gethrtime();
415 415 pause = *wakeup + interval - now;
416 416
417 417 if (pause <= 0 || pause < (interval / 4)) {
418 418 if (forever || *caught_cont) {
419 419 *wakeup = now + interval;
420 420 pause = interval;
421 421 } else {
422 422 pause = interval / 2;
423 423 *wakeup += interval;
424 424 }
425 425 } else {
426 426 *wakeup += interval;
427 427 }
428 428
429 429 if (pause < 1000) {
430 430 return;
431 431 }
432 432
433 433 pause_left = pause;
434 434 do {
435 435 pause_tv.tv_sec = pause_left / NANOSEC;
436 436 pause_tv.tv_nsec = pause_left % NANOSEC;
437 437 status = nanosleep(&pause_tv, (struct timespec *)NULL);
438 438 if (status < 0) {
439 439 if (errno == EINTR) {
440 440 now = gethrtime();
441 441 pause_left = *wakeup - now;
442 442 if (pause_left < 1000) {
443 443 return;
444 444 }
445 445 } else {
446 446 perror("nanosleep");
447 447 exit(3);
448 448 }
449 449 }
450 450 } while (status != 0);
451 451 }
452 452
453 453 /*
454 454 * Inserts an instance in the per selector list.
455 455 */
456 456 static void
457 457 nvpair_insert(ks_instance_t *ksi, char *name, ks_value_t *value,
458 458 uchar_t data_type)
459 459 {
460 460 ks_nvpair_t *instance;
461 461 ks_nvpair_t *tmp;
462 462
463 463 instance = (ks_nvpair_t *)malloc(sizeof (ks_nvpair_t));
464 464 if (instance == NULL) {
465 465 perror("malloc");
466 466 exit(3);
467 467 }
468 468
469 469 (void) strlcpy(instance->name, name, KSTAT_STRLEN);
470 470 (void) memcpy(&instance->value, value, sizeof (ks_value_t));
471 471 instance->data_type = data_type;
472 472
473 473 tmp = list_head(&ksi->ks_nvlist);
474 474 while (tmp != NULL && strcasecmp(instance->name, tmp->name) > 0)
475 475 tmp = list_next(&ksi->ks_nvlist, tmp);
476 476
477 477 list_insert_before(&ksi->ks_nvlist, tmp, instance);
478 478 }
479 479
480 480 /*
481 481 * Allocates a new all-matching selector.
482 482 */
483 483 static ks_selector_t *
484 484 new_selector(void)
485 485 {
486 486 ks_selector_t *selector;
487 487
488 488 selector = (ks_selector_t *)malloc(sizeof (ks_selector_t));
489 489 if (selector == NULL) {
490 490 perror("malloc");
491 491 exit(3);
492 492 }
493 493
494 494 list_link_init(&selector->ks_next);
495 495
496 496 selector->ks_module.pstr = "*";
497 497 selector->ks_instance.pstr = "*";
498 498 selector->ks_name.pstr = "*";
499 499 selector->ks_statistic.pstr = "*";
500 500
501 501 return (selector);
502 502 }
503 503
504 504 /*
505 505 * This function was taken from the perl kstat module code - please
506 506 * see for further comments there.
507 507 */
508 508 static kstat_raw_reader_t
509 509 lookup_raw_kstat_fn(char *module, char *name)
510 510 {
511 511 char key[KSTAT_STRLEN * 2];
512 512 register char *f, *t;
513 513 int n = 0;
514 514
515 515 for (f = module, t = key; *f != '\0'; f++, t++) {
516 516 while (*f != '\0' && isdigit(*f))
517 517 f++;
518 518 *t = *f;
519 519 }
520 520 *t++ = ':';
521 521
522 522 for (f = name; *f != '\0'; f++, t++) {
523 523 while (*f != '\0' && isdigit(*f))
524 524 f++;
525 525 *t = *f;
526 526 }
527 527 *t = '\0';
528 528
529 529 while (ks_raw_lookup[n].fn != NULL) {
530 530 if (strncmp(ks_raw_lookup[n].name, key, strlen(key)) == 0)
531 531 return (ks_raw_lookup[n].fn);
532 532 n++;
533 533 }
534 534
535 535 return (0);
536 536 }
537 537
538 538 /*
539 539 * Match a string against a shell glob or extended regular expression.
540 540 */
541 541 static boolean_t
542 542 ks_match(const char *str, ks_pattern_t *pattern)
543 543 {
544 544 int regcode;
545 545 char *regstr;
546 546 char *errbuf;
547 547 size_t bufsz;
548 548
549 549 if (pattern->pstr != NULL && gmatch(pattern->pstr, "/*/") != 0) {
550 550 /* All regex patterns are strdup'd copies */
551 551 regstr = pattern->pstr + 1;
552 552 *(strrchr(regstr, '/')) = '\0';
553 553
554 554 regcode = regcomp(&pattern->preg, regstr,
555 555 REG_EXTENDED | REG_NOSUB);
556 556 if (regcode != 0) {
557 557 bufsz = regerror(regcode, NULL, NULL, 0);
558 558 if (bufsz != 0) {
559 559 errbuf = malloc(bufsz);
560 560 if (errbuf == NULL) {
561 561 perror("malloc");
562 562 exit(3);
563 563 }
564 564 (void) regerror(regcode, NULL, errbuf, bufsz);
565 565 (void) fprintf(stderr, "kstat: %s\n", errbuf);
566 566 }
567 567 usage();
568 568 exit(2);
569 569 }
570 570
571 571 pattern->pstr = NULL;
572 572 }
573 573
574 574 if (pattern->pstr == NULL) {
575 575 return (regexec(&pattern->preg, str, 0, NULL, 0) == 0);
576 576 }
577 577
578 578 return ((gmatch(str, pattern->pstr) != 0));
579 579 }
580 580
581 581 /*
582 582 * Iterate over all kernel statistics and save matches.
583 583 */
584 584 static void
585 585 ks_instances_read(kstat_ctl_t *kc)
586 586 {
587 587 kstat_raw_reader_t save_raw = NULL;
588 588 kid_t id;
589 589 ks_selector_t *selector;
590 590 ks_instance_t *ksi;
591 591 ks_instance_t *tmp;
592 592 kstat_t *kp;
593 593 boolean_t skip;
594 594
595 595 for (kp = kc->kc_chain; kp != NULL; kp = kp->ks_next) {
596 596 /* Don't bother storing the kstat headers */
597 597 if (strncmp(kp->ks_name, "kstat_", 6) == 0) {
598 598 continue;
599 599 }
600 600
601 601 /* Don't bother storing raw stats we don't understand */
602 602 if (kp->ks_type == KSTAT_TYPE_RAW) {
603 603 save_raw = lookup_raw_kstat_fn(kp->ks_module,
604 604 kp->ks_name);
605 605 if (save_raw == NULL) {
606 606 #ifdef REPORT_UNKNOWN
607 607 (void) fprintf(stderr,
608 608 "Unknown kstat type %s:%d:%s - "
609 609 "%d of size %d\n", kp->ks_module,
610 610 kp->ks_instance, kp->ks_name,
611 611 kp->ks_ndata, kp->ks_data_size);
612 612 #endif
613 613 continue;
614 614 }
615 615 }
616 616
617 617 /*
618 618 * Iterate over the list of selectors and skip
619 619 * instances we dont want. We filter for statistics
620 620 * later, as we dont know them yet.
621 621 */
622 622 skip = B_TRUE;
623 623 selector = list_head(&selector_list);
624 624 while (selector != NULL) {
625 625 if (ks_match(kp->ks_module, &selector->ks_module) &&
626 626 ks_match(kp->ks_name, &selector->ks_name)) {
627 627 skip = B_FALSE;
628 628 break;
629 629 }
630 630 selector = list_next(&selector_list, selector);
631 631 }
632 632
633 633 if (skip) {
634 634 continue;
635 635 }
636 636
637 637 /*
638 638 * Allocate a new instance and fill in the values
639 639 * we know so far.
640 640 */
641 641 ksi = (ks_instance_t *)malloc(sizeof (ks_instance_t));
642 642 if (ksi == NULL) {
643 643 perror("malloc");
644 644 exit(3);
645 645 }
646 646
647 647 list_link_init(&ksi->ks_next);
648 648
649 649 (void) strlcpy(ksi->ks_module, kp->ks_module, KSTAT_STRLEN);
↓ open down ↓ |
649 lines elided |
↑ open up ↑ |
650 650 (void) strlcpy(ksi->ks_name, kp->ks_name, KSTAT_STRLEN);
651 651 (void) strlcpy(ksi->ks_class, kp->ks_class, KSTAT_STRLEN);
652 652
653 653 ksi->ks_instance = kp->ks_instance;
654 654 ksi->ks_snaptime = kp->ks_snaptime;
655 655 ksi->ks_type = kp->ks_type;
656 656
657 657 list_create(&ksi->ks_nvlist, sizeof (ks_nvpair_t),
658 658 offsetof(ks_nvpair_t, nv_next));
659 659
660 - SAVE_HRTIME_X(ksi, "crtime", kp->ks_crtime);
661 - SAVE_HRTIME_X(ksi, "snaptime", kp->ks_snaptime);
662 - if (g_pflg) {
663 - SAVE_STRING_X(ksi, "class", kp->ks_class);
664 - }
665 -
666 660 /* Insert this instance into a sorted list */
667 661 tmp = list_head(&instances_list);
668 662 while (tmp != NULL && compare_instances(ksi, tmp) > 0)
669 663 tmp = list_next(&instances_list, tmp);
670 664
671 665 list_insert_before(&instances_list, tmp, ksi);
672 666
673 667 /* Read the actual statistics */
674 668 id = kstat_read(kc, kp, NULL);
675 669 if (id == -1) {
676 670 #ifdef REPORT_UNKNOWN
677 671 perror("kstat_read");
678 672 #endif
679 673 continue;
680 674 }
681 675
676 + SAVE_HRTIME_X(ksi, "crtime", kp->ks_crtime);
677 + SAVE_HRTIME_X(ksi, "snaptime", kp->ks_snaptime);
678 + if (g_pflg) {
679 + SAVE_STRING_X(ksi, "class", kp->ks_class);
680 + }
681 +
682 682 switch (kp->ks_type) {
683 683 case KSTAT_TYPE_RAW:
684 684 save_raw(kp, ksi);
685 685 break;
686 686 case KSTAT_TYPE_NAMED:
687 687 save_named(kp, ksi);
688 688 break;
689 689 case KSTAT_TYPE_INTR:
690 690 save_intr(kp, ksi);
691 691 break;
692 692 case KSTAT_TYPE_IO:
693 693 save_io(kp, ksi);
694 694 break;
695 695 case KSTAT_TYPE_TIMER:
696 696 save_timer(kp, ksi);
697 697 break;
698 698 default:
699 699 assert(B_FALSE); /* Invalid type */
700 700 break;
701 701 }
702 702 }
703 703 }
704 704
705 705 /*
706 706 * Print the value of a name-value pair.
707 707 */
708 708 static void
709 709 ks_value_print(ks_nvpair_t *nvpair)
710 710 {
711 711 switch (nvpair->data_type) {
712 712 case KSTAT_DATA_CHAR:
713 713 (void) fprintf(stdout, "%s", nvpair->value.c);
714 714 break;
715 715 case KSTAT_DATA_INT32:
716 716 (void) fprintf(stdout, "%d", nvpair->value.i32);
717 717 break;
718 718 case KSTAT_DATA_UINT32:
719 719 (void) fprintf(stdout, "%u", nvpair->value.ui32);
720 720 break;
721 721 case KSTAT_DATA_INT64:
722 722 (void) fprintf(stdout, "%lld", nvpair->value.i64);
723 723 break;
724 724 case KSTAT_DATA_UINT64:
725 725 (void) fprintf(stdout, "%llu", nvpair->value.ui64);
726 726 break;
727 727 case KSTAT_DATA_STRING:
728 728 (void) fprintf(stdout, "%s", KSTAT_NAMED_STR_PTR(nvpair));
729 729 break;
730 730 case KSTAT_DATA_HRTIME:
731 731 if (nvpair->value.ui64 == 0)
732 732 (void) fprintf(stdout, "0");
733 733 else
734 734 (void) fprintf(stdout, "%.9f",
735 735 nvpair->value.ui64 / 1000000000.0);
736 736 break;
737 737 default:
738 738 assert(B_FALSE);
739 739 }
740 740 }
741 741
742 742 /*
743 743 * Print a single instance.
744 744 */
745 745 static void
746 746 ks_instance_print(ks_instance_t *ksi, ks_nvpair_t *nvpair)
747 747 {
748 748 if (g_headerflg) {
749 749 if (!g_pflg) {
750 750 (void) fprintf(stdout, DFLT_FMT,
751 751 ksi->ks_module, ksi->ks_instance,
752 752 ksi->ks_name, ksi->ks_class);
753 753 }
754 754 g_headerflg = B_FALSE;
755 755 }
756 756
757 757 if (g_pflg) {
758 758 (void) fprintf(stdout, KS_PFMT,
759 759 ksi->ks_module, ksi->ks_instance,
760 760 ksi->ks_name, nvpair->name);
761 761 if (!g_lflg) {
762 762 (void) putchar(g_cflg ? ':': '\t');
763 763 ks_value_print(nvpair);
764 764 }
765 765 } else {
766 766 (void) fprintf(stdout, KS_DFMT, nvpair->name);
767 767 ks_value_print(nvpair);
768 768 }
769 769
770 770 (void) putchar('\n');
771 771 }
772 772
773 773 /*
774 774 * Print a single instance in JSON format.
775 775 */
776 776 static void
777 777 ks_instance_print_json(ks_instance_t *ksi, ks_nvpair_t *nvpair)
778 778 {
779 779 if (g_headerflg) {
780 780 (void) fprintf(stdout, JSON_FMT,
781 781 ksi->ks_module, ksi->ks_instance,
782 782 ksi->ks_name, ksi->ks_class,
783 783 ksi->ks_type);
784 784
785 785 if (ksi->ks_snaptime == 0)
786 786 (void) fprintf(stdout, "\t\"snaptime\": 0,\n");
787 787 else
788 788 (void) fprintf(stdout, "\t\"snaptime\": %.9f,\n",
789 789 ksi->ks_snaptime / 1000000000.0);
790 790
791 791 (void) fprintf(stdout, "\t\"data\": {\n");
792 792
793 793 g_headerflg = B_FALSE;
794 794 }
795 795
796 796 (void) fprintf(stdout, KS_JFMT, nvpair->name);
797 797 if (nvpair->data_type == KSTAT_DATA_STRING) {
798 798 (void) putchar('\"');
799 799 ks_value_print(nvpair);
800 800 (void) putchar('\"');
801 801 } else {
802 802 ks_value_print(nvpair);
803 803 }
804 804 if (nvpair != list_tail(&ksi->ks_nvlist))
805 805 (void) putchar(',');
806 806
807 807 (void) putchar('\n');
808 808 }
809 809
810 810 /*
811 811 * Print all instances.
812 812 */
813 813 static void
814 814 ks_instances_print(void)
815 815 {
816 816 ks_selector_t *selector;
817 817 ks_instance_t *ksi, *ktmp;
818 818 ks_nvpair_t *nvpair, *ntmp;
819 819 void (*ks_print_fn)(ks_instance_t *, ks_nvpair_t *);
820 820 char *ks_number;
821 821
822 822 if (g_timestamp_fmt != NODATE)
823 823 print_timestamp(g_timestamp_fmt);
824 824
825 825 if (g_jflg) {
826 826 ks_print_fn = &ks_instance_print_json;
827 827 (void) putchar('[');
828 828 } else {
829 829 ks_print_fn = &ks_instance_print;
830 830 }
831 831
832 832 /* Iterate over each selector */
833 833 selector = list_head(&selector_list);
834 834 while (selector != NULL) {
835 835
836 836 /* Iterate over each instance */
837 837 for (ksi = list_head(&instances_list); ksi != NULL;
838 838 ksi = list_next(&instances_list, ksi)) {
839 839
840 840 (void) asprintf(&ks_number, "%d", ksi->ks_instance);
841 841 if (!(ks_match(ksi->ks_module, &selector->ks_module) &&
842 842 ks_match(ksi->ks_name, &selector->ks_name) &&
843 843 ks_match(ks_number, &selector->ks_instance) &&
844 844 ks_match(ksi->ks_class, &g_ks_class))) {
845 845 free(ks_number);
846 846 continue;
847 847 }
848 848
849 849 free(ks_number);
850 850
851 851 /* Finally iterate over each statistic */
852 852 g_headerflg = B_TRUE;
853 853 for (nvpair = list_head(&ksi->ks_nvlist);
854 854 nvpair != NULL;
855 855 nvpair = list_next(&ksi->ks_nvlist, nvpair)) {
856 856 if (!ks_match(nvpair->name,
857 857 &selector->ks_statistic))
858 858 continue;
859 859
860 860 g_matched = 0;
861 861 if (!g_qflg)
862 862 (*ks_print_fn)(ksi, nvpair);
863 863 }
864 864
865 865 if (!g_headerflg) {
866 866 if (g_jflg) {
867 867 (void) fprintf(stdout, "\t}\n}");
868 868 if (ksi != list_tail(&instances_list))
869 869 (void) putchar(',');
870 870 } else if (!g_pflg) {
871 871 (void) putchar('\n');
872 872 }
873 873 }
874 874 }
875 875
876 876 selector = list_next(&selector_list, selector);
877 877 }
878 878
879 879 if (g_jflg)
880 880 (void) fprintf(stdout, "]\n");
881 881
882 882 (void) fflush(stdout);
883 883
884 884 /* Free the instances list */
885 885 ksi = list_head(&instances_list);
886 886 while (ksi != NULL) {
887 887 nvpair = list_head(&ksi->ks_nvlist);
888 888 while (nvpair != NULL) {
889 889 ntmp = nvpair;
890 890 nvpair = list_next(&ksi->ks_nvlist, nvpair);
891 891 list_remove(&ksi->ks_nvlist, ntmp);
892 892 if (ntmp->data_type == KSTAT_DATA_STRING)
893 893 free(ntmp->value.str.addr.ptr);
894 894 free(ntmp);
895 895 }
896 896
897 897 ktmp = ksi;
898 898 ksi = list_next(&instances_list, ksi);
899 899 list_remove(&instances_list, ktmp);
900 900 list_destroy(&ktmp->ks_nvlist);
901 901 free(ktmp);
902 902 }
903 903 }
904 904
905 905 static void
906 906 save_cpu_stat(kstat_t *kp, ks_instance_t *ksi)
907 907 {
908 908 cpu_stat_t *stat;
909 909 cpu_sysinfo_t *sysinfo;
910 910 cpu_syswait_t *syswait;
911 911 cpu_vminfo_t *vminfo;
912 912
913 913 stat = (cpu_stat_t *)(kp->ks_data);
914 914 sysinfo = &stat->cpu_sysinfo;
915 915 syswait = &stat->cpu_syswait;
916 916 vminfo = &stat->cpu_vminfo;
917 917
918 918 SAVE_UINT32_X(ksi, "idle", sysinfo->cpu[CPU_IDLE]);
919 919 SAVE_UINT32_X(ksi, "user", sysinfo->cpu[CPU_USER]);
920 920 SAVE_UINT32_X(ksi, "kernel", sysinfo->cpu[CPU_KERNEL]);
921 921 SAVE_UINT32_X(ksi, "wait", sysinfo->cpu[CPU_WAIT]);
922 922 SAVE_UINT32_X(ksi, "wait_io", sysinfo->wait[W_IO]);
923 923 SAVE_UINT32_X(ksi, "wait_swap", sysinfo->wait[W_SWAP]);
924 924 SAVE_UINT32_X(ksi, "wait_pio", sysinfo->wait[W_PIO]);
925 925 SAVE_UINT32(ksi, sysinfo, bread);
926 926 SAVE_UINT32(ksi, sysinfo, bwrite);
927 927 SAVE_UINT32(ksi, sysinfo, lread);
928 928 SAVE_UINT32(ksi, sysinfo, lwrite);
929 929 SAVE_UINT32(ksi, sysinfo, phread);
930 930 SAVE_UINT32(ksi, sysinfo, phwrite);
931 931 SAVE_UINT32(ksi, sysinfo, pswitch);
932 932 SAVE_UINT32(ksi, sysinfo, trap);
933 933 SAVE_UINT32(ksi, sysinfo, intr);
934 934 SAVE_UINT32(ksi, sysinfo, syscall);
935 935 SAVE_UINT32(ksi, sysinfo, sysread);
936 936 SAVE_UINT32(ksi, sysinfo, syswrite);
937 937 SAVE_UINT32(ksi, sysinfo, sysfork);
938 938 SAVE_UINT32(ksi, sysinfo, sysvfork);
939 939 SAVE_UINT32(ksi, sysinfo, sysexec);
940 940 SAVE_UINT32(ksi, sysinfo, readch);
941 941 SAVE_UINT32(ksi, sysinfo, writech);
942 942 SAVE_UINT32(ksi, sysinfo, rcvint);
943 943 SAVE_UINT32(ksi, sysinfo, xmtint);
944 944 SAVE_UINT32(ksi, sysinfo, mdmint);
945 945 SAVE_UINT32(ksi, sysinfo, rawch);
946 946 SAVE_UINT32(ksi, sysinfo, canch);
947 947 SAVE_UINT32(ksi, sysinfo, outch);
948 948 SAVE_UINT32(ksi, sysinfo, msg);
949 949 SAVE_UINT32(ksi, sysinfo, sema);
950 950 SAVE_UINT32(ksi, sysinfo, namei);
951 951 SAVE_UINT32(ksi, sysinfo, ufsiget);
952 952 SAVE_UINT32(ksi, sysinfo, ufsdirblk);
953 953 SAVE_UINT32(ksi, sysinfo, ufsipage);
954 954 SAVE_UINT32(ksi, sysinfo, ufsinopage);
955 955 SAVE_UINT32(ksi, sysinfo, inodeovf);
956 956 SAVE_UINT32(ksi, sysinfo, fileovf);
957 957 SAVE_UINT32(ksi, sysinfo, procovf);
958 958 SAVE_UINT32(ksi, sysinfo, intrthread);
959 959 SAVE_UINT32(ksi, sysinfo, intrblk);
960 960 SAVE_UINT32(ksi, sysinfo, idlethread);
961 961 SAVE_UINT32(ksi, sysinfo, inv_swtch);
962 962 SAVE_UINT32(ksi, sysinfo, nthreads);
963 963 SAVE_UINT32(ksi, sysinfo, cpumigrate);
964 964 SAVE_UINT32(ksi, sysinfo, xcalls);
965 965 SAVE_UINT32(ksi, sysinfo, mutex_adenters);
966 966 SAVE_UINT32(ksi, sysinfo, rw_rdfails);
967 967 SAVE_UINT32(ksi, sysinfo, rw_wrfails);
968 968 SAVE_UINT32(ksi, sysinfo, modload);
969 969 SAVE_UINT32(ksi, sysinfo, modunload);
970 970 SAVE_UINT32(ksi, sysinfo, bawrite);
971 971 #ifdef STATISTICS /* see header file */
972 972 SAVE_UINT32(ksi, sysinfo, rw_enters);
973 973 SAVE_UINT32(ksi, sysinfo, win_uo_cnt);
974 974 SAVE_UINT32(ksi, sysinfo, win_uu_cnt);
975 975 SAVE_UINT32(ksi, sysinfo, win_so_cnt);
976 976 SAVE_UINT32(ksi, sysinfo, win_su_cnt);
977 977 SAVE_UINT32(ksi, sysinfo, win_suo_cnt);
978 978 #endif
979 979
980 980 SAVE_INT32(ksi, syswait, iowait);
981 981 SAVE_INT32(ksi, syswait, swap);
982 982 SAVE_INT32(ksi, syswait, physio);
983 983
984 984 SAVE_UINT32(ksi, vminfo, pgrec);
985 985 SAVE_UINT32(ksi, vminfo, pgfrec);
986 986 SAVE_UINT32(ksi, vminfo, pgin);
987 987 SAVE_UINT32(ksi, vminfo, pgpgin);
988 988 SAVE_UINT32(ksi, vminfo, pgout);
989 989 SAVE_UINT32(ksi, vminfo, pgpgout);
990 990 SAVE_UINT32(ksi, vminfo, swapin);
991 991 SAVE_UINT32(ksi, vminfo, pgswapin);
992 992 SAVE_UINT32(ksi, vminfo, swapout);
993 993 SAVE_UINT32(ksi, vminfo, pgswapout);
994 994 SAVE_UINT32(ksi, vminfo, zfod);
995 995 SAVE_UINT32(ksi, vminfo, dfree);
996 996 SAVE_UINT32(ksi, vminfo, scan);
997 997 SAVE_UINT32(ksi, vminfo, rev);
998 998 SAVE_UINT32(ksi, vminfo, hat_fault);
999 999 SAVE_UINT32(ksi, vminfo, as_fault);
1000 1000 SAVE_UINT32(ksi, vminfo, maj_fault);
1001 1001 SAVE_UINT32(ksi, vminfo, cow_fault);
1002 1002 SAVE_UINT32(ksi, vminfo, prot_fault);
1003 1003 SAVE_UINT32(ksi, vminfo, softlock);
1004 1004 SAVE_UINT32(ksi, vminfo, kernel_asflt);
1005 1005 SAVE_UINT32(ksi, vminfo, pgrrun);
1006 1006 SAVE_UINT32(ksi, vminfo, execpgin);
1007 1007 SAVE_UINT32(ksi, vminfo, execpgout);
1008 1008 SAVE_UINT32(ksi, vminfo, execfree);
1009 1009 SAVE_UINT32(ksi, vminfo, anonpgin);
1010 1010 SAVE_UINT32(ksi, vminfo, anonpgout);
1011 1011 SAVE_UINT32(ksi, vminfo, anonfree);
1012 1012 SAVE_UINT32(ksi, vminfo, fspgin);
1013 1013 SAVE_UINT32(ksi, vminfo, fspgout);
1014 1014 SAVE_UINT32(ksi, vminfo, fsfree);
1015 1015 }
1016 1016
1017 1017 static void
1018 1018 save_var(kstat_t *kp, ks_instance_t *ksi)
1019 1019 {
1020 1020 struct var *var = (struct var *)(kp->ks_data);
1021 1021
1022 1022 assert(kp->ks_data_size == sizeof (struct var));
1023 1023
1024 1024 SAVE_INT32(ksi, var, v_buf);
1025 1025 SAVE_INT32(ksi, var, v_call);
1026 1026 SAVE_INT32(ksi, var, v_proc);
1027 1027 SAVE_INT32(ksi, var, v_maxupttl);
1028 1028 SAVE_INT32(ksi, var, v_nglobpris);
1029 1029 SAVE_INT32(ksi, var, v_maxsyspri);
1030 1030 SAVE_INT32(ksi, var, v_clist);
1031 1031 SAVE_INT32(ksi, var, v_maxup);
1032 1032 SAVE_INT32(ksi, var, v_hbuf);
1033 1033 SAVE_INT32(ksi, var, v_hmask);
1034 1034 SAVE_INT32(ksi, var, v_pbuf);
1035 1035 SAVE_INT32(ksi, var, v_sptmap);
1036 1036 SAVE_INT32(ksi, var, v_maxpmem);
1037 1037 SAVE_INT32(ksi, var, v_autoup);
1038 1038 SAVE_INT32(ksi, var, v_bufhwm);
1039 1039 }
1040 1040
1041 1041 static void
1042 1042 save_ncstats(kstat_t *kp, ks_instance_t *ksi)
1043 1043 {
1044 1044 struct ncstats *ncstats = (struct ncstats *)(kp->ks_data);
1045 1045
1046 1046 assert(kp->ks_data_size == sizeof (struct ncstats));
1047 1047
1048 1048 SAVE_INT32(ksi, ncstats, hits);
1049 1049 SAVE_INT32(ksi, ncstats, misses);
1050 1050 SAVE_INT32(ksi, ncstats, enters);
1051 1051 SAVE_INT32(ksi, ncstats, dbl_enters);
1052 1052 SAVE_INT32(ksi, ncstats, long_enter);
1053 1053 SAVE_INT32(ksi, ncstats, long_look);
1054 1054 SAVE_INT32(ksi, ncstats, move_to_front);
1055 1055 SAVE_INT32(ksi, ncstats, purges);
1056 1056 }
1057 1057
1058 1058 static void
1059 1059 save_sysinfo(kstat_t *kp, ks_instance_t *ksi)
1060 1060 {
1061 1061 sysinfo_t *sysinfo = (sysinfo_t *)(kp->ks_data);
1062 1062
1063 1063 assert(kp->ks_data_size == sizeof (sysinfo_t));
1064 1064
1065 1065 SAVE_UINT32(ksi, sysinfo, updates);
1066 1066 SAVE_UINT32(ksi, sysinfo, runque);
1067 1067 SAVE_UINT32(ksi, sysinfo, runocc);
1068 1068 SAVE_UINT32(ksi, sysinfo, swpque);
1069 1069 SAVE_UINT32(ksi, sysinfo, swpocc);
1070 1070 SAVE_UINT32(ksi, sysinfo, waiting);
1071 1071 }
1072 1072
1073 1073 static void
1074 1074 save_vminfo(kstat_t *kp, ks_instance_t *ksi)
1075 1075 {
1076 1076 vminfo_t *vminfo = (vminfo_t *)(kp->ks_data);
1077 1077
1078 1078 assert(kp->ks_data_size == sizeof (vminfo_t));
1079 1079
1080 1080 SAVE_UINT64(ksi, vminfo, freemem);
1081 1081 SAVE_UINT64(ksi, vminfo, swap_resv);
1082 1082 SAVE_UINT64(ksi, vminfo, swap_alloc);
1083 1083 SAVE_UINT64(ksi, vminfo, swap_avail);
1084 1084 SAVE_UINT64(ksi, vminfo, swap_free);
1085 1085 SAVE_UINT64(ksi, vminfo, updates);
1086 1086 }
1087 1087
1088 1088 static void
1089 1089 save_nfs(kstat_t *kp, ks_instance_t *ksi)
1090 1090 {
1091 1091 struct mntinfo_kstat *mntinfo = (struct mntinfo_kstat *)(kp->ks_data);
1092 1092
1093 1093 assert(kp->ks_data_size == sizeof (struct mntinfo_kstat));
1094 1094
1095 1095 SAVE_STRING(ksi, mntinfo, mik_proto);
1096 1096 SAVE_UINT32(ksi, mntinfo, mik_vers);
1097 1097 SAVE_UINT32(ksi, mntinfo, mik_flags);
1098 1098 SAVE_UINT32(ksi, mntinfo, mik_secmod);
1099 1099 SAVE_UINT32(ksi, mntinfo, mik_curread);
1100 1100 SAVE_UINT32(ksi, mntinfo, mik_curwrite);
1101 1101 SAVE_INT32(ksi, mntinfo, mik_timeo);
1102 1102 SAVE_INT32(ksi, mntinfo, mik_retrans);
1103 1103 SAVE_UINT32(ksi, mntinfo, mik_acregmin);
1104 1104 SAVE_UINT32(ksi, mntinfo, mik_acregmax);
1105 1105 SAVE_UINT32(ksi, mntinfo, mik_acdirmin);
1106 1106 SAVE_UINT32(ksi, mntinfo, mik_acdirmax);
1107 1107 SAVE_UINT32_X(ksi, "lookup_srtt", mntinfo->mik_timers[0].srtt);
1108 1108 SAVE_UINT32_X(ksi, "lookup_deviate", mntinfo->mik_timers[0].deviate);
1109 1109 SAVE_UINT32_X(ksi, "lookup_rtxcur", mntinfo->mik_timers[0].rtxcur);
1110 1110 SAVE_UINT32_X(ksi, "read_srtt", mntinfo->mik_timers[1].srtt);
1111 1111 SAVE_UINT32_X(ksi, "read_deviate", mntinfo->mik_timers[1].deviate);
1112 1112 SAVE_UINT32_X(ksi, "read_rtxcur", mntinfo->mik_timers[1].rtxcur);
1113 1113 SAVE_UINT32_X(ksi, "write_srtt", mntinfo->mik_timers[2].srtt);
1114 1114 SAVE_UINT32_X(ksi, "write_deviate", mntinfo->mik_timers[2].deviate);
1115 1115 SAVE_UINT32_X(ksi, "write_rtxcur", mntinfo->mik_timers[2].rtxcur);
1116 1116 SAVE_UINT32(ksi, mntinfo, mik_noresponse);
1117 1117 SAVE_UINT32(ksi, mntinfo, mik_failover);
1118 1118 SAVE_UINT32(ksi, mntinfo, mik_remap);
1119 1119 SAVE_STRING(ksi, mntinfo, mik_curserver);
1120 1120 }
1121 1121
1122 1122 #ifdef __sparc
1123 1123 static void
1124 1124 save_sfmmu_global_stat(kstat_t *kp, ks_instance_t *ksi)
1125 1125 {
1126 1126 struct sfmmu_global_stat *sfmmug =
1127 1127 (struct sfmmu_global_stat *)(kp->ks_data);
1128 1128
1129 1129 assert(kp->ks_data_size == sizeof (struct sfmmu_global_stat));
1130 1130
1131 1131 SAVE_INT32(ksi, sfmmug, sf_tsb_exceptions);
1132 1132 SAVE_INT32(ksi, sfmmug, sf_tsb_raise_exception);
1133 1133 SAVE_INT32(ksi, sfmmug, sf_pagefaults);
1134 1134 SAVE_INT32(ksi, sfmmug, sf_uhash_searches);
1135 1135 SAVE_INT32(ksi, sfmmug, sf_uhash_links);
1136 1136 SAVE_INT32(ksi, sfmmug, sf_khash_searches);
1137 1137 SAVE_INT32(ksi, sfmmug, sf_khash_links);
1138 1138 SAVE_INT32(ksi, sfmmug, sf_swapout);
1139 1139 SAVE_INT32(ksi, sfmmug, sf_tsb_alloc);
1140 1140 SAVE_INT32(ksi, sfmmug, sf_tsb_allocfail);
1141 1141 SAVE_INT32(ksi, sfmmug, sf_tsb_sectsb_create);
1142 1142 SAVE_INT32(ksi, sfmmug, sf_scd_1sttsb_alloc);
1143 1143 SAVE_INT32(ksi, sfmmug, sf_scd_2ndtsb_alloc);
1144 1144 SAVE_INT32(ksi, sfmmug, sf_scd_1sttsb_allocfail);
1145 1145 SAVE_INT32(ksi, sfmmug, sf_scd_2ndtsb_allocfail);
1146 1146 SAVE_INT32(ksi, sfmmug, sf_tteload8k);
1147 1147 SAVE_INT32(ksi, sfmmug, sf_tteload64k);
1148 1148 SAVE_INT32(ksi, sfmmug, sf_tteload512k);
1149 1149 SAVE_INT32(ksi, sfmmug, sf_tteload4m);
1150 1150 SAVE_INT32(ksi, sfmmug, sf_tteload32m);
1151 1151 SAVE_INT32(ksi, sfmmug, sf_tteload256m);
1152 1152 SAVE_INT32(ksi, sfmmug, sf_tsb_load8k);
1153 1153 SAVE_INT32(ksi, sfmmug, sf_tsb_load4m);
1154 1154 SAVE_INT32(ksi, sfmmug, sf_hblk_hit);
1155 1155 SAVE_INT32(ksi, sfmmug, sf_hblk8_ncreate);
1156 1156 SAVE_INT32(ksi, sfmmug, sf_hblk8_nalloc);
1157 1157 SAVE_INT32(ksi, sfmmug, sf_hblk1_ncreate);
1158 1158 SAVE_INT32(ksi, sfmmug, sf_hblk1_nalloc);
1159 1159 SAVE_INT32(ksi, sfmmug, sf_hblk_slab_cnt);
1160 1160 SAVE_INT32(ksi, sfmmug, sf_hblk_reserve_cnt);
1161 1161 SAVE_INT32(ksi, sfmmug, sf_hblk_recurse_cnt);
1162 1162 SAVE_INT32(ksi, sfmmug, sf_hblk_reserve_hit);
1163 1163 SAVE_INT32(ksi, sfmmug, sf_get_free_success);
1164 1164 SAVE_INT32(ksi, sfmmug, sf_get_free_throttle);
1165 1165 SAVE_INT32(ksi, sfmmug, sf_get_free_fail);
1166 1166 SAVE_INT32(ksi, sfmmug, sf_put_free_success);
1167 1167 SAVE_INT32(ksi, sfmmug, sf_put_free_fail);
1168 1168 SAVE_INT32(ksi, sfmmug, sf_pgcolor_conflict);
1169 1169 SAVE_INT32(ksi, sfmmug, sf_uncache_conflict);
1170 1170 SAVE_INT32(ksi, sfmmug, sf_unload_conflict);
1171 1171 SAVE_INT32(ksi, sfmmug, sf_ism_uncache);
1172 1172 SAVE_INT32(ksi, sfmmug, sf_ism_recache);
1173 1173 SAVE_INT32(ksi, sfmmug, sf_recache);
1174 1174 SAVE_INT32(ksi, sfmmug, sf_steal_count);
1175 1175 SAVE_INT32(ksi, sfmmug, sf_pagesync);
1176 1176 SAVE_INT32(ksi, sfmmug, sf_clrwrt);
1177 1177 SAVE_INT32(ksi, sfmmug, sf_pagesync_invalid);
1178 1178 SAVE_INT32(ksi, sfmmug, sf_kernel_xcalls);
1179 1179 SAVE_INT32(ksi, sfmmug, sf_user_xcalls);
1180 1180 SAVE_INT32(ksi, sfmmug, sf_tsb_grow);
1181 1181 SAVE_INT32(ksi, sfmmug, sf_tsb_shrink);
1182 1182 SAVE_INT32(ksi, sfmmug, sf_tsb_resize_failures);
1183 1183 SAVE_INT32(ksi, sfmmug, sf_tsb_reloc);
1184 1184 SAVE_INT32(ksi, sfmmug, sf_user_vtop);
1185 1185 SAVE_INT32(ksi, sfmmug, sf_ctx_inv);
1186 1186 SAVE_INT32(ksi, sfmmug, sf_tlb_reprog_pgsz);
1187 1187 SAVE_INT32(ksi, sfmmug, sf_region_remap_demap);
1188 1188 SAVE_INT32(ksi, sfmmug, sf_create_scd);
1189 1189 SAVE_INT32(ksi, sfmmug, sf_join_scd);
1190 1190 SAVE_INT32(ksi, sfmmug, sf_leave_scd);
1191 1191 SAVE_INT32(ksi, sfmmug, sf_destroy_scd);
1192 1192 }
1193 1193 #endif
1194 1194
1195 1195 #ifdef __sparc
1196 1196 static void
1197 1197 save_sfmmu_tsbsize_stat(kstat_t *kp, ks_instance_t *ksi)
1198 1198 {
1199 1199 struct sfmmu_tsbsize_stat *sfmmut;
1200 1200
1201 1201 assert(kp->ks_data_size == sizeof (struct sfmmu_tsbsize_stat));
1202 1202 sfmmut = (struct sfmmu_tsbsize_stat *)(kp->ks_data);
1203 1203
1204 1204 SAVE_INT32(ksi, sfmmut, sf_tsbsz_8k);
1205 1205 SAVE_INT32(ksi, sfmmut, sf_tsbsz_16k);
1206 1206 SAVE_INT32(ksi, sfmmut, sf_tsbsz_32k);
1207 1207 SAVE_INT32(ksi, sfmmut, sf_tsbsz_64k);
1208 1208 SAVE_INT32(ksi, sfmmut, sf_tsbsz_128k);
1209 1209 SAVE_INT32(ksi, sfmmut, sf_tsbsz_256k);
1210 1210 SAVE_INT32(ksi, sfmmut, sf_tsbsz_512k);
1211 1211 SAVE_INT32(ksi, sfmmut, sf_tsbsz_1m);
1212 1212 SAVE_INT32(ksi, sfmmut, sf_tsbsz_2m);
1213 1213 SAVE_INT32(ksi, sfmmut, sf_tsbsz_4m);
1214 1214 }
1215 1215 #endif
1216 1216
1217 1217 #ifdef __sparc
1218 1218 static void
1219 1219 save_simmstat(kstat_t *kp, ks_instance_t *ksi)
1220 1220 {
1221 1221 uchar_t *simmstat;
1222 1222 char *simm_buf;
1223 1223 char *list = NULL;
1224 1224 int i;
1225 1225
1226 1226 assert(kp->ks_data_size == sizeof (uchar_t) * SIMM_COUNT);
1227 1227
1228 1228 for (i = 0, simmstat = (uchar_t *)(kp->ks_data); i < SIMM_COUNT - 1;
1229 1229 i++, simmstat++) {
1230 1230 if (list == NULL) {
1231 1231 (void) asprintf(&simm_buf, "%d,", *simmstat);
1232 1232 } else {
1233 1233 (void) asprintf(&simm_buf, "%s%d,", list, *simmstat);
1234 1234 free(list);
1235 1235 }
1236 1236 list = simm_buf;
1237 1237 }
1238 1238
1239 1239 (void) asprintf(&simm_buf, "%s%d", list, *simmstat);
1240 1240 SAVE_STRING_X(ksi, "status", simm_buf);
1241 1241 free(list);
1242 1242 free(simm_buf);
1243 1243 }
1244 1244 #endif
1245 1245
1246 1246 #ifdef __sparc
1247 1247 /*
1248 1248 * Helper function for save_temperature().
1249 1249 */
1250 1250 static char *
1251 1251 short_array_to_string(short *shortp, int len)
1252 1252 {
1253 1253 char *list = NULL;
1254 1254 char *list_buf;
1255 1255
1256 1256 for (; len > 1; len--, shortp++) {
1257 1257 if (list == NULL) {
1258 1258 (void) asprintf(&list_buf, "%hd,", *shortp);
1259 1259 } else {
1260 1260 (void) asprintf(&list_buf, "%s%hd,", list, *shortp);
1261 1261 free(list);
1262 1262 }
1263 1263 list = list_buf;
1264 1264 }
1265 1265
1266 1266 (void) asprintf(&list_buf, "%s%hd", list, *shortp);
1267 1267 free(list);
1268 1268 return (list_buf);
1269 1269 }
1270 1270
1271 1271 static void
1272 1272 save_temperature(kstat_t *kp, ks_instance_t *ksi)
1273 1273 {
1274 1274 struct temp_stats *temps = (struct temp_stats *)(kp->ks_data);
1275 1275 char *buf;
1276 1276
1277 1277 assert(kp->ks_data_size == sizeof (struct temp_stats));
1278 1278
1279 1279 SAVE_UINT32(ksi, temps, index);
1280 1280
1281 1281 buf = short_array_to_string(temps->l1, L1_SZ);
1282 1282 SAVE_STRING_X(ksi, "l1", buf);
1283 1283 free(buf);
1284 1284
1285 1285 buf = short_array_to_string(temps->l2, L2_SZ);
1286 1286 SAVE_STRING_X(ksi, "l2", buf);
1287 1287 free(buf);
1288 1288
1289 1289 buf = short_array_to_string(temps->l3, L3_SZ);
1290 1290 SAVE_STRING_X(ksi, "l3", buf);
1291 1291 free(buf);
1292 1292
1293 1293 buf = short_array_to_string(temps->l4, L4_SZ);
1294 1294 SAVE_STRING_X(ksi, "l4", buf);
1295 1295 free(buf);
1296 1296
1297 1297 buf = short_array_to_string(temps->l5, L5_SZ);
1298 1298 SAVE_STRING_X(ksi, "l5", buf);
1299 1299 free(buf);
1300 1300
1301 1301 SAVE_INT32(ksi, temps, max);
1302 1302 SAVE_INT32(ksi, temps, min);
1303 1303 SAVE_INT32(ksi, temps, state);
1304 1304 SAVE_INT32(ksi, temps, temp_cnt);
1305 1305 SAVE_INT32(ksi, temps, shutdown_cnt);
1306 1306 SAVE_INT32(ksi, temps, version);
1307 1307 SAVE_INT32(ksi, temps, trend);
1308 1308 SAVE_INT32(ksi, temps, override);
1309 1309 }
1310 1310 #endif
1311 1311
1312 1312 #ifdef __sparc
1313 1313 static void
1314 1314 save_temp_over(kstat_t *kp, ks_instance_t *ksi)
1315 1315 {
1316 1316 short *sh = (short *)(kp->ks_data);
1317 1317 char *value;
1318 1318
1319 1319 assert(kp->ks_data_size == sizeof (short));
1320 1320
1321 1321 (void) asprintf(&value, "%hu", *sh);
1322 1322 SAVE_STRING_X(ksi, "override", value);
1323 1323 free(value);
1324 1324 }
1325 1325 #endif
1326 1326
1327 1327 #ifdef __sparc
1328 1328 static void
1329 1329 save_ps_shadow(kstat_t *kp, ks_instance_t *ksi)
1330 1330 {
1331 1331 uchar_t *uchar = (uchar_t *)(kp->ks_data);
1332 1332
1333 1333 assert(kp->ks_data_size == SYS_PS_COUNT);
1334 1334
1335 1335 SAVE_CHAR_X(ksi, "core_0", *uchar++);
1336 1336 SAVE_CHAR_X(ksi, "core_1", *uchar++);
1337 1337 SAVE_CHAR_X(ksi, "core_2", *uchar++);
1338 1338 SAVE_CHAR_X(ksi, "core_3", *uchar++);
1339 1339 SAVE_CHAR_X(ksi, "core_4", *uchar++);
1340 1340 SAVE_CHAR_X(ksi, "core_5", *uchar++);
1341 1341 SAVE_CHAR_X(ksi, "core_6", *uchar++);
1342 1342 SAVE_CHAR_X(ksi, "core_7", *uchar++);
1343 1343 SAVE_CHAR_X(ksi, "pps_0", *uchar++);
1344 1344 SAVE_CHAR_X(ksi, "clk_33", *uchar++);
1345 1345 SAVE_CHAR_X(ksi, "clk_50", *uchar++);
1346 1346 SAVE_CHAR_X(ksi, "v5_p", *uchar++);
1347 1347 SAVE_CHAR_X(ksi, "v12_p", *uchar++);
1348 1348 SAVE_CHAR_X(ksi, "v5_aux", *uchar++);
1349 1349 SAVE_CHAR_X(ksi, "v5_p_pch", *uchar++);
1350 1350 SAVE_CHAR_X(ksi, "v12_p_pch", *uchar++);
1351 1351 SAVE_CHAR_X(ksi, "v3_pch", *uchar++);
1352 1352 SAVE_CHAR_X(ksi, "v5_pch", *uchar++);
1353 1353 SAVE_CHAR_X(ksi, "p_fan", *uchar++);
1354 1354 }
1355 1355 #endif
1356 1356
1357 1357 #ifdef __sparc
1358 1358 static void
1359 1359 save_fault_list(kstat_t *kp, ks_instance_t *ksi)
1360 1360 {
1361 1361 struct ft_list *fault;
1362 1362 char name[KSTAT_STRLEN + 7];
1363 1363 int i;
1364 1364
1365 1365 for (i = 1, fault = (struct ft_list *)(kp->ks_data);
1366 1366 i <= 999999 && i <= kp->ks_data_size / sizeof (struct ft_list);
1367 1367 i++, fault++) {
1368 1368 (void) snprintf(name, sizeof (name), "unit_%d", i);
1369 1369 SAVE_INT32_X(ksi, name, fault->unit);
1370 1370 (void) snprintf(name, sizeof (name), "type_%d", i);
1371 1371 SAVE_INT32_X(ksi, name, fault->type);
1372 1372 (void) snprintf(name, sizeof (name), "fclass_%d", i);
1373 1373 SAVE_INT32_X(ksi, name, fault->fclass);
1374 1374 (void) snprintf(name, sizeof (name), "create_time_%d", i);
1375 1375 SAVE_HRTIME_X(ksi, name, fault->create_time);
1376 1376 (void) snprintf(name, sizeof (name), "msg_%d", i);
1377 1377 SAVE_STRING_X(ksi, name, fault->msg);
1378 1378 }
1379 1379 }
1380 1380 #endif
1381 1381
1382 1382 static void
1383 1383 save_named(kstat_t *kp, ks_instance_t *ksi)
1384 1384 {
1385 1385 kstat_named_t *knp;
1386 1386 int n;
1387 1387
1388 1388 for (n = kp->ks_ndata, knp = KSTAT_NAMED_PTR(kp); n > 0; n--, knp++) {
1389 1389 switch (knp->data_type) {
1390 1390 case KSTAT_DATA_CHAR:
1391 1391 nvpair_insert(ksi, knp->name,
1392 1392 (ks_value_t *)&knp->value, KSTAT_DATA_CHAR);
1393 1393 break;
1394 1394 case KSTAT_DATA_INT32:
1395 1395 nvpair_insert(ksi, knp->name,
1396 1396 (ks_value_t *)&knp->value, KSTAT_DATA_INT32);
1397 1397 break;
1398 1398 case KSTAT_DATA_UINT32:
1399 1399 nvpair_insert(ksi, knp->name,
1400 1400 (ks_value_t *)&knp->value, KSTAT_DATA_UINT32);
1401 1401 break;
1402 1402 case KSTAT_DATA_INT64:
1403 1403 nvpair_insert(ksi, knp->name,
1404 1404 (ks_value_t *)&knp->value, KSTAT_DATA_INT64);
1405 1405 break;
1406 1406 case KSTAT_DATA_UINT64:
1407 1407 nvpair_insert(ksi, knp->name,
1408 1408 (ks_value_t *)&knp->value, KSTAT_DATA_UINT64);
1409 1409 break;
1410 1410 case KSTAT_DATA_STRING:
1411 1411 SAVE_STRING_X(ksi, knp->name, KSTAT_NAMED_STR_PTR(knp));
1412 1412 break;
1413 1413 default:
1414 1414 assert(B_FALSE); /* Invalid data type */
1415 1415 break;
1416 1416 }
1417 1417 }
1418 1418 }
1419 1419
1420 1420 static void
1421 1421 save_intr(kstat_t *kp, ks_instance_t *ksi)
1422 1422 {
1423 1423 kstat_intr_t *intr = KSTAT_INTR_PTR(kp);
1424 1424 char *intr_names[] = {"hard", "soft", "watchdog", "spurious",
1425 1425 "multiple_service"};
1426 1426 int n;
1427 1427
1428 1428 for (n = 0; n < KSTAT_NUM_INTRS; n++)
1429 1429 SAVE_UINT32_X(ksi, intr_names[n], intr->intrs[n]);
1430 1430 }
1431 1431
1432 1432 static void
1433 1433 save_io(kstat_t *kp, ks_instance_t *ksi)
1434 1434 {
1435 1435 kstat_io_t *ksio = KSTAT_IO_PTR(kp);
1436 1436
1437 1437 SAVE_UINT64(ksi, ksio, nread);
1438 1438 SAVE_UINT64(ksi, ksio, nwritten);
1439 1439 SAVE_UINT32(ksi, ksio, reads);
1440 1440 SAVE_UINT32(ksi, ksio, writes);
1441 1441 SAVE_HRTIME(ksi, ksio, wtime);
1442 1442 SAVE_HRTIME(ksi, ksio, wlentime);
1443 1443 SAVE_HRTIME(ksi, ksio, wlastupdate);
1444 1444 SAVE_HRTIME(ksi, ksio, rtime);
1445 1445 SAVE_HRTIME(ksi, ksio, rlentime);
1446 1446 SAVE_HRTIME(ksi, ksio, rlastupdate);
1447 1447 SAVE_UINT32(ksi, ksio, wcnt);
1448 1448 SAVE_UINT32(ksi, ksio, rcnt);
1449 1449 }
1450 1450
1451 1451 static void
1452 1452 save_timer(kstat_t *kp, ks_instance_t *ksi)
1453 1453 {
1454 1454 kstat_timer_t *ktimer = KSTAT_TIMER_PTR(kp);
1455 1455
1456 1456 SAVE_STRING(ksi, ktimer, name);
1457 1457 SAVE_UINT64(ksi, ktimer, num_events);
1458 1458 SAVE_HRTIME(ksi, ktimer, elapsed_time);
1459 1459 SAVE_HRTIME(ksi, ktimer, min_time);
1460 1460 SAVE_HRTIME(ksi, ktimer, max_time);
1461 1461 SAVE_HRTIME(ksi, ktimer, start_time);
1462 1462 SAVE_HRTIME(ksi, ktimer, stop_time);
1463 1463 }
↓ open down ↓ |
772 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX