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