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