blob: 4a73d89ce5d1a1a1b190b8049fe1d5bc60e9ebb8 [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"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020020
Peter Zijlstra97124d52009-06-02 15:52:24 +020021#include <unistd.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020022#include <sched.h>
Peter Zijlstrade9ac072009-04-08 15:01:31 +020023
Peter Zijlstrade9ac072009-04-08 15:01:31 +020024static int fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020025
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020026static long default_interval = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020027
Ingo Molnar42e59d72009-10-06 15:14:21 +020028static int nr_cpus = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020029static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020030static unsigned int mmap_pages = 128;
31static int freq = 1000;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020032static int output;
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020033static const char *output_name = "perf.data";
Ingo Molnar42e59d72009-10-06 15:14:21 +020034static int group = 0;
35static unsigned int realtime_prio = 0;
36static int raw_samples = 0;
37static int system_wide = 0;
38static int profile_cpu = -1;
39static pid_t target_pid = -1;
40static pid_t child_pid = -1;
41static int inherit = 1;
42static int force = 0;
43static int append_file = 0;
44static int call_graph = 0;
45static int inherit_stat = 0;
46static int no_samples = 0;
47static int sample_address = 0;
48static int multiplex = 0;
49static int multiplex_fd = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020050
Ingo Molnar42e59d72009-10-06 15:14:21 +020051static long samples = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020052static struct timeval last_read;
53static struct timeval this_read;
54
Ingo Molnar42e59d72009-10-06 15:14:21 +020055static u64 bytes_written = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020056
57static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
58
Ingo Molnar42e59d72009-10-06 15:14:21 +020059static int nr_poll = 0;
60static int nr_cpu = 0;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020061
Ingo Molnar42e59d72009-10-06 15:14:21 +020062static int file_new = 1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020063
Ingo Molnar42e59d72009-10-06 15:14:21 +020064struct perf_header *header = NULL;
Peter Zijlstraf5970552009-06-18 23:22:55 +020065
Ingo Molnara21ca2c2009-06-06 09:58:57 +020066struct mmap_data {
67 int counter;
68 void *base;
69 unsigned int mask;
70 unsigned int prev;
71};
72
73static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
74
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020075static unsigned long mmap_read_head(struct mmap_data *md)
Peter Zijlstrade9ac072009-04-08 15:01:31 +020076{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020077 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020078 long head;
Peter Zijlstrade9ac072009-04-08 15:01:31 +020079
80 head = pc->data_head;
81 rmb();
82
83 return head;
84}
85
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020086static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
87{
Ingo Molnarcdd6c482009-09-21 12:02:48 +020088 struct perf_event_mmap_page *pc = md->base;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +020089
90 /*
91 * ensure all reads are done before we write the tail out.
92 */
93 /* mb(); */
94 pc->data_tail = tail;
95}
96
Peter Zijlstraf5970552009-06-18 23:22:55 +020097static void write_output(void *buf, size_t size)
98{
99 while (size) {
100 int ret = write(output, buf, size);
101
102 if (ret < 0)
103 die("failed to write");
104
105 size -= ret;
106 buf += ret;
107
108 bytes_written += ret;
109 }
110}
111
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200112static int process_synthesized_event(event_t *event)
113{
114 write_output(event, event->header.size);
115 return 0;
116}
117
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200118static void mmap_read(struct mmap_data *md)
119{
120 unsigned int head = mmap_read_head(md);
121 unsigned int old = md->prev;
122 unsigned char *data = md->base + page_size;
123 unsigned long size;
124 void *buf;
125 int diff;
126
127 gettimeofday(&this_read, NULL);
128
129 /*
130 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200131 * the writer will bite our tail and mess up the samples under us.
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200132 *
133 * If we somehow ended up ahead of the head, we got messed up.
134 *
135 * In either case, truncate and restart at head.
136 */
137 diff = head - old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200138 if (diff < 0) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200139 struct timeval iv;
140 unsigned long msecs;
141
142 timersub(&this_read, &last_read, &iv);
143 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
144
145 fprintf(stderr, "WARNING: failed to keep up with mmap data."
146 " Last read %lu msecs ago.\n", msecs);
147
148 /*
149 * head points to a known good entry, start there.
150 */
151 old = head;
152 }
153
154 last_read = this_read;
155
156 if (old != head)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200157 samples++;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200158
159 size = head - old;
160
161 if ((old & md->mask) + size != (head & md->mask)) {
162 buf = &data[old & md->mask];
163 size = md->mask + 1 - (old & md->mask);
164 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200165
Peter Zijlstraf5970552009-06-18 23:22:55 +0200166 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200167 }
168
169 buf = &data[old & md->mask];
170 size = head - old;
171 old += size;
Ingo Molnar021e9f42009-06-03 19:27:19 +0200172
Peter Zijlstraf5970552009-06-18 23:22:55 +0200173 write_output(buf, size);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200174
175 md->prev = old;
Peter Zijlstra9d91a6f2009-06-18 11:40:28 +0200176 mmap_write_tail(md, old);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200177}
178
179static volatile int done = 0;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200180static volatile int signr = -1;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200181
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200182static void sig_handler(int sig)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200183{
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200184 done = 1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200185 signr = sig;
186}
187
188static void sig_atexit(void)
189{
Chris Wilson933da832009-10-04 01:35:01 +0100190 if (child_pid != -1)
191 kill(child_pid, SIGTERM);
192
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200193 if (signr == -1)
194 return;
195
196 signal(signr, SIG_DFL);
197 kill(getpid(), signr);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200198}
199
Ingo Molnarf250c0302009-06-05 13:18:41 +0200200static int group_fd;
201
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200202static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200203{
204 struct perf_header_attr *h_attr;
205
206 if (nr < header->attrs) {
207 h_attr = header->attr[nr];
208 } else {
209 h_attr = perf_header_attr__new(a);
210 perf_header__add_attr(header, h_attr);
211 }
212
213 return h_attr;
214}
215
Ingo Molnarf250c0302009-06-05 13:18:41 +0200216static void create_counter(int counter, int cpu, pid_t pid)
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200217{
Li Zefanc171b552009-10-15 11:22:07 +0800218 char *filter = filters[counter];
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200219 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200220 struct perf_header_attr *h_attr;
221 int track = !counter; /* only the first counter needs these */
Li Zefanc171b552009-10-15 11:22:07 +0800222 int ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200223 struct {
224 u64 count;
225 u64 time_enabled;
226 u64 time_running;
227 u64 id;
228 } read_data;
229
230 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
231 PERF_FORMAT_TOTAL_TIME_RUNNING |
232 PERF_FORMAT_ID;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200233
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200234 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200235
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200236 if (freq) {
Peter Zijlstraea1900e2009-06-10 21:45:22 +0200237 attr->sample_type |= PERF_SAMPLE_PERIOD;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200238 attr->freq = 1;
239 attr->sample_freq = freq;
Ingo Molnar1dba15e2009-06-05 18:37:22 +0200240 }
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200241
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200242 if (no_samples)
243 attr->sample_freq = 0;
244
245 if (inherit_stat)
246 attr->inherit_stat = 1;
247
Anton Blanchard4bba8282009-07-16 15:44:29 +0200248 if (sample_address)
249 attr->sample_type |= PERF_SAMPLE_ADDR;
250
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200251 if (call_graph)
252 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
253
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200254 if (raw_samples) {
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200255 attr->sample_type |= PERF_SAMPLE_TIME;
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200256 attr->sample_type |= PERF_SAMPLE_RAW;
Ingo Molnarcd6feee2009-09-02 20:20:38 +0200257 attr->sample_type |= PERF_SAMPLE_CPU;
258 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +0200259
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200260 attr->mmap = track;
261 attr->comm = track;
262 attr->inherit = (cpu < 0) && inherit;
Peter Zijlstra4502d772009-06-10 15:03:06 +0200263 attr->disabled = 1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200264
Ingo Molnar3da297a2009-06-07 17:39:02 +0200265try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200266 fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200267
268 if (fd[nr_cpu][counter] < 0) {
269 int err = errno;
270
Ingo Molnarf250c0302009-06-05 13:18:41 +0200271 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200272 die("Permission error - are you root?\n");
Jens Axboe0a5ac842009-08-12 11:18:01 +0200273 else if (err == ENODEV && profile_cpu != -1)
274 die("No such device - did you specify an out-of-range profile CPU?\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +0200275
276 /*
277 * If it's cycles then fall back to hrtimer
278 * based cpu-clock-tick sw counter, which
279 * is always available even if no PMU support:
280 */
281 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200282 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar3da297a2009-06-07 17:39:02 +0200283
284 if (verbose)
285 warning(" ... trying to fall back to cpu-clock-ticks\n");
286 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200287 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar3da297a2009-06-07 17:39:02 +0200288 goto try_again;
289 }
Ingo Molnar30c806a2009-06-07 17:46:24 +0200290 printf("\n");
291 error("perfcounter syscall returned with %d (%s)\n",
292 fd[nr_cpu][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200293 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnarf250c0302009-06-05 13:18:41 +0200294 exit(-1);
295 }
Ingo Molnar3da297a2009-06-07 17:39:02 +0200296
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200297 h_attr = get_header_attr(attr, counter);
298
299 if (!file_new) {
300 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
301 fprintf(stderr, "incompatible append\n");
302 exit(-1);
303 }
304 }
305
Frederic Weisbecker3928ddb2009-06-25 22:21:27 +0200306 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
307 perror("Unable to read perf file descriptor\n");
308 exit(-1);
309 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200310
311 perf_header_attr__add_id(h_attr, read_data.id);
312
Ingo Molnarf250c0302009-06-05 13:18:41 +0200313 assert(fd[nr_cpu][counter] >= 0);
314 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
315
316 /*
317 * First counter acts as the group leader:
318 */
319 if (group && group_fd == -1)
320 group_fd = fd[nr_cpu][counter];
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200321 if (multiplex && multiplex_fd == -1)
322 multiplex_fd = fd[nr_cpu][counter];
Ingo Molnarf250c0302009-06-05 13:18:41 +0200323
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200324 if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
Ingo Molnarf250c0302009-06-05 13:18:41 +0200325
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200326 ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200327 assert(ret != -1);
328 } else {
329 event_array[nr_poll].fd = fd[nr_cpu][counter];
330 event_array[nr_poll].events = POLLIN;
331 nr_poll++;
332
333 mmap_array[nr_cpu][counter].counter = counter;
334 mmap_array[nr_cpu][counter].prev = 0;
335 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
336 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
337 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
338 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
339 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
340 exit(-1);
341 }
Ingo Molnarf250c0302009-06-05 13:18:41 +0200342 }
Peter Zijlstra4502d772009-06-10 15:03:06 +0200343
Li Zefanc171b552009-10-15 11:22:07 +0800344 if (filter != NULL) {
345 ret = ioctl(fd[nr_cpu][counter],
346 PERF_EVENT_IOC_SET_FILTER, filter);
347 if (ret) {
348 error("failed to set filter with %d (%s)\n", errno,
349 strerror(errno));
350 exit(-1);
351 }
352 }
353
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200354 ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
Ingo Molnarf250c0302009-06-05 13:18:41 +0200355}
356
357static void open_counters(int cpu, pid_t pid)
358{
359 int counter;
360
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200361 group_fd = -1;
Ingo Molnarf250c0302009-06-05 13:18:41 +0200362 for (counter = 0; counter < nr_counters; counter++)
363 create_counter(counter, cpu, pid);
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200364
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200365 nr_cpu++;
366}
367
Peter Zijlstraf5970552009-06-18 23:22:55 +0200368static void atexit_header(void)
369{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200370 header->data_size += bytes_written;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200371
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200372 perf_header__write(header, output);
Peter Zijlstraf5970552009-06-18 23:22:55 +0200373}
374
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200375static int __cmd_record(int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200376{
377 int i, counter;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200378 struct stat st;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200379 pid_t pid = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200380 int flags;
381 int ret;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200382 unsigned long waking = 0;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200383
384 page_size = sysconf(_SC_PAGE_SIZE);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200385 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
386 assert(nr_cpus <= MAX_NR_CPUS);
387 assert(nr_cpus >= 0);
388
Peter Zijlstraf5970552009-06-18 23:22:55 +0200389 atexit(sig_atexit);
390 signal(SIGCHLD, sig_handler);
391 signal(SIGINT, sig_handler);
392
Pierre Habouzit266e0e22009-08-07 14:16:01 +0200393 if (!stat(output_name, &st) && st.st_size) {
394 if (!force && !append_file) {
395 fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
396 output_name);
397 exit(-1);
398 }
399 } else {
400 append_file = 0;
Peter Zijlstra97124d52009-06-02 15:52:24 +0200401 }
402
Ingo Molnarabaff322009-06-02 22:59:57 +0200403 flags = O_CREAT|O_RDWR;
404 if (append_file)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200405 file_new = 0;
Ingo Molnarabaff322009-06-02 22:59:57 +0200406 else
407 flags |= O_TRUNC;
408
409 output = open(output_name, flags, S_IRUSR|S_IWUSR);
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200410 if (output < 0) {
411 perror("failed to create output file");
412 exit(-1);
413 }
414
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200415 if (!file_new)
416 header = perf_header__read(output);
417 else
418 header = perf_header__new();
Peter Zijlstraf5970552009-06-18 23:22:55 +0200419
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200420 if (raw_samples) {
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200421 perf_header__feat_trace_info(header);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200422 } else {
423 for (i = 0; i < nr_counters; i++) {
424 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200425 perf_header__feat_trace_info(header);
Frederic Weisbecker9df37dd2009-08-17 23:07:50 +0200426 break;
427 }
428 }
429 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200430
Peter Zijlstraf5970552009-06-18 23:22:55 +0200431 atexit(atexit_header);
432
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300433 if (!system_wide) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200434 pid = target_pid;
435 if (pid == -1)
436 pid = getpid();
437
Jens Axboe0a5ac842009-08-12 11:18:01 +0200438 open_counters(profile_cpu, pid);
439 } else {
440 if (profile_cpu != -1) {
441 open_counters(profile_cpu, target_pid);
442 } else {
443 for (i = 0; i < nr_cpus; i++)
444 open_counters(i, target_pid);
445 }
446 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200447
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200448 if (file_new)
449 perf_header__write(header, output);
450
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200451 if (!system_wide)
452 event__synthesize_thread(pid, process_synthesized_event);
453 else
454 event__synthesize_threads(process_synthesized_event);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200455
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200456 if (target_pid == -1 && argc) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300457 pid = fork();
458 if (pid < 0)
459 perror("failed to fork");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200460
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300461 if (!pid) {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200462 if (execvp(argv[0], (char **)argv)) {
Arnaldo Carvalho de Melo1a853e32009-05-14 22:50:46 -0300463 perror(argv[0]);
464 exit(-1);
465 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200466 }
Chris Wilson933da832009-10-04 01:35:01 +0100467
468 child_pid = pid;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200469 }
470
471 if (realtime_prio) {
472 struct sched_param param;
473
474 param.sched_priority = realtime_prio;
475 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200476 pr_err("Could not set realtime priority.\n");
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200477 exit(-1);
478 }
479 }
480
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200481 for (;;) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200482 int hits = samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200483
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200484 for (i = 0; i < nr_cpu; i++) {
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200485 for (counter = 0; counter < nr_counters; counter++) {
486 if (mmap_array[i][counter].base)
487 mmap_read(&mmap_array[i][counter]);
488 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200489 }
490
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200491 if (hits == samples) {
492 if (done)
493 break;
Peter Zijlstra8b412662009-09-17 19:59:05 +0200494 ret = poll(event_array, nr_poll, -1);
495 waking++;
496 }
497
498 if (done) {
499 for (i = 0; i < nr_cpu; i++) {
500 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200501 ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
Peter Zijlstra8b412662009-09-17 19:59:05 +0200502 }
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200503 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200504 }
505
Peter Zijlstra8b412662009-09-17 19:59:05 +0200506 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
507
Ingo Molnar021e9f42009-06-03 19:27:19 +0200508 /*
509 * Approximate RIP event size: 24 bytes.
510 */
511 fprintf(stderr,
Ingo Molnar2debbc82009-06-05 14:29:10 +0200512 "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
Ingo Molnar021e9f42009-06-03 19:27:19 +0200513 (double)bytes_written / 1024.0 / 1024.0,
514 output_name,
515 bytes_written / 24);
Ingo Molnaraddc2782009-06-02 23:43:11 +0200516
Peter Zijlstrade9ac072009-04-08 15:01:31 +0200517 return 0;
518}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200519
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200520static const char * const record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +0200521 "perf record [<options>] [<command>]",
522 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200523 NULL
524};
525
Ingo Molnar52425192009-05-26 09:17:18 +0200526static const struct option options[] = {
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200527 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200528 "event selector. use 'perf list' to list available events",
529 parse_events),
Li Zefanc171b552009-10-15 11:22:07 +0800530 OPT_CALLBACK(0, "filter", NULL, "filter",
531 "event filter", parse_filter),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200532 OPT_INTEGER('p', "pid", &target_pid,
533 "record events on existing pid"),
534 OPT_INTEGER('r', "realtime", &realtime_prio,
535 "collect data with this RT SCHED_FIFO priority"),
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +0200536 OPT_BOOLEAN('R', "raw-samples", &raw_samples,
537 "collect raw sample records from all opened counters"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200538 OPT_BOOLEAN('a', "all-cpus", &system_wide,
539 "system-wide collection from all CPUs"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200540 OPT_BOOLEAN('A', "append", &append_file,
541 "append to the output file to do incremental profiling"),
Jens Axboe0a5ac842009-08-12 11:18:01 +0200542 OPT_INTEGER('C', "profile_cpu", &profile_cpu,
543 "CPU to profile on"),
Peter Zijlstra97124d52009-06-02 15:52:24 +0200544 OPT_BOOLEAN('f', "force", &force,
545 "overwrite existing data file"),
Peter Zijlstrae61078a2009-06-03 11:24:33 +0200546 OPT_LONG('c', "count", &default_interval,
Ingo Molnarabaff322009-06-02 22:59:57 +0200547 "event period to sample"),
548 OPT_STRING('o', "output", &output_name, "file",
549 "output file name"),
550 OPT_BOOLEAN('i', "inherit", &inherit,
551 "child tasks inherit counters"),
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200552 OPT_INTEGER('F', "freq", &freq,
553 "profile at this frequency"),
Ingo Molnarabaff322009-06-02 22:59:57 +0200554 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
555 "number of mmap data pages"),
Ingo Molnar3efa1cc92009-06-14 15:04:15 +0200556 OPT_BOOLEAN('g', "call-graph", &call_graph,
557 "do call-graph (stack chain/backtrace) recording"),
Ingo Molnar3da297a2009-06-07 17:39:02 +0200558 OPT_BOOLEAN('v', "verbose", &verbose,
559 "be more verbose (show counter open errors, etc)"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200560 OPT_BOOLEAN('s', "stat", &inherit_stat,
561 "per thread counts"),
Anton Blanchard4bba8282009-07-16 15:44:29 +0200562 OPT_BOOLEAN('d', "data", &sample_address,
563 "Sample addresses"),
Peter Zijlstra649c48a2009-06-24 21:12:48 +0200564 OPT_BOOLEAN('n', "no-samples", &no_samples,
565 "don't sample"),
Frederic Weisbeckerd1302522009-09-14 08:57:15 +0200566 OPT_BOOLEAN('M', "multiplex", &multiplex,
567 "multiplex counter output in a single channel"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200568 OPT_END()
569};
570
Ingo Molnarf37a2912009-07-01 12:37:06 +0200571int cmd_record(int argc, const char **argv, const char *prefix __used)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200572{
573 int counter;
574
Anton Blancharda0541232009-07-22 23:04:12 +1000575 argc = parse_options(argc, argv, options, record_usage,
576 PARSE_OPT_STOP_AT_NON_OPTION);
Mike Galbraithef65b2a2009-05-27 10:10:51 +0200577 if (!argc && target_pid == -1 && !system_wide)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200578 usage_with_options(record_usage, options);
579
Peter Zijlstrabbd36e52009-06-11 23:11:50 +0200580 if (!nr_counters) {
581 nr_counters = 1;
582 attrs[0].type = PERF_TYPE_HARDWARE;
583 attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
584 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200585
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +0200586 /*
587 * User specified count overrides default frequency.
588 */
589 if (default_interval)
590 freq = 0;
591 else if (freq) {
592 default_interval = freq;
593 } else {
594 fprintf(stderr, "frequency and count are zero, aborting\n");
595 exit(EXIT_FAILURE);
596 }
597
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200598 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200599 if (attrs[counter].sample_period)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200600 continue;
601
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200602 attrs[counter].sample_period = default_interval;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +0200603 }
604
605 return __cmd_record(argc, argv);
606}