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, "Unable to determine the number of " 151 "PMCs in CPU %d", c); 152 npmc = t > npmc ? t : npmc; 153 } 154 155 if (npmc == 0) 156 errx(EX_CONFIG, "No PMCs found"); 157 158 if ((map = malloc(npmc * ncpu)) == NULL) 159 err(EX_SOFTWARE, "Out of memory"); 160 161 (void) memset(map, PMCC_OP_IGNORE, npmc*ncpu); 162 163 error = 0; 164 STAILQ_FOREACH(np, op_list, op_next) { 165 166 cpu = np->op_cpu; 167 pmc = np->op_pmc; 168 op = np->op_op; 169 170 if (cpu >= ncpu) 171 errx(EX_DATAERR, "CPU id too large: \"%d\"", cpu); 172 173 if (pmc >= npmc) 174 errx(EX_DATAERR, "PMC id too large: \"%d\"", pmc); 175 176 #define MARKMAP(M,C,P,V) do { \ 177 *((M) + (C)*npmc + (P)) = (V); \ 178 } while (0) 179 180 #define SET_PMCS(C,P,V) do { \ 181 if ((P) == PMCC_PMC_ALL) { \ 182 for (j = 0; j < npmc; j++) \ 183 MARKMAP(map, (C), j, (V)); \ 184 } else \ 185 MARKMAP(map, (C), (P), (V)); \ 186 } while (0) 187 188 #define MAP(M,C,P) (*((M) + (C)*npmc + (P))) 189 190 if (cpu == PMCC_CPU_ALL) 191 for (i = 0; i < ncpu; i++) { 192 SET_PMCS(i, pmc, op); 193 } 194 else 195 SET_PMCS(cpu, pmc, op); 196 } 197 198 /* Configure PMCS */ 199 for (i = 0; i < ncpu; i++) 200 for (j = 0; j < npmc; j++) { 201 unsigned char b; 202 203 b = MAP(map, i, j); 204 205 error = 0; 206 207 if (b == PMCC_OP_ENABLE) 208 error = pmc_enable(i, j); 209 else if (b == PMCC_OP_DISABLE) 210 error = pmc_disable(i, j); 211 212 if (error < 0) 213 err(EX_OSERR, "%s of PMC %d on CPU %d failed", 214 b == PMCC_OP_ENABLE ? "Enable" : 215 "Disable", j, i); 216 } 217 218 return error; 219 } 220 221 static int 222 pmcc_do_list_state(void) 223 { 224 cpuset_t logical_cpus_mask; 225 long cpusetsize; 226 size_t setsize; 227 int c, cpu, n, npmc, ncpu; 228 struct pmc_info *pd; 229 struct pmc_pmcinfo *pi; 230 const struct pmc_cpuinfo *pc; 231 232 if (pmc_cpuinfo(&pc) != 0) 233 err(EX_OSERR, "Unable to determine CPU information"); 234 235 printf("%d %s CPUs present, with %d PMCs per CPU\n", pc->pm_ncpu, 236 pmc_name_of_cputype(pc->pm_cputype), 237 pc->pm_npmc); 238 239 /* Determine the set of logical CPUs. */ 240 cpusetsize = sysconf(_SC_CPUSET_SIZE); 241 if (cpusetsize == -1 || (u_long)cpusetsize > sizeof(cpuset_t)) 242 err(EX_OSERR, "Cannot determine which CPUs are logical"); 243 CPU_ZERO(&logical_cpus_mask); 244 setsize = (size_t)cpusetsize; 245 if (sysctlbyname("machdep.logical_cpus_mask", &logical_cpus_mask, 246 &setsize, NULL, 0) < 0) 247 CPU_ZERO(&logical_cpus_mask); 248 249 ncpu = pc->pm_ncpu; 250 251 for (c = cpu = 0; cpu < ncpu; cpu++) { 252 #if defined(__i386__) || defined(__amd64__) 253 if (pc->pm_cputype == PMC_CPU_INTEL_PIV && 254 CPU_ISSET(cpu, &logical_cpus_mask)) 255 continue; /* skip P4-style 'logical' cpus */ 256 #endif 257 if (pmc_pmcinfo(cpu, &pi) < 0) { 258 if (errno == ENXIO) 259 continue; 260 err(EX_OSERR, "Unable to get PMC status for CPU %d", 261 cpu); 262 } 263 264 printf("#CPU %d:\n", c++); 265 npmc = pmc_npmc(cpu); 266 printf("#N NAME CLASS STATE ROW-DISP\n"); 267 268 for (n = 0; n < npmc; n++) { 269 pd = &pi->pm_pmcs[n]; 270 271 printf(" %-2d %-16s %-6s %-8s %-10s", 272 n, 273 pd->pm_name, 274 pmc_name_of_class(pd->pm_class), 275 pd->pm_enabled ? "ENABLED" : "DISABLED", 276 pmc_name_of_disposition(pd->pm_rowdisp)); 277 278 if (pd->pm_ownerpid != -1) { 279 printf(" (pid %d)", pd->pm_ownerpid); 280 printf(" %-32s", 281 pmc_name_of_event(pd->pm_event)); 282 if (PMC_IS_SAMPLING_MODE(pd->pm_mode)) 283 printf(" (reload count %jd)", 284 pd->pm_reloadcount); 285 } 286 printf("\n"); 287 } 288 free(pi); 289 } 290 return 0; 291 } 292 293 static int 294 pmcc_do_list_events(void) 295 { 296 enum pmc_class c; 297 unsigned int i, j, nevents; 298 const char **eventnamelist; 299 const struct pmc_cpuinfo *ci; 300 301 if (pmc_cpuinfo(&ci) != 0) 302 err(EX_OSERR, "Unable to determine CPU information"); 303 304 eventnamelist = NULL; 305 306 for (i = 0; i < ci->pm_nclass; i++) { 307 c = ci->pm_classes[i].pm_class; 308 309 printf("%s\n", pmc_name_of_class(c)); 310 if (pmc_event_names_of_class(c, &eventnamelist, &nevents) < 0) 311 err(EX_OSERR, "ERROR: Cannot find information for " 312 "event class \"%s\"", pmc_name_of_class(c)); 313 314 for (j = 0; j < nevents; j++) 315 printf("\t%s\n", eventnamelist[j]); 316 317 free(eventnamelist); 318 } 319 return 0; 320 } 321 322 static int 323 pmcc_show_statistics(void) 324 { 325 326 struct pmc_driverstats gms; 327 328 if (pmc_get_driver_stats(&gms) < 0) 329 err(EX_OSERR, "ERROR: cannot retrieve driver statistics"); 330 331 /* 332 * Print statistics. 333 */ 334 335 #define PRINT(N,V) (void) printf("%-40s %d\n", (N), gms.pm_##V) 336 PRINT("interrupts processed:", intr_processed); 337 PRINT("non-PMC interrupts:", intr_ignored); 338 PRINT("sampling stalls due to space shortages:", intr_bufferfull); 339 PRINT("system calls:", syscalls); 340 PRINT("system calls with errors:", syscall_errors); 341 PRINT("buffer requests:", buffer_requests); 342 PRINT("buffer requests failed:", buffer_requests_failed); 343 PRINT("sampling log sweeps:", log_sweeps); 344 345 return 0; 346 } 347 348 /* 349 * Main 350 */ 351 352 int 353 main(int argc, char **argv) 354 { 355 int error, command, currentcpu, option, pmc; 356 char *dummy; 357 struct pmcc_op *p; 358 359 #if DEBUG 360 pmcc_init_debug(); 361 #endif 362 363 /* parse args */ 364 365 currentcpu = PMCC_CPU_ALL; 366 command = PMCC_PRINT_USAGE; 367 error = 0; 368 369 STAILQ_INIT(&head); 370 371 while ((option = getopt(argc, argv, ":c:d:e:lLs")) != -1) 372 switch (option) { 373 case 'L': 374 if (command != PMCC_PRINT_USAGE) { 375 error = 1; 376 break; 377 } 378 command = PMCC_PRINT_EVENTS; 379 break; 380 381 case 'c': 382 if (command != PMCC_PRINT_USAGE && 383 command != PMCC_ENABLE_DISABLE) { 384 error = 1; 385 break; 386 } 387 command = PMCC_ENABLE_DISABLE; 388 389 if (*optarg == PMCC_CPU_WILDCARD) 390 currentcpu = PMCC_CPU_ALL; 391 else { 392 currentcpu = strtoul(optarg, &dummy, 0); 393 if (*dummy != '\0' || currentcpu < 0) 394 errx(EX_DATAERR, 395 "\"%s\" is not a valid CPU id", 396 optarg); 397 } 398 break; 399 400 case 'd': 401 case 'e': 402 if (command != PMCC_PRINT_USAGE && 403 command != PMCC_ENABLE_DISABLE) { 404 error = 1; 405 break; 406 } 407 command = PMCC_ENABLE_DISABLE; 408 409 if (*optarg == PMCC_PMC_WILDCARD) 410 pmc = PMCC_PMC_ALL; 411 else { 412 pmc = strtoul(optarg, &dummy, 0); 413 if (*dummy != '\0' || pmc < 0) 414 errx(EX_DATAERR, 415 "\"%s\" is not a valid PMC id", 416 optarg); 417 } 418 419 if ((p = malloc(sizeof(*p))) == NULL) 420 err(EX_SOFTWARE, "Out of memory"); 421 422 p->op_cpu = currentcpu; 423 p->op_pmc = pmc; 424 p->op_op = option == 'd' ? PMCC_OP_DISABLE : 425 PMCC_OP_ENABLE; 426 427 STAILQ_INSERT_TAIL(&head, p, op_next); 428 break; 429 430 case 'l': 431 if (command != PMCC_PRINT_USAGE) { 432 error = 1; 433 break; 434 } 435 command = PMCC_LIST_STATE; 436 break; 437 438 case 's': 439 if (command != PMCC_PRINT_USAGE) { 440 error = 1; 441 break; 442 } 443 command = PMCC_SHOW_STATISTICS; 444 break; 445 446 case ':': 447 errx(EX_USAGE, 448 "Missing argument to option '-%c'", optopt); 449 break; 450 451 case '?': 452 warnx("Unrecognized option \"-%c\"", optopt); 453 errx(EX_USAGE, usage_message); 454 break; 455 456 default: 457 error = 1; 458 break; 459 460 } 461 462 if (command == PMCC_PRINT_USAGE) 463 (void) errx(EX_USAGE, usage_message); 464 465 if (error) 466 exit(EX_USAGE); 467 468 if (pmc_init() < 0) 469 err(EX_UNAVAILABLE, 470 "Initialization of the pmc(3) library failed"); 471 472 switch (command) { 473 case PMCC_LIST_STATE: 474 error = pmcc_do_list_state(); 475 break; 476 case PMCC_PRINT_EVENTS: 477 error = pmcc_do_list_events(); 478 break; 479 case PMCC_SHOW_STATISTICS: 480 error = pmcc_show_statistics(); 481 break; 482 case PMCC_ENABLE_DISABLE: 483 if (STAILQ_EMPTY(&head)) 484 errx(EX_USAGE, "No PMCs specified to enable or disable"); 485 error = pmcc_do_enable_disable(&head); 486 break; 487 default: 488 assert(0); 489 490 } 491 492 if (error != 0) 493 err(EX_OSERR, "Command failed"); 494 exit(0); 495 } 496