perf record: Use perf_evlist__mmap
[deliverable/linux.git] / tools / perf / builtin-record.c
1 /*
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.
7 */
8 #define _FILE_OFFSET_BITS 64
9
10 #include "builtin.h"
11
12 #include "perf.h"
13
14 #include "util/build-id.h"
15 #include "util/util.h"
16 #include "util/parse-options.h"
17 #include "util/parse-events.h"
18
19 #include "util/header.h"
20 #include "util/event.h"
21 #include "util/evlist.h"
22 #include "util/evsel.h"
23 #include "util/debug.h"
24 #include "util/session.h"
25 #include "util/symbol.h"
26 #include "util/cpumap.h"
27
28 #include <unistd.h>
29 #include <sched.h>
30 #include <sys/mman.h>
31
32 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
33 #define SID(e, x, y) xyarray__entry(e->id, x, y)
34
35 enum write_mode_t {
36 WRITE_FORCE,
37 WRITE_APPEND
38 };
39
40 static u64 user_interval = ULLONG_MAX;
41 static u64 default_interval = 0;
42 static u64 sample_type;
43
44 static struct cpu_map *cpus;
45 static unsigned int page_size;
46 static unsigned int mmap_pages = 128;
47 static unsigned int user_freq = UINT_MAX;
48 static int freq = 1000;
49 static int output;
50 static int pipe_output = 0;
51 static const char *output_name = "perf.data";
52 static int group = 0;
53 static int realtime_prio = 0;
54 static bool nodelay = false;
55 static bool raw_samples = false;
56 static bool sample_id_all_avail = true;
57 static bool system_wide = false;
58 static pid_t target_pid = -1;
59 static pid_t target_tid = -1;
60 static struct thread_map *threads;
61 static pid_t child_pid = -1;
62 static bool no_inherit = false;
63 static enum write_mode_t write_mode = WRITE_FORCE;
64 static bool call_graph = false;
65 static bool inherit_stat = false;
66 static bool no_samples = false;
67 static bool sample_address = false;
68 static bool sample_time = false;
69 static bool no_buildid = false;
70 static bool no_buildid_cache = false;
71 static struct perf_evlist *evsel_list;
72
73 static long samples = 0;
74 static u64 bytes_written = 0;
75
76 static int file_new = 1;
77 static off_t post_processing_offset;
78
79 static struct perf_session *session;
80 static const char *cpu_list;
81
82 static void advance_output(size_t size)
83 {
84 bytes_written += size;
85 }
86
87 static void write_output(void *buf, size_t size)
88 {
89 while (size) {
90 int ret = write(output, buf, size);
91
92 if (ret < 0)
93 die("failed to write");
94
95 size -= ret;
96 buf += ret;
97
98 bytes_written += ret;
99 }
100 }
101
102 static int process_synthesized_event(event_t *event,
103 struct sample_data *sample __used,
104 struct perf_session *self __used)
105 {
106 write_output(event, event->header.size);
107 return 0;
108 }
109
110 static void mmap_read(struct perf_mmap *md)
111 {
112 unsigned int head = perf_mmap__read_head(md);
113 unsigned int old = md->prev;
114 unsigned char *data = md->base + page_size;
115 unsigned long size;
116 void *buf;
117 int diff;
118
119 /*
120 * If we're further behind than half the buffer, there's a chance
121 * the writer will bite our tail and mess up the samples under us.
122 *
123 * If we somehow ended up ahead of the head, we got messed up.
124 *
125 * In either case, truncate and restart at head.
126 */
127 diff = head - old;
128 if (diff < 0) {
129 fprintf(stderr, "WARNING: failed to keep up with mmap data\n");
130 /*
131 * head points to a known good entry, start there.
132 */
133 old = head;
134 }
135
136 if (old != head)
137 samples++;
138
139 size = head - old;
140
141 if ((old & md->mask) + size != (head & md->mask)) {
142 buf = &data[old & md->mask];
143 size = md->mask + 1 - (old & md->mask);
144 old += size;
145
146 write_output(buf, size);
147 }
148
149 buf = &data[old & md->mask];
150 size = head - old;
151 old += size;
152
153 write_output(buf, size);
154
155 md->prev = old;
156 perf_mmap__write_tail(md, old);
157 }
158
159 static volatile int done = 0;
160 static volatile int signr = -1;
161
162 static void sig_handler(int sig)
163 {
164 done = 1;
165 signr = sig;
166 }
167
168 static void sig_atexit(void)
169 {
170 if (child_pid > 0)
171 kill(child_pid, SIGTERM);
172
173 if (signr == -1 || signr == SIGUSR1)
174 return;
175
176 signal(signr, SIG_DFL);
177 kill(getpid(), signr);
178 }
179
180 static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
181 {
182 struct perf_header_attr *h_attr;
183
184 if (nr < session->header.attrs) {
185 h_attr = session->header.attr[nr];
186 } else {
187 h_attr = perf_header_attr__new(a);
188 if (h_attr != NULL)
189 if (perf_header__add_attr(&session->header, h_attr) < 0) {
190 perf_header_attr__delete(h_attr);
191 h_attr = NULL;
192 }
193 }
194
195 return h_attr;
196 }
197
198 static void create_counter(struct perf_evsel *evsel, int cpu)
199 {
200 char *filter = evsel->filter;
201 struct perf_event_attr *attr = &evsel->attr;
202 struct perf_header_attr *h_attr;
203 struct perf_sample_id *sid;
204 int thread_index;
205 int ret;
206
207 for (thread_index = 0; thread_index < threads->nr; thread_index++) {
208 h_attr = get_header_attr(attr, evsel->idx);
209 if (h_attr == NULL)
210 die("nomem\n");
211
212 if (!file_new) {
213 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
214 fprintf(stderr, "incompatible append\n");
215 exit(-1);
216 }
217 }
218
219 sid = SID(evsel, cpu, thread_index);
220 if (perf_header_attr__add_id(h_attr, sid->id) < 0) {
221 pr_warning("Not enough memory to add id\n");
222 exit(-1);
223 }
224
225 if (filter != NULL) {
226 ret = ioctl(FD(evsel, cpu, thread_index),
227 PERF_EVENT_IOC_SET_FILTER, filter);
228 if (ret) {
229 error("failed to set filter with %d (%s)\n", errno,
230 strerror(errno));
231 exit(-1);
232 }
233 }
234 }
235
236 if (!sample_type)
237 sample_type = attr->sample_type;
238 }
239
240 static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist)
241 {
242 struct perf_event_attr *attr = &evsel->attr;
243 int track = !evsel->idx; /* only the first counter needs these */
244
245 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
246 PERF_FORMAT_TOTAL_TIME_RUNNING |
247 PERF_FORMAT_ID;
248
249 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
250
251 if (evlist->nr_entries > 1)
252 attr->sample_type |= PERF_SAMPLE_ID;
253
254 /*
255 * We default some events to a 1 default interval. But keep
256 * it a weak assumption overridable by the user.
257 */
258 if (!attr->sample_period || (user_freq != UINT_MAX &&
259 user_interval != ULLONG_MAX)) {
260 if (freq) {
261 attr->sample_type |= PERF_SAMPLE_PERIOD;
262 attr->freq = 1;
263 attr->sample_freq = freq;
264 } else {
265 attr->sample_period = default_interval;
266 }
267 }
268
269 if (no_samples)
270 attr->sample_freq = 0;
271
272 if (inherit_stat)
273 attr->inherit_stat = 1;
274
275 if (sample_address) {
276 attr->sample_type |= PERF_SAMPLE_ADDR;
277 attr->mmap_data = track;
278 }
279
280 if (call_graph)
281 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
282
283 if (system_wide)
284 attr->sample_type |= PERF_SAMPLE_CPU;
285
286 if (sample_id_all_avail &&
287 (sample_time || system_wide || !no_inherit || cpu_list))
288 attr->sample_type |= PERF_SAMPLE_TIME;
289
290 if (raw_samples) {
291 attr->sample_type |= PERF_SAMPLE_TIME;
292 attr->sample_type |= PERF_SAMPLE_RAW;
293 attr->sample_type |= PERF_SAMPLE_CPU;
294 }
295
296 if (nodelay) {
297 attr->watermark = 0;
298 attr->wakeup_events = 1;
299 }
300
301 attr->mmap = track;
302 attr->comm = track;
303
304 if (target_pid == -1 && target_tid == -1 && !system_wide) {
305 attr->disabled = 1;
306 attr->enable_on_exec = 1;
307 }
308 }
309
310 static void open_counters(struct perf_evlist *evlist)
311 {
312 struct perf_evsel *pos;
313 int cpu;
314
315 list_for_each_entry(pos, &evlist->entries, node) {
316 struct perf_event_attr *attr = &pos->attr;
317 /*
318 * Check if parse_single_tracepoint_event has already asked for
319 * PERF_SAMPLE_TIME.
320 *
321 * XXX this is kludgy but short term fix for problems introduced by
322 * eac23d1c that broke 'perf script' by having different sample_types
323 * when using multiple tracepoint events when we use a perf binary
324 * that tries to use sample_id_all on an older kernel.
325 *
326 * We need to move counter creation to perf_session, support
327 * different sample_types, etc.
328 */
329 bool time_needed = attr->sample_type & PERF_SAMPLE_TIME;
330
331 config_attr(pos, evlist);
332 retry_sample_id:
333 attr->sample_id_all = sample_id_all_avail ? 1 : 0;
334 try_again:
335 if (perf_evsel__open(pos, cpus, threads, group, !no_inherit) < 0) {
336 int err = errno;
337
338 if (err == EPERM || err == EACCES)
339 die("Permission error - are you root?\n"
340 "\t Consider tweaking"
341 " /proc/sys/kernel/perf_event_paranoid.\n");
342 else if (err == ENODEV && cpu_list) {
343 die("No such device - did you specify"
344 " an out-of-range profile CPU?\n");
345 } else if (err == EINVAL && sample_id_all_avail) {
346 /*
347 * Old kernel, no attr->sample_id_type_all field
348 */
349 sample_id_all_avail = false;
350 if (!sample_time && !raw_samples && !time_needed)
351 attr->sample_type &= ~PERF_SAMPLE_TIME;
352
353 goto retry_sample_id;
354 }
355
356 /*
357 * If it's cycles then fall back to hrtimer
358 * based cpu-clock-tick sw counter, which
359 * is always available even if no PMU support:
360 */
361 if (attr->type == PERF_TYPE_HARDWARE
362 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
363
364 if (verbose)
365 warning(" ... trying to fall back to cpu-clock-ticks\n");
366 attr->type = PERF_TYPE_SOFTWARE;
367 attr->config = PERF_COUNT_SW_CPU_CLOCK;
368 goto try_again;
369 }
370 printf("\n");
371 error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
372 err, strerror(err));
373
374 #if defined(__i386__) || defined(__x86_64__)
375 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
376 die("No hardware sampling interrupt available."
377 " No APIC? If so then you can boot the kernel"
378 " with the \"lapic\" boot parameter to"
379 " force-enable it.\n");
380 #endif
381
382 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
383 }
384 }
385
386 if (perf_evlist__mmap(evlist, cpus, threads, mmap_pages, false) < 0)
387 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
388
389 for (cpu = 0; cpu < cpus->nr; ++cpu) {
390 list_for_each_entry(pos, &evlist->entries, node)
391 create_counter(pos, cpu);
392 }
393 }
394
395 static int process_buildids(void)
396 {
397 u64 size = lseek(output, 0, SEEK_CUR);
398
399 if (size == 0)
400 return 0;
401
402 session->fd = output;
403 return __perf_session__process_events(session, post_processing_offset,
404 size - post_processing_offset,
405 size, &build_id__mark_dso_hit_ops);
406 }
407
408 static void atexit_header(void)
409 {
410 if (!pipe_output) {
411 session->header.data_size += bytes_written;
412
413 if (!no_buildid)
414 process_buildids();
415 perf_header__write(&session->header, evsel_list, output, true);
416 perf_session__delete(session);
417 perf_evlist__delete(evsel_list);
418 symbol__exit();
419 }
420 }
421
422 static void event__synthesize_guest_os(struct machine *machine, void *data)
423 {
424 int err;
425 struct perf_session *psession = data;
426
427 if (machine__is_host(machine))
428 return;
429
430 /*
431 *As for guest kernel when processing subcommand record&report,
432 *we arrange module mmap prior to guest kernel mmap and trigger
433 *a preload dso because default guest module symbols are loaded
434 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
435 *method is used to avoid symbol missing when the first addr is
436 *in module instead of in guest kernel.
437 */
438 err = event__synthesize_modules(process_synthesized_event,
439 psession, machine);
440 if (err < 0)
441 pr_err("Couldn't record guest kernel [%d]'s reference"
442 " relocation symbol.\n", machine->pid);
443
444 /*
445 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
446 * have no _text sometimes.
447 */
448 err = event__synthesize_kernel_mmap(process_synthesized_event,
449 psession, machine, "_text");
450 if (err < 0)
451 err = event__synthesize_kernel_mmap(process_synthesized_event,
452 psession, machine, "_stext");
453 if (err < 0)
454 pr_err("Couldn't record guest kernel [%d]'s reference"
455 " relocation symbol.\n", machine->pid);
456 }
457
458 static struct perf_event_header finished_round_event = {
459 .size = sizeof(struct perf_event_header),
460 .type = PERF_RECORD_FINISHED_ROUND,
461 };
462
463 static void mmap_read_all(void)
464 {
465 int i;
466
467 for (i = 0; i < cpus->nr; i++) {
468 if (evsel_list->mmap[i].base)
469 mmap_read(&evsel_list->mmap[i]);
470 }
471
472 if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO))
473 write_output(&finished_round_event, sizeof(finished_round_event));
474 }
475
476 static int __cmd_record(int argc, const char **argv)
477 {
478 int i;
479 struct stat st;
480 int flags;
481 int err;
482 unsigned long waking = 0;
483 int child_ready_pipe[2], go_pipe[2];
484 const bool forks = argc > 0;
485 char buf;
486 struct machine *machine;
487
488 page_size = sysconf(_SC_PAGE_SIZE);
489
490 atexit(sig_atexit);
491 signal(SIGCHLD, sig_handler);
492 signal(SIGINT, sig_handler);
493 signal(SIGUSR1, sig_handler);
494
495 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
496 perror("failed to create pipes");
497 exit(-1);
498 }
499
500 if (!strcmp(output_name, "-"))
501 pipe_output = 1;
502 else if (!stat(output_name, &st) && st.st_size) {
503 if (write_mode == WRITE_FORCE) {
504 char oldname[PATH_MAX];
505 snprintf(oldname, sizeof(oldname), "%s.old",
506 output_name);
507 unlink(oldname);
508 rename(output_name, oldname);
509 }
510 } else if (write_mode == WRITE_APPEND) {
511 write_mode = WRITE_FORCE;
512 }
513
514 flags = O_CREAT|O_RDWR;
515 if (write_mode == WRITE_APPEND)
516 file_new = 0;
517 else
518 flags |= O_TRUNC;
519
520 if (pipe_output)
521 output = STDOUT_FILENO;
522 else
523 output = open(output_name, flags, S_IRUSR | S_IWUSR);
524 if (output < 0) {
525 perror("failed to create output file");
526 exit(-1);
527 }
528
529 session = perf_session__new(output_name, O_WRONLY,
530 write_mode == WRITE_FORCE, false, NULL);
531 if (session == NULL) {
532 pr_err("Not enough memory for reading perf file header\n");
533 return -1;
534 }
535
536 if (!no_buildid)
537 perf_header__set_feat(&session->header, HEADER_BUILD_ID);
538
539 if (!file_new) {
540 err = perf_header__read(session, output);
541 if (err < 0)
542 goto out_delete_session;
543 }
544
545 if (have_tracepoints(&evsel_list->entries))
546 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
547
548 /*
549 * perf_session__delete(session) will be called at atexit_header()
550 */
551 atexit(atexit_header);
552
553 if (forks) {
554 child_pid = fork();
555 if (child_pid < 0) {
556 perror("failed to fork");
557 exit(-1);
558 }
559
560 if (!child_pid) {
561 if (pipe_output)
562 dup2(2, 1);
563 close(child_ready_pipe[0]);
564 close(go_pipe[1]);
565 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
566
567 /*
568 * Do a dummy execvp to get the PLT entry resolved,
569 * so we avoid the resolver overhead on the real
570 * execvp call.
571 */
572 execvp("", (char **)argv);
573
574 /*
575 * Tell the parent we're ready to go
576 */
577 close(child_ready_pipe[1]);
578
579 /*
580 * Wait until the parent tells us to go.
581 */
582 if (read(go_pipe[0], &buf, 1) == -1)
583 perror("unable to read pipe");
584
585 execvp(argv[0], (char **)argv);
586
587 perror(argv[0]);
588 kill(getppid(), SIGUSR1);
589 exit(-1);
590 }
591
592 if (!system_wide && target_tid == -1 && target_pid == -1)
593 threads->map[0] = child_pid;
594
595 close(child_ready_pipe[1]);
596 close(go_pipe[0]);
597 /*
598 * wait for child to settle
599 */
600 if (read(child_ready_pipe[0], &buf, 1) == -1) {
601 perror("unable to read pipe");
602 exit(-1);
603 }
604 close(child_ready_pipe[0]);
605 }
606
607 open_counters(evsel_list);
608
609 perf_session__set_sample_type(session, sample_type);
610
611 if (pipe_output) {
612 err = perf_header__write_pipe(output);
613 if (err < 0)
614 return err;
615 } else if (file_new) {
616 err = perf_header__write(&session->header, evsel_list,
617 output, false);
618 if (err < 0)
619 return err;
620 }
621
622 post_processing_offset = lseek(output, 0, SEEK_CUR);
623
624 perf_session__set_sample_id_all(session, sample_id_all_avail);
625
626 if (pipe_output) {
627 err = event__synthesize_attrs(&session->header,
628 process_synthesized_event,
629 session);
630 if (err < 0) {
631 pr_err("Couldn't synthesize attrs.\n");
632 return err;
633 }
634
635 err = event__synthesize_event_types(process_synthesized_event,
636 session);
637 if (err < 0) {
638 pr_err("Couldn't synthesize event_types.\n");
639 return err;
640 }
641
642 if (have_tracepoints(&evsel_list->entries)) {
643 /*
644 * FIXME err <= 0 here actually means that
645 * there were no tracepoints so its not really
646 * an error, just that we don't need to
647 * synthesize anything. We really have to
648 * return this more properly and also
649 * propagate errors that now are calling die()
650 */
651 err = event__synthesize_tracing_data(output, evsel_list,
652 process_synthesized_event,
653 session);
654 if (err <= 0) {
655 pr_err("Couldn't record tracing data.\n");
656 return err;
657 }
658 advance_output(err);
659 }
660 }
661
662 machine = perf_session__find_host_machine(session);
663 if (!machine) {
664 pr_err("Couldn't find native kernel information.\n");
665 return -1;
666 }
667
668 err = event__synthesize_kernel_mmap(process_synthesized_event,
669 session, machine, "_text");
670 if (err < 0)
671 err = event__synthesize_kernel_mmap(process_synthesized_event,
672 session, machine, "_stext");
673 if (err < 0)
674 pr_err("Couldn't record kernel reference relocation symbol\n"
675 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
676 "Check /proc/kallsyms permission or run as root.\n");
677
678 err = event__synthesize_modules(process_synthesized_event,
679 session, machine);
680 if (err < 0)
681 pr_err("Couldn't record kernel module information.\n"
682 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
683 "Check /proc/modules permission or run as root.\n");
684
685 if (perf_guest)
686 perf_session__process_machines(session, event__synthesize_guest_os);
687
688 if (!system_wide)
689 event__synthesize_thread(target_tid, process_synthesized_event,
690 session);
691 else
692 event__synthesize_threads(process_synthesized_event, session);
693
694 if (realtime_prio) {
695 struct sched_param param;
696
697 param.sched_priority = realtime_prio;
698 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
699 pr_err("Could not set realtime priority.\n");
700 exit(-1);
701 }
702 }
703
704 /*
705 * Let the child rip
706 */
707 if (forks)
708 close(go_pipe[1]);
709
710 for (;;) {
711 int hits = samples;
712 int thread;
713
714 mmap_read_all();
715
716 if (hits == samples) {
717 if (done)
718 break;
719 err = poll(evsel_list->pollfd, evsel_list->nr_fds, -1);
720 waking++;
721 }
722
723 if (done) {
724 for (i = 0; i < cpus->nr; i++) {
725 struct perf_evsel *pos;
726
727 list_for_each_entry(pos, &evsel_list->entries, node) {
728 for (thread = 0;
729 thread < threads->nr;
730 thread++)
731 ioctl(FD(pos, i, thread),
732 PERF_EVENT_IOC_DISABLE);
733 }
734 }
735 }
736 }
737
738 if (quiet || signr == SIGUSR1)
739 return 0;
740
741 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
742
743 /*
744 * Approximate RIP event size: 24 bytes.
745 */
746 fprintf(stderr,
747 "[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n",
748 (double)bytes_written / 1024.0 / 1024.0,
749 output_name,
750 bytes_written / 24);
751
752 return 0;
753
754 out_delete_session:
755 perf_session__delete(session);
756 return err;
757 }
758
759 static const char * const record_usage[] = {
760 "perf record [<options>] [<command>]",
761 "perf record [<options>] -- <command> [<options>]",
762 NULL
763 };
764
765 static bool force, append_file;
766
767 const struct option record_options[] = {
768 OPT_CALLBACK('e', "event", &evsel_list, "event",
769 "event selector. use 'perf list' to list available events",
770 parse_events),
771 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
772 "event filter", parse_filter),
773 OPT_INTEGER('p', "pid", &target_pid,
774 "record events on existing process id"),
775 OPT_INTEGER('t', "tid", &target_tid,
776 "record events on existing thread id"),
777 OPT_INTEGER('r', "realtime", &realtime_prio,
778 "collect data with this RT SCHED_FIFO priority"),
779 OPT_BOOLEAN('D', "no-delay", &nodelay,
780 "collect data without buffering"),
781 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
782 "collect raw sample records from all opened counters"),
783 OPT_BOOLEAN('a', "all-cpus", &system_wide,
784 "system-wide collection from all CPUs"),
785 OPT_BOOLEAN('A', "append", &append_file,
786 "append to the output file to do incremental profiling"),
787 OPT_STRING('C', "cpu", &cpu_list, "cpu",
788 "list of cpus to monitor"),
789 OPT_BOOLEAN('f', "force", &force,
790 "overwrite existing data file (deprecated)"),
791 OPT_U64('c', "count", &user_interval, "event period to sample"),
792 OPT_STRING('o', "output", &output_name, "file",
793 "output file name"),
794 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
795 "child tasks do not inherit counters"),
796 OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"),
797 OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
798 OPT_BOOLEAN('g', "call-graph", &call_graph,
799 "do call-graph (stack chain/backtrace) recording"),
800 OPT_INCR('v', "verbose", &verbose,
801 "be more verbose (show counter open errors, etc)"),
802 OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
803 OPT_BOOLEAN('s', "stat", &inherit_stat,
804 "per thread counts"),
805 OPT_BOOLEAN('d', "data", &sample_address,
806 "Sample addresses"),
807 OPT_BOOLEAN('T', "timestamp", &sample_time, "Sample timestamps"),
808 OPT_BOOLEAN('n', "no-samples", &no_samples,
809 "don't sample"),
810 OPT_BOOLEAN('N', "no-buildid-cache", &no_buildid_cache,
811 "do not update the buildid cache"),
812 OPT_BOOLEAN('B', "no-buildid", &no_buildid,
813 "do not collect buildids in perf.data"),
814 OPT_END()
815 };
816
817 int cmd_record(int argc, const char **argv, const char *prefix __used)
818 {
819 int err = -ENOMEM;
820 struct perf_evsel *pos;
821
822 evsel_list = perf_evlist__new();
823 if (evsel_list == NULL)
824 return -ENOMEM;
825
826 argc = parse_options(argc, argv, record_options, record_usage,
827 PARSE_OPT_STOP_AT_NON_OPTION);
828 if (!argc && target_pid == -1 && target_tid == -1 &&
829 !system_wide && !cpu_list)
830 usage_with_options(record_usage, record_options);
831
832 if (force && append_file) {
833 fprintf(stderr, "Can't overwrite and append at the same time."
834 " You need to choose between -f and -A");
835 usage_with_options(record_usage, record_options);
836 } else if (append_file) {
837 write_mode = WRITE_APPEND;
838 } else {
839 write_mode = WRITE_FORCE;
840 }
841
842 symbol__init();
843
844 if (no_buildid_cache || no_buildid)
845 disable_buildid_cache();
846
847 if (evsel_list->nr_entries == 0 &&
848 perf_evlist__add_default(evsel_list) < 0) {
849 pr_err("Not enough memory for event selector list\n");
850 goto out_symbol_exit;
851 }
852
853 if (target_pid != -1)
854 target_tid = target_pid;
855
856 threads = thread_map__new(target_pid, target_tid);
857 if (threads == NULL) {
858 pr_err("Problems finding threads of monitor\n");
859 usage_with_options(record_usage, record_options);
860 }
861
862 if (target_tid != -1)
863 cpus = cpu_map__dummy_new();
864 else
865 cpus = cpu_map__new(cpu_list);
866
867 if (cpus == NULL)
868 usage_with_options(record_usage, record_options);
869
870 list_for_each_entry(pos, &evsel_list->entries, node) {
871 if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
872 goto out_free_fd;
873 if (perf_header__push_event(pos->attr.config, event_name(pos)))
874 goto out_free_fd;
875 }
876
877 if (perf_evlist__alloc_pollfd(evsel_list, cpus->nr, threads->nr) < 0)
878 goto out_free_fd;
879
880 if (user_interval != ULLONG_MAX)
881 default_interval = user_interval;
882 if (user_freq != UINT_MAX)
883 freq = user_freq;
884
885 /*
886 * User specified count overrides default frequency.
887 */
888 if (default_interval)
889 freq = 0;
890 else if (freq) {
891 default_interval = freq;
892 } else {
893 fprintf(stderr, "frequency and count are zero, aborting\n");
894 err = -EINVAL;
895 goto out_free_fd;
896 }
897
898 err = __cmd_record(argc, argv);
899
900 out_free_fd:
901 thread_map__delete(threads);
902 threads = NULL;
903 out_symbol_exit:
904 symbol__exit();
905 return err;
906 }
This page took 0.050799 seconds and 5 git commands to generate.