perf: Convert perf header build_ids into build_id events
[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 */
b8f46c5a
XG
8#define _FILE_OFFSET_BITS 64
9
16f762a2 10#include "builtin.h"
bf9e1876
IM
11
12#include "perf.h"
13
6122e4e4 14#include "util/build-id.h"
6eda5838 15#include "util/util.h"
0e9b20b8 16#include "util/parse-options.h"
8ad8db37 17#include "util/parse-events.h"
6eda5838 18
7c6a1c65 19#include "util/header.h"
66e274f3 20#include "util/event.h"
8f28827a 21#include "util/debug.h"
94c744b6 22#include "util/session.h"
8d06367f 23#include "util/symbol.h"
a12b51c4 24#include "util/cpumap.h"
7c6a1c65 25
97124d5e 26#include <unistd.h>
de9ac07b 27#include <sched.h>
de9ac07b 28
d6d901c2 29static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
a21ca2ca 30
7e4ff9e3 31static long default_interval = 0;
a21ca2ca 32
42e59d7d 33static int nr_cpus = 0;
de9ac07b 34static unsigned int page_size;
42e59d7d
IM
35static unsigned int mmap_pages = 128;
36static int freq = 1000;
de9ac07b 37static int output;
529870e3 38static int pipe_output = 0;
23ac9cbe 39static const char *output_name = "perf.data";
42e59d7d
IM
40static int group = 0;
41static unsigned int realtime_prio = 0;
c0555642
IM
42static bool raw_samples = false;
43static bool system_wide = false;
42e59d7d
IM
44static int profile_cpu = -1;
45static pid_t target_pid = -1;
d6d901c2
ZY
46static pid_t target_tid = -1;
47static pid_t *all_tids = NULL;
48static int thread_num = 0;
42e59d7d 49static pid_t child_pid = -1;
c0555642
IM
50static bool inherit = true;
51static bool force = false;
52static bool append_file = false;
53static bool call_graph = false;
54static bool inherit_stat = false;
55static bool no_samples = false;
56static bool sample_address = false;
57static bool multiplex = false;
42e59d7d
IM
58static int multiplex_fd = -1;
59
60static long samples = 0;
a21ca2ca
IM
61static struct timeval last_read;
62static struct timeval this_read;
63
42e59d7d 64static u64 bytes_written = 0;
a21ca2ca 65
d6d901c2 66static struct pollfd *event_array;
a21ca2ca 67
42e59d7d
IM
68static int nr_poll = 0;
69static int nr_cpu = 0;
a21ca2ca 70
42e59d7d 71static int file_new = 1;
6122e4e4 72static off_t post_processing_offset;
7c6a1c65 73
94c744b6 74static struct perf_session *session;
f5970550 75
de9ac07b 76struct mmap_data {
a21ca2ca
IM
77 int counter;
78 void *base;
79 unsigned int mask;
80 unsigned int prev;
de9ac07b
PZ
81};
82
d6d901c2 83static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
a21ca2ca 84
9d91a6f7 85static unsigned long mmap_read_head(struct mmap_data *md)
de9ac07b 86{
cdd6c482 87 struct perf_event_mmap_page *pc = md->base;
9d91a6f7 88 long head;
de9ac07b
PZ
89
90 head = pc->data_head;
91 rmb();
92
93 return head;
94}
95
9d91a6f7
PZ
96static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
97{
cdd6c482 98 struct perf_event_mmap_page *pc = md->base;
9d91a6f7
PZ
99
100 /*
101 * ensure all reads are done before we write the tail out.
102 */
103 /* mb(); */
104 pc->data_tail = tail;
105}
106
9215545e
TZ
107static void advance_output(size_t size)
108{
109 bytes_written += size;
110}
111
f5970550
PZ
112static void write_output(void *buf, size_t size)
113{
114 while (size) {
115 int ret = write(output, buf, size);
116
117 if (ret < 0)
118 die("failed to write");
119
120 size -= ret;
121 buf += ret;
122
123 bytes_written += ret;
124 }
125}
126
d8f66248
ACM
127static int process_synthesized_event(event_t *event,
128 struct perf_session *self __used)
234fbbf5 129{
6122e4e4 130 write_output(event, event->header.size);
234fbbf5
ACM
131 return 0;
132}
133
de9ac07b
PZ
134static void mmap_read(struct mmap_data *md)
135{
136 unsigned int head = mmap_read_head(md);
137 unsigned int old = md->prev;
138 unsigned char *data = md->base + page_size;
139 unsigned long size;
140 void *buf;
141 int diff;
142
143 gettimeofday(&this_read, NULL);
144
145 /*
146 * If we're further behind than half the buffer, there's a chance
2debbc83 147 * the writer will bite our tail and mess up the samples under us.
de9ac07b
PZ
148 *
149 * If we somehow ended up ahead of the head, we got messed up.
150 *
151 * In either case, truncate and restart at head.
152 */
153 diff = head - old;
9d91a6f7 154 if (diff < 0) {
de9ac07b
PZ
155 struct timeval iv;
156 unsigned long msecs;
157
158 timersub(&this_read, &last_read, &iv);
159 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
160
161 fprintf(stderr, "WARNING: failed to keep up with mmap data."
162 " Last read %lu msecs ago.\n", msecs);
163
164 /*
165 * head points to a known good entry, start there.
166 */
167 old = head;
168 }
169
170 last_read = this_read;
171
172 if (old != head)
2debbc83 173 samples++;
de9ac07b
PZ
174
175 size = head - old;
176
177 if ((old & md->mask) + size != (head & md->mask)) {
178 buf = &data[old & md->mask];
179 size = md->mask + 1 - (old & md->mask);
180 old += size;
021e9f47 181
6122e4e4 182 write_output(buf, size);
de9ac07b
PZ
183 }
184
185 buf = &data[old & md->mask];
186 size = head - old;
187 old += size;
021e9f47 188
6122e4e4 189 write_output(buf, size);
de9ac07b
PZ
190
191 md->prev = old;
9d91a6f7 192 mmap_write_tail(md, old);
de9ac07b
PZ
193}
194
195static volatile int done = 0;
f7b7c26e 196static volatile int signr = -1;
de9ac07b 197
16c8a109 198static void sig_handler(int sig)
de9ac07b 199{
16c8a109 200 done = 1;
f7b7c26e
PZ
201 signr = sig;
202}
203
204static void sig_atexit(void)
205{
933da83a
CW
206 if (child_pid != -1)
207 kill(child_pid, SIGTERM);
208
f7b7c26e
PZ
209 if (signr == -1)
210 return;
211
212 signal(signr, SIG_DFL);
213 kill(getpid(), signr);
de9ac07b
PZ
214}
215
f250c030
IM
216static int group_fd;
217
cdd6c482 218static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
7c6a1c65
PZ
219{
220 struct perf_header_attr *h_attr;
221
94c744b6
ACM
222 if (nr < session->header.attrs) {
223 h_attr = session->header.attr[nr];
7c6a1c65
PZ
224 } else {
225 h_attr = perf_header_attr__new(a);
dc79c0fc 226 if (h_attr != NULL)
94c744b6 227 if (perf_header__add_attr(&session->header, h_attr) < 0) {
11deb1f9
ACM
228 perf_header_attr__delete(h_attr);
229 h_attr = NULL;
230 }
7c6a1c65
PZ
231 }
232
233 return h_attr;
234}
235
d6d901c2 236static void create_counter(int counter, int cpu)
de9ac07b 237{
c171b552 238 char *filter = filters[counter];
cdd6c482 239 struct perf_event_attr *attr = attrs + counter;
7c6a1c65
PZ
240 struct perf_header_attr *h_attr;
241 int track = !counter; /* only the first counter needs these */
d6d901c2 242 int thread_index;
c171b552 243 int ret;
7c6a1c65
PZ
244 struct {
245 u64 count;
246 u64 time_enabled;
247 u64 time_running;
248 u64 id;
249 } read_data;
250
251 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
252 PERF_FORMAT_TOTAL_TIME_RUNNING |
253 PERF_FORMAT_ID;
16c8a109 254
3a9f131f 255 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
3efa1cc9 256
8907fd60
EM
257 if (nr_counters > 1)
258 attr->sample_type |= PERF_SAMPLE_ID;
259
1dba15e7 260 if (freq) {
ea1900e5 261 attr->sample_type |= PERF_SAMPLE_PERIOD;
a21ca2ca
IM
262 attr->freq = 1;
263 attr->sample_freq = freq;
1dba15e7 264 }
3efa1cc9 265
649c48a9
PZ
266 if (no_samples)
267 attr->sample_freq = 0;
268
269 if (inherit_stat)
270 attr->inherit_stat = 1;
271
4bba828d
AB
272 if (sample_address)
273 attr->sample_type |= PERF_SAMPLE_ADDR;
274
3efa1cc9
IM
275 if (call_graph)
276 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
277
cd6feeea 278 if (raw_samples) {
6ddf259d 279 attr->sample_type |= PERF_SAMPLE_TIME;
daac07b2 280 attr->sample_type |= PERF_SAMPLE_RAW;
cd6feeea
IM
281 attr->sample_type |= PERF_SAMPLE_CPU;
282 }
f413cdb8 283
a21ca2ca
IM
284 attr->mmap = track;
285 attr->comm = track;
60ab2716 286 attr->inherit = inherit;
46be604b
ZY
287 if (target_pid == -1 && !system_wide) {
288 attr->disabled = 1;
bedbfdea 289 attr->enable_on_exec = 1;
46be604b 290 }
bedbfdea 291
d6d901c2 292 for (thread_index = 0; thread_index < thread_num; thread_index++) {
3da297a6 293try_again:
d6d901c2
ZY
294 fd[nr_cpu][counter][thread_index] = sys_perf_event_open(attr,
295 all_tids[thread_index], cpu, group_fd, 0);
296
297 if (fd[nr_cpu][counter][thread_index] < 0) {
298 int err = errno;
299
300 if (err == EPERM || err == EACCES)
301 die("Permission error - are you root?\n"
302 "\t Consider tweaking"
303 " /proc/sys/kernel/perf_event_paranoid.\n");
304 else if (err == ENODEV && profile_cpu != -1) {
305 die("No such device - did you specify"
306 " an out-of-range profile CPU?\n");
307 }
3da297a6 308
d6d901c2
ZY
309 /*
310 * If it's cycles then fall back to hrtimer
311 * based cpu-clock-tick sw counter, which
312 * is always available even if no PMU support:
313 */
314 if (attr->type == PERF_TYPE_HARDWARE
315 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
316
317 if (verbose)
318 warning(" ... trying to fall back to cpu-clock-ticks\n");
319 attr->type = PERF_TYPE_SOFTWARE;
320 attr->config = PERF_COUNT_SW_CPU_CLOCK;
321 goto try_again;
322 }
323 printf("\n");
324 error("perfcounter syscall returned with %d (%s)\n",
325 fd[nr_cpu][counter][thread_index], strerror(err));
bfd45118
SK
326
327#if defined(__i386__) || defined(__x86_64__)
d6d901c2
ZY
328 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
329 die("No hardware sampling interrupt available."
330 " No APIC? If so then you can boot the kernel"
331 " with the \"lapic\" boot parameter to"
332 " force-enable it.\n");
bfd45118
SK
333#endif
334
d6d901c2
ZY
335 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
336 exit(-1);
337 }
3da297a6 338
d6d901c2
ZY
339 h_attr = get_header_attr(attr, counter);
340 if (h_attr == NULL)
341 die("nomem\n");
7c6a1c65 342
d6d901c2
ZY
343 if (!file_new) {
344 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
345 fprintf(stderr, "incompatible append\n");
346 exit(-1);
347 }
7c6a1c65 348 }
7c6a1c65 349
d6d901c2
ZY
350 if (read(fd[nr_cpu][counter][thread_index], &read_data, sizeof(read_data)) == -1) {
351 perror("Unable to read perf file descriptor\n");
352 exit(-1);
353 }
7c6a1c65 354
d6d901c2
ZY
355 if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
356 pr_warning("Not enough memory to add id\n");
357 exit(-1);
358 }
7c6a1c65 359
d6d901c2
ZY
360 assert(fd[nr_cpu][counter][thread_index] >= 0);
361 fcntl(fd[nr_cpu][counter][thread_index], F_SETFL, O_NONBLOCK);
16c8a109 362
d6d901c2
ZY
363 /*
364 * First counter acts as the group leader:
365 */
366 if (group && group_fd == -1)
367 group_fd = fd[nr_cpu][counter][thread_index];
368 if (multiplex && multiplex_fd == -1)
369 multiplex_fd = fd[nr_cpu][counter][thread_index];
f250c030 370
d6d901c2 371 if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) {
4502d77c 372
d6d901c2
ZY
373 ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
374 assert(ret != -1);
375 } else {
376 event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index];
377 event_array[nr_poll].events = POLLIN;
378 nr_poll++;
379
380 mmap_array[nr_cpu][counter][thread_index].counter = counter;
381 mmap_array[nr_cpu][counter][thread_index].prev = 0;
382 mmap_array[nr_cpu][counter][thread_index].mask = mmap_pages*page_size - 1;
383 mmap_array[nr_cpu][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
384 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0);
385 if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) {
386 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
387 exit(-1);
388 }
ea57c4f5 389 }
d1302522 390
d6d901c2
ZY
391 if (filter != NULL) {
392 ret = ioctl(fd[nr_cpu][counter][thread_index],
393 PERF_EVENT_IOC_SET_FILTER, filter);
394 if (ret) {
395 error("failed to set filter with %d (%s)\n", errno,
396 strerror(errno));
397 exit(-1);
398 }
c171b552
LZ
399 }
400 }
f250c030 401}
f2521b6e 402
d6d901c2 403static void open_counters(int cpu)
f250c030
IM
404{
405 int counter;
16c8a109 406
f250c030
IM
407 group_fd = -1;
408 for (counter = 0; counter < nr_counters; counter++)
d6d901c2 409 create_counter(counter, cpu);
f250c030 410
16c8a109
PZ
411 nr_cpu++;
412}
413
6122e4e4
ACM
414static int process_buildids(void)
415{
416 u64 size = lseek(output, 0, SEEK_CUR);
417
9f591fd7
ACM
418 if (size == 0)
419 return 0;
420
6122e4e4
ACM
421 session->fd = output;
422 return __perf_session__process_events(session, post_processing_offset,
423 size - post_processing_offset,
424 size, &build_id__mark_dso_hit_ops);
425}
426
f5970550
PZ
427static void atexit_header(void)
428{
c7929e47
TZ
429 if (!pipe_output) {
430 session->header.data_size += bytes_written;
f5970550 431
c7929e47
TZ
432 process_buildids();
433 perf_header__write(&session->header, output, true);
434 } else {
435 int err;
436
437 err = event__synthesize_build_ids(process_synthesized_event,
438 session);
439 if (err < 0)
440 pr_err("Couldn't synthesize build ids.\n");
441 }
f5970550
PZ
442}
443
d4db3f16 444static int __cmd_record(int argc, const char **argv)
16c8a109
PZ
445{
446 int i, counter;
abaff32a 447 struct stat st;
7c6a1c65 448 pid_t pid = 0;
abaff32a 449 int flags;
4dc0a04b 450 int err;
8b412664 451 unsigned long waking = 0;
856e9660 452 int child_ready_pipe[2], go_pipe[2];
46be604b 453 const bool forks = argc > 0;
856e9660 454 char buf;
de9ac07b
PZ
455
456 page_size = sysconf(_SC_PAGE_SIZE);
de9ac07b 457
f5970550
PZ
458 atexit(sig_atexit);
459 signal(SIGCHLD, sig_handler);
460 signal(SIGINT, sig_handler);
461
d4db3f16 462 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
856e9660
PZ
463 perror("failed to create pipes");
464 exit(-1);
465 }
466
529870e3
TZ
467 if (!strcmp(output_name, "-"))
468 pipe_output = 1;
469 else if (!stat(output_name, &st) && st.st_size) {
b38d3464
ACM
470 if (!force) {
471 if (!append_file) {
472 pr_err("Error, output file %s exists, use -A "
473 "to append or -f to overwrite.\n",
474 output_name);
475 exit(-1);
476 }
477 } else {
478 char oldname[PATH_MAX];
479 snprintf(oldname, sizeof(oldname), "%s.old",
480 output_name);
481 unlink(oldname);
482 rename(output_name, oldname);
266e0e21
PH
483 }
484 } else {
c0555642 485 append_file = false;
97124d5e
PZ
486 }
487
f887f301 488 flags = O_CREAT|O_RDWR;
abaff32a 489 if (append_file)
f5970550 490 file_new = 0;
abaff32a
IM
491 else
492 flags |= O_TRUNC;
493
529870e3
TZ
494 if (pipe_output)
495 output = STDOUT_FILENO;
496 else
497 output = open(output_name, flags, S_IRUSR | S_IWUSR);
de9ac07b
PZ
498 if (output < 0) {
499 perror("failed to create output file");
500 exit(-1);
501 }
502
75be6cf4 503 session = perf_session__new(output_name, O_WRONLY, force);
94c744b6 504 if (session == NULL) {
a9a70bbc
ACM
505 pr_err("Not enough memory for reading perf file header\n");
506 return -1;
507 }
508
4dc0a04b 509 if (!file_new) {
8dc58101 510 err = perf_header__read(session, output);
4dc0a04b
ACM
511 if (err < 0)
512 return err;
513 }
514
9df37ddd 515 if (raw_samples) {
94c744b6 516 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
9df37ddd
FW
517 } else {
518 for (i = 0; i < nr_counters; i++) {
519 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
94c744b6 520 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
9df37ddd
FW
521 break;
522 }
523 }
524 }
03456a15 525
f5970550
PZ
526 atexit(atexit_header);
527
d4db3f16 528 if (forks) {
46be604b 529 child_pid = fork();
856e9660
PZ
530 if (pid < 0) {
531 perror("failed to fork");
532 exit(-1);
533 }
7c6a1c65 534
46be604b 535 if (!child_pid) {
529870e3
TZ
536 if (pipe_output)
537 dup2(2, 1);
856e9660
PZ
538 close(child_ready_pipe[0]);
539 close(go_pipe[1]);
540 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
541
542 /*
543 * Do a dummy execvp to get the PLT entry resolved,
544 * so we avoid the resolver overhead on the real
545 * execvp call.
546 */
547 execvp("", (char **)argv);
548
549 /*
550 * Tell the parent we're ready to go
551 */
552 close(child_ready_pipe[1]);
553
554 /*
555 * Wait until the parent tells us to go.
556 */
557 if (read(go_pipe[0], &buf, 1) == -1)
558 perror("unable to read pipe");
559
560 execvp(argv[0], (char **)argv);
561
562 perror(argv[0]);
563 exit(-1);
0a5ac846 564 }
856e9660 565
d6d901c2
ZY
566 if (!system_wide && target_tid == -1 && target_pid == -1)
567 all_tids[0] = child_pid;
568
856e9660
PZ
569 close(child_ready_pipe[1]);
570 close(go_pipe[0]);
571 /*
572 * wait for child to settle
573 */
574 if (read(child_ready_pipe[0], &buf, 1) == -1) {
575 perror("unable to read pipe");
576 exit(-1);
577 }
578 close(child_ready_pipe[0]);
579 }
580
60ab2716 581 if ((!system_wide && !inherit) || profile_cpu != -1) {
d6d901c2 582 open_counters(profile_cpu);
856e9660 583 } else {
a12b51c4 584 nr_cpus = read_cpu_map();
856e9660 585 for (i = 0; i < nr_cpus; i++)
d6d901c2 586 open_counters(cpumap[i]);
0a5ac846 587 }
de9ac07b 588
529870e3
TZ
589 if (pipe_output) {
590 err = perf_header__write_pipe(output);
591 if (err < 0)
592 return err;
593 } else if (file_new) {
94c744b6 594 err = perf_header__write(&session->header, output, false);
d5eed904
ACM
595 if (err < 0)
596 return err;
56b03f3c
ACM
597 }
598
6122e4e4
ACM
599 post_processing_offset = lseek(output, 0, SEEK_CUR);
600
2c46dbb5
TZ
601 if (pipe_output) {
602 err = event__synthesize_attrs(&session->header,
603 process_synthesized_event,
604 session);
605 if (err < 0) {
606 pr_err("Couldn't synthesize attrs.\n");
607 return err;
608 }
cd19a035
TZ
609
610 err = event__synthesize_event_types(process_synthesized_event,
611 session);
612 if (err < 0) {
613 pr_err("Couldn't synthesize event_types.\n");
614 return err;
615 }
9215545e
TZ
616
617 err = event__synthesize_tracing_data(output, attrs,
618 nr_counters,
619 process_synthesized_event,
620 session);
621 if (err <= 0) {
622 pr_err("Couldn't record tracing data.\n");
623 return err;
624 }
625
626 advance_output(err);
2c46dbb5
TZ
627 }
628
56b03f3c
ACM
629 err = event__synthesize_kernel_mmap(process_synthesized_event,
630 session, "_text");
70162138
ACM
631 if (err < 0)
632 err = event__synthesize_kernel_mmap(process_synthesized_event,
633 session, "_stext");
56b03f3c
ACM
634 if (err < 0) {
635 pr_err("Couldn't record kernel reference relocation symbol.\n");
636 return err;
b7cece76
ACM
637 }
638
639 err = event__synthesize_modules(process_synthesized_event, session);
640 if (err < 0) {
641 pr_err("Couldn't record kernel reference relocation symbol.\n");
642 return err;
d5eed904 643 }
7c6a1c65 644
d4db3f16 645 if (!system_wide && profile_cpu == -1)
d6d901c2 646 event__synthesize_thread(target_tid, process_synthesized_event,
d8f66248 647 session);
234fbbf5 648 else
d8f66248 649 event__synthesize_threads(process_synthesized_event, session);
7c6a1c65 650
de9ac07b
PZ
651 if (realtime_prio) {
652 struct sched_param param;
653
654 param.sched_priority = realtime_prio;
655 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
6beba7ad 656 pr_err("Could not set realtime priority.\n");
de9ac07b
PZ
657 exit(-1);
658 }
659 }
660
856e9660
PZ
661 /*
662 * Let the child rip
663 */
d4db3f16
ACM
664 if (forks)
665 close(go_pipe[1]);
856e9660 666
649c48a9 667 for (;;) {
2debbc83 668 int hits = samples;
d6d901c2 669 int thread;
de9ac07b 670
16c8a109 671 for (i = 0; i < nr_cpu; i++) {
ea57c4f5 672 for (counter = 0; counter < nr_counters; counter++) {
d6d901c2
ZY
673 for (thread = 0;
674 thread < thread_num; thread++) {
675 if (mmap_array[i][counter][thread].base)
676 mmap_read(&mmap_array[i][counter][thread]);
677 }
678
ea57c4f5 679 }
de9ac07b
PZ
680 }
681
649c48a9
PZ
682 if (hits == samples) {
683 if (done)
684 break;
4dc0a04b 685 err = poll(event_array, nr_poll, -1);
8b412664
PZ
686 waking++;
687 }
688
689 if (done) {
690 for (i = 0; i < nr_cpu; i++) {
d6d901c2
ZY
691 for (counter = 0;
692 counter < nr_counters;
693 counter++) {
694 for (thread = 0;
695 thread < thread_num;
696 thread++)
697 ioctl(fd[i][counter][thread],
698 PERF_EVENT_IOC_DISABLE);
699 }
8b412664 700 }
649c48a9 701 }
de9ac07b
PZ
702 }
703
8b412664
PZ
704 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
705
021e9f47
IM
706 /*
707 * Approximate RIP event size: 24 bytes.
708 */
709 fprintf(stderr,
2debbc83 710 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
021e9f47
IM
711 (double)bytes_written / 1024.0 / 1024.0,
712 output_name,
713 bytes_written / 24);
addc2785 714
de9ac07b
PZ
715 return 0;
716}
0e9b20b8 717
0e9b20b8 718static const char * const record_usage[] = {
9e096753
MG
719 "perf record [<options>] [<command>]",
720 "perf record [<options>] -- <command> [<options>]",
0e9b20b8
IM
721 NULL
722};
723
5242519b 724static const struct option options[] = {
0e9b20b8 725 OPT_CALLBACK('e', "event", NULL, "event",
86847b62
TG
726 "event selector. use 'perf list' to list available events",
727 parse_events),
c171b552
LZ
728 OPT_CALLBACK(0, "filter", NULL, "filter",
729 "event filter", parse_filter),
0e9b20b8 730 OPT_INTEGER('p', "pid", &target_pid,
d6d901c2
ZY
731 "record events on existing process id"),
732 OPT_INTEGER('t', "tid", &target_tid,
733 "record events on existing thread id"),
0e9b20b8
IM
734 OPT_INTEGER('r', "realtime", &realtime_prio,
735 "collect data with this RT SCHED_FIFO priority"),
daac07b2
FW
736 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
737 "collect raw sample records from all opened counters"),
0e9b20b8
IM
738 OPT_BOOLEAN('a', "all-cpus", &system_wide,
739 "system-wide collection from all CPUs"),
abaff32a
IM
740 OPT_BOOLEAN('A', "append", &append_file,
741 "append to the output file to do incremental profiling"),
0a5ac846
JA
742 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
743 "CPU to profile on"),
97124d5e
PZ
744 OPT_BOOLEAN('f', "force", &force,
745 "overwrite existing data file"),
e61078a0 746 OPT_LONG('c', "count", &default_interval,
abaff32a
IM
747 "event period to sample"),
748 OPT_STRING('o', "output", &output_name, "file",
749 "output file name"),
750 OPT_BOOLEAN('i', "inherit", &inherit,
751 "child tasks inherit counters"),
cf1f4574
IM
752 OPT_INTEGER('F', "freq", &freq,
753 "profile at this frequency"),
abaff32a
IM
754 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
755 "number of mmap data pages"),
3efa1cc9
IM
756 OPT_BOOLEAN('g', "call-graph", &call_graph,
757 "do call-graph (stack chain/backtrace) recording"),
c0555642 758 OPT_INCR('v', "verbose", &verbose,
3da297a6 759 "be more verbose (show counter open errors, etc)"),
649c48a9
PZ
760 OPT_BOOLEAN('s', "stat", &inherit_stat,
761 "per thread counts"),
4bba828d
AB
762 OPT_BOOLEAN('d', "data", &sample_address,
763 "Sample addresses"),
649c48a9
PZ
764 OPT_BOOLEAN('n', "no-samples", &no_samples,
765 "don't sample"),
d1302522
FW
766 OPT_BOOLEAN('M', "multiplex", &multiplex,
767 "multiplex counter output in a single channel"),
0e9b20b8
IM
768 OPT_END()
769};
770
f37a291c 771int cmd_record(int argc, const char **argv, const char *prefix __used)
0e9b20b8
IM
772{
773 int counter;
d6d901c2 774 int i,j;
0e9b20b8 775
a0541234 776 argc = parse_options(argc, argv, options, record_usage,
655000e7 777 PARSE_OPT_STOP_AT_NON_OPTION);
d6d901c2
ZY
778 if (!argc && target_pid == -1 && target_tid == -1 &&
779 !system_wide && profile_cpu == -1)
0e9b20b8
IM
780 usage_with_options(record_usage, options);
781
655000e7
ACM
782 symbol__init();
783
bbd36e5e
PZ
784 if (!nr_counters) {
785 nr_counters = 1;
786 attrs[0].type = PERF_TYPE_HARDWARE;
787 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
788 }
0e9b20b8 789
d6d901c2
ZY
790 if (target_pid != -1) {
791 target_tid = target_pid;
792 thread_num = find_all_tid(target_pid, &all_tids);
793 if (thread_num <= 0) {
794 fprintf(stderr, "Can't find all threads of pid %d\n",
795 target_pid);
796 usage_with_options(record_usage, options);
797 }
798 } else {
799 all_tids=malloc(sizeof(pid_t));
800 if (!all_tids)
801 return -ENOMEM;
802
803 all_tids[0] = target_tid;
804 thread_num = 1;
805 }
806
807 for (i = 0; i < MAX_NR_CPUS; i++) {
808 for (j = 0; j < MAX_COUNTERS; j++) {
809 fd[i][j] = malloc(sizeof(int)*thread_num);
5a103174 810 mmap_array[i][j] = zalloc(
d6d901c2
ZY
811 sizeof(struct mmap_data)*thread_num);
812 if (!fd[i][j] || !mmap_array[i][j])
813 return -ENOMEM;
814 }
815 }
816 event_array = malloc(
817 sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
818 if (!event_array)
819 return -ENOMEM;
820
7e4ff9e3
MG
821 /*
822 * User specified count overrides default frequency.
823 */
824 if (default_interval)
825 freq = 0;
826 else if (freq) {
827 default_interval = freq;
828 } else {
829 fprintf(stderr, "frequency and count are zero, aborting\n");
830 exit(EXIT_FAILURE);
831 }
832
0e9b20b8 833 for (counter = 0; counter < nr_counters; counter++) {
a21ca2ca 834 if (attrs[counter].sample_period)
0e9b20b8
IM
835 continue;
836
a21ca2ca 837 attrs[counter].sample_period = default_interval;
0e9b20b8
IM
838 }
839
840 return __cmd_record(argc, argv);
841}
This page took 0.135086 seconds and 5 git commands to generate.