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