xref: /openbmc/linux/tools/perf/builtin-record.c (revision 8b412664d0a487c2e16ac43f4fcede346df33254)
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;
3586470930SIngo Molnar static int			freq				= 0;
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;
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 
5486470930SIngo Molnar static long			samples;
5586470930SIngo Molnar static struct timeval		last_read;
5686470930SIngo Molnar static struct timeval		this_read;
5786470930SIngo Molnar 
589cffa8d5SPaul Mackerras static u64			bytes_written;
5986470930SIngo Molnar 
6086470930SIngo Molnar static struct pollfd		event_array[MAX_NR_CPUS * MAX_COUNTERS];
6186470930SIngo Molnar 
6286470930SIngo Molnar static int			nr_poll;
6386470930SIngo Molnar static int			nr_cpu;
6486470930SIngo Molnar 
65f5970550SPeter Zijlstra static int			file_new = 1;
667c6a1c65SPeter Zijlstra 
677c6a1c65SPeter Zijlstra struct perf_header		*header;
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 {
8086470930SIngo Molnar 	struct perf_counter_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 {
919d91a6f7SPeter Zijlstra 	struct perf_counter_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 {
187f7b7c26eSPeter Zijlstra 	if (signr == -1)
188f7b7c26eSPeter Zijlstra 		return;
189f7b7c26eSPeter Zijlstra 
190f7b7c26eSPeter Zijlstra 	signal(signr, SIG_DFL);
191f7b7c26eSPeter Zijlstra 	kill(getpid(), signr);
19286470930SIngo Molnar }
19386470930SIngo Molnar 
1942a8083f0SArnaldo Carvalho de Melo static pid_t pid_synthesize_comm_event(pid_t pid, int full)
19586470930SIngo Molnar {
19686470930SIngo Molnar 	struct comm_event comm_ev;
19786470930SIngo Molnar 	char filename[PATH_MAX];
19886470930SIngo Molnar 	char bf[BUFSIZ];
1992a8083f0SArnaldo Carvalho de Melo 	FILE *fp;
2002a8083f0SArnaldo Carvalho de Melo 	size_t size = 0;
20186470930SIngo Molnar 	DIR *tasks;
20286470930SIngo Molnar 	struct dirent dirent, *next;
2032a8083f0SArnaldo Carvalho de Melo 	pid_t tgid = 0;
20486470930SIngo Molnar 
2052a8083f0SArnaldo Carvalho de Melo 	snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
20686470930SIngo Molnar 
2072a8083f0SArnaldo Carvalho de Melo 	fp = fopen(filename, "r");
20839e6dd73SArnaldo Carvalho de Melo 	if (fp == NULL) {
209613d8602SIngo Molnar 		/*
210613d8602SIngo Molnar 		 * We raced with a task exiting - just return:
211613d8602SIngo Molnar 		 */
212613d8602SIngo Molnar 		if (verbose)
21386470930SIngo Molnar 			fprintf(stderr, "couldn't open %s\n", filename);
2142a8083f0SArnaldo Carvalho de Melo 		return 0;
21586470930SIngo Molnar 	}
21686470930SIngo Molnar 
21786470930SIngo Molnar 	memset(&comm_ev, 0, sizeof(comm_ev));
2182a8083f0SArnaldo Carvalho de Melo 	while (!comm_ev.comm[0] || !comm_ev.pid) {
2192a8083f0SArnaldo Carvalho de Melo 		if (fgets(bf, sizeof(bf), fp) == NULL)
22086470930SIngo Molnar 			goto out_failure;
22186470930SIngo Molnar 
2222a8083f0SArnaldo Carvalho de Melo 		if (memcmp(bf, "Name:", 5) == 0) {
2232a8083f0SArnaldo Carvalho de Melo 			char *name = bf + 5;
2242a8083f0SArnaldo Carvalho de Melo 			while (*name && isspace(*name))
2252a8083f0SArnaldo Carvalho de Melo 				++name;
2262a8083f0SArnaldo Carvalho de Melo 			size = strlen(name) - 1;
2272a8083f0SArnaldo Carvalho de Melo 			memcpy(comm_ev.comm, name, size++);
2282a8083f0SArnaldo Carvalho de Melo 		} else if (memcmp(bf, "Tgid:", 5) == 0) {
2292a8083f0SArnaldo Carvalho de Melo 			char *tgids = bf + 5;
2302a8083f0SArnaldo Carvalho de Melo 			while (*tgids && isspace(*tgids))
2312a8083f0SArnaldo Carvalho de Melo 				++tgids;
2322a8083f0SArnaldo Carvalho de Melo 			tgid = comm_ev.pid = atoi(tgids);
2332a8083f0SArnaldo Carvalho de Melo 		}
2342a8083f0SArnaldo Carvalho de Melo 	}
2352a8083f0SArnaldo Carvalho de Melo 
23686470930SIngo Molnar 	comm_ev.header.type = PERF_EVENT_COMM;
2379cffa8d5SPaul Mackerras 	size = ALIGN(size, sizeof(u64));
23886470930SIngo Molnar 	comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
23986470930SIngo Molnar 
24086470930SIngo Molnar 	if (!full) {
24186470930SIngo Molnar 		comm_ev.tid = pid;
24286470930SIngo Molnar 
243f5970550SPeter Zijlstra 		write_output(&comm_ev, comm_ev.header.size);
2442a8083f0SArnaldo Carvalho de Melo 		goto out_fclose;
24586470930SIngo Molnar 	}
24686470930SIngo Molnar 
24786470930SIngo Molnar 	snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
24886470930SIngo Molnar 
24986470930SIngo Molnar 	tasks = opendir(filename);
25086470930SIngo Molnar 	while (!readdir_r(tasks, &dirent, &next) && next) {
25186470930SIngo Molnar 		char *end;
25286470930SIngo Molnar 		pid = strtol(dirent.d_name, &end, 10);
25386470930SIngo Molnar 		if (*end)
25486470930SIngo Molnar 			continue;
25586470930SIngo Molnar 
25686470930SIngo Molnar 		comm_ev.tid = pid;
25786470930SIngo Molnar 
258f5970550SPeter Zijlstra 		write_output(&comm_ev, comm_ev.header.size);
25986470930SIngo Molnar 	}
26086470930SIngo Molnar 	closedir(tasks);
2612a8083f0SArnaldo Carvalho de Melo 
2622a8083f0SArnaldo Carvalho de Melo out_fclose:
2632a8083f0SArnaldo Carvalho de Melo 	fclose(fp);
2642a8083f0SArnaldo Carvalho de Melo 	return tgid;
26586470930SIngo Molnar 
26686470930SIngo Molnar out_failure:
26786470930SIngo Molnar 	fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
26886470930SIngo Molnar 		filename);
26986470930SIngo Molnar 	exit(EXIT_FAILURE);
27086470930SIngo Molnar }
27186470930SIngo Molnar 
2722a8083f0SArnaldo Carvalho de Melo static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
27386470930SIngo Molnar {
27486470930SIngo Molnar 	char filename[PATH_MAX];
27586470930SIngo Molnar 	FILE *fp;
27686470930SIngo Molnar 
27786470930SIngo Molnar 	snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
27886470930SIngo Molnar 
27986470930SIngo Molnar 	fp = fopen(filename, "r");
28086470930SIngo Molnar 	if (fp == NULL) {
281613d8602SIngo Molnar 		/*
282613d8602SIngo Molnar 		 * We raced with a task exiting - just return:
283613d8602SIngo Molnar 		 */
284613d8602SIngo Molnar 		if (verbose)
28586470930SIngo Molnar 			fprintf(stderr, "couldn't open %s\n", filename);
286613d8602SIngo Molnar 		return;
28786470930SIngo Molnar 	}
28886470930SIngo Molnar 	while (1) {
28986470930SIngo Molnar 		char bf[BUFSIZ], *pbf = bf;
29086470930SIngo Molnar 		struct mmap_event mmap_ev = {
291f37a291cSIngo Molnar 			.header = { .type = PERF_EVENT_MMAP },
29286470930SIngo Molnar 		};
29386470930SIngo Molnar 		int n;
29486470930SIngo Molnar 		size_t size;
29586470930SIngo Molnar 		if (fgets(bf, sizeof(bf), fp) == NULL)
29686470930SIngo Molnar 			break;
29786470930SIngo Molnar 
29886470930SIngo Molnar 		/* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
29986470930SIngo Molnar 		n = hex2u64(pbf, &mmap_ev.start);
30086470930SIngo Molnar 		if (n < 0)
30186470930SIngo Molnar 			continue;
30286470930SIngo Molnar 		pbf += n + 1;
30386470930SIngo Molnar 		n = hex2u64(pbf, &mmap_ev.len);
30486470930SIngo Molnar 		if (n < 0)
30586470930SIngo Molnar 			continue;
30686470930SIngo Molnar 		pbf += n + 3;
30786470930SIngo Molnar 		if (*pbf == 'x') { /* vm_exec */
30876c64c5eSJohannes Weiner 			char *execname = strchr(bf, '/');
30986470930SIngo Molnar 
31011b5f81eSAnton Blanchard 			/* Catch VDSO */
31111b5f81eSAnton Blanchard 			if (execname == NULL)
31211b5f81eSAnton Blanchard 				execname = strstr(bf, "[vdso]");
31311b5f81eSAnton Blanchard 
31476c64c5eSJohannes Weiner 			if (execname == NULL)
31586470930SIngo Molnar 				continue;
31686470930SIngo Molnar 
31786470930SIngo Molnar 			size = strlen(execname);
31886470930SIngo Molnar 			execname[size - 1] = '\0'; /* Remove \n */
31986470930SIngo Molnar 			memcpy(mmap_ev.filename, execname, size);
3209cffa8d5SPaul Mackerras 			size = ALIGN(size, sizeof(u64));
32186470930SIngo Molnar 			mmap_ev.len -= mmap_ev.start;
32286470930SIngo Molnar 			mmap_ev.header.size = (sizeof(mmap_ev) -
32386470930SIngo Molnar 					       (sizeof(mmap_ev.filename) - size));
3242a8083f0SArnaldo Carvalho de Melo 			mmap_ev.pid = tgid;
32586470930SIngo Molnar 			mmap_ev.tid = pid;
32686470930SIngo Molnar 
327f5970550SPeter Zijlstra 			write_output(&mmap_ev, mmap_ev.header.size);
32886470930SIngo Molnar 		}
32986470930SIngo Molnar 	}
33086470930SIngo Molnar 
33186470930SIngo Molnar 	fclose(fp);
33286470930SIngo Molnar }
33386470930SIngo Molnar 
3347c6a1c65SPeter Zijlstra static void synthesize_all(void)
33586470930SIngo Molnar {
33686470930SIngo Molnar 	DIR *proc;
33786470930SIngo Molnar 	struct dirent dirent, *next;
33886470930SIngo Molnar 
33986470930SIngo Molnar 	proc = opendir("/proc");
34086470930SIngo Molnar 
34186470930SIngo Molnar 	while (!readdir_r(proc, &dirent, &next) && next) {
34286470930SIngo Molnar 		char *end;
3432a8083f0SArnaldo Carvalho de Melo 		pid_t pid, tgid;
34486470930SIngo Molnar 
34586470930SIngo Molnar 		pid = strtol(dirent.d_name, &end, 10);
34686470930SIngo Molnar 		if (*end) /* only interested in proper numerical dirents */
34786470930SIngo Molnar 			continue;
34886470930SIngo Molnar 
3492a8083f0SArnaldo Carvalho de Melo 		tgid = pid_synthesize_comm_event(pid, 1);
3502a8083f0SArnaldo Carvalho de Melo 		pid_synthesize_mmap_samples(pid, tgid);
35186470930SIngo Molnar 	}
35286470930SIngo Molnar 
35386470930SIngo Molnar 	closedir(proc);
35486470930SIngo Molnar }
35586470930SIngo Molnar 
35686470930SIngo Molnar static int group_fd;
35786470930SIngo Molnar 
3587c6a1c65SPeter Zijlstra static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
3597c6a1c65SPeter Zijlstra {
3607c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
3617c6a1c65SPeter Zijlstra 
3627c6a1c65SPeter Zijlstra 	if (nr < header->attrs) {
3637c6a1c65SPeter Zijlstra 		h_attr = header->attr[nr];
3647c6a1c65SPeter Zijlstra 	} else {
3657c6a1c65SPeter Zijlstra 		h_attr = perf_header_attr__new(a);
3667c6a1c65SPeter Zijlstra 		perf_header__add_attr(header, h_attr);
3677c6a1c65SPeter Zijlstra 	}
3687c6a1c65SPeter Zijlstra 
3697c6a1c65SPeter Zijlstra 	return h_attr;
3707c6a1c65SPeter Zijlstra }
3717c6a1c65SPeter Zijlstra 
37286470930SIngo Molnar static void create_counter(int counter, int cpu, pid_t pid)
37386470930SIngo Molnar {
37486470930SIngo Molnar 	struct perf_counter_attr *attr = attrs + counter;
3757c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
3767c6a1c65SPeter Zijlstra 	int track = !counter; /* only the first counter needs these */
3777c6a1c65SPeter Zijlstra 	struct {
3787c6a1c65SPeter Zijlstra 		u64 count;
3797c6a1c65SPeter Zijlstra 		u64 time_enabled;
3807c6a1c65SPeter Zijlstra 		u64 time_running;
3817c6a1c65SPeter Zijlstra 		u64 id;
3827c6a1c65SPeter Zijlstra 	} read_data;
3837c6a1c65SPeter Zijlstra 
3847c6a1c65SPeter Zijlstra 	attr->read_format	= PERF_FORMAT_TOTAL_TIME_ENABLED |
3857c6a1c65SPeter Zijlstra 				  PERF_FORMAT_TOTAL_TIME_RUNNING |
3867c6a1c65SPeter Zijlstra 				  PERF_FORMAT_ID;
38786470930SIngo Molnar 
3883a9f131fSFrederic Weisbecker 	attr->sample_type	|= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
3893efa1cc9SIngo Molnar 
39086470930SIngo Molnar 	if (freq) {
391ea1900e5SPeter Zijlstra 		attr->sample_type	|= PERF_SAMPLE_PERIOD;
39286470930SIngo Molnar 		attr->freq		= 1;
39386470930SIngo Molnar 		attr->sample_freq	= freq;
39486470930SIngo Molnar 	}
3953efa1cc9SIngo Molnar 
396649c48a9SPeter Zijlstra 	if (no_samples)
397649c48a9SPeter Zijlstra 		attr->sample_freq = 0;
398649c48a9SPeter Zijlstra 
399649c48a9SPeter Zijlstra 	if (inherit_stat)
400649c48a9SPeter Zijlstra 		attr->inherit_stat = 1;
401649c48a9SPeter Zijlstra 
4024bba828dSAnton Blanchard 	if (sample_address)
4034bba828dSAnton Blanchard 		attr->sample_type	|= PERF_SAMPLE_ADDR;
4044bba828dSAnton Blanchard 
4053efa1cc9SIngo Molnar 	if (call_graph)
4063efa1cc9SIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
4073efa1cc9SIngo Molnar 
408cd6feeeaSIngo Molnar 	if (raw_samples) {
4096ddf259dSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_TIME;
410daac07b2SFrederic Weisbecker 		attr->sample_type	|= PERF_SAMPLE_RAW;
411cd6feeeaSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CPU;
412cd6feeeaSIngo Molnar 	}
413f413cdb8SFrederic Weisbecker 
41486470930SIngo Molnar 	attr->mmap		= track;
41586470930SIngo Molnar 	attr->comm		= track;
41686470930SIngo Molnar 	attr->inherit		= (cpu < 0) && inherit;
4174502d77cSPeter Zijlstra 	attr->disabled		= 1;
41886470930SIngo Molnar 
4193da297a6SIngo Molnar try_again:
42086470930SIngo Molnar 	fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
42186470930SIngo Molnar 
42286470930SIngo Molnar 	if (fd[nr_cpu][counter] < 0) {
42386470930SIngo Molnar 		int err = errno;
42486470930SIngo Molnar 
42586470930SIngo Molnar 		if (err == EPERM)
4263da297a6SIngo Molnar 			die("Permission error - are you root?\n");
4270a5ac846SJens Axboe 		else if (err ==  ENODEV && profile_cpu != -1)
4280a5ac846SJens Axboe 			die("No such device - did you specify an out-of-range profile CPU?\n");
4293da297a6SIngo Molnar 
4303da297a6SIngo Molnar 		/*
4313da297a6SIngo Molnar 		 * If it's cycles then fall back to hrtimer
4323da297a6SIngo Molnar 		 * based cpu-clock-tick sw counter, which
4333da297a6SIngo Molnar 		 * is always available even if no PMU support:
4343da297a6SIngo Molnar 		 */
4353da297a6SIngo Molnar 		if (attr->type == PERF_TYPE_HARDWARE
436f4dbfa8fSPeter Zijlstra 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
4373da297a6SIngo Molnar 
4383da297a6SIngo Molnar 			if (verbose)
4393da297a6SIngo Molnar 				warning(" ... trying to fall back to cpu-clock-ticks\n");
4403da297a6SIngo Molnar 			attr->type = PERF_TYPE_SOFTWARE;
441f4dbfa8fSPeter Zijlstra 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
4423da297a6SIngo Molnar 			goto try_again;
4433da297a6SIngo Molnar 		}
44430c806a0SIngo Molnar 		printf("\n");
44530c806a0SIngo Molnar 		error("perfcounter syscall returned with %d (%s)\n",
44630c806a0SIngo Molnar 			fd[nr_cpu][counter], strerror(err));
44730c806a0SIngo Molnar 		die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
44886470930SIngo Molnar 		exit(-1);
44986470930SIngo Molnar 	}
4503da297a6SIngo Molnar 
4517c6a1c65SPeter Zijlstra 	h_attr = get_header_attr(attr, counter);
4527c6a1c65SPeter Zijlstra 
4537c6a1c65SPeter Zijlstra 	if (!file_new) {
4547c6a1c65SPeter Zijlstra 		if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
4557c6a1c65SPeter Zijlstra 			fprintf(stderr, "incompatible append\n");
4567c6a1c65SPeter Zijlstra 			exit(-1);
4577c6a1c65SPeter Zijlstra 		}
4587c6a1c65SPeter Zijlstra 	}
4597c6a1c65SPeter Zijlstra 
4603928ddbeSFrederic Weisbecker 	if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
4613928ddbeSFrederic Weisbecker 		perror("Unable to read perf file descriptor\n");
4623928ddbeSFrederic Weisbecker 		exit(-1);
4633928ddbeSFrederic Weisbecker 	}
4647c6a1c65SPeter Zijlstra 
4657c6a1c65SPeter Zijlstra 	perf_header_attr__add_id(h_attr, read_data.id);
4667c6a1c65SPeter Zijlstra 
46786470930SIngo Molnar 	assert(fd[nr_cpu][counter] >= 0);
46886470930SIngo Molnar 	fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
46986470930SIngo Molnar 
47086470930SIngo Molnar 	/*
47186470930SIngo Molnar 	 * First counter acts as the group leader:
47286470930SIngo Molnar 	 */
47386470930SIngo Molnar 	if (group && group_fd == -1)
47486470930SIngo Molnar 		group_fd = fd[nr_cpu][counter];
475ea57c4f5SIngo Molnar 	if (multiplex && multiplex_fd == -1)
476ea57c4f5SIngo Molnar 		multiplex_fd = fd[nr_cpu][counter];
47786470930SIngo Molnar 
478ea57c4f5SIngo Molnar 	if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
479ea57c4f5SIngo Molnar 		int ret;
480ea57c4f5SIngo Molnar 
481ea57c4f5SIngo Molnar 		ret = ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_SET_OUTPUT, multiplex_fd);
482ea57c4f5SIngo Molnar 		assert(ret != -1);
483ea57c4f5SIngo Molnar 	} else {
48486470930SIngo Molnar 		event_array[nr_poll].fd = fd[nr_cpu][counter];
48586470930SIngo Molnar 		event_array[nr_poll].events = POLLIN;
48686470930SIngo Molnar 		nr_poll++;
48786470930SIngo Molnar 
48886470930SIngo Molnar 		mmap_array[nr_cpu][counter].counter = counter;
48986470930SIngo Molnar 		mmap_array[nr_cpu][counter].prev = 0;
49086470930SIngo Molnar 		mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
49186470930SIngo Molnar 		mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
4929d91a6f7SPeter Zijlstra 				PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
49386470930SIngo Molnar 		if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
49486470930SIngo Molnar 			error("failed to mmap with %d (%s)\n", errno, strerror(errno));
49586470930SIngo Molnar 			exit(-1);
49686470930SIngo Molnar 		}
497ea57c4f5SIngo Molnar 	}
498d1302522SFrederic Weisbecker 
4994502d77cSPeter Zijlstra 	ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
50086470930SIngo Molnar }
50186470930SIngo Molnar 
50286470930SIngo Molnar static void open_counters(int cpu, pid_t pid)
50386470930SIngo Molnar {
50486470930SIngo Molnar 	int counter;
50586470930SIngo Molnar 
50686470930SIngo Molnar 	group_fd = -1;
50786470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++)
50886470930SIngo Molnar 		create_counter(counter, cpu, pid);
50986470930SIngo Molnar 
51086470930SIngo Molnar 	nr_cpu++;
51186470930SIngo Molnar }
51286470930SIngo Molnar 
513f5970550SPeter Zijlstra static void atexit_header(void)
514f5970550SPeter Zijlstra {
5157c6a1c65SPeter Zijlstra 	header->data_size += bytes_written;
516f5970550SPeter Zijlstra 
5177c6a1c65SPeter Zijlstra 	perf_header__write(header, output);
518f5970550SPeter Zijlstra }
519f5970550SPeter Zijlstra 
52086470930SIngo Molnar static int __cmd_record(int argc, const char **argv)
52186470930SIngo Molnar {
52286470930SIngo Molnar 	int i, counter;
52386470930SIngo Molnar 	struct stat st;
5247c6a1c65SPeter Zijlstra 	pid_t pid = 0;
52586470930SIngo Molnar 	int flags;
52686470930SIngo Molnar 	int ret;
527*8b412664SPeter Zijlstra 	unsigned long waking = 0;
52886470930SIngo Molnar 
52986470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
53086470930SIngo Molnar 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
53186470930SIngo Molnar 	assert(nr_cpus <= MAX_NR_CPUS);
53286470930SIngo Molnar 	assert(nr_cpus >= 0);
53386470930SIngo Molnar 
534f5970550SPeter Zijlstra 	atexit(sig_atexit);
535f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
536f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
537f5970550SPeter Zijlstra 
538266e0e21SPierre Habouzit 	if (!stat(output_name, &st) && st.st_size) {
539266e0e21SPierre Habouzit 		if (!force && !append_file) {
54086470930SIngo Molnar 			fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
54186470930SIngo Molnar 					output_name);
54286470930SIngo Molnar 			exit(-1);
54386470930SIngo Molnar 		}
544266e0e21SPierre Habouzit 	} else {
545266e0e21SPierre Habouzit 		append_file = 0;
546266e0e21SPierre Habouzit 	}
54786470930SIngo Molnar 
54886470930SIngo Molnar 	flags = O_CREAT|O_RDWR;
54986470930SIngo Molnar 	if (append_file)
550f5970550SPeter Zijlstra 		file_new = 0;
55186470930SIngo Molnar 	else
55286470930SIngo Molnar 		flags |= O_TRUNC;
55386470930SIngo Molnar 
55486470930SIngo Molnar 	output = open(output_name, flags, S_IRUSR|S_IWUSR);
55586470930SIngo Molnar 	if (output < 0) {
55686470930SIngo Molnar 		perror("failed to create output file");
55786470930SIngo Molnar 		exit(-1);
55886470930SIngo Molnar 	}
55986470930SIngo Molnar 
5607c6a1c65SPeter Zijlstra 	if (!file_new)
5617c6a1c65SPeter Zijlstra 		header = perf_header__read(output);
5627c6a1c65SPeter Zijlstra 	else
5637c6a1c65SPeter Zijlstra 		header = perf_header__new();
564f5970550SPeter Zijlstra 
5659df37dddSFrederic Weisbecker 
5669df37dddSFrederic Weisbecker 	if (raw_samples) {
5671ef2ed10SFrederic Weisbecker 		read_tracing_data(attrs, nr_counters);
5689df37dddSFrederic Weisbecker 	} else {
5699df37dddSFrederic Weisbecker 		for (i = 0; i < nr_counters; i++) {
5709df37dddSFrederic Weisbecker 			if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
5711ef2ed10SFrederic Weisbecker 				read_tracing_data(attrs, nr_counters);
5729df37dddSFrederic Weisbecker 				break;
5739df37dddSFrederic Weisbecker 			}
5749df37dddSFrederic Weisbecker 		}
5759df37dddSFrederic Weisbecker 	}
576f5970550SPeter Zijlstra 	atexit(atexit_header);
577f5970550SPeter Zijlstra 
57886470930SIngo Molnar 	if (!system_wide) {
5797c6a1c65SPeter Zijlstra 		pid = target_pid;
5807c6a1c65SPeter Zijlstra 		if (pid == -1)
5817c6a1c65SPeter Zijlstra 			pid = getpid();
5827c6a1c65SPeter Zijlstra 
5830a5ac846SJens Axboe 		open_counters(profile_cpu, pid);
5840a5ac846SJens Axboe 	} else {
5850a5ac846SJens Axboe 		if (profile_cpu != -1) {
5860a5ac846SJens Axboe 			open_counters(profile_cpu, target_pid);
5870a5ac846SJens Axboe 		} else {
5880a5ac846SJens Axboe 			for (i = 0; i < nr_cpus; i++)
58986470930SIngo Molnar 				open_counters(i, target_pid);
5900a5ac846SJens Axboe 		}
5910a5ac846SJens Axboe 	}
59286470930SIngo Molnar 
5937c6a1c65SPeter Zijlstra 	if (file_new)
5947c6a1c65SPeter Zijlstra 		perf_header__write(header, output);
5957c6a1c65SPeter Zijlstra 
5967c6a1c65SPeter Zijlstra 	if (!system_wide) {
5972a8083f0SArnaldo Carvalho de Melo 		pid_t tgid = pid_synthesize_comm_event(pid, 0);
5982a8083f0SArnaldo Carvalho de Melo 		pid_synthesize_mmap_samples(pid, tgid);
5997c6a1c65SPeter Zijlstra 	} else
6007c6a1c65SPeter Zijlstra 		synthesize_all();
6017c6a1c65SPeter Zijlstra 
60286470930SIngo Molnar 	if (target_pid == -1 && argc) {
60386470930SIngo Molnar 		pid = fork();
60486470930SIngo Molnar 		if (pid < 0)
60586470930SIngo Molnar 			perror("failed to fork");
60686470930SIngo Molnar 
60786470930SIngo Molnar 		if (!pid) {
60886470930SIngo Molnar 			if (execvp(argv[0], (char **)argv)) {
60986470930SIngo Molnar 				perror(argv[0]);
61086470930SIngo Molnar 				exit(-1);
61186470930SIngo Molnar 			}
61286470930SIngo Molnar 		}
61386470930SIngo Molnar 	}
61486470930SIngo Molnar 
61586470930SIngo Molnar 	if (realtime_prio) {
61686470930SIngo Molnar 		struct sched_param param;
61786470930SIngo Molnar 
61886470930SIngo Molnar 		param.sched_priority = realtime_prio;
61986470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
62086470930SIngo Molnar 			printf("Could not set realtime priority.\n");
62186470930SIngo Molnar 			exit(-1);
62286470930SIngo Molnar 		}
62386470930SIngo Molnar 	}
62486470930SIngo Molnar 
625649c48a9SPeter Zijlstra 	for (;;) {
62686470930SIngo Molnar 		int hits = samples;
62786470930SIngo Molnar 
62886470930SIngo Molnar 		for (i = 0; i < nr_cpu; i++) {
629ea57c4f5SIngo Molnar 			for (counter = 0; counter < nr_counters; counter++) {
630ea57c4f5SIngo Molnar 				if (mmap_array[i][counter].base)
63186470930SIngo Molnar 					mmap_read(&mmap_array[i][counter]);
63286470930SIngo Molnar 			}
633ea57c4f5SIngo Molnar 		}
63486470930SIngo Molnar 
635649c48a9SPeter Zijlstra 		if (hits == samples) {
636649c48a9SPeter Zijlstra 			if (done)
637649c48a9SPeter Zijlstra 				break;
638*8b412664SPeter Zijlstra 			ret = poll(event_array, nr_poll, -1);
639*8b412664SPeter Zijlstra 			waking++;
640*8b412664SPeter Zijlstra 		}
641*8b412664SPeter Zijlstra 
642*8b412664SPeter Zijlstra 		if (done) {
643*8b412664SPeter Zijlstra 			for (i = 0; i < nr_cpu; i++) {
644*8b412664SPeter Zijlstra 				for (counter = 0; counter < nr_counters; counter++)
645*8b412664SPeter Zijlstra 					ioctl(fd[i][counter], PERF_COUNTER_IOC_DISABLE);
64686470930SIngo Molnar 			}
647649c48a9SPeter Zijlstra 		}
648*8b412664SPeter Zijlstra 	}
649*8b412664SPeter Zijlstra 
650*8b412664SPeter Zijlstra 	fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
65186470930SIngo Molnar 
65286470930SIngo Molnar 	/*
65386470930SIngo Molnar 	 * Approximate RIP event size: 24 bytes.
65486470930SIngo Molnar 	 */
65586470930SIngo Molnar 	fprintf(stderr,
65686470930SIngo Molnar 		"[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
65786470930SIngo Molnar 		(double)bytes_written / 1024.0 / 1024.0,
65886470930SIngo Molnar 		output_name,
65986470930SIngo Molnar 		bytes_written / 24);
66086470930SIngo Molnar 
66186470930SIngo Molnar 	return 0;
66286470930SIngo Molnar }
66386470930SIngo Molnar 
66486470930SIngo Molnar static const char * const record_usage[] = {
66586470930SIngo Molnar 	"perf record [<options>] [<command>]",
66686470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
66786470930SIngo Molnar 	NULL
66886470930SIngo Molnar };
66986470930SIngo Molnar 
67086470930SIngo Molnar static const struct option options[] = {
67186470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
67286470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
67386470930SIngo Molnar 		     parse_events),
67486470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
67586470930SIngo Molnar 		    "record events on existing pid"),
67686470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
67786470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
678daac07b2SFrederic Weisbecker 	OPT_BOOLEAN('R', "raw-samples", &raw_samples,
679daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
68086470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
68186470930SIngo Molnar 			    "system-wide collection from all CPUs"),
68286470930SIngo Molnar 	OPT_BOOLEAN('A', "append", &append_file,
68386470930SIngo Molnar 			    "append to the output file to do incremental profiling"),
6840a5ac846SJens Axboe 	OPT_INTEGER('C', "profile_cpu", &profile_cpu,
6850a5ac846SJens Axboe 			    "CPU to profile on"),
68686470930SIngo Molnar 	OPT_BOOLEAN('f', "force", &force,
68786470930SIngo Molnar 			"overwrite existing data file"),
68886470930SIngo Molnar 	OPT_LONG('c', "count", &default_interval,
68986470930SIngo Molnar 		    "event period to sample"),
69086470930SIngo Molnar 	OPT_STRING('o', "output", &output_name, "file",
69186470930SIngo Molnar 		    "output file name"),
69286470930SIngo Molnar 	OPT_BOOLEAN('i', "inherit", &inherit,
69386470930SIngo Molnar 		    "child tasks inherit counters"),
69486470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
69586470930SIngo Molnar 		    "profile at this frequency"),
69686470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
69786470930SIngo Molnar 		    "number of mmap data pages"),
6983efa1cc9SIngo Molnar 	OPT_BOOLEAN('g', "call-graph", &call_graph,
6993efa1cc9SIngo Molnar 		    "do call-graph (stack chain/backtrace) recording"),
7003da297a6SIngo Molnar 	OPT_BOOLEAN('v', "verbose", &verbose,
7013da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
702649c48a9SPeter Zijlstra 	OPT_BOOLEAN('s', "stat", &inherit_stat,
703649c48a9SPeter Zijlstra 		    "per thread counts"),
7044bba828dSAnton Blanchard 	OPT_BOOLEAN('d', "data", &sample_address,
7054bba828dSAnton Blanchard 		    "Sample addresses"),
706649c48a9SPeter Zijlstra 	OPT_BOOLEAN('n', "no-samples", &no_samples,
707649c48a9SPeter Zijlstra 		    "don't sample"),
708d1302522SFrederic Weisbecker 	OPT_BOOLEAN('M', "multiplex", &multiplex,
709d1302522SFrederic Weisbecker 		    "multiplex counter output in a single channel"),
71086470930SIngo Molnar 	OPT_END()
71186470930SIngo Molnar };
71286470930SIngo Molnar 
713f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used)
71486470930SIngo Molnar {
71586470930SIngo Molnar 	int counter;
71686470930SIngo Molnar 
717a0541234SAnton Blanchard 	argc = parse_options(argc, argv, options, record_usage,
718a0541234SAnton Blanchard 		PARSE_OPT_STOP_AT_NON_OPTION);
71986470930SIngo Molnar 	if (!argc && target_pid == -1 && !system_wide)
72086470930SIngo Molnar 		usage_with_options(record_usage, options);
72186470930SIngo Molnar 
722bbd36e5eSPeter Zijlstra 	if (!nr_counters) {
72386470930SIngo Molnar 		nr_counters	= 1;
724bbd36e5eSPeter Zijlstra 		attrs[0].type	= PERF_TYPE_HARDWARE;
725bbd36e5eSPeter Zijlstra 		attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
726bbd36e5eSPeter Zijlstra 	}
72786470930SIngo Molnar 
72886470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
72986470930SIngo Molnar 		if (attrs[counter].sample_period)
73086470930SIngo Molnar 			continue;
73186470930SIngo Molnar 
73286470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
73386470930SIngo Molnar 	}
73486470930SIngo Molnar 
73586470930SIngo Molnar 	return __cmd_record(argc, argv);
73686470930SIngo Molnar }
737