blob: 106c6abd1c38ad18c411034115449d3d84562441 [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"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019
Peter Zijlstra97124d52009-06-02 15:52:24 +020020#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020021#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020022
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020023#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
24#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030025
Peter Zijlstrade9ac072009-04-08 15:01:31 +020026static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020027
28static long default_interval = 100000;
29
Ingo Molnar3cf165f2009-06-02 23:02:59 +020030static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020031static unsigned int page_size;
Ingo Molnar3cf165f2009-06-02 23:02:59 +020032static unsigned int mmap_pages = 128;
Ingo Molnarcf1f4572009-06-05 13:27:02 +020033static int freq = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020034static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020035static const char *output_name = "perf.data";
Peter Zijlstrade9ac072009-04-08 15:01:31 +020036static int group = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020037static unsigned int realtime_prio = 0;
38static int system_wide = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030039static pid_t target_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020040static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020041static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020042static int append_file = 0;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020043static int call_graph = 0;
Peter Zijlstra649c48a2009-06-24 21:12:48 +020044static int inherit_stat = 0;
45static int no_samples = 0;
Anton Blanchard4bba8282009-07-16 15:44:29 +020046static int sample_address = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020047
Ingo Molnara21ca2c2009-06-06 09:58:57 +020048static long samples;
49static struct timeval last_read;
50static struct timeval this_read;
51
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100052static u64 bytes_written;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020053
54static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
55
56static int nr_poll;
57static int nr_cpu;
58
Peter Zijlstraf5970552009-06-18 23:22:55 +020059static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020060
61struct perf_header *header;
Peter Zijlstraf5970552009-06-18 23:22:55 +020062
Ingo Molnara21ca2c2009-06-06 09:58:57 +020063struct mmap_data {
64 int counter;
65 void *base;
66 unsigned int mask;
67 unsigned int prev;
68};
69
70static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
71
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020072static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020073{
74 struct perf_counter_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020075 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020076
77 head = pc->data_head;
78 rmb();
79
80 return head;
81}
82
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020083static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
84{
85 struct perf_counter_mmap_page *pc = md->base;
86
87 /*
88 * ensure all reads are done before we write the tail out.
89 */
90 /* mb(); */
91 pc->data_tail = tail;
92}
93
Peter Zijlstraf5970552009-06-18 23:22:55 +020094static void write_output(void *buf, size_t size)
95{
96 while (size) {
97 int ret = write(output, buf, size);
98
99 if (ret < 0)
100 die("failed to write");
101
102 size -= ret;
103 buf += ret;
104
105 bytes_written += ret;
106 }
107}
108
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200109static void mmap_read(struct mmap_data *md)
110{
111 unsigned int head = mmap_read_head(md);
112 unsigned int old = md->prev;
113 unsigned char *data = md->base + page_size;
114 unsigned long size;
115 void *buf;
116 int diff;
117
118 gettimeofday(&this_read, NULL);
119
120 /*
121 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200122 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200123 *
124 * If we somehow ended up ahead of the head, we got messed up.
125 *
126 * In either case, truncate and restart at head.
127 */
128 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200129 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200130 struct timeval iv;
131 unsigned long msecs;
132
133 timersub(&this_read, &last_read, &iv);
134 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
135
136 fprintf(stderr, "WARNING: failed to keep up with mmap data."
137 " Last read %lu msecs ago.\n", msecs);
138
139 /*
140 * head points to a known good entry, start there.
141 */
142 old = head;
143 }
144
145 last_read = this_read;
146
147 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200148 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200149
150 size = head - old;
151
152 if ((old & md->mask) + size != (head & md->mask)) {
153 buf = &data[old & md->mask];
154 size = md->mask + 1 - (old & md->mask);
155 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200156
Peter Zijlstraf5970552009-06-18 23:22:55 +0200157 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200158 }
159
160 buf = &data[old & md->mask];
161 size = head - old;
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 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200167 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200168}
169
170static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200171static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200172
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200173static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200174{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200175 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200176 signr = sig;
177}
178
179static void sig_atexit(void)
180{
181 if (signr == -1)
182 return;
183
184 signal(signr, SIG_DFL);
185 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200186}
187
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200188static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300189{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300190 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200191 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200192 char bf[BUFSIZ];
Peter Zijlstraf5970552009-06-18 23:22:55 +0200193 int fd;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300194 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300195 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200196 DIR *tasks;
197 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300198
199 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
200
201 fd = open(filename, O_RDONLY);
202 if (fd < 0) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200203 /*
204 * We raced with a task exiting - just return:
205 */
206 if (verbose)
207 fprintf(stderr, "couldn't open %s\n", filename);
208 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300209 }
210 if (read(fd, bf, sizeof(bf)) < 0) {
211 fprintf(stderr, "couldn't read %s\n", filename);
212 exit(EXIT_FAILURE);
213 }
214 close(fd);
215
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300216 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300217 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300218 field = strchr(bf, '(');
219 if (field == NULL)
220 goto out_failure;
221 sep = strchr(++field, ')');
222 if (sep == NULL)
223 goto out_failure;
224 size = sep - field;
225 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200226
227 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300228 comm_ev.header.type = PERF_EVENT_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000229 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300230 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200231
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200232 if (!full) {
233 comm_ev.tid = pid;
234
Peter Zijlstraf5970552009-06-18 23:22:55 +0200235 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200236 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300237 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200238
239 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
240
241 tasks = opendir(filename);
242 while (!readdir_r(tasks, &dirent, &next) && next) {
243 char *end;
244 pid = strtol(dirent.d_name, &end, 10);
245 if (*end)
246 continue;
247
248 comm_ev.tid = pid;
249
Peter Zijlstraf5970552009-06-18 23:22:55 +0200250 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200251 }
252 closedir(tasks);
253 return;
254
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300255out_failure:
256 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
257 filename);
258 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300259}
260
Ingo Molnar2debbc82009-06-05 14:29:10 +0200261static void pid_synthesize_mmap_samples(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300262{
263 char filename[PATH_MAX];
264 FILE *fp;
265
266 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
267
268 fp = fopen(filename, "r");
269 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200270 /*
271 * We raced with a task exiting - just return:
272 */
273 if (verbose)
274 fprintf(stderr, "couldn't open %s\n", filename);
275 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300276 }
277 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300278 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300279 struct mmap_event mmap_ev = {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200280 .header = { .type = PERF_EVENT_MMAP },
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300281 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300282 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300283 size_t size;
284 if (fgets(bf, sizeof(bf), fp) == NULL)
285 break;
286
287 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300288 n = hex2u64(pbf, &mmap_ev.start);
289 if (n < 0)
290 continue;
291 pbf += n + 1;
292 n = hex2u64(pbf, &mmap_ev.len);
293 if (n < 0)
294 continue;
295 pbf += n + 3;
296 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200297 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300298
Anton Blanchard11b5f812009-07-16 15:44:29 +0200299 /* Catch VDSO */
300 if (execname == NULL)
301 execname = strstr(bf, "[vdso]");
302
Johannes Weiner76c64c52009-06-24 21:08:36 +0200303 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300304 continue;
305
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300306 size = strlen(execname);
307 execname[size - 1] = '\0'; /* Remove \n */
308 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000309 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300310 mmap_ev.len -= mmap_ev.start;
311 mmap_ev.header.size = (sizeof(mmap_ev) -
312 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200313 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300314 mmap_ev.tid = pid;
315
Peter Zijlstraf5970552009-06-18 23:22:55 +0200316 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300317 }
318 }
319
320 fclose(fp);
321}
322
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200323static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200324{
325 DIR *proc;
326 struct dirent dirent, *next;
327
328 proc = opendir("/proc");
329
330 while (!readdir_r(proc, &dirent, &next) && next) {
331 char *end;
332 pid_t pid;
333
334 pid = strtol(dirent.d_name, &end, 10);
335 if (*end) /* only interested in proper numerical dirents */
336 continue;
337
338 pid_synthesize_comm_event(pid, 1);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200339 pid_synthesize_mmap_samples(pid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200340 }
341
342 closedir(proc);
343}
344
Ingo Molnarf250c0302009-06-05 13:18:41 +0200345static int group_fd;
346
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200347static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
348{
349 struct perf_header_attr *h_attr;
350
351 if (nr < header->attrs) {
352 h_attr = header->attr[nr];
353 } else {
354 h_attr = perf_header_attr__new(a);
355 perf_header__add_attr(header, h_attr);
356 }
357
358 return h_attr;
359}
360
Ingo Molnarf250c0302009-06-05 13:18:41 +0200361static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200362{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200363 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200364 struct perf_header_attr *h_attr;
365 int track = !counter; /* only the first counter needs these */
366 struct {
367 u64 count;
368 u64 time_enabled;
369 u64 time_running;
370 u64 id;
371 } read_data;
372
373 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
374 PERF_FORMAT_TOTAL_TIME_RUNNING |
375 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200376
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200377 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200378
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200379 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200380 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200381 attr->freq = 1;
382 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200383 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200384
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200385 if (no_samples)
386 attr->sample_freq = 0;
387
388 if (inherit_stat)
389 attr->inherit_stat = 1;
390
Anton Blanchard4bba8282009-07-16 15:44:29 +0200391 if (sample_address)
392 attr->sample_type |= PERF_SAMPLE_ADDR;
393
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200394 if (call_graph)
395 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
396
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200397
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200398 attr->mmap = track;
399 attr->comm = track;
400 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200401 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200402
Ingo Molnar3da297a2009-06-07 17:39:02 +0200403try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200404 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200405
406 if (fd[nr_cpu][counter] < 0) {
407 int err = errno;
408
Ingo Molnarf250c0302009-06-05 13:18:41 +0200409 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200410 die("Permission error - are you root?\n");
411
412 /*
413 * If it's cycles then fall back to hrtimer
414 * based cpu-clock-tick sw counter, which
415 * is always available even if no PMU support:
416 */
417 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200418 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200419
420 if (verbose)
421 warning(" ... trying to fall back to cpu-clock-ticks\n");
422 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200423 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200424 goto try_again;
425 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200426 printf("\n");
427 error("perfcounter syscall returned with %d (%s)\n",
428 fd[nr_cpu][counter], strerror(err));
429 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200430 exit(-1);
431 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200432
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200433 h_attr = get_header_attr(attr, counter);
434
435 if (!file_new) {
436 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
437 fprintf(stderr, "incompatible append\n");
438 exit(-1);
439 }
440 }
441
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200442 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
443 perror("Unable to read perf file descriptor\n");
444 exit(-1);
445 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200446
447 perf_header_attr__add_id(h_attr, read_data.id);
448
Ingo Molnarf250c0302009-06-05 13:18:41 +0200449 assert(fd[nr_cpu][counter] >= 0);
450 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
451
452 /*
453 * First counter acts as the group leader:
454 */
455 if (group && group_fd == -1)
456 group_fd = fd[nr_cpu][counter];
457
458 event_array[nr_poll].fd = fd[nr_cpu][counter];
459 event_array[nr_poll].events = POLLIN;
460 nr_poll++;
461
462 mmap_array[nr_cpu][counter].counter = counter;
463 mmap_array[nr_cpu][counter].prev = 0;
464 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
465 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200466 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200467 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
468 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
469 exit(-1);
470 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200471
472 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200473}
474
475static void open_counters(int cpu, pid_t pid)
476{
477 int counter;
478
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200479 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200480 for (counter = 0; counter < nr_counters; counter++)
481 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200482
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200483 nr_cpu++;
484}
485
Peter Zijlstraf5970552009-06-18 23:22:55 +0200486static void atexit_header(void)
487{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200488 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200489
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200490 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200491}
492
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200493static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200494{
495 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200496 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200497 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200498 int flags;
499 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200500
501 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200502 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
503 assert(nr_cpus <= MAX_NR_CPUS);
504 assert(nr_cpus >= 0);
505
Peter Zijlstraf5970552009-06-18 23:22:55 +0200506 atexit(sig_atexit);
507 signal(SIGCHLD, sig_handler);
508 signal(SIGINT, sig_handler);
509
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200510 if (!stat(output_name, &st) && st.st_size) {
511 if (!force && !append_file) {
512 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
513 output_name);
514 exit(-1);
515 }
516 } else {
517 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200518 }
519
Ingo Molnarabaff322009-06-02 22:59:57 +0200520 flags = O_CREAT|O_RDWR;
521 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200522 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200523 else
524 flags |= O_TRUNC;
525
526 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200527 if (output < 0) {
528 perror("failed to create output file");
529 exit(-1);
530 }
531
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200532 if (!file_new)
533 header = perf_header__read(output);
534 else
535 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200536
537 atexit(atexit_header);
538
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300539 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200540 pid = target_pid;
541 if (pid == -1)
542 pid = getpid();
543
544 open_counters(-1, pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300545 } else for (i = 0; i < nr_cpus; i++)
546 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200547
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200548 if (file_new)
549 perf_header__write(header, output);
550
551 if (!system_wide) {
552 pid_synthesize_comm_event(pid, 0);
553 pid_synthesize_mmap_samples(pid);
554 } else
555 synthesize_all();
556
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200557 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300558 pid = fork();
559 if (pid < 0)
560 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200561
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300562 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200563 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300564 perror(argv[0]);
565 exit(-1);
566 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200567 }
568 }
569
570 if (realtime_prio) {
571 struct sched_param param;
572
573 param.sched_priority = realtime_prio;
574 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
575 printf("Could not set realtime priority.\n");
576 exit(-1);
577 }
578 }
579
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200580 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200581 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200582
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200583 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200584 for (counter = 0; counter < nr_counters; counter++)
585 mmap_read(&mmap_array[i][counter]);
586 }
587
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200588 if (hits == samples) {
589 if (done)
590 break;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200591 ret = poll(event_array, nr_poll, 100);
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200592 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200593 }
594
Ingo Molnar021e9f42009-06-03 19:27:19 +0200595 /*
596 * Approximate RIP event size: 24 bytes.
597 */
598 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200599 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200600 (double)bytes_written / 1024.0 / 1024.0,
601 output_name,
602 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200603
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200604 return 0;
605}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200606
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200607static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200608 "perf record [<options>] [<command>]",
609 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200610 NULL
611};
612
Ingo Molnar52425192009-05-26 09:17:18 +0200613static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200614 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200615 "event selector. use 'perf list' to list available events",
616 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200617 OPT_INTEGER('p', "pid", &target_pid,
618 "record events on existing pid"),
619 OPT_INTEGER('r', "realtime", &realtime_prio,
620 "collect data with this RT SCHED_FIFO priority"),
621 OPT_BOOLEAN('a', "all-cpus", &system_wide,
622 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200623 OPT_BOOLEAN('A', "append", &append_file,
624 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200625 OPT_BOOLEAN('f', "force", &force,
626 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200627 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200628 "event period to sample"),
629 OPT_STRING('o', "output", &output_name, "file",
630 "output file name"),
631 OPT_BOOLEAN('i', "inherit", &inherit,
632 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200633 OPT_INTEGER('F', "freq", &freq,
634 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200635 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
636 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200637 OPT_BOOLEAN('g', "call-graph", &call_graph,
638 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200639 OPT_BOOLEAN('v', "verbose", &verbose,
640 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200641 OPT_BOOLEAN('s', "stat", &inherit_stat,
642 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200643 OPT_BOOLEAN('d', "data", &sample_address,
644 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200645 OPT_BOOLEAN('n', "no-samples", &no_samples,
646 "don't sample"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200647 OPT_END()
648};
649
Ingo Molnarf37a2912009-07-01 12:37:06 +0200650int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200651{
652 int counter;
653
Anton Blancharda0541232009-07-22 23:04:12 +1000654 argc = parse_options(argc, argv, options, record_usage,
655 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200656 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200657 usage_with_options(record_usage, options);
658
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200659 if (!nr_counters) {
660 nr_counters = 1;
661 attrs[0].type = PERF_TYPE_HARDWARE;
662 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
663 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200664
665 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200666 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200667 continue;
668
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200669 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200670 }
671
672 return __cmd_record(argc, argv);
673}