1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <stdio.h> 30 #include <stdarg.h> 31 #include <dtrace.h> 32 #include <errno.h> 33 #include <string.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include <limits.h> 37 #include <strings.h> 38 #include <termio.h> 39 #include <signal.h> 40 41 #define INTRSTAT_COLUMN_OFFS 14 42 #define INTRSTAT_COLUMNS_PER_CPU 15 43 #define INTRSTAT_CPUS_PER_LINE(w) \ 44 (((w) - INTRSTAT_COLUMN_OFFS) / INTRSTAT_COLUMNS_PER_CPU) 45 46 static dtrace_hdl_t *g_dtp; 47 static int *g_present; 48 static int g_max_cpus; 49 static int g_start, g_end; 50 static int g_header; 51 static long g_sleeptime = 1; 52 static hrtime_t g_interval = NANOSEC; 53 static int g_intr; 54 static psetid_t g_pset = PS_NONE; 55 static processorid_t *g_pset_cpus; 56 static uint_t g_pset_ncpus; 57 static int g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(80); 58 59 static const char *g_pname = "intrstat"; 60 static const char *g_prog = 61 "interrupt-start" 62 "{" 63 " self->ts = vtimestamp;" 64 "}" 65 "" 66 "interrupt-complete" 67 "/self->ts/" 68 "{" 69 " this->devi = (struct dev_info *)arg0;" 70 " @counts[stringof(`devnamesp[this->devi->devi_major].dn_name)," 71 " this->devi->devi_instance] = count();" 72 " @times[stringof(`devnamesp[this->devi->devi_major].dn_name)," 73 " this->devi->devi_instance] = sum(vtimestamp - self->ts);" 74 " self->ts = 0;" 75 "}"; 76 77 static void 78 usage(void) 79 { 80 (void) fprintf(stderr, 81 "usage: intrstat [ -C psrset | -c cpulist ] " 82 "[interval [ count]]\n"); 83 84 exit(EXIT_FAILURE); 85 } 86 87 static void 88 fatal(const char *fmt, ...) 89 { 90 va_list ap; 91 92 va_start(ap, fmt); 93 94 (void) fprintf(stderr, "%s: ", g_pname); 95 (void) vfprintf(stderr, fmt, ap); 96 97 if (fmt[strlen(fmt) - 1] != '\n') 98 (void) fprintf(stderr, ": %s\n", 99 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 100 101 exit(EXIT_FAILURE); 102 } 103 104 /*ARGSUSED*/ 105 static void 106 intr(int signo) 107 { 108 g_intr++; 109 } 110 111 static void 112 status(void) 113 {} 114 115 static void 116 set_width(void) 117 { 118 struct winsize win; 119 120 if (!isatty(fileno(stdout))) 121 return; 122 123 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1) 124 return; 125 126 if (win.ws_col == 0) { 127 /* 128 * If TIOCGWINSZ returned 0 for the columns, just return -- 129 * thereby using the default value of g_cpus_per_line. (This 130 * happens, e.g., when running over a tip line.) 131 */ 132 return; 133 } 134 135 g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(win.ws_col); 136 137 if (g_cpus_per_line < 1) 138 g_cpus_per_line = 1; 139 } 140 141 static void 142 print_header() 143 { 144 int i, j; 145 char c[256]; 146 147 if (!g_header) 148 return; 149 150 (void) printf("\n%12s |", "device"); 151 for (i = g_start, j = 0; i < g_max_cpus; i++) { 152 if (!g_present[i]) 153 continue; 154 155 (void) sprintf(c, "cpu%d", i); 156 (void) printf(" %9s %%tim", c); 157 158 if (++j >= g_cpus_per_line) 159 break; 160 } 161 162 (void) printf("\n-------------+"); 163 164 while (j--) 165 (void) printf("---------------"); 166 167 (void) printf("\n"); 168 g_header = 0; 169 } 170 171 /*ARGSUSED*/ 172 static int 173 walk(dtrace_aggdata_t *data, void *arg) 174 { 175 dtrace_aggdesc_t *aggdesc = data->dtada_desc; 176 dtrace_recdesc_t *nrec, *irec; 177 char *name, c[256]; 178 int32_t *instance; 179 static dtrace_aggdata_t *count; 180 int i, j; 181 182 if (count == NULL) { 183 count = data; 184 return (DTRACE_AGGWALK_NEXT); 185 } 186 187 nrec = &aggdesc->dtagd_rec[1]; 188 irec = &aggdesc->dtagd_rec[2]; 189 190 name = data->dtada_data + nrec->dtrd_offset; 191 /* LINTED - alignment */ 192 instance = (int32_t *)(data->dtada_data + irec->dtrd_offset); 193 194 for (i = g_start, j = 0; i < g_max_cpus && j < g_cpus_per_line; i++) { 195 /* LINTED - alignment */ 196 uint64_t time = *((uint64_t *)(data->dtada_percpu[i])); 197 /* LINTED - alignment */ 198 uint64_t n = *((uint64_t *)(count->dtada_percpu[i])); 199 200 if (!g_present[i]) 201 continue; 202 203 if (j++ == 0) { 204 print_header(); 205 (void) snprintf(c, sizeof (c), "%s#%d", 206 name, *instance); 207 (void) printf("%12s |", c); 208 } 209 210 (void) printf(" %9lld %4.1f", 211 (unsigned long long)((double)n / 212 ((double)g_interval / (double)NANOSEC)), 213 ((double)time * (double)100.0) / (double)g_interval); 214 } 215 216 (void) printf(j ? "\n" : ""); 217 g_end = i; 218 count = NULL; 219 return (DTRACE_AGGWALK_NEXT); 220 } 221 222 static void 223 select_cpu(processorid_t cpu) 224 { 225 if (g_pset != PS_NONE) 226 fatal("cannot specify both a processor set and a processor\n"); 227 228 if (cpu < 0 || cpu >= g_max_cpus) 229 fatal("cpu %d out of range\n", cpu); 230 231 if (p_online(cpu, P_STATUS) == -1) { 232 if (errno != EINVAL) 233 fatal("could not get status for cpu %d", cpu); 234 fatal("cpu %d not present\n", cpu); 235 } 236 237 g_present[cpu] = 1; 238 } 239 240 static void 241 select_cpus(processorid_t low, processorid_t high) 242 { 243 if (g_pset != PS_NONE) 244 fatal("cannot specify both a processor set and processors\n"); 245 246 if (low < 0 || low >= g_max_cpus) 247 fatal("invalid cpu '%d'\n", low); 248 249 if (high < 0 || high >= g_max_cpus) 250 fatal("invalid cpu '%d'\n", high); 251 252 if (low >= high) 253 fatal("invalid range '%d' to '%d'\n", low, high); 254 255 do { 256 if (p_online(low, P_STATUS) != -1) 257 g_present[low] = 1; 258 } while (++low <= high); 259 } 260 261 static void 262 select_pset(psetid_t pset) 263 { 264 processorid_t i; 265 266 if (pset < 0) 267 fatal("processor set %d is out of range\n", pset); 268 269 /* 270 * Only one processor set can be specified. 271 */ 272 if (g_pset != PS_NONE) 273 fatal("at most one processor set may be specified\n"); 274 275 /* 276 * One cannot select processors _and_ a processor set. 277 */ 278 for (i = 0; i < g_max_cpus; i++) 279 if (g_present[i]) 280 break; 281 282 if (i != g_max_cpus) 283 fatal("cannot specify both a processor and a processor set\n"); 284 285 g_pset = pset; 286 g_pset_ncpus = g_max_cpus; 287 288 if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1) 289 fatal("invalid processor set: %d\n", g_pset); 290 291 if (g_pset_ncpus == 0) 292 fatal("processor set %d empty\n", g_pset); 293 294 for (i = 0; i < g_pset_ncpus; i++) 295 g_present[g_pset_cpus[i]] = 1; 296 } 297 298 static void 299 check_pset(void) 300 { 301 uint_t ncpus = g_max_cpus; 302 processorid_t i; 303 304 if (g_pset == PS_NONE) 305 return; 306 307 if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) { 308 if (errno == EINVAL) 309 fatal("processor set %d destroyed\n", g_pset); 310 311 fatal("couldn't get info for processor set %d", g_pset); 312 } 313 314 if (ncpus == 0) 315 fatal("processor set %d empty\n", g_pset); 316 317 if (ncpus == g_pset_ncpus) { 318 for (i = 0; i < g_pset_ncpus; i++) { 319 if (!g_present[g_pset_cpus[i]]) 320 break; 321 } 322 323 /* 324 * If the number of CPUs hasn't changed, and every CPU 325 * in the processor set is also selected, we know that the 326 * processor set itself hasn't changed. 327 */ 328 if (i == g_pset_ncpus) 329 return; 330 } 331 332 /* 333 * If we're here, we have a new processor set. First, we need 334 * to zero out the present array. 335 */ 336 bzero(g_present, sizeof (processorid_t) * g_max_cpus); 337 338 g_pset_ncpus = ncpus; 339 340 for (i = 0; i < g_pset_ncpus; i++) 341 g_present[g_pset_cpus[i]] = 1; 342 } 343 344 int 345 main(int argc, char **argv) 346 { 347 dtrace_prog_t *prog; 348 dtrace_proginfo_t info; 349 int err, i, indefinite = 1; 350 long iter; 351 processorid_t id; 352 struct sigaction act; 353 struct itimerspec ts; 354 struct sigevent ev; 355 sigset_t set; 356 timer_t tid; 357 char *end; 358 char c; 359 hrtime_t last, now; 360 dtrace_optval_t statustime; 361 362 (void) sigemptyset(&act.sa_mask); 363 act.sa_flags = 0; 364 act.sa_handler = set_width; 365 (void) sigaction(SIGWINCH, &act, NULL); 366 367 (void) sigemptyset(&act.sa_mask); 368 act.sa_flags = 0; 369 act.sa_handler = intr; 370 (void) sigaction(SIGUSR1, &act, NULL); 371 372 (void) sigemptyset(&act.sa_mask); 373 act.sa_flags = 0; 374 act.sa_handler = status; 375 (void) sigaction(SIGUSR2, &act, NULL); 376 377 act.sa_handler = set_width; 378 (void) sigaction(SIGWINCH, &act, NULL); 379 set_width(); 380 381 (void) sigemptyset(&set); 382 (void) sigaddset(&set, SIGUSR1); 383 (void) sigaddset(&set, SIGWINCH); 384 (void) sigprocmask(SIG_BLOCK, &set, NULL); 385 386 ev.sigev_notify = SIGEV_SIGNAL; 387 ev.sigev_signo = SIGUSR1; 388 389 if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1) 390 fatal("cannot create CLOCK_HIGHRES timer"); 391 392 g_max_cpus = sysconf(_SC_CPUID_MAX) + 1; 393 394 if ((g_present = malloc(sizeof (processorid_t) * g_max_cpus)) == NULL) 395 fatal("could not allocate g_present array\n"); 396 397 bzero(g_present, sizeof (processorid_t) * g_max_cpus); 398 399 g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus); 400 if (g_pset_cpus == NULL) 401 fatal("could not allocate g_pset_cpus"); 402 403 bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus); 404 405 while ((c = getopt(argc, argv, "c:C:")) != EOF) { 406 switch (c) { 407 case 'c': { 408 /* 409 * We allow CPUs to be specified as an optionally 410 * comma separated list of either CPU IDs or ranges 411 * of CPU IDs. 412 */ 413 char *s = strtok(optarg, ","); 414 415 while (s != NULL) { 416 id = strtoul(s, &end, 0); 417 418 if (id == ULONG_MAX && errno == ERANGE) { 419 *end = '\0'; 420 fatal("invalid cpu '%s'\n", s); 421 } 422 423 if (*(s = end) != '\0') { 424 processorid_t p; 425 426 if (*s != '-') 427 fatal("invalid cpu '%s'\n", s); 428 p = strtoul(++s, &end, 0); 429 430 if (*end != '\0' || 431 (p == ULONG_MAX && errno == ERANGE)) 432 fatal("invalid cpu '%s'\n", s); 433 434 select_cpus(id, p); 435 } else { 436 select_cpu(id); 437 } 438 439 s = strtok(NULL, ","); 440 } 441 442 break; 443 } 444 445 case 'C': { 446 psetid_t pset = strtoul(optarg, &end, 0); 447 448 if (*end != '\0' || 449 (pset == ULONG_MAX && errno == ERANGE)) 450 fatal("invalid processor set '%s'\n", optarg); 451 452 select_pset(pset); 453 break; 454 } 455 456 default: 457 usage(); 458 } 459 } 460 461 if (optind != argc) { 462 g_sleeptime = strtol(argv[optind], &end, 10); 463 464 if (*end != NULL || g_sleeptime == 0) 465 fatal("invalid interval '%s'\n", argv[1]); 466 467 if (g_sleeptime <= 0) 468 fatal("interval must be greater than zero.\n"); 469 470 if (g_sleeptime == LONG_MAX && errno == ERANGE) 471 fatal("invalid interval '%s'\n", argv[optind]); 472 473 if (++optind != argc) { 474 char *s = argv[optind]; 475 476 iter = strtol(s, &end, 0); 477 indefinite = 0; 478 479 if (*end != '\0' || iter <= 0 || 480 (iter == LONG_MAX && errno == ERANGE)) 481 fatal("invalid count '%s'\n", s); 482 } 483 } 484 485 ts.it_value.tv_sec = g_sleeptime; 486 ts.it_value.tv_nsec = 0; 487 ts.it_interval.tv_sec = g_sleeptime; 488 ts.it_interval.tv_nsec = 0; 489 490 if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1) 491 fatal("cannot set time on CLOCK_REALTIME timer"); 492 493 for (i = 0; i < g_max_cpus && !g_present[i]; i++) 494 continue; 495 496 if (i == g_max_cpus) { 497 for (i = 0; i < g_max_cpus; i++) 498 g_present[i] = p_online(i, P_STATUS) == -1 ? 0 : 1; 499 } 500 501 if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) { 502 fatal("cannot open dtrace library: %s\n", 503 dtrace_errmsg(NULL, err)); 504 } 505 506 if ((prog = dtrace_program_strcompile(g_dtp, g_prog, 507 DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL) 508 fatal("invalid program"); 509 510 if (dtrace_program_exec(g_dtp, prog, &info) == -1) 511 fatal("failed to enable probes"); 512 513 if (dtrace_setopt(g_dtp, "aggsize", "128k") == -1) 514 fatal("failed to set 'aggsize'"); 515 516 if (dtrace_setopt(g_dtp, "aggrate", "0") == -1) 517 fatal("failed to set 'aggrate'"); 518 519 if (dtrace_setopt(g_dtp, "aggpercpu", 0) == -1) 520 fatal("failed to set 'aggpercpu'"); 521 522 if (dtrace_go(g_dtp) != 0) 523 fatal("dtrace_go()"); 524 525 last = gethrtime(); 526 527 if (dtrace_getopt(g_dtp, "statusrate", &statustime) == -1) 528 fatal("failed to get 'statusrate'"); 529 530 if (statustime < ((dtrace_optval_t)g_sleeptime * NANOSEC)) { 531 ev.sigev_notify = SIGEV_SIGNAL; 532 ev.sigev_signo = SIGUSR2; 533 534 if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1) 535 fatal("cannot create status timer"); 536 537 ts.it_value.tv_sec = statustime / NANOSEC; 538 ts.it_value.tv_nsec = statustime % NANOSEC; 539 ts.it_interval = ts.it_value; 540 541 if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1) 542 fatal("cannot set time on status timer"); 543 } 544 545 (void) sigemptyset(&set); 546 547 while (indefinite || iter) { 548 549 (void) sigsuspend(&set); 550 551 (void) dtrace_status(g_dtp); 552 553 if (g_intr == 0) 554 continue; 555 556 iter--; 557 g_intr--; 558 check_pset(); 559 560 now = gethrtime(); 561 g_interval = now - last; 562 last = now; 563 564 if (dtrace_aggregate_snap(g_dtp) != 0) 565 fatal("failed to add to aggregate"); 566 567 g_start = g_end = 0; 568 569 do { 570 g_header = 1; 571 572 if (dtrace_aggregate_walk_keyvarsorted(g_dtp, 573 walk, NULL) != 0) 574 fatal("failed to sort aggregate"); 575 576 if (g_start == g_end) 577 break; 578 } while ((g_start = g_end) < g_max_cpus); 579 580 dtrace_aggregate_clear(g_dtp); 581 } 582 583 return (0); 584 } 585