blob: a5a050af8e7dbca2e474d3f2907b4bc5f5cebbae [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"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020018#include "util/event.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020019#include "util/debug.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020020#include "util/trace-event.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020021
Peter Zijlstra97124d52009-06-02 15:52:24 +020022#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020023#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020024
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020025#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
26#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030027
Peter Zijlstrade9ac072009-04-08 15:01:31 +020028static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020029
30static long default_interval = 100000;
31
Ingo Molnar3cf165f2009-06-02 23:02:59 +020032static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020033static unsigned int page_size;
Ingo Molnar3cf165f2009-06-02 23:02:59 +020034static unsigned int mmap_pages = 128;
Ingo Molnarcf1f4572009-06-05 13:27:02 +020035static int freq = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020036static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020037static const char *output_name = "perf.data";
Peter Zijlstrade9ac072009-04-08 15:01:31 +020038static int group = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020039static unsigned int realtime_prio = 0;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +020040static int raw_samples = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020041static int system_wide = 0;
Jens Axboe0a5ac842009-08-12 11:18:01 +020042static int profile_cpu = -1;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030043static pid_t target_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020044static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020045static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020046static int append_file = 0;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020047static int call_graph = 0;
Peter Zijlstra649c48a2009-06-24 21:12:48 +020048static int inherit_stat = 0;
49static int no_samples = 0;
Anton Blanchard4bba8282009-07-16 15:44:29 +020050static int sample_address = 0;
Frederic Weisbeckerd1302522009-09-14 08:57:15 +020051static int multiplex = 0;
Ingo Molnarea57c4f2009-09-13 18:15:54 +020052static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020053
Ingo Molnara21ca2c2009-06-06 09:58:57 +020054static long samples;
55static struct timeval last_read;
56static struct timeval this_read;
57
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100058static u64 bytes_written;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020059
60static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
61
62static int nr_poll;
63static int nr_cpu;
64
Peter Zijlstraf5970552009-06-18 23:22:55 +020065static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066
67struct perf_header *header;
Peter Zijlstraf5970552009-06-18 23:22:55 +020068
Ingo Molnara21ca2c2009-06-06 09:58:57 +020069struct mmap_data {
70 int counter;
71 void *base;
72 unsigned int mask;
73 unsigned int prev;
74};
75
76static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
77
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020078static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020079{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020080 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020081 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020082
83 head = pc->data_head;
84 rmb();
85
86 return head;
87}
88
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020089static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
90{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020091 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020092
93 /*
94 * ensure all reads are done before we write the tail out.
95 */
96 /* mb(); */
97 pc->data_tail = tail;
98}
99
Peter Zijlstraf5970552009-06-18 23:22:55 +0200100static void write_output(void *buf, size_t size)
101{
102 while (size) {
103 int ret = write(output, buf, size);
104
105 if (ret < 0)
106 die("failed to write");
107
108 size -= ret;
109 buf += ret;
110
111 bytes_written += ret;
112 }
113}
114
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200115static void mmap_read(struct mmap_data *md)
116{
117 unsigned int head = mmap_read_head(md);
118 unsigned int old = md->prev;
119 unsigned char *data = md->base + page_size;
120 unsigned long size;
121 void *buf;
122 int diff;
123
124 gettimeofday(&this_read, NULL);
125
126 /*
127 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200128 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200129 *
130 * If we somehow ended up ahead of the head, we got messed up.
131 *
132 * In either case, truncate and restart at head.
133 */
134 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200135 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200136 struct timeval iv;
137 unsigned long msecs;
138
139 timersub(&this_read, &last_read, &iv);
140 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
141
142 fprintf(stderr, "WARNING: failed to keep up with mmap data."
143 " Last read %lu msecs ago.\n", msecs);
144
145 /*
146 * head points to a known good entry, start there.
147 */
148 old = head;
149 }
150
151 last_read = this_read;
152
153 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200154 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200155
156 size = head - old;
157
158 if ((old & md->mask) + size != (head & md->mask)) {
159 buf = &data[old & md->mask];
160 size = md->mask + 1 - (old & md->mask);
161 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200162
Peter Zijlstraf5970552009-06-18 23:22:55 +0200163 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200164 }
165
166 buf = &data[old & md->mask];
167 size = head - old;
168 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200169
Peter Zijlstraf5970552009-06-18 23:22:55 +0200170 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200171
172 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200173 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200174}
175
176static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200177static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200178
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200179static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200180{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200181 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200182 signr = sig;
183}
184
185static void sig_atexit(void)
186{
187 if (signr == -1)
188 return;
189
190 signal(signr, SIG_DFL);
191 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200192}
193
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300194static pid_t pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300195{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300196 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200197 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200198 char bf[BUFSIZ];
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300199 FILE *fp;
200 size_t size = 0;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200201 DIR *tasks;
202 struct dirent dirent, *next;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300203 pid_t tgid = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300204
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300205 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300206
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300207 fp = fopen(filename, "r");
Arnaldo Carvalho de Melo39e6dd72009-08-14 15:26:32 -0300208 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200209 /*
210 * We raced with a task exiting - just return:
211 */
212 if (verbose)
213 fprintf(stderr, "couldn't open %s\n", filename);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300214 return 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300215 }
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300216
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300217 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300218 while (!comm_ev.comm[0] || !comm_ev.pid) {
219 if (fgets(bf, sizeof(bf), fp) == NULL)
220 goto out_failure;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200221
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300222 if (memcmp(bf, "Name:", 5) == 0) {
223 char *name = bf + 5;
224 while (*name && isspace(*name))
225 ++name;
226 size = strlen(name) - 1;
227 memcpy(comm_ev.comm, name, size++);
228 } else if (memcmp(bf, "Tgid:", 5) == 0) {
229 char *tgids = bf + 5;
230 while (*tgids && isspace(*tgids))
231 ++tgids;
232 tgid = comm_ev.pid = atoi(tgids);
233 }
234 }
235
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200236 comm_ev.header.type = PERF_RECORD_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000237 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300238 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200239
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200240 if (!full) {
241 comm_ev.tid = pid;
242
Peter Zijlstraf5970552009-06-18 23:22:55 +0200243 write_output(&comm_ev, comm_ev.header.size);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300244 goto out_fclose;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300245 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200246
247 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
248
249 tasks = opendir(filename);
250 while (!readdir_r(tasks, &dirent, &next) && next) {
251 char *end;
252 pid = strtol(dirent.d_name, &end, 10);
253 if (*end)
254 continue;
255
256 comm_ev.tid = pid;
257
Peter Zijlstraf5970552009-06-18 23:22:55 +0200258 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200259 }
260 closedir(tasks);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300261
262out_fclose:
263 fclose(fp);
264 return tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200265
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300266out_failure:
267 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
268 filename);
269 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300270}
271
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300272static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300273{
274 char filename[PATH_MAX];
275 FILE *fp;
276
277 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
278
279 fp = fopen(filename, "r");
280 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200281 /*
282 * We raced with a task exiting - just return:
283 */
284 if (verbose)
285 fprintf(stderr, "couldn't open %s\n", filename);
286 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300287 }
288 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300289 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300290 struct mmap_event mmap_ev = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200291 .header = { .type = PERF_RECORD_MMAP },
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300292 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300293 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300294 size_t size;
295 if (fgets(bf, sizeof(bf), fp) == NULL)
296 break;
297
298 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300299 n = hex2u64(pbf, &mmap_ev.start);
300 if (n < 0)
301 continue;
302 pbf += n + 1;
303 n = hex2u64(pbf, &mmap_ev.len);
304 if (n < 0)
305 continue;
306 pbf += n + 3;
307 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200308 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300309
Anton Blanchard11b5f812009-07-16 15:44:29 +0200310 /* Catch VDSO */
311 if (execname == NULL)
312 execname = strstr(bf, "[vdso]");
313
Johannes Weiner76c64c52009-06-24 21:08:36 +0200314 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300315 continue;
316
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300317 size = strlen(execname);
318 execname[size - 1] = '\0'; /* Remove \n */
319 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000320 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300321 mmap_ev.len -= mmap_ev.start;
322 mmap_ev.header.size = (sizeof(mmap_ev) -
323 (sizeof(mmap_ev.filename) - size));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300324 mmap_ev.pid = tgid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300325 mmap_ev.tid = pid;
326
Peter Zijlstraf5970552009-06-18 23:22:55 +0200327 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300328 }
329 }
330
331 fclose(fp);
332}
333
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200334static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200335{
336 DIR *proc;
337 struct dirent dirent, *next;
338
339 proc = opendir("/proc");
340
341 while (!readdir_r(proc, &dirent, &next) && next) {
342 char *end;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300343 pid_t pid, tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200344
345 pid = strtol(dirent.d_name, &end, 10);
346 if (*end) /* only interested in proper numerical dirents */
347 continue;
348
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300349 tgid = pid_synthesize_comm_event(pid, 1);
350 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200351 }
352
353 closedir(proc);
354}
355
Ingo Molnarf250c0302009-06-05 13:18:41 +0200356static int group_fd;
357
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200358static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200359{
360 struct perf_header_attr *h_attr;
361
362 if (nr < header->attrs) {
363 h_attr = header->attr[nr];
364 } else {
365 h_attr = perf_header_attr__new(a);
366 perf_header__add_attr(header, h_attr);
367 }
368
369 return h_attr;
370}
371
Ingo Molnarf250c0302009-06-05 13:18:41 +0200372static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200373{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200374 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200375 struct perf_header_attr *h_attr;
376 int track = !counter; /* only the first counter needs these */
377 struct {
378 u64 count;
379 u64 time_enabled;
380 u64 time_running;
381 u64 id;
382 } read_data;
383
384 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
385 PERF_FORMAT_TOTAL_TIME_RUNNING |
386 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200387
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200388 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200389
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200390 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200391 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200392 attr->freq = 1;
393 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200394 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200395
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200396 if (no_samples)
397 attr->sample_freq = 0;
398
399 if (inherit_stat)
400 attr->inherit_stat = 1;
401
Anton Blanchard4bba8282009-07-16 15:44:29 +0200402 if (sample_address)
403 attr->sample_type |= PERF_SAMPLE_ADDR;
404
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200405 if (call_graph)
406 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
407
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200408 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200409 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200410 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200411 attr->sample_type |= PERF_SAMPLE_CPU;
412 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200413
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200414 attr->mmap = track;
415 attr->comm = track;
416 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200417 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200418
Ingo Molnar3da297a2009-06-07 17:39:02 +0200419try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200420 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200421
422 if (fd[nr_cpu][counter] < 0) {
423 int err = errno;
424
Ingo Molnarf250c0302009-06-05 13:18:41 +0200425 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200426 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200427 else if (err == ENODEV && profile_cpu != -1)
428 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200429
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));
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200447 die("No CONFIG_PERF_EVENTS=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];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200475 if (multiplex && multiplex_fd == -1)
476 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200477
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200478 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
479 int ret;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200480
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200481 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200482 assert(ret != -1);
483 } else {
484 event_array[nr_poll].fd = fd[nr_cpu][counter];
485 event_array[nr_poll].events = POLLIN;
486 nr_poll++;
487
488 mmap_array[nr_cpu][counter].counter = counter;
489 mmap_array[nr_cpu][counter].prev = 0;
490 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
491 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
492 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
493 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
494 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
495 exit(-1);
496 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200497 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200498
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200499 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200500}
501
502static void open_counters(int cpu, pid_t pid)
503{
504 int counter;
505
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200506 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200507 for (counter = 0; counter < nr_counters; counter++)
508 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200509
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200510 nr_cpu++;
511}
512
Peter Zijlstraf5970552009-06-18 23:22:55 +0200513static void atexit_header(void)
514{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200515 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200516
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200517 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200518}
519
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200520static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200521{
522 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200523 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200524 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200525 int flags;
526 int ret;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200527 unsigned long waking = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200528
529 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200530 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
531 assert(nr_cpus <= MAX_NR_CPUS);
532 assert(nr_cpus >= 0);
533
Peter Zijlstraf5970552009-06-18 23:22:55 +0200534 atexit(sig_atexit);
535 signal(SIGCHLD, sig_handler);
536 signal(SIGINT, sig_handler);
537
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200538 if (!stat(output_name, &st) && st.st_size) {
539 if (!force && !append_file) {
540 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
541 output_name);
542 exit(-1);
543 }
544 } else {
545 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200546 }
547
Ingo Molnarabaff322009-06-02 22:59:57 +0200548 flags = O_CREAT|O_RDWR;
549 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200550 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200551 else
552 flags |= O_TRUNC;
553
554 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200555 if (output < 0) {
556 perror("failed to create output file");
557 exit(-1);
558 }
559
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200560 if (!file_new)
561 header = perf_header__read(output);
562 else
563 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200564
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200565
566 if (raw_samples) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200567 read_tracing_data(attrs, nr_counters);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200568 } else {
569 for (i = 0; i < nr_counters; i++) {
570 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200571 read_tracing_data(attrs, nr_counters);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200572 break;
573 }
574 }
575 }
Peter Zijlstraf5970552009-06-18 23:22:55 +0200576 atexit(atexit_header);
577
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300578 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200579 pid = target_pid;
580 if (pid == -1)
581 pid = getpid();
582
Jens Axboe0a5ac842009-08-12 11:18:01 +0200583 open_counters(profile_cpu, pid);
584 } else {
585 if (profile_cpu != -1) {
586 open_counters(profile_cpu, target_pid);
587 } else {
588 for (i = 0; i < nr_cpus; i++)
589 open_counters(i, target_pid);
590 }
591 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200592
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200593 if (file_new)
594 perf_header__write(header, output);
595
596 if (!system_wide) {
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300597 pid_t tgid = pid_synthesize_comm_event(pid, 0);
598 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200599 } else
600 synthesize_all();
601
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200602 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300603 pid = fork();
604 if (pid < 0)
605 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200606
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300607 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200608 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300609 perror(argv[0]);
610 exit(-1);
611 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200612 }
613 }
614
615 if (realtime_prio) {
616 struct sched_param param;
617
618 param.sched_priority = realtime_prio;
619 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
620 printf("Could not set realtime priority.\n");
621 exit(-1);
622 }
623 }
624
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200625 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200626 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200627
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200628 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200629 for (counter = 0; counter < nr_counters; counter++) {
630 if (mmap_array[i][counter].base)
631 mmap_read(&mmap_array[i][counter]);
632 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200633 }
634
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200635 if (hits == samples) {
636 if (done)
637 break;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200638 ret = poll(event_array, nr_poll, -1);
639 waking++;
640 }
641
642 if (done) {
643 for (i = 0; i < nr_cpu; i++) {
644 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200645 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200646 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200647 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200648 }
649
Peter Zijlstra8b412662009-09-17 19:59:05 +0200650 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
651
Ingo Molnar021e9f42009-06-03 19:27:19 +0200652 /*
653 * Approximate RIP event size: 24 bytes.
654 */
655 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200656 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200657 (double)bytes_written / 1024.0 / 1024.0,
658 output_name,
659 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200660
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200661 return 0;
662}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200663
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200664static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200665 "perf record [<options>] [<command>]",
666 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200667 NULL
668};
669
Ingo Molnar52425192009-05-26 09:17:18 +0200670static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200671 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200672 "event selector. use 'perf list' to list available events",
673 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200674 OPT_INTEGER('p', "pid", &target_pid,
675 "record events on existing pid"),
676 OPT_INTEGER('r', "realtime", &realtime_prio,
677 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200678 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
679 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200680 OPT_BOOLEAN('a', "all-cpus", &system_wide,
681 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200682 OPT_BOOLEAN('A', "append", &append_file,
683 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200684 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
685 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200686 OPT_BOOLEAN('f', "force", &force,
687 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200688 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200689 "event period to sample"),
690 OPT_STRING('o', "output", &output_name, "file",
691 "output file name"),
692 OPT_BOOLEAN('i', "inherit", &inherit,
693 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200694 OPT_INTEGER('F', "freq", &freq,
695 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200696 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
697 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200698 OPT_BOOLEAN('g', "call-graph", &call_graph,
699 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200700 OPT_BOOLEAN('v', "verbose", &verbose,
701 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200702 OPT_BOOLEAN('s', "stat", &inherit_stat,
703 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200704 OPT_BOOLEAN('d', "data", &sample_address,
705 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200706 OPT_BOOLEAN('n', "no-samples", &no_samples,
707 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200708 OPT_BOOLEAN('M', "multiplex", &multiplex,
709 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200710 OPT_END()
711};
712
Ingo Molnarf37a2912009-07-01 12:37:06 +0200713int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200714{
715 int counter;
716
Anton Blancharda0541232009-07-22 23:04:12 +1000717 argc = parse_options(argc, argv, options, record_usage,
718 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200719 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200720 usage_with_options(record_usage, options);
721
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200722 if (!nr_counters) {
723 nr_counters = 1;
724 attrs[0].type = PERF_TYPE_HARDWARE;
725 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
726 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200727
728 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200729 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200730 continue;
731
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200732 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200733 }
734
735 return __cmd_record(argc, argv);
736}