blob: 4decbd14eaed080ac046b8a10c793b721280c1df [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 Weisbecker8f28827a2009-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 Melo234fbbf2009-10-26 19:23:18 -0200126static int process_synthesized_event(event_t *event)
127{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200128 write_event(event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200129 return 0;
130}
131
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200132static void mmap_read(struct mmap_data *md)
133{
134 unsigned int head = mmap_read_head(md);
135 unsigned int old = md->prev;
136 unsigned char *data = md->base + page_size;
137 unsigned long size;
138 void *buf;
139 int diff;
140
141 gettimeofday(&this_read, NULL);
142
143 /*
144 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200145 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200146 *
147 * If we somehow ended up ahead of the head, we got messed up.
148 *
149 * In either case, truncate and restart at head.
150 */
151 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200152 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200153 struct timeval iv;
154 unsigned long msecs;
155
156 timersub(&this_read, &last_read, &iv);
157 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
158
159 fprintf(stderr, "WARNING: failed to keep up with mmap data."
160 " Last read %lu msecs ago.\n", msecs);
161
162 /*
163 * head points to a known good entry, start there.
164 */
165 old = head;
166 }
167
168 last_read = this_read;
169
170 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200171 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200172
173 size = head - old;
174
175 if ((old & md->mask) + size != (head & md->mask)) {
176 buf = &data[old & md->mask];
177 size = md->mask + 1 - (old & md->mask);
178 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200179
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200180 write_event(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200181 }
182
183 buf = &data[old & md->mask];
184 size = head - old;
185 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200186
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200187 write_event(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200188
189 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200190 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200191}
192
193static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200194static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200195
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200196static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200197{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200198 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200199 signr = sig;
200}
201
202static void sig_atexit(void)
203{
Chris Wilson933da832009-10-04 01:35:01 +0100204 if (child_pid != -1)
205 kill(child_pid, SIGTERM);
206
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200207 if (signr == -1)
208 return;
209
210 signal(signr, SIG_DFL);
211 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200212}
213
Ingo Molnarf250c0302009-06-05 13:18:41 +0200214static int group_fd;
215
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200216static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200217{
218 struct perf_header_attr *h_attr;
219
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200220 if (nr < session->header.attrs) {
221 h_attr = session->header.attr[nr];
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200222 } else {
223 h_attr = perf_header_attr__new(a);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200224 if (h_attr != NULL)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200225 if (perf_header__add_attr(&session->header, h_attr) < 0) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200226 perf_header_attr__delete(h_attr);
227 h_attr = NULL;
228 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200229 }
230
231 return h_attr;
232}
233
Ingo Molnarf250c0302009-06-05 13:18:41 +0200234static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200235{
Li Zefanc171b552009-10-15 11:22:07 +0800236 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200237 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200238 struct perf_header_attr *h_attr;
239 int track = !counter; /* only the first counter needs these */
Li Zefanc171b552009-10-15 11:22:07 +0800240 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200241 struct {
242 u64 count;
243 u64 time_enabled;
244 u64 time_running;
245 u64 id;
246 } read_data;
247
248 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
249 PERF_FORMAT_TOTAL_TIME_RUNNING |
250 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200251
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200252 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200253
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200254 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200255 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200256 attr->freq = 1;
257 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200258 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200259
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200260 if (no_samples)
261 attr->sample_freq = 0;
262
263 if (inherit_stat)
264 attr->inherit_stat = 1;
265
Anton Blanchard4bba8282009-07-16 15:44:29 +0200266 if (sample_address)
267 attr->sample_type |= PERF_SAMPLE_ADDR;
268
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200269 if (call_graph)
270 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
271
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200272 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200273 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200274 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200275 attr->sample_type |= PERF_SAMPLE_CPU;
276 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200277
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200278 attr->mmap = track;
279 attr->comm = track;
280 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200281 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200282
Ingo Molnar3da297a2009-06-07 17:39:02 +0200283try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200284 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200285
286 if (fd[nr_cpu][counter] < 0) {
287 int err = errno;
288
Pekka Enbergc10edee2009-11-08 18:01:06 +0200289 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200290 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200291 else if (err == ENODEV && profile_cpu != -1)
292 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200293
294 /*
295 * If it's cycles then fall back to hrtimer
296 * based cpu-clock-tick sw counter, which
297 * is always available even if no PMU support:
298 */
299 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200300 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200301
302 if (verbose)
303 warning(" ... trying to fall back to cpu-clock-ticks\n");
304 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200305 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200306 goto try_again;
307 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200308 printf("\n");
309 error("perfcounter syscall returned with %d (%s)\n",
310 fd[nr_cpu][counter], strerror(err));
Simon Kaempfleinbfd45112009-11-16 15:25:53 +1000311
312#if defined(__i386__) || defined(__x86_64__)
313 if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
314 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");
315#endif
316
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200317 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200318 exit(-1);
319 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200320
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200321 h_attr = get_header_attr(attr, counter);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200322 if (h_attr == NULL)
323 die("nomem\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200324
325 if (!file_new) {
326 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
327 fprintf(stderr, "incompatible append\n");
328 exit(-1);
329 }
330 }
331
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200332 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
333 perror("Unable to read perf file descriptor\n");
334 exit(-1);
335 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200336
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -0200337 if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
338 pr_warning("Not enough memory to add id\n");
339 exit(-1);
340 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200341
Ingo Molnarf250c0302009-06-05 13:18:41 +0200342 assert(fd[nr_cpu][counter] >= 0);
343 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
344
345 /*
346 * First counter acts as the group leader:
347 */
348 if (group && group_fd == -1)
349 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200350 if (multiplex && multiplex_fd == -1)
351 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200352
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200353 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
Ingo Molnarf250c0302009-06-05 13:18:41 +0200354
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200355 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200356 assert(ret != -1);
357 } else {
358 event_array[nr_poll].fd = fd[nr_cpu][counter];
359 event_array[nr_poll].events = POLLIN;
360 nr_poll++;
361
362 mmap_array[nr_cpu][counter].counter = counter;
363 mmap_array[nr_cpu][counter].prev = 0;
364 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
365 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
366 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
367 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
368 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
369 exit(-1);
370 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200371 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200372
Li Zefanc171b552009-10-15 11:22:07 +0800373 if (filter != NULL) {
374 ret = ioctl(fd[nr_cpu][counter],
375 PERF_EVENT_IOC_SET_FILTER, filter);
376 if (ret) {
377 error("failed to set filter with %d (%s)\n", errno,
378 strerror(errno));
379 exit(-1);
380 }
381 }
382
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200383 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200384}
385
386static void open_counters(int cpu, pid_t pid)
387{
388 int counter;
389
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200390 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200391 for (counter = 0; counter < nr_counters; counter++)
392 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200393
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200394 nr_cpu++;
395}
396
Peter Zijlstraf5970552009-06-18 23:22:55 +0200397static void atexit_header(void)
398{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200399 session->header.data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200400
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200401 perf_header__write(&session->header, output, true);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200402}
403
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200404static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200405{
406 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200407 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200408 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200409 int flags;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200410 int err;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200411 unsigned long waking = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200412
413 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200414 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
415 assert(nr_cpus <= MAX_NR_CPUS);
416 assert(nr_cpus >= 0);
417
Peter Zijlstraf5970552009-06-18 23:22:55 +0200418 atexit(sig_atexit);
419 signal(SIGCHLD, sig_handler);
420 signal(SIGINT, sig_handler);
421
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200422 if (!stat(output_name, &st) && st.st_size) {
423 if (!force && !append_file) {
424 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
425 output_name);
426 exit(-1);
427 }
428 } else {
429 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200430 }
431
Ingo Molnarabaff322009-06-02 22:59:57 +0200432 flags = O_CREAT|O_RDWR;
433 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200434 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200435 else
436 flags |= O_TRUNC;
437
438 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200439 if (output < 0) {
440 perror("failed to create output file");
441 exit(-1);
442 }
443
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200444 session = perf_session__new(output_name, O_WRONLY, force);
445 if (session == NULL) {
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -0200446 pr_err("Not enough memory for reading perf file header\n");
447 return -1;
448 }
449
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200450 if (!file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200451 err = perf_header__read(&session->header, output);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200452 if (err < 0)
453 return err;
454 }
455
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200456 if (raw_samples) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200457 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200458 } else {
459 for (i = 0; i < nr_counters; i++) {
460 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200461 perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200462 break;
463 }
464 }
465 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200466
Peter Zijlstraf5970552009-06-18 23:22:55 +0200467 atexit(atexit_header);
468
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300469 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200470 pid = target_pid;
471 if (pid == -1)
472 pid = getpid();
473
Jens Axboe0a5ac842009-08-12 11:18:01 +0200474 open_counters(profile_cpu, pid);
475 } else {
476 if (profile_cpu != -1) {
477 open_counters(profile_cpu, target_pid);
478 } else {
479 for (i = 0; i < nr_cpus; i++)
480 open_counters(i, target_pid);
481 }
482 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200483
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200484 if (file_new) {
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200485 err = perf_header__write(&session->header, output, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200486 if (err < 0)
487 return err;
488 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200489
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200490 if (!system_wide)
491 event__synthesize_thread(pid, process_synthesized_event);
492 else
493 event__synthesize_threads(process_synthesized_event);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200494
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200495 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300496 pid = fork();
497 if (pid < 0)
Frederic Weisbeckerde896722009-11-11 04:51:02 +0100498 die("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200499
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300500 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200501 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300502 perror(argv[0]);
503 exit(-1);
504 }
Frederic Weisbeckerde896722009-11-11 04:51:02 +0100505 } else {
506 /*
507 * Wait a bit for the execv'ed child to appear
508 * and be updated in /proc
509 * FIXME: Do you know a less heuristical solution?
510 */
511 usleep(1000);
512 event__synthesize_thread(pid,
513 process_synthesized_event);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200514 }
Chris Wilson933da832009-10-04 01:35:01 +0100515
516 child_pid = pid;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200517 }
518
519 if (realtime_prio) {
520 struct sched_param param;
521
522 param.sched_priority = realtime_prio;
523 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200524 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200525 exit(-1);
526 }
527 }
528
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200529 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200530 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200531
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200532 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200533 for (counter = 0; counter < nr_counters; counter++) {
534 if (mmap_array[i][counter].base)
535 mmap_read(&mmap_array[i][counter]);
536 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200537 }
538
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200539 if (hits == samples) {
540 if (done)
541 break;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200542 err = poll(event_array, nr_poll, -1);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200543 waking++;
544 }
545
546 if (done) {
547 for (i = 0; i < nr_cpu; i++) {
548 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200549 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200550 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200551 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200552 }
553
Peter Zijlstra8b412662009-09-17 19:59:05 +0200554 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
555
Ingo Molnar021e9f42009-06-03 19:27:19 +0200556 /*
557 * Approximate RIP event size: 24 bytes.
558 */
559 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200560 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200561 (double)bytes_written / 1024.0 / 1024.0,
562 output_name,
563 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200564
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200565 return 0;
566}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200567
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200568static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200569 "perf record [<options>] [<command>]",
570 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200571 NULL
572};
573
Ingo Molnar52425192009-05-26 09:17:18 +0200574static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200575 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200576 "event selector. use 'perf list' to list available events",
577 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800578 OPT_CALLBACK(0, "filter", NULL, "filter",
579 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200580 OPT_INTEGER('p', "pid", &target_pid,
581 "record events on existing pid"),
582 OPT_INTEGER('r', "realtime", &realtime_prio,
583 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200584 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
585 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200586 OPT_BOOLEAN('a', "all-cpus", &system_wide,
587 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200588 OPT_BOOLEAN('A', "append", &append_file,
589 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200590 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
591 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200592 OPT_BOOLEAN('f', "force", &force,
593 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200594 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200595 "event period to sample"),
596 OPT_STRING('o', "output", &output_name, "file",
597 "output file name"),
598 OPT_BOOLEAN('i', "inherit", &inherit,
599 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200600 OPT_INTEGER('F', "freq", &freq,
601 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200602 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
603 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200604 OPT_BOOLEAN('g', "call-graph", &call_graph,
605 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200606 OPT_BOOLEAN('v', "verbose", &verbose,
607 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200608 OPT_BOOLEAN('s', "stat", &inherit_stat,
609 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200610 OPT_BOOLEAN('d', "data", &sample_address,
611 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200612 OPT_BOOLEAN('n', "no-samples", &no_samples,
613 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200614 OPT_BOOLEAN('M', "multiplex", &multiplex,
615 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200616 OPT_END()
617};
618
Ingo Molnarf37a2912009-07-01 12:37:06 +0200619int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200620{
621 int counter;
622
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200623 symbol__init(0);
624
Anton Blancharda0541232009-07-22 23:04:12 +1000625 argc = parse_options(argc, argv, options, record_usage,
626 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200627 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200628 usage_with_options(record_usage, options);
629
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200630 if (!nr_counters) {
631 nr_counters = 1;
632 attrs[0].type = PERF_TYPE_HARDWARE;
633 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
634 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200635
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200636 /*
637 * User specified count overrides default frequency.
638 */
639 if (default_interval)
640 freq = 0;
641 else if (freq) {
642 default_interval = freq;
643 } else {
644 fprintf(stderr, "frequency and count are zero, aborting\n");
645 exit(EXIT_FAILURE);
646 }
647
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200648 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200649 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200650 continue;
651
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200652 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200653 }
654
655 return __cmd_record(argc, argv);
656}