blob: 28304677c73e7166b7168104177fd1c3ca7dd6d1 [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 Zijlstra97124d52009-06-02 15:52:24 +020017#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020018#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020019
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020020#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
21#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030022
Peter Zijlstrade9ac072009-04-08 15:01:31 +020023static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020024
25static long default_interval = 100000;
26
Ingo Molnar3cf165f2009-06-02 23:02:59 +020027static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020028static unsigned int page_size;
Ingo Molnar3cf165f2009-06-02 23:02:59 +020029static unsigned int mmap_pages = 128;
Ingo Molnarcf1f4572009-06-05 13:27:02 +020030static int freq = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020031static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020032static const char *output_name = "perf.data";
Peter Zijlstrade9ac072009-04-08 15:01:31 +020033static int group = 0;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020034static unsigned int realtime_prio = 0;
35static int system_wide = 0;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -030036static pid_t target_pid = -1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020037static int inherit = 1;
Peter Zijlstra97124d52009-06-02 15:52:24 +020038static int force = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +020039static int append_file = 0;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +020040static int call_graph = 0;
Ingo Molnar3da297a2009-06-07 17:39:02 +020041static int verbose = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020042
Ingo Molnara21ca2c2009-06-06 09:58:57 +020043static long samples;
44static struct timeval last_read;
45static struct timeval this_read;
46
47static __u64 bytes_written;
48
49static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
50
51static int nr_poll;
52static int nr_cpu;
53
Peter Zijlstraf5970552009-06-18 23:22:55 +020054static int file_new = 1;
55static struct perf_file_header file_header;
56
Ingo Molnara21ca2c2009-06-06 09:58:57 +020057struct mmap_event {
58 struct perf_event_header header;
59 __u32 pid;
60 __u32 tid;
61 __u64 start;
62 __u64 len;
63 __u64 pgoff;
64 char filename[PATH_MAX];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020065};
66
Ingo Molnara21ca2c2009-06-06 09:58:57 +020067struct comm_event {
68 struct perf_event_header header;
69 __u32 pid;
70 __u32 tid;
71 char comm[16];
Peter Zijlstrade9ac072009-04-08 15:01:31 +020072};
73
Ingo Molnara21ca2c2009-06-06 09:58:57 +020074
75struct mmap_data {
76 int counter;
77 void *base;
78 unsigned int mask;
79 unsigned int prev;
80};
81
82static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
83
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020084static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020085{
86 struct perf_counter_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020087 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020088
89 head = pc->data_head;
90 rmb();
91
92 return head;
93}
94
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020095static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
96{
97 struct perf_counter_mmap_page *pc = md->base;
98
99 /*
100 * ensure all reads are done before we write the tail out.
101 */
102 /* mb(); */
103 pc->data_tail = tail;
104}
105
Peter Zijlstraf5970552009-06-18 23:22:55 +0200106static void write_output(void *buf, size_t size)
107{
108 while (size) {
109 int ret = write(output, buf, size);
110
111 if (ret < 0)
112 die("failed to write");
113
114 size -= ret;
115 buf += ret;
116
117 bytes_written += ret;
118 }
119}
120
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200121static void mmap_read(struct mmap_data *md)
122{
123 unsigned int head = mmap_read_head(md);
124 unsigned int old = md->prev;
125 unsigned char *data = md->base + page_size;
126 unsigned long size;
127 void *buf;
128 int diff;
129
130 gettimeofday(&this_read, NULL);
131
132 /*
133 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200134 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200135 *
136 * If we somehow ended up ahead of the head, we got messed up.
137 *
138 * In either case, truncate and restart at head.
139 */
140 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200141 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200142 struct timeval iv;
143 unsigned long msecs;
144
145 timersub(&this_read, &last_read, &iv);
146 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
147
148 fprintf(stderr, "WARNING: failed to keep up with mmap data."
149 " Last read %lu msecs ago.\n", msecs);
150
151 /*
152 * head points to a known good entry, start there.
153 */
154 old = head;
155 }
156
157 last_read = this_read;
158
159 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200160 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200161
162 size = head - old;
163
164 if ((old & md->mask) + size != (head & md->mask)) {
165 buf = &data[old & md->mask];
166 size = md->mask + 1 - (old & md->mask);
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
172 buf = &data[old & md->mask];
173 size = head - old;
174 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200175
Peter Zijlstraf5970552009-06-18 23:22:55 +0200176 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200177
178 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200179 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200180}
181
182static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200183static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200184
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200185static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200186{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200187 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200188 signr = sig;
189}
190
191static void sig_atexit(void)
192{
193 if (signr == -1)
194 return;
195
196 signal(signr, SIG_DFL);
197 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200198}
199
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200200static void pid_synthesize_comm_event(pid_t pid, int full)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300201{
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300202 struct comm_event comm_ev;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200203 char filename[PATH_MAX];
Ingo Molnar16f762a2009-05-27 09:10:38 +0200204 char bf[BUFSIZ];
Peter Zijlstraf5970552009-06-18 23:22:55 +0200205 int fd;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300206 size_t size;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300207 char *field, *sep;
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200208 DIR *tasks;
209 struct dirent dirent, *next;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300210
211 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
212
213 fd = open(filename, O_RDONLY);
214 if (fd < 0) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200215 /*
216 * We raced with a task exiting - just return:
217 */
218 if (verbose)
219 fprintf(stderr, "couldn't open %s\n", filename);
220 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300221 }
222 if (read(fd, bf, sizeof(bf)) < 0) {
223 fprintf(stderr, "couldn't read %s\n", filename);
224 exit(EXIT_FAILURE);
225 }
226 close(fd);
227
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300228 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300229 memset(&comm_ev, 0, sizeof(comm_ev));
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300230 field = strchr(bf, '(');
231 if (field == NULL)
232 goto out_failure;
233 sep = strchr(++field, ')');
234 if (sep == NULL)
235 goto out_failure;
236 size = sep - field;
237 memcpy(comm_ev.comm, field, size++);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200238
239 comm_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300240 comm_ev.header.type = PERF_EVENT_COMM;
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200241 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);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200248 return;
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);
265 return;
266
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300267out_failure:
268 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
269 filename);
270 exit(EXIT_FAILURE);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300271}
272
Ingo Molnar2debbc82009-06-05 14:29:10 +0200273static void pid_synthesize_mmap_samples(pid_t pid)
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300274{
275 char filename[PATH_MAX];
276 FILE *fp;
277
278 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
279
280 fp = fopen(filename, "r");
281 if (fp == NULL) {
Ingo Molnar613d8602009-06-15 08:17:12 +0200282 /*
283 * We raced with a task exiting - just return:
284 */
285 if (verbose)
286 fprintf(stderr, "couldn't open %s\n", filename);
287 return;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300288 }
289 while (1) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300290 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300291 struct mmap_event mmap_ev = {
292 .header.type = PERF_EVENT_MMAP,
293 };
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300294 int n;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300295 size_t size;
296 if (fgets(bf, sizeof(bf), fp) == NULL)
297 break;
298
299 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300300 n = hex2u64(pbf, &mmap_ev.start);
301 if (n < 0)
302 continue;
303 pbf += n + 1;
304 n = hex2u64(pbf, &mmap_ev.len);
305 if (n < 0)
306 continue;
307 pbf += n + 3;
308 if (*pbf == 'x') { /* vm_exec */
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300309 char *execname = strrchr(bf, ' ');
310
311 if (execname == NULL || execname[1] != '/')
312 continue;
313
314 execname += 1;
315 size = strlen(execname);
316 execname[size - 1] = '\0'; /* Remove \n */
317 memcpy(mmap_ev.filename, execname, size);
Ingo Molnar729ff5e22009-06-11 14:16:15 +0200318 size = ALIGN(size, sizeof(__u64));
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300319 mmap_ev.len -= mmap_ev.start;
320 mmap_ev.header.size = (sizeof(mmap_ev) -
321 (sizeof(mmap_ev.filename) - size));
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200322 mmap_ev.pid = pid;
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300323 mmap_ev.tid = pid;
324
Peter Zijlstraf5970552009-06-18 23:22:55 +0200325 write_output(&mmap_ev, mmap_ev.header.size);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300326 }
327 }
328
329 fclose(fp);
330}
331
Ingo Molnar2debbc82009-06-05 14:29:10 +0200332static void synthesize_samples(void)
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200333{
334 DIR *proc;
335 struct dirent dirent, *next;
336
337 proc = opendir("/proc");
338
339 while (!readdir_r(proc, &dirent, &next) && next) {
340 char *end;
341 pid_t pid;
342
343 pid = strtol(dirent.d_name, &end, 10);
344 if (*end) /* only interested in proper numerical dirents */
345 continue;
346
347 pid_synthesize_comm_event(pid, 1);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200348 pid_synthesize_mmap_samples(pid);
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200349 }
350
351 closedir(proc);
352}
353
Ingo Molnarf250c0302009-06-05 13:18:41 +0200354static int group_fd;
355
356static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200357{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200358 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200359 int track = 1;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200360
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200361 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200362
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200363 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200364 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200365 attr->freq = 1;
366 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200367 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200368
369 if (call_graph)
370 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
371
Peter Zijlstraf5970552009-06-18 23:22:55 +0200372 if (file_new) {
373 file_header.sample_type = attr->sample_type;
374 } else {
375 if (file_header.sample_type != attr->sample_type) {
376 fprintf(stderr, "incompatible append\n");
377 exit(-1);
378 }
379 }
380
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200381 attr->mmap = track;
382 attr->comm = track;
383 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200384 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200385
386 track = 0; /* only the first counter needs these */
387
Ingo Molnar3da297a2009-06-07 17:39:02 +0200388try_again:
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200389 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200390
391 if (fd[nr_cpu][counter] < 0) {
392 int err = errno;
393
Ingo Molnarf250c0302009-06-05 13:18:41 +0200394 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200395 die("Permission error - are you root?\n");
396
397 /*
398 * If it's cycles then fall back to hrtimer
399 * based cpu-clock-tick sw counter, which
400 * is always available even if no PMU support:
401 */
402 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200403 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200404
405 if (verbose)
406 warning(" ... trying to fall back to cpu-clock-ticks\n");
407 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200408 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200409 goto try_again;
410 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200411 printf("\n");
412 error("perfcounter syscall returned with %d (%s)\n",
413 fd[nr_cpu][counter], strerror(err));
414 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200415 exit(-1);
416 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200417
Ingo Molnarf250c0302009-06-05 13:18:41 +0200418 assert(fd[nr_cpu][counter] >= 0);
419 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
420
421 /*
422 * First counter acts as the group leader:
423 */
424 if (group && group_fd == -1)
425 group_fd = fd[nr_cpu][counter];
426
427 event_array[nr_poll].fd = fd[nr_cpu][counter];
428 event_array[nr_poll].events = POLLIN;
429 nr_poll++;
430
431 mmap_array[nr_cpu][counter].counter = counter;
432 mmap_array[nr_cpu][counter].prev = 0;
433 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
434 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200435 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200436 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
437 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
438 exit(-1);
439 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200440
441 ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200442}
443
444static void open_counters(int cpu, pid_t pid)
445{
446 int counter;
447
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300448 if (pid > 0) {
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200449 pid_synthesize_comm_event(pid, 0);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200450 pid_synthesize_mmap_samples(pid);
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300451 }
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200452
453 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200454 for (counter = 0; counter < nr_counters; counter++)
455 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200456
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200457 nr_cpu++;
458}
459
Peter Zijlstraf5970552009-06-18 23:22:55 +0200460static void atexit_header(void)
461{
462 file_header.data_size += bytes_written;
463
464 pwrite(output, &file_header, sizeof(file_header), 0);
465}
466
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200467static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200468{
469 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200470 struct stat st;
Ingo Molnarabaff322009-06-02 22:59:57 +0200471 pid_t pid;
472 int flags;
473 int ret;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200474
475 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200476 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
477 assert(nr_cpus <= MAX_NR_CPUS);
478 assert(nr_cpus >= 0);
479
Peter Zijlstraf5970552009-06-18 23:22:55 +0200480 atexit(sig_atexit);
481 signal(SIGCHLD, sig_handler);
482 signal(SIGINT, sig_handler);
483
Ingo Molnarabaff322009-06-02 22:59:57 +0200484 if (!stat(output_name, &st) && !force && !append_file) {
485 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
Peter Zijlstra97124d52009-06-02 15:52:24 +0200486 output_name);
487 exit(-1);
488 }
489
Ingo Molnarabaff322009-06-02 22:59:57 +0200490 flags = O_CREAT|O_RDWR;
491 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200492 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200493 else
494 flags |= O_TRUNC;
495
496 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200497 if (output < 0) {
498 perror("failed to create output file");
499 exit(-1);
500 }
501
Peter Zijlstraf5970552009-06-18 23:22:55 +0200502 if (!file_new) {
503 read(output, &file_header, sizeof(file_header));
504 lseek(output, file_header.data_size, SEEK_CUR);
505 }
506
507 atexit(atexit_header);
508
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300509 if (!system_wide) {
Ingo Molnardf979922009-06-04 13:41:22 +0200510 open_counters(-1, target_pid != -1 ? target_pid : getpid());
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300511 } else for (i = 0; i < nr_cpus; i++)
512 open_counters(i, target_pid);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200513
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200514 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300515 pid = fork();
516 if (pid < 0)
517 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200518
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300519 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200520 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300521 perror(argv[0]);
522 exit(-1);
523 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200524 }
525 }
526
527 if (realtime_prio) {
528 struct sched_param param;
529
530 param.sched_priority = realtime_prio;
531 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
532 printf("Could not set realtime priority.\n");
533 exit(-1);
534 }
535 }
536
Peter Zijlstraf70e87d2009-06-02 14:13:24 +0200537 if (system_wide)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200538 synthesize_samples();
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200539
540 while (!done) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200541 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200542
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200543 for (i = 0; i < nr_cpu; i++) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200544 for (counter = 0; counter < nr_counters; counter++)
545 mmap_read(&mmap_array[i][counter]);
546 }
547
Ingo Molnar2debbc82009-06-05 14:29:10 +0200548 if (hits == samples)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200549 ret = poll(event_array, nr_poll, 100);
550 }
551
Ingo Molnar021e9f42009-06-03 19:27:19 +0200552 /*
553 * Approximate RIP event size: 24 bytes.
554 */
555 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200556 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200557 (double)bytes_written / 1024.0 / 1024.0,
558 output_name,
559 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200560
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200561 return 0;
562}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200563
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200564static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200565 "perf record [<options>] [<command>]",
566 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200567 NULL
568};
569
Ingo Molnar52425192009-05-26 09:17:18 +0200570static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200571 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200572 "event selector. use 'perf list' to list available events",
573 parse_events),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200574 OPT_INTEGER('p', "pid", &target_pid,
575 "record events on existing pid"),
576 OPT_INTEGER('r', "realtime", &realtime_prio,
577 "collect data with this RT SCHED_FIFO priority"),
578 OPT_BOOLEAN('a', "all-cpus", &system_wide,
579 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200580 OPT_BOOLEAN('A', "append", &append_file,
581 "append to the output file to do incremental profiling"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200582 OPT_BOOLEAN('f', "force", &force,
583 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200584 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200585 "event period to sample"),
586 OPT_STRING('o', "output", &output_name, "file",
587 "output file name"),
588 OPT_BOOLEAN('i', "inherit", &inherit,
589 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200590 OPT_INTEGER('F', "freq", &freq,
591 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200592 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
593 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200594 OPT_BOOLEAN('g', "call-graph", &call_graph,
595 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200596 OPT_BOOLEAN('v', "verbose", &verbose,
597 "be more verbose (show counter open errors, etc)"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200598 OPT_END()
599};
600
601int cmd_record(int argc, const char **argv, const char *prefix)
602{
603 int counter;
604
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200605 argc = parse_options(argc, argv, options, record_usage, 0);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200606 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200607 usage_with_options(record_usage, options);
608
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200609 if (!nr_counters) {
610 nr_counters = 1;
611 attrs[0].type = PERF_TYPE_HARDWARE;
612 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
613 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200614
615 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200616 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200617 continue;
618
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200619 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200620 }
621
622 return __cmd_record(argc, argv);
623}