blob: e2b35ad82a77b7e81180ec801b0aaccfc23cdf38 [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 */
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08008#define _FILE_OFFSET_BITS 64
9
Ingo Molnar16f762a2009-05-27 09:10:38 +020010#include "builtin.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020011
12#include "perf.h"
13
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -020014#include "util/build-id.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020015#include "util/util.h"
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020016#include "util/parse-options.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020017#include "util/parse-events.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030018#include "util/string.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020019
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020020#include "util/header.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020021#include "util/event.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020022#include "util/debug.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020023#include "util/session.h"
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020024#include "util/symbol.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110025#include "util/cpumap.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020026
Peter Zijlstra97124d52009-06-02 15:52:24 +020027#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020028#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020029
Peter Zijlstrade9ac072009-04-08 15:01:31 +020030static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020031
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020032static long default_interval = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020033
Ingo Molnar42e59d72009-10-06 15:14:21 +020034static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020035static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020036static unsigned int mmap_pages = 128;
37static int freq = 1000;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020038static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020039static const char *output_name = "perf.data";
Ingo Molnar42e59d72009-10-06 15:14:21 +020040static int group = 0;
41static unsigned int realtime_prio = 0;
42static int raw_samples = 0;
43static int system_wide = 0;
44static int profile_cpu = -1;
45static pid_t target_pid = -1;
46static pid_t child_pid = -1;
47static int inherit = 1;
48static int force = 0;
49static int append_file = 0;
50static int call_graph = 0;
51static int inherit_stat = 0;
52static int no_samples = 0;
53static int sample_address = 0;
54static int multiplex = 0;
55static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020056
Ingo Molnar42e59d72009-10-06 15:14:21 +020057static long samples = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020058static struct timeval last_read;
59static struct timeval this_read;
60
Ingo Molnar42e59d72009-10-06 15:14:21 +020061static u64 bytes_written = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020062
63static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
64
Ingo Molnar42e59d72009-10-06 15:14:21 +020065static int nr_poll = 0;
66static int nr_cpu = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020067
Ingo Molnar42e59d72009-10-06 15:14:21 +020068static int file_new = 1;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -020069static off_t post_processing_offset;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020070
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020071static struct perf_session *session;
Peter Zijlstraf5970552009-06-18 23:22:55 +020072
Ingo Molnara21ca2c2009-06-06 09:58:57 +020073struct mmap_data {
74 int counter;
75 void *base;
76 unsigned int mask;
77 unsigned int prev;
78};
79
80static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
81
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020082static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020083{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020084 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020085 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020086
87 head = pc->data_head;
88 rmb();
89
90 return head;
91}
92
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020093static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
94{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020095 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020096
97 /*
98 * ensure all reads are done before we write the tail out.
99 */
100 /* mb(); */
101 pc->data_tail = tail;
102}
103
Peter Zijlstraf5970552009-06-18 23:22:55 +0200104static void write_output(void *buf, size_t size)
105{
106 while (size) {
107 int ret = write(output, buf, size);
108
109 if (ret < 0)
110 die("failed to write");
111
112 size -= ret;
113 buf += ret;
114
115 bytes_written += ret;
116 }
117}
118
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200119static int process_synthesized_event(event_t *event,
120 struct perf_session *self __used)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200121{
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200122 write_output(event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200123 return 0;
124}
125
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200126static void mmap_read(struct mmap_data *md)
127{
128 unsigned int head = mmap_read_head(md);
129 unsigned int old = md->prev;
130 unsigned char *data = md->base + page_size;
131 unsigned long size;
132 void *buf;
133 int diff;
134
135 gettimeofday(&this_read, NULL);
136
137 /*
138 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200139 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200140 *
141 * If we somehow ended up ahead of the head, we got messed up.
142 *
143 * In either case, truncate and restart at head.
144 */
145 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200146 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200147 struct timeval iv;
148 unsigned long msecs;
149
150 timersub(&this_read, &last_read, &iv);
151 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
152
153 fprintf(stderr, "WARNING: failed to keep up with mmap data."
154 " Last read %lu msecs ago.\n", msecs);
155
156 /*
157 * head points to a known good entry, start there.
158 */
159 old = head;
160 }
161
162 last_read = this_read;
163
164 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200165 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200166
167 size = head - old;
168
169 if ((old & md->mask) + size != (head & md->mask)) {
170 buf = &data[old & md->mask];
171 size = md->mask + 1 - (old & md->mask);
172 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200173
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200174 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200175 }
176
177 buf = &data[old & md->mask];
178 size = head - old;
179 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200180
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200181 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200182
183 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200184 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200185}
186
187static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200188static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200189
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200190static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200191{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200192 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200193 signr = sig;
194}
195
196static void sig_atexit(void)
197{
Chris Wilson933da832009-10-04 01:35:01 +0100198 if (child_pid != -1)
199 kill(child_pid, SIGTERM);
200
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200201 if (signr == -1)
202 return;
203
204 signal(signr, SIG_DFL);
205 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200206}
207
Ingo Molnarf250c0302009-06-05 13:18:41 +0200208static int group_fd;
209
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200210static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200211{
212 struct perf_header_attr *h_attr;
213
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200214 if (nr < session->header.attrs) {
215 h_attr = session->header.attr[nr];
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200216 } else {
217 h_attr = perf_header_attr__new(a);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200218 if (h_attr != NULL)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200219 if (perf_header__add_attr(&session->header, h_attr) < 0) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200220 perf_header_attr__delete(h_attr);
221 h_attr = NULL;
222 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200223 }
224
225 return h_attr;
226}
227
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300228static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200229{
Li Zefanc171b552009-10-15 11:22:07 +0800230 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200231 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200232 struct perf_header_attr *h_attr;
233 int track = !counter; /* only the first counter needs these */
Li Zefanc171b552009-10-15 11:22:07 +0800234 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200235 struct {
236 u64 count;
237 u64 time_enabled;
238 u64 time_running;
239 u64 id;
240 } read_data;
241
242 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
243 PERF_FORMAT_TOTAL_TIME_RUNNING |
244 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200245
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200246 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200247
Eric B Munson8907fd62010-03-05 12:51:05 -0300248 if (nr_counters > 1)
249 attr->sample_type |= PERF_SAMPLE_ID;
250
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200251 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200252 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200253 attr->freq = 1;
254 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200255 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200256
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200257 if (no_samples)
258 attr->sample_freq = 0;
259
260 if (inherit_stat)
261 attr->inherit_stat = 1;
262
Anton Blanchard4bba8282009-07-16 15:44:29 +0200263 if (sample_address)
264 attr->sample_type |= PERF_SAMPLE_ADDR;
265
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200266 if (call_graph)
267 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
268
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200269 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200270 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200271 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200272 attr->sample_type |= PERF_SAMPLE_CPU;
273 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200274
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200275 attr->mmap = track;
276 attr->comm = track;
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100277 attr->inherit = inherit;
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300278 if (target_pid == -1 && !system_wide) {
279 attr->disabled = 1;
Eric B Munsonbedbfde2010-03-15 11:46:57 -0300280 attr->enable_on_exec = 1;
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300281 }
Eric B Munsonbedbfde2010-03-15 11:46:57 -0300282
Ingo Molnar3da297a2009-06-07 17:39:02 +0200283try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200284 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200285
286 if (fd[nr_cpu][counter] < 0) {
287 int err = errno;
288
Pekka Enbergc10edee2009-11-08 18:01:06 +0200289 if (err == EPERM || err == EACCES)
Arnaldo Carvalho de Melo6230f2c2010-03-11 15:53:12 -0300290 die("Permission error - are you root?\n"
291 "\t Consider tweaking /proc/sys/kernel/perf_event_paranoid.\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200292 else if (err == ENODEV && profile_cpu != -1)
293 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200294
295 /*
296 * If it's cycles then fall back to hrtimer
297 * based cpu-clock-tick sw counter, which
298 * is always available even if no PMU support:
299 */
300 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200301 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200302
303 if (verbose)
304 warning(" ... trying to fall back to cpu-clock-ticks\n");
305 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200306 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200307 goto try_again;
308 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200309 printf("\n");
310 error("perfcounter syscall returned with %d (%s)\n",
311 fd[nr_cpu][counter], strerror(err));
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000312
313#if defined(__i386__) || defined(__x86_64__)
314 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
315 die("No hardware sampling interrupt available. No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.\n");
316#endif
317
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200318 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200319 exit(-1);
320 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200321
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200322 h_attr = get_header_attr(attr, counter);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200323 if (h_attr == NULL)
324 die("nomem\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200325
326 if (!file_new) {
327 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
328 fprintf(stderr, "incompatible append\n");
329 exit(-1);
330 }
331 }
332
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200333 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
334 perror("Unable to read perf file descriptor\n");
335 exit(-1);
336 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200337
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -0200338 if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
339 pr_warning("Not enough memory to add id\n");
340 exit(-1);
341 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200342
Ingo Molnarf250c0302009-06-05 13:18:41 +0200343 assert(fd[nr_cpu][counter] >= 0);
344 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
345
346 /*
347 * First counter acts as the group leader:
348 */
349 if (group && group_fd == -1)
350 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200351 if (multiplex && multiplex_fd == -1)
352 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200353
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200354 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
Ingo Molnarf250c0302009-06-05 13:18:41 +0200355
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200356 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200357 assert(ret != -1);
358 } else {
359 event_array[nr_poll].fd = fd[nr_cpu][counter];
360 event_array[nr_poll].events = POLLIN;
361 nr_poll++;
362
363 mmap_array[nr_cpu][counter].counter = counter;
364 mmap_array[nr_cpu][counter].prev = 0;
365 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
366 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
367 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
368 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
369 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
370 exit(-1);
371 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200372 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200373
Li Zefanc171b552009-10-15 11:22:07 +0800374 if (filter != NULL) {
375 ret = ioctl(fd[nr_cpu][counter],
376 PERF_EVENT_IOC_SET_FILTER, filter);
377 if (ret) {
378 error("failed to set filter with %d (%s)\n", errno,
379 strerror(errno));
380 exit(-1);
381 }
382 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200383}
384
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300385static void open_counters(int cpu, pid_t pid)
Ingo Molnarf250c0302009-06-05 13:18:41 +0200386{
387 int counter;
388
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200389 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200390 for (counter = 0; counter < nr_counters; counter++)
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300391 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200392
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200393 nr_cpu++;
394}
395
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200396static int process_buildids(void)
397{
398 u64 size = lseek(output, 0, SEEK_CUR);
399
Arnaldo Carvalho de Melo9f591fd2010-03-11 15:53:11 -0300400 if (size == 0)
401 return 0;
402
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200403 session->fd = output;
404 return __perf_session__process_events(session, post_processing_offset,
405 size - post_processing_offset,
406 size, &build_id__mark_dso_hit_ops);
407}
408
Peter Zijlstraf5970552009-06-18 23:22:55 +0200409static void atexit_header(void)
410{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200411 session->header.data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200412
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200413 process_buildids();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200414 perf_header__write(&session->header, output, true);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200415}
416
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200417static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200418{
419 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200420 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200421 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200422 int flags;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200423 int err;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200424 unsigned long waking = 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100425 int child_ready_pipe[2], go_pipe[2];
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300426 const bool forks = argc > 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100427 char buf;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200428
429 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200430
Peter Zijlstraf5970552009-06-18 23:22:55 +0200431 atexit(sig_atexit);
432 signal(SIGCHLD, sig_handler);
433 signal(SIGINT, sig_handler);
434
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200435 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100436 perror("failed to create pipes");
437 exit(-1);
438 }
439
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200440 if (!stat(output_name, &st) && st.st_size) {
Arnaldo Carvalho de Melob38d3462009-12-14 20:09:30 -0200441 if (!force) {
442 if (!append_file) {
443 pr_err("Error, output file %s exists, use -A "
444 "to append or -f to overwrite.\n",
445 output_name);
446 exit(-1);
447 }
448 } else {
449 char oldname[PATH_MAX];
450 snprintf(oldname, sizeof(oldname), "%s.old",
451 output_name);
452 unlink(oldname);
453 rename(output_name, oldname);
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200454 }
455 } else {
456 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200457 }
458
Xiao Guangrongf887f302010-02-04 16:46:42 +0800459 flags = O_CREAT|O_RDWR;
Ingo Molnarabaff322009-06-02 22:59:57 +0200460 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200461 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200462 else
463 flags |= O_TRUNC;
464
465 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200466 if (output < 0) {
467 perror("failed to create output file");
468 exit(-1);
469 }
470
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200471 session = perf_session__new(output_name, O_WRONLY, force);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200472 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200473 pr_err("Not enough memory for reading perf file header\n");
474 return -1;
475 }
476
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200477 if (!file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200478 err = perf_header__read(&session->header, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200479 if (err < 0)
480 return err;
481 }
482
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200483 if (raw_samples) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200484 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200485 } else {
486 for (i = 0; i < nr_counters; i++) {
487 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200488 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200489 break;
490 }
491 }
492 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200493
Peter Zijlstraf5970552009-06-18 23:22:55 +0200494 atexit(atexit_header);
495
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200496 if (forks) {
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300497 child_pid = fork();
Peter Zijlstra856e9662009-12-16 17:55:55 +0100498 if (pid < 0) {
499 perror("failed to fork");
500 exit(-1);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200501 }
Peter Zijlstra856e9662009-12-16 17:55:55 +0100502
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300503 if (!child_pid) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100504 close(child_ready_pipe[0]);
505 close(go_pipe[1]);
506 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
507
508 /*
509 * Do a dummy execvp to get the PLT entry resolved,
510 * so we avoid the resolver overhead on the real
511 * execvp call.
512 */
513 execvp("", (char **)argv);
514
515 /*
516 * Tell the parent we're ready to go
517 */
518 close(child_ready_pipe[1]);
519
520 /*
521 * Wait until the parent tells us to go.
522 */
523 if (read(go_pipe[0], &buf, 1) == -1)
524 perror("unable to read pipe");
525
526 execvp(argv[0], (char **)argv);
527
528 perror(argv[0]);
529 exit(-1);
530 }
531
Peter Zijlstra856e9662009-12-16 17:55:55 +0100532 close(child_ready_pipe[1]);
533 close(go_pipe[0]);
534 /*
535 * wait for child to settle
536 */
537 if (read(child_ready_pipe[0], &buf, 1) == -1) {
538 perror("unable to read pipe");
539 exit(-1);
540 }
541 close(child_ready_pipe[0]);
542 }
543
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300544 if (forks && target_pid == -1 && !system_wide)
545 pid = child_pid;
546 else
547 pid = target_pid;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100548
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100549 if ((!system_wide && !inherit) || profile_cpu != -1) {
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300550 open_counters(profile_cpu, pid);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100551 } else {
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100552 nr_cpus = read_cpu_map();
Peter Zijlstra856e9662009-12-16 17:55:55 +0100553 for (i = 0; i < nr_cpus; i++)
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300554 open_counters(cpumap[i], pid);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200555 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200556
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200557 if (file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200558 err = perf_header__write(&session->header, output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200559 if (err < 0)
560 return err;
561 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200562
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200563 post_processing_offset = lseek(output, 0, SEEK_CUR);
564
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200565 err = event__synthesize_kernel_mmap(process_synthesized_event,
566 session, "_text");
567 if (err < 0) {
568 pr_err("Couldn't record kernel reference relocation symbol.\n");
569 return err;
570 }
571
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200572 err = event__synthesize_modules(process_synthesized_event, session);
573 if (err < 0) {
574 pr_err("Couldn't record kernel reference relocation symbol.\n");
575 return err;
576 }
577
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200578 if (!system_wide && profile_cpu == -1)
austin_zhang@linux.intel.comf7e7ee32010-02-05 09:02:42 -0800579 event__synthesize_thread(target_pid, process_synthesized_event,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200580 session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200581 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200582 event__synthesize_threads(process_synthesized_event, session);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200583
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200584 if (realtime_prio) {
585 struct sched_param param;
586
587 param.sched_priority = realtime_prio;
588 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200589 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200590 exit(-1);
591 }
592 }
593
Peter Zijlstra856e9662009-12-16 17:55:55 +0100594 /*
595 * Let the child rip
596 */
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200597 if (forks)
598 close(go_pipe[1]);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100599
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200600 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200601 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200602
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200603 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200604 for (counter = 0; counter < nr_counters; counter++) {
605 if (mmap_array[i][counter].base)
606 mmap_read(&mmap_array[i][counter]);
607 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200608 }
609
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200610 if (hits == samples) {
611 if (done)
612 break;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200613 err = poll(event_array, nr_poll, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200614 waking++;
615 }
616
617 if (done) {
618 for (i = 0; i < nr_cpu; i++) {
619 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200620 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200621 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200622 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200623 }
624
Peter Zijlstra8b412662009-09-17 19:59:05 +0200625 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
626
Ingo Molnar021e9f42009-06-03 19:27:19 +0200627 /*
628 * Approximate RIP event size: 24 bytes.
629 */
630 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200631 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200632 (double)bytes_written / 1024.0 / 1024.0,
633 output_name,
634 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200635
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200636 return 0;
637}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200638
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200639static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200640 "perf record [<options>] [<command>]",
641 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200642 NULL
643};
644
Ingo Molnar52425192009-05-26 09:17:18 +0200645static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200646 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200647 "event selector. use 'perf list' to list available events",
648 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800649 OPT_CALLBACK(0, "filter", NULL, "filter",
650 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200651 OPT_INTEGER('p', "pid", &target_pid,
652 "record events on existing pid"),
653 OPT_INTEGER('r', "realtime", &realtime_prio,
654 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200655 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
656 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200657 OPT_BOOLEAN('a', "all-cpus", &system_wide,
658 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200659 OPT_BOOLEAN('A', "append", &append_file,
660 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200661 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
662 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200663 OPT_BOOLEAN('f', "force", &force,
664 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200665 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200666 "event period to sample"),
667 OPT_STRING('o', "output", &output_name, "file",
668 "output file name"),
669 OPT_BOOLEAN('i', "inherit", &inherit,
670 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200671 OPT_INTEGER('F', "freq", &freq,
672 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200673 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
674 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200675 OPT_BOOLEAN('g', "call-graph", &call_graph,
676 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200677 OPT_BOOLEAN('v', "verbose", &verbose,
678 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200679 OPT_BOOLEAN('s', "stat", &inherit_stat,
680 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200681 OPT_BOOLEAN('d', "data", &sample_address,
682 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200683 OPT_BOOLEAN('n', "no-samples", &no_samples,
684 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200685 OPT_BOOLEAN('M', "multiplex", &multiplex,
686 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200687 OPT_END()
688};
689
Ingo Molnarf37a2912009-07-01 12:37:06 +0200690int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200691{
692 int counter;
693
Anton Blancharda0541232009-07-22 23:04:12 +1000694 argc = parse_options(argc, argv, options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200695 PARSE_OPT_STOP_AT_NON_OPTION);
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200696 if (!argc && target_pid == -1 && !system_wide && profile_cpu == -1)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200697 usage_with_options(record_usage, options);
698
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200699 symbol__init();
700
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200701 if (!nr_counters) {
702 nr_counters = 1;
703 attrs[0].type = PERF_TYPE_HARDWARE;
704 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
705 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200706
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200707 /*
708 * User specified count overrides default frequency.
709 */
710 if (default_interval)
711 freq = 0;
712 else if (freq) {
713 default_interval = freq;
714 } else {
715 fprintf(stderr, "frequency and count are zero, aborting\n");
716 exit(EXIT_FAILURE);
717 }
718
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200719 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200720 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200721 continue;
722
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200723 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200724 }
725
726 return __cmd_record(argc, argv);
727}