perf symbols: Factor out buildid reading routine
[deliverable/linux.git] / tools / perf / builtin-record.c
CommitLineData
abaff32a 1/*
bf9e1876
IM
2 * builtin-record.c
3 *
4 * Builtin record command: Record the profile of a workload
5 * (or a CPU, or a PID) into the perf.data output file - for
6 * later analysis via perf report.
abaff32a 7 */
16f762a2 8#include "builtin.h"
bf9e1876
IM
9
10#include "perf.h"
11
6eda5838 12#include "util/util.h"
0e9b20b8 13#include "util/parse-options.h"
8ad8db37 14#include "util/parse-events.h"
a0055ae2 15#include "util/string.h"
6eda5838 16
7c6a1c65 17#include "util/header.h"
66e274f3 18#include "util/event.h"
8f28827a 19#include "util/debug.h"
7c6a1c65 20
97124d5e 21#include <unistd.h>
de9ac07b 22#include <sched.h>
de9ac07b 23
de9ac07b 24static int fd[MAX_NR_CPUS][MAX_COUNTERS];
a21ca2ca 25
7e4ff9e3 26static long default_interval = 0;
a21ca2ca 27
42e59d7d 28static int nr_cpus = 0;
de9ac07b 29static unsigned int page_size;
42e59d7d
IM
30static unsigned int mmap_pages = 128;
31static int freq = 1000;
de9ac07b 32static int output;
23ac9cbe 33static const char *output_name = "perf.data";
42e59d7d
IM
34static int group = 0;
35static unsigned int realtime_prio = 0;
36static int raw_samples = 0;
37static int system_wide = 0;
38static int profile_cpu = -1;
39static pid_t target_pid = -1;
40static pid_t child_pid = -1;
41static int inherit = 1;
42static int force = 0;
43static int append_file = 0;
44static int call_graph = 0;
45static int inherit_stat = 0;
46static int no_samples = 0;
47static int sample_address = 0;
48static int multiplex = 0;
49static int multiplex_fd = -1;
50
51static long samples = 0;
a21ca2ca
IM
52static struct timeval last_read;
53static struct timeval this_read;
54
42e59d7d 55static u64 bytes_written = 0;
a21ca2ca
IM
56
57static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
58
42e59d7d
IM
59static int nr_poll = 0;
60static int nr_cpu = 0;
a21ca2ca 61
42e59d7d 62static int file_new = 1;
7c6a1c65 63
42e59d7d 64struct perf_header *header = NULL;
f5970550 65
de9ac07b 66struct mmap_data {
a21ca2ca
IM
67 int counter;
68 void *base;
69 unsigned int mask;
70 unsigned int prev;
de9ac07b
PZ
71};
72
a21ca2ca
IM
73static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
74
9d91a6f7 75static unsigned long mmap_read_head(struct mmap_data *md)
de9ac07b 76{
cdd6c482 77 struct perf_event_mmap_page *pc = md->base;
9d91a6f7 78 long head;
de9ac07b
PZ
79
80 head = pc->data_head;
81 rmb();
82
83 return head;
84}
85
9d91a6f7
PZ
86static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
87{
cdd6c482 88 struct perf_event_mmap_page *pc = md->base;
9d91a6f7
PZ
89
90 /*
91 * ensure all reads are done before we write the tail out.
92 */
93 /* mb(); */
94 pc->data_tail = tail;
95}
96
f5970550
PZ
97static void write_output(void *buf, size_t size)
98{
99 while (size) {
100 int ret = write(output, buf, size);
101
102 if (ret < 0)
103 die("failed to write");
104
105 size -= ret;
106 buf += ret;
107
108 bytes_written += ret;
109 }
110}
111
234fbbf5
ACM
112static int process_synthesized_event(event_t *event)
113{
114 write_output(event, event->header.size);
115 return 0;
116}
117
de9ac07b
PZ
118static void mmap_read(struct mmap_data *md)
119{
120 unsigned int head = mmap_read_head(md);
121 unsigned int old = md->prev;
122 unsigned char *data = md->base + page_size;
123 unsigned long size;
124 void *buf;
125 int diff;
126
127 gettimeofday(&this_read, NULL);
128
129 /*
130 * If we're further behind than half the buffer, there's a chance
2debbc83 131 * the writer will bite our tail and mess up the samples under us.
de9ac07b
PZ
132 *
133 * If we somehow ended up ahead of the head, we got messed up.
134 *
135 * In either case, truncate and restart at head.
136 */
137 diff = head - old;
9d91a6f7 138 if (diff < 0) {
de9ac07b
PZ
139 struct timeval iv;
140 unsigned long msecs;
141
142 timersub(&this_read, &last_read, &iv);
143 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
144
145 fprintf(stderr, "WARNING: failed to keep up with mmap data."
146 " Last read %lu msecs ago.\n", msecs);
147
148 /*
149 * head points to a known good entry, start there.
150 */
151 old = head;
152 }
153
154 last_read = this_read;
155
156 if (old != head)
2debbc83 157 samples++;
de9ac07b
PZ
158
159 size = head - old;
160
161 if ((old & md->mask) + size != (head & md->mask)) {
162 buf = &data[old & md->mask];
163 size = md->mask + 1 - (old & md->mask);
164 old += size;
021e9f47 165
f5970550 166 write_output(buf, size);
de9ac07b
PZ
167 }
168
169 buf = &data[old & md->mask];
170 size = head - old;
171 old += size;
021e9f47 172
f5970550 173 write_output(buf, size);
de9ac07b
PZ
174
175 md->prev = old;
9d91a6f7 176 mmap_write_tail(md, old);
de9ac07b
PZ
177}
178
179static volatile int done = 0;
f7b7c26e 180static volatile int signr = -1;
de9ac07b 181
16c8a109 182static void sig_handler(int sig)
de9ac07b 183{
16c8a109 184 done = 1;
f7b7c26e
PZ
185 signr = sig;
186}
187
188static void sig_atexit(void)
189{
933da83a
CW
190 if (child_pid != -1)
191 kill(child_pid, SIGTERM);
192
f7b7c26e
PZ
193 if (signr == -1)
194 return;
195
196 signal(signr, SIG_DFL);
197 kill(getpid(), signr);
de9ac07b
PZ
198}
199
f250c030
IM
200static int group_fd;
201
cdd6c482 202static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
7c6a1c65
PZ
203{
204 struct perf_header_attr *h_attr;
205
206 if (nr < header->attrs) {
207 h_attr = header->attr[nr];
208 } else {
209 h_attr = perf_header_attr__new(a);
210 perf_header__add_attr(header, h_attr);
211 }
212
213 return h_attr;
214}
215
f250c030 216static void create_counter(int counter, int cpu, pid_t pid)
de9ac07b 217{
c171b552 218 char *filter = filters[counter];
cdd6c482 219 struct perf_event_attr *attr = attrs + counter;
7c6a1c65
PZ
220 struct perf_header_attr *h_attr;
221 int track = !counter; /* only the first counter needs these */
c171b552 222 int ret;
7c6a1c65
PZ
223 struct {
224 u64 count;
225 u64 time_enabled;
226 u64 time_running;
227 u64 id;
228 } read_data;
229
230 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
231 PERF_FORMAT_TOTAL_TIME_RUNNING |
232 PERF_FORMAT_ID;
16c8a109 233
3a9f131f 234 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
3efa1cc9 235
1dba15e7 236 if (freq) {
ea1900e5 237 attr->sample_type |= PERF_SAMPLE_PERIOD;
a21ca2ca
IM
238 attr->freq = 1;
239 attr->sample_freq = freq;
1dba15e7 240 }
3efa1cc9 241
649c48a9
PZ
242 if (no_samples)
243 attr->sample_freq = 0;
244
245 if (inherit_stat)
246 attr->inherit_stat = 1;
247
4bba828d
AB
248 if (sample_address)
249 attr->sample_type |= PERF_SAMPLE_ADDR;
250
3efa1cc9
IM
251 if (call_graph)
252 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
253
cd6feeea 254 if (raw_samples) {
6ddf259d 255 attr->sample_type |= PERF_SAMPLE_TIME;
daac07b2 256 attr->sample_type |= PERF_SAMPLE_RAW;
cd6feeea
IM
257 attr->sample_type |= PERF_SAMPLE_CPU;
258 }
f413cdb8 259
a21ca2ca
IM
260 attr->mmap = track;
261 attr->comm = track;
262 attr->inherit = (cpu < 0) && inherit;
4502d77c 263 attr->disabled = 1;
16c8a109 264
3da297a6 265try_again:
cdd6c482 266 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
16c8a109 267
f250c030
IM
268 if (fd[nr_cpu][counter] < 0) {
269 int err = errno;
16c8a109 270
f250c030 271 if (err == EPERM)
3da297a6 272 die("Permission error - are you root?\n");
0a5ac846
JA
273 else if (err == ENODEV && profile_cpu != -1)
274 die("No such device - did you specify an out-of-range profile CPU?\n");
3da297a6
IM
275
276 /*
277 * If it's cycles then fall back to hrtimer
278 * based cpu-clock-tick sw counter, which
279 * is always available even if no PMU support:
280 */
281 if (attr->type == PERF_TYPE_HARDWARE
f4dbfa8f 282 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
3da297a6
IM
283
284 if (verbose)
285 warning(" ... trying to fall back to cpu-clock-ticks\n");
286 attr->type = PERF_TYPE_SOFTWARE;
f4dbfa8f 287 attr->config = PERF_COUNT_SW_CPU_CLOCK;
3da297a6
IM
288 goto try_again;
289 }
30c806a0
IM
290 printf("\n");
291 error("perfcounter syscall returned with %d (%s)\n",
292 fd[nr_cpu][counter], strerror(err));
cdd6c482 293 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
f250c030
IM
294 exit(-1);
295 }
3da297a6 296
7c6a1c65
PZ
297 h_attr = get_header_attr(attr, counter);
298
299 if (!file_new) {
300 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
301 fprintf(stderr, "incompatible append\n");
302 exit(-1);
303 }
304 }
305
3928ddbe
FW
306 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
307 perror("Unable to read perf file descriptor\n");
308 exit(-1);
309 }
7c6a1c65
PZ
310
311 perf_header_attr__add_id(h_attr, read_data.id);
312
f250c030
IM
313 assert(fd[nr_cpu][counter] >= 0);
314 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
16c8a109 315
f250c030
IM
316 /*
317 * First counter acts as the group leader:
318 */
319 if (group && group_fd == -1)
320 group_fd = fd[nr_cpu][counter];
ea57c4f5
IM
321 if (multiplex && multiplex_fd == -1)
322 multiplex_fd = fd[nr_cpu][counter];
f250c030 323
ea57c4f5 324 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
4502d77c 325
cdd6c482 326 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
ea57c4f5
IM
327 assert(ret != -1);
328 } else {
329 event_array[nr_poll].fd = fd[nr_cpu][counter];
330 event_array[nr_poll].events = POLLIN;
331 nr_poll++;
332
333 mmap_array[nr_cpu][counter].counter = counter;
334 mmap_array[nr_cpu][counter].prev = 0;
335 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
336 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
337 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
338 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
339 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
340 exit(-1);
341 }
342 }
d1302522 343
c171b552
LZ
344 if (filter != NULL) {
345 ret = ioctl(fd[nr_cpu][counter],
346 PERF_EVENT_IOC_SET_FILTER, filter);
347 if (ret) {
348 error("failed to set filter with %d (%s)\n", errno,
349 strerror(errno));
350 exit(-1);
351 }
352 }
353
cdd6c482 354 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
f250c030 355}
f2521b6e 356
f250c030
IM
357static void open_counters(int cpu, pid_t pid)
358{
359 int counter;
16c8a109 360
f250c030
IM
361 group_fd = -1;
362 for (counter = 0; counter < nr_counters; counter++)
363 create_counter(counter, cpu, pid);
364
16c8a109
PZ
365 nr_cpu++;
366}
367
f5970550
PZ
368static void atexit_header(void)
369{
7c6a1c65 370 header->data_size += bytes_written;
f5970550 371
7c6a1c65 372 perf_header__write(header, output);
f5970550
PZ
373}
374
0e9b20b8 375static int __cmd_record(int argc, const char **argv)
16c8a109
PZ
376{
377 int i, counter;
abaff32a 378 struct stat st;
7c6a1c65 379 pid_t pid = 0;
abaff32a 380 int flags;
de9ac07b 381 int ret;
8b412664 382 unsigned long waking = 0;
de9ac07b
PZ
383
384 page_size = sysconf(_SC_PAGE_SIZE);
de9ac07b
PZ
385 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
386 assert(nr_cpus <= MAX_NR_CPUS);
387 assert(nr_cpus >= 0);
388
f5970550
PZ
389 atexit(sig_atexit);
390 signal(SIGCHLD, sig_handler);
391 signal(SIGINT, sig_handler);
392
266e0e21
PH
393 if (!stat(output_name, &st) && st.st_size) {
394 if (!force && !append_file) {
395 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
396 output_name);
397 exit(-1);
398 }
399 } else {
400 append_file = 0;
97124d5e
PZ
401 }
402
abaff32a
IM
403 flags = O_CREAT|O_RDWR;
404 if (append_file)
f5970550 405 file_new = 0;
abaff32a
IM
406 else
407 flags |= O_TRUNC;
408
409 output = open(output_name, flags, S_IRUSR|S_IWUSR);
de9ac07b
PZ
410 if (output < 0) {
411 perror("failed to create output file");
412 exit(-1);
413 }
414
7c6a1c65
PZ
415 if (!file_new)
416 header = perf_header__read(output);
417 else
418 header = perf_header__new();
f5970550 419
9df37ddd 420 if (raw_samples) {
2ba08250 421 perf_header__feat_trace_info(header);
9df37ddd
FW
422 } else {
423 for (i = 0; i < nr_counters; i++) {
424 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
2ba08250 425 perf_header__feat_trace_info(header);
9df37ddd
FW
426 break;
427 }
428 }
429 }
03456a15 430
f5970550
PZ
431 atexit(atexit_header);
432
1a853e36 433 if (!system_wide) {
7c6a1c65
PZ
434 pid = target_pid;
435 if (pid == -1)
436 pid = getpid();
437
0a5ac846
JA
438 open_counters(profile_cpu, pid);
439 } else {
440 if (profile_cpu != -1) {
441 open_counters(profile_cpu, target_pid);
442 } else {
443 for (i = 0; i < nr_cpus; i++)
444 open_counters(i, target_pid);
445 }
446 }
de9ac07b 447
7c6a1c65
PZ
448 if (file_new)
449 perf_header__write(header, output);
450
234fbbf5
ACM
451 if (!system_wide)
452 event__synthesize_thread(pid, process_synthesized_event);
453 else
454 event__synthesize_threads(process_synthesized_event);
7c6a1c65 455
ef65b2a0 456 if (target_pid == -1 && argc) {
1a853e36
ACM
457 pid = fork();
458 if (pid < 0)
459 perror("failed to fork");
de9ac07b 460
1a853e36 461 if (!pid) {
0e9b20b8 462 if (execvp(argv[0], (char **)argv)) {
1a853e36
ACM
463 perror(argv[0]);
464 exit(-1);
465 }
de9ac07b 466 }
933da83a
CW
467
468 child_pid = pid;
de9ac07b
PZ
469 }
470
471 if (realtime_prio) {
472 struct sched_param param;
473
474 param.sched_priority = realtime_prio;
475 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
6beba7ad 476 pr_err("Could not set realtime priority.\n");
de9ac07b
PZ
477 exit(-1);
478 }
479 }
480
649c48a9 481 for (;;) {
2debbc83 482 int hits = samples;
de9ac07b 483
16c8a109 484 for (i = 0; i < nr_cpu; i++) {
ea57c4f5
IM
485 for (counter = 0; counter < nr_counters; counter++) {
486 if (mmap_array[i][counter].base)
487 mmap_read(&mmap_array[i][counter]);
488 }
de9ac07b
PZ
489 }
490
649c48a9
PZ
491 if (hits == samples) {
492 if (done)
493 break;
8b412664
PZ
494 ret = poll(event_array, nr_poll, -1);
495 waking++;
496 }
497
498 if (done) {
499 for (i = 0; i < nr_cpu; i++) {
500 for (counter = 0; counter < nr_counters; counter++)
cdd6c482 501 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
8b412664 502 }
649c48a9 503 }
de9ac07b
PZ
504 }
505
8b412664
PZ
506 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
507
021e9f47
IM
508 /*
509 * Approximate RIP event size: 24 bytes.
510 */
511 fprintf(stderr,
2debbc83 512 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
021e9f47
IM
513 (double)bytes_written / 1024.0 / 1024.0,
514 output_name,
515 bytes_written / 24);
addc2785 516
de9ac07b
PZ
517 return 0;
518}
0e9b20b8 519
0e9b20b8 520static const char * const record_usage[] = {
9e096753
MG
521 "perf record [<options>] [<command>]",
522 "perf record [<options>] -- <command> [<options>]",
0e9b20b8
IM
523 NULL
524};
525
5242519b 526static const struct option options[] = {
0e9b20b8 527 OPT_CALLBACK('e', "event", NULL, "event",
86847b62
TG
528 "event selector. use 'perf list' to list available events",
529 parse_events),
c171b552
LZ
530 OPT_CALLBACK(0, "filter", NULL, "filter",
531 "event filter", parse_filter),
0e9b20b8
IM
532 OPT_INTEGER('p', "pid", &target_pid,
533 "record events on existing pid"),
534 OPT_INTEGER('r', "realtime", &realtime_prio,
535 "collect data with this RT SCHED_FIFO priority"),
daac07b2
FW
536 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
537 "collect raw sample records from all opened counters"),
0e9b20b8
IM
538 OPT_BOOLEAN('a', "all-cpus", &system_wide,
539 "system-wide collection from all CPUs"),
abaff32a
IM
540 OPT_BOOLEAN('A', "append", &append_file,
541 "append to the output file to do incremental profiling"),
0a5ac846
JA
542 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
543 "CPU to profile on"),
97124d5e
PZ
544 OPT_BOOLEAN('f', "force", &force,
545 "overwrite existing data file"),
e61078a0 546 OPT_LONG('c', "count", &default_interval,
abaff32a
IM
547 "event period to sample"),
548 OPT_STRING('o', "output", &output_name, "file",
549 "output file name"),
550 OPT_BOOLEAN('i', "inherit", &inherit,
551 "child tasks inherit counters"),
cf1f4574
IM
552 OPT_INTEGER('F', "freq", &freq,
553 "profile at this frequency"),
abaff32a
IM
554 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
555 "number of mmap data pages"),
3efa1cc9
IM
556 OPT_BOOLEAN('g', "call-graph", &call_graph,
557 "do call-graph (stack chain/backtrace) recording"),
3da297a6
IM
558 OPT_BOOLEAN('v', "verbose", &verbose,
559 "be more verbose (show counter open errors, etc)"),
649c48a9
PZ
560 OPT_BOOLEAN('s', "stat", &inherit_stat,
561 "per thread counts"),
4bba828d
AB
562 OPT_BOOLEAN('d', "data", &sample_address,
563 "Sample addresses"),
649c48a9
PZ
564 OPT_BOOLEAN('n', "no-samples", &no_samples,
565 "don't sample"),
d1302522
FW
566 OPT_BOOLEAN('M', "multiplex", &multiplex,
567 "multiplex counter output in a single channel"),
0e9b20b8
IM
568 OPT_END()
569};
570
f37a291c 571int cmd_record(int argc, const char **argv, const char *prefix __used)
0e9b20b8
IM
572{
573 int counter;
574
a0541234
AB
575 argc = parse_options(argc, argv, options, record_usage,
576 PARSE_OPT_STOP_AT_NON_OPTION);
ef65b2a0 577 if (!argc && target_pid == -1 && !system_wide)
0e9b20b8
IM
578 usage_with_options(record_usage, options);
579
bbd36e5e
PZ
580 if (!nr_counters) {
581 nr_counters = 1;
582 attrs[0].type = PERF_TYPE_HARDWARE;
583 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
584 }
0e9b20b8 585
7e4ff9e3
MG
586 /*
587 * User specified count overrides default frequency.
588 */
589 if (default_interval)
590 freq = 0;
591 else if (freq) {
592 default_interval = freq;
593 } else {
594 fprintf(stderr, "frequency and count are zero, aborting\n");
595 exit(EXIT_FAILURE);
596 }
597
0e9b20b8 598 for (counter = 0; counter < nr_counters; counter++) {
a21ca2ca 599 if (attrs[counter].sample_period)
0e9b20b8
IM
600 continue;
601
a21ca2ca 602 attrs[counter].sample_period = default_interval;
0e9b20b8
IM
603 }
604
605 return __cmd_record(argc, argv);
606}
This page took 0.102792 seconds and 5 git commands to generate.