Home | History | Annotate | Line # | Download | only in tprof
tprof.c revision 1.7
      1  1.7   maxv /*	$NetBSD: tprof.c,v 1.7 2018/07/13 09:04:31 maxv 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.7   maxv __RCSID("$NetBSD: tprof.c,v 1.7 2018/07/13 09:04:31 maxv Exp $");
     61  1.1   yamt #endif /* not lint */
     62  1.1   yamt 
     63  1.1   yamt #include <sys/ioctl.h>
     64  1.1   yamt #include <sys/wait.h>
     65  1.1   yamt 
     66  1.1   yamt #include <dev/tprof/tprof_ioctl.h>
     67  1.1   yamt 
     68  1.1   yamt #include <err.h>
     69  1.1   yamt #include <errno.h>
     70  1.1   yamt #include <fcntl.h>
     71  1.1   yamt #include <inttypes.h>
     72  1.1   yamt #include <pthread.h>
     73  1.1   yamt #include <signal.h>
     74  1.1   yamt #include <stdbool.h>
     75  1.1   yamt #include <stdio.h>
     76  1.1   yamt #include <stdlib.h>
     77  1.1   yamt #include <string.h>
     78  1.1   yamt #include <unistd.h>
     79  1.6   maxv #include "tprof.h"
     80  1.1   yamt 
     81  1.1   yamt #define	_PATH_TPROF	"/dev/tprof"
     82  1.1   yamt 
     83  1.1   yamt int devfd;
     84  1.1   yamt int outfd;
     85  1.1   yamt 
     86  1.7   maxv static void tprof_list(int, char **);
     87  1.7   maxv static void tprof_monitor(int, char **);
     88  1.7   maxv 
     89  1.7   maxv static struct cmdtab {
     90  1.7   maxv 	const char *label;
     91  1.7   maxv 	bool takesargs;
     92  1.7   maxv 	bool argsoptional;
     93  1.7   maxv 	void (*func)(int, char **);
     94  1.7   maxv } const tprof_cmdtab[] = {
     95  1.7   maxv 	{ "list",	false, false, tprof_list },
     96  1.7   maxv 	{ "monitor",	true,  false, tprof_monitor },
     97  1.7   maxv 	{ NULL,		false, false, NULL },
     98  1.7   maxv };
     99  1.7   maxv 
    100  1.5  joerg __dead static void
    101  1.1   yamt usage(void)
    102  1.1   yamt {
    103  1.1   yamt 
    104  1.7   maxv 	fprintf(stderr, "%s [op] [options] [command]\n", getprogname());
    105  1.2   yamt 	fprintf(stderr, "\n");
    106  1.7   maxv 	fprintf(stderr, "\tlist\n");
    107  1.7   maxv 	fprintf(stderr, "\t\tList the available events.\n");
    108  1.7   maxv 	fprintf(stderr, "\tmonitor -e name:option [-o outfile] command\n");
    109  1.7   maxv 	fprintf(stderr, "\t\tMonitor the event 'name' with option 'option'\n"
    110  1.7   maxv 	    "\t\tcounted during the execution of 'command'.\n");
    111  1.1   yamt 
    112  1.1   yamt 	exit(EXIT_FAILURE);
    113  1.1   yamt }
    114  1.1   yamt 
    115  1.1   yamt static void *
    116  1.1   yamt process_samples(void *dummy)
    117  1.1   yamt {
    118  1.1   yamt 
    119  1.1   yamt 	for (;;) {
    120  1.1   yamt 		char buf[4096];
    121  1.1   yamt 		const char *cp;
    122  1.1   yamt 		ssize_t ssz;
    123  1.1   yamt 
    124  1.1   yamt 		ssz = read(devfd, buf, sizeof(buf));
    125  1.1   yamt 		if (ssz == -1) {
    126  1.1   yamt 			err(EXIT_FAILURE, "read");
    127  1.1   yamt 		}
    128  1.1   yamt 		if (ssz == 0) {
    129  1.1   yamt 			break;
    130  1.1   yamt 		}
    131  1.1   yamt 		cp = buf;
    132  1.1   yamt 		while (ssz) {
    133  1.1   yamt 			ssize_t wsz;
    134  1.1   yamt 
    135  1.1   yamt 			wsz = write(outfd, cp, ssz);
    136  1.1   yamt 			if (wsz == -1) {
    137  1.1   yamt 				err(EXIT_FAILURE, "write");
    138  1.1   yamt 			}
    139  1.1   yamt 			ssz -= wsz;
    140  1.1   yamt 			cp += wsz;
    141  1.1   yamt 		}
    142  1.1   yamt 	}
    143  1.1   yamt 	return NULL;
    144  1.1   yamt }
    145  1.1   yamt 
    146  1.7   maxv static void
    147  1.7   maxv tprof_list(int argc, char **argv)
    148  1.7   maxv {
    149  1.7   maxv 	tprof_event_list();
    150  1.7   maxv }
    151  1.7   maxv 
    152  1.7   maxv static void
    153  1.7   maxv tprof_monitor(int argc, char **argv)
    154  1.1   yamt {
    155  1.7   maxv 	const char *outfile = "tprof.out";
    156  1.1   yamt 	struct tprof_param param;
    157  1.1   yamt 	struct tprof_stat ts;
    158  1.1   yamt 	pid_t pid;
    159  1.1   yamt 	pthread_t pt;
    160  1.7   maxv 	int ret, ch;
    161  1.6   maxv 	char *tokens[2];
    162  1.6   maxv 
    163  1.6   maxv 	memset(&param, 0, sizeof(param));
    164  1.6   maxv 
    165  1.7   maxv 	while ((ch = getopt(argc, argv, "o:e:")) != -1) {
    166  1.1   yamt 		switch (ch) {
    167  1.1   yamt 		case 'o':
    168  1.1   yamt 			outfile = optarg;
    169  1.1   yamt 			break;
    170  1.6   maxv 		case 'e':
    171  1.6   maxv 			tokens[0] = strtok(optarg, ":");
    172  1.6   maxv 			tokens[1] = strtok(NULL, ":");
    173  1.6   maxv 			if (tokens[1] == NULL)
    174  1.6   maxv 				usage();
    175  1.6   maxv 			tprof_event_lookup(tokens[0], &param);
    176  1.6   maxv 			if (strchr(tokens[1], 'u'))
    177  1.6   maxv 				param.p_flags |= TPROF_PARAM_USER;
    178  1.6   maxv 			if (strchr(tokens[1], 'k'))
    179  1.6   maxv 				param.p_flags |= TPROF_PARAM_KERN;
    180  1.6   maxv 			break;
    181  1.1   yamt 		default:
    182  1.1   yamt 			usage();
    183  1.1   yamt 		}
    184  1.1   yamt 	}
    185  1.1   yamt 	argc -= optind;
    186  1.1   yamt 	argv += optind;
    187  1.1   yamt 	if (argc == 0) {
    188  1.1   yamt 		usage();
    189  1.1   yamt 	}
    190  1.1   yamt 
    191  1.6   maxv 	if (param.p_flags == 0) {
    192  1.6   maxv 		usage();
    193  1.6   maxv 	}
    194  1.6   maxv 
    195  1.7   maxv 	outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    196  1.7   maxv 	if (outfd == -1) {
    197  1.7   maxv 		err(EXIT_FAILURE, "%s", outfile);
    198  1.1   yamt 	}
    199  1.1   yamt 
    200  1.1   yamt 	ret = ioctl(devfd, TPROF_IOC_START, &param);
    201  1.1   yamt 	if (ret == -1) {
    202  1.3   yamt 		err(EXIT_FAILURE, "TPROF_IOC_START");
    203  1.1   yamt 	}
    204  1.1   yamt 
    205  1.1   yamt 	pid = fork();
    206  1.1   yamt 	switch (pid) {
    207  1.1   yamt 	case -1:
    208  1.1   yamt 		err(EXIT_FAILURE, "fork");
    209  1.1   yamt 	case 0:
    210  1.1   yamt 		close(devfd);
    211  1.1   yamt 		execvp(argv[0], argv);
    212  1.1   yamt 		_Exit(EXIT_FAILURE);
    213  1.1   yamt 	}
    214  1.1   yamt 
    215  1.1   yamt 	signal(SIGINT, SIG_IGN);
    216  1.1   yamt 
    217  1.7   maxv 	ret = pthread_create(&pt, NULL, process_samples, NULL);
    218  1.7   maxv 	if (ret != 0) {
    219  1.7   maxv 		errx(1, "pthread_create: %s", strerror(ret));
    220  1.1   yamt 	}
    221  1.1   yamt 
    222  1.1   yamt 	for (;;) {
    223  1.1   yamt 		int status;
    224  1.1   yamt 
    225  1.1   yamt 		pid = wait4(-1, &status, 0, NULL);
    226  1.1   yamt 		if (pid == -1) {
    227  1.1   yamt 			if (errno == ECHILD) {
    228  1.1   yamt 				break;
    229  1.1   yamt 			}
    230  1.1   yamt 			err(EXIT_FAILURE, "wait4");
    231  1.1   yamt 		}
    232  1.1   yamt 		if (pid != 0 && WIFEXITED(status)) {
    233  1.1   yamt 			break;
    234  1.1   yamt 		}
    235  1.1   yamt 	}
    236  1.1   yamt 
    237  1.1   yamt 	ret = ioctl(devfd, TPROF_IOC_STOP, NULL);
    238  1.1   yamt 	if (ret == -1) {
    239  1.1   yamt 		err(EXIT_FAILURE, "TPROF_IOC_STOP");
    240  1.1   yamt 	}
    241  1.1   yamt 
    242  1.1   yamt 	pthread_join(pt, NULL);
    243  1.1   yamt 
    244  1.1   yamt 	ret = ioctl(devfd, TPROF_IOC_GETSTAT, &ts);
    245  1.1   yamt 	if (ret == -1) {
    246  1.1   yamt 		err(EXIT_FAILURE, "TPROF_IOC_GETSTAT");
    247  1.1   yamt 	}
    248  1.1   yamt 
    249  1.1   yamt 	fprintf(stderr, "\n%s statistics:\n", getprogname());
    250  1.1   yamt 	fprintf(stderr, "\tsample %" PRIu64 "\n", ts.ts_sample);
    251  1.1   yamt 	fprintf(stderr, "\toverflow %" PRIu64 "\n", ts.ts_overflow);
    252  1.1   yamt 	fprintf(stderr, "\tbuf %" PRIu64 "\n", ts.ts_buf);
    253  1.1   yamt 	fprintf(stderr, "\temptybuf %" PRIu64 "\n", ts.ts_emptybuf);
    254  1.1   yamt 	fprintf(stderr, "\tdropbuf %" PRIu64 "\n", ts.ts_dropbuf);
    255  1.1   yamt 	fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n", ts.ts_dropbuf_sample);
    256  1.1   yamt 
    257  1.1   yamt 	exit(EXIT_SUCCESS);
    258  1.1   yamt }
    259  1.7   maxv 
    260  1.7   maxv int
    261  1.7   maxv main(int argc, char *argv[])
    262  1.7   maxv {
    263  1.7   maxv 	struct tprof_info info;
    264  1.7   maxv 	const struct cmdtab *ct;
    265  1.7   maxv 	int ret;
    266  1.7   maxv 
    267  1.7   maxv 	setprogname(argv[0]);
    268  1.7   maxv 	argv += 1, argc -= 1;
    269  1.7   maxv 
    270  1.7   maxv 	devfd = open(_PATH_TPROF, O_RDWR);
    271  1.7   maxv 	if (devfd == -1) {
    272  1.7   maxv 		err(EXIT_FAILURE, "%s", _PATH_TPROF);
    273  1.7   maxv 	}
    274  1.7   maxv 
    275  1.7   maxv 	ret = ioctl(devfd, TPROF_IOC_GETINFO, &info);
    276  1.7   maxv 	if (ret == -1) {
    277  1.7   maxv 		err(EXIT_FAILURE, "TPROF_IOC_GETINFO");
    278  1.7   maxv 	}
    279  1.7   maxv 	if (info.ti_version != TPROF_VERSION) {
    280  1.7   maxv 		errx(EXIT_FAILURE, "version mismatch: version=%d, expected=%d",
    281  1.7   maxv 		    info.ti_version, TPROF_VERSION);
    282  1.7   maxv 	}
    283  1.7   maxv 	if (tprof_event_init(info.ti_ident) == -1) {
    284  1.7   maxv 		err(EXIT_FAILURE, "cpu not supported");
    285  1.7   maxv 	}
    286  1.7   maxv 
    287  1.7   maxv 	for (ct = tprof_cmdtab; ct->label != NULL; ct++) {
    288  1.7   maxv 		if (strcmp(argv[0], ct->label) == 0) {
    289  1.7   maxv 			if (!ct->argsoptional &&
    290  1.7   maxv 			    ((ct->takesargs == 0) ^ (argv[1] == NULL)))
    291  1.7   maxv 			{
    292  1.7   maxv 				usage();
    293  1.7   maxv 			}
    294  1.7   maxv 			(*ct->func)(argc, argv);
    295  1.7   maxv 			break;
    296  1.7   maxv 		}
    297  1.7   maxv 	}
    298  1.7   maxv }
    299