dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / intrstat / intrstat.c
blob4af202506ecaa14b5cfcb8682d28aacbf54ee79a
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <dtrace.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <strings.h>
35 #include <termio.h>
36 #include <signal.h>
37 #include <locale.h>
39 #include "statcommon.h"
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 #define INTRSTAT_OPTSTR "x:c:C:T:"
47 static uint_t timestamp_fmt = NODATE;
49 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
50 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't */
51 #endif
53 static dtrace_hdl_t *g_dtp;
54 static int *g_present;
55 static int g_max_cpus;
56 static int g_start, g_end;
57 static int g_header;
58 static long g_sleeptime = 1;
59 static hrtime_t g_interval = NANOSEC;
60 static int g_intr;
61 static psetid_t g_pset = PS_NONE;
62 static processorid_t *g_pset_cpus;
63 static uint_t g_pset_ncpus;
64 static int g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(80);
66 static const char *g_pname = "intrstat";
67 static const char *g_prog =
68 "interrupt-start"
69 "/arg0 != NULL/"
70 "{"
71 " self->ts = vtimestamp;"
72 "}"
74 "interrupt-complete"
75 "/self->ts/"
76 "{"
77 " this->devi = (struct dev_info *)arg0;"
78 " @counts[stringof(`devnamesp[this->devi->devi_major].dn_name),"
79 " this->devi->devi_instance] = count();"
80 " @times[stringof(`devnamesp[this->devi->devi_major].dn_name),"
81 " this->devi->devi_instance] = sum(vtimestamp - self->ts);"
82 " self->ts = 0;"
83 "}";
85 static void
86 usage(void)
88 (void) fprintf(stderr,
89 "usage: intrstat [ -C psrset | -c cpulist ] [-x opt[=val]] "
90 "[-T d|u] [interval [ count]]\n");
92 exit(EXIT_FAILURE);
95 static void
96 fatal(const char *fmt, ...)
98 va_list ap;
100 va_start(ap, fmt);
102 (void) fprintf(stderr, "%s: ", g_pname);
103 (void) vfprintf(stderr, fmt, ap);
105 if (fmt[strlen(fmt) - 1] != '\n')
106 (void) fprintf(stderr, ": %s\n",
107 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
109 exit(EXIT_FAILURE);
112 /*ARGSUSED*/
113 static void
114 intr(int signo)
116 g_intr++;
119 static void
120 status(void)
123 static void
124 set_width(void)
126 struct winsize win;
128 if (!isatty(fileno(stdout)))
129 return;
131 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
132 return;
134 if (win.ws_col == 0) {
136 * If TIOCGWINSZ returned 0 for the columns, just return --
137 * thereby using the default value of g_cpus_per_line. (This
138 * happens, e.g., when running over a tip line.)
140 return;
143 g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(win.ws_col);
145 if (g_cpus_per_line < 1)
146 g_cpus_per_line = 1;
149 static void
150 print_header()
152 int i, j;
153 char c[256];
155 if (!g_header)
156 return;
158 (void) printf("\n%12s |", "device");
159 for (i = g_start, j = 0; i < g_max_cpus; i++) {
160 if (!g_present[i])
161 continue;
163 (void) sprintf(c, "cpu%d", i);
164 (void) printf(" %9s %%tim", c);
166 if (++j >= g_cpus_per_line)
167 break;
170 (void) printf("\n-------------+");
172 while (j--)
173 (void) printf("---------------");
175 (void) printf("\n");
176 g_header = 0;
179 /*ARGSUSED*/
180 static int
181 walk(const dtrace_aggdata_t *data, void *arg)
183 dtrace_aggdesc_t *aggdesc = data->dtada_desc;
184 dtrace_recdesc_t *nrec, *irec;
185 char *name, c[256];
186 int32_t *instance;
187 static const dtrace_aggdata_t *count;
188 int i, j;
190 if (count == NULL) {
191 count = data;
192 return (DTRACE_AGGWALK_NEXT);
195 nrec = &aggdesc->dtagd_rec[1];
196 irec = &aggdesc->dtagd_rec[2];
198 name = data->dtada_data + nrec->dtrd_offset;
199 /* LINTED - alignment */
200 instance = (int32_t *)(data->dtada_data + irec->dtrd_offset);
202 for (i = g_start, j = 0; i < g_max_cpus && j < g_cpus_per_line; i++) {
203 /* LINTED - alignment */
204 uint64_t time = *((uint64_t *)(data->dtada_percpu[i]));
205 /* LINTED - alignment */
206 uint64_t n = *((uint64_t *)(count->dtada_percpu[i]));
208 if (!g_present[i])
209 continue;
211 if (j++ == 0) {
212 print_header();
213 (void) snprintf(c, sizeof (c), "%s#%d",
214 name, *instance);
215 (void) printf("%12s |", c);
218 (void) printf(" %9lld %4.1f",
219 (unsigned long long)((double)n /
220 ((double)g_interval / (double)NANOSEC)),
221 ((double)time * (double)100.0) / (double)g_interval);
224 (void) printf(j ? "\n" : "");
225 g_end = i;
226 count = NULL;
227 return (DTRACE_AGGWALK_NEXT);
230 static void
231 select_cpu(processorid_t cpu)
233 if (g_pset != PS_NONE)
234 fatal("cannot specify both a processor set and a processor\n");
236 if (cpu < 0 || cpu >= g_max_cpus)
237 fatal("cpu %d out of range\n", cpu);
239 if (p_online(cpu, P_STATUS) == -1) {
240 if (errno != EINVAL)
241 fatal("could not get status for cpu %d", cpu);
242 fatal("cpu %d not present\n", cpu);
245 g_present[cpu] = 1;
248 static void
249 select_cpus(processorid_t low, processorid_t high)
251 if (g_pset != PS_NONE)
252 fatal("cannot specify both a processor set and processors\n");
254 if (low < 0 || low >= g_max_cpus)
255 fatal("invalid cpu '%d'\n", low);
257 if (high < 0 || high >= g_max_cpus)
258 fatal("invalid cpu '%d'\n", high);
260 if (low >= high)
261 fatal("invalid range '%d' to '%d'\n", low, high);
263 do {
264 if (p_online(low, P_STATUS) != -1)
265 g_present[low] = 1;
266 } while (++low <= high);
269 static void
270 select_pset(psetid_t pset)
272 processorid_t i;
274 if (pset < 0)
275 fatal("processor set %d is out of range\n", pset);
278 * Only one processor set can be specified.
280 if (g_pset != PS_NONE)
281 fatal("at most one processor set may be specified\n");
284 * One cannot select processors _and_ a processor set.
286 for (i = 0; i < g_max_cpus; i++)
287 if (g_present[i])
288 break;
290 if (i != g_max_cpus)
291 fatal("cannot specify both a processor and a processor set\n");
293 g_pset = pset;
294 g_pset_ncpus = g_max_cpus;
296 if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
297 fatal("invalid processor set: %d\n", g_pset);
299 if (g_pset_ncpus == 0)
300 fatal("processor set %d empty\n", g_pset);
302 for (i = 0; i < g_pset_ncpus; i++)
303 g_present[g_pset_cpus[i]] = 1;
306 static void
307 check_pset(void)
309 uint_t ncpus = g_max_cpus;
310 processorid_t i;
312 if (g_pset == PS_NONE)
313 return;
315 if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) {
316 if (errno == EINVAL)
317 fatal("processor set %d destroyed\n", g_pset);
319 fatal("couldn't get info for processor set %d", g_pset);
322 if (ncpus == 0)
323 fatal("processor set %d empty\n", g_pset);
325 if (ncpus == g_pset_ncpus) {
326 for (i = 0; i < g_pset_ncpus; i++) {
327 if (!g_present[g_pset_cpus[i]])
328 break;
332 * If the number of CPUs hasn't changed, and every CPU
333 * in the processor set is also selected, we know that the
334 * processor set itself hasn't changed.
336 if (i == g_pset_ncpus)
337 return;
341 * If we're here, we have a new processor set. First, we need
342 * to zero out the present array.
344 bzero(g_present, sizeof (processorid_t) * g_max_cpus);
346 g_pset_ncpus = ncpus;
348 for (i = 0; i < g_pset_ncpus; i++)
349 g_present[g_pset_cpus[i]] = 1;
353 main(int argc, char **argv)
355 dtrace_prog_t *prog;
356 dtrace_proginfo_t info;
357 int err, i, indefinite = 1;
358 long iter;
359 processorid_t id;
360 struct sigaction act;
361 struct itimerspec ts;
362 struct sigevent ev;
363 sigset_t set;
364 timer_t tid;
365 char *end, *p;
366 char c;
367 hrtime_t last, now;
368 dtrace_optval_t statustime;
370 (void) setlocale(LC_ALL, "");
371 (void) textdomain(TEXT_DOMAIN);
373 (void) sigemptyset(&act.sa_mask);
374 act.sa_flags = 0;
375 act.sa_handler = set_width;
376 (void) sigaction(SIGWINCH, &act, NULL);
378 (void) sigemptyset(&act.sa_mask);
379 act.sa_flags = 0;
380 act.sa_handler = intr;
381 (void) sigaction(SIGUSR1, &act, NULL);
383 (void) sigemptyset(&act.sa_mask);
384 act.sa_flags = 0;
385 act.sa_handler = status;
386 (void) sigaction(SIGUSR2, &act, NULL);
388 act.sa_handler = set_width;
389 (void) sigaction(SIGWINCH, &act, NULL);
390 set_width();
392 (void) sigemptyset(&set);
393 (void) sigaddset(&set, SIGUSR1);
394 (void) sigaddset(&set, SIGWINCH);
395 (void) sigprocmask(SIG_BLOCK, &set, NULL);
397 ev.sigev_notify = SIGEV_SIGNAL;
398 ev.sigev_signo = SIGUSR1;
400 if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
401 fatal("cannot create CLOCK_HIGHRES timer");
403 g_max_cpus = sysconf(_SC_CPUID_MAX) + 1;
405 if ((g_present = malloc(sizeof (processorid_t) * g_max_cpus)) == NULL)
406 fatal("could not allocate g_present array\n");
408 bzero(g_present, sizeof (processorid_t) * g_max_cpus);
410 g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus);
411 if (g_pset_cpus == NULL)
412 fatal("could not allocate g_pset_cpus");
414 bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
416 while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
417 switch (c) {
418 case 'c': {
420 * We allow CPUs to be specified as an optionally
421 * comma separated list of either CPU IDs or ranges
422 * of CPU IDs.
424 char *s = strtok(optarg, ",");
426 while (s != NULL) {
427 id = strtoul(s, &end, 0);
429 if (id == ULONG_MAX && errno == ERANGE) {
430 *end = '\0';
431 fatal("invalid cpu '%s'\n", s);
434 if (*(s = end) != '\0') {
435 processorid_t p;
437 if (*s != '-')
438 fatal("invalid cpu '%s'\n", s);
439 p = strtoul(++s, &end, 0);
441 if (*end != '\0' ||
442 (p == ULONG_MAX && errno == ERANGE))
443 fatal("invalid cpu '%s'\n", s);
445 select_cpus(id, p);
446 } else {
447 select_cpu(id);
450 s = strtok(NULL, ",");
453 break;
456 case 'C': {
457 psetid_t pset = strtoul(optarg, &end, 0);
459 if (*end != '\0' ||
460 (pset == ULONG_MAX && errno == ERANGE))
461 fatal("invalid processor set '%s'\n", optarg);
463 select_pset(pset);
464 break;
467 case 'T':
468 if (optarg) {
469 if (*optarg == 'u')
470 timestamp_fmt = UDATE;
471 else if (*optarg == 'd')
472 timestamp_fmt = DDATE;
473 else
474 usage();
475 } else {
476 usage();
478 break;
480 default:
481 if (strchr(INTRSTAT_OPTSTR, c) == NULL)
482 usage();
486 if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
487 fatal("cannot open dtrace library: %s\n",
488 dtrace_errmsg(NULL, err));
491 if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
492 DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
493 fatal("invalid program");
495 if (dtrace_program_exec(g_dtp, prog, &info) == -1)
496 fatal("failed to enable probes");
498 if (dtrace_setopt(g_dtp, "aggsize", "128k") == -1)
499 fatal("failed to set 'aggsize'");
501 if (dtrace_setopt(g_dtp, "aggrate", "0") == -1)
502 fatal("failed to set 'aggrate'");
504 if (dtrace_setopt(g_dtp, "aggpercpu", 0) == -1)
505 fatal("failed to set 'aggpercpu'");
507 optind = 1;
508 while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
509 switch (c) {
510 case 'x':
511 if ((p = strchr(optarg, '=')) != NULL)
512 *p++ = '\0';
514 if (dtrace_setopt(g_dtp, optarg, p) != 0)
515 fatal("failed to set -x %s", optarg);
516 break;
520 if (optind != argc) {
521 g_sleeptime = strtol(argv[optind], &end, 10);
523 if (*end != '\0'|| g_sleeptime == 0)
524 fatal("invalid interval '%s'\n", argv[1]);
526 if (g_sleeptime <= 0)
527 fatal("interval must be greater than zero.\n");
529 if (g_sleeptime == LONG_MAX && errno == ERANGE)
530 fatal("invalid interval '%s'\n", argv[optind]);
532 if (++optind != argc) {
533 char *s = argv[optind];
535 iter = strtol(s, &end, 0);
536 indefinite = 0;
538 if (*end != '\0' || iter <= 0 ||
539 (iter == LONG_MAX && errno == ERANGE))
540 fatal("invalid count '%s'\n", s);
544 ts.it_value.tv_sec = g_sleeptime;
545 ts.it_value.tv_nsec = 0;
546 ts.it_interval.tv_sec = g_sleeptime;
547 ts.it_interval.tv_nsec = 0;
549 if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
550 fatal("cannot set time on CLOCK_REALTIME timer");
552 for (i = 0; i < g_max_cpus && !g_present[i]; i++)
553 continue;
555 if (i == g_max_cpus) {
556 for (i = 0; i < g_max_cpus; i++)
557 g_present[i] = p_online(i, P_STATUS) == -1 ? 0 : 1;
560 if (dtrace_go(g_dtp) != 0)
561 fatal("dtrace_go()");
563 last = gethrtime();
565 if (dtrace_getopt(g_dtp, "statusrate", &statustime) == -1)
566 fatal("failed to get 'statusrate'");
568 if (statustime < ((dtrace_optval_t)g_sleeptime * NANOSEC)) {
569 ev.sigev_notify = SIGEV_SIGNAL;
570 ev.sigev_signo = SIGUSR2;
572 if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
573 fatal("cannot create status timer");
575 ts.it_value.tv_sec = statustime / NANOSEC;
576 ts.it_value.tv_nsec = statustime % NANOSEC;
577 ts.it_interval = ts.it_value;
579 if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
580 fatal("cannot set time on status timer");
583 (void) sigemptyset(&set);
585 while (indefinite || iter) {
587 (void) sigsuspend(&set);
589 if (dtrace_status(g_dtp) == -1)
590 fatal("dtrace_status()");
592 if (g_intr == 0)
593 continue;
595 iter--;
596 g_intr--;
597 check_pset();
599 now = gethrtime();
600 g_interval = now - last;
601 last = now;
603 if (dtrace_aggregate_snap(g_dtp) != 0)
604 fatal("failed to add to aggregate");
606 g_start = g_end = 0;
608 if (timestamp_fmt != NODATE)
609 print_timestamp(timestamp_fmt);
611 do {
612 g_header = 1;
614 if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
615 walk, NULL) != 0)
616 fatal("failed to sort aggregate");
618 if (g_start == g_end)
619 break;
620 } while ((g_start = g_end) < g_max_cpus);
622 dtrace_aggregate_clear(g_dtp);
625 return (0);