blob: 3775abe2af78621e2356f97b7719eff45c703438 [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{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200429 session->header.data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200430
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200431 process_buildids();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200432 perf_header__write(&session->header, output, true);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200433}
434
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200435static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200436{
437 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200438 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200439 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200440 int flags;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200441 int err;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200442 unsigned long waking = 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100443 int child_ready_pipe[2], go_pipe[2];
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300444 const bool forks = argc > 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100445 char buf;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200446
447 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200448
Peter Zijlstraf5970552009-06-18 23:22:55 +0200449 atexit(sig_atexit);
450 signal(SIGCHLD, sig_handler);
451 signal(SIGINT, sig_handler);
452
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200453 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100454 perror("failed to create pipes");
455 exit(-1);
456 }
457
Tom Zanussi529870e2010-04-01 23:59:16 -0500458 if (!strcmp(output_name, "-"))
459 pipe_output = 1;
460 else if (!stat(output_name, &st) && st.st_size) {
Arnaldo Carvalho de Melob38d3462009-12-14 20:09:30 -0200461 if (!force) {
462 if (!append_file) {
463 pr_err("Error, output file %s exists, use -A "
464 "to append or -f to overwrite.\n",
465 output_name);
466 exit(-1);
467 }
468 } else {
469 char oldname[PATH_MAX];
470 snprintf(oldname, sizeof(oldname), "%s.old",
471 output_name);
472 unlink(oldname);
473 rename(output_name, oldname);
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200474 }
475 } else {
Ian Munsiec0555642010-04-13 18:37:33 +1000476 append_file = false;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200477 }
478
Xiao Guangrongf887f302010-02-04 16:46:42 +0800479 flags = O_CREAT|O_RDWR;
Ingo Molnarabaff322009-06-02 22:59:57 +0200480 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200481 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200482 else
483 flags |= O_TRUNC;
484
Tom Zanussi529870e2010-04-01 23:59:16 -0500485 if (pipe_output)
486 output = STDOUT_FILENO;
487 else
488 output = open(output_name, flags, S_IRUSR | S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200489 if (output < 0) {
490 perror("failed to create output file");
491 exit(-1);
492 }
493
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200494 session = perf_session__new(output_name, O_WRONLY, force);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200495 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200496 pr_err("Not enough memory for reading perf file header\n");
497 return -1;
498 }
499
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200500 if (!file_new) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500501 err = perf_header__read(session, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200502 if (err < 0)
503 return err;
504 }
505
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200506 if (raw_samples) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200507 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200508 } else {
509 for (i = 0; i < nr_counters; i++) {
510 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200511 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200512 break;
513 }
514 }
515 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200516
Peter Zijlstraf5970552009-06-18 23:22:55 +0200517 atexit(atexit_header);
518
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200519 if (forks) {
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300520 child_pid = fork();
Peter Zijlstra856e9662009-12-16 17:55:55 +0100521 if (pid < 0) {
522 perror("failed to fork");
523 exit(-1);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200524 }
Peter Zijlstra856e9662009-12-16 17:55:55 +0100525
Zhang, Yanmin46be6042010-03-18 11:36:04 -0300526 if (!child_pid) {
Tom Zanussi529870e2010-04-01 23:59:16 -0500527 if (pipe_output)
528 dup2(2, 1);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100529 close(child_ready_pipe[0]);
530 close(go_pipe[1]);
531 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
532
533 /*
534 * Do a dummy execvp to get the PLT entry resolved,
535 * so we avoid the resolver overhead on the real
536 * execvp call.
537 */
538 execvp("", (char **)argv);
539
540 /*
541 * Tell the parent we're ready to go
542 */
543 close(child_ready_pipe[1]);
544
545 /*
546 * Wait until the parent tells us to go.
547 */
548 if (read(go_pipe[0], &buf, 1) == -1)
549 perror("unable to read pipe");
550
551 execvp(argv[0], (char **)argv);
552
553 perror(argv[0]);
554 exit(-1);
555 }
556
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300557 if (!system_wide && target_tid == -1 && target_pid == -1)
558 all_tids[0] = child_pid;
559
Peter Zijlstra856e9662009-12-16 17:55:55 +0100560 close(child_ready_pipe[1]);
561 close(go_pipe[0]);
562 /*
563 * wait for child to settle
564 */
565 if (read(child_ready_pipe[0], &buf, 1) == -1) {
566 perror("unable to read pipe");
567 exit(-1);
568 }
569 close(child_ready_pipe[0]);
570 }
571
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100572 if ((!system_wide && !inherit) || profile_cpu != -1) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300573 open_counters(profile_cpu);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100574 } else {
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100575 nr_cpus = read_cpu_map();
Peter Zijlstra856e9662009-12-16 17:55:55 +0100576 for (i = 0; i < nr_cpus; i++)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300577 open_counters(cpumap[i]);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200578 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200579
Tom Zanussi529870e2010-04-01 23:59:16 -0500580 if (pipe_output) {
581 err = perf_header__write_pipe(output);
582 if (err < 0)
583 return err;
584 } else if (file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200585 err = perf_header__write(&session->header, output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200586 if (err < 0)
587 return err;
588 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200589
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200590 post_processing_offset = lseek(output, 0, SEEK_CUR);
591
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500592 if (pipe_output) {
593 err = event__synthesize_attrs(&session->header,
594 process_synthesized_event,
595 session);
596 if (err < 0) {
597 pr_err("Couldn't synthesize attrs.\n");
598 return err;
599 }
Tom Zanussicd19a032010-04-01 23:59:20 -0500600
601 err = event__synthesize_event_types(process_synthesized_event,
602 session);
603 if (err < 0) {
604 pr_err("Couldn't synthesize event_types.\n");
605 return err;
606 }
Tom Zanussi92155452010-04-01 23:59:21 -0500607
608 err = event__synthesize_tracing_data(output, attrs,
609 nr_counters,
610 process_synthesized_event,
611 session);
612 if (err <= 0) {
613 pr_err("Couldn't record tracing data.\n");
614 return err;
615 }
616
617 advance_output(err);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500618 }
619
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200620 err = event__synthesize_kernel_mmap(process_synthesized_event,
621 session, "_text");
Arnaldo Carvalho de Melo70162132010-03-30 18:27:39 -0300622 if (err < 0)
623 err = event__synthesize_kernel_mmap(process_synthesized_event,
624 session, "_stext");
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200625 if (err < 0) {
626 pr_err("Couldn't record kernel reference relocation symbol.\n");
627 return err;
628 }
629
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200630 err = event__synthesize_modules(process_synthesized_event, session);
631 if (err < 0) {
632 pr_err("Couldn't record kernel reference relocation symbol.\n");
633 return err;
634 }
635
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200636 if (!system_wide && profile_cpu == -1)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300637 event__synthesize_thread(target_tid, process_synthesized_event,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200638 session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200639 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200640 event__synthesize_threads(process_synthesized_event, session);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200641
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200642 if (realtime_prio) {
643 struct sched_param param;
644
645 param.sched_priority = realtime_prio;
646 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200647 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200648 exit(-1);
649 }
650 }
651
Peter Zijlstra856e9662009-12-16 17:55:55 +0100652 /*
653 * Let the child rip
654 */
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200655 if (forks)
656 close(go_pipe[1]);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100657
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200658 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200659 int hits = samples;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300660 int thread;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200661
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200662 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200663 for (counter = 0; counter < nr_counters; counter++) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300664 for (thread = 0;
665 thread < thread_num; thread++) {
666 if (mmap_array[i][counter][thread].base)
667 mmap_read(&mmap_array[i][counter][thread]);
668 }
669
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200670 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200671 }
672
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200673 if (hits == samples) {
674 if (done)
675 break;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200676 err = poll(event_array, nr_poll, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200677 waking++;
678 }
679
680 if (done) {
681 for (i = 0; i < nr_cpu; i++) {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300682 for (counter = 0;
683 counter < nr_counters;
684 counter++) {
685 for (thread = 0;
686 thread < thread_num;
687 thread++)
688 ioctl(fd[i][counter][thread],
689 PERF_EVENT_IOC_DISABLE);
690 }
Peter Zijlstra8b412662009-09-17 19:59:05 +0200691 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200692 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200693 }
694
Peter Zijlstra8b412662009-09-17 19:59:05 +0200695 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
696
Ingo Molnar021e9f42009-06-03 19:27:19 +0200697 /*
698 * Approximate RIP event size: 24 bytes.
699 */
700 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200701 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200702 (double)bytes_written / 1024.0 / 1024.0,
703 output_name,
704 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200705
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200706 return 0;
707}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200708
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200709static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200710 "perf record [<options>] [<command>]",
711 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200712 NULL
713};
714
Ingo Molnar52425192009-05-26 09:17:18 +0200715static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200716 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200717 "event selector. use 'perf list' to list available events",
718 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800719 OPT_CALLBACK(0, "filter", NULL, "filter",
720 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200721 OPT_INTEGER('p', "pid", &target_pid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300722 "record events on existing process id"),
723 OPT_INTEGER('t', "tid", &target_tid,
724 "record events on existing thread id"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200725 OPT_INTEGER('r', "realtime", &realtime_prio,
726 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200727 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
728 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200729 OPT_BOOLEAN('a', "all-cpus", &system_wide,
730 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200731 OPT_BOOLEAN('A', "append", &append_file,
732 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200733 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
734 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200735 OPT_BOOLEAN('f', "force", &force,
736 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200737 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200738 "event period to sample"),
739 OPT_STRING('o', "output", &output_name, "file",
740 "output file name"),
741 OPT_BOOLEAN('i', "inherit", &inherit,
742 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200743 OPT_INTEGER('F', "freq", &freq,
744 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200745 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
746 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200747 OPT_BOOLEAN('g', "call-graph", &call_graph,
748 "do call-graph (stack chain/backtrace) recording"),
Ian Munsiec0555642010-04-13 18:37:33 +1000749 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +0200750 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200751 OPT_BOOLEAN('s', "stat", &inherit_stat,
752 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200753 OPT_BOOLEAN('d', "data", &sample_address,
754 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200755 OPT_BOOLEAN('n', "no-samples", &no_samples,
756 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200757 OPT_BOOLEAN('M', "multiplex", &multiplex,
758 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200759 OPT_END()
760};
761
Ingo Molnarf37a2912009-07-01 12:37:06 +0200762int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200763{
764 int counter;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300765 int i,j;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200766
Anton Blancharda0541232009-07-22 23:04:12 +1000767 argc = parse_options(argc, argv, options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200768 PARSE_OPT_STOP_AT_NON_OPTION);
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300769 if (!argc && target_pid == -1 && target_tid == -1 &&
770 !system_wide && profile_cpu == -1)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200771 usage_with_options(record_usage, options);
772
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200773 symbol__init();
774
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200775 if (!nr_counters) {
776 nr_counters = 1;
777 attrs[0].type = PERF_TYPE_HARDWARE;
778 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
779 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200780
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300781 if (target_pid != -1) {
782 target_tid = target_pid;
783 thread_num = find_all_tid(target_pid, &all_tids);
784 if (thread_num <= 0) {
785 fprintf(stderr, "Can't find all threads of pid %d\n",
786 target_pid);
787 usage_with_options(record_usage, options);
788 }
789 } else {
790 all_tids=malloc(sizeof(pid_t));
791 if (!all_tids)
792 return -ENOMEM;
793
794 all_tids[0] = target_tid;
795 thread_num = 1;
796 }
797
798 for (i = 0; i < MAX_NR_CPUS; i++) {
799 for (j = 0; j < MAX_COUNTERS; j++) {
800 fd[i][j] = malloc(sizeof(int)*thread_num);
Zhang, Yanmin5a103172010-03-25 19:59:01 -0300801 mmap_array[i][j] = zalloc(
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300802 sizeof(struct mmap_data)*thread_num);
803 if (!fd[i][j] || !mmap_array[i][j])
804 return -ENOMEM;
805 }
806 }
807 event_array = malloc(
808 sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
809 if (!event_array)
810 return -ENOMEM;
811
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200812 /*
813 * User specified count overrides default frequency.
814 */
815 if (default_interval)
816 freq = 0;
817 else if (freq) {
818 default_interval = freq;
819 } else {
820 fprintf(stderr, "frequency and count are zero, aborting\n");
821 exit(EXIT_FAILURE);
822 }
823
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200824 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200825 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200826 continue;
827
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200828 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200829 }
830
831 return __cmd_record(argc, argv);
832}