xref: /openbmc/linux/tools/perf/builtin-record.c (revision 42e59d7d19dc4b49feab2a860fd9a8ca3248c833)
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"
205f9c39dcSFrederic Weisbecker #include "util/trace-event.h"
217c6a1c65SPeter Zijlstra 
2286470930SIngo Molnar #include <unistd.h>
2386470930SIngo Molnar #include <sched.h>
2486470930SIngo Molnar 
2586470930SIngo Molnar #define ALIGN(x, a)		__ALIGN_MASK(x, (typeof(x))(a)-1)
2686470930SIngo Molnar #define __ALIGN_MASK(x, mask)	(((x)+(mask))&~(mask))
2786470930SIngo Molnar 
2886470930SIngo Molnar static int			fd[MAX_NR_CPUS][MAX_COUNTERS];
2986470930SIngo Molnar 
3086470930SIngo Molnar static long			default_interval		= 100000;
3186470930SIngo Molnar 
3286470930SIngo Molnar static int			nr_cpus				=      0;
3386470930SIngo Molnar static unsigned int		page_size;
3486470930SIngo Molnar static unsigned int		mmap_pages			=    128;
35*42e59d7dSIngo Molnar static int			freq				=   1000;
3686470930SIngo Molnar static int			output;
3786470930SIngo Molnar static const char		*output_name			= "perf.data";
3886470930SIngo Molnar static int			group				=      0;
3986470930SIngo Molnar static unsigned int		realtime_prio			=      0;
40daac07b2SFrederic Weisbecker static int			raw_samples			=      0;
4186470930SIngo Molnar static int			system_wide			=      0;
420a5ac846SJens Axboe static int			profile_cpu			=     -1;
4386470930SIngo Molnar static pid_t			target_pid			=     -1;
44933da83aSChris Wilson static pid_t			child_pid			=     -1;
4586470930SIngo Molnar static int			inherit				=      1;
4686470930SIngo Molnar static int			force				=      0;
4786470930SIngo Molnar static int			append_file			=      0;
483efa1cc9SIngo Molnar static int			call_graph			=      0;
49649c48a9SPeter Zijlstra static int			inherit_stat			=      0;
50649c48a9SPeter Zijlstra static int			no_samples			=      0;
514bba828dSAnton Blanchard static int			sample_address			=      0;
52d1302522SFrederic Weisbecker static int			multiplex			=      0;
53ea57c4f5SIngo Molnar static int			multiplex_fd			=     -1;
5486470930SIngo Molnar 
55*42e59d7dSIngo Molnar static long			samples				=      0;
5686470930SIngo Molnar static struct timeval		last_read;
5786470930SIngo Molnar static struct timeval		this_read;
5886470930SIngo Molnar 
59*42e59d7dSIngo Molnar static u64			bytes_written			=      0;
6086470930SIngo Molnar 
6186470930SIngo Molnar static struct pollfd		event_array[MAX_NR_CPUS * MAX_COUNTERS];
6286470930SIngo Molnar 
63*42e59d7dSIngo Molnar static int			nr_poll				=      0;
64*42e59d7dSIngo Molnar static int			nr_cpu				=      0;
6586470930SIngo Molnar 
66f5970550SPeter Zijlstra static int			file_new			=      1;
677c6a1c65SPeter Zijlstra 
68*42e59d7dSIngo Molnar struct perf_header		*header				=   NULL;
69f5970550SPeter Zijlstra 
7086470930SIngo Molnar struct mmap_data {
7186470930SIngo Molnar 	int			counter;
7286470930SIngo Molnar 	void			*base;
7386470930SIngo Molnar 	unsigned int		mask;
7486470930SIngo Molnar 	unsigned int		prev;
7586470930SIngo Molnar };
7686470930SIngo Molnar 
7786470930SIngo Molnar static struct mmap_data		mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
7886470930SIngo Molnar 
799d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md)
8086470930SIngo Molnar {
81cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
829d91a6f7SPeter Zijlstra 	long head;
8386470930SIngo Molnar 
8486470930SIngo Molnar 	head = pc->data_head;
8586470930SIngo Molnar 	rmb();
8686470930SIngo Molnar 
8786470930SIngo Molnar 	return head;
8886470930SIngo Molnar }
8986470930SIngo Molnar 
909d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
919d91a6f7SPeter Zijlstra {
92cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
939d91a6f7SPeter Zijlstra 
949d91a6f7SPeter Zijlstra 	/*
959d91a6f7SPeter Zijlstra 	 * ensure all reads are done before we write the tail out.
969d91a6f7SPeter Zijlstra 	 */
979d91a6f7SPeter Zijlstra 	/* mb(); */
989d91a6f7SPeter Zijlstra 	pc->data_tail = tail;
999d91a6f7SPeter Zijlstra }
1009d91a6f7SPeter Zijlstra 
101f5970550SPeter Zijlstra static void write_output(void *buf, size_t size)
102f5970550SPeter Zijlstra {
103f5970550SPeter Zijlstra 	while (size) {
104f5970550SPeter Zijlstra 		int ret = write(output, buf, size);
105f5970550SPeter Zijlstra 
106f5970550SPeter Zijlstra 		if (ret < 0)
107f5970550SPeter Zijlstra 			die("failed to write");
108f5970550SPeter Zijlstra 
109f5970550SPeter Zijlstra 		size -= ret;
110f5970550SPeter Zijlstra 		buf += ret;
111f5970550SPeter Zijlstra 
112f5970550SPeter Zijlstra 		bytes_written += ret;
113f5970550SPeter Zijlstra 	}
114f5970550SPeter Zijlstra }
115f5970550SPeter Zijlstra 
11686470930SIngo Molnar static void mmap_read(struct mmap_data *md)
11786470930SIngo Molnar {
11886470930SIngo Molnar 	unsigned int head = mmap_read_head(md);
11986470930SIngo Molnar 	unsigned int old = md->prev;
12086470930SIngo Molnar 	unsigned char *data = md->base + page_size;
12186470930SIngo Molnar 	unsigned long size;
12286470930SIngo Molnar 	void *buf;
12386470930SIngo Molnar 	int diff;
12486470930SIngo Molnar 
12586470930SIngo Molnar 	gettimeofday(&this_read, NULL);
12686470930SIngo Molnar 
12786470930SIngo Molnar 	/*
12886470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
12986470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
13086470930SIngo Molnar 	 *
13186470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
13286470930SIngo Molnar 	 *
13386470930SIngo Molnar 	 * In either case, truncate and restart at head.
13486470930SIngo Molnar 	 */
13586470930SIngo Molnar 	diff = head - old;
1369d91a6f7SPeter Zijlstra 	if (diff < 0) {
13786470930SIngo Molnar 		struct timeval iv;
13886470930SIngo Molnar 		unsigned long msecs;
13986470930SIngo Molnar 
14086470930SIngo Molnar 		timersub(&this_read, &last_read, &iv);
14186470930SIngo Molnar 		msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
14286470930SIngo Molnar 
14386470930SIngo Molnar 		fprintf(stderr, "WARNING: failed to keep up with mmap data."
14486470930SIngo Molnar 				"  Last read %lu msecs ago.\n", msecs);
14586470930SIngo Molnar 
14686470930SIngo Molnar 		/*
14786470930SIngo Molnar 		 * head points to a known good entry, start there.
14886470930SIngo Molnar 		 */
14986470930SIngo Molnar 		old = head;
15086470930SIngo Molnar 	}
15186470930SIngo Molnar 
15286470930SIngo Molnar 	last_read = this_read;
15386470930SIngo Molnar 
15486470930SIngo Molnar 	if (old != head)
15586470930SIngo Molnar 		samples++;
15686470930SIngo Molnar 
15786470930SIngo Molnar 	size = head - old;
15886470930SIngo Molnar 
15986470930SIngo Molnar 	if ((old & md->mask) + size != (head & md->mask)) {
16086470930SIngo Molnar 		buf = &data[old & md->mask];
16186470930SIngo Molnar 		size = md->mask + 1 - (old & md->mask);
16286470930SIngo Molnar 		old += size;
16386470930SIngo Molnar 
164f5970550SPeter Zijlstra 		write_output(buf, size);
16586470930SIngo Molnar 	}
16686470930SIngo Molnar 
16786470930SIngo Molnar 	buf = &data[old & md->mask];
16886470930SIngo Molnar 	size = head - old;
16986470930SIngo Molnar 	old += size;
17086470930SIngo Molnar 
171f5970550SPeter Zijlstra 	write_output(buf, size);
17286470930SIngo Molnar 
17386470930SIngo Molnar 	md->prev = old;
1749d91a6f7SPeter Zijlstra 	mmap_write_tail(md, old);
17586470930SIngo Molnar }
17686470930SIngo Molnar 
17786470930SIngo Molnar static volatile int done = 0;
178f7b7c26eSPeter Zijlstra static volatile int signr = -1;
17986470930SIngo Molnar 
18086470930SIngo Molnar static void sig_handler(int sig)
18186470930SIngo Molnar {
18286470930SIngo Molnar 	done = 1;
183f7b7c26eSPeter Zijlstra 	signr = sig;
184f7b7c26eSPeter Zijlstra }
185f7b7c26eSPeter Zijlstra 
186f7b7c26eSPeter Zijlstra static void sig_atexit(void)
187f7b7c26eSPeter Zijlstra {
188933da83aSChris Wilson 	if (child_pid != -1)
189933da83aSChris Wilson 		kill(child_pid, SIGTERM);
190933da83aSChris Wilson 
191f7b7c26eSPeter Zijlstra 	if (signr == -1)
192f7b7c26eSPeter Zijlstra 		return;
193f7b7c26eSPeter Zijlstra 
194f7b7c26eSPeter Zijlstra 	signal(signr, SIG_DFL);
195f7b7c26eSPeter Zijlstra 	kill(getpid(), signr);
19686470930SIngo Molnar }
19786470930SIngo Molnar 
1982a8083f0SArnaldo Carvalho de Melo static pid_t pid_synthesize_comm_event(pid_t pid, int full)
19986470930SIngo Molnar {
20086470930SIngo Molnar 	struct comm_event comm_ev;
20186470930SIngo Molnar 	char filename[PATH_MAX];
20286470930SIngo Molnar 	char bf[BUFSIZ];
2032a8083f0SArnaldo Carvalho de Melo 	FILE *fp;
2042a8083f0SArnaldo Carvalho de Melo 	size_t size = 0;
20586470930SIngo Molnar 	DIR *tasks;
20686470930SIngo Molnar 	struct dirent dirent, *next;
2072a8083f0SArnaldo Carvalho de Melo 	pid_t tgid = 0;
20886470930SIngo Molnar 
2092a8083f0SArnaldo Carvalho de Melo 	snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
21086470930SIngo Molnar 
2112a8083f0SArnaldo Carvalho de Melo 	fp = fopen(filename, "r");
21239e6dd73SArnaldo Carvalho de Melo 	if (fp == NULL) {
213613d8602SIngo Molnar 		/*
214613d8602SIngo Molnar 		 * We raced with a task exiting - just return:
215613d8602SIngo Molnar 		 */
216613d8602SIngo Molnar 		if (verbose)
21786470930SIngo Molnar 			fprintf(stderr, "couldn't open %s\n", filename);
2182a8083f0SArnaldo Carvalho de Melo 		return 0;
21986470930SIngo Molnar 	}
22086470930SIngo Molnar 
22186470930SIngo Molnar 	memset(&comm_ev, 0, sizeof(comm_ev));
2222a8083f0SArnaldo Carvalho de Melo 	while (!comm_ev.comm[0] || !comm_ev.pid) {
2232a8083f0SArnaldo Carvalho de Melo 		if (fgets(bf, sizeof(bf), fp) == NULL)
22486470930SIngo Molnar 			goto out_failure;
22586470930SIngo Molnar 
2262a8083f0SArnaldo Carvalho de Melo 		if (memcmp(bf, "Name:", 5) == 0) {
2272a8083f0SArnaldo Carvalho de Melo 			char *name = bf + 5;
2282a8083f0SArnaldo Carvalho de Melo 			while (*name && isspace(*name))
2292a8083f0SArnaldo Carvalho de Melo 				++name;
2302a8083f0SArnaldo Carvalho de Melo 			size = strlen(name) - 1;
2312a8083f0SArnaldo Carvalho de Melo 			memcpy(comm_ev.comm, name, size++);
2322a8083f0SArnaldo Carvalho de Melo 		} else if (memcmp(bf, "Tgid:", 5) == 0) {
2332a8083f0SArnaldo Carvalho de Melo 			char *tgids = bf + 5;
2342a8083f0SArnaldo Carvalho de Melo 			while (*tgids && isspace(*tgids))
2352a8083f0SArnaldo Carvalho de Melo 				++tgids;
2362a8083f0SArnaldo Carvalho de Melo 			tgid = comm_ev.pid = atoi(tgids);
2372a8083f0SArnaldo Carvalho de Melo 		}
2382a8083f0SArnaldo Carvalho de Melo 	}
2392a8083f0SArnaldo Carvalho de Melo 
240cdd6c482SIngo Molnar 	comm_ev.header.type = PERF_RECORD_COMM;
2419cffa8d5SPaul Mackerras 	size = ALIGN(size, sizeof(u64));
24286470930SIngo Molnar 	comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
24386470930SIngo Molnar 
24486470930SIngo Molnar 	if (!full) {
24586470930SIngo Molnar 		comm_ev.tid = pid;
24686470930SIngo Molnar 
247f5970550SPeter Zijlstra 		write_output(&comm_ev, comm_ev.header.size);
2482a8083f0SArnaldo Carvalho de Melo 		goto out_fclose;
24986470930SIngo Molnar 	}
25086470930SIngo Molnar 
25186470930SIngo Molnar 	snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
25286470930SIngo Molnar 
25386470930SIngo Molnar 	tasks = opendir(filename);
25486470930SIngo Molnar 	while (!readdir_r(tasks, &dirent, &next) && next) {
25586470930SIngo Molnar 		char *end;
25686470930SIngo Molnar 		pid = strtol(dirent.d_name, &end, 10);
25786470930SIngo Molnar 		if (*end)
25886470930SIngo Molnar 			continue;
25986470930SIngo Molnar 
26086470930SIngo Molnar 		comm_ev.tid = pid;
26186470930SIngo Molnar 
262f5970550SPeter Zijlstra 		write_output(&comm_ev, comm_ev.header.size);
26386470930SIngo Molnar 	}
26486470930SIngo Molnar 	closedir(tasks);
2652a8083f0SArnaldo Carvalho de Melo 
2662a8083f0SArnaldo Carvalho de Melo out_fclose:
2672a8083f0SArnaldo Carvalho de Melo 	fclose(fp);
2682a8083f0SArnaldo Carvalho de Melo 	return tgid;
26986470930SIngo Molnar 
27086470930SIngo Molnar out_failure:
27186470930SIngo Molnar 	fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
27286470930SIngo Molnar 		filename);
27386470930SIngo Molnar 	exit(EXIT_FAILURE);
27486470930SIngo Molnar }
27586470930SIngo Molnar 
2762a8083f0SArnaldo Carvalho de Melo static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
27786470930SIngo Molnar {
27886470930SIngo Molnar 	char filename[PATH_MAX];
27986470930SIngo Molnar 	FILE *fp;
28086470930SIngo Molnar 
28186470930SIngo Molnar 	snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
28286470930SIngo Molnar 
28386470930SIngo Molnar 	fp = fopen(filename, "r");
28486470930SIngo Molnar 	if (fp == NULL) {
285613d8602SIngo Molnar 		/*
286613d8602SIngo Molnar 		 * We raced with a task exiting - just return:
287613d8602SIngo Molnar 		 */
288613d8602SIngo Molnar 		if (verbose)
28986470930SIngo Molnar 			fprintf(stderr, "couldn't open %s\n", filename);
290613d8602SIngo Molnar 		return;
29186470930SIngo Molnar 	}
29286470930SIngo Molnar 	while (1) {
29386470930SIngo Molnar 		char bf[BUFSIZ], *pbf = bf;
29486470930SIngo Molnar 		struct mmap_event mmap_ev = {
295cdd6c482SIngo Molnar 			.header = { .type = PERF_RECORD_MMAP },
29686470930SIngo Molnar 		};
29786470930SIngo Molnar 		int n;
29886470930SIngo Molnar 		size_t size;
29986470930SIngo Molnar 		if (fgets(bf, sizeof(bf), fp) == NULL)
30086470930SIngo Molnar 			break;
30186470930SIngo Molnar 
30286470930SIngo Molnar 		/* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
30386470930SIngo Molnar 		n = hex2u64(pbf, &mmap_ev.start);
30486470930SIngo Molnar 		if (n < 0)
30586470930SIngo Molnar 			continue;
30686470930SIngo Molnar 		pbf += n + 1;
30786470930SIngo Molnar 		n = hex2u64(pbf, &mmap_ev.len);
30886470930SIngo Molnar 		if (n < 0)
30986470930SIngo Molnar 			continue;
31086470930SIngo Molnar 		pbf += n + 3;
31186470930SIngo Molnar 		if (*pbf == 'x') { /* vm_exec */
31276c64c5eSJohannes Weiner 			char *execname = strchr(bf, '/');
31386470930SIngo Molnar 
31411b5f81eSAnton Blanchard 			/* Catch VDSO */
31511b5f81eSAnton Blanchard 			if (execname == NULL)
31611b5f81eSAnton Blanchard 				execname = strstr(bf, "[vdso]");
31711b5f81eSAnton Blanchard 
31876c64c5eSJohannes Weiner 			if (execname == NULL)
31986470930SIngo Molnar 				continue;
32086470930SIngo Molnar 
32186470930SIngo Molnar 			size = strlen(execname);
32286470930SIngo Molnar 			execname[size - 1] = '\0'; /* Remove \n */
32386470930SIngo Molnar 			memcpy(mmap_ev.filename, execname, size);
3249cffa8d5SPaul Mackerras 			size = ALIGN(size, sizeof(u64));
32586470930SIngo Molnar 			mmap_ev.len -= mmap_ev.start;
32686470930SIngo Molnar 			mmap_ev.header.size = (sizeof(mmap_ev) -
32786470930SIngo Molnar 					       (sizeof(mmap_ev.filename) - size));
3282a8083f0SArnaldo Carvalho de Melo 			mmap_ev.pid = tgid;
32986470930SIngo Molnar 			mmap_ev.tid = pid;
33086470930SIngo Molnar 
331f5970550SPeter Zijlstra 			write_output(&mmap_ev, mmap_ev.header.size);
33286470930SIngo Molnar 		}
33386470930SIngo Molnar 	}
33486470930SIngo Molnar 
33586470930SIngo Molnar 	fclose(fp);
33686470930SIngo Molnar }
33786470930SIngo Molnar 
3387c6a1c65SPeter Zijlstra static void synthesize_all(void)
33986470930SIngo Molnar {
34086470930SIngo Molnar 	DIR *proc;
34186470930SIngo Molnar 	struct dirent dirent, *next;
34286470930SIngo Molnar 
34386470930SIngo Molnar 	proc = opendir("/proc");
34486470930SIngo Molnar 
34586470930SIngo Molnar 	while (!readdir_r(proc, &dirent, &next) && next) {
34686470930SIngo Molnar 		char *end;
3472a8083f0SArnaldo Carvalho de Melo 		pid_t pid, tgid;
34886470930SIngo Molnar 
34986470930SIngo Molnar 		pid = strtol(dirent.d_name, &end, 10);
35086470930SIngo Molnar 		if (*end) /* only interested in proper numerical dirents */
35186470930SIngo Molnar 			continue;
35286470930SIngo Molnar 
3532a8083f0SArnaldo Carvalho de Melo 		tgid = pid_synthesize_comm_event(pid, 1);
3542a8083f0SArnaldo Carvalho de Melo 		pid_synthesize_mmap_samples(pid, tgid);
35586470930SIngo Molnar 	}
35686470930SIngo Molnar 
35786470930SIngo Molnar 	closedir(proc);
35886470930SIngo Molnar }
35986470930SIngo Molnar 
36086470930SIngo Molnar static int group_fd;
36186470930SIngo Molnar 
362cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
3637c6a1c65SPeter Zijlstra {
3647c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
3657c6a1c65SPeter Zijlstra 
3667c6a1c65SPeter Zijlstra 	if (nr < header->attrs) {
3677c6a1c65SPeter Zijlstra 		h_attr = header->attr[nr];
3687c6a1c65SPeter Zijlstra 	} else {
3697c6a1c65SPeter Zijlstra 		h_attr = perf_header_attr__new(a);
3707c6a1c65SPeter Zijlstra 		perf_header__add_attr(header, h_attr);
3717c6a1c65SPeter Zijlstra 	}
3727c6a1c65SPeter Zijlstra 
3737c6a1c65SPeter Zijlstra 	return h_attr;
3747c6a1c65SPeter Zijlstra }
3757c6a1c65SPeter Zijlstra 
37686470930SIngo Molnar static void create_counter(int counter, int cpu, pid_t pid)
37786470930SIngo Molnar {
378cdd6c482SIngo Molnar 	struct perf_event_attr *attr = attrs + counter;
3797c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
3807c6a1c65SPeter Zijlstra 	int track = !counter; /* only the first counter needs these */
3817c6a1c65SPeter Zijlstra 	struct {
3827c6a1c65SPeter Zijlstra 		u64 count;
3837c6a1c65SPeter Zijlstra 		u64 time_enabled;
3847c6a1c65SPeter Zijlstra 		u64 time_running;
3857c6a1c65SPeter Zijlstra 		u64 id;
3867c6a1c65SPeter Zijlstra 	} read_data;
3877c6a1c65SPeter Zijlstra 
3887c6a1c65SPeter Zijlstra 	attr->read_format	= PERF_FORMAT_TOTAL_TIME_ENABLED |
3897c6a1c65SPeter Zijlstra 				  PERF_FORMAT_TOTAL_TIME_RUNNING |
3907c6a1c65SPeter Zijlstra 				  PERF_FORMAT_ID;
39186470930SIngo Molnar 
3923a9f131fSFrederic Weisbecker 	attr->sample_type	|= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
3933efa1cc9SIngo Molnar 
39486470930SIngo Molnar 	if (freq) {
395ea1900e5SPeter Zijlstra 		attr->sample_type	|= PERF_SAMPLE_PERIOD;
39686470930SIngo Molnar 		attr->freq		= 1;
39786470930SIngo Molnar 		attr->sample_freq	= freq;
39886470930SIngo Molnar 	}
3993efa1cc9SIngo Molnar 
400649c48a9SPeter Zijlstra 	if (no_samples)
401649c48a9SPeter Zijlstra 		attr->sample_freq = 0;
402649c48a9SPeter Zijlstra 
403649c48a9SPeter Zijlstra 	if (inherit_stat)
404649c48a9SPeter Zijlstra 		attr->inherit_stat = 1;
405649c48a9SPeter Zijlstra 
4064bba828dSAnton Blanchard 	if (sample_address)
4074bba828dSAnton Blanchard 		attr->sample_type	|= PERF_SAMPLE_ADDR;
4084bba828dSAnton Blanchard 
4093efa1cc9SIngo Molnar 	if (call_graph)
4103efa1cc9SIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
4113efa1cc9SIngo Molnar 
412cd6feeeaSIngo Molnar 	if (raw_samples) {
4136ddf259dSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_TIME;
414daac07b2SFrederic Weisbecker 		attr->sample_type	|= PERF_SAMPLE_RAW;
415cd6feeeaSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CPU;
416cd6feeeaSIngo Molnar 	}
417f413cdb8SFrederic Weisbecker 
41886470930SIngo Molnar 	attr->mmap		= track;
41986470930SIngo Molnar 	attr->comm		= track;
42086470930SIngo Molnar 	attr->inherit		= (cpu < 0) && inherit;
4214502d77cSPeter Zijlstra 	attr->disabled		= 1;
42286470930SIngo Molnar 
4233da297a6SIngo Molnar try_again:
424cdd6c482SIngo Molnar 	fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
42586470930SIngo Molnar 
42686470930SIngo Molnar 	if (fd[nr_cpu][counter] < 0) {
42786470930SIngo Molnar 		int err = errno;
42886470930SIngo Molnar 
42986470930SIngo Molnar 		if (err == EPERM)
4303da297a6SIngo Molnar 			die("Permission error - are you root?\n");
4310a5ac846SJens Axboe 		else if (err ==  ENODEV && profile_cpu != -1)
4320a5ac846SJens Axboe 			die("No such device - did you specify an out-of-range profile CPU?\n");
4333da297a6SIngo Molnar 
4343da297a6SIngo Molnar 		/*
4353da297a6SIngo Molnar 		 * If it's cycles then fall back to hrtimer
4363da297a6SIngo Molnar 		 * based cpu-clock-tick sw counter, which
4373da297a6SIngo Molnar 		 * is always available even if no PMU support:
4383da297a6SIngo Molnar 		 */
4393da297a6SIngo Molnar 		if (attr->type == PERF_TYPE_HARDWARE
440f4dbfa8fSPeter Zijlstra 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
4413da297a6SIngo Molnar 
4423da297a6SIngo Molnar 			if (verbose)
4433da297a6SIngo Molnar 				warning(" ... trying to fall back to cpu-clock-ticks\n");
4443da297a6SIngo Molnar 			attr->type = PERF_TYPE_SOFTWARE;
445f4dbfa8fSPeter Zijlstra 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
4463da297a6SIngo Molnar 			goto try_again;
4473da297a6SIngo Molnar 		}
44830c806a0SIngo Molnar 		printf("\n");
44930c806a0SIngo Molnar 		error("perfcounter syscall returned with %d (%s)\n",
45030c806a0SIngo Molnar 			fd[nr_cpu][counter], strerror(err));
451cdd6c482SIngo Molnar 		die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
45286470930SIngo Molnar 		exit(-1);
45386470930SIngo Molnar 	}
4543da297a6SIngo Molnar 
4557c6a1c65SPeter Zijlstra 	h_attr = get_header_attr(attr, counter);
4567c6a1c65SPeter Zijlstra 
4577c6a1c65SPeter Zijlstra 	if (!file_new) {
4587c6a1c65SPeter Zijlstra 		if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
4597c6a1c65SPeter Zijlstra 			fprintf(stderr, "incompatible append\n");
4607c6a1c65SPeter Zijlstra 			exit(-1);
4617c6a1c65SPeter Zijlstra 		}
4627c6a1c65SPeter Zijlstra 	}
4637c6a1c65SPeter Zijlstra 
4643928ddbeSFrederic Weisbecker 	if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
4653928ddbeSFrederic Weisbecker 		perror("Unable to read perf file descriptor\n");
4663928ddbeSFrederic Weisbecker 		exit(-1);
4673928ddbeSFrederic Weisbecker 	}
4687c6a1c65SPeter Zijlstra 
4697c6a1c65SPeter Zijlstra 	perf_header_attr__add_id(h_attr, read_data.id);
4707c6a1c65SPeter Zijlstra 
47186470930SIngo Molnar 	assert(fd[nr_cpu][counter] >= 0);
47286470930SIngo Molnar 	fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
47386470930SIngo Molnar 
47486470930SIngo Molnar 	/*
47586470930SIngo Molnar 	 * First counter acts as the group leader:
47686470930SIngo Molnar 	 */
47786470930SIngo Molnar 	if (group && group_fd == -1)
47886470930SIngo Molnar 		group_fd = fd[nr_cpu][counter];
479ea57c4f5SIngo Molnar 	if (multiplex && multiplex_fd == -1)
480ea57c4f5SIngo Molnar 		multiplex_fd = fd[nr_cpu][counter];
48186470930SIngo Molnar 
482ea57c4f5SIngo Molnar 	if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
483ea57c4f5SIngo Molnar 		int ret;
484ea57c4f5SIngo Molnar 
485cdd6c482SIngo Molnar 		ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
486ea57c4f5SIngo Molnar 		assert(ret != -1);
487ea57c4f5SIngo Molnar 	} else {
48886470930SIngo Molnar 		event_array[nr_poll].fd = fd[nr_cpu][counter];
48986470930SIngo Molnar 		event_array[nr_poll].events = POLLIN;
49086470930SIngo Molnar 		nr_poll++;
49186470930SIngo Molnar 
49286470930SIngo Molnar 		mmap_array[nr_cpu][counter].counter = counter;
49386470930SIngo Molnar 		mmap_array[nr_cpu][counter].prev = 0;
49486470930SIngo Molnar 		mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
49586470930SIngo Molnar 		mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
4969d91a6f7SPeter Zijlstra 				PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
49786470930SIngo Molnar 		if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
49886470930SIngo Molnar 			error("failed to mmap with %d (%s)\n", errno, strerror(errno));
49986470930SIngo Molnar 			exit(-1);
50086470930SIngo Molnar 		}
501ea57c4f5SIngo Molnar 	}
502d1302522SFrederic Weisbecker 
503cdd6c482SIngo Molnar 	ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
50486470930SIngo Molnar }
50586470930SIngo Molnar 
50686470930SIngo Molnar static void open_counters(int cpu, pid_t pid)
50786470930SIngo Molnar {
50886470930SIngo Molnar 	int counter;
50986470930SIngo Molnar 
51086470930SIngo Molnar 	group_fd = -1;
51186470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++)
51286470930SIngo Molnar 		create_counter(counter, cpu, pid);
51386470930SIngo Molnar 
51486470930SIngo Molnar 	nr_cpu++;
51586470930SIngo Molnar }
51686470930SIngo Molnar 
517f5970550SPeter Zijlstra static void atexit_header(void)
518f5970550SPeter Zijlstra {
5197c6a1c65SPeter Zijlstra 	header->data_size += bytes_written;
520f5970550SPeter Zijlstra 
5217c6a1c65SPeter Zijlstra 	perf_header__write(header, output);
522f5970550SPeter Zijlstra }
523f5970550SPeter Zijlstra 
52486470930SIngo Molnar static int __cmd_record(int argc, const char **argv)
52586470930SIngo Molnar {
52686470930SIngo Molnar 	int i, counter;
52786470930SIngo Molnar 	struct stat st;
5287c6a1c65SPeter Zijlstra 	pid_t pid = 0;
52986470930SIngo Molnar 	int flags;
53086470930SIngo Molnar 	int ret;
5318b412664SPeter Zijlstra 	unsigned long waking = 0;
53286470930SIngo Molnar 
53386470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
53486470930SIngo Molnar 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
53586470930SIngo Molnar 	assert(nr_cpus <= MAX_NR_CPUS);
53686470930SIngo Molnar 	assert(nr_cpus >= 0);
53786470930SIngo Molnar 
538f5970550SPeter Zijlstra 	atexit(sig_atexit);
539f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
540f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
541f5970550SPeter Zijlstra 
542266e0e21SPierre Habouzit 	if (!stat(output_name, &st) && st.st_size) {
543266e0e21SPierre Habouzit 		if (!force && !append_file) {
54486470930SIngo Molnar 			fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
54586470930SIngo Molnar 					output_name);
54686470930SIngo Molnar 			exit(-1);
54786470930SIngo Molnar 		}
548266e0e21SPierre Habouzit 	} else {
549266e0e21SPierre Habouzit 		append_file = 0;
550266e0e21SPierre Habouzit 	}
55186470930SIngo Molnar 
55286470930SIngo Molnar 	flags = O_CREAT|O_RDWR;
55386470930SIngo Molnar 	if (append_file)
554f5970550SPeter Zijlstra 		file_new = 0;
55586470930SIngo Molnar 	else
55686470930SIngo Molnar 		flags |= O_TRUNC;
55786470930SIngo Molnar 
55886470930SIngo Molnar 	output = open(output_name, flags, S_IRUSR|S_IWUSR);
55986470930SIngo Molnar 	if (output < 0) {
56086470930SIngo Molnar 		perror("failed to create output file");
56186470930SIngo Molnar 		exit(-1);
56286470930SIngo Molnar 	}
56386470930SIngo Molnar 
5647c6a1c65SPeter Zijlstra 	if (!file_new)
5657c6a1c65SPeter Zijlstra 		header = perf_header__read(output);
5667c6a1c65SPeter Zijlstra 	else
5677c6a1c65SPeter Zijlstra 		header = perf_header__new();
568f5970550SPeter Zijlstra 
5699df37dddSFrederic Weisbecker 
5709df37dddSFrederic Weisbecker 	if (raw_samples) {
5711ef2ed10SFrederic Weisbecker 		read_tracing_data(attrs, nr_counters);
5729df37dddSFrederic Weisbecker 	} else {
5739df37dddSFrederic Weisbecker 		for (i = 0; i < nr_counters; i++) {
5749df37dddSFrederic Weisbecker 			if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
5751ef2ed10SFrederic Weisbecker 				read_tracing_data(attrs, nr_counters);
5769df37dddSFrederic Weisbecker 				break;
5779df37dddSFrederic Weisbecker 			}
5789df37dddSFrederic Weisbecker 		}
5799df37dddSFrederic Weisbecker 	}
580f5970550SPeter Zijlstra 	atexit(atexit_header);
581f5970550SPeter Zijlstra 
58286470930SIngo Molnar 	if (!system_wide) {
5837c6a1c65SPeter Zijlstra 		pid = target_pid;
5847c6a1c65SPeter Zijlstra 		if (pid == -1)
5857c6a1c65SPeter Zijlstra 			pid = getpid();
5867c6a1c65SPeter Zijlstra 
5870a5ac846SJens Axboe 		open_counters(profile_cpu, pid);
5880a5ac846SJens Axboe 	} else {
5890a5ac846SJens Axboe 		if (profile_cpu != -1) {
5900a5ac846SJens Axboe 			open_counters(profile_cpu, target_pid);
5910a5ac846SJens Axboe 		} else {
5920a5ac846SJens Axboe 			for (i = 0; i < nr_cpus; i++)
59386470930SIngo Molnar 				open_counters(i, target_pid);
5940a5ac846SJens Axboe 		}
5950a5ac846SJens Axboe 	}
59686470930SIngo Molnar 
5977c6a1c65SPeter Zijlstra 	if (file_new)
5987c6a1c65SPeter Zijlstra 		perf_header__write(header, output);
5997c6a1c65SPeter Zijlstra 
6007c6a1c65SPeter Zijlstra 	if (!system_wide) {
6012a8083f0SArnaldo Carvalho de Melo 		pid_t tgid = pid_synthesize_comm_event(pid, 0);
6022a8083f0SArnaldo Carvalho de Melo 		pid_synthesize_mmap_samples(pid, tgid);
6037c6a1c65SPeter Zijlstra 	} else
6047c6a1c65SPeter Zijlstra 		synthesize_all();
6057c6a1c65SPeter Zijlstra 
60686470930SIngo Molnar 	if (target_pid == -1 && argc) {
60786470930SIngo Molnar 		pid = fork();
60886470930SIngo Molnar 		if (pid < 0)
60986470930SIngo Molnar 			perror("failed to fork");
61086470930SIngo Molnar 
61186470930SIngo Molnar 		if (!pid) {
61286470930SIngo Molnar 			if (execvp(argv[0], (char **)argv)) {
61386470930SIngo Molnar 				perror(argv[0]);
61486470930SIngo Molnar 				exit(-1);
61586470930SIngo Molnar 			}
61686470930SIngo Molnar 		}
617933da83aSChris Wilson 
618933da83aSChris Wilson 		child_pid = pid;
61986470930SIngo Molnar 	}
62086470930SIngo Molnar 
62186470930SIngo Molnar 	if (realtime_prio) {
62286470930SIngo Molnar 		struct sched_param param;
62386470930SIngo Molnar 
62486470930SIngo Molnar 		param.sched_priority = realtime_prio;
62586470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
62686470930SIngo Molnar 			printf("Could not set realtime priority.\n");
62786470930SIngo Molnar 			exit(-1);
62886470930SIngo Molnar 		}
62986470930SIngo Molnar 	}
63086470930SIngo Molnar 
631649c48a9SPeter Zijlstra 	for (;;) {
63286470930SIngo Molnar 		int hits = samples;
63386470930SIngo Molnar 
63486470930SIngo Molnar 		for (i = 0; i < nr_cpu; i++) {
635ea57c4f5SIngo Molnar 			for (counter = 0; counter < nr_counters; counter++) {
636ea57c4f5SIngo Molnar 				if (mmap_array[i][counter].base)
63786470930SIngo Molnar 					mmap_read(&mmap_array[i][counter]);
63886470930SIngo Molnar 			}
639ea57c4f5SIngo Molnar 		}
64086470930SIngo Molnar 
641649c48a9SPeter Zijlstra 		if (hits == samples) {
642649c48a9SPeter Zijlstra 			if (done)
643649c48a9SPeter Zijlstra 				break;
6448b412664SPeter Zijlstra 			ret = poll(event_array, nr_poll, -1);
6458b412664SPeter Zijlstra 			waking++;
6468b412664SPeter Zijlstra 		}
6478b412664SPeter Zijlstra 
6488b412664SPeter Zijlstra 		if (done) {
6498b412664SPeter Zijlstra 			for (i = 0; i < nr_cpu; i++) {
6508b412664SPeter Zijlstra 				for (counter = 0; counter < nr_counters; counter++)
651cdd6c482SIngo Molnar 					ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
65286470930SIngo Molnar 			}
653649c48a9SPeter Zijlstra 		}
6548b412664SPeter Zijlstra 	}
6558b412664SPeter Zijlstra 
6568b412664SPeter Zijlstra 	fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
65786470930SIngo Molnar 
65886470930SIngo Molnar 	/*
65986470930SIngo Molnar 	 * Approximate RIP event size: 24 bytes.
66086470930SIngo Molnar 	 */
66186470930SIngo Molnar 	fprintf(stderr,
66286470930SIngo Molnar 		"[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
66386470930SIngo Molnar 		(double)bytes_written / 1024.0 / 1024.0,
66486470930SIngo Molnar 		output_name,
66586470930SIngo Molnar 		bytes_written / 24);
66686470930SIngo Molnar 
66786470930SIngo Molnar 	return 0;
66886470930SIngo Molnar }
66986470930SIngo Molnar 
67086470930SIngo Molnar static const char * const record_usage[] = {
67186470930SIngo Molnar 	"perf record [<options>] [<command>]",
67286470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
67386470930SIngo Molnar 	NULL
67486470930SIngo Molnar };
67586470930SIngo Molnar 
67686470930SIngo Molnar static const struct option options[] = {
67786470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
67886470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
67986470930SIngo Molnar 		     parse_events),
68086470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
68186470930SIngo Molnar 		    "record events on existing pid"),
68286470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
68386470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
684daac07b2SFrederic Weisbecker 	OPT_BOOLEAN('R', "raw-samples", &raw_samples,
685daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
68686470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
68786470930SIngo Molnar 			    "system-wide collection from all CPUs"),
68886470930SIngo Molnar 	OPT_BOOLEAN('A', "append", &append_file,
68986470930SIngo Molnar 			    "append to the output file to do incremental profiling"),
6900a5ac846SJens Axboe 	OPT_INTEGER('C', "profile_cpu", &profile_cpu,
6910a5ac846SJens Axboe 			    "CPU to profile on"),
69286470930SIngo Molnar 	OPT_BOOLEAN('f', "force", &force,
69386470930SIngo Molnar 			"overwrite existing data file"),
69486470930SIngo Molnar 	OPT_LONG('c', "count", &default_interval,
69586470930SIngo Molnar 		    "event period to sample"),
69686470930SIngo Molnar 	OPT_STRING('o', "output", &output_name, "file",
69786470930SIngo Molnar 		    "output file name"),
69886470930SIngo Molnar 	OPT_BOOLEAN('i', "inherit", &inherit,
69986470930SIngo Molnar 		    "child tasks inherit counters"),
70086470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
70186470930SIngo Molnar 		    "profile at this frequency"),
70286470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
70386470930SIngo Molnar 		    "number of mmap data pages"),
7043efa1cc9SIngo Molnar 	OPT_BOOLEAN('g', "call-graph", &call_graph,
7053efa1cc9SIngo Molnar 		    "do call-graph (stack chain/backtrace) recording"),
7063da297a6SIngo Molnar 	OPT_BOOLEAN('v', "verbose", &verbose,
7073da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
708649c48a9SPeter Zijlstra 	OPT_BOOLEAN('s', "stat", &inherit_stat,
709649c48a9SPeter Zijlstra 		    "per thread counts"),
7104bba828dSAnton Blanchard 	OPT_BOOLEAN('d', "data", &sample_address,
7114bba828dSAnton Blanchard 		    "Sample addresses"),
712649c48a9SPeter Zijlstra 	OPT_BOOLEAN('n', "no-samples", &no_samples,
713649c48a9SPeter Zijlstra 		    "don't sample"),
714d1302522SFrederic Weisbecker 	OPT_BOOLEAN('M', "multiplex", &multiplex,
715d1302522SFrederic Weisbecker 		    "multiplex counter output in a single channel"),
71686470930SIngo Molnar 	OPT_END()
71786470930SIngo Molnar };
71886470930SIngo Molnar 
719f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used)
72086470930SIngo Molnar {
72186470930SIngo Molnar 	int counter;
72286470930SIngo Molnar 
723a0541234SAnton Blanchard 	argc = parse_options(argc, argv, options, record_usage,
724a0541234SAnton Blanchard 		PARSE_OPT_STOP_AT_NON_OPTION);
72586470930SIngo Molnar 	if (!argc && target_pid == -1 && !system_wide)
72686470930SIngo Molnar 		usage_with_options(record_usage, options);
72786470930SIngo Molnar 
728bbd36e5eSPeter Zijlstra 	if (!nr_counters) {
72986470930SIngo Molnar 		nr_counters	= 1;
730bbd36e5eSPeter Zijlstra 		attrs[0].type	= PERF_TYPE_HARDWARE;
731bbd36e5eSPeter Zijlstra 		attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
732bbd36e5eSPeter Zijlstra 	}
73386470930SIngo Molnar 
73486470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
73586470930SIngo Molnar 		if (attrs[counter].sample_period)
73686470930SIngo Molnar 			continue;
73786470930SIngo Molnar 
73886470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
73986470930SIngo Molnar 	}
74086470930SIngo Molnar 
74186470930SIngo Molnar 	return __cmd_record(argc, argv);
74286470930SIngo Molnar }
743