xref: /openbmc/linux/tools/perf/builtin-record.c (revision 03456a158d9067d2f657bec170506009db81756d)
186470930SIngo Molnar /*
286470930SIngo Molnar  * builtin-record.c
386470930SIngo Molnar  *
486470930SIngo Molnar  * Builtin record command: Record the profile of a workload
586470930SIngo Molnar  * (or a CPU, or a PID) into the perf.data output file - for
686470930SIngo Molnar  * later analysis via perf report.
786470930SIngo Molnar  */
886470930SIngo Molnar #include "builtin.h"
986470930SIngo Molnar 
1086470930SIngo Molnar #include "perf.h"
1186470930SIngo Molnar 
1286470930SIngo Molnar #include "util/util.h"
1386470930SIngo Molnar #include "util/parse-options.h"
1486470930SIngo Molnar #include "util/parse-events.h"
1586470930SIngo Molnar #include "util/string.h"
1686470930SIngo Molnar 
177c6a1c65SPeter Zijlstra #include "util/header.h"
1866e274f3SFrederic Weisbecker #include "util/event.h"
198f28827aSFrederic Weisbecker #include "util/debug.h"
207c6a1c65SPeter Zijlstra 
2186470930SIngo Molnar #include <unistd.h>
2286470930SIngo Molnar #include <sched.h>
2386470930SIngo Molnar 
2486470930SIngo Molnar #define ALIGN(x, a)		__ALIGN_MASK(x, (typeof(x))(a)-1)
2586470930SIngo Molnar #define __ALIGN_MASK(x, mask)	(((x)+(mask))&~(mask))
2686470930SIngo Molnar 
2786470930SIngo Molnar static int			fd[MAX_NR_CPUS][MAX_COUNTERS];
2886470930SIngo Molnar 
2986470930SIngo Molnar static long			default_interval		= 100000;
3086470930SIngo Molnar 
3186470930SIngo Molnar static int			nr_cpus				=      0;
3286470930SIngo Molnar static unsigned int		page_size;
3386470930SIngo Molnar static unsigned int		mmap_pages			=    128;
3442e59d7dSIngo Molnar static int			freq				=   1000;
3586470930SIngo Molnar static int			output;
3686470930SIngo Molnar static const char		*output_name			= "perf.data";
3786470930SIngo Molnar static int			group				=      0;
3886470930SIngo Molnar static unsigned int		realtime_prio			=      0;
39daac07b2SFrederic Weisbecker static int			raw_samples			=      0;
4086470930SIngo Molnar static int			system_wide			=      0;
410a5ac846SJens Axboe static int			profile_cpu			=     -1;
4286470930SIngo Molnar static pid_t			target_pid			=     -1;
43933da83aSChris Wilson static pid_t			child_pid			=     -1;
4486470930SIngo Molnar static int			inherit				=      1;
4586470930SIngo Molnar static int			force				=      0;
4686470930SIngo Molnar static int			append_file			=      0;
473efa1cc9SIngo Molnar static int			call_graph			=      0;
48649c48a9SPeter Zijlstra static int			inherit_stat			=      0;
49649c48a9SPeter Zijlstra static int			no_samples			=      0;
504bba828dSAnton Blanchard static int			sample_address			=      0;
51d1302522SFrederic Weisbecker static int			multiplex			=      0;
52ea57c4f5SIngo Molnar static int			multiplex_fd			=     -1;
5386470930SIngo Molnar 
5442e59d7dSIngo Molnar static long			samples				=      0;
5586470930SIngo Molnar static struct timeval		last_read;
5686470930SIngo Molnar static struct timeval		this_read;
5786470930SIngo Molnar 
5842e59d7dSIngo Molnar static u64			bytes_written			=      0;
5986470930SIngo Molnar 
6086470930SIngo Molnar static struct pollfd		event_array[MAX_NR_CPUS * MAX_COUNTERS];
6186470930SIngo Molnar 
6242e59d7dSIngo Molnar static int			nr_poll				=      0;
6342e59d7dSIngo Molnar static int			nr_cpu				=      0;
6486470930SIngo Molnar 
65f5970550SPeter Zijlstra static int			file_new			=      1;
667c6a1c65SPeter Zijlstra 
6742e59d7dSIngo Molnar struct perf_header		*header				=   NULL;
68f5970550SPeter Zijlstra 
6986470930SIngo Molnar struct mmap_data {
7086470930SIngo Molnar 	int			counter;
7186470930SIngo Molnar 	void			*base;
7286470930SIngo Molnar 	unsigned int		mask;
7386470930SIngo Molnar 	unsigned int		prev;
7486470930SIngo Molnar };
7586470930SIngo Molnar 
7686470930SIngo Molnar static struct mmap_data		mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
7786470930SIngo Molnar 
789d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md)
7986470930SIngo Molnar {
80cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
819d91a6f7SPeter Zijlstra 	long head;
8286470930SIngo Molnar 
8386470930SIngo Molnar 	head = pc->data_head;
8486470930SIngo Molnar 	rmb();
8586470930SIngo Molnar 
8686470930SIngo Molnar 	return head;
8786470930SIngo Molnar }
8886470930SIngo Molnar 
899d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
909d91a6f7SPeter Zijlstra {
91cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
929d91a6f7SPeter Zijlstra 
939d91a6f7SPeter Zijlstra 	/*
949d91a6f7SPeter Zijlstra 	 * ensure all reads are done before we write the tail out.
959d91a6f7SPeter Zijlstra 	 */
969d91a6f7SPeter Zijlstra 	/* mb(); */
979d91a6f7SPeter Zijlstra 	pc->data_tail = tail;
989d91a6f7SPeter Zijlstra }
999d91a6f7SPeter Zijlstra 
100f5970550SPeter Zijlstra static void write_output(void *buf, size_t size)
101f5970550SPeter Zijlstra {
102f5970550SPeter Zijlstra 	while (size) {
103f5970550SPeter Zijlstra 		int ret = write(output, buf, size);
104f5970550SPeter Zijlstra 
105f5970550SPeter Zijlstra 		if (ret < 0)
106f5970550SPeter Zijlstra 			die("failed to write");
107f5970550SPeter Zijlstra 
108f5970550SPeter Zijlstra 		size -= ret;
109f5970550SPeter Zijlstra 		buf += ret;
110f5970550SPeter Zijlstra 
111f5970550SPeter Zijlstra 		bytes_written += ret;
112f5970550SPeter Zijlstra 	}
113f5970550SPeter Zijlstra }
114f5970550SPeter Zijlstra 
11586470930SIngo Molnar static void mmap_read(struct mmap_data *md)
11686470930SIngo Molnar {
11786470930SIngo Molnar 	unsigned int head = mmap_read_head(md);
11886470930SIngo Molnar 	unsigned int old = md->prev;
11986470930SIngo Molnar 	unsigned char *data = md->base + page_size;
12086470930SIngo Molnar 	unsigned long size;
12186470930SIngo Molnar 	void *buf;
12286470930SIngo Molnar 	int diff;
12386470930SIngo Molnar 
12486470930SIngo Molnar 	gettimeofday(&this_read, NULL);
12586470930SIngo Molnar 
12686470930SIngo Molnar 	/*
12786470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
12886470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
12986470930SIngo Molnar 	 *
13086470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
13186470930SIngo Molnar 	 *
13286470930SIngo Molnar 	 * In either case, truncate and restart at head.
13386470930SIngo Molnar 	 */
13486470930SIngo Molnar 	diff = head - old;
1359d91a6f7SPeter Zijlstra 	if (diff < 0) {
13686470930SIngo Molnar 		struct timeval iv;
13786470930SIngo Molnar 		unsigned long msecs;
13886470930SIngo Molnar 
13986470930SIngo Molnar 		timersub(&this_read, &last_read, &iv);
14086470930SIngo Molnar 		msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
14186470930SIngo Molnar 
14286470930SIngo Molnar 		fprintf(stderr, "WARNING: failed to keep up with mmap data."
14386470930SIngo Molnar 				"  Last read %lu msecs ago.\n", msecs);
14486470930SIngo Molnar 
14586470930SIngo Molnar 		/*
14686470930SIngo Molnar 		 * head points to a known good entry, start there.
14786470930SIngo Molnar 		 */
14886470930SIngo Molnar 		old = head;
14986470930SIngo Molnar 	}
15086470930SIngo Molnar 
15186470930SIngo Molnar 	last_read = this_read;
15286470930SIngo Molnar 
15386470930SIngo Molnar 	if (old != head)
15486470930SIngo Molnar 		samples++;
15586470930SIngo Molnar 
15686470930SIngo Molnar 	size = head - old;
15786470930SIngo Molnar 
15886470930SIngo Molnar 	if ((old & md->mask) + size != (head & md->mask)) {
15986470930SIngo Molnar 		buf = &data[old & md->mask];
16086470930SIngo Molnar 		size = md->mask + 1 - (old & md->mask);
16186470930SIngo Molnar 		old += size;
16286470930SIngo Molnar 
163f5970550SPeter Zijlstra 		write_output(buf, size);
16486470930SIngo Molnar 	}
16586470930SIngo Molnar 
16686470930SIngo Molnar 	buf = &data[old & md->mask];
16786470930SIngo Molnar 	size = head - old;
16886470930SIngo Molnar 	old += size;
16986470930SIngo Molnar 
170f5970550SPeter Zijlstra 	write_output(buf, size);
17186470930SIngo Molnar 
17286470930SIngo Molnar 	md->prev = old;
1739d91a6f7SPeter Zijlstra 	mmap_write_tail(md, old);
17486470930SIngo Molnar }
17586470930SIngo Molnar 
17686470930SIngo Molnar static volatile int done = 0;
177f7b7c26eSPeter Zijlstra static volatile int signr = -1;
17886470930SIngo Molnar 
17986470930SIngo Molnar static void sig_handler(int sig)
18086470930SIngo Molnar {
18186470930SIngo Molnar 	done = 1;
182f7b7c26eSPeter Zijlstra 	signr = sig;
183f7b7c26eSPeter Zijlstra }
184f7b7c26eSPeter Zijlstra 
185f7b7c26eSPeter Zijlstra static void sig_atexit(void)
186f7b7c26eSPeter Zijlstra {
187933da83aSChris Wilson 	if (child_pid != -1)
188933da83aSChris Wilson 		kill(child_pid, SIGTERM);
189933da83aSChris Wilson 
190f7b7c26eSPeter Zijlstra 	if (signr == -1)
191f7b7c26eSPeter Zijlstra 		return;
192f7b7c26eSPeter Zijlstra 
193f7b7c26eSPeter Zijlstra 	signal(signr, SIG_DFL);
194f7b7c26eSPeter Zijlstra 	kill(getpid(), signr);
19586470930SIngo Molnar }
19686470930SIngo Molnar 
1972a8083f0SArnaldo Carvalho de Melo static pid_t pid_synthesize_comm_event(pid_t pid, int full)
19886470930SIngo Molnar {
19986470930SIngo Molnar 	struct comm_event comm_ev;
20086470930SIngo Molnar 	char filename[PATH_MAX];
20186470930SIngo Molnar 	char bf[BUFSIZ];
2022a8083f0SArnaldo Carvalho de Melo 	FILE *fp;
2032a8083f0SArnaldo Carvalho de Melo 	size_t size = 0;
20486470930SIngo Molnar 	DIR *tasks;
20586470930SIngo Molnar 	struct dirent dirent, *next;
2062a8083f0SArnaldo Carvalho de Melo 	pid_t tgid = 0;
20786470930SIngo Molnar 
2082a8083f0SArnaldo Carvalho de Melo 	snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
20986470930SIngo Molnar 
2102a8083f0SArnaldo Carvalho de Melo 	fp = fopen(filename, "r");
21139e6dd73SArnaldo Carvalho de Melo 	if (fp == NULL) {
212613d8602SIngo Molnar 		/*
213613d8602SIngo Molnar 		 * We raced with a task exiting - just return:
214613d8602SIngo Molnar 		 */
215613d8602SIngo Molnar 		if (verbose)
21686470930SIngo Molnar 			fprintf(stderr, "couldn't open %s\n", filename);
2172a8083f0SArnaldo Carvalho de Melo 		return 0;
21886470930SIngo Molnar 	}
21986470930SIngo Molnar 
22086470930SIngo Molnar 	memset(&comm_ev, 0, sizeof(comm_ev));
2212a8083f0SArnaldo Carvalho de Melo 	while (!comm_ev.comm[0] || !comm_ev.pid) {
2222a8083f0SArnaldo Carvalho de Melo 		if (fgets(bf, sizeof(bf), fp) == NULL)
22386470930SIngo Molnar 			goto out_failure;
22486470930SIngo Molnar 
2252a8083f0SArnaldo Carvalho de Melo 		if (memcmp(bf, "Name:", 5) == 0) {
2262a8083f0SArnaldo Carvalho de Melo 			char *name = bf + 5;
2272a8083f0SArnaldo Carvalho de Melo 			while (*name && isspace(*name))
2282a8083f0SArnaldo Carvalho de Melo 				++name;
2292a8083f0SArnaldo Carvalho de Melo 			size = strlen(name) - 1;
2302a8083f0SArnaldo Carvalho de Melo 			memcpy(comm_ev.comm, name, size++);
2312a8083f0SArnaldo Carvalho de Melo 		} else if (memcmp(bf, "Tgid:", 5) == 0) {
2322a8083f0SArnaldo Carvalho de Melo 			char *tgids = bf + 5;
2332a8083f0SArnaldo Carvalho de Melo 			while (*tgids && isspace(*tgids))
2342a8083f0SArnaldo Carvalho de Melo 				++tgids;
2352a8083f0SArnaldo Carvalho de Melo 			tgid = comm_ev.pid = atoi(tgids);
2362a8083f0SArnaldo Carvalho de Melo 		}
2372a8083f0SArnaldo Carvalho de Melo 	}
2382a8083f0SArnaldo Carvalho de Melo 
239cdd6c482SIngo Molnar 	comm_ev.header.type = PERF_RECORD_COMM;
2409cffa8d5SPaul Mackerras 	size = ALIGN(size, sizeof(u64));
24186470930SIngo Molnar 	comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
24286470930SIngo Molnar 
24386470930SIngo Molnar 	if (!full) {
24486470930SIngo Molnar 		comm_ev.tid = pid;
24586470930SIngo Molnar 
246f5970550SPeter Zijlstra 		write_output(&comm_ev, comm_ev.header.size);
2472a8083f0SArnaldo Carvalho de Melo 		goto out_fclose;
24886470930SIngo Molnar 	}
24986470930SIngo Molnar 
25086470930SIngo Molnar 	snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
25186470930SIngo Molnar 
25286470930SIngo Molnar 	tasks = opendir(filename);
25386470930SIngo Molnar 	while (!readdir_r(tasks, &dirent, &next) && next) {
25486470930SIngo Molnar 		char *end;
25586470930SIngo Molnar 		pid = strtol(dirent.d_name, &end, 10);
25686470930SIngo Molnar 		if (*end)
25786470930SIngo Molnar 			continue;
25886470930SIngo Molnar 
25986470930SIngo Molnar 		comm_ev.tid = pid;
26086470930SIngo Molnar 
261f5970550SPeter Zijlstra 		write_output(&comm_ev, comm_ev.header.size);
26286470930SIngo Molnar 	}
26386470930SIngo Molnar 	closedir(tasks);
2642a8083f0SArnaldo Carvalho de Melo 
2652a8083f0SArnaldo Carvalho de Melo out_fclose:
2662a8083f0SArnaldo Carvalho de Melo 	fclose(fp);
2672a8083f0SArnaldo Carvalho de Melo 	return tgid;
26886470930SIngo Molnar 
26986470930SIngo Molnar out_failure:
27086470930SIngo Molnar 	fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
27186470930SIngo Molnar 		filename);
27286470930SIngo Molnar 	exit(EXIT_FAILURE);
27386470930SIngo Molnar }
27486470930SIngo Molnar 
2752a8083f0SArnaldo Carvalho de Melo static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
27686470930SIngo Molnar {
27786470930SIngo Molnar 	char filename[PATH_MAX];
27886470930SIngo Molnar 	FILE *fp;
27986470930SIngo Molnar 
28086470930SIngo Molnar 	snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
28186470930SIngo Molnar 
28286470930SIngo Molnar 	fp = fopen(filename, "r");
28386470930SIngo Molnar 	if (fp == NULL) {
284613d8602SIngo Molnar 		/*
285613d8602SIngo Molnar 		 * We raced with a task exiting - just return:
286613d8602SIngo Molnar 		 */
287613d8602SIngo Molnar 		if (verbose)
28886470930SIngo Molnar 			fprintf(stderr, "couldn't open %s\n", filename);
289613d8602SIngo Molnar 		return;
29086470930SIngo Molnar 	}
29186470930SIngo Molnar 	while (1) {
29286470930SIngo Molnar 		char bf[BUFSIZ], *pbf = bf;
29386470930SIngo Molnar 		struct mmap_event mmap_ev = {
294cdd6c482SIngo Molnar 			.header = { .type = PERF_RECORD_MMAP },
29586470930SIngo Molnar 		};
29686470930SIngo Molnar 		int n;
29786470930SIngo Molnar 		size_t size;
29886470930SIngo Molnar 		if (fgets(bf, sizeof(bf), fp) == NULL)
29986470930SIngo Molnar 			break;
30086470930SIngo Molnar 
30186470930SIngo Molnar 		/* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
30286470930SIngo Molnar 		n = hex2u64(pbf, &mmap_ev.start);
30386470930SIngo Molnar 		if (n < 0)
30486470930SIngo Molnar 			continue;
30586470930SIngo Molnar 		pbf += n + 1;
30686470930SIngo Molnar 		n = hex2u64(pbf, &mmap_ev.len);
30786470930SIngo Molnar 		if (n < 0)
30886470930SIngo Molnar 			continue;
30986470930SIngo Molnar 		pbf += n + 3;
31086470930SIngo Molnar 		if (*pbf == 'x') { /* vm_exec */
31176c64c5eSJohannes Weiner 			char *execname = strchr(bf, '/');
31286470930SIngo Molnar 
31311b5f81eSAnton Blanchard 			/* Catch VDSO */
31411b5f81eSAnton Blanchard 			if (execname == NULL)
31511b5f81eSAnton Blanchard 				execname = strstr(bf, "[vdso]");
31611b5f81eSAnton Blanchard 
31776c64c5eSJohannes Weiner 			if (execname == NULL)
31886470930SIngo Molnar 				continue;
31986470930SIngo Molnar 
32086470930SIngo Molnar 			size = strlen(execname);
32186470930SIngo Molnar 			execname[size - 1] = '\0'; /* Remove \n */
32286470930SIngo Molnar 			memcpy(mmap_ev.filename, execname, size);
3239cffa8d5SPaul Mackerras 			size = ALIGN(size, sizeof(u64));
32486470930SIngo Molnar 			mmap_ev.len -= mmap_ev.start;
32586470930SIngo Molnar 			mmap_ev.header.size = (sizeof(mmap_ev) -
32686470930SIngo Molnar 					       (sizeof(mmap_ev.filename) - size));
3272a8083f0SArnaldo Carvalho de Melo 			mmap_ev.pid = tgid;
32886470930SIngo Molnar 			mmap_ev.tid = pid;
32986470930SIngo Molnar 
330f5970550SPeter Zijlstra 			write_output(&mmap_ev, mmap_ev.header.size);
33186470930SIngo Molnar 		}
33286470930SIngo Molnar 	}
33386470930SIngo Molnar 
33486470930SIngo Molnar 	fclose(fp);
33586470930SIngo Molnar }
33686470930SIngo Molnar 
3377c6a1c65SPeter Zijlstra static void synthesize_all(void)
33886470930SIngo Molnar {
33986470930SIngo Molnar 	DIR *proc;
34086470930SIngo Molnar 	struct dirent dirent, *next;
34186470930SIngo Molnar 
34286470930SIngo Molnar 	proc = opendir("/proc");
34386470930SIngo Molnar 
34486470930SIngo Molnar 	while (!readdir_r(proc, &dirent, &next) && next) {
34586470930SIngo Molnar 		char *end;
3462a8083f0SArnaldo Carvalho de Melo 		pid_t pid, tgid;
34786470930SIngo Molnar 
34886470930SIngo Molnar 		pid = strtol(dirent.d_name, &end, 10);
34986470930SIngo Molnar 		if (*end) /* only interested in proper numerical dirents */
35086470930SIngo Molnar 			continue;
35186470930SIngo Molnar 
3522a8083f0SArnaldo Carvalho de Melo 		tgid = pid_synthesize_comm_event(pid, 1);
3532a8083f0SArnaldo Carvalho de Melo 		pid_synthesize_mmap_samples(pid, tgid);
35486470930SIngo Molnar 	}
35586470930SIngo Molnar 
35686470930SIngo Molnar 	closedir(proc);
35786470930SIngo Molnar }
35886470930SIngo Molnar 
35986470930SIngo Molnar static int group_fd;
36086470930SIngo Molnar 
361cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
3627c6a1c65SPeter Zijlstra {
3637c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
3647c6a1c65SPeter Zijlstra 
3657c6a1c65SPeter Zijlstra 	if (nr < header->attrs) {
3667c6a1c65SPeter Zijlstra 		h_attr = header->attr[nr];
3677c6a1c65SPeter Zijlstra 	} else {
3687c6a1c65SPeter Zijlstra 		h_attr = perf_header_attr__new(a);
3697c6a1c65SPeter Zijlstra 		perf_header__add_attr(header, h_attr);
3707c6a1c65SPeter Zijlstra 	}
3717c6a1c65SPeter Zijlstra 
3727c6a1c65SPeter Zijlstra 	return h_attr;
3737c6a1c65SPeter Zijlstra }
3747c6a1c65SPeter Zijlstra 
37586470930SIngo Molnar static void create_counter(int counter, int cpu, pid_t pid)
37686470930SIngo Molnar {
377cdd6c482SIngo Molnar 	struct perf_event_attr *attr = attrs + counter;
3787c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
3797c6a1c65SPeter Zijlstra 	int track = !counter; /* only the first counter needs these */
3807c6a1c65SPeter Zijlstra 	struct {
3817c6a1c65SPeter Zijlstra 		u64 count;
3827c6a1c65SPeter Zijlstra 		u64 time_enabled;
3837c6a1c65SPeter Zijlstra 		u64 time_running;
3847c6a1c65SPeter Zijlstra 		u64 id;
3857c6a1c65SPeter Zijlstra 	} read_data;
3867c6a1c65SPeter Zijlstra 
3877c6a1c65SPeter Zijlstra 	attr->read_format	= PERF_FORMAT_TOTAL_TIME_ENABLED |
3887c6a1c65SPeter Zijlstra 				  PERF_FORMAT_TOTAL_TIME_RUNNING |
3897c6a1c65SPeter Zijlstra 				  PERF_FORMAT_ID;
39086470930SIngo Molnar 
3913a9f131fSFrederic Weisbecker 	attr->sample_type	|= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
3923efa1cc9SIngo Molnar 
39386470930SIngo Molnar 	if (freq) {
394ea1900e5SPeter Zijlstra 		attr->sample_type	|= PERF_SAMPLE_PERIOD;
39586470930SIngo Molnar 		attr->freq		= 1;
39686470930SIngo Molnar 		attr->sample_freq	= freq;
39786470930SIngo Molnar 	}
3983efa1cc9SIngo Molnar 
399649c48a9SPeter Zijlstra 	if (no_samples)
400649c48a9SPeter Zijlstra 		attr->sample_freq = 0;
401649c48a9SPeter Zijlstra 
402649c48a9SPeter Zijlstra 	if (inherit_stat)
403649c48a9SPeter Zijlstra 		attr->inherit_stat = 1;
404649c48a9SPeter Zijlstra 
4054bba828dSAnton Blanchard 	if (sample_address)
4064bba828dSAnton Blanchard 		attr->sample_type	|= PERF_SAMPLE_ADDR;
4074bba828dSAnton Blanchard 
4083efa1cc9SIngo Molnar 	if (call_graph)
4093efa1cc9SIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
4103efa1cc9SIngo Molnar 
411cd6feeeaSIngo Molnar 	if (raw_samples) {
4126ddf259dSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_TIME;
413daac07b2SFrederic Weisbecker 		attr->sample_type	|= PERF_SAMPLE_RAW;
414cd6feeeaSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CPU;
415cd6feeeaSIngo Molnar 	}
416f413cdb8SFrederic Weisbecker 
41786470930SIngo Molnar 	attr->mmap		= track;
41886470930SIngo Molnar 	attr->comm		= track;
41986470930SIngo Molnar 	attr->inherit		= (cpu < 0) && inherit;
4204502d77cSPeter Zijlstra 	attr->disabled		= 1;
42186470930SIngo Molnar 
4223da297a6SIngo Molnar try_again:
423cdd6c482SIngo Molnar 	fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
42486470930SIngo Molnar 
42586470930SIngo Molnar 	if (fd[nr_cpu][counter] < 0) {
42686470930SIngo Molnar 		int err = errno;
42786470930SIngo Molnar 
42886470930SIngo Molnar 		if (err == EPERM)
4293da297a6SIngo Molnar 			die("Permission error - are you root?\n");
4300a5ac846SJens Axboe 		else if (err ==  ENODEV && profile_cpu != -1)
4310a5ac846SJens Axboe 			die("No such device - did you specify an out-of-range profile CPU?\n");
4323da297a6SIngo Molnar 
4333da297a6SIngo Molnar 		/*
4343da297a6SIngo Molnar 		 * If it's cycles then fall back to hrtimer
4353da297a6SIngo Molnar 		 * based cpu-clock-tick sw counter, which
4363da297a6SIngo Molnar 		 * is always available even if no PMU support:
4373da297a6SIngo Molnar 		 */
4383da297a6SIngo Molnar 		if (attr->type == PERF_TYPE_HARDWARE
439f4dbfa8fSPeter Zijlstra 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
4403da297a6SIngo Molnar 
4413da297a6SIngo Molnar 			if (verbose)
4423da297a6SIngo Molnar 				warning(" ... trying to fall back to cpu-clock-ticks\n");
4433da297a6SIngo Molnar 			attr->type = PERF_TYPE_SOFTWARE;
444f4dbfa8fSPeter Zijlstra 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
4453da297a6SIngo Molnar 			goto try_again;
4463da297a6SIngo Molnar 		}
44730c806a0SIngo Molnar 		printf("\n");
44830c806a0SIngo Molnar 		error("perfcounter syscall returned with %d (%s)\n",
44930c806a0SIngo Molnar 			fd[nr_cpu][counter], strerror(err));
450cdd6c482SIngo Molnar 		die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
45186470930SIngo Molnar 		exit(-1);
45286470930SIngo Molnar 	}
4533da297a6SIngo Molnar 
4547c6a1c65SPeter Zijlstra 	h_attr = get_header_attr(attr, counter);
4557c6a1c65SPeter Zijlstra 
4567c6a1c65SPeter Zijlstra 	if (!file_new) {
4577c6a1c65SPeter Zijlstra 		if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
4587c6a1c65SPeter Zijlstra 			fprintf(stderr, "incompatible append\n");
4597c6a1c65SPeter Zijlstra 			exit(-1);
4607c6a1c65SPeter Zijlstra 		}
4617c6a1c65SPeter Zijlstra 	}
4627c6a1c65SPeter Zijlstra 
4633928ddbeSFrederic Weisbecker 	if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
4643928ddbeSFrederic Weisbecker 		perror("Unable to read perf file descriptor\n");
4653928ddbeSFrederic Weisbecker 		exit(-1);
4663928ddbeSFrederic Weisbecker 	}
4677c6a1c65SPeter Zijlstra 
4687c6a1c65SPeter Zijlstra 	perf_header_attr__add_id(h_attr, read_data.id);
4697c6a1c65SPeter Zijlstra 
47086470930SIngo Molnar 	assert(fd[nr_cpu][counter] >= 0);
47186470930SIngo Molnar 	fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
47286470930SIngo Molnar 
47386470930SIngo Molnar 	/*
47486470930SIngo Molnar 	 * First counter acts as the group leader:
47586470930SIngo Molnar 	 */
47686470930SIngo Molnar 	if (group && group_fd == -1)
47786470930SIngo Molnar 		group_fd = fd[nr_cpu][counter];
478ea57c4f5SIngo Molnar 	if (multiplex && multiplex_fd == -1)
479ea57c4f5SIngo Molnar 		multiplex_fd = fd[nr_cpu][counter];
48086470930SIngo Molnar 
481ea57c4f5SIngo Molnar 	if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
482ea57c4f5SIngo Molnar 		int ret;
483ea57c4f5SIngo Molnar 
484cdd6c482SIngo Molnar 		ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
485ea57c4f5SIngo Molnar 		assert(ret != -1);
486ea57c4f5SIngo Molnar 	} else {
48786470930SIngo Molnar 		event_array[nr_poll].fd = fd[nr_cpu][counter];
48886470930SIngo Molnar 		event_array[nr_poll].events = POLLIN;
48986470930SIngo Molnar 		nr_poll++;
49086470930SIngo Molnar 
49186470930SIngo Molnar 		mmap_array[nr_cpu][counter].counter = counter;
49286470930SIngo Molnar 		mmap_array[nr_cpu][counter].prev = 0;
49386470930SIngo Molnar 		mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
49486470930SIngo Molnar 		mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
4959d91a6f7SPeter Zijlstra 				PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
49686470930SIngo Molnar 		if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
49786470930SIngo Molnar 			error("failed to mmap with %d (%s)\n", errno, strerror(errno));
49886470930SIngo Molnar 			exit(-1);
49986470930SIngo Molnar 		}
500ea57c4f5SIngo Molnar 	}
501d1302522SFrederic Weisbecker 
502cdd6c482SIngo Molnar 	ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
50386470930SIngo Molnar }
50486470930SIngo Molnar 
50586470930SIngo Molnar static void open_counters(int cpu, pid_t pid)
50686470930SIngo Molnar {
50786470930SIngo Molnar 	int counter;
50886470930SIngo Molnar 
50986470930SIngo Molnar 	group_fd = -1;
51086470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++)
51186470930SIngo Molnar 		create_counter(counter, cpu, pid);
51286470930SIngo Molnar 
51386470930SIngo Molnar 	nr_cpu++;
51486470930SIngo Molnar }
51586470930SIngo Molnar 
516f5970550SPeter Zijlstra static void atexit_header(void)
517f5970550SPeter Zijlstra {
5187c6a1c65SPeter Zijlstra 	header->data_size += bytes_written;
519f5970550SPeter Zijlstra 
5207c6a1c65SPeter Zijlstra 	perf_header__write(header, output);
521f5970550SPeter Zijlstra }
522f5970550SPeter Zijlstra 
52386470930SIngo Molnar static int __cmd_record(int argc, const char **argv)
52486470930SIngo Molnar {
52586470930SIngo Molnar 	int i, counter;
52686470930SIngo Molnar 	struct stat st;
5277c6a1c65SPeter Zijlstra 	pid_t pid = 0;
52886470930SIngo Molnar 	int flags;
52986470930SIngo Molnar 	int ret;
5308b412664SPeter Zijlstra 	unsigned long waking = 0;
53186470930SIngo Molnar 
53286470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
53386470930SIngo Molnar 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
53486470930SIngo Molnar 	assert(nr_cpus <= MAX_NR_CPUS);
53586470930SIngo Molnar 	assert(nr_cpus >= 0);
53686470930SIngo Molnar 
537f5970550SPeter Zijlstra 	atexit(sig_atexit);
538f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
539f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
540f5970550SPeter Zijlstra 
541266e0e21SPierre Habouzit 	if (!stat(output_name, &st) && st.st_size) {
542266e0e21SPierre Habouzit 		if (!force && !append_file) {
54386470930SIngo Molnar 			fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
54486470930SIngo Molnar 					output_name);
54586470930SIngo Molnar 			exit(-1);
54686470930SIngo Molnar 		}
547266e0e21SPierre Habouzit 	} else {
548266e0e21SPierre Habouzit 		append_file = 0;
549266e0e21SPierre Habouzit 	}
55086470930SIngo Molnar 
55186470930SIngo Molnar 	flags = O_CREAT|O_RDWR;
55286470930SIngo Molnar 	if (append_file)
553f5970550SPeter Zijlstra 		file_new = 0;
55486470930SIngo Molnar 	else
55586470930SIngo Molnar 		flags |= O_TRUNC;
55686470930SIngo Molnar 
55786470930SIngo Molnar 	output = open(output_name, flags, S_IRUSR|S_IWUSR);
55886470930SIngo Molnar 	if (output < 0) {
55986470930SIngo Molnar 		perror("failed to create output file");
56086470930SIngo Molnar 		exit(-1);
56186470930SIngo Molnar 	}
56286470930SIngo Molnar 
5637c6a1c65SPeter Zijlstra 	if (!file_new)
5647c6a1c65SPeter Zijlstra 		header = perf_header__read(output);
5657c6a1c65SPeter Zijlstra 	else
5667c6a1c65SPeter Zijlstra 		header = perf_header__new();
567f5970550SPeter Zijlstra 
5689df37dddSFrederic Weisbecker 	if (raw_samples) {
569*03456a15SFrederic Weisbecker 		perf_header__set_trace_info();
5709df37dddSFrederic Weisbecker 	} else {
5719df37dddSFrederic Weisbecker 		for (i = 0; i < nr_counters; i++) {
5729df37dddSFrederic Weisbecker 			if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
573*03456a15SFrederic Weisbecker 				perf_header__set_trace_info();
5749df37dddSFrederic Weisbecker 				break;
5759df37dddSFrederic Weisbecker 			}
5769df37dddSFrederic Weisbecker 		}
5779df37dddSFrederic Weisbecker 	}
578*03456a15SFrederic Weisbecker 
579f5970550SPeter Zijlstra 	atexit(atexit_header);
580f5970550SPeter Zijlstra 
58186470930SIngo Molnar 	if (!system_wide) {
5827c6a1c65SPeter Zijlstra 		pid = target_pid;
5837c6a1c65SPeter Zijlstra 		if (pid == -1)
5847c6a1c65SPeter Zijlstra 			pid = getpid();
5857c6a1c65SPeter Zijlstra 
5860a5ac846SJens Axboe 		open_counters(profile_cpu, pid);
5870a5ac846SJens Axboe 	} else {
5880a5ac846SJens Axboe 		if (profile_cpu != -1) {
5890a5ac846SJens Axboe 			open_counters(profile_cpu, target_pid);
5900a5ac846SJens Axboe 		} else {
5910a5ac846SJens Axboe 			for (i = 0; i < nr_cpus; i++)
59286470930SIngo Molnar 				open_counters(i, target_pid);
5930a5ac846SJens Axboe 		}
5940a5ac846SJens Axboe 	}
59586470930SIngo Molnar 
5967c6a1c65SPeter Zijlstra 	if (file_new)
5977c6a1c65SPeter Zijlstra 		perf_header__write(header, output);
5987c6a1c65SPeter Zijlstra 
5997c6a1c65SPeter Zijlstra 	if (!system_wide) {
6002a8083f0SArnaldo Carvalho de Melo 		pid_t tgid = pid_synthesize_comm_event(pid, 0);
6012a8083f0SArnaldo Carvalho de Melo 		pid_synthesize_mmap_samples(pid, tgid);
6027c6a1c65SPeter Zijlstra 	} else
6037c6a1c65SPeter Zijlstra 		synthesize_all();
6047c6a1c65SPeter Zijlstra 
60586470930SIngo Molnar 	if (target_pid == -1 && argc) {
60686470930SIngo Molnar 		pid = fork();
60786470930SIngo Molnar 		if (pid < 0)
60886470930SIngo Molnar 			perror("failed to fork");
60986470930SIngo Molnar 
61086470930SIngo Molnar 		if (!pid) {
61186470930SIngo Molnar 			if (execvp(argv[0], (char **)argv)) {
61286470930SIngo Molnar 				perror(argv[0]);
61386470930SIngo Molnar 				exit(-1);
61486470930SIngo Molnar 			}
61586470930SIngo Molnar 		}
616933da83aSChris Wilson 
617933da83aSChris Wilson 		child_pid = pid;
61886470930SIngo Molnar 	}
61986470930SIngo Molnar 
62086470930SIngo Molnar 	if (realtime_prio) {
62186470930SIngo Molnar 		struct sched_param param;
62286470930SIngo Molnar 
62386470930SIngo Molnar 		param.sched_priority = realtime_prio;
62486470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
62586470930SIngo Molnar 			printf("Could not set realtime priority.\n");
62686470930SIngo Molnar 			exit(-1);
62786470930SIngo Molnar 		}
62886470930SIngo Molnar 	}
62986470930SIngo Molnar 
630649c48a9SPeter Zijlstra 	for (;;) {
63186470930SIngo Molnar 		int hits = samples;
63286470930SIngo Molnar 
63386470930SIngo Molnar 		for (i = 0; i < nr_cpu; i++) {
634ea57c4f5SIngo Molnar 			for (counter = 0; counter < nr_counters; counter++) {
635ea57c4f5SIngo Molnar 				if (mmap_array[i][counter].base)
63686470930SIngo Molnar 					mmap_read(&mmap_array[i][counter]);
63786470930SIngo Molnar 			}
638ea57c4f5SIngo Molnar 		}
63986470930SIngo Molnar 
640649c48a9SPeter Zijlstra 		if (hits == samples) {
641649c48a9SPeter Zijlstra 			if (done)
642649c48a9SPeter Zijlstra 				break;
6438b412664SPeter Zijlstra 			ret = poll(event_array, nr_poll, -1);
6448b412664SPeter Zijlstra 			waking++;
6458b412664SPeter Zijlstra 		}
6468b412664SPeter Zijlstra 
6478b412664SPeter Zijlstra 		if (done) {
6488b412664SPeter Zijlstra 			for (i = 0; i < nr_cpu; i++) {
6498b412664SPeter Zijlstra 				for (counter = 0; counter < nr_counters; counter++)
650cdd6c482SIngo Molnar 					ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
65186470930SIngo Molnar 			}
652649c48a9SPeter Zijlstra 		}
6538b412664SPeter Zijlstra 	}
6548b412664SPeter Zijlstra 
6558b412664SPeter Zijlstra 	fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
65686470930SIngo Molnar 
65786470930SIngo Molnar 	/*
65886470930SIngo Molnar 	 * Approximate RIP event size: 24 bytes.
65986470930SIngo Molnar 	 */
66086470930SIngo Molnar 	fprintf(stderr,
66186470930SIngo Molnar 		"[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
66286470930SIngo Molnar 		(double)bytes_written / 1024.0 / 1024.0,
66386470930SIngo Molnar 		output_name,
66486470930SIngo Molnar 		bytes_written / 24);
66586470930SIngo Molnar 
66686470930SIngo Molnar 	return 0;
66786470930SIngo Molnar }
66886470930SIngo Molnar 
66986470930SIngo Molnar static const char * const record_usage[] = {
67086470930SIngo Molnar 	"perf record [<options>] [<command>]",
67186470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
67286470930SIngo Molnar 	NULL
67386470930SIngo Molnar };
67486470930SIngo Molnar 
67586470930SIngo Molnar static const struct option options[] = {
67686470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
67786470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
67886470930SIngo Molnar 		     parse_events),
67986470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
68086470930SIngo Molnar 		    "record events on existing pid"),
68186470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
68286470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
683daac07b2SFrederic Weisbecker 	OPT_BOOLEAN('R', "raw-samples", &raw_samples,
684daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
68586470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
68686470930SIngo Molnar 			    "system-wide collection from all CPUs"),
68786470930SIngo Molnar 	OPT_BOOLEAN('A', "append", &append_file,
68886470930SIngo Molnar 			    "append to the output file to do incremental profiling"),
6890a5ac846SJens Axboe 	OPT_INTEGER('C', "profile_cpu", &profile_cpu,
6900a5ac846SJens Axboe 			    "CPU to profile on"),
69186470930SIngo Molnar 	OPT_BOOLEAN('f', "force", &force,
69286470930SIngo Molnar 			"overwrite existing data file"),
69386470930SIngo Molnar 	OPT_LONG('c', "count", &default_interval,
69486470930SIngo Molnar 		    "event period to sample"),
69586470930SIngo Molnar 	OPT_STRING('o', "output", &output_name, "file",
69686470930SIngo Molnar 		    "output file name"),
69786470930SIngo Molnar 	OPT_BOOLEAN('i', "inherit", &inherit,
69886470930SIngo Molnar 		    "child tasks inherit counters"),
69986470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
70086470930SIngo Molnar 		    "profile at this frequency"),
70186470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
70286470930SIngo Molnar 		    "number of mmap data pages"),
7033efa1cc9SIngo Molnar 	OPT_BOOLEAN('g', "call-graph", &call_graph,
7043efa1cc9SIngo Molnar 		    "do call-graph (stack chain/backtrace) recording"),
7053da297a6SIngo Molnar 	OPT_BOOLEAN('v', "verbose", &verbose,
7063da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
707649c48a9SPeter Zijlstra 	OPT_BOOLEAN('s', "stat", &inherit_stat,
708649c48a9SPeter Zijlstra 		    "per thread counts"),
7094bba828dSAnton Blanchard 	OPT_BOOLEAN('d', "data", &sample_address,
7104bba828dSAnton Blanchard 		    "Sample addresses"),
711649c48a9SPeter Zijlstra 	OPT_BOOLEAN('n', "no-samples", &no_samples,
712649c48a9SPeter Zijlstra 		    "don't sample"),
713d1302522SFrederic Weisbecker 	OPT_BOOLEAN('M', "multiplex", &multiplex,
714d1302522SFrederic Weisbecker 		    "multiplex counter output in a single channel"),
71586470930SIngo Molnar 	OPT_END()
71686470930SIngo Molnar };
71786470930SIngo Molnar 
718f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used)
71986470930SIngo Molnar {
72086470930SIngo Molnar 	int counter;
72186470930SIngo Molnar 
722a0541234SAnton Blanchard 	argc = parse_options(argc, argv, options, record_usage,
723a0541234SAnton Blanchard 		PARSE_OPT_STOP_AT_NON_OPTION);
72486470930SIngo Molnar 	if (!argc && target_pid == -1 && !system_wide)
72586470930SIngo Molnar 		usage_with_options(record_usage, options);
72686470930SIngo Molnar 
727bbd36e5eSPeter Zijlstra 	if (!nr_counters) {
72886470930SIngo Molnar 		nr_counters	= 1;
729bbd36e5eSPeter Zijlstra 		attrs[0].type	= PERF_TYPE_HARDWARE;
730bbd36e5eSPeter Zijlstra 		attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
731bbd36e5eSPeter Zijlstra 	}
73286470930SIngo Molnar 
73386470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
73486470930SIngo Molnar 		if (attrs[counter].sample_period)
73586470930SIngo Molnar 			continue;
73686470930SIngo Molnar 
73786470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
73886470930SIngo Molnar 	}
73986470930SIngo Molnar 
74086470930SIngo Molnar 	return __cmd_record(argc, argv);
74186470930SIngo Molnar }
742