xref: /openbmc/linux/tools/perf/builtin-record.c (revision 9df37ddd81f54dd41dc4958055c3a3c9b6840aef)
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;
5186470930SIngo Molnar 
5286470930SIngo Molnar static long			samples;
5386470930SIngo Molnar static struct timeval		last_read;
5486470930SIngo Molnar static struct timeval		this_read;
5586470930SIngo Molnar 
569cffa8d5SPaul Mackerras static u64			bytes_written;
5786470930SIngo Molnar 
5886470930SIngo Molnar static struct pollfd		event_array[MAX_NR_CPUS * MAX_COUNTERS];
5986470930SIngo Molnar 
6086470930SIngo Molnar static int			nr_poll;
6186470930SIngo Molnar static int			nr_cpu;
6286470930SIngo Molnar 
63f5970550SPeter Zijlstra static int			file_new = 1;
647c6a1c65SPeter Zijlstra 
657c6a1c65SPeter Zijlstra struct perf_header		*header;
66f5970550SPeter Zijlstra 
6786470930SIngo Molnar struct mmap_data {
6886470930SIngo Molnar 	int			counter;
6986470930SIngo Molnar 	void			*base;
7086470930SIngo Molnar 	unsigned int		mask;
7186470930SIngo Molnar 	unsigned int		prev;
7286470930SIngo Molnar };
7386470930SIngo Molnar 
7486470930SIngo Molnar static struct mmap_data		mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
7586470930SIngo Molnar 
769d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md)
7786470930SIngo Molnar {
7886470930SIngo Molnar 	struct perf_counter_mmap_page *pc = md->base;
799d91a6f7SPeter Zijlstra 	long head;
8086470930SIngo Molnar 
8186470930SIngo Molnar 	head = pc->data_head;
8286470930SIngo Molnar 	rmb();
8386470930SIngo Molnar 
8486470930SIngo Molnar 	return head;
8586470930SIngo Molnar }
8686470930SIngo Molnar 
879d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
889d91a6f7SPeter Zijlstra {
899d91a6f7SPeter Zijlstra 	struct perf_counter_mmap_page *pc = md->base;
909d91a6f7SPeter Zijlstra 
919d91a6f7SPeter Zijlstra 	/*
929d91a6f7SPeter Zijlstra 	 * ensure all reads are done before we write the tail out.
939d91a6f7SPeter Zijlstra 	 */
949d91a6f7SPeter Zijlstra 	/* mb(); */
959d91a6f7SPeter Zijlstra 	pc->data_tail = tail;
969d91a6f7SPeter Zijlstra }
979d91a6f7SPeter Zijlstra 
98f5970550SPeter Zijlstra static void write_output(void *buf, size_t size)
99f5970550SPeter Zijlstra {
100f5970550SPeter Zijlstra 	while (size) {
101f5970550SPeter Zijlstra 		int ret = write(output, buf, size);
102f5970550SPeter Zijlstra 
103f5970550SPeter Zijlstra 		if (ret < 0)
104f5970550SPeter Zijlstra 			die("failed to write");
105f5970550SPeter Zijlstra 
106f5970550SPeter Zijlstra 		size -= ret;
107f5970550SPeter Zijlstra 		buf += ret;
108f5970550SPeter Zijlstra 
109f5970550SPeter Zijlstra 		bytes_written += ret;
110f5970550SPeter Zijlstra 	}
111f5970550SPeter Zijlstra }
112f5970550SPeter Zijlstra 
11386470930SIngo Molnar static void mmap_read(struct mmap_data *md)
11486470930SIngo Molnar {
11586470930SIngo Molnar 	unsigned int head = mmap_read_head(md);
11686470930SIngo Molnar 	unsigned int old = md->prev;
11786470930SIngo Molnar 	unsigned char *data = md->base + page_size;
11886470930SIngo Molnar 	unsigned long size;
11986470930SIngo Molnar 	void *buf;
12086470930SIngo Molnar 	int diff;
12186470930SIngo Molnar 
12286470930SIngo Molnar 	gettimeofday(&this_read, NULL);
12386470930SIngo Molnar 
12486470930SIngo Molnar 	/*
12586470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
12686470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
12786470930SIngo Molnar 	 *
12886470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
12986470930SIngo Molnar 	 *
13086470930SIngo Molnar 	 * In either case, truncate and restart at head.
13186470930SIngo Molnar 	 */
13286470930SIngo Molnar 	diff = head - old;
1339d91a6f7SPeter Zijlstra 	if (diff < 0) {
13486470930SIngo Molnar 		struct timeval iv;
13586470930SIngo Molnar 		unsigned long msecs;
13686470930SIngo Molnar 
13786470930SIngo Molnar 		timersub(&this_read, &last_read, &iv);
13886470930SIngo Molnar 		msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
13986470930SIngo Molnar 
14086470930SIngo Molnar 		fprintf(stderr, "WARNING: failed to keep up with mmap data."
14186470930SIngo Molnar 				"  Last read %lu msecs ago.\n", msecs);
14286470930SIngo Molnar 
14386470930SIngo Molnar 		/*
14486470930SIngo Molnar 		 * head points to a known good entry, start there.
14586470930SIngo Molnar 		 */
14686470930SIngo Molnar 		old = head;
14786470930SIngo Molnar 	}
14886470930SIngo Molnar 
14986470930SIngo Molnar 	last_read = this_read;
15086470930SIngo Molnar 
15186470930SIngo Molnar 	if (old != head)
15286470930SIngo Molnar 		samples++;
15386470930SIngo Molnar 
15486470930SIngo Molnar 	size = head - old;
15586470930SIngo Molnar 
15686470930SIngo Molnar 	if ((old & md->mask) + size != (head & md->mask)) {
15786470930SIngo Molnar 		buf = &data[old & md->mask];
15886470930SIngo Molnar 		size = md->mask + 1 - (old & md->mask);
15986470930SIngo Molnar 		old += size;
16086470930SIngo Molnar 
161f5970550SPeter Zijlstra 		write_output(buf, size);
16286470930SIngo Molnar 	}
16386470930SIngo Molnar 
16486470930SIngo Molnar 	buf = &data[old & md->mask];
16586470930SIngo Molnar 	size = head - old;
16686470930SIngo Molnar 	old += size;
16786470930SIngo Molnar 
168f5970550SPeter Zijlstra 	write_output(buf, size);
16986470930SIngo Molnar 
17086470930SIngo Molnar 	md->prev = old;
1719d91a6f7SPeter Zijlstra 	mmap_write_tail(md, old);
17286470930SIngo Molnar }
17386470930SIngo Molnar 
17486470930SIngo Molnar static volatile int done = 0;
175f7b7c26eSPeter Zijlstra static volatile int signr = -1;
17686470930SIngo Molnar 
17786470930SIngo Molnar static void sig_handler(int sig)
17886470930SIngo Molnar {
17986470930SIngo Molnar 	done = 1;
180f7b7c26eSPeter Zijlstra 	signr = sig;
181f7b7c26eSPeter Zijlstra }
182f7b7c26eSPeter Zijlstra 
183f7b7c26eSPeter Zijlstra static void sig_atexit(void)
184f7b7c26eSPeter Zijlstra {
185f7b7c26eSPeter Zijlstra 	if (signr == -1)
186f7b7c26eSPeter Zijlstra 		return;
187f7b7c26eSPeter Zijlstra 
188f7b7c26eSPeter Zijlstra 	signal(signr, SIG_DFL);
189f7b7c26eSPeter Zijlstra 	kill(getpid(), signr);
19086470930SIngo Molnar }
19186470930SIngo Molnar 
1922a8083f0SArnaldo Carvalho de Melo static pid_t pid_synthesize_comm_event(pid_t pid, int full)
19386470930SIngo Molnar {
19486470930SIngo Molnar 	struct comm_event comm_ev;
19586470930SIngo Molnar 	char filename[PATH_MAX];
19686470930SIngo Molnar 	char bf[BUFSIZ];
1972a8083f0SArnaldo Carvalho de Melo 	FILE *fp;
1982a8083f0SArnaldo Carvalho de Melo 	size_t size = 0;
19986470930SIngo Molnar 	DIR *tasks;
20086470930SIngo Molnar 	struct dirent dirent, *next;
2012a8083f0SArnaldo Carvalho de Melo 	pid_t tgid = 0;
20286470930SIngo Molnar 
2032a8083f0SArnaldo Carvalho de Melo 	snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
20486470930SIngo Molnar 
2052a8083f0SArnaldo Carvalho de Melo 	fp = fopen(filename, "r");
20639e6dd73SArnaldo Carvalho de Melo 	if (fp == NULL) {
207613d8602SIngo Molnar 		/*
208613d8602SIngo Molnar 		 * We raced with a task exiting - just return:
209613d8602SIngo Molnar 		 */
210613d8602SIngo Molnar 		if (verbose)
21186470930SIngo Molnar 			fprintf(stderr, "couldn't open %s\n", filename);
2122a8083f0SArnaldo Carvalho de Melo 		return 0;
21386470930SIngo Molnar 	}
21486470930SIngo Molnar 
21586470930SIngo Molnar 	memset(&comm_ev, 0, sizeof(comm_ev));
2162a8083f0SArnaldo Carvalho de Melo 	while (!comm_ev.comm[0] || !comm_ev.pid) {
2172a8083f0SArnaldo Carvalho de Melo 		if (fgets(bf, sizeof(bf), fp) == NULL)
21886470930SIngo Molnar 			goto out_failure;
21986470930SIngo Molnar 
2202a8083f0SArnaldo Carvalho de Melo 		if (memcmp(bf, "Name:", 5) == 0) {
2212a8083f0SArnaldo Carvalho de Melo 			char *name = bf + 5;
2222a8083f0SArnaldo Carvalho de Melo 			while (*name && isspace(*name))
2232a8083f0SArnaldo Carvalho de Melo 				++name;
2242a8083f0SArnaldo Carvalho de Melo 			size = strlen(name) - 1;
2252a8083f0SArnaldo Carvalho de Melo 			memcpy(comm_ev.comm, name, size++);
2262a8083f0SArnaldo Carvalho de Melo 		} else if (memcmp(bf, "Tgid:", 5) == 0) {
2272a8083f0SArnaldo Carvalho de Melo 			char *tgids = bf + 5;
2282a8083f0SArnaldo Carvalho de Melo 			while (*tgids && isspace(*tgids))
2292a8083f0SArnaldo Carvalho de Melo 				++tgids;
2302a8083f0SArnaldo Carvalho de Melo 			tgid = comm_ev.pid = atoi(tgids);
2312a8083f0SArnaldo Carvalho de Melo 		}
2322a8083f0SArnaldo Carvalho de Melo 	}
2332a8083f0SArnaldo Carvalho de Melo 
23486470930SIngo Molnar 	comm_ev.header.type = PERF_EVENT_COMM;
2359cffa8d5SPaul Mackerras 	size = ALIGN(size, sizeof(u64));
23686470930SIngo Molnar 	comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
23786470930SIngo Molnar 
23886470930SIngo Molnar 	if (!full) {
23986470930SIngo Molnar 		comm_ev.tid = pid;
24086470930SIngo Molnar 
241f5970550SPeter Zijlstra 		write_output(&comm_ev, comm_ev.header.size);
2422a8083f0SArnaldo Carvalho de Melo 		goto out_fclose;
24386470930SIngo Molnar 	}
24486470930SIngo Molnar 
24586470930SIngo Molnar 	snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
24686470930SIngo Molnar 
24786470930SIngo Molnar 	tasks = opendir(filename);
24886470930SIngo Molnar 	while (!readdir_r(tasks, &dirent, &next) && next) {
24986470930SIngo Molnar 		char *end;
25086470930SIngo Molnar 		pid = strtol(dirent.d_name, &end, 10);
25186470930SIngo Molnar 		if (*end)
25286470930SIngo Molnar 			continue;
25386470930SIngo Molnar 
25486470930SIngo Molnar 		comm_ev.tid = pid;
25586470930SIngo Molnar 
256f5970550SPeter Zijlstra 		write_output(&comm_ev, comm_ev.header.size);
25786470930SIngo Molnar 	}
25886470930SIngo Molnar 	closedir(tasks);
2592a8083f0SArnaldo Carvalho de Melo 
2602a8083f0SArnaldo Carvalho de Melo out_fclose:
2612a8083f0SArnaldo Carvalho de Melo 	fclose(fp);
2622a8083f0SArnaldo Carvalho de Melo 	return tgid;
26386470930SIngo Molnar 
26486470930SIngo Molnar out_failure:
26586470930SIngo Molnar 	fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
26686470930SIngo Molnar 		filename);
26786470930SIngo Molnar 	exit(EXIT_FAILURE);
26886470930SIngo Molnar }
26986470930SIngo Molnar 
2702a8083f0SArnaldo Carvalho de Melo static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid)
27186470930SIngo Molnar {
27286470930SIngo Molnar 	char filename[PATH_MAX];
27386470930SIngo Molnar 	FILE *fp;
27486470930SIngo Molnar 
27586470930SIngo Molnar 	snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
27686470930SIngo Molnar 
27786470930SIngo Molnar 	fp = fopen(filename, "r");
27886470930SIngo Molnar 	if (fp == NULL) {
279613d8602SIngo Molnar 		/*
280613d8602SIngo Molnar 		 * We raced with a task exiting - just return:
281613d8602SIngo Molnar 		 */
282613d8602SIngo Molnar 		if (verbose)
28386470930SIngo Molnar 			fprintf(stderr, "couldn't open %s\n", filename);
284613d8602SIngo Molnar 		return;
28586470930SIngo Molnar 	}
28686470930SIngo Molnar 	while (1) {
28786470930SIngo Molnar 		char bf[BUFSIZ], *pbf = bf;
28886470930SIngo Molnar 		struct mmap_event mmap_ev = {
289f37a291cSIngo Molnar 			.header = { .type = PERF_EVENT_MMAP },
29086470930SIngo Molnar 		};
29186470930SIngo Molnar 		int n;
29286470930SIngo Molnar 		size_t size;
29386470930SIngo Molnar 		if (fgets(bf, sizeof(bf), fp) == NULL)
29486470930SIngo Molnar 			break;
29586470930SIngo Molnar 
29686470930SIngo Molnar 		/* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
29786470930SIngo Molnar 		n = hex2u64(pbf, &mmap_ev.start);
29886470930SIngo Molnar 		if (n < 0)
29986470930SIngo Molnar 			continue;
30086470930SIngo Molnar 		pbf += n + 1;
30186470930SIngo Molnar 		n = hex2u64(pbf, &mmap_ev.len);
30286470930SIngo Molnar 		if (n < 0)
30386470930SIngo Molnar 			continue;
30486470930SIngo Molnar 		pbf += n + 3;
30586470930SIngo Molnar 		if (*pbf == 'x') { /* vm_exec */
30676c64c5eSJohannes Weiner 			char *execname = strchr(bf, '/');
30786470930SIngo Molnar 
30811b5f81eSAnton Blanchard 			/* Catch VDSO */
30911b5f81eSAnton Blanchard 			if (execname == NULL)
31011b5f81eSAnton Blanchard 				execname = strstr(bf, "[vdso]");
31111b5f81eSAnton Blanchard 
31276c64c5eSJohannes Weiner 			if (execname == NULL)
31386470930SIngo Molnar 				continue;
31486470930SIngo Molnar 
31586470930SIngo Molnar 			size = strlen(execname);
31686470930SIngo Molnar 			execname[size - 1] = '\0'; /* Remove \n */
31786470930SIngo Molnar 			memcpy(mmap_ev.filename, execname, size);
3189cffa8d5SPaul Mackerras 			size = ALIGN(size, sizeof(u64));
31986470930SIngo Molnar 			mmap_ev.len -= mmap_ev.start;
32086470930SIngo Molnar 			mmap_ev.header.size = (sizeof(mmap_ev) -
32186470930SIngo Molnar 					       (sizeof(mmap_ev.filename) - size));
3222a8083f0SArnaldo Carvalho de Melo 			mmap_ev.pid = tgid;
32386470930SIngo Molnar 			mmap_ev.tid = pid;
32486470930SIngo Molnar 
325f5970550SPeter Zijlstra 			write_output(&mmap_ev, mmap_ev.header.size);
32686470930SIngo Molnar 		}
32786470930SIngo Molnar 	}
32886470930SIngo Molnar 
32986470930SIngo Molnar 	fclose(fp);
33086470930SIngo Molnar }
33186470930SIngo Molnar 
3327c6a1c65SPeter Zijlstra static void synthesize_all(void)
33386470930SIngo Molnar {
33486470930SIngo Molnar 	DIR *proc;
33586470930SIngo Molnar 	struct dirent dirent, *next;
33686470930SIngo Molnar 
33786470930SIngo Molnar 	proc = opendir("/proc");
33886470930SIngo Molnar 
33986470930SIngo Molnar 	while (!readdir_r(proc, &dirent, &next) && next) {
34086470930SIngo Molnar 		char *end;
3412a8083f0SArnaldo Carvalho de Melo 		pid_t pid, tgid;
34286470930SIngo Molnar 
34386470930SIngo Molnar 		pid = strtol(dirent.d_name, &end, 10);
34486470930SIngo Molnar 		if (*end) /* only interested in proper numerical dirents */
34586470930SIngo Molnar 			continue;
34686470930SIngo Molnar 
3472a8083f0SArnaldo Carvalho de Melo 		tgid = pid_synthesize_comm_event(pid, 1);
3482a8083f0SArnaldo Carvalho de Melo 		pid_synthesize_mmap_samples(pid, tgid);
34986470930SIngo Molnar 	}
35086470930SIngo Molnar 
35186470930SIngo Molnar 	closedir(proc);
35286470930SIngo Molnar }
35386470930SIngo Molnar 
35486470930SIngo Molnar static int group_fd;
35586470930SIngo Molnar 
3567c6a1c65SPeter Zijlstra static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
3577c6a1c65SPeter Zijlstra {
3587c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
3597c6a1c65SPeter Zijlstra 
3607c6a1c65SPeter Zijlstra 	if (nr < header->attrs) {
3617c6a1c65SPeter Zijlstra 		h_attr = header->attr[nr];
3627c6a1c65SPeter Zijlstra 	} else {
3637c6a1c65SPeter Zijlstra 		h_attr = perf_header_attr__new(a);
3647c6a1c65SPeter Zijlstra 		perf_header__add_attr(header, h_attr);
3657c6a1c65SPeter Zijlstra 	}
3667c6a1c65SPeter Zijlstra 
3677c6a1c65SPeter Zijlstra 	return h_attr;
3687c6a1c65SPeter Zijlstra }
3697c6a1c65SPeter Zijlstra 
37086470930SIngo Molnar static void create_counter(int counter, int cpu, pid_t pid)
37186470930SIngo Molnar {
37286470930SIngo Molnar 	struct perf_counter_attr *attr = attrs + counter;
3737c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
3747c6a1c65SPeter Zijlstra 	int track = !counter; /* only the first counter needs these */
3757c6a1c65SPeter Zijlstra 	struct {
3767c6a1c65SPeter Zijlstra 		u64 count;
3777c6a1c65SPeter Zijlstra 		u64 time_enabled;
3787c6a1c65SPeter Zijlstra 		u64 time_running;
3797c6a1c65SPeter Zijlstra 		u64 id;
3807c6a1c65SPeter Zijlstra 	} read_data;
3817c6a1c65SPeter Zijlstra 
3827c6a1c65SPeter Zijlstra 	attr->read_format	= PERF_FORMAT_TOTAL_TIME_ENABLED |
3837c6a1c65SPeter Zijlstra 				  PERF_FORMAT_TOTAL_TIME_RUNNING |
3847c6a1c65SPeter Zijlstra 				  PERF_FORMAT_ID;
38586470930SIngo Molnar 
3863a9f131fSFrederic Weisbecker 	attr->sample_type	|= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
3873efa1cc9SIngo Molnar 
38886470930SIngo Molnar 	if (freq) {
389ea1900e5SPeter Zijlstra 		attr->sample_type	|= PERF_SAMPLE_PERIOD;
39086470930SIngo Molnar 		attr->freq		= 1;
39186470930SIngo Molnar 		attr->sample_freq	= freq;
39286470930SIngo Molnar 	}
3933efa1cc9SIngo Molnar 
394649c48a9SPeter Zijlstra 	if (no_samples)
395649c48a9SPeter Zijlstra 		attr->sample_freq = 0;
396649c48a9SPeter Zijlstra 
397649c48a9SPeter Zijlstra 	if (inherit_stat)
398649c48a9SPeter Zijlstra 		attr->inherit_stat = 1;
399649c48a9SPeter Zijlstra 
4004bba828dSAnton Blanchard 	if (sample_address)
4014bba828dSAnton Blanchard 		attr->sample_type	|= PERF_SAMPLE_ADDR;
4024bba828dSAnton Blanchard 
4033efa1cc9SIngo Molnar 	if (call_graph)
4043efa1cc9SIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
4053efa1cc9SIngo Molnar 
406daac07b2SFrederic Weisbecker 	if (raw_samples)
407daac07b2SFrederic Weisbecker 		attr->sample_type	|= PERF_SAMPLE_RAW;
408f413cdb8SFrederic Weisbecker 
40986470930SIngo Molnar 	attr->mmap		= track;
41086470930SIngo Molnar 	attr->comm		= track;
41186470930SIngo Molnar 	attr->inherit		= (cpu < 0) && inherit;
4124502d77cSPeter Zijlstra 	attr->disabled		= 1;
41386470930SIngo Molnar 
4143da297a6SIngo Molnar try_again:
41586470930SIngo Molnar 	fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
41686470930SIngo Molnar 
41786470930SIngo Molnar 	if (fd[nr_cpu][counter] < 0) {
41886470930SIngo Molnar 		int err = errno;
41986470930SIngo Molnar 
42086470930SIngo Molnar 		if (err == EPERM)
4213da297a6SIngo Molnar 			die("Permission error - are you root?\n");
4220a5ac846SJens Axboe 		else if (err ==  ENODEV && profile_cpu != -1)
4230a5ac846SJens Axboe 			die("No such device - did you specify an out-of-range profile CPU?\n");
4243da297a6SIngo Molnar 
4253da297a6SIngo Molnar 		/*
4263da297a6SIngo Molnar 		 * If it's cycles then fall back to hrtimer
4273da297a6SIngo Molnar 		 * based cpu-clock-tick sw counter, which
4283da297a6SIngo Molnar 		 * is always available even if no PMU support:
4293da297a6SIngo Molnar 		 */
4303da297a6SIngo Molnar 		if (attr->type == PERF_TYPE_HARDWARE
431f4dbfa8fSPeter Zijlstra 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
4323da297a6SIngo Molnar 
4333da297a6SIngo Molnar 			if (verbose)
4343da297a6SIngo Molnar 				warning(" ... trying to fall back to cpu-clock-ticks\n");
4353da297a6SIngo Molnar 			attr->type = PERF_TYPE_SOFTWARE;
436f4dbfa8fSPeter Zijlstra 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
4373da297a6SIngo Molnar 			goto try_again;
4383da297a6SIngo Molnar 		}
43930c806a0SIngo Molnar 		printf("\n");
44030c806a0SIngo Molnar 		error("perfcounter syscall returned with %d (%s)\n",
44130c806a0SIngo Molnar 			fd[nr_cpu][counter], strerror(err));
44230c806a0SIngo Molnar 		die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
44386470930SIngo Molnar 		exit(-1);
44486470930SIngo Molnar 	}
4453da297a6SIngo Molnar 
4467c6a1c65SPeter Zijlstra 	h_attr = get_header_attr(attr, counter);
4477c6a1c65SPeter Zijlstra 
4487c6a1c65SPeter Zijlstra 	if (!file_new) {
4497c6a1c65SPeter Zijlstra 		if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
4507c6a1c65SPeter Zijlstra 			fprintf(stderr, "incompatible append\n");
4517c6a1c65SPeter Zijlstra 			exit(-1);
4527c6a1c65SPeter Zijlstra 		}
4537c6a1c65SPeter Zijlstra 	}
4547c6a1c65SPeter Zijlstra 
4553928ddbeSFrederic Weisbecker 	if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
4563928ddbeSFrederic Weisbecker 		perror("Unable to read perf file descriptor\n");
4573928ddbeSFrederic Weisbecker 		exit(-1);
4583928ddbeSFrederic Weisbecker 	}
4597c6a1c65SPeter Zijlstra 
4607c6a1c65SPeter Zijlstra 	perf_header_attr__add_id(h_attr, read_data.id);
4617c6a1c65SPeter Zijlstra 
46286470930SIngo Molnar 	assert(fd[nr_cpu][counter] >= 0);
46386470930SIngo Molnar 	fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
46486470930SIngo Molnar 
46586470930SIngo Molnar 	/*
46686470930SIngo Molnar 	 * First counter acts as the group leader:
46786470930SIngo Molnar 	 */
46886470930SIngo Molnar 	if (group && group_fd == -1)
46986470930SIngo Molnar 		group_fd = fd[nr_cpu][counter];
47086470930SIngo Molnar 
47186470930SIngo Molnar 	event_array[nr_poll].fd = fd[nr_cpu][counter];
47286470930SIngo Molnar 	event_array[nr_poll].events = POLLIN;
47386470930SIngo Molnar 	nr_poll++;
47486470930SIngo Molnar 
47586470930SIngo Molnar 	mmap_array[nr_cpu][counter].counter = counter;
47686470930SIngo Molnar 	mmap_array[nr_cpu][counter].prev = 0;
47786470930SIngo Molnar 	mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
47886470930SIngo Molnar 	mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
4799d91a6f7SPeter Zijlstra 			PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
48086470930SIngo Molnar 	if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
48186470930SIngo Molnar 		error("failed to mmap with %d (%s)\n", errno, strerror(errno));
48286470930SIngo Molnar 		exit(-1);
48386470930SIngo Molnar 	}
4844502d77cSPeter Zijlstra 
4854502d77cSPeter Zijlstra 	ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE);
48686470930SIngo Molnar }
48786470930SIngo Molnar 
48886470930SIngo Molnar static void open_counters(int cpu, pid_t pid)
48986470930SIngo Molnar {
49086470930SIngo Molnar 	int counter;
49186470930SIngo Molnar 
49286470930SIngo Molnar 	group_fd = -1;
49386470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++)
49486470930SIngo Molnar 		create_counter(counter, cpu, pid);
49586470930SIngo Molnar 
49686470930SIngo Molnar 	nr_cpu++;
49786470930SIngo Molnar }
49886470930SIngo Molnar 
499f5970550SPeter Zijlstra static void atexit_header(void)
500f5970550SPeter Zijlstra {
5017c6a1c65SPeter Zijlstra 	header->data_size += bytes_written;
502f5970550SPeter Zijlstra 
5037c6a1c65SPeter Zijlstra 	perf_header__write(header, output);
504f5970550SPeter Zijlstra }
505f5970550SPeter Zijlstra 
50686470930SIngo Molnar static int __cmd_record(int argc, const char **argv)
50786470930SIngo Molnar {
50886470930SIngo Molnar 	int i, counter;
50986470930SIngo Molnar 	struct stat st;
5107c6a1c65SPeter Zijlstra 	pid_t pid = 0;
51186470930SIngo Molnar 	int flags;
51286470930SIngo Molnar 	int ret;
51386470930SIngo Molnar 
51486470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
51586470930SIngo Molnar 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
51686470930SIngo Molnar 	assert(nr_cpus <= MAX_NR_CPUS);
51786470930SIngo Molnar 	assert(nr_cpus >= 0);
51886470930SIngo Molnar 
519f5970550SPeter Zijlstra 	atexit(sig_atexit);
520f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
521f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
522f5970550SPeter Zijlstra 
523266e0e21SPierre Habouzit 	if (!stat(output_name, &st) && st.st_size) {
524266e0e21SPierre Habouzit 		if (!force && !append_file) {
52586470930SIngo Molnar 			fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
52686470930SIngo Molnar 					output_name);
52786470930SIngo Molnar 			exit(-1);
52886470930SIngo Molnar 		}
529266e0e21SPierre Habouzit 	} else {
530266e0e21SPierre Habouzit 		append_file = 0;
531266e0e21SPierre Habouzit 	}
53286470930SIngo Molnar 
53386470930SIngo Molnar 	flags = O_CREAT|O_RDWR;
53486470930SIngo Molnar 	if (append_file)
535f5970550SPeter Zijlstra 		file_new = 0;
53686470930SIngo Molnar 	else
53786470930SIngo Molnar 		flags |= O_TRUNC;
53886470930SIngo Molnar 
53986470930SIngo Molnar 	output = open(output_name, flags, S_IRUSR|S_IWUSR);
54086470930SIngo Molnar 	if (output < 0) {
54186470930SIngo Molnar 		perror("failed to create output file");
54286470930SIngo Molnar 		exit(-1);
54386470930SIngo Molnar 	}
54486470930SIngo Molnar 
5457c6a1c65SPeter Zijlstra 	if (!file_new)
5467c6a1c65SPeter Zijlstra 		header = perf_header__read(output);
5477c6a1c65SPeter Zijlstra 	else
5487c6a1c65SPeter Zijlstra 		header = perf_header__new();
549f5970550SPeter Zijlstra 
550*9df37dddSFrederic Weisbecker 
551*9df37dddSFrederic Weisbecker 	if (raw_samples) {
552*9df37dddSFrederic Weisbecker 		read_tracing_data();
553*9df37dddSFrederic Weisbecker 	} else {
554*9df37dddSFrederic Weisbecker 		for (i = 0; i < nr_counters; i++) {
555*9df37dddSFrederic Weisbecker 			if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
556*9df37dddSFrederic Weisbecker 				read_tracing_data();
557*9df37dddSFrederic Weisbecker 				break;
558*9df37dddSFrederic Weisbecker 			}
559*9df37dddSFrederic Weisbecker 		}
560*9df37dddSFrederic Weisbecker 	}
561f5970550SPeter Zijlstra 	atexit(atexit_header);
562f5970550SPeter Zijlstra 
56386470930SIngo Molnar 	if (!system_wide) {
5647c6a1c65SPeter Zijlstra 		pid = target_pid;
5657c6a1c65SPeter Zijlstra 		if (pid == -1)
5667c6a1c65SPeter Zijlstra 			pid = getpid();
5677c6a1c65SPeter Zijlstra 
5680a5ac846SJens Axboe 		open_counters(profile_cpu, pid);
5690a5ac846SJens Axboe 	} else {
5700a5ac846SJens Axboe 		if (profile_cpu != -1) {
5710a5ac846SJens Axboe 			open_counters(profile_cpu, target_pid);
5720a5ac846SJens Axboe 		} else {
5730a5ac846SJens Axboe 			for (i = 0; i < nr_cpus; i++)
57486470930SIngo Molnar 				open_counters(i, target_pid);
5750a5ac846SJens Axboe 		}
5760a5ac846SJens Axboe 	}
57786470930SIngo Molnar 
5787c6a1c65SPeter Zijlstra 	if (file_new)
5797c6a1c65SPeter Zijlstra 		perf_header__write(header, output);
5807c6a1c65SPeter Zijlstra 
5817c6a1c65SPeter Zijlstra 	if (!system_wide) {
5822a8083f0SArnaldo Carvalho de Melo 		pid_t tgid = pid_synthesize_comm_event(pid, 0);
5832a8083f0SArnaldo Carvalho de Melo 		pid_synthesize_mmap_samples(pid, tgid);
5847c6a1c65SPeter Zijlstra 	} else
5857c6a1c65SPeter Zijlstra 		synthesize_all();
5867c6a1c65SPeter Zijlstra 
58786470930SIngo Molnar 	if (target_pid == -1 && argc) {
58886470930SIngo Molnar 		pid = fork();
58986470930SIngo Molnar 		if (pid < 0)
59086470930SIngo Molnar 			perror("failed to fork");
59186470930SIngo Molnar 
59286470930SIngo Molnar 		if (!pid) {
59386470930SIngo Molnar 			if (execvp(argv[0], (char **)argv)) {
59486470930SIngo Molnar 				perror(argv[0]);
59586470930SIngo Molnar 				exit(-1);
59686470930SIngo Molnar 			}
59786470930SIngo Molnar 		}
59886470930SIngo Molnar 	}
59986470930SIngo Molnar 
60086470930SIngo Molnar 	if (realtime_prio) {
60186470930SIngo Molnar 		struct sched_param param;
60286470930SIngo Molnar 
60386470930SIngo Molnar 		param.sched_priority = realtime_prio;
60486470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
60586470930SIngo Molnar 			printf("Could not set realtime priority.\n");
60686470930SIngo Molnar 			exit(-1);
60786470930SIngo Molnar 		}
60886470930SIngo Molnar 	}
60986470930SIngo Molnar 
610649c48a9SPeter Zijlstra 	for (;;) {
61186470930SIngo Molnar 		int hits = samples;
61286470930SIngo Molnar 
61386470930SIngo Molnar 		for (i = 0; i < nr_cpu; i++) {
61486470930SIngo Molnar 			for (counter = 0; counter < nr_counters; counter++)
61586470930SIngo Molnar 				mmap_read(&mmap_array[i][counter]);
61686470930SIngo Molnar 		}
61786470930SIngo Molnar 
618649c48a9SPeter Zijlstra 		if (hits == samples) {
619649c48a9SPeter Zijlstra 			if (done)
620649c48a9SPeter Zijlstra 				break;
62186470930SIngo Molnar 			ret = poll(event_array, nr_poll, 100);
62286470930SIngo Molnar 		}
623649c48a9SPeter Zijlstra 	}
62486470930SIngo Molnar 
62586470930SIngo Molnar 	/*
62686470930SIngo Molnar 	 * Approximate RIP event size: 24 bytes.
62786470930SIngo Molnar 	 */
62886470930SIngo Molnar 	fprintf(stderr,
62986470930SIngo Molnar 		"[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
63086470930SIngo Molnar 		(double)bytes_written / 1024.0 / 1024.0,
63186470930SIngo Molnar 		output_name,
63286470930SIngo Molnar 		bytes_written / 24);
63386470930SIngo Molnar 
63486470930SIngo Molnar 	return 0;
63586470930SIngo Molnar }
63686470930SIngo Molnar 
63786470930SIngo Molnar static const char * const record_usage[] = {
63886470930SIngo Molnar 	"perf record [<options>] [<command>]",
63986470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
64086470930SIngo Molnar 	NULL
64186470930SIngo Molnar };
64286470930SIngo Molnar 
64386470930SIngo Molnar static const struct option options[] = {
64486470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
64586470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
64686470930SIngo Molnar 		     parse_events),
64786470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
64886470930SIngo Molnar 		    "record events on existing pid"),
64986470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
65086470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
651daac07b2SFrederic Weisbecker 	OPT_BOOLEAN('R', "raw-samples", &raw_samples,
652daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
65386470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
65486470930SIngo Molnar 			    "system-wide collection from all CPUs"),
65586470930SIngo Molnar 	OPT_BOOLEAN('A', "append", &append_file,
65686470930SIngo Molnar 			    "append to the output file to do incremental profiling"),
6570a5ac846SJens Axboe 	OPT_INTEGER('C', "profile_cpu", &profile_cpu,
6580a5ac846SJens Axboe 			    "CPU to profile on"),
65986470930SIngo Molnar 	OPT_BOOLEAN('f', "force", &force,
66086470930SIngo Molnar 			"overwrite existing data file"),
66186470930SIngo Molnar 	OPT_LONG('c', "count", &default_interval,
66286470930SIngo Molnar 		    "event period to sample"),
66386470930SIngo Molnar 	OPT_STRING('o', "output", &output_name, "file",
66486470930SIngo Molnar 		    "output file name"),
66586470930SIngo Molnar 	OPT_BOOLEAN('i', "inherit", &inherit,
66686470930SIngo Molnar 		    "child tasks inherit counters"),
66786470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
66886470930SIngo Molnar 		    "profile at this frequency"),
66986470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
67086470930SIngo Molnar 		    "number of mmap data pages"),
6713efa1cc9SIngo Molnar 	OPT_BOOLEAN('g', "call-graph", &call_graph,
6723efa1cc9SIngo Molnar 		    "do call-graph (stack chain/backtrace) recording"),
6733da297a6SIngo Molnar 	OPT_BOOLEAN('v', "verbose", &verbose,
6743da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
675649c48a9SPeter Zijlstra 	OPT_BOOLEAN('s', "stat", &inherit_stat,
676649c48a9SPeter Zijlstra 		    "per thread counts"),
6774bba828dSAnton Blanchard 	OPT_BOOLEAN('d', "data", &sample_address,
6784bba828dSAnton Blanchard 		    "Sample addresses"),
679649c48a9SPeter Zijlstra 	OPT_BOOLEAN('n', "no-samples", &no_samples,
680649c48a9SPeter Zijlstra 		    "don't sample"),
68186470930SIngo Molnar 	OPT_END()
68286470930SIngo Molnar };
68386470930SIngo Molnar 
684f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used)
68586470930SIngo Molnar {
68686470930SIngo Molnar 	int counter;
68786470930SIngo Molnar 
688a0541234SAnton Blanchard 	argc = parse_options(argc, argv, options, record_usage,
689a0541234SAnton Blanchard 		PARSE_OPT_STOP_AT_NON_OPTION);
69086470930SIngo Molnar 	if (!argc && target_pid == -1 && !system_wide)
69186470930SIngo Molnar 		usage_with_options(record_usage, options);
69286470930SIngo Molnar 
693bbd36e5eSPeter Zijlstra 	if (!nr_counters) {
69486470930SIngo Molnar 		nr_counters	= 1;
695bbd36e5eSPeter Zijlstra 		attrs[0].type	= PERF_TYPE_HARDWARE;
696bbd36e5eSPeter Zijlstra 		attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
697bbd36e5eSPeter Zijlstra 	}
69886470930SIngo Molnar 
69986470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
70086470930SIngo Molnar 		if (attrs[counter].sample_period)
70186470930SIngo Molnar 			continue;
70286470930SIngo Molnar 
70386470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
70486470930SIngo Molnar 	}
70586470930SIngo Molnar 
70686470930SIngo Molnar 	return __cmd_record(argc, argv);
70786470930SIngo Molnar }
708