tprof.c revision 1.18 1 1.18 ryo /* $NetBSD: tprof.c,v 1.18 2022/12/16 08:02:04 ryo 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.18 ryo __RCSID("$NetBSD: tprof.c,v 1.18 2022/12/16 08:02:04 ryo Exp $");
61 1.1 yamt #endif /* not lint */
62 1.1 yamt
63 1.15 ryo #include <sys/atomic.h>
64 1.1 yamt #include <sys/ioctl.h>
65 1.15 ryo #include <sys/sysctl.h>
66 1.1 yamt #include <sys/wait.h>
67 1.1 yamt
68 1.1 yamt #include <dev/tprof/tprof_ioctl.h>
69 1.1 yamt
70 1.1 yamt #include <err.h>
71 1.1 yamt #include <errno.h>
72 1.1 yamt #include <fcntl.h>
73 1.1 yamt #include <inttypes.h>
74 1.15 ryo #include <math.h>
75 1.1 yamt #include <pthread.h>
76 1.1 yamt #include <signal.h>
77 1.1 yamt #include <stdbool.h>
78 1.1 yamt #include <stdio.h>
79 1.1 yamt #include <stdlib.h>
80 1.1 yamt #include <string.h>
81 1.15 ryo #include <time.h>
82 1.1 yamt #include <unistd.h>
83 1.15 ryo #include <util.h>
84 1.6 maxv #include "tprof.h"
85 1.1 yamt
86 1.1 yamt #define _PATH_TPROF "/dev/tprof"
87 1.1 yamt
88 1.14 ryo struct tprof_info tprof_info;
89 1.14 ryo u_int ncounters;
90 1.1 yamt int devfd;
91 1.1 yamt int outfd;
92 1.15 ryo int ncpu;
93 1.14 ryo u_int nevent;
94 1.15 ryo double interval = 0xffffffff; /* XXX */
95 1.15 ryo const char *eventname[TPROF_MAXCOUNTERS];
96 1.15 ryo u_int eventnamewidth[TPROF_MAXCOUNTERS];
97 1.15 ryo #define COUNTER_COLUMNS_WIDTH 11
98 1.1 yamt
99 1.7 maxv static void tprof_list(int, char **);
100 1.15 ryo static void tprof_monitor_common(bool, int, char **) __dead;
101 1.17 ryo static void tprof_monitor(int, char **) __dead;
102 1.17 ryo static void tprof_count(int, char **) __dead;
103 1.7 maxv
104 1.7 maxv static struct cmdtab {
105 1.7 maxv const char *label;
106 1.7 maxv bool takesargs;
107 1.7 maxv bool argsoptional;
108 1.7 maxv void (*func)(int, char **);
109 1.7 maxv } const tprof_cmdtab[] = {
110 1.7 maxv { "list", false, false, tprof_list },
111 1.7 maxv { "monitor", true, false, tprof_monitor },
112 1.15 ryo { "count", true, false, tprof_count },
113 1.8 maxv { "analyze", true, true, tprof_analyze },
114 1.16 ryo { "top", true, true, tprof_top },
115 1.7 maxv { NULL, false, false, NULL },
116 1.7 maxv };
117 1.7 maxv
118 1.5 joerg __dead static void
119 1.1 yamt usage(void)
120 1.1 yamt {
121 1.1 yamt
122 1.8 maxv fprintf(stderr, "%s op [arguments]\n", getprogname());
123 1.2 yamt fprintf(stderr, "\n");
124 1.7 maxv fprintf(stderr, "\tlist\n");
125 1.7 maxv fprintf(stderr, "\t\tList the available events.\n");
126 1.16 ryo fprintf(stderr, "\tmonitor -e name[:option] [-e ...] [-o outfile]"
127 1.16 ryo " command\n");
128 1.7 maxv fprintf(stderr, "\t\tMonitor the event 'name' with option 'option'\n"
129 1.7 maxv "\t\tcounted during the execution of 'command'.\n");
130 1.15 ryo fprintf(stderr, "\tcount -e name[:option] [-e ...] [-i interval]"
131 1.15 ryo " command\n");
132 1.15 ryo fprintf(stderr, "\t\tSame as monitor, but does not profile,"
133 1.15 ryo " only outputs a counter.\n");
134 1.12 wiz fprintf(stderr, "\tanalyze [-CkLPs] [-p pid] file\n");
135 1.9 maxv fprintf(stderr, "\t\tAnalyze the samples of the file 'file'.\n");
136 1.16 ryo fprintf(stderr, "\ttop [-e name [-e ...]] [-i interval] [-u]\n");
137 1.16 ryo fprintf(stderr, "\t\tDisplay profiling results in real-time.\n");
138 1.1 yamt exit(EXIT_FAILURE);
139 1.1 yamt }
140 1.1 yamt
141 1.15 ryo static int
142 1.15 ryo getncpu(void)
143 1.15 ryo {
144 1.15 ryo size_t size;
145 1.15 ryo int mib[2];
146 1.15 ryo
147 1.15 ryo mib[0] = CTL_HW;
148 1.15 ryo mib[1] = HW_NCPU;
149 1.15 ryo size = sizeof(ncpu);
150 1.15 ryo if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1)
151 1.15 ryo ncpu = 1;
152 1.15 ryo return ncpu;
153 1.15 ryo }
154 1.15 ryo
155 1.1 yamt static void *
156 1.1 yamt process_samples(void *dummy)
157 1.1 yamt {
158 1.1 yamt
159 1.1 yamt for (;;) {
160 1.1 yamt char buf[4096];
161 1.1 yamt const char *cp;
162 1.1 yamt ssize_t ssz;
163 1.1 yamt
164 1.1 yamt ssz = read(devfd, buf, sizeof(buf));
165 1.1 yamt if (ssz == -1) {
166 1.1 yamt err(EXIT_FAILURE, "read");
167 1.1 yamt }
168 1.1 yamt if (ssz == 0) {
169 1.1 yamt break;
170 1.1 yamt }
171 1.1 yamt cp = buf;
172 1.1 yamt while (ssz) {
173 1.1 yamt ssize_t wsz;
174 1.1 yamt
175 1.1 yamt wsz = write(outfd, cp, ssz);
176 1.1 yamt if (wsz == -1) {
177 1.1 yamt err(EXIT_FAILURE, "write");
178 1.1 yamt }
179 1.1 yamt ssz -= wsz;
180 1.1 yamt cp += wsz;
181 1.1 yamt }
182 1.1 yamt }
183 1.1 yamt return NULL;
184 1.1 yamt }
185 1.1 yamt
186 1.7 maxv static void
187 1.15 ryo show_counters(void)
188 1.15 ryo {
189 1.15 ryo unsigned int i;
190 1.15 ryo int n, ret;
191 1.15 ryo
192 1.15 ryo fprintf(stderr, " ");
193 1.15 ryo for (i = 0; i < nevent; i++)
194 1.15 ryo fprintf(stderr, " %*s", eventnamewidth[i], eventname[i]);
195 1.15 ryo fprintf(stderr, "\n");
196 1.15 ryo
197 1.15 ryo for (n = 0; n < ncpu; n++) {
198 1.15 ryo tprof_counts_t counts;
199 1.15 ryo
200 1.15 ryo memset(&counts, 0, sizeof(counts));
201 1.15 ryo counts.c_cpu = n;
202 1.15 ryo ret = ioctl(devfd, TPROF_IOC_GETCOUNTS, &counts);
203 1.15 ryo if (ret == -1)
204 1.15 ryo err(EXIT_FAILURE, "TPROF_IOC_GETCOUNTS");
205 1.15 ryo
206 1.15 ryo fprintf(stderr, "CPU%-3d", n);
207 1.15 ryo for (i = 0; i < nevent; i++) {
208 1.15 ryo fprintf(stderr, " %*"PRIu64,
209 1.15 ryo eventnamewidth[i], counts.c_count[i]);
210 1.15 ryo }
211 1.15 ryo fprintf(stderr, "\n");
212 1.15 ryo }
213 1.15 ryo }
214 1.15 ryo
215 1.15 ryo /* XXX: avoid mixing with the output of the child process SIGINFO handler... */
216 1.15 ryo static void
217 1.15 ryo output_delay(void)
218 1.15 ryo {
219 1.15 ryo struct timespec delay_ts;
220 1.15 ryo
221 1.15 ryo delay_ts.tv_sec = 0;
222 1.15 ryo delay_ts.tv_nsec = 100000000;
223 1.15 ryo nanosleep(&delay_ts, NULL);
224 1.15 ryo }
225 1.15 ryo
226 1.15 ryo static void
227 1.15 ryo siginfo_nothing(int signo)
228 1.15 ryo {
229 1.15 ryo __nothing;
230 1.15 ryo }
231 1.15 ryo
232 1.15 ryo static void
233 1.15 ryo siginfo_showcount(int signo)
234 1.15 ryo {
235 1.15 ryo output_delay();
236 1.15 ryo show_counters();
237 1.15 ryo }
238 1.15 ryo
239 1.15 ryo static void *
240 1.15 ryo process_stat(void *arg)
241 1.15 ryo {
242 1.15 ryo unsigned int *done = arg;
243 1.15 ryo double ival, fval;
244 1.15 ryo struct timespec ts;
245 1.15 ryo
246 1.15 ryo ival = floor(interval);
247 1.15 ryo fval = (1000000000 * (interval - ival));
248 1.15 ryo ts.tv_sec = ival;
249 1.15 ryo ts.tv_nsec = fval;
250 1.15 ryo
251 1.15 ryo while (atomic_add_int_nv(done, 0) == 0) {
252 1.15 ryo show_counters();
253 1.15 ryo nanosleep(&ts, NULL);
254 1.15 ryo if (errno == EINTR) /* interrupted by SIGINFO? */
255 1.15 ryo output_delay();
256 1.15 ryo }
257 1.15 ryo return NULL;
258 1.15 ryo }
259 1.15 ryo
260 1.15 ryo static void
261 1.7 maxv tprof_list(int argc, char **argv)
262 1.7 maxv {
263 1.16 ryo printf("%u events can be counted at the same time\n", ncounters);
264 1.7 maxv tprof_event_list();
265 1.7 maxv }
266 1.7 maxv
267 1.18 ryo int
268 1.18 ryo tprof_parse_event(tprof_param_t *param, const char *str, uint32_t flags,
269 1.18 ryo const char **eventnamep, char **errmsgp)
270 1.18 ryo {
271 1.18 ryo double d;
272 1.18 ryo uint64_t n;
273 1.18 ryo int error = 0;
274 1.18 ryo char *p, *event = NULL, *opt = NULL, *scale = NULL;
275 1.18 ryo bool allow_option, allow_scale;
276 1.18 ryo static char errmsgbuf[128];
277 1.18 ryo
278 1.18 ryo allow_option = flags & TPROF_PARSE_EVENT_F_ALLOWOPTION;
279 1.18 ryo allow_scale = flags & TPROF_PARSE_EVENT_F_ALLOWSCALE;
280 1.18 ryo
281 1.18 ryo p = estrdup(str);
282 1.18 ryo event = p;
283 1.18 ryo if (allow_option) {
284 1.18 ryo opt = strchr(p, ':');
285 1.18 ryo if (opt != NULL) {
286 1.18 ryo *opt++ = '\0';
287 1.18 ryo p = opt;
288 1.18 ryo }
289 1.18 ryo }
290 1.18 ryo if (allow_scale) {
291 1.18 ryo scale = strchr(p, ',');
292 1.18 ryo if (scale != NULL)
293 1.18 ryo *scale++ = '\0';
294 1.18 ryo }
295 1.18 ryo
296 1.18 ryo tprof_event_lookup(event, param);
297 1.18 ryo
298 1.18 ryo if (opt != NULL) {
299 1.18 ryo while (*opt != '\0') {
300 1.18 ryo switch (*opt) {
301 1.18 ryo case 'u':
302 1.18 ryo param->p_flags |= TPROF_PARAM_USER;
303 1.18 ryo break;
304 1.18 ryo case 'k':
305 1.18 ryo param->p_flags |= TPROF_PARAM_KERN;
306 1.18 ryo break;
307 1.18 ryo default:
308 1.18 ryo error = -1;
309 1.18 ryo snprintf(errmsgbuf, sizeof(errmsgbuf),
310 1.18 ryo "invalid option: '%c'", *opt);
311 1.18 ryo goto done;
312 1.18 ryo }
313 1.18 ryo }
314 1.18 ryo } else if (allow_option) {
315 1.18 ryo param->p_flags |= TPROF_PARAM_USER;
316 1.18 ryo param->p_flags |= TPROF_PARAM_KERN;
317 1.18 ryo }
318 1.18 ryo
319 1.18 ryo if (scale != NULL) {
320 1.18 ryo if (*scale == '=') {
321 1.18 ryo scale++;
322 1.18 ryo n = strtoull(scale, &p, 0);
323 1.18 ryo if (*p != '\0') {
324 1.18 ryo error = -1;
325 1.18 ryo } else {
326 1.18 ryo param->p_value2 = n;
327 1.18 ryo param->p_flags |=
328 1.18 ryo TPROF_PARAM_VALUE2_TRIGGERCOUNT;
329 1.18 ryo }
330 1.18 ryo } else {
331 1.18 ryo if (strncasecmp("0x", scale, 2) == 0)
332 1.18 ryo d = strtol(scale, &p, 0);
333 1.18 ryo else
334 1.18 ryo d = strtod(scale, &p);
335 1.18 ryo if (*p != '\0' || d <= 0) {
336 1.18 ryo error = -1;
337 1.18 ryo } else {
338 1.18 ryo param->p_value2 = 0x100000000ULL / d;
339 1.18 ryo param->p_flags |= TPROF_PARAM_VALUE2_SCALE;
340 1.18 ryo }
341 1.18 ryo }
342 1.18 ryo
343 1.18 ryo if (error != 0) {
344 1.18 ryo snprintf(errmsgbuf, sizeof(errmsgbuf),
345 1.18 ryo "invalid scale: %s", scale);
346 1.18 ryo goto done;
347 1.18 ryo }
348 1.18 ryo }
349 1.18 ryo
350 1.18 ryo done:
351 1.18 ryo if (eventnamep != NULL)
352 1.18 ryo *eventnamep = event;
353 1.18 ryo if (error != 0 && errmsgp != NULL)
354 1.18 ryo *errmsgp = errmsgbuf;
355 1.18 ryo return error;
356 1.18 ryo }
357 1.18 ryo
358 1.7 maxv static void
359 1.15 ryo tprof_monitor_common(bool do_profile, int argc, char **argv)
360 1.1 yamt {
361 1.7 maxv const char *outfile = "tprof.out";
362 1.1 yamt struct tprof_stat ts;
363 1.14 ryo tprof_param_t params[TPROF_MAXCOUNTERS];
364 1.1 yamt pid_t pid;
365 1.1 yamt pthread_t pt;
366 1.14 ryo int ret, ch, i;
367 1.18 ryo char *p, *errmsg;
368 1.14 ryo tprof_countermask_t mask = TPROF_COUNTERMASK_ALL;
369 1.6 maxv
370 1.14 ryo memset(params, 0, sizeof(params));
371 1.6 maxv
372 1.15 ryo while ((ch = getopt(argc, argv, do_profile ? "o:e:" : "e:i:")) != -1) {
373 1.1 yamt switch (ch) {
374 1.1 yamt case 'o':
375 1.1 yamt outfile = optarg;
376 1.1 yamt break;
377 1.15 ryo case 'i':
378 1.15 ryo interval = strtod(optarg, &p);
379 1.15 ryo if (*p != '\0' || interval <= 0)
380 1.15 ryo errx(EXIT_FAILURE, "Bad/invalid interval: %s",
381 1.15 ryo optarg);
382 1.15 ryo break;
383 1.6 maxv case 'e':
384 1.18 ryo if (tprof_parse_event(¶ms[nevent], optarg,
385 1.18 ryo TPROF_PARSE_EVENT_F_ALLOWOPTION |
386 1.18 ryo (do_profile ? TPROF_PARSE_EVENT_F_ALLOWSCALE : 0),
387 1.18 ryo &eventname[nevent], &errmsg) != 0) {
388 1.18 ryo errx(EXIT_FAILURE, "%s", errmsg);
389 1.15 ryo }
390 1.15 ryo eventnamewidth[nevent] = strlen(eventname[nevent]);
391 1.15 ryo if (eventnamewidth[nevent] < COUNTER_COLUMNS_WIDTH)
392 1.15 ryo eventnamewidth[nevent] = COUNTER_COLUMNS_WIDTH;
393 1.14 ryo nevent++;
394 1.14 ryo if (nevent > __arraycount(params) ||
395 1.14 ryo nevent > ncounters)
396 1.16 ryo errx(EXIT_FAILURE, "Too many events. Only a"
397 1.16 ryo " maximum of %d counters can be used.",
398 1.16 ryo ncounters);
399 1.6 maxv break;
400 1.1 yamt default:
401 1.1 yamt usage();
402 1.1 yamt }
403 1.1 yamt }
404 1.1 yamt argc -= optind;
405 1.1 yamt argv += optind;
406 1.14 ryo if (argc == 0 || nevent == 0) {
407 1.6 maxv usage();
408 1.6 maxv }
409 1.6 maxv
410 1.15 ryo if (do_profile) {
411 1.15 ryo outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
412 1.15 ryo if (outfd == -1) {
413 1.15 ryo err(EXIT_FAILURE, "%s", outfile);
414 1.15 ryo }
415 1.1 yamt }
416 1.1 yamt
417 1.14 ryo for (i = 0; i < (int)nevent; i++) {
418 1.14 ryo params[i].p_counter = i;
419 1.15 ryo if (do_profile)
420 1.15 ryo params[i].p_flags |= TPROF_PARAM_PROFILE;
421 1.14 ryo ret = ioctl(devfd, TPROF_IOC_CONFIGURE_EVENT, ¶ms[i]);
422 1.16 ryo if (ret == -1) {
423 1.16 ryo err(EXIT_FAILURE, "TPROF_IOC_CONFIGURE_EVENT: %s",
424 1.16 ryo eventname[i]);
425 1.16 ryo }
426 1.14 ryo }
427 1.14 ryo
428 1.14 ryo ret = ioctl(devfd, TPROF_IOC_START, &mask);
429 1.1 yamt if (ret == -1) {
430 1.3 yamt err(EXIT_FAILURE, "TPROF_IOC_START");
431 1.1 yamt }
432 1.1 yamt
433 1.1 yamt pid = fork();
434 1.1 yamt switch (pid) {
435 1.1 yamt case -1:
436 1.1 yamt err(EXIT_FAILURE, "fork");
437 1.1 yamt case 0:
438 1.1 yamt close(devfd);
439 1.1 yamt execvp(argv[0], argv);
440 1.1 yamt _Exit(EXIT_FAILURE);
441 1.1 yamt }
442 1.1 yamt
443 1.1 yamt signal(SIGINT, SIG_IGN);
444 1.15 ryo if (do_profile)
445 1.15 ryo signal(SIGINFO, siginfo_showcount);
446 1.15 ryo else
447 1.15 ryo signal(SIGINFO, siginfo_nothing);
448 1.15 ryo
449 1.15 ryo unsigned int done = 0;
450 1.15 ryo if (do_profile)
451 1.15 ryo ret = pthread_create(&pt, NULL, process_samples, NULL);
452 1.15 ryo else
453 1.15 ryo ret = pthread_create(&pt, NULL, process_stat, &done);
454 1.15 ryo if (ret != 0)
455 1.7 maxv errx(1, "pthread_create: %s", strerror(ret));
456 1.1 yamt
457 1.1 yamt for (;;) {
458 1.1 yamt int status;
459 1.1 yamt
460 1.1 yamt pid = wait4(-1, &status, 0, NULL);
461 1.1 yamt if (pid == -1) {
462 1.1 yamt if (errno == ECHILD) {
463 1.1 yamt break;
464 1.1 yamt }
465 1.1 yamt err(EXIT_FAILURE, "wait4");
466 1.1 yamt }
467 1.1 yamt if (pid != 0 && WIFEXITED(status)) {
468 1.1 yamt break;
469 1.1 yamt }
470 1.1 yamt }
471 1.1 yamt
472 1.14 ryo ret = ioctl(devfd, TPROF_IOC_STOP, &mask);
473 1.1 yamt if (ret == -1) {
474 1.1 yamt err(EXIT_FAILURE, "TPROF_IOC_STOP");
475 1.1 yamt }
476 1.1 yamt
477 1.15 ryo if (!do_profile) {
478 1.15 ryo atomic_add_int(&done, 1); /* terminate thread */
479 1.15 ryo kill(0, SIGINFO);
480 1.15 ryo }
481 1.15 ryo
482 1.1 yamt pthread_join(pt, NULL);
483 1.1 yamt
484 1.15 ryo if (do_profile) {
485 1.15 ryo ret = ioctl(devfd, TPROF_IOC_GETSTAT, &ts);
486 1.15 ryo if (ret == -1)
487 1.15 ryo err(EXIT_FAILURE, "TPROF_IOC_GETSTAT");
488 1.15 ryo
489 1.15 ryo fprintf(stderr, "\n%s statistics:\n", getprogname());
490 1.15 ryo fprintf(stderr, "\tsample %" PRIu64 "\n", ts.ts_sample);
491 1.15 ryo fprintf(stderr, "\toverflow %" PRIu64 "\n", ts.ts_overflow);
492 1.15 ryo fprintf(stderr, "\tbuf %" PRIu64 "\n", ts.ts_buf);
493 1.15 ryo fprintf(stderr, "\temptybuf %" PRIu64 "\n", ts.ts_emptybuf);
494 1.15 ryo fprintf(stderr, "\tdropbuf %" PRIu64 "\n", ts.ts_dropbuf);
495 1.16 ryo fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n",
496 1.16 ryo ts.ts_dropbuf_sample);
497 1.15 ryo
498 1.15 ryo fprintf(stderr, "\n");
499 1.1 yamt }
500 1.15 ryo show_counters();
501 1.1 yamt
502 1.15 ryo exit(EXIT_SUCCESS);
503 1.15 ryo }
504 1.1 yamt
505 1.15 ryo static void
506 1.15 ryo tprof_monitor(int argc, char **argv)
507 1.15 ryo {
508 1.15 ryo tprof_monitor_common(true, argc, argv);
509 1.15 ryo }
510 1.15 ryo
511 1.15 ryo static void
512 1.15 ryo tprof_count(int argc, char **argv)
513 1.15 ryo {
514 1.15 ryo tprof_monitor_common(false, argc, argv);
515 1.1 yamt }
516 1.7 maxv
517 1.7 maxv int
518 1.7 maxv main(int argc, char *argv[])
519 1.7 maxv {
520 1.7 maxv const struct cmdtab *ct;
521 1.7 maxv int ret;
522 1.7 maxv
523 1.15 ryo getncpu();
524 1.7 maxv setprogname(argv[0]);
525 1.7 maxv argv += 1, argc -= 1;
526 1.7 maxv
527 1.7 maxv devfd = open(_PATH_TPROF, O_RDWR);
528 1.7 maxv if (devfd == -1) {
529 1.7 maxv err(EXIT_FAILURE, "%s", _PATH_TPROF);
530 1.7 maxv }
531 1.7 maxv
532 1.14 ryo ret = ioctl(devfd, TPROF_IOC_GETINFO, &tprof_info);
533 1.7 maxv if (ret == -1) {
534 1.7 maxv err(EXIT_FAILURE, "TPROF_IOC_GETINFO");
535 1.7 maxv }
536 1.14 ryo if (tprof_info.ti_version != TPROF_VERSION) {
537 1.7 maxv errx(EXIT_FAILURE, "version mismatch: version=%d, expected=%d",
538 1.14 ryo tprof_info.ti_version, TPROF_VERSION);
539 1.7 maxv }
540 1.14 ryo if (tprof_event_init(tprof_info.ti_ident) == -1) {
541 1.13 maxv errx(EXIT_FAILURE, "cpu not supported");
542 1.7 maxv }
543 1.7 maxv
544 1.14 ryo ret = ioctl(devfd, TPROF_IOC_GETNCOUNTERS, &ncounters);
545 1.14 ryo if (ret == -1) {
546 1.14 ryo err(EXIT_FAILURE, "TPROF_IOC_GETNCOUNTERS");
547 1.14 ryo }
548 1.14 ryo if (ncounters == 0) {
549 1.14 ryo errx(EXIT_FAILURE, "no available counters");
550 1.14 ryo }
551 1.14 ryo
552 1.11 jmcneill if (argc == 0)
553 1.11 jmcneill usage();
554 1.11 jmcneill
555 1.7 maxv for (ct = tprof_cmdtab; ct->label != NULL; ct++) {
556 1.7 maxv if (strcmp(argv[0], ct->label) == 0) {
557 1.7 maxv if (!ct->argsoptional &&
558 1.7 maxv ((ct->takesargs == 0) ^ (argv[1] == NULL)))
559 1.7 maxv {
560 1.7 maxv usage();
561 1.7 maxv }
562 1.7 maxv (*ct->func)(argc, argv);
563 1.7 maxv break;
564 1.7 maxv }
565 1.7 maxv }
566 1.9 maxv if (ct->label == NULL) {
567 1.9 maxv usage();
568 1.9 maxv }
569 1.7 maxv }
570