Home | History | Annotate | Line # | Download | only in tprof
tprof.c revision 1.13
      1  1.13      maxv /*	$NetBSD: tprof.c,v 1.13 2018/07/24 09:50:37 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.13      maxv __RCSID("$NetBSD: tprof.c,v 1.13 2018/07/24 09:50:37 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.10     joerg static void tprof_monitor(int, char **) __dead;
     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.8      maxv 	{ "analyze",	true,  true,  tprof_analyze },
     98   1.7      maxv 	{ NULL,		false, false, NULL },
     99   1.7      maxv };
    100   1.7      maxv 
    101   1.5     joerg __dead static void
    102   1.1      yamt usage(void)
    103   1.1      yamt {
    104   1.1      yamt 
    105   1.8      maxv 	fprintf(stderr, "%s op [arguments]\n", getprogname());
    106   1.2      yamt 	fprintf(stderr, "\n");
    107   1.7      maxv 	fprintf(stderr, "\tlist\n");
    108   1.7      maxv 	fprintf(stderr, "\t\tList the available events.\n");
    109   1.7      maxv 	fprintf(stderr, "\tmonitor -e name:option [-o outfile] command\n");
    110   1.7      maxv 	fprintf(stderr, "\t\tMonitor the event 'name' with option 'option'\n"
    111   1.7      maxv 	    "\t\tcounted during the execution of 'command'.\n");
    112  1.12       wiz 	fprintf(stderr, "\tanalyze [-CkLPs] [-p pid] file\n");
    113   1.9      maxv 	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.7      maxv static void
    150   1.7      maxv tprof_list(int argc, char **argv)
    151   1.7      maxv {
    152   1.7      maxv 	tprof_event_list();
    153   1.7      maxv }
    154   1.7      maxv 
    155   1.7      maxv static void
    156   1.7      maxv tprof_monitor(int argc, char **argv)
    157   1.1      yamt {
    158   1.7      maxv 	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.7      maxv 	int ret, ch;
    164   1.6      maxv 	char *tokens[2];
    165   1.6      maxv 
    166   1.6      maxv 	memset(&param, 0, sizeof(param));
    167   1.6      maxv 
    168   1.7      maxv 	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.6      maxv 		case 'e':
    174   1.6      maxv 			tokens[0] = strtok(optarg, ":");
    175   1.6      maxv 			tokens[1] = strtok(NULL, ":");
    176   1.6      maxv 			if (tokens[1] == NULL)
    177   1.6      maxv 				usage();
    178   1.6      maxv 			tprof_event_lookup(tokens[0], &param);
    179   1.6      maxv 			if (strchr(tokens[1], 'u'))
    180   1.6      maxv 				param.p_flags |= TPROF_PARAM_USER;
    181   1.6      maxv 			if (strchr(tokens[1], 'k'))
    182   1.6      maxv 				param.p_flags |= TPROF_PARAM_KERN;
    183   1.6      maxv 			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.6      maxv 	if (param.p_flags == 0) {
    195   1.6      maxv 		usage();
    196   1.6      maxv 	}
    197   1.6      maxv 
    198   1.7      maxv 	outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    199   1.7      maxv 	if (outfd == -1) {
    200   1.7      maxv 		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.7      maxv 	ret = pthread_create(&pt, NULL, process_samples, NULL);
    221   1.7      maxv 	if (ret != 0) {
    222   1.7      maxv 		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.7      maxv 
    263   1.7      maxv int
    264   1.7      maxv main(int argc, char *argv[])
    265   1.7      maxv {
    266   1.7      maxv 	struct tprof_info info;
    267   1.7      maxv 	const struct cmdtab *ct;
    268   1.7      maxv 	int ret;
    269   1.7      maxv 
    270   1.7      maxv 	setprogname(argv[0]);
    271   1.7      maxv 	argv += 1, argc -= 1;
    272   1.7      maxv 
    273   1.7      maxv 	devfd = open(_PATH_TPROF, O_RDWR);
    274   1.7      maxv 	if (devfd == -1) {
    275   1.7      maxv 		err(EXIT_FAILURE, "%s", _PATH_TPROF);
    276   1.7      maxv 	}
    277   1.7      maxv 
    278   1.7      maxv 	ret = ioctl(devfd, TPROF_IOC_GETINFO, &info);
    279   1.7      maxv 	if (ret == -1) {
    280   1.7      maxv 		err(EXIT_FAILURE, "TPROF_IOC_GETINFO");
    281   1.7      maxv 	}
    282   1.7      maxv 	if (info.ti_version != TPROF_VERSION) {
    283   1.7      maxv 		errx(EXIT_FAILURE, "version mismatch: version=%d, expected=%d",
    284   1.7      maxv 		    info.ti_version, TPROF_VERSION);
    285   1.7      maxv 	}
    286   1.7      maxv 	if (tprof_event_init(info.ti_ident) == -1) {
    287  1.13      maxv 		errx(EXIT_FAILURE, "cpu not supported");
    288   1.7      maxv 	}
    289   1.7      maxv 
    290  1.11  jmcneill 	if (argc == 0)
    291  1.11  jmcneill 		usage();
    292  1.11  jmcneill 
    293   1.7      maxv 	for (ct = tprof_cmdtab; ct->label != NULL; ct++) {
    294   1.7      maxv 		if (strcmp(argv[0], ct->label) == 0) {
    295   1.7      maxv 			if (!ct->argsoptional &&
    296   1.7      maxv 			    ((ct->takesargs == 0) ^ (argv[1] == NULL)))
    297   1.7      maxv 			{
    298   1.7      maxv 				usage();
    299   1.7      maxv 			}
    300   1.7      maxv 			(*ct->func)(argc, argv);
    301   1.7      maxv 			break;
    302   1.7      maxv 		}
    303   1.7      maxv 	}
    304   1.9      maxv 	if (ct->label == NULL) {
    305   1.9      maxv 		usage();
    306   1.9      maxv 	}
    307   1.7      maxv }
    308