1 /* $NetBSD: tprof.c,v 1.3 2009/01/03 07:20:57 yamt Exp $ */
4 * Copyright (c)2008 YAMAMOTO Takashi,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: tprof.c,v 1.3 2009/01/03 07:20:57 yamt Exp $");
34 #include <sys/ioctl.h>
37 #include <dev/tprof/tprof_ioctl.h>
51 #define _PATH_TPROF "/dev/tprof"
60 fprintf(stderr
, "%s [options] command ...\n", getprogname());
61 fprintf(stderr
, "\n");
62 fprintf(stderr
, "-o filename\t"
63 "output to the file. [default: -o tprof.out]\n");
64 fprintf(stderr
, "-c\t\t"
65 "output to stdout. NOTE: the output is a binary stream.\n");
71 process_samples(void *dummy
)
79 ssz
= read(devfd
, buf
, sizeof(buf
));
81 err(EXIT_FAILURE
, "read");
90 wsz
= write(outfd
, cp
, ssz
);
92 err(EXIT_FAILURE
, "write");
102 main(int argc
, char *argv
[])
104 struct tprof_param param
;
105 struct tprof_stat ts
;
106 const char *outfile
= "tprof.out";
115 while ((ch
= getopt(argc
, argv
, "co:")) != -1) {
134 outfd
= STDOUT_FILENO
;
136 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
138 err(EXIT_FAILURE
, "%s", outfile
);
142 devfd
= open(_PATH_TPROF
, O_RDWR
);
144 err(EXIT_FAILURE
, "%s", _PATH_TPROF
);
147 ret
= ioctl(devfd
, TPROF_IOC_GETVERSION
, &version
);
149 err(EXIT_FAILURE
, "TPROF_IOC_GETVERSION");
151 if (version
!= TPROF_VERSION
) {
152 errx(EXIT_FAILURE
, "version mismatch: version=%d, expected=%d",
153 version
, TPROF_VERSION
);
156 memset(¶m
, 0, sizeof(param
));
157 ret
= ioctl(devfd
, TPROF_IOC_START
, ¶m
);
159 err(EXIT_FAILURE
, "TPROF_IOC_START");
165 err(EXIT_FAILURE
, "fork");
168 execvp(argv
[0], argv
);
172 signal(SIGINT
, SIG_IGN
);
174 error
= pthread_create(&pt
, NULL
, process_samples
, NULL
);
176 errx(1, "pthread_create: %s", strerror(error
));
182 pid
= wait4(-1, &status
, 0, NULL
);
184 if (errno
== ECHILD
) {
187 err(EXIT_FAILURE
, "wait4");
189 if (pid
!= 0 && WIFEXITED(status
)) {
194 ret
= ioctl(devfd
, TPROF_IOC_STOP
, NULL
);
196 err(EXIT_FAILURE
, "TPROF_IOC_STOP");
199 pthread_join(pt
, NULL
);
201 ret
= ioctl(devfd
, TPROF_IOC_GETSTAT
, &ts
);
203 err(EXIT_FAILURE
, "TPROF_IOC_GETSTAT");
206 fprintf(stderr
, "\n%s statistics:\n", getprogname());
207 fprintf(stderr
, "\tsample %" PRIu64
"\n", ts
.ts_sample
);
208 fprintf(stderr
, "\toverflow %" PRIu64
"\n", ts
.ts_overflow
);
209 fprintf(stderr
, "\tbuf %" PRIu64
"\n", ts
.ts_buf
);
210 fprintf(stderr
, "\temptybuf %" PRIu64
"\n", ts
.ts_emptybuf
);
211 fprintf(stderr
, "\tdropbuf %" PRIu64
"\n", ts
.ts_dropbuf
);
212 fprintf(stderr
, "\tdropbuf_sample %" PRIu64
"\n", ts
.ts_dropbuf_sample
);