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