blob: 265425322734eecfc3087a7be42a60960c89f2b0 [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 */
Ingo Molnar16f762a2009-05-27 09:10:38 +02008#include "builtin.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +02009
10#include "perf.h"
11
Thomas Gleixner6eda5832009-05-01 18:29:57 +020012#include "util/util.h"
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020013#include "util/parse-options.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020014#include "util/parse-events.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -030015#include "util/string.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020016
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020017#include "util/header.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020018#include "util/event.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020019#include "util/debug.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020020#include "util/session.h"
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020021#include "util/symbol.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022
Peter Zijlstra97124d52009-06-02 15:52:24 +020023#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020024#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020025
Peter Zijlstrade9ac072009-04-08 15:01:31 +020026static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020027
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020028static long default_interval = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020029
Ingo Molnar42e59d72009-10-06 15:14:21 +020030static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020031static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020032static unsigned int mmap_pages = 128;
33static int freq = 1000;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020034static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020035static const char *output_name = "perf.data";
Ingo Molnar42e59d72009-10-06 15:14:21 +020036static int group = 0;
37static unsigned int realtime_prio = 0;
38static int raw_samples = 0;
39static int system_wide = 0;
40static int profile_cpu = -1;
41static pid_t target_pid = -1;
42static pid_t child_pid = -1;
43static int inherit = 1;
44static int force = 0;
45static int append_file = 0;
46static int call_graph = 0;
47static int inherit_stat = 0;
48static int no_samples = 0;
49static int sample_address = 0;
50static int multiplex = 0;
51static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020052
Ingo Molnar42e59d72009-10-06 15:14:21 +020053static long samples = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020054static struct timeval last_read;
55static struct timeval this_read;
56
Ingo Molnar42e59d72009-10-06 15:14:21 +020057static u64 bytes_written = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020058
59static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
60
Ingo Molnar42e59d72009-10-06 15:14:21 +020061static int nr_poll = 0;
62static int nr_cpu = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020063
Ingo Molnar42e59d72009-10-06 15:14:21 +020064static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020065
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020066static struct perf_session *session;
Peter Zijlstraf5970552009-06-18 23:22:55 +020067
Ingo Molnara21ca2c2009-06-06 09:58:57 +020068struct mmap_data {
69 int counter;
70 void *base;
71 unsigned int mask;
72 unsigned int prev;
73};
74
75static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
76
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020077static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020078{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020079 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020080 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020081
82 head = pc->data_head;
83 rmb();
84
85 return head;
86}
87
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020088static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
89{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020090 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020091
92 /*
93 * ensure all reads are done before we write the tail out.
94 */
95 /* mb(); */
96 pc->data_tail = tail;
97}
98
Peter Zijlstraf5970552009-06-18 23:22:55 +020099static void write_output(void *buf, size_t size)
100{
101 while (size) {
102 int ret = write(output, buf, size);
103
104 if (ret < 0)
105 die("failed to write");
106
107 size -= ret;
108 buf += ret;
109
110 bytes_written += ret;
111 }
112}
113
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200114static void write_event(event_t *buf, size_t size)
115{
116 /*
117 * Add it to the list of DSOs, so that when we finish this
118 * record session we can pick the available build-ids.
119 */
120 if (buf->header.type == PERF_RECORD_MMAP)
121 dsos__findnew(buf->mmap.filename);
122
123 write_output(buf, size);
124}
125
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200126static int process_synthesized_event(event_t *event,
127 struct perf_session *self __used)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200128{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200129 write_event(event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200130 return 0;
131}
132
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200133static void mmap_read(struct mmap_data *md)
134{
135 unsigned int head = mmap_read_head(md);
136 unsigned int old = md->prev;
137 unsigned char *data = md->base + page_size;
138 unsigned long size;
139 void *buf;
140 int diff;
141
142 gettimeofday(&this_read, NULL);
143
144 /*
145 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200146 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200147 *
148 * If we somehow ended up ahead of the head, we got messed up.
149 *
150 * In either case, truncate and restart at head.
151 */
152 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200153 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200154 struct timeval iv;
155 unsigned long msecs;
156
157 timersub(&this_read, &last_read, &iv);
158 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
159
160 fprintf(stderr, "WARNING: failed to keep up with mmap data."
161 " Last read %lu msecs ago.\n", msecs);
162
163 /*
164 * head points to a known good entry, start there.
165 */
166 old = head;
167 }
168
169 last_read = this_read;
170
171 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200172 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200173
174 size = head - old;
175
176 if ((old & md->mask) + size != (head & md->mask)) {
177 buf = &data[old & md->mask];
178 size = md->mask + 1 - (old & md->mask);
179 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200180
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200181 write_event(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200182 }
183
184 buf = &data[old & md->mask];
185 size = head - old;
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 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200191 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200192}
193
194static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200195static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200196
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200197static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200198{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200199 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200200 signr = sig;
201}
202
203static void sig_atexit(void)
204{
Chris Wilson933da832009-10-04 01:35:01 +0100205 if (child_pid != -1)
206 kill(child_pid, SIGTERM);
207
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200208 if (signr == -1)
209 return;
210
211 signal(signr, SIG_DFL);
212 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200213}
214
Ingo Molnarf250c0302009-06-05 13:18:41 +0200215static int group_fd;
216
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200217static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200218{
219 struct perf_header_attr *h_attr;
220
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200221 if (nr < session->header.attrs) {
222 h_attr = session->header.attr[nr];
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200223 } else {
224 h_attr = perf_header_attr__new(a);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200225 if (h_attr != NULL)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200226 if (perf_header__add_attr(&session->header, h_attr) < 0) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200227 perf_header_attr__delete(h_attr);
228 h_attr = NULL;
229 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200230 }
231
232 return h_attr;
233}
234
Ingo Molnarf250c0302009-06-05 13:18:41 +0200235static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200236{
Li Zefanc171b552009-10-15 11:22:07 +0800237 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200238 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200239 struct perf_header_attr *h_attr;
240 int track = !counter; /* only the first counter needs these */
Li Zefanc171b552009-10-15 11:22:07 +0800241 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200242 struct {
243 u64 count;
244 u64 time_enabled;
245 u64 time_running;
246 u64 id;
247 } read_data;
248
249 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
250 PERF_FORMAT_TOTAL_TIME_RUNNING |
251 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200252
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200253 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200254
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200255 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200256 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200257 attr->freq = 1;
258 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200259 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200260
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200261 if (no_samples)
262 attr->sample_freq = 0;
263
264 if (inherit_stat)
265 attr->inherit_stat = 1;
266
Anton Blanchard4bba8282009-07-16 15:44:29 +0200267 if (sample_address)
268 attr->sample_type |= PERF_SAMPLE_ADDR;
269
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200270 if (call_graph)
271 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
272
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200273 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200274 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200275 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200276 attr->sample_type |= PERF_SAMPLE_CPU;
277 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200278
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200279 attr->mmap = track;
280 attr->comm = track;
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100281 attr->inherit = inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200282 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200283
Ingo Molnar3da297a2009-06-07 17:39:02 +0200284try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200285 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200286
287 if (fd[nr_cpu][counter] < 0) {
288 int err = errno;
289
Pekka Enbergc10edee2009-11-08 18:01:06 +0200290 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200291 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200292 else if (err == ENODEV && profile_cpu != -1)
293 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200294
295 /*
296 * If it's cycles then fall back to hrtimer
297 * based cpu-clock-tick sw counter, which
298 * is always available even if no PMU support:
299 */
300 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200301 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200302
303 if (verbose)
304 warning(" ... trying to fall back to cpu-clock-ticks\n");
305 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200306 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200307 goto try_again;
308 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200309 printf("\n");
310 error("perfcounter syscall returned with %d (%s)\n",
311 fd[nr_cpu][counter], strerror(err));
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000312
313#if defined(__i386__) || defined(__x86_64__)
314 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
315 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");
316#endif
317
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200318 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200319 exit(-1);
320 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200321
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200322 h_attr = get_header_attr(attr, counter);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200323 if (h_attr == NULL)
324 die("nomem\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200325
326 if (!file_new) {
327 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
328 fprintf(stderr, "incompatible append\n");
329 exit(-1);
330 }
331 }
332
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200333 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
334 perror("Unable to read perf file descriptor\n");
335 exit(-1);
336 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200337
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -0200338 if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
339 pr_warning("Not enough memory to add id\n");
340 exit(-1);
341 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200342
Ingo Molnarf250c0302009-06-05 13:18:41 +0200343 assert(fd[nr_cpu][counter] >= 0);
344 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
345
346 /*
347 * First counter acts as the group leader:
348 */
349 if (group && group_fd == -1)
350 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200351 if (multiplex && multiplex_fd == -1)
352 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200353
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200354 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
Ingo Molnarf250c0302009-06-05 13:18:41 +0200355
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200356 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200357 assert(ret != -1);
358 } else {
359 event_array[nr_poll].fd = fd[nr_cpu][counter];
360 event_array[nr_poll].events = POLLIN;
361 nr_poll++;
362
363 mmap_array[nr_cpu][counter].counter = counter;
364 mmap_array[nr_cpu][counter].prev = 0;
365 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
366 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
367 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
368 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
369 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
370 exit(-1);
371 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200372 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200373
Li Zefanc171b552009-10-15 11:22:07 +0800374 if (filter != NULL) {
375 ret = ioctl(fd[nr_cpu][counter],
376 PERF_EVENT_IOC_SET_FILTER, filter);
377 if (ret) {
378 error("failed to set filter with %d (%s)\n", errno,
379 strerror(errno));
380 exit(-1);
381 }
382 }
383
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200384 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200385}
386
387static void open_counters(int cpu, pid_t pid)
388{
389 int counter;
390
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200391 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200392 for (counter = 0; counter < nr_counters; counter++)
393 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200394
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200395 nr_cpu++;
396}
397
Peter Zijlstraf5970552009-06-18 23:22:55 +0200398static void atexit_header(void)
399{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200400 session->header.data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200401
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200402 perf_header__write(&session->header, output, true);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200403}
404
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200405static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200406{
407 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200408 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200409 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200410 int flags;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200411 int err;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200412 unsigned long waking = 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100413 int child_ready_pipe[2], go_pipe[2];
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200414 const bool forks = target_pid == -1 && argc > 0;
Peter Zijlstra856e9662009-12-16 17:55:55 +0100415 char buf;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200416
417 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200418 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
419 assert(nr_cpus <= MAX_NR_CPUS);
420 assert(nr_cpus >= 0);
421
Peter Zijlstraf5970552009-06-18 23:22:55 +0200422 atexit(sig_atexit);
423 signal(SIGCHLD, sig_handler);
424 signal(SIGINT, sig_handler);
425
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200426 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100427 perror("failed to create pipes");
428 exit(-1);
429 }
430
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200431 if (!stat(output_name, &st) && st.st_size) {
Arnaldo Carvalho de Melob38d3462009-12-14 20:09:30 -0200432 if (!force) {
433 if (!append_file) {
434 pr_err("Error, output file %s exists, use -A "
435 "to append or -f to overwrite.\n",
436 output_name);
437 exit(-1);
438 }
439 } else {
440 char oldname[PATH_MAX];
441 snprintf(oldname, sizeof(oldname), "%s.old",
442 output_name);
443 unlink(oldname);
444 rename(output_name, oldname);
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200445 }
446 } else {
447 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200448 }
449
Ingo Molnarabaff322009-06-02 22:59:57 +0200450 flags = O_CREAT|O_RDWR;
451 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200452 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200453 else
454 flags |= O_TRUNC;
455
456 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200457 if (output < 0) {
458 perror("failed to create output file");
459 exit(-1);
460 }
461
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200462 session = perf_session__new(output_name, O_WRONLY, force);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200463 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200464 pr_err("Not enough memory for reading perf file header\n");
465 return -1;
466 }
467
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200468 if (!file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200469 err = perf_header__read(&session->header, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200470 if (err < 0)
471 return err;
472 }
473
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200474 if (raw_samples) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200475 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200476 } else {
477 for (i = 0; i < nr_counters; i++) {
478 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200479 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200480 break;
481 }
482 }
483 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200484
Peter Zijlstraf5970552009-06-18 23:22:55 +0200485 atexit(atexit_header);
486
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200487 if (forks) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100488 pid = fork();
489 if (pid < 0) {
490 perror("failed to fork");
491 exit(-1);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200492 }
Peter Zijlstra856e9662009-12-16 17:55:55 +0100493
494 if (!pid) {
495 close(child_ready_pipe[0]);
496 close(go_pipe[1]);
497 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
498
499 /*
500 * Do a dummy execvp to get the PLT entry resolved,
501 * so we avoid the resolver overhead on the real
502 * execvp call.
503 */
504 execvp("", (char **)argv);
505
506 /*
507 * Tell the parent we're ready to go
508 */
509 close(child_ready_pipe[1]);
510
511 /*
512 * Wait until the parent tells us to go.
513 */
514 if (read(go_pipe[0], &buf, 1) == -1)
515 perror("unable to read pipe");
516
517 execvp(argv[0], (char **)argv);
518
519 perror(argv[0]);
520 exit(-1);
521 }
522
523 child_pid = pid;
524
525 if (!system_wide)
526 target_pid = pid;
527
528 close(child_ready_pipe[1]);
529 close(go_pipe[0]);
530 /*
531 * wait for child to settle
532 */
533 if (read(child_ready_pipe[0], &buf, 1) == -1) {
534 perror("unable to read pipe");
535 exit(-1);
536 }
537 close(child_ready_pipe[0]);
538 }
539
540
Peter Zijlstra60ab2712009-12-16 17:55:56 +0100541 if ((!system_wide && !inherit) || profile_cpu != -1) {
Peter Zijlstra856e9662009-12-16 17:55:55 +0100542 open_counters(profile_cpu, target_pid);
543 } else {
544 for (i = 0; i < nr_cpus; i++)
545 open_counters(i, target_pid);
Jens Axboe0a5ac842009-08-12 11:18:01 +0200546 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200547
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200548 if (file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200549 err = perf_header__write(&session->header, output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200550 if (err < 0)
551 return err;
552 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200553
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200554 if (!system_wide && profile_cpu == -1)
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200555 event__synthesize_thread(pid, process_synthesized_event,
556 session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200557 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200558 event__synthesize_threads(process_synthesized_event, session);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200559
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200560 if (realtime_prio) {
561 struct sched_param param;
562
563 param.sched_priority = realtime_prio;
564 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200565 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200566 exit(-1);
567 }
568 }
569
Peter Zijlstra856e9662009-12-16 17:55:55 +0100570 /*
571 * Let the child rip
572 */
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200573 if (forks)
574 close(go_pipe[1]);
Peter Zijlstra856e9662009-12-16 17:55:55 +0100575
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200576 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200577 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200578
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200579 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200580 for (counter = 0; counter < nr_counters; counter++) {
581 if (mmap_array[i][counter].base)
582 mmap_read(&mmap_array[i][counter]);
583 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200584 }
585
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200586 if (hits == samples) {
587 if (done)
588 break;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200589 err = poll(event_array, nr_poll, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200590 waking++;
591 }
592
593 if (done) {
594 for (i = 0; i < nr_cpu; i++) {
595 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200596 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200597 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200598 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200599 }
600
Peter Zijlstra8b412662009-09-17 19:59:05 +0200601 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
602
Ingo Molnar021e9f42009-06-03 19:27:19 +0200603 /*
604 * Approximate RIP event size: 24 bytes.
605 */
606 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200607 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200608 (double)bytes_written / 1024.0 / 1024.0,
609 output_name,
610 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200611
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200612 return 0;
613}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200614
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200615static const char * const record_usage[] = {
Mike Galbraith9e096752009-05-28 16:25:34 +0200616 "perf record [<options>] [<command>]",
617 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200618 NULL
619};
620
Ingo Molnar52425192009-05-26 09:17:18 +0200621static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200622 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200623 "event selector. use 'perf list' to list available events",
624 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800625 OPT_CALLBACK(0, "filter", NULL, "filter",
626 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200627 OPT_INTEGER('p', "pid", &target_pid,
628 "record events on existing pid"),
629 OPT_INTEGER('r', "realtime", &realtime_prio,
630 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200631 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
632 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200633 OPT_BOOLEAN('a', "all-cpus", &system_wide,
634 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200635 OPT_BOOLEAN('A', "append", &append_file,
636 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200637 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
638 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200639 OPT_BOOLEAN('f', "force", &force,
640 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200641 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200642 "event period to sample"),
643 OPT_STRING('o', "output", &output_name, "file",
644 "output file name"),
645 OPT_BOOLEAN('i', "inherit", &inherit,
646 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200647 OPT_INTEGER('F', "freq", &freq,
648 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200649 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
650 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200651 OPT_BOOLEAN('g', "call-graph", &call_graph,
652 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200653 OPT_BOOLEAN('v', "verbose", &verbose,
654 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200655 OPT_BOOLEAN('s', "stat", &inherit_stat,
656 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200657 OPT_BOOLEAN('d', "data", &sample_address,
658 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200659 OPT_BOOLEAN('n', "no-samples", &no_samples,
660 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200661 OPT_BOOLEAN('M', "multiplex", &multiplex,
662 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200663 OPT_END()
664};
665
Ingo Molnarf37a2912009-07-01 12:37:06 +0200666int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200667{
668 int counter;
669
Anton Blancharda0541232009-07-22 23:04:12 +1000670 argc = parse_options(argc, argv, options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200671 PARSE_OPT_STOP_AT_NON_OPTION);
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -0200672 if (!argc && target_pid == -1 && !system_wide && profile_cpu == -1)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200673 usage_with_options(record_usage, options);
674
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200675 symbol__init();
676
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200677 if (!nr_counters) {
678 nr_counters = 1;
679 attrs[0].type = PERF_TYPE_HARDWARE;
680 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
681 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200682
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200683 /*
684 * User specified count overrides default frequency.
685 */
686 if (default_interval)
687 freq = 0;
688 else if (freq) {
689 default_interval = freq;
690 } else {
691 fprintf(stderr, "frequency and count are zero, aborting\n");
692 exit(EXIT_FAILURE);
693 }
694
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200695 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200696 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200697 continue;
698
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200699 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200700 }
701
702 return __cmd_record(argc, argv);
703}