blob: 4c03bb7a4eba3d104b6ec609d9b65101b55676e3 [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 Melo8d063672009-11-04 18:50:43 -020020#include "util/symbol.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020021
Peter Zijlstra97124d52009-06-02 15:52:24 +020022#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020023#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020024
Peter Zijlstrade9ac072009-04-08 15:01:31 +020025static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020026
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020027static long default_interval = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020028
Ingo Molnar42e59d72009-10-06 15:14:21 +020029static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020030static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020031static unsigned int mmap_pages = 128;
32static int freq = 1000;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020033static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020034static const char *output_name = "perf.data";
Ingo Molnar42e59d72009-10-06 15:14:21 +020035static int group = 0;
36static unsigned int realtime_prio = 0;
37static int raw_samples = 0;
38static int system_wide = 0;
39static int profile_cpu = -1;
40static pid_t target_pid = -1;
41static pid_t child_pid = -1;
42static int inherit = 1;
43static int force = 0;
44static int append_file = 0;
45static int call_graph = 0;
46static int inherit_stat = 0;
47static int no_samples = 0;
48static int sample_address = 0;
49static int multiplex = 0;
50static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020051
Ingo Molnar42e59d72009-10-06 15:14:21 +020052static long samples = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020053static struct timeval last_read;
54static struct timeval this_read;
55
Ingo Molnar42e59d72009-10-06 15:14:21 +020056static u64 bytes_written = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020057
58static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
59
Ingo Molnar42e59d72009-10-06 15:14:21 +020060static int nr_poll = 0;
61static int nr_cpu = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020062
Ingo Molnar42e59d72009-10-06 15:14:21 +020063static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020064
Ingo Molnar42e59d72009-10-06 15:14:21 +020065struct perf_header *header = NULL;
Peter Zijlstraf5970552009-06-18 23:22:55 +020066
Ingo Molnara21ca2c2009-06-06 09:58:57 +020067struct mmap_data {
68 int counter;
69 void *base;
70 unsigned int mask;
71 unsigned int prev;
72};
73
74static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
75
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020076static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020077{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020078 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020079 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020080
81 head = pc->data_head;
82 rmb();
83
84 return head;
85}
86
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020087static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
88{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020089 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020090
91 /*
92 * ensure all reads are done before we write the tail out.
93 */
94 /* mb(); */
95 pc->data_tail = tail;
96}
97
Peter Zijlstraf5970552009-06-18 23:22:55 +020098static void write_output(void *buf, size_t size)
99{
100 while (size) {
101 int ret = write(output, buf, size);
102
103 if (ret < 0)
104 die("failed to write");
105
106 size -= ret;
107 buf += ret;
108
109 bytes_written += ret;
110 }
111}
112
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200113static void write_event(event_t *buf, size_t size)
114{
115 /*
116 * Add it to the list of DSOs, so that when we finish this
117 * record session we can pick the available build-ids.
118 */
119 if (buf->header.type == PERF_RECORD_MMAP)
120 dsos__findnew(buf->mmap.filename);
121
122 write_output(buf, size);
123}
124
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200125static int process_synthesized_event(event_t *event)
126{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200127 write_event(event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200128 return 0;
129}
130
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200131static void mmap_read(struct mmap_data *md)
132{
133 unsigned int head = mmap_read_head(md);
134 unsigned int old = md->prev;
135 unsigned char *data = md->base + page_size;
136 unsigned long size;
137 void *buf;
138 int diff;
139
140 gettimeofday(&this_read, NULL);
141
142 /*
143 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200144 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200145 *
146 * If we somehow ended up ahead of the head, we got messed up.
147 *
148 * In either case, truncate and restart at head.
149 */
150 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200151 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200152 struct timeval iv;
153 unsigned long msecs;
154
155 timersub(&this_read, &last_read, &iv);
156 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
157
158 fprintf(stderr, "WARNING: failed to keep up with mmap data."
159 " Last read %lu msecs ago.\n", msecs);
160
161 /*
162 * head points to a known good entry, start there.
163 */
164 old = head;
165 }
166
167 last_read = this_read;
168
169 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200170 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200171
172 size = head - old;
173
174 if ((old & md->mask) + size != (head & md->mask)) {
175 buf = &data[old & md->mask];
176 size = md->mask + 1 - (old & md->mask);
177 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200178
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200179 write_event(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200180 }
181
182 buf = &data[old & md->mask];
183 size = head - old;
184 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200185
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200186 write_event(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200187
188 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200189 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200190}
191
192static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200193static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200194
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200195static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200196{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200197 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200198 signr = sig;
199}
200
201static void sig_atexit(void)
202{
Chris Wilson933da832009-10-04 01:35:01 +0100203 if (child_pid != -1)
204 kill(child_pid, SIGTERM);
205
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200206 if (signr == -1)
207 return;
208
209 signal(signr, SIG_DFL);
210 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200211}
212
Ingo Molnarf250c0302009-06-05 13:18:41 +0200213static int group_fd;
214
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200215static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200216{
217 struct perf_header_attr *h_attr;
218
219 if (nr < header->attrs) {
220 h_attr = header->attr[nr];
221 } else {
222 h_attr = perf_header_attr__new(a);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200223 if (h_attr != NULL)
224 perf_header__add_attr(header, h_attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200225 }
226
227 return h_attr;
228}
229
Ingo Molnarf250c0302009-06-05 13:18:41 +0200230static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200231{
Li Zefanc171b552009-10-15 11:22:07 +0800232 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200233 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200234 struct perf_header_attr *h_attr;
235 int track = !counter; /* only the first counter needs these */
Li Zefanc171b552009-10-15 11:22:07 +0800236 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200237 struct {
238 u64 count;
239 u64 time_enabled;
240 u64 time_running;
241 u64 id;
242 } read_data;
243
244 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
245 PERF_FORMAT_TOTAL_TIME_RUNNING |
246 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200247
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200248 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200249
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200250 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200251 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200252 attr->freq = 1;
253 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200254 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200255
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200256 if (no_samples)
257 attr->sample_freq = 0;
258
259 if (inherit_stat)
260 attr->inherit_stat = 1;
261
Anton Blanchard4bba8282009-07-16 15:44:29 +0200262 if (sample_address)
263 attr->sample_type |= PERF_SAMPLE_ADDR;
264
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200265 if (call_graph)
266 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
267
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200268 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200269 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200270 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200271 attr->sample_type |= PERF_SAMPLE_CPU;
272 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200273
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200274 attr->mmap = track;
275 attr->comm = track;
276 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200277 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200278
Ingo Molnar3da297a2009-06-07 17:39:02 +0200279try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200280 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200281
282 if (fd[nr_cpu][counter] < 0) {
283 int err = errno;
284
Pekka Enbergc10edee2009-11-08 18:01:06 +0200285 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200286 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200287 else if (err == ENODEV && profile_cpu != -1)
288 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200289
290 /*
291 * If it's cycles then fall back to hrtimer
292 * based cpu-clock-tick sw counter, which
293 * is always available even if no PMU support:
294 */
295 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200296 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200297
298 if (verbose)
299 warning(" ... trying to fall back to cpu-clock-ticks\n");
300 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200301 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200302 goto try_again;
303 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200304 printf("\n");
305 error("perfcounter syscall returned with %d (%s)\n",
306 fd[nr_cpu][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200307 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200308 exit(-1);
309 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200310
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200311 h_attr = get_header_attr(attr, counter);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200312 if (h_attr == NULL)
313 die("nomem\n");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200314
315 if (!file_new) {
316 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
317 fprintf(stderr, "incompatible append\n");
318 exit(-1);
319 }
320 }
321
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200322 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
323 perror("Unable to read perf file descriptor\n");
324 exit(-1);
325 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200326
327 perf_header_attr__add_id(h_attr, read_data.id);
328
Ingo Molnarf250c0302009-06-05 13:18:41 +0200329 assert(fd[nr_cpu][counter] >= 0);
330 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
331
332 /*
333 * First counter acts as the group leader:
334 */
335 if (group && group_fd == -1)
336 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200337 if (multiplex && multiplex_fd == -1)
338 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200339
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200340 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
Ingo Molnarf250c0302009-06-05 13:18:41 +0200341
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200342 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200343 assert(ret != -1);
344 } else {
345 event_array[nr_poll].fd = fd[nr_cpu][counter];
346 event_array[nr_poll].events = POLLIN;
347 nr_poll++;
348
349 mmap_array[nr_cpu][counter].counter = counter;
350 mmap_array[nr_cpu][counter].prev = 0;
351 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
352 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
353 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
354 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
355 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
356 exit(-1);
357 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200358 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200359
Li Zefanc171b552009-10-15 11:22:07 +0800360 if (filter != NULL) {
361 ret = ioctl(fd[nr_cpu][counter],
362 PERF_EVENT_IOC_SET_FILTER, filter);
363 if (ret) {
364 error("failed to set filter with %d (%s)\n", errno,
365 strerror(errno));
366 exit(-1);
367 }
368 }
369
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200370 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200371}
372
373static void open_counters(int cpu, pid_t pid)
374{
375 int counter;
376
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200377 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200378 for (counter = 0; counter < nr_counters; counter++)
379 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200380
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200381 nr_cpu++;
382}
383
Peter Zijlstraf5970552009-06-18 23:22:55 +0200384static void atexit_header(void)
385{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200386 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200387
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100388 perf_header__write(header, output, true);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200389}
390
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200391static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200392{
393 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200394 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200395 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200396 int flags;
397 int ret;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200398 unsigned long waking = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200399
400 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200401 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
402 assert(nr_cpus <= MAX_NR_CPUS);
403 assert(nr_cpus >= 0);
404
Peter Zijlstraf5970552009-06-18 23:22:55 +0200405 atexit(sig_atexit);
406 signal(SIGCHLD, sig_handler);
407 signal(SIGINT, sig_handler);
408
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200409 if (!stat(output_name, &st) && st.st_size) {
410 if (!force && !append_file) {
411 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
412 output_name);
413 exit(-1);
414 }
415 } else {
416 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200417 }
418
Ingo Molnarabaff322009-06-02 22:59:57 +0200419 flags = O_CREAT|O_RDWR;
420 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200421 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200422 else
423 flags |= O_TRUNC;
424
425 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200426 if (output < 0) {
427 perror("failed to create output file");
428 exit(-1);
429 }
430
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200431 if (!file_new)
432 header = perf_header__read(output);
433 else
434 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200435
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200436 if (raw_samples) {
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100437 perf_header__set_feat(header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200438 } else {
439 for (i = 0; i < nr_counters; i++) {
440 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100441 perf_header__set_feat(header, HEADER_TRACE_INFO);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200442 break;
443 }
444 }
445 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200446
Peter Zijlstraf5970552009-06-18 23:22:55 +0200447 atexit(atexit_header);
448
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300449 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200450 pid = target_pid;
451 if (pid == -1)
452 pid = getpid();
453
Jens Axboe0a5ac842009-08-12 11:18:01 +0200454 open_counters(profile_cpu, pid);
455 } else {
456 if (profile_cpu != -1) {
457 open_counters(profile_cpu, target_pid);
458 } else {
459 for (i = 0; i < nr_cpus; i++)
460 open_counters(i, target_pid);
461 }
462 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200463
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200464 if (file_new)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100465 perf_header__write(header, output, false);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200466
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200467 if (!system_wide)
468 event__synthesize_thread(pid, process_synthesized_event);
469 else
470 event__synthesize_threads(process_synthesized_event);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200471
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200472 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300473 pid = fork();
474 if (pid < 0)
Frederic Weisbeckerde896722009-11-11 04:51:02 +0100475 die("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200476
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300477 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200478 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300479 perror(argv[0]);
480 exit(-1);
481 }
Frederic Weisbeckerde896722009-11-11 04:51:02 +0100482 } else {
483 /*
484 * Wait a bit for the execv'ed child to appear
485 * and be updated in /proc
486 * FIXME: Do you know a less heuristical solution?
487 */
488 usleep(1000);
489 event__synthesize_thread(pid,
490 process_synthesized_event);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200491 }
Chris Wilson933da832009-10-04 01:35:01 +0100492
493 child_pid = pid;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200494 }
495
496 if (realtime_prio) {
497 struct sched_param param;
498
499 param.sched_priority = realtime_prio;
500 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200501 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200502 exit(-1);
503 }
504 }
505
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200506 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200507 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200508
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200509 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200510 for (counter = 0; counter < nr_counters; counter++) {
511 if (mmap_array[i][counter].base)
512 mmap_read(&mmap_array[i][counter]);
513 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200514 }
515
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200516 if (hits == samples) {
517 if (done)
518 break;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200519 ret = poll(event_array, nr_poll, -1);
520 waking++;
521 }
522
523 if (done) {
524 for (i = 0; i < nr_cpu; i++) {
525 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200526 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200527 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200528 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200529 }
530
Peter Zijlstra8b412662009-09-17 19:59:05 +0200531 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
532
Ingo Molnar021e9f42009-06-03 19:27:19 +0200533 /*
534 * Approximate RIP event size: 24 bytes.
535 */
536 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200537 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200538 (double)bytes_written / 1024.0 / 1024.0,
539 output_name,
540 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200541
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200542 return 0;
543}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200544
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200545static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200546 "perf record [<options>] [<command>]",
547 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200548 NULL
549};
550
Ingo Molnar52425192009-05-26 09:17:18 +0200551static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200552 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200553 "event selector. use 'perf list' to list available events",
554 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800555 OPT_CALLBACK(0, "filter", NULL, "filter",
556 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200557 OPT_INTEGER('p', "pid", &target_pid,
558 "record events on existing pid"),
559 OPT_INTEGER('r', "realtime", &realtime_prio,
560 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200561 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
562 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200563 OPT_BOOLEAN('a', "all-cpus", &system_wide,
564 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200565 OPT_BOOLEAN('A', "append", &append_file,
566 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200567 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
568 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200569 OPT_BOOLEAN('f', "force", &force,
570 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200571 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200572 "event period to sample"),
573 OPT_STRING('o', "output", &output_name, "file",
574 "output file name"),
575 OPT_BOOLEAN('i', "inherit", &inherit,
576 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200577 OPT_INTEGER('F', "freq", &freq,
578 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200579 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
580 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200581 OPT_BOOLEAN('g', "call-graph", &call_graph,
582 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200583 OPT_BOOLEAN('v', "verbose", &verbose,
584 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200585 OPT_BOOLEAN('s', "stat", &inherit_stat,
586 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200587 OPT_BOOLEAN('d', "data", &sample_address,
588 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200589 OPT_BOOLEAN('n', "no-samples", &no_samples,
590 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200591 OPT_BOOLEAN('M', "multiplex", &multiplex,
592 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200593 OPT_END()
594};
595
Ingo Molnarf37a2912009-07-01 12:37:06 +0200596int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200597{
598 int counter;
599
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200600 symbol__init(0);
601
Anton Blancharda0541232009-07-22 23:04:12 +1000602 argc = parse_options(argc, argv, options, record_usage,
603 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200604 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200605 usage_with_options(record_usage, options);
606
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200607 if (!nr_counters) {
608 nr_counters = 1;
609 attrs[0].type = PERF_TYPE_HARDWARE;
610 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
611 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200612
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200613 /*
614 * User specified count overrides default frequency.
615 */
616 if (default_interval)
617 freq = 0;
618 else if (freq) {
619 default_interval = freq;
620 } else {
621 fprintf(stderr, "frequency and count are zero, aborting\n");
622 exit(EXIT_FAILURE);
623 }
624
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200625 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200626 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200627 continue;
628
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200629 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200630 }
631
632 return __cmd_record(argc, argv);
633}