xref: /linux/tools/perf/builtin-record.c (revision ac6a0cf6716bb46813d0161024c66c2af66e53d1)
1 /*
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.
7  */
8 #include "builtin.h"
9 
10 #include "perf.h"
11 
12 #include "util/util.h"
13 #include "util/parse-options.h"
14 #include "util/parse-events.h"
15 #include "util/string.h"
16 
17 #include "util/header.h"
18 
19 #include <unistd.h>
20 #include <sched.h>
21 
22 #define ALIGN(x, a)		__ALIGN_MASK(x, (typeof(x))(a)-1)
23 #define __ALIGN_MASK(x, mask)	(((x)+(mask))&~(mask))
24 
25 static int			fd[MAX_NR_CPUS][MAX_COUNTERS];
26 
27 static long			default_interval		= 100000;
28 
29 static int			nr_cpus				= 0;
30 static unsigned int		page_size;
31 static unsigned int		mmap_pages			= 128;
32 static int			freq				= 0;
33 static int			output;
34 static const char		*output_name			= "perf.data";
35 static int			group				= 0;
36 static unsigned int		realtime_prio			= 0;
37 static int			raw_samples			= 0;
38 static int			system_wide			= 0;
39 static int			profile_cpu			= -1;
40 static pid_t			target_pid			= -1;
41 static int			inherit				= 1;
42 static int			force				= 0;
43 static int			append_file			= 0;
44 static int			call_graph			= 0;
45 static int			verbose				= 0;
46 static int			inherit_stat			= 0;
47 static int			no_samples			= 0;
48 static int			sample_address			= 0;
49 
50 static long			samples;
51 static struct timeval		last_read;
52 static struct timeval		this_read;
53 
54 static u64			bytes_written;
55 
56 static struct pollfd		event_array[MAX_NR_CPUS * MAX_COUNTERS];
57 
58 static int			nr_poll;
59 static int			nr_cpu;
60 
61 static int			file_new = 1;
62 
63 struct perf_header		*header;
64 
65 struct mmap_event {
66 	struct perf_event_header	header;
67 	u32				pid;
68 	u32				tid;
69 	u64				start;
70 	u64				len;
71 	u64				pgoff;
72 	char				filename[PATH_MAX];
73 };
74 
75 struct comm_event {
76 	struct perf_event_header	header;
77 	u32				pid;
78 	u32				tid;
79 	char				comm[16];
80 };
81 
82 
83 struct mmap_data {
84 	int			counter;
85 	void			*base;
86 	unsigned int		mask;
87 	unsigned int		prev;
88 };
89 
90 static struct mmap_data		mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
91 
92 static unsigned long mmap_read_head(struct mmap_data *md)
93 {
94 	struct perf_counter_mmap_page *pc = md->base;
95 	long head;
96 
97 	head = pc->data_head;
98 	rmb();
99 
100 	return head;
101 }
102 
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 
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 
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
142 	 * the writer will bite our tail and mess up the samples under us.
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;
149 	if (diff < 0) {
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)
168 		samples++;
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;
176 
177 		write_output(buf, size);
178 	}
179 
180 	buf = &data[old & md->mask];
181 	size = head - old;
182 	old += size;
183 
184 	write_output(buf, size);
185 
186 	md->prev = old;
187 	mmap_write_tail(md, old);
188 }
189 
190 static volatile int done = 0;
191 static volatile int signr = -1;
192 
193 static void sig_handler(int sig)
194 {
195 	done = 1;
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);
206 }
207 
208 static pid_t pid_synthesize_comm_event(pid_t pid, int full)
209 {
210 	struct comm_event comm_ev;
211 	char filename[PATH_MAX];
212 	char bf[BUFSIZ];
213 	FILE *fp;
214 	size_t size = 0;
215 	DIR *tasks;
216 	struct dirent dirent, *next;
217 	pid_t tgid = 0;
218 
219 	snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
220 
221 	fp = fopen(filename, "r");
222 	if (fp == NULL) {
223 		/*
224 		 * We raced with a task exiting - just return:
225 		 */
226 		if (verbose)
227 			fprintf(stderr, "couldn't open %s\n", filename);
228 		return 0;
229 	}
230 
231 	memset(&comm_ev, 0, sizeof(comm_ev));
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 
250 	comm_ev.header.type = PERF_EVENT_COMM;
251 	size = ALIGN(size, sizeof(u64));
252 	comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
253 
254 	if (!full) {
255 		comm_ev.tid = pid;
256 
257 		write_output(&comm_ev, comm_ev.header.size);
258 		goto out_fclose;
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 
272 		write_output(&comm_ev, comm_ev.header.size);
273 	}
274 	closedir(tasks);
275 
276 out_fclose:
277 	fclose(fp);
278 	return tgid;
279 
280 out_failure:
281 	fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
282 		filename);
283 	exit(EXIT_FAILURE);
284 }
285 
286 static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
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) {
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;
301 	}
302 	while (1) {
303 		char bf[BUFSIZ], *pbf = bf;
304 		struct mmap_event mmap_ev = {
305 			.header = { .type = PERF_EVENT_MMAP },
306 		};
307 		int n;
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 */
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 */
322 			char *execname = strchr(bf, '/');
323 
324 			/* Catch VDSO */
325 			if (execname == NULL)
326 				execname = strstr(bf, "[vdso]");
327 
328 			if (execname == NULL)
329 				continue;
330 
331 			size = strlen(execname);
332 			execname[size - 1] = '\0'; /* Remove \n */
333 			memcpy(mmap_ev.filename, execname, size);
334 			size = ALIGN(size, sizeof(u64));
335 			mmap_ev.len -= mmap_ev.start;
336 			mmap_ev.header.size = (sizeof(mmap_ev) -
337 					       (sizeof(mmap_ev.filename) - size));
338 			mmap_ev.pid = tgid;
339 			mmap_ev.tid = pid;
340 
341 			write_output(&mmap_ev, mmap_ev.header.size);
342 		}
343 	}
344 
345 	fclose(fp);
346 }
347 
348 static void synthesize_all(void)
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;
357 		pid_t pid, tgid;
358 
359 		pid = strtol(dirent.d_name, &end, 10);
360 		if (*end) /* only interested in proper numerical dirents */
361 			continue;
362 
363 		tgid = pid_synthesize_comm_event(pid, 1);
364 		pid_synthesize_mmap_samples(pid, tgid);
365 	}
366 
367 	closedir(proc);
368 }
369 
370 static int group_fd;
371 
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 
386 static void create_counter(int counter, int cpu, pid_t pid)
387 {
388 	struct perf_counter_attr *attr = attrs + counter;
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;
401 
402 	attr->sample_type	|= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
403 
404 	if (freq) {
405 		attr->sample_type	|= PERF_SAMPLE_PERIOD;
406 		attr->freq		= 1;
407 		attr->sample_freq	= freq;
408 	}
409 
410 	if (no_samples)
411 		attr->sample_freq = 0;
412 
413 	if (inherit_stat)
414 		attr->inherit_stat = 1;
415 
416 	if (sample_address)
417 		attr->sample_type	|= PERF_SAMPLE_ADDR;
418 
419 	if (call_graph)
420 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
421 
422 	if (raw_samples)
423 		attr->sample_type	|= PERF_SAMPLE_RAW;
424 
425 	attr->mmap		= track;
426 	attr->comm		= track;
427 	attr->inherit		= (cpu < 0) && inherit;
428 	attr->disabled		= 1;
429 
430 try_again:
431 	fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
432 
433 	if (fd[nr_cpu][counter] < 0) {
434 		int err = errno;
435 
436 		if (err == EPERM)
437 			die("Permission error - are you root?\n");
438 		else if (err ==  ENODEV && profile_cpu != -1)
439 			die("No such device - did you specify an out-of-range profile CPU?\n");
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
447 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
448 
449 			if (verbose)
450 				warning(" ... trying to fall back to cpu-clock-ticks\n");
451 			attr->type = PERF_TYPE_SOFTWARE;
452 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
453 			goto try_again;
454 		}
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");
459 		exit(-1);
460 	}
461 
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 
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 	}
475 
476 	perf_header_attr__add_id(h_attr, read_data.id);
477 
478 	assert(fd[nr_cpu][counter] >= 0);
479 	fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
480 
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,
495 			PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
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 	}
500 
501 	ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
502 }
503 
504 static void open_counters(int cpu, pid_t pid)
505 {
506 	int counter;
507 
508 	group_fd = -1;
509 	for (counter = 0; counter < nr_counters; counter++)
510 		create_counter(counter, cpu, pid);
511 
512 	nr_cpu++;
513 }
514 
515 static void atexit_header(void)
516 {
517 	header->data_size += bytes_written;
518 
519 	perf_header__write(header, output);
520 }
521 
522 static int __cmd_record(int argc, const char **argv)
523 {
524 	int i, counter;
525 	struct stat st;
526 	pid_t pid = 0;
527 	int flags;
528 	int ret;
529 
530 	page_size = sysconf(_SC_PAGE_SIZE);
531 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
532 	assert(nr_cpus <= MAX_NR_CPUS);
533 	assert(nr_cpus >= 0);
534 
535 	atexit(sig_atexit);
536 	signal(SIGCHLD, sig_handler);
537 	signal(SIGINT, sig_handler);
538 
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;
547 	}
548 
549 	flags = O_CREAT|O_RDWR;
550 	if (append_file)
551 		file_new = 0;
552 	else
553 		flags |= O_TRUNC;
554 
555 	output = open(output_name, flags, S_IRUSR|S_IWUSR);
556 	if (output < 0) {
557 		perror("failed to create output file");
558 		exit(-1);
559 	}
560 
561 	if (!file_new)
562 		header = perf_header__read(output);
563 	else
564 		header = perf_header__new();
565 
566 	atexit(atexit_header);
567 
568 	if (!system_wide) {
569 		pid = target_pid;
570 		if (pid == -1)
571 			pid = getpid();
572 
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 	}
582 
583 	if (file_new)
584 		perf_header__write(header, output);
585 
586 	if (!system_wide) {
587 		pid_t tgid = pid_synthesize_comm_event(pid, 0);
588 		pid_synthesize_mmap_samples(pid, tgid);
589 	} else
590 		synthesize_all();
591 
592 	if (target_pid == -1 && argc) {
593 		pid = fork();
594 		if (pid < 0)
595 			perror("failed to fork");
596 
597 		if (!pid) {
598 			if (execvp(argv[0], (char **)argv)) {
599 				perror(argv[0]);
600 				exit(-1);
601 			}
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, &param)) {
610 			printf("Could not set realtime priority.\n");
611 			exit(-1);
612 		}
613 	}
614 
615 	for (;;) {
616 		int hits = samples;
617 
618 		for (i = 0; i < nr_cpu; i++) {
619 			for (counter = 0; counter < nr_counters; counter++)
620 				mmap_read(&mmap_array[i][counter]);
621 		}
622 
623 		if (hits == samples) {
624 			if (done)
625 				break;
626 			ret = poll(event_array, nr_poll, 100);
627 		}
628 	}
629 
630 	/*
631 	 * Approximate RIP event size: 24 bytes.
632 	 */
633 	fprintf(stderr,
634 		"[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
635 		(double)bytes_written / 1024.0 / 1024.0,
636 		output_name,
637 		bytes_written / 24);
638 
639 	return 0;
640 }
641 
642 static const char * const record_usage[] = {
643 	"perf record [<options>] [<command>]",
644 	"perf record [<options>] -- <command> [<options>]",
645 	NULL
646 };
647 
648 static const struct option options[] = {
649 	OPT_CALLBACK('e', "event", NULL, "event",
650 		     "event selector. use 'perf list' to list available events",
651 		     parse_events),
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"),
656 	OPT_BOOLEAN('R', "raw-samples", &raw_samples,
657 		    "collect raw sample records from all opened counters"),
658 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
659 			    "system-wide collection from all CPUs"),
660 	OPT_BOOLEAN('A', "append", &append_file,
661 			    "append to the output file to do incremental profiling"),
662 	OPT_INTEGER('C', "profile_cpu", &profile_cpu,
663 			    "CPU to profile on"),
664 	OPT_BOOLEAN('f', "force", &force,
665 			"overwrite existing data file"),
666 	OPT_LONG('c', "count", &default_interval,
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"),
672 	OPT_INTEGER('F', "freq", &freq,
673 		    "profile at this frequency"),
674 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
675 		    "number of mmap data pages"),
676 	OPT_BOOLEAN('g', "call-graph", &call_graph,
677 		    "do call-graph (stack chain/backtrace) recording"),
678 	OPT_BOOLEAN('v', "verbose", &verbose,
679 		    "be more verbose (show counter open errors, etc)"),
680 	OPT_BOOLEAN('s', "stat", &inherit_stat,
681 		    "per thread counts"),
682 	OPT_BOOLEAN('d', "data", &sample_address,
683 		    "Sample addresses"),
684 	OPT_BOOLEAN('n', "no-samples", &no_samples,
685 		    "don't sample"),
686 	OPT_END()
687 };
688 
689 int cmd_record(int argc, const char **argv, const char *prefix __used)
690 {
691 	int counter;
692 
693 	argc = parse_options(argc, argv, options, record_usage,
694 		PARSE_OPT_STOP_AT_NON_OPTION);
695 	if (!argc && target_pid == -1 && !system_wide)
696 		usage_with_options(record_usage, options);
697 
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 	}
703 
704 	for (counter = 0; counter < nr_counters; counter++) {
705 		if (attrs[counter].sample_period)
706 			continue;
707 
708 		attrs[counter].sample_period = default_interval;
709 	}
710 
711 	return __cmd_record(argc, argv);
712 }
713