blob: bf59df5bddf3651ff30a2cd69e098c9574cff2ae [file] [log] [blame]
Ingo Molnarabaff322009-06-02 22:59:57 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * 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.
Ingo Molnarabaff322009-06-02 22:59:57 +02007 */
Ingo Molnar16f762a2009-05-27 09:10:38 +02008#include "builtin.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +02009
10#include "perf.h"
11
Thomas Gleixner6eda5832009-05-01 18:29:57 +020012#include "util/util.h"
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020013#include "util/parse-options.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020014#include "util/parse-events.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030015#include "util/string.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020016
Peter Zijlstra97124d52009-06-02 15:52:24 +020017#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020018#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020019
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020020#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
21#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030022
Peter Zijlstrae61078a2009-06-03 11:24:33 +020023static long default_interval = 100000;
24static long event_count[MAX_COUNTERS];
Ingo Molnar8ad8db32009-05-26 11:10:09 +020025
Peter Zijlstrade9ac072009-04-08 15:01:31 +020026static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnar3cf165f2009-06-02 23:02:59 +020027static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020028static unsigned int page_size;
Ingo Molnar3cf165f2009-06-02 23:02:59 +020029static unsigned int mmap_pages = 128;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020030static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020031static const char *output_name = "perf.data";
Peter Zijlstrade9ac072009-04-08 15:01:31 +020032static int group = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020033static unsigned int realtime_prio = 0;
34static int system_wide = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030035static pid_t target_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020036static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020037static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020038static int append_file = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020039
40const unsigned int default_count[] = {
41 1000000,
42 1000000,
43 10000,
44 10000,
45 1000000,
46 10000,
47};
48
Peter Zijlstrade9ac072009-04-08 15:01:31 +020049struct mmap_data {
50 int counter;
51 void *base;
52 unsigned int mask;
53 unsigned int prev;
54};
55
56static unsigned int mmap_read_head(struct mmap_data *md)
57{
58 struct perf_counter_mmap_page *pc = md->base;
59 int head;
60
61 head = pc->data_head;
62 rmb();
63
64 return head;
65}
66
67static long events;
68static struct timeval last_read, this_read;
69
Ingo Molnar021e9f42009-06-03 19:27:19 +020070static __u64 bytes_written;
71
Peter Zijlstrade9ac072009-04-08 15:01:31 +020072static void mmap_read(struct mmap_data *md)
73{
74 unsigned int head = mmap_read_head(md);
75 unsigned int old = md->prev;
76 unsigned char *data = md->base + page_size;
77 unsigned long size;
78 void *buf;
79 int diff;
80
81 gettimeofday(&this_read, NULL);
82
83 /*
84 * If we're further behind than half the buffer, there's a chance
85 * the writer will bite our tail and screw up the events under us.
86 *
87 * If we somehow ended up ahead of the head, we got messed up.
88 *
89 * In either case, truncate and restart at head.
90 */
91 diff = head - old;
92 if (diff > md->mask / 2 || diff < 0) {
93 struct timeval iv;
94 unsigned long msecs;
95
96 timersub(&this_read, &last_read, &iv);
97 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
98
99 fprintf(stderr, "WARNING: failed to keep up with mmap data."
100 " Last read %lu msecs ago.\n", msecs);
101
102 /*
103 * head points to a known good entry, start there.
104 */
105 old = head;
106 }
107
108 last_read = this_read;
109
110 if (old != head)
111 events++;
112
113 size = head - old;
114
115 if ((old & md->mask) + size != (head & md->mask)) {
116 buf = &data[old & md->mask];
117 size = md->mask + 1 - (old & md->mask);
118 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200119
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200120 while (size) {
121 int ret = write(output, buf, size);
Ingo Molnar021e9f42009-06-03 19:27:19 +0200122
123 if (ret < 0)
124 die("failed to write");
125
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200126 size -= ret;
127 buf += ret;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200128
129 bytes_written += ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200130 }
131 }
132
133 buf = &data[old & md->mask];
134 size = head - old;
135 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200136
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200137 while (size) {
138 int ret = write(output, buf, size);
Ingo Molnar021e9f42009-06-03 19:27:19 +0200139
140 if (ret < 0)
141 die("failed to write");
142
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200143 size -= ret;
144 buf += ret;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200145
146 bytes_written += ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200147 }
148
149 md->prev = old;
150}
151
152static volatile int done = 0;
153
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200154static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200155{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200156 done = 1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200157}
158
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200159static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
160static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
161
162static int nr_poll;
163static int nr_cpu;
164
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300165struct mmap_event {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200166 struct perf_event_header header;
167 __u32 pid;
168 __u32 tid;
169 __u64 start;
170 __u64 len;
171 __u64 pgoff;
172 char filename[PATH_MAX];
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300173};
Ingo Molnar16f762a2009-05-27 09:10:38 +0200174
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300175struct comm_event {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200176 struct perf_event_header header;
177 __u32 pid;
178 __u32 tid;
179 char comm[16];
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300180};
181
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200182static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300183{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300184 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200185 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200186 char bf[BUFSIZ];
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300187 int fd, ret;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300188 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300189 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200190 DIR *tasks;
191 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300192
193 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
194
195 fd = open(filename, O_RDONLY);
196 if (fd < 0) {
197 fprintf(stderr, "couldn't open %s\n", filename);
198 exit(EXIT_FAILURE);
199 }
200 if (read(fd, bf, sizeof(bf)) < 0) {
201 fprintf(stderr, "couldn't read %s\n", filename);
202 exit(EXIT_FAILURE);
203 }
204 close(fd);
205
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300206 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300207 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300208 field = strchr(bf, '(');
209 if (field == NULL)
210 goto out_failure;
211 sep = strchr(++field, ')');
212 if (sep == NULL)
213 goto out_failure;
214 size = sep - field;
215 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200216
217 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300218 comm_ev.header.type = PERF_EVENT_COMM;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300219 size = ALIGN(size, sizeof(uint64_t));
220 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200221
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200222 if (!full) {
223 comm_ev.tid = pid;
224
225 ret = write(output, &comm_ev, comm_ev.header.size);
226 if (ret < 0) {
227 perror("failed to write");
228 exit(-1);
229 }
230 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300231 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200232
233 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
234
235 tasks = opendir(filename);
236 while (!readdir_r(tasks, &dirent, &next) && next) {
237 char *end;
238 pid = strtol(dirent.d_name, &end, 10);
239 if (*end)
240 continue;
241
242 comm_ev.tid = pid;
243
244 ret = write(output, &comm_ev, comm_ev.header.size);
245 if (ret < 0) {
246 perror("failed to write");
247 exit(-1);
248 }
249 }
250 closedir(tasks);
251 return;
252
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300253out_failure:
254 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
255 filename);
256 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300257}
258
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200259static void pid_synthesize_mmap_events(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300260{
261 char filename[PATH_MAX];
262 FILE *fp;
263
264 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
265
266 fp = fopen(filename, "r");
267 if (fp == NULL) {
268 fprintf(stderr, "couldn't open %s\n", filename);
269 exit(EXIT_FAILURE);
270 }
271 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300272 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300273 struct mmap_event mmap_ev = {
274 .header.type = PERF_EVENT_MMAP,
275 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300276 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300277 size_t size;
278 if (fgets(bf, sizeof(bf), fp) == NULL)
279 break;
280
281 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300282 n = hex2u64(pbf, &mmap_ev.start);
283 if (n < 0)
284 continue;
285 pbf += n + 1;
286 n = hex2u64(pbf, &mmap_ev.len);
287 if (n < 0)
288 continue;
289 pbf += n + 3;
290 if (*pbf == 'x') { /* vm_exec */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300291 char *execname = strrchr(bf, ' ');
292
293 if (execname == NULL || execname[1] != '/')
294 continue;
295
296 execname += 1;
297 size = strlen(execname);
298 execname[size - 1] = '\0'; /* Remove \n */
299 memcpy(mmap_ev.filename, execname, size);
300 size = ALIGN(size, sizeof(uint64_t));
301 mmap_ev.len -= mmap_ev.start;
302 mmap_ev.header.size = (sizeof(mmap_ev) -
303 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200304 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300305 mmap_ev.tid = pid;
306
307 if (write(output, &mmap_ev, mmap_ev.header.size) < 0) {
308 perror("failed to write");
309 exit(-1);
310 }
311 }
312 }
313
314 fclose(fp);
315}
316
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200317static void synthesize_events(void)
318{
319 DIR *proc;
320 struct dirent dirent, *next;
321
322 proc = opendir("/proc");
323
324 while (!readdir_r(proc, &dirent, &next) && next) {
325 char *end;
326 pid_t pid;
327
328 pid = strtol(dirent.d_name, &end, 10);
329 if (*end) /* only interested in proper numerical dirents */
330 continue;
331
332 pid_synthesize_comm_event(pid, 1);
333 pid_synthesize_mmap_events(pid);
334 }
335
336 closedir(proc);
337}
338
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300339static void open_counters(int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200340{
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200341 struct perf_counter_attr attr;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200342 int counter, group_fd;
343 int track = 1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200344
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300345 if (pid > 0) {
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200346 pid_synthesize_comm_event(pid, 0);
347 pid_synthesize_mmap_events(pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300348 }
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200349
350 group_fd = -1;
351 for (counter = 0; counter < nr_counters; counter++) {
352
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200353 memset(&attr, 0, sizeof(attr));
354 attr.config = event_id[counter];
355 attr.sample_period = event_count[counter];
356 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
357 attr.mmap = track;
358 attr.comm = track;
359 attr.inherit = (cpu < 0) && inherit;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200360
361 track = 0; // only the first counter needs these
362
363 fd[nr_cpu][counter] =
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200364 sys_perf_counter_open(&attr, pid, cpu, group_fd, 0);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200365
366 if (fd[nr_cpu][counter] < 0) {
367 int err = errno;
Ingo Molnarf2521b62009-06-03 19:17:25 +0200368
369 error("syscall returned with %d (%s)\n",
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200370 fd[nr_cpu][counter], strerror(err));
371 if (err == EPERM)
372 printf("Are you root?\n");
373 exit(-1);
374 }
375 assert(fd[nr_cpu][counter] >= 0);
376 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
377
378 /*
379 * First counter acts as the group leader:
380 */
381 if (group && group_fd == -1)
382 group_fd = fd[nr_cpu][counter];
383
384 event_array[nr_poll].fd = fd[nr_cpu][counter];
385 event_array[nr_poll].events = POLLIN;
386 nr_poll++;
387
388 mmap_array[nr_cpu][counter].counter = counter;
389 mmap_array[nr_cpu][counter].prev = 0;
390 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
391 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
392 PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
393 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
Ingo Molnarf2521b62009-06-03 19:17:25 +0200394 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200395 exit(-1);
396 }
397 }
398 nr_cpu++;
399}
400
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200401static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200402{
403 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200404 struct stat st;
Ingo Molnarabaff322009-06-02 22:59:57 +0200405 pid_t pid;
406 int flags;
407 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200408
409 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200410 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
411 assert(nr_cpus <= MAX_NR_CPUS);
412 assert(nr_cpus >= 0);
413
Ingo Molnarabaff322009-06-02 22:59:57 +0200414 if (!stat(output_name, &st) && !force && !append_file) {
415 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
Peter Zijlstra97124d52009-06-02 15:52:24 +0200416 output_name);
417 exit(-1);
418 }
419
Ingo Molnarabaff322009-06-02 22:59:57 +0200420 flags = O_CREAT|O_RDWR;
421 if (append_file)
422 flags |= O_APPEND;
423 else
424 flags |= O_TRUNC;
425
426 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200427 if (output < 0) {
428 perror("failed to create output file");
429 exit(-1);
430 }
431
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300432 if (!system_wide) {
Ingo Molnardf979922009-06-04 13:41:22 +0200433 open_counters(-1, target_pid != -1 ? target_pid : getpid());
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300434 } else for (i = 0; i < nr_cpus; i++)
435 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200436
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200437 signal(SIGCHLD, sig_handler);
438 signal(SIGINT, sig_handler);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200439
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200440 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300441 pid = fork();
442 if (pid < 0)
443 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200444
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300445 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200446 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300447 perror(argv[0]);
448 exit(-1);
449 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200450 }
451 }
452
453 if (realtime_prio) {
454 struct sched_param param;
455
456 param.sched_priority = realtime_prio;
457 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
458 printf("Could not set realtime priority.\n");
459 exit(-1);
460 }
461 }
462
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200463 if (system_wide)
464 synthesize_events();
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200465
466 while (!done) {
467 int hits = events;
468
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200469 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200470 for (counter = 0; counter < nr_counters; counter++)
471 mmap_read(&mmap_array[i][counter]);
472 }
473
474 if (hits == events)
475 ret = poll(event_array, nr_poll, 100);
476 }
477
Ingo Molnar021e9f42009-06-03 19:27:19 +0200478 /*
479 * Approximate RIP event size: 24 bytes.
480 */
481 fprintf(stderr,
482 "[ perf record: Captured and wrote %.3f MB %s (~%lld events) ]\n",
483 (double)bytes_written / 1024.0 / 1024.0,
484 output_name,
485 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200486
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200487 return 0;
488}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200489
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200490static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200491 "perf record [<options>] [<command>]",
492 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200493 NULL
494};
495
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200496static char events_help_msg[EVENTS_HELP_MAX];
497
Ingo Molnar52425192009-05-26 09:17:18 +0200498static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200499 OPT_CALLBACK('e', "event", NULL, "event",
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200500 events_help_msg, parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200501 OPT_INTEGER('p', "pid", &target_pid,
502 "record events on existing pid"),
503 OPT_INTEGER('r', "realtime", &realtime_prio,
504 "collect data with this RT SCHED_FIFO priority"),
505 OPT_BOOLEAN('a', "all-cpus", &system_wide,
506 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200507 OPT_BOOLEAN('A', "append", &append_file,
508 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200509 OPT_BOOLEAN('f', "force", &force,
510 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200511 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200512 "event period to sample"),
513 OPT_STRING('o', "output", &output_name, "file",
514 "output file name"),
515 OPT_BOOLEAN('i', "inherit", &inherit,
516 "child tasks inherit counters"),
517 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
518 "number of mmap data pages"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200519 OPT_END()
520};
521
522int cmd_record(int argc, const char **argv, const char *prefix)
523{
524 int counter;
525
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200526 create_events_help(events_help_msg);
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200527
528 argc = parse_options(argc, argv, options, record_usage, 0);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200529 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200530 usage_with_options(record_usage, options);
531
532 if (!nr_counters) {
533 nr_counters = 1;
534 event_id[0] = 0;
535 }
536
537 for (counter = 0; counter < nr_counters; counter++) {
538 if (event_count[counter])
539 continue;
540
541 event_count[counter] = default_interval;
542 }
543
544 return __cmd_record(argc, argv);
545}