1199767fToomas Soome/*
2199767fToomas Soome * CDDL HEADER START
3199767fToomas Soome *
4199767fToomas Soome * The contents of this file are subject to the terms of the
5199767fToomas Soome * Common Development and Distribution License (the "License").
6199767fToomas Soome * You may not use this file except in compliance with the License.
7199767fToomas Soome *
8199767fToomas Soome * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9199767fToomas Soome * or http://www.opensolaris.org/os/licensing.
10199767fToomas Soome * See the License for the specific language governing permissions
11199767fToomas Soome * and limitations under the License.
12199767fToomas Soome *
13199767fToomas Soome * When distributing Covered Code, include this CDDL HEADER in each
14199767fToomas Soome * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15199767fToomas Soome * If applicable, add the following below this CDDL HEADER, with the
161974da4Toomas Soome * fields enclosed by brackets "[]" replaced with your own identifying
17199767fToomas Soome * information: Portions Copyright [yyyy] [name of copyright owner]
18199767fToomas Soome *
19199767fToomas Soome * CDDL HEADER END
20199767fToomas Soome */
21199767fToomas Soome/*
22199767fToomas Soome * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23199767fToomas Soome * Use is subject to license terms.
24199767fToomas Soome */
25199767fToomas Soome
26199767fToomas Soome#include <stdio.h>
27199767fToomas Soome#include <stdarg.h>
28199767fToomas Soome#include <dtrace.h>
29199767fToomas Soome#include <errno.h>
30199767fToomas Soome#include <string.h>
31199767fToomas Soome#include <stdlib.h>
32199767fToomas Soome#include <unistd.h>
33199767fToomas Soome#include <limits.h>
34199767fToomas Soome#include <strings.h>
35199767fToomas Soome#include <termio.h>
36199767fToomas Soome#include <signal.h>
37199767fToomas Soome#include <locale.h>
38199767fToomas Soome
39199767fToomas Soome#include "statcommon.h"
40199767fToomas Soome
41199767fToomas Soome#define	INTRSTAT_COLUMN_OFFS		14
42199767fToomas Soome#define	INTRSTAT_COLUMNS_PER_CPU	15
43199767fToomas Soome#define	INTRSTAT_CPUS_PER_LINE(w)	\
44199767fToomas Soome	(((w) - INTRSTAT_COLUMN_OFFS) / INTRSTAT_COLUMNS_PER_CPU)
45199767fToomas Soome#define	INTRSTAT_OPTSTR			"x:c:C:T:"
46199767fToomas Soome
47199767fToomas Soomestatic uint_t timestamp_fmt = NODATE;
48199767fToomas Soome
49199767fToomas Soome#if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
50199767fToomas Soome#define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it isn't */
51199767fToomas Soome#endif
52199767fToomas Soome
53199767fToomas Soomestatic dtrace_hdl_t *g_dtp;
54199767fToomas Soomestatic int *g_present;
55199767fToomas Soomestatic int g_max_cpus;
56199767fToomas Soomestatic int g_start, g_end;
57199767fToomas Soomestatic int g_header;
58199767fToomas Soomestatic long g_sleeptime = 1;
59199767fToomas Soomestatic hrtime_t g_interval = NANOSEC;
60199767fToomas Soomestatic int g_intr;
61199767fToomas Soomestatic psetid_t g_pset = PS_NONE;
62199767fToomas Soomestatic processorid_t *g_pset_cpus;
63199767fToomas Soomestatic uint_t g_pset_ncpus;
64199767fToomas Soomestatic int g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(80);
65199767fToomas Soome
66199767fToomas Soomestatic const char *g_pname = "intrstat";
67199767fToomas Soomestatic const char *g_prog =
68199767fToomas Soome"interrupt-start"
69199767fToomas Soome"/arg0 != NULL/"
70199767fToomas Soome"{"
71199767fToomas Soome"	self->ts = vtimestamp;"
72199767fToomas Soome"}"
73199767fToomas Soome""
74199767fToomas Soome"interrupt-complete"
75199767fToomas Soome"/self->ts/"
76199767fToomas Soome"{"
77199767fToomas Soome"	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
85static void
86usage(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
95static void
96fatal(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*/
113static void
114intr(int signo)
115{
116	g_intr++;
117}
118
119static void
120status(void)
121{}
122
123static void
124set_width(void)
125{
126	struct winsize win;
127
128	if (!isatty(fileno(stdout)))
129		return;
130
131	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
132		return;
133
134	if (win.ws_col == 0) {
135		/*
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.)
139		 */
140		return;
141	}
142
143	g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(win.ws_col);
144
145	if (g_cpus_per_line < 1)
146		g_cpus_per_line = 1;
147}
148
149static void
150print_header()
151{
152	int i, j;
153	char c[256];
154
155	if (!g_header)
156		return;
157
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;
162
163		(void) sprintf(c, "cpu%d", i);
164		(void) printf(" %9s %%tim", c);
165
166		if (++j >= g_cpus_per_line)
167			break;
168	}
169
170	(void) printf("\n-------------+");
171
172	while (j--)
173		(void) printf("---------------");
174
175	(void) printf("\n");
176	g_header = 0;
177}
178
179/*ARGSUSED*/
180static int
181walk(const dtrace_aggdata_t *data, void *arg)
182{
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;
189
190	if (count == NULL) {
191		count = data;
192		return (DTRACE_AGGWALK_NEXT);
193	}
194
195	nrec = &aggdesc->dtagd_rec[1];
196	irec = &aggdesc->dtagd_rec[2];
197
198	name = data->dtada_data + nrec->dtrd_offset;
199	/* LINTED - alignment */
200	instance = (int32_t *)(data->dtada_data + irec->dtrd_offset);
201
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]));
207
208		if (!g_present[i])
209			continue;
210
211		if (j++ == 0) {
212			print_header();
213			(void) snprintf(c, sizeof (c), "%s#%d",
214			    name, *instance);
215			(void) printf("%12s |", c);
216		}
217
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);
222	}
223
224	(void) printf(j ? "\n" : "");
225	g_end = i;
226	count = NULL;
227	return (DTRACE_AGGWALK_NEXT);
228}
229
230static void
231select_cpu(processorid_t cpu)
232{
233	if (g_pset != PS_NONE)
234		fatal("cannot specify both a processor set and a processor\n");
235
236	if (cpu < 0 || cpu >= g_max_cpus)
237		fatal("cpu %d out of range\n", cpu);
238
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);
243	}
244
245	g_present[cpu] = 1;
246}
247
248static void
249select_cpus(processorid_t low, processorid_t high)
250{
251	if (g_pset != PS_NONE)
252		fatal("cannot specify both a processor set and processors\n");
253
254	if (low < 0 || low >= g_max_cpus)
255		fatal("invalid cpu '%d'\n", low);
256
257	if (high < 0 || high >= g_max_cpus)
258		fatal("invalid cpu '%d'\n", high);
259
260	if (low >= high)
261		fatal("invalid range '%d' to '%d'\n", low, high);
262
263	do {
264		if (p_online(low, P_STATUS) != -1)
265			g_present[low] = 1;
266	} while (++low <= high);
267}
268
269static void
270select_pset(psetid_t pset)
271{
272	processorid_t i;
273
274	if (pset < 0)
275		fatal("processor set %d is out of range\n", pset);
276
277	/*
278	 * Only one processor set can be specified.
279	 */
280	if (g_pset != PS_NONE)
281		fatal("at most one processor set may be specified\n");
282
283	/*
284	 * One cannot select processors _and_ a processor set.
285	 */
286	for (i = 0; i < g_max_cpus; i++)
287		if (g_present[i])
288			break;
289
290	if (i != g_max_cpus)
291		fatal("cannot specify both a processor and a processor set\n");
292
293	g_pset = pset;
294	g_pset_ncpus = g_max_cpus;
295
296	if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
297		fatal("invalid processor set: %d\n", g_pset);
298
299	if (g_pset_ncpus == 0)
300		fatal("processor set %d empty\n", g_pset);
301
302	for (i = 0; i < g_pset_ncpus; i++)
303		g_present[g_pset_cpus[i]] = 1;
304}
305
306static void
307check_pset(void)
308{
309	uint_t ncpus = g_max_cpus;
310	processorid_t i;
311
312	if (g_pset == PS_NONE)
313		return;
314
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);
318
319		fatal("couldn't get info for processor set %d", g_pset);
320	}
321
322	if (ncpus == 0)
323		fatal("processor set %d empty\n", g_pset);
324
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;
329		}
330
331		/*
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.
335		 */
336		if (i == g_pset_ncpus)
337			return;
338	}
339
340	/*
341	 * If we're here, we have a new processor set.  First, we need
342	 * to zero out the present array.
343	 */
344	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
345
346	g_pset_ncpus = ncpus;
347
348	for (i = 0; i < g_pset_ncpus; i++)
349		g_present[g_pset_cpus[i]] = 1;
350}
351
352int
353main(int argc, char **argv)
354{
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;
369
370	(void) setlocale(LC_ALL, "");
371	(void) textdomain(TEXT_DOMAIN);
372
373	(void) sigemptyset(&act.sa_mask);
374	act.sa_flags = 0;
375	act.sa_handler = set_width;
376	(void) sigaction(SIGWINCH, &act, NULL);
377
378	(void) sigemptyset(&act.sa_mask);
379	act.sa_flags = 0;
380	act.sa_handler = intr;
381	(void) sigaction(SIGUSR1, &act, NULL);
382
383	(void) sigemptyset(&act.sa_mask);
384	act.sa_flags = 0;
385	act.sa_handler = status;
386	(void) sigaction(SIGUSR2, &act, NULL);
387
388	act.sa_handler = set_width;
389	(void) sigaction(SIGWINCH, &act, NULL);
390	set_width();
391
392	(void) sigemptyset(&set);
393	(void) sigaddset(&set, SIGUSR1);
394	(void) sigaddset(&set, SIGWINCH);
395	(void) sigprocmask(SIG_BLOCK, &set, NULL);
396
397	ev.sigev_notify = SIGEV_SIGNAL;
398	ev.sigev_signo = SIGUSR1;
399
400	if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
401		fatal("cannot create CLOCK_HIGHRES timer");
402
403	g_max_cpus = sysconf(_SC_CPUID_MAX) + 1;
404
405	if ((g_present = malloc(sizeof (processorid_t) * g_max_cpus)) == NULL)
406		fatal("could not allocate g_present array\n");
407
408	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
409
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");
413
414	bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
415
416	while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
417		switch (c) {
418		case 'c': {
419			/*
420			 * We allow CPUs to be specified as an optionally
421			 * comma separated list of either CPU IDs or ranges
422			 * of CPU IDs.
423			 */
424			char *s = strtok(optarg, ",");
425
426			while (s != NULL) {
427				id = strtoul(s, &end, 0);
428
429				if (id == ULONG_MAX && errno == ERANGE) {
430					*end = '\0';
431					fatal("invalid cpu '%s'\n", s);
432				}
433
434				if (*(s = end) != '\0') {
435					processorid_t p;
436
437					if (*s != '-')
438						fatal("invalid cpu '%s'\n", s);
439					p = strtoul(++s, &end, 0);
440
441					if (*end != '\0' ||
442					    (p == ULONG_MAX && errno == ERANGE))
443						fatal("invalid cpu '%s'\n", s);
444
445					select_cpus(id, p);
446				} else {
447					select_cpu(id);
448				}
449
450				s = strtok(NULL, ",");
451			}
452
453			break;
454		}
455
456		case 'C': {
457			psetid_t pset = strtoul(optarg, &end, 0);
458
459			if (*end != '\0' ||
460			    (pset == ULONG_MAX && errno == ERANGE))
461				fatal("invalid processor set '%s'\n", optarg);
462
463			select_pset(pset);
464			break;
465		}
466
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();
477			}
478			break;
479
480		default:
481			if (strchr(INTRSTAT_OPTSTR, c) == NULL)
482				usage();
483		}
484	}
485
486	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
487		fatal("cannot open dtrace library: %s\n",
488		    dtrace_errmsg(NULL, err));
489	}
490
491	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
492	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
493		fatal("invalid program");
494
495	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
496		fatal("failed to enable probes");
497
498	if (dtrace_setopt(g_dtp, "aggsize", "128k") == -1)
499		fatal("failed to set 'aggsize'");
500
501	if (dtrace_setopt(g_dtp, "aggrate", "0") == -1)
502		fatal("failed to set 'aggrate'");
503
504	if (dtrace_setopt(g_dtp, "aggpercpu", 0) == -1)
505		fatal("failed to set 'aggpercpu'");
506
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';
513
514			if (dtrace_setopt(g_dtp, optarg, p) != 0)
515				fatal("failed to set -x %s", optarg);
516			break;
517		}
518	}
519
520	if (optind != argc) {
521		g_sleeptime = strtol(argv[optind], &end, 10);
522
523		if (*end != '\0' || g_sleeptime == 0)
524			fatal("invalid interval '%s'\n", argv[1]);
525
526		if (g_sleeptime <= 0)
527			fatal("interval must be greater than zero.\n");
528
529		if (g_sleeptime == LONG_MAX && errno == ERANGE)
530			fatal("invalid interval '%s'\n", argv[optind]);
531
532		if (++optind != argc) {
533			char *s = argv[optind];
534
535			iter = strtol(s, &end, 0);
536			indefinite = 0;
537
538			if (*end != '\0' || iter <= 0 ||
539			    (iter == LONG_MAX && errno == ERANGE))
540				fatal("invalid count '%s'\n", s);
541		}
542	}
543
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;
548
549	if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
550		fatal("cannot set time on CLOCK_REALTIME timer");
551
552	for (i = 0; i < g_max_cpus && !g_present[i]; i++)
553		continue;
554
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;
558	}
559
560	if (dtrace_go(g_dtp) != 0)
561		fatal("dtrace_go()");
562
563	last = gethrtime();
564
565	if (dtrace_getopt(g_dtp, "statusrate", &statustime) == -1)
566		fatal("failed to get 'statusrate'");
567
568	if (statustime < ((dtrace_optval_t)g_sleeptime * NANOSEC)) {
569		ev.sigev_notify = SIGEV_SIGNAL;
570		ev.sigev_signo = SIGUSR2;
571
572		if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
573			fatal("cannot create status timer");
574
575		ts.it_value.tv_sec = statustime / NANOSEC;
576		ts.it_value.tv_nsec = statustime % NANOSEC;
577		ts.it_interval = ts.it_value;
578
579		if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
580			fatal("cannot set time on status timer");
581	}
582
583	(void) sigemptyset(&set);
584
585	while (indefinite || iter) {
586
587		(void) sigsuspend(&set);
588
589		if (dtrace_status(g_dtp) == -1)
590			fatal("dtrace_status()");
591
592		if (g_intr == 0)
593			continue;
594
595		iter--;
596		g_intr--;
597		check_pset();
598
599		now = gethrtime();
600		g_interval = now - last;
601		last = now;
602
603		if (dtrace_aggregate_snap(g_dtp) != 0)
604			fatal("failed to add to aggregate");
605
606		g_start = g_end = 0;
607
608		if (timestamp_fmt != NODATE)
609			print_timestamp(timestamp_fmt);
610
611		do {
612			g_header = 1;
613
614			if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
615			    walk, NULL) != 0)
616				fatal("failed to sort aggregate");
617
618			if (g_start == g_end)
619				break;
620		} while ((g_start = g_end) < g_max_cpus);
621
622		dtrace_aggregate_clear(g_dtp);
623	}
624
625	return (0);
626}
627