Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * perfstat: /usr/bin/time -alike performance counter statistics utility |
| 3 | * |
| 4 | * It summarizes the counter events of all tasks (and child tasks), |
| 5 | * covering all CPUs that the command (or workload) executes on. |
| 6 | * It only counts the per-task events of the workload started, |
| 7 | * independent of how many other tasks run on those CPUs. |
| 8 | * |
| 9 | * Build with: cc -O2 -g -lrt -Wall -W -o perfstat perfstat.c |
| 10 | * |
| 11 | * Sample output: |
| 12 | * |
| 13 | |
| 14 | $ ./perfstat -e 1 -e 3 -e 5 ls -lR /usr/include/ >/dev/null |
| 15 | |
| 16 | Performance counter stats for 'ls': |
| 17 | |
| 18 | 163516953 instructions |
| 19 | 2295 cache-misses |
| 20 | 2855182 branch-misses |
| 21 | |
| 22 | * |
| 23 | * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com> |
| 24 | * |
| 25 | * Released under the GPLv2 (not later). |
| 26 | * |
| 27 | * Percpu counter support by: Yanmin Zhang <yanmin_zhang@linux.intel.com> |
| 28 | * Symbolic event options by: Wu Fengguang <fengguang.wu@intel.com> |
| 29 | */ |
| 30 | #define _GNU_SOURCE |
| 31 | |
| 32 | #include <assert.h> |
| 33 | #include <getopt.h> |
| 34 | #include <stdint.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <unistd.h> |
| 38 | #include <ctype.h> |
| 39 | #include <errno.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <stdio.h> |
| 42 | #include <time.h> |
| 43 | |
| 44 | #include <sys/syscall.h> |
| 45 | #include <sys/ioctl.h> |
| 46 | #include <sys/prctl.h> |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <sys/time.h> |
| 50 | #include <sys/wait.h> |
| 51 | #include <sys/uio.h> |
| 52 | |
| 53 | #include <linux/unistd.h> |
| 54 | |
| 55 | #ifdef __x86_64__ |
| 56 | # define __NR_perf_counter_open 295 |
| 57 | #endif |
| 58 | |
| 59 | #ifdef __i386__ |
| 60 | # define __NR_perf_counter_open 333 |
| 61 | #endif |
| 62 | |
| 63 | #ifdef __powerpc__ |
| 64 | #define __NR_perf_counter_open 319 |
| 65 | #endif |
| 66 | |
| 67 | /* |
| 68 | * Pick up some kernel type conventions: |
| 69 | */ |
| 70 | #define __user |
| 71 | #define asmlinkage |
| 72 | |
| 73 | typedef unsigned int __u32; |
| 74 | typedef unsigned long long __u64; |
| 75 | typedef long long __s64; |
| 76 | |
| 77 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 78 | |
| 79 | /* |
| 80 | * User-space ABI bits: |
| 81 | */ |
| 82 | |
| 83 | /* |
| 84 | * Generalized performance counter event types, used by the hw_event.type |
| 85 | * parameter of the sys_perf_counter_open() syscall: |
| 86 | */ |
| 87 | enum hw_event_types { |
| 88 | /* |
| 89 | * Common hardware events, generalized by the kernel: |
| 90 | */ |
| 91 | PERF_COUNT_CPU_CYCLES = 0, |
| 92 | PERF_COUNT_INSTRUCTIONS = 1, |
| 93 | PERF_COUNT_CACHE_REFERENCES = 2, |
| 94 | PERF_COUNT_CACHE_MISSES = 3, |
| 95 | PERF_COUNT_BRANCH_INSTRUCTIONS = 4, |
| 96 | PERF_COUNT_BRANCH_MISSES = 5, |
| 97 | PERF_COUNT_BUS_CYCLES = 6, |
| 98 | |
| 99 | PERF_HW_EVENTS_MAX = 7, |
| 100 | |
| 101 | /* |
| 102 | * Special "software" counters provided by the kernel, even if |
| 103 | * the hardware does not support performance counters. These |
| 104 | * counters measure various physical and sw events of the |
| 105 | * kernel (and allow the profiling of them as well): |
| 106 | */ |
| 107 | PERF_COUNT_CPU_CLOCK = -1, |
| 108 | PERF_COUNT_TASK_CLOCK = -2, |
| 109 | PERF_COUNT_PAGE_FAULTS = -3, |
| 110 | PERF_COUNT_CONTEXT_SWITCHES = -4, |
| 111 | PERF_COUNT_CPU_MIGRATIONS = -5, |
| 112 | |
| 113 | PERF_SW_EVENTS_MIN = -6, |
| 114 | }; |
| 115 | |
| 116 | /* |
| 117 | * IRQ-notification data record type: |
| 118 | */ |
| 119 | enum perf_counter_record_type { |
| 120 | PERF_RECORD_SIMPLE = 0, |
| 121 | PERF_RECORD_IRQ = 1, |
| 122 | PERF_RECORD_GROUP = 2, |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * Hardware event to monitor via a performance monitoring counter: |
| 127 | */ |
| 128 | struct perf_counter_hw_event { |
| 129 | __s64 type; |
| 130 | |
| 131 | __u64 irq_period; |
| 132 | __u64 record_type; |
| 133 | __u64 read_format; |
| 134 | |
| 135 | __u64 disabled : 1, /* off by default */ |
| 136 | nmi : 1, /* NMI sampling */ |
| 137 | raw : 1, /* raw event type */ |
| 138 | inherit : 1, /* children inherit it */ |
| 139 | pinned : 1, /* must always be on PMU */ |
| 140 | exclusive : 1, /* only group on PMU */ |
| 141 | exclude_user : 1, /* don't count user */ |
| 142 | exclude_kernel : 1, /* ditto kernel */ |
| 143 | exclude_hv : 1, /* ditto hypervisor */ |
| 144 | exclude_idle : 1, /* don't count when idle */ |
| 145 | |
| 146 | __reserved_1 : 54; |
| 147 | |
| 148 | __u32 extra_config_len; |
| 149 | __u32 __reserved_4; |
| 150 | |
| 151 | __u64 __reserved_2; |
| 152 | __u64 __reserved_3; |
| 153 | }; |
| 154 | |
| 155 | /* |
| 156 | * Ioctls that can be done on a perf counter fd: |
| 157 | */ |
| 158 | #define PERF_COUNTER_IOC_ENABLE _IO('$', 0) |
| 159 | #define PERF_COUNTER_IOC_DISABLE _IO('$', 1) |
| 160 | |
| 161 | asmlinkage int sys_perf_counter_open( |
| 162 | |
| 163 | struct perf_counter_hw_event *hw_event_uptr __user, |
| 164 | pid_t pid, |
| 165 | int cpu, |
| 166 | int group_fd, |
| 167 | unsigned long flags) |
| 168 | { |
| 169 | int ret; |
| 170 | |
| 171 | ret = syscall( |
| 172 | __NR_perf_counter_open, hw_event_uptr, pid, cpu, group_fd, flags); |
| 173 | #if defined(__x86_64__) || defined(__i386__) |
| 174 | if (ret < 0 && ret > -4096) { |
| 175 | errno = -ret; |
| 176 | ret = -1; |
| 177 | } |
| 178 | #endif |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static char *hw_event_names [] = { |
| 184 | "CPU cycles", |
| 185 | "instructions", |
| 186 | "cache references", |
| 187 | "cache misses", |
| 188 | "branches", |
| 189 | "branch misses", |
| 190 | "bus cycles", |
| 191 | }; |
| 192 | |
| 193 | static char *sw_event_names [] = { |
| 194 | "cpu clock ticks", |
| 195 | "task clock ticks", |
| 196 | "pagefaults", |
| 197 | "context switches", |
| 198 | "CPU migrations", |
| 199 | }; |
| 200 | |
| 201 | struct event_symbol { |
| 202 | int event; |
| 203 | char *symbol; |
| 204 | }; |
| 205 | |
| 206 | static struct event_symbol event_symbols [] = { |
| 207 | {PERF_COUNT_CPU_CYCLES, "cpu-cycles", }, |
| 208 | {PERF_COUNT_CPU_CYCLES, "cycles", }, |
| 209 | {PERF_COUNT_INSTRUCTIONS, "instructions", }, |
| 210 | {PERF_COUNT_CACHE_REFERENCES, "cache-references", }, |
| 211 | {PERF_COUNT_CACHE_MISSES, "cache-misses", }, |
| 212 | {PERF_COUNT_BRANCH_INSTRUCTIONS, "branch-instructions", }, |
| 213 | {PERF_COUNT_BRANCH_INSTRUCTIONS, "branches", }, |
| 214 | {PERF_COUNT_BRANCH_MISSES, "branch-misses", }, |
| 215 | {PERF_COUNT_BUS_CYCLES, "bus-cycles", }, |
| 216 | {PERF_COUNT_CPU_CLOCK, "cpu-ticks", }, |
| 217 | {PERF_COUNT_CPU_CLOCK, "ticks", }, |
| 218 | {PERF_COUNT_TASK_CLOCK, "task-ticks", }, |
| 219 | {PERF_COUNT_PAGE_FAULTS, "page-faults", }, |
| 220 | {PERF_COUNT_PAGE_FAULTS, "faults", }, |
| 221 | {PERF_COUNT_CONTEXT_SWITCHES, "context-switches", }, |
| 222 | {PERF_COUNT_CONTEXT_SWITCHES, "cs", }, |
| 223 | {PERF_COUNT_CPU_MIGRATIONS, "cpu-migrations", }, |
| 224 | {PERF_COUNT_CPU_MIGRATIONS, "migrations", }, |
| 225 | }; |
| 226 | |
| 227 | #define MAX_COUNTERS 64 |
| 228 | #define MAX_NR_CPUS 256 |
| 229 | |
| 230 | static int nr_counters = 0; |
| 231 | static int nr_cpus = 0; |
| 232 | |
| 233 | static int event_id[MAX_COUNTERS] = |
| 234 | { -2, -5, -4, -3, 0, 1, 2, 3}; |
| 235 | |
| 236 | static int event_raw[MAX_COUNTERS]; |
| 237 | |
| 238 | static int system_wide = 0; |
| 239 | |
| 240 | static void display_help(void) |
| 241 | { |
| 242 | unsigned int i; |
| 243 | int e; |
| 244 | |
| 245 | printf( |
| 246 | "Usage: perfstat [<events...>] <cmd...>\n\n" |
| 247 | "PerfStat Options (up to %d event types can be specified):\n\n", |
| 248 | MAX_COUNTERS); |
| 249 | printf( |
| 250 | " -e EVENT --event=EVENT # symbolic-name abbreviations"); |
| 251 | |
| 252 | for (i = 0, e = PERF_HW_EVENTS_MAX; i < ARRAY_SIZE(event_symbols); i++) { |
| 253 | if (e != event_symbols[i].event) { |
| 254 | e = event_symbols[i].event; |
| 255 | printf( |
| 256 | "\n %2d: %-20s", e, event_symbols[i].symbol); |
| 257 | } else |
| 258 | printf(" %s", event_symbols[i].symbol); |
| 259 | } |
| 260 | |
| 261 | printf("\n" |
| 262 | " rNNN: raw event type\n\n" |
| 263 | " -s # system-wide collection\n\n" |
| 264 | " -c <cmd..> --command=<cmd..> # command+arguments to be timed.\n" |
| 265 | "\n"); |
| 266 | exit(0); |
| 267 | } |
| 268 | |
| 269 | static int type_valid(int type) |
| 270 | { |
| 271 | if (type >= PERF_HW_EVENTS_MAX) |
| 272 | return 0; |
| 273 | if (type <= PERF_SW_EVENTS_MIN) |
| 274 | return 0; |
| 275 | |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | static char *event_name(int ctr) |
| 280 | { |
| 281 | int type = event_id[ctr]; |
| 282 | static char buf[32]; |
| 283 | |
| 284 | if (event_raw[ctr]) { |
| 285 | sprintf(buf, "raw 0x%x", type); |
| 286 | return buf; |
| 287 | } |
| 288 | if (!type_valid(type)) |
| 289 | return "unknown"; |
| 290 | |
| 291 | if (type >= 0) |
| 292 | return hw_event_names[type]; |
| 293 | |
| 294 | return sw_event_names[-type-1]; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Each event can have multiple symbolic names. |
| 299 | * Symbolic names are (almost) exactly matched. |
| 300 | */ |
| 301 | static int match_event_symbols(char *str) |
| 302 | { |
| 303 | unsigned int i; |
| 304 | |
| 305 | if (isdigit(str[0]) || str[0] == '-') |
| 306 | return atoi(str); |
| 307 | |
| 308 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { |
| 309 | if (!strncmp(str, event_symbols[i].symbol, |
| 310 | strlen(event_symbols[i].symbol))) |
| 311 | return event_symbols[i].event; |
| 312 | } |
| 313 | |
| 314 | return PERF_HW_EVENTS_MAX; |
| 315 | } |
| 316 | |
| 317 | static void parse_events(char *str) |
| 318 | { |
| 319 | int type, raw; |
| 320 | |
| 321 | again: |
| 322 | nr_counters++; |
| 323 | if (nr_counters == MAX_COUNTERS) |
| 324 | display_help(); |
| 325 | |
| 326 | raw = 0; |
| 327 | if (*str == 'r') { |
| 328 | raw = 1; |
| 329 | ++str; |
| 330 | type = strtol(str, NULL, 16); |
| 331 | } else { |
| 332 | type = match_event_symbols(str); |
| 333 | if (!type_valid(type)) |
| 334 | display_help(); |
| 335 | } |
| 336 | |
| 337 | event_id[nr_counters] = type; |
| 338 | event_raw[nr_counters] = raw; |
| 339 | |
| 340 | str = strstr(str, ","); |
| 341 | if (str) { |
| 342 | str++; |
| 343 | goto again; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | static void process_options(int argc, char *argv[]) |
| 348 | { |
| 349 | for (;;) { |
| 350 | int option_index = 0; |
| 351 | /** Options for getopt */ |
| 352 | static struct option long_options[] = { |
| 353 | {"event", required_argument, NULL, 'e'}, |
| 354 | {"help", no_argument, NULL, 'h'}, |
| 355 | {"command", no_argument, NULL, 'c'}, |
| 356 | {NULL, 0, NULL, 0 } |
| 357 | }; |
| 358 | int c = getopt_long(argc, argv, "+:e:c:s", |
| 359 | long_options, &option_index); |
| 360 | if (c == -1) |
| 361 | break; |
| 362 | |
| 363 | switch (c) { |
| 364 | case 'c': |
| 365 | break; |
| 366 | case 's': |
| 367 | system_wide = 1; |
| 368 | break; |
| 369 | case 'e': |
| 370 | parse_events(optarg); |
| 371 | break; |
| 372 | default: |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | if (optind == argc) |
| 377 | goto err; |
| 378 | |
| 379 | if (!nr_counters) |
| 380 | nr_counters = 8; |
| 381 | else |
| 382 | nr_counters++; |
| 383 | return; |
| 384 | |
| 385 | err: |
| 386 | display_help(); |
| 387 | } |
| 388 | |
| 389 | char fault_here[1000000]; |
| 390 | |
| 391 | #define PR_TASK_PERF_COUNTERS_DISABLE 31 |
| 392 | #define PR_TASK_PERF_COUNTERS_ENABLE 32 |
| 393 | |
| 394 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 395 | |
| 396 | static void create_counter(int counter) |
| 397 | { |
| 398 | struct perf_counter_hw_event hw_event; |
| 399 | |
| 400 | memset(&hw_event, 0, sizeof(hw_event)); |
| 401 | hw_event.type = event_id[counter]; |
| 402 | hw_event.raw = event_raw[counter]; |
| 403 | hw_event.record_type = PERF_RECORD_SIMPLE; |
| 404 | hw_event.nmi = 0; |
| 405 | |
| 406 | if (system_wide) { |
| 407 | int cpu; |
| 408 | for (cpu = 0; cpu < nr_cpus; cpu ++) { |
| 409 | fd[cpu][counter] = sys_perf_counter_open(&hw_event, -1, cpu, -1, 0); |
| 410 | if (fd[cpu][counter] < 0) { |
| 411 | printf("perfstat error: syscall returned with %d (%s)\n", |
| 412 | fd[cpu][counter], strerror(errno)); |
| 413 | exit(-1); |
| 414 | } |
| 415 | |
| 416 | } |
| 417 | } else { |
| 418 | hw_event.inherit = 1; |
| 419 | hw_event.disabled = 1; |
| 420 | |
| 421 | fd[0][counter] = sys_perf_counter_open(&hw_event, 0, -1, -1, 0); |
| 422 | if (fd[0][counter] < 0) { |
| 423 | printf("perfstat error: syscall returned with %d (%s)\n", |
| 424 | fd[0][counter], strerror(errno)); |
| 425 | exit(-1); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | |
| 431 | #define rdclock() \ |
| 432 | ({ \ |
| 433 | struct timespec ts; \ |
| 434 | \ |
| 435 | clock_gettime(CLOCK_MONOTONIC, &ts); \ |
| 436 | ts.tv_sec * 1000000000ULL + ts.tv_nsec; \ |
| 437 | }) |
| 438 | |
| 439 | int main(int argc, char *argv[]) |
| 440 | { |
| 441 | unsigned long long t0, t1; |
| 442 | int counter; |
| 443 | ssize_t res; |
| 444 | int status; |
| 445 | int pid; |
| 446 | |
| 447 | process_options(argc, argv); |
| 448 | |
| 449 | if (system_wide) { |
| 450 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 451 | assert(nr_cpus <= MAX_NR_CPUS); |
| 452 | assert(nr_cpus >= 0); |
| 453 | } else |
| 454 | nr_cpus = 1; |
| 455 | |
| 456 | for (counter = 0; counter < nr_counters; counter++) |
| 457 | create_counter(counter); |
| 458 | |
| 459 | argc -= optind; |
| 460 | argv += optind; |
| 461 | |
| 462 | /* |
| 463 | * Enable counters and exec the command: |
| 464 | */ |
| 465 | t0 = rdclock(); |
| 466 | prctl(PR_TASK_PERF_COUNTERS_ENABLE); |
| 467 | |
| 468 | if ((pid = fork()) < 0) |
| 469 | perror("failed to fork"); |
| 470 | if (!pid) { |
| 471 | if (execvp(argv[0], argv)) { |
| 472 | perror(argv[0]); |
| 473 | exit(-1); |
| 474 | } |
| 475 | } |
| 476 | while (wait(&status) >= 0) |
| 477 | ; |
| 478 | prctl(PR_TASK_PERF_COUNTERS_DISABLE); |
| 479 | t1 = rdclock(); |
| 480 | |
| 481 | fflush(stdout); |
| 482 | |
| 483 | fprintf(stderr, "\n"); |
| 484 | fprintf(stderr, " Performance counter stats for \'%s\':\n", |
| 485 | argv[0]); |
| 486 | fprintf(stderr, "\n"); |
| 487 | |
| 488 | for (counter = 0; counter < nr_counters; counter++) { |
| 489 | int cpu; |
| 490 | __u64 count, single_count; |
| 491 | |
| 492 | count = 0; |
| 493 | for (cpu = 0; cpu < nr_cpus; cpu ++) { |
| 494 | res = read(fd[cpu][counter], |
| 495 | (char *) &single_count, sizeof(single_count)); |
| 496 | assert(res == sizeof(single_count)); |
| 497 | count += single_count; |
| 498 | } |
| 499 | |
| 500 | if (!event_raw[counter] && |
| 501 | (event_id[counter] == PERF_COUNT_CPU_CLOCK || |
| 502 | event_id[counter] == PERF_COUNT_TASK_CLOCK)) { |
| 503 | |
| 504 | double msecs = (double)count / 1000000; |
| 505 | |
| 506 | fprintf(stderr, " %14.6f %-20s (msecs)\n", |
| 507 | msecs, event_name(counter)); |
| 508 | } else { |
| 509 | fprintf(stderr, " %14Ld %-20s (events)\n", |
| 510 | count, event_name(counter)); |
| 511 | } |
| 512 | if (!counter) |
| 513 | fprintf(stderr, "\n"); |
| 514 | } |
| 515 | fprintf(stderr, "\n"); |
| 516 | fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n", |
| 517 | (double)(t1-t0)/1e6); |
| 518 | fprintf(stderr, "\n"); |
| 519 | |
| 520 | return 0; |
| 521 | } |