blob: 89a5ddcd1ded753e6835c39935989d4c96ac2fdd [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;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +020037static int raw_samples = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020038static int system_wide = 0;
Jens Axboe0a5ac842009-08-12 11:18:01 +020039static int profile_cpu = -1;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030040static pid_t target_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020041static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020042static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020043static int append_file = 0;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020044static int call_graph = 0;
Ingo Molnar3da297a2009-06-07 17:39:02 +020045static int verbose = 0;
Peter Zijlstra649c48a2009-06-24 21:12:48 +020046static int inherit_stat = 0;
47static int no_samples = 0;
Anton Blanchard4bba8282009-07-16 15:44:29 +020048static int sample_address = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020049
Ingo Molnara21ca2c2009-06-06 09:58:57 +020050static long samples;
51static struct timeval last_read;
52static struct timeval this_read;
53
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100054static u64 bytes_written;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020055
56static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
57
58static int nr_poll;
59static int nr_cpu;
60
Peter Zijlstraf5970552009-06-18 23:22:55 +020061static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020062
63struct perf_header *header;
Peter Zijlstraf5970552009-06-18 23:22:55 +020064
Ingo Molnara21ca2c2009-06-06 09:58:57 +020065struct mmap_event {
66 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100067 u32 pid;
68 u32 tid;
69 u64 start;
70 u64 len;
71 u64 pgoff;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020072 char filename[PATH_MAX];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020073};
74
Ingo Molnara21ca2c2009-06-06 09:58:57 +020075struct comm_event {
76 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100077 u32 pid;
78 u32 tid;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020079 char comm[16];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020080};
81
Ingo Molnara21ca2c2009-06-06 09:58:57 +020082
83struct mmap_data {
84 int counter;
85 void *base;
86 unsigned int mask;
87 unsigned int prev;
88};
89
90static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
91
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020092static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020093{
94 struct perf_counter_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020095 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020096
97 head = pc->data_head;
98 rmb();
99
100 return head;
101}
102
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200103static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
104{
105 struct perf_counter_mmap_page *pc = md->base;
106
107 /*
108 * ensure all reads are done before we write the tail out.
109 */
110 /* mb(); */
111 pc->data_tail = tail;
112}
113
Peter Zijlstraf5970552009-06-18 23:22:55 +0200114static void write_output(void *buf, size_t size)
115{
116 while (size) {
117 int ret = write(output, buf, size);
118
119 if (ret < 0)
120 die("failed to write");
121
122 size -= ret;
123 buf += ret;
124
125 bytes_written += ret;
126 }
127}
128
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200129static void mmap_read(struct mmap_data *md)
130{
131 unsigned int head = mmap_read_head(md);
132 unsigned int old = md->prev;
133 unsigned char *data = md->base + page_size;
134 unsigned long size;
135 void *buf;
136 int diff;
137
138 gettimeofday(&this_read, NULL);
139
140 /*
141 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200142 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200143 *
144 * If we somehow ended up ahead of the head, we got messed up.
145 *
146 * In either case, truncate and restart at head.
147 */
148 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200149 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200150 struct timeval iv;
151 unsigned long msecs;
152
153 timersub(&this_read, &last_read, &iv);
154 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
155
156 fprintf(stderr, "WARNING: failed to keep up with mmap data."
157 " Last read %lu msecs ago.\n", msecs);
158
159 /*
160 * head points to a known good entry, start there.
161 */
162 old = head;
163 }
164
165 last_read = this_read;
166
167 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200168 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200169
170 size = head - old;
171
172 if ((old & md->mask) + size != (head & md->mask)) {
173 buf = &data[old & md->mask];
174 size = md->mask + 1 - (old & md->mask);
175 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200176
Peter Zijlstraf5970552009-06-18 23:22:55 +0200177 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200178 }
179
180 buf = &data[old & md->mask];
181 size = head - old;
182 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200183
Peter Zijlstraf5970552009-06-18 23:22:55 +0200184 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200185
186 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200187 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200188}
189
190static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200191static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200192
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200193static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200194{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200195 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200196 signr = sig;
197}
198
199static void sig_atexit(void)
200{
201 if (signr == -1)
202 return;
203
204 signal(signr, SIG_DFL);
205 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200206}
207
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300208static pid_t pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300209{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300210 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200211 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200212 char bf[BUFSIZ];
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300213 FILE *fp;
214 size_t size = 0;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200215 DIR *tasks;
216 struct dirent dirent, *next;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300217 pid_t tgid = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300218
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300219 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300220
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300221 fp = fopen(filename, "r");
Arnaldo Carvalho de Melo39e6dd72009-08-14 15:26:32 -0300222 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200223 /*
224 * We raced with a task exiting - just return:
225 */
226 if (verbose)
227 fprintf(stderr, "couldn't open %s\n", filename);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300228 return 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300229 }
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300230
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300231 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300232 while (!comm_ev.comm[0] || !comm_ev.pid) {
233 if (fgets(bf, sizeof(bf), fp) == NULL)
234 goto out_failure;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200235
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300236 if (memcmp(bf, "Name:", 5) == 0) {
237 char *name = bf + 5;
238 while (*name && isspace(*name))
239 ++name;
240 size = strlen(name) - 1;
241 memcpy(comm_ev.comm, name, size++);
242 } else if (memcmp(bf, "Tgid:", 5) == 0) {
243 char *tgids = bf + 5;
244 while (*tgids && isspace(*tgids))
245 ++tgids;
246 tgid = comm_ev.pid = atoi(tgids);
247 }
248 }
249
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300250 comm_ev.header.type = PERF_EVENT_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000251 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300252 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200253
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200254 if (!full) {
255 comm_ev.tid = pid;
256
Peter Zijlstraf5970552009-06-18 23:22:55 +0200257 write_output(&comm_ev, comm_ev.header.size);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300258 goto out_fclose;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300259 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200260
261 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
262
263 tasks = opendir(filename);
264 while (!readdir_r(tasks, &dirent, &next) && next) {
265 char *end;
266 pid = strtol(dirent.d_name, &end, 10);
267 if (*end)
268 continue;
269
270 comm_ev.tid = pid;
271
Peter Zijlstraf5970552009-06-18 23:22:55 +0200272 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200273 }
274 closedir(tasks);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300275
276out_fclose:
277 fclose(fp);
278 return tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200279
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300280out_failure:
281 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
282 filename);
283 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300284}
285
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300286static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300287{
288 char filename[PATH_MAX];
289 FILE *fp;
290
291 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
292
293 fp = fopen(filename, "r");
294 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200295 /*
296 * We raced with a task exiting - just return:
297 */
298 if (verbose)
299 fprintf(stderr, "couldn't open %s\n", filename);
300 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300301 }
302 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300303 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300304 struct mmap_event mmap_ev = {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200305 .header = { .type = PERF_EVENT_MMAP },
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300306 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300307 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300308 size_t size;
309 if (fgets(bf, sizeof(bf), fp) == NULL)
310 break;
311
312 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300313 n = hex2u64(pbf, &mmap_ev.start);
314 if (n < 0)
315 continue;
316 pbf += n + 1;
317 n = hex2u64(pbf, &mmap_ev.len);
318 if (n < 0)
319 continue;
320 pbf += n + 3;
321 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200322 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300323
Anton Blanchard11b5f812009-07-16 15:44:29 +0200324 /* Catch VDSO */
325 if (execname == NULL)
326 execname = strstr(bf, "[vdso]");
327
Johannes Weiner76c64c52009-06-24 21:08:36 +0200328 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300329 continue;
330
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300331 size = strlen(execname);
332 execname[size - 1] = '\0'; /* Remove \n */
333 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000334 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300335 mmap_ev.len -= mmap_ev.start;
336 mmap_ev.header.size = (sizeof(mmap_ev) -
337 (sizeof(mmap_ev.filename) - size));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300338 mmap_ev.pid = tgid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300339 mmap_ev.tid = pid;
340
Peter Zijlstraf5970552009-06-18 23:22:55 +0200341 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300342 }
343 }
344
345 fclose(fp);
346}
347
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200348static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200349{
350 DIR *proc;
351 struct dirent dirent, *next;
352
353 proc = opendir("/proc");
354
355 while (!readdir_r(proc, &dirent, &next) && next) {
356 char *end;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300357 pid_t pid, tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200358
359 pid = strtol(dirent.d_name, &end, 10);
360 if (*end) /* only interested in proper numerical dirents */
361 continue;
362
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300363 tgid = pid_synthesize_comm_event(pid, 1);
364 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200365 }
366
367 closedir(proc);
368}
369
Ingo Molnarf250c0302009-06-05 13:18:41 +0200370static int group_fd;
371
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200372static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
373{
374 struct perf_header_attr *h_attr;
375
376 if (nr < header->attrs) {
377 h_attr = header->attr[nr];
378 } else {
379 h_attr = perf_header_attr__new(a);
380 perf_header__add_attr(header, h_attr);
381 }
382
383 return h_attr;
384}
385
Ingo Molnarf250c0302009-06-05 13:18:41 +0200386static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200387{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200388 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200389 struct perf_header_attr *h_attr;
390 int track = !counter; /* only the first counter needs these */
391 struct {
392 u64 count;
393 u64 time_enabled;
394 u64 time_running;
395 u64 id;
396 } read_data;
397
398 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
399 PERF_FORMAT_TOTAL_TIME_RUNNING |
400 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200401
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200402 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200403
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200404 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200405 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200406 attr->freq = 1;
407 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200408 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200409
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200410 if (no_samples)
411 attr->sample_freq = 0;
412
413 if (inherit_stat)
414 attr->inherit_stat = 1;
415
Anton Blanchard4bba8282009-07-16 15:44:29 +0200416 if (sample_address)
417 attr->sample_type |= PERF_SAMPLE_ADDR;
418
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200419 if (call_graph)
420 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
421
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200422 if (raw_samples)
423 attr->sample_type |= PERF_SAMPLE_RAW;
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200424
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200425 attr->mmap = track;
426 attr->comm = track;
427 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200428 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200429
Ingo Molnar3da297a2009-06-07 17:39:02 +0200430try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200431 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200432
433 if (fd[nr_cpu][counter] < 0) {
434 int err = errno;
435
Ingo Molnarf250c0302009-06-05 13:18:41 +0200436 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200437 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200438 else if (err == ENODEV && profile_cpu != -1)
439 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200440
441 /*
442 * If it's cycles then fall back to hrtimer
443 * based cpu-clock-tick sw counter, which
444 * is always available even if no PMU support:
445 */
446 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200447 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200448
449 if (verbose)
450 warning(" ... trying to fall back to cpu-clock-ticks\n");
451 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200452 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200453 goto try_again;
454 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200455 printf("\n");
456 error("perfcounter syscall returned with %d (%s)\n",
457 fd[nr_cpu][counter], strerror(err));
458 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200459 exit(-1);
460 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200461
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200462 h_attr = get_header_attr(attr, counter);
463
464 if (!file_new) {
465 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
466 fprintf(stderr, "incompatible append\n");
467 exit(-1);
468 }
469 }
470
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200471 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
472 perror("Unable to read perf file descriptor\n");
473 exit(-1);
474 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200475
476 perf_header_attr__add_id(h_attr, read_data.id);
477
Ingo Molnarf250c0302009-06-05 13:18:41 +0200478 assert(fd[nr_cpu][counter] >= 0);
479 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
480
481 /*
482 * First counter acts as the group leader:
483 */
484 if (group && group_fd == -1)
485 group_fd = fd[nr_cpu][counter];
486
487 event_array[nr_poll].fd = fd[nr_cpu][counter];
488 event_array[nr_poll].events = POLLIN;
489 nr_poll++;
490
491 mmap_array[nr_cpu][counter].counter = counter;
492 mmap_array[nr_cpu][counter].prev = 0;
493 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
494 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200495 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200496 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
497 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
498 exit(-1);
499 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200500
501 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200502}
503
504static void open_counters(int cpu, pid_t pid)
505{
506 int counter;
507
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200508 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200509 for (counter = 0; counter < nr_counters; counter++)
510 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200511
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200512 nr_cpu++;
513}
514
Peter Zijlstraf5970552009-06-18 23:22:55 +0200515static void atexit_header(void)
516{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200517 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200518
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200519 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200520}
521
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200522static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200523{
524 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200525 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200526 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200527 int flags;
528 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200529
530 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200531 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
532 assert(nr_cpus <= MAX_NR_CPUS);
533 assert(nr_cpus >= 0);
534
Peter Zijlstraf5970552009-06-18 23:22:55 +0200535 atexit(sig_atexit);
536 signal(SIGCHLD, sig_handler);
537 signal(SIGINT, sig_handler);
538
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200539 if (!stat(output_name, &st) && st.st_size) {
540 if (!force && !append_file) {
541 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
542 output_name);
543 exit(-1);
544 }
545 } else {
546 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200547 }
548
Ingo Molnarabaff322009-06-02 22:59:57 +0200549 flags = O_CREAT|O_RDWR;
550 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200551 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200552 else
553 flags |= O_TRUNC;
554
555 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200556 if (output < 0) {
557 perror("failed to create output file");
558 exit(-1);
559 }
560
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200561 if (!file_new)
562 header = perf_header__read(output);
563 else
564 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200565
566 atexit(atexit_header);
567
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300568 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200569 pid = target_pid;
570 if (pid == -1)
571 pid = getpid();
572
Jens Axboe0a5ac842009-08-12 11:18:01 +0200573 open_counters(profile_cpu, pid);
574 } else {
575 if (profile_cpu != -1) {
576 open_counters(profile_cpu, target_pid);
577 } else {
578 for (i = 0; i < nr_cpus; i++)
579 open_counters(i, target_pid);
580 }
581 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200582
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200583 if (file_new)
584 perf_header__write(header, output);
585
586 if (!system_wide) {
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300587 pid_t tgid = pid_synthesize_comm_event(pid, 0);
588 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200589 } else
590 synthesize_all();
591
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200592 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300593 pid = fork();
594 if (pid < 0)
595 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200596
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300597 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200598 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300599 perror(argv[0]);
600 exit(-1);
601 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200602 }
603 }
604
605 if (realtime_prio) {
606 struct sched_param param;
607
608 param.sched_priority = realtime_prio;
609 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
610 printf("Could not set realtime priority.\n");
611 exit(-1);
612 }
613 }
614
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200615 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200616 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200617
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200618 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200619 for (counter = 0; counter < nr_counters; counter++)
620 mmap_read(&mmap_array[i][counter]);
621 }
622
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200623 if (hits == samples) {
624 if (done)
625 break;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200626 ret = poll(event_array, nr_poll, 100);
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200627 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200628 }
629
Ingo Molnar021e9f42009-06-03 19:27:19 +0200630 /*
631 * Approximate RIP event size: 24 bytes.
632 */
633 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200634 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200635 (double)bytes_written / 1024.0 / 1024.0,
636 output_name,
637 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200638
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200639 return 0;
640}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200641
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200642static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200643 "perf record [<options>] [<command>]",
644 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200645 NULL
646};
647
Ingo Molnar52425192009-05-26 09:17:18 +0200648static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200649 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200650 "event selector. use 'perf list' to list available events",
651 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200652 OPT_INTEGER('p', "pid", &target_pid,
653 "record events on existing pid"),
654 OPT_INTEGER('r', "realtime", &realtime_prio,
655 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200656 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
657 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200658 OPT_BOOLEAN('a', "all-cpus", &system_wide,
659 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200660 OPT_BOOLEAN('A', "append", &append_file,
661 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200662 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
663 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200664 OPT_BOOLEAN('f', "force", &force,
665 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200666 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200667 "event period to sample"),
668 OPT_STRING('o', "output", &output_name, "file",
669 "output file name"),
670 OPT_BOOLEAN('i', "inherit", &inherit,
671 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200672 OPT_INTEGER('F', "freq", &freq,
673 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200674 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
675 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200676 OPT_BOOLEAN('g', "call-graph", &call_graph,
677 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200678 OPT_BOOLEAN('v', "verbose", &verbose,
679 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200680 OPT_BOOLEAN('s', "stat", &inherit_stat,
681 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200682 OPT_BOOLEAN('d', "data", &sample_address,
683 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200684 OPT_BOOLEAN('n', "no-samples", &no_samples,
685 "don't sample"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200686 OPT_END()
687};
688
Ingo Molnarf37a2912009-07-01 12:37:06 +0200689int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200690{
691 int counter;
692
Anton Blancharda0541232009-07-22 23:04:12 +1000693 argc = parse_options(argc, argv, options, record_usage,
694 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200695 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200696 usage_with_options(record_usage, options);
697
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200698 if (!nr_counters) {
699 nr_counters = 1;
700 attrs[0].type = PERF_TYPE_HARDWARE;
701 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
702 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200703
704 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200705 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200706 continue;
707
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200708 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200709 }
710
711 return __cmd_record(argc, argv);
712}