xref: /openbmc/linux/tools/perf/builtin-record.c (revision 2c46dbb517a10b18d459e6ceffefde5bfb290cf6)
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  */
8b8f46c5aSXiao Guangrong #define _FILE_OFFSET_BITS 64
9b8f46c5aSXiao Guangrong 
1086470930SIngo Molnar #include "builtin.h"
1186470930SIngo Molnar 
1286470930SIngo Molnar #include "perf.h"
1386470930SIngo Molnar 
146122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h"
1586470930SIngo Molnar #include "util/util.h"
1686470930SIngo Molnar #include "util/parse-options.h"
1786470930SIngo Molnar #include "util/parse-events.h"
1886470930SIngo Molnar 
197c6a1c65SPeter Zijlstra #include "util/header.h"
2066e274f3SFrederic Weisbecker #include "util/event.h"
218f28827aSFrederic Weisbecker #include "util/debug.h"
2294c744b6SArnaldo Carvalho de Melo #include "util/session.h"
238d06367fSArnaldo Carvalho de Melo #include "util/symbol.h"
24a12b51c4SPaul Mackerras #include "util/cpumap.h"
257c6a1c65SPeter Zijlstra 
2686470930SIngo Molnar #include <unistd.h>
2786470930SIngo Molnar #include <sched.h>
2886470930SIngo Molnar 
29d6d901c2SZhang, Yanmin static int			*fd[MAX_NR_CPUS][MAX_COUNTERS];
3086470930SIngo Molnar 
317e4ff9e3SMike Galbraith static long			default_interval		=      0;
3286470930SIngo Molnar 
3386470930SIngo Molnar static int			nr_cpus				=      0;
3486470930SIngo Molnar static unsigned int		page_size;
3586470930SIngo Molnar static unsigned int		mmap_pages			=    128;
3642e59d7dSIngo Molnar static int			freq				=   1000;
3786470930SIngo Molnar static int			output;
38529870e3STom Zanussi static int			pipe_output			=      0;
3986470930SIngo Molnar static const char		*output_name			= "perf.data";
4086470930SIngo Molnar static int			group				=      0;
4186470930SIngo Molnar static unsigned int		realtime_prio			=      0;
42c0555642SIan Munsie static bool			raw_samples			=  false;
43c0555642SIan Munsie static bool			system_wide			=  false;
440a5ac846SJens Axboe static int			profile_cpu			=     -1;
4586470930SIngo Molnar static pid_t			target_pid			=     -1;
46d6d901c2SZhang, Yanmin static pid_t			target_tid			=     -1;
47d6d901c2SZhang, Yanmin static pid_t			*all_tids			=      NULL;
48d6d901c2SZhang, Yanmin static int			thread_num			=      0;
49933da83aSChris Wilson static pid_t			child_pid			=     -1;
50c0555642SIan Munsie static bool			inherit				=   true;
51c0555642SIan Munsie static bool			force				=  false;
52c0555642SIan Munsie static bool			append_file			=  false;
53c0555642SIan Munsie static bool			call_graph			=  false;
54c0555642SIan Munsie static bool			inherit_stat			=  false;
55c0555642SIan Munsie static bool			no_samples			=  false;
56c0555642SIan Munsie static bool			sample_address			=  false;
57c0555642SIan Munsie static bool			multiplex			=  false;
58ea57c4f5SIngo Molnar static int			multiplex_fd			=     -1;
5986470930SIngo Molnar 
6042e59d7dSIngo Molnar static long			samples				=      0;
6186470930SIngo Molnar static struct timeval		last_read;
6286470930SIngo Molnar static struct timeval		this_read;
6386470930SIngo Molnar 
6442e59d7dSIngo Molnar static u64			bytes_written			=      0;
6586470930SIngo Molnar 
66d6d901c2SZhang, Yanmin static struct pollfd		*event_array;
6786470930SIngo Molnar 
6842e59d7dSIngo Molnar static int			nr_poll				=      0;
6942e59d7dSIngo Molnar static int			nr_cpu				=      0;
7086470930SIngo Molnar 
71f5970550SPeter Zijlstra static int			file_new			=      1;
726122e4e4SArnaldo Carvalho de Melo static off_t			post_processing_offset;
737c6a1c65SPeter Zijlstra 
7494c744b6SArnaldo Carvalho de Melo static struct perf_session	*session;
75f5970550SPeter Zijlstra 
7686470930SIngo Molnar struct mmap_data {
7786470930SIngo Molnar 	int			counter;
7886470930SIngo Molnar 	void			*base;
7986470930SIngo Molnar 	unsigned int		mask;
8086470930SIngo Molnar 	unsigned int		prev;
8186470930SIngo Molnar };
8286470930SIngo Molnar 
83d6d901c2SZhang, Yanmin static struct mmap_data		*mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
8486470930SIngo Molnar 
859d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md)
8686470930SIngo Molnar {
87cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
889d91a6f7SPeter Zijlstra 	long head;
8986470930SIngo Molnar 
9086470930SIngo Molnar 	head = pc->data_head;
9186470930SIngo Molnar 	rmb();
9286470930SIngo Molnar 
9386470930SIngo Molnar 	return head;
9486470930SIngo Molnar }
9586470930SIngo Molnar 
969d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
979d91a6f7SPeter Zijlstra {
98cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
999d91a6f7SPeter Zijlstra 
1009d91a6f7SPeter Zijlstra 	/*
1019d91a6f7SPeter Zijlstra 	 * ensure all reads are done before we write the tail out.
1029d91a6f7SPeter Zijlstra 	 */
1039d91a6f7SPeter Zijlstra 	/* mb(); */
1049d91a6f7SPeter Zijlstra 	pc->data_tail = tail;
1059d91a6f7SPeter Zijlstra }
1069d91a6f7SPeter Zijlstra 
107f5970550SPeter Zijlstra static void write_output(void *buf, size_t size)
108f5970550SPeter Zijlstra {
109f5970550SPeter Zijlstra 	while (size) {
110f5970550SPeter Zijlstra 		int ret = write(output, buf, size);
111f5970550SPeter Zijlstra 
112f5970550SPeter Zijlstra 		if (ret < 0)
113f5970550SPeter Zijlstra 			die("failed to write");
114f5970550SPeter Zijlstra 
115f5970550SPeter Zijlstra 		size -= ret;
116f5970550SPeter Zijlstra 		buf += ret;
117f5970550SPeter Zijlstra 
118f5970550SPeter Zijlstra 		bytes_written += ret;
119f5970550SPeter Zijlstra 	}
120f5970550SPeter Zijlstra }
121f5970550SPeter Zijlstra 
122d8f66248SArnaldo Carvalho de Melo static int process_synthesized_event(event_t *event,
123d8f66248SArnaldo Carvalho de Melo 				     struct perf_session *self __used)
124234fbbf5SArnaldo Carvalho de Melo {
1256122e4e4SArnaldo Carvalho de Melo 	write_output(event, event->header.size);
126234fbbf5SArnaldo Carvalho de Melo 	return 0;
127234fbbf5SArnaldo Carvalho de Melo }
128234fbbf5SArnaldo Carvalho de Melo 
12986470930SIngo Molnar static void mmap_read(struct mmap_data *md)
13086470930SIngo Molnar {
13186470930SIngo Molnar 	unsigned int head = mmap_read_head(md);
13286470930SIngo Molnar 	unsigned int old = md->prev;
13386470930SIngo Molnar 	unsigned char *data = md->base + page_size;
13486470930SIngo Molnar 	unsigned long size;
13586470930SIngo Molnar 	void *buf;
13686470930SIngo Molnar 	int diff;
13786470930SIngo Molnar 
13886470930SIngo Molnar 	gettimeofday(&this_read, NULL);
13986470930SIngo Molnar 
14086470930SIngo Molnar 	/*
14186470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
14286470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
14386470930SIngo Molnar 	 *
14486470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
14586470930SIngo Molnar 	 *
14686470930SIngo Molnar 	 * In either case, truncate and restart at head.
14786470930SIngo Molnar 	 */
14886470930SIngo Molnar 	diff = head - old;
1499d91a6f7SPeter Zijlstra 	if (diff < 0) {
15086470930SIngo Molnar 		struct timeval iv;
15186470930SIngo Molnar 		unsigned long msecs;
15286470930SIngo Molnar 
15386470930SIngo Molnar 		timersub(&this_read, &last_read, &iv);
15486470930SIngo Molnar 		msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
15586470930SIngo Molnar 
15686470930SIngo Molnar 		fprintf(stderr, "WARNING: failed to keep up with mmap data."
15786470930SIngo Molnar 				"  Last read %lu msecs ago.\n", msecs);
15886470930SIngo Molnar 
15986470930SIngo Molnar 		/*
16086470930SIngo Molnar 		 * head points to a known good entry, start there.
16186470930SIngo Molnar 		 */
16286470930SIngo Molnar 		old = head;
16386470930SIngo Molnar 	}
16486470930SIngo Molnar 
16586470930SIngo Molnar 	last_read = this_read;
16686470930SIngo Molnar 
16786470930SIngo Molnar 	if (old != head)
16886470930SIngo Molnar 		samples++;
16986470930SIngo Molnar 
17086470930SIngo Molnar 	size = head - old;
17186470930SIngo Molnar 
17286470930SIngo Molnar 	if ((old & md->mask) + size != (head & md->mask)) {
17386470930SIngo Molnar 		buf = &data[old & md->mask];
17486470930SIngo Molnar 		size = md->mask + 1 - (old & md->mask);
17586470930SIngo Molnar 		old += size;
17686470930SIngo Molnar 
1776122e4e4SArnaldo Carvalho de Melo 		write_output(buf, size);
17886470930SIngo Molnar 	}
17986470930SIngo Molnar 
18086470930SIngo Molnar 	buf = &data[old & md->mask];
18186470930SIngo Molnar 	size = head - old;
18286470930SIngo Molnar 	old += size;
18386470930SIngo Molnar 
1846122e4e4SArnaldo Carvalho de Melo 	write_output(buf, size);
18586470930SIngo Molnar 
18686470930SIngo Molnar 	md->prev = old;
1879d91a6f7SPeter Zijlstra 	mmap_write_tail(md, old);
18886470930SIngo Molnar }
18986470930SIngo Molnar 
19086470930SIngo Molnar static volatile int done = 0;
191f7b7c26eSPeter Zijlstra static volatile int signr = -1;
19286470930SIngo Molnar 
19386470930SIngo Molnar static void sig_handler(int sig)
19486470930SIngo Molnar {
19586470930SIngo Molnar 	done = 1;
196f7b7c26eSPeter Zijlstra 	signr = sig;
197f7b7c26eSPeter Zijlstra }
198f7b7c26eSPeter Zijlstra 
199f7b7c26eSPeter Zijlstra static void sig_atexit(void)
200f7b7c26eSPeter Zijlstra {
201933da83aSChris Wilson 	if (child_pid != -1)
202933da83aSChris Wilson 		kill(child_pid, SIGTERM);
203933da83aSChris Wilson 
204f7b7c26eSPeter Zijlstra 	if (signr == -1)
205f7b7c26eSPeter Zijlstra 		return;
206f7b7c26eSPeter Zijlstra 
207f7b7c26eSPeter Zijlstra 	signal(signr, SIG_DFL);
208f7b7c26eSPeter Zijlstra 	kill(getpid(), signr);
20986470930SIngo Molnar }
21086470930SIngo Molnar 
21186470930SIngo Molnar static int group_fd;
21286470930SIngo Molnar 
213cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
2147c6a1c65SPeter Zijlstra {
2157c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
2167c6a1c65SPeter Zijlstra 
21794c744b6SArnaldo Carvalho de Melo 	if (nr < session->header.attrs) {
21894c744b6SArnaldo Carvalho de Melo 		h_attr = session->header.attr[nr];
2197c6a1c65SPeter Zijlstra 	} else {
2207c6a1c65SPeter Zijlstra 		h_attr = perf_header_attr__new(a);
221dc79c0fcSArnaldo Carvalho de Melo 		if (h_attr != NULL)
22294c744b6SArnaldo Carvalho de Melo 			if (perf_header__add_attr(&session->header, h_attr) < 0) {
22311deb1f9SArnaldo Carvalho de Melo 				perf_header_attr__delete(h_attr);
22411deb1f9SArnaldo Carvalho de Melo 				h_attr = NULL;
22511deb1f9SArnaldo Carvalho de Melo 			}
2267c6a1c65SPeter Zijlstra 	}
2277c6a1c65SPeter Zijlstra 
2287c6a1c65SPeter Zijlstra 	return h_attr;
2297c6a1c65SPeter Zijlstra }
2307c6a1c65SPeter Zijlstra 
231d6d901c2SZhang, Yanmin static void create_counter(int counter, int cpu)
23286470930SIngo Molnar {
233c171b552SLi Zefan 	char *filter = filters[counter];
234cdd6c482SIngo Molnar 	struct perf_event_attr *attr = attrs + counter;
2357c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
2367c6a1c65SPeter Zijlstra 	int track = !counter; /* only the first counter needs these */
237d6d901c2SZhang, Yanmin 	int thread_index;
238c171b552SLi Zefan 	int ret;
2397c6a1c65SPeter Zijlstra 	struct {
2407c6a1c65SPeter Zijlstra 		u64 count;
2417c6a1c65SPeter Zijlstra 		u64 time_enabled;
2427c6a1c65SPeter Zijlstra 		u64 time_running;
2437c6a1c65SPeter Zijlstra 		u64 id;
2447c6a1c65SPeter Zijlstra 	} read_data;
2457c6a1c65SPeter Zijlstra 
2467c6a1c65SPeter Zijlstra 	attr->read_format	= PERF_FORMAT_TOTAL_TIME_ENABLED |
2477c6a1c65SPeter Zijlstra 				  PERF_FORMAT_TOTAL_TIME_RUNNING |
2487c6a1c65SPeter Zijlstra 				  PERF_FORMAT_ID;
24986470930SIngo Molnar 
2503a9f131fSFrederic Weisbecker 	attr->sample_type	|= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
2513efa1cc9SIngo Molnar 
2528907fd60SEric B Munson 	if (nr_counters > 1)
2538907fd60SEric B Munson 		attr->sample_type |= PERF_SAMPLE_ID;
2548907fd60SEric B Munson 
25586470930SIngo Molnar 	if (freq) {
256ea1900e5SPeter Zijlstra 		attr->sample_type	|= PERF_SAMPLE_PERIOD;
25786470930SIngo Molnar 		attr->freq		= 1;
25886470930SIngo Molnar 		attr->sample_freq	= freq;
25986470930SIngo Molnar 	}
2603efa1cc9SIngo Molnar 
261649c48a9SPeter Zijlstra 	if (no_samples)
262649c48a9SPeter Zijlstra 		attr->sample_freq = 0;
263649c48a9SPeter Zijlstra 
264649c48a9SPeter Zijlstra 	if (inherit_stat)
265649c48a9SPeter Zijlstra 		attr->inherit_stat = 1;
266649c48a9SPeter Zijlstra 
2674bba828dSAnton Blanchard 	if (sample_address)
2684bba828dSAnton Blanchard 		attr->sample_type	|= PERF_SAMPLE_ADDR;
2694bba828dSAnton Blanchard 
2703efa1cc9SIngo Molnar 	if (call_graph)
2713efa1cc9SIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
2723efa1cc9SIngo Molnar 
273cd6feeeaSIngo Molnar 	if (raw_samples) {
2746ddf259dSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_TIME;
275daac07b2SFrederic Weisbecker 		attr->sample_type	|= PERF_SAMPLE_RAW;
276cd6feeeaSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CPU;
277cd6feeeaSIngo Molnar 	}
278f413cdb8SFrederic Weisbecker 
27986470930SIngo Molnar 	attr->mmap		= track;
28086470930SIngo Molnar 	attr->comm		= track;
28160ab2716SPeter Zijlstra 	attr->inherit		= inherit;
28246be604bSZhang, Yanmin 	if (target_pid == -1 && !system_wide) {
2834502d77cSPeter Zijlstra 		attr->disabled = 1;
284bedbfdeaSEric B Munson 		attr->enable_on_exec = 1;
28546be604bSZhang, Yanmin 	}
286bedbfdeaSEric B Munson 
287d6d901c2SZhang, Yanmin 	for (thread_index = 0; thread_index < thread_num; thread_index++) {
2883da297a6SIngo Molnar try_again:
289d6d901c2SZhang, Yanmin 		fd[nr_cpu][counter][thread_index] = sys_perf_event_open(attr,
290d6d901c2SZhang, Yanmin 				all_tids[thread_index], cpu, group_fd, 0);
29186470930SIngo Molnar 
292d6d901c2SZhang, Yanmin 		if (fd[nr_cpu][counter][thread_index] < 0) {
29386470930SIngo Molnar 			int err = errno;
29486470930SIngo Molnar 
295c10edee2SPekka Enberg 			if (err == EPERM || err == EACCES)
2966230f2c7SArnaldo Carvalho de Melo 				die("Permission error - are you root?\n"
297d6d901c2SZhang, Yanmin 					"\t Consider tweaking"
298d6d901c2SZhang, Yanmin 					" /proc/sys/kernel/perf_event_paranoid.\n");
299d6d901c2SZhang, Yanmin 			else if (err ==  ENODEV && profile_cpu != -1) {
300d6d901c2SZhang, Yanmin 				die("No such device - did you specify"
301d6d901c2SZhang, Yanmin 					" an out-of-range profile CPU?\n");
302d6d901c2SZhang, Yanmin 			}
3033da297a6SIngo Molnar 
3043da297a6SIngo Molnar 			/*
3053da297a6SIngo Molnar 			 * If it's cycles then fall back to hrtimer
3063da297a6SIngo Molnar 			 * based cpu-clock-tick sw counter, which
3073da297a6SIngo Molnar 			 * is always available even if no PMU support:
3083da297a6SIngo Molnar 			 */
3093da297a6SIngo Molnar 			if (attr->type == PERF_TYPE_HARDWARE
310f4dbfa8fSPeter Zijlstra 					&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
3113da297a6SIngo Molnar 
3123da297a6SIngo Molnar 				if (verbose)
3133da297a6SIngo Molnar 					warning(" ... trying to fall back to cpu-clock-ticks\n");
3143da297a6SIngo Molnar 				attr->type = PERF_TYPE_SOFTWARE;
315f4dbfa8fSPeter Zijlstra 				attr->config = PERF_COUNT_SW_CPU_CLOCK;
3163da297a6SIngo Molnar 				goto try_again;
3173da297a6SIngo Molnar 			}
31830c806a0SIngo Molnar 			printf("\n");
31930c806a0SIngo Molnar 			error("perfcounter syscall returned with %d (%s)\n",
320d6d901c2SZhang, Yanmin 					fd[nr_cpu][counter][thread_index], strerror(err));
321bfd45118SSimon Kaempflein 
322bfd45118SSimon Kaempflein #if defined(__i386__) || defined(__x86_64__)
323bfd45118SSimon Kaempflein 			if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
324d6d901c2SZhang, Yanmin 				die("No hardware sampling interrupt available."
325d6d901c2SZhang, Yanmin 				    " No APIC? If so then you can boot the kernel"
326d6d901c2SZhang, Yanmin 				    " with the \"lapic\" boot parameter to"
327d6d901c2SZhang, Yanmin 				    " force-enable it.\n");
328bfd45118SSimon Kaempflein #endif
329bfd45118SSimon Kaempflein 
330cdd6c482SIngo Molnar 			die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
33186470930SIngo Molnar 			exit(-1);
33286470930SIngo Molnar 		}
3333da297a6SIngo Molnar 
3347c6a1c65SPeter Zijlstra 		h_attr = get_header_attr(attr, counter);
335dc79c0fcSArnaldo Carvalho de Melo 		if (h_attr == NULL)
336dc79c0fcSArnaldo Carvalho de Melo 			die("nomem\n");
3377c6a1c65SPeter Zijlstra 
3387c6a1c65SPeter Zijlstra 		if (!file_new) {
3397c6a1c65SPeter Zijlstra 			if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
3407c6a1c65SPeter Zijlstra 				fprintf(stderr, "incompatible append\n");
3417c6a1c65SPeter Zijlstra 				exit(-1);
3427c6a1c65SPeter Zijlstra 			}
3437c6a1c65SPeter Zijlstra 		}
3447c6a1c65SPeter Zijlstra 
345d6d901c2SZhang, Yanmin 		if (read(fd[nr_cpu][counter][thread_index], &read_data, sizeof(read_data)) == -1) {
3463928ddbeSFrederic Weisbecker 			perror("Unable to read perf file descriptor\n");
3473928ddbeSFrederic Weisbecker 			exit(-1);
3483928ddbeSFrederic Weisbecker 		}
3497c6a1c65SPeter Zijlstra 
35058754121SArnaldo Carvalho de Melo 		if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
35158754121SArnaldo Carvalho de Melo 			pr_warning("Not enough memory to add id\n");
35258754121SArnaldo Carvalho de Melo 			exit(-1);
35358754121SArnaldo Carvalho de Melo 		}
3547c6a1c65SPeter Zijlstra 
355d6d901c2SZhang, Yanmin 		assert(fd[nr_cpu][counter][thread_index] >= 0);
356d6d901c2SZhang, Yanmin 		fcntl(fd[nr_cpu][counter][thread_index], F_SETFL, O_NONBLOCK);
35786470930SIngo Molnar 
35886470930SIngo Molnar 		/*
35986470930SIngo Molnar 		 * First counter acts as the group leader:
36086470930SIngo Molnar 		 */
36186470930SIngo Molnar 		if (group && group_fd == -1)
362d6d901c2SZhang, Yanmin 			group_fd = fd[nr_cpu][counter][thread_index];
363ea57c4f5SIngo Molnar 		if (multiplex && multiplex_fd == -1)
364d6d901c2SZhang, Yanmin 			multiplex_fd = fd[nr_cpu][counter][thread_index];
36586470930SIngo Molnar 
366d6d901c2SZhang, Yanmin 		if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) {
367ea57c4f5SIngo Molnar 
368d6d901c2SZhang, Yanmin 			ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
369ea57c4f5SIngo Molnar 			assert(ret != -1);
370ea57c4f5SIngo Molnar 		} else {
371d6d901c2SZhang, Yanmin 			event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index];
37286470930SIngo Molnar 			event_array[nr_poll].events = POLLIN;
37386470930SIngo Molnar 			nr_poll++;
37486470930SIngo Molnar 
375d6d901c2SZhang, Yanmin 			mmap_array[nr_cpu][counter][thread_index].counter = counter;
376d6d901c2SZhang, Yanmin 			mmap_array[nr_cpu][counter][thread_index].prev = 0;
377d6d901c2SZhang, Yanmin 			mmap_array[nr_cpu][counter][thread_index].mask = mmap_pages*page_size - 1;
378d6d901c2SZhang, Yanmin 			mmap_array[nr_cpu][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
379d6d901c2SZhang, Yanmin 				PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0);
380d6d901c2SZhang, Yanmin 			if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) {
38186470930SIngo Molnar 				error("failed to mmap with %d (%s)\n", errno, strerror(errno));
38286470930SIngo Molnar 				exit(-1);
38386470930SIngo Molnar 			}
384ea57c4f5SIngo Molnar 		}
385d1302522SFrederic Weisbecker 
386c171b552SLi Zefan 		if (filter != NULL) {
387d6d901c2SZhang, Yanmin 			ret = ioctl(fd[nr_cpu][counter][thread_index],
388c171b552SLi Zefan 					PERF_EVENT_IOC_SET_FILTER, filter);
389c171b552SLi Zefan 			if (ret) {
390c171b552SLi Zefan 				error("failed to set filter with %d (%s)\n", errno,
391c171b552SLi Zefan 						strerror(errno));
392c171b552SLi Zefan 				exit(-1);
393c171b552SLi Zefan 			}
394c171b552SLi Zefan 		}
39586470930SIngo Molnar 	}
396d6d901c2SZhang, Yanmin }
39786470930SIngo Molnar 
398d6d901c2SZhang, Yanmin static void open_counters(int cpu)
39986470930SIngo Molnar {
40086470930SIngo Molnar 	int counter;
40186470930SIngo Molnar 
40286470930SIngo Molnar 	group_fd = -1;
40386470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++)
404d6d901c2SZhang, Yanmin 		create_counter(counter, cpu);
40586470930SIngo Molnar 
40686470930SIngo Molnar 	nr_cpu++;
40786470930SIngo Molnar }
40886470930SIngo Molnar 
4096122e4e4SArnaldo Carvalho de Melo static int process_buildids(void)
4106122e4e4SArnaldo Carvalho de Melo {
4116122e4e4SArnaldo Carvalho de Melo 	u64 size = lseek(output, 0, SEEK_CUR);
4126122e4e4SArnaldo Carvalho de Melo 
4139f591fd7SArnaldo Carvalho de Melo 	if (size == 0)
4149f591fd7SArnaldo Carvalho de Melo 		return 0;
4159f591fd7SArnaldo Carvalho de Melo 
4166122e4e4SArnaldo Carvalho de Melo 	session->fd = output;
4176122e4e4SArnaldo Carvalho de Melo 	return __perf_session__process_events(session, post_processing_offset,
4186122e4e4SArnaldo Carvalho de Melo 					      size - post_processing_offset,
4196122e4e4SArnaldo Carvalho de Melo 					      size, &build_id__mark_dso_hit_ops);
4206122e4e4SArnaldo Carvalho de Melo }
4216122e4e4SArnaldo Carvalho de Melo 
422f5970550SPeter Zijlstra static void atexit_header(void)
423f5970550SPeter Zijlstra {
42494c744b6SArnaldo Carvalho de Melo 	session->header.data_size += bytes_written;
425f5970550SPeter Zijlstra 
4266122e4e4SArnaldo Carvalho de Melo 	process_buildids();
42794c744b6SArnaldo Carvalho de Melo 	perf_header__write(&session->header, output, true);
428f5970550SPeter Zijlstra }
429f5970550SPeter Zijlstra 
430d4db3f16SArnaldo Carvalho de Melo static int __cmd_record(int argc, const char **argv)
43186470930SIngo Molnar {
43286470930SIngo Molnar 	int i, counter;
43386470930SIngo Molnar 	struct stat st;
4347c6a1c65SPeter Zijlstra 	pid_t pid = 0;
43586470930SIngo Molnar 	int flags;
4364dc0a04bSArnaldo Carvalho de Melo 	int err;
4378b412664SPeter Zijlstra 	unsigned long waking = 0;
438856e9660SPeter Zijlstra 	int child_ready_pipe[2], go_pipe[2];
43946be604bSZhang, Yanmin 	const bool forks = argc > 0;
440856e9660SPeter Zijlstra 	char buf;
44186470930SIngo Molnar 
44286470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
44386470930SIngo Molnar 
444f5970550SPeter Zijlstra 	atexit(sig_atexit);
445f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
446f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
447f5970550SPeter Zijlstra 
448d4db3f16SArnaldo Carvalho de Melo 	if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
449856e9660SPeter Zijlstra 		perror("failed to create pipes");
450856e9660SPeter Zijlstra 		exit(-1);
451856e9660SPeter Zijlstra 	}
452856e9660SPeter Zijlstra 
453529870e3STom Zanussi 	if (!strcmp(output_name, "-"))
454529870e3STom Zanussi 		pipe_output = 1;
455529870e3STom Zanussi 	else if (!stat(output_name, &st) && st.st_size) {
456b38d3464SArnaldo Carvalho de Melo 		if (!force) {
457b38d3464SArnaldo Carvalho de Melo 			if (!append_file) {
458b38d3464SArnaldo Carvalho de Melo 				pr_err("Error, output file %s exists, use -A "
459b38d3464SArnaldo Carvalho de Melo 				       "to append or -f to overwrite.\n",
46086470930SIngo Molnar 				       output_name);
46186470930SIngo Molnar 				exit(-1);
46286470930SIngo Molnar 			}
463266e0e21SPierre Habouzit 		} else {
464b38d3464SArnaldo Carvalho de Melo 			char oldname[PATH_MAX];
465b38d3464SArnaldo Carvalho de Melo 			snprintf(oldname, sizeof(oldname), "%s.old",
466b38d3464SArnaldo Carvalho de Melo 				 output_name);
467b38d3464SArnaldo Carvalho de Melo 			unlink(oldname);
468b38d3464SArnaldo Carvalho de Melo 			rename(output_name, oldname);
469b38d3464SArnaldo Carvalho de Melo 		}
470b38d3464SArnaldo Carvalho de Melo 	} else {
471c0555642SIan Munsie 		append_file = false;
472266e0e21SPierre Habouzit 	}
47386470930SIngo Molnar 
474f887f301SXiao Guangrong 	flags = O_CREAT|O_RDWR;
47586470930SIngo Molnar 	if (append_file)
476f5970550SPeter Zijlstra 		file_new = 0;
47786470930SIngo Molnar 	else
47886470930SIngo Molnar 		flags |= O_TRUNC;
47986470930SIngo Molnar 
480529870e3STom Zanussi 	if (pipe_output)
481529870e3STom Zanussi 		output = STDOUT_FILENO;
482529870e3STom Zanussi 	else
48386470930SIngo Molnar 		output = open(output_name, flags, S_IRUSR | S_IWUSR);
48486470930SIngo Molnar 	if (output < 0) {
48586470930SIngo Molnar 		perror("failed to create output file");
48686470930SIngo Molnar 		exit(-1);
48786470930SIngo Molnar 	}
48886470930SIngo Molnar 
48975be6cf4SArnaldo Carvalho de Melo 	session = perf_session__new(output_name, O_WRONLY, force);
49094c744b6SArnaldo Carvalho de Melo 	if (session == NULL) {
491a9a70bbcSArnaldo Carvalho de Melo 		pr_err("Not enough memory for reading perf file header\n");
492a9a70bbcSArnaldo Carvalho de Melo 		return -1;
493a9a70bbcSArnaldo Carvalho de Melo 	}
494a9a70bbcSArnaldo Carvalho de Melo 
4954dc0a04bSArnaldo Carvalho de Melo 	if (!file_new) {
4968dc58101STom Zanussi 		err = perf_header__read(session, output);
4974dc0a04bSArnaldo Carvalho de Melo 		if (err < 0)
4984dc0a04bSArnaldo Carvalho de Melo 			return err;
4994dc0a04bSArnaldo Carvalho de Melo 	}
5004dc0a04bSArnaldo Carvalho de Melo 
5019df37dddSFrederic Weisbecker 	if (raw_samples) {
50294c744b6SArnaldo Carvalho de Melo 		perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
5039df37dddSFrederic Weisbecker 	} else {
5049df37dddSFrederic Weisbecker 		for (i = 0; i < nr_counters; i++) {
5059df37dddSFrederic Weisbecker 			if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
50694c744b6SArnaldo Carvalho de Melo 				perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
5079df37dddSFrederic Weisbecker 				break;
5089df37dddSFrederic Weisbecker 			}
5099df37dddSFrederic Weisbecker 		}
5109df37dddSFrederic Weisbecker 	}
51103456a15SFrederic Weisbecker 
512f5970550SPeter Zijlstra 	atexit(atexit_header);
513f5970550SPeter Zijlstra 
514d4db3f16SArnaldo Carvalho de Melo 	if (forks) {
51546be604bSZhang, Yanmin 		child_pid = fork();
516856e9660SPeter Zijlstra 		if (pid < 0) {
517856e9660SPeter Zijlstra 			perror("failed to fork");
518856e9660SPeter Zijlstra 			exit(-1);
519856e9660SPeter Zijlstra 		}
5207c6a1c65SPeter Zijlstra 
52146be604bSZhang, Yanmin 		if (!child_pid) {
522529870e3STom Zanussi 			if (pipe_output)
523529870e3STom Zanussi 				dup2(2, 1);
524856e9660SPeter Zijlstra 			close(child_ready_pipe[0]);
525856e9660SPeter Zijlstra 			close(go_pipe[1]);
526856e9660SPeter Zijlstra 			fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
527856e9660SPeter Zijlstra 
528856e9660SPeter Zijlstra 			/*
529856e9660SPeter Zijlstra 			 * Do a dummy execvp to get the PLT entry resolved,
530856e9660SPeter Zijlstra 			 * so we avoid the resolver overhead on the real
531856e9660SPeter Zijlstra 			 * execvp call.
532856e9660SPeter Zijlstra 			 */
533856e9660SPeter Zijlstra 			execvp("", (char **)argv);
534856e9660SPeter Zijlstra 
535856e9660SPeter Zijlstra 			/*
536856e9660SPeter Zijlstra 			 * Tell the parent we're ready to go
537856e9660SPeter Zijlstra 			 */
538856e9660SPeter Zijlstra 			close(child_ready_pipe[1]);
539856e9660SPeter Zijlstra 
540856e9660SPeter Zijlstra 			/*
541856e9660SPeter Zijlstra 			 * Wait until the parent tells us to go.
542856e9660SPeter Zijlstra 			 */
543856e9660SPeter Zijlstra 			if (read(go_pipe[0], &buf, 1) == -1)
544856e9660SPeter Zijlstra 				perror("unable to read pipe");
545856e9660SPeter Zijlstra 
546856e9660SPeter Zijlstra 			execvp(argv[0], (char **)argv);
547856e9660SPeter Zijlstra 
548856e9660SPeter Zijlstra 			perror(argv[0]);
549856e9660SPeter Zijlstra 			exit(-1);
550856e9660SPeter Zijlstra 		}
551856e9660SPeter Zijlstra 
552d6d901c2SZhang, Yanmin 		if (!system_wide && target_tid == -1 && target_pid == -1)
553d6d901c2SZhang, Yanmin 			all_tids[0] = child_pid;
554d6d901c2SZhang, Yanmin 
555856e9660SPeter Zijlstra 		close(child_ready_pipe[1]);
556856e9660SPeter Zijlstra 		close(go_pipe[0]);
557856e9660SPeter Zijlstra 		/*
558856e9660SPeter Zijlstra 		 * wait for child to settle
559856e9660SPeter Zijlstra 		 */
560856e9660SPeter Zijlstra 		if (read(child_ready_pipe[0], &buf, 1) == -1) {
561856e9660SPeter Zijlstra 			perror("unable to read pipe");
562856e9660SPeter Zijlstra 			exit(-1);
563856e9660SPeter Zijlstra 		}
564856e9660SPeter Zijlstra 		close(child_ready_pipe[0]);
565856e9660SPeter Zijlstra 	}
566856e9660SPeter Zijlstra 
56760ab2716SPeter Zijlstra 	if ((!system_wide && !inherit) || profile_cpu != -1) {
568d6d901c2SZhang, Yanmin 		open_counters(profile_cpu);
5690a5ac846SJens Axboe 	} else {
570a12b51c4SPaul Mackerras 		nr_cpus = read_cpu_map();
5710a5ac846SJens Axboe 		for (i = 0; i < nr_cpus; i++)
572d6d901c2SZhang, Yanmin 			open_counters(cpumap[i]);
5730a5ac846SJens Axboe 	}
57486470930SIngo Molnar 
575529870e3STom Zanussi 	if (pipe_output) {
576529870e3STom Zanussi 		err = perf_header__write_pipe(output);
577529870e3STom Zanussi 		if (err < 0)
578529870e3STom Zanussi 			return err;
579529870e3STom Zanussi 	} else if (file_new) {
58094c744b6SArnaldo Carvalho de Melo 		err = perf_header__write(&session->header, output, false);
581d5eed904SArnaldo Carvalho de Melo 		if (err < 0)
582d5eed904SArnaldo Carvalho de Melo 			return err;
583d5eed904SArnaldo Carvalho de Melo 	}
5847c6a1c65SPeter Zijlstra 
5856122e4e4SArnaldo Carvalho de Melo 	post_processing_offset = lseek(output, 0, SEEK_CUR);
5866122e4e4SArnaldo Carvalho de Melo 
587*2c46dbb5STom Zanussi 	if (pipe_output) {
588*2c46dbb5STom Zanussi 		err = event__synthesize_attrs(&session->header,
589*2c46dbb5STom Zanussi 					      process_synthesized_event,
590*2c46dbb5STom Zanussi 					      session);
591*2c46dbb5STom Zanussi 		if (err < 0) {
592*2c46dbb5STom Zanussi 			pr_err("Couldn't synthesize attrs.\n");
593*2c46dbb5STom Zanussi 			return err;
594*2c46dbb5STom Zanussi 		}
595*2c46dbb5STom Zanussi 	}
596*2c46dbb5STom Zanussi 
59756b03f3cSArnaldo Carvalho de Melo 	err = event__synthesize_kernel_mmap(process_synthesized_event,
59856b03f3cSArnaldo Carvalho de Melo 					    session, "_text");
59970162138SArnaldo Carvalho de Melo 	if (err < 0)
60070162138SArnaldo Carvalho de Melo 		err = event__synthesize_kernel_mmap(process_synthesized_event,
60170162138SArnaldo Carvalho de Melo 						    session, "_stext");
60256b03f3cSArnaldo Carvalho de Melo 	if (err < 0) {
60356b03f3cSArnaldo Carvalho de Melo 		pr_err("Couldn't record kernel reference relocation symbol.\n");
60456b03f3cSArnaldo Carvalho de Melo 		return err;
60556b03f3cSArnaldo Carvalho de Melo 	}
60656b03f3cSArnaldo Carvalho de Melo 
607b7cece76SArnaldo Carvalho de Melo 	err = event__synthesize_modules(process_synthesized_event, session);
608b7cece76SArnaldo Carvalho de Melo 	if (err < 0) {
609b7cece76SArnaldo Carvalho de Melo 		pr_err("Couldn't record kernel reference relocation symbol.\n");
610b7cece76SArnaldo Carvalho de Melo 		return err;
611b7cece76SArnaldo Carvalho de Melo 	}
612b7cece76SArnaldo Carvalho de Melo 
613d4db3f16SArnaldo Carvalho de Melo 	if (!system_wide && profile_cpu == -1)
614d6d901c2SZhang, Yanmin 		event__synthesize_thread(target_tid, process_synthesized_event,
615d8f66248SArnaldo Carvalho de Melo 					 session);
616234fbbf5SArnaldo Carvalho de Melo 	else
617d8f66248SArnaldo Carvalho de Melo 		event__synthesize_threads(process_synthesized_event, session);
6187c6a1c65SPeter Zijlstra 
61986470930SIngo Molnar 	if (realtime_prio) {
62086470930SIngo Molnar 		struct sched_param param;
62186470930SIngo Molnar 
62286470930SIngo Molnar 		param.sched_priority = realtime_prio;
62386470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
6246beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
62586470930SIngo Molnar 			exit(-1);
62686470930SIngo Molnar 		}
62786470930SIngo Molnar 	}
62886470930SIngo Molnar 
629856e9660SPeter Zijlstra 	/*
630856e9660SPeter Zijlstra 	 * Let the child rip
631856e9660SPeter Zijlstra 	 */
632d4db3f16SArnaldo Carvalho de Melo 	if (forks)
633856e9660SPeter Zijlstra 		close(go_pipe[1]);
634856e9660SPeter Zijlstra 
635649c48a9SPeter Zijlstra 	for (;;) {
63686470930SIngo Molnar 		int hits = samples;
637d6d901c2SZhang, Yanmin 		int thread;
63886470930SIngo Molnar 
63986470930SIngo Molnar 		for (i = 0; i < nr_cpu; i++) {
640ea57c4f5SIngo Molnar 			for (counter = 0; counter < nr_counters; counter++) {
641d6d901c2SZhang, Yanmin 				for (thread = 0;
642d6d901c2SZhang, Yanmin 					thread < thread_num; thread++) {
643d6d901c2SZhang, Yanmin 					if (mmap_array[i][counter][thread].base)
644d6d901c2SZhang, Yanmin 						mmap_read(&mmap_array[i][counter][thread]);
645d6d901c2SZhang, Yanmin 				}
646d6d901c2SZhang, Yanmin 
64786470930SIngo Molnar 			}
648ea57c4f5SIngo Molnar 		}
64986470930SIngo Molnar 
650649c48a9SPeter Zijlstra 		if (hits == samples) {
651649c48a9SPeter Zijlstra 			if (done)
652649c48a9SPeter Zijlstra 				break;
6534dc0a04bSArnaldo Carvalho de Melo 			err = poll(event_array, nr_poll, -1);
6548b412664SPeter Zijlstra 			waking++;
6558b412664SPeter Zijlstra 		}
6568b412664SPeter Zijlstra 
6578b412664SPeter Zijlstra 		if (done) {
6588b412664SPeter Zijlstra 			for (i = 0; i < nr_cpu; i++) {
659d6d901c2SZhang, Yanmin 				for (counter = 0;
660d6d901c2SZhang, Yanmin 					counter < nr_counters;
661d6d901c2SZhang, Yanmin 					counter++) {
662d6d901c2SZhang, Yanmin 					for (thread = 0;
663d6d901c2SZhang, Yanmin 						thread < thread_num;
664d6d901c2SZhang, Yanmin 						thread++)
665d6d901c2SZhang, Yanmin 						ioctl(fd[i][counter][thread],
666d6d901c2SZhang, Yanmin 							PERF_EVENT_IOC_DISABLE);
667d6d901c2SZhang, Yanmin 				}
66886470930SIngo Molnar 			}
669649c48a9SPeter Zijlstra 		}
6708b412664SPeter Zijlstra 	}
6718b412664SPeter Zijlstra 
6728b412664SPeter Zijlstra 	fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
67386470930SIngo Molnar 
67486470930SIngo Molnar 	/*
67586470930SIngo Molnar 	 * Approximate RIP event size: 24 bytes.
67686470930SIngo Molnar 	 */
67786470930SIngo Molnar 	fprintf(stderr,
67886470930SIngo Molnar 		"[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
67986470930SIngo Molnar 		(double)bytes_written / 1024.0 / 1024.0,
68086470930SIngo Molnar 		output_name,
68186470930SIngo Molnar 		bytes_written / 24);
68286470930SIngo Molnar 
68386470930SIngo Molnar 	return 0;
68486470930SIngo Molnar }
68586470930SIngo Molnar 
68686470930SIngo Molnar static const char * const record_usage[] = {
68786470930SIngo Molnar 	"perf record [<options>] [<command>]",
68886470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
68986470930SIngo Molnar 	NULL
69086470930SIngo Molnar };
69186470930SIngo Molnar 
69286470930SIngo Molnar static const struct option options[] = {
69386470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
69486470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
69586470930SIngo Molnar 		     parse_events),
696c171b552SLi Zefan 	OPT_CALLBACK(0, "filter", NULL, "filter",
697c171b552SLi Zefan 		     "event filter", parse_filter),
69886470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
699d6d901c2SZhang, Yanmin 		    "record events on existing process id"),
700d6d901c2SZhang, Yanmin 	OPT_INTEGER('t', "tid", &target_tid,
701d6d901c2SZhang, Yanmin 		    "record events on existing thread id"),
70286470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
70386470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
704daac07b2SFrederic Weisbecker 	OPT_BOOLEAN('R', "raw-samples", &raw_samples,
705daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
70686470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
70786470930SIngo Molnar 			    "system-wide collection from all CPUs"),
70886470930SIngo Molnar 	OPT_BOOLEAN('A', "append", &append_file,
70986470930SIngo Molnar 			    "append to the output file to do incremental profiling"),
7100a5ac846SJens Axboe 	OPT_INTEGER('C', "profile_cpu", &profile_cpu,
7110a5ac846SJens Axboe 			    "CPU to profile on"),
71286470930SIngo Molnar 	OPT_BOOLEAN('f', "force", &force,
71386470930SIngo Molnar 			"overwrite existing data file"),
71486470930SIngo Molnar 	OPT_LONG('c', "count", &default_interval,
71586470930SIngo Molnar 		    "event period to sample"),
71686470930SIngo Molnar 	OPT_STRING('o', "output", &output_name, "file",
71786470930SIngo Molnar 		    "output file name"),
71886470930SIngo Molnar 	OPT_BOOLEAN('i', "inherit", &inherit,
71986470930SIngo Molnar 		    "child tasks inherit counters"),
72086470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
72186470930SIngo Molnar 		    "profile at this frequency"),
72286470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
72386470930SIngo Molnar 		    "number of mmap data pages"),
7243efa1cc9SIngo Molnar 	OPT_BOOLEAN('g', "call-graph", &call_graph,
7253efa1cc9SIngo Molnar 		    "do call-graph (stack chain/backtrace) recording"),
726c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
7273da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
728649c48a9SPeter Zijlstra 	OPT_BOOLEAN('s', "stat", &inherit_stat,
729649c48a9SPeter Zijlstra 		    "per thread counts"),
7304bba828dSAnton Blanchard 	OPT_BOOLEAN('d', "data", &sample_address,
7314bba828dSAnton Blanchard 		    "Sample addresses"),
732649c48a9SPeter Zijlstra 	OPT_BOOLEAN('n', "no-samples", &no_samples,
733649c48a9SPeter Zijlstra 		    "don't sample"),
734d1302522SFrederic Weisbecker 	OPT_BOOLEAN('M', "multiplex", &multiplex,
735d1302522SFrederic Weisbecker 		    "multiplex counter output in a single channel"),
73686470930SIngo Molnar 	OPT_END()
73786470930SIngo Molnar };
73886470930SIngo Molnar 
739f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used)
74086470930SIngo Molnar {
74186470930SIngo Molnar 	int counter;
742d6d901c2SZhang, Yanmin 	int i,j;
74386470930SIngo Molnar 
744a0541234SAnton Blanchard 	argc = parse_options(argc, argv, options, record_usage,
745a0541234SAnton Blanchard 			    PARSE_OPT_STOP_AT_NON_OPTION);
746d6d901c2SZhang, Yanmin 	if (!argc && target_pid == -1 && target_tid == -1 &&
747d6d901c2SZhang, Yanmin 		!system_wide && profile_cpu == -1)
74886470930SIngo Molnar 		usage_with_options(record_usage, options);
74986470930SIngo Molnar 
750655000e7SArnaldo Carvalho de Melo 	symbol__init();
751655000e7SArnaldo Carvalho de Melo 
752bbd36e5eSPeter Zijlstra 	if (!nr_counters) {
75386470930SIngo Molnar 		nr_counters	= 1;
754bbd36e5eSPeter Zijlstra 		attrs[0].type	= PERF_TYPE_HARDWARE;
755bbd36e5eSPeter Zijlstra 		attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
756bbd36e5eSPeter Zijlstra 	}
75786470930SIngo Molnar 
758d6d901c2SZhang, Yanmin 	if (target_pid != -1) {
759d6d901c2SZhang, Yanmin 		target_tid = target_pid;
760d6d901c2SZhang, Yanmin 		thread_num = find_all_tid(target_pid, &all_tids);
761d6d901c2SZhang, Yanmin 		if (thread_num <= 0) {
762d6d901c2SZhang, Yanmin 			fprintf(stderr, "Can't find all threads of pid %d\n",
763d6d901c2SZhang, Yanmin 					target_pid);
764d6d901c2SZhang, Yanmin 			usage_with_options(record_usage, options);
765d6d901c2SZhang, Yanmin 		}
766d6d901c2SZhang, Yanmin 	} else {
767d6d901c2SZhang, Yanmin 		all_tids=malloc(sizeof(pid_t));
768d6d901c2SZhang, Yanmin 		if (!all_tids)
769d6d901c2SZhang, Yanmin 			return -ENOMEM;
770d6d901c2SZhang, Yanmin 
771d6d901c2SZhang, Yanmin 		all_tids[0] = target_tid;
772d6d901c2SZhang, Yanmin 		thread_num = 1;
773d6d901c2SZhang, Yanmin 	}
774d6d901c2SZhang, Yanmin 
775d6d901c2SZhang, Yanmin 	for (i = 0; i < MAX_NR_CPUS; i++) {
776d6d901c2SZhang, Yanmin 		for (j = 0; j < MAX_COUNTERS; j++) {
777d6d901c2SZhang, Yanmin 			fd[i][j] = malloc(sizeof(int)*thread_num);
7785a103174SZhang, Yanmin 			mmap_array[i][j] = zalloc(
779d6d901c2SZhang, Yanmin 				sizeof(struct mmap_data)*thread_num);
780d6d901c2SZhang, Yanmin 			if (!fd[i][j] || !mmap_array[i][j])
781d6d901c2SZhang, Yanmin 				return -ENOMEM;
782d6d901c2SZhang, Yanmin 		}
783d6d901c2SZhang, Yanmin 	}
784d6d901c2SZhang, Yanmin 	event_array = malloc(
785d6d901c2SZhang, Yanmin 		sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
786d6d901c2SZhang, Yanmin 	if (!event_array)
787d6d901c2SZhang, Yanmin 		return -ENOMEM;
788d6d901c2SZhang, Yanmin 
7897e4ff9e3SMike Galbraith 	/*
7907e4ff9e3SMike Galbraith 	 * User specified count overrides default frequency.
7917e4ff9e3SMike Galbraith 	 */
7927e4ff9e3SMike Galbraith 	if (default_interval)
7937e4ff9e3SMike Galbraith 		freq = 0;
7947e4ff9e3SMike Galbraith 	else if (freq) {
7957e4ff9e3SMike Galbraith 		default_interval = freq;
7967e4ff9e3SMike Galbraith 	} else {
7977e4ff9e3SMike Galbraith 		fprintf(stderr, "frequency and count are zero, aborting\n");
7987e4ff9e3SMike Galbraith 		exit(EXIT_FAILURE);
7997e4ff9e3SMike Galbraith 	}
8007e4ff9e3SMike Galbraith 
80186470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
80286470930SIngo Molnar 		if (attrs[counter].sample_period)
80386470930SIngo Molnar 			continue;
80486470930SIngo Molnar 
80586470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
80686470930SIngo Molnar 	}
80786470930SIngo Molnar 
80886470930SIngo Molnar 	return __cmd_record(argc, argv);
80986470930SIngo Molnar }
810