blob: 949167efa1ed115ac337e8da201046a8f99112cc [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 _LARGEFILE64_SOURCE
9#define _FILE_OFFSET_BITS 64
10
Ingo Molnar16f762a2009-05-27 09:10:38 +020011#include "builtin.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020012
13#include "perf.h"
14
Thomas Gleixner6eda5832009-05-01 18:29:57 +020015#include "util/util.h"
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020016#include "util/parse-options.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020017#include "util/parse-events.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030018#include "util/string.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020019
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020020#include "util/header.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020021#include "util/event.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020022#include "util/debug.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020023#include "util/session.h"
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020024#include "util/symbol.h"
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
Peter Zijlstrade9ac072009-04-08 15:01:31 +020029static 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;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020038static const char *output_name = "perf.data";
Ingo Molnar42e59d72009-10-06 15:14:21 +020039static int group = 0;
40static unsigned int realtime_prio = 0;
41static int raw_samples = 0;
42static int system_wide = 0;
43static int profile_cpu = -1;
44static pid_t target_pid = -1;
45static pid_t child_pid = -1;
46static int inherit = 1;
47static int force = 0;
48static int append_file = 0;
49static int call_graph = 0;
50static int inherit_stat = 0;
51static int no_samples = 0;
52static int sample_address = 0;
53static int multiplex = 0;
54static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020055
Ingo Molnar42e59d72009-10-06 15:14:21 +020056static long samples = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020057static struct timeval last_read;
58static struct timeval this_read;
59
Ingo Molnar42e59d72009-10-06 15:14:21 +020060static u64 bytes_written = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020061
62static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
63
Ingo Molnar42e59d72009-10-06 15:14:21 +020064static int nr_poll = 0;
65static int nr_cpu = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020066
Ingo Molnar42e59d72009-10-06 15:14:21 +020067static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020068
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020069static struct perf_session *session;
Peter Zijlstraf5970552009-06-18 23:22:55 +020070
Ingo Molnara21ca2c2009-06-06 09:58:57 +020071struct mmap_data {
72 int counter;
73 void *base;
74 unsigned int mask;
75 unsigned int prev;
76};
77
78static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
79
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020080static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020081{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020082 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020083 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020084
85 head = pc->data_head;
86 rmb();
87
88 return head;
89}
90
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020091static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
92{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020093 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020094
95 /*
96 * ensure all reads are done before we write the tail out.
97 */
98 /* mb(); */
99 pc->data_tail = tail;
100}
101
Peter Zijlstraf5970552009-06-18 23:22:55 +0200102static void write_output(void *buf, size_t size)
103{
104 while (size) {
105 int ret = write(output, buf, size);
106
107 if (ret < 0)
108 die("failed to write");
109
110 size -= ret;
111 buf += ret;
112
113 bytes_written += ret;
114 }
115}
116
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200117static void write_event(event_t *buf, size_t size)
118{
Hitoshi Mitakea8e6f732010-01-30 20:55:41 +0900119 /*
120 * Add it to the list of DSOs, so that when we finish this
121 * record session we can pick the available build-ids.
122 */
123 if (buf->header.type == PERF_RECORD_MMAP) {
124 struct list_head *head = &dsos__user;
125 if (buf->mmap.header.misc == 1)
126 head = &dsos__kernel;
127 __dsos__findnew(head, buf->mmap.filename);
128 }
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200129
130 write_output(buf, size);
131}
132
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200133static int process_synthesized_event(event_t *event,
134 struct perf_session *self __used)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200135{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200136 write_event(event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200137 return 0;
138}
139
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200140static void mmap_read(struct mmap_data *md)
141{
142 unsigned int head = mmap_read_head(md);
143 unsigned int old = md->prev;
144 unsigned char *data = md->base + page_size;
145 unsigned long size;
146 void *buf;
147 int diff;
148
149 gettimeofday(&this_read, NULL);
150
151 /*
152 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200153 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200154 *
155 * If we somehow ended up ahead of the head, we got messed up.
156 *
157 * In either case, truncate and restart at head.
158 */
159 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200160 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200161 struct timeval iv;
162 unsigned long msecs;
163
164 timersub(&this_read, &last_read, &iv);
165 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
166
167 fprintf(stderr, "WARNING: failed to keep up with mmap data."
168 " Last read %lu msecs ago.\n", msecs);
169
170 /*
171 * head points to a known good entry, start there.
172 */
173 old = head;
174 }
175
176 last_read = this_read;
177
178 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200179 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200180
181 size = head - old;
182
183 if ((old & md->mask) + size != (head & md->mask)) {
184 buf = &data[old & md->mask];
185 size = md->mask + 1 - (old & md->mask);
186 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200187
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200188 write_event(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200189 }
190
191 buf = &data[old & md->mask];
192 size = head - old;
193 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200194
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200195 write_event(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200196
197 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200198 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200199}
200
201static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200202static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200203
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200204static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200205{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200206 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200207 signr = sig;
208}
209
210static void sig_atexit(void)
211{
Chris Wilson933da832009-10-04 01:35:01 +0100212 if (child_pid != -1)
213 kill(child_pid, SIGTERM);
214
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200215 if (signr == -1)
216 return;
217
218 signal(signr, SIG_DFL);
219 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200220}
221
Ingo Molnarf250c0302009-06-05 13:18:41 +0200222static int group_fd;
223
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200224static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200225{
226 struct perf_header_attr *h_attr;
227
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200228 if (nr < session->header.attrs) {
229 h_attr = session->header.attr[nr];
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200230 } else {
231 h_attr = perf_header_attr__new(a);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200232 if (h_attr != NULL)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200233 if (perf_header__add_attr(&session->header, h_attr) < 0) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200234 perf_header_attr__delete(h_attr);
235 h_attr = NULL;
236 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200237 }
238
239 return h_attr;
240}
241
Ingo Molnarf250c0302009-06-05 13:18:41 +0200242static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200243{
Li Zefanc171b552009-10-15 11:22:07 +0800244 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200245 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200246 struct perf_header_attr *h_attr;
247 int track = !counter; /* only the first counter needs these */
Li Zefanc171b552009-10-15 11:22:07 +0800248 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200249 struct {
250 u64 count;
251 u64 time_enabled;
252 u64 time_running;
253 u64 id;
254 } read_data;
255
256 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
257 PERF_FORMAT_TOTAL_TIME_RUNNING |
258 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200259
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200260 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200261
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200262 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200263 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200264 attr->freq = 1;
265 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200266 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200267
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200268 if (no_samples)
269 attr->sample_freq = 0;
270
271 if (inherit_stat)
272 attr->inherit_stat = 1;
273
Anton Blanchard4bba8282009-07-16 15:44:29 +0200274 if (sample_address)
275 attr->sample_type |= PERF_SAMPLE_ADDR;
276
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200277 if (call_graph)
278 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
279
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200280 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200281 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200282 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200283 attr->sample_type |= PERF_SAMPLE_CPU;
284 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200285
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200286 attr->mmap = track;
287 attr->comm = track;
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100288 attr->inherit = inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200289 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200290
Ingo Molnar3da297a2009-06-07 17:39:02 +0200291try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200292 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200293
294 if (fd[nr_cpu][counter] < 0) {
295 int err = errno;
296
Pekka Enbergc10edee2009-11-08 18:01:06 +0200297 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200298 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200299 else if (err == ENODEV && profile_cpu != -1)
300 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200301
302 /*
303 * If it's cycles then fall back to hrtimer
304 * based cpu-clock-tick sw counter, which
305 * is always available even if no PMU support:
306 */
307 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200308 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200309
310 if (verbose)
311 warning(" ... trying to fall back to cpu-clock-ticks\n");
312 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200313 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200314 goto try_again;
315 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200316 printf("\n");
317 error("perfcounter syscall returned with %d (%s)\n",
318 fd[nr_cpu][counter], strerror(err));
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000319
320#if defined(__i386__) || defined(__x86_64__)
321 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
322 die("No hardware sampling interrupt available. No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.\n");
323#endif
324
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200325 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200326 exit(-1);
327 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200328
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200329 h_attr = get_header_attr(attr, counter);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200330 if (h_attr == NULL)
331 die("nomem\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200332
333 if (!file_new) {
334 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
335 fprintf(stderr, "incompatible append\n");
336 exit(-1);
337 }
338 }
339
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200340 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
341 perror("Unable to read perf file descriptor\n");
342 exit(-1);
343 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200344
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -0200345 if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
346 pr_warning("Not enough memory to add id\n");
347 exit(-1);
348 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200349
Ingo Molnarf250c0302009-06-05 13:18:41 +0200350 assert(fd[nr_cpu][counter] >= 0);
351 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
352
353 /*
354 * First counter acts as the group leader:
355 */
356 if (group && group_fd == -1)
357 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200358 if (multiplex && multiplex_fd == -1)
359 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200360
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200361 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
Ingo Molnarf250c0302009-06-05 13:18:41 +0200362
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200363 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200364 assert(ret != -1);
365 } else {
366 event_array[nr_poll].fd = fd[nr_cpu][counter];
367 event_array[nr_poll].events = POLLIN;
368 nr_poll++;
369
370 mmap_array[nr_cpu][counter].counter = counter;
371 mmap_array[nr_cpu][counter].prev = 0;
372 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
373 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
374 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
375 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
376 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
377 exit(-1);
378 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200379 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200380
Li Zefanc171b552009-10-15 11:22:07 +0800381 if (filter != NULL) {
382 ret = ioctl(fd[nr_cpu][counter],
383 PERF_EVENT_IOC_SET_FILTER, filter);
384 if (ret) {
385 error("failed to set filter with %d (%s)\n", errno,
386 strerror(errno));
387 exit(-1);
388 }
389 }
390
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200391 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200392}
393
394static void open_counters(int cpu, pid_t pid)
395{
396 int counter;
397
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200398 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200399 for (counter = 0; counter < nr_counters; counter++)
400 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200401
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200402 nr_cpu++;
403}
404
Peter Zijlstraf5970552009-06-18 23:22:55 +0200405static void atexit_header(void)
406{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200407 session->header.data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200408
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200409 perf_header__write(&session->header, output, true);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200410}
411
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200412static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200413{
414 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200415 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200416 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200417 int flags;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200418 int err;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200419 unsigned long waking = 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100420 int child_ready_pipe[2], go_pipe[2];
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200421 const bool forks = target_pid == -1 && argc > 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100422 char buf;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200423
424 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200425 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
426 assert(nr_cpus <= MAX_NR_CPUS);
427 assert(nr_cpus >= 0);
428
Peter Zijlstraf5970552009-06-18 23:22:55 +0200429 atexit(sig_atexit);
430 signal(SIGCHLD, sig_handler);
431 signal(SIGINT, sig_handler);
432
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200433 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100434 perror("failed to create pipes");
435 exit(-1);
436 }
437
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200438 if (!stat(output_name, &st) && st.st_size) {
Arnaldo Carvalho de Melob38d3462009-12-14 20:09:30 -0200439 if (!force) {
440 if (!append_file) {
441 pr_err("Error, output file %s exists, use -A "
442 "to append or -f to overwrite.\n",
443 output_name);
444 exit(-1);
445 }
446 } else {
447 char oldname[PATH_MAX];
448 snprintf(oldname, sizeof(oldname), "%s.old",
449 output_name);
450 unlink(oldname);
451 rename(output_name, oldname);
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200452 }
453 } else {
454 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200455 }
456
Xiao Guangrongb8f46c52010-02-03 11:53:14 +0800457 flags = O_CREAT|O_RDWR|O_LARGEFILE;
Ingo Molnarabaff322009-06-02 22:59:57 +0200458 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200459 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200460 else
461 flags |= O_TRUNC;
462
463 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200464 if (output < 0) {
465 perror("failed to create output file");
466 exit(-1);
467 }
468
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200469 session = perf_session__new(output_name, O_WRONLY, force);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200470 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200471 pr_err("Not enough memory for reading perf file header\n");
472 return -1;
473 }
474
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200475 if (!file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200476 err = perf_header__read(&session->header, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200477 if (err < 0)
478 return err;
479 }
480
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200481 if (raw_samples) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200482 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200483 } else {
484 for (i = 0; i < nr_counters; i++) {
485 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200486 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200487 break;
488 }
489 }
490 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200491
Peter Zijlstraf5970552009-06-18 23:22:55 +0200492 atexit(atexit_header);
493
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200494 if (forks) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100495 pid = fork();
496 if (pid < 0) {
497 perror("failed to fork");
498 exit(-1);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200499 }
Peter Zijlstra856e9662009-12-16 17:55:55 +0100500
501 if (!pid) {
502 close(child_ready_pipe[0]);
503 close(go_pipe[1]);
504 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
505
506 /*
507 * Do a dummy execvp to get the PLT entry resolved,
508 * so we avoid the resolver overhead on the real
509 * execvp call.
510 */
511 execvp("", (char **)argv);
512
513 /*
514 * Tell the parent we're ready to go
515 */
516 close(child_ready_pipe[1]);
517
518 /*
519 * Wait until the parent tells us to go.
520 */
521 if (read(go_pipe[0], &buf, 1) == -1)
522 perror("unable to read pipe");
523
524 execvp(argv[0], (char **)argv);
525
526 perror(argv[0]);
527 exit(-1);
528 }
529
530 child_pid = pid;
531
532 if (!system_wide)
533 target_pid = pid;
534
535 close(child_ready_pipe[1]);
536 close(go_pipe[0]);
537 /*
538 * wait for child to settle
539 */
540 if (read(child_ready_pipe[0], &buf, 1) == -1) {
541 perror("unable to read pipe");
542 exit(-1);
543 }
544 close(child_ready_pipe[0]);
545 }
546
547
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100548 if ((!system_wide && !inherit) || profile_cpu != -1) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100549 open_counters(profile_cpu, target_pid);
550 } else {
551 for (i = 0; i < nr_cpus; i++)
552 open_counters(i, target_pid);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200553 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200554
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200555 if (file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200556 err = perf_header__write(&session->header, output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200557 if (err < 0)
558 return err;
559 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200560
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200561 err = event__synthesize_kernel_mmap(process_synthesized_event,
562 session, "_text");
563 if (err < 0) {
564 pr_err("Couldn't record kernel reference relocation symbol.\n");
565 return err;
566 }
567
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200568 err = event__synthesize_modules(process_synthesized_event, session);
569 if (err < 0) {
570 pr_err("Couldn't record kernel reference relocation symbol.\n");
571 return err;
572 }
573
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200574 if (!system_wide && profile_cpu == -1)
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200575 event__synthesize_thread(pid, process_synthesized_event,
576 session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200577 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200578 event__synthesize_threads(process_synthesized_event, session);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200579
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200580 if (realtime_prio) {
581 struct sched_param param;
582
583 param.sched_priority = realtime_prio;
584 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200585 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200586 exit(-1);
587 }
588 }
589
Peter Zijlstra856e9662009-12-16 17:55:55 +0100590 /*
591 * Let the child rip
592 */
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200593 if (forks)
594 close(go_pipe[1]);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100595
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200596 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200597 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200598
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200599 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200600 for (counter = 0; counter < nr_counters; counter++) {
601 if (mmap_array[i][counter].base)
602 mmap_read(&mmap_array[i][counter]);
603 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200604 }
605
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200606 if (hits == samples) {
607 if (done)
608 break;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200609 err = poll(event_array, nr_poll, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200610 waking++;
611 }
612
613 if (done) {
614 for (i = 0; i < nr_cpu; i++) {
615 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200616 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200617 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200618 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200619 }
620
Peter Zijlstra8b412662009-09-17 19:59:05 +0200621 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
622
Ingo Molnar021e9f42009-06-03 19:27:19 +0200623 /*
624 * Approximate RIP event size: 24 bytes.
625 */
626 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200627 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200628 (double)bytes_written / 1024.0 / 1024.0,
629 output_name,
630 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200631
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200632 return 0;
633}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200634
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200635static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200636 "perf record [<options>] [<command>]",
637 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200638 NULL
639};
640
Ingo Molnar52425192009-05-26 09:17:18 +0200641static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200642 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200643 "event selector. use 'perf list' to list available events",
644 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800645 OPT_CALLBACK(0, "filter", NULL, "filter",
646 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200647 OPT_INTEGER('p', "pid", &target_pid,
648 "record events on existing pid"),
649 OPT_INTEGER('r', "realtime", &realtime_prio,
650 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200651 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
652 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200653 OPT_BOOLEAN('a', "all-cpus", &system_wide,
654 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200655 OPT_BOOLEAN('A', "append", &append_file,
656 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200657 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
658 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200659 OPT_BOOLEAN('f', "force", &force,
660 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200661 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200662 "event period to sample"),
663 OPT_STRING('o', "output", &output_name, "file",
664 "output file name"),
665 OPT_BOOLEAN('i', "inherit", &inherit,
666 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200667 OPT_INTEGER('F', "freq", &freq,
668 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200669 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
670 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200671 OPT_BOOLEAN('g', "call-graph", &call_graph,
672 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200673 OPT_BOOLEAN('v', "verbose", &verbose,
674 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200675 OPT_BOOLEAN('s', "stat", &inherit_stat,
676 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200677 OPT_BOOLEAN('d', "data", &sample_address,
678 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200679 OPT_BOOLEAN('n', "no-samples", &no_samples,
680 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200681 OPT_BOOLEAN('M', "multiplex", &multiplex,
682 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200683 OPT_END()
684};
685
Ingo Molnarf37a2912009-07-01 12:37:06 +0200686int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200687{
688 int counter;
689
Anton Blancharda0541232009-07-22 23:04:12 +1000690 argc = parse_options(argc, argv, options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200691 PARSE_OPT_STOP_AT_NON_OPTION);
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200692 if (!argc && target_pid == -1 && !system_wide && profile_cpu == -1)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200693 usage_with_options(record_usage, options);
694
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200695 symbol__init();
696
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200697 if (!nr_counters) {
698 nr_counters = 1;
699 attrs[0].type = PERF_TYPE_HARDWARE;
700 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
701 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200702
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200703 /*
704 * User specified count overrides default frequency.
705 */
706 if (default_interval)
707 freq = 0;
708 else if (freq) {
709 default_interval = freq;
710 } else {
711 fprintf(stderr, "frequency and count are zero, aborting\n");
712 exit(EXIT_FAILURE);
713 }
714
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200715 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200716 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200717 continue;
718
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200719 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200720 }
721
722 return __cmd_record(argc, argv);
723}