blob: 8b2c860c49a23111fc5b677570ba158559d321e0 [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"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020020
Peter Zijlstra97124d52009-06-02 15:52:24 +020021#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020022#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020023
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020024#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
25#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030026
Peter Zijlstrade9ac072009-04-08 15:01:31 +020027static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020028
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020029static long default_interval = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020030
Ingo Molnar42e59d72009-10-06 15:14:21 +020031static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020032static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020033static unsigned int mmap_pages = 128;
34static int freq = 1000;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020035static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020036static const char *output_name = "perf.data";
Ingo Molnar42e59d72009-10-06 15:14:21 +020037static int group = 0;
38static unsigned int realtime_prio = 0;
39static int raw_samples = 0;
40static int system_wide = 0;
41static int profile_cpu = -1;
42static pid_t target_pid = -1;
43static pid_t child_pid = -1;
44static int inherit = 1;
45static int force = 0;
46static int append_file = 0;
47static int call_graph = 0;
48static int inherit_stat = 0;
49static int no_samples = 0;
50static int sample_address = 0;
51static int multiplex = 0;
52static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020053
Ingo Molnar42e59d72009-10-06 15:14:21 +020054static long samples = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020055static struct timeval last_read;
56static struct timeval this_read;
57
Ingo Molnar42e59d72009-10-06 15:14:21 +020058static u64 bytes_written = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020059
60static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
61
Ingo Molnar42e59d72009-10-06 15:14:21 +020062static int nr_poll = 0;
63static int nr_cpu = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020064
Ingo Molnar42e59d72009-10-06 15:14:21 +020065static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066
Ingo Molnar42e59d72009-10-06 15:14:21 +020067struct perf_header *header = NULL;
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{
Chris Wilson933da832009-10-04 01:35:01 +0100187 if (child_pid != -1)
188 kill(child_pid, SIGTERM);
189
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200190 if (signr == -1)
191 return;
192
193 signal(signr, SIG_DFL);
194 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200195}
196
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300197static pid_t pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300198{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300199 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200200 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200201 char bf[BUFSIZ];
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300202 FILE *fp;
203 size_t size = 0;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200204 DIR *tasks;
205 struct dirent dirent, *next;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300206 pid_t tgid = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300207
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300208 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300209
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300210 fp = fopen(filename, "r");
Arnaldo Carvalho de Melo39e6dd72009-08-14 15:26:32 -0300211 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200212 /*
213 * We raced with a task exiting - just return:
214 */
215 if (verbose)
216 fprintf(stderr, "couldn't open %s\n", filename);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300217 return 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300218 }
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300219
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300220 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300221 while (!comm_ev.comm[0] || !comm_ev.pid) {
222 if (fgets(bf, sizeof(bf), fp) == NULL)
223 goto out_failure;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200224
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300225 if (memcmp(bf, "Name:", 5) == 0) {
226 char *name = bf + 5;
227 while (*name && isspace(*name))
228 ++name;
229 size = strlen(name) - 1;
230 memcpy(comm_ev.comm, name, size++);
231 } else if (memcmp(bf, "Tgid:", 5) == 0) {
232 char *tgids = bf + 5;
233 while (*tgids && isspace(*tgids))
234 ++tgids;
235 tgid = comm_ev.pid = atoi(tgids);
236 }
237 }
238
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200239 comm_ev.header.type = PERF_RECORD_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000240 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300241 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200242
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200243 if (!full) {
244 comm_ev.tid = pid;
245
Peter Zijlstraf5970552009-06-18 23:22:55 +0200246 write_output(&comm_ev, comm_ev.header.size);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300247 goto out_fclose;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300248 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200249
250 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
251
252 tasks = opendir(filename);
253 while (!readdir_r(tasks, &dirent, &next) && next) {
254 char *end;
255 pid = strtol(dirent.d_name, &end, 10);
256 if (*end)
257 continue;
258
259 comm_ev.tid = pid;
260
Peter Zijlstraf5970552009-06-18 23:22:55 +0200261 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200262 }
263 closedir(tasks);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300264
265out_fclose:
266 fclose(fp);
267 return tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200268
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300269out_failure:
270 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
271 filename);
272 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300273}
274
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300275static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300276{
277 char filename[PATH_MAX];
278 FILE *fp;
279
280 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
281
282 fp = fopen(filename, "r");
283 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200284 /*
285 * We raced with a task exiting - just return:
286 */
287 if (verbose)
288 fprintf(stderr, "couldn't open %s\n", filename);
289 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300290 }
291 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300292 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300293 struct mmap_event mmap_ev = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200294 .header = { .type = PERF_RECORD_MMAP },
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300295 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300296 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300297 size_t size;
298 if (fgets(bf, sizeof(bf), fp) == NULL)
299 break;
300
301 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300302 n = hex2u64(pbf, &mmap_ev.start);
303 if (n < 0)
304 continue;
305 pbf += n + 1;
306 n = hex2u64(pbf, &mmap_ev.len);
307 if (n < 0)
308 continue;
309 pbf += n + 3;
310 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200311 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300312
Anton Blanchard11b5f812009-07-16 15:44:29 +0200313 /* Catch VDSO */
314 if (execname == NULL)
315 execname = strstr(bf, "[vdso]");
316
Johannes Weiner76c64c52009-06-24 21:08:36 +0200317 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300318 continue;
319
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300320 size = strlen(execname);
321 execname[size - 1] = '\0'; /* Remove \n */
322 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000323 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300324 mmap_ev.len -= mmap_ev.start;
325 mmap_ev.header.size = (sizeof(mmap_ev) -
326 (sizeof(mmap_ev.filename) - size));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300327 mmap_ev.pid = tgid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300328 mmap_ev.tid = pid;
329
Peter Zijlstraf5970552009-06-18 23:22:55 +0200330 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300331 }
332 }
333
334 fclose(fp);
335}
336
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200337static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200338{
339 DIR *proc;
340 struct dirent dirent, *next;
341
342 proc = opendir("/proc");
343
344 while (!readdir_r(proc, &dirent, &next) && next) {
345 char *end;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300346 pid_t pid, tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200347
348 pid = strtol(dirent.d_name, &end, 10);
349 if (*end) /* only interested in proper numerical dirents */
350 continue;
351
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300352 tgid = pid_synthesize_comm_event(pid, 1);
353 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200354 }
355
356 closedir(proc);
357}
358
Ingo Molnarf250c0302009-06-05 13:18:41 +0200359static int group_fd;
360
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200361static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200362{
363 struct perf_header_attr *h_attr;
364
365 if (nr < header->attrs) {
366 h_attr = header->attr[nr];
367 } else {
368 h_attr = perf_header_attr__new(a);
369 perf_header__add_attr(header, h_attr);
370 }
371
372 return h_attr;
373}
374
Ingo Molnarf250c0302009-06-05 13:18:41 +0200375static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200376{
Li Zefanc171b552009-10-15 11:22:07 +0800377 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200378 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200379 struct perf_header_attr *h_attr;
380 int track = !counter; /* only the first counter needs these */
Li Zefanc171b552009-10-15 11:22:07 +0800381 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200382 struct {
383 u64 count;
384 u64 time_enabled;
385 u64 time_running;
386 u64 id;
387 } read_data;
388
389 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
390 PERF_FORMAT_TOTAL_TIME_RUNNING |
391 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200392
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200393 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200394
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200395 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200396 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200397 attr->freq = 1;
398 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200399 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200400
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200401 if (no_samples)
402 attr->sample_freq = 0;
403
404 if (inherit_stat)
405 attr->inherit_stat = 1;
406
Anton Blanchard4bba8282009-07-16 15:44:29 +0200407 if (sample_address)
408 attr->sample_type |= PERF_SAMPLE_ADDR;
409
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200410 if (call_graph)
411 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
412
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200413 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200414 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200415 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200416 attr->sample_type |= PERF_SAMPLE_CPU;
417 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200418
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200419 attr->mmap = track;
420 attr->comm = track;
421 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200422 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200423
Ingo Molnar3da297a2009-06-07 17:39:02 +0200424try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200425 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200426
427 if (fd[nr_cpu][counter] < 0) {
428 int err = errno;
429
Ingo Molnarf250c0302009-06-05 13:18:41 +0200430 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200431 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200432 else if (err == ENODEV && profile_cpu != -1)
433 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200434
435 /*
436 * If it's cycles then fall back to hrtimer
437 * based cpu-clock-tick sw counter, which
438 * is always available even if no PMU support:
439 */
440 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200441 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200442
443 if (verbose)
444 warning(" ... trying to fall back to cpu-clock-ticks\n");
445 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200446 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200447 goto try_again;
448 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200449 printf("\n");
450 error("perfcounter syscall returned with %d (%s)\n",
451 fd[nr_cpu][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200452 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200453 exit(-1);
454 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200455
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200456 h_attr = get_header_attr(attr, counter);
457
458 if (!file_new) {
459 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
460 fprintf(stderr, "incompatible append\n");
461 exit(-1);
462 }
463 }
464
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200465 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
466 perror("Unable to read perf file descriptor\n");
467 exit(-1);
468 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200469
470 perf_header_attr__add_id(h_attr, read_data.id);
471
Ingo Molnarf250c0302009-06-05 13:18:41 +0200472 assert(fd[nr_cpu][counter] >= 0);
473 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
474
475 /*
476 * First counter acts as the group leader:
477 */
478 if (group && group_fd == -1)
479 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200480 if (multiplex && multiplex_fd == -1)
481 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200482
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200483 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
Ingo Molnarf250c0302009-06-05 13:18:41 +0200484
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200485 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200486 assert(ret != -1);
487 } else {
488 event_array[nr_poll].fd = fd[nr_cpu][counter];
489 event_array[nr_poll].events = POLLIN;
490 nr_poll++;
491
492 mmap_array[nr_cpu][counter].counter = counter;
493 mmap_array[nr_cpu][counter].prev = 0;
494 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
495 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
496 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
497 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
498 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
499 exit(-1);
500 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200501 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200502
Li Zefanc171b552009-10-15 11:22:07 +0800503 if (filter != NULL) {
504 ret = ioctl(fd[nr_cpu][counter],
505 PERF_EVENT_IOC_SET_FILTER, filter);
506 if (ret) {
507 error("failed to set filter with %d (%s)\n", errno,
508 strerror(errno));
509 exit(-1);
510 }
511 }
512
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200513 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200514}
515
516static void open_counters(int cpu, pid_t pid)
517{
518 int counter;
519
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200520 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200521 for (counter = 0; counter < nr_counters; counter++)
522 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200523
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200524 nr_cpu++;
525}
526
Peter Zijlstraf5970552009-06-18 23:22:55 +0200527static void atexit_header(void)
528{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200529 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200530
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200531 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200532}
533
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200534static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200535{
536 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200537 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200538 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200539 int flags;
540 int ret;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200541 unsigned long waking = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200542
543 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200544 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
545 assert(nr_cpus <= MAX_NR_CPUS);
546 assert(nr_cpus >= 0);
547
Peter Zijlstraf5970552009-06-18 23:22:55 +0200548 atexit(sig_atexit);
549 signal(SIGCHLD, sig_handler);
550 signal(SIGINT, sig_handler);
551
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200552 if (!stat(output_name, &st) && st.st_size) {
553 if (!force && !append_file) {
554 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
555 output_name);
556 exit(-1);
557 }
558 } else {
559 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200560 }
561
Ingo Molnarabaff322009-06-02 22:59:57 +0200562 flags = O_CREAT|O_RDWR;
563 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200564 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200565 else
566 flags |= O_TRUNC;
567
568 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200569 if (output < 0) {
570 perror("failed to create output file");
571 exit(-1);
572 }
573
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200574 if (!file_new)
575 header = perf_header__read(output);
576 else
577 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200578
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200579 if (raw_samples) {
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200580 perf_header__set_trace_info();
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200581 } else {
582 for (i = 0; i < nr_counters; i++) {
583 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200584 perf_header__set_trace_info();
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200585 break;
586 }
587 }
588 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200589
Peter Zijlstraf5970552009-06-18 23:22:55 +0200590 atexit(atexit_header);
591
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300592 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200593 pid = target_pid;
594 if (pid == -1)
595 pid = getpid();
596
Jens Axboe0a5ac842009-08-12 11:18:01 +0200597 open_counters(profile_cpu, pid);
598 } else {
599 if (profile_cpu != -1) {
600 open_counters(profile_cpu, target_pid);
601 } else {
602 for (i = 0; i < nr_cpus; i++)
603 open_counters(i, target_pid);
604 }
605 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200606
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200607 if (file_new)
608 perf_header__write(header, output);
609
610 if (!system_wide) {
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300611 pid_t tgid = pid_synthesize_comm_event(pid, 0);
612 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200613 } else
614 synthesize_all();
615
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200616 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300617 pid = fork();
618 if (pid < 0)
619 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200620
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300621 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200622 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300623 perror(argv[0]);
624 exit(-1);
625 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200626 }
Chris Wilson933da832009-10-04 01:35:01 +0100627
628 child_pid = pid;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200629 }
630
631 if (realtime_prio) {
632 struct sched_param param;
633
634 param.sched_priority = realtime_prio;
635 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
636 printf("Could not set realtime priority.\n");
637 exit(-1);
638 }
639 }
640
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200641 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200642 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200643
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200644 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200645 for (counter = 0; counter < nr_counters; counter++) {
646 if (mmap_array[i][counter].base)
647 mmap_read(&mmap_array[i][counter]);
648 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200649 }
650
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200651 if (hits == samples) {
652 if (done)
653 break;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200654 ret = poll(event_array, nr_poll, -1);
655 waking++;
656 }
657
658 if (done) {
659 for (i = 0; i < nr_cpu; i++) {
660 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200661 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200662 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200663 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200664 }
665
Peter Zijlstra8b412662009-09-17 19:59:05 +0200666 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
667
Ingo Molnar021e9f42009-06-03 19:27:19 +0200668 /*
669 * Approximate RIP event size: 24 bytes.
670 */
671 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200672 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200673 (double)bytes_written / 1024.0 / 1024.0,
674 output_name,
675 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200676
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200677 return 0;
678}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200679
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200680static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200681 "perf record [<options>] [<command>]",
682 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200683 NULL
684};
685
Ingo Molnar52425192009-05-26 09:17:18 +0200686static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200687 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200688 "event selector. use 'perf list' to list available events",
689 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800690 OPT_CALLBACK(0, "filter", NULL, "filter",
691 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200692 OPT_INTEGER('p', "pid", &target_pid,
693 "record events on existing pid"),
694 OPT_INTEGER('r', "realtime", &realtime_prio,
695 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200696 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
697 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200698 OPT_BOOLEAN('a', "all-cpus", &system_wide,
699 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200700 OPT_BOOLEAN('A', "append", &append_file,
701 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200702 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
703 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200704 OPT_BOOLEAN('f', "force", &force,
705 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200706 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200707 "event period to sample"),
708 OPT_STRING('o', "output", &output_name, "file",
709 "output file name"),
710 OPT_BOOLEAN('i', "inherit", &inherit,
711 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200712 OPT_INTEGER('F', "freq", &freq,
713 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200714 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
715 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200716 OPT_BOOLEAN('g', "call-graph", &call_graph,
717 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200718 OPT_BOOLEAN('v', "verbose", &verbose,
719 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200720 OPT_BOOLEAN('s', "stat", &inherit_stat,
721 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200722 OPT_BOOLEAN('d', "data", &sample_address,
723 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200724 OPT_BOOLEAN('n', "no-samples", &no_samples,
725 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200726 OPT_BOOLEAN('M', "multiplex", &multiplex,
727 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200728 OPT_END()
729};
730
Ingo Molnarf37a2912009-07-01 12:37:06 +0200731int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200732{
733 int counter;
734
Anton Blancharda0541232009-07-22 23:04:12 +1000735 argc = parse_options(argc, argv, options, record_usage,
736 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200737 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200738 usage_with_options(record_usage, options);
739
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200740 if (!nr_counters) {
741 nr_counters = 1;
742 attrs[0].type = PERF_TYPE_HARDWARE;
743 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
744 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200745
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200746 /*
747 * User specified count overrides default frequency.
748 */
749 if (default_interval)
750 freq = 0;
751 else if (freq) {
752 default_interval = freq;
753 } else {
754 fprintf(stderr, "frequency and count are zero, aborting\n");
755 exit(EXIT_FAILURE);
756 }
757
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200758 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200759 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200760 continue;
761
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200762 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200763 }
764
765 return __cmd_record(argc, argv);
766}