blob: 771533ced6a803d23886214d5551df1f0db440b1 [file] [log] [blame]
Ingo Molnarabaff322009-06-02 22:59:57 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-record.c
3 *
4 * Builtin record command: Record the profile of a workload
5 * (or a CPU, or a PID) into the perf.data output file - for
6 * later analysis via perf report.
Ingo Molnarabaff322009-06-02 22:59:57 +02007 */
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08008#define _FILE_OFFSET_BITS 64
9
Ingo Molnar16f762a2009-05-27 09:10:38 +020010#include "builtin.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020011
12#include "perf.h"
13
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -020014#include "util/build-id.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020015#include "util/util.h"
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020016#include "util/parse-options.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020017#include "util/parse-events.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030018#include "util/string.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020019
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020020#include "util/header.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020021#include "util/event.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020022#include "util/debug.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020023#include "util/session.h"
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020024#include "util/symbol.h"
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;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -020068static off_t post_processing_offset;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020069
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020070static struct perf_session *session;
Peter Zijlstraf5970552009-06-18 23:22:55 +020071
Ingo Molnara21ca2c2009-06-06 09:58:57 +020072struct mmap_data {
73 int counter;
74 void *base;
75 unsigned int mask;
76 unsigned int prev;
77};
78
79static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
80
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020081static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020082{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020083 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020084 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020085
86 head = pc->data_head;
87 rmb();
88
89 return head;
90}
91
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020092static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
93{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020094 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020095
96 /*
97 * ensure all reads are done before we write the tail out.
98 */
99 /* mb(); */
100 pc->data_tail = tail;
101}
102
Peter Zijlstraf5970552009-06-18 23:22:55 +0200103static void write_output(void *buf, size_t size)
104{
105 while (size) {
106 int ret = write(output, buf, size);
107
108 if (ret < 0)
109 die("failed to write");
110
111 size -= ret;
112 buf += ret;
113
114 bytes_written += ret;
115 }
116}
117
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200118static int process_synthesized_event(event_t *event,
119 struct perf_session *self __used)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200120{
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200121 write_output(event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200122 return 0;
123}
124
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200125static void mmap_read(struct mmap_data *md)
126{
127 unsigned int head = mmap_read_head(md);
128 unsigned int old = md->prev;
129 unsigned char *data = md->base + page_size;
130 unsigned long size;
131 void *buf;
132 int diff;
133
134 gettimeofday(&this_read, NULL);
135
136 /*
137 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200138 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200139 *
140 * If we somehow ended up ahead of the head, we got messed up.
141 *
142 * In either case, truncate and restart at head.
143 */
144 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200145 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200146 struct timeval iv;
147 unsigned long msecs;
148
149 timersub(&this_read, &last_read, &iv);
150 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
151
152 fprintf(stderr, "WARNING: failed to keep up with mmap data."
153 " Last read %lu msecs ago.\n", msecs);
154
155 /*
156 * head points to a known good entry, start there.
157 */
158 old = head;
159 }
160
161 last_read = this_read;
162
163 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200164 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200165
166 size = head - old;
167
168 if ((old & md->mask) + size != (head & md->mask)) {
169 buf = &data[old & md->mask];
170 size = md->mask + 1 - (old & md->mask);
171 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200172
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200173 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200174 }
175
176 buf = &data[old & md->mask];
177 size = head - old;
178 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200179
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200180 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200181
182 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200183 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200184}
185
186static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200187static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200188
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200189static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200190{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200191 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200192 signr = sig;
193}
194
195static void sig_atexit(void)
196{
Chris Wilson933da832009-10-04 01:35:01 +0100197 if (child_pid != -1)
198 kill(child_pid, SIGTERM);
199
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200200 if (signr == -1)
201 return;
202
203 signal(signr, SIG_DFL);
204 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200205}
206
Ingo Molnarf250c0302009-06-05 13:18:41 +0200207static int group_fd;
208
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200209static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200210{
211 struct perf_header_attr *h_attr;
212
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200213 if (nr < session->header.attrs) {
214 h_attr = session->header.attr[nr];
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200215 } else {
216 h_attr = perf_header_attr__new(a);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200217 if (h_attr != NULL)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200218 if (perf_header__add_attr(&session->header, h_attr) < 0) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200219 perf_header_attr__delete(h_attr);
220 h_attr = NULL;
221 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200222 }
223
224 return h_attr;
225}
226
Ingo Molnarf250c0302009-06-05 13:18:41 +0200227static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200228{
Li Zefanc171b552009-10-15 11:22:07 +0800229 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200230 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200231 struct perf_header_attr *h_attr;
232 int track = !counter; /* only the first counter needs these */
Li Zefanc171b552009-10-15 11:22:07 +0800233 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200234 struct {
235 u64 count;
236 u64 time_enabled;
237 u64 time_running;
238 u64 id;
239 } read_data;
240
241 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
242 PERF_FORMAT_TOTAL_TIME_RUNNING |
243 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200244
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200245 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200246
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200247 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200248 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200249 attr->freq = 1;
250 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200251 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200252
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200253 if (no_samples)
254 attr->sample_freq = 0;
255
256 if (inherit_stat)
257 attr->inherit_stat = 1;
258
Anton Blanchard4bba8282009-07-16 15:44:29 +0200259 if (sample_address)
260 attr->sample_type |= PERF_SAMPLE_ADDR;
261
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200262 if (call_graph)
263 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
264
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200265 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200266 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200267 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200268 attr->sample_type |= PERF_SAMPLE_CPU;
269 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200270
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200271 attr->mmap = track;
272 attr->comm = track;
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100273 attr->inherit = inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200274 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200275
Ingo Molnar3da297a2009-06-07 17:39:02 +0200276try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200277 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200278
279 if (fd[nr_cpu][counter] < 0) {
280 int err = errno;
281
Pekka Enbergc10edee2009-11-08 18:01:06 +0200282 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200283 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200284 else if (err == ENODEV && profile_cpu != -1)
285 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200286
287 /*
288 * If it's cycles then fall back to hrtimer
289 * based cpu-clock-tick sw counter, which
290 * is always available even if no PMU support:
291 */
292 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200293 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200294
295 if (verbose)
296 warning(" ... trying to fall back to cpu-clock-ticks\n");
297 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200298 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200299 goto try_again;
300 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200301 printf("\n");
302 error("perfcounter syscall returned with %d (%s)\n",
303 fd[nr_cpu][counter], strerror(err));
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000304
305#if defined(__i386__) || defined(__x86_64__)
306 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
307 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");
308#endif
309
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200310 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200311 exit(-1);
312 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200313
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200314 h_attr = get_header_attr(attr, counter);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200315 if (h_attr == NULL)
316 die("nomem\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200317
318 if (!file_new) {
319 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
320 fprintf(stderr, "incompatible append\n");
321 exit(-1);
322 }
323 }
324
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200325 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
326 perror("Unable to read perf file descriptor\n");
327 exit(-1);
328 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200329
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -0200330 if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
331 pr_warning("Not enough memory to add id\n");
332 exit(-1);
333 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200334
Ingo Molnarf250c0302009-06-05 13:18:41 +0200335 assert(fd[nr_cpu][counter] >= 0);
336 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
337
338 /*
339 * First counter acts as the group leader:
340 */
341 if (group && group_fd == -1)
342 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200343 if (multiplex && multiplex_fd == -1)
344 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200345
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200346 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
Ingo Molnarf250c0302009-06-05 13:18:41 +0200347
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200348 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200349 assert(ret != -1);
350 } else {
351 event_array[nr_poll].fd = fd[nr_cpu][counter];
352 event_array[nr_poll].events = POLLIN;
353 nr_poll++;
354
355 mmap_array[nr_cpu][counter].counter = counter;
356 mmap_array[nr_cpu][counter].prev = 0;
357 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
358 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
359 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
360 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
361 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
362 exit(-1);
363 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200364 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200365
Li Zefanc171b552009-10-15 11:22:07 +0800366 if (filter != NULL) {
367 ret = ioctl(fd[nr_cpu][counter],
368 PERF_EVENT_IOC_SET_FILTER, filter);
369 if (ret) {
370 error("failed to set filter with %d (%s)\n", errno,
371 strerror(errno));
372 exit(-1);
373 }
374 }
375
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200376 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200377}
378
379static void open_counters(int cpu, pid_t pid)
380{
381 int counter;
382
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200383 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200384 for (counter = 0; counter < nr_counters; counter++)
385 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200386
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200387 nr_cpu++;
388}
389
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200390static int process_buildids(void)
391{
392 u64 size = lseek(output, 0, SEEK_CUR);
393
394 session->fd = output;
395 return __perf_session__process_events(session, post_processing_offset,
396 size - post_processing_offset,
397 size, &build_id__mark_dso_hit_ops);
398}
399
Peter Zijlstraf5970552009-06-18 23:22:55 +0200400static void atexit_header(void)
401{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200402 session->header.data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200403
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200404 process_buildids();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200405 perf_header__write(&session->header, output, true);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200406}
407
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200408static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200409{
410 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200411 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200412 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200413 int flags;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200414 int err;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200415 unsigned long waking = 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100416 int child_ready_pipe[2], go_pipe[2];
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200417 const bool forks = target_pid == -1 && argc > 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100418 char buf;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200419
420 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200421 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
422 assert(nr_cpus <= MAX_NR_CPUS);
423 assert(nr_cpus >= 0);
424
Peter Zijlstraf5970552009-06-18 23:22:55 +0200425 atexit(sig_atexit);
426 signal(SIGCHLD, sig_handler);
427 signal(SIGINT, sig_handler);
428
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200429 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100430 perror("failed to create pipes");
431 exit(-1);
432 }
433
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200434 if (!stat(output_name, &st) && st.st_size) {
Arnaldo Carvalho de Melob38d3462009-12-14 20:09:30 -0200435 if (!force) {
436 if (!append_file) {
437 pr_err("Error, output file %s exists, use -A "
438 "to append or -f to overwrite.\n",
439 output_name);
440 exit(-1);
441 }
442 } else {
443 char oldname[PATH_MAX];
444 snprintf(oldname, sizeof(oldname), "%s.old",
445 output_name);
446 unlink(oldname);
447 rename(output_name, oldname);
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200448 }
449 } else {
450 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200451 }
452
Xiao Guangrongf887f302010-02-04 16:46:42 +0800453 flags = O_CREAT|O_RDWR;
Ingo Molnarabaff322009-06-02 22:59:57 +0200454 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200455 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200456 else
457 flags |= O_TRUNC;
458
459 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200460 if (output < 0) {
461 perror("failed to create output file");
462 exit(-1);
463 }
464
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200465 session = perf_session__new(output_name, O_WRONLY, force);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200466 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200467 pr_err("Not enough memory for reading perf file header\n");
468 return -1;
469 }
470
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200471 if (!file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200472 err = perf_header__read(&session->header, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200473 if (err < 0)
474 return err;
475 }
476
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200477 if (raw_samples) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200478 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200479 } else {
480 for (i = 0; i < nr_counters; i++) {
481 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
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 break;
484 }
485 }
486 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200487
Peter Zijlstraf5970552009-06-18 23:22:55 +0200488 atexit(atexit_header);
489
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200490 if (forks) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100491 pid = fork();
492 if (pid < 0) {
493 perror("failed to fork");
494 exit(-1);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200495 }
Peter Zijlstra856e9662009-12-16 17:55:55 +0100496
497 if (!pid) {
498 close(child_ready_pipe[0]);
499 close(go_pipe[1]);
500 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
501
502 /*
503 * Do a dummy execvp to get the PLT entry resolved,
504 * so we avoid the resolver overhead on the real
505 * execvp call.
506 */
507 execvp("", (char **)argv);
508
509 /*
510 * Tell the parent we're ready to go
511 */
512 close(child_ready_pipe[1]);
513
514 /*
515 * Wait until the parent tells us to go.
516 */
517 if (read(go_pipe[0], &buf, 1) == -1)
518 perror("unable to read pipe");
519
520 execvp(argv[0], (char **)argv);
521
522 perror(argv[0]);
523 exit(-1);
524 }
525
526 child_pid = pid;
527
528 if (!system_wide)
529 target_pid = pid;
530
531 close(child_ready_pipe[1]);
532 close(go_pipe[0]);
533 /*
534 * wait for child to settle
535 */
536 if (read(child_ready_pipe[0], &buf, 1) == -1) {
537 perror("unable to read pipe");
538 exit(-1);
539 }
540 close(child_ready_pipe[0]);
541 }
542
543
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100544 if ((!system_wide && !inherit) || profile_cpu != -1) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100545 open_counters(profile_cpu, target_pid);
546 } else {
547 for (i = 0; i < nr_cpus; i++)
548 open_counters(i, target_pid);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200549 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200550
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200551 if (file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200552 err = perf_header__write(&session->header, output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200553 if (err < 0)
554 return err;
555 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200556
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200557 post_processing_offset = lseek(output, 0, SEEK_CUR);
558
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200559 err = event__synthesize_kernel_mmap(process_synthesized_event,
560 session, "_text");
561 if (err < 0) {
562 pr_err("Couldn't record kernel reference relocation symbol.\n");
563 return err;
564 }
565
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200566 err = event__synthesize_modules(process_synthesized_event, session);
567 if (err < 0) {
568 pr_err("Couldn't record kernel reference relocation symbol.\n");
569 return err;
570 }
571
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200572 if (!system_wide && profile_cpu == -1)
austin_zhang@linux.intel.comf7e7ee32010-02-05 09:02:42 -0800573 event__synthesize_thread(target_pid, process_synthesized_event,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200574 session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200575 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200576 event__synthesize_threads(process_synthesized_event, session);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200577
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200578 if (realtime_prio) {
579 struct sched_param param;
580
581 param.sched_priority = realtime_prio;
582 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200583 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200584 exit(-1);
585 }
586 }
587
Peter Zijlstra856e9662009-12-16 17:55:55 +0100588 /*
589 * Let the child rip
590 */
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200591 if (forks)
592 close(go_pipe[1]);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100593
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200594 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200595 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200596
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200597 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200598 for (counter = 0; counter < nr_counters; counter++) {
599 if (mmap_array[i][counter].base)
600 mmap_read(&mmap_array[i][counter]);
601 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200602 }
603
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200604 if (hits == samples) {
605 if (done)
606 break;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200607 err = poll(event_array, nr_poll, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200608 waking++;
609 }
610
611 if (done) {
612 for (i = 0; i < nr_cpu; i++) {
613 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200614 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200615 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200616 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200617 }
618
Peter Zijlstra8b412662009-09-17 19:59:05 +0200619 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
620
Ingo Molnar021e9f42009-06-03 19:27:19 +0200621 /*
622 * Approximate RIP event size: 24 bytes.
623 */
624 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200625 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200626 (double)bytes_written / 1024.0 / 1024.0,
627 output_name,
628 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200629
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200630 return 0;
631}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200632
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200633static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200634 "perf record [<options>] [<command>]",
635 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200636 NULL
637};
638
Ingo Molnar52425192009-05-26 09:17:18 +0200639static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200640 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200641 "event selector. use 'perf list' to list available events",
642 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800643 OPT_CALLBACK(0, "filter", NULL, "filter",
644 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200645 OPT_INTEGER('p', "pid", &target_pid,
646 "record events on existing pid"),
647 OPT_INTEGER('r', "realtime", &realtime_prio,
648 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200649 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
650 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200651 OPT_BOOLEAN('a', "all-cpus", &system_wide,
652 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200653 OPT_BOOLEAN('A', "append", &append_file,
654 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200655 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
656 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200657 OPT_BOOLEAN('f', "force", &force,
658 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200659 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200660 "event period to sample"),
661 OPT_STRING('o', "output", &output_name, "file",
662 "output file name"),
663 OPT_BOOLEAN('i', "inherit", &inherit,
664 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200665 OPT_INTEGER('F', "freq", &freq,
666 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200667 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
668 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200669 OPT_BOOLEAN('g', "call-graph", &call_graph,
670 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200671 OPT_BOOLEAN('v', "verbose", &verbose,
672 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200673 OPT_BOOLEAN('s', "stat", &inherit_stat,
674 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200675 OPT_BOOLEAN('d', "data", &sample_address,
676 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200677 OPT_BOOLEAN('n', "no-samples", &no_samples,
678 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200679 OPT_BOOLEAN('M', "multiplex", &multiplex,
680 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200681 OPT_END()
682};
683
Ingo Molnarf37a2912009-07-01 12:37:06 +0200684int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200685{
686 int counter;
687
Anton Blancharda0541232009-07-22 23:04:12 +1000688 argc = parse_options(argc, argv, options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200689 PARSE_OPT_STOP_AT_NON_OPTION);
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200690 if (!argc && target_pid == -1 && !system_wide && profile_cpu == -1)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200691 usage_with_options(record_usage, options);
692
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200693 symbol__init();
694
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200695 if (!nr_counters) {
696 nr_counters = 1;
697 attrs[0].type = PERF_TYPE_HARDWARE;
698 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
699 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200700
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200701 /*
702 * User specified count overrides default frequency.
703 */
704 if (default_interval)
705 freq = 0;
706 else if (freq) {
707 default_interval = freq;
708 } else {
709 fprintf(stderr, "frequency and count are zero, aborting\n");
710 exit(EXIT_FAILURE);
711 }
712
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200713 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200714 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200715 continue;
716
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200717 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200718 }
719
720 return __cmd_record(argc, argv);
721}