blob: a177a591b52cdb377b7eee330be286d102d16fa5 [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 Zijlstrade9ac072009-04-08 15:01:31 +020023static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020024
25static long default_interval = 100000;
26
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;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020040static int call_graph = 0;
Ingo Molnar3da297a2009-06-07 17:39:02 +020041static int verbose = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020042
Ingo Molnara21ca2c2009-06-06 09:58:57 +020043static long samples;
44static struct timeval last_read;
45static struct timeval this_read;
46
47static __u64 bytes_written;
48
49static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
50
51static int nr_poll;
52static int nr_cpu;
53
54struct mmap_event {
55 struct perf_event_header header;
56 __u32 pid;
57 __u32 tid;
58 __u64 start;
59 __u64 len;
60 __u64 pgoff;
61 char filename[PATH_MAX];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020062};
63
Ingo Molnara21ca2c2009-06-06 09:58:57 +020064struct comm_event {
65 struct perf_event_header header;
66 __u32 pid;
67 __u32 tid;
68 char comm[16];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020069};
70
Ingo Molnara21ca2c2009-06-06 09:58:57 +020071
72struct mmap_data {
73 int counter;
74 void *base;
75 unsigned int mask;
76 unsigned int prev;
77};
78
79static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
80
Peter Zijlstrade9ac072009-04-08 15:01:31 +020081static unsigned int mmap_read_head(struct mmap_data *md)
82{
83 struct perf_counter_mmap_page *pc = md->base;
84 int head;
85
86 head = pc->data_head;
87 rmb();
88
89 return head;
90}
91
Peter Zijlstrade9ac072009-04-08 15:01:31 +020092static void mmap_read(struct mmap_data *md)
93{
94 unsigned int head = mmap_read_head(md);
95 unsigned int old = md->prev;
96 unsigned char *data = md->base + page_size;
97 unsigned long size;
98 void *buf;
99 int diff;
100
101 gettimeofday(&this_read, NULL);
102
103 /*
104 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200105 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200106 *
107 * If we somehow ended up ahead of the head, we got messed up.
108 *
109 * In either case, truncate and restart at head.
110 */
111 diff = head - old;
112 if (diff > md->mask / 2 || diff < 0) {
113 struct timeval iv;
114 unsigned long msecs;
115
116 timersub(&this_read, &last_read, &iv);
117 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
118
119 fprintf(stderr, "WARNING: failed to keep up with mmap data."
120 " Last read %lu msecs ago.\n", msecs);
121
122 /*
123 * head points to a known good entry, start there.
124 */
125 old = head;
126 }
127
128 last_read = this_read;
129
130 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200131 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200132
133 size = head - old;
134
135 if ((old & md->mask) + size != (head & md->mask)) {
136 buf = &data[old & md->mask];
137 size = md->mask + 1 - (old & md->mask);
138 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200139
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200140 while (size) {
141 int ret = write(output, buf, size);
Ingo Molnar021e9f42009-06-03 19:27:19 +0200142
143 if (ret < 0)
144 die("failed to write");
145
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200146 size -= ret;
147 buf += ret;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200148
149 bytes_written += ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200150 }
151 }
152
153 buf = &data[old & md->mask];
154 size = head - old;
155 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200156
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200157 while (size) {
158 int ret = write(output, buf, size);
Ingo Molnar021e9f42009-06-03 19:27:19 +0200159
160 if (ret < 0)
161 die("failed to write");
162
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200163 size -= ret;
164 buf += ret;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200165
166 bytes_written += ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200167 }
168
169 md->prev = old;
170}
171
172static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200173static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200174
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200175static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200176{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200177 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200178 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);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200188}
189
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200190static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300191{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300192 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200193 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200194 char bf[BUFSIZ];
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300195 int fd, ret;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300196 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300197 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200198 DIR *tasks;
199 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300200
201 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
202
203 fd = open(filename, O_RDONLY);
204 if (fd < 0) {
205 fprintf(stderr, "couldn't open %s\n", filename);
206 exit(EXIT_FAILURE);
207 }
208 if (read(fd, bf, sizeof(bf)) < 0) {
209 fprintf(stderr, "couldn't read %s\n", filename);
210 exit(EXIT_FAILURE);
211 }
212 close(fd);
213
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300214 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300215 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300216 field = strchr(bf, '(');
217 if (field == NULL)
218 goto out_failure;
219 sep = strchr(++field, ')');
220 if (sep == NULL)
221 goto out_failure;
222 size = sep - field;
223 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200224
225 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300226 comm_ev.header.type = PERF_EVENT_COMM;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200227 size = ALIGN(size, sizeof(__u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300228 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200229
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200230 if (!full) {
231 comm_ev.tid = pid;
232
233 ret = write(output, &comm_ev, comm_ev.header.size);
234 if (ret < 0) {
235 perror("failed to write");
236 exit(-1);
237 }
238 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300239 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200240
241 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
242
243 tasks = opendir(filename);
244 while (!readdir_r(tasks, &dirent, &next) && next) {
245 char *end;
246 pid = strtol(dirent.d_name, &end, 10);
247 if (*end)
248 continue;
249
250 comm_ev.tid = pid;
251
252 ret = write(output, &comm_ev, comm_ev.header.size);
253 if (ret < 0) {
254 perror("failed to write");
255 exit(-1);
256 }
257 }
258 closedir(tasks);
259 return;
260
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300261out_failure:
262 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
263 filename);
264 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300265}
266
Ingo Molnar2debbc82009-06-05 14:29:10 +0200267static void pid_synthesize_mmap_samples(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300268{
269 char filename[PATH_MAX];
270 FILE *fp;
271
272 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
273
274 fp = fopen(filename, "r");
275 if (fp == NULL) {
276 fprintf(stderr, "couldn't open %s\n", filename);
277 exit(EXIT_FAILURE);
278 }
279 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300280 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300281 struct mmap_event mmap_ev = {
282 .header.type = PERF_EVENT_MMAP,
283 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300284 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300285 size_t size;
286 if (fgets(bf, sizeof(bf), fp) == NULL)
287 break;
288
289 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300290 n = hex2u64(pbf, &mmap_ev.start);
291 if (n < 0)
292 continue;
293 pbf += n + 1;
294 n = hex2u64(pbf, &mmap_ev.len);
295 if (n < 0)
296 continue;
297 pbf += n + 3;
298 if (*pbf == 'x') { /* vm_exec */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300299 char *execname = strrchr(bf, ' ');
300
301 if (execname == NULL || execname[1] != '/')
302 continue;
303
304 execname += 1;
305 size = strlen(execname);
306 execname[size - 1] = '\0'; /* Remove \n */
307 memcpy(mmap_ev.filename, execname, size);
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200308 size = ALIGN(size, sizeof(__u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300309 mmap_ev.len -= mmap_ev.start;
310 mmap_ev.header.size = (sizeof(mmap_ev) -
311 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200312 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300313 mmap_ev.tid = pid;
314
315 if (write(output, &mmap_ev, mmap_ev.header.size) < 0) {
316 perror("failed to write");
317 exit(-1);
318 }
319 }
320 }
321
322 fclose(fp);
323}
324
Ingo Molnar2debbc82009-06-05 14:29:10 +0200325static void synthesize_samples(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200326{
327 DIR *proc;
328 struct dirent dirent, *next;
329
330 proc = opendir("/proc");
331
332 while (!readdir_r(proc, &dirent, &next) && next) {
333 char *end;
334 pid_t pid;
335
336 pid = strtol(dirent.d_name, &end, 10);
337 if (*end) /* only interested in proper numerical dirents */
338 continue;
339
340 pid_synthesize_comm_event(pid, 1);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200341 pid_synthesize_mmap_samples(pid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200342 }
343
344 closedir(proc);
345}
346
Ingo Molnarf250c0302009-06-05 13:18:41 +0200347static int group_fd;
348
349static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200350{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200351 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200352 int track = 1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200353
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200354 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200355
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200356 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200357 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200358 attr->freq = 1;
359 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200360 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200361
362 if (call_graph)
363 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
364
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200365 attr->mmap = track;
366 attr->comm = track;
367 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200368 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200369
370 track = 0; /* only the first counter needs these */
371
Ingo Molnar3da297a2009-06-07 17:39:02 +0200372try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200373 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200374
375 if (fd[nr_cpu][counter] < 0) {
376 int err = errno;
377
Ingo Molnarf250c0302009-06-05 13:18:41 +0200378 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200379 die("Permission error - are you root?\n");
380
381 /*
382 * If it's cycles then fall back to hrtimer
383 * based cpu-clock-tick sw counter, which
384 * is always available even if no PMU support:
385 */
386 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200387 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200388
389 if (verbose)
390 warning(" ... trying to fall back to cpu-clock-ticks\n");
391 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200392 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200393 goto try_again;
394 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200395 printf("\n");
396 error("perfcounter syscall returned with %d (%s)\n",
397 fd[nr_cpu][counter], strerror(err));
398 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200399 exit(-1);
400 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200401
Ingo Molnarf250c0302009-06-05 13:18:41 +0200402 assert(fd[nr_cpu][counter] >= 0);
403 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
404
405 /*
406 * First counter acts as the group leader:
407 */
408 if (group && group_fd == -1)
409 group_fd = fd[nr_cpu][counter];
410
411 event_array[nr_poll].fd = fd[nr_cpu][counter];
412 event_array[nr_poll].events = POLLIN;
413 nr_poll++;
414
415 mmap_array[nr_cpu][counter].counter = counter;
416 mmap_array[nr_cpu][counter].prev = 0;
417 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
418 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
419 PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
420 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
421 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
422 exit(-1);
423 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200424
425 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200426}
427
428static void open_counters(int cpu, pid_t pid)
429{
430 int counter;
431
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300432 if (pid > 0) {
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200433 pid_synthesize_comm_event(pid, 0);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200434 pid_synthesize_mmap_samples(pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300435 }
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200436
437 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200438 for (counter = 0; counter < nr_counters; counter++)
439 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200440
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200441 nr_cpu++;
442}
443
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200444static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200445{
446 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200447 struct stat st;
Ingo Molnarabaff322009-06-02 22:59:57 +0200448 pid_t pid;
449 int flags;
450 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200451
452 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200453 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
454 assert(nr_cpus <= MAX_NR_CPUS);
455 assert(nr_cpus >= 0);
456
Ingo Molnarabaff322009-06-02 22:59:57 +0200457 if (!stat(output_name, &st) && !force && !append_file) {
458 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
Peter Zijlstra97124d52009-06-02 15:52:24 +0200459 output_name);
460 exit(-1);
461 }
462
Ingo Molnarabaff322009-06-02 22:59:57 +0200463 flags = O_CREAT|O_RDWR;
464 if (append_file)
465 flags |= O_APPEND;
466 else
467 flags |= O_TRUNC;
468
469 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200470 if (output < 0) {
471 perror("failed to create output file");
472 exit(-1);
473 }
474
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300475 if (!system_wide) {
Ingo Molnardf979922009-06-04 13:41:22 +0200476 open_counters(-1, target_pid != -1 ? target_pid : getpid());
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300477 } else for (i = 0; i < nr_cpus; i++)
478 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200479
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200480 atexit(sig_atexit);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200481 signal(SIGCHLD, sig_handler);
482 signal(SIGINT, sig_handler);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200483
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200484 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300485 pid = fork();
486 if (pid < 0)
487 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200488
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300489 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200490 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300491 perror(argv[0]);
492 exit(-1);
493 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200494 }
495 }
496
497 if (realtime_prio) {
498 struct sched_param param;
499
500 param.sched_priority = realtime_prio;
501 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
502 printf("Could not set realtime priority.\n");
503 exit(-1);
504 }
505 }
506
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200507 if (system_wide)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200508 synthesize_samples();
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200509
510 while (!done) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200511 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200512
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200513 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200514 for (counter = 0; counter < nr_counters; counter++)
515 mmap_read(&mmap_array[i][counter]);
516 }
517
Ingo Molnar2debbc82009-06-05 14:29:10 +0200518 if (hits == samples)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200519 ret = poll(event_array, nr_poll, 100);
520 }
521
Ingo Molnar021e9f42009-06-03 19:27:19 +0200522 /*
523 * Approximate RIP event size: 24 bytes.
524 */
525 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200526 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200527 (double)bytes_written / 1024.0 / 1024.0,
528 output_name,
529 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200530
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200531 return 0;
532}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200533
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200534static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200535 "perf record [<options>] [<command>]",
536 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200537 NULL
538};
539
Ingo Molnar52425192009-05-26 09:17:18 +0200540static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200541 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200542 "event selector. use 'perf list' to list available events",
543 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200544 OPT_INTEGER('p', "pid", &target_pid,
545 "record events on existing pid"),
546 OPT_INTEGER('r', "realtime", &realtime_prio,
547 "collect data with this RT SCHED_FIFO priority"),
548 OPT_BOOLEAN('a', "all-cpus", &system_wide,
549 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200550 OPT_BOOLEAN('A', "append", &append_file,
551 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200552 OPT_BOOLEAN('f', "force", &force,
553 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200554 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200555 "event period to sample"),
556 OPT_STRING('o', "output", &output_name, "file",
557 "output file name"),
558 OPT_BOOLEAN('i', "inherit", &inherit,
559 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200560 OPT_INTEGER('F', "freq", &freq,
561 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200562 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
563 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200564 OPT_BOOLEAN('g', "call-graph", &call_graph,
565 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200566 OPT_BOOLEAN('v', "verbose", &verbose,
567 "be more verbose (show counter open errors, etc)"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200568 OPT_END()
569};
570
571int cmd_record(int argc, const char **argv, const char *prefix)
572{
573 int counter;
574
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200575 argc = parse_options(argc, argv, options, record_usage, 0);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200576 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200577 usage_with_options(record_usage, options);
578
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200579 if (!nr_counters) {
580 nr_counters = 1;
581 attrs[0].type = PERF_TYPE_HARDWARE;
582 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
583 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200584
585 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200586 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200587 continue;
588
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200589 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200590 }
591
592 return __cmd_record(argc, argv);
593}