blob: d18546f37d7c5661cfa0d6d55c7d921124461d00 [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 Zijlstra7c6a1c62009-06-25 17:05:54 +020017#include "util/header.h"
18
Peter Zijlstra97124d52009-06-02 15:52:24 +020019#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020020#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020021
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020022#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
23#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030024
Peter Zijlstrade9ac072009-04-08 15:01:31 +020025static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020026
27static long default_interval = 100000;
28
Ingo Molnar3cf165f2009-06-02 23:02:59 +020029static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020030static unsigned int page_size;
Ingo Molnar3cf165f2009-06-02 23:02:59 +020031static unsigned int mmap_pages = 128;
Ingo Molnarcf1f4572009-06-05 13:27:02 +020032static int freq = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020033static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020034static const char *output_name = "perf.data";
Peter Zijlstrade9ac072009-04-08 15:01:31 +020035static int group = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020036static unsigned int realtime_prio = 0;
37static int system_wide = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030038static pid_t target_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020039static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020040static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020041static int append_file = 0;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020042static int call_graph = 0;
Ingo Molnar3da297a2009-06-07 17:39:02 +020043static int verbose = 0;
Peter Zijlstra649c48a2009-06-24 21:12:48 +020044static int inherit_stat = 0;
45static int no_samples = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020046
Ingo Molnara21ca2c2009-06-06 09:58:57 +020047static long samples;
48static struct timeval last_read;
49static struct timeval this_read;
50
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100051static u64 bytes_written;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020052
53static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
54
55static int nr_poll;
56static int nr_cpu;
57
Peter Zijlstraf5970552009-06-18 23:22:55 +020058static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059
60struct perf_header *header;
Peter Zijlstraf5970552009-06-18 23:22:55 +020061
Ingo Molnara21ca2c2009-06-06 09:58:57 +020062struct mmap_event {
63 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100064 u32 pid;
65 u32 tid;
66 u64 start;
67 u64 len;
68 u64 pgoff;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020069 char filename[PATH_MAX];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020070};
71
Ingo Molnara21ca2c2009-06-06 09:58:57 +020072struct comm_event {
73 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100074 u32 pid;
75 u32 tid;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020076 char comm[16];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020077};
78
Ingo Molnara21ca2c2009-06-06 09:58:57 +020079
80struct mmap_data {
81 int counter;
82 void *base;
83 unsigned int mask;
84 unsigned int prev;
85};
86
87static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
88
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020089static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020090{
91 struct perf_counter_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020092 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020093
94 head = pc->data_head;
95 rmb();
96
97 return head;
98}
99
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200100static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
101{
102 struct perf_counter_mmap_page *pc = md->base;
103
104 /*
105 * ensure all reads are done before we write the tail out.
106 */
107 /* mb(); */
108 pc->data_tail = tail;
109}
110
Peter Zijlstraf5970552009-06-18 23:22:55 +0200111static void write_output(void *buf, size_t size)
112{
113 while (size) {
114 int ret = write(output, buf, size);
115
116 if (ret < 0)
117 die("failed to write");
118
119 size -= ret;
120 buf += ret;
121
122 bytes_written += ret;
123 }
124}
125
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200126static void mmap_read(struct mmap_data *md)
127{
128 unsigned int head = mmap_read_head(md);
129 unsigned int old = md->prev;
130 unsigned char *data = md->base + page_size;
131 unsigned long size;
132 void *buf;
133 int diff;
134
135 gettimeofday(&this_read, NULL);
136
137 /*
138 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200139 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200140 *
141 * If we somehow ended up ahead of the head, we got messed up.
142 *
143 * In either case, truncate and restart at head.
144 */
145 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200146 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200147 struct timeval iv;
148 unsigned long msecs;
149
150 timersub(&this_read, &last_read, &iv);
151 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
152
153 fprintf(stderr, "WARNING: failed to keep up with mmap data."
154 " Last read %lu msecs ago.\n", msecs);
155
156 /*
157 * head points to a known good entry, start there.
158 */
159 old = head;
160 }
161
162 last_read = this_read;
163
164 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200165 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200166
167 size = head - old;
168
169 if ((old & md->mask) + size != (head & md->mask)) {
170 buf = &data[old & md->mask];
171 size = md->mask + 1 - (old & md->mask);
172 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200173
Peter Zijlstraf5970552009-06-18 23:22:55 +0200174 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200175 }
176
177 buf = &data[old & md->mask];
178 size = head - old;
179 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200180
Peter Zijlstraf5970552009-06-18 23:22:55 +0200181 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200182
183 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200184 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200185}
186
187static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200188static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200189
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200190static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200191{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200192 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200193 signr = sig;
194}
195
196static void sig_atexit(void)
197{
198 if (signr == -1)
199 return;
200
201 signal(signr, SIG_DFL);
202 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200203}
204
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200205static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300206{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300207 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200208 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200209 char bf[BUFSIZ];
Peter Zijlstraf5970552009-06-18 23:22:55 +0200210 int fd;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300211 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300212 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200213 DIR *tasks;
214 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300215
216 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
217
218 fd = open(filename, O_RDONLY);
219 if (fd < 0) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200220 /*
221 * We raced with a task exiting - just return:
222 */
223 if (verbose)
224 fprintf(stderr, "couldn't open %s\n", filename);
225 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300226 }
227 if (read(fd, bf, sizeof(bf)) < 0) {
228 fprintf(stderr, "couldn't read %s\n", filename);
229 exit(EXIT_FAILURE);
230 }
231 close(fd);
232
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300233 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300234 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300235 field = strchr(bf, '(');
236 if (field == NULL)
237 goto out_failure;
238 sep = strchr(++field, ')');
239 if (sep == NULL)
240 goto out_failure;
241 size = sep - field;
242 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200243
244 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300245 comm_ev.header.type = PERF_EVENT_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000246 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300247 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200248
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200249 if (!full) {
250 comm_ev.tid = pid;
251
Peter Zijlstraf5970552009-06-18 23:22:55 +0200252 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200253 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300254 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200255
256 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
257
258 tasks = opendir(filename);
259 while (!readdir_r(tasks, &dirent, &next) && next) {
260 char *end;
261 pid = strtol(dirent.d_name, &end, 10);
262 if (*end)
263 continue;
264
265 comm_ev.tid = pid;
266
Peter Zijlstraf5970552009-06-18 23:22:55 +0200267 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200268 }
269 closedir(tasks);
270 return;
271
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300272out_failure:
273 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
274 filename);
275 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300276}
277
Ingo Molnar2debbc82009-06-05 14:29:10 +0200278static void pid_synthesize_mmap_samples(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300279{
280 char filename[PATH_MAX];
281 FILE *fp;
282
283 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
284
285 fp = fopen(filename, "r");
286 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200287 /*
288 * We raced with a task exiting - just return:
289 */
290 if (verbose)
291 fprintf(stderr, "couldn't open %s\n", filename);
292 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300293 }
294 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300295 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300296 struct mmap_event mmap_ev = {
297 .header.type = PERF_EVENT_MMAP,
298 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300299 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300300 size_t size;
301 if (fgets(bf, sizeof(bf), fp) == NULL)
302 break;
303
304 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300305 n = hex2u64(pbf, &mmap_ev.start);
306 if (n < 0)
307 continue;
308 pbf += n + 1;
309 n = hex2u64(pbf, &mmap_ev.len);
310 if (n < 0)
311 continue;
312 pbf += n + 3;
313 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200314 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300315
Johannes Weiner76c64c52009-06-24 21:08:36 +0200316 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300317 continue;
318
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300319 size = strlen(execname);
320 execname[size - 1] = '\0'; /* Remove \n */
321 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000322 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300323 mmap_ev.len -= mmap_ev.start;
324 mmap_ev.header.size = (sizeof(mmap_ev) -
325 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200326 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300327 mmap_ev.tid = pid;
328
Peter Zijlstraf5970552009-06-18 23:22:55 +0200329 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300330 }
331 }
332
333 fclose(fp);
334}
335
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200336static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200337{
338 DIR *proc;
339 struct dirent dirent, *next;
340
341 proc = opendir("/proc");
342
343 while (!readdir_r(proc, &dirent, &next) && next) {
344 char *end;
345 pid_t pid;
346
347 pid = strtol(dirent.d_name, &end, 10);
348 if (*end) /* only interested in proper numerical dirents */
349 continue;
350
351 pid_synthesize_comm_event(pid, 1);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200352 pid_synthesize_mmap_samples(pid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200353 }
354
355 closedir(proc);
356}
357
Ingo Molnarf250c0302009-06-05 13:18:41 +0200358static int group_fd;
359
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200360static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
361{
362 struct perf_header_attr *h_attr;
363
364 if (nr < header->attrs) {
365 h_attr = header->attr[nr];
366 } else {
367 h_attr = perf_header_attr__new(a);
368 perf_header__add_attr(header, h_attr);
369 }
370
371 return h_attr;
372}
373
Ingo Molnarf250c0302009-06-05 13:18:41 +0200374static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200375{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200376 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200377 struct perf_header_attr *h_attr;
378 int track = !counter; /* only the first counter needs these */
379 struct {
380 u64 count;
381 u64 time_enabled;
382 u64 time_running;
383 u64 id;
384 } read_data;
385
386 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
387 PERF_FORMAT_TOTAL_TIME_RUNNING |
388 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200389
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200390 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200391
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200392 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200393 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200394 attr->freq = 1;
395 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200396 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200397
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200398 if (no_samples)
399 attr->sample_freq = 0;
400
401 if (inherit_stat)
402 attr->inherit_stat = 1;
403
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200404 if (call_graph)
405 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
406
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200407 attr->mmap = track;
408 attr->comm = track;
409 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200410 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200411
Ingo Molnar3da297a2009-06-07 17:39:02 +0200412try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200413 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200414
415 if (fd[nr_cpu][counter] < 0) {
416 int err = errno;
417
Ingo Molnarf250c0302009-06-05 13:18:41 +0200418 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200419 die("Permission error - are you root?\n");
420
421 /*
422 * If it's cycles then fall back to hrtimer
423 * based cpu-clock-tick sw counter, which
424 * is always available even if no PMU support:
425 */
426 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200427 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200428
429 if (verbose)
430 warning(" ... trying to fall back to cpu-clock-ticks\n");
431 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200432 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200433 goto try_again;
434 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200435 printf("\n");
436 error("perfcounter syscall returned with %d (%s)\n",
437 fd[nr_cpu][counter], strerror(err));
438 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200439 exit(-1);
440 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200441
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200442 h_attr = get_header_attr(attr, counter);
443
444 if (!file_new) {
445 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
446 fprintf(stderr, "incompatible append\n");
447 exit(-1);
448 }
449 }
450
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200451 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
452 perror("Unable to read perf file descriptor\n");
453 exit(-1);
454 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200455
456 perf_header_attr__add_id(h_attr, read_data.id);
457
Ingo Molnarf250c0302009-06-05 13:18:41 +0200458 assert(fd[nr_cpu][counter] >= 0);
459 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
460
461 /*
462 * First counter acts as the group leader:
463 */
464 if (group && group_fd == -1)
465 group_fd = fd[nr_cpu][counter];
466
467 event_array[nr_poll].fd = fd[nr_cpu][counter];
468 event_array[nr_poll].events = POLLIN;
469 nr_poll++;
470
471 mmap_array[nr_cpu][counter].counter = counter;
472 mmap_array[nr_cpu][counter].prev = 0;
473 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
474 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200475 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200476 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
477 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
478 exit(-1);
479 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200480
481 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200482}
483
484static void open_counters(int cpu, pid_t pid)
485{
486 int counter;
487
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200488 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200489 for (counter = 0; counter < nr_counters; counter++)
490 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200491
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200492 nr_cpu++;
493}
494
Peter Zijlstraf5970552009-06-18 23:22:55 +0200495static void atexit_header(void)
496{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200497 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200498
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200499 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200500}
501
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200502static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200503{
504 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200505 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200506 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200507 int flags;
508 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200509
510 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200511 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
512 assert(nr_cpus <= MAX_NR_CPUS);
513 assert(nr_cpus >= 0);
514
Peter Zijlstraf5970552009-06-18 23:22:55 +0200515 atexit(sig_atexit);
516 signal(SIGCHLD, sig_handler);
517 signal(SIGINT, sig_handler);
518
Ingo Molnarabaff322009-06-02 22:59:57 +0200519 if (!stat(output_name, &st) && !force && !append_file) {
520 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
Peter Zijlstra97124d52009-06-02 15:52:24 +0200521 output_name);
522 exit(-1);
523 }
524
Ingo Molnarabaff322009-06-02 22:59:57 +0200525 flags = O_CREAT|O_RDWR;
526 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200527 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200528 else
529 flags |= O_TRUNC;
530
531 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200532 if (output < 0) {
533 perror("failed to create output file");
534 exit(-1);
535 }
536
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200537 if (!file_new)
538 header = perf_header__read(output);
539 else
540 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200541
542 atexit(atexit_header);
543
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300544 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200545 pid = target_pid;
546 if (pid == -1)
547 pid = getpid();
548
549 open_counters(-1, pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300550 } else for (i = 0; i < nr_cpus; i++)
551 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200552
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200553 if (file_new)
554 perf_header__write(header, output);
555
556 if (!system_wide) {
557 pid_synthesize_comm_event(pid, 0);
558 pid_synthesize_mmap_samples(pid);
559 } else
560 synthesize_all();
561
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200562 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300563 pid = fork();
564 if (pid < 0)
565 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200566
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300567 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200568 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300569 perror(argv[0]);
570 exit(-1);
571 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200572 }
573 }
574
575 if (realtime_prio) {
576 struct sched_param param;
577
578 param.sched_priority = realtime_prio;
579 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
580 printf("Could not set realtime priority.\n");
581 exit(-1);
582 }
583 }
584
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200585 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200586 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200587
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200588 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200589 for (counter = 0; counter < nr_counters; counter++)
590 mmap_read(&mmap_array[i][counter]);
591 }
592
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200593 if (hits == samples) {
594 if (done)
595 break;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200596 ret = poll(event_array, nr_poll, 100);
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200597 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200598 }
599
Ingo Molnar021e9f42009-06-03 19:27:19 +0200600 /*
601 * Approximate RIP event size: 24 bytes.
602 */
603 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200604 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200605 (double)bytes_written / 1024.0 / 1024.0,
606 output_name,
607 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200608
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200609 return 0;
610}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200611
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200612static const char * const record_usage[] = {
Mike Galbraith9e096752009-05-28 16:25:34 +0200613 "perf record [<options>] [<command>]",
614 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200615 NULL
616};
617
Ingo Molnar52425192009-05-26 09:17:18 +0200618static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200619 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200620 "event selector. use 'perf list' to list available events",
621 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200622 OPT_INTEGER('p', "pid", &target_pid,
623 "record events on existing pid"),
624 OPT_INTEGER('r', "realtime", &realtime_prio,
625 "collect data with this RT SCHED_FIFO priority"),
626 OPT_BOOLEAN('a', "all-cpus", &system_wide,
627 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200628 OPT_BOOLEAN('A', "append", &append_file,
629 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200630 OPT_BOOLEAN('f', "force", &force,
631 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200632 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200633 "event period to sample"),
634 OPT_STRING('o', "output", &output_name, "file",
635 "output file name"),
636 OPT_BOOLEAN('i', "inherit", &inherit,
637 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200638 OPT_INTEGER('F', "freq", &freq,
639 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200640 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
641 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200642 OPT_BOOLEAN('g', "call-graph", &call_graph,
643 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200644 OPT_BOOLEAN('v', "verbose", &verbose,
645 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200646 OPT_BOOLEAN('s', "stat", &inherit_stat,
647 "per thread counts"),
648 OPT_BOOLEAN('n', "no-samples", &no_samples,
649 "don't sample"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200650 OPT_END()
651};
652
653int cmd_record(int argc, const char **argv, const char *prefix)
654{
655 int counter;
656
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200657 argc = parse_options(argc, argv, options, record_usage, 0);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200658 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200659 usage_with_options(record_usage, options);
660
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200661 if (!nr_counters) {
662 nr_counters = 1;
663 attrs[0].type = PERF_TYPE_HARDWARE;
664 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
665 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200666
667 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200668 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200669 continue;
670
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200671 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200672 }
673
674 return __cmd_record(argc, argv);
675}