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