Home | History | Annotate | Line # | Download | only in tprof
tprof_top.c revision 1.7
      1  1.7  ryo /*	$NetBSD: tprof_top.c,v 1.7 2022/12/16 08:02:04 ryo Exp $	*/
      2  1.1  ryo 
      3  1.1  ryo /*-
      4  1.1  ryo  * Copyright (c) 2022 Ryo Shimizu <ryo (at) nerv.org>
      5  1.1  ryo  * All rights reserved.
      6  1.1  ryo  *
      7  1.1  ryo  * Redistribution and use in source and binary forms, with or without
      8  1.1  ryo  * modification, are permitted provided that the following conditions
      9  1.1  ryo  * are met:
     10  1.1  ryo  * 1. Redistributions of source code must retain the above copyright
     11  1.1  ryo  *    notice, this list of conditions and the following disclaimer.
     12  1.1  ryo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  ryo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  ryo  *    documentation and/or other materials provided with the distribution.
     15  1.1  ryo  *
     16  1.1  ryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  1.1  ryo  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  1.1  ryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  ryo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  1.1  ryo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  1.1  ryo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.1  ryo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  ryo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  1.1  ryo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25  1.1  ryo  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  ryo  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  ryo  */
     28  1.1  ryo 
     29  1.1  ryo #include <sys/cdefs.h>
     30  1.1  ryo #ifndef lint
     31  1.7  ryo __RCSID("$NetBSD: tprof_top.c,v 1.7 2022/12/16 08:02:04 ryo Exp $");
     32  1.1  ryo #endif /* not lint */
     33  1.1  ryo 
     34  1.1  ryo #include <sys/param.h>
     35  1.1  ryo #include <sys/types.h>
     36  1.4  ryo #include <sys/ioctl.h>
     37  1.1  ryo #include <sys/rbtree.h>
     38  1.4  ryo #include <sys/select.h>
     39  1.1  ryo #include <sys/time.h>
     40  1.1  ryo 
     41  1.4  ryo #include <assert.h>
     42  1.1  ryo #include <err.h>
     43  1.1  ryo #include <errno.h>
     44  1.1  ryo #include <fcntl.h>
     45  1.1  ryo #include <inttypes.h>
     46  1.1  ryo #include <math.h>
     47  1.1  ryo #include <signal.h>
     48  1.1  ryo #include <stdio.h>
     49  1.1  ryo #include <stdlib.h>
     50  1.1  ryo #include <string.h>
     51  1.4  ryo #include <term.h>
     52  1.4  ryo #include <termios.h>
     53  1.1  ryo #include <unistd.h>
     54  1.1  ryo #include <util.h>
     55  1.1  ryo 
     56  1.1  ryo #include <dev/tprof/tprof_ioctl.h>
     57  1.1  ryo #include "tprof.h"
     58  1.1  ryo #include "ksyms.h"
     59  1.1  ryo 
     60  1.3  ryo #define SAMPLE_MODE_ACCUMULATIVE	0
     61  1.3  ryo #define SAMPLE_MODE_INSTANTANEOUS	1
     62  1.3  ryo #define SAMPLE_MODE_NUM			2
     63  1.3  ryo 
     64  1.4  ryo #define LINESTR	"-------------------------------------------------------------"
     65  1.4  ryo #define SYMBOL_LEN			32	/* symbol and event name */
     66  1.4  ryo 
     67  1.3  ryo struct sample_elm {
     68  1.3  ryo 	struct rb_node node;
     69  1.3  ryo 	uint64_t addr;
     70  1.3  ryo 	const char *name;
     71  1.3  ryo 	uint32_t flags;
     72  1.3  ryo #define SAMPLE_ELM_FLAGS_USER	0x00000001
     73  1.3  ryo 	uint32_t num[SAMPLE_MODE_NUM];
     74  1.3  ryo 	uint32_t num_cpu[];	/* [SAMPLE_MODE_NUM][ncpu] */
     75  1.3  ryo #define SAMPLE_ELM_NUM_CPU(e, k)		\
     76  1.3  ryo 	((e)->num_cpu + (k) *  ncpu)
     77  1.3  ryo };
     78  1.3  ryo 
     79  1.3  ryo struct ptrarray {
     80  1.3  ryo 	void **pa_ptrs;
     81  1.3  ryo 	size_t pa_allocnum;
     82  1.3  ryo 	size_t pa_inuse;
     83  1.3  ryo };
     84  1.3  ryo 
     85  1.3  ryo static int opt_mode = SAMPLE_MODE_INSTANTANEOUS;
     86  1.3  ryo static int opt_userland = 0;
     87  1.3  ryo static int opt_showcounter = 0;
     88  1.3  ryo 
     89  1.3  ryo /* for display */
     90  1.4  ryo static char *term;
     91  1.3  ryo static struct winsize win;
     92  1.3  ryo static int nontty;
     93  1.4  ryo static struct termios termios_save;
     94  1.4  ryo static bool termios_saved;
     95  1.3  ryo static long top_interval = 1;
     96  1.4  ryo static bool do_redraw;
     97  1.4  ryo static u_int nshow;
     98  1.3  ryo 
     99  1.3  ryo /* for profiling and counting samples */
    100  1.3  ryo static sig_atomic_t sigalrm;
    101  1.1  ryo static struct sym **ksyms;
    102  1.1  ryo static size_t nksyms;
    103  1.1  ryo static u_int nevent;
    104  1.1  ryo static const char *eventname[TPROF_MAXCOUNTERS];
    105  1.3  ryo static size_t sizeof_sample_elm;
    106  1.3  ryo static rb_tree_t rb_tree_sample;
    107  1.3  ryo struct ptrarray sample_list[SAMPLE_MODE_NUM];
    108  1.3  ryo static u_int sample_n_kern[SAMPLE_MODE_NUM];
    109  1.3  ryo static u_int sample_n_user[SAMPLE_MODE_NUM];
    110  1.4  ryo static u_int sample_event_width = 7;
    111  1.4  ryo static u_int *sample_cpu_width;					/* [ncpu] */
    112  1.3  ryo static uint32_t *sample_n_kern_per_cpu[SAMPLE_MODE_NUM];	/* [ncpu] */
    113  1.3  ryo static uint32_t *sample_n_user_per_cpu[SAMPLE_MODE_NUM];	/* [ncpu] */
    114  1.3  ryo static uint64_t *sample_n_per_event[SAMPLE_MODE_NUM];		/* [nevent] */
    115  1.3  ryo static uint64_t *sample_n_per_event_cpu[SAMPLE_MODE_NUM];	/* [ncpu] */
    116  1.3  ryo 
    117  1.3  ryo /* raw event counter */
    118  1.3  ryo static uint64_t *counters;	/* counters[2][ncpu][nevent] */
    119  1.3  ryo static u_int counters_i;
    120  1.3  ryo 
    121  1.3  ryo static const char *
    122  1.3  ryo cycle_event_name(void)
    123  1.3  ryo {
    124  1.3  ryo 	const char *cycleevent;
    125  1.3  ryo 
    126  1.3  ryo 	switch (tprof_info.ti_ident) {
    127  1.3  ryo 	case TPROF_IDENT_INTEL_GENERIC:
    128  1.3  ryo 		cycleevent = "unhalted-core-cycles";
    129  1.3  ryo 		break;
    130  1.3  ryo 	case TPROF_IDENT_AMD_GENERIC:
    131  1.3  ryo 		cycleevent = "LsNotHaltedCyc";
    132  1.3  ryo 		break;
    133  1.3  ryo 	case TPROF_IDENT_ARMV8_GENERIC:
    134  1.3  ryo 	case TPROF_IDENT_ARMV7_GENERIC:
    135  1.3  ryo 		cycleevent = "CPU_CYCLES";
    136  1.3  ryo 		break;
    137  1.3  ryo 	default:
    138  1.3  ryo 		cycleevent = NULL;
    139  1.3  ryo 		break;
    140  1.3  ryo 	}
    141  1.3  ryo 	return cycleevent;
    142  1.3  ryo }
    143  1.1  ryo 
    144  1.1  ryo static void
    145  1.4  ryo reset_cursor_pos(void)
    146  1.1  ryo {
    147  1.4  ryo 	int i;
    148  1.4  ryo 	char *p;
    149  1.4  ryo 
    150  1.4  ryo 	if (nontty || term == NULL)
    151  1.1  ryo 		return;
    152  1.4  ryo 
    153  1.4  ryo 	printf("\r");
    154  1.4  ryo 
    155  1.4  ryo 	/* cursor_up * n */
    156  1.4  ryo 	if ((p = tigetstr("cuu")) != NULL) {
    157  1.6  ryo 		putp(tparm(p, win.ws_row - 1, 0, 0, 0, 0, 0, 0, 0, 0));
    158  1.4  ryo 	} else if ((p = tigetstr("cuu1")) != NULL) {
    159  1.4  ryo 		for (i = win.ws_row - 1; i > 0; i--)
    160  1.6  ryo 			putp(p);
    161  1.4  ryo 	}
    162  1.1  ryo }
    163  1.1  ryo 
    164  1.1  ryo static void
    165  1.4  ryo clr_to_eol(void)
    166  1.1  ryo {
    167  1.4  ryo 	char *p;
    168  1.4  ryo 
    169  1.4  ryo 	if (nontty || term == NULL)
    170  1.1  ryo 		return;
    171  1.4  ryo 
    172  1.4  ryo 	if ((p = tigetstr("el")) != NULL)
    173  1.6  ryo 		putp(p);
    174  1.1  ryo }
    175  1.1  ryo 
    176  1.4  ryo /* newline, and clearing to end of line if needed */
    177  1.1  ryo static void
    178  1.4  ryo lim_newline(int *lim)
    179  1.1  ryo {
    180  1.4  ryo 	if (*lim >= 1)
    181  1.4  ryo 		clr_to_eol();
    182  1.4  ryo 
    183  1.4  ryo 	printf("\n");
    184  1.4  ryo 	*lim = win.ws_col;
    185  1.1  ryo }
    186  1.1  ryo 
    187  1.4  ryo static int
    188  1.4  ryo lim_printf(int *lim, const char *fmt, ...)
    189  1.1  ryo {
    190  1.4  ryo 	va_list ap;
    191  1.4  ryo 	size_t written;
    192  1.4  ryo 	char *p;
    193  1.4  ryo 
    194  1.4  ryo 	if (*lim <= 0)
    195  1.4  ryo 		return 0;
    196  1.4  ryo 
    197  1.4  ryo 	p = alloca(*lim + 1);
    198  1.4  ryo 
    199  1.4  ryo 	va_start(ap, fmt);
    200  1.4  ryo 	vsnprintf(p, *lim + 1, fmt, ap);
    201  1.4  ryo 	va_end(ap);
    202  1.4  ryo 
    203  1.4  ryo 	written = strlen(p);
    204  1.4  ryo 	if (written == 0) {
    205  1.4  ryo 		*lim = 0;
    206  1.4  ryo 		return 0;
    207  1.4  ryo 	}
    208  1.4  ryo 
    209  1.4  ryo 	fwrite(p, written, 1, stdout);
    210  1.4  ryo 	*lim -= written;
    211  1.4  ryo 
    212  1.4  ryo 	return written;
    213  1.1  ryo }
    214  1.1  ryo 
    215  1.1  ryo static void
    216  1.1  ryo sigwinch_handler(int signo)
    217  1.1  ryo {
    218  1.4  ryo 	char *p;
    219  1.4  ryo 
    220  1.4  ryo 	win.ws_col = tigetnum("lines");
    221  1.4  ryo 	win.ws_row = tigetnum("cols");
    222  1.4  ryo 
    223  1.1  ryo 	nontty = ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
    224  1.4  ryo 	if (nontty != 0) {
    225  1.4  ryo 		nontty = !isatty(STDOUT_FILENO);
    226  1.4  ryo 		win.ws_col = 65535;
    227  1.4  ryo 		win.ws_row = 65535;
    228  1.4  ryo 	}
    229  1.4  ryo 
    230  1.4  ryo 	if ((p = getenv("LINES")) != NULL)
    231  1.4  ryo 		win.ws_row = strtoul(p, NULL, 0);
    232  1.4  ryo 	if ((p = getenv("COLUMNS")) != NULL)
    233  1.4  ryo 		win.ws_col = strtoul(p, NULL, 0);
    234  1.4  ryo 
    235  1.4  ryo 	do_redraw = true;
    236  1.4  ryo }
    237  1.4  ryo 
    238  1.4  ryo static void
    239  1.4  ryo tty_setup(void)
    240  1.4  ryo {
    241  1.4  ryo 	struct termios termios;
    242  1.4  ryo 
    243  1.4  ryo 	term = getenv("TERM");
    244  1.4  ryo 	if (term != NULL)
    245  1.4  ryo 		setupterm(term, 0, NULL);
    246  1.4  ryo 
    247  1.4  ryo 	sigwinch_handler(0);
    248  1.4  ryo 
    249  1.4  ryo 	if (tcgetattr(STDOUT_FILENO, &termios_save) == 0) {
    250  1.4  ryo 		termios_saved = true;
    251  1.4  ryo 
    252  1.4  ryo 		/* stty cbreak */
    253  1.4  ryo 		termios = termios_save;
    254  1.4  ryo 		termios.c_iflag |= BRKINT|IXON|IMAXBEL;
    255  1.4  ryo 		termios.c_oflag |= OPOST;
    256  1.4  ryo 		termios.c_lflag |= ISIG|IEXTEN;
    257  1.4  ryo 		termios.c_lflag &= ~(ICANON|ECHO);
    258  1.4  ryo 		tcsetattr(STDOUT_FILENO, TCSADRAIN, &termios);
    259  1.4  ryo 	}
    260  1.4  ryo }
    261  1.4  ryo 
    262  1.4  ryo static void
    263  1.4  ryo tty_restore(void)
    264  1.4  ryo {
    265  1.4  ryo 	if (termios_saved) {
    266  1.4  ryo 		tcsetattr(STDOUT_FILENO, TCSADRAIN, &termios_save);
    267  1.4  ryo 		termios_saved = false;
    268  1.4  ryo 	}
    269  1.4  ryo }
    270  1.4  ryo 
    271  1.4  ryo static void
    272  1.4  ryo sigtstp_handler(int signo)
    273  1.4  ryo {
    274  1.4  ryo 	tty_restore();
    275  1.4  ryo 
    276  1.4  ryo 	signal(SIGWINCH, SIG_DFL);
    277  1.4  ryo 	signal(SIGINT, SIG_DFL);
    278  1.4  ryo 	signal(SIGQUIT, SIG_DFL);
    279  1.4  ryo 	signal(SIGTERM, SIG_DFL);
    280  1.4  ryo 	signal(SIGTSTP, SIG_DFL);
    281  1.4  ryo 	kill(0, SIGTSTP);
    282  1.4  ryo 	nshow = 0;
    283  1.1  ryo }
    284  1.1  ryo 
    285  1.1  ryo static void
    286  1.1  ryo sigalrm_handler(int signo)
    287  1.1  ryo {
    288  1.1  ryo 	sigalrm = 1;
    289  1.1  ryo }
    290  1.1  ryo 
    291  1.6  ryo __dead static void
    292  1.4  ryo die(int signo)
    293  1.4  ryo {
    294  1.4  ryo 	tty_restore();
    295  1.4  ryo 	printf("\n");
    296  1.4  ryo 
    297  1.4  ryo 	exit(EXIT_SUCCESS);
    298  1.4  ryo }
    299  1.4  ryo 
    300  1.6  ryo __dead static void
    301  1.4  ryo die_errc(int status, int code, const char *fmt, ...)
    302  1.4  ryo {
    303  1.4  ryo 	va_list ap;
    304  1.4  ryo 
    305  1.4  ryo 	tty_restore();
    306  1.4  ryo 
    307  1.4  ryo 	va_start(ap, fmt);
    308  1.4  ryo 	if (code == 0)
    309  1.4  ryo 		verrx(status, fmt, ap);
    310  1.4  ryo 	else
    311  1.4  ryo 		verrc(status, code, fmt, ap);
    312  1.4  ryo 	va_end(ap);
    313  1.4  ryo }
    314  1.4  ryo 
    315  1.4  ryo static void
    316  1.3  ryo ptrarray_push(struct ptrarray *ptrarray, void *ptr)
    317  1.3  ryo {
    318  1.3  ryo 	int error;
    319  1.3  ryo 
    320  1.3  ryo 	if (ptrarray->pa_inuse >= ptrarray->pa_allocnum) {
    321  1.3  ryo 		/* increase buffer */
    322  1.3  ryo 		ptrarray->pa_allocnum += 1024;
    323  1.3  ryo 		error = reallocarr(&ptrarray->pa_ptrs, ptrarray->pa_allocnum,
    324  1.3  ryo 		    sizeof(*ptrarray->pa_ptrs));
    325  1.3  ryo 		if (error != 0)
    326  1.4  ryo 			die_errc(EXIT_FAILURE, error, "rellocarr failed");
    327  1.3  ryo 	}
    328  1.3  ryo 	ptrarray->pa_ptrs[ptrarray->pa_inuse++] = ptr;
    329  1.3  ryo }
    330  1.3  ryo 
    331  1.3  ryo static void
    332  1.3  ryo ptrarray_iterate(struct ptrarray *ptrarray, void (*ifunc)(void *))
    333  1.3  ryo {
    334  1.3  ryo 	size_t i;
    335  1.1  ryo 
    336  1.3  ryo 	for (i = 0; i < ptrarray->pa_inuse; i++) {
    337  1.3  ryo 		(*ifunc)(ptrarray->pa_ptrs[i]);
    338  1.3  ryo 	}
    339  1.3  ryo }
    340  1.1  ryo 
    341  1.3  ryo static void
    342  1.3  ryo ptrarray_clear(struct ptrarray *ptrarray)
    343  1.3  ryo {
    344  1.3  ryo 	ptrarray->pa_inuse = 0;
    345  1.3  ryo }
    346  1.1  ryo 
    347  1.1  ryo static int
    348  1.1  ryo sample_compare_key(void *ctx, const void *n1, const void *keyp)
    349  1.1  ryo {
    350  1.1  ryo 	const struct sample_elm *a1 = n1;
    351  1.1  ryo 	const struct sample_elm *a2 = (const struct sample_elm *)keyp;
    352  1.1  ryo 	return a1->addr - a2->addr;
    353  1.1  ryo }
    354  1.1  ryo 
    355  1.1  ryo static signed int
    356  1.1  ryo sample_compare_nodes(void *ctx, const void *n1, const void *n2)
    357  1.1  ryo {
    358  1.1  ryo 	const struct addr *a2 = n2;
    359  1.1  ryo 	return sample_compare_key(ctx, n1, a2);
    360  1.1  ryo }
    361  1.1  ryo 
    362  1.1  ryo static const rb_tree_ops_t sample_ops = {
    363  1.1  ryo 	.rbto_compare_nodes = sample_compare_nodes,
    364  1.1  ryo 	.rbto_compare_key = sample_compare_key
    365  1.1  ryo };
    366  1.1  ryo 
    367  1.3  ryo static u_int
    368  1.3  ryo n_align(u_int n, u_int align)
    369  1.3  ryo {
    370  1.3  ryo 	return (n + align - 1) / align * align;
    371  1.3  ryo }
    372  1.3  ryo 
    373  1.1  ryo static void
    374  1.1  ryo sample_init(void)
    375  1.1  ryo {
    376  1.3  ryo 	const struct sample_elm *e;
    377  1.4  ryo 	int l, mode, n;
    378  1.4  ryo 	u_int size;
    379  1.4  ryo 	char buf[16];
    380  1.3  ryo 
    381  1.4  ryo 	size = sizeof(struct sample_elm) +
    382  1.3  ryo 	    sizeof(e->num_cpu[0]) * SAMPLE_MODE_NUM * ncpu;
    383  1.3  ryo 	sizeof_sample_elm = n_align(size, __alignof(struct sample_elm));
    384  1.3  ryo 
    385  1.4  ryo 	sample_cpu_width = ecalloc(1, sizeof(*sample_cpu_width) * ncpu);
    386  1.4  ryo 	for (n = 0; n < ncpu; n++) {
    387  1.4  ryo 		sample_cpu_width[n] = 5;
    388  1.4  ryo 		l = snprintf(buf, sizeof(buf), "CPU%d", n);
    389  1.4  ryo 		if (sample_cpu_width[n] < (u_int)l)
    390  1.4  ryo 			sample_cpu_width[n] = l;
    391  1.4  ryo 	}
    392  1.4  ryo 
    393  1.3  ryo 	for (mode = 0; mode < SAMPLE_MODE_NUM; mode++) {
    394  1.3  ryo 		sample_n_kern_per_cpu[mode] = ecalloc(1,
    395  1.3  ryo 		    sizeof(typeof(*sample_n_kern_per_cpu[mode])) * ncpu);
    396  1.3  ryo 		sample_n_user_per_cpu[mode] = ecalloc(1,
    397  1.3  ryo 		    sizeof(typeof(*sample_n_user_per_cpu[mode])) * ncpu);
    398  1.3  ryo 		sample_n_per_event[mode] = ecalloc(1,
    399  1.3  ryo 		    sizeof(typeof(*sample_n_per_event[mode])) * nevent);
    400  1.3  ryo 		sample_n_per_event_cpu[mode] = ecalloc(1,
    401  1.3  ryo 		    sizeof(typeof(*sample_n_per_event_cpu[mode])) *
    402  1.3  ryo 		    nevent * ncpu);
    403  1.3  ryo 	}
    404  1.1  ryo }
    405  1.1  ryo 
    406  1.1  ryo static void
    407  1.3  ryo sample_clear_instantaneous(void *arg)
    408  1.1  ryo {
    409  1.3  ryo 	struct sample_elm *e = (void *)arg;
    410  1.1  ryo 
    411  1.3  ryo 	e->num[SAMPLE_MODE_INSTANTANEOUS] = 0;
    412  1.3  ryo 	memset(SAMPLE_ELM_NUM_CPU(e, SAMPLE_MODE_INSTANTANEOUS),
    413  1.3  ryo 	    0, sizeof(e->num_cpu[0]) * ncpu);
    414  1.1  ryo }
    415  1.1  ryo 
    416  1.3  ryo static void
    417  1.3  ryo sample_reset(bool reset_accumulative)
    418  1.1  ryo {
    419  1.3  ryo 	int mode;
    420  1.3  ryo 
    421  1.3  ryo 	for (mode = 0; mode < SAMPLE_MODE_NUM; mode++) {
    422  1.3  ryo 		if (mode == SAMPLE_MODE_ACCUMULATIVE && !reset_accumulative)
    423  1.3  ryo 			continue;
    424  1.1  ryo 
    425  1.3  ryo 		sample_n_kern[mode] = 0;
    426  1.3  ryo 		sample_n_user[mode] = 0;
    427  1.3  ryo 		memset(sample_n_kern_per_cpu[mode], 0,
    428  1.3  ryo 		    sizeof(typeof(*sample_n_kern_per_cpu[mode])) * ncpu);
    429  1.3  ryo 		memset(sample_n_user_per_cpu[mode], 0,
    430  1.3  ryo 		    sizeof(typeof(*sample_n_user_per_cpu[mode])) * ncpu);
    431  1.3  ryo 		memset(sample_n_per_event[mode], 0,
    432  1.3  ryo 		    sizeof(typeof(*sample_n_per_event[mode])) * nevent);
    433  1.3  ryo 		memset(sample_n_per_event_cpu[mode], 0,
    434  1.3  ryo 		    sizeof(typeof(*sample_n_per_event_cpu[mode])) *
    435  1.3  ryo 		    nevent * ncpu);
    436  1.1  ryo 	}
    437  1.1  ryo 
    438  1.3  ryo 	if (reset_accumulative) {
    439  1.3  ryo 		rb_tree_init(&rb_tree_sample, &sample_ops);
    440  1.3  ryo 		ptrarray_iterate(&sample_list[SAMPLE_MODE_ACCUMULATIVE], free);
    441  1.3  ryo 		ptrarray_clear(&sample_list[SAMPLE_MODE_ACCUMULATIVE]);
    442  1.3  ryo 		ptrarray_clear(&sample_list[SAMPLE_MODE_INSTANTANEOUS]);
    443  1.3  ryo 	} else {
    444  1.3  ryo 		ptrarray_iterate(&sample_list[SAMPLE_MODE_INSTANTANEOUS],
    445  1.3  ryo 		    sample_clear_instantaneous);
    446  1.3  ryo 		ptrarray_clear(&sample_list[SAMPLE_MODE_INSTANTANEOUS]);
    447  1.3  ryo 	}
    448  1.1  ryo }
    449  1.1  ryo 
    450  1.3  ryo static int __unused
    451  1.3  ryo sample_sortfunc_accumulative(const void *a, const void *b)
    452  1.1  ryo {
    453  1.3  ryo 	struct sample_elm * const *ea = a;
    454  1.3  ryo 	struct sample_elm * const *eb = b;
    455  1.3  ryo 	return (*eb)->num[SAMPLE_MODE_ACCUMULATIVE] -
    456  1.3  ryo 	    (*ea)->num[SAMPLE_MODE_ACCUMULATIVE];
    457  1.1  ryo }
    458  1.1  ryo 
    459  1.1  ryo static int
    460  1.3  ryo sample_sortfunc_instantaneous(const void *a, const void *b)
    461  1.1  ryo {
    462  1.3  ryo 	struct sample_elm * const *ea = a;
    463  1.3  ryo 	struct sample_elm * const *eb = b;
    464  1.3  ryo 	return (*eb)->num[SAMPLE_MODE_INSTANTANEOUS] -
    465  1.3  ryo 	    (*ea)->num[SAMPLE_MODE_INSTANTANEOUS];
    466  1.1  ryo }
    467  1.1  ryo 
    468  1.1  ryo static void
    469  1.3  ryo sample_sort_accumulative(void)
    470  1.1  ryo {
    471  1.3  ryo 	qsort(sample_list[SAMPLE_MODE_ACCUMULATIVE].pa_ptrs,
    472  1.3  ryo 	    sample_list[SAMPLE_MODE_ACCUMULATIVE].pa_inuse,
    473  1.3  ryo 	    sizeof(struct sample_elm *), sample_sortfunc_accumulative);
    474  1.3  ryo }
    475  1.3  ryo 
    476  1.3  ryo static void
    477  1.3  ryo sample_sort_instantaneous(void)
    478  1.3  ryo {
    479  1.3  ryo 	qsort(sample_list[SAMPLE_MODE_INSTANTANEOUS].pa_ptrs,
    480  1.3  ryo 	    sample_list[SAMPLE_MODE_INSTANTANEOUS].pa_inuse,
    481  1.3  ryo 	    sizeof(struct sample_elm *), sample_sortfunc_instantaneous);
    482  1.1  ryo }
    483  1.1  ryo 
    484  1.1  ryo static void
    485  1.1  ryo sample_collect(tprof_sample_t *s)
    486  1.1  ryo {
    487  1.1  ryo 	struct sample_elm *e, *o;
    488  1.1  ryo 	const char *name;
    489  1.1  ryo 	size_t symid;
    490  1.1  ryo 	uint64_t addr, offset;
    491  1.1  ryo 	uint32_t flags = 0;
    492  1.1  ryo 	uint32_t eventid, cpuid;
    493  1.3  ryo 	int mode;
    494  1.1  ryo 
    495  1.1  ryo 	eventid = __SHIFTOUT(s->s_flags, TPROF_SAMPLE_COUNTER_MASK);
    496  1.1  ryo 	cpuid = s->s_cpuid;
    497  1.1  ryo 
    498  1.3  ryo 	if (eventid >= nevent)	/* unknown event from tprof? */
    499  1.3  ryo 		return;
    500  1.3  ryo 
    501  1.3  ryo 	for (mode = 0; mode < SAMPLE_MODE_NUM; mode++) {
    502  1.3  ryo 		sample_n_per_event[mode][eventid]++;
    503  1.3  ryo 		sample_n_per_event_cpu[mode][nevent * cpuid + eventid]++;
    504  1.3  ryo 	}
    505  1.1  ryo 
    506  1.1  ryo 	if ((s->s_flags & TPROF_SAMPLE_INKERNEL) == 0) {
    507  1.3  ryo 		sample_n_user[SAMPLE_MODE_ACCUMULATIVE]++;
    508  1.3  ryo 		sample_n_user[SAMPLE_MODE_INSTANTANEOUS]++;
    509  1.3  ryo 		sample_n_user_per_cpu[SAMPLE_MODE_ACCUMULATIVE][cpuid]++;
    510  1.3  ryo 		sample_n_user_per_cpu[SAMPLE_MODE_INSTANTANEOUS][cpuid]++;
    511  1.1  ryo 
    512  1.1  ryo 		name = NULL;
    513  1.1  ryo 		addr = s->s_pid;	/* XXX */
    514  1.1  ryo 		flags |= SAMPLE_ELM_FLAGS_USER;
    515  1.1  ryo 
    516  1.1  ryo 		if (!opt_userland)
    517  1.1  ryo 			return;
    518  1.1  ryo 	} else {
    519  1.3  ryo 		sample_n_kern[SAMPLE_MODE_ACCUMULATIVE]++;
    520  1.3  ryo 		sample_n_kern[SAMPLE_MODE_INSTANTANEOUS]++;
    521  1.3  ryo 		sample_n_kern_per_cpu[SAMPLE_MODE_ACCUMULATIVE][cpuid]++;
    522  1.3  ryo 		sample_n_kern_per_cpu[SAMPLE_MODE_INSTANTANEOUS][cpuid]++;
    523  1.1  ryo 
    524  1.1  ryo 		name = ksymlookup(s->s_pc, &offset, &symid);
    525  1.1  ryo 		if (name != NULL) {
    526  1.1  ryo 			addr = ksyms[symid]->value;
    527  1.1  ryo 		} else {
    528  1.1  ryo 			addr = s->s_pc;
    529  1.1  ryo 		}
    530  1.1  ryo 	}
    531  1.1  ryo 
    532  1.3  ryo 	e = ecalloc(1, sizeof_sample_elm);
    533  1.1  ryo 	e->addr = addr;
    534  1.1  ryo 	e->name = name;
    535  1.1  ryo 	e->flags = flags;
    536  1.3  ryo 	e->num[SAMPLE_MODE_ACCUMULATIVE] = 1;
    537  1.3  ryo 	e->num[SAMPLE_MODE_INSTANTANEOUS] = 1;
    538  1.3  ryo 	SAMPLE_ELM_NUM_CPU(e, SAMPLE_MODE_ACCUMULATIVE)[cpuid] = 1;
    539  1.3  ryo 	SAMPLE_ELM_NUM_CPU(e, SAMPLE_MODE_INSTANTANEOUS)[cpuid] = 1;
    540  1.1  ryo 	o = rb_tree_insert_node(&rb_tree_sample, e);
    541  1.3  ryo 	if (o == e) {
    542  1.3  ryo 		/* new symbol. add to list for sort */
    543  1.3  ryo 		ptrarray_push(&sample_list[SAMPLE_MODE_ACCUMULATIVE], o);
    544  1.3  ryo 		ptrarray_push(&sample_list[SAMPLE_MODE_INSTANTANEOUS], o);
    545  1.3  ryo 	} else {
    546  1.1  ryo 		/* already exists */
    547  1.3  ryo 		free(e);
    548  1.3  ryo 
    549  1.3  ryo 		o->num[SAMPLE_MODE_ACCUMULATIVE]++;
    550  1.3  ryo 		if (o->num[SAMPLE_MODE_INSTANTANEOUS]++ == 0) {
    551  1.3  ryo 			/* new instantaneous symbols. add to list for sort */
    552  1.3  ryo 			ptrarray_push(&sample_list[SAMPLE_MODE_INSTANTANEOUS],
    553  1.3  ryo 			    o);
    554  1.3  ryo 		}
    555  1.3  ryo 		SAMPLE_ELM_NUM_CPU(o, SAMPLE_MODE_ACCUMULATIVE)[cpuid]++;
    556  1.3  ryo 		SAMPLE_ELM_NUM_CPU(o, SAMPLE_MODE_INSTANTANEOUS)[cpuid]++;
    557  1.1  ryo 	}
    558  1.1  ryo }
    559  1.1  ryo 
    560  1.1  ryo static void
    561  1.4  ryo show_tprof_stat(int *lim)
    562  1.1  ryo {
    563  1.1  ryo 	static struct tprof_stat tsbuf[2], *ts0, *ts;
    564  1.1  ryo 	static u_int ts_i = 0;
    565  1.4  ryo 	static int tprofstat_width[6];
    566  1.4  ryo 	int ret, l;
    567  1.4  ryo 	char tmpbuf[128];
    568  1.1  ryo 
    569  1.1  ryo 	ts0 = &tsbuf[ts_i++ & 1];
    570  1.1  ryo 	ts = &tsbuf[ts_i & 1];
    571  1.1  ryo 	ret = ioctl(devfd, TPROF_IOC_GETSTAT, ts);
    572  1.1  ryo 	if (ret == -1)
    573  1.4  ryo 		die_errc(EXIT_FAILURE, errno, "TPROF_IOC_GETSTAT");
    574  1.1  ryo 
    575  1.4  ryo #define TS_PRINT(idx, label, _m)					\
    576  1.4  ryo 	do {								\
    577  1.4  ryo 		__CTASSERT(idx < __arraycount(tprofstat_width));	\
    578  1.4  ryo 		lim_printf(lim, "%s", label);			\
    579  1.4  ryo 		l = snprintf(tmpbuf, sizeof(tmpbuf), "%"PRIu64, ts->_m);\
    580  1.4  ryo 		if (ts->_m != ts0->_m)					\
    581  1.4  ryo 			l += snprintf(tmpbuf + l, sizeof(tmpbuf) - l,	\
    582  1.4  ryo 			    "(+%"PRIu64")", ts->_m - ts0->_m);		\
    583  1.4  ryo 		assert(l < (int)sizeof(tmpbuf));			\
    584  1.4  ryo 		if (tprofstat_width[idx] < l)				\
    585  1.4  ryo 			tprofstat_width[idx] = l;			\
    586  1.4  ryo 		lim_printf(lim, "%-*.*s  ", tprofstat_width[idx],	\
    587  1.4  ryo 		    tprofstat_width[idx], tmpbuf);			\
    588  1.1  ryo 	} while (0)
    589  1.4  ryo 	lim_printf(lim, "tprof ");
    590  1.4  ryo 	TS_PRINT(0, "sample:", ts_sample);
    591  1.4  ryo 	TS_PRINT(1, "overflow:", ts_overflow);
    592  1.4  ryo 	TS_PRINT(2, "buf:", ts_buf);
    593  1.4  ryo 	TS_PRINT(3, "emptybuf:", ts_emptybuf);
    594  1.4  ryo 	TS_PRINT(4, "dropbuf:", ts_dropbuf);
    595  1.4  ryo 	TS_PRINT(5, "dropbuf_sample:", ts_dropbuf_sample);
    596  1.1  ryo }
    597  1.1  ryo 
    598  1.1  ryo static void
    599  1.1  ryo show_timestamp(void)
    600  1.1  ryo {
    601  1.1  ryo 	struct timeval tv;
    602  1.1  ryo 	gettimeofday(&tv, NULL);
    603  1.1  ryo 	printf("%-8.8s", &(ctime((time_t *)&tv.tv_sec)[11]));
    604  1.1  ryo }
    605  1.1  ryo 
    606  1.1  ryo static void
    607  1.1  ryo show_counters_alloc(void)
    608  1.1  ryo {
    609  1.3  ryo 	size_t sz = 2 * ncpu * nevent * sizeof(*counters);
    610  1.3  ryo 	counters = ecalloc(1, sz);
    611  1.1  ryo }
    612  1.1  ryo 
    613  1.1  ryo static void
    614  1.4  ryo show_counters(int *lim)
    615  1.1  ryo {
    616  1.1  ryo 	tprof_counts_t countsbuf;
    617  1.1  ryo 	uint64_t *cn[2], *c0, *c;
    618  1.1  ryo 	u_int i;
    619  1.1  ryo 	int n, ret;
    620  1.1  ryo 
    621  1.1  ryo 	cn[0] = counters;
    622  1.1  ryo 	cn[1] = counters + ncpu * nevent;
    623  1.1  ryo 	c0 = cn[counters_i++ & 1];
    624  1.1  ryo 	c = cn[counters_i & 1];
    625  1.1  ryo 
    626  1.1  ryo 	for (n = 0; n < ncpu; n++) {
    627  1.1  ryo 		countsbuf.c_cpu = n;
    628  1.1  ryo 		ret = ioctl(devfd, TPROF_IOC_GETCOUNTS, &countsbuf);
    629  1.1  ryo 		if (ret == -1)
    630  1.4  ryo 			die_errc(EXIT_FAILURE, errno, "TPROF_IOC_GETCOUNTS");
    631  1.1  ryo 
    632  1.1  ryo 		for (i = 0; i < nevent; i++)
    633  1.1  ryo 			c[n * nevent + i] = countsbuf.c_count[i];
    634  1.1  ryo 	}
    635  1.1  ryo 
    636  1.4  ryo 	if (do_redraw) {
    637  1.4  ryo 		lim_printf(lim, "%-22s", "Event counter (delta)");
    638  1.4  ryo 		for (n = 0; n < ncpu; n++) {
    639  1.4  ryo 			char cpuname[16];
    640  1.4  ryo 			snprintf(cpuname, sizeof(cpuname), "CPU%u", n);
    641  1.4  ryo 			lim_printf(lim, "%11s", cpuname);
    642  1.4  ryo 		}
    643  1.4  ryo 		lim_newline(lim);
    644  1.4  ryo 	} else {
    645  1.4  ryo 		printf("\n");
    646  1.1  ryo 	}
    647  1.1  ryo 
    648  1.1  ryo 	for (i = 0; i < nevent; i++) {
    649  1.4  ryo 		lim_printf(lim, "%-22.22s", eventname[i]);
    650  1.1  ryo 		for (n = 0; n < ncpu; n++) {
    651  1.4  ryo 			lim_printf(lim, "%11"PRIu64,
    652  1.1  ryo 			    c[n * nevent + i] - c0[n * nevent + i]);
    653  1.1  ryo 		}
    654  1.4  ryo 		lim_newline(lim);
    655  1.1  ryo 	}
    656  1.4  ryo 	lim_newline(lim);
    657  1.1  ryo }
    658  1.1  ryo 
    659  1.1  ryo static void
    660  1.4  ryo show_count_per_event(int *lim)
    661  1.1  ryo {
    662  1.1  ryo 	u_int i, nsample_total;
    663  1.4  ryo 	int n, l;
    664  1.4  ryo 	char buf[32];
    665  1.1  ryo 
    666  1.3  ryo 	nsample_total = sample_n_kern[opt_mode] + sample_n_user[opt_mode];
    667  1.4  ryo 	if (nsample_total == 0)
    668  1.4  ryo 		nsample_total = 1;
    669  1.4  ryo 
    670  1.4  ryo 	/* calc width in advance */
    671  1.4  ryo 	for (i = 0; i < nevent; i++) {
    672  1.4  ryo 		l = snprintf(buf, sizeof(buf), "%"PRIu64,
    673  1.4  ryo 		    sample_n_per_event[opt_mode][i]);
    674  1.5  ryo 		if (sample_event_width < (u_int)l) {
    675  1.4  ryo 			sample_event_width = l;
    676  1.5  ryo 			do_redraw = true;
    677  1.5  ryo 		}
    678  1.4  ryo 	}
    679  1.6  ryo 	for (n = 0; n < ncpu; n++) {
    680  1.6  ryo 		uint64_t sum = 0;
    681  1.6  ryo 		for (i = 0; i < nevent; i++)
    682  1.6  ryo 			sum += sample_n_per_event_cpu[opt_mode][nevent * n + i];
    683  1.6  ryo 		l = snprintf(buf, sizeof(buf), "%"PRIu64, sum);
    684  1.6  ryo 		if (sample_cpu_width[n] < (u_int)l) {
    685  1.6  ryo 			sample_cpu_width[n] = l;
    686  1.6  ryo 			do_redraw = true;
    687  1.4  ryo 		}
    688  1.4  ryo 	}
    689  1.4  ryo 
    690  1.4  ryo 	if (do_redraw) {
    691  1.6  ryo 		lim_printf(lim, "  Rate %*s %-*s",
    692  1.6  ryo 		    sample_event_width, "Sample#",
    693  1.6  ryo 		    SYMBOL_LEN, "Eventname");
    694  1.4  ryo 		for (n = 0; n < ncpu; n++) {
    695  1.4  ryo 			snprintf(buf, sizeof(buf), "CPU%d", n);
    696  1.4  ryo 			lim_printf(lim, " %*s", sample_cpu_width[n], buf);
    697  1.4  ryo 		}
    698  1.4  ryo 		lim_newline(lim);
    699  1.4  ryo 
    700  1.4  ryo 		lim_printf(lim, "------ %*.*s %*.*s",
    701  1.4  ryo 		    sample_event_width, sample_event_width, LINESTR,
    702  1.4  ryo 		    SYMBOL_LEN, SYMBOL_LEN, LINESTR);
    703  1.4  ryo 		for (n = 0; n < ncpu; n++) {
    704  1.4  ryo 			lim_printf(lim, " %*.*s",
    705  1.4  ryo 			    sample_cpu_width[n], sample_cpu_width[n], LINESTR);
    706  1.4  ryo 		}
    707  1.4  ryo 		lim_newline(lim);
    708  1.4  ryo 	} else {
    709  1.4  ryo 		printf("\n\n");
    710  1.4  ryo 	}
    711  1.1  ryo 
    712  1.1  ryo 	for (i = 0; i < nevent; i++) {
    713  1.3  ryo 		if (sample_n_per_event[opt_mode][i] >= nsample_total) {
    714  1.4  ryo 			lim_printf(lim, "%5.1f%%", 100.0 *
    715  1.4  ryo 			    sample_n_per_event[opt_mode][i] / nsample_total);
    716  1.1  ryo 		} else {
    717  1.4  ryo 			lim_printf(lim, "%5.2f%%", 100.0 *
    718  1.4  ryo 			    sample_n_per_event[opt_mode][i] / nsample_total);
    719  1.1  ryo 		}
    720  1.4  ryo 		lim_printf(lim, " %*"PRIu64" ", sample_event_width,
    721  1.4  ryo 		    sample_n_per_event[opt_mode][i]);
    722  1.1  ryo 
    723  1.4  ryo 		lim_printf(lim, "%-32.32s", eventname[i]);
    724  1.1  ryo 		for (n = 0; n < ncpu; n++) {
    725  1.4  ryo 			lim_printf(lim, " %*"PRIu64, sample_cpu_width[n],
    726  1.3  ryo 			    sample_n_per_event_cpu[opt_mode][nevent * n + i]);
    727  1.1  ryo 		}
    728  1.4  ryo 		lim_newline(lim);
    729  1.1  ryo 	}
    730  1.1  ryo }
    731  1.1  ryo 
    732  1.1  ryo static void
    733  1.1  ryo sample_show(void)
    734  1.1  ryo {
    735  1.1  ryo 	struct sample_elm *e;
    736  1.3  ryo 	struct ptrarray *samples;
    737  1.1  ryo 	u_int nsample_total;
    738  1.4  ryo 	int i, l, lim, n, ndisp;
    739  1.1  ryo 	char namebuf[32];
    740  1.1  ryo 	const char *name;
    741  1.1  ryo 
    742  1.4  ryo 	if (nshow++ == 0) {
    743  1.4  ryo 		printf("\n");
    744  1.4  ryo 		if (!nontty) {
    745  1.4  ryo 			signal(SIGWINCH, sigwinch_handler);
    746  1.4  ryo 			signal(SIGINT, die);
    747  1.4  ryo 			signal(SIGQUIT, die);
    748  1.4  ryo 			signal(SIGTERM, die);
    749  1.4  ryo 			signal(SIGTSTP, sigtstp_handler);
    750  1.4  ryo 
    751  1.4  ryo 			tty_setup();
    752  1.4  ryo 		}
    753  1.4  ryo 	} else {
    754  1.4  ryo 		reset_cursor_pos();
    755  1.4  ryo 	}
    756  1.4  ryo 
    757  1.1  ryo 	int margin_lines = 7;
    758  1.1  ryo 
    759  1.1  ryo 	margin_lines += 3 + nevent;	/* show_counter_per_event() */
    760  1.1  ryo 
    761  1.3  ryo 	if (opt_mode == SAMPLE_MODE_INSTANTANEOUS)
    762  1.3  ryo 		sample_sort_instantaneous();
    763  1.3  ryo 	else
    764  1.3  ryo 		sample_sort_accumulative();
    765  1.3  ryo 	samples = &sample_list[opt_mode];
    766  1.3  ryo 
    767  1.1  ryo 	if (opt_showcounter)
    768  1.1  ryo 		margin_lines += 2 + nevent;
    769  1.1  ryo 	if (opt_userland)
    770  1.1  ryo 		margin_lines += 1;
    771  1.1  ryo 
    772  1.3  ryo 	ndisp = samples->pa_inuse;
    773  1.1  ryo 	if (!nontty && ndisp > (win.ws_row - margin_lines))
    774  1.1  ryo 		ndisp = win.ws_row - margin_lines;
    775  1.1  ryo 
    776  1.4  ryo 	lim = win.ws_col;
    777  1.3  ryo 	if (opt_mode == SAMPLE_MODE_ACCUMULATIVE)
    778  1.4  ryo 		lim_printf(&lim, "[Accumulative mode] ");
    779  1.4  ryo 	show_tprof_stat(&lim);
    780  1.3  ryo 
    781  1.4  ryo 	if (lim >= 16) {
    782  1.4  ryo 		l = win.ws_col - lim;
    783  1.4  ryo 		if (!nontty) {
    784  1.4  ryo 			clr_to_eol();
    785  1.4  ryo 			for (; l <= win.ws_col - 17; l = ((l + 8) & -8))
    786  1.4  ryo 				printf("\t");
    787  1.4  ryo 		}
    788  1.4  ryo 		show_timestamp();
    789  1.4  ryo 	}
    790  1.4  ryo 	lim_newline(&lim);
    791  1.4  ryo 	lim_newline(&lim);
    792  1.1  ryo 
    793  1.4  ryo 	if (opt_showcounter)
    794  1.4  ryo 		show_counters(&lim);
    795  1.1  ryo 
    796  1.4  ryo 	show_count_per_event(&lim);
    797  1.4  ryo 	lim_newline(&lim);
    798  1.1  ryo 
    799  1.4  ryo 	if (do_redraw) {
    800  1.6  ryo 		lim_printf(&lim, "  Rate %*s %-*s",
    801  1.6  ryo 		    sample_event_width, "Sample#",
    802  1.6  ryo 		    SYMBOL_LEN, "Symbol");
    803  1.4  ryo 		for (n = 0; n < ncpu; n++) {
    804  1.1  ryo 			snprintf(namebuf, sizeof(namebuf), "CPU%d", n);
    805  1.4  ryo 			lim_printf(&lim, " %*s", sample_cpu_width[n], namebuf);
    806  1.1  ryo 		}
    807  1.4  ryo 		lim_newline(&lim);
    808  1.1  ryo 
    809  1.4  ryo 		lim_printf(&lim, "------ %*.*s %*.*s",
    810  1.4  ryo 		    sample_event_width, sample_event_width, LINESTR,
    811  1.4  ryo 		    SYMBOL_LEN, SYMBOL_LEN, LINESTR);
    812  1.4  ryo 		for (n = 0; n < ncpu; n++) {
    813  1.4  ryo 			lim_printf(&lim, " %*.*s", sample_cpu_width[n],
    814  1.4  ryo 			    sample_cpu_width[n], LINESTR);
    815  1.1  ryo 		}
    816  1.4  ryo 		lim_newline(&lim);
    817  1.4  ryo 	} else {
    818  1.4  ryo 		printf("\n\n");
    819  1.1  ryo 	}
    820  1.1  ryo 
    821  1.1  ryo 	for (i = 0; i < ndisp; i++) {
    822  1.3  ryo 		e = (struct sample_elm *)samples->pa_ptrs[i];
    823  1.1  ryo 		name = e->name;
    824  1.1  ryo 		if (name == NULL) {
    825  1.1  ryo 			if (e->flags & SAMPLE_ELM_FLAGS_USER) {
    826  1.3  ryo 				snprintf(namebuf, sizeof(namebuf),
    827  1.3  ryo 				    "<PID:%"PRIu64">", e->addr);
    828  1.1  ryo 			} else {
    829  1.3  ryo 				snprintf(namebuf, sizeof(namebuf),
    830  1.3  ryo 				    "0x%016"PRIx64, e->addr);
    831  1.1  ryo 			}
    832  1.1  ryo 			name = namebuf;
    833  1.1  ryo 		}
    834  1.1  ryo 
    835  1.3  ryo 		nsample_total = sample_n_kern[opt_mode];
    836  1.1  ryo 		if (opt_userland)
    837  1.3  ryo 			nsample_total += sample_n_user[opt_mode];
    838  1.1  ryo 		/*
    839  1.1  ryo 		 * even when only kernel mode events are configured,
    840  1.1  ryo 		 * interrupts may still occur in the user mode state.
    841  1.1  ryo 		 */
    842  1.1  ryo 		if (nsample_total == 0)
    843  1.1  ryo 			nsample_total = 1;
    844  1.1  ryo 
    845  1.3  ryo 		if (e->num[opt_mode] >= nsample_total) {
    846  1.4  ryo 			lim_printf(&lim, "%5.1f%%", 100.0 *
    847  1.4  ryo 			    e->num[opt_mode] / nsample_total);
    848  1.1  ryo 		} else {
    849  1.4  ryo 			lim_printf(&lim, "%5.2f%%", 100.0 *
    850  1.4  ryo 			    e->num[opt_mode] / nsample_total);
    851  1.1  ryo 		}
    852  1.4  ryo 		lim_printf(&lim, " %*u %-32.32s", sample_event_width,
    853  1.4  ryo 		    e->num[opt_mode], name);
    854  1.1  ryo 
    855  1.1  ryo 		for (n = 0; n < ncpu; n++) {
    856  1.3  ryo 			if (SAMPLE_ELM_NUM_CPU(e, opt_mode)[n] == 0) {
    857  1.4  ryo 				lim_printf(&lim, " %*s", sample_cpu_width[n],
    858  1.4  ryo 				    ".");
    859  1.3  ryo 			} else {
    860  1.4  ryo 				lim_printf(&lim, " %*u", sample_cpu_width[n],
    861  1.3  ryo 				    SAMPLE_ELM_NUM_CPU(e, opt_mode)[n]);
    862  1.3  ryo 			}
    863  1.1  ryo 		}
    864  1.4  ryo 		lim_newline(&lim);
    865  1.1  ryo 	}
    866  1.1  ryo 
    867  1.3  ryo 	if ((u_int)ndisp != samples->pa_inuse) {
    868  1.4  ryo 		lim_printf(&lim, "     : %*s (more %zu symbols omitted)",
    869  1.4  ryo 		    sample_event_width, ":", samples->pa_inuse - ndisp);
    870  1.4  ryo 		lim_newline(&lim);
    871  1.4  ryo 	} else if (!nontty) {
    872  1.1  ryo 		for (i = ndisp; i <= win.ws_row - margin_lines; i++) {
    873  1.1  ryo 			printf("~");
    874  1.4  ryo 			lim_newline(&lim);
    875  1.1  ryo 		}
    876  1.1  ryo 	}
    877  1.1  ryo 
    878  1.4  ryo 	if (do_redraw) {
    879  1.4  ryo 		lim_printf(&lim, "------ %*.*s %*.*s",
    880  1.4  ryo 		    sample_event_width, sample_event_width, LINESTR,
    881  1.4  ryo 		    SYMBOL_LEN, SYMBOL_LEN, LINESTR);
    882  1.4  ryo 		for (n = 0; n < ncpu; n++) {
    883  1.4  ryo 			lim_printf(&lim, " %*.*s",
    884  1.4  ryo 			    sample_cpu_width[n], sample_cpu_width[n], LINESTR);
    885  1.4  ryo 		}
    886  1.4  ryo 		lim_newline(&lim);
    887  1.4  ryo 	} else {
    888  1.4  ryo 		printf("\n");
    889  1.1  ryo 	}
    890  1.1  ryo 
    891  1.4  ryo 	lim_printf(&lim, "Total  %*u %-32.32s",
    892  1.4  ryo 	    sample_event_width, sample_n_kern[opt_mode], "in-kernel");
    893  1.1  ryo 	for (n = 0; n < ncpu; n++) {
    894  1.4  ryo 		lim_printf(&lim, " %*u", sample_cpu_width[n],
    895  1.4  ryo 		    sample_n_kern_per_cpu[opt_mode][n]);
    896  1.1  ryo 	}
    897  1.1  ryo 
    898  1.1  ryo 	if (opt_userland) {
    899  1.4  ryo 		lim_newline(&lim);
    900  1.4  ryo 		lim_printf(&lim, "       %*u %-32.32s",
    901  1.4  ryo 		    sample_event_width, sample_n_user[opt_mode], "userland");
    902  1.1  ryo 		for (n = 0; n < ncpu; n++) {
    903  1.4  ryo 			lim_printf(&lim, " %*u", sample_cpu_width[n],
    904  1.4  ryo 			    sample_n_user_per_cpu[opt_mode][n]);
    905  1.1  ryo 		}
    906  1.1  ryo 	}
    907  1.1  ryo 
    908  1.4  ryo 	if (nontty)
    909  1.4  ryo 		printf("\n");
    910  1.4  ryo 	else
    911  1.4  ryo 		clr_to_eol();
    912  1.1  ryo }
    913  1.1  ryo 
    914  1.1  ryo __dead static void
    915  1.1  ryo tprof_top_usage(void)
    916  1.1  ryo {
    917  1.3  ryo 	fprintf(stderr, "%s top [-acu] [-e name[,scale] [-e ...]]"
    918  1.3  ryo 	    " [-i interval]\n", getprogname());
    919  1.1  ryo 	exit(EXIT_FAILURE);
    920  1.1  ryo }
    921  1.1  ryo 
    922  1.6  ryo __dead void
    923  1.1  ryo tprof_top(int argc, char **argv)
    924  1.1  ryo {
    925  1.1  ryo 	tprof_param_t params[TPROF_MAXCOUNTERS];
    926  1.1  ryo 	struct itimerval it;
    927  1.4  ryo 	ssize_t tprof_bufsize, len;
    928  1.1  ryo 	u_int i;
    929  1.1  ryo 	int ch, ret;
    930  1.7  ryo 	char *tprof_buf, *p, *errmsg;
    931  1.4  ryo 	bool noinput = false;
    932  1.1  ryo 
    933  1.1  ryo 	memset(params, 0, sizeof(params));
    934  1.1  ryo 	nevent = 0;
    935  1.1  ryo 
    936  1.4  ryo 	while ((ch = getopt(argc, argv, "ace:i:L:u")) != -1) {
    937  1.1  ryo 		switch (ch) {
    938  1.3  ryo 		case 'a':
    939  1.3  ryo 			opt_mode = SAMPLE_MODE_ACCUMULATIVE;
    940  1.3  ryo 			break;
    941  1.1  ryo 		case 'c':
    942  1.1  ryo 			opt_showcounter = 1;
    943  1.1  ryo 			break;
    944  1.1  ryo 		case 'e':
    945  1.7  ryo 			if (tprof_parse_event(&params[nevent], optarg,
    946  1.7  ryo 			    TPROF_PARSE_EVENT_F_ALLOWSCALE,
    947  1.7  ryo 			    &eventname[nevent], &errmsg) != 0) {
    948  1.7  ryo 				die_errc(EXIT_FAILURE, 0, "%s", errmsg);
    949  1.1  ryo 			}
    950  1.1  ryo 			nevent++;
    951  1.1  ryo 			if (nevent > __arraycount(params) ||
    952  1.1  ryo 			    nevent > ncounters)
    953  1.4  ryo 				die_errc(EXIT_FAILURE, 0,
    954  1.4  ryo 				    "Too many events. Only a maximum of %d "
    955  1.4  ryo 				    "counters can be used.", ncounters);
    956  1.1  ryo 			break;
    957  1.1  ryo 		case 'i':
    958  1.1  ryo 			top_interval = strtol(optarg, &p, 10);
    959  1.1  ryo 			if (*p != '\0' || top_interval <= 0)
    960  1.4  ryo 				die_errc(EXIT_FAILURE, 0,
    961  1.4  ryo 				    "Bad/invalid interval: %s", optarg);
    962  1.1  ryo 			break;
    963  1.1  ryo 		case 'u':
    964  1.1  ryo 			opt_userland = 1;
    965  1.1  ryo 			break;
    966  1.1  ryo 		default:
    967  1.1  ryo 			tprof_top_usage();
    968  1.1  ryo 		}
    969  1.1  ryo 	}
    970  1.1  ryo 	argc -= optind;
    971  1.1  ryo 	argv += optind;
    972  1.1  ryo 
    973  1.1  ryo 	if (argc != 0)
    974  1.1  ryo 		tprof_top_usage();
    975  1.1  ryo 
    976  1.1  ryo 	if (nevent == 0) {
    977  1.3  ryo 		const char *defaultevent = cycle_event_name();
    978  1.1  ryo 		if (defaultevent == NULL)
    979  1.4  ryo 			die_errc(EXIT_FAILURE, 0, "cpu not supported");
    980  1.1  ryo 
    981  1.1  ryo 		tprof_event_lookup(defaultevent, &params[nevent]);
    982  1.1  ryo 		eventname[nevent] = defaultevent;
    983  1.1  ryo 		nevent++;
    984  1.1  ryo 	}
    985  1.1  ryo 
    986  1.3  ryo 	sample_init();
    987  1.1  ryo 	show_counters_alloc();
    988  1.1  ryo 
    989  1.1  ryo 	for (i = 0; i < nevent; i++) {
    990  1.1  ryo 		params[i].p_counter = i;
    991  1.1  ryo 		params[i].p_flags |= TPROF_PARAM_KERN | TPROF_PARAM_PROFILE;
    992  1.1  ryo 		if (opt_userland)
    993  1.1  ryo 			params[i].p_flags |= TPROF_PARAM_USER;
    994  1.1  ryo 		ret = ioctl(devfd, TPROF_IOC_CONFIGURE_EVENT, &params[i]);
    995  1.1  ryo 		if (ret == -1)
    996  1.4  ryo 			die_errc(EXIT_FAILURE, errno,
    997  1.4  ryo 			    "TPROF_IOC_CONFIGURE_EVENT: %s", eventname[i]);
    998  1.1  ryo 	}
    999  1.1  ryo 
   1000  1.1  ryo 	tprof_countermask_t mask = TPROF_COUNTERMASK_ALL;
   1001  1.1  ryo 	ret = ioctl(devfd, TPROF_IOC_START, &mask);
   1002  1.1  ryo 	if (ret == -1)
   1003  1.4  ryo 		die_errc(EXIT_FAILURE, errno, "TPROF_IOC_START");
   1004  1.1  ryo 
   1005  1.1  ryo 	ksyms = ksymload(&nksyms);
   1006  1.1  ryo 
   1007  1.4  ryo 	signal(SIGALRM, sigalrm_handler);
   1008  1.1  ryo 
   1009  1.1  ryo 	it.it_interval.tv_sec = it.it_value.tv_sec = top_interval;
   1010  1.1  ryo 	it.it_interval.tv_usec = it.it_value.tv_usec = 0;
   1011  1.1  ryo 	setitimer(ITIMER_REAL, &it, NULL);
   1012  1.1  ryo 
   1013  1.3  ryo 	sample_reset(true);
   1014  1.1  ryo 	printf("collecting samples...");
   1015  1.1  ryo 	fflush(stdout);
   1016  1.1  ryo 
   1017  1.6  ryo 	tprof_bufsize = sizeof(tprof_sample_t) * 1024 * 32;
   1018  1.3  ryo 	tprof_buf = emalloc(tprof_bufsize);
   1019  1.1  ryo 	do {
   1020  1.4  ryo 		bool force_update = false;
   1021  1.4  ryo 
   1022  1.6  ryo 		while (sigalrm == 0 && !force_update) {
   1023  1.4  ryo 			fd_set r;
   1024  1.4  ryo 			int nfound;
   1025  1.4  ryo 			char c;
   1026  1.4  ryo 
   1027  1.4  ryo 			FD_ZERO(&r);
   1028  1.4  ryo 			if (!noinput)
   1029  1.4  ryo 				FD_SET(STDIN_FILENO, &r);
   1030  1.4  ryo 			FD_SET(devfd, &r);
   1031  1.4  ryo 			nfound = select(devfd + 1, &r, NULL, NULL, NULL);
   1032  1.4  ryo 			if (nfound == -1) {
   1033  1.4  ryo 				if (errno == EINTR)
   1034  1.4  ryo 					break;
   1035  1.4  ryo 				die_errc(EXIT_FAILURE, errno, "select");
   1036  1.4  ryo 			}
   1037  1.4  ryo 
   1038  1.4  ryo 			if (FD_ISSET(STDIN_FILENO, &r)) {
   1039  1.4  ryo 				len = read(STDIN_FILENO, &c, 1);
   1040  1.4  ryo 				if (len <= 0) {
   1041  1.4  ryo 					noinput = true;
   1042  1.4  ryo 					continue;
   1043  1.4  ryo 				}
   1044  1.4  ryo 				switch (c) {
   1045  1.4  ryo 				case 0x0c:	/* ^L */
   1046  1.4  ryo 					do_redraw = true;
   1047  1.4  ryo 					break;
   1048  1.4  ryo 				case 'a':
   1049  1.4  ryo 					/* toggle mode */
   1050  1.4  ryo 					opt_mode = (opt_mode + 1) %
   1051  1.4  ryo 					    SAMPLE_MODE_NUM;
   1052  1.6  ryo 					do_redraw = true;
   1053  1.6  ryo 					break;
   1054  1.6  ryo 				case 'c':
   1055  1.6  ryo 					/* toggle mode */
   1056  1.6  ryo 					opt_showcounter ^= 1;
   1057  1.6  ryo 					do_redraw = true;
   1058  1.4  ryo 					break;
   1059  1.4  ryo 				case 'q':
   1060  1.4  ryo 					goto done;
   1061  1.4  ryo 				case 'z':
   1062  1.4  ryo 					sample_reset(true);
   1063  1.4  ryo 					break;
   1064  1.4  ryo 				default:
   1065  1.6  ryo 					continue;
   1066  1.4  ryo 				}
   1067  1.4  ryo 				force_update = true;
   1068  1.4  ryo 			}
   1069  1.4  ryo 
   1070  1.4  ryo 			if (FD_ISSET(devfd, &r)) {
   1071  1.4  ryo 				len = read(devfd, tprof_buf, tprof_bufsize);
   1072  1.4  ryo 				if (len == -1 && errno != EINTR)
   1073  1.4  ryo 					die_errc(EXIT_FAILURE, errno, "read");
   1074  1.4  ryo 				if (len > 0) {
   1075  1.4  ryo 					tprof_sample_t *s =
   1076  1.4  ryo 					    (tprof_sample_t *)tprof_buf;
   1077  1.4  ryo 					while (s <
   1078  1.4  ryo 					    (tprof_sample_t *)(tprof_buf + len))
   1079  1.4  ryo 						sample_collect(s++);
   1080  1.4  ryo 				}
   1081  1.1  ryo 			}
   1082  1.1  ryo 		}
   1083  1.1  ryo 		sigalrm = 0;
   1084  1.1  ryo 
   1085  1.1  ryo 		/* update screen */
   1086  1.1  ryo 		sample_show();
   1087  1.1  ryo 		fflush(stdout);
   1088  1.4  ryo 		do_redraw = false;
   1089  1.4  ryo 		if (force_update)
   1090  1.4  ryo 			continue;
   1091  1.1  ryo 
   1092  1.3  ryo 		sample_reset(false);
   1093  1.4  ryo 
   1094  1.1  ryo 	} while (!nontty);
   1095  1.1  ryo 
   1096  1.4  ryo  done:
   1097  1.4  ryo 	die(0);
   1098  1.1  ryo }
   1099