blob: 3eeef339c787bdb414a0faba3e31d02a6efa2a02 [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 Weisbecker8f288272009-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;
Chris Wilson933da832009-10-04 01:35:01 +010044static pid_t child_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020045static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020046static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020047static int append_file = 0;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020048static int call_graph = 0;
Peter Zijlstra649c48a2009-06-24 21:12:48 +020049static int inherit_stat = 0;
50static int no_samples = 0;
Anton Blanchard4bba8282009-07-16 15:44:29 +020051static int sample_address = 0;
Frederic Weisbeckerd1302522009-09-14 08:57:15 +020052static int multiplex = 0;
Ingo Molnarea57c4f2009-09-13 18:15:54 +020053static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020054
Ingo Molnara21ca2c2009-06-06 09:58:57 +020055static long samples;
56static struct timeval last_read;
57static struct timeval this_read;
58
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100059static u64 bytes_written;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020060
61static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
62
63static int nr_poll;
64static int nr_cpu;
65
Peter Zijlstraf5970552009-06-18 23:22:55 +020066static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020067
68struct perf_header *header;
Peter Zijlstraf5970552009-06-18 23:22:55 +020069
Ingo Molnara21ca2c2009-06-06 09:58:57 +020070struct mmap_data {
71 int counter;
72 void *base;
73 unsigned int mask;
74 unsigned int prev;
75};
76
77static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
78
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020079static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020080{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020081 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020082 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020083
84 head = pc->data_head;
85 rmb();
86
87 return head;
88}
89
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020090static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
91{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020092 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020093
94 /*
95 * ensure all reads are done before we write the tail out.
96 */
97 /* mb(); */
98 pc->data_tail = tail;
99}
100
Peter Zijlstraf5970552009-06-18 23:22:55 +0200101static void write_output(void *buf, size_t size)
102{
103 while (size) {
104 int ret = write(output, buf, size);
105
106 if (ret < 0)
107 die("failed to write");
108
109 size -= ret;
110 buf += ret;
111
112 bytes_written += ret;
113 }
114}
115
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200116static void mmap_read(struct mmap_data *md)
117{
118 unsigned int head = mmap_read_head(md);
119 unsigned int old = md->prev;
120 unsigned char *data = md->base + page_size;
121 unsigned long size;
122 void *buf;
123 int diff;
124
125 gettimeofday(&this_read, NULL);
126
127 /*
128 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200129 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200130 *
131 * If we somehow ended up ahead of the head, we got messed up.
132 *
133 * In either case, truncate and restart at head.
134 */
135 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200136 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200137 struct timeval iv;
138 unsigned long msecs;
139
140 timersub(&this_read, &last_read, &iv);
141 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
142
143 fprintf(stderr, "WARNING: failed to keep up with mmap data."
144 " Last read %lu msecs ago.\n", msecs);
145
146 /*
147 * head points to a known good entry, start there.
148 */
149 old = head;
150 }
151
152 last_read = this_read;
153
154 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200155 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200156
157 size = head - old;
158
159 if ((old & md->mask) + size != (head & md->mask)) {
160 buf = &data[old & md->mask];
161 size = md->mask + 1 - (old & md->mask);
162 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200163
Peter Zijlstraf5970552009-06-18 23:22:55 +0200164 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200165 }
166
167 buf = &data[old & md->mask];
168 size = head - old;
169 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200170
Peter Zijlstraf5970552009-06-18 23:22:55 +0200171 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200172
173 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200174 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200175}
176
177static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200178static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200179
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200180static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200181{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200182 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200183 signr = sig;
184}
185
186static void sig_atexit(void)
187{
Chris Wilson933da832009-10-04 01:35:01 +0100188 if (child_pid != -1)
189 kill(child_pid, SIGTERM);
190
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200191 if (signr == -1)
192 return;
193
194 signal(signr, SIG_DFL);
195 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200196}
197
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300198static pid_t pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300199{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300200 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200201 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200202 char bf[BUFSIZ];
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300203 FILE *fp;
204 size_t size = 0;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200205 DIR *tasks;
206 struct dirent dirent, *next;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300207 pid_t tgid = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300208
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300209 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300210
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300211 fp = fopen(filename, "r");
Arnaldo Carvalho de Melo39e6dd72009-08-14 15:26:32 -0300212 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200213 /*
214 * We raced with a task exiting - just return:
215 */
216 if (verbose)
217 fprintf(stderr, "couldn't open %s\n", filename);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300218 return 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300219 }
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300220
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300221 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300222 while (!comm_ev.comm[0] || !comm_ev.pid) {
223 if (fgets(bf, sizeof(bf), fp) == NULL)
224 goto out_failure;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200225
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300226 if (memcmp(bf, "Name:", 5) == 0) {
227 char *name = bf + 5;
228 while (*name && isspace(*name))
229 ++name;
230 size = strlen(name) - 1;
231 memcpy(comm_ev.comm, name, size++);
232 } else if (memcmp(bf, "Tgid:", 5) == 0) {
233 char *tgids = bf + 5;
234 while (*tgids && isspace(*tgids))
235 ++tgids;
236 tgid = comm_ev.pid = atoi(tgids);
237 }
238 }
239
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200240 comm_ev.header.type = PERF_RECORD_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000241 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300242 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200243
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200244 if (!full) {
245 comm_ev.tid = pid;
246
Peter Zijlstraf5970552009-06-18 23:22:55 +0200247 write_output(&comm_ev, comm_ev.header.size);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300248 goto out_fclose;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300249 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200250
251 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
252
253 tasks = opendir(filename);
254 while (!readdir_r(tasks, &dirent, &next) && next) {
255 char *end;
256 pid = strtol(dirent.d_name, &end, 10);
257 if (*end)
258 continue;
259
260 comm_ev.tid = pid;
261
Peter Zijlstraf5970552009-06-18 23:22:55 +0200262 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200263 }
264 closedir(tasks);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300265
266out_fclose:
267 fclose(fp);
268 return tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200269
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300270out_failure:
271 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
272 filename);
273 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300274}
275
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300276static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300277{
278 char filename[PATH_MAX];
279 FILE *fp;
280
281 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
282
283 fp = fopen(filename, "r");
284 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200285 /*
286 * We raced with a task exiting - just return:
287 */
288 if (verbose)
289 fprintf(stderr, "couldn't open %s\n", filename);
290 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300291 }
292 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300293 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300294 struct mmap_event mmap_ev = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200295 .header = { .type = PERF_RECORD_MMAP },
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300296 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300297 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300298 size_t size;
299 if (fgets(bf, sizeof(bf), fp) == NULL)
300 break;
301
302 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300303 n = hex2u64(pbf, &mmap_ev.start);
304 if (n < 0)
305 continue;
306 pbf += n + 1;
307 n = hex2u64(pbf, &mmap_ev.len);
308 if (n < 0)
309 continue;
310 pbf += n + 3;
311 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200312 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300313
Anton Blanchard11b5f812009-07-16 15:44:29 +0200314 /* Catch VDSO */
315 if (execname == NULL)
316 execname = strstr(bf, "[vdso]");
317
Johannes Weiner76c64c52009-06-24 21:08:36 +0200318 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300319 continue;
320
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300321 size = strlen(execname);
322 execname[size - 1] = '\0'; /* Remove \n */
323 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000324 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300325 mmap_ev.len -= mmap_ev.start;
326 mmap_ev.header.size = (sizeof(mmap_ev) -
327 (sizeof(mmap_ev.filename) - size));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300328 mmap_ev.pid = tgid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300329 mmap_ev.tid = pid;
330
Peter Zijlstraf5970552009-06-18 23:22:55 +0200331 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300332 }
333 }
334
335 fclose(fp);
336}
337
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200338static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200339{
340 DIR *proc;
341 struct dirent dirent, *next;
342
343 proc = opendir("/proc");
344
345 while (!readdir_r(proc, &dirent, &next) && next) {
346 char *end;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300347 pid_t pid, tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200348
349 pid = strtol(dirent.d_name, &end, 10);
350 if (*end) /* only interested in proper numerical dirents */
351 continue;
352
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300353 tgid = pid_synthesize_comm_event(pid, 1);
354 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200355 }
356
357 closedir(proc);
358}
359
Ingo Molnarf250c0302009-06-05 13:18:41 +0200360static int group_fd;
361
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200362static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200363{
364 struct perf_header_attr *h_attr;
365
366 if (nr < header->attrs) {
367 h_attr = header->attr[nr];
368 } else {
369 h_attr = perf_header_attr__new(a);
370 perf_header__add_attr(header, h_attr);
371 }
372
373 return h_attr;
374}
375
Ingo Molnarf250c0302009-06-05 13:18:41 +0200376static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200377{
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 */
381 struct {
382 u64 count;
383 u64 time_enabled;
384 u64 time_running;
385 u64 id;
386 } read_data;
387
388 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
389 PERF_FORMAT_TOTAL_TIME_RUNNING |
390 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200391
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200392 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200393
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200394 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200395 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200396 attr->freq = 1;
397 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200398 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200399
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200400 if (no_samples)
401 attr->sample_freq = 0;
402
403 if (inherit_stat)
404 attr->inherit_stat = 1;
405
Anton Blanchard4bba8282009-07-16 15:44:29 +0200406 if (sample_address)
407 attr->sample_type |= PERF_SAMPLE_ADDR;
408
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200409 if (call_graph)
410 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
411
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200412 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200413 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200414 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200415 attr->sample_type |= PERF_SAMPLE_CPU;
416 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200417
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200418 attr->mmap = track;
419 attr->comm = track;
420 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200421 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200422
Ingo Molnar3da297a2009-06-07 17:39:02 +0200423try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200424 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200425
426 if (fd[nr_cpu][counter] < 0) {
427 int err = errno;
428
Ingo Molnarf250c0302009-06-05 13:18:41 +0200429 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200430 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200431 else if (err == ENODEV && profile_cpu != -1)
432 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200433
434 /*
435 * If it's cycles then fall back to hrtimer
436 * based cpu-clock-tick sw counter, which
437 * is always available even if no PMU support:
438 */
439 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200440 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200441
442 if (verbose)
443 warning(" ... trying to fall back to cpu-clock-ticks\n");
444 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200445 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200446 goto try_again;
447 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200448 printf("\n");
449 error("perfcounter syscall returned with %d (%s)\n",
450 fd[nr_cpu][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200451 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200452 exit(-1);
453 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200454
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200455 h_attr = get_header_attr(attr, counter);
456
457 if (!file_new) {
458 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
459 fprintf(stderr, "incompatible append\n");
460 exit(-1);
461 }
462 }
463
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200464 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
465 perror("Unable to read perf file descriptor\n");
466 exit(-1);
467 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200468
469 perf_header_attr__add_id(h_attr, read_data.id);
470
Ingo Molnarf250c0302009-06-05 13:18:41 +0200471 assert(fd[nr_cpu][counter] >= 0);
472 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
473
474 /*
475 * First counter acts as the group leader:
476 */
477 if (group && group_fd == -1)
478 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200479 if (multiplex && multiplex_fd == -1)
480 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200481
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200482 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
483 int ret;
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
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200503 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200504}
505
506static void open_counters(int cpu, pid_t pid)
507{
508 int counter;
509
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200510 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200511 for (counter = 0; counter < nr_counters; counter++)
512 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200513
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200514 nr_cpu++;
515}
516
Peter Zijlstraf5970552009-06-18 23:22:55 +0200517static void atexit_header(void)
518{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200519 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200520
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200521 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200522}
523
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200524static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200525{
526 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200527 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200528 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200529 int flags;
530 int ret;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200531 unsigned long waking = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200532
533 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200534 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
535 assert(nr_cpus <= MAX_NR_CPUS);
536 assert(nr_cpus >= 0);
537
Peter Zijlstraf5970552009-06-18 23:22:55 +0200538 atexit(sig_atexit);
539 signal(SIGCHLD, sig_handler);
540 signal(SIGINT, sig_handler);
541
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200542 if (!stat(output_name, &st) && st.st_size) {
543 if (!force && !append_file) {
544 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
545 output_name);
546 exit(-1);
547 }
548 } else {
549 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200550 }
551
Ingo Molnarabaff322009-06-02 22:59:57 +0200552 flags = O_CREAT|O_RDWR;
553 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200554 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200555 else
556 flags |= O_TRUNC;
557
558 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200559 if (output < 0) {
560 perror("failed to create output file");
561 exit(-1);
562 }
563
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200564 if (!file_new)
565 header = perf_header__read(output);
566 else
567 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200568
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200569
570 if (raw_samples) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200571 read_tracing_data(attrs, nr_counters);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200572 } else {
573 for (i = 0; i < nr_counters; i++) {
574 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200575 read_tracing_data(attrs, nr_counters);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200576 break;
577 }
578 }
579 }
Peter Zijlstraf5970552009-06-18 23:22:55 +0200580 atexit(atexit_header);
581
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300582 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200583 pid = target_pid;
584 if (pid == -1)
585 pid = getpid();
586
Jens Axboe0a5ac842009-08-12 11:18:01 +0200587 open_counters(profile_cpu, pid);
588 } else {
589 if (profile_cpu != -1) {
590 open_counters(profile_cpu, target_pid);
591 } else {
592 for (i = 0; i < nr_cpus; i++)
593 open_counters(i, target_pid);
594 }
595 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200596
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200597 if (file_new)
598 perf_header__write(header, output);
599
600 if (!system_wide) {
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300601 pid_t tgid = pid_synthesize_comm_event(pid, 0);
602 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200603 } else
604 synthesize_all();
605
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200606 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300607 pid = fork();
608 if (pid < 0)
609 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200610
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300611 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200612 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300613 perror(argv[0]);
614 exit(-1);
615 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200616 }
Chris Wilson933da832009-10-04 01:35:01 +0100617
618 child_pid = pid;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200619 }
620
621 if (realtime_prio) {
622 struct sched_param param;
623
624 param.sched_priority = realtime_prio;
625 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
626 printf("Could not set realtime priority.\n");
627 exit(-1);
628 }
629 }
630
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200631 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200632 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200633
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200634 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200635 for (counter = 0; counter < nr_counters; counter++) {
636 if (mmap_array[i][counter].base)
637 mmap_read(&mmap_array[i][counter]);
638 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200639 }
640
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200641 if (hits == samples) {
642 if (done)
643 break;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200644 ret = poll(event_array, nr_poll, -1);
645 waking++;
646 }
647
648 if (done) {
649 for (i = 0; i < nr_cpu; i++) {
650 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200651 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200652 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200653 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200654 }
655
Peter Zijlstra8b412662009-09-17 19:59:05 +0200656 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
657
Ingo Molnar021e9f42009-06-03 19:27:19 +0200658 /*
659 * Approximate RIP event size: 24 bytes.
660 */
661 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200662 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200663 (double)bytes_written / 1024.0 / 1024.0,
664 output_name,
665 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200666
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200667 return 0;
668}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200669
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200670static const char * const record_usage[] = {
Mike Galbraith9e096752009-05-28 16:25:34 +0200671 "perf record [<options>] [<command>]",
672 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200673 NULL
674};
675
Ingo Molnar52425192009-05-26 09:17:18 +0200676static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200677 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200678 "event selector. use 'perf list' to list available events",
679 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200680 OPT_INTEGER('p', "pid", &target_pid,
681 "record events on existing pid"),
682 OPT_INTEGER('r', "realtime", &realtime_prio,
683 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200684 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
685 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200686 OPT_BOOLEAN('a', "all-cpus", &system_wide,
687 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200688 OPT_BOOLEAN('A', "append", &append_file,
689 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200690 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
691 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200692 OPT_BOOLEAN('f', "force", &force,
693 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200694 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200695 "event period to sample"),
696 OPT_STRING('o', "output", &output_name, "file",
697 "output file name"),
698 OPT_BOOLEAN('i', "inherit", &inherit,
699 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200700 OPT_INTEGER('F', "freq", &freq,
701 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200702 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
703 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200704 OPT_BOOLEAN('g', "call-graph", &call_graph,
705 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200706 OPT_BOOLEAN('v', "verbose", &verbose,
707 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200708 OPT_BOOLEAN('s', "stat", &inherit_stat,
709 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200710 OPT_BOOLEAN('d', "data", &sample_address,
711 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200712 OPT_BOOLEAN('n', "no-samples", &no_samples,
713 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200714 OPT_BOOLEAN('M', "multiplex", &multiplex,
715 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200716 OPT_END()
717};
718
Ingo Molnarf37a2912009-07-01 12:37:06 +0200719int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200720{
721 int counter;
722
Anton Blancharda0541232009-07-22 23:04:12 +1000723 argc = parse_options(argc, argv, options, record_usage,
724 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200725 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200726 usage_with_options(record_usage, options);
727
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200728 if (!nr_counters) {
729 nr_counters = 1;
730 attrs[0].type = PERF_TYPE_HARDWARE;
731 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
732 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200733
734 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200735 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200736 continue;
737
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200738 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200739 }
740
741 return __cmd_record(argc, argv);
742}