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