blob: dcda8993082dd6aa0b4f7eba542cc0359f9bf295 [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"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020018
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019#include "util/header.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020020#include "util/event.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020021#include "util/debug.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020022#include "util/session.h"
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020023#include "util/symbol.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110024#include "util/cpumap.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020025
Peter Zijlstra97124d52009-06-02 15:52:24 +020026#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020027#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020028
Frederic Weisbecker7865e812010-04-14 19:42:07 +020029enum write_mode_t {
30 WRITE_FORCE,
31 WRITE_APPEND
32};
33
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030034static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020035
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020036static long default_interval = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020037
Ingo Molnar42e59d72009-10-06 15:14:21 +020038static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020039static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020040static unsigned int mmap_pages = 128;
41static int freq = 1000;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020042static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020043static const char *output_name = "perf.data";
Ingo Molnar42e59d72009-10-06 15:14:21 +020044static int group = 0;
45static unsigned int realtime_prio = 0;
Ian Munsiec0555642010-04-13 18:37:33 +100046static bool raw_samples = false;
47static bool system_wide = false;
Ingo Molnar42e59d72009-10-06 15:14:21 +020048static int profile_cpu = -1;
49static pid_t target_pid = -1;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030050static pid_t target_tid = -1;
51static pid_t *all_tids = NULL;
52static int thread_num = 0;
Ingo Molnar42e59d72009-10-06 15:14:21 +020053static pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +100054static bool inherit = true;
Frederic Weisbecker7865e812010-04-14 19:42:07 +020055static enum write_mode_t write_mode = WRITE_FORCE;
Ian Munsiec0555642010-04-13 18:37:33 +100056static bool call_graph = false;
57static bool inherit_stat = false;
58static bool no_samples = false;
59static bool sample_address = false;
60static bool multiplex = false;
Ingo Molnar42e59d72009-10-06 15:14:21 +020061static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020062
Ingo Molnar42e59d72009-10-06 15:14:21 +020063static long samples = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020064static struct timeval last_read;
65static struct timeval this_read;
66
Ingo Molnar42e59d72009-10-06 15:14:21 +020067static u64 bytes_written = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020068
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030069static struct pollfd *event_array;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020070
Ingo Molnar42e59d72009-10-06 15:14:21 +020071static int nr_poll = 0;
72static int nr_cpu = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020073
Ingo Molnar42e59d72009-10-06 15:14:21 +020074static int file_new = 1;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -020075static off_t post_processing_offset;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020076
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020077static struct perf_session *session;
Peter Zijlstraf5970552009-06-18 23:22:55 +020078
Ingo Molnara21ca2c2009-06-06 09:58:57 +020079struct mmap_data {
80 int counter;
81 void *base;
82 unsigned int mask;
83 unsigned int prev;
84};
85
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030086static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020087
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020088static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020089{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020090 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020091 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020092
93 head = pc->data_head;
94 rmb();
95
96 return head;
97}
98
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020099static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
100{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200101 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200102
103 /*
104 * ensure all reads are done before we write the tail out.
105 */
106 /* mb(); */
107 pc->data_tail = tail;
108}
109
Peter Zijlstraf5970552009-06-18 23:22:55 +0200110static void write_output(void *buf, size_t size)
111{
112 while (size) {
113 int ret = write(output, buf, size);
114
115 if (ret < 0)
116 die("failed to write");
117
118 size -= ret;
119 buf += ret;
120
121 bytes_written += ret;
122 }
123}
124
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200125static int process_synthesized_event(event_t *event,
126 struct perf_session *self __used)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200127{
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200128 write_output(event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200129 return 0;
130}
131
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200132static void mmap_read(struct mmap_data *md)
133{
134 unsigned int head = mmap_read_head(md);
135 unsigned int old = md->prev;
136 unsigned char *data = md->base + page_size;
137 unsigned long size;
138 void *buf;
139 int diff;
140
141 gettimeofday(&this_read, NULL);
142
143 /*
144 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200145 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200146 *
147 * If we somehow ended up ahead of the head, we got messed up.
148 *
149 * In either case, truncate and restart at head.
150 */
151 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200152 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200153 struct timeval iv;
154 unsigned long msecs;
155
156 timersub(&this_read, &last_read, &iv);
157 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
158
159 fprintf(stderr, "WARNING: failed to keep up with mmap data."
160 " Last read %lu msecs ago.\n", msecs);
161
162 /*
163 * head points to a known good entry, start there.
164 */
165 old = head;
166 }
167
168 last_read = this_read;
169
170 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200171 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200172
173 size = head - old;
174
175 if ((old & md->mask) + size != (head & md->mask)) {
176 buf = &data[old & md->mask];
177 size = md->mask + 1 - (old & md->mask);
178 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200179
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200180 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200181 }
182
183 buf = &data[old & md->mask];
184 size = head - old;
185 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200186
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200187 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200188
189 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200190 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200191}
192
193static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200194static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200195
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200196static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200197{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200198 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200199 signr = sig;
200}
201
202static void sig_atexit(void)
203{
Chris Wilson933da832009-10-04 01:35:01 +0100204 if (child_pid != -1)
205 kill(child_pid, SIGTERM);
206
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200207 if (signr == -1)
208 return;
209
210 signal(signr, SIG_DFL);
211 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200212}
213
Ingo Molnarf250c0302009-06-05 13:18:41 +0200214static int group_fd;
215
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200216static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200217{
218 struct perf_header_attr *h_attr;
219
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200220 if (nr < session->header.attrs) {
221 h_attr = session->header.attr[nr];
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200222 } else {
223 h_attr = perf_header_attr__new(a);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200224 if (h_attr != NULL)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200225 if (perf_header__add_attr(&session->header, h_attr) < 0) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200226 perf_header_attr__delete(h_attr);
227 h_attr = NULL;
228 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200229 }
230
231 return h_attr;
232}
233
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300234static void create_counter(int counter, int cpu)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200235{
Li Zefanc171b552009-10-15 11:22:07 +0800236 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200237 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200238 struct perf_header_attr *h_attr;
239 int track = !counter; /* only the first counter needs these */
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300240 int thread_index;
Li Zefanc171b552009-10-15 11:22:07 +0800241 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200242 struct {
243 u64 count;
244 u64 time_enabled;
245 u64 time_running;
246 u64 id;
247 } read_data;
248
249 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
250 PERF_FORMAT_TOTAL_TIME_RUNNING |
251 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200252
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200253 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200254
Eric B Munson8907fd62010-03-05 12:51:05 -0300255 if (nr_counters > 1)
256 attr->sample_type |= PERF_SAMPLE_ID;
257
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200258 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200259 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200260 attr->freq = 1;
261 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200262 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200263
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200264 if (no_samples)
265 attr->sample_freq = 0;
266
267 if (inherit_stat)
268 attr->inherit_stat = 1;
269
Anton Blanchard4bba8282009-07-16 15:44:29 +0200270 if (sample_address)
271 attr->sample_type |= PERF_SAMPLE_ADDR;
272
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200273 if (call_graph)
274 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
275
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200276 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200277 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200278 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200279 attr->sample_type |= PERF_SAMPLE_CPU;
280 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200281
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200282 attr->mmap = track;
283 attr->comm = track;
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100284 attr->inherit = inherit;
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300285 if (target_pid == -1 && !system_wide) {
286 attr->disabled = 1;
Eric B Munsonbedbfde2010-03-15 11:46:57 -0300287 attr->enable_on_exec = 1;
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300288 }
Eric B Munsonbedbfde2010-03-15 11:46:57 -0300289
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300290 for (thread_index = 0; thread_index < thread_num; thread_index++) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200291try_again:
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300292 fd[nr_cpu][counter][thread_index] = sys_perf_event_open(attr,
293 all_tids[thread_index], cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200294
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300295 if (fd[nr_cpu][counter][thread_index] < 0) {
296 int err = errno;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200297
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300298 if (err == EPERM || err == EACCES)
299 die("Permission error - are you root?\n"
300 "\t Consider tweaking"
301 " /proc/sys/kernel/perf_event_paranoid.\n");
302 else if (err == ENODEV && profile_cpu != -1) {
303 die("No such device - did you specify"
304 " an out-of-range profile CPU?\n");
305 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200306
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300307 /*
308 * If it's cycles then fall back to hrtimer
309 * based cpu-clock-tick sw counter, which
310 * is always available even if no PMU support:
311 */
312 if (attr->type == PERF_TYPE_HARDWARE
313 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200314
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300315 if (verbose)
316 warning(" ... trying to fall back to cpu-clock-ticks\n");
317 attr->type = PERF_TYPE_SOFTWARE;
318 attr->config = PERF_COUNT_SW_CPU_CLOCK;
319 goto try_again;
320 }
321 printf("\n");
322 error("perfcounter syscall returned with %d (%s)\n",
323 fd[nr_cpu][counter][thread_index], strerror(err));
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000324
325#if defined(__i386__) || defined(__x86_64__)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300326 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
327 die("No hardware sampling interrupt available."
328 " No APIC? If so then you can boot the kernel"
329 " with the \"lapic\" boot parameter to"
330 " force-enable it.\n");
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000331#endif
332
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300333 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200334 exit(-1);
335 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200336
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300337 h_attr = get_header_attr(attr, counter);
338 if (h_attr == NULL)
339 die("nomem\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200340
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300341 if (!file_new) {
342 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
343 fprintf(stderr, "incompatible append\n");
344 exit(-1);
345 }
346 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200347
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300348 if (read(fd[nr_cpu][counter][thread_index], &read_data, sizeof(read_data)) == -1) {
349 perror("Unable to read perf file descriptor\n");
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200350 exit(-1);
351 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200352
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300353 if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
354 pr_warning("Not enough memory to add id\n");
Li Zefanc171b552009-10-15 11:22:07 +0800355 exit(-1);
356 }
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300357
358 assert(fd[nr_cpu][counter][thread_index] >= 0);
359 fcntl(fd[nr_cpu][counter][thread_index], F_SETFL, O_NONBLOCK);
360
361 /*
362 * First counter acts as the group leader:
363 */
364 if (group && group_fd == -1)
365 group_fd = fd[nr_cpu][counter][thread_index];
366 if (multiplex && multiplex_fd == -1)
367 multiplex_fd = fd[nr_cpu][counter][thread_index];
368
369 if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) {
370
371 ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
372 assert(ret != -1);
373 } else {
374 event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index];
375 event_array[nr_poll].events = POLLIN;
376 nr_poll++;
377
378 mmap_array[nr_cpu][counter][thread_index].counter = counter;
379 mmap_array[nr_cpu][counter][thread_index].prev = 0;
380 mmap_array[nr_cpu][counter][thread_index].mask = mmap_pages*page_size - 1;
381 mmap_array[nr_cpu][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
382 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0);
383 if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) {
384 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
385 exit(-1);
386 }
387 }
388
389 if (filter != NULL) {
390 ret = ioctl(fd[nr_cpu][counter][thread_index],
391 PERF_EVENT_IOC_SET_FILTER, filter);
392 if (ret) {
393 error("failed to set filter with %d (%s)\n", errno,
394 strerror(errno));
395 exit(-1);
396 }
397 }
Li Zefanc171b552009-10-15 11:22:07 +0800398 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200399}
400
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300401static void open_counters(int cpu)
Ingo Molnarf250c0302009-06-05 13:18:41 +0200402{
403 int counter;
404
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200405 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200406 for (counter = 0; counter < nr_counters; counter++)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300407 create_counter(counter, cpu);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200408
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200409 nr_cpu++;
410}
411
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200412static int process_buildids(void)
413{
414 u64 size = lseek(output, 0, SEEK_CUR);
415
Arnaldo Carvalho de Melo9f591fd2010-03-11 15:53:11 -0300416 if (size == 0)
417 return 0;
418
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200419 session->fd = output;
420 return __perf_session__process_events(session, post_processing_offset,
421 size - post_processing_offset,
422 size, &build_id__mark_dso_hit_ops);
423}
424
Peter Zijlstraf5970552009-06-18 23:22:55 +0200425static void atexit_header(void)
426{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200427 session->header.data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200428
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200429 process_buildids();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200430 perf_header__write(&session->header, output, true);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200431}
432
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200433static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200434{
435 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200436 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200437 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200438 int flags;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200439 int err;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200440 unsigned long waking = 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100441 int child_ready_pipe[2], go_pipe[2];
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300442 const bool forks = argc > 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100443 char buf;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200444
445 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200446
Peter Zijlstraf5970552009-06-18 23:22:55 +0200447 atexit(sig_atexit);
448 signal(SIGCHLD, sig_handler);
449 signal(SIGINT, sig_handler);
450
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200451 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100452 perror("failed to create pipes");
453 exit(-1);
454 }
455
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200456 if (!stat(output_name, &st) && st.st_size) {
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200457 if (write_mode == WRITE_FORCE) {
Arnaldo Carvalho de Melob38d3462009-12-14 20:09:30 -0200458 char oldname[PATH_MAX];
459 snprintf(oldname, sizeof(oldname), "%s.old",
460 output_name);
461 unlink(oldname);
462 rename(output_name, oldname);
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200463 }
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200464 } else if (write_mode == WRITE_APPEND) {
465 write_mode = WRITE_FORCE;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200466 }
467
Xiao Guangrongf887f302010-02-04 16:46:42 +0800468 flags = O_CREAT|O_RDWR;
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200469 if (write_mode == WRITE_APPEND)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200470 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200471 else
472 flags |= O_TRUNC;
473
474 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200475 if (output < 0) {
476 perror("failed to create output file");
477 exit(-1);
478 }
479
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200480 session = perf_session__new(output_name, O_WRONLY,
481 write_mode == WRITE_FORCE);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200482 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200483 pr_err("Not enough memory for reading perf file header\n");
484 return -1;
485 }
486
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200487 if (!file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200488 err = perf_header__read(&session->header, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200489 if (err < 0)
490 return err;
491 }
492
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200493 if (raw_samples) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200494 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200495 } else {
496 for (i = 0; i < nr_counters; i++) {
497 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200498 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200499 break;
500 }
501 }
502 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200503
Peter Zijlstraf5970552009-06-18 23:22:55 +0200504 atexit(atexit_header);
505
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200506 if (forks) {
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300507 child_pid = fork();
Peter Zijlstra856e9662009-12-16 17:55:55 +0100508 if (pid < 0) {
509 perror("failed to fork");
510 exit(-1);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200511 }
Peter Zijlstra856e9662009-12-16 17:55:55 +0100512
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300513 if (!child_pid) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100514 close(child_ready_pipe[0]);
515 close(go_pipe[1]);
516 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
517
518 /*
519 * Do a dummy execvp to get the PLT entry resolved,
520 * so we avoid the resolver overhead on the real
521 * execvp call.
522 */
523 execvp("", (char **)argv);
524
525 /*
526 * Tell the parent we're ready to go
527 */
528 close(child_ready_pipe[1]);
529
530 /*
531 * Wait until the parent tells us to go.
532 */
533 if (read(go_pipe[0], &buf, 1) == -1)
534 perror("unable to read pipe");
535
536 execvp(argv[0], (char **)argv);
537
538 perror(argv[0]);
539 exit(-1);
540 }
541
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300542 if (!system_wide && target_tid == -1 && target_pid == -1)
543 all_tids[0] = child_pid;
544
Peter Zijlstra856e9662009-12-16 17:55:55 +0100545 close(child_ready_pipe[1]);
546 close(go_pipe[0]);
547 /*
548 * wait for child to settle
549 */
550 if (read(child_ready_pipe[0], &buf, 1) == -1) {
551 perror("unable to read pipe");
552 exit(-1);
553 }
554 close(child_ready_pipe[0]);
555 }
556
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100557 if ((!system_wide && !inherit) || profile_cpu != -1) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300558 open_counters(profile_cpu);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100559 } else {
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100560 nr_cpus = read_cpu_map();
Peter Zijlstra856e9662009-12-16 17:55:55 +0100561 for (i = 0; i < nr_cpus; i++)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300562 open_counters(cpumap[i]);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200563 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200564
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200565 if (file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200566 err = perf_header__write(&session->header, output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200567 if (err < 0)
568 return err;
569 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200570
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200571 post_processing_offset = lseek(output, 0, SEEK_CUR);
572
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200573 err = event__synthesize_kernel_mmap(process_synthesized_event,
574 session, "_text");
Arnaldo Carvalho de Melo70162132010-03-30 18:27:39 -0300575 if (err < 0)
576 err = event__synthesize_kernel_mmap(process_synthesized_event,
577 session, "_stext");
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200578 if (err < 0) {
579 pr_err("Couldn't record kernel reference relocation symbol.\n");
580 return err;
581 }
582
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200583 err = event__synthesize_modules(process_synthesized_event, session);
584 if (err < 0) {
585 pr_err("Couldn't record kernel reference relocation symbol.\n");
586 return err;
587 }
588
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200589 if (!system_wide && profile_cpu == -1)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300590 event__synthesize_thread(target_tid, process_synthesized_event,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200591 session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200592 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200593 event__synthesize_threads(process_synthesized_event, session);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200594
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200595 if (realtime_prio) {
596 struct sched_param param;
597
598 param.sched_priority = realtime_prio;
599 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200600 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200601 exit(-1);
602 }
603 }
604
Peter Zijlstra856e9662009-12-16 17:55:55 +0100605 /*
606 * Let the child rip
607 */
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200608 if (forks)
609 close(go_pipe[1]);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100610
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200611 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200612 int hits = samples;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300613 int thread;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200614
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200615 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200616 for (counter = 0; counter < nr_counters; counter++) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300617 for (thread = 0;
618 thread < thread_num; thread++) {
619 if (mmap_array[i][counter][thread].base)
620 mmap_read(&mmap_array[i][counter][thread]);
621 }
622
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200623 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200624 }
625
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200626 if (hits == samples) {
627 if (done)
628 break;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200629 err = poll(event_array, nr_poll, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200630 waking++;
631 }
632
633 if (done) {
634 for (i = 0; i < nr_cpu; i++) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300635 for (counter = 0;
636 counter < nr_counters;
637 counter++) {
638 for (thread = 0;
639 thread < thread_num;
640 thread++)
641 ioctl(fd[i][counter][thread],
642 PERF_EVENT_IOC_DISABLE);
643 }
Peter Zijlstra8b412662009-09-17 19:59:05 +0200644 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200645 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200646 }
647
Peter Zijlstra8b412662009-09-17 19:59:05 +0200648 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
649
Ingo Molnar021e9f42009-06-03 19:27:19 +0200650 /*
651 * Approximate RIP event size: 24 bytes.
652 */
653 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200654 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200655 (double)bytes_written / 1024.0 / 1024.0,
656 output_name,
657 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200658
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200659 return 0;
660}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200661
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200662static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200663 "perf record [<options>] [<command>]",
664 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200665 NULL
666};
667
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200668static bool force, append_file;
669
Ingo Molnar52425192009-05-26 09:17:18 +0200670static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200671 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200672 "event selector. use 'perf list' to list available events",
673 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800674 OPT_CALLBACK(0, "filter", NULL, "filter",
675 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200676 OPT_INTEGER('p', "pid", &target_pid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300677 "record events on existing process id"),
678 OPT_INTEGER('t', "tid", &target_tid,
679 "record events on existing thread id"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200680 OPT_INTEGER('r', "realtime", &realtime_prio,
681 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200682 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
683 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200684 OPT_BOOLEAN('a', "all-cpus", &system_wide,
685 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200686 OPT_BOOLEAN('A', "append", &append_file,
687 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200688 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
689 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200690 OPT_BOOLEAN('f', "force", &force,
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200691 "overwrite existing data file (deprecated)"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200692 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200693 "event period to sample"),
694 OPT_STRING('o', "output", &output_name, "file",
695 "output file name"),
696 OPT_BOOLEAN('i', "inherit", &inherit,
697 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200698 OPT_INTEGER('F', "freq", &freq,
699 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200700 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
701 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200702 OPT_BOOLEAN('g', "call-graph", &call_graph,
703 "do call-graph (stack chain/backtrace) recording"),
Ian Munsiec0555642010-04-13 18:37:33 +1000704 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +0200705 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200706 OPT_BOOLEAN('s', "stat", &inherit_stat,
707 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200708 OPT_BOOLEAN('d', "data", &sample_address,
709 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200710 OPT_BOOLEAN('n', "no-samples", &no_samples,
711 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200712 OPT_BOOLEAN('M', "multiplex", &multiplex,
713 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200714 OPT_END()
715};
716
Ingo Molnarf37a2912009-07-01 12:37:06 +0200717int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200718{
719 int counter;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300720 int i,j;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200721
Anton Blancharda0541232009-07-22 23:04:12 +1000722 argc = parse_options(argc, argv, options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200723 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300724 if (!argc && target_pid == -1 && target_tid == -1 &&
725 !system_wide && profile_cpu == -1)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200726 usage_with_options(record_usage, options);
727
Frederic Weisbecker7865e812010-04-14 19:42:07 +0200728 if (force && append_file) {
729 fprintf(stderr, "Can't overwrite and append at the same time."
730 " You need to choose between -f and -A");
731 usage_with_options(record_usage, options);
732 } else if (append_file) {
733 write_mode = WRITE_APPEND;
734 } else {
735 write_mode = WRITE_FORCE;
736 }
737
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200738 symbol__init();
739
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200740 if (!nr_counters) {
741 nr_counters = 1;
742 attrs[0].type = PERF_TYPE_HARDWARE;
743 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
744 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200745
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300746 if (target_pid != -1) {
747 target_tid = target_pid;
748 thread_num = find_all_tid(target_pid, &all_tids);
749 if (thread_num <= 0) {
750 fprintf(stderr, "Can't find all threads of pid %d\n",
751 target_pid);
752 usage_with_options(record_usage, options);
753 }
754 } else {
755 all_tids=malloc(sizeof(pid_t));
756 if (!all_tids)
757 return -ENOMEM;
758
759 all_tids[0] = target_tid;
760 thread_num = 1;
761 }
762
763 for (i = 0; i < MAX_NR_CPUS; i++) {
764 for (j = 0; j < MAX_COUNTERS; j++) {
765 fd[i][j] = malloc(sizeof(int)*thread_num);
Zhang, Yanmin5a103172010-03-25 19:59:01 -0300766 mmap_array[i][j] = zalloc(
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300767 sizeof(struct mmap_data)*thread_num);
768 if (!fd[i][j] || !mmap_array[i][j])
769 return -ENOMEM;
770 }
771 }
772 event_array = malloc(
773 sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
774 if (!event_array)
775 return -ENOMEM;
776
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200777 /*
778 * User specified count overrides default frequency.
779 */
780 if (default_interval)
781 freq = 0;
782 else if (freq) {
783 default_interval = freq;
784 } else {
785 fprintf(stderr, "frequency and count are zero, aborting\n");
786 exit(EXIT_FAILURE);
787 }
788
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200789 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200790 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200791 continue;
792
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200793 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200794 }
795
796 return __cmd_record(argc, argv);
797}