Commit | Line | Data |
---|---|---|
abaff32a | 1 | /* |
bf9e1876 IM |
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. | |
abaff32a | 7 | */ |
16f762a2 | 8 | #include "builtin.h" |
bf9e1876 IM |
9 | |
10 | #include "perf.h" | |
11 | ||
6eda5838 | 12 | #include "util/util.h" |
0e9b20b8 | 13 | #include "util/parse-options.h" |
8ad8db37 | 14 | #include "util/parse-events.h" |
a0055ae2 | 15 | #include "util/string.h" |
6eda5838 | 16 | |
7c6a1c65 PZ |
17 | #include "util/header.h" |
18 | ||
97124d5e | 19 | #include <unistd.h> |
de9ac07b | 20 | #include <sched.h> |
de9ac07b | 21 | |
0e9b20b8 IM |
22 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) |
23 | #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) | |
1a853e36 | 24 | |
de9ac07b | 25 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
a21ca2ca IM |
26 | |
27 | static long default_interval = 100000; | |
28 | ||
3cf165fc | 29 | static int nr_cpus = 0; |
de9ac07b | 30 | static unsigned int page_size; |
3cf165fc | 31 | static unsigned int mmap_pages = 128; |
cf1f4574 | 32 | static int freq = 0; |
de9ac07b | 33 | static int output; |
23ac9cbe | 34 | static const char *output_name = "perf.data"; |
de9ac07b | 35 | static int group = 0; |
16c8a109 PZ |
36 | static unsigned int realtime_prio = 0; |
37 | static int system_wide = 0; | |
1a853e36 | 38 | static pid_t target_pid = -1; |
16c8a109 | 39 | static int inherit = 1; |
97124d5e | 40 | static int force = 0; |
abaff32a | 41 | static int append_file = 0; |
3efa1cc9 | 42 | static int call_graph = 0; |
3da297a6 | 43 | static int verbose = 0; |
649c48a9 PZ |
44 | static int inherit_stat = 0; |
45 | static int no_samples = 0; | |
4bba828d | 46 | static int sample_address = 0; |
de9ac07b | 47 | |
a21ca2ca IM |
48 | static long samples; |
49 | static struct timeval last_read; | |
50 | static struct timeval this_read; | |
51 | ||
9cffa8d5 | 52 | static u64 bytes_written; |
a21ca2ca IM |
53 | |
54 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; | |
55 | ||
56 | static int nr_poll; | |
57 | static int nr_cpu; | |
58 | ||
f5970550 | 59 | static int file_new = 1; |
7c6a1c65 PZ |
60 | |
61 | struct perf_header *header; | |
f5970550 | 62 | |
a21ca2ca IM |
63 | struct mmap_event { |
64 | struct perf_event_header header; | |
9cffa8d5 PM |
65 | u32 pid; |
66 | u32 tid; | |
67 | u64 start; | |
68 | u64 len; | |
69 | u64 pgoff; | |
a21ca2ca IM |
70 | char filename[PATH_MAX]; |
71 | }; | |
72 | ||
73 | struct comm_event { | |
74 | struct perf_event_header header; | |
9cffa8d5 PM |
75 | u32 pid; |
76 | u32 tid; | |
a21ca2ca | 77 | char comm[16]; |
de9ac07b PZ |
78 | }; |
79 | ||
a21ca2ca | 80 | |
de9ac07b | 81 | struct mmap_data { |
a21ca2ca IM |
82 | int counter; |
83 | void *base; | |
84 | unsigned int mask; | |
85 | unsigned int prev; | |
de9ac07b PZ |
86 | }; |
87 | ||
a21ca2ca IM |
88 | static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; |
89 | ||
9d91a6f7 | 90 | static unsigned long mmap_read_head(struct mmap_data *md) |
de9ac07b PZ |
91 | { |
92 | struct perf_counter_mmap_page *pc = md->base; | |
9d91a6f7 | 93 | long head; |
de9ac07b PZ |
94 | |
95 | head = pc->data_head; | |
96 | rmb(); | |
97 | ||
98 | return head; | |
99 | } | |
100 | ||
9d91a6f7 PZ |
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 | ||
f5970550 PZ |
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 | ||
de9ac07b PZ |
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 | |
2debbc83 | 140 | * the writer will bite our tail and mess up the samples under us. |
de9ac07b PZ |
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; | |
9d91a6f7 | 147 | if (diff < 0) { |
de9ac07b PZ |
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) | |
2debbc83 | 166 | samples++; |
de9ac07b PZ |
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; | |
021e9f47 | 174 | |
f5970550 | 175 | write_output(buf, size); |
de9ac07b PZ |
176 | } |
177 | ||
178 | buf = &data[old & md->mask]; | |
179 | size = head - old; | |
180 | old += size; | |
021e9f47 | 181 | |
f5970550 | 182 | write_output(buf, size); |
de9ac07b PZ |
183 | |
184 | md->prev = old; | |
9d91a6f7 | 185 | mmap_write_tail(md, old); |
de9ac07b PZ |
186 | } |
187 | ||
188 | static volatile int done = 0; | |
f7b7c26e | 189 | static volatile int signr = -1; |
de9ac07b | 190 | |
16c8a109 | 191 | static void sig_handler(int sig) |
de9ac07b | 192 | { |
16c8a109 | 193 | done = 1; |
f7b7c26e PZ |
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); | |
de9ac07b PZ |
204 | } |
205 | ||
f70e87d7 | 206 | static void pid_synthesize_comm_event(pid_t pid, int full) |
1a853e36 | 207 | { |
16f762a2 | 208 | struct comm_event comm_ev; |
1a853e36 ACM |
209 | char filename[PATH_MAX]; |
210 | char bf[BUFSIZ]; | |
f5970550 | 211 | int fd; |
1a853e36 | 212 | size_t size; |
a0055ae2 | 213 | char *field, *sep; |
f70e87d7 PZ |
214 | DIR *tasks; |
215 | struct dirent dirent, *next; | |
1a853e36 ACM |
216 | |
217 | snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); | |
218 | ||
219 | fd = open(filename, O_RDONLY); | |
220 | if (fd < 0) { | |
613d8602 IM |
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; | |
1a853e36 ACM |
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 | ||
a0055ae2 | 234 | /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */ |
1a853e36 | 235 | memset(&comm_ev, 0, sizeof(comm_ev)); |
a0055ae2 ACM |
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++); | |
f70e87d7 PZ |
244 | |
245 | comm_ev.pid = pid; | |
1a853e36 | 246 | comm_ev.header.type = PERF_EVENT_COMM; |
9cffa8d5 | 247 | size = ALIGN(size, sizeof(u64)); |
1a853e36 | 248 | comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size); |
16f762a2 | 249 | |
f70e87d7 PZ |
250 | if (!full) { |
251 | comm_ev.tid = pid; | |
252 | ||
f5970550 | 253 | write_output(&comm_ev, comm_ev.header.size); |
f70e87d7 PZ |
254 | return; |
255 | } | |
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 | ||
f5970550 | 268 | write_output(&comm_ev, comm_ev.header.size); |
1a853e36 | 269 | } |
f70e87d7 PZ |
270 | closedir(tasks); |
271 | return; | |
272 | ||
a0055ae2 ACM |
273 | out_failure: |
274 | fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n", | |
275 | filename); | |
276 | exit(EXIT_FAILURE); | |
1a853e36 ACM |
277 | } |
278 | ||
2debbc83 | 279 | static void pid_synthesize_mmap_samples(pid_t pid) |
1a853e36 ACM |
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) { | |
613d8602 IM |
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; | |
1a853e36 ACM |
294 | } |
295 | while (1) { | |
a0055ae2 | 296 | char bf[BUFSIZ], *pbf = bf; |
1a853e36 | 297 | struct mmap_event mmap_ev = { |
f37a291c | 298 | .header = { .type = PERF_EVENT_MMAP }, |
1a853e36 | 299 | }; |
a0055ae2 | 300 | int n; |
1a853e36 ACM |
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 */ | |
a0055ae2 ACM |
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 */ | |
76c64c5e | 315 | char *execname = strchr(bf, '/'); |
1a853e36 | 316 | |
11b5f81e AB |
317 | /* Catch VDSO */ |
318 | if (execname == NULL) | |
319 | execname = strstr(bf, "[vdso]"); | |
320 | ||
76c64c5e | 321 | if (execname == NULL) |
1a853e36 ACM |
322 | continue; |
323 | ||
1a853e36 ACM |
324 | size = strlen(execname); |
325 | execname[size - 1] = '\0'; /* Remove \n */ | |
326 | memcpy(mmap_ev.filename, execname, size); | |
9cffa8d5 | 327 | size = ALIGN(size, sizeof(u64)); |
1a853e36 ACM |
328 | mmap_ev.len -= mmap_ev.start; |
329 | mmap_ev.header.size = (sizeof(mmap_ev) - | |
330 | (sizeof(mmap_ev.filename) - size)); | |
f70e87d7 | 331 | mmap_ev.pid = pid; |
1a853e36 ACM |
332 | mmap_ev.tid = pid; |
333 | ||
f5970550 | 334 | write_output(&mmap_ev, mmap_ev.header.size); |
1a853e36 ACM |
335 | } |
336 | } | |
337 | ||
338 | fclose(fp); | |
339 | } | |
340 | ||
7c6a1c65 | 341 | static void synthesize_all(void) |
f70e87d7 PZ |
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); | |
2debbc83 | 357 | pid_synthesize_mmap_samples(pid); |
f70e87d7 PZ |
358 | } |
359 | ||
360 | closedir(proc); | |
361 | } | |
362 | ||
f250c030 IM |
363 | static int group_fd; |
364 | ||
7c6a1c65 PZ |
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 | ||
f250c030 | 379 | static void create_counter(int counter, int cpu, pid_t pid) |
de9ac07b | 380 | { |
a21ca2ca | 381 | struct perf_counter_attr *attr = attrs + counter; |
7c6a1c65 PZ |
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; | |
16c8a109 | 394 | |
ea1900e5 | 395 | attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
3efa1cc9 | 396 | |
1dba15e7 | 397 | if (freq) { |
ea1900e5 | 398 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
a21ca2ca IM |
399 | attr->freq = 1; |
400 | attr->sample_freq = freq; | |
1dba15e7 | 401 | } |
3efa1cc9 | 402 | |
649c48a9 PZ |
403 | if (no_samples) |
404 | attr->sample_freq = 0; | |
405 | ||
406 | if (inherit_stat) | |
407 | attr->inherit_stat = 1; | |
408 | ||
4bba828d AB |
409 | if (sample_address) |
410 | attr->sample_type |= PERF_SAMPLE_ADDR; | |
411 | ||
3efa1cc9 IM |
412 | if (call_graph) |
413 | attr->sample_type |= PERF_SAMPLE_CALLCHAIN; | |
414 | ||
f413cdb8 | 415 | |
a21ca2ca IM |
416 | attr->mmap = track; |
417 | attr->comm = track; | |
418 | attr->inherit = (cpu < 0) && inherit; | |
4502d77c | 419 | attr->disabled = 1; |
16c8a109 | 420 | |
3da297a6 | 421 | try_again: |
a21ca2ca | 422 | fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0); |
16c8a109 | 423 | |
f250c030 IM |
424 | if (fd[nr_cpu][counter] < 0) { |
425 | int err = errno; | |
16c8a109 | 426 | |
f250c030 | 427 | if (err == EPERM) |
3da297a6 IM |
428 | die("Permission error - are you root?\n"); |
429 | ||
430 | /* | |
431 | * If it's cycles then fall back to hrtimer | |
432 | * based cpu-clock-tick sw counter, which | |
433 | * is always available even if no PMU support: | |
434 | */ | |
435 | if (attr->type == PERF_TYPE_HARDWARE | |
f4dbfa8f | 436 | && attr->config == PERF_COUNT_HW_CPU_CYCLES) { |
3da297a6 IM |
437 | |
438 | if (verbose) | |
439 | warning(" ... trying to fall back to cpu-clock-ticks\n"); | |
440 | attr->type = PERF_TYPE_SOFTWARE; | |
f4dbfa8f | 441 | attr->config = PERF_COUNT_SW_CPU_CLOCK; |
3da297a6 IM |
442 | goto try_again; |
443 | } | |
30c806a0 IM |
444 | printf("\n"); |
445 | error("perfcounter syscall returned with %d (%s)\n", | |
446 | fd[nr_cpu][counter], strerror(err)); | |
447 | die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n"); | |
f250c030 IM |
448 | exit(-1); |
449 | } | |
3da297a6 | 450 | |
7c6a1c65 PZ |
451 | h_attr = get_header_attr(attr, counter); |
452 | ||
453 | if (!file_new) { | |
454 | if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { | |
455 | fprintf(stderr, "incompatible append\n"); | |
456 | exit(-1); | |
457 | } | |
458 | } | |
459 | ||
3928ddbe FW |
460 | if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) { |
461 | perror("Unable to read perf file descriptor\n"); | |
462 | exit(-1); | |
463 | } | |
7c6a1c65 PZ |
464 | |
465 | perf_header_attr__add_id(h_attr, read_data.id); | |
466 | ||
f250c030 IM |
467 | assert(fd[nr_cpu][counter] >= 0); |
468 | fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); | |
16c8a109 | 469 | |
f250c030 IM |
470 | /* |
471 | * First counter acts as the group leader: | |
472 | */ | |
473 | if (group && group_fd == -1) | |
474 | group_fd = fd[nr_cpu][counter]; | |
475 | ||
476 | event_array[nr_poll].fd = fd[nr_cpu][counter]; | |
477 | event_array[nr_poll].events = POLLIN; | |
478 | nr_poll++; | |
479 | ||
480 | mmap_array[nr_cpu][counter].counter = counter; | |
481 | mmap_array[nr_cpu][counter].prev = 0; | |
482 | mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; | |
483 | mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, | |
9d91a6f7 | 484 | PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0); |
f250c030 IM |
485 | if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { |
486 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); | |
487 | exit(-1); | |
488 | } | |
4502d77c PZ |
489 | |
490 | ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE); | |
f250c030 | 491 | } |
f2521b6e | 492 | |
f250c030 IM |
493 | static void open_counters(int cpu, pid_t pid) |
494 | { | |
495 | int counter; | |
16c8a109 | 496 | |
f250c030 IM |
497 | group_fd = -1; |
498 | for (counter = 0; counter < nr_counters; counter++) | |
499 | create_counter(counter, cpu, pid); | |
500 | ||
16c8a109 PZ |
501 | nr_cpu++; |
502 | } | |
503 | ||
f5970550 PZ |
504 | static void atexit_header(void) |
505 | { | |
7c6a1c65 | 506 | header->data_size += bytes_written; |
f5970550 | 507 | |
7c6a1c65 | 508 | perf_header__write(header, output); |
f5970550 PZ |
509 | } |
510 | ||
0e9b20b8 | 511 | static int __cmd_record(int argc, const char **argv) |
16c8a109 PZ |
512 | { |
513 | int i, counter; | |
abaff32a | 514 | struct stat st; |
7c6a1c65 | 515 | pid_t pid = 0; |
abaff32a | 516 | int flags; |
de9ac07b PZ |
517 | int ret; |
518 | ||
519 | page_size = sysconf(_SC_PAGE_SIZE); | |
de9ac07b PZ |
520 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
521 | assert(nr_cpus <= MAX_NR_CPUS); | |
522 | assert(nr_cpus >= 0); | |
523 | ||
f5970550 PZ |
524 | atexit(sig_atexit); |
525 | signal(SIGCHLD, sig_handler); | |
526 | signal(SIGINT, sig_handler); | |
527 | ||
abaff32a IM |
528 | if (!stat(output_name, &st) && !force && !append_file) { |
529 | fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n", | |
97124d5e PZ |
530 | output_name); |
531 | exit(-1); | |
532 | } | |
533 | ||
abaff32a IM |
534 | flags = O_CREAT|O_RDWR; |
535 | if (append_file) | |
f5970550 | 536 | file_new = 0; |
abaff32a IM |
537 | else |
538 | flags |= O_TRUNC; | |
539 | ||
540 | output = open(output_name, flags, S_IRUSR|S_IWUSR); | |
de9ac07b PZ |
541 | if (output < 0) { |
542 | perror("failed to create output file"); | |
543 | exit(-1); | |
544 | } | |
545 | ||
7c6a1c65 PZ |
546 | if (!file_new) |
547 | header = perf_header__read(output); | |
548 | else | |
549 | header = perf_header__new(); | |
f5970550 PZ |
550 | |
551 | atexit(atexit_header); | |
552 | ||
1a853e36 | 553 | if (!system_wide) { |
7c6a1c65 PZ |
554 | pid = target_pid; |
555 | if (pid == -1) | |
556 | pid = getpid(); | |
557 | ||
558 | open_counters(-1, pid); | |
1a853e36 ACM |
559 | } else for (i = 0; i < nr_cpus; i++) |
560 | open_counters(i, target_pid); | |
de9ac07b | 561 | |
7c6a1c65 PZ |
562 | if (file_new) |
563 | perf_header__write(header, output); | |
564 | ||
565 | if (!system_wide) { | |
566 | pid_synthesize_comm_event(pid, 0); | |
567 | pid_synthesize_mmap_samples(pid); | |
568 | } else | |
569 | synthesize_all(); | |
570 | ||
ef65b2a0 | 571 | if (target_pid == -1 && argc) { |
1a853e36 ACM |
572 | pid = fork(); |
573 | if (pid < 0) | |
574 | perror("failed to fork"); | |
de9ac07b | 575 | |
1a853e36 | 576 | if (!pid) { |
0e9b20b8 | 577 | if (execvp(argv[0], (char **)argv)) { |
1a853e36 ACM |
578 | perror(argv[0]); |
579 | exit(-1); | |
580 | } | |
de9ac07b PZ |
581 | } |
582 | } | |
583 | ||
584 | if (realtime_prio) { | |
585 | struct sched_param param; | |
586 | ||
587 | param.sched_priority = realtime_prio; | |
588 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | |
589 | printf("Could not set realtime priority.\n"); | |
590 | exit(-1); | |
591 | } | |
592 | } | |
593 | ||
649c48a9 | 594 | for (;;) { |
2debbc83 | 595 | int hits = samples; |
de9ac07b | 596 | |
16c8a109 | 597 | for (i = 0; i < nr_cpu; i++) { |
de9ac07b PZ |
598 | for (counter = 0; counter < nr_counters; counter++) |
599 | mmap_read(&mmap_array[i][counter]); | |
600 | } | |
601 | ||
649c48a9 PZ |
602 | if (hits == samples) { |
603 | if (done) | |
604 | break; | |
de9ac07b | 605 | ret = poll(event_array, nr_poll, 100); |
649c48a9 | 606 | } |
de9ac07b PZ |
607 | } |
608 | ||
021e9f47 IM |
609 | /* |
610 | * Approximate RIP event size: 24 bytes. | |
611 | */ | |
612 | fprintf(stderr, | |
2debbc83 | 613 | "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", |
021e9f47 IM |
614 | (double)bytes_written / 1024.0 / 1024.0, |
615 | output_name, | |
616 | bytes_written / 24); | |
addc2785 | 617 | |
de9ac07b PZ |
618 | return 0; |
619 | } | |
0e9b20b8 | 620 | |
0e9b20b8 | 621 | static const char * const record_usage[] = { |
9e096753 MG |
622 | "perf record [<options>] [<command>]", |
623 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
624 | NULL |
625 | }; | |
626 | ||
5242519b | 627 | static const struct option options[] = { |
0e9b20b8 | 628 | OPT_CALLBACK('e', "event", NULL, "event", |
86847b62 TG |
629 | "event selector. use 'perf list' to list available events", |
630 | parse_events), | |
0e9b20b8 IM |
631 | OPT_INTEGER('p', "pid", &target_pid, |
632 | "record events on existing pid"), | |
633 | OPT_INTEGER('r', "realtime", &realtime_prio, | |
634 | "collect data with this RT SCHED_FIFO priority"), | |
635 | OPT_BOOLEAN('a', "all-cpus", &system_wide, | |
636 | "system-wide collection from all CPUs"), | |
abaff32a IM |
637 | OPT_BOOLEAN('A', "append", &append_file, |
638 | "append to the output file to do incremental profiling"), | |
97124d5e PZ |
639 | OPT_BOOLEAN('f', "force", &force, |
640 | "overwrite existing data file"), | |
e61078a0 | 641 | OPT_LONG('c', "count", &default_interval, |
abaff32a IM |
642 | "event period to sample"), |
643 | OPT_STRING('o', "output", &output_name, "file", | |
644 | "output file name"), | |
645 | OPT_BOOLEAN('i', "inherit", &inherit, | |
646 | "child tasks inherit counters"), | |
cf1f4574 IM |
647 | OPT_INTEGER('F', "freq", &freq, |
648 | "profile at this frequency"), | |
abaff32a IM |
649 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
650 | "number of mmap data pages"), | |
3efa1cc9 IM |
651 | OPT_BOOLEAN('g', "call-graph", &call_graph, |
652 | "do call-graph (stack chain/backtrace) recording"), | |
3da297a6 IM |
653 | OPT_BOOLEAN('v', "verbose", &verbose, |
654 | "be more verbose (show counter open errors, etc)"), | |
649c48a9 PZ |
655 | OPT_BOOLEAN('s', "stat", &inherit_stat, |
656 | "per thread counts"), | |
4bba828d AB |
657 | OPT_BOOLEAN('d', "data", &sample_address, |
658 | "Sample addresses"), | |
649c48a9 PZ |
659 | OPT_BOOLEAN('n', "no-samples", &no_samples, |
660 | "don't sample"), | |
0e9b20b8 IM |
661 | OPT_END() |
662 | }; | |
663 | ||
f37a291c | 664 | int cmd_record(int argc, const char **argv, const char *prefix __used) |
0e9b20b8 IM |
665 | { |
666 | int counter; | |
667 | ||
a0541234 AB |
668 | argc = parse_options(argc, argv, options, record_usage, |
669 | PARSE_OPT_STOP_AT_NON_OPTION); | |
ef65b2a0 | 670 | if (!argc && target_pid == -1 && !system_wide) |
0e9b20b8 IM |
671 | usage_with_options(record_usage, options); |
672 | ||
bbd36e5e PZ |
673 | if (!nr_counters) { |
674 | nr_counters = 1; | |
675 | attrs[0].type = PERF_TYPE_HARDWARE; | |
676 | attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; | |
677 | } | |
0e9b20b8 IM |
678 | |
679 | for (counter = 0; counter < nr_counters; counter++) { | |
a21ca2ca | 680 | if (attrs[counter].sample_period) |
0e9b20b8 IM |
681 | continue; |
682 | ||
a21ca2ca | 683 | attrs[counter].sample_period = default_interval; |
0e9b20b8 IM |
684 | } |
685 | ||
686 | return __cmd_record(argc, argv); | |
687 | } |