tprof.c revision 1.15 1 1.15 ryo /* $NetBSD: tprof.c,v 1.15 2022/12/01 00:40:05 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.15 ryo __RCSID("$NetBSD: tprof.c,v 1.15 2022/12/01 00:40:05 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.15 ryo static void tprof_monitor(int, char **);
102 1.15 ryo static void tprof_count(int, char **);
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.7 maxv { NULL, false, false, NULL },
115 1.7 maxv };
116 1.7 maxv
117 1.5 joerg __dead static void
118 1.1 yamt usage(void)
119 1.1 yamt {
120 1.1 yamt
121 1.8 maxv fprintf(stderr, "%s op [arguments]\n", getprogname());
122 1.2 yamt fprintf(stderr, "\n");
123 1.7 maxv fprintf(stderr, "\tlist\n");
124 1.7 maxv fprintf(stderr, "\t\tList the available events.\n");
125 1.15 ryo fprintf(stderr, "\tmonitor -e name[:option] [-e ...] [-o outfile] command\n");
126 1.7 maxv fprintf(stderr, "\t\tMonitor the event 'name' with option 'option'\n"
127 1.7 maxv "\t\tcounted during the execution of 'command'.\n");
128 1.15 ryo fprintf(stderr, "\tcount -e name[:option] [-e ...] [-i interval]"
129 1.15 ryo " command\n");
130 1.15 ryo fprintf(stderr, "\t\tSame as monitor, but does not profile,"
131 1.15 ryo " only outputs a counter.\n");
132 1.12 wiz fprintf(stderr, "\tanalyze [-CkLPs] [-p pid] file\n");
133 1.9 maxv fprintf(stderr, "\t\tAnalyze the samples of the file 'file'.\n");
134 1.1 yamt
135 1.1 yamt exit(EXIT_FAILURE);
136 1.1 yamt }
137 1.1 yamt
138 1.15 ryo static int
139 1.15 ryo getncpu(void)
140 1.15 ryo {
141 1.15 ryo size_t size;
142 1.15 ryo int mib[2];
143 1.15 ryo
144 1.15 ryo mib[0] = CTL_HW;
145 1.15 ryo mib[1] = HW_NCPU;
146 1.15 ryo size = sizeof(ncpu);
147 1.15 ryo if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1)
148 1.15 ryo ncpu = 1;
149 1.15 ryo return ncpu;
150 1.15 ryo }
151 1.15 ryo
152 1.1 yamt static void *
153 1.1 yamt process_samples(void *dummy)
154 1.1 yamt {
155 1.1 yamt
156 1.1 yamt for (;;) {
157 1.1 yamt char buf[4096];
158 1.1 yamt const char *cp;
159 1.1 yamt ssize_t ssz;
160 1.1 yamt
161 1.1 yamt ssz = read(devfd, buf, sizeof(buf));
162 1.1 yamt if (ssz == -1) {
163 1.1 yamt err(EXIT_FAILURE, "read");
164 1.1 yamt }
165 1.1 yamt if (ssz == 0) {
166 1.1 yamt break;
167 1.1 yamt }
168 1.1 yamt cp = buf;
169 1.1 yamt while (ssz) {
170 1.1 yamt ssize_t wsz;
171 1.1 yamt
172 1.1 yamt wsz = write(outfd, cp, ssz);
173 1.1 yamt if (wsz == -1) {
174 1.1 yamt err(EXIT_FAILURE, "write");
175 1.1 yamt }
176 1.1 yamt ssz -= wsz;
177 1.1 yamt cp += wsz;
178 1.1 yamt }
179 1.1 yamt }
180 1.1 yamt return NULL;
181 1.1 yamt }
182 1.1 yamt
183 1.7 maxv static void
184 1.15 ryo show_counters(void)
185 1.15 ryo {
186 1.15 ryo unsigned int i;
187 1.15 ryo int n, ret;
188 1.15 ryo
189 1.15 ryo fprintf(stderr, " ");
190 1.15 ryo for (i = 0; i < nevent; i++)
191 1.15 ryo fprintf(stderr, " %*s", eventnamewidth[i], eventname[i]);
192 1.15 ryo fprintf(stderr, "\n");
193 1.15 ryo
194 1.15 ryo for (n = 0; n < ncpu; n++) {
195 1.15 ryo tprof_counts_t counts;
196 1.15 ryo
197 1.15 ryo memset(&counts, 0, sizeof(counts));
198 1.15 ryo counts.c_cpu = n;
199 1.15 ryo ret = ioctl(devfd, TPROF_IOC_GETCOUNTS, &counts);
200 1.15 ryo if (ret == -1)
201 1.15 ryo err(EXIT_FAILURE, "TPROF_IOC_GETCOUNTS");
202 1.15 ryo
203 1.15 ryo fprintf(stderr, "CPU%-3d", n);
204 1.15 ryo for (i = 0; i < nevent; i++) {
205 1.15 ryo fprintf(stderr, " %*"PRIu64,
206 1.15 ryo eventnamewidth[i], counts.c_count[i]);
207 1.15 ryo }
208 1.15 ryo fprintf(stderr, "\n");
209 1.15 ryo }
210 1.15 ryo }
211 1.15 ryo
212 1.15 ryo /* XXX: avoid mixing with the output of the child process SIGINFO handler... */
213 1.15 ryo static void
214 1.15 ryo output_delay(void)
215 1.15 ryo {
216 1.15 ryo struct timespec delay_ts;
217 1.15 ryo
218 1.15 ryo delay_ts.tv_sec = 0;
219 1.15 ryo delay_ts.tv_nsec = 100000000;
220 1.15 ryo nanosleep(&delay_ts, NULL);
221 1.15 ryo }
222 1.15 ryo
223 1.15 ryo static void
224 1.15 ryo siginfo_nothing(int signo)
225 1.15 ryo {
226 1.15 ryo __nothing;
227 1.15 ryo }
228 1.15 ryo
229 1.15 ryo static void
230 1.15 ryo siginfo_showcount(int signo)
231 1.15 ryo {
232 1.15 ryo output_delay();
233 1.15 ryo show_counters();
234 1.15 ryo }
235 1.15 ryo
236 1.15 ryo static void *
237 1.15 ryo process_stat(void *arg)
238 1.15 ryo {
239 1.15 ryo unsigned int *done = arg;
240 1.15 ryo double ival, fval;
241 1.15 ryo struct timespec ts;
242 1.15 ryo
243 1.15 ryo ival = floor(interval);
244 1.15 ryo fval = (1000000000 * (interval - ival));
245 1.15 ryo ts.tv_sec = ival;
246 1.15 ryo ts.tv_nsec = fval;
247 1.15 ryo
248 1.15 ryo while (atomic_add_int_nv(done, 0) == 0) {
249 1.15 ryo show_counters();
250 1.15 ryo nanosleep(&ts, NULL);
251 1.15 ryo if (errno == EINTR) /* interrupted by SIGINFO? */
252 1.15 ryo output_delay();
253 1.15 ryo }
254 1.15 ryo return NULL;
255 1.15 ryo }
256 1.15 ryo
257 1.15 ryo static void
258 1.7 maxv tprof_list(int argc, char **argv)
259 1.7 maxv {
260 1.7 maxv tprof_event_list();
261 1.7 maxv }
262 1.7 maxv
263 1.7 maxv static void
264 1.15 ryo tprof_monitor_common(bool do_profile, int argc, char **argv)
265 1.1 yamt {
266 1.7 maxv const char *outfile = "tprof.out";
267 1.1 yamt struct tprof_stat ts;
268 1.14 ryo tprof_param_t params[TPROF_MAXCOUNTERS];
269 1.1 yamt pid_t pid;
270 1.1 yamt pthread_t pt;
271 1.14 ryo int ret, ch, i;
272 1.15 ryo char *tokens[2], *p;
273 1.14 ryo tprof_countermask_t mask = TPROF_COUNTERMASK_ALL;
274 1.6 maxv
275 1.14 ryo memset(params, 0, sizeof(params));
276 1.6 maxv
277 1.15 ryo while ((ch = getopt(argc, argv, do_profile ? "o:e:" : "e:i:")) != -1) {
278 1.1 yamt switch (ch) {
279 1.1 yamt case 'o':
280 1.1 yamt outfile = optarg;
281 1.1 yamt break;
282 1.15 ryo case 'i':
283 1.15 ryo interval = strtod(optarg, &p);
284 1.15 ryo if (*p != '\0' || interval <= 0)
285 1.15 ryo errx(EXIT_FAILURE, "Bad/invalid interval: %s",
286 1.15 ryo optarg);
287 1.15 ryo break;
288 1.6 maxv case 'e':
289 1.15 ryo p = estrdup(optarg);
290 1.15 ryo tokens[0] = strtok(p, ":");
291 1.6 maxv tokens[1] = strtok(NULL, ":");
292 1.14 ryo tprof_event_lookup(tokens[0], ¶ms[nevent]);
293 1.15 ryo
294 1.15 ryo if (tokens[1] == NULL) {
295 1.15 ryo params[nevent].p_flags |=
296 1.15 ryo (TPROF_PARAM_USER | TPROF_PARAM_KERN);
297 1.15 ryo } else {
298 1.15 ryo if (strchr(tokens[1], 'u'))
299 1.15 ryo params[nevent].p_flags |=
300 1.15 ryo TPROF_PARAM_USER;
301 1.15 ryo if (strchr(tokens[1], 'k'))
302 1.15 ryo params[nevent].p_flags |=
303 1.15 ryo TPROF_PARAM_KERN;
304 1.15 ryo }
305 1.15 ryo eventname[nevent] = tokens[0];
306 1.15 ryo eventnamewidth[nevent] = strlen(eventname[nevent]);
307 1.15 ryo if (eventnamewidth[nevent] < COUNTER_COLUMNS_WIDTH)
308 1.15 ryo eventnamewidth[nevent] = COUNTER_COLUMNS_WIDTH;
309 1.14 ryo nevent++;
310 1.14 ryo if (nevent > __arraycount(params) ||
311 1.14 ryo nevent > ncounters)
312 1.14 ryo errx(EXIT_FAILURE, "Too many events");
313 1.6 maxv break;
314 1.1 yamt default:
315 1.1 yamt usage();
316 1.1 yamt }
317 1.1 yamt }
318 1.1 yamt argc -= optind;
319 1.1 yamt argv += optind;
320 1.14 ryo if (argc == 0 || nevent == 0) {
321 1.6 maxv usage();
322 1.6 maxv }
323 1.6 maxv
324 1.15 ryo if (do_profile) {
325 1.15 ryo outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
326 1.15 ryo if (outfd == -1) {
327 1.15 ryo err(EXIT_FAILURE, "%s", outfile);
328 1.15 ryo }
329 1.1 yamt }
330 1.1 yamt
331 1.14 ryo for (i = 0; i < (int)nevent; i++) {
332 1.14 ryo params[i].p_counter = i;
333 1.15 ryo if (do_profile)
334 1.15 ryo params[i].p_flags |= TPROF_PARAM_PROFILE;
335 1.14 ryo ret = ioctl(devfd, TPROF_IOC_CONFIGURE_EVENT, ¶ms[i]);
336 1.14 ryo if (ret == -1)
337 1.14 ryo err(EXIT_FAILURE, "TPROF_IOC_CONFIGURE_EVENT");
338 1.14 ryo }
339 1.14 ryo
340 1.14 ryo ret = ioctl(devfd, TPROF_IOC_START, &mask);
341 1.1 yamt if (ret == -1) {
342 1.3 yamt err(EXIT_FAILURE, "TPROF_IOC_START");
343 1.1 yamt }
344 1.1 yamt
345 1.1 yamt pid = fork();
346 1.1 yamt switch (pid) {
347 1.1 yamt case -1:
348 1.1 yamt err(EXIT_FAILURE, "fork");
349 1.1 yamt case 0:
350 1.1 yamt close(devfd);
351 1.1 yamt execvp(argv[0], argv);
352 1.1 yamt _Exit(EXIT_FAILURE);
353 1.1 yamt }
354 1.1 yamt
355 1.1 yamt signal(SIGINT, SIG_IGN);
356 1.15 ryo if (do_profile)
357 1.15 ryo signal(SIGINFO, siginfo_showcount);
358 1.15 ryo else
359 1.15 ryo signal(SIGINFO, siginfo_nothing);
360 1.15 ryo
361 1.15 ryo unsigned int done = 0;
362 1.15 ryo if (do_profile)
363 1.15 ryo ret = pthread_create(&pt, NULL, process_samples, NULL);
364 1.15 ryo else
365 1.15 ryo ret = pthread_create(&pt, NULL, process_stat, &done);
366 1.15 ryo if (ret != 0)
367 1.7 maxv errx(1, "pthread_create: %s", strerror(ret));
368 1.1 yamt
369 1.1 yamt for (;;) {
370 1.1 yamt int status;
371 1.1 yamt
372 1.1 yamt pid = wait4(-1, &status, 0, NULL);
373 1.1 yamt if (pid == -1) {
374 1.1 yamt if (errno == ECHILD) {
375 1.1 yamt break;
376 1.1 yamt }
377 1.1 yamt err(EXIT_FAILURE, "wait4");
378 1.1 yamt }
379 1.1 yamt if (pid != 0 && WIFEXITED(status)) {
380 1.1 yamt break;
381 1.1 yamt }
382 1.1 yamt }
383 1.1 yamt
384 1.14 ryo ret = ioctl(devfd, TPROF_IOC_STOP, &mask);
385 1.1 yamt if (ret == -1) {
386 1.1 yamt err(EXIT_FAILURE, "TPROF_IOC_STOP");
387 1.1 yamt }
388 1.1 yamt
389 1.15 ryo if (!do_profile) {
390 1.15 ryo atomic_add_int(&done, 1); /* terminate thread */
391 1.15 ryo kill(0, SIGINFO);
392 1.15 ryo }
393 1.15 ryo
394 1.1 yamt pthread_join(pt, NULL);
395 1.1 yamt
396 1.15 ryo if (do_profile) {
397 1.15 ryo ret = ioctl(devfd, TPROF_IOC_GETSTAT, &ts);
398 1.15 ryo if (ret == -1)
399 1.15 ryo err(EXIT_FAILURE, "TPROF_IOC_GETSTAT");
400 1.15 ryo
401 1.15 ryo fprintf(stderr, "\n%s statistics:\n", getprogname());
402 1.15 ryo fprintf(stderr, "\tsample %" PRIu64 "\n", ts.ts_sample);
403 1.15 ryo fprintf(stderr, "\toverflow %" PRIu64 "\n", ts.ts_overflow);
404 1.15 ryo fprintf(stderr, "\tbuf %" PRIu64 "\n", ts.ts_buf);
405 1.15 ryo fprintf(stderr, "\temptybuf %" PRIu64 "\n", ts.ts_emptybuf);
406 1.15 ryo fprintf(stderr, "\tdropbuf %" PRIu64 "\n", ts.ts_dropbuf);
407 1.15 ryo fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n", ts.ts_dropbuf_sample);
408 1.15 ryo
409 1.15 ryo fprintf(stderr, "\n");
410 1.1 yamt }
411 1.15 ryo show_counters();
412 1.1 yamt
413 1.15 ryo exit(EXIT_SUCCESS);
414 1.15 ryo }
415 1.1 yamt
416 1.15 ryo static void
417 1.15 ryo tprof_monitor(int argc, char **argv)
418 1.15 ryo {
419 1.15 ryo tprof_monitor_common(true, argc, argv);
420 1.15 ryo }
421 1.15 ryo
422 1.15 ryo static void
423 1.15 ryo tprof_count(int argc, char **argv)
424 1.15 ryo {
425 1.15 ryo tprof_monitor_common(false, argc, argv);
426 1.1 yamt }
427 1.7 maxv
428 1.7 maxv int
429 1.7 maxv main(int argc, char *argv[])
430 1.7 maxv {
431 1.7 maxv const struct cmdtab *ct;
432 1.7 maxv int ret;
433 1.7 maxv
434 1.15 ryo getncpu();
435 1.7 maxv setprogname(argv[0]);
436 1.7 maxv argv += 1, argc -= 1;
437 1.7 maxv
438 1.7 maxv devfd = open(_PATH_TPROF, O_RDWR);
439 1.7 maxv if (devfd == -1) {
440 1.7 maxv err(EXIT_FAILURE, "%s", _PATH_TPROF);
441 1.7 maxv }
442 1.7 maxv
443 1.14 ryo ret = ioctl(devfd, TPROF_IOC_GETINFO, &tprof_info);
444 1.7 maxv if (ret == -1) {
445 1.7 maxv err(EXIT_FAILURE, "TPROF_IOC_GETINFO");
446 1.7 maxv }
447 1.14 ryo if (tprof_info.ti_version != TPROF_VERSION) {
448 1.7 maxv errx(EXIT_FAILURE, "version mismatch: version=%d, expected=%d",
449 1.14 ryo tprof_info.ti_version, TPROF_VERSION);
450 1.7 maxv }
451 1.14 ryo if (tprof_event_init(tprof_info.ti_ident) == -1) {
452 1.13 maxv errx(EXIT_FAILURE, "cpu not supported");
453 1.7 maxv }
454 1.7 maxv
455 1.14 ryo ret = ioctl(devfd, TPROF_IOC_GETNCOUNTERS, &ncounters);
456 1.14 ryo if (ret == -1) {
457 1.14 ryo err(EXIT_FAILURE, "TPROF_IOC_GETNCOUNTERS");
458 1.14 ryo }
459 1.14 ryo if (ncounters == 0) {
460 1.14 ryo errx(EXIT_FAILURE, "no available counters");
461 1.14 ryo }
462 1.14 ryo
463 1.11 jmcneill if (argc == 0)
464 1.11 jmcneill usage();
465 1.11 jmcneill
466 1.7 maxv for (ct = tprof_cmdtab; ct->label != NULL; ct++) {
467 1.7 maxv if (strcmp(argv[0], ct->label) == 0) {
468 1.7 maxv if (!ct->argsoptional &&
469 1.7 maxv ((ct->takesargs == 0) ^ (argv[1] == NULL)))
470 1.7 maxv {
471 1.7 maxv usage();
472 1.7 maxv }
473 1.7 maxv (*ct->func)(argc, argv);
474 1.7 maxv break;
475 1.7 maxv }
476 1.7 maxv }
477 1.9 maxv if (ct->label == NULL) {
478 1.9 maxv usage();
479 1.9 maxv }
480 1.7 maxv }
481