blob: 0bde31bc8e2e63f1ddcd41103b16d19ce194afcd [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
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030029static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020030
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020031static long default_interval = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020032
Ingo Molnar42e59d72009-10-06 15:14:21 +020033static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020034static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020035static unsigned int mmap_pages = 128;
36static int freq = 1000;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020037static int output;
Tom Zanussi529870e2010-04-01 23:59:16 -050038static int pipe_output = 0;
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;
Ian Munsiec0555642010-04-13 18:37:33 +100042static bool raw_samples = false;
43static bool system_wide = false;
Ingo Molnar42e59d72009-10-06 15:14:21 +020044static int profile_cpu = -1;
45static pid_t target_pid = -1;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030046static pid_t target_tid = -1;
47static pid_t *all_tids = NULL;
48static int thread_num = 0;
Ingo Molnar42e59d72009-10-06 15:14:21 +020049static pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +100050static bool inherit = true;
51static bool force = false;
52static bool append_file = false;
53static bool call_graph = false;
54static bool inherit_stat = false;
55static bool no_samples = false;
56static bool sample_address = false;
57static bool multiplex = false;
Ingo Molnar42e59d72009-10-06 15:14:21 +020058static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020059
Ingo Molnar42e59d72009-10-06 15:14:21 +020060static long samples = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020061static struct timeval last_read;
62static struct timeval this_read;
63
Ingo Molnar42e59d72009-10-06 15:14:21 +020064static u64 bytes_written = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020065
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030066static struct pollfd *event_array;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020067
Ingo Molnar42e59d72009-10-06 15:14:21 +020068static int nr_poll = 0;
69static int nr_cpu = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020070
Ingo Molnar42e59d72009-10-06 15:14:21 +020071static int file_new = 1;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -020072static off_t post_processing_offset;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020073
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020074static struct perf_session *session;
Peter Zijlstraf5970552009-06-18 23:22:55 +020075
Ingo Molnara21ca2c2009-06-06 09:58:57 +020076struct mmap_data {
77 int counter;
78 void *base;
79 unsigned int mask;
80 unsigned int prev;
81};
82
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030083static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020084
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020085static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020086{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020087 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020088 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020089
90 head = pc->data_head;
91 rmb();
92
93 return head;
94}
95
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020096static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
97{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020098 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020099
100 /*
101 * ensure all reads are done before we write the tail out.
102 */
103 /* mb(); */
104 pc->data_tail = tail;
105}
106
Tom Zanussi92155452010-04-01 23:59:21 -0500107static void advance_output(size_t size)
108{
109 bytes_written += size;
110}
111
Peter Zijlstraf5970552009-06-18 23:22:55 +0200112static void write_output(void *buf, size_t size)
113{
114 while (size) {
115 int ret = write(output, buf, size);
116
117 if (ret < 0)
118 die("failed to write");
119
120 size -= ret;
121 buf += ret;
122
123 bytes_written += ret;
124 }
125}
126
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200127static int process_synthesized_event(event_t *event,
128 struct perf_session *self __used)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200129{
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200130 write_output(event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200131 return 0;
132}
133
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200134static void mmap_read(struct mmap_data *md)
135{
136 unsigned int head = mmap_read_head(md);
137 unsigned int old = md->prev;
138 unsigned char *data = md->base + page_size;
139 unsigned long size;
140 void *buf;
141 int diff;
142
143 gettimeofday(&this_read, NULL);
144
145 /*
146 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200147 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200148 *
149 * If we somehow ended up ahead of the head, we got messed up.
150 *
151 * In either case, truncate and restart at head.
152 */
153 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200154 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200155 struct timeval iv;
156 unsigned long msecs;
157
158 timersub(&this_read, &last_read, &iv);
159 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
160
161 fprintf(stderr, "WARNING: failed to keep up with mmap data."
162 " Last read %lu msecs ago.\n", msecs);
163
164 /*
165 * head points to a known good entry, start there.
166 */
167 old = head;
168 }
169
170 last_read = this_read;
171
172 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200173 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200174
175 size = head - old;
176
177 if ((old & md->mask) + size != (head & md->mask)) {
178 buf = &data[old & md->mask];
179 size = md->mask + 1 - (old & md->mask);
180 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200181
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200182 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200183 }
184
185 buf = &data[old & md->mask];
186 size = head - old;
187 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200188
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200189 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200190
191 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200192 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200193}
194
195static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200196static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200197
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200198static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200199{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200200 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200201 signr = sig;
202}
203
204static void sig_atexit(void)
205{
Chris Wilson933da832009-10-04 01:35:01 +0100206 if (child_pid != -1)
207 kill(child_pid, SIGTERM);
208
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200209 if (signr == -1)
210 return;
211
212 signal(signr, SIG_DFL);
213 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200214}
215
Ingo Molnarf250c0302009-06-05 13:18:41 +0200216static int group_fd;
217
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200218static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200219{
220 struct perf_header_attr *h_attr;
221
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200222 if (nr < session->header.attrs) {
223 h_attr = session->header.attr[nr];
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200224 } else {
225 h_attr = perf_header_attr__new(a);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200226 if (h_attr != NULL)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200227 if (perf_header__add_attr(&session->header, h_attr) < 0) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200228 perf_header_attr__delete(h_attr);
229 h_attr = NULL;
230 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200231 }
232
233 return h_attr;
234}
235
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300236static void create_counter(int counter, int cpu)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200237{
Li Zefanc171b552009-10-15 11:22:07 +0800238 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200239 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200240 struct perf_header_attr *h_attr;
241 int track = !counter; /* only the first counter needs these */
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300242 int thread_index;
Li Zefanc171b552009-10-15 11:22:07 +0800243 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200244 struct {
245 u64 count;
246 u64 time_enabled;
247 u64 time_running;
248 u64 id;
249 } read_data;
250
251 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
252 PERF_FORMAT_TOTAL_TIME_RUNNING |
253 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200254
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200255 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200256
Eric B Munson8907fd62010-03-05 12:51:05 -0300257 if (nr_counters > 1)
258 attr->sample_type |= PERF_SAMPLE_ID;
259
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200260 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200261 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200262 attr->freq = 1;
263 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200264 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200265
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200266 if (no_samples)
267 attr->sample_freq = 0;
268
269 if (inherit_stat)
270 attr->inherit_stat = 1;
271
Anton Blanchard4bba8282009-07-16 15:44:29 +0200272 if (sample_address)
273 attr->sample_type |= PERF_SAMPLE_ADDR;
274
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200275 if (call_graph)
276 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
277
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200278 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200279 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200280 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200281 attr->sample_type |= PERF_SAMPLE_CPU;
282 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200283
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200284 attr->mmap = track;
285 attr->comm = track;
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100286 attr->inherit = inherit;
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300287 if (target_pid == -1 && !system_wide) {
288 attr->disabled = 1;
Eric B Munsonbedbfde2010-03-15 11:46:57 -0300289 attr->enable_on_exec = 1;
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300290 }
Eric B Munsonbedbfde2010-03-15 11:46:57 -0300291
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300292 for (thread_index = 0; thread_index < thread_num; thread_index++) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200293try_again:
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300294 fd[nr_cpu][counter][thread_index] = sys_perf_event_open(attr,
295 all_tids[thread_index], cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200296
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300297 if (fd[nr_cpu][counter][thread_index] < 0) {
298 int err = errno;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200299
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300300 if (err == EPERM || err == EACCES)
301 die("Permission error - are you root?\n"
302 "\t Consider tweaking"
303 " /proc/sys/kernel/perf_event_paranoid.\n");
304 else if (err == ENODEV && profile_cpu != -1) {
305 die("No such device - did you specify"
306 " an out-of-range profile CPU?\n");
307 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200308
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300309 /*
310 * If it's cycles then fall back to hrtimer
311 * based cpu-clock-tick sw counter, which
312 * is always available even if no PMU support:
313 */
314 if (attr->type == PERF_TYPE_HARDWARE
315 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200316
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300317 if (verbose)
318 warning(" ... trying to fall back to cpu-clock-ticks\n");
319 attr->type = PERF_TYPE_SOFTWARE;
320 attr->config = PERF_COUNT_SW_CPU_CLOCK;
321 goto try_again;
322 }
323 printf("\n");
324 error("perfcounter syscall returned with %d (%s)\n",
325 fd[nr_cpu][counter][thread_index], strerror(err));
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000326
327#if defined(__i386__) || defined(__x86_64__)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300328 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
329 die("No hardware sampling interrupt available."
330 " No APIC? If so then you can boot the kernel"
331 " with the \"lapic\" boot parameter to"
332 " force-enable it.\n");
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000333#endif
334
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300335 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200336 exit(-1);
337 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200338
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300339 h_attr = get_header_attr(attr, counter);
340 if (h_attr == NULL)
341 die("nomem\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200342
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300343 if (!file_new) {
344 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
345 fprintf(stderr, "incompatible append\n");
346 exit(-1);
347 }
348 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200349
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300350 if (read(fd[nr_cpu][counter][thread_index], &read_data, sizeof(read_data)) == -1) {
351 perror("Unable to read perf file descriptor\n");
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200352 exit(-1);
353 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200354
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300355 if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
356 pr_warning("Not enough memory to add id\n");
Li Zefanc171b552009-10-15 11:22:07 +0800357 exit(-1);
358 }
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300359
360 assert(fd[nr_cpu][counter][thread_index] >= 0);
361 fcntl(fd[nr_cpu][counter][thread_index], F_SETFL, O_NONBLOCK);
362
363 /*
364 * First counter acts as the group leader:
365 */
366 if (group && group_fd == -1)
367 group_fd = fd[nr_cpu][counter][thread_index];
368 if (multiplex && multiplex_fd == -1)
369 multiplex_fd = fd[nr_cpu][counter][thread_index];
370
371 if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) {
372
373 ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
374 assert(ret != -1);
375 } else {
376 event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index];
377 event_array[nr_poll].events = POLLIN;
378 nr_poll++;
379
380 mmap_array[nr_cpu][counter][thread_index].counter = counter;
381 mmap_array[nr_cpu][counter][thread_index].prev = 0;
382 mmap_array[nr_cpu][counter][thread_index].mask = mmap_pages*page_size - 1;
383 mmap_array[nr_cpu][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
384 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0);
385 if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) {
386 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
387 exit(-1);
388 }
389 }
390
391 if (filter != NULL) {
392 ret = ioctl(fd[nr_cpu][counter][thread_index],
393 PERF_EVENT_IOC_SET_FILTER, filter);
394 if (ret) {
395 error("failed to set filter with %d (%s)\n", errno,
396 strerror(errno));
397 exit(-1);
398 }
399 }
Li Zefanc171b552009-10-15 11:22:07 +0800400 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200401}
402
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300403static void open_counters(int cpu)
Ingo Molnarf250c0302009-06-05 13:18:41 +0200404{
405 int counter;
406
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200407 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200408 for (counter = 0; counter < nr_counters; counter++)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300409 create_counter(counter, cpu);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200410
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200411 nr_cpu++;
412}
413
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200414static int process_buildids(void)
415{
416 u64 size = lseek(output, 0, SEEK_CUR);
417
Arnaldo Carvalho de Melo9f591fd2010-03-11 15:53:11 -0300418 if (size == 0)
419 return 0;
420
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200421 session->fd = output;
422 return __perf_session__process_events(session, post_processing_offset,
423 size - post_processing_offset,
424 size, &build_id__mark_dso_hit_ops);
425}
426
Peter Zijlstraf5970552009-06-18 23:22:55 +0200427static void atexit_header(void)
428{
Tom Zanussic7929e42010-04-01 23:59:22 -0500429 if (!pipe_output) {
430 session->header.data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200431
Tom Zanussic7929e42010-04-01 23:59:22 -0500432 process_buildids();
433 perf_header__write(&session->header, output, true);
434 } else {
435 int err;
436
437 err = event__synthesize_build_ids(process_synthesized_event,
438 session);
439 if (err < 0)
440 pr_err("Couldn't synthesize build ids.\n");
441 }
Peter Zijlstraf5970552009-06-18 23:22:55 +0200442}
443
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200444static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200445{
446 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200447 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200448 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200449 int flags;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200450 int err;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200451 unsigned long waking = 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100452 int child_ready_pipe[2], go_pipe[2];
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300453 const bool forks = argc > 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100454 char buf;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200455
456 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200457
Peter Zijlstraf5970552009-06-18 23:22:55 +0200458 atexit(sig_atexit);
459 signal(SIGCHLD, sig_handler);
460 signal(SIGINT, sig_handler);
461
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200462 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100463 perror("failed to create pipes");
464 exit(-1);
465 }
466
Tom Zanussi529870e2010-04-01 23:59:16 -0500467 if (!strcmp(output_name, "-"))
468 pipe_output = 1;
469 else if (!stat(output_name, &st) && st.st_size) {
Arnaldo Carvalho de Melob38d3462009-12-14 20:09:30 -0200470 if (!force) {
471 if (!append_file) {
472 pr_err("Error, output file %s exists, use -A "
473 "to append or -f to overwrite.\n",
474 output_name);
475 exit(-1);
476 }
477 } else {
478 char oldname[PATH_MAX];
479 snprintf(oldname, sizeof(oldname), "%s.old",
480 output_name);
481 unlink(oldname);
482 rename(output_name, oldname);
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200483 }
484 } else {
Ian Munsiec0555642010-04-13 18:37:33 +1000485 append_file = false;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200486 }
487
Xiao Guangrongf887f302010-02-04 16:46:42 +0800488 flags = O_CREAT|O_RDWR;
Ingo Molnarabaff322009-06-02 22:59:57 +0200489 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200490 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200491 else
492 flags |= O_TRUNC;
493
Tom Zanussi529870e2010-04-01 23:59:16 -0500494 if (pipe_output)
495 output = STDOUT_FILENO;
496 else
497 output = open(output_name, flags, S_IRUSR | S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200498 if (output < 0) {
499 perror("failed to create output file");
500 exit(-1);
501 }
502
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200503 session = perf_session__new(output_name, O_WRONLY, force);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200504 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200505 pr_err("Not enough memory for reading perf file header\n");
506 return -1;
507 }
508
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200509 if (!file_new) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500510 err = perf_header__read(session, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200511 if (err < 0)
512 return err;
513 }
514
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200515 if (raw_samples) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200516 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200517 } else {
518 for (i = 0; i < nr_counters; i++) {
519 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200520 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200521 break;
522 }
523 }
524 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200525
Peter Zijlstraf5970552009-06-18 23:22:55 +0200526 atexit(atexit_header);
527
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200528 if (forks) {
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300529 child_pid = fork();
Peter Zijlstra856e9662009-12-16 17:55:55 +0100530 if (pid < 0) {
531 perror("failed to fork");
532 exit(-1);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200533 }
Peter Zijlstra856e9662009-12-16 17:55:55 +0100534
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300535 if (!child_pid) {
Tom Zanussi529870e2010-04-01 23:59:16 -0500536 if (pipe_output)
537 dup2(2, 1);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100538 close(child_ready_pipe[0]);
539 close(go_pipe[1]);
540 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
541
542 /*
543 * Do a dummy execvp to get the PLT entry resolved,
544 * so we avoid the resolver overhead on the real
545 * execvp call.
546 */
547 execvp("", (char **)argv);
548
549 /*
550 * Tell the parent we're ready to go
551 */
552 close(child_ready_pipe[1]);
553
554 /*
555 * Wait until the parent tells us to go.
556 */
557 if (read(go_pipe[0], &buf, 1) == -1)
558 perror("unable to read pipe");
559
560 execvp(argv[0], (char **)argv);
561
562 perror(argv[0]);
563 exit(-1);
564 }
565
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300566 if (!system_wide && target_tid == -1 && target_pid == -1)
567 all_tids[0] = child_pid;
568
Peter Zijlstra856e9662009-12-16 17:55:55 +0100569 close(child_ready_pipe[1]);
570 close(go_pipe[0]);
571 /*
572 * wait for child to settle
573 */
574 if (read(child_ready_pipe[0], &buf, 1) == -1) {
575 perror("unable to read pipe");
576 exit(-1);
577 }
578 close(child_ready_pipe[0]);
579 }
580
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100581 if ((!system_wide && !inherit) || profile_cpu != -1) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300582 open_counters(profile_cpu);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100583 } else {
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100584 nr_cpus = read_cpu_map();
Peter Zijlstra856e9662009-12-16 17:55:55 +0100585 for (i = 0; i < nr_cpus; i++)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300586 open_counters(cpumap[i]);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200587 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200588
Tom Zanussi529870e2010-04-01 23:59:16 -0500589 if (pipe_output) {
590 err = perf_header__write_pipe(output);
591 if (err < 0)
592 return err;
593 } else if (file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200594 err = perf_header__write(&session->header, output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200595 if (err < 0)
596 return err;
597 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200598
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200599 post_processing_offset = lseek(output, 0, SEEK_CUR);
600
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500601 if (pipe_output) {
602 err = event__synthesize_attrs(&session->header,
603 process_synthesized_event,
604 session);
605 if (err < 0) {
606 pr_err("Couldn't synthesize attrs.\n");
607 return err;
608 }
Tom Zanussicd19a032010-04-01 23:59:20 -0500609
610 err = event__synthesize_event_types(process_synthesized_event,
611 session);
612 if (err < 0) {
613 pr_err("Couldn't synthesize event_types.\n");
614 return err;
615 }
Tom Zanussi92155452010-04-01 23:59:21 -0500616
617 err = event__synthesize_tracing_data(output, attrs,
618 nr_counters,
619 process_synthesized_event,
620 session);
621 if (err <= 0) {
622 pr_err("Couldn't record tracing data.\n");
623 return err;
624 }
625
626 advance_output(err);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500627 }
628
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200629 err = event__synthesize_kernel_mmap(process_synthesized_event,
630 session, "_text");
Arnaldo Carvalho de Melo70162132010-03-30 18:27:39 -0300631 if (err < 0)
632 err = event__synthesize_kernel_mmap(process_synthesized_event,
633 session, "_stext");
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200634 if (err < 0) {
635 pr_err("Couldn't record kernel reference relocation symbol.\n");
636 return err;
637 }
638
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200639 err = event__synthesize_modules(process_synthesized_event, session);
640 if (err < 0) {
641 pr_err("Couldn't record kernel reference relocation symbol.\n");
642 return err;
643 }
644
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200645 if (!system_wide && profile_cpu == -1)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300646 event__synthesize_thread(target_tid, process_synthesized_event,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200647 session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200648 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200649 event__synthesize_threads(process_synthesized_event, session);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200650
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200651 if (realtime_prio) {
652 struct sched_param param;
653
654 param.sched_priority = realtime_prio;
655 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200656 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200657 exit(-1);
658 }
659 }
660
Peter Zijlstra856e9662009-12-16 17:55:55 +0100661 /*
662 * Let the child rip
663 */
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200664 if (forks)
665 close(go_pipe[1]);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100666
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200667 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200668 int hits = samples;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300669 int thread;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200670
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200671 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200672 for (counter = 0; counter < nr_counters; counter++) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300673 for (thread = 0;
674 thread < thread_num; thread++) {
675 if (mmap_array[i][counter][thread].base)
676 mmap_read(&mmap_array[i][counter][thread]);
677 }
678
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200679 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200680 }
681
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200682 if (hits == samples) {
683 if (done)
684 break;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200685 err = poll(event_array, nr_poll, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200686 waking++;
687 }
688
689 if (done) {
690 for (i = 0; i < nr_cpu; i++) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300691 for (counter = 0;
692 counter < nr_counters;
693 counter++) {
694 for (thread = 0;
695 thread < thread_num;
696 thread++)
697 ioctl(fd[i][counter][thread],
698 PERF_EVENT_IOC_DISABLE);
699 }
Peter Zijlstra8b412662009-09-17 19:59:05 +0200700 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200701 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200702 }
703
Peter Zijlstra8b412662009-09-17 19:59:05 +0200704 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
705
Ingo Molnar021e9f42009-06-03 19:27:19 +0200706 /*
707 * Approximate RIP event size: 24 bytes.
708 */
709 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200710 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200711 (double)bytes_written / 1024.0 / 1024.0,
712 output_name,
713 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200714
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200715 return 0;
716}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200717
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200718static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200719 "perf record [<options>] [<command>]",
720 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200721 NULL
722};
723
Ingo Molnar52425192009-05-26 09:17:18 +0200724static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200725 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200726 "event selector. use 'perf list' to list available events",
727 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800728 OPT_CALLBACK(0, "filter", NULL, "filter",
729 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200730 OPT_INTEGER('p', "pid", &target_pid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300731 "record events on existing process id"),
732 OPT_INTEGER('t', "tid", &target_tid,
733 "record events on existing thread id"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200734 OPT_INTEGER('r', "realtime", &realtime_prio,
735 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200736 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
737 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200738 OPT_BOOLEAN('a', "all-cpus", &system_wide,
739 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200740 OPT_BOOLEAN('A', "append", &append_file,
741 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200742 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
743 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200744 OPT_BOOLEAN('f', "force", &force,
745 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200746 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200747 "event period to sample"),
748 OPT_STRING('o', "output", &output_name, "file",
749 "output file name"),
750 OPT_BOOLEAN('i', "inherit", &inherit,
751 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200752 OPT_INTEGER('F', "freq", &freq,
753 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200754 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
755 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200756 OPT_BOOLEAN('g', "call-graph", &call_graph,
757 "do call-graph (stack chain/backtrace) recording"),
Ian Munsiec0555642010-04-13 18:37:33 +1000758 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +0200759 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200760 OPT_BOOLEAN('s', "stat", &inherit_stat,
761 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200762 OPT_BOOLEAN('d', "data", &sample_address,
763 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200764 OPT_BOOLEAN('n', "no-samples", &no_samples,
765 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200766 OPT_BOOLEAN('M', "multiplex", &multiplex,
767 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200768 OPT_END()
769};
770
Ingo Molnarf37a2912009-07-01 12:37:06 +0200771int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200772{
773 int counter;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300774 int i,j;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200775
Anton Blancharda0541232009-07-22 23:04:12 +1000776 argc = parse_options(argc, argv, options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200777 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300778 if (!argc && target_pid == -1 && target_tid == -1 &&
779 !system_wide && profile_cpu == -1)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200780 usage_with_options(record_usage, options);
781
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200782 symbol__init();
783
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200784 if (!nr_counters) {
785 nr_counters = 1;
786 attrs[0].type = PERF_TYPE_HARDWARE;
787 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
788 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200789
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300790 if (target_pid != -1) {
791 target_tid = target_pid;
792 thread_num = find_all_tid(target_pid, &all_tids);
793 if (thread_num <= 0) {
794 fprintf(stderr, "Can't find all threads of pid %d\n",
795 target_pid);
796 usage_with_options(record_usage, options);
797 }
798 } else {
799 all_tids=malloc(sizeof(pid_t));
800 if (!all_tids)
801 return -ENOMEM;
802
803 all_tids[0] = target_tid;
804 thread_num = 1;
805 }
806
807 for (i = 0; i < MAX_NR_CPUS; i++) {
808 for (j = 0; j < MAX_COUNTERS; j++) {
809 fd[i][j] = malloc(sizeof(int)*thread_num);
Zhang, Yanmin5a103172010-03-25 19:59:01 -0300810 mmap_array[i][j] = zalloc(
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300811 sizeof(struct mmap_data)*thread_num);
812 if (!fd[i][j] || !mmap_array[i][j])
813 return -ENOMEM;
814 }
815 }
816 event_array = malloc(
817 sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
818 if (!event_array)
819 return -ENOMEM;
820
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200821 /*
822 * User specified count overrides default frequency.
823 */
824 if (default_interval)
825 freq = 0;
826 else if (freq) {
827 default_interval = freq;
828 } else {
829 fprintf(stderr, "frequency and count are zero, aborting\n");
830 exit(EXIT_FAILURE);
831 }
832
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200833 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200834 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200835 continue;
836
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200837 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200838 }
839
840 return __cmd_record(argc, argv);
841}