blob: 718b8f7b6aac005e34e912fea880968d395d899d [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"
18
Peter Zijlstra97124d52009-06-02 15:52:24 +020019#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020020#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020021
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020022#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
23#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030024
Peter Zijlstrade9ac072009-04-08 15:01:31 +020025static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020026
27static long default_interval = 100000;
28
Ingo Molnar3cf165f2009-06-02 23:02:59 +020029static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020030static unsigned int page_size;
Ingo Molnar3cf165f2009-06-02 23:02:59 +020031static unsigned int mmap_pages = 128;
Ingo Molnarcf1f4572009-06-05 13:27:02 +020032static int freq = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020033static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020034static const char *output_name = "perf.data";
Peter Zijlstrade9ac072009-04-08 15:01:31 +020035static int group = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020036static unsigned int realtime_prio = 0;
37static int system_wide = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030038static pid_t target_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020039static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020040static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020041static int append_file = 0;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020042static int call_graph = 0;
Peter Zijlstra649c48a2009-06-24 21:12:48 +020043static int inherit_stat = 0;
44static int no_samples = 0;
Anton Blanchard4bba8282009-07-16 15:44:29 +020045static int sample_address = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020046
Ingo Molnara21ca2c2009-06-06 09:58:57 +020047static long samples;
48static struct timeval last_read;
49static struct timeval this_read;
50
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100051static u64 bytes_written;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020052
53static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
54
55static int nr_poll;
56static int nr_cpu;
57
Peter Zijlstraf5970552009-06-18 23:22:55 +020058static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059
60struct perf_header *header;
Peter Zijlstraf5970552009-06-18 23:22:55 +020061
Ingo Molnara21ca2c2009-06-06 09:58:57 +020062struct mmap_data {
63 int counter;
64 void *base;
65 unsigned int mask;
66 unsigned int prev;
67};
68
69static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
70
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020071static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020072{
73 struct perf_counter_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020074 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020075
76 head = pc->data_head;
77 rmb();
78
79 return head;
80}
81
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020082static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
83{
84 struct perf_counter_mmap_page *pc = md->base;
85
86 /*
87 * ensure all reads are done before we write the tail out.
88 */
89 /* mb(); */
90 pc->data_tail = tail;
91}
92
Peter Zijlstraf5970552009-06-18 23:22:55 +020093static void write_output(void *buf, size_t size)
94{
95 while (size) {
96 int ret = write(output, buf, size);
97
98 if (ret < 0)
99 die("failed to write");
100
101 size -= ret;
102 buf += ret;
103
104 bytes_written += ret;
105 }
106}
107
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200108static void mmap_read(struct mmap_data *md)
109{
110 unsigned int head = mmap_read_head(md);
111 unsigned int old = md->prev;
112 unsigned char *data = md->base + page_size;
113 unsigned long size;
114 void *buf;
115 int diff;
116
117 gettimeofday(&this_read, NULL);
118
119 /*
120 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200121 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200122 *
123 * If we somehow ended up ahead of the head, we got messed up.
124 *
125 * In either case, truncate and restart at head.
126 */
127 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200128 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200129 struct timeval iv;
130 unsigned long msecs;
131
132 timersub(&this_read, &last_read, &iv);
133 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
134
135 fprintf(stderr, "WARNING: failed to keep up with mmap data."
136 " Last read %lu msecs ago.\n", msecs);
137
138 /*
139 * head points to a known good entry, start there.
140 */
141 old = head;
142 }
143
144 last_read = this_read;
145
146 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200147 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200148
149 size = head - old;
150
151 if ((old & md->mask) + size != (head & md->mask)) {
152 buf = &data[old & md->mask];
153 size = md->mask + 1 - (old & md->mask);
154 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200155
Peter Zijlstraf5970552009-06-18 23:22:55 +0200156 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200157 }
158
159 buf = &data[old & md->mask];
160 size = head - old;
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 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200166 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200167}
168
169static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200170static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200171
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200172static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200173{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200174 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200175 signr = sig;
176}
177
178static void sig_atexit(void)
179{
180 if (signr == -1)
181 return;
182
183 signal(signr, SIG_DFL);
184 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200185}
186
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200187static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300188{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300189 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200190 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200191 char bf[BUFSIZ];
Peter Zijlstraf5970552009-06-18 23:22:55 +0200192 int fd;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300193 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300194 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200195 DIR *tasks;
196 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300197
198 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
199
200 fd = open(filename, O_RDONLY);
201 if (fd < 0) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200202 /*
203 * We raced with a task exiting - just return:
204 */
205 if (verbose)
206 fprintf(stderr, "couldn't open %s\n", filename);
207 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300208 }
209 if (read(fd, bf, sizeof(bf)) < 0) {
210 fprintf(stderr, "couldn't read %s\n", filename);
211 exit(EXIT_FAILURE);
212 }
213 close(fd);
214
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300215 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300216 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300217 field = strchr(bf, '(');
218 if (field == NULL)
219 goto out_failure;
220 sep = strchr(++field, ')');
221 if (sep == NULL)
222 goto out_failure;
223 size = sep - field;
224 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200225
226 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300227 comm_ev.header.type = PERF_EVENT_COMM;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000228 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300229 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200230
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200231 if (!full) {
232 comm_ev.tid = pid;
233
Peter Zijlstraf5970552009-06-18 23:22:55 +0200234 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200235 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300236 }
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200237
238 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
239
240 tasks = opendir(filename);
241 while (!readdir_r(tasks, &dirent, &next) && next) {
242 char *end;
243 pid = strtol(dirent.d_name, &end, 10);
244 if (*end)
245 continue;
246
247 comm_ev.tid = pid;
248
Peter Zijlstraf5970552009-06-18 23:22:55 +0200249 write_output(&comm_ev, comm_ev.header.size);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200250 }
251 closedir(tasks);
252 return;
253
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300254out_failure:
255 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
256 filename);
257 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300258}
259
Ingo Molnar2debbc82009-06-05 14:29:10 +0200260static void pid_synthesize_mmap_samples(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300261{
262 char filename[PATH_MAX];
263 FILE *fp;
264
265 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
266
267 fp = fopen(filename, "r");
268 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200269 /*
270 * We raced with a task exiting - just return:
271 */
272 if (verbose)
273 fprintf(stderr, "couldn't open %s\n", filename);
274 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300275 }
276 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300277 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300278 struct mmap_event mmap_ev = {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200279 .header = { .type = PERF_EVENT_MMAP },
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300280 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300281 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300282 size_t size;
283 if (fgets(bf, sizeof(bf), fp) == NULL)
284 break;
285
286 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300287 n = hex2u64(pbf, &mmap_ev.start);
288 if (n < 0)
289 continue;
290 pbf += n + 1;
291 n = hex2u64(pbf, &mmap_ev.len);
292 if (n < 0)
293 continue;
294 pbf += n + 3;
295 if (*pbf == 'x') { /* vm_exec */
Johannes Weiner76c64c52009-06-24 21:08:36 +0200296 char *execname = strchr(bf, '/');
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300297
Anton Blanchard11b5f812009-07-16 15:44:29 +0200298 /* Catch VDSO */
299 if (execname == NULL)
300 execname = strstr(bf, "[vdso]");
301
Johannes Weiner76c64c52009-06-24 21:08:36 +0200302 if (execname == NULL)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300303 continue;
304
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300305 size = strlen(execname);
306 execname[size - 1] = '\0'; /* Remove \n */
307 memcpy(mmap_ev.filename, execname, size);
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000308 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300309 mmap_ev.len -= mmap_ev.start;
310 mmap_ev.header.size = (sizeof(mmap_ev) -
311 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200312 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300313 mmap_ev.tid = pid;
314
Peter Zijlstraf5970552009-06-18 23:22:55 +0200315 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300316 }
317 }
318
319 fclose(fp);
320}
321
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200322static void synthesize_all(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200323{
324 DIR *proc;
325 struct dirent dirent, *next;
326
327 proc = opendir("/proc");
328
329 while (!readdir_r(proc, &dirent, &next) && next) {
330 char *end;
331 pid_t pid;
332
333 pid = strtol(dirent.d_name, &end, 10);
334 if (*end) /* only interested in proper numerical dirents */
335 continue;
336
337 pid_synthesize_comm_event(pid, 1);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200338 pid_synthesize_mmap_samples(pid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200339 }
340
341 closedir(proc);
342}
343
Ingo Molnarf250c0302009-06-05 13:18:41 +0200344static int group_fd;
345
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200346static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
347{
348 struct perf_header_attr *h_attr;
349
350 if (nr < header->attrs) {
351 h_attr = header->attr[nr];
352 } else {
353 h_attr = perf_header_attr__new(a);
354 perf_header__add_attr(header, h_attr);
355 }
356
357 return h_attr;
358}
359
Ingo Molnarf250c0302009-06-05 13:18:41 +0200360static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200361{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200362 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200363 struct perf_header_attr *h_attr;
364 int track = !counter; /* only the first counter needs these */
365 struct {
366 u64 count;
367 u64 time_enabled;
368 u64 time_running;
369 u64 id;
370 } read_data;
371
372 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
373 PERF_FORMAT_TOTAL_TIME_RUNNING |
374 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200375
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200376 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200377
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200378 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200379 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200380 attr->freq = 1;
381 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200382 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200383
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200384 if (no_samples)
385 attr->sample_freq = 0;
386
387 if (inherit_stat)
388 attr->inherit_stat = 1;
389
Anton Blanchard4bba8282009-07-16 15:44:29 +0200390 if (sample_address)
391 attr->sample_type |= PERF_SAMPLE_ADDR;
392
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200393 if (call_graph)
394 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
395
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200396
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200397 attr->mmap = track;
398 attr->comm = track;
399 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200400 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200401
Ingo Molnar3da297a2009-06-07 17:39:02 +0200402try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200403 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200404
405 if (fd[nr_cpu][counter] < 0) {
406 int err = errno;
407
Ingo Molnarf250c0302009-06-05 13:18:41 +0200408 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200409 die("Permission error - are you root?\n");
410
411 /*
412 * If it's cycles then fall back to hrtimer
413 * based cpu-clock-tick sw counter, which
414 * is always available even if no PMU support:
415 */
416 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200417 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200418
419 if (verbose)
420 warning(" ... trying to fall back to cpu-clock-ticks\n");
421 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200422 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200423 goto try_again;
424 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200425 printf("\n");
426 error("perfcounter syscall returned with %d (%s)\n",
427 fd[nr_cpu][counter], strerror(err));
428 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200429 exit(-1);
430 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200431
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200432 h_attr = get_header_attr(attr, counter);
433
434 if (!file_new) {
435 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
436 fprintf(stderr, "incompatible append\n");
437 exit(-1);
438 }
439 }
440
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200441 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
442 perror("Unable to read perf file descriptor\n");
443 exit(-1);
444 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200445
446 perf_header_attr__add_id(h_attr, read_data.id);
447
Ingo Molnarf250c0302009-06-05 13:18:41 +0200448 assert(fd[nr_cpu][counter] >= 0);
449 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
450
451 /*
452 * First counter acts as the group leader:
453 */
454 if (group && group_fd == -1)
455 group_fd = fd[nr_cpu][counter];
456
457 event_array[nr_poll].fd = fd[nr_cpu][counter];
458 event_array[nr_poll].events = POLLIN;
459 nr_poll++;
460
461 mmap_array[nr_cpu][counter].counter = counter;
462 mmap_array[nr_cpu][counter].prev = 0;
463 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
464 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200465 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200466 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
467 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
468 exit(-1);
469 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200470
471 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200472}
473
474static void open_counters(int cpu, pid_t pid)
475{
476 int counter;
477
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200478 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200479 for (counter = 0; counter < nr_counters; counter++)
480 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200481
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200482 nr_cpu++;
483}
484
Peter Zijlstraf5970552009-06-18 23:22:55 +0200485static void atexit_header(void)
486{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200487 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200488
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200489 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200490}
491
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200492static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200493{
494 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200495 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200496 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200497 int flags;
498 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200499
500 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200501 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
502 assert(nr_cpus <= MAX_NR_CPUS);
503 assert(nr_cpus >= 0);
504
Peter Zijlstraf5970552009-06-18 23:22:55 +0200505 atexit(sig_atexit);
506 signal(SIGCHLD, sig_handler);
507 signal(SIGINT, sig_handler);
508
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200509 if (!stat(output_name, &st) && st.st_size) {
510 if (!force && !append_file) {
511 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
512 output_name);
513 exit(-1);
514 }
515 } else {
516 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200517 }
518
Ingo Molnarabaff322009-06-02 22:59:57 +0200519 flags = O_CREAT|O_RDWR;
520 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200521 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200522 else
523 flags |= O_TRUNC;
524
525 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200526 if (output < 0) {
527 perror("failed to create output file");
528 exit(-1);
529 }
530
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200531 if (!file_new)
532 header = perf_header__read(output);
533 else
534 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200535
536 atexit(atexit_header);
537
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300538 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200539 pid = target_pid;
540 if (pid == -1)
541 pid = getpid();
542
543 open_counters(-1, pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300544 } else for (i = 0; i < nr_cpus; i++)
545 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200546
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200547 if (file_new)
548 perf_header__write(header, output);
549
550 if (!system_wide) {
551 pid_synthesize_comm_event(pid, 0);
552 pid_synthesize_mmap_samples(pid);
553 } else
554 synthesize_all();
555
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200556 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300557 pid = fork();
558 if (pid < 0)
559 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200560
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300561 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200562 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300563 perror(argv[0]);
564 exit(-1);
565 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200566 }
567 }
568
569 if (realtime_prio) {
570 struct sched_param param;
571
572 param.sched_priority = realtime_prio;
573 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
574 printf("Could not set realtime priority.\n");
575 exit(-1);
576 }
577 }
578
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200579 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200580 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200581
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200582 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200583 for (counter = 0; counter < nr_counters; counter++)
584 mmap_read(&mmap_array[i][counter]);
585 }
586
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200587 if (hits == samples) {
588 if (done)
589 break;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200590 ret = poll(event_array, nr_poll, 100);
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200591 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200592 }
593
Ingo Molnar021e9f42009-06-03 19:27:19 +0200594 /*
595 * Approximate RIP event size: 24 bytes.
596 */
597 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200598 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200599 (double)bytes_written / 1024.0 / 1024.0,
600 output_name,
601 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200602
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200603 return 0;
604}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200605
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200606static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200607 "perf record [<options>] [<command>]",
608 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200609 NULL
610};
611
Ingo Molnar52425192009-05-26 09:17:18 +0200612static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200613 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200614 "event selector. use 'perf list' to list available events",
615 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200616 OPT_INTEGER('p', "pid", &target_pid,
617 "record events on existing pid"),
618 OPT_INTEGER('r', "realtime", &realtime_prio,
619 "collect data with this RT SCHED_FIFO priority"),
620 OPT_BOOLEAN('a', "all-cpus", &system_wide,
621 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200622 OPT_BOOLEAN('A', "append", &append_file,
623 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200624 OPT_BOOLEAN('f', "force", &force,
625 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200626 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200627 "event period to sample"),
628 OPT_STRING('o', "output", &output_name, "file",
629 "output file name"),
630 OPT_BOOLEAN('i', "inherit", &inherit,
631 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200632 OPT_INTEGER('F', "freq", &freq,
633 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200634 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
635 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200636 OPT_BOOLEAN('g', "call-graph", &call_graph,
637 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200638 OPT_BOOLEAN('v', "verbose", &verbose,
639 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200640 OPT_BOOLEAN('s', "stat", &inherit_stat,
641 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200642 OPT_BOOLEAN('d', "data", &sample_address,
643 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200644 OPT_BOOLEAN('n', "no-samples", &no_samples,
645 "don't sample"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200646 OPT_END()
647};
648
Ingo Molnarf37a2912009-07-01 12:37:06 +0200649int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200650{
651 int counter;
652
Anton Blancharda0541232009-07-22 23:04:12 +1000653 argc = parse_options(argc, argv, options, record_usage,
654 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200655 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200656 usage_with_options(record_usage, options);
657
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200658 if (!nr_counters) {
659 nr_counters = 1;
660 attrs[0].type = PERF_TYPE_HARDWARE;
661 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
662 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200663
664 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200665 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200666 continue;
667
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200668 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200669 }
670
671 return __cmd_record(argc, argv);
672}