1 1.21 msaitoh /* $NetBSD: tprof.c,v 1.21 2023/04/17 08:37:24 msaitoh 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.21 msaitoh __RCSID("$NetBSD: tprof.c,v 1.21 2023/04/17 08:37:24 msaitoh 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.19 ryoon fprintf(stderr, "\ttop [-e name [-e ...]] [-i interval] [-acu]\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.21 msaitoh const char *defaultevent = tprof_cycle_event_name(); 264 1.21 msaitoh 265 1.21 msaitoh printf("%u events can be counted at the same time.\n", ncounters); 266 1.21 msaitoh if (defaultevent != NULL) 267 1.21 msaitoh printf("The default counter for monitor and top command is " 268 1.21 msaitoh "\"%s\".\n", defaultevent); 269 1.7 maxv tprof_event_list(); 270 1.7 maxv } 271 1.7 maxv 272 1.18 ryo int 273 1.18 ryo tprof_parse_event(tprof_param_t *param, const char *str, uint32_t flags, 274 1.18 ryo const char **eventnamep, char **errmsgp) 275 1.18 ryo { 276 1.18 ryo double d; 277 1.18 ryo uint64_t n; 278 1.18 ryo int error = 0; 279 1.18 ryo char *p, *event = NULL, *opt = NULL, *scale = NULL; 280 1.18 ryo bool allow_option, allow_scale; 281 1.18 ryo static char errmsgbuf[128]; 282 1.18 ryo 283 1.18 ryo allow_option = flags & TPROF_PARSE_EVENT_F_ALLOWOPTION; 284 1.18 ryo allow_scale = flags & TPROF_PARSE_EVENT_F_ALLOWSCALE; 285 1.18 ryo 286 1.18 ryo p = estrdup(str); 287 1.18 ryo event = p; 288 1.18 ryo if (allow_option) { 289 1.18 ryo opt = strchr(p, ':'); 290 1.18 ryo if (opt != NULL) { 291 1.18 ryo *opt++ = '\0'; 292 1.18 ryo p = opt; 293 1.18 ryo } 294 1.18 ryo } 295 1.18 ryo if (allow_scale) { 296 1.18 ryo scale = strchr(p, ','); 297 1.18 ryo if (scale != NULL) 298 1.18 ryo *scale++ = '\0'; 299 1.18 ryo } 300 1.18 ryo 301 1.18 ryo tprof_event_lookup(event, param); 302 1.18 ryo 303 1.18 ryo if (opt != NULL) { 304 1.18 ryo while (*opt != '\0') { 305 1.18 ryo switch (*opt) { 306 1.18 ryo case 'u': 307 1.18 ryo param->p_flags |= TPROF_PARAM_USER; 308 1.18 ryo break; 309 1.18 ryo case 'k': 310 1.18 ryo param->p_flags |= TPROF_PARAM_KERN; 311 1.18 ryo break; 312 1.18 ryo default: 313 1.18 ryo error = -1; 314 1.18 ryo snprintf(errmsgbuf, sizeof(errmsgbuf), 315 1.18 ryo "invalid option: '%c'", *opt); 316 1.18 ryo goto done; 317 1.18 ryo } 318 1.20 ryo opt++; 319 1.18 ryo } 320 1.18 ryo } else if (allow_option) { 321 1.18 ryo param->p_flags |= TPROF_PARAM_USER; 322 1.18 ryo param->p_flags |= TPROF_PARAM_KERN; 323 1.18 ryo } 324 1.18 ryo 325 1.18 ryo if (scale != NULL) { 326 1.18 ryo if (*scale == '=') { 327 1.18 ryo scale++; 328 1.18 ryo n = strtoull(scale, &p, 0); 329 1.18 ryo if (*p != '\0') { 330 1.18 ryo error = -1; 331 1.18 ryo } else { 332 1.18 ryo param->p_value2 = n; 333 1.18 ryo param->p_flags |= 334 1.18 ryo TPROF_PARAM_VALUE2_TRIGGERCOUNT; 335 1.18 ryo } 336 1.18 ryo } else { 337 1.18 ryo if (strncasecmp("0x", scale, 2) == 0) 338 1.18 ryo d = strtol(scale, &p, 0); 339 1.18 ryo else 340 1.18 ryo d = strtod(scale, &p); 341 1.18 ryo if (*p != '\0' || d <= 0) { 342 1.18 ryo error = -1; 343 1.18 ryo } else { 344 1.18 ryo param->p_value2 = 0x100000000ULL / d; 345 1.18 ryo param->p_flags |= TPROF_PARAM_VALUE2_SCALE; 346 1.18 ryo } 347 1.18 ryo } 348 1.18 ryo 349 1.18 ryo if (error != 0) { 350 1.18 ryo snprintf(errmsgbuf, sizeof(errmsgbuf), 351 1.18 ryo "invalid scale: %s", scale); 352 1.18 ryo goto done; 353 1.18 ryo } 354 1.18 ryo } 355 1.18 ryo 356 1.18 ryo done: 357 1.18 ryo if (eventnamep != NULL) 358 1.18 ryo *eventnamep = event; 359 1.18 ryo if (error != 0 && errmsgp != NULL) 360 1.18 ryo *errmsgp = errmsgbuf; 361 1.18 ryo return error; 362 1.18 ryo } 363 1.18 ryo 364 1.21 msaitoh const char * 365 1.21 msaitoh tprof_cycle_event_name(void) 366 1.21 msaitoh { 367 1.21 msaitoh const char *cycleevent; 368 1.21 msaitoh 369 1.21 msaitoh switch (tprof_info.ti_ident) { 370 1.21 msaitoh case TPROF_IDENT_INTEL_GENERIC: 371 1.21 msaitoh cycleevent = "unhalted-core-cycles"; 372 1.21 msaitoh break; 373 1.21 msaitoh case TPROF_IDENT_AMD_GENERIC: 374 1.21 msaitoh cycleevent = "LsNotHaltedCyc"; 375 1.21 msaitoh break; 376 1.21 msaitoh case TPROF_IDENT_ARMV8_GENERIC: 377 1.21 msaitoh case TPROF_IDENT_ARMV7_GENERIC: 378 1.21 msaitoh cycleevent = "CPU_CYCLES"; 379 1.21 msaitoh break; 380 1.21 msaitoh default: 381 1.21 msaitoh cycleevent = NULL; 382 1.21 msaitoh break; 383 1.21 msaitoh } 384 1.21 msaitoh return cycleevent; 385 1.21 msaitoh } 386 1.21 msaitoh 387 1.7 maxv static void 388 1.15 ryo tprof_monitor_common(bool do_profile, int argc, char **argv) 389 1.1 yamt { 390 1.7 maxv const char *outfile = "tprof.out"; 391 1.1 yamt struct tprof_stat ts; 392 1.14 ryo tprof_param_t params[TPROF_MAXCOUNTERS]; 393 1.1 yamt pid_t pid; 394 1.1 yamt pthread_t pt; 395 1.14 ryo int ret, ch, i; 396 1.18 ryo char *p, *errmsg; 397 1.14 ryo tprof_countermask_t mask = TPROF_COUNTERMASK_ALL; 398 1.6 maxv 399 1.14 ryo memset(params, 0, sizeof(params)); 400 1.6 maxv 401 1.15 ryo while ((ch = getopt(argc, argv, do_profile ? "o:e:" : "e:i:")) != -1) { 402 1.1 yamt switch (ch) { 403 1.1 yamt case 'o': 404 1.1 yamt outfile = optarg; 405 1.1 yamt break; 406 1.15 ryo case 'i': 407 1.15 ryo interval = strtod(optarg, &p); 408 1.15 ryo if (*p != '\0' || interval <= 0) 409 1.15 ryo errx(EXIT_FAILURE, "Bad/invalid interval: %s", 410 1.15 ryo optarg); 411 1.15 ryo break; 412 1.6 maxv case 'e': 413 1.18 ryo if (tprof_parse_event(¶ms[nevent], optarg, 414 1.18 ryo TPROF_PARSE_EVENT_F_ALLOWOPTION | 415 1.18 ryo (do_profile ? TPROF_PARSE_EVENT_F_ALLOWSCALE : 0), 416 1.18 ryo &eventname[nevent], &errmsg) != 0) { 417 1.18 ryo errx(EXIT_FAILURE, "%s", errmsg); 418 1.15 ryo } 419 1.15 ryo eventnamewidth[nevent] = strlen(eventname[nevent]); 420 1.15 ryo if (eventnamewidth[nevent] < COUNTER_COLUMNS_WIDTH) 421 1.15 ryo eventnamewidth[nevent] = COUNTER_COLUMNS_WIDTH; 422 1.14 ryo nevent++; 423 1.14 ryo if (nevent > __arraycount(params) || 424 1.14 ryo nevent > ncounters) 425 1.16 ryo errx(EXIT_FAILURE, "Too many events. Only a" 426 1.16 ryo " maximum of %d counters can be used.", 427 1.16 ryo ncounters); 428 1.6 maxv break; 429 1.1 yamt default: 430 1.1 yamt usage(); 431 1.1 yamt } 432 1.1 yamt } 433 1.1 yamt argc -= optind; 434 1.1 yamt argv += optind; 435 1.21 msaitoh if (argc == 0) 436 1.6 maxv usage(); 437 1.21 msaitoh if (nevent == 0) { 438 1.21 msaitoh const char *defaultevent = tprof_cycle_event_name(); 439 1.21 msaitoh if (defaultevent == NULL) 440 1.21 msaitoh errx(EXIT_FAILURE, "cpu not supported"); 441 1.21 msaitoh 442 1.21 msaitoh tprof_event_lookup(defaultevent, ¶ms[nevent]); 443 1.21 msaitoh eventname[nevent] = defaultevent; 444 1.21 msaitoh params[nevent].p_flags |= TPROF_PARAM_KERN; 445 1.21 msaitoh nevent++; 446 1.6 maxv } 447 1.6 maxv 448 1.15 ryo if (do_profile) { 449 1.15 ryo outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666); 450 1.15 ryo if (outfd == -1) { 451 1.15 ryo err(EXIT_FAILURE, "%s", outfile); 452 1.15 ryo } 453 1.1 yamt } 454 1.1 yamt 455 1.14 ryo for (i = 0; i < (int)nevent; i++) { 456 1.14 ryo params[i].p_counter = i; 457 1.15 ryo if (do_profile) 458 1.15 ryo params[i].p_flags |= TPROF_PARAM_PROFILE; 459 1.14 ryo ret = ioctl(devfd, TPROF_IOC_CONFIGURE_EVENT, ¶ms[i]); 460 1.16 ryo if (ret == -1) { 461 1.16 ryo err(EXIT_FAILURE, "TPROF_IOC_CONFIGURE_EVENT: %s", 462 1.16 ryo eventname[i]); 463 1.16 ryo } 464 1.14 ryo } 465 1.14 ryo 466 1.14 ryo ret = ioctl(devfd, TPROF_IOC_START, &mask); 467 1.1 yamt if (ret == -1) { 468 1.3 yamt err(EXIT_FAILURE, "TPROF_IOC_START"); 469 1.1 yamt } 470 1.1 yamt 471 1.1 yamt pid = fork(); 472 1.1 yamt switch (pid) { 473 1.1 yamt case -1: 474 1.1 yamt err(EXIT_FAILURE, "fork"); 475 1.1 yamt case 0: 476 1.1 yamt close(devfd); 477 1.1 yamt execvp(argv[0], argv); 478 1.1 yamt _Exit(EXIT_FAILURE); 479 1.1 yamt } 480 1.1 yamt 481 1.1 yamt signal(SIGINT, SIG_IGN); 482 1.15 ryo if (do_profile) 483 1.15 ryo signal(SIGINFO, siginfo_showcount); 484 1.15 ryo else 485 1.15 ryo signal(SIGINFO, siginfo_nothing); 486 1.15 ryo 487 1.15 ryo unsigned int done = 0; 488 1.15 ryo if (do_profile) 489 1.15 ryo ret = pthread_create(&pt, NULL, process_samples, NULL); 490 1.15 ryo else 491 1.15 ryo ret = pthread_create(&pt, NULL, process_stat, &done); 492 1.15 ryo if (ret != 0) 493 1.7 maxv errx(1, "pthread_create: %s", strerror(ret)); 494 1.1 yamt 495 1.1 yamt for (;;) { 496 1.1 yamt int status; 497 1.1 yamt 498 1.1 yamt pid = wait4(-1, &status, 0, NULL); 499 1.1 yamt if (pid == -1) { 500 1.1 yamt if (errno == ECHILD) { 501 1.1 yamt break; 502 1.1 yamt } 503 1.1 yamt err(EXIT_FAILURE, "wait4"); 504 1.1 yamt } 505 1.1 yamt if (pid != 0 && WIFEXITED(status)) { 506 1.1 yamt break; 507 1.1 yamt } 508 1.1 yamt } 509 1.1 yamt 510 1.14 ryo ret = ioctl(devfd, TPROF_IOC_STOP, &mask); 511 1.1 yamt if (ret == -1) { 512 1.1 yamt err(EXIT_FAILURE, "TPROF_IOC_STOP"); 513 1.1 yamt } 514 1.1 yamt 515 1.15 ryo if (!do_profile) { 516 1.15 ryo atomic_add_int(&done, 1); /* terminate thread */ 517 1.15 ryo kill(0, SIGINFO); 518 1.15 ryo } 519 1.15 ryo 520 1.1 yamt pthread_join(pt, NULL); 521 1.1 yamt 522 1.15 ryo if (do_profile) { 523 1.15 ryo ret = ioctl(devfd, TPROF_IOC_GETSTAT, &ts); 524 1.15 ryo if (ret == -1) 525 1.15 ryo err(EXIT_FAILURE, "TPROF_IOC_GETSTAT"); 526 1.15 ryo 527 1.15 ryo fprintf(stderr, "\n%s statistics:\n", getprogname()); 528 1.15 ryo fprintf(stderr, "\tsample %" PRIu64 "\n", ts.ts_sample); 529 1.15 ryo fprintf(stderr, "\toverflow %" PRIu64 "\n", ts.ts_overflow); 530 1.15 ryo fprintf(stderr, "\tbuf %" PRIu64 "\n", ts.ts_buf); 531 1.15 ryo fprintf(stderr, "\temptybuf %" PRIu64 "\n", ts.ts_emptybuf); 532 1.15 ryo fprintf(stderr, "\tdropbuf %" PRIu64 "\n", ts.ts_dropbuf); 533 1.16 ryo fprintf(stderr, "\tdropbuf_sample %" PRIu64 "\n", 534 1.16 ryo ts.ts_dropbuf_sample); 535 1.15 ryo 536 1.15 ryo fprintf(stderr, "\n"); 537 1.1 yamt } 538 1.15 ryo show_counters(); 539 1.1 yamt 540 1.15 ryo exit(EXIT_SUCCESS); 541 1.15 ryo } 542 1.1 yamt 543 1.15 ryo static void 544 1.15 ryo tprof_monitor(int argc, char **argv) 545 1.15 ryo { 546 1.15 ryo tprof_monitor_common(true, argc, argv); 547 1.15 ryo } 548 1.15 ryo 549 1.15 ryo static void 550 1.15 ryo tprof_count(int argc, char **argv) 551 1.15 ryo { 552 1.15 ryo tprof_monitor_common(false, argc, argv); 553 1.1 yamt } 554 1.7 maxv 555 1.7 maxv int 556 1.7 maxv main(int argc, char *argv[]) 557 1.7 maxv { 558 1.7 maxv const struct cmdtab *ct; 559 1.7 maxv int ret; 560 1.7 maxv 561 1.15 ryo getncpu(); 562 1.7 maxv setprogname(argv[0]); 563 1.7 maxv argv += 1, argc -= 1; 564 1.7 maxv 565 1.7 maxv devfd = open(_PATH_TPROF, O_RDWR); 566 1.7 maxv if (devfd == -1) { 567 1.7 maxv err(EXIT_FAILURE, "%s", _PATH_TPROF); 568 1.7 maxv } 569 1.7 maxv 570 1.14 ryo ret = ioctl(devfd, TPROF_IOC_GETINFO, &tprof_info); 571 1.7 maxv if (ret == -1) { 572 1.7 maxv err(EXIT_FAILURE, "TPROF_IOC_GETINFO"); 573 1.7 maxv } 574 1.14 ryo if (tprof_info.ti_version != TPROF_VERSION) { 575 1.7 maxv errx(EXIT_FAILURE, "version mismatch: version=%d, expected=%d", 576 1.14 ryo tprof_info.ti_version, TPROF_VERSION); 577 1.7 maxv } 578 1.14 ryo if (tprof_event_init(tprof_info.ti_ident) == -1) { 579 1.13 maxv errx(EXIT_FAILURE, "cpu not supported"); 580 1.7 maxv } 581 1.7 maxv 582 1.14 ryo ret = ioctl(devfd, TPROF_IOC_GETNCOUNTERS, &ncounters); 583 1.14 ryo if (ret == -1) { 584 1.14 ryo err(EXIT_FAILURE, "TPROF_IOC_GETNCOUNTERS"); 585 1.14 ryo } 586 1.14 ryo if (ncounters == 0) { 587 1.14 ryo errx(EXIT_FAILURE, "no available counters"); 588 1.14 ryo } 589 1.14 ryo 590 1.11 jmcneill if (argc == 0) 591 1.11 jmcneill usage(); 592 1.11 jmcneill 593 1.7 maxv for (ct = tprof_cmdtab; ct->label != NULL; ct++) { 594 1.7 maxv if (strcmp(argv[0], ct->label) == 0) { 595 1.7 maxv if (!ct->argsoptional && 596 1.7 maxv ((ct->takesargs == 0) ^ (argv[1] == NULL))) 597 1.7 maxv { 598 1.7 maxv usage(); 599 1.7 maxv } 600 1.7 maxv (*ct->func)(argc, argv); 601 1.7 maxv break; 602 1.7 maxv } 603 1.7 maxv } 604 1.9 maxv if (ct->label == NULL) { 605 1.9 maxv usage(); 606 1.9 maxv } 607 1.7 maxv } 608