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