tracing/profile: Add filter support
[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 */
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
0e9b20b8
IM
24#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
25#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
1a853e36 26
de9ac07b 27static int fd[MAX_NR_CPUS][MAX_COUNTERS];
a21ca2ca 28
7e4ff9e3 29static long default_interval = 0;
a21ca2ca 30
42e59d7d 31static int nr_cpus = 0;
de9ac07b 32static unsigned int page_size;
42e59d7d
IM
33static unsigned int mmap_pages = 128;
34static int freq = 1000;
de9ac07b 35static int output;
23ac9cbe 36static const char *output_name = "perf.data";
42e59d7d
IM
37static int group = 0;
38static unsigned int realtime_prio = 0;
39static int raw_samples = 0;
40static int system_wide = 0;
41static int profile_cpu = -1;
42static pid_t target_pid = -1;
43static pid_t child_pid = -1;
44static int inherit = 1;
45static int force = 0;
46static int append_file = 0;
47static int call_graph = 0;
48static int inherit_stat = 0;
49static int no_samples = 0;
50static int sample_address = 0;
51static int multiplex = 0;
52static int multiplex_fd = -1;
53
54static long samples = 0;
a21ca2ca
IM
55static struct timeval last_read;
56static struct timeval this_read;
57
42e59d7d 58static u64 bytes_written = 0;
a21ca2ca
IM
59
60static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
61
42e59d7d
IM
62static int nr_poll = 0;
63static int nr_cpu = 0;
a21ca2ca 64
42e59d7d 65static int file_new = 1;
7c6a1c65 66
42e59d7d 67struct perf_header *header = NULL;
f5970550 68
de9ac07b 69struct mmap_data {
a21ca2ca
IM
70 int counter;
71 void *base;
72 unsigned int mask;
73 unsigned int prev;
de9ac07b
PZ
74};
75
a21ca2ca
IM
76static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
77
9d91a6f7 78static unsigned long mmap_read_head(struct mmap_data *md)
de9ac07b 79{
cdd6c482 80 struct perf_event_mmap_page *pc = md->base;
9d91a6f7 81 long head;
de9ac07b
PZ
82
83 head = pc->data_head;
84 rmb();
85
86 return head;
87}
88
9d91a6f7
PZ
89static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
90{
cdd6c482 91 struct perf_event_mmap_page *pc = md->base;
9d91a6f7
PZ
92
93 /*
94 * ensure all reads are done before we write the tail out.
95 */
96 /* mb(); */
97 pc->data_tail = tail;
98}
99
f5970550
PZ
100static void write_output(void *buf, size_t size)
101{
102 while (size) {
103 int ret = write(output, buf, size);
104
105 if (ret < 0)
106 die("failed to write");
107
108 size -= ret;
109 buf += ret;
110
111 bytes_written += ret;
112 }
113}
114
de9ac07b
PZ
115static void mmap_read(struct mmap_data *md)
116{
117 unsigned int head = mmap_read_head(md);
118 unsigned int old = md->prev;
119 unsigned char *data = md->base + page_size;
120 unsigned long size;
121 void *buf;
122 int diff;
123
124 gettimeofday(&this_read, NULL);
125
126 /*
127 * If we're further behind than half the buffer, there's a chance
2debbc83 128 * the writer will bite our tail and mess up the samples under us.
de9ac07b
PZ
129 *
130 * If we somehow ended up ahead of the head, we got messed up.
131 *
132 * In either case, truncate and restart at head.
133 */
134 diff = head - old;
9d91a6f7 135 if (diff < 0) {
de9ac07b
PZ
136 struct timeval iv;
137 unsigned long msecs;
138
139 timersub(&this_read, &last_read, &iv);
140 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
141
142 fprintf(stderr, "WARNING: failed to keep up with mmap data."
143 " Last read %lu msecs ago.\n", msecs);
144
145 /*
146 * head points to a known good entry, start there.
147 */
148 old = head;
149 }
150
151 last_read = this_read;
152
153 if (old != head)
2debbc83 154 samples++;
de9ac07b
PZ
155
156 size = head - old;
157
158 if ((old & md->mask) + size != (head & md->mask)) {
159 buf = &data[old & md->mask];
160 size = md->mask + 1 - (old & md->mask);
161 old += size;
021e9f47 162
f5970550 163 write_output(buf, size);
de9ac07b
PZ
164 }
165
166 buf = &data[old & md->mask];
167 size = head - old;
168 old += size;
021e9f47 169
f5970550 170 write_output(buf, size);
de9ac07b
PZ
171
172 md->prev = old;
9d91a6f7 173 mmap_write_tail(md, old);
de9ac07b
PZ
174}
175
176static volatile int done = 0;
f7b7c26e 177static volatile int signr = -1;
de9ac07b 178
16c8a109 179static void sig_handler(int sig)
de9ac07b 180{
16c8a109 181 done = 1;
f7b7c26e
PZ
182 signr = sig;
183}
184
185static void sig_atexit(void)
186{
933da83a
CW
187 if (child_pid != -1)
188 kill(child_pid, SIGTERM);
189
f7b7c26e
PZ
190 if (signr == -1)
191 return;
192
193 signal(signr, SIG_DFL);
194 kill(getpid(), signr);
de9ac07b
PZ
195}
196
2a8083f0 197static pid_t pid_synthesize_comm_event(pid_t pid, int full)
1a853e36 198{
16f762a2 199 struct comm_event comm_ev;
1a853e36
ACM
200 char filename[PATH_MAX];
201 char bf[BUFSIZ];
2a8083f0
ACM
202 FILE *fp;
203 size_t size = 0;
f70e87d7
PZ
204 DIR *tasks;
205 struct dirent dirent, *next;
2a8083f0 206 pid_t tgid = 0;
1a853e36 207
2a8083f0 208 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
1a853e36 209
2a8083f0 210 fp = fopen(filename, "r");
39e6dd73 211 if (fp == NULL) {
613d8602
IM
212 /*
213 * We raced with a task exiting - just return:
214 */
215 if (verbose)
216 fprintf(stderr, "couldn't open %s\n", filename);
2a8083f0 217 return 0;
1a853e36 218 }
1a853e36 219
1a853e36 220 memset(&comm_ev, 0, sizeof(comm_ev));
2a8083f0
ACM
221 while (!comm_ev.comm[0] || !comm_ev.pid) {
222 if (fgets(bf, sizeof(bf), fp) == NULL)
223 goto out_failure;
224
225 if (memcmp(bf, "Name:", 5) == 0) {
226 char *name = bf + 5;
227 while (*name && isspace(*name))
228 ++name;
229 size = strlen(name) - 1;
230 memcpy(comm_ev.comm, name, size++);
231 } else if (memcmp(bf, "Tgid:", 5) == 0) {
232 char *tgids = bf + 5;
233 while (*tgids && isspace(*tgids))
234 ++tgids;
235 tgid = comm_ev.pid = atoi(tgids);
236 }
237 }
238
cdd6c482 239 comm_ev.header.type = PERF_RECORD_COMM;
9cffa8d5 240 size = ALIGN(size, sizeof(u64));
1a853e36 241 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
16f762a2 242
f70e87d7
PZ
243 if (!full) {
244 comm_ev.tid = pid;
245
f5970550 246 write_output(&comm_ev, comm_ev.header.size);
2a8083f0 247 goto out_fclose;
f70e87d7
PZ
248 }
249
250 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
251
252 tasks = opendir(filename);
253 while (!readdir_r(tasks, &dirent, &next) && next) {
254 char *end;
255 pid = strtol(dirent.d_name, &end, 10);
256 if (*end)
257 continue;
258
259 comm_ev.tid = pid;
260
f5970550 261 write_output(&comm_ev, comm_ev.header.size);
1a853e36 262 }
f70e87d7 263 closedir(tasks);
2a8083f0
ACM
264
265out_fclose:
266 fclose(fp);
267 return tgid;
f70e87d7 268
a0055ae2
ACM
269out_failure:
270 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
271 filename);
272 exit(EXIT_FAILURE);
1a853e36
ACM
273}
274
2a8083f0 275static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
1a853e36
ACM
276{
277 char filename[PATH_MAX];
278 FILE *fp;
279
280 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
281
282 fp = fopen(filename, "r");
283 if (fp == NULL) {
613d8602
IM
284 /*
285 * We raced with a task exiting - just return:
286 */
287 if (verbose)
288 fprintf(stderr, "couldn't open %s\n", filename);
289 return;
1a853e36
ACM
290 }
291 while (1) {
a0055ae2 292 char bf[BUFSIZ], *pbf = bf;
1a853e36 293 struct mmap_event mmap_ev = {
cdd6c482 294 .header = { .type = PERF_RECORD_MMAP },
1a853e36 295 };
a0055ae2 296 int n;
1a853e36
ACM
297 size_t size;
298 if (fgets(bf, sizeof(bf), fp) == NULL)
299 break;
300
301 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
a0055ae2
ACM
302 n = hex2u64(pbf, &mmap_ev.start);
303 if (n < 0)
304 continue;
305 pbf += n + 1;
306 n = hex2u64(pbf, &mmap_ev.len);
307 if (n < 0)
308 continue;
309 pbf += n + 3;
310 if (*pbf == 'x') { /* vm_exec */
76c64c5e 311 char *execname = strchr(bf, '/');
1a853e36 312
11b5f81e
AB
313 /* Catch VDSO */
314 if (execname == NULL)
315 execname = strstr(bf, "[vdso]");
316
76c64c5e 317 if (execname == NULL)
1a853e36
ACM
318 continue;
319
1a853e36
ACM
320 size = strlen(execname);
321 execname[size - 1] = '\0'; /* Remove \n */
322 memcpy(mmap_ev.filename, execname, size);
9cffa8d5 323 size = ALIGN(size, sizeof(u64));
1a853e36
ACM
324 mmap_ev.len -= mmap_ev.start;
325 mmap_ev.header.size = (sizeof(mmap_ev) -
326 (sizeof(mmap_ev.filename) - size));
2a8083f0 327 mmap_ev.pid = tgid;
1a853e36
ACM
328 mmap_ev.tid = pid;
329
f5970550 330 write_output(&mmap_ev, mmap_ev.header.size);
1a853e36
ACM
331 }
332 }
333
334 fclose(fp);
335}
336
7c6a1c65 337static void synthesize_all(void)
f70e87d7
PZ
338{
339 DIR *proc;
340 struct dirent dirent, *next;
341
342 proc = opendir("/proc");
343
344 while (!readdir_r(proc, &dirent, &next) && next) {
345 char *end;
2a8083f0 346 pid_t pid, tgid;
f70e87d7
PZ
347
348 pid = strtol(dirent.d_name, &end, 10);
349 if (*end) /* only interested in proper numerical dirents */
350 continue;
351
2a8083f0
ACM
352 tgid = pid_synthesize_comm_event(pid, 1);
353 pid_synthesize_mmap_samples(pid, tgid);
f70e87d7
PZ
354 }
355
356 closedir(proc);
357}
358
f250c030
IM
359static int group_fd;
360
cdd6c482 361static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
7c6a1c65
PZ
362{
363 struct perf_header_attr *h_attr;
364
365 if (nr < header->attrs) {
366 h_attr = header->attr[nr];
367 } else {
368 h_attr = perf_header_attr__new(a);
369 perf_header__add_attr(header, h_attr);
370 }
371
372 return h_attr;
373}
374
f250c030 375static void create_counter(int counter, int cpu, pid_t pid)
de9ac07b 376{
cdd6c482 377 struct perf_event_attr *attr = attrs + counter;
7c6a1c65
PZ
378 struct perf_header_attr *h_attr;
379 int track = !counter; /* only the first counter needs these */
380 struct {
381 u64 count;
382 u64 time_enabled;
383 u64 time_running;
384 u64 id;
385 } read_data;
386
387 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
388 PERF_FORMAT_TOTAL_TIME_RUNNING |
389 PERF_FORMAT_ID;
16c8a109 390
3a9f131f 391 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
3efa1cc9 392
1dba15e7 393 if (freq) {
ea1900e5 394 attr->sample_type |= PERF_SAMPLE_PERIOD;
a21ca2ca
IM
395 attr->freq = 1;
396 attr->sample_freq = freq;
1dba15e7 397 }
3efa1cc9 398
649c48a9
PZ
399 if (no_samples)
400 attr->sample_freq = 0;
401
402 if (inherit_stat)
403 attr->inherit_stat = 1;
404
4bba828d
AB
405 if (sample_address)
406 attr->sample_type |= PERF_SAMPLE_ADDR;
407
3efa1cc9
IM
408 if (call_graph)
409 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
410
cd6feeea 411 if (raw_samples) {
6ddf259d 412 attr->sample_type |= PERF_SAMPLE_TIME;
daac07b2 413 attr->sample_type |= PERF_SAMPLE_RAW;
cd6feeea
IM
414 attr->sample_type |= PERF_SAMPLE_CPU;
415 }
f413cdb8 416
a21ca2ca
IM
417 attr->mmap = track;
418 attr->comm = track;
419 attr->inherit = (cpu < 0) && inherit;
4502d77c 420 attr->disabled = 1;
16c8a109 421
3da297a6 422try_again:
cdd6c482 423 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
16c8a109 424
f250c030
IM
425 if (fd[nr_cpu][counter] < 0) {
426 int err = errno;
16c8a109 427
f250c030 428 if (err == EPERM)
3da297a6 429 die("Permission error - are you root?\n");
0a5ac846
JA
430 else if (err == ENODEV && profile_cpu != -1)
431 die("No such device - did you specify an out-of-range profile CPU?\n");
3da297a6
IM
432
433 /*
434 * If it's cycles then fall back to hrtimer
435 * based cpu-clock-tick sw counter, which
436 * is always available even if no PMU support:
437 */
438 if (attr->type == PERF_TYPE_HARDWARE
f4dbfa8f 439 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
3da297a6
IM
440
441 if (verbose)
442 warning(" ... trying to fall back to cpu-clock-ticks\n");
443 attr->type = PERF_TYPE_SOFTWARE;
f4dbfa8f 444 attr->config = PERF_COUNT_SW_CPU_CLOCK;
3da297a6
IM
445 goto try_again;
446 }
30c806a0
IM
447 printf("\n");
448 error("perfcounter syscall returned with %d (%s)\n",
449 fd[nr_cpu][counter], strerror(err));
cdd6c482 450 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
f250c030
IM
451 exit(-1);
452 }
3da297a6 453
7c6a1c65
PZ
454 h_attr = get_header_attr(attr, counter);
455
456 if (!file_new) {
457 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
458 fprintf(stderr, "incompatible append\n");
459 exit(-1);
460 }
461 }
462
3928ddbe
FW
463 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
464 perror("Unable to read perf file descriptor\n");
465 exit(-1);
466 }
7c6a1c65
PZ
467
468 perf_header_attr__add_id(h_attr, read_data.id);
469
f250c030
IM
470 assert(fd[nr_cpu][counter] >= 0);
471 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
16c8a109 472
f250c030
IM
473 /*
474 * First counter acts as the group leader:
475 */
476 if (group && group_fd == -1)
477 group_fd = fd[nr_cpu][counter];
ea57c4f5
IM
478 if (multiplex && multiplex_fd == -1)
479 multiplex_fd = fd[nr_cpu][counter];
f250c030 480
ea57c4f5
IM
481 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
482 int ret;
4502d77c 483
cdd6c482 484 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
ea57c4f5
IM
485 assert(ret != -1);
486 } else {
487 event_array[nr_poll].fd = fd[nr_cpu][counter];
488 event_array[nr_poll].events = POLLIN;
489 nr_poll++;
490
491 mmap_array[nr_cpu][counter].counter = counter;
492 mmap_array[nr_cpu][counter].prev = 0;
493 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
494 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
495 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
496 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
497 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
498 exit(-1);
499 }
500 }
d1302522 501
cdd6c482 502 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
f250c030 503}
f2521b6e 504
f250c030
IM
505static void open_counters(int cpu, pid_t pid)
506{
507 int counter;
16c8a109 508
f250c030
IM
509 group_fd = -1;
510 for (counter = 0; counter < nr_counters; counter++)
511 create_counter(counter, cpu, pid);
512
16c8a109
PZ
513 nr_cpu++;
514}
515
f5970550
PZ
516static void atexit_header(void)
517{
7c6a1c65 518 header->data_size += bytes_written;
f5970550 519
7c6a1c65 520 perf_header__write(header, output);
f5970550
PZ
521}
522
0e9b20b8 523static int __cmd_record(int argc, const char **argv)
16c8a109
PZ
524{
525 int i, counter;
abaff32a 526 struct stat st;
7c6a1c65 527 pid_t pid = 0;
abaff32a 528 int flags;
de9ac07b 529 int ret;
8b412664 530 unsigned long waking = 0;
de9ac07b
PZ
531
532 page_size = sysconf(_SC_PAGE_SIZE);
de9ac07b
PZ
533 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
534 assert(nr_cpus <= MAX_NR_CPUS);
535 assert(nr_cpus >= 0);
536
f5970550
PZ
537 atexit(sig_atexit);
538 signal(SIGCHLD, sig_handler);
539 signal(SIGINT, sig_handler);
540
266e0e21
PH
541 if (!stat(output_name, &st) && st.st_size) {
542 if (!force && !append_file) {
543 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
544 output_name);
545 exit(-1);
546 }
547 } else {
548 append_file = 0;
97124d5e
PZ
549 }
550
abaff32a
IM
551 flags = O_CREAT|O_RDWR;
552 if (append_file)
f5970550 553 file_new = 0;
abaff32a
IM
554 else
555 flags |= O_TRUNC;
556
557 output = open(output_name, flags, S_IRUSR|S_IWUSR);
de9ac07b
PZ
558 if (output < 0) {
559 perror("failed to create output file");
560 exit(-1);
561 }
562
7c6a1c65
PZ
563 if (!file_new)
564 header = perf_header__read(output);
565 else
566 header = perf_header__new();
f5970550 567
9df37ddd 568 if (raw_samples) {
03456a15 569 perf_header__set_trace_info();
9df37ddd
FW
570 } else {
571 for (i = 0; i < nr_counters; i++) {
572 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
03456a15 573 perf_header__set_trace_info();
9df37ddd
FW
574 break;
575 }
576 }
577 }
03456a15 578
f5970550
PZ
579 atexit(atexit_header);
580
1a853e36 581 if (!system_wide) {
7c6a1c65
PZ
582 pid = target_pid;
583 if (pid == -1)
584 pid = getpid();
585
0a5ac846
JA
586 open_counters(profile_cpu, pid);
587 } else {
588 if (profile_cpu != -1) {
589 open_counters(profile_cpu, target_pid);
590 } else {
591 for (i = 0; i < nr_cpus; i++)
592 open_counters(i, target_pid);
593 }
594 }
de9ac07b 595
7c6a1c65
PZ
596 if (file_new)
597 perf_header__write(header, output);
598
599 if (!system_wide) {
2a8083f0
ACM
600 pid_t tgid = pid_synthesize_comm_event(pid, 0);
601 pid_synthesize_mmap_samples(pid, tgid);
7c6a1c65
PZ
602 } else
603 synthesize_all();
604
ef65b2a0 605 if (target_pid == -1 && argc) {
1a853e36
ACM
606 pid = fork();
607 if (pid < 0)
608 perror("failed to fork");
de9ac07b 609
1a853e36 610 if (!pid) {
0e9b20b8 611 if (execvp(argv[0], (char **)argv)) {
1a853e36
ACM
612 perror(argv[0]);
613 exit(-1);
614 }
de9ac07b 615 }
933da83a
CW
616
617 child_pid = pid;
de9ac07b
PZ
618 }
619
620 if (realtime_prio) {
621 struct sched_param param;
622
623 param.sched_priority = realtime_prio;
624 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
625 printf("Could not set realtime priority.\n");
626 exit(-1);
627 }
628 }
629
649c48a9 630 for (;;) {
2debbc83 631 int hits = samples;
de9ac07b 632
16c8a109 633 for (i = 0; i < nr_cpu; i++) {
ea57c4f5
IM
634 for (counter = 0; counter < nr_counters; counter++) {
635 if (mmap_array[i][counter].base)
636 mmap_read(&mmap_array[i][counter]);
637 }
de9ac07b
PZ
638 }
639
649c48a9
PZ
640 if (hits == samples) {
641 if (done)
642 break;
8b412664
PZ
643 ret = poll(event_array, nr_poll, -1);
644 waking++;
645 }
646
647 if (done) {
648 for (i = 0; i < nr_cpu; i++) {
649 for (counter = 0; counter < nr_counters; counter++)
cdd6c482 650 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
8b412664 651 }
649c48a9 652 }
de9ac07b
PZ
653 }
654
8b412664
PZ
655 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
656
021e9f47
IM
657 /*
658 * Approximate RIP event size: 24 bytes.
659 */
660 fprintf(stderr,
2debbc83 661 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
021e9f47
IM
662 (double)bytes_written / 1024.0 / 1024.0,
663 output_name,
664 bytes_written / 24);
addc2785 665
de9ac07b
PZ
666 return 0;
667}
0e9b20b8 668
0e9b20b8 669static const char * const record_usage[] = {
9e096753
MG
670 "perf record [<options>] [<command>]",
671 "perf record [<options>] -- <command> [<options>]",
0e9b20b8
IM
672 NULL
673};
674
5242519b 675static const struct option options[] = {
0e9b20b8 676 OPT_CALLBACK('e', "event", NULL, "event",
86847b62
TG
677 "event selector. use 'perf list' to list available events",
678 parse_events),
0e9b20b8
IM
679 OPT_INTEGER('p', "pid", &target_pid,
680 "record events on existing pid"),
681 OPT_INTEGER('r', "realtime", &realtime_prio,
682 "collect data with this RT SCHED_FIFO priority"),
daac07b2
FW
683 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
684 "collect raw sample records from all opened counters"),
0e9b20b8
IM
685 OPT_BOOLEAN('a', "all-cpus", &system_wide,
686 "system-wide collection from all CPUs"),
abaff32a
IM
687 OPT_BOOLEAN('A', "append", &append_file,
688 "append to the output file to do incremental profiling"),
0a5ac846
JA
689 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
690 "CPU to profile on"),
97124d5e
PZ
691 OPT_BOOLEAN('f', "force", &force,
692 "overwrite existing data file"),
e61078a0 693 OPT_LONG('c', "count", &default_interval,
abaff32a
IM
694 "event period to sample"),
695 OPT_STRING('o', "output", &output_name, "file",
696 "output file name"),
697 OPT_BOOLEAN('i', "inherit", &inherit,
698 "child tasks inherit counters"),
cf1f4574
IM
699 OPT_INTEGER('F', "freq", &freq,
700 "profile at this frequency"),
abaff32a
IM
701 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
702 "number of mmap data pages"),
3efa1cc9
IM
703 OPT_BOOLEAN('g', "call-graph", &call_graph,
704 "do call-graph (stack chain/backtrace) recording"),
3da297a6
IM
705 OPT_BOOLEAN('v', "verbose", &verbose,
706 "be more verbose (show counter open errors, etc)"),
649c48a9
PZ
707 OPT_BOOLEAN('s', "stat", &inherit_stat,
708 "per thread counts"),
4bba828d
AB
709 OPT_BOOLEAN('d', "data", &sample_address,
710 "Sample addresses"),
649c48a9
PZ
711 OPT_BOOLEAN('n', "no-samples", &no_samples,
712 "don't sample"),
d1302522
FW
713 OPT_BOOLEAN('M', "multiplex", &multiplex,
714 "multiplex counter output in a single channel"),
0e9b20b8
IM
715 OPT_END()
716};
717
f37a291c 718int cmd_record(int argc, const char **argv, const char *prefix __used)
0e9b20b8
IM
719{
720 int counter;
721
a0541234
AB
722 argc = parse_options(argc, argv, options, record_usage,
723 PARSE_OPT_STOP_AT_NON_OPTION);
ef65b2a0 724 if (!argc && target_pid == -1 && !system_wide)
0e9b20b8
IM
725 usage_with_options(record_usage, options);
726
bbd36e5e
PZ
727 if (!nr_counters) {
728 nr_counters = 1;
729 attrs[0].type = PERF_TYPE_HARDWARE;
730 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
731 }
0e9b20b8 732
7e4ff9e3
MG
733 /*
734 * User specified count overrides default frequency.
735 */
736 if (default_interval)
737 freq = 0;
738 else if (freq) {
739 default_interval = freq;
740 } else {
741 fprintf(stderr, "frequency and count are zero, aborting\n");
742 exit(EXIT_FAILURE);
743 }
744
0e9b20b8 745 for (counter = 0; counter < nr_counters; counter++) {
a21ca2ca 746 if (attrs[counter].sample_period)
0e9b20b8
IM
747 continue;
748
a21ca2ca 749 attrs[counter].sample_period = default_interval;
0e9b20b8
IM
750 }
751
752 return __cmd_record(argc, argv);
753}