blob: 79f99dba5be0ce493d37c48a0fb765aca6b355b3 [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;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020052
Ingo Molnara21ca2c2009-06-06 09:58:57 +020053static long samples;
54static struct timeval last_read;
55static struct timeval this_read;
56
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100057static u64 bytes_written;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020058
59static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
60
61static int nr_poll;
62static int nr_cpu;
63
Peter Zijlstraf5970552009-06-18 23:22:55 +020064static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020065
66struct perf_header *header;
Peter Zijlstraf5970552009-06-18 23:22:55 +020067
Ingo Molnara21ca2c2009-06-06 09:58:57 +020068struct mmap_data {
69 int counter;
70 void *base;
71 unsigned int mask;
72 unsigned int prev;
73};
74
75static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
76
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020077static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020078{
79 struct perf_counter_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020080 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020081
82 head = pc->data_head;
83 rmb();
84
85 return head;
86}
87
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020088static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
89{
90 struct perf_counter_mmap_page *pc = md->base;
91
92 /*
93 * ensure all reads are done before we write the tail out.
94 */
95 /* mb(); */
96 pc->data_tail = tail;
97}
98
Peter Zijlstraf5970552009-06-18 23:22:55 +020099static void write_output(void *buf, size_t size)
100{
101 while (size) {
102 int ret = write(output, buf, size);
103
104 if (ret < 0)
105 die("failed to write");
106
107 size -= ret;
108 buf += ret;
109
110 bytes_written += ret;
111 }
112}
113
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200114static void mmap_read(struct mmap_data *md)
115{
116 unsigned int head = mmap_read_head(md);
117 unsigned int old = md->prev;
118 unsigned char *data = md->base + page_size;
119 unsigned long size;
120 void *buf;
121 int diff;
122
123 gettimeofday(&this_read, NULL);
124
125 /*
126 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200127 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200128 *
129 * If we somehow ended up ahead of the head, we got messed up.
130 *
131 * In either case, truncate and restart at head.
132 */
133 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200134 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200135 struct timeval iv;
136 unsigned long msecs;
137
138 timersub(&this_read, &last_read, &iv);
139 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
140
141 fprintf(stderr, "WARNING: failed to keep up with mmap data."
142 " Last read %lu msecs ago.\n", msecs);
143
144 /*
145 * head points to a known good entry, start there.
146 */
147 old = head;
148 }
149
150 last_read = this_read;
151
152 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200153 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200154
155 size = head - old;
156
157 if ((old & md->mask) + size != (head & md->mask)) {
158 buf = &data[old & md->mask];
159 size = md->mask + 1 - (old & md->mask);
160 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200161
Peter Zijlstraf5970552009-06-18 23:22:55 +0200162 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200163 }
164
165 buf = &data[old & md->mask];
166 size = head - old;
167 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200168
Peter Zijlstraf5970552009-06-18 23:22:55 +0200169 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200170
171 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200172 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200173}
174
175static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200176static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200177
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200178static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200179{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200180 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200181 signr = sig;
182}
183
184static void sig_atexit(void)
185{
186 if (signr == -1)
187 return;
188
189 signal(signr, SIG_DFL);
190 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200191}
192
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300193static pid_t pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300194{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300195 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200196 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200197 char bf[BUFSIZ];
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300198 FILE *fp;
199 size_t size = 0;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200200 DIR *tasks;
201 struct dirent dirent, *next;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300202 pid_t tgid = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300203
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300204 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300205
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300206 fp = fopen(filename, "r");
Arnaldo Carvalho de Melo39e6dd72009-08-14 15:26:32 -0300207 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200208 /*
209 * We raced with a task exiting - just return:
210 */
211 if (verbose)
212 fprintf(stderr, "couldn't open %s\n", filename);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300213 return 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300214 }
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300215
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300216 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300217 while (!comm_ev.comm[0] || !comm_ev.pid) {
218 if (fgets(bf, sizeof(bf), fp) == NULL)
219 goto out_failure;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200220
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300221 if (memcmp(bf, "Name:", 5) == 0) {
222 char *name = bf + 5;
223 while (*name && isspace(*name))
224 ++name;
225 size = strlen(name) - 1;
226 memcpy(comm_ev.comm, name, size++);
227 } else if (memcmp(bf, "Tgid:", 5) == 0) {
228 char *tgids = bf + 5;
229 while (*tgids && isspace(*tgids))
230 ++tgids;
231 tgid = comm_ev.pid = atoi(tgids);
232 }
233 }
234
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300235 comm_ev.header.type = PERF_EVENT_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000236 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300237 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200238
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200239 if (!full) {
240 comm_ev.tid = pid;
241
Peter Zijlstraf5970552009-06-18 23:22:55 +0200242 write_output(&comm_ev, comm_ev.header.size);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300243 goto out_fclose;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300244 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200245
246 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
247
248 tasks = opendir(filename);
249 while (!readdir_r(tasks, &dirent, &next) && next) {
250 char *end;
251 pid = strtol(dirent.d_name, &end, 10);
252 if (*end)
253 continue;
254
255 comm_ev.tid = pid;
256
Peter Zijlstraf5970552009-06-18 23:22:55 +0200257 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200258 }
259 closedir(tasks);
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300260
261out_fclose:
262 fclose(fp);
263 return tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200264
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300265out_failure:
266 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
267 filename);
268 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300269}
270
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300271static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300272{
273 char filename[PATH_MAX];
274 FILE *fp;
275
276 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
277
278 fp = fopen(filename, "r");
279 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200280 /*
281 * We raced with a task exiting - just return:
282 */
283 if (verbose)
284 fprintf(stderr, "couldn't open %s\n", filename);
285 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300286 }
287 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300288 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300289 struct mmap_event mmap_ev = {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200290 .header = { .type = PERF_EVENT_MMAP },
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300291 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300292 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300293 size_t size;
294 if (fgets(bf, sizeof(bf), fp) == NULL)
295 break;
296
297 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300298 n = hex2u64(pbf, &mmap_ev.start);
299 if (n < 0)
300 continue;
301 pbf += n + 1;
302 n = hex2u64(pbf, &mmap_ev.len);
303 if (n < 0)
304 continue;
305 pbf += n + 3;
306 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200307 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300308
Anton Blanchard11b5f812009-07-16 15:44:29 +0200309 /* Catch VDSO */
310 if (execname == NULL)
311 execname = strstr(bf, "[vdso]");
312
Johannes Weiner76c64c52009-06-24 21:08:36 +0200313 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300314 continue;
315
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300316 size = strlen(execname);
317 execname[size - 1] = '\0'; /* Remove \n */
318 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000319 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300320 mmap_ev.len -= mmap_ev.start;
321 mmap_ev.header.size = (sizeof(mmap_ev) -
322 (sizeof(mmap_ev.filename) - size));
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300323 mmap_ev.pid = tgid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300324 mmap_ev.tid = pid;
325
Peter Zijlstraf5970552009-06-18 23:22:55 +0200326 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300327 }
328 }
329
330 fclose(fp);
331}
332
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200333static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200334{
335 DIR *proc;
336 struct dirent dirent, *next;
337
338 proc = opendir("/proc");
339
340 while (!readdir_r(proc, &dirent, &next) && next) {
341 char *end;
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300342 pid_t pid, tgid;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200343
344 pid = strtol(dirent.d_name, &end, 10);
345 if (*end) /* only interested in proper numerical dirents */
346 continue;
347
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300348 tgid = pid_synthesize_comm_event(pid, 1);
349 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200350 }
351
352 closedir(proc);
353}
354
Ingo Molnarf250c0302009-06-05 13:18:41 +0200355static int group_fd;
356
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200357static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
358{
359 struct perf_header_attr *h_attr;
360
361 if (nr < header->attrs) {
362 h_attr = header->attr[nr];
363 } else {
364 h_attr = perf_header_attr__new(a);
365 perf_header__add_attr(header, h_attr);
366 }
367
368 return h_attr;
369}
370
Ingo Molnarf250c0302009-06-05 13:18:41 +0200371static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200372{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200373 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200374 struct perf_header_attr *h_attr;
375 int track = !counter; /* only the first counter needs these */
376 struct {
377 u64 count;
378 u64 time_enabled;
379 u64 time_running;
380 u64 id;
381 } read_data;
382
383 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
384 PERF_FORMAT_TOTAL_TIME_RUNNING |
385 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200386
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200387 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200388
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200389 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200390 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200391 attr->freq = 1;
392 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200393 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200394
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200395 if (no_samples)
396 attr->sample_freq = 0;
397
398 if (inherit_stat)
399 attr->inherit_stat = 1;
400
Anton Blanchard4bba8282009-07-16 15:44:29 +0200401 if (sample_address)
402 attr->sample_type |= PERF_SAMPLE_ADDR;
403
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200404 if (call_graph)
405 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
406
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200407 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200408 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200409 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200410 attr->sample_type |= PERF_SAMPLE_CPU;
411 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200412
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200413 attr->mmap = track;
414 attr->comm = track;
415 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200416 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200417
Ingo Molnar3da297a2009-06-07 17:39:02 +0200418try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200419 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200420
421 if (fd[nr_cpu][counter] < 0) {
422 int err = errno;
423
Ingo Molnarf250c0302009-06-05 13:18:41 +0200424 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200425 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200426 else if (err == ENODEV && profile_cpu != -1)
427 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200428
429 /*
430 * If it's cycles then fall back to hrtimer
431 * based cpu-clock-tick sw counter, which
432 * is always available even if no PMU support:
433 */
434 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200435 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200436
437 if (verbose)
438 warning(" ... trying to fall back to cpu-clock-ticks\n");
439 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200440 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200441 goto try_again;
442 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200443 printf("\n");
444 error("perfcounter syscall returned with %d (%s)\n",
445 fd[nr_cpu][counter], strerror(err));
446 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200447 exit(-1);
448 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200449
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200450 h_attr = get_header_attr(attr, counter);
451
452 if (!file_new) {
453 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
454 fprintf(stderr, "incompatible append\n");
455 exit(-1);
456 }
457 }
458
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200459 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
460 perror("Unable to read perf file descriptor\n");
461 exit(-1);
462 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200463
464 perf_header_attr__add_id(h_attr, read_data.id);
465
Ingo Molnarf250c0302009-06-05 13:18:41 +0200466 assert(fd[nr_cpu][counter] >= 0);
467 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
468
469 /*
470 * First counter acts as the group leader:
471 */
472 if (group && group_fd == -1)
473 group_fd = fd[nr_cpu][counter];
474
475 event_array[nr_poll].fd = fd[nr_cpu][counter];
476 event_array[nr_poll].events = POLLIN;
477 nr_poll++;
478
479 mmap_array[nr_cpu][counter].counter = counter;
480 mmap_array[nr_cpu][counter].prev = 0;
481 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
482 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200483 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200484 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
485 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
486 exit(-1);
487 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200488
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200489 if (multiplex && fd[nr_cpu][counter] != group_fd)
490 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_SET_OUTPUT, group_fd);
491
Peter Zijlstra4502d772009-06-10 15:03:06 +0200492 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200493}
494
495static void open_counters(int cpu, pid_t pid)
496{
497 int counter;
498
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200499 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200500 for (counter = 0; counter < nr_counters; counter++)
501 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200502
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200503 nr_cpu++;
504}
505
Peter Zijlstraf5970552009-06-18 23:22:55 +0200506static void atexit_header(void)
507{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200508 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200509
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200510 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200511}
512
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200513static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200514{
515 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200516 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200517 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200518 int flags;
519 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200520
521 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200522 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
523 assert(nr_cpus <= MAX_NR_CPUS);
524 assert(nr_cpus >= 0);
525
Peter Zijlstraf5970552009-06-18 23:22:55 +0200526 atexit(sig_atexit);
527 signal(SIGCHLD, sig_handler);
528 signal(SIGINT, sig_handler);
529
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200530 if (!stat(output_name, &st) && st.st_size) {
531 if (!force && !append_file) {
532 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
533 output_name);
534 exit(-1);
535 }
536 } else {
537 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200538 }
539
Ingo Molnarabaff322009-06-02 22:59:57 +0200540 flags = O_CREAT|O_RDWR;
541 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200542 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200543 else
544 flags |= O_TRUNC;
545
546 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200547 if (output < 0) {
548 perror("failed to create output file");
549 exit(-1);
550 }
551
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200552 if (!file_new)
553 header = perf_header__read(output);
554 else
555 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200556
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200557
558 if (raw_samples) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200559 read_tracing_data(attrs, nr_counters);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200560 } else {
561 for (i = 0; i < nr_counters; i++) {
562 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200563 read_tracing_data(attrs, nr_counters);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200564 break;
565 }
566 }
567 }
Peter Zijlstraf5970552009-06-18 23:22:55 +0200568 atexit(atexit_header);
569
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300570 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200571 pid = target_pid;
572 if (pid == -1)
573 pid = getpid();
574
Jens Axboe0a5ac842009-08-12 11:18:01 +0200575 open_counters(profile_cpu, pid);
576 } else {
577 if (profile_cpu != -1) {
578 open_counters(profile_cpu, target_pid);
579 } else {
580 for (i = 0; i < nr_cpus; i++)
581 open_counters(i, target_pid);
582 }
583 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200584
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200585 if (file_new)
586 perf_header__write(header, output);
587
588 if (!system_wide) {
Arnaldo Carvalho de Melo2a8083f2009-08-11 16:22:00 -0300589 pid_t tgid = pid_synthesize_comm_event(pid, 0);
590 pid_synthesize_mmap_samples(pid, tgid);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200591 } else
592 synthesize_all();
593
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200594 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300595 pid = fork();
596 if (pid < 0)
597 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200598
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300599 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200600 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300601 perror(argv[0]);
602 exit(-1);
603 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200604 }
605 }
606
607 if (realtime_prio) {
608 struct sched_param param;
609
610 param.sched_priority = realtime_prio;
611 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
612 printf("Could not set realtime priority.\n");
613 exit(-1);
614 }
615 }
616
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200617 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200618 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200619
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200620 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200621 for (counter = 0; counter < nr_counters; counter++)
622 mmap_read(&mmap_array[i][counter]);
623 }
624
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200625 if (hits == samples) {
626 if (done)
627 break;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200628 ret = poll(event_array, nr_poll, 100);
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200629 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200630 }
631
Ingo Molnar021e9f42009-06-03 19:27:19 +0200632 /*
633 * Approximate RIP event size: 24 bytes.
634 */
635 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200636 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200637 (double)bytes_written / 1024.0 / 1024.0,
638 output_name,
639 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200640
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200641 return 0;
642}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200643
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200644static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200645 "perf record [<options>] [<command>]",
646 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200647 NULL
648};
649
Ingo Molnar52425192009-05-26 09:17:18 +0200650static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200651 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200652 "event selector. use 'perf list' to list available events",
653 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200654 OPT_INTEGER('p', "pid", &target_pid,
655 "record events on existing pid"),
656 OPT_INTEGER('r', "realtime", &realtime_prio,
657 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200658 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
659 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200660 OPT_BOOLEAN('a', "all-cpus", &system_wide,
661 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200662 OPT_BOOLEAN('A', "append", &append_file,
663 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200664 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
665 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200666 OPT_BOOLEAN('f', "force", &force,
667 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200668 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200669 "event period to sample"),
670 OPT_STRING('o', "output", &output_name, "file",
671 "output file name"),
672 OPT_BOOLEAN('i', "inherit", &inherit,
673 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200674 OPT_INTEGER('F', "freq", &freq,
675 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200676 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
677 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200678 OPT_BOOLEAN('g', "call-graph", &call_graph,
679 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200680 OPT_BOOLEAN('v', "verbose", &verbose,
681 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200682 OPT_BOOLEAN('s', "stat", &inherit_stat,
683 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200684 OPT_BOOLEAN('d', "data", &sample_address,
685 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200686 OPT_BOOLEAN('n', "no-samples", &no_samples,
687 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200688 OPT_BOOLEAN('M', "multiplex", &multiplex,
689 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200690 OPT_END()
691};
692
Ingo Molnarf37a2912009-07-01 12:37:06 +0200693int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200694{
695 int counter;
696
Anton Blancharda0541232009-07-22 23:04:12 +1000697 argc = parse_options(argc, argv, options, record_usage,
698 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200699 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200700 usage_with_options(record_usage, options);
701
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200702 if (!nr_counters) {
703 nr_counters = 1;
704 attrs[0].type = PERF_TYPE_HARDWARE;
705 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
706 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200707
708 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200709 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200710 continue;
711
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200712 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200713 }
714
715 return __cmd_record(argc, argv);
716}