1 /*- 2 * Copyright (c) 2003,2004 Joseph Koshy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/types.h> 32 #include <sys/queue.h> 33 #include <sys/sysctl.h> 34 35 #include <assert.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <limits.h> 40 #include <pmc.h> 41 #include <stdarg.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <sysexits.h> 46 #include <unistd.h> 47 48 /* Compile time defaults */ 49 50 #define PMCC_PRINT_USAGE 0 51 #define PMCC_PRINT_EVENTS 1 52 #define PMCC_LIST_STATE 2 53 #define PMCC_ENABLE_DISABLE 3 54 #define PMCC_SHOW_STATISTICS 4 55 56 #define PMCC_CPU_ALL -1 57 #define PMCC_CPU_WILDCARD '*' 58 59 #define PMCC_PMC_ALL -1 60 #define PMCC_PMC_WILDCARD '*' 61 62 #define PMCC_OP_IGNORE 0 63 #define PMCC_OP_DISABLE 1 64 #define PMCC_OP_ENABLE 2 65 66 #define PMCC_PROGRAM_NAME "pmccontrol" 67 68 STAILQ_HEAD(pmcc_op_list, pmcc_op) head = STAILQ_HEAD_INITIALIZER(head); 69 70 struct pmcc_op { 71 char op_cpu; 72 char op_pmc; 73 char op_op; 74 STAILQ_ENTRY(pmcc_op) op_next; 75 }; 76 77 /* Function Prototypes */ 78 #if DEBUG 79 static void pmcc_init_debug(void); 80 #endif 81 82 static int pmcc_do_list_state(void); 83 static int pmcc_do_enable_disable(struct pmcc_op_list *); 84 static int pmcc_do_list_events(void); 85 86 /* Globals */ 87 88 static char usage_message[] = 89 "Usage:\n" 90 " " PMCC_PROGRAM_NAME " -L\n" 91 " " PMCC_PROGRAM_NAME " -l\n" 92 " " PMCC_PROGRAM_NAME " -s\n" 93 " " PMCC_PROGRAM_NAME " [-e pmc | -d pmc | -c cpu] ..."; 94 95 #if DEBUG 96 FILE *debug_stream = NULL; 97 #endif 98 99 #if DEBUG 100 #define DEBUG_MSG(...) \ 101 (void) fprintf(debug_stream, "[pmccontrol] " __VA_ARGS__); 102 #else 103 #define DEBUG_MSG(m) /* */ 104 #endif /* !DEBUG */ 105 106 int pmc_syscall = -1; 107 108 #define PMC_CALL(cmd, params) \ 109 if ((error = syscall(pmc_syscall, PMC_OP_##cmd, (params))) != 0) \ 110 { \ 111 DEBUG_MSG("ERROR: syscall [" #cmd "]"); \ 112 exit(EX_OSERR); \ 113 } 114 115 #if DEBUG 116 /* log debug messages to a separate file */ 117 static void 118 pmcc_init_debug(void) 119 { 120 char *fn; 121 122 fn = getenv("PMCCONTROL_DEBUG"); 123 if (fn != NULL) 124 { 125 debug_stream = fopen(fn, "w"); 126 if (debug_stream == NULL) 127 debug_stream = stderr; 128 } else 129 debug_stream = stderr; 130 } 131 #endif 132 133 static int 134 pmcc_do_enable_disable(struct pmcc_op_list *op_list) 135 { 136 unsigned char op; 137 int c, error, i, j, ncpu, npmc, t; 138 int cpu, pmc; 139 struct pmcc_op *np; 140 unsigned char *map; 141 142 if ((ncpu = pmc_ncpu()) < 0) 143 err(EX_OSERR, "Unable to determine the number of cpus"); 144 145 /* determine the maximum number of PMCs in any CPU */ 146 npmc = 0; 147 for (c = 0; c < ncpu; c++) { 148 if ((t = pmc_npmc(c)) < 0) 149 err(EX_OSERR, "Unable to determine the number of PMCs in " 150 "CPU %d", c); 151 npmc = t > npmc ? t : npmc; 152 } 153 154 if (npmc == 0) 155 errx(EX_CONFIG, "No PMCs found"); 156 157 if ((map = malloc(npmc * ncpu)) == NULL) 158 err(EX_SOFTWARE, "Out of memory"); 159 160 (void) memset(map, PMCC_OP_IGNORE, npmc*ncpu); 161 162 error = 0; 163 STAILQ_FOREACH(np, op_list, op_next) { 164 165 cpu = np->op_cpu; 166 pmc = np->op_pmc; 167 op = np->op_op; 168 169 if (cpu >= ncpu) 170 errx(EX_DATAERR, "CPU id too large: \"%d\"", cpu); 171 172 if (pmc >= npmc) 173 errx(EX_DATAERR, "PMC id too large: \"%d\"", pmc); 174 175 #define MARKMAP(M,C,P,V) do { \ 176 *((M) + (C)*npmc + (P)) = (V); \ 177 } while (0) 178 179 #define SET_PMCS(C,P,V) do { \ 180 if ((P) == PMCC_PMC_ALL) { \ 181 for (j = 0; j < npmc; j++) \ 182 MARKMAP(map, (C), j, (V)); \ 183 } else \ 184 MARKMAP(map, (C), (P), (V)); \ 185 } while (0) 186 187 #define MAP(M,C,P) (*((M) + (C)*npmc + (P))) 188 189 if (cpu == PMCC_CPU_ALL) 190 for (i = 0; i < ncpu; i++) 191 SET_PMCS(i, pmc, op); 192 else 193 SET_PMCS(cpu, pmc, op); 194 } 195 196 /* Configure PMCS */ 197 for (i = 0; i < ncpu; i++) 198 for (j = 0; j < npmc; j++) { 199 unsigned char b; 200 201 b = MAP(map, i, j); 202 203 error = 0; 204 205 if (b == PMCC_OP_ENABLE) 206 error = pmc_enable(i, j); 207 else if (b == PMCC_OP_DISABLE) 208 error = pmc_disable(i, j); 209 210 if (error < 0) 211 err(EX_OSERR, "%s of PMC %d on CPU %d failed", 212 b == PMCC_OP_ENABLE ? "Enable" : 213 "Disable", j, i); 214 } 215 216 return error; 217 } 218 219 static int 220 pmcc_do_list_state(void) 221 { 222 size_t dummy; 223 int c, cpu, n, npmc, ncpu; 224 unsigned int logical_cpus_mask; 225 struct pmc_info *pd; 226 struct pmc_pmcinfo *pi; 227 const struct pmc_cpuinfo *pc; 228 229 if (pmc_cpuinfo(&pc) != 0) 230 err(EX_OSERR, "Unable to determine CPU information"); 231 232 dummy = sizeof(logical_cpus_mask); 233 if (sysctlbyname("machdep.logical_cpus_mask", &logical_cpus_mask, 234 &dummy, NULL, 0) < 0) 235 logical_cpus_mask = 0; 236 237 ncpu = pc->pm_ncpu; 238 239 for (c = cpu = 0; cpu < ncpu; cpu++) { 240 #if defined(__i386__) || defined(__amd64__) 241 if (pc->pm_cputype == PMC_CPU_INTEL_PIV && 242 (logical_cpus_mask & (1 << cpu))) 243 continue; /* skip P4-style 'logical' cpus */ 244 #endif 245 if (pmc_pmcinfo(cpu, &pi) < 0) 246 err(EX_OSERR, "Unable to get PMC status for CPU %d", 247 cpu); 248 249 printf("#CPU %d:\n", c++); 250 npmc = pmc_npmc(cpu); 251 printf("#N NAME CLASS STATE ROW-DISP\n"); 252 253 for (n = 0; n < npmc; n++) { 254 pd = &pi->pm_pmcs[n]; 255 256 printf(" %-2d %-16s %-6s %-8s %-10s", 257 n, 258 pd->pm_name, 259 pmc_name_of_class(pd->pm_class), 260 pd->pm_enabled ? "ENABLED" : "DISABLED", 261 pmc_name_of_disposition(pd->pm_rowdisp)); 262 263 if (pd->pm_ownerpid != -1) { 264 printf(" (pid %d)", pd->pm_ownerpid); 265 printf(" %-32s", 266 pmc_name_of_event(pd->pm_event)); 267 if (PMC_IS_SAMPLING_MODE(pd->pm_mode)) 268 printf(" (reload count %jd)", 269 pd->pm_reloadcount); 270 } 271 printf("\n"); 272 } 273 free(pi); 274 } 275 return 0; 276 } 277 278 static int 279 pmcc_do_list_events(void) 280 { 281 enum pmc_class c; 282 unsigned int i, j, nevents; 283 const char **eventnamelist; 284 const struct pmc_cpuinfo *ci; 285 286 if (pmc_cpuinfo(&ci) != 0) 287 err(EX_OSERR, "Unable to determine CPU information"); 288 289 eventnamelist = NULL; 290 291 for (i = 0; i < ci->pm_nclass; i++) { 292 c = ci->pm_classes[i].pm_class; 293 294 printf("%s\n", pmc_name_of_class(c)); 295 if (pmc_event_names_of_class(c, &eventnamelist, &nevents) < 0) 296 err(EX_OSERR, "ERROR: Cannot find information for " 297 "event class \"%s\"", pmc_name_of_class(c)); 298 299 for (j = 0; j < nevents; j++) 300 printf("\t%s\n", eventnamelist[j]); 301 302 free(eventnamelist); 303 } 304 return 0; 305 } 306 307 static int 308 pmcc_show_statistics(void) 309 { 310 311 struct pmc_driverstats gms; 312 313 if (pmc_get_driver_stats(&gms) < 0) 314 err(EX_OSERR, "ERROR: cannot retrieve driver statistics"); 315 316 /* 317 * Print statistics. 318 */ 319 320 #define PRINT(N,V) (void) printf("%-40s %d\n", (N), gms.pm_##V) 321 PRINT("interrupts processed:", intr_processed); 322 PRINT("non-PMC interrupts:", intr_ignored); 323 PRINT("sampling stalls due to space shortages:", intr_bufferfull); 324 PRINT("system calls:", syscalls); 325 PRINT("system calls with errors:", syscall_errors); 326 PRINT("buffer requests:", buffer_requests); 327 PRINT("buffer requests failed:", buffer_requests_failed); 328 PRINT("sampling log sweeps:", log_sweeps); 329 330 return 0; 331 } 332 333 /* 334 * Main 335 */ 336 337 int 338 main(int argc, char **argv) 339 { 340 int error, command, currentcpu, option, pmc; 341 char *dummy; 342 struct pmcc_op *p; 343 344 #if DEBUG 345 pmcc_init_debug(); 346 #endif 347 348 /* parse args */ 349 350 currentcpu = PMCC_CPU_ALL; 351 command = PMCC_PRINT_USAGE; 352 error = 0; 353 354 STAILQ_INIT(&head); 355 356 while ((option = getopt(argc, argv, ":c:d:e:lLs")) != -1) 357 switch (option) { 358 case 'L': 359 if (command != PMCC_PRINT_USAGE) { 360 error = 1; 361 break; 362 } 363 command = PMCC_PRINT_EVENTS; 364 break; 365 366 case 'c': 367 if (command != PMCC_PRINT_USAGE && 368 command != PMCC_ENABLE_DISABLE) { 369 error = 1; 370 break; 371 } 372 command = PMCC_ENABLE_DISABLE; 373 374 if (*optarg == PMCC_CPU_WILDCARD) 375 currentcpu = PMCC_CPU_ALL; 376 else { 377 currentcpu = strtoul(optarg, &dummy, 0); 378 if (*dummy != '\0' || currentcpu < 0) 379 errx(EX_DATAERR, 380 "\"%s\" is not a valid CPU id", 381 optarg); 382 } 383 break; 384 385 case 'd': 386 case 'e': 387 if (command != PMCC_PRINT_USAGE && 388 command != PMCC_ENABLE_DISABLE) { 389 error = 1; 390 break; 391 } 392 command = PMCC_ENABLE_DISABLE; 393 394 if (*optarg == PMCC_PMC_WILDCARD) 395 pmc = PMCC_PMC_ALL; 396 else { 397 pmc = strtoul(optarg, &dummy, 0); 398 if (*dummy != '\0' || pmc < 0) 399 errx(EX_DATAERR, 400 "\"%s\" is not a valid PMC id", 401 optarg); 402 } 403 404 if ((p = malloc(sizeof(*p))) == NULL) 405 err(EX_SOFTWARE, "Out of memory"); 406 407 p->op_cpu = currentcpu; 408 p->op_pmc = pmc; 409 p->op_op = option == 'd' ? PMCC_OP_DISABLE : 410 PMCC_OP_ENABLE; 411 412 STAILQ_INSERT_TAIL(&head, p, op_next); 413 break; 414 415 case 'l': 416 if (command != PMCC_PRINT_USAGE) { 417 error = 1; 418 break; 419 } 420 command = PMCC_LIST_STATE; 421 break; 422 423 case 's': 424 if (command != PMCC_PRINT_USAGE) { 425 error = 1; 426 break; 427 } 428 command = PMCC_SHOW_STATISTICS; 429 break; 430 431 case ':': 432 errx(EX_USAGE, 433 "Missing argument to option '-%c'", optopt); 434 break; 435 436 case '?': 437 warnx("Unrecognized option \"-%c\"", optopt); 438 errx(EX_USAGE, usage_message); 439 break; 440 441 default: 442 error = 1; 443 break; 444 445 } 446 447 if (command == PMCC_PRINT_USAGE) 448 (void) errx(EX_USAGE, usage_message); 449 450 if (error) 451 exit(EX_USAGE); 452 453 if (pmc_init() < 0) 454 err(EX_UNAVAILABLE, 455 "Initialization of the pmc(3) library failed"); 456 457 switch (command) { 458 case PMCC_LIST_STATE: 459 error = pmcc_do_list_state(); 460 break; 461 case PMCC_PRINT_EVENTS: 462 error = pmcc_do_list_events(); 463 break; 464 case PMCC_SHOW_STATISTICS: 465 error = pmcc_show_statistics(); 466 break; 467 case PMCC_ENABLE_DISABLE: 468 if (STAILQ_EMPTY(&head)) 469 errx(EX_USAGE, "No PMCs specified to enable or disable"); 470 error = pmcc_do_enable_disable(&head); 471 break; 472 default: 473 assert(0); 474 475 } 476 477 if (error != 0) 478 err(EX_OSERR, "Command failed"); 479 exit(0); 480 } 481