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