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