Home | History | Annotate | Line # | Download | only in tprof
tprof.c revision 1.2.2.2
      1  1.2.2.2  matt /*	$NetBSD: tprof.c,v 1.2.2.2 2008/01/09 02:02:35 matt Exp $	*/
      2  1.2.2.2  matt 
      3  1.2.2.2  matt /*-
      4  1.2.2.2  matt  * Copyright (c)2008 YAMAMOTO Takashi,
      5  1.2.2.2  matt  * All rights reserved.
      6  1.2.2.2  matt  *
      7  1.2.2.2  matt  * Redistribution and use in source and binary forms, with or without
      8  1.2.2.2  matt  * modification, are permitted provided that the following conditions
      9  1.2.2.2  matt  * are met:
     10  1.2.2.2  matt  * 1. Redistributions of source code must retain the above copyright
     11  1.2.2.2  matt  *    notice, this list of conditions and the following disclaimer.
     12  1.2.2.2  matt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.2.2  matt  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.2.2  matt  *    documentation and/or other materials provided with the distribution.
     15  1.2.2.2  matt  *
     16  1.2.2.2  matt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.2.2.2  matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.2.2.2  matt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.2.2.2  matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.2.2.2  matt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.2.2.2  matt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.2.2.2  matt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.2.2.2  matt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.2.2.2  matt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.2.2.2  matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.2.2.2  matt  * SUCH DAMAGE.
     27  1.2.2.2  matt  */
     28  1.2.2.2  matt 
     29  1.2.2.2  matt #include <sys/cdefs.h>
     30  1.2.2.2  matt #ifndef lint
     31  1.2.2.2  matt __RCSID("$NetBSD: tprof.c,v 1.2.2.2 2008/01/09 02:02:35 matt Exp $");
     32  1.2.2.2  matt #endif /* not lint */
     33  1.2.2.2  matt 
     34  1.2.2.2  matt #include <sys/ioctl.h>
     35  1.2.2.2  matt #include <sys/wait.h>
     36  1.2.2.2  matt 
     37  1.2.2.2  matt #include <dev/tprof/tprof_ioctl.h>
     38  1.2.2.2  matt 
     39  1.2.2.2  matt #include <err.h>
     40  1.2.2.2  matt #include <errno.h>
     41  1.2.2.2  matt #include <fcntl.h>
     42  1.2.2.2  matt #include <inttypes.h>
     43  1.2.2.2  matt #include <pthread.h>
     44  1.2.2.2  matt #include <signal.h>
     45  1.2.2.2  matt #include <stdbool.h>
     46  1.2.2.2  matt #include <stdio.h>
     47  1.2.2.2  matt #include <stdlib.h>
     48  1.2.2.2  matt #include <string.h>
     49  1.2.2.2  matt #include <unistd.h>
     50  1.2.2.2  matt 
     51  1.2.2.2  matt #define	_PATH_TPROF	"/dev/tprof"
     52  1.2.2.2  matt 
     53  1.2.2.2  matt int devfd;
     54  1.2.2.2  matt int outfd;
     55  1.2.2.2  matt 
     56  1.2.2.2  matt static void
     57  1.2.2.2  matt usage(void)
     58  1.2.2.2  matt {
     59  1.2.2.2  matt 
     60  1.2.2.2  matt 	fprintf(stderr, "%s [options] command ...\n", getprogname());
     61  1.2.2.2  matt 	fprintf(stderr, "\n");
     62  1.2.2.2  matt 	fprintf(stderr, "-o filename\t"
     63  1.2.2.2  matt 	    "output to the file.  [default: -o tprof.out]\n");
     64  1.2.2.2  matt 	fprintf(stderr, "-c\t\t"
     65  1.2.2.2  matt 	    "output to stdout.  NOTE: the output is a binary stream.\n");
     66  1.2.2.2  matt 
     67  1.2.2.2  matt 	exit(EXIT_FAILURE);
     68  1.2.2.2  matt }
     69  1.2.2.2  matt 
     70  1.2.2.2  matt static void *
     71  1.2.2.2  matt process_samples(void *dummy)
     72  1.2.2.2  matt {
     73  1.2.2.2  matt 
     74  1.2.2.2  matt 	for (;;) {
     75  1.2.2.2  matt 		char buf[4096];
     76  1.2.2.2  matt 		const char *cp;
     77  1.2.2.2  matt 		ssize_t ssz;
     78  1.2.2.2  matt 
     79  1.2.2.2  matt 		ssz = read(devfd, buf, sizeof(buf));
     80  1.2.2.2  matt 		if (ssz == -1) {
     81  1.2.2.2  matt 			err(EXIT_FAILURE, "read");
     82  1.2.2.2  matt 		}
     83  1.2.2.2  matt 		if (ssz == 0) {
     84  1.2.2.2  matt 			break;
     85  1.2.2.2  matt 		}
     86  1.2.2.2  matt 		cp = buf;
     87  1.2.2.2  matt 		while (ssz) {
     88  1.2.2.2  matt 			ssize_t wsz;
     89  1.2.2.2  matt 
     90  1.2.2.2  matt 			wsz = write(outfd, cp, ssz);
     91  1.2.2.2  matt 			if (wsz == -1) {
     92  1.2.2.2  matt 				err(EXIT_FAILURE, "write");
     93  1.2.2.2  matt 			}
     94  1.2.2.2  matt 			ssz -= wsz;
     95  1.2.2.2  matt 			cp += wsz;
     96  1.2.2.2  matt 		}
     97  1.2.2.2  matt 	}
     98  1.2.2.2  matt 	return NULL;
     99  1.2.2.2  matt }
    100  1.2.2.2  matt 
    101  1.2.2.2  matt int
    102  1.2.2.2  matt main(int argc, char *argv[])
    103  1.2.2.2  matt {
    104  1.2.2.2  matt 	struct tprof_param param;
    105  1.2.2.2  matt 	struct tprof_stat ts;
    106  1.2.2.2  matt 	const char *outfile = "tprof.out";
    107  1.2.2.2  matt 	bool cflag = false;
    108  1.2.2.2  matt 	pid_t pid;
    109  1.2.2.2  matt 	pthread_t pt;
    110  1.2.2.2  matt 	int error;
    111  1.2.2.2  matt 	int ret;
    112  1.2.2.2  matt 	int ch;
    113  1.2.2.2  matt 	int version;
    114  1.2.2.2  matt 
    115  1.2.2.2  matt 	while ((ch = getopt(argc, argv, "co:")) != -1) {
    116  1.2.2.2  matt 		switch (ch) {
    117  1.2.2.2  matt 		case 'c':
    118  1.2.2.2  matt 			cflag = true;
    119  1.2.2.2  matt 			break;
    120  1.2.2.2  matt 		case 'o':
    121  1.2.2.2  matt 			outfile = optarg;
    122  1.2.2.2  matt 			break;
    123  1.2.2.2  matt 		default:
    124  1.2.2.2  matt 			usage();
    125  1.2.2.2  matt 		}
    126  1.2.2.2  matt 	}
    127  1.2.2.2  matt 	argc -= optind;
    128  1.2.2.2  matt 	argv += optind;
    129  1.2.2.2  matt 	if (argc == 0) {
    130  1.2.2.2  matt 		usage();
    131  1.2.2.2  matt 	}
    132  1.2.2.2  matt 
    133  1.2.2.2  matt 	if (cflag) {
    134  1.2.2.2  matt 		outfd = STDOUT_FILENO;
    135  1.2.2.2  matt 	} else {
    136  1.2.2.2  matt 		outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    137  1.2.2.2  matt 		if (outfd == -1) {
    138  1.2.2.2  matt 			err(EXIT_FAILURE, "%s", optarg);
    139  1.2.2.2  matt 		}
    140  1.2.2.2  matt 	}
    141  1.2.2.2  matt 
    142  1.2.2.2  matt 	devfd = open(_PATH_TPROF, O_RDWR);
    143  1.2.2.2  matt 	if (devfd == -1) {
    144  1.2.2.2  matt 		err(EXIT_SUCCESS, "%s", _PATH_TPROF);
    145  1.2.2.2  matt 	}
    146  1.2.2.2  matt 
    147  1.2.2.2  matt 	ret = ioctl(devfd, TPROF_IOC_GETVERSION, &version);
    148  1.2.2.2  matt 	if (ret == -1) {
    149  1.2.2.2  matt 		err(EXIT_SUCCESS, "TPROF_IOC_GETVERSION");
    150  1.2.2.2  matt 	}
    151  1.2.2.2  matt 	if (version != TPROF_VERSION) {
    152  1.2.2.2  matt 		errx(EXIT_SUCCESS, "version mismatch: version=%d, expected=%d",
    153  1.2.2.2  matt 		    version, TPROF_VERSION);
    154  1.2.2.2  matt 	}
    155  1.2.2.2  matt 
    156  1.2.2.2  matt 	memset(&param, 0, sizeof(param));
    157  1.2.2.2  matt 	ret = ioctl(devfd, TPROF_IOC_START, &param);
    158  1.2.2.2  matt 	if (ret == -1) {
    159  1.2.2.2  matt 		err(EXIT_SUCCESS, "TPROF_IOC_START");
    160  1.2.2.2  matt 	}
    161  1.2.2.2  matt 
    162  1.2.2.2  matt 	pid = fork();
    163  1.2.2.2  matt 	switch (pid) {
    164  1.2.2.2  matt 	case -1:
    165  1.2.2.2  matt 		err(EXIT_FAILURE, "fork");
    166  1.2.2.2  matt 	case 0:
    167  1.2.2.2  matt 		close(devfd);
    168  1.2.2.2  matt 		execvp(argv[0], argv);
    169  1.2.2.2  matt 		_Exit(EXIT_FAILURE);
    170  1.2.2.2  matt 	}
    171  1.2.2.2  matt 
    172  1.2.2.2  matt 	signal(SIGINT, SIG_IGN);
    173  1.2.2.2  matt 
    174  1.2.2.2  matt 	error = pthread_create(&pt, NULL, process_samples, NULL);
    175  1.2.2.2  matt 	if (error != 0) {
    176  1.2.2.2  matt 		errx(1, "pthread_create: %s", strerror(error));
    177  1.2.2.2  matt 	}
    178  1.2.2.2  matt 
    179  1.2.2.2  matt 	for (;;) {
    180  1.2.2.2  matt 		int status;
    181  1.2.2.2  matt 
    182  1.2.2.2  matt 		pid = wait4(-1, &status, 0, NULL);
    183  1.2.2.2  matt 		if (pid == -1) {
    184  1.2.2.2  matt 			if (errno == ECHILD) {
    185  1.2.2.2  matt 				break;
    186  1.2.2.2  matt 			}
    187  1.2.2.2  matt 			err(EXIT_FAILURE, "wait4");
    188  1.2.2.2  matt 		}
    189  1.2.2.2  matt 		if (pid != 0 && WIFEXITED(status)) {
    190  1.2.2.2  matt 			break;
    191  1.2.2.2  matt 		}
    192  1.2.2.2  matt 	}
    193  1.2.2.2  matt 
    194  1.2.2.2  matt 	ret = ioctl(devfd, TPROF_IOC_STOP, NULL);
    195  1.2.2.2  matt 	if (ret == -1) {
    196  1.2.2.2  matt 		err(EXIT_FAILURE, "TPROF_IOC_STOP");
    197  1.2.2.2  matt 	}
    198  1.2.2.2  matt 
    199  1.2.2.2  matt 	pthread_join(pt, NULL);
    200  1.2.2.2  matt 
    201  1.2.2.2  matt 	ret = ioctl(devfd, TPROF_IOC_GETSTAT, &ts);
    202  1.2.2.2  matt 	if (ret == -1) {
    203  1.2.2.2  matt 		err(EXIT_FAILURE, "TPROF_IOC_GETSTAT");
    204  1.2.2.2  matt 	}
    205  1.2.2.2  matt 
    206  1.2.2.2  matt 	fprintf(stderr, "\n%s statistics:\n", getprogname());
    207  1.2.2.2  matt 	fprintf(stderr, "\tsample %" PRIu64 "\n", ts.ts_sample);
    208  1.2.2.2  matt 	fprintf(stderr, "\toverflow %" PRIu64 "\n", ts.ts_overflow);
    209  1.2.2.2  matt 	fprintf(stderr, "\tbuf %" PRIu64 "\n", ts.ts_buf);
    210  1.2.2.2  matt 	fprintf(stderr, "\temptybuf %" PRIu64 "\n", ts.ts_emptybuf);
    211  1.2.2.2  matt 	fprintf(stderr, "\tdropbuf %" PRIu64 "\n", ts.ts_dropbuf);
    212  1.2.2.2  matt 	fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n", ts.ts_dropbuf_sample);
    213  1.2.2.2  matt 
    214  1.2.2.2  matt 	exit(EXIT_SUCCESS);
    215  1.2.2.2  matt }
    216