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