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