Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 1 | |
| 2 | |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 3 | #include "util/util.h" |
| 4 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/time.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdint.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <limits.h> |
| 13 | #include <getopt.h> |
| 14 | #include <assert.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <errno.h> |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 18 | #include <time.h> |
| 19 | #include <sched.h> |
| 20 | #include <pthread.h> |
| 21 | |
| 22 | #include <sys/syscall.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/poll.h> |
| 25 | #include <sys/prctl.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <sys/uio.h> |
| 28 | #include <sys/mman.h> |
| 29 | |
| 30 | #include <linux/unistd.h> |
| 31 | #include <linux/types.h> |
| 32 | |
| 33 | #include "../../include/linux/perf_counter.h" |
| 34 | |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 35 | #include "perf.h" |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 36 | |
| 37 | static int nr_counters = 0; |
| 38 | static __u64 event_id[MAX_COUNTERS] = { }; |
| 39 | static int default_interval = 100000; |
| 40 | static int event_count[MAX_COUNTERS]; |
| 41 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 42 | static int nr_cpus = 0; |
| 43 | static unsigned int page_size; |
| 44 | static unsigned int mmap_pages = 16; |
| 45 | static int output; |
| 46 | static char *output_name = "output.perf"; |
| 47 | static int group = 0; |
| 48 | static unsigned int realtime_prio = 0; |
| 49 | |
| 50 | const unsigned int default_count[] = { |
| 51 | 1000000, |
| 52 | 1000000, |
| 53 | 10000, |
| 54 | 10000, |
| 55 | 1000000, |
| 56 | 10000, |
| 57 | }; |
| 58 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 59 | struct event_symbol { |
| 60 | __u64 event; |
| 61 | char *symbol; |
| 62 | }; |
| 63 | |
| 64 | static struct event_symbol event_symbols[] = { |
| 65 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cpu-cycles", }, |
| 66 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cycles", }, |
| 67 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS), "instructions", }, |
| 68 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES), "cache-references", }, |
| 69 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES), "cache-misses", }, |
| 70 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branch-instructions", }, |
| 71 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branches", }, |
| 72 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_MISSES), "branch-misses", }, |
| 73 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BUS_CYCLES), "bus-cycles", }, |
| 74 | |
| 75 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK), "cpu-clock", }, |
| 76 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK), "task-clock", }, |
| 77 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "page-faults", }, |
| 78 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "faults", }, |
| 79 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MIN), "minor-faults", }, |
| 80 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MAJ), "major-faults", }, |
| 81 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "context-switches", }, |
| 82 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "cs", }, |
| 83 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "cpu-migrations", }, |
| 84 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "migrations", }, |
| 85 | }; |
| 86 | |
| 87 | /* |
| 88 | * Each event can have multiple symbolic names. |
| 89 | * Symbolic names are (almost) exactly matched. |
| 90 | */ |
| 91 | static __u64 match_event_symbols(char *str) |
| 92 | { |
| 93 | __u64 config, id; |
| 94 | int type; |
| 95 | unsigned int i; |
| 96 | |
| 97 | if (sscanf(str, "r%llx", &config) == 1) |
| 98 | return config | PERF_COUNTER_RAW_MASK; |
| 99 | |
| 100 | if (sscanf(str, "%d:%llu", &type, &id) == 2) |
| 101 | return EID(type, id); |
| 102 | |
| 103 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { |
| 104 | if (!strncmp(str, event_symbols[i].symbol, |
| 105 | strlen(event_symbols[i].symbol))) |
| 106 | return event_symbols[i].event; |
| 107 | } |
| 108 | |
| 109 | return ~0ULL; |
| 110 | } |
| 111 | |
| 112 | static int parse_events(char *str) |
| 113 | { |
| 114 | __u64 config; |
| 115 | |
| 116 | again: |
| 117 | if (nr_counters == MAX_COUNTERS) |
| 118 | return -1; |
| 119 | |
| 120 | config = match_event_symbols(str); |
| 121 | if (config == ~0ULL) |
| 122 | return -1; |
| 123 | |
| 124 | event_id[nr_counters] = config; |
| 125 | nr_counters++; |
| 126 | |
| 127 | str = strstr(str, ","); |
| 128 | if (str) { |
| 129 | str++; |
| 130 | goto again; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | #define __PERF_COUNTER_FIELD(config, name) \ |
| 137 | ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT) |
| 138 | |
| 139 | #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW) |
| 140 | #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG) |
| 141 | #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE) |
| 142 | #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT) |
| 143 | |
| 144 | static void display_events_help(void) |
| 145 | { |
| 146 | unsigned int i; |
| 147 | __u64 e; |
| 148 | |
| 149 | printf( |
| 150 | " -e EVENT --event=EVENT # symbolic-name abbreviations"); |
| 151 | |
| 152 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { |
| 153 | int type, id; |
| 154 | |
| 155 | e = event_symbols[i].event; |
| 156 | type = PERF_COUNTER_TYPE(e); |
| 157 | id = PERF_COUNTER_ID(e); |
| 158 | |
| 159 | printf("\n %d:%d: %-20s", |
| 160 | type, id, event_symbols[i].symbol); |
| 161 | } |
| 162 | |
| 163 | printf("\n" |
| 164 | " rNNN: raw PMU events (eventsel+umask)\n\n"); |
| 165 | } |
| 166 | |
| 167 | static void display_help(void) |
| 168 | { |
| 169 | printf( |
| 170 | "Usage: perf-record [<options>]\n" |
| 171 | "perf-record Options (up to %d event types can be specified at once):\n\n", |
| 172 | MAX_COUNTERS); |
| 173 | |
| 174 | display_events_help(); |
| 175 | |
| 176 | printf( |
| 177 | " -c CNT --count=CNT # event period to sample\n" |
| 178 | " -m pages --mmap_pages=<pages> # number of mmap data pages\n" |
| 179 | " -o file --output=<file> # output file\n" |
| 180 | " -r prio --realtime=<prio> # use RT prio\n" |
| 181 | ); |
| 182 | |
| 183 | exit(0); |
| 184 | } |
| 185 | |
| 186 | static void process_options(int argc, char *argv[]) |
| 187 | { |
| 188 | int error = 0, counter; |
| 189 | |
| 190 | for (;;) { |
| 191 | int option_index = 0; |
| 192 | /** Options for getopt */ |
| 193 | static struct option long_options[] = { |
| 194 | {"count", required_argument, NULL, 'c'}, |
| 195 | {"event", required_argument, NULL, 'e'}, |
| 196 | {"mmap_pages", required_argument, NULL, 'm'}, |
| 197 | {"output", required_argument, NULL, 'o'}, |
| 198 | {"realtime", required_argument, NULL, 'r'}, |
| 199 | {NULL, 0, NULL, 0 } |
| 200 | }; |
| 201 | int c = getopt_long(argc, argv, "+:c:e:m:o:r:", |
| 202 | long_options, &option_index); |
| 203 | if (c == -1) |
| 204 | break; |
| 205 | |
| 206 | switch (c) { |
| 207 | case 'c': default_interval = atoi(optarg); break; |
| 208 | case 'e': error = parse_events(optarg); break; |
| 209 | case 'm': mmap_pages = atoi(optarg); break; |
| 210 | case 'o': output_name = strdup(optarg); break; |
| 211 | case 'r': realtime_prio = atoi(optarg); break; |
| 212 | default: error = 1; break; |
| 213 | } |
| 214 | } |
| 215 | if (error) |
| 216 | display_help(); |
| 217 | |
| 218 | if (!nr_counters) { |
| 219 | nr_counters = 1; |
| 220 | event_id[0] = 0; |
| 221 | } |
| 222 | |
| 223 | for (counter = 0; counter < nr_counters; counter++) { |
| 224 | if (event_count[counter]) |
| 225 | continue; |
| 226 | |
| 227 | event_count[counter] = default_interval; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | struct mmap_data { |
| 232 | int counter; |
| 233 | void *base; |
| 234 | unsigned int mask; |
| 235 | unsigned int prev; |
| 236 | }; |
| 237 | |
| 238 | static unsigned int mmap_read_head(struct mmap_data *md) |
| 239 | { |
| 240 | struct perf_counter_mmap_page *pc = md->base; |
| 241 | int head; |
| 242 | |
| 243 | head = pc->data_head; |
| 244 | rmb(); |
| 245 | |
| 246 | return head; |
| 247 | } |
| 248 | |
| 249 | static long events; |
| 250 | static struct timeval last_read, this_read; |
| 251 | |
| 252 | static void mmap_read(struct mmap_data *md) |
| 253 | { |
| 254 | unsigned int head = mmap_read_head(md); |
| 255 | unsigned int old = md->prev; |
| 256 | unsigned char *data = md->base + page_size; |
| 257 | unsigned long size; |
| 258 | void *buf; |
| 259 | int diff; |
| 260 | |
| 261 | gettimeofday(&this_read, NULL); |
| 262 | |
| 263 | /* |
| 264 | * If we're further behind than half the buffer, there's a chance |
| 265 | * the writer will bite our tail and screw up the events under us. |
| 266 | * |
| 267 | * If we somehow ended up ahead of the head, we got messed up. |
| 268 | * |
| 269 | * In either case, truncate and restart at head. |
| 270 | */ |
| 271 | diff = head - old; |
| 272 | if (diff > md->mask / 2 || diff < 0) { |
| 273 | struct timeval iv; |
| 274 | unsigned long msecs; |
| 275 | |
| 276 | timersub(&this_read, &last_read, &iv); |
| 277 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; |
| 278 | |
| 279 | fprintf(stderr, "WARNING: failed to keep up with mmap data." |
| 280 | " Last read %lu msecs ago.\n", msecs); |
| 281 | |
| 282 | /* |
| 283 | * head points to a known good entry, start there. |
| 284 | */ |
| 285 | old = head; |
| 286 | } |
| 287 | |
| 288 | last_read = this_read; |
| 289 | |
| 290 | if (old != head) |
| 291 | events++; |
| 292 | |
| 293 | size = head - old; |
| 294 | |
| 295 | if ((old & md->mask) + size != (head & md->mask)) { |
| 296 | buf = &data[old & md->mask]; |
| 297 | size = md->mask + 1 - (old & md->mask); |
| 298 | old += size; |
| 299 | while (size) { |
| 300 | int ret = write(output, buf, size); |
| 301 | if (ret < 0) { |
| 302 | perror("failed to write"); |
| 303 | exit(-1); |
| 304 | } |
| 305 | size -= ret; |
| 306 | buf += ret; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | buf = &data[old & md->mask]; |
| 311 | size = head - old; |
| 312 | old += size; |
| 313 | while (size) { |
| 314 | int ret = write(output, buf, size); |
| 315 | if (ret < 0) { |
| 316 | perror("failed to write"); |
| 317 | exit(-1); |
| 318 | } |
| 319 | size -= ret; |
| 320 | buf += ret; |
| 321 | } |
| 322 | |
| 323 | md->prev = old; |
| 324 | } |
| 325 | |
| 326 | static volatile int done = 0; |
| 327 | |
| 328 | static void sigchld_handler(int sig) |
| 329 | { |
| 330 | if (sig == SIGCHLD) |
| 331 | done = 1; |
| 332 | } |
| 333 | |
Thomas Gleixner | 7bd5469 | 2009-05-01 18:42:47 +0200 | [diff] [blame] | 334 | int cmd_record(int argc, char **argv) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 335 | { |
| 336 | struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; |
| 337 | struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; |
| 338 | struct perf_counter_hw_event hw_event; |
| 339 | int i, counter, group_fd, nr_poll = 0; |
| 340 | pid_t pid; |
| 341 | int ret; |
| 342 | |
| 343 | page_size = sysconf(_SC_PAGE_SIZE); |
| 344 | |
| 345 | process_options(argc, argv); |
| 346 | |
| 347 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 348 | assert(nr_cpus <= MAX_NR_CPUS); |
| 349 | assert(nr_cpus >= 0); |
| 350 | |
| 351 | output = open(output_name, O_CREAT|O_RDWR, S_IRWXU); |
| 352 | if (output < 0) { |
| 353 | perror("failed to create output file"); |
| 354 | exit(-1); |
| 355 | } |
| 356 | |
| 357 | argc -= optind; |
| 358 | argv += optind; |
| 359 | |
| 360 | for (i = 0; i < nr_cpus; i++) { |
| 361 | group_fd = -1; |
| 362 | for (counter = 0; counter < nr_counters; counter++) { |
| 363 | |
| 364 | memset(&hw_event, 0, sizeof(hw_event)); |
| 365 | hw_event.config = event_id[counter]; |
| 366 | hw_event.irq_period = event_count[counter]; |
| 367 | hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID; |
| 368 | hw_event.nmi = 1; |
| 369 | hw_event.mmap = 1; |
| 370 | hw_event.comm = 1; |
| 371 | |
| 372 | fd[i][counter] = sys_perf_counter_open(&hw_event, -1, i, group_fd, 0); |
| 373 | if (fd[i][counter] < 0) { |
| 374 | int err = errno; |
| 375 | printf("kerneltop error: syscall returned with %d (%s)\n", |
| 376 | fd[i][counter], strerror(err)); |
| 377 | if (err == EPERM) |
| 378 | printf("Are you root?\n"); |
| 379 | exit(-1); |
| 380 | } |
| 381 | assert(fd[i][counter] >= 0); |
| 382 | fcntl(fd[i][counter], F_SETFL, O_NONBLOCK); |
| 383 | |
| 384 | /* |
| 385 | * First counter acts as the group leader: |
| 386 | */ |
| 387 | if (group && group_fd == -1) |
| 388 | group_fd = fd[i][counter]; |
| 389 | |
| 390 | event_array[nr_poll].fd = fd[i][counter]; |
| 391 | event_array[nr_poll].events = POLLIN; |
| 392 | nr_poll++; |
| 393 | |
| 394 | mmap_array[i][counter].counter = counter; |
| 395 | mmap_array[i][counter].prev = 0; |
| 396 | mmap_array[i][counter].mask = mmap_pages*page_size - 1; |
| 397 | mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size, |
| 398 | PROT_READ, MAP_SHARED, fd[i][counter], 0); |
| 399 | if (mmap_array[i][counter].base == MAP_FAILED) { |
| 400 | printf("kerneltop error: failed to mmap with %d (%s)\n", |
| 401 | errno, strerror(errno)); |
| 402 | exit(-1); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | signal(SIGCHLD, sigchld_handler); |
| 408 | |
| 409 | pid = fork(); |
| 410 | if (pid < 0) |
| 411 | perror("failed to fork"); |
| 412 | |
| 413 | if (!pid) { |
| 414 | if (execvp(argv[0], argv)) { |
| 415 | perror(argv[0]); |
| 416 | exit(-1); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (realtime_prio) { |
| 421 | struct sched_param param; |
| 422 | |
| 423 | param.sched_priority = realtime_prio; |
| 424 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 425 | printf("Could not set realtime priority.\n"); |
| 426 | exit(-1); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * TODO: store the current /proc/$/maps information somewhere |
| 432 | */ |
| 433 | |
| 434 | while (!done) { |
| 435 | int hits = events; |
| 436 | |
| 437 | for (i = 0; i < nr_cpus; i++) { |
| 438 | for (counter = 0; counter < nr_counters; counter++) |
| 439 | mmap_read(&mmap_array[i][counter]); |
| 440 | } |
| 441 | |
| 442 | if (hits == events) |
| 443 | ret = poll(event_array, nr_poll, 100); |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |