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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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 */
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
;
58 static long g_sleeptime
= 1;
59 static hrtime_t g_interval
= NANOSEC
;
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
=
71 " self->ts = vtimestamp;"
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);"
88 (void) fprintf(stderr
,
89 "usage: intrstat [ -C psrset | -c cpulist ] [-x opt[=val]] "
90 "[-T d|u] [interval [ count]]\n");
96 fatal(const char *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
)));
128 if (!isatty(fileno(stdout
)))
131 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &win
) == -1)
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.)
143 g_cpus_per_line
= INTRSTAT_CPUS_PER_LINE(win
.ws_col
);
145 if (g_cpus_per_line
< 1)
158 (void) printf("\n%12s |", "device");
159 for (i
= g_start
, j
= 0; i
< g_max_cpus
; i
++) {
163 (void) sprintf(c
, "cpu%d", i
);
164 (void) printf(" %9s %%tim", c
);
166 if (++j
>= g_cpus_per_line
)
170 (void) printf("\n-------------+");
173 (void) printf("---------------");
181 walk(const dtrace_aggdata_t
*data
, void *arg
)
183 dtrace_aggdesc_t
*aggdesc
= data
->dtada_desc
;
184 dtrace_recdesc_t
*nrec
, *irec
;
187 static const dtrace_aggdata_t
*count
;
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
]));
213 (void) snprintf(c
, sizeof (c
), "%s#%d",
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" : "");
227 return (DTRACE_AGGWALK_NEXT
);
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) {
241 fatal("could not get status for cpu %d", cpu
);
242 fatal("cpu %d not present\n", cpu
);
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
);
261 fatal("invalid range '%d' to '%d'\n", low
, high
);
264 if (p_online(low
, P_STATUS
) != -1)
266 } while (++low
<= high
);
270 select_pset(psetid_t pset
)
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
++)
291 fatal("cannot specify both a processor and a processor set\n");
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;
309 uint_t ncpus
= g_max_cpus
;
312 if (g_pset
== PS_NONE
)
315 if (pset_info(g_pset
, NULL
, &ncpus
, g_pset_cpus
) == -1) {
317 fatal("processor set %d destroyed\n", g_pset
);
319 fatal("couldn't get info for processor set %d", g_pset
);
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
]])
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
)
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
)
356 dtrace_proginfo_t info
;
357 int err
, i
, indefinite
= 1;
360 struct sigaction act
;
361 struct itimerspec ts
;
368 dtrace_optval_t statustime
;
370 (void) setlocale(LC_ALL
, "");
371 (void) textdomain(TEXT_DOMAIN
);
373 (void) sigemptyset(&act
.sa_mask
);
375 act
.sa_handler
= set_width
;
376 (void) sigaction(SIGWINCH
, &act
, NULL
);
378 (void) sigemptyset(&act
.sa_mask
);
380 act
.sa_handler
= intr
;
381 (void) sigaction(SIGUSR1
, &act
, NULL
);
383 (void) sigemptyset(&act
.sa_mask
);
385 act
.sa_handler
= status
;
386 (void) sigaction(SIGUSR2
, &act
, NULL
);
388 act
.sa_handler
= set_width
;
389 (void) sigaction(SIGWINCH
, &act
, NULL
);
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
) {
420 * We allow CPUs to be specified as an optionally
421 * comma separated list of either CPU IDs or ranges
424 char *s
= strtok(optarg
, ",");
427 id
= strtoul(s
, &end
, 0);
429 if (id
== ULONG_MAX
&& errno
== ERANGE
) {
431 fatal("invalid cpu '%s'\n", s
);
434 if (*(s
= end
) != '\0') {
438 fatal("invalid cpu '%s'\n", s
);
439 p
= strtoul(++s
, &end
, 0);
442 (p
== ULONG_MAX
&& errno
== ERANGE
))
443 fatal("invalid cpu '%s'\n", s
);
450 s
= strtok(NULL
, ",");
457 psetid_t pset
= strtoul(optarg
, &end
, 0);
460 (pset
== ULONG_MAX
&& errno
== ERANGE
))
461 fatal("invalid processor set '%s'\n", optarg
);
470 timestamp_fmt
= UDATE
;
471 else if (*optarg
== 'd')
472 timestamp_fmt
= DDATE
;
481 if (strchr(INTRSTAT_OPTSTR
, c
) == NULL
)
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'");
508 while ((c
= getopt(argc
, argv
, INTRSTAT_OPTSTR
)) != EOF
) {
511 if ((p
= strchr(optarg
, '=')) != NULL
)
514 if (dtrace_setopt(g_dtp
, optarg
, p
) != 0)
515 fatal("failed to set -x %s", optarg
);
520 if (optind
!= argc
) {
521 g_sleeptime
= strtol(argv
[optind
], &end
, 10);
523 if (*end
!= NULL
|| 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);
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
++)
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()");
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()");
600 g_interval
= now
- last
;
603 if (dtrace_aggregate_snap(g_dtp
) != 0)
604 fatal("failed to add to aggregate");
608 if (timestamp_fmt
!= NODATE
)
609 print_timestamp(timestamp_fmt
);
614 if (dtrace_aggregate_walk_keyvarsorted(g_dtp
,
616 fatal("failed to sort aggregate");
618 if (g_start
== g_end
)
620 } while ((g_start
= g_end
) < g_max_cpus
);
622 dtrace_aggregate_clear(g_dtp
);