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