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 | |
Wu Fengguang | cea92ce | 2009-03-20 10:08:02 +0800 | [diff] [blame^] | 55 | #include "perfcounters.h" |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 56 | |
| 57 | static char *hw_event_names [] = { |
| 58 | "CPU cycles", |
| 59 | "instructions", |
| 60 | "cache references", |
| 61 | "cache misses", |
| 62 | "branches", |
| 63 | "branch misses", |
| 64 | "bus cycles", |
| 65 | }; |
| 66 | |
| 67 | static char *sw_event_names [] = { |
| 68 | "cpu clock ticks", |
| 69 | "task clock ticks", |
| 70 | "pagefaults", |
| 71 | "context switches", |
| 72 | "CPU migrations", |
| 73 | }; |
| 74 | |
| 75 | struct event_symbol { |
| 76 | int event; |
| 77 | char *symbol; |
| 78 | }; |
| 79 | |
| 80 | static struct event_symbol event_symbols [] = { |
| 81 | {PERF_COUNT_CPU_CYCLES, "cpu-cycles", }, |
| 82 | {PERF_COUNT_CPU_CYCLES, "cycles", }, |
| 83 | {PERF_COUNT_INSTRUCTIONS, "instructions", }, |
| 84 | {PERF_COUNT_CACHE_REFERENCES, "cache-references", }, |
| 85 | {PERF_COUNT_CACHE_MISSES, "cache-misses", }, |
| 86 | {PERF_COUNT_BRANCH_INSTRUCTIONS, "branch-instructions", }, |
| 87 | {PERF_COUNT_BRANCH_INSTRUCTIONS, "branches", }, |
| 88 | {PERF_COUNT_BRANCH_MISSES, "branch-misses", }, |
| 89 | {PERF_COUNT_BUS_CYCLES, "bus-cycles", }, |
| 90 | {PERF_COUNT_CPU_CLOCK, "cpu-ticks", }, |
| 91 | {PERF_COUNT_CPU_CLOCK, "ticks", }, |
| 92 | {PERF_COUNT_TASK_CLOCK, "task-ticks", }, |
| 93 | {PERF_COUNT_PAGE_FAULTS, "page-faults", }, |
| 94 | {PERF_COUNT_PAGE_FAULTS, "faults", }, |
| 95 | {PERF_COUNT_CONTEXT_SWITCHES, "context-switches", }, |
| 96 | {PERF_COUNT_CONTEXT_SWITCHES, "cs", }, |
| 97 | {PERF_COUNT_CPU_MIGRATIONS, "cpu-migrations", }, |
| 98 | {PERF_COUNT_CPU_MIGRATIONS, "migrations", }, |
| 99 | }; |
| 100 | |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 101 | static int nr_counters = 0; |
| 102 | static int nr_cpus = 0; |
| 103 | |
| 104 | static int event_id[MAX_COUNTERS] = |
| 105 | { -2, -5, -4, -3, 0, 1, 2, 3}; |
| 106 | |
| 107 | static int event_raw[MAX_COUNTERS]; |
| 108 | |
| 109 | static int system_wide = 0; |
| 110 | |
| 111 | static void display_help(void) |
| 112 | { |
| 113 | unsigned int i; |
| 114 | int e; |
| 115 | |
| 116 | printf( |
| 117 | "Usage: perfstat [<events...>] <cmd...>\n\n" |
| 118 | "PerfStat Options (up to %d event types can be specified):\n\n", |
| 119 | MAX_COUNTERS); |
| 120 | printf( |
| 121 | " -e EVENT --event=EVENT # symbolic-name abbreviations"); |
| 122 | |
| 123 | for (i = 0, e = PERF_HW_EVENTS_MAX; i < ARRAY_SIZE(event_symbols); i++) { |
| 124 | if (e != event_symbols[i].event) { |
| 125 | e = event_symbols[i].event; |
| 126 | printf( |
| 127 | "\n %2d: %-20s", e, event_symbols[i].symbol); |
| 128 | } else |
| 129 | printf(" %s", event_symbols[i].symbol); |
| 130 | } |
| 131 | |
| 132 | printf("\n" |
| 133 | " rNNN: raw event type\n\n" |
| 134 | " -s # system-wide collection\n\n" |
| 135 | " -c <cmd..> --command=<cmd..> # command+arguments to be timed.\n" |
| 136 | "\n"); |
| 137 | exit(0); |
| 138 | } |
| 139 | |
| 140 | static int type_valid(int type) |
| 141 | { |
| 142 | if (type >= PERF_HW_EVENTS_MAX) |
| 143 | return 0; |
| 144 | if (type <= PERF_SW_EVENTS_MIN) |
| 145 | return 0; |
| 146 | |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | static char *event_name(int ctr) |
| 151 | { |
| 152 | int type = event_id[ctr]; |
| 153 | static char buf[32]; |
| 154 | |
| 155 | if (event_raw[ctr]) { |
| 156 | sprintf(buf, "raw 0x%x", type); |
| 157 | return buf; |
| 158 | } |
| 159 | if (!type_valid(type)) |
| 160 | return "unknown"; |
| 161 | |
| 162 | if (type >= 0) |
| 163 | return hw_event_names[type]; |
| 164 | |
| 165 | return sw_event_names[-type-1]; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Each event can have multiple symbolic names. |
| 170 | * Symbolic names are (almost) exactly matched. |
| 171 | */ |
| 172 | static int match_event_symbols(char *str) |
| 173 | { |
| 174 | unsigned int i; |
| 175 | |
| 176 | if (isdigit(str[0]) || str[0] == '-') |
| 177 | return atoi(str); |
| 178 | |
| 179 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { |
| 180 | if (!strncmp(str, event_symbols[i].symbol, |
| 181 | strlen(event_symbols[i].symbol))) |
| 182 | return event_symbols[i].event; |
| 183 | } |
| 184 | |
| 185 | return PERF_HW_EVENTS_MAX; |
| 186 | } |
| 187 | |
| 188 | static void parse_events(char *str) |
| 189 | { |
| 190 | int type, raw; |
| 191 | |
| 192 | again: |
| 193 | nr_counters++; |
| 194 | if (nr_counters == MAX_COUNTERS) |
| 195 | display_help(); |
| 196 | |
| 197 | raw = 0; |
| 198 | if (*str == 'r') { |
| 199 | raw = 1; |
| 200 | ++str; |
| 201 | type = strtol(str, NULL, 16); |
| 202 | } else { |
| 203 | type = match_event_symbols(str); |
| 204 | if (!type_valid(type)) |
| 205 | display_help(); |
| 206 | } |
| 207 | |
| 208 | event_id[nr_counters] = type; |
| 209 | event_raw[nr_counters] = raw; |
| 210 | |
| 211 | str = strstr(str, ","); |
| 212 | if (str) { |
| 213 | str++; |
| 214 | goto again; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void process_options(int argc, char *argv[]) |
| 219 | { |
| 220 | for (;;) { |
| 221 | int option_index = 0; |
| 222 | /** Options for getopt */ |
| 223 | static struct option long_options[] = { |
| 224 | {"event", required_argument, NULL, 'e'}, |
| 225 | {"help", no_argument, NULL, 'h'}, |
| 226 | {"command", no_argument, NULL, 'c'}, |
| 227 | {NULL, 0, NULL, 0 } |
| 228 | }; |
| 229 | int c = getopt_long(argc, argv, "+:e:c:s", |
| 230 | long_options, &option_index); |
| 231 | if (c == -1) |
| 232 | break; |
| 233 | |
| 234 | switch (c) { |
| 235 | case 'c': |
| 236 | break; |
| 237 | case 's': |
| 238 | system_wide = 1; |
| 239 | break; |
| 240 | case 'e': |
| 241 | parse_events(optarg); |
| 242 | break; |
| 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | if (optind == argc) |
| 248 | goto err; |
| 249 | |
| 250 | if (!nr_counters) |
| 251 | nr_counters = 8; |
| 252 | else |
| 253 | nr_counters++; |
| 254 | return; |
| 255 | |
| 256 | err: |
| 257 | display_help(); |
| 258 | } |
| 259 | |
| 260 | char fault_here[1000000]; |
| 261 | |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 262 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 263 | |
| 264 | static void create_counter(int counter) |
| 265 | { |
| 266 | struct perf_counter_hw_event hw_event; |
| 267 | |
| 268 | memset(&hw_event, 0, sizeof(hw_event)); |
| 269 | hw_event.type = event_id[counter]; |
| 270 | hw_event.raw = event_raw[counter]; |
| 271 | hw_event.record_type = PERF_RECORD_SIMPLE; |
| 272 | hw_event.nmi = 0; |
| 273 | |
| 274 | if (system_wide) { |
| 275 | int cpu; |
| 276 | for (cpu = 0; cpu < nr_cpus; cpu ++) { |
| 277 | fd[cpu][counter] = sys_perf_counter_open(&hw_event, -1, cpu, -1, 0); |
| 278 | if (fd[cpu][counter] < 0) { |
| 279 | printf("perfstat error: syscall returned with %d (%s)\n", |
| 280 | fd[cpu][counter], strerror(errno)); |
| 281 | exit(-1); |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | } else { |
| 286 | hw_event.inherit = 1; |
| 287 | hw_event.disabled = 1; |
| 288 | |
| 289 | fd[0][counter] = sys_perf_counter_open(&hw_event, 0, -1, -1, 0); |
| 290 | if (fd[0][counter] < 0) { |
| 291 | printf("perfstat error: syscall returned with %d (%s)\n", |
| 292 | fd[0][counter], strerror(errno)); |
| 293 | exit(-1); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | |
| 299 | #define rdclock() \ |
| 300 | ({ \ |
| 301 | struct timespec ts; \ |
| 302 | \ |
| 303 | clock_gettime(CLOCK_MONOTONIC, &ts); \ |
| 304 | ts.tv_sec * 1000000000ULL + ts.tv_nsec; \ |
| 305 | }) |
| 306 | |
| 307 | int main(int argc, char *argv[]) |
| 308 | { |
| 309 | unsigned long long t0, t1; |
| 310 | int counter; |
| 311 | ssize_t res; |
| 312 | int status; |
| 313 | int pid; |
| 314 | |
| 315 | process_options(argc, argv); |
| 316 | |
| 317 | if (system_wide) { |
| 318 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 319 | assert(nr_cpus <= MAX_NR_CPUS); |
| 320 | assert(nr_cpus >= 0); |
| 321 | } else |
| 322 | nr_cpus = 1; |
| 323 | |
| 324 | for (counter = 0; counter < nr_counters; counter++) |
| 325 | create_counter(counter); |
| 326 | |
| 327 | argc -= optind; |
| 328 | argv += optind; |
| 329 | |
| 330 | /* |
| 331 | * Enable counters and exec the command: |
| 332 | */ |
| 333 | t0 = rdclock(); |
| 334 | prctl(PR_TASK_PERF_COUNTERS_ENABLE); |
| 335 | |
| 336 | if ((pid = fork()) < 0) |
| 337 | perror("failed to fork"); |
| 338 | if (!pid) { |
| 339 | if (execvp(argv[0], argv)) { |
| 340 | perror(argv[0]); |
| 341 | exit(-1); |
| 342 | } |
| 343 | } |
| 344 | while (wait(&status) >= 0) |
| 345 | ; |
| 346 | prctl(PR_TASK_PERF_COUNTERS_DISABLE); |
| 347 | t1 = rdclock(); |
| 348 | |
| 349 | fflush(stdout); |
| 350 | |
| 351 | fprintf(stderr, "\n"); |
| 352 | fprintf(stderr, " Performance counter stats for \'%s\':\n", |
| 353 | argv[0]); |
| 354 | fprintf(stderr, "\n"); |
| 355 | |
| 356 | for (counter = 0; counter < nr_counters; counter++) { |
| 357 | int cpu; |
| 358 | __u64 count, single_count; |
| 359 | |
| 360 | count = 0; |
| 361 | for (cpu = 0; cpu < nr_cpus; cpu ++) { |
| 362 | res = read(fd[cpu][counter], |
| 363 | (char *) &single_count, sizeof(single_count)); |
| 364 | assert(res == sizeof(single_count)); |
| 365 | count += single_count; |
| 366 | } |
| 367 | |
| 368 | if (!event_raw[counter] && |
| 369 | (event_id[counter] == PERF_COUNT_CPU_CLOCK || |
| 370 | event_id[counter] == PERF_COUNT_TASK_CLOCK)) { |
| 371 | |
| 372 | double msecs = (double)count / 1000000; |
| 373 | |
| 374 | fprintf(stderr, " %14.6f %-20s (msecs)\n", |
| 375 | msecs, event_name(counter)); |
| 376 | } else { |
| 377 | fprintf(stderr, " %14Ld %-20s (events)\n", |
| 378 | count, event_name(counter)); |
| 379 | } |
| 380 | if (!counter) |
| 381 | fprintf(stderr, "\n"); |
| 382 | } |
| 383 | fprintf(stderr, "\n"); |
| 384 | fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n", |
| 385 | (double)(t1-t0)/1e6); |
| 386 | fprintf(stderr, "\n"); |
| 387 | |
| 388 | return 0; |
| 389 | } |