Home | History | Annotate | Line # | Download | only in tprof
tprof.c revision 1.5.38.1
      1  1.5.38.1  pgoyette /*	$NetBSD: tprof.c,v 1.5.38.1 2018/07/28 04:38:15 pgoyette Exp $	*/
      2       1.1      yamt 
      3  1.5.38.1  pgoyette /*
      4  1.5.38.1  pgoyette  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.5.38.1  pgoyette  * All rights reserved.
      6  1.5.38.1  pgoyette  *
      7  1.5.38.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5.38.1  pgoyette  * by Maxime Villard.
      9  1.5.38.1  pgoyette  *
     10  1.5.38.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11  1.5.38.1  pgoyette  * modification, are permitted provided that the following conditions
     12  1.5.38.1  pgoyette  * are met:
     13  1.5.38.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14  1.5.38.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15  1.5.38.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5.38.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17  1.5.38.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18  1.5.38.1  pgoyette  *
     19  1.5.38.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.5.38.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.5.38.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.5.38.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.5.38.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.5.38.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.5.38.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.5.38.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.5.38.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.5.38.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.5.38.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30  1.5.38.1  pgoyette  */
     31  1.5.38.1  pgoyette 
     32  1.5.38.1  pgoyette /*
     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.5.38.1  pgoyette __RCSID("$NetBSD: tprof.c,v 1.5.38.1 2018/07/28 04:38:15 pgoyette 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.5.38.1  pgoyette #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.5.38.1  pgoyette static void tprof_list(int, char **);
     87  1.5.38.1  pgoyette static void tprof_monitor(int, char **) __dead;
     88  1.5.38.1  pgoyette 
     89  1.5.38.1  pgoyette static struct cmdtab {
     90  1.5.38.1  pgoyette 	const char *label;
     91  1.5.38.1  pgoyette 	bool takesargs;
     92  1.5.38.1  pgoyette 	bool argsoptional;
     93  1.5.38.1  pgoyette 	void (*func)(int, char **);
     94  1.5.38.1  pgoyette } const tprof_cmdtab[] = {
     95  1.5.38.1  pgoyette 	{ "list",	false, false, tprof_list },
     96  1.5.38.1  pgoyette 	{ "monitor",	true,  false, tprof_monitor },
     97  1.5.38.1  pgoyette 	{ "analyze",	true,  true,  tprof_analyze },
     98  1.5.38.1  pgoyette 	{ NULL,		false, false, NULL },
     99  1.5.38.1  pgoyette };
    100  1.5.38.1  pgoyette 
    101       1.5     joerg __dead static void
    102       1.1      yamt usage(void)
    103       1.1      yamt {
    104       1.1      yamt 
    105  1.5.38.1  pgoyette 	fprintf(stderr, "%s op [arguments]\n", getprogname());
    106       1.2      yamt 	fprintf(stderr, "\n");
    107  1.5.38.1  pgoyette 	fprintf(stderr, "\tlist\n");
    108  1.5.38.1  pgoyette 	fprintf(stderr, "\t\tList the available events.\n");
    109  1.5.38.1  pgoyette 	fprintf(stderr, "\tmonitor -e name:option [-o outfile] command\n");
    110  1.5.38.1  pgoyette 	fprintf(stderr, "\t\tMonitor the event 'name' with option 'option'\n"
    111  1.5.38.1  pgoyette 	    "\t\tcounted during the execution of 'command'.\n");
    112  1.5.38.1  pgoyette 	fprintf(stderr, "\tanalyze [-CkLPs] [-p pid] file\n");
    113  1.5.38.1  pgoyette 	fprintf(stderr, "\t\tAnalyze the samples of the file 'file'.\n");
    114       1.1      yamt 
    115       1.1      yamt 	exit(EXIT_FAILURE);
    116       1.1      yamt }
    117       1.1      yamt 
    118       1.1      yamt static void *
    119       1.1      yamt process_samples(void *dummy)
    120       1.1      yamt {
    121       1.1      yamt 
    122       1.1      yamt 	for (;;) {
    123       1.1      yamt 		char buf[4096];
    124       1.1      yamt 		const char *cp;
    125       1.1      yamt 		ssize_t ssz;
    126       1.1      yamt 
    127       1.1      yamt 		ssz = read(devfd, buf, sizeof(buf));
    128       1.1      yamt 		if (ssz == -1) {
    129       1.1      yamt 			err(EXIT_FAILURE, "read");
    130       1.1      yamt 		}
    131       1.1      yamt 		if (ssz == 0) {
    132       1.1      yamt 			break;
    133       1.1      yamt 		}
    134       1.1      yamt 		cp = buf;
    135       1.1      yamt 		while (ssz) {
    136       1.1      yamt 			ssize_t wsz;
    137       1.1      yamt 
    138       1.1      yamt 			wsz = write(outfd, cp, ssz);
    139       1.1      yamt 			if (wsz == -1) {
    140       1.1      yamt 				err(EXIT_FAILURE, "write");
    141       1.1      yamt 			}
    142       1.1      yamt 			ssz -= wsz;
    143       1.1      yamt 			cp += wsz;
    144       1.1      yamt 		}
    145       1.1      yamt 	}
    146       1.1      yamt 	return NULL;
    147       1.1      yamt }
    148       1.1      yamt 
    149  1.5.38.1  pgoyette static void
    150  1.5.38.1  pgoyette tprof_list(int argc, char **argv)
    151  1.5.38.1  pgoyette {
    152  1.5.38.1  pgoyette 	tprof_event_list();
    153  1.5.38.1  pgoyette }
    154  1.5.38.1  pgoyette 
    155  1.5.38.1  pgoyette static void
    156  1.5.38.1  pgoyette tprof_monitor(int argc, char **argv)
    157       1.1      yamt {
    158  1.5.38.1  pgoyette 	const char *outfile = "tprof.out";
    159       1.1      yamt 	struct tprof_param param;
    160       1.1      yamt 	struct tprof_stat ts;
    161       1.1      yamt 	pid_t pid;
    162       1.1      yamt 	pthread_t pt;
    163  1.5.38.1  pgoyette 	int ret, ch;
    164  1.5.38.1  pgoyette 	char *tokens[2];
    165  1.5.38.1  pgoyette 
    166  1.5.38.1  pgoyette 	memset(&param, 0, sizeof(param));
    167       1.1      yamt 
    168  1.5.38.1  pgoyette 	while ((ch = getopt(argc, argv, "o:e:")) != -1) {
    169       1.1      yamt 		switch (ch) {
    170       1.1      yamt 		case 'o':
    171       1.1      yamt 			outfile = optarg;
    172       1.1      yamt 			break;
    173  1.5.38.1  pgoyette 		case 'e':
    174  1.5.38.1  pgoyette 			tokens[0] = strtok(optarg, ":");
    175  1.5.38.1  pgoyette 			tokens[1] = strtok(NULL, ":");
    176  1.5.38.1  pgoyette 			if (tokens[1] == NULL)
    177  1.5.38.1  pgoyette 				usage();
    178  1.5.38.1  pgoyette 			tprof_event_lookup(tokens[0], &param);
    179  1.5.38.1  pgoyette 			if (strchr(tokens[1], 'u'))
    180  1.5.38.1  pgoyette 				param.p_flags |= TPROF_PARAM_USER;
    181  1.5.38.1  pgoyette 			if (strchr(tokens[1], 'k'))
    182  1.5.38.1  pgoyette 				param.p_flags |= TPROF_PARAM_KERN;
    183  1.5.38.1  pgoyette 			break;
    184       1.1      yamt 		default:
    185       1.1      yamt 			usage();
    186       1.1      yamt 		}
    187       1.1      yamt 	}
    188       1.1      yamt 	argc -= optind;
    189       1.1      yamt 	argv += optind;
    190       1.1      yamt 	if (argc == 0) {
    191       1.1      yamt 		usage();
    192       1.1      yamt 	}
    193       1.1      yamt 
    194  1.5.38.1  pgoyette 	if (param.p_flags == 0) {
    195  1.5.38.1  pgoyette 		usage();
    196       1.1      yamt 	}
    197       1.1      yamt 
    198  1.5.38.1  pgoyette 	outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    199  1.5.38.1  pgoyette 	if (outfd == -1) {
    200  1.5.38.1  pgoyette 		err(EXIT_FAILURE, "%s", outfile);
    201       1.1      yamt 	}
    202       1.1      yamt 
    203       1.1      yamt 	ret = ioctl(devfd, TPROF_IOC_START, &param);
    204       1.1      yamt 	if (ret == -1) {
    205       1.3      yamt 		err(EXIT_FAILURE, "TPROF_IOC_START");
    206       1.1      yamt 	}
    207       1.1      yamt 
    208       1.1      yamt 	pid = fork();
    209       1.1      yamt 	switch (pid) {
    210       1.1      yamt 	case -1:
    211       1.1      yamt 		err(EXIT_FAILURE, "fork");
    212       1.1      yamt 	case 0:
    213       1.1      yamt 		close(devfd);
    214       1.1      yamt 		execvp(argv[0], argv);
    215       1.1      yamt 		_Exit(EXIT_FAILURE);
    216       1.1      yamt 	}
    217       1.1      yamt 
    218       1.1      yamt 	signal(SIGINT, SIG_IGN);
    219       1.1      yamt 
    220  1.5.38.1  pgoyette 	ret = pthread_create(&pt, NULL, process_samples, NULL);
    221  1.5.38.1  pgoyette 	if (ret != 0) {
    222  1.5.38.1  pgoyette 		errx(1, "pthread_create: %s", strerror(ret));
    223       1.1      yamt 	}
    224       1.1      yamt 
    225       1.1      yamt 	for (;;) {
    226       1.1      yamt 		int status;
    227       1.1      yamt 
    228       1.1      yamt 		pid = wait4(-1, &status, 0, NULL);
    229       1.1      yamt 		if (pid == -1) {
    230       1.1      yamt 			if (errno == ECHILD) {
    231       1.1      yamt 				break;
    232       1.1      yamt 			}
    233       1.1      yamt 			err(EXIT_FAILURE, "wait4");
    234       1.1      yamt 		}
    235       1.1      yamt 		if (pid != 0 && WIFEXITED(status)) {
    236       1.1      yamt 			break;
    237       1.1      yamt 		}
    238       1.1      yamt 	}
    239       1.1      yamt 
    240       1.1      yamt 	ret = ioctl(devfd, TPROF_IOC_STOP, NULL);
    241       1.1      yamt 	if (ret == -1) {
    242       1.1      yamt 		err(EXIT_FAILURE, "TPROF_IOC_STOP");
    243       1.1      yamt 	}
    244       1.1      yamt 
    245       1.1      yamt 	pthread_join(pt, NULL);
    246       1.1      yamt 
    247       1.1      yamt 	ret = ioctl(devfd, TPROF_IOC_GETSTAT, &ts);
    248       1.1      yamt 	if (ret == -1) {
    249       1.1      yamt 		err(EXIT_FAILURE, "TPROF_IOC_GETSTAT");
    250       1.1      yamt 	}
    251       1.1      yamt 
    252       1.1      yamt 	fprintf(stderr, "\n%s statistics:\n", getprogname());
    253       1.1      yamt 	fprintf(stderr, "\tsample %" PRIu64 "\n", ts.ts_sample);
    254       1.1      yamt 	fprintf(stderr, "\toverflow %" PRIu64 "\n", ts.ts_overflow);
    255       1.1      yamt 	fprintf(stderr, "\tbuf %" PRIu64 "\n", ts.ts_buf);
    256       1.1      yamt 	fprintf(stderr, "\temptybuf %" PRIu64 "\n", ts.ts_emptybuf);
    257       1.1      yamt 	fprintf(stderr, "\tdropbuf %" PRIu64 "\n", ts.ts_dropbuf);
    258       1.1      yamt 	fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n", ts.ts_dropbuf_sample);
    259       1.1      yamt 
    260       1.1      yamt 	exit(EXIT_SUCCESS);
    261       1.1      yamt }
    262  1.5.38.1  pgoyette 
    263  1.5.38.1  pgoyette int
    264  1.5.38.1  pgoyette main(int argc, char *argv[])
    265  1.5.38.1  pgoyette {
    266  1.5.38.1  pgoyette 	struct tprof_info info;
    267  1.5.38.1  pgoyette 	const struct cmdtab *ct;
    268  1.5.38.1  pgoyette 	int ret;
    269  1.5.38.1  pgoyette 
    270  1.5.38.1  pgoyette 	setprogname(argv[0]);
    271  1.5.38.1  pgoyette 	argv += 1, argc -= 1;
    272  1.5.38.1  pgoyette 
    273  1.5.38.1  pgoyette 	devfd = open(_PATH_TPROF, O_RDWR);
    274  1.5.38.1  pgoyette 	if (devfd == -1) {
    275  1.5.38.1  pgoyette 		err(EXIT_FAILURE, "%s", _PATH_TPROF);
    276  1.5.38.1  pgoyette 	}
    277  1.5.38.1  pgoyette 
    278  1.5.38.1  pgoyette 	ret = ioctl(devfd, TPROF_IOC_GETINFO, &info);
    279  1.5.38.1  pgoyette 	if (ret == -1) {
    280  1.5.38.1  pgoyette 		err(EXIT_FAILURE, "TPROF_IOC_GETINFO");
    281  1.5.38.1  pgoyette 	}
    282  1.5.38.1  pgoyette 	if (info.ti_version != TPROF_VERSION) {
    283  1.5.38.1  pgoyette 		errx(EXIT_FAILURE, "version mismatch: version=%d, expected=%d",
    284  1.5.38.1  pgoyette 		    info.ti_version, TPROF_VERSION);
    285  1.5.38.1  pgoyette 	}
    286  1.5.38.1  pgoyette 	if (tprof_event_init(info.ti_ident) == -1) {
    287  1.5.38.1  pgoyette 		errx(EXIT_FAILURE, "cpu not supported");
    288  1.5.38.1  pgoyette 	}
    289  1.5.38.1  pgoyette 
    290  1.5.38.1  pgoyette 	if (argc == 0)
    291  1.5.38.1  pgoyette 		usage();
    292  1.5.38.1  pgoyette 
    293  1.5.38.1  pgoyette 	for (ct = tprof_cmdtab; ct->label != NULL; ct++) {
    294  1.5.38.1  pgoyette 		if (strcmp(argv[0], ct->label) == 0) {
    295  1.5.38.1  pgoyette 			if (!ct->argsoptional &&
    296  1.5.38.1  pgoyette 			    ((ct->takesargs == 0) ^ (argv[1] == NULL)))
    297  1.5.38.1  pgoyette 			{
    298  1.5.38.1  pgoyette 				usage();
    299  1.5.38.1  pgoyette 			}
    300  1.5.38.1  pgoyette 			(*ct->func)(argc, argv);
    301  1.5.38.1  pgoyette 			break;
    302  1.5.38.1  pgoyette 		}
    303  1.5.38.1  pgoyette 	}
    304  1.5.38.1  pgoyette 	if (ct->label == NULL) {
    305  1.5.38.1  pgoyette 		usage();
    306  1.5.38.1  pgoyette 	}
    307  1.5.38.1  pgoyette }
    308