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