blob: 90c98082af106da85261a8995224386fcd228c4d [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;
Anton Blanchard4bba8282009-07-16 15:44:29 +020046static int sample_address = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020047
Ingo Molnara21ca2c2009-06-06 09:58:57 +020048static long samples;
49static struct timeval last_read;
50static struct timeval this_read;
51
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100052static u64 bytes_written;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020053
54static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
55
56static int nr_poll;
57static int nr_cpu;
58
Peter Zijlstraf5970552009-06-18 23:22:55 +020059static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020060
61struct perf_header *header;
Peter Zijlstraf5970552009-06-18 23:22:55 +020062
Ingo Molnara21ca2c2009-06-06 09:58:57 +020063struct mmap_event {
64 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100065 u32 pid;
66 u32 tid;
67 u64 start;
68 u64 len;
69 u64 pgoff;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020070 char filename[PATH_MAX];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020071};
72
Ingo Molnara21ca2c2009-06-06 09:58:57 +020073struct comm_event {
74 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100075 u32 pid;
76 u32 tid;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020077 char comm[16];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020078};
79
Ingo Molnara21ca2c2009-06-06 09:58:57 +020080
81struct mmap_data {
82 int counter;
83 void *base;
84 unsigned int mask;
85 unsigned int prev;
86};
87
88static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
89
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020090static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020091{
92 struct perf_counter_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020093 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020094
95 head = pc->data_head;
96 rmb();
97
98 return head;
99}
100
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200101static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
102{
103 struct perf_counter_mmap_page *pc = md->base;
104
105 /*
106 * ensure all reads are done before we write the tail out.
107 */
108 /* mb(); */
109 pc->data_tail = tail;
110}
111
Peter Zijlstraf5970552009-06-18 23:22:55 +0200112static void write_output(void *buf, size_t size)
113{
114 while (size) {
115 int ret = write(output, buf, size);
116
117 if (ret < 0)
118 die("failed to write");
119
120 size -= ret;
121 buf += ret;
122
123 bytes_written += ret;
124 }
125}
126
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200127static void mmap_read(struct mmap_data *md)
128{
129 unsigned int head = mmap_read_head(md);
130 unsigned int old = md->prev;
131 unsigned char *data = md->base + page_size;
132 unsigned long size;
133 void *buf;
134 int diff;
135
136 gettimeofday(&this_read, NULL);
137
138 /*
139 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200140 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200141 *
142 * If we somehow ended up ahead of the head, we got messed up.
143 *
144 * In either case, truncate and restart at head.
145 */
146 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200147 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200148 struct timeval iv;
149 unsigned long msecs;
150
151 timersub(&this_read, &last_read, &iv);
152 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
153
154 fprintf(stderr, "WARNING: failed to keep up with mmap data."
155 " Last read %lu msecs ago.\n", msecs);
156
157 /*
158 * head points to a known good entry, start there.
159 */
160 old = head;
161 }
162
163 last_read = this_read;
164
165 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200166 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200167
168 size = head - old;
169
170 if ((old & md->mask) + size != (head & md->mask)) {
171 buf = &data[old & md->mask];
172 size = md->mask + 1 - (old & md->mask);
173 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200174
Peter Zijlstraf5970552009-06-18 23:22:55 +0200175 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200176 }
177
178 buf = &data[old & md->mask];
179 size = head - old;
180 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200181
Peter Zijlstraf5970552009-06-18 23:22:55 +0200182 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200183
184 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200185 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200186}
187
188static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200189static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200190
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200191static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200192{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200193 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200194 signr = sig;
195}
196
197static void sig_atexit(void)
198{
199 if (signr == -1)
200 return;
201
202 signal(signr, SIG_DFL);
203 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200204}
205
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200206static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300207{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300208 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200209 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200210 char bf[BUFSIZ];
Peter Zijlstraf5970552009-06-18 23:22:55 +0200211 int fd;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300212 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300213 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200214 DIR *tasks;
215 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300216
217 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
218
219 fd = open(filename, O_RDONLY);
220 if (fd < 0) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200221 /*
222 * We raced with a task exiting - just return:
223 */
224 if (verbose)
225 fprintf(stderr, "couldn't open %s\n", filename);
226 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300227 }
228 if (read(fd, bf, sizeof(bf)) < 0) {
229 fprintf(stderr, "couldn't read %s\n", filename);
230 exit(EXIT_FAILURE);
231 }
232 close(fd);
233
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300234 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300235 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300236 field = strchr(bf, '(');
237 if (field == NULL)
238 goto out_failure;
239 sep = strchr(++field, ')');
240 if (sep == NULL)
241 goto out_failure;
242 size = sep - field;
243 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200244
245 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300246 comm_ev.header.type = PERF_EVENT_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000247 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300248 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200249
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200250 if (!full) {
251 comm_ev.tid = pid;
252
Peter Zijlstraf5970552009-06-18 23:22:55 +0200253 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200254 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
Peter Zijlstraf5970552009-06-18 23:22:55 +0200268 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200269 }
270 closedir(tasks);
271 return;
272
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300273out_failure:
274 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
275 filename);
276 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300277}
278
Ingo Molnar2debbc82009-06-05 14:29:10 +0200279static void pid_synthesize_mmap_samples(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300280{
281 char filename[PATH_MAX];
282 FILE *fp;
283
284 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
285
286 fp = fopen(filename, "r");
287 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200288 /*
289 * We raced with a task exiting - just return:
290 */
291 if (verbose)
292 fprintf(stderr, "couldn't open %s\n", filename);
293 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300294 }
295 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300296 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300297 struct mmap_event mmap_ev = {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200298 .header = { .type = PERF_EVENT_MMAP },
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300299 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300300 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300301 size_t size;
302 if (fgets(bf, sizeof(bf), fp) == NULL)
303 break;
304
305 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300306 n = hex2u64(pbf, &mmap_ev.start);
307 if (n < 0)
308 continue;
309 pbf += n + 1;
310 n = hex2u64(pbf, &mmap_ev.len);
311 if (n < 0)
312 continue;
313 pbf += n + 3;
314 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200315 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300316
Anton Blanchard11b5f812009-07-16 15:44:29 +0200317 /* Catch VDSO */
318 if (execname == NULL)
319 execname = strstr(bf, "[vdso]");
320
Johannes Weiner76c64c52009-06-24 21:08:36 +0200321 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300322 continue;
323
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300324 size = strlen(execname);
325 execname[size - 1] = '\0'; /* Remove \n */
326 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000327 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300328 mmap_ev.len -= mmap_ev.start;
329 mmap_ev.header.size = (sizeof(mmap_ev) -
330 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200331 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300332 mmap_ev.tid = pid;
333
Peter Zijlstraf5970552009-06-18 23:22:55 +0200334 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300335 }
336 }
337
338 fclose(fp);
339}
340
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200341static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200342{
343 DIR *proc;
344 struct dirent dirent, *next;
345
346 proc = opendir("/proc");
347
348 while (!readdir_r(proc, &dirent, &next) && next) {
349 char *end;
350 pid_t pid;
351
352 pid = strtol(dirent.d_name, &end, 10);
353 if (*end) /* only interested in proper numerical dirents */
354 continue;
355
356 pid_synthesize_comm_event(pid, 1);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200357 pid_synthesize_mmap_samples(pid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200358 }
359
360 closedir(proc);
361}
362
Ingo Molnarf250c0302009-06-05 13:18:41 +0200363static int group_fd;
364
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200365static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
366{
367 struct perf_header_attr *h_attr;
368
369 if (nr < header->attrs) {
370 h_attr = header->attr[nr];
371 } else {
372 h_attr = perf_header_attr__new(a);
373 perf_header__add_attr(header, h_attr);
374 }
375
376 return h_attr;
377}
378
Ingo Molnarf250c0302009-06-05 13:18:41 +0200379static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200380{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200381 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200382 struct perf_header_attr *h_attr;
383 int track = !counter; /* only the first counter needs these */
384 struct {
385 u64 count;
386 u64 time_enabled;
387 u64 time_running;
388 u64 id;
389 } read_data;
390
391 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
392 PERF_FORMAT_TOTAL_TIME_RUNNING |
393 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200394
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200395 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200396
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200397 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200398 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200399 attr->freq = 1;
400 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200401 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200402
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200403 if (no_samples)
404 attr->sample_freq = 0;
405
406 if (inherit_stat)
407 attr->inherit_stat = 1;
408
Anton Blanchard4bba8282009-07-16 15:44:29 +0200409 if (sample_address)
410 attr->sample_type |= PERF_SAMPLE_ADDR;
411
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200412 if (call_graph)
413 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
414
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200415
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200416 attr->mmap = track;
417 attr->comm = track;
418 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200419 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200420
Ingo Molnar3da297a2009-06-07 17:39:02 +0200421try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200422 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200423
424 if (fd[nr_cpu][counter] < 0) {
425 int err = errno;
426
Ingo Molnarf250c0302009-06-05 13:18:41 +0200427 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200428 die("Permission error - are you root?\n");
429
430 /*
431 * If it's cycles then fall back to hrtimer
432 * based cpu-clock-tick sw counter, which
433 * is always available even if no PMU support:
434 */
435 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200436 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200437
438 if (verbose)
439 warning(" ... trying to fall back to cpu-clock-ticks\n");
440 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200441 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200442 goto try_again;
443 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200444 printf("\n");
445 error("perfcounter syscall returned with %d (%s)\n",
446 fd[nr_cpu][counter], strerror(err));
447 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200448 exit(-1);
449 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200450
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200451 h_attr = get_header_attr(attr, counter);
452
453 if (!file_new) {
454 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
455 fprintf(stderr, "incompatible append\n");
456 exit(-1);
457 }
458 }
459
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200460 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
461 perror("Unable to read perf file descriptor\n");
462 exit(-1);
463 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200464
465 perf_header_attr__add_id(h_attr, read_data.id);
466
Ingo Molnarf250c0302009-06-05 13:18:41 +0200467 assert(fd[nr_cpu][counter] >= 0);
468 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
469
470 /*
471 * First counter acts as the group leader:
472 */
473 if (group && group_fd == -1)
474 group_fd = fd[nr_cpu][counter];
475
476 event_array[nr_poll].fd = fd[nr_cpu][counter];
477 event_array[nr_poll].events = POLLIN;
478 nr_poll++;
479
480 mmap_array[nr_cpu][counter].counter = counter;
481 mmap_array[nr_cpu][counter].prev = 0;
482 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
483 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200484 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200485 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
486 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
487 exit(-1);
488 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200489
490 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200491}
492
493static void open_counters(int cpu, pid_t pid)
494{
495 int counter;
496
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200497 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200498 for (counter = 0; counter < nr_counters; counter++)
499 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200500
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200501 nr_cpu++;
502}
503
Peter Zijlstraf5970552009-06-18 23:22:55 +0200504static void atexit_header(void)
505{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200506 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200507
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200508 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200509}
510
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200511static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200512{
513 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200514 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200515 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200516 int flags;
517 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200518
519 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200520 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
521 assert(nr_cpus <= MAX_NR_CPUS);
522 assert(nr_cpus >= 0);
523
Peter Zijlstraf5970552009-06-18 23:22:55 +0200524 atexit(sig_atexit);
525 signal(SIGCHLD, sig_handler);
526 signal(SIGINT, sig_handler);
527
Ingo Molnarabaff322009-06-02 22:59:57 +0200528 if (!stat(output_name, &st) && !force && !append_file) {
529 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
Peter Zijlstra97124d52009-06-02 15:52:24 +0200530 output_name);
531 exit(-1);
532 }
533
Ingo Molnarabaff322009-06-02 22:59:57 +0200534 flags = O_CREAT|O_RDWR;
535 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200536 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200537 else
538 flags |= O_TRUNC;
539
540 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200541 if (output < 0) {
542 perror("failed to create output file");
543 exit(-1);
544 }
545
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200546 if (!file_new)
547 header = perf_header__read(output);
548 else
549 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200550
551 atexit(atexit_header);
552
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300553 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200554 pid = target_pid;
555 if (pid == -1)
556 pid = getpid();
557
558 open_counters(-1, pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300559 } else for (i = 0; i < nr_cpus; i++)
560 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200561
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200562 if (file_new)
563 perf_header__write(header, output);
564
565 if (!system_wide) {
566 pid_synthesize_comm_event(pid, 0);
567 pid_synthesize_mmap_samples(pid);
568 } else
569 synthesize_all();
570
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200571 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300572 pid = fork();
573 if (pid < 0)
574 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200575
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300576 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200577 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300578 perror(argv[0]);
579 exit(-1);
580 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200581 }
582 }
583
584 if (realtime_prio) {
585 struct sched_param param;
586
587 param.sched_priority = realtime_prio;
588 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
589 printf("Could not set realtime priority.\n");
590 exit(-1);
591 }
592 }
593
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200594 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200595 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200596
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200597 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200598 for (counter = 0; counter < nr_counters; counter++)
599 mmap_read(&mmap_array[i][counter]);
600 }
601
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200602 if (hits == samples) {
603 if (done)
604 break;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200605 ret = poll(event_array, nr_poll, 100);
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200606 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200607 }
608
Ingo Molnar021e9f42009-06-03 19:27:19 +0200609 /*
610 * Approximate RIP event size: 24 bytes.
611 */
612 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200613 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200614 (double)bytes_written / 1024.0 / 1024.0,
615 output_name,
616 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200617
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200618 return 0;
619}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200620
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200621static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200622 "perf record [<options>] [<command>]",
623 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200624 NULL
625};
626
Ingo Molnar52425192009-05-26 09:17:18 +0200627static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200628 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200629 "event selector. use 'perf list' to list available events",
630 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200631 OPT_INTEGER('p', "pid", &target_pid,
632 "record events on existing pid"),
633 OPT_INTEGER('r', "realtime", &realtime_prio,
634 "collect data with this RT SCHED_FIFO priority"),
635 OPT_BOOLEAN('a', "all-cpus", &system_wide,
636 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200637 OPT_BOOLEAN('A', "append", &append_file,
638 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200639 OPT_BOOLEAN('f', "force", &force,
640 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200641 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200642 "event period to sample"),
643 OPT_STRING('o', "output", &output_name, "file",
644 "output file name"),
645 OPT_BOOLEAN('i', "inherit", &inherit,
646 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200647 OPT_INTEGER('F', "freq", &freq,
648 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200649 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
650 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200651 OPT_BOOLEAN('g', "call-graph", &call_graph,
652 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200653 OPT_BOOLEAN('v', "verbose", &verbose,
654 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200655 OPT_BOOLEAN('s', "stat", &inherit_stat,
656 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200657 OPT_BOOLEAN('d', "data", &sample_address,
658 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200659 OPT_BOOLEAN('n', "no-samples", &no_samples,
660 "don't sample"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200661 OPT_END()
662};
663
Ingo Molnarf37a2912009-07-01 12:37:06 +0200664int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200665{
666 int counter;
667
Anton Blancharda0541232009-07-22 23:04:12 +1000668 argc = parse_options(argc, argv, options, record_usage,
669 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200670 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200671 usage_with_options(record_usage, options);
672
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200673 if (!nr_counters) {
674 nr_counters = 1;
675 attrs[0].type = PERF_TYPE_HARDWARE;
676 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
677 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200678
679 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200680 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200681 continue;
682
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200683 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200684 }
685
686 return __cmd_record(argc, argv);
687}