blob: 06fdfb8b482805f2dc4e056c1075fd39b901e08f [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 Zijlstra9d91a6f2009-06-18 11:40:28 +020081static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020082{
83 struct perf_counter_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020084 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020085
86 head = pc->data_head;
87 rmb();
88
89 return head;
90}
91
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020092static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
93{
94 struct perf_counter_mmap_page *pc = md->base;
95
96 /*
97 * ensure all reads are done before we write the tail out.
98 */
99 /* mb(); */
100 pc->data_tail = tail;
101}
102
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200103static void mmap_read(struct mmap_data *md)
104{
105 unsigned int head = mmap_read_head(md);
106 unsigned int old = md->prev;
107 unsigned char *data = md->base + page_size;
108 unsigned long size;
109 void *buf;
110 int diff;
111
112 gettimeofday(&this_read, NULL);
113
114 /*
115 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200116 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200117 *
118 * If we somehow ended up ahead of the head, we got messed up.
119 *
120 * In either case, truncate and restart at head.
121 */
122 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200123 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200124 struct timeval iv;
125 unsigned long msecs;
126
127 timersub(&this_read, &last_read, &iv);
128 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
129
130 fprintf(stderr, "WARNING: failed to keep up with mmap data."
131 " Last read %lu msecs ago.\n", msecs);
132
133 /*
134 * head points to a known good entry, start there.
135 */
136 old = head;
137 }
138
139 last_read = this_read;
140
141 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200142 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200143
144 size = head - old;
145
146 if ((old & md->mask) + size != (head & md->mask)) {
147 buf = &data[old & md->mask];
148 size = md->mask + 1 - (old & md->mask);
149 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200150
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200151 while (size) {
152 int ret = write(output, buf, size);
Ingo Molnar021e9f42009-06-03 19:27:19 +0200153
154 if (ret < 0)
155 die("failed to write");
156
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200157 size -= ret;
158 buf += ret;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200159
160 bytes_written += ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200161 }
162 }
163
164 buf = &data[old & md->mask];
165 size = head - old;
166 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200167
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200168 while (size) {
169 int ret = write(output, buf, size);
Ingo Molnar021e9f42009-06-03 19:27:19 +0200170
171 if (ret < 0)
172 die("failed to write");
173
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200174 size -= ret;
175 buf += ret;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200176
177 bytes_written += ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200178 }
179
180 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200181 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200182}
183
184static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200185static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200186
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200187static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200188{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200189 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200190 signr = sig;
191}
192
193static void sig_atexit(void)
194{
195 if (signr == -1)
196 return;
197
198 signal(signr, SIG_DFL);
199 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200200}
201
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200202static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300203{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300204 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200205 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200206 char bf[BUFSIZ];
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300207 int fd, ret;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300208 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300209 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200210 DIR *tasks;
211 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300212
213 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
214
215 fd = open(filename, O_RDONLY);
216 if (fd < 0) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200217 /*
218 * We raced with a task exiting - just return:
219 */
220 if (verbose)
221 fprintf(stderr, "couldn't open %s\n", filename);
222 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300223 }
224 if (read(fd, bf, sizeof(bf)) < 0) {
225 fprintf(stderr, "couldn't read %s\n", filename);
226 exit(EXIT_FAILURE);
227 }
228 close(fd);
229
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300230 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300231 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300232 field = strchr(bf, '(');
233 if (field == NULL)
234 goto out_failure;
235 sep = strchr(++field, ')');
236 if (sep == NULL)
237 goto out_failure;
238 size = sep - field;
239 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200240
241 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300242 comm_ev.header.type = PERF_EVENT_COMM;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200243 size = ALIGN(size, sizeof(__u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300244 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200245
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200246 if (!full) {
247 comm_ev.tid = pid;
248
249 ret = write(output, &comm_ev, comm_ev.header.size);
250 if (ret < 0) {
251 perror("failed to write");
252 exit(-1);
253 }
254 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300255 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200256
257 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
258
259 tasks = opendir(filename);
260 while (!readdir_r(tasks, &dirent, &next) && next) {
261 char *end;
262 pid = strtol(dirent.d_name, &end, 10);
263 if (*end)
264 continue;
265
266 comm_ev.tid = pid;
267
268 ret = write(output, &comm_ev, comm_ev.header.size);
269 if (ret < 0) {
270 perror("failed to write");
271 exit(-1);
272 }
273 }
274 closedir(tasks);
275 return;
276
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300277out_failure:
278 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
279 filename);
280 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300281}
282
Ingo Molnar2debbc82009-06-05 14:29:10 +0200283static void pid_synthesize_mmap_samples(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300284{
285 char filename[PATH_MAX];
286 FILE *fp;
287
288 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
289
290 fp = fopen(filename, "r");
291 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200292 /*
293 * We raced with a task exiting - just return:
294 */
295 if (verbose)
296 fprintf(stderr, "couldn't open %s\n", filename);
297 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300298 }
299 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300300 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300301 struct mmap_event mmap_ev = {
302 .header.type = PERF_EVENT_MMAP,
303 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300304 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300305 size_t size;
306 if (fgets(bf, sizeof(bf), fp) == NULL)
307 break;
308
309 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300310 n = hex2u64(pbf, &mmap_ev.start);
311 if (n < 0)
312 continue;
313 pbf += n + 1;
314 n = hex2u64(pbf, &mmap_ev.len);
315 if (n < 0)
316 continue;
317 pbf += n + 3;
318 if (*pbf == 'x') { /* vm_exec */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300319 char *execname = strrchr(bf, ' ');
320
321 if (execname == NULL || execname[1] != '/')
322 continue;
323
324 execname += 1;
325 size = strlen(execname);
326 execname[size - 1] = '\0'; /* Remove \n */
327 memcpy(mmap_ev.filename, execname, size);
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200328 size = ALIGN(size, sizeof(__u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300329 mmap_ev.len -= mmap_ev.start;
330 mmap_ev.header.size = (sizeof(mmap_ev) -
331 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200332 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300333 mmap_ev.tid = pid;
334
335 if (write(output, &mmap_ev, mmap_ev.header.size) < 0) {
336 perror("failed to write");
337 exit(-1);
338 }
339 }
340 }
341
342 fclose(fp);
343}
344
Ingo Molnar2debbc82009-06-05 14:29:10 +0200345static void synthesize_samples(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200346{
347 DIR *proc;
348 struct dirent dirent, *next;
349
350 proc = opendir("/proc");
351
352 while (!readdir_r(proc, &dirent, &next) && next) {
353 char *end;
354 pid_t pid;
355
356 pid = strtol(dirent.d_name, &end, 10);
357 if (*end) /* only interested in proper numerical dirents */
358 continue;
359
360 pid_synthesize_comm_event(pid, 1);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200361 pid_synthesize_mmap_samples(pid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200362 }
363
364 closedir(proc);
365}
366
Ingo Molnarf250c0302009-06-05 13:18:41 +0200367static int group_fd;
368
369static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200370{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200371 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200372 int track = 1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200373
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200374 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200375
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200376 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200377 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200378 attr->freq = 1;
379 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200380 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200381
382 if (call_graph)
383 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
384
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200385 attr->mmap = track;
386 attr->comm = track;
387 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200388 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200389
390 track = 0; /* only the first counter needs these */
391
Ingo Molnar3da297a2009-06-07 17:39:02 +0200392try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200393 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200394
395 if (fd[nr_cpu][counter] < 0) {
396 int err = errno;
397
Ingo Molnarf250c0302009-06-05 13:18:41 +0200398 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200399 die("Permission error - are you root?\n");
400
401 /*
402 * If it's cycles then fall back to hrtimer
403 * based cpu-clock-tick sw counter, which
404 * is always available even if no PMU support:
405 */
406 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200407 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200408
409 if (verbose)
410 warning(" ... trying to fall back to cpu-clock-ticks\n");
411 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200412 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200413 goto try_again;
414 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200415 printf("\n");
416 error("perfcounter syscall returned with %d (%s)\n",
417 fd[nr_cpu][counter], strerror(err));
418 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200419 exit(-1);
420 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200421
Ingo Molnarf250c0302009-06-05 13:18:41 +0200422 assert(fd[nr_cpu][counter] >= 0);
423 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
424
425 /*
426 * First counter acts as the group leader:
427 */
428 if (group && group_fd == -1)
429 group_fd = fd[nr_cpu][counter];
430
431 event_array[nr_poll].fd = fd[nr_cpu][counter];
432 event_array[nr_poll].events = POLLIN;
433 nr_poll++;
434
435 mmap_array[nr_cpu][counter].counter = counter;
436 mmap_array[nr_cpu][counter].prev = 0;
437 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
438 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200439 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200440 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
441 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
442 exit(-1);
443 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200444
445 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200446}
447
448static void open_counters(int cpu, pid_t pid)
449{
450 int counter;
451
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300452 if (pid > 0) {
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200453 pid_synthesize_comm_event(pid, 0);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200454 pid_synthesize_mmap_samples(pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300455 }
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200456
457 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200458 for (counter = 0; counter < nr_counters; counter++)
459 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200460
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200461 nr_cpu++;
462}
463
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200464static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200465{
466 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200467 struct stat st;
Ingo Molnarabaff322009-06-02 22:59:57 +0200468 pid_t pid;
469 int flags;
470 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200471
472 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200473 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
474 assert(nr_cpus <= MAX_NR_CPUS);
475 assert(nr_cpus >= 0);
476
Ingo Molnarabaff322009-06-02 22:59:57 +0200477 if (!stat(output_name, &st) && !force && !append_file) {
478 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
Peter Zijlstra97124d52009-06-02 15:52:24 +0200479 output_name);
480 exit(-1);
481 }
482
Ingo Molnarabaff322009-06-02 22:59:57 +0200483 flags = O_CREAT|O_RDWR;
484 if (append_file)
485 flags |= O_APPEND;
486 else
487 flags |= O_TRUNC;
488
489 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200490 if (output < 0) {
491 perror("failed to create output file");
492 exit(-1);
493 }
494
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300495 if (!system_wide) {
Ingo Molnardf979922009-06-04 13:41:22 +0200496 open_counters(-1, target_pid != -1 ? target_pid : getpid());
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300497 } else for (i = 0; i < nr_cpus; i++)
498 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200499
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200500 atexit(sig_atexit);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200501 signal(SIGCHLD, sig_handler);
502 signal(SIGINT, sig_handler);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200503
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200504 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300505 pid = fork();
506 if (pid < 0)
507 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200508
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300509 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200510 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300511 perror(argv[0]);
512 exit(-1);
513 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200514 }
515 }
516
517 if (realtime_prio) {
518 struct sched_param param;
519
520 param.sched_priority = realtime_prio;
521 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
522 printf("Could not set realtime priority.\n");
523 exit(-1);
524 }
525 }
526
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200527 if (system_wide)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200528 synthesize_samples();
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200529
530 while (!done) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200531 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200532
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200533 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200534 for (counter = 0; counter < nr_counters; counter++)
535 mmap_read(&mmap_array[i][counter]);
536 }
537
Ingo Molnar2debbc82009-06-05 14:29:10 +0200538 if (hits == samples)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200539 ret = poll(event_array, nr_poll, 100);
540 }
541
Ingo Molnar021e9f42009-06-03 19:27:19 +0200542 /*
543 * Approximate RIP event size: 24 bytes.
544 */
545 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200546 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200547 (double)bytes_written / 1024.0 / 1024.0,
548 output_name,
549 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200550
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200551 return 0;
552}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200553
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200554static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200555 "perf record [<options>] [<command>]",
556 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200557 NULL
558};
559
Ingo Molnar52425192009-05-26 09:17:18 +0200560static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200561 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200562 "event selector. use 'perf list' to list available events",
563 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200564 OPT_INTEGER('p', "pid", &target_pid,
565 "record events on existing pid"),
566 OPT_INTEGER('r', "realtime", &realtime_prio,
567 "collect data with this RT SCHED_FIFO priority"),
568 OPT_BOOLEAN('a', "all-cpus", &system_wide,
569 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200570 OPT_BOOLEAN('A', "append", &append_file,
571 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200572 OPT_BOOLEAN('f', "force", &force,
573 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200574 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200575 "event period to sample"),
576 OPT_STRING('o', "output", &output_name, "file",
577 "output file name"),
578 OPT_BOOLEAN('i', "inherit", &inherit,
579 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200580 OPT_INTEGER('F', "freq", &freq,
581 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200582 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
583 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200584 OPT_BOOLEAN('g', "call-graph", &call_graph,
585 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200586 OPT_BOOLEAN('v', "verbose", &verbose,
587 "be more verbose (show counter open errors, etc)"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200588 OPT_END()
589};
590
591int cmd_record(int argc, const char **argv, const char *prefix)
592{
593 int counter;
594
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200595 argc = parse_options(argc, argv, options, record_usage, 0);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200596 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200597 usage_with_options(record_usage, options);
598
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200599 if (!nr_counters) {
600 nr_counters = 1;
601 attrs[0].type = PERF_TYPE_HARDWARE;
602 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
603 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200604
605 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200606 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200607 continue;
608
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200609 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200610 }
611
612 return __cmd_record(argc, argv);
613}