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