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" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 18 | #include "util/string.h" |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 19 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 20 | #include "util/header.h" |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 21 | #include "util/event.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 22 | #include "util/debug.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 23 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 24 | #include "util/symbol.h" |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 25 | #include "util/cpumap.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 26 | |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 27 | #include <unistd.h> |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 28 | #include <sched.h> |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 29 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 30 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 31 | |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 32 | static long default_interval = 0; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 33 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 34 | static int nr_cpus = 0; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 35 | static unsigned int page_size; |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 36 | static unsigned int mmap_pages = 128; |
| 37 | static int freq = 1000; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 38 | static int output; |
Ingo Molnar | 23ac9cb | 2009-05-27 09:33:18 +0200 | [diff] [blame] | 39 | static const char *output_name = "perf.data"; |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 40 | static int group = 0; |
| 41 | static unsigned int realtime_prio = 0; |
| 42 | static int raw_samples = 0; |
| 43 | static int system_wide = 0; |
| 44 | static int profile_cpu = -1; |
| 45 | static pid_t target_pid = -1; |
| 46 | static pid_t child_pid = -1; |
| 47 | static int inherit = 1; |
| 48 | static int force = 0; |
| 49 | static int append_file = 0; |
| 50 | static int call_graph = 0; |
| 51 | static int inherit_stat = 0; |
| 52 | static int no_samples = 0; |
| 53 | static int sample_address = 0; |
| 54 | static int multiplex = 0; |
| 55 | static int multiplex_fd = -1; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 56 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 57 | static long samples = 0; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 58 | static struct timeval last_read; |
| 59 | static struct timeval this_read; |
| 60 | |
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 | |
| 63 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; |
| 64 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 65 | static int nr_poll = 0; |
| 66 | static int nr_cpu = 0; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 67 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 68 | static int file_new = 1; |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 69 | static off_t post_processing_offset; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 70 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 71 | static struct perf_session *session; |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 72 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 73 | struct mmap_data { |
| 74 | int counter; |
| 75 | void *base; |
| 76 | unsigned int mask; |
| 77 | unsigned int prev; |
| 78 | }; |
| 79 | |
| 80 | static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; |
| 81 | |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 82 | static unsigned long mmap_read_head(struct mmap_data *md) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 83 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 84 | struct perf_event_mmap_page *pc = md->base; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 85 | long head; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 86 | |
| 87 | head = pc->data_head; |
| 88 | rmb(); |
| 89 | |
| 90 | return head; |
| 91 | } |
| 92 | |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 93 | static void mmap_write_tail(struct mmap_data *md, unsigned long tail) |
| 94 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 95 | struct perf_event_mmap_page *pc = md->base; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * ensure all reads are done before we write the tail out. |
| 99 | */ |
| 100 | /* mb(); */ |
| 101 | pc->data_tail = tail; |
| 102 | } |
| 103 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 104 | static void write_output(void *buf, size_t size) |
| 105 | { |
| 106 | while (size) { |
| 107 | int ret = write(output, buf, size); |
| 108 | |
| 109 | if (ret < 0) |
| 110 | die("failed to write"); |
| 111 | |
| 112 | size -= ret; |
| 113 | buf += ret; |
| 114 | |
| 115 | bytes_written += ret; |
| 116 | } |
| 117 | } |
| 118 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 119 | static int process_synthesized_event(event_t *event, |
| 120 | struct perf_session *self __used) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 121 | { |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 122 | write_output(event, event->header.size); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 126 | static void mmap_read(struct mmap_data *md) |
| 127 | { |
| 128 | unsigned int head = mmap_read_head(md); |
| 129 | unsigned int old = md->prev; |
| 130 | unsigned char *data = md->base + page_size; |
| 131 | unsigned long size; |
| 132 | void *buf; |
| 133 | int diff; |
| 134 | |
| 135 | gettimeofday(&this_read, NULL); |
| 136 | |
| 137 | /* |
| 138 | * If we're further behind than half the buffer, there's a chance |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 139 | * the writer will bite our tail and mess up the samples under us. |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 140 | * |
| 141 | * If we somehow ended up ahead of the head, we got messed up. |
| 142 | * |
| 143 | * In either case, truncate and restart at head. |
| 144 | */ |
| 145 | diff = head - old; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 146 | if (diff < 0) { |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 147 | struct timeval iv; |
| 148 | unsigned long msecs; |
| 149 | |
| 150 | timersub(&this_read, &last_read, &iv); |
| 151 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; |
| 152 | |
| 153 | fprintf(stderr, "WARNING: failed to keep up with mmap data." |
| 154 | " Last read %lu msecs ago.\n", msecs); |
| 155 | |
| 156 | /* |
| 157 | * head points to a known good entry, start there. |
| 158 | */ |
| 159 | old = head; |
| 160 | } |
| 161 | |
| 162 | last_read = this_read; |
| 163 | |
| 164 | if (old != head) |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 165 | samples++; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 166 | |
| 167 | size = head - old; |
| 168 | |
| 169 | if ((old & md->mask) + size != (head & md->mask)) { |
| 170 | buf = &data[old & md->mask]; |
| 171 | size = md->mask + 1 - (old & md->mask); |
| 172 | old += size; |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 173 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 174 | write_output(buf, size); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | buf = &data[old & md->mask]; |
| 178 | size = head - old; |
| 179 | old += size; |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 180 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 181 | write_output(buf, size); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 182 | |
| 183 | md->prev = old; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 184 | mmap_write_tail(md, old); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static volatile int done = 0; |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 188 | static volatile int signr = -1; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 189 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 190 | static void sig_handler(int sig) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 191 | { |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 192 | done = 1; |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 193 | signr = sig; |
| 194 | } |
| 195 | |
| 196 | static void sig_atexit(void) |
| 197 | { |
Chris Wilson | 933da83 | 2009-10-04 01:35:01 +0100 | [diff] [blame] | 198 | if (child_pid != -1) |
| 199 | kill(child_pid, SIGTERM); |
| 200 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 201 | if (signr == -1) |
| 202 | return; |
| 203 | |
| 204 | signal(signr, SIG_DFL); |
| 205 | kill(getpid(), signr); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 208 | static int group_fd; |
| 209 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 210 | static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 211 | { |
| 212 | struct perf_header_attr *h_attr; |
| 213 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 214 | if (nr < session->header.attrs) { |
| 215 | h_attr = session->header.attr[nr]; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 216 | } else { |
| 217 | h_attr = perf_header_attr__new(a); |
Arnaldo Carvalho de Melo | dc79c0f | 2009-11-16 19:30:26 -0200 | [diff] [blame] | 218 | if (h_attr != NULL) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 219 | if (perf_header__add_attr(&session->header, h_attr) < 0) { |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 220 | perf_header_attr__delete(h_attr); |
| 221 | h_attr = NULL; |
| 222 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return h_attr; |
| 226 | } |
| 227 | |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 228 | static void create_counter(int counter, int cpu, pid_t pid) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 229 | { |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 230 | char *filter = filters[counter]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 231 | struct perf_event_attr *attr = attrs + counter; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 232 | struct perf_header_attr *h_attr; |
| 233 | int track = !counter; /* only the first counter needs these */ |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 234 | int ret; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 235 | struct { |
| 236 | u64 count; |
| 237 | u64 time_enabled; |
| 238 | u64 time_running; |
| 239 | u64 id; |
| 240 | } read_data; |
| 241 | |
| 242 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
| 243 | PERF_FORMAT_TOTAL_TIME_RUNNING | |
| 244 | PERF_FORMAT_ID; |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 245 | |
Frederic Weisbecker | 3a9f131 | 2009-08-13 10:27:18 +0200 | [diff] [blame] | 246 | attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 247 | |
Eric B Munson | 8907fd6 | 2010-03-05 12:51:05 -0300 | [diff] [blame] | 248 | if (nr_counters > 1) |
| 249 | attr->sample_type |= PERF_SAMPLE_ID; |
| 250 | |
Ingo Molnar | 1dba15e | 2009-06-05 18:37:22 +0200 | [diff] [blame] | 251 | if (freq) { |
Peter Zijlstra | ea1900e | 2009-06-10 21:45:22 +0200 | [diff] [blame] | 252 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 253 | attr->freq = 1; |
| 254 | attr->sample_freq = freq; |
Ingo Molnar | 1dba15e | 2009-06-05 18:37:22 +0200 | [diff] [blame] | 255 | } |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 256 | |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 257 | if (no_samples) |
| 258 | attr->sample_freq = 0; |
| 259 | |
| 260 | if (inherit_stat) |
| 261 | attr->inherit_stat = 1; |
| 262 | |
Anton Blanchard | 4bba828 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 263 | if (sample_address) |
| 264 | attr->sample_type |= PERF_SAMPLE_ADDR; |
| 265 | |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 266 | if (call_graph) |
| 267 | attr->sample_type |= PERF_SAMPLE_CALLCHAIN; |
| 268 | |
Ingo Molnar | cd6feee | 2009-09-02 20:20:38 +0200 | [diff] [blame] | 269 | if (raw_samples) { |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 270 | attr->sample_type |= PERF_SAMPLE_TIME; |
Frederic Weisbecker | daac07b | 2009-08-13 10:27:19 +0200 | [diff] [blame] | 271 | attr->sample_type |= PERF_SAMPLE_RAW; |
Ingo Molnar | cd6feee | 2009-09-02 20:20:38 +0200 | [diff] [blame] | 272 | attr->sample_type |= PERF_SAMPLE_CPU; |
| 273 | } |
Frederic Weisbecker | f413cdb | 2009-08-07 01:25:54 +0200 | [diff] [blame] | 274 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 275 | attr->mmap = track; |
| 276 | attr->comm = track; |
Peter Zijlstra | 60ab271 | 2009-12-16 17:55:56 +0100 | [diff] [blame] | 277 | attr->inherit = inherit; |
Peter Zijlstra | 4502d77 | 2009-06-10 15:03:06 +0200 | [diff] [blame] | 278 | attr->disabled = 1; |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 279 | |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 280 | try_again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 281 | fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0); |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 282 | |
| 283 | if (fd[nr_cpu][counter] < 0) { |
| 284 | int err = errno; |
| 285 | |
Pekka Enberg | c10edee | 2009-11-08 18:01:06 +0200 | [diff] [blame] | 286 | if (err == EPERM || err == EACCES) |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 287 | die("Permission error - are you root?\n"); |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 288 | else if (err == ENODEV && profile_cpu != -1) |
| 289 | die("No such device - did you specify an out-of-range profile CPU?\n"); |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * If it's cycles then fall back to hrtimer |
| 293 | * based cpu-clock-tick sw counter, which |
| 294 | * is always available even if no PMU support: |
| 295 | */ |
| 296 | if (attr->type == PERF_TYPE_HARDWARE |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 297 | && attr->config == PERF_COUNT_HW_CPU_CYCLES) { |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 298 | |
| 299 | if (verbose) |
| 300 | warning(" ... trying to fall back to cpu-clock-ticks\n"); |
| 301 | attr->type = PERF_TYPE_SOFTWARE; |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 302 | attr->config = PERF_COUNT_SW_CPU_CLOCK; |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 303 | goto try_again; |
| 304 | } |
Ingo Molnar | 30c806a | 2009-06-07 17:46:24 +0200 | [diff] [blame] | 305 | printf("\n"); |
| 306 | error("perfcounter syscall returned with %d (%s)\n", |
| 307 | fd[nr_cpu][counter], strerror(err)); |
Simon Kaempflein | bfd4511 | 2009-11-16 15:25:53 +1000 | [diff] [blame] | 308 | |
| 309 | #if defined(__i386__) || defined(__x86_64__) |
| 310 | if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) |
| 311 | die("No hardware sampling interrupt available. No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.\n"); |
| 312 | #endif |
| 313 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 314 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 315 | exit(-1); |
| 316 | } |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 317 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 318 | h_attr = get_header_attr(attr, counter); |
Arnaldo Carvalho de Melo | dc79c0f | 2009-11-16 19:30:26 -0200 | [diff] [blame] | 319 | if (h_attr == NULL) |
| 320 | die("nomem\n"); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 321 | |
| 322 | if (!file_new) { |
| 323 | if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { |
| 324 | fprintf(stderr, "incompatible append\n"); |
| 325 | exit(-1); |
| 326 | } |
| 327 | } |
| 328 | |
Frederic Weisbecker | 3928ddb | 2009-06-25 22:21:27 +0200 | [diff] [blame] | 329 | if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) { |
| 330 | perror("Unable to read perf file descriptor\n"); |
| 331 | exit(-1); |
| 332 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 333 | |
Arnaldo Carvalho de Melo | 5875412 | 2009-11-17 01:18:10 -0200 | [diff] [blame] | 334 | if (perf_header_attr__add_id(h_attr, read_data.id) < 0) { |
| 335 | pr_warning("Not enough memory to add id\n"); |
| 336 | exit(-1); |
| 337 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 338 | |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 339 | assert(fd[nr_cpu][counter] >= 0); |
| 340 | fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); |
| 341 | |
| 342 | /* |
| 343 | * First counter acts as the group leader: |
| 344 | */ |
| 345 | if (group && group_fd == -1) |
| 346 | group_fd = fd[nr_cpu][counter]; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 347 | if (multiplex && multiplex_fd == -1) |
| 348 | multiplex_fd = fd[nr_cpu][counter]; |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 349 | |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 350 | if (multiplex && fd[nr_cpu][counter] != multiplex_fd) { |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 351 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 352 | ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd); |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 353 | assert(ret != -1); |
| 354 | } else { |
| 355 | event_array[nr_poll].fd = fd[nr_cpu][counter]; |
| 356 | event_array[nr_poll].events = POLLIN; |
| 357 | nr_poll++; |
| 358 | |
| 359 | mmap_array[nr_cpu][counter].counter = counter; |
| 360 | mmap_array[nr_cpu][counter].prev = 0; |
| 361 | mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; |
| 362 | mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, |
| 363 | PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0); |
| 364 | if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { |
| 365 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); |
| 366 | exit(-1); |
| 367 | } |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 368 | } |
Peter Zijlstra | 4502d77 | 2009-06-10 15:03:06 +0200 | [diff] [blame] | 369 | |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 370 | if (filter != NULL) { |
| 371 | ret = ioctl(fd[nr_cpu][counter], |
| 372 | PERF_EVENT_IOC_SET_FILTER, filter); |
| 373 | if (ret) { |
| 374 | error("failed to set filter with %d (%s)\n", errno, |
| 375 | strerror(errno)); |
| 376 | exit(-1); |
| 377 | } |
| 378 | } |
| 379 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 380 | ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE); |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static void open_counters(int cpu, pid_t pid) |
| 384 | { |
| 385 | int counter; |
| 386 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 387 | group_fd = -1; |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 388 | for (counter = 0; counter < nr_counters; counter++) |
| 389 | create_counter(counter, cpu, pid); |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 390 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 391 | nr_cpu++; |
| 392 | } |
| 393 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 394 | static int process_buildids(void) |
| 395 | { |
| 396 | u64 size = lseek(output, 0, SEEK_CUR); |
| 397 | |
Arnaldo Carvalho de Melo | 9f591fd | 2010-03-11 15:53:11 -0300 | [diff] [blame] | 398 | if (size == 0) |
| 399 | return 0; |
| 400 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 401 | session->fd = output; |
| 402 | return __perf_session__process_events(session, post_processing_offset, |
| 403 | size - post_processing_offset, |
| 404 | size, &build_id__mark_dso_hit_ops); |
| 405 | } |
| 406 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 407 | static void atexit_header(void) |
| 408 | { |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 409 | session->header.data_size += bytes_written; |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 410 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 411 | process_buildids(); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 412 | perf_header__write(&session->header, output, true); |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 415 | static int __cmd_record(int argc, const char **argv) |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 416 | { |
| 417 | int i, counter; |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 418 | struct stat st; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 419 | pid_t pid = 0; |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 420 | int flags; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 421 | int err; |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 422 | unsigned long waking = 0; |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 423 | int child_ready_pipe[2], go_pipe[2]; |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 424 | const bool forks = target_pid == -1 && argc > 0; |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 425 | char buf; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 426 | |
| 427 | page_size = sysconf(_SC_PAGE_SIZE); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 428 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 429 | atexit(sig_atexit); |
| 430 | signal(SIGCHLD, sig_handler); |
| 431 | signal(SIGINT, sig_handler); |
| 432 | |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 433 | if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 434 | perror("failed to create pipes"); |
| 435 | exit(-1); |
| 436 | } |
| 437 | |
Pierre Habouzit | 266e0e2 | 2009-08-07 14:16:01 +0200 | [diff] [blame] | 438 | if (!stat(output_name, &st) && st.st_size) { |
Arnaldo Carvalho de Melo | b38d346 | 2009-12-14 20:09:30 -0200 | [diff] [blame] | 439 | if (!force) { |
| 440 | if (!append_file) { |
| 441 | pr_err("Error, output file %s exists, use -A " |
| 442 | "to append or -f to overwrite.\n", |
| 443 | output_name); |
| 444 | exit(-1); |
| 445 | } |
| 446 | } else { |
| 447 | char oldname[PATH_MAX]; |
| 448 | snprintf(oldname, sizeof(oldname), "%s.old", |
| 449 | output_name); |
| 450 | unlink(oldname); |
| 451 | rename(output_name, oldname); |
Pierre Habouzit | 266e0e2 | 2009-08-07 14:16:01 +0200 | [diff] [blame] | 452 | } |
| 453 | } else { |
| 454 | append_file = 0; |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 457 | flags = O_CREAT|O_RDWR; |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 458 | if (append_file) |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 459 | file_new = 0; |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 460 | else |
| 461 | flags |= O_TRUNC; |
| 462 | |
| 463 | output = open(output_name, flags, S_IRUSR|S_IWUSR); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 464 | if (output < 0) { |
| 465 | perror("failed to create output file"); |
| 466 | exit(-1); |
| 467 | } |
| 468 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 469 | session = perf_session__new(output_name, O_WRONLY, force); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 470 | if (session == NULL) { |
Arnaldo Carvalho de Melo | a9a70bb | 2009-11-17 01:18:11 -0200 | [diff] [blame] | 471 | pr_err("Not enough memory for reading perf file header\n"); |
| 472 | return -1; |
| 473 | } |
| 474 | |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 475 | if (!file_new) { |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 476 | err = perf_header__read(&session->header, output); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 477 | if (err < 0) |
| 478 | return err; |
| 479 | } |
| 480 | |
Frederic Weisbecker | 9df37dd | 2009-08-17 23:07:50 +0200 | [diff] [blame] | 481 | if (raw_samples) { |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 482 | perf_header__set_feat(&session->header, HEADER_TRACE_INFO); |
Frederic Weisbecker | 9df37dd | 2009-08-17 23:07:50 +0200 | [diff] [blame] | 483 | } else { |
| 484 | for (i = 0; i < nr_counters; i++) { |
| 485 | if (attrs[i].sample_type & PERF_SAMPLE_RAW) { |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 486 | perf_header__set_feat(&session->header, HEADER_TRACE_INFO); |
Frederic Weisbecker | 9df37dd | 2009-08-17 23:07:50 +0200 | [diff] [blame] | 487 | break; |
| 488 | } |
| 489 | } |
| 490 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 491 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 492 | atexit(atexit_header); |
| 493 | |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 494 | if (forks) { |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 495 | pid = fork(); |
| 496 | if (pid < 0) { |
| 497 | perror("failed to fork"); |
| 498 | exit(-1); |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 499 | } |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 500 | |
| 501 | if (!pid) { |
| 502 | close(child_ready_pipe[0]); |
| 503 | close(go_pipe[1]); |
| 504 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); |
| 505 | |
| 506 | /* |
| 507 | * Do a dummy execvp to get the PLT entry resolved, |
| 508 | * so we avoid the resolver overhead on the real |
| 509 | * execvp call. |
| 510 | */ |
| 511 | execvp("", (char **)argv); |
| 512 | |
| 513 | /* |
| 514 | * Tell the parent we're ready to go |
| 515 | */ |
| 516 | close(child_ready_pipe[1]); |
| 517 | |
| 518 | /* |
| 519 | * Wait until the parent tells us to go. |
| 520 | */ |
| 521 | if (read(go_pipe[0], &buf, 1) == -1) |
| 522 | perror("unable to read pipe"); |
| 523 | |
| 524 | execvp(argv[0], (char **)argv); |
| 525 | |
| 526 | perror(argv[0]); |
| 527 | exit(-1); |
| 528 | } |
| 529 | |
| 530 | child_pid = pid; |
| 531 | |
| 532 | if (!system_wide) |
| 533 | target_pid = pid; |
| 534 | |
| 535 | close(child_ready_pipe[1]); |
| 536 | close(go_pipe[0]); |
| 537 | /* |
| 538 | * wait for child to settle |
| 539 | */ |
| 540 | if (read(child_ready_pipe[0], &buf, 1) == -1) { |
| 541 | perror("unable to read pipe"); |
| 542 | exit(-1); |
| 543 | } |
| 544 | close(child_ready_pipe[0]); |
| 545 | } |
| 546 | |
| 547 | |
Peter Zijlstra | 60ab271 | 2009-12-16 17:55:56 +0100 | [diff] [blame] | 548 | if ((!system_wide && !inherit) || profile_cpu != -1) { |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 549 | open_counters(profile_cpu, target_pid); |
| 550 | } else { |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 551 | nr_cpus = read_cpu_map(); |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 552 | for (i = 0; i < nr_cpus; i++) |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 553 | open_counters(cpumap[i], target_pid); |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 554 | } |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 555 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 556 | if (file_new) { |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 557 | err = perf_header__write(&session->header, output, false); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 558 | if (err < 0) |
| 559 | return err; |
| 560 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 561 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 562 | post_processing_offset = lseek(output, 0, SEEK_CUR); |
| 563 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 564 | err = event__synthesize_kernel_mmap(process_synthesized_event, |
| 565 | session, "_text"); |
| 566 | if (err < 0) { |
| 567 | pr_err("Couldn't record kernel reference relocation symbol.\n"); |
| 568 | return err; |
| 569 | } |
| 570 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 571 | err = event__synthesize_modules(process_synthesized_event, session); |
| 572 | if (err < 0) { |
| 573 | pr_err("Couldn't record kernel reference relocation symbol.\n"); |
| 574 | return err; |
| 575 | } |
| 576 | |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 577 | if (!system_wide && profile_cpu == -1) |
austin_zhang@linux.intel.com | f7e7ee3 | 2010-02-05 09:02:42 -0800 | [diff] [blame] | 578 | event__synthesize_thread(target_pid, process_synthesized_event, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 579 | session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 580 | else |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 581 | event__synthesize_threads(process_synthesized_event, session); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 582 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 583 | if (realtime_prio) { |
| 584 | struct sched_param param; |
| 585 | |
| 586 | param.sched_priority = realtime_prio; |
| 587 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 588 | pr_err("Could not set realtime priority.\n"); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 589 | exit(-1); |
| 590 | } |
| 591 | } |
| 592 | |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 593 | /* |
| 594 | * Let the child rip |
| 595 | */ |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 596 | if (forks) |
| 597 | close(go_pipe[1]); |
Peter Zijlstra | 856e966 | 2009-12-16 17:55:55 +0100 | [diff] [blame] | 598 | |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 599 | for (;;) { |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 600 | int hits = samples; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 601 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 602 | for (i = 0; i < nr_cpu; i++) { |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 603 | for (counter = 0; counter < nr_counters; counter++) { |
| 604 | if (mmap_array[i][counter].base) |
| 605 | mmap_read(&mmap_array[i][counter]); |
| 606 | } |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 609 | if (hits == samples) { |
| 610 | if (done) |
| 611 | break; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 612 | err = poll(event_array, nr_poll, -1); |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 613 | waking++; |
| 614 | } |
| 615 | |
| 616 | if (done) { |
| 617 | for (i = 0; i < nr_cpu; i++) { |
| 618 | for (counter = 0; counter < nr_counters; counter++) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 619 | ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE); |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 620 | } |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 621 | } |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 624 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
| 625 | |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 626 | /* |
| 627 | * Approximate RIP event size: 24 bytes. |
| 628 | */ |
| 629 | fprintf(stderr, |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 630 | "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 631 | (double)bytes_written / 1024.0 / 1024.0, |
| 632 | output_name, |
| 633 | bytes_written / 24); |
Ingo Molnar | addc278 | 2009-06-02 23:43:11 +0200 | [diff] [blame] | 634 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 635 | return 0; |
| 636 | } |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 637 | |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 638 | static const char * const record_usage[] = { |
Mike Galbraith | 9e09675 | 2009-05-28 16:25:34 +0200 | [diff] [blame] | 639 | "perf record [<options>] [<command>]", |
| 640 | "perf record [<options>] -- <command> [<options>]", |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 641 | NULL |
| 642 | }; |
| 643 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 644 | static const struct option options[] = { |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 645 | OPT_CALLBACK('e', "event", NULL, "event", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 646 | "event selector. use 'perf list' to list available events", |
| 647 | parse_events), |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 648 | OPT_CALLBACK(0, "filter", NULL, "filter", |
| 649 | "event filter", parse_filter), |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 650 | OPT_INTEGER('p', "pid", &target_pid, |
| 651 | "record events on existing pid"), |
| 652 | OPT_INTEGER('r', "realtime", &realtime_prio, |
| 653 | "collect data with this RT SCHED_FIFO priority"), |
Frederic Weisbecker | daac07b | 2009-08-13 10:27:19 +0200 | [diff] [blame] | 654 | OPT_BOOLEAN('R', "raw-samples", &raw_samples, |
| 655 | "collect raw sample records from all opened counters"), |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 656 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
| 657 | "system-wide collection from all CPUs"), |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 658 | OPT_BOOLEAN('A', "append", &append_file, |
| 659 | "append to the output file to do incremental profiling"), |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 660 | OPT_INTEGER('C', "profile_cpu", &profile_cpu, |
| 661 | "CPU to profile on"), |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 662 | OPT_BOOLEAN('f', "force", &force, |
| 663 | "overwrite existing data file"), |
Peter Zijlstra | e61078a | 2009-06-03 11:24:33 +0200 | [diff] [blame] | 664 | OPT_LONG('c', "count", &default_interval, |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 665 | "event period to sample"), |
| 666 | OPT_STRING('o', "output", &output_name, "file", |
| 667 | "output file name"), |
| 668 | OPT_BOOLEAN('i', "inherit", &inherit, |
| 669 | "child tasks inherit counters"), |
Ingo Molnar | cf1f457 | 2009-06-05 13:27:02 +0200 | [diff] [blame] | 670 | OPT_INTEGER('F', "freq", &freq, |
| 671 | "profile at this frequency"), |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 672 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
| 673 | "number of mmap data pages"), |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 674 | OPT_BOOLEAN('g', "call-graph", &call_graph, |
| 675 | "do call-graph (stack chain/backtrace) recording"), |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 676 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 677 | "be more verbose (show counter open errors, etc)"), |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 678 | OPT_BOOLEAN('s', "stat", &inherit_stat, |
| 679 | "per thread counts"), |
Anton Blanchard | 4bba828 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 680 | OPT_BOOLEAN('d', "data", &sample_address, |
| 681 | "Sample addresses"), |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 682 | OPT_BOOLEAN('n', "no-samples", &no_samples, |
| 683 | "don't sample"), |
Frederic Weisbecker | d130252 | 2009-09-14 08:57:15 +0200 | [diff] [blame] | 684 | OPT_BOOLEAN('M', "multiplex", &multiplex, |
| 685 | "multiplex counter output in a single channel"), |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 686 | OPT_END() |
| 687 | }; |
| 688 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 689 | int cmd_record(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 690 | { |
| 691 | int counter; |
| 692 | |
Anton Blanchard | a054123 | 2009-07-22 23:04:12 +1000 | [diff] [blame] | 693 | argc = parse_options(argc, argv, options, record_usage, |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 694 | PARSE_OPT_STOP_AT_NON_OPTION); |
Arnaldo Carvalho de Melo | d4db3f1 | 2009-12-27 21:36:57 -0200 | [diff] [blame] | 695 | if (!argc && target_pid == -1 && !system_wide && profile_cpu == -1) |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 696 | usage_with_options(record_usage, options); |
| 697 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 698 | symbol__init(); |
| 699 | |
Peter Zijlstra | bbd36e5 | 2009-06-11 23:11:50 +0200 | [diff] [blame] | 700 | if (!nr_counters) { |
| 701 | nr_counters = 1; |
| 702 | attrs[0].type = PERF_TYPE_HARDWARE; |
| 703 | attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; |
| 704 | } |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 705 | |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 706 | /* |
| 707 | * User specified count overrides default frequency. |
| 708 | */ |
| 709 | if (default_interval) |
| 710 | freq = 0; |
| 711 | else if (freq) { |
| 712 | default_interval = freq; |
| 713 | } else { |
| 714 | fprintf(stderr, "frequency and count are zero, aborting\n"); |
| 715 | exit(EXIT_FAILURE); |
| 716 | } |
| 717 | |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 718 | for (counter = 0; counter < nr_counters; counter++) { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 719 | if (attrs[counter].sample_period) |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 720 | continue; |
| 721 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 722 | attrs[counter].sample_period = default_interval; |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | return __cmd_record(argc, argv); |
| 726 | } |