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