xref: /openbmc/linux/tools/perf/builtin-record.c (revision 234fbbf508c58c5084292b11b242377553897459)
186470930SIngo Molnar /*
286470930SIngo Molnar  * builtin-record.c
386470930SIngo Molnar  *
486470930SIngo Molnar  * Builtin record command: Record the profile of a workload
586470930SIngo Molnar  * (or a CPU, or a PID) into the perf.data output file - for
686470930SIngo Molnar  * later analysis via perf report.
786470930SIngo Molnar  */
886470930SIngo Molnar #include "builtin.h"
986470930SIngo Molnar 
1086470930SIngo Molnar #include "perf.h"
1186470930SIngo Molnar 
1286470930SIngo Molnar #include "util/util.h"
1386470930SIngo Molnar #include "util/parse-options.h"
1486470930SIngo Molnar #include "util/parse-events.h"
1586470930SIngo Molnar #include "util/string.h"
1686470930SIngo Molnar 
177c6a1c65SPeter Zijlstra #include "util/header.h"
1866e274f3SFrederic Weisbecker #include "util/event.h"
198f28827aSFrederic Weisbecker #include "util/debug.h"
207c6a1c65SPeter Zijlstra 
2186470930SIngo Molnar #include <unistd.h>
2286470930SIngo Molnar #include <sched.h>
2386470930SIngo Molnar 
2486470930SIngo Molnar static int			fd[MAX_NR_CPUS][MAX_COUNTERS];
2586470930SIngo Molnar 
267e4ff9e3SMike Galbraith static long			default_interval		=      0;
2786470930SIngo Molnar 
2886470930SIngo Molnar static int			nr_cpus				=      0;
2986470930SIngo Molnar static unsigned int		page_size;
3086470930SIngo Molnar static unsigned int		mmap_pages			=    128;
3142e59d7dSIngo Molnar static int			freq				=   1000;
3286470930SIngo Molnar static int			output;
3386470930SIngo Molnar static const char		*output_name			= "perf.data";
3486470930SIngo Molnar static int			group				=      0;
3586470930SIngo Molnar static unsigned int		realtime_prio			=      0;
36daac07b2SFrederic Weisbecker static int			raw_samples			=      0;
3786470930SIngo Molnar static int			system_wide			=      0;
380a5ac846SJens Axboe static int			profile_cpu			=     -1;
3986470930SIngo Molnar static pid_t			target_pid			=     -1;
40933da83aSChris Wilson static pid_t			child_pid			=     -1;
4186470930SIngo Molnar static int			inherit				=      1;
4286470930SIngo Molnar static int			force				=      0;
4386470930SIngo Molnar static int			append_file			=      0;
443efa1cc9SIngo Molnar static int			call_graph			=      0;
45649c48a9SPeter Zijlstra static int			inherit_stat			=      0;
46649c48a9SPeter Zijlstra static int			no_samples			=      0;
474bba828dSAnton Blanchard static int			sample_address			=      0;
48d1302522SFrederic Weisbecker static int			multiplex			=      0;
49ea57c4f5SIngo Molnar static int			multiplex_fd			=     -1;
5086470930SIngo Molnar 
5142e59d7dSIngo Molnar static long			samples				=      0;
5286470930SIngo Molnar static struct timeval		last_read;
5386470930SIngo Molnar static struct timeval		this_read;
5486470930SIngo Molnar 
5542e59d7dSIngo Molnar static u64			bytes_written			=      0;
5686470930SIngo Molnar 
5786470930SIngo Molnar static struct pollfd		event_array[MAX_NR_CPUS * MAX_COUNTERS];
5886470930SIngo Molnar 
5942e59d7dSIngo Molnar static int			nr_poll				=      0;
6042e59d7dSIngo Molnar static int			nr_cpu				=      0;
6186470930SIngo Molnar 
62f5970550SPeter Zijlstra static int			file_new			=      1;
637c6a1c65SPeter Zijlstra 
6442e59d7dSIngo Molnar struct perf_header		*header				=   NULL;
65f5970550SPeter Zijlstra 
6686470930SIngo Molnar struct mmap_data {
6786470930SIngo Molnar 	int			counter;
6886470930SIngo Molnar 	void			*base;
6986470930SIngo Molnar 	unsigned int		mask;
7086470930SIngo Molnar 	unsigned int		prev;
7186470930SIngo Molnar };
7286470930SIngo Molnar 
7386470930SIngo Molnar static struct mmap_data		mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
7486470930SIngo Molnar 
759d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md)
7686470930SIngo Molnar {
77cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
789d91a6f7SPeter Zijlstra 	long head;
7986470930SIngo Molnar 
8086470930SIngo Molnar 	head = pc->data_head;
8186470930SIngo Molnar 	rmb();
8286470930SIngo Molnar 
8386470930SIngo Molnar 	return head;
8486470930SIngo Molnar }
8586470930SIngo Molnar 
869d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
879d91a6f7SPeter Zijlstra {
88cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
899d91a6f7SPeter Zijlstra 
909d91a6f7SPeter Zijlstra 	/*
919d91a6f7SPeter Zijlstra 	 * ensure all reads are done before we write the tail out.
929d91a6f7SPeter Zijlstra 	 */
939d91a6f7SPeter Zijlstra 	/* mb(); */
949d91a6f7SPeter Zijlstra 	pc->data_tail = tail;
959d91a6f7SPeter Zijlstra }
969d91a6f7SPeter Zijlstra 
97f5970550SPeter Zijlstra static void write_output(void *buf, size_t size)
98f5970550SPeter Zijlstra {
99f5970550SPeter Zijlstra 	while (size) {
100f5970550SPeter Zijlstra 		int ret = write(output, buf, size);
101f5970550SPeter Zijlstra 
102f5970550SPeter Zijlstra 		if (ret < 0)
103f5970550SPeter Zijlstra 			die("failed to write");
104f5970550SPeter Zijlstra 
105f5970550SPeter Zijlstra 		size -= ret;
106f5970550SPeter Zijlstra 		buf += ret;
107f5970550SPeter Zijlstra 
108f5970550SPeter Zijlstra 		bytes_written += ret;
109f5970550SPeter Zijlstra 	}
110f5970550SPeter Zijlstra }
111f5970550SPeter Zijlstra 
112*234fbbf5SArnaldo Carvalho de Melo static int process_synthesized_event(event_t *event)
113*234fbbf5SArnaldo Carvalho de Melo {
114*234fbbf5SArnaldo Carvalho de Melo 	write_output(event, event->header.size);
115*234fbbf5SArnaldo Carvalho de Melo 	return 0;
116*234fbbf5SArnaldo Carvalho de Melo }
117*234fbbf5SArnaldo Carvalho de Melo 
11886470930SIngo Molnar static void mmap_read(struct mmap_data *md)
11986470930SIngo Molnar {
12086470930SIngo Molnar 	unsigned int head = mmap_read_head(md);
12186470930SIngo Molnar 	unsigned int old = md->prev;
12286470930SIngo Molnar 	unsigned char *data = md->base + page_size;
12386470930SIngo Molnar 	unsigned long size;
12486470930SIngo Molnar 	void *buf;
12586470930SIngo Molnar 	int diff;
12686470930SIngo Molnar 
12786470930SIngo Molnar 	gettimeofday(&this_read, NULL);
12886470930SIngo Molnar 
12986470930SIngo Molnar 	/*
13086470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
13186470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
13286470930SIngo Molnar 	 *
13386470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
13486470930SIngo Molnar 	 *
13586470930SIngo Molnar 	 * In either case, truncate and restart at head.
13686470930SIngo Molnar 	 */
13786470930SIngo Molnar 	diff = head - old;
1389d91a6f7SPeter Zijlstra 	if (diff < 0) {
13986470930SIngo Molnar 		struct timeval iv;
14086470930SIngo Molnar 		unsigned long msecs;
14186470930SIngo Molnar 
14286470930SIngo Molnar 		timersub(&this_read, &last_read, &iv);
14386470930SIngo Molnar 		msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
14486470930SIngo Molnar 
14586470930SIngo Molnar 		fprintf(stderr, "WARNING: failed to keep up with mmap data."
14686470930SIngo Molnar 				"  Last read %lu msecs ago.\n", msecs);
14786470930SIngo Molnar 
14886470930SIngo Molnar 		/*
14986470930SIngo Molnar 		 * head points to a known good entry, start there.
15086470930SIngo Molnar 		 */
15186470930SIngo Molnar 		old = head;
15286470930SIngo Molnar 	}
15386470930SIngo Molnar 
15486470930SIngo Molnar 	last_read = this_read;
15586470930SIngo Molnar 
15686470930SIngo Molnar 	if (old != head)
15786470930SIngo Molnar 		samples++;
15886470930SIngo Molnar 
15986470930SIngo Molnar 	size = head - old;
16086470930SIngo Molnar 
16186470930SIngo Molnar 	if ((old & md->mask) + size != (head & md->mask)) {
16286470930SIngo Molnar 		buf = &data[old & md->mask];
16386470930SIngo Molnar 		size = md->mask + 1 - (old & md->mask);
16486470930SIngo Molnar 		old += size;
16586470930SIngo Molnar 
166f5970550SPeter Zijlstra 		write_output(buf, size);
16786470930SIngo Molnar 	}
16886470930SIngo Molnar 
16986470930SIngo Molnar 	buf = &data[old & md->mask];
17086470930SIngo Molnar 	size = head - old;
17186470930SIngo Molnar 	old += size;
17286470930SIngo Molnar 
173f5970550SPeter Zijlstra 	write_output(buf, size);
17486470930SIngo Molnar 
17586470930SIngo Molnar 	md->prev = old;
1769d91a6f7SPeter Zijlstra 	mmap_write_tail(md, old);
17786470930SIngo Molnar }
17886470930SIngo Molnar 
17986470930SIngo Molnar static volatile int done = 0;
180f7b7c26eSPeter Zijlstra static volatile int signr = -1;
18186470930SIngo Molnar 
18286470930SIngo Molnar static void sig_handler(int sig)
18386470930SIngo Molnar {
18486470930SIngo Molnar 	done = 1;
185f7b7c26eSPeter Zijlstra 	signr = sig;
186f7b7c26eSPeter Zijlstra }
187f7b7c26eSPeter Zijlstra 
188f7b7c26eSPeter Zijlstra static void sig_atexit(void)
189f7b7c26eSPeter Zijlstra {
190933da83aSChris Wilson 	if (child_pid != -1)
191933da83aSChris Wilson 		kill(child_pid, SIGTERM);
192933da83aSChris Wilson 
193f7b7c26eSPeter Zijlstra 	if (signr == -1)
194f7b7c26eSPeter Zijlstra 		return;
195f7b7c26eSPeter Zijlstra 
196f7b7c26eSPeter Zijlstra 	signal(signr, SIG_DFL);
197f7b7c26eSPeter Zijlstra 	kill(getpid(), signr);
19886470930SIngo Molnar }
19986470930SIngo Molnar 
20086470930SIngo Molnar static int group_fd;
20186470930SIngo Molnar 
202cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
2037c6a1c65SPeter Zijlstra {
2047c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
2057c6a1c65SPeter Zijlstra 
2067c6a1c65SPeter Zijlstra 	if (nr < header->attrs) {
2077c6a1c65SPeter Zijlstra 		h_attr = header->attr[nr];
2087c6a1c65SPeter Zijlstra 	} else {
2097c6a1c65SPeter Zijlstra 		h_attr = perf_header_attr__new(a);
2107c6a1c65SPeter Zijlstra 		perf_header__add_attr(header, h_attr);
2117c6a1c65SPeter Zijlstra 	}
2127c6a1c65SPeter Zijlstra 
2137c6a1c65SPeter Zijlstra 	return h_attr;
2147c6a1c65SPeter Zijlstra }
2157c6a1c65SPeter Zijlstra 
21686470930SIngo Molnar static void create_counter(int counter, int cpu, pid_t pid)
21786470930SIngo Molnar {
218c171b552SLi Zefan 	char *filter = filters[counter];
219cdd6c482SIngo Molnar 	struct perf_event_attr *attr = attrs + counter;
2207c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
2217c6a1c65SPeter Zijlstra 	int track = !counter; /* only the first counter needs these */
222c171b552SLi Zefan 	int ret;
2237c6a1c65SPeter Zijlstra 	struct {
2247c6a1c65SPeter Zijlstra 		u64 count;
2257c6a1c65SPeter Zijlstra 		u64 time_enabled;
2267c6a1c65SPeter Zijlstra 		u64 time_running;
2277c6a1c65SPeter Zijlstra 		u64 id;
2287c6a1c65SPeter Zijlstra 	} read_data;
2297c6a1c65SPeter Zijlstra 
2307c6a1c65SPeter Zijlstra 	attr->read_format	= PERF_FORMAT_TOTAL_TIME_ENABLED |
2317c6a1c65SPeter Zijlstra 				  PERF_FORMAT_TOTAL_TIME_RUNNING |
2327c6a1c65SPeter Zijlstra 				  PERF_FORMAT_ID;
23386470930SIngo Molnar 
2343a9f131fSFrederic Weisbecker 	attr->sample_type	|= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
2353efa1cc9SIngo Molnar 
23686470930SIngo Molnar 	if (freq) {
237ea1900e5SPeter Zijlstra 		attr->sample_type	|= PERF_SAMPLE_PERIOD;
23886470930SIngo Molnar 		attr->freq		= 1;
23986470930SIngo Molnar 		attr->sample_freq	= freq;
24086470930SIngo Molnar 	}
2413efa1cc9SIngo Molnar 
242649c48a9SPeter Zijlstra 	if (no_samples)
243649c48a9SPeter Zijlstra 		attr->sample_freq = 0;
244649c48a9SPeter Zijlstra 
245649c48a9SPeter Zijlstra 	if (inherit_stat)
246649c48a9SPeter Zijlstra 		attr->inherit_stat = 1;
247649c48a9SPeter Zijlstra 
2484bba828dSAnton Blanchard 	if (sample_address)
2494bba828dSAnton Blanchard 		attr->sample_type	|= PERF_SAMPLE_ADDR;
2504bba828dSAnton Blanchard 
2513efa1cc9SIngo Molnar 	if (call_graph)
2523efa1cc9SIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
2533efa1cc9SIngo Molnar 
254cd6feeeaSIngo Molnar 	if (raw_samples) {
2556ddf259dSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_TIME;
256daac07b2SFrederic Weisbecker 		attr->sample_type	|= PERF_SAMPLE_RAW;
257cd6feeeaSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CPU;
258cd6feeeaSIngo Molnar 	}
259f413cdb8SFrederic Weisbecker 
26086470930SIngo Molnar 	attr->mmap		= track;
26186470930SIngo Molnar 	attr->comm		= track;
26286470930SIngo Molnar 	attr->inherit		= (cpu < 0) && inherit;
2634502d77cSPeter Zijlstra 	attr->disabled		= 1;
26486470930SIngo Molnar 
2653da297a6SIngo Molnar try_again:
266cdd6c482SIngo Molnar 	fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0);
26786470930SIngo Molnar 
26886470930SIngo Molnar 	if (fd[nr_cpu][counter] < 0) {
26986470930SIngo Molnar 		int err = errno;
27086470930SIngo Molnar 
27186470930SIngo Molnar 		if (err == EPERM)
2723da297a6SIngo Molnar 			die("Permission error - are you root?\n");
2730a5ac846SJens Axboe 		else if (err ==  ENODEV && profile_cpu != -1)
2740a5ac846SJens Axboe 			die("No such device - did you specify an out-of-range profile CPU?\n");
2753da297a6SIngo Molnar 
2763da297a6SIngo Molnar 		/*
2773da297a6SIngo Molnar 		 * If it's cycles then fall back to hrtimer
2783da297a6SIngo Molnar 		 * based cpu-clock-tick sw counter, which
2793da297a6SIngo Molnar 		 * is always available even if no PMU support:
2803da297a6SIngo Molnar 		 */
2813da297a6SIngo Molnar 		if (attr->type == PERF_TYPE_HARDWARE
282f4dbfa8fSPeter Zijlstra 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
2833da297a6SIngo Molnar 
2843da297a6SIngo Molnar 			if (verbose)
2853da297a6SIngo Molnar 				warning(" ... trying to fall back to cpu-clock-ticks\n");
2863da297a6SIngo Molnar 			attr->type = PERF_TYPE_SOFTWARE;
287f4dbfa8fSPeter Zijlstra 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
2883da297a6SIngo Molnar 			goto try_again;
2893da297a6SIngo Molnar 		}
29030c806a0SIngo Molnar 		printf("\n");
29130c806a0SIngo Molnar 		error("perfcounter syscall returned with %d (%s)\n",
29230c806a0SIngo Molnar 			fd[nr_cpu][counter], strerror(err));
293cdd6c482SIngo Molnar 		die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
29486470930SIngo Molnar 		exit(-1);
29586470930SIngo Molnar 	}
2963da297a6SIngo Molnar 
2977c6a1c65SPeter Zijlstra 	h_attr = get_header_attr(attr, counter);
2987c6a1c65SPeter Zijlstra 
2997c6a1c65SPeter Zijlstra 	if (!file_new) {
3007c6a1c65SPeter Zijlstra 		if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
3017c6a1c65SPeter Zijlstra 			fprintf(stderr, "incompatible append\n");
3027c6a1c65SPeter Zijlstra 			exit(-1);
3037c6a1c65SPeter Zijlstra 		}
3047c6a1c65SPeter Zijlstra 	}
3057c6a1c65SPeter Zijlstra 
3063928ddbeSFrederic Weisbecker 	if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
3073928ddbeSFrederic Weisbecker 		perror("Unable to read perf file descriptor\n");
3083928ddbeSFrederic Weisbecker 		exit(-1);
3093928ddbeSFrederic Weisbecker 	}
3107c6a1c65SPeter Zijlstra 
3117c6a1c65SPeter Zijlstra 	perf_header_attr__add_id(h_attr, read_data.id);
3127c6a1c65SPeter Zijlstra 
31386470930SIngo Molnar 	assert(fd[nr_cpu][counter] >= 0);
31486470930SIngo Molnar 	fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
31586470930SIngo Molnar 
31686470930SIngo Molnar 	/*
31786470930SIngo Molnar 	 * First counter acts as the group leader:
31886470930SIngo Molnar 	 */
31986470930SIngo Molnar 	if (group && group_fd == -1)
32086470930SIngo Molnar 		group_fd = fd[nr_cpu][counter];
321ea57c4f5SIngo Molnar 	if (multiplex && multiplex_fd == -1)
322ea57c4f5SIngo Molnar 		multiplex_fd = fd[nr_cpu][counter];
32386470930SIngo Molnar 
324ea57c4f5SIngo Molnar 	if (multiplex && fd[nr_cpu][counter] != multiplex_fd) {
325ea57c4f5SIngo Molnar 
326cdd6c482SIngo Molnar 		ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
327ea57c4f5SIngo Molnar 		assert(ret != -1);
328ea57c4f5SIngo Molnar 	} else {
32986470930SIngo Molnar 		event_array[nr_poll].fd = fd[nr_cpu][counter];
33086470930SIngo Molnar 		event_array[nr_poll].events = POLLIN;
33186470930SIngo Molnar 		nr_poll++;
33286470930SIngo Molnar 
33386470930SIngo Molnar 		mmap_array[nr_cpu][counter].counter = counter;
33486470930SIngo Molnar 		mmap_array[nr_cpu][counter].prev = 0;
33586470930SIngo Molnar 		mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
33686470930SIngo Molnar 		mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
3379d91a6f7SPeter Zijlstra 				PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0);
33886470930SIngo Molnar 		if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
33986470930SIngo Molnar 			error("failed to mmap with %d (%s)\n", errno, strerror(errno));
34086470930SIngo Molnar 			exit(-1);
34186470930SIngo Molnar 		}
342ea57c4f5SIngo Molnar 	}
343d1302522SFrederic Weisbecker 
344c171b552SLi Zefan 	if (filter != NULL) {
345c171b552SLi Zefan 		ret = ioctl(fd[nr_cpu][counter],
346c171b552SLi Zefan 			    PERF_EVENT_IOC_SET_FILTER, filter);
347c171b552SLi Zefan 		if (ret) {
348c171b552SLi Zefan 			error("failed to set filter with %d (%s)\n", errno,
349c171b552SLi Zefan 			      strerror(errno));
350c171b552SLi Zefan 			exit(-1);
351c171b552SLi Zefan 		}
352c171b552SLi Zefan 	}
353c171b552SLi Zefan 
354cdd6c482SIngo Molnar 	ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE);
35586470930SIngo Molnar }
35686470930SIngo Molnar 
35786470930SIngo Molnar static void open_counters(int cpu, pid_t pid)
35886470930SIngo Molnar {
35986470930SIngo Molnar 	int counter;
36086470930SIngo Molnar 
36186470930SIngo Molnar 	group_fd = -1;
36286470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++)
36386470930SIngo Molnar 		create_counter(counter, cpu, pid);
36486470930SIngo Molnar 
36586470930SIngo Molnar 	nr_cpu++;
36686470930SIngo Molnar }
36786470930SIngo Molnar 
368f5970550SPeter Zijlstra static void atexit_header(void)
369f5970550SPeter Zijlstra {
3707c6a1c65SPeter Zijlstra 	header->data_size += bytes_written;
371f5970550SPeter Zijlstra 
3727c6a1c65SPeter Zijlstra 	perf_header__write(header, output);
373f5970550SPeter Zijlstra }
374f5970550SPeter Zijlstra 
37586470930SIngo Molnar static int __cmd_record(int argc, const char **argv)
37686470930SIngo Molnar {
37786470930SIngo Molnar 	int i, counter;
37886470930SIngo Molnar 	struct stat st;
3797c6a1c65SPeter Zijlstra 	pid_t pid = 0;
38086470930SIngo Molnar 	int flags;
38186470930SIngo Molnar 	int ret;
3828b412664SPeter Zijlstra 	unsigned long waking = 0;
38386470930SIngo Molnar 
38486470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
38586470930SIngo Molnar 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
38686470930SIngo Molnar 	assert(nr_cpus <= MAX_NR_CPUS);
38786470930SIngo Molnar 	assert(nr_cpus >= 0);
38886470930SIngo Molnar 
389f5970550SPeter Zijlstra 	atexit(sig_atexit);
390f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
391f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
392f5970550SPeter Zijlstra 
393266e0e21SPierre Habouzit 	if (!stat(output_name, &st) && st.st_size) {
394266e0e21SPierre Habouzit 		if (!force && !append_file) {
39586470930SIngo Molnar 			fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n",
39686470930SIngo Molnar 					output_name);
39786470930SIngo Molnar 			exit(-1);
39886470930SIngo Molnar 		}
399266e0e21SPierre Habouzit 	} else {
400266e0e21SPierre Habouzit 		append_file = 0;
401266e0e21SPierre Habouzit 	}
40286470930SIngo Molnar 
40386470930SIngo Molnar 	flags = O_CREAT|O_RDWR;
40486470930SIngo Molnar 	if (append_file)
405f5970550SPeter Zijlstra 		file_new = 0;
40686470930SIngo Molnar 	else
40786470930SIngo Molnar 		flags |= O_TRUNC;
40886470930SIngo Molnar 
40986470930SIngo Molnar 	output = open(output_name, flags, S_IRUSR|S_IWUSR);
41086470930SIngo Molnar 	if (output < 0) {
41186470930SIngo Molnar 		perror("failed to create output file");
41286470930SIngo Molnar 		exit(-1);
41386470930SIngo Molnar 	}
41486470930SIngo Molnar 
4157c6a1c65SPeter Zijlstra 	if (!file_new)
4167c6a1c65SPeter Zijlstra 		header = perf_header__read(output);
4177c6a1c65SPeter Zijlstra 	else
4187c6a1c65SPeter Zijlstra 		header = perf_header__new();
419f5970550SPeter Zijlstra 
4209df37dddSFrederic Weisbecker 	if (raw_samples) {
4212ba08250SFrederic Weisbecker 		perf_header__feat_trace_info(header);
4229df37dddSFrederic Weisbecker 	} else {
4239df37dddSFrederic Weisbecker 		for (i = 0; i < nr_counters; i++) {
4249df37dddSFrederic Weisbecker 			if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
4252ba08250SFrederic Weisbecker 				perf_header__feat_trace_info(header);
4269df37dddSFrederic Weisbecker 				break;
4279df37dddSFrederic Weisbecker 			}
4289df37dddSFrederic Weisbecker 		}
4299df37dddSFrederic Weisbecker 	}
43003456a15SFrederic Weisbecker 
431f5970550SPeter Zijlstra 	atexit(atexit_header);
432f5970550SPeter Zijlstra 
43386470930SIngo Molnar 	if (!system_wide) {
4347c6a1c65SPeter Zijlstra 		pid = target_pid;
4357c6a1c65SPeter Zijlstra 		if (pid == -1)
4367c6a1c65SPeter Zijlstra 			pid = getpid();
4377c6a1c65SPeter Zijlstra 
4380a5ac846SJens Axboe 		open_counters(profile_cpu, pid);
4390a5ac846SJens Axboe 	} else {
4400a5ac846SJens Axboe 		if (profile_cpu != -1) {
4410a5ac846SJens Axboe 			open_counters(profile_cpu, target_pid);
4420a5ac846SJens Axboe 		} else {
4430a5ac846SJens Axboe 			for (i = 0; i < nr_cpus; i++)
44486470930SIngo Molnar 				open_counters(i, target_pid);
4450a5ac846SJens Axboe 		}
4460a5ac846SJens Axboe 	}
44786470930SIngo Molnar 
4487c6a1c65SPeter Zijlstra 	if (file_new)
4497c6a1c65SPeter Zijlstra 		perf_header__write(header, output);
4507c6a1c65SPeter Zijlstra 
451*234fbbf5SArnaldo Carvalho de Melo 	if (!system_wide)
452*234fbbf5SArnaldo Carvalho de Melo 		event__synthesize_thread(pid, process_synthesized_event);
453*234fbbf5SArnaldo Carvalho de Melo 	else
454*234fbbf5SArnaldo Carvalho de Melo 		event__synthesize_threads(process_synthesized_event);
4557c6a1c65SPeter Zijlstra 
45686470930SIngo Molnar 	if (target_pid == -1 && argc) {
45786470930SIngo Molnar 		pid = fork();
45886470930SIngo Molnar 		if (pid < 0)
45986470930SIngo Molnar 			perror("failed to fork");
46086470930SIngo Molnar 
46186470930SIngo Molnar 		if (!pid) {
46286470930SIngo Molnar 			if (execvp(argv[0], (char **)argv)) {
46386470930SIngo Molnar 				perror(argv[0]);
46486470930SIngo Molnar 				exit(-1);
46586470930SIngo Molnar 			}
46686470930SIngo Molnar 		}
467933da83aSChris Wilson 
468933da83aSChris Wilson 		child_pid = pid;
46986470930SIngo Molnar 	}
47086470930SIngo Molnar 
47186470930SIngo Molnar 	if (realtime_prio) {
47286470930SIngo Molnar 		struct sched_param param;
47386470930SIngo Molnar 
47486470930SIngo Molnar 		param.sched_priority = realtime_prio;
47586470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
4766beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
47786470930SIngo Molnar 			exit(-1);
47886470930SIngo Molnar 		}
47986470930SIngo Molnar 	}
48086470930SIngo Molnar 
481649c48a9SPeter Zijlstra 	for (;;) {
48286470930SIngo Molnar 		int hits = samples;
48386470930SIngo Molnar 
48486470930SIngo Molnar 		for (i = 0; i < nr_cpu; i++) {
485ea57c4f5SIngo Molnar 			for (counter = 0; counter < nr_counters; counter++) {
486ea57c4f5SIngo Molnar 				if (mmap_array[i][counter].base)
48786470930SIngo Molnar 					mmap_read(&mmap_array[i][counter]);
48886470930SIngo Molnar 			}
489ea57c4f5SIngo Molnar 		}
49086470930SIngo Molnar 
491649c48a9SPeter Zijlstra 		if (hits == samples) {
492649c48a9SPeter Zijlstra 			if (done)
493649c48a9SPeter Zijlstra 				break;
4948b412664SPeter Zijlstra 			ret = poll(event_array, nr_poll, -1);
4958b412664SPeter Zijlstra 			waking++;
4968b412664SPeter Zijlstra 		}
4978b412664SPeter Zijlstra 
4988b412664SPeter Zijlstra 		if (done) {
4998b412664SPeter Zijlstra 			for (i = 0; i < nr_cpu; i++) {
5008b412664SPeter Zijlstra 				for (counter = 0; counter < nr_counters; counter++)
501cdd6c482SIngo Molnar 					ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE);
50286470930SIngo Molnar 			}
503649c48a9SPeter Zijlstra 		}
5048b412664SPeter Zijlstra 	}
5058b412664SPeter Zijlstra 
5068b412664SPeter Zijlstra 	fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
50786470930SIngo Molnar 
50886470930SIngo Molnar 	/*
50986470930SIngo Molnar 	 * Approximate RIP event size: 24 bytes.
51086470930SIngo Molnar 	 */
51186470930SIngo Molnar 	fprintf(stderr,
51286470930SIngo Molnar 		"[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
51386470930SIngo Molnar 		(double)bytes_written / 1024.0 / 1024.0,
51486470930SIngo Molnar 		output_name,
51586470930SIngo Molnar 		bytes_written / 24);
51686470930SIngo Molnar 
51786470930SIngo Molnar 	return 0;
51886470930SIngo Molnar }
51986470930SIngo Molnar 
52086470930SIngo Molnar static const char * const record_usage[] = {
52186470930SIngo Molnar 	"perf record [<options>] [<command>]",
52286470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
52386470930SIngo Molnar 	NULL
52486470930SIngo Molnar };
52586470930SIngo Molnar 
52686470930SIngo Molnar static const struct option options[] = {
52786470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
52886470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
52986470930SIngo Molnar 		     parse_events),
530c171b552SLi Zefan 	OPT_CALLBACK(0, "filter", NULL, "filter",
531c171b552SLi Zefan 		     "event filter", parse_filter),
53286470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
53386470930SIngo Molnar 		    "record events on existing pid"),
53486470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
53586470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
536daac07b2SFrederic Weisbecker 	OPT_BOOLEAN('R', "raw-samples", &raw_samples,
537daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
53886470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
53986470930SIngo Molnar 			    "system-wide collection from all CPUs"),
54086470930SIngo Molnar 	OPT_BOOLEAN('A', "append", &append_file,
54186470930SIngo Molnar 			    "append to the output file to do incremental profiling"),
5420a5ac846SJens Axboe 	OPT_INTEGER('C', "profile_cpu", &profile_cpu,
5430a5ac846SJens Axboe 			    "CPU to profile on"),
54486470930SIngo Molnar 	OPT_BOOLEAN('f', "force", &force,
54586470930SIngo Molnar 			"overwrite existing data file"),
54686470930SIngo Molnar 	OPT_LONG('c', "count", &default_interval,
54786470930SIngo Molnar 		    "event period to sample"),
54886470930SIngo Molnar 	OPT_STRING('o', "output", &output_name, "file",
54986470930SIngo Molnar 		    "output file name"),
55086470930SIngo Molnar 	OPT_BOOLEAN('i', "inherit", &inherit,
55186470930SIngo Molnar 		    "child tasks inherit counters"),
55286470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
55386470930SIngo Molnar 		    "profile at this frequency"),
55486470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
55586470930SIngo Molnar 		    "number of mmap data pages"),
5563efa1cc9SIngo Molnar 	OPT_BOOLEAN('g', "call-graph", &call_graph,
5573efa1cc9SIngo Molnar 		    "do call-graph (stack chain/backtrace) recording"),
5583da297a6SIngo Molnar 	OPT_BOOLEAN('v', "verbose", &verbose,
5593da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
560649c48a9SPeter Zijlstra 	OPT_BOOLEAN('s', "stat", &inherit_stat,
561649c48a9SPeter Zijlstra 		    "per thread counts"),
5624bba828dSAnton Blanchard 	OPT_BOOLEAN('d', "data", &sample_address,
5634bba828dSAnton Blanchard 		    "Sample addresses"),
564649c48a9SPeter Zijlstra 	OPT_BOOLEAN('n', "no-samples", &no_samples,
565649c48a9SPeter Zijlstra 		    "don't sample"),
566d1302522SFrederic Weisbecker 	OPT_BOOLEAN('M', "multiplex", &multiplex,
567d1302522SFrederic Weisbecker 		    "multiplex counter output in a single channel"),
56886470930SIngo Molnar 	OPT_END()
56986470930SIngo Molnar };
57086470930SIngo Molnar 
571f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used)
57286470930SIngo Molnar {
57386470930SIngo Molnar 	int counter;
57486470930SIngo Molnar 
575a0541234SAnton Blanchard 	argc = parse_options(argc, argv, options, record_usage,
576a0541234SAnton Blanchard 		PARSE_OPT_STOP_AT_NON_OPTION);
57786470930SIngo Molnar 	if (!argc && target_pid == -1 && !system_wide)
57886470930SIngo Molnar 		usage_with_options(record_usage, options);
57986470930SIngo Molnar 
580bbd36e5eSPeter Zijlstra 	if (!nr_counters) {
58186470930SIngo Molnar 		nr_counters	= 1;
582bbd36e5eSPeter Zijlstra 		attrs[0].type	= PERF_TYPE_HARDWARE;
583bbd36e5eSPeter Zijlstra 		attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
584bbd36e5eSPeter Zijlstra 	}
58586470930SIngo Molnar 
5867e4ff9e3SMike Galbraith 	/*
5877e4ff9e3SMike Galbraith 	 * User specified count overrides default frequency.
5887e4ff9e3SMike Galbraith 	 */
5897e4ff9e3SMike Galbraith 	if (default_interval)
5907e4ff9e3SMike Galbraith 		freq = 0;
5917e4ff9e3SMike Galbraith 	else if (freq) {
5927e4ff9e3SMike Galbraith 		default_interval = freq;
5937e4ff9e3SMike Galbraith 	} else {
5947e4ff9e3SMike Galbraith 		fprintf(stderr, "frequency and count are zero, aborting\n");
5957e4ff9e3SMike Galbraith 		exit(EXIT_FAILURE);
5967e4ff9e3SMike Galbraith 	}
5977e4ff9e3SMike Galbraith 
59886470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
59986470930SIngo Molnar 		if (attrs[counter].sample_period)
60086470930SIngo Molnar 			continue;
60186470930SIngo Molnar 
60286470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
60386470930SIngo Molnar 	}
60486470930SIngo Molnar 
60586470930SIngo Molnar 	return __cmd_record(argc, argv);
60686470930SIngo Molnar }
607