Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 2 | * 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 Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 7 | */ |
Xiao Guangrong | b8f46c5 | 2010-02-03 11:53:14 +0800 | [diff] [blame] | 8 | #define _FILE_OFFSET_BITS 64 |
| 9 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 10 | #include "builtin.h" |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 11 | |
| 12 | #include "perf.h" |
| 13 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 14 | #include "util/build-id.h" |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 15 | #include "util/util.h" |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 16 | #include "util/parse-options.h" |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 17 | #include "util/parse-events.h" |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 18 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 19 | #include "util/header.h" |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 20 | #include "util/event.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 21 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 22 | #include "util/evsel.h" |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 23 | #include "util/debug.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 24 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 25 | #include "util/symbol.h" |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 26 | #include "util/cpumap.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 27 | #include "util/thread_map.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 28 | |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 29 | #include <unistd.h> |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 30 | #include <sched.h> |
Arnaldo Carvalho de Melo | a41794c | 2010-05-18 18:29:23 -0300 | [diff] [blame] | 31 | #include <sys/mman.h> |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 32 | |
Frederic Weisbecker | 7865e81 | 2010-04-14 19:42:07 +0200 | [diff] [blame] | 33 | enum write_mode_t { |
| 34 | WRITE_FORCE, |
| 35 | WRITE_APPEND |
| 36 | }; |
| 37 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 38 | static struct perf_record_opts record_opts = { |
| 39 | .target_pid = -1, |
| 40 | .target_tid = -1, |
| 41 | .user_freq = UINT_MAX, |
| 42 | .user_interval = ULLONG_MAX, |
| 43 | .freq = 1000, |
| 44 | .sample_id_all_avail = true, |
| 45 | }; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 46 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 47 | static unsigned int page_size; |
Frederic Weisbecker | 800cd25 | 2011-03-31 03:35:24 +0200 | [diff] [blame] | 48 | static unsigned int mmap_pages = UINT_MAX; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 49 | static int output; |
Tom Zanussi | 529870e | 2010-04-01 23:59:16 -0500 | [diff] [blame] | 50 | static int pipe_output = 0; |
Franck Bui-Huu | d7065ad | 2011-01-16 17:14:45 +0100 | [diff] [blame] | 51 | static const char *output_name = NULL; |
Lin Ming | 43bece7 | 2011-08-17 18:42:07 +0800 | [diff] [blame] | 52 | static bool group = false; |
Arnaldo Carvalho de Melo | 1967936 | 2010-05-17 15:39:16 -0300 | [diff] [blame] | 53 | static int realtime_prio = 0; |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 54 | static pid_t child_pid = -1; |
Frederic Weisbecker | 7865e81 | 2010-04-14 19:42:07 +0200 | [diff] [blame] | 55 | static enum write_mode_t write_mode = WRITE_FORCE; |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 56 | static bool no_buildid = false; |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 57 | static bool no_buildid_cache = false; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 58 | static struct perf_evlist *evsel_list; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 59 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 60 | static long samples = 0; |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 61 | static u64 bytes_written = 0; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 62 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 63 | static int file_new = 1; |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 64 | static off_t post_processing_offset; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 65 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 66 | static struct perf_session *session; |
Andi Kleen | 33e49ea | 2011-09-15 14:31:40 -0700 | [diff] [blame] | 67 | static const char *progname; |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 68 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 69 | static void advance_output(size_t size) |
| 70 | { |
| 71 | bytes_written += size; |
| 72 | } |
| 73 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 74 | static void write_output(void *buf, size_t size) |
| 75 | { |
| 76 | while (size) { |
| 77 | int ret = write(output, buf, size); |
| 78 | |
| 79 | if (ret < 0) |
| 80 | die("failed to write"); |
| 81 | |
| 82 | size -= ret; |
| 83 | buf += ret; |
| 84 | |
| 85 | bytes_written += ret; |
| 86 | } |
| 87 | } |
| 88 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 89 | static int process_synthesized_event(union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 90 | struct perf_sample *sample __used, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 91 | struct perf_session *self __used) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 92 | { |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 93 | write_output(event, event->header.size); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
Arnaldo Carvalho de Melo | 744bd8a | 2011-01-12 17:07:28 -0200 | [diff] [blame] | 97 | static void mmap_read(struct perf_mmap *md) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 98 | { |
Arnaldo Carvalho de Melo | 744bd8a | 2011-01-12 17:07:28 -0200 | [diff] [blame] | 99 | unsigned int head = perf_mmap__read_head(md); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 100 | unsigned int old = md->prev; |
| 101 | unsigned char *data = md->base + page_size; |
| 102 | unsigned long size; |
| 103 | void *buf; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 104 | |
Arnaldo Carvalho de Melo | dc82009 | 2011-01-28 14:49:19 -0200 | [diff] [blame] | 105 | if (old == head) |
| 106 | return; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 107 | |
Arnaldo Carvalho de Melo | dc82009 | 2011-01-28 14:49:19 -0200 | [diff] [blame] | 108 | samples++; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 109 | |
| 110 | size = head - old; |
| 111 | |
| 112 | if ((old & md->mask) + size != (head & md->mask)) { |
| 113 | buf = &data[old & md->mask]; |
| 114 | size = md->mask + 1 - (old & md->mask); |
| 115 | old += size; |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 116 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 117 | write_output(buf, size); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | buf = &data[old & md->mask]; |
| 121 | size = head - old; |
| 122 | old += size; |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 123 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 124 | write_output(buf, size); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 125 | |
| 126 | md->prev = old; |
Arnaldo Carvalho de Melo | 115d2d8 | 2011-01-12 17:11:53 -0200 | [diff] [blame] | 127 | perf_mmap__write_tail(md, old); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static volatile int done = 0; |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 131 | static volatile int signr = -1; |
Andi Kleen | 33e49ea | 2011-09-15 14:31:40 -0700 | [diff] [blame] | 132 | static volatile int child_finished = 0; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 133 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 134 | static void sig_handler(int sig) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 135 | { |
Andi Kleen | 33e49ea | 2011-09-15 14:31:40 -0700 | [diff] [blame] | 136 | if (sig == SIGCHLD) |
| 137 | child_finished = 1; |
| 138 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 139 | done = 1; |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 140 | signr = sig; |
| 141 | } |
| 142 | |
| 143 | static void sig_atexit(void) |
| 144 | { |
Andi Kleen | 33e49ea | 2011-09-15 14:31:40 -0700 | [diff] [blame] | 145 | int status; |
| 146 | |
| 147 | if (child_pid > 0) { |
| 148 | if (!child_finished) |
| 149 | kill(child_pid, SIGTERM); |
| 150 | |
| 151 | wait(&status); |
| 152 | if (WIFSIGNALED(status)) |
| 153 | psignal(WTERMSIG(status), progname); |
| 154 | } |
Chris Wilson | 933da83 | 2009-10-04 01:35:01 +0100 | [diff] [blame] | 155 | |
Arnaldo Carvalho de Melo | 18483b8 | 2010-12-06 15:13:38 -0200 | [diff] [blame] | 156 | if (signr == -1 || signr == SIGUSR1) |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 157 | return; |
| 158 | |
| 159 | signal(signr, SIG_DFL); |
| 160 | kill(getpid(), signr); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 163 | static bool perf_evlist__equal(struct perf_evlist *evlist, |
| 164 | struct perf_evlist *other) |
| 165 | { |
| 166 | struct perf_evsel *pos, *pair; |
| 167 | |
| 168 | if (evlist->nr_entries != other->nr_entries) |
| 169 | return false; |
| 170 | |
| 171 | pair = list_entry(other->entries.next, struct perf_evsel, node); |
| 172 | |
| 173 | list_for_each_entry(pos, &evlist->entries, node) { |
| 174 | if (memcmp(&pos->attr, &pair->attr, sizeof(pos->attr) != 0)) |
| 175 | return false; |
| 176 | pair = list_entry(pair->node.next, struct perf_evsel, node); |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
Arnaldo Carvalho de Melo | dd7927f | 2011-01-12 14:28:51 -0200 | [diff] [blame] | 182 | static void open_counters(struct perf_evlist *evlist) |
| 183 | { |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 184 | struct perf_evsel *pos, *first; |
Arnaldo Carvalho de Melo | dd7927f | 2011-01-12 14:28:51 -0200 | [diff] [blame] | 185 | |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 186 | first = list_entry(evlist->entries.next, struct perf_evsel, node); |
| 187 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 188 | perf_evlist__config_attrs(evlist, &record_opts); |
| 189 | |
Arnaldo Carvalho de Melo | dd7927f | 2011-01-12 14:28:51 -0200 | [diff] [blame] | 190 | list_for_each_entry(pos, &evlist->entries, node) { |
| 191 | struct perf_event_attr *attr = &pos->attr; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 192 | struct xyarray *group_fd = NULL; |
Arnaldo Carvalho de Melo | dd7927f | 2011-01-12 14:28:51 -0200 | [diff] [blame] | 193 | /* |
| 194 | * Check if parse_single_tracepoint_event has already asked for |
| 195 | * PERF_SAMPLE_TIME. |
| 196 | * |
| 197 | * XXX this is kludgy but short term fix for problems introduced by |
| 198 | * eac23d1c that broke 'perf script' by having different sample_types |
| 199 | * when using multiple tracepoint events when we use a perf binary |
| 200 | * that tries to use sample_id_all on an older kernel. |
| 201 | * |
| 202 | * We need to move counter creation to perf_session, support |
| 203 | * different sample_types, etc. |
| 204 | */ |
| 205 | bool time_needed = attr->sample_type & PERF_SAMPLE_TIME; |
| 206 | |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 207 | if (group && pos != first) |
| 208 | group_fd = first->fd; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 209 | retry_sample_id: |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 210 | attr->sample_id_all = record_opts.sample_id_all_avail ? 1 : 0; |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 211 | try_again: |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 212 | if (perf_evsel__open(pos, evlist->cpus, evlist->threads, group, |
| 213 | group_fd) < 0) { |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 214 | int err = errno; |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 215 | |
Arnaldo Carvalho de Melo | c286c41 | 2011-03-28 09:50:11 -0300 | [diff] [blame] | 216 | if (err == EPERM || err == EACCES) { |
Arnaldo Carvalho de Melo | b8631e6 | 2011-10-26 08:02:55 -0200 | [diff] [blame] | 217 | ui__error_paranoid(); |
Arnaldo Carvalho de Melo | c286c41 | 2011-03-28 09:50:11 -0300 | [diff] [blame] | 218 | exit(EXIT_FAILURE); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 219 | } else if (err == ENODEV && record_opts.cpu_list) { |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 220 | die("No such device - did you specify" |
| 221 | " an out-of-range profile CPU?\n"); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 222 | } else if (err == EINVAL && record_opts.sample_id_all_avail) { |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 223 | /* |
| 224 | * Old kernel, no attr->sample_id_type_all field |
| 225 | */ |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 226 | record_opts.sample_id_all_avail = false; |
| 227 | if (!record_opts.sample_time && !record_opts.raw_samples && !time_needed) |
Ian Munsie | eac23d1 | 2010-12-09 16:33:53 +1100 | [diff] [blame] | 228 | attr->sample_type &= ~PERF_SAMPLE_TIME; |
| 229 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 230 | goto retry_sample_id; |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 231 | } |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 232 | |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 233 | /* |
| 234 | * If it's cycles then fall back to hrtimer |
| 235 | * based cpu-clock-tick sw counter, which |
| 236 | * is always available even if no PMU support: |
| 237 | */ |
| 238 | if (attr->type == PERF_TYPE_HARDWARE |
| 239 | && attr->config == PERF_COUNT_HW_CPU_CYCLES) { |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 240 | |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 241 | if (verbose) |
David Ahern | ca6a425 | 2011-03-25 13:11:11 -0600 | [diff] [blame] | 242 | ui__warning("The cycles event is not supported, " |
| 243 | "trying to fall back to cpu-clock-ticks\n"); |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 244 | attr->type = PERF_TYPE_SOFTWARE; |
| 245 | attr->config = PERF_COUNT_SW_CPU_CLOCK; |
| 246 | goto try_again; |
| 247 | } |
David Ahern | ca6a425 | 2011-03-25 13:11:11 -0600 | [diff] [blame] | 248 | |
| 249 | if (err == ENOENT) { |
| 250 | ui__warning("The %s event is not supported.\n", |
| 251 | event_name(pos)); |
| 252 | exit(EXIT_FAILURE); |
| 253 | } |
| 254 | |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 255 | printf("\n"); |
Corey Ashford | d9cf837 | 2010-11-19 17:37:24 -0800 | [diff] [blame] | 256 | error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n", |
Arnaldo Carvalho de Melo | dd7927f | 2011-01-12 14:28:51 -0200 | [diff] [blame] | 257 | err, strerror(err)); |
Simon Kaempflein | bfd4511 | 2009-11-16 15:25:53 +1000 | [diff] [blame] | 258 | |
| 259 | #if defined(__i386__) || defined(__x86_64__) |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 260 | if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) |
| 261 | die("No hardware sampling interrupt available." |
| 262 | " No APIC? If so then you can boot the kernel" |
| 263 | " with the \"lapic\" boot parameter to" |
| 264 | " force-enable it.\n"); |
Simon Kaempflein | bfd4511 | 2009-11-16 15:25:53 +1000 | [diff] [blame] | 265 | #endif |
| 266 | |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 267 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 268 | } |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 269 | } |
Arnaldo Carvalho de Melo | a43d3f0 | 2010-12-25 12:12:25 -0200 | [diff] [blame] | 270 | |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 271 | if (perf_evlist__set_filters(evlist)) { |
| 272 | error("failed to set filter with %d (%s)\n", errno, |
| 273 | strerror(errno)); |
| 274 | exit(-1); |
| 275 | } |
| 276 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 277 | if (perf_evlist__mmap(evlist, mmap_pages, false) < 0) |
Arnaldo Carvalho de Melo | 0a27d7f | 2011-01-14 15:50:51 -0200 | [diff] [blame] | 278 | die("failed to mmap with %d (%s)\n", errno, strerror(errno)); |
| 279 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 280 | if (file_new) |
| 281 | session->evlist = evlist; |
| 282 | else { |
| 283 | if (!perf_evlist__equal(session->evlist, evlist)) { |
| 284 | fprintf(stderr, "incompatible append\n"); |
| 285 | exit(-1); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | perf_session__update_sample_type(session); |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 292 | static int process_buildids(void) |
| 293 | { |
| 294 | u64 size = lseek(output, 0, SEEK_CUR); |
| 295 | |
Arnaldo Carvalho de Melo | 9f591fd | 2010-03-11 15:53:11 -0300 | [diff] [blame] | 296 | if (size == 0) |
| 297 | return 0; |
| 298 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 299 | session->fd = output; |
| 300 | return __perf_session__process_events(session, post_processing_offset, |
| 301 | size - post_processing_offset, |
| 302 | size, &build_id__mark_dso_hit_ops); |
| 303 | } |
| 304 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 305 | static void atexit_header(void) |
| 306 | { |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 307 | if (!pipe_output) { |
| 308 | session->header.data_size += bytes_written; |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 309 | |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 310 | if (!no_buildid) |
| 311 | process_buildids(); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 312 | perf_session__write_header(session, evsel_list, output, true); |
Arnaldo Carvalho de Melo | 39d17da | 2010-07-29 14:08:55 -0300 | [diff] [blame] | 313 | perf_session__delete(session); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 314 | perf_evlist__delete(evsel_list); |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 315 | symbol__exit(); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 316 | } |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 319 | static void perf_event__synthesize_guest_os(struct machine *machine, void *data) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 320 | { |
| 321 | int err; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 322 | struct perf_session *psession = data; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 323 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 324 | if (machine__is_host(machine)) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 325 | return; |
| 326 | |
| 327 | /* |
| 328 | *As for guest kernel when processing subcommand record&report, |
| 329 | *we arrange module mmap prior to guest kernel mmap and trigger |
| 330 | *a preload dso because default guest module symbols are loaded |
| 331 | *from guest kallsyms instead of /lib/modules/XXX/XXX. This |
| 332 | *method is used to avoid symbol missing when the first addr is |
| 333 | *in module instead of in guest kernel. |
| 334 | */ |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 335 | err = perf_event__synthesize_modules(process_synthesized_event, |
| 336 | psession, machine); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 337 | if (err < 0) |
| 338 | pr_err("Couldn't record guest kernel [%d]'s reference" |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 339 | " relocation symbol.\n", machine->pid); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 340 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 341 | /* |
| 342 | * We use _stext for guest kernel because guest kernel's /proc/kallsyms |
| 343 | * have no _text sometimes. |
| 344 | */ |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 345 | err = perf_event__synthesize_kernel_mmap(process_synthesized_event, |
| 346 | psession, machine, "_text"); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 347 | if (err < 0) |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 348 | err = perf_event__synthesize_kernel_mmap(process_synthesized_event, |
| 349 | psession, machine, |
| 350 | "_stext"); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 351 | if (err < 0) |
| 352 | pr_err("Couldn't record guest kernel [%d]'s reference" |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 353 | " relocation symbol.\n", machine->pid); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 354 | } |
| 355 | |
Frederic Weisbecker | 9840280 | 2010-05-02 22:05:29 +0200 | [diff] [blame] | 356 | static struct perf_event_header finished_round_event = { |
| 357 | .size = sizeof(struct perf_event_header), |
| 358 | .type = PERF_RECORD_FINISHED_ROUND, |
| 359 | }; |
| 360 | |
| 361 | static void mmap_read_all(void) |
| 362 | { |
Peter Zijlstra | 0e2e63d | 2010-05-20 14:45:26 +0200 | [diff] [blame] | 363 | int i; |
Frederic Weisbecker | 9840280 | 2010-05-02 22:05:29 +0200 | [diff] [blame] | 364 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 365 | for (i = 0; i < evsel_list->nr_mmaps; i++) { |
Arnaldo Carvalho de Melo | 0a27d7f | 2011-01-14 15:50:51 -0200 | [diff] [blame] | 366 | if (evsel_list->mmap[i].base) |
| 367 | mmap_read(&evsel_list->mmap[i]); |
Frederic Weisbecker | 9840280 | 2010-05-02 22:05:29 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO)) |
| 371 | write_output(&finished_round_event, sizeof(finished_round_event)); |
| 372 | } |
| 373 | |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 374 | static int __cmd_record(int argc, const char **argv) |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 375 | { |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 376 | struct stat st; |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 377 | int flags; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 378 | int err; |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 379 | unsigned long waking = 0; |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 380 | int child_ready_pipe[2], go_pipe[2]; |
Zhang, Yanmin | 46be604 | 2010-03-18 11:36:04 -0300 | [diff] [blame] | 381 | const bool forks = argc > 0; |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 382 | char buf; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 383 | struct machine *machine; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 384 | |
Andi Kleen | 33e49ea | 2011-09-15 14:31:40 -0700 | [diff] [blame] | 385 | progname = argv[0]; |
| 386 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 387 | page_size = sysconf(_SC_PAGE_SIZE); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 388 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 389 | atexit(sig_atexit); |
| 390 | signal(SIGCHLD, sig_handler); |
| 391 | signal(SIGINT, sig_handler); |
Arnaldo Carvalho de Melo | 18483b8 | 2010-12-06 15:13:38 -0200 | [diff] [blame] | 392 | signal(SIGUSR1, sig_handler); |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 393 | |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 394 | if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 395 | perror("failed to create pipes"); |
| 396 | exit(-1); |
| 397 | } |
| 398 | |
Franck Bui-Huu | d7065ad | 2011-01-16 17:14:45 +0100 | [diff] [blame] | 399 | if (!output_name) { |
| 400 | if (!fstat(STDOUT_FILENO, &st) && S_ISFIFO(st.st_mode)) |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 401 | pipe_output = true; |
Franck Bui-Huu | d7065ad | 2011-01-16 17:14:45 +0100 | [diff] [blame] | 402 | else |
| 403 | output_name = "perf.data"; |
| 404 | } |
| 405 | if (output_name) { |
| 406 | if (!strcmp(output_name, "-")) |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 407 | pipe_output = true; |
Franck Bui-Huu | d7065ad | 2011-01-16 17:14:45 +0100 | [diff] [blame] | 408 | else if (!stat(output_name, &st) && st.st_size) { |
| 409 | if (write_mode == WRITE_FORCE) { |
| 410 | char oldname[PATH_MAX]; |
| 411 | snprintf(oldname, sizeof(oldname), "%s.old", |
| 412 | output_name); |
| 413 | unlink(oldname); |
| 414 | rename(output_name, oldname); |
| 415 | } |
| 416 | } else if (write_mode == WRITE_APPEND) { |
| 417 | write_mode = WRITE_FORCE; |
Pierre Habouzit | 266e0e2 | 2009-08-07 14:16:01 +0200 | [diff] [blame] | 418 | } |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 421 | flags = O_CREAT|O_RDWR; |
Frederic Weisbecker | 7865e81 | 2010-04-14 19:42:07 +0200 | [diff] [blame] | 422 | if (write_mode == WRITE_APPEND) |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 423 | file_new = 0; |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 424 | else |
| 425 | flags |= O_TRUNC; |
| 426 | |
Tom Zanussi | 529870e | 2010-04-01 23:59:16 -0500 | [diff] [blame] | 427 | if (pipe_output) |
| 428 | output = STDOUT_FILENO; |
| 429 | else |
| 430 | output = open(output_name, flags, S_IRUSR | S_IWUSR); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 431 | if (output < 0) { |
| 432 | perror("failed to create output file"); |
| 433 | exit(-1); |
| 434 | } |
| 435 | |
Frederic Weisbecker | 7865e81 | 2010-04-14 19:42:07 +0200 | [diff] [blame] | 436 | session = perf_session__new(output_name, O_WRONLY, |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 437 | write_mode == WRITE_FORCE, false, NULL); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 438 | if (session == NULL) { |
Arnaldo Carvalho de Melo | a9a70bb | 2009-11-17 01:18:11 -0200 | [diff] [blame] | 439 | pr_err("Not enough memory for reading perf file header\n"); |
| 440 | return -1; |
| 441 | } |
| 442 | |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 443 | if (!no_buildid) |
| 444 | perf_header__set_feat(&session->header, HEADER_BUILD_ID); |
| 445 | |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 446 | if (!file_new) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 447 | err = perf_session__read_header(session, output); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 448 | if (err < 0) |
Arnaldo Carvalho de Melo | 39d17da | 2010-07-29 14:08:55 -0300 | [diff] [blame] | 449 | goto out_delete_session; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 450 | } |
| 451 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 452 | if (have_tracepoints(&evsel_list->entries)) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 453 | perf_header__set_feat(&session->header, HEADER_TRACE_INFO); |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 454 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 455 | perf_header__set_feat(&session->header, HEADER_HOSTNAME); |
| 456 | perf_header__set_feat(&session->header, HEADER_OSRELEASE); |
| 457 | perf_header__set_feat(&session->header, HEADER_ARCH); |
| 458 | perf_header__set_feat(&session->header, HEADER_CPUDESC); |
| 459 | perf_header__set_feat(&session->header, HEADER_NRCPUS); |
| 460 | perf_header__set_feat(&session->header, HEADER_EVENT_DESC); |
| 461 | perf_header__set_feat(&session->header, HEADER_CMDLINE); |
| 462 | perf_header__set_feat(&session->header, HEADER_VERSION); |
| 463 | perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY); |
| 464 | perf_header__set_feat(&session->header, HEADER_TOTAL_MEM); |
| 465 | perf_header__set_feat(&session->header, HEADER_NUMA_TOPOLOGY); |
| 466 | perf_header__set_feat(&session->header, HEADER_CPUID); |
| 467 | |
Frederic Weisbecker | 800cd25 | 2011-03-31 03:35:24 +0200 | [diff] [blame] | 468 | /* 512 kiB: default amount of unprivileged mlocked memory */ |
| 469 | if (mmap_pages == UINT_MAX) |
| 470 | mmap_pages = (512 * 1024) / page_size; |
| 471 | |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 472 | if (forks) { |
Zhang, Yanmin | 46be604 | 2010-03-18 11:36:04 -0300 | [diff] [blame] | 473 | child_pid = fork(); |
Borislav Petkov | 2fb750e | 2010-05-31 23:18:18 +0200 | [diff] [blame] | 474 | if (child_pid < 0) { |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 475 | perror("failed to fork"); |
| 476 | exit(-1); |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 477 | } |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 478 | |
Zhang, Yanmin | 46be604 | 2010-03-18 11:36:04 -0300 | [diff] [blame] | 479 | if (!child_pid) { |
Tom Zanussi | 529870e | 2010-04-01 23:59:16 -0500 | [diff] [blame] | 480 | if (pipe_output) |
| 481 | dup2(2, 1); |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 482 | close(child_ready_pipe[0]); |
| 483 | close(go_pipe[1]); |
| 484 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); |
| 485 | |
| 486 | /* |
| 487 | * Do a dummy execvp to get the PLT entry resolved, |
| 488 | * so we avoid the resolver overhead on the real |
| 489 | * execvp call. |
| 490 | */ |
| 491 | execvp("", (char **)argv); |
| 492 | |
| 493 | /* |
| 494 | * Tell the parent we're ready to go |
| 495 | */ |
| 496 | close(child_ready_pipe[1]); |
| 497 | |
| 498 | /* |
| 499 | * Wait until the parent tells us to go. |
| 500 | */ |
| 501 | if (read(go_pipe[0], &buf, 1) == -1) |
| 502 | perror("unable to read pipe"); |
| 503 | |
| 504 | execvp(argv[0], (char **)argv); |
| 505 | |
| 506 | perror(argv[0]); |
Arnaldo Carvalho de Melo | 18483b8 | 2010-12-06 15:13:38 -0200 | [diff] [blame] | 507 | kill(getppid(), SIGUSR1); |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 508 | exit(-1); |
| 509 | } |
| 510 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 511 | if (!record_opts.system_wide && record_opts.target_tid == -1 && record_opts.target_pid == -1) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 512 | evsel_list->threads->map[0] = child_pid; |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 513 | |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 514 | close(child_ready_pipe[1]); |
| 515 | close(go_pipe[0]); |
| 516 | /* |
| 517 | * wait for child to settle |
| 518 | */ |
| 519 | if (read(child_ready_pipe[0], &buf, 1) == -1) { |
| 520 | perror("unable to read pipe"); |
| 521 | exit(-1); |
| 522 | } |
| 523 | close(child_ready_pipe[0]); |
| 524 | } |
| 525 | |
Arnaldo Carvalho de Melo | dd7927f | 2011-01-12 14:28:51 -0200 | [diff] [blame] | 526 | open_counters(evsel_list); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 527 | |
Arnaldo Carvalho de Melo | 712a4b6 | 2011-02-17 12:18:42 -0200 | [diff] [blame] | 528 | /* |
| 529 | * perf_session__delete(session) will be called at atexit_header() |
| 530 | */ |
| 531 | atexit(atexit_header); |
| 532 | |
Tom Zanussi | 529870e | 2010-04-01 23:59:16 -0500 | [diff] [blame] | 533 | if (pipe_output) { |
| 534 | err = perf_header__write_pipe(output); |
| 535 | if (err < 0) |
| 536 | return err; |
| 537 | } else if (file_new) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 538 | err = perf_session__write_header(session, evsel_list, |
| 539 | output, false); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 540 | if (err < 0) |
| 541 | return err; |
| 542 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 543 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 544 | post_processing_offset = lseek(output, 0, SEEK_CUR); |
| 545 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 546 | if (pipe_output) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 547 | err = perf_session__synthesize_attrs(session, |
| 548 | process_synthesized_event); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 549 | if (err < 0) { |
| 550 | pr_err("Couldn't synthesize attrs.\n"); |
| 551 | return err; |
| 552 | } |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 553 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 554 | err = perf_event__synthesize_event_types(process_synthesized_event, |
| 555 | session); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 556 | if (err < 0) { |
| 557 | pr_err("Couldn't synthesize event_types.\n"); |
| 558 | return err; |
| 559 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 560 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 561 | if (have_tracepoints(&evsel_list->entries)) { |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 562 | /* |
| 563 | * FIXME err <= 0 here actually means that |
| 564 | * there were no tracepoints so its not really |
| 565 | * an error, just that we don't need to |
| 566 | * synthesize anything. We really have to |
| 567 | * return this more properly and also |
| 568 | * propagate errors that now are calling die() |
| 569 | */ |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 570 | err = perf_event__synthesize_tracing_data(output, evsel_list, |
| 571 | process_synthesized_event, |
| 572 | session); |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 573 | if (err <= 0) { |
| 574 | pr_err("Couldn't record tracing data.\n"); |
| 575 | return err; |
| 576 | } |
Arnaldo Carvalho de Melo | 2c9faa0 | 2010-05-02 13:37:24 -0300 | [diff] [blame] | 577 | advance_output(err); |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 578 | } |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 579 | } |
| 580 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 581 | machine = perf_session__find_host_machine(session); |
| 582 | if (!machine) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 583 | pr_err("Couldn't find native kernel information.\n"); |
| 584 | return -1; |
| 585 | } |
| 586 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 587 | err = perf_event__synthesize_kernel_mmap(process_synthesized_event, |
| 588 | session, machine, "_text"); |
Arnaldo Carvalho de Melo | 7016213 | 2010-03-30 18:27:39 -0300 | [diff] [blame] | 589 | if (err < 0) |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 590 | err = perf_event__synthesize_kernel_mmap(process_synthesized_event, |
| 591 | session, machine, "_stext"); |
Arnaldo Carvalho de Melo | c1a3a4b | 2010-11-22 14:01:55 -0200 | [diff] [blame] | 592 | if (err < 0) |
| 593 | pr_err("Couldn't record kernel reference relocation symbol\n" |
| 594 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" |
| 595 | "Check /proc/kallsyms permission or run as root.\n"); |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 596 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 597 | err = perf_event__synthesize_modules(process_synthesized_event, |
| 598 | session, machine); |
Arnaldo Carvalho de Melo | c1a3a4b | 2010-11-22 14:01:55 -0200 | [diff] [blame] | 599 | if (err < 0) |
| 600 | pr_err("Couldn't record kernel module information.\n" |
| 601 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" |
| 602 | "Check /proc/modules permission or run as root.\n"); |
| 603 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 604 | if (perf_guest) |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 605 | perf_session__process_machines(session, |
| 606 | perf_event__synthesize_guest_os); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 607 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 608 | if (!record_opts.system_wide) |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 609 | perf_event__synthesize_thread_map(evsel_list->threads, |
| 610 | process_synthesized_event, |
| 611 | session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 612 | else |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 613 | perf_event__synthesize_threads(process_synthesized_event, |
| 614 | session); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 615 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 616 | if (realtime_prio) { |
| 617 | struct sched_param param; |
| 618 | |
| 619 | param.sched_priority = realtime_prio; |
| 620 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 621 | pr_err("Could not set realtime priority.\n"); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 622 | exit(-1); |
| 623 | } |
| 624 | } |
| 625 | |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 626 | perf_evlist__enable(evsel_list); |
| 627 | |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 628 | /* |
| 629 | * Let the child rip |
| 630 | */ |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 631 | if (forks) |
| 632 | close(go_pipe[1]); |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 633 | |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 634 | for (;;) { |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 635 | int hits = samples; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 636 | |
Frederic Weisbecker | 9840280 | 2010-05-02 22:05:29 +0200 | [diff] [blame] | 637 | mmap_read_all(); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 638 | |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 639 | if (hits == samples) { |
| 640 | if (done) |
| 641 | break; |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 642 | err = poll(evsel_list->pollfd, evsel_list->nr_fds, -1); |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 643 | waking++; |
| 644 | } |
| 645 | |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 646 | if (done) |
| 647 | perf_evlist__disable(evsel_list); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Arnaldo Carvalho de Melo | 18483b8 | 2010-12-06 15:13:38 -0200 | [diff] [blame] | 650 | if (quiet || signr == SIGUSR1) |
Arnaldo Carvalho de Melo | b44308f | 2010-10-26 15:20:09 -0200 | [diff] [blame] | 651 | return 0; |
| 652 | |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 653 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
| 654 | |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 655 | /* |
| 656 | * Approximate RIP event size: 24 bytes. |
| 657 | */ |
| 658 | fprintf(stderr, |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 659 | "[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n", |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 660 | (double)bytes_written / 1024.0 / 1024.0, |
| 661 | output_name, |
| 662 | bytes_written / 24); |
Ingo Molnar | addc278 | 2009-06-02 23:43:11 +0200 | [diff] [blame] | 663 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 664 | return 0; |
Arnaldo Carvalho de Melo | 39d17da | 2010-07-29 14:08:55 -0300 | [diff] [blame] | 665 | |
| 666 | out_delete_session: |
| 667 | perf_session__delete(session); |
| 668 | return err; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 669 | } |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 670 | |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 671 | static const char * const record_usage[] = { |
Mike Galbraith | 9e096753 | 2009-05-28 16:25:34 +0200 | [diff] [blame] | 672 | "perf record [<options>] [<command>]", |
| 673 | "perf record [<options>] -- <command> [<options>]", |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 674 | NULL |
| 675 | }; |
| 676 | |
Frederic Weisbecker | 7865e81 | 2010-04-14 19:42:07 +0200 | [diff] [blame] | 677 | static bool force, append_file; |
| 678 | |
Tom Zanussi | bca647a | 2010-11-10 08:11:30 -0600 | [diff] [blame] | 679 | const struct option record_options[] = { |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 680 | OPT_CALLBACK('e', "event", &evsel_list, "event", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 681 | "event selector. use 'perf list' to list available events", |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 682 | parse_events_option), |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 683 | OPT_CALLBACK(0, "filter", &evsel_list, "filter", |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 684 | "event filter", parse_filter), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 685 | OPT_INTEGER('p', "pid", &record_opts.target_pid, |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 686 | "record events on existing process id"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 687 | OPT_INTEGER('t', "tid", &record_opts.target_tid, |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 688 | "record events on existing thread id"), |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 689 | OPT_INTEGER('r', "realtime", &realtime_prio, |
| 690 | "collect data with this RT SCHED_FIFO priority"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 691 | OPT_BOOLEAN('D', "no-delay", &record_opts.no_delay, |
Kirill Smelkov | acac03f | 2011-01-12 17:59:36 +0300 | [diff] [blame] | 692 | "collect data without buffering"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 693 | OPT_BOOLEAN('R', "raw-samples", &record_opts.raw_samples, |
Frederic Weisbecker | daac07b | 2009-08-13 10:27:19 +0200 | [diff] [blame] | 694 | "collect raw sample records from all opened counters"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 695 | OPT_BOOLEAN('a', "all-cpus", &record_opts.system_wide, |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 696 | "system-wide collection from all CPUs"), |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 697 | OPT_BOOLEAN('A', "append", &append_file, |
| 698 | "append to the output file to do incremental profiling"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 699 | OPT_STRING('C', "cpu", &record_opts.cpu_list, "cpu", |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 700 | "list of cpus to monitor"), |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 701 | OPT_BOOLEAN('f', "force", &force, |
Frederic Weisbecker | 7865e81 | 2010-04-14 19:42:07 +0200 | [diff] [blame] | 702 | "overwrite existing data file (deprecated)"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 703 | OPT_U64('c', "count", &record_opts.user_interval, "event period to sample"), |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 704 | OPT_STRING('o', "output", &output_name, "file", |
| 705 | "output file name"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 706 | OPT_BOOLEAN('i', "no-inherit", &record_opts.no_inherit, |
Stephane Eranian | 2e6cdf9 | 2010-05-12 10:40:01 +0200 | [diff] [blame] | 707 | "child tasks do not inherit counters"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 708 | OPT_UINTEGER('F', "freq", &record_opts.user_freq, "profile at this frequency"), |
Arnaldo Carvalho de Melo | 1967936 | 2010-05-17 15:39:16 -0300 | [diff] [blame] | 709 | OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"), |
Lin Ming | 43bece7 | 2011-08-17 18:42:07 +0800 | [diff] [blame] | 710 | OPT_BOOLEAN(0, "group", &group, |
| 711 | "put the counters into a counter group"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 712 | OPT_BOOLEAN('g', "call-graph", &record_opts.call_graph, |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 713 | "do call-graph (stack chain/backtrace) recording"), |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 714 | OPT_INCR('v', "verbose", &verbose, |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 715 | "be more verbose (show counter open errors, etc)"), |
Arnaldo Carvalho de Melo | b44308f | 2010-10-26 15:20:09 -0200 | [diff] [blame] | 716 | OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 717 | OPT_BOOLEAN('s', "stat", &record_opts.inherit_stat, |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 718 | "per thread counts"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 719 | OPT_BOOLEAN('d', "data", &record_opts.sample_address, |
Anton Blanchard | 4bba828 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 720 | "Sample addresses"), |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 721 | OPT_BOOLEAN('T', "timestamp", &record_opts.sample_time, "Sample timestamps"), |
| 722 | OPT_BOOLEAN('n', "no-samples", &record_opts.no_samples, |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 723 | "don't sample"), |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 724 | OPT_BOOLEAN('N', "no-buildid-cache", &no_buildid_cache, |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 725 | "do not update the buildid cache"), |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 726 | OPT_BOOLEAN('B', "no-buildid", &no_buildid, |
| 727 | "do not collect buildids in perf.data"), |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 728 | OPT_CALLBACK('G', "cgroup", &evsel_list, "name", |
| 729 | "monitor event in cgroup name only", |
| 730 | parse_cgroups), |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 731 | OPT_END() |
| 732 | }; |
| 733 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 734 | int cmd_record(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 735 | { |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 736 | int err = -ENOMEM; |
| 737 | struct perf_evsel *pos; |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 738 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 739 | perf_header__set_cmdline(argc, argv); |
| 740 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 741 | evsel_list = perf_evlist__new(NULL, NULL); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 742 | if (evsel_list == NULL) |
| 743 | return -ENOMEM; |
| 744 | |
Tom Zanussi | bca647a | 2010-11-10 08:11:30 -0600 | [diff] [blame] | 745 | argc = parse_options(argc, argv, record_options, record_usage, |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 746 | PARSE_OPT_STOP_AT_NON_OPTION); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 747 | if (!argc && record_opts.target_pid == -1 && record_opts.target_tid == -1 && |
| 748 | !record_opts.system_wide && !record_opts.cpu_list) |
Tom Zanussi | bca647a | 2010-11-10 08:11:30 -0600 | [diff] [blame] | 749 | usage_with_options(record_usage, record_options); |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 750 | |
Frederic Weisbecker | 7865e81 | 2010-04-14 19:42:07 +0200 | [diff] [blame] | 751 | if (force && append_file) { |
| 752 | fprintf(stderr, "Can't overwrite and append at the same time." |
| 753 | " You need to choose between -f and -A"); |
Tom Zanussi | bca647a | 2010-11-10 08:11:30 -0600 | [diff] [blame] | 754 | usage_with_options(record_usage, record_options); |
Frederic Weisbecker | 7865e81 | 2010-04-14 19:42:07 +0200 | [diff] [blame] | 755 | } else if (append_file) { |
| 756 | write_mode = WRITE_APPEND; |
| 757 | } else { |
| 758 | write_mode = WRITE_FORCE; |
| 759 | } |
| 760 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 761 | if (nr_cgroups && !record_opts.system_wide) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 762 | fprintf(stderr, "cgroup monitoring only available in" |
| 763 | " system-wide mode\n"); |
| 764 | usage_with_options(record_usage, record_options); |
| 765 | } |
| 766 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 767 | symbol__init(); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 768 | |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 769 | if (symbol_conf.kptr_restrict) |
Arnaldo Carvalho de Melo | 646aaea | 2011-05-27 11:00:41 -0300 | [diff] [blame] | 770 | pr_warning( |
| 771 | "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" |
| 772 | "check /proc/sys/kernel/kptr_restrict.\n\n" |
| 773 | "Samples in kernel functions may not be resolved if a suitable vmlinux\n" |
| 774 | "file is not found in the buildid cache or in the vmlinux path.\n\n" |
| 775 | "Samples in kernel modules won't be resolved at all.\n\n" |
| 776 | "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" |
| 777 | "even with a suitable vmlinux or kallsyms file.\n\n"); |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 778 | |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 779 | if (no_buildid_cache || no_buildid) |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 780 | disable_buildid_cache(); |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 781 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 782 | if (evsel_list->nr_entries == 0 && |
| 783 | perf_evlist__add_default(evsel_list) < 0) { |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 784 | pr_err("Not enough memory for event selector list\n"); |
| 785 | goto out_symbol_exit; |
Peter Zijlstra | bbd36e5 | 2009-06-11 23:11:50 +0200 | [diff] [blame] | 786 | } |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 787 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 788 | if (record_opts.target_pid != -1) |
| 789 | record_opts.target_tid = record_opts.target_pid; |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 790 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 791 | if (perf_evlist__create_maps(evsel_list, record_opts.target_pid, |
| 792 | record_opts.target_tid, record_opts.cpu_list) < 0) |
Arnaldo Carvalho de Melo | dd7927f | 2011-01-12 14:28:51 -0200 | [diff] [blame] | 793 | usage_with_options(record_usage, record_options); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 794 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 795 | list_for_each_entry(pos, &evsel_list->entries, node) { |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 796 | if (perf_evsel__alloc_fd(pos, evsel_list->cpus->nr, |
| 797 | evsel_list->threads->nr) < 0) |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 798 | goto out_free_fd; |
Arnaldo Carvalho de Melo | ad7f4e3 | 2011-01-17 18:28:13 -0200 | [diff] [blame] | 799 | if (perf_header__push_event(pos->attr.config, event_name(pos))) |
| 800 | goto out_free_fd; |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 801 | } |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 802 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 803 | if (perf_evlist__alloc_pollfd(evsel_list) < 0) |
Arnaldo Carvalho de Melo | 39d17da | 2010-07-29 14:08:55 -0300 | [diff] [blame] | 804 | goto out_free_fd; |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 805 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 806 | if (record_opts.user_interval != ULLONG_MAX) |
| 807 | record_opts.default_interval = record_opts.user_interval; |
| 808 | if (record_opts.user_freq != UINT_MAX) |
| 809 | record_opts.freq = record_opts.user_freq; |
Frederic Weisbecker | f921281 | 2010-04-14 22:09:02 +0200 | [diff] [blame] | 810 | |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 811 | /* |
| 812 | * User specified count overrides default frequency. |
| 813 | */ |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame^] | 814 | if (record_opts.default_interval) |
| 815 | record_opts.freq = 0; |
| 816 | else if (record_opts.freq) { |
| 817 | record_opts.default_interval = record_opts.freq; |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 818 | } else { |
| 819 | fprintf(stderr, "frequency and count are zero, aborting\n"); |
Arnaldo Carvalho de Melo | 39d17da | 2010-07-29 14:08:55 -0300 | [diff] [blame] | 820 | err = -EINVAL; |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 821 | goto out_free_fd; |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Arnaldo Carvalho de Melo | 39d17da | 2010-07-29 14:08:55 -0300 | [diff] [blame] | 824 | err = __cmd_record(argc, argv); |
Arnaldo Carvalho de Melo | 39d17da | 2010-07-29 14:08:55 -0300 | [diff] [blame] | 825 | out_free_fd: |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 826 | perf_evlist__delete_maps(evsel_list); |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 827 | out_symbol_exit: |
| 828 | symbol__exit(); |
Arnaldo Carvalho de Melo | 39d17da | 2010-07-29 14:08:55 -0300 | [diff] [blame] | 829 | return err; |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 830 | } |