Home | History | Annotate | Line # | Download | only in tprof
tprof.c revision 1.16
      1  1.16       ryo /*	$NetBSD: tprof.c,v 1.16 2022/12/01 00:43:27 ryo Exp $	*/
      2   1.1      yamt 
      3   1.6      maxv /*
      4   1.6      maxv  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5   1.6      maxv  * All rights reserved.
      6   1.6      maxv  *
      7   1.6      maxv  * This code is derived from software contributed to The NetBSD Foundation
      8   1.6      maxv  * by Maxime Villard.
      9   1.6      maxv  *
     10   1.6      maxv  * Redistribution and use in source and binary forms, with or without
     11   1.6      maxv  * modification, are permitted provided that the following conditions
     12   1.6      maxv  * are met:
     13   1.6      maxv  * 1. Redistributions of source code must retain the above copyright
     14   1.6      maxv  *    notice, this list of conditions and the following disclaimer.
     15   1.6      maxv  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.6      maxv  *    notice, this list of conditions and the following disclaimer in the
     17   1.6      maxv  *    documentation and/or other materials provided with the distribution.
     18   1.6      maxv  *
     19   1.6      maxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.6      maxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.6      maxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.6      maxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.6      maxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.6      maxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.6      maxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.6      maxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.6      maxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.6      maxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.6      maxv  * POSSIBILITY OF SUCH DAMAGE.
     30   1.6      maxv  */
     31   1.6      maxv 
     32   1.6      maxv /*
     33   1.1      yamt  * Copyright (c)2008 YAMAMOTO Takashi,
     34   1.1      yamt  * All rights reserved.
     35   1.1      yamt  *
     36   1.1      yamt  * Redistribution and use in source and binary forms, with or without
     37   1.1      yamt  * modification, are permitted provided that the following conditions
     38   1.1      yamt  * are met:
     39   1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     40   1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     41   1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     43   1.1      yamt  *    documentation and/or other materials provided with the distribution.
     44   1.1      yamt  *
     45   1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     46   1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47   1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48   1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     49   1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50   1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51   1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52   1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53   1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54   1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55   1.1      yamt  * SUCH DAMAGE.
     56   1.1      yamt  */
     57   1.1      yamt 
     58   1.1      yamt #include <sys/cdefs.h>
     59   1.1      yamt #ifndef lint
     60  1.16       ryo __RCSID("$NetBSD: tprof.c,v 1.16 2022/12/01 00:43:27 ryo Exp $");
     61   1.1      yamt #endif /* not lint */
     62   1.1      yamt 
     63  1.15       ryo #include <sys/atomic.h>
     64   1.1      yamt #include <sys/ioctl.h>
     65  1.15       ryo #include <sys/sysctl.h>
     66   1.1      yamt #include <sys/wait.h>
     67   1.1      yamt 
     68   1.1      yamt #include <dev/tprof/tprof_ioctl.h>
     69   1.1      yamt 
     70   1.1      yamt #include <err.h>
     71   1.1      yamt #include <errno.h>
     72   1.1      yamt #include <fcntl.h>
     73   1.1      yamt #include <inttypes.h>
     74  1.15       ryo #include <math.h>
     75   1.1      yamt #include <pthread.h>
     76   1.1      yamt #include <signal.h>
     77   1.1      yamt #include <stdbool.h>
     78   1.1      yamt #include <stdio.h>
     79   1.1      yamt #include <stdlib.h>
     80   1.1      yamt #include <string.h>
     81  1.15       ryo #include <time.h>
     82   1.1      yamt #include <unistd.h>
     83  1.15       ryo #include <util.h>
     84   1.6      maxv #include "tprof.h"
     85   1.1      yamt 
     86   1.1      yamt #define	_PATH_TPROF	"/dev/tprof"
     87   1.1      yamt 
     88  1.14       ryo struct tprof_info tprof_info;
     89  1.14       ryo u_int ncounters;
     90   1.1      yamt int devfd;
     91   1.1      yamt int outfd;
     92  1.15       ryo int ncpu;
     93  1.14       ryo u_int nevent;
     94  1.15       ryo double interval = 0xffffffff;	/* XXX */
     95  1.15       ryo const char *eventname[TPROF_MAXCOUNTERS];
     96  1.15       ryo u_int eventnamewidth[TPROF_MAXCOUNTERS];
     97  1.15       ryo #define	COUNTER_COLUMNS_WIDTH	11
     98   1.1      yamt 
     99   1.7      maxv static void tprof_list(int, char **);
    100  1.15       ryo static void tprof_monitor_common(bool, int, char **) __dead;
    101  1.15       ryo static void tprof_monitor(int, char **);
    102  1.15       ryo static void tprof_count(int, char **);
    103   1.7      maxv 
    104   1.7      maxv static struct cmdtab {
    105   1.7      maxv 	const char *label;
    106   1.7      maxv 	bool takesargs;
    107   1.7      maxv 	bool argsoptional;
    108   1.7      maxv 	void (*func)(int, char **);
    109   1.7      maxv } const tprof_cmdtab[] = {
    110   1.7      maxv 	{ "list",	false, false, tprof_list },
    111   1.7      maxv 	{ "monitor",	true,  false, tprof_monitor },
    112  1.15       ryo 	{ "count",	true,  false, tprof_count },
    113   1.8      maxv 	{ "analyze",	true,  true,  tprof_analyze },
    114  1.16       ryo 	{ "top",	true,  true,  tprof_top },
    115   1.7      maxv 	{ NULL,		false, false, NULL },
    116   1.7      maxv };
    117   1.7      maxv 
    118   1.5     joerg __dead static void
    119   1.1      yamt usage(void)
    120   1.1      yamt {
    121   1.1      yamt 
    122   1.8      maxv 	fprintf(stderr, "%s op [arguments]\n", getprogname());
    123   1.2      yamt 	fprintf(stderr, "\n");
    124   1.7      maxv 	fprintf(stderr, "\tlist\n");
    125   1.7      maxv 	fprintf(stderr, "\t\tList the available events.\n");
    126  1.16       ryo 	fprintf(stderr, "\tmonitor -e name[:option] [-e ...] [-o outfile]"
    127  1.16       ryo 	    " command\n");
    128   1.7      maxv 	fprintf(stderr, "\t\tMonitor the event 'name' with option 'option'\n"
    129   1.7      maxv 	    "\t\tcounted during the execution of 'command'.\n");
    130  1.15       ryo 	fprintf(stderr, "\tcount -e name[:option] [-e ...] [-i interval]"
    131  1.15       ryo 	    " command\n");
    132  1.15       ryo 	fprintf(stderr, "\t\tSame as monitor, but does not profile,"
    133  1.15       ryo 	    " only outputs a counter.\n");
    134  1.12       wiz 	fprintf(stderr, "\tanalyze [-CkLPs] [-p pid] file\n");
    135   1.9      maxv 	fprintf(stderr, "\t\tAnalyze the samples of the file 'file'.\n");
    136  1.16       ryo 	fprintf(stderr, "\ttop [-e name [-e ...]] [-i interval] [-u]\n");
    137  1.16       ryo 	fprintf(stderr, "\t\tDisplay profiling results in real-time.\n");
    138   1.1      yamt 	exit(EXIT_FAILURE);
    139   1.1      yamt }
    140   1.1      yamt 
    141  1.15       ryo static int
    142  1.15       ryo getncpu(void)
    143  1.15       ryo {
    144  1.15       ryo 	size_t size;
    145  1.15       ryo 	int mib[2];
    146  1.15       ryo 
    147  1.15       ryo 	mib[0] = CTL_HW;
    148  1.15       ryo 	mib[1] = HW_NCPU;
    149  1.15       ryo 	size = sizeof(ncpu);
    150  1.15       ryo 	if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1)
    151  1.15       ryo 		ncpu = 1;
    152  1.15       ryo 	return ncpu;
    153  1.15       ryo }
    154  1.15       ryo 
    155   1.1      yamt static void *
    156   1.1      yamt process_samples(void *dummy)
    157   1.1      yamt {
    158   1.1      yamt 
    159   1.1      yamt 	for (;;) {
    160   1.1      yamt 		char buf[4096];
    161   1.1      yamt 		const char *cp;
    162   1.1      yamt 		ssize_t ssz;
    163   1.1      yamt 
    164   1.1      yamt 		ssz = read(devfd, buf, sizeof(buf));
    165   1.1      yamt 		if (ssz == -1) {
    166   1.1      yamt 			err(EXIT_FAILURE, "read");
    167   1.1      yamt 		}
    168   1.1      yamt 		if (ssz == 0) {
    169   1.1      yamt 			break;
    170   1.1      yamt 		}
    171   1.1      yamt 		cp = buf;
    172   1.1      yamt 		while (ssz) {
    173   1.1      yamt 			ssize_t wsz;
    174   1.1      yamt 
    175   1.1      yamt 			wsz = write(outfd, cp, ssz);
    176   1.1      yamt 			if (wsz == -1) {
    177   1.1      yamt 				err(EXIT_FAILURE, "write");
    178   1.1      yamt 			}
    179   1.1      yamt 			ssz -= wsz;
    180   1.1      yamt 			cp += wsz;
    181   1.1      yamt 		}
    182   1.1      yamt 	}
    183   1.1      yamt 	return NULL;
    184   1.1      yamt }
    185   1.1      yamt 
    186   1.7      maxv static void
    187  1.15       ryo show_counters(void)
    188  1.15       ryo {
    189  1.15       ryo 	unsigned int i;
    190  1.15       ryo 	int n, ret;
    191  1.15       ryo 
    192  1.15       ryo 	fprintf(stderr, "      ");
    193  1.15       ryo 	for (i = 0; i < nevent; i++)
    194  1.15       ryo 		fprintf(stderr, " %*s", eventnamewidth[i], eventname[i]);
    195  1.15       ryo 	fprintf(stderr, "\n");
    196  1.15       ryo 
    197  1.15       ryo 	for (n = 0; n < ncpu; n++) {
    198  1.15       ryo 		tprof_counts_t counts;
    199  1.15       ryo 
    200  1.15       ryo 		memset(&counts, 0, sizeof(counts));
    201  1.15       ryo 		counts.c_cpu = n;
    202  1.15       ryo 		ret = ioctl(devfd, TPROF_IOC_GETCOUNTS, &counts);
    203  1.15       ryo 		if (ret == -1)
    204  1.15       ryo 			err(EXIT_FAILURE, "TPROF_IOC_GETCOUNTS");
    205  1.15       ryo 
    206  1.15       ryo 		fprintf(stderr, "CPU%-3d", n);
    207  1.15       ryo 		for (i = 0; i < nevent; i++) {
    208  1.15       ryo 			fprintf(stderr, " %*"PRIu64,
    209  1.15       ryo 			    eventnamewidth[i], counts.c_count[i]);
    210  1.15       ryo 		}
    211  1.15       ryo 		fprintf(stderr, "\n");
    212  1.15       ryo 	}
    213  1.15       ryo }
    214  1.15       ryo 
    215  1.15       ryo /* XXX: avoid mixing with the output of the child process SIGINFO handler... */
    216  1.15       ryo static void
    217  1.15       ryo output_delay(void)
    218  1.15       ryo {
    219  1.15       ryo 	struct timespec delay_ts;
    220  1.15       ryo 
    221  1.15       ryo 	delay_ts.tv_sec = 0;
    222  1.15       ryo 	delay_ts.tv_nsec = 100000000;
    223  1.15       ryo 	nanosleep(&delay_ts, NULL);
    224  1.15       ryo }
    225  1.15       ryo 
    226  1.15       ryo static void
    227  1.15       ryo siginfo_nothing(int signo)
    228  1.15       ryo {
    229  1.15       ryo 	__nothing;
    230  1.15       ryo }
    231  1.15       ryo 
    232  1.15       ryo static void
    233  1.15       ryo siginfo_showcount(int signo)
    234  1.15       ryo {
    235  1.15       ryo 	output_delay();
    236  1.15       ryo 	show_counters();
    237  1.15       ryo }
    238  1.15       ryo 
    239  1.15       ryo static void *
    240  1.15       ryo process_stat(void *arg)
    241  1.15       ryo {
    242  1.15       ryo 	unsigned int *done = arg;
    243  1.15       ryo 	double ival, fval;
    244  1.15       ryo 	struct timespec ts;
    245  1.15       ryo 
    246  1.15       ryo 	ival = floor(interval);
    247  1.15       ryo 	fval = (1000000000 * (interval - ival));
    248  1.15       ryo 	ts.tv_sec = ival;
    249  1.15       ryo 	ts.tv_nsec = fval;
    250  1.15       ryo 
    251  1.15       ryo 	while (atomic_add_int_nv(done, 0) == 0) {
    252  1.15       ryo 		show_counters();
    253  1.15       ryo 		nanosleep(&ts, NULL);
    254  1.15       ryo 		if (errno == EINTR)	/* interrupted by SIGINFO? */
    255  1.15       ryo 			output_delay();
    256  1.15       ryo 	}
    257  1.15       ryo 	return NULL;
    258  1.15       ryo }
    259  1.15       ryo 
    260  1.15       ryo static void
    261   1.7      maxv tprof_list(int argc, char **argv)
    262   1.7      maxv {
    263  1.16       ryo 	printf("%u events can be counted at the same time\n", ncounters);
    264   1.7      maxv 	tprof_event_list();
    265   1.7      maxv }
    266   1.7      maxv 
    267   1.7      maxv static void
    268  1.15       ryo tprof_monitor_common(bool do_profile, int argc, char **argv)
    269   1.1      yamt {
    270   1.7      maxv 	const char *outfile = "tprof.out";
    271   1.1      yamt 	struct tprof_stat ts;
    272  1.14       ryo 	tprof_param_t params[TPROF_MAXCOUNTERS];
    273   1.1      yamt 	pid_t pid;
    274   1.1      yamt 	pthread_t pt;
    275  1.14       ryo 	int ret, ch, i;
    276  1.15       ryo 	char *tokens[2], *p;
    277  1.14       ryo 	tprof_countermask_t mask = TPROF_COUNTERMASK_ALL;
    278   1.6      maxv 
    279  1.14       ryo 	memset(params, 0, sizeof(params));
    280   1.6      maxv 
    281  1.15       ryo 	while ((ch = getopt(argc, argv, do_profile ? "o:e:" : "e:i:")) != -1) {
    282   1.1      yamt 		switch (ch) {
    283   1.1      yamt 		case 'o':
    284   1.1      yamt 			outfile = optarg;
    285   1.1      yamt 			break;
    286  1.15       ryo 		case 'i':
    287  1.15       ryo 			interval = strtod(optarg, &p);
    288  1.15       ryo 			if (*p != '\0' || interval <= 0)
    289  1.15       ryo 				errx(EXIT_FAILURE, "Bad/invalid interval: %s",
    290  1.15       ryo 				    optarg);
    291  1.15       ryo 			break;
    292   1.6      maxv 		case 'e':
    293  1.15       ryo 			p = estrdup(optarg);
    294  1.15       ryo 			tokens[0] = strtok(p, ":");
    295   1.6      maxv 			tokens[1] = strtok(NULL, ":");
    296  1.14       ryo 			tprof_event_lookup(tokens[0], &params[nevent]);
    297  1.15       ryo 
    298  1.15       ryo 			if (tokens[1] == NULL) {
    299  1.15       ryo 				params[nevent].p_flags |=
    300  1.15       ryo 				    (TPROF_PARAM_USER | TPROF_PARAM_KERN);
    301  1.15       ryo 			} else {
    302  1.15       ryo 				if (strchr(tokens[1], 'u'))
    303  1.15       ryo 					params[nevent].p_flags |=
    304  1.15       ryo 					    TPROF_PARAM_USER;
    305  1.15       ryo 				if (strchr(tokens[1], 'k'))
    306  1.15       ryo 					params[nevent].p_flags |=
    307  1.15       ryo 					    TPROF_PARAM_KERN;
    308  1.15       ryo 			}
    309  1.15       ryo 			eventname[nevent] = tokens[0];
    310  1.15       ryo 			eventnamewidth[nevent] = strlen(eventname[nevent]);
    311  1.15       ryo 			if (eventnamewidth[nevent] < COUNTER_COLUMNS_WIDTH)
    312  1.15       ryo 				eventnamewidth[nevent] = COUNTER_COLUMNS_WIDTH;
    313  1.14       ryo 			nevent++;
    314  1.14       ryo 			if (nevent > __arraycount(params) ||
    315  1.14       ryo 			    nevent > ncounters)
    316  1.16       ryo 				errx(EXIT_FAILURE, "Too many events. Only a"
    317  1.16       ryo 				    " maximum of %d counters can be used.",
    318  1.16       ryo 				    ncounters);
    319   1.6      maxv 			break;
    320   1.1      yamt 		default:
    321   1.1      yamt 			usage();
    322   1.1      yamt 		}
    323   1.1      yamt 	}
    324   1.1      yamt 	argc -= optind;
    325   1.1      yamt 	argv += optind;
    326  1.14       ryo 	if (argc == 0 || nevent == 0) {
    327   1.6      maxv 		usage();
    328   1.6      maxv 	}
    329   1.6      maxv 
    330  1.15       ryo 	if (do_profile) {
    331  1.15       ryo 		outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    332  1.15       ryo 		if (outfd == -1) {
    333  1.15       ryo 			err(EXIT_FAILURE, "%s", outfile);
    334  1.15       ryo 		}
    335   1.1      yamt 	}
    336   1.1      yamt 
    337  1.14       ryo 	for (i = 0; i < (int)nevent; i++) {
    338  1.14       ryo 		params[i].p_counter = i;
    339  1.15       ryo 		if (do_profile)
    340  1.15       ryo 			params[i].p_flags |= TPROF_PARAM_PROFILE;
    341  1.14       ryo 		ret = ioctl(devfd, TPROF_IOC_CONFIGURE_EVENT, &params[i]);
    342  1.16       ryo 		if (ret == -1) {
    343  1.16       ryo 			err(EXIT_FAILURE, "TPROF_IOC_CONFIGURE_EVENT: %s",
    344  1.16       ryo 			    eventname[i]);
    345  1.16       ryo 		}
    346  1.14       ryo 	}
    347  1.14       ryo 
    348  1.14       ryo 	ret = ioctl(devfd, TPROF_IOC_START, &mask);
    349   1.1      yamt 	if (ret == -1) {
    350   1.3      yamt 		err(EXIT_FAILURE, "TPROF_IOC_START");
    351   1.1      yamt 	}
    352   1.1      yamt 
    353   1.1      yamt 	pid = fork();
    354   1.1      yamt 	switch (pid) {
    355   1.1      yamt 	case -1:
    356   1.1      yamt 		err(EXIT_FAILURE, "fork");
    357   1.1      yamt 	case 0:
    358   1.1      yamt 		close(devfd);
    359   1.1      yamt 		execvp(argv[0], argv);
    360   1.1      yamt 		_Exit(EXIT_FAILURE);
    361   1.1      yamt 	}
    362   1.1      yamt 
    363   1.1      yamt 	signal(SIGINT, SIG_IGN);
    364  1.15       ryo 	if (do_profile)
    365  1.15       ryo 		signal(SIGINFO, siginfo_showcount);
    366  1.15       ryo 	else
    367  1.15       ryo 		signal(SIGINFO, siginfo_nothing);
    368  1.15       ryo 
    369  1.15       ryo 	unsigned int done = 0;
    370  1.15       ryo 	if (do_profile)
    371  1.15       ryo 		ret = pthread_create(&pt, NULL, process_samples, NULL);
    372  1.15       ryo 	else
    373  1.15       ryo 		ret = pthread_create(&pt, NULL, process_stat, &done);
    374  1.15       ryo 	if (ret != 0)
    375   1.7      maxv 		errx(1, "pthread_create: %s", strerror(ret));
    376   1.1      yamt 
    377   1.1      yamt 	for (;;) {
    378   1.1      yamt 		int status;
    379   1.1      yamt 
    380   1.1      yamt 		pid = wait4(-1, &status, 0, NULL);
    381   1.1      yamt 		if (pid == -1) {
    382   1.1      yamt 			if (errno == ECHILD) {
    383   1.1      yamt 				break;
    384   1.1      yamt 			}
    385   1.1      yamt 			err(EXIT_FAILURE, "wait4");
    386   1.1      yamt 		}
    387   1.1      yamt 		if (pid != 0 && WIFEXITED(status)) {
    388   1.1      yamt 			break;
    389   1.1      yamt 		}
    390   1.1      yamt 	}
    391   1.1      yamt 
    392  1.14       ryo 	ret = ioctl(devfd, TPROF_IOC_STOP, &mask);
    393   1.1      yamt 	if (ret == -1) {
    394   1.1      yamt 		err(EXIT_FAILURE, "TPROF_IOC_STOP");
    395   1.1      yamt 	}
    396   1.1      yamt 
    397  1.15       ryo 	if (!do_profile) {
    398  1.15       ryo 		atomic_add_int(&done, 1);	/* terminate thread */
    399  1.15       ryo 		kill(0, SIGINFO);
    400  1.15       ryo 	}
    401  1.15       ryo 
    402   1.1      yamt 	pthread_join(pt, NULL);
    403   1.1      yamt 
    404  1.15       ryo 	if (do_profile) {
    405  1.15       ryo 		ret = ioctl(devfd, TPROF_IOC_GETSTAT, &ts);
    406  1.15       ryo 		if (ret == -1)
    407  1.15       ryo 			err(EXIT_FAILURE, "TPROF_IOC_GETSTAT");
    408  1.15       ryo 
    409  1.15       ryo 		fprintf(stderr, "\n%s statistics:\n", getprogname());
    410  1.15       ryo 		fprintf(stderr, "\tsample %" PRIu64 "\n", ts.ts_sample);
    411  1.15       ryo 		fprintf(stderr, "\toverflow %" PRIu64 "\n", ts.ts_overflow);
    412  1.15       ryo 		fprintf(stderr, "\tbuf %" PRIu64 "\n", ts.ts_buf);
    413  1.15       ryo 		fprintf(stderr, "\temptybuf %" PRIu64 "\n", ts.ts_emptybuf);
    414  1.15       ryo 		fprintf(stderr, "\tdropbuf %" PRIu64 "\n", ts.ts_dropbuf);
    415  1.16       ryo 		fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n",
    416  1.16       ryo 		    ts.ts_dropbuf_sample);
    417  1.15       ryo 
    418  1.15       ryo 		fprintf(stderr, "\n");
    419   1.1      yamt 	}
    420  1.15       ryo 	show_counters();
    421   1.1      yamt 
    422  1.15       ryo 	exit(EXIT_SUCCESS);
    423  1.15       ryo }
    424   1.1      yamt 
    425  1.15       ryo static void
    426  1.15       ryo tprof_monitor(int argc, char **argv)
    427  1.15       ryo {
    428  1.15       ryo 	tprof_monitor_common(true, argc, argv);
    429  1.15       ryo }
    430  1.15       ryo 
    431  1.15       ryo static void
    432  1.15       ryo tprof_count(int argc, char **argv)
    433  1.15       ryo {
    434  1.15       ryo 	tprof_monitor_common(false, argc, argv);
    435   1.1      yamt }
    436   1.7      maxv 
    437   1.7      maxv int
    438   1.7      maxv main(int argc, char *argv[])
    439   1.7      maxv {
    440   1.7      maxv 	const struct cmdtab *ct;
    441   1.7      maxv 	int ret;
    442   1.7      maxv 
    443  1.15       ryo 	getncpu();
    444   1.7      maxv 	setprogname(argv[0]);
    445   1.7      maxv 	argv += 1, argc -= 1;
    446   1.7      maxv 
    447   1.7      maxv 	devfd = open(_PATH_TPROF, O_RDWR);
    448   1.7      maxv 	if (devfd == -1) {
    449   1.7      maxv 		err(EXIT_FAILURE, "%s", _PATH_TPROF);
    450   1.7      maxv 	}
    451   1.7      maxv 
    452  1.14       ryo 	ret = ioctl(devfd, TPROF_IOC_GETINFO, &tprof_info);
    453   1.7      maxv 	if (ret == -1) {
    454   1.7      maxv 		err(EXIT_FAILURE, "TPROF_IOC_GETINFO");
    455   1.7      maxv 	}
    456  1.14       ryo 	if (tprof_info.ti_version != TPROF_VERSION) {
    457   1.7      maxv 		errx(EXIT_FAILURE, "version mismatch: version=%d, expected=%d",
    458  1.14       ryo 		    tprof_info.ti_version, TPROF_VERSION);
    459   1.7      maxv 	}
    460  1.14       ryo 	if (tprof_event_init(tprof_info.ti_ident) == -1) {
    461  1.13      maxv 		errx(EXIT_FAILURE, "cpu not supported");
    462   1.7      maxv 	}
    463   1.7      maxv 
    464  1.14       ryo 	ret = ioctl(devfd, TPROF_IOC_GETNCOUNTERS, &ncounters);
    465  1.14       ryo 	if (ret == -1) {
    466  1.14       ryo 		err(EXIT_FAILURE, "TPROF_IOC_GETNCOUNTERS");
    467  1.14       ryo 	}
    468  1.14       ryo 	if (ncounters == 0) {
    469  1.14       ryo 		errx(EXIT_FAILURE, "no available counters");
    470  1.14       ryo 	}
    471  1.14       ryo 
    472  1.11  jmcneill 	if (argc == 0)
    473  1.11  jmcneill 		usage();
    474  1.11  jmcneill 
    475   1.7      maxv 	for (ct = tprof_cmdtab; ct->label != NULL; ct++) {
    476   1.7      maxv 		if (strcmp(argv[0], ct->label) == 0) {
    477   1.7      maxv 			if (!ct->argsoptional &&
    478   1.7      maxv 			    ((ct->takesargs == 0) ^ (argv[1] == NULL)))
    479   1.7      maxv 			{
    480   1.7      maxv 				usage();
    481   1.7      maxv 			}
    482   1.7      maxv 			(*ct->func)(argc, argv);
    483   1.7      maxv 			break;
    484   1.7      maxv 		}
    485   1.7      maxv 	}
    486   1.9      maxv 	if (ct->label == NULL) {
    487   1.9      maxv 		usage();
    488   1.9      maxv 	}
    489   1.7      maxv }
    490