blob: 43ddab31ac399c894e27fdf3bb057ad7f7d3ed55 [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;
Ingo Molnarcf1f4572009-06-05 13:27:02 +020030static int freq = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020031static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020032static const char *output_name = "perf.data";
Peter Zijlstrade9ac072009-04-08 15:01:31 +020033static int group = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020034static unsigned int realtime_prio = 0;
35static int system_wide = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030036static pid_t target_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020037static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020038static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020039static int append_file = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020040
41const unsigned int default_count[] = {
42 1000000,
43 1000000,
44 10000,
45 10000,
46 1000000,
47 10000,
48};
49
Peter Zijlstrade9ac072009-04-08 15:01:31 +020050struct mmap_data {
51 int counter;
52 void *base;
53 unsigned int mask;
54 unsigned int prev;
55};
56
57static unsigned int mmap_read_head(struct mmap_data *md)
58{
59 struct perf_counter_mmap_page *pc = md->base;
60 int head;
61
62 head = pc->data_head;
63 rmb();
64
65 return head;
66}
67
Ingo Molnar2debbc82009-06-05 14:29:10 +020068static long samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020069static struct timeval last_read, this_read;
70
Ingo Molnar021e9f42009-06-03 19:27:19 +020071static __u64 bytes_written;
72
Peter Zijlstrade9ac072009-04-08 15:01:31 +020073static void mmap_read(struct mmap_data *md)
74{
75 unsigned int head = mmap_read_head(md);
76 unsigned int old = md->prev;
77 unsigned char *data = md->base + page_size;
78 unsigned long size;
79 void *buf;
80 int diff;
81
82 gettimeofday(&this_read, NULL);
83
84 /*
85 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +020086 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +020087 *
88 * If we somehow ended up ahead of the head, we got messed up.
89 *
90 * In either case, truncate and restart at head.
91 */
92 diff = head - old;
93 if (diff > md->mask / 2 || diff < 0) {
94 struct timeval iv;
95 unsigned long msecs;
96
97 timersub(&this_read, &last_read, &iv);
98 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
99
100 fprintf(stderr, "WARNING: failed to keep up with mmap data."
101 " Last read %lu msecs ago.\n", msecs);
102
103 /*
104 * head points to a known good entry, start there.
105 */
106 old = head;
107 }
108
109 last_read = this_read;
110
111 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200112 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200113
114 size = head - old;
115
116 if ((old & md->mask) + size != (head & md->mask)) {
117 buf = &data[old & md->mask];
118 size = md->mask + 1 - (old & md->mask);
119 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200120
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200121 while (size) {
122 int ret = write(output, buf, size);
Ingo Molnar021e9f42009-06-03 19:27:19 +0200123
124 if (ret < 0)
125 die("failed to write");
126
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200127 size -= ret;
128 buf += ret;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200129
130 bytes_written += ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200131 }
132 }
133
134 buf = &data[old & md->mask];
135 size = head - old;
136 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200137
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200138 while (size) {
139 int ret = write(output, buf, size);
Ingo Molnar021e9f42009-06-03 19:27:19 +0200140
141 if (ret < 0)
142 die("failed to write");
143
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200144 size -= ret;
145 buf += ret;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200146
147 bytes_written += ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200148 }
149
150 md->prev = old;
151}
152
153static volatile int done = 0;
154
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200155static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200156{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200157 done = 1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200158}
159
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200160static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
161static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
162
163static int nr_poll;
164static int nr_cpu;
165
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300166struct mmap_event {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200167 struct perf_event_header header;
168 __u32 pid;
169 __u32 tid;
170 __u64 start;
171 __u64 len;
172 __u64 pgoff;
173 char filename[PATH_MAX];
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300174};
Ingo Molnar16f762a2009-05-27 09:10:38 +0200175
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300176struct comm_event {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200177 struct perf_event_header header;
178 __u32 pid;
179 __u32 tid;
180 char comm[16];
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300181};
182
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200183static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300184{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300185 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200186 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200187 char bf[BUFSIZ];
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300188 int fd, ret;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300189 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300190 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200191 DIR *tasks;
192 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300193
194 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
195
196 fd = open(filename, O_RDONLY);
197 if (fd < 0) {
198 fprintf(stderr, "couldn't open %s\n", filename);
199 exit(EXIT_FAILURE);
200 }
201 if (read(fd, bf, sizeof(bf)) < 0) {
202 fprintf(stderr, "couldn't read %s\n", filename);
203 exit(EXIT_FAILURE);
204 }
205 close(fd);
206
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300207 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300208 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300209 field = strchr(bf, '(');
210 if (field == NULL)
211 goto out_failure;
212 sep = strchr(++field, ')');
213 if (sep == NULL)
214 goto out_failure;
215 size = sep - field;
216 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200217
218 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300219 comm_ev.header.type = PERF_EVENT_COMM;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300220 size = ALIGN(size, sizeof(uint64_t));
221 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200222
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200223 if (!full) {
224 comm_ev.tid = pid;
225
226 ret = write(output, &comm_ev, comm_ev.header.size);
227 if (ret < 0) {
228 perror("failed to write");
229 exit(-1);
230 }
231 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300232 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200233
234 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
235
236 tasks = opendir(filename);
237 while (!readdir_r(tasks, &dirent, &next) && next) {
238 char *end;
239 pid = strtol(dirent.d_name, &end, 10);
240 if (*end)
241 continue;
242
243 comm_ev.tid = pid;
244
245 ret = write(output, &comm_ev, comm_ev.header.size);
246 if (ret < 0) {
247 perror("failed to write");
248 exit(-1);
249 }
250 }
251 closedir(tasks);
252 return;
253
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300254out_failure:
255 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
256 filename);
257 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300258}
259
Ingo Molnar2debbc82009-06-05 14:29:10 +0200260static void pid_synthesize_mmap_samples(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300261{
262 char filename[PATH_MAX];
263 FILE *fp;
264
265 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
266
267 fp = fopen(filename, "r");
268 if (fp == NULL) {
269 fprintf(stderr, "couldn't open %s\n", filename);
270 exit(EXIT_FAILURE);
271 }
272 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300273 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300274 struct mmap_event mmap_ev = {
275 .header.type = PERF_EVENT_MMAP,
276 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300277 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300278 size_t size;
279 if (fgets(bf, sizeof(bf), fp) == NULL)
280 break;
281
282 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300283 n = hex2u64(pbf, &mmap_ev.start);
284 if (n < 0)
285 continue;
286 pbf += n + 1;
287 n = hex2u64(pbf, &mmap_ev.len);
288 if (n < 0)
289 continue;
290 pbf += n + 3;
291 if (*pbf == 'x') { /* vm_exec */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300292 char *execname = strrchr(bf, ' ');
293
294 if (execname == NULL || execname[1] != '/')
295 continue;
296
297 execname += 1;
298 size = strlen(execname);
299 execname[size - 1] = '\0'; /* Remove \n */
300 memcpy(mmap_ev.filename, execname, size);
301 size = ALIGN(size, sizeof(uint64_t));
302 mmap_ev.len -= mmap_ev.start;
303 mmap_ev.header.size = (sizeof(mmap_ev) -
304 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200305 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300306 mmap_ev.tid = pid;
307
308 if (write(output, &mmap_ev, mmap_ev.header.size) < 0) {
309 perror("failed to write");
310 exit(-1);
311 }
312 }
313 }
314
315 fclose(fp);
316}
317
Ingo Molnar2debbc82009-06-05 14:29:10 +0200318static void synthesize_samples(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200319{
320 DIR *proc;
321 struct dirent dirent, *next;
322
323 proc = opendir("/proc");
324
325 while (!readdir_r(proc, &dirent, &next) && next) {
326 char *end;
327 pid_t pid;
328
329 pid = strtol(dirent.d_name, &end, 10);
330 if (*end) /* only interested in proper numerical dirents */
331 continue;
332
333 pid_synthesize_comm_event(pid, 1);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200334 pid_synthesize_mmap_samples(pid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200335 }
336
337 closedir(proc);
338}
339
Ingo Molnarf250c0302009-06-05 13:18:41 +0200340static int group_fd;
341
342static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200343{
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200344 struct perf_counter_attr attr;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200345 int track = 1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200346
Ingo Molnarf250c0302009-06-05 13:18:41 +0200347 memset(&attr, 0, sizeof(attr));
348 attr.config = event_id[counter];
349 attr.sample_period = event_count[counter];
Ingo Molnarb2fef072009-06-05 18:07:51 +0200350 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD;
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200351 attr.freq = freq;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200352 attr.mmap = track;
353 attr.comm = track;
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200354 attr.inherit = (cpu < 0) && inherit;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200355
356 track = 0; /* only the first counter needs these */
357
358 fd[nr_cpu][counter] = sys_perf_counter_open(&attr, pid, cpu, group_fd, 0);
359
360 if (fd[nr_cpu][counter] < 0) {
361 int err = errno;
362
363 error("syscall returned with %d (%s)\n",
364 fd[nr_cpu][counter], strerror(err));
365 if (err == EPERM)
366 printf("Are you root?\n");
367 exit(-1);
368 }
369 assert(fd[nr_cpu][counter] >= 0);
370 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
371
372 /*
373 * First counter acts as the group leader:
374 */
375 if (group && group_fd == -1)
376 group_fd = fd[nr_cpu][counter];
377
378 event_array[nr_poll].fd = fd[nr_cpu][counter];
379 event_array[nr_poll].events = POLLIN;
380 nr_poll++;
381
382 mmap_array[nr_cpu][counter].counter = counter;
383 mmap_array[nr_cpu][counter].prev = 0;
384 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
385 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
386 PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
387 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
388 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
389 exit(-1);
390 }
391}
392
393static void open_counters(int cpu, pid_t pid)
394{
395 int counter;
396
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300397 if (pid > 0) {
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200398 pid_synthesize_comm_event(pid, 0);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200399 pid_synthesize_mmap_samples(pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300400 }
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200401
402 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200403 for (counter = 0; counter < nr_counters; counter++)
404 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200405
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200406 nr_cpu++;
407}
408
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200409static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200410{
411 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200412 struct stat st;
Ingo Molnarabaff322009-06-02 22:59:57 +0200413 pid_t pid;
414 int flags;
415 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200416
417 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200418 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
419 assert(nr_cpus <= MAX_NR_CPUS);
420 assert(nr_cpus >= 0);
421
Ingo Molnarabaff322009-06-02 22:59:57 +0200422 if (!stat(output_name, &st) && !force && !append_file) {
423 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
Peter Zijlstra97124d52009-06-02 15:52:24 +0200424 output_name);
425 exit(-1);
426 }
427
Ingo Molnarabaff322009-06-02 22:59:57 +0200428 flags = O_CREAT|O_RDWR;
429 if (append_file)
430 flags |= O_APPEND;
431 else
432 flags |= O_TRUNC;
433
434 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200435 if (output < 0) {
436 perror("failed to create output file");
437 exit(-1);
438 }
439
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300440 if (!system_wide) {
Ingo Molnardf979922009-06-04 13:41:22 +0200441 open_counters(-1, target_pid != -1 ? target_pid : getpid());
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300442 } else for (i = 0; i < nr_cpus; i++)
443 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200444
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200445 signal(SIGCHLD, sig_handler);
446 signal(SIGINT, sig_handler);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200447
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200448 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300449 pid = fork();
450 if (pid < 0)
451 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200452
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300453 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200454 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300455 perror(argv[0]);
456 exit(-1);
457 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200458 }
459 }
460
461 if (realtime_prio) {
462 struct sched_param param;
463
464 param.sched_priority = realtime_prio;
465 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
466 printf("Could not set realtime priority.\n");
467 exit(-1);
468 }
469 }
470
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200471 if (system_wide)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200472 synthesize_samples();
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200473
474 while (!done) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200475 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200476
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200477 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200478 for (counter = 0; counter < nr_counters; counter++)
479 mmap_read(&mmap_array[i][counter]);
480 }
481
Ingo Molnar2debbc82009-06-05 14:29:10 +0200482 if (hits == samples)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200483 ret = poll(event_array, nr_poll, 100);
484 }
485
Ingo Molnar021e9f42009-06-03 19:27:19 +0200486 /*
487 * Approximate RIP event size: 24 bytes.
488 */
489 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200490 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200491 (double)bytes_written / 1024.0 / 1024.0,
492 output_name,
493 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200494
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200495 return 0;
496}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200497
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200498static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200499 "perf record [<options>] [<command>]",
500 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200501 NULL
502};
503
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200504static char events_help_msg[EVENTS_HELP_MAX];
505
Ingo Molnar52425192009-05-26 09:17:18 +0200506static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200507 OPT_CALLBACK('e', "event", NULL, "event",
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200508 events_help_msg, parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200509 OPT_INTEGER('p', "pid", &target_pid,
510 "record events on existing pid"),
511 OPT_INTEGER('r', "realtime", &realtime_prio,
512 "collect data with this RT SCHED_FIFO priority"),
513 OPT_BOOLEAN('a', "all-cpus", &system_wide,
514 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200515 OPT_BOOLEAN('A', "append", &append_file,
516 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200517 OPT_BOOLEAN('f', "force", &force,
518 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200519 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200520 "event period to sample"),
521 OPT_STRING('o', "output", &output_name, "file",
522 "output file name"),
523 OPT_BOOLEAN('i', "inherit", &inherit,
524 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200525 OPT_INTEGER('F', "freq", &freq,
526 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200527 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
528 "number of mmap data pages"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200529 OPT_END()
530};
531
532int cmd_record(int argc, const char **argv, const char *prefix)
533{
534 int counter;
535
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200536 create_events_help(events_help_msg);
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200537
538 argc = parse_options(argc, argv, options, record_usage, 0);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200539 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200540 usage_with_options(record_usage, options);
541
542 if (!nr_counters) {
543 nr_counters = 1;
544 event_id[0] = 0;
545 }
546
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200547 if (freq) {
548 default_interval = freq;
549 freq = 1;
550 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200551 for (counter = 0; counter < nr_counters; counter++) {
552 if (event_count[counter])
553 continue;
554
555 event_count[counter] = default_interval;
556 }
557
558 return __cmd_record(argc, argv);
559}