xref: /openbmc/linux/tools/perf/builtin-record.c (revision d7065adb9b4f3384c2615f0a3dbdb6c3aae1eb18)
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"
21361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h"
2269aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h"
238f28827aSFrederic Weisbecker #include "util/debug.h"
2494c744b6SArnaldo Carvalho de Melo #include "util/session.h"
258d06367fSArnaldo Carvalho de Melo #include "util/symbol.h"
26a12b51c4SPaul Mackerras #include "util/cpumap.h"
277c6a1c65SPeter Zijlstra 
2886470930SIngo Molnar #include <unistd.h>
2986470930SIngo Molnar #include <sched.h>
30a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h>
3186470930SIngo Molnar 
3269aad6f1SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
330a27d7f9SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->id, x, y)
3469aad6f1SArnaldo Carvalho de Melo 
357865e817SFrederic Weisbecker enum write_mode_t {
367865e817SFrederic Weisbecker 	WRITE_FORCE,
377865e817SFrederic Weisbecker 	WRITE_APPEND
387865e817SFrederic Weisbecker };
397865e817SFrederic Weisbecker 
403de29cabSStephane Eranian static u64			user_interval			= ULLONG_MAX;
413de29cabSStephane Eranian static u64			default_interval		=      0;
42640c03ceSArnaldo Carvalho de Melo static u64			sample_type;
4386470930SIngo Molnar 
4460d567e2SArnaldo Carvalho de Melo static struct cpu_map		*cpus;
4586470930SIngo Molnar static unsigned int		page_size;
4686470930SIngo Molnar static unsigned int		mmap_pages			=    128;
47f9212819SFrederic Weisbecker static unsigned int		user_freq 			= UINT_MAX;
4842e59d7dSIngo Molnar static int			freq				=   1000;
4986470930SIngo Molnar static int			output;
50529870e3STom Zanussi static int			pipe_output			=      0;
51*d7065adbSFranck Bui-Huu static const char		*output_name			= NULL;
5286470930SIngo Molnar static int			group				=      0;
531967936dSArnaldo Carvalho de Melo static int			realtime_prio			=      0;
54acac03faSKirill Smelkov static bool			nodelay				=  false;
55c0555642SIan Munsie static bool			raw_samples			=  false;
569c90a61cSArnaldo Carvalho de Melo static bool			sample_id_all_avail		=   true;
57c0555642SIan Munsie static bool			system_wide			=  false;
5886470930SIngo Molnar static pid_t			target_pid			=     -1;
59d6d901c2SZhang, Yanmin static pid_t			target_tid			=     -1;
605c98d466SArnaldo Carvalho de Melo static struct thread_map	*threads;
61933da83aSChris Wilson static pid_t			child_pid			=     -1;
622e6cdf99SStephane Eranian static bool			no_inherit			=  false;
637865e817SFrederic Weisbecker static enum write_mode_t	write_mode			= WRITE_FORCE;
64c0555642SIan Munsie static bool			call_graph			=  false;
65c0555642SIan Munsie static bool			inherit_stat			=  false;
66c0555642SIan Munsie static bool			no_samples			=  false;
67c0555642SIan Munsie static bool			sample_address			=  false;
689c90a61cSArnaldo Carvalho de Melo static bool			sample_time			=  false;
69a1ac1d3cSStephane Eranian static bool			no_buildid			=  false;
70baa2f6ceSArnaldo Carvalho de Melo static bool			no_buildid_cache		=  false;
71361c99a6SArnaldo Carvalho de Melo static struct perf_evlist	*evsel_list;
7286470930SIngo Molnar 
7342e59d7dSIngo Molnar static long			samples				=      0;
7442e59d7dSIngo Molnar static u64			bytes_written			=      0;
7586470930SIngo Molnar 
76f5970550SPeter Zijlstra static int			file_new			=      1;
776122e4e4SArnaldo Carvalho de Melo static off_t			post_processing_offset;
787c6a1c65SPeter Zijlstra 
7994c744b6SArnaldo Carvalho de Melo static struct perf_session	*session;
80c45c6ea2SStephane Eranian static const char		*cpu_list;
81f5970550SPeter Zijlstra 
829215545eSTom Zanussi static void advance_output(size_t size)
839215545eSTom Zanussi {
849215545eSTom Zanussi 	bytes_written += size;
859215545eSTom Zanussi }
869215545eSTom Zanussi 
87f5970550SPeter Zijlstra static void write_output(void *buf, size_t size)
88f5970550SPeter Zijlstra {
89f5970550SPeter Zijlstra 	while (size) {
90f5970550SPeter Zijlstra 		int ret = write(output, buf, size);
91f5970550SPeter Zijlstra 
92f5970550SPeter Zijlstra 		if (ret < 0)
93f5970550SPeter Zijlstra 			die("failed to write");
94f5970550SPeter Zijlstra 
95f5970550SPeter Zijlstra 		size -= ret;
96f5970550SPeter Zijlstra 		buf += ret;
97f5970550SPeter Zijlstra 
98f5970550SPeter Zijlstra 		bytes_written += ret;
99f5970550SPeter Zijlstra 	}
100f5970550SPeter Zijlstra }
101f5970550SPeter Zijlstra 
102d8f66248SArnaldo Carvalho de Melo static int process_synthesized_event(event_t *event,
103640c03ceSArnaldo Carvalho de Melo 				     struct sample_data *sample __used,
104d8f66248SArnaldo Carvalho de Melo 				     struct perf_session *self __used)
105234fbbf5SArnaldo Carvalho de Melo {
1066122e4e4SArnaldo Carvalho de Melo 	write_output(event, event->header.size);
107234fbbf5SArnaldo Carvalho de Melo 	return 0;
108234fbbf5SArnaldo Carvalho de Melo }
109234fbbf5SArnaldo Carvalho de Melo 
110744bd8aaSArnaldo Carvalho de Melo static void mmap_read(struct perf_mmap *md)
11186470930SIngo Molnar {
112744bd8aaSArnaldo Carvalho de Melo 	unsigned int head = perf_mmap__read_head(md);
11386470930SIngo Molnar 	unsigned int old = md->prev;
11486470930SIngo Molnar 	unsigned char *data = md->base + page_size;
11586470930SIngo Molnar 	unsigned long size;
11686470930SIngo Molnar 	void *buf;
11786470930SIngo Molnar 	int diff;
11886470930SIngo Molnar 
11986470930SIngo Molnar 	/*
12086470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
12186470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
12286470930SIngo Molnar 	 *
12386470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
12486470930SIngo Molnar 	 *
12586470930SIngo Molnar 	 * In either case, truncate and restart at head.
12686470930SIngo Molnar 	 */
12786470930SIngo Molnar 	diff = head - old;
1289d91a6f7SPeter Zijlstra 	if (diff < 0) {
129ef365cefSRuss Anderson 		fprintf(stderr, "WARNING: failed to keep up with mmap data\n");
13086470930SIngo Molnar 		/*
13186470930SIngo Molnar 		 * head points to a known good entry, start there.
13286470930SIngo Molnar 		 */
13386470930SIngo Molnar 		old = head;
13486470930SIngo Molnar 	}
13586470930SIngo Molnar 
13686470930SIngo Molnar 	if (old != head)
13786470930SIngo Molnar 		samples++;
13886470930SIngo Molnar 
13986470930SIngo Molnar 	size = head - old;
14086470930SIngo Molnar 
14186470930SIngo Molnar 	if ((old & md->mask) + size != (head & md->mask)) {
14286470930SIngo Molnar 		buf = &data[old & md->mask];
14386470930SIngo Molnar 		size = md->mask + 1 - (old & md->mask);
14486470930SIngo Molnar 		old += size;
14586470930SIngo Molnar 
1466122e4e4SArnaldo Carvalho de Melo 		write_output(buf, size);
14786470930SIngo Molnar 	}
14886470930SIngo Molnar 
14986470930SIngo Molnar 	buf = &data[old & md->mask];
15086470930SIngo Molnar 	size = head - old;
15186470930SIngo Molnar 	old += size;
15286470930SIngo Molnar 
1536122e4e4SArnaldo Carvalho de Melo 	write_output(buf, size);
15486470930SIngo Molnar 
15586470930SIngo Molnar 	md->prev = old;
156115d2d89SArnaldo Carvalho de Melo 	perf_mmap__write_tail(md, old);
15786470930SIngo Molnar }
15886470930SIngo Molnar 
15986470930SIngo Molnar static volatile int done = 0;
160f7b7c26eSPeter Zijlstra static volatile int signr = -1;
16186470930SIngo Molnar 
16286470930SIngo Molnar static void sig_handler(int sig)
16386470930SIngo Molnar {
16486470930SIngo Molnar 	done = 1;
165f7b7c26eSPeter Zijlstra 	signr = sig;
166f7b7c26eSPeter Zijlstra }
167f7b7c26eSPeter Zijlstra 
168f7b7c26eSPeter Zijlstra static void sig_atexit(void)
169f7b7c26eSPeter Zijlstra {
1705ffc8881SIan Munsie 	if (child_pid > 0)
171933da83aSChris Wilson 		kill(child_pid, SIGTERM);
172933da83aSChris Wilson 
17318483b81SArnaldo Carvalho de Melo 	if (signr == -1 || signr == SIGUSR1)
174f7b7c26eSPeter Zijlstra 		return;
175f7b7c26eSPeter Zijlstra 
176f7b7c26eSPeter Zijlstra 	signal(signr, SIG_DFL);
177f7b7c26eSPeter Zijlstra 	kill(getpid(), signr);
17886470930SIngo Molnar }
17986470930SIngo Molnar 
180cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr)
1817c6a1c65SPeter Zijlstra {
1827c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
1837c6a1c65SPeter Zijlstra 
18494c744b6SArnaldo Carvalho de Melo 	if (nr < session->header.attrs) {
18594c744b6SArnaldo Carvalho de Melo 		h_attr = session->header.attr[nr];
1867c6a1c65SPeter Zijlstra 	} else {
1877c6a1c65SPeter Zijlstra 		h_attr = perf_header_attr__new(a);
188dc79c0fcSArnaldo Carvalho de Melo 		if (h_attr != NULL)
18994c744b6SArnaldo Carvalho de Melo 			if (perf_header__add_attr(&session->header, h_attr) < 0) {
19011deb1f9SArnaldo Carvalho de Melo 				perf_header_attr__delete(h_attr);
19111deb1f9SArnaldo Carvalho de Melo 				h_attr = NULL;
19211deb1f9SArnaldo Carvalho de Melo 			}
1937c6a1c65SPeter Zijlstra 	}
1947c6a1c65SPeter Zijlstra 
1957c6a1c65SPeter Zijlstra 	return h_attr;
1967c6a1c65SPeter Zijlstra }
1977c6a1c65SPeter Zijlstra 
1980a27d7f9SArnaldo Carvalho de Melo static void create_counter(struct perf_evsel *evsel, int cpu)
19986470930SIngo Molnar {
20069aad6f1SArnaldo Carvalho de Melo 	char *filter = evsel->filter;
20169aad6f1SArnaldo Carvalho de Melo 	struct perf_event_attr *attr = &evsel->attr;
2027c6a1c65SPeter Zijlstra 	struct perf_header_attr *h_attr;
2030a27d7f9SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
204d6d901c2SZhang, Yanmin 	int thread_index;
205c171b552SLi Zefan 	int ret;
206dd7927f4SArnaldo Carvalho de Melo 
207dd7927f4SArnaldo Carvalho de Melo 	for (thread_index = 0; thread_index < threads->nr; thread_index++) {
208dd7927f4SArnaldo Carvalho de Melo 		h_attr = get_header_attr(attr, evsel->idx);
209dd7927f4SArnaldo Carvalho de Melo 		if (h_attr == NULL)
210dd7927f4SArnaldo Carvalho de Melo 			die("nomem\n");
211dd7927f4SArnaldo Carvalho de Melo 
212dd7927f4SArnaldo Carvalho de Melo 		if (!file_new) {
213dd7927f4SArnaldo Carvalho de Melo 			if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
214dd7927f4SArnaldo Carvalho de Melo 				fprintf(stderr, "incompatible append\n");
215dd7927f4SArnaldo Carvalho de Melo 				exit(-1);
216dd7927f4SArnaldo Carvalho de Melo 			}
217dd7927f4SArnaldo Carvalho de Melo 		}
218dd7927f4SArnaldo Carvalho de Melo 
2190a27d7f9SArnaldo Carvalho de Melo 		sid = SID(evsel, cpu, thread_index);
2200a27d7f9SArnaldo Carvalho de Melo 		if (perf_header_attr__add_id(h_attr, sid->id) < 0) {
221dd7927f4SArnaldo Carvalho de Melo 			pr_warning("Not enough memory to add id\n");
222dd7927f4SArnaldo Carvalho de Melo 			exit(-1);
223dd7927f4SArnaldo Carvalho de Melo 		}
224dd7927f4SArnaldo Carvalho de Melo 
225dd7927f4SArnaldo Carvalho de Melo 		if (filter != NULL) {
226dd7927f4SArnaldo Carvalho de Melo 			ret = ioctl(FD(evsel, cpu, thread_index),
227dd7927f4SArnaldo Carvalho de Melo 				    PERF_EVENT_IOC_SET_FILTER, filter);
228dd7927f4SArnaldo Carvalho de Melo 			if (ret) {
229dd7927f4SArnaldo Carvalho de Melo 				error("failed to set filter with %d (%s)\n", errno,
230dd7927f4SArnaldo Carvalho de Melo 						strerror(errno));
231dd7927f4SArnaldo Carvalho de Melo 				exit(-1);
232dd7927f4SArnaldo Carvalho de Melo 			}
233dd7927f4SArnaldo Carvalho de Melo 		}
234dd7927f4SArnaldo Carvalho de Melo 	}
235dd7927f4SArnaldo Carvalho de Melo 
236dd7927f4SArnaldo Carvalho de Melo 	if (!sample_type)
237dd7927f4SArnaldo Carvalho de Melo 		sample_type = attr->sample_type;
238dd7927f4SArnaldo Carvalho de Melo }
239dd7927f4SArnaldo Carvalho de Melo 
240dd7927f4SArnaldo Carvalho de Melo static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist)
241dd7927f4SArnaldo Carvalho de Melo {
242dd7927f4SArnaldo Carvalho de Melo 	struct perf_event_attr *attr = &evsel->attr;
243dd7927f4SArnaldo Carvalho de Melo 	int track = !evsel->idx; /* only the first counter needs these */
2447c6a1c65SPeter Zijlstra 
2457c6a1c65SPeter Zijlstra 	attr->read_format	= PERF_FORMAT_TOTAL_TIME_ENABLED |
2467c6a1c65SPeter Zijlstra 				  PERF_FORMAT_TOTAL_TIME_RUNNING |
2477c6a1c65SPeter Zijlstra 				  PERF_FORMAT_ID;
24886470930SIngo Molnar 
2493a9f131fSFrederic Weisbecker 	attr->sample_type	|= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
2503efa1cc9SIngo Molnar 
251361c99a6SArnaldo Carvalho de Melo 	if (evlist->nr_entries > 1)
2528907fd60SEric B Munson 		attr->sample_type |= PERF_SAMPLE_ID;
2538907fd60SEric B Munson 
254f9212819SFrederic Weisbecker 	/*
255f9212819SFrederic Weisbecker 	 * We default some events to a 1 default interval. But keep
256f9212819SFrederic Weisbecker 	 * it a weak assumption overridable by the user.
257f9212819SFrederic Weisbecker 	 */
258f9212819SFrederic Weisbecker 	if (!attr->sample_period || (user_freq != UINT_MAX &&
2593de29cabSStephane Eranian 				     user_interval != ULLONG_MAX)) {
26086470930SIngo Molnar 		if (freq) {
261ea1900e5SPeter Zijlstra 			attr->sample_type	|= PERF_SAMPLE_PERIOD;
26286470930SIngo Molnar 			attr->freq		= 1;
26386470930SIngo Molnar 			attr->sample_freq	= freq;
264f9212819SFrederic Weisbecker 		} else {
265f9212819SFrederic Weisbecker 			attr->sample_period = default_interval;
266f9212819SFrederic Weisbecker 		}
26786470930SIngo Molnar 	}
2683efa1cc9SIngo Molnar 
269649c48a9SPeter Zijlstra 	if (no_samples)
270649c48a9SPeter Zijlstra 		attr->sample_freq = 0;
271649c48a9SPeter Zijlstra 
272649c48a9SPeter Zijlstra 	if (inherit_stat)
273649c48a9SPeter Zijlstra 		attr->inherit_stat = 1;
274649c48a9SPeter Zijlstra 
2753af9e859SEric B Munson 	if (sample_address) {
2764bba828dSAnton Blanchard 		attr->sample_type	|= PERF_SAMPLE_ADDR;
2773af9e859SEric B Munson 		attr->mmap_data = track;
2783af9e859SEric B Munson 	}
2794bba828dSAnton Blanchard 
2803efa1cc9SIngo Molnar 	if (call_graph)
2813efa1cc9SIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
2823efa1cc9SIngo Molnar 
283f60f3593SArun Sharma 	if (system_wide)
284f60f3593SArun Sharma 		attr->sample_type	|= PERF_SAMPLE_CPU;
285f60f3593SArun Sharma 
286a43d3f08SArnaldo Carvalho de Melo 	if (sample_id_all_avail &&
287a43d3f08SArnaldo Carvalho de Melo 	    (sample_time || system_wide || !no_inherit || cpu_list))
2889c90a61cSArnaldo Carvalho de Melo 		attr->sample_type	|= PERF_SAMPLE_TIME;
2899c90a61cSArnaldo Carvalho de Melo 
290cd6feeeaSIngo Molnar 	if (raw_samples) {
2916ddf259dSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_TIME;
292daac07b2SFrederic Weisbecker 		attr->sample_type	|= PERF_SAMPLE_RAW;
293cd6feeeaSIngo Molnar 		attr->sample_type	|= PERF_SAMPLE_CPU;
294cd6feeeaSIngo Molnar 	}
295f413cdb8SFrederic Weisbecker 
296acac03faSKirill Smelkov 	if (nodelay) {
297acac03faSKirill Smelkov 		attr->watermark = 0;
298acac03faSKirill Smelkov 		attr->wakeup_events = 1;
299acac03faSKirill Smelkov 	}
300acac03faSKirill Smelkov 
30186470930SIngo Molnar 	attr->mmap		= track;
30286470930SIngo Molnar 	attr->comm		= track;
303dd7927f4SArnaldo Carvalho de Melo 
3042e6cdf99SStephane Eranian 	if (target_pid == -1 && target_tid == -1 && !system_wide) {
3054502d77cSPeter Zijlstra 		attr->disabled = 1;
306bedbfdeaSEric B Munson 		attr->enable_on_exec = 1;
30746be604bSZhang, Yanmin 	}
308dd7927f4SArnaldo Carvalho de Melo }
309dd7927f4SArnaldo Carvalho de Melo 
310dd7927f4SArnaldo Carvalho de Melo static void open_counters(struct perf_evlist *evlist)
311dd7927f4SArnaldo Carvalho de Melo {
312dd7927f4SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
313dd7927f4SArnaldo Carvalho de Melo 	int cpu;
314dd7927f4SArnaldo Carvalho de Melo 
315dd7927f4SArnaldo Carvalho de Melo 	list_for_each_entry(pos, &evlist->entries, node) {
316dd7927f4SArnaldo Carvalho de Melo 		struct perf_event_attr *attr = &pos->attr;
317dd7927f4SArnaldo Carvalho de Melo 		/*
318dd7927f4SArnaldo Carvalho de Melo 		 * Check if parse_single_tracepoint_event has already asked for
319dd7927f4SArnaldo Carvalho de Melo 		 * PERF_SAMPLE_TIME.
320dd7927f4SArnaldo Carvalho de Melo 		 *
321dd7927f4SArnaldo Carvalho de Melo 		 * XXX this is kludgy but short term fix for problems introduced by
322dd7927f4SArnaldo Carvalho de Melo 		 * eac23d1c that broke 'perf script' by having different sample_types
323dd7927f4SArnaldo Carvalho de Melo 		 * when using multiple tracepoint events when we use a perf binary
324dd7927f4SArnaldo Carvalho de Melo 		 * that tries to use sample_id_all on an older kernel.
325dd7927f4SArnaldo Carvalho de Melo 		 *
326dd7927f4SArnaldo Carvalho de Melo 		 * We need to move counter creation to perf_session, support
327dd7927f4SArnaldo Carvalho de Melo 		 * different sample_types, etc.
328dd7927f4SArnaldo Carvalho de Melo 		 */
329dd7927f4SArnaldo Carvalho de Melo 		bool time_needed = attr->sample_type & PERF_SAMPLE_TIME;
330dd7927f4SArnaldo Carvalho de Melo 
331dd7927f4SArnaldo Carvalho de Melo 		config_attr(pos, evlist);
3329c90a61cSArnaldo Carvalho de Melo retry_sample_id:
3339c90a61cSArnaldo Carvalho de Melo 		attr->sample_id_all = sample_id_all_avail ? 1 : 0;
3343da297a6SIngo Molnar try_again:
335dd7927f4SArnaldo Carvalho de Melo 		if (perf_evsel__open(pos, cpus, threads, group, !no_inherit) < 0) {
33686470930SIngo Molnar 			int err = errno;
33786470930SIngo Molnar 
338c10edee2SPekka Enberg 			if (err == EPERM || err == EACCES)
3396230f2c7SArnaldo Carvalho de Melo 				die("Permission error - are you root?\n"
340d6d901c2SZhang, Yanmin 					"\t Consider tweaking"
341d6d901c2SZhang, Yanmin 					" /proc/sys/kernel/perf_event_paranoid.\n");
342c45c6ea2SStephane Eranian 			else if (err ==  ENODEV && cpu_list) {
343d6d901c2SZhang, Yanmin 				die("No such device - did you specify"
344d6d901c2SZhang, Yanmin 					" an out-of-range profile CPU?\n");
3459c90a61cSArnaldo Carvalho de Melo 			} else if (err == EINVAL && sample_id_all_avail) {
3469c90a61cSArnaldo Carvalho de Melo 				/*
3479c90a61cSArnaldo Carvalho de Melo 				 * Old kernel, no attr->sample_id_type_all field
3489c90a61cSArnaldo Carvalho de Melo 				 */
3499c90a61cSArnaldo Carvalho de Melo 				sample_id_all_avail = false;
350a43d3f08SArnaldo Carvalho de Melo 				if (!sample_time && !raw_samples && !time_needed)
351eac23d1cSIan Munsie 					attr->sample_type &= ~PERF_SAMPLE_TIME;
352eac23d1cSIan Munsie 
3539c90a61cSArnaldo Carvalho de Melo 				goto retry_sample_id;
354d6d901c2SZhang, Yanmin 			}
3553da297a6SIngo Molnar 
3563da297a6SIngo Molnar 			/*
3573da297a6SIngo Molnar 			 * If it's cycles then fall back to hrtimer
3583da297a6SIngo Molnar 			 * based cpu-clock-tick sw counter, which
3593da297a6SIngo Molnar 			 * is always available even if no PMU support:
3603da297a6SIngo Molnar 			 */
3613da297a6SIngo Molnar 			if (attr->type == PERF_TYPE_HARDWARE
362f4dbfa8fSPeter Zijlstra 					&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
3633da297a6SIngo Molnar 
3643da297a6SIngo Molnar 				if (verbose)
3653da297a6SIngo Molnar 					warning(" ... trying to fall back to cpu-clock-ticks\n");
3663da297a6SIngo Molnar 				attr->type = PERF_TYPE_SOFTWARE;
367f4dbfa8fSPeter Zijlstra 				attr->config = PERF_COUNT_SW_CPU_CLOCK;
3683da297a6SIngo Molnar 				goto try_again;
3693da297a6SIngo Molnar 			}
37030c806a0SIngo Molnar 			printf("\n");
371d9cf837eSCorey Ashford 			error("sys_perf_event_open() syscall returned with %d (%s).  /bin/dmesg may provide additional information.\n",
372dd7927f4SArnaldo Carvalho de Melo 			      err, strerror(err));
373bfd45118SSimon Kaempflein 
374bfd45118SSimon Kaempflein #if defined(__i386__) || defined(__x86_64__)
375bfd45118SSimon Kaempflein 			if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
376d6d901c2SZhang, Yanmin 				die("No hardware sampling interrupt available."
377d6d901c2SZhang, Yanmin 				    " No APIC? If so then you can boot the kernel"
378d6d901c2SZhang, Yanmin 				    " with the \"lapic\" boot parameter to"
379d6d901c2SZhang, Yanmin 				    " force-enable it.\n");
380bfd45118SSimon Kaempflein #endif
381bfd45118SSimon Kaempflein 
382cdd6c482SIngo Molnar 			die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
3837c6a1c65SPeter Zijlstra 		}
3847c6a1c65SPeter Zijlstra 	}
3857c6a1c65SPeter Zijlstra 
3860a27d7f9SArnaldo Carvalho de Melo 	if (perf_evlist__mmap(evlist, cpus, threads, mmap_pages, false) < 0)
3870a27d7f9SArnaldo Carvalho de Melo 		die("failed to mmap with %d (%s)\n", errno, strerror(errno));
3880a27d7f9SArnaldo Carvalho de Melo 
389dd7927f4SArnaldo Carvalho de Melo 	for (cpu = 0; cpu < cpus->nr; ++cpu) {
390361c99a6SArnaldo Carvalho de Melo 		list_for_each_entry(pos, &evlist->entries, node)
3910a27d7f9SArnaldo Carvalho de Melo 			create_counter(pos, cpu);
392dd7927f4SArnaldo Carvalho de Melo 	}
39386470930SIngo Molnar }
39486470930SIngo Molnar 
3956122e4e4SArnaldo Carvalho de Melo static int process_buildids(void)
3966122e4e4SArnaldo Carvalho de Melo {
3976122e4e4SArnaldo Carvalho de Melo 	u64 size = lseek(output, 0, SEEK_CUR);
3986122e4e4SArnaldo Carvalho de Melo 
3999f591fd7SArnaldo Carvalho de Melo 	if (size == 0)
4009f591fd7SArnaldo Carvalho de Melo 		return 0;
4019f591fd7SArnaldo Carvalho de Melo 
4026122e4e4SArnaldo Carvalho de Melo 	session->fd = output;
4036122e4e4SArnaldo Carvalho de Melo 	return __perf_session__process_events(session, post_processing_offset,
4046122e4e4SArnaldo Carvalho de Melo 					      size - post_processing_offset,
4056122e4e4SArnaldo Carvalho de Melo 					      size, &build_id__mark_dso_hit_ops);
4066122e4e4SArnaldo Carvalho de Melo }
4076122e4e4SArnaldo Carvalho de Melo 
408f5970550SPeter Zijlstra static void atexit_header(void)
409f5970550SPeter Zijlstra {
410c7929e47STom Zanussi 	if (!pipe_output) {
41194c744b6SArnaldo Carvalho de Melo 		session->header.data_size += bytes_written;
412f5970550SPeter Zijlstra 
413baa2f6ceSArnaldo Carvalho de Melo 		if (!no_buildid)
4146122e4e4SArnaldo Carvalho de Melo 			process_buildids();
415361c99a6SArnaldo Carvalho de Melo 		perf_header__write(&session->header, evsel_list, output, true);
41639d17dacSArnaldo Carvalho de Melo 		perf_session__delete(session);
417361c99a6SArnaldo Carvalho de Melo 		perf_evlist__delete(evsel_list);
418d65a458bSArnaldo Carvalho de Melo 		symbol__exit();
419c7929e47STom Zanussi 	}
420f5970550SPeter Zijlstra }
421f5970550SPeter Zijlstra 
42223346f21SArnaldo Carvalho de Melo static void event__synthesize_guest_os(struct machine *machine, void *data)
423a1645ce1SZhang, Yanmin {
424a1645ce1SZhang, Yanmin 	int err;
42523346f21SArnaldo Carvalho de Melo 	struct perf_session *psession = data;
426a1645ce1SZhang, Yanmin 
42723346f21SArnaldo Carvalho de Melo 	if (machine__is_host(machine))
428a1645ce1SZhang, Yanmin 		return;
429a1645ce1SZhang, Yanmin 
430a1645ce1SZhang, Yanmin 	/*
431a1645ce1SZhang, Yanmin 	 *As for guest kernel when processing subcommand record&report,
432a1645ce1SZhang, Yanmin 	 *we arrange module mmap prior to guest kernel mmap and trigger
433a1645ce1SZhang, Yanmin 	 *a preload dso because default guest module symbols are loaded
434a1645ce1SZhang, Yanmin 	 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
435a1645ce1SZhang, Yanmin 	 *method is used to avoid symbol missing when the first addr is
436a1645ce1SZhang, Yanmin 	 *in module instead of in guest kernel.
437a1645ce1SZhang, Yanmin 	 */
438a1645ce1SZhang, Yanmin 	err = event__synthesize_modules(process_synthesized_event,
43923346f21SArnaldo Carvalho de Melo 					psession, machine);
440a1645ce1SZhang, Yanmin 	if (err < 0)
441a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
44223346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
443a1645ce1SZhang, Yanmin 
444a1645ce1SZhang, Yanmin 	/*
445a1645ce1SZhang, Yanmin 	 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
446a1645ce1SZhang, Yanmin 	 * have no _text sometimes.
447a1645ce1SZhang, Yanmin 	 */
448a1645ce1SZhang, Yanmin 	err = event__synthesize_kernel_mmap(process_synthesized_event,
44923346f21SArnaldo Carvalho de Melo 					    psession, machine, "_text");
450a1645ce1SZhang, Yanmin 	if (err < 0)
451a1645ce1SZhang, Yanmin 		err = event__synthesize_kernel_mmap(process_synthesized_event,
45223346f21SArnaldo Carvalho de Melo 						    psession, machine, "_stext");
453a1645ce1SZhang, Yanmin 	if (err < 0)
454a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
45523346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
456a1645ce1SZhang, Yanmin }
457a1645ce1SZhang, Yanmin 
45898402807SFrederic Weisbecker static struct perf_event_header finished_round_event = {
45998402807SFrederic Weisbecker 	.size = sizeof(struct perf_event_header),
46098402807SFrederic Weisbecker 	.type = PERF_RECORD_FINISHED_ROUND,
46198402807SFrederic Weisbecker };
46298402807SFrederic Weisbecker 
46398402807SFrederic Weisbecker static void mmap_read_all(void)
46498402807SFrederic Weisbecker {
4650e2e63ddSPeter Zijlstra 	int i;
46698402807SFrederic Weisbecker 
467dd7927f4SArnaldo Carvalho de Melo 	for (i = 0; i < cpus->nr; i++) {
4680a27d7f9SArnaldo Carvalho de Melo 		if (evsel_list->mmap[i].base)
4690a27d7f9SArnaldo Carvalho de Melo 			mmap_read(&evsel_list->mmap[i]);
47098402807SFrederic Weisbecker 	}
47198402807SFrederic Weisbecker 
47298402807SFrederic Weisbecker 	if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO))
47398402807SFrederic Weisbecker 		write_output(&finished_round_event, sizeof(finished_round_event));
47498402807SFrederic Weisbecker }
47598402807SFrederic Weisbecker 
476d4db3f16SArnaldo Carvalho de Melo static int __cmd_record(int argc, const char **argv)
47786470930SIngo Molnar {
47869aad6f1SArnaldo Carvalho de Melo 	int i;
47986470930SIngo Molnar 	struct stat st;
48086470930SIngo Molnar 	int flags;
4814dc0a04bSArnaldo Carvalho de Melo 	int err;
4828b412664SPeter Zijlstra 	unsigned long waking = 0;
483856e9660SPeter Zijlstra 	int child_ready_pipe[2], go_pipe[2];
48446be604bSZhang, Yanmin 	const bool forks = argc > 0;
485856e9660SPeter Zijlstra 	char buf;
48623346f21SArnaldo Carvalho de Melo 	struct machine *machine;
48786470930SIngo Molnar 
48886470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
48986470930SIngo Molnar 
490f5970550SPeter Zijlstra 	atexit(sig_atexit);
491f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
492f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
49318483b81SArnaldo Carvalho de Melo 	signal(SIGUSR1, sig_handler);
494f5970550SPeter Zijlstra 
495d4db3f16SArnaldo Carvalho de Melo 	if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
496856e9660SPeter Zijlstra 		perror("failed to create pipes");
497856e9660SPeter Zijlstra 		exit(-1);
498856e9660SPeter Zijlstra 	}
499856e9660SPeter Zijlstra 
500*d7065adbSFranck Bui-Huu 	if (!output_name) {
501*d7065adbSFranck Bui-Huu 		if (!fstat(STDOUT_FILENO, &st) && S_ISFIFO(st.st_mode))
502*d7065adbSFranck Bui-Huu 			pipe_output = 1;
503*d7065adbSFranck Bui-Huu 		else
504*d7065adbSFranck Bui-Huu 			output_name = "perf.data";
505*d7065adbSFranck Bui-Huu 	}
506*d7065adbSFranck Bui-Huu 	if (output_name) {
507529870e3STom Zanussi 		if (!strcmp(output_name, "-"))
508529870e3STom Zanussi 			pipe_output = 1;
509529870e3STom Zanussi 		else if (!stat(output_name, &st) && st.st_size) {
5107865e817SFrederic Weisbecker 			if (write_mode == WRITE_FORCE) {
511b38d3464SArnaldo Carvalho de Melo 				char oldname[PATH_MAX];
512b38d3464SArnaldo Carvalho de Melo 				snprintf(oldname, sizeof(oldname), "%s.old",
513b38d3464SArnaldo Carvalho de Melo 					 output_name);
514b38d3464SArnaldo Carvalho de Melo 				unlink(oldname);
515b38d3464SArnaldo Carvalho de Melo 				rename(output_name, oldname);
516b38d3464SArnaldo Carvalho de Melo 			}
5177865e817SFrederic Weisbecker 		} else if (write_mode == WRITE_APPEND) {
5187865e817SFrederic Weisbecker 			write_mode = WRITE_FORCE;
519266e0e21SPierre Habouzit 		}
520*d7065adbSFranck Bui-Huu 	}
52186470930SIngo Molnar 
522f887f301SXiao Guangrong 	flags = O_CREAT|O_RDWR;
5237865e817SFrederic Weisbecker 	if (write_mode == WRITE_APPEND)
524f5970550SPeter Zijlstra 		file_new = 0;
52586470930SIngo Molnar 	else
52686470930SIngo Molnar 		flags |= O_TRUNC;
52786470930SIngo Molnar 
528529870e3STom Zanussi 	if (pipe_output)
529529870e3STom Zanussi 		output = STDOUT_FILENO;
530529870e3STom Zanussi 	else
53186470930SIngo Molnar 		output = open(output_name, flags, S_IRUSR | S_IWUSR);
53286470930SIngo Molnar 	if (output < 0) {
53386470930SIngo Molnar 		perror("failed to create output file");
53486470930SIngo Molnar 		exit(-1);
53586470930SIngo Molnar 	}
53686470930SIngo Molnar 
5377865e817SFrederic Weisbecker 	session = perf_session__new(output_name, O_WRONLY,
53821ef97f0SIan Munsie 				    write_mode == WRITE_FORCE, false, NULL);
53994c744b6SArnaldo Carvalho de Melo 	if (session == NULL) {
540a9a70bbcSArnaldo Carvalho de Melo 		pr_err("Not enough memory for reading perf file header\n");
541a9a70bbcSArnaldo Carvalho de Melo 		return -1;
542a9a70bbcSArnaldo Carvalho de Melo 	}
543a9a70bbcSArnaldo Carvalho de Melo 
544baa2f6ceSArnaldo Carvalho de Melo 	if (!no_buildid)
545baa2f6ceSArnaldo Carvalho de Melo 		perf_header__set_feat(&session->header, HEADER_BUILD_ID);
546baa2f6ceSArnaldo Carvalho de Melo 
5474dc0a04bSArnaldo Carvalho de Melo 	if (!file_new) {
5488dc58101STom Zanussi 		err = perf_header__read(session, output);
5494dc0a04bSArnaldo Carvalho de Melo 		if (err < 0)
55039d17dacSArnaldo Carvalho de Melo 			goto out_delete_session;
5514dc0a04bSArnaldo Carvalho de Melo 	}
5524dc0a04bSArnaldo Carvalho de Melo 
553361c99a6SArnaldo Carvalho de Melo 	if (have_tracepoints(&evsel_list->entries))
55494c744b6SArnaldo Carvalho de Melo 		perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
55503456a15SFrederic Weisbecker 
55639d17dacSArnaldo Carvalho de Melo 	/*
55739d17dacSArnaldo Carvalho de Melo  	 * perf_session__delete(session) will be called at atexit_header()
55839d17dacSArnaldo Carvalho de Melo 	 */
559f5970550SPeter Zijlstra 	atexit(atexit_header);
560f5970550SPeter Zijlstra 
561d4db3f16SArnaldo Carvalho de Melo 	if (forks) {
56246be604bSZhang, Yanmin 		child_pid = fork();
5632fb750e8SBorislav Petkov 		if (child_pid < 0) {
564856e9660SPeter Zijlstra 			perror("failed to fork");
565856e9660SPeter Zijlstra 			exit(-1);
566856e9660SPeter Zijlstra 		}
5677c6a1c65SPeter Zijlstra 
56846be604bSZhang, Yanmin 		if (!child_pid) {
569529870e3STom Zanussi 			if (pipe_output)
570529870e3STom Zanussi 				dup2(2, 1);
571856e9660SPeter Zijlstra 			close(child_ready_pipe[0]);
572856e9660SPeter Zijlstra 			close(go_pipe[1]);
573856e9660SPeter Zijlstra 			fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
574856e9660SPeter Zijlstra 
575856e9660SPeter Zijlstra 			/*
576856e9660SPeter Zijlstra 			 * Do a dummy execvp to get the PLT entry resolved,
577856e9660SPeter Zijlstra 			 * so we avoid the resolver overhead on the real
578856e9660SPeter Zijlstra 			 * execvp call.
579856e9660SPeter Zijlstra 			 */
580856e9660SPeter Zijlstra 			execvp("", (char **)argv);
581856e9660SPeter Zijlstra 
582856e9660SPeter Zijlstra 			/*
583856e9660SPeter Zijlstra 			 * Tell the parent we're ready to go
584856e9660SPeter Zijlstra 			 */
585856e9660SPeter Zijlstra 			close(child_ready_pipe[1]);
586856e9660SPeter Zijlstra 
587856e9660SPeter Zijlstra 			/*
588856e9660SPeter Zijlstra 			 * Wait until the parent tells us to go.
589856e9660SPeter Zijlstra 			 */
590856e9660SPeter Zijlstra 			if (read(go_pipe[0], &buf, 1) == -1)
591856e9660SPeter Zijlstra 				perror("unable to read pipe");
592856e9660SPeter Zijlstra 
593856e9660SPeter Zijlstra 			execvp(argv[0], (char **)argv);
594856e9660SPeter Zijlstra 
595856e9660SPeter Zijlstra 			perror(argv[0]);
59618483b81SArnaldo Carvalho de Melo 			kill(getppid(), SIGUSR1);
597856e9660SPeter Zijlstra 			exit(-1);
598856e9660SPeter Zijlstra 		}
599856e9660SPeter Zijlstra 
600d6d901c2SZhang, Yanmin 		if (!system_wide && target_tid == -1 && target_pid == -1)
6015c98d466SArnaldo Carvalho de Melo 			threads->map[0] = child_pid;
602d6d901c2SZhang, Yanmin 
603856e9660SPeter Zijlstra 		close(child_ready_pipe[1]);
604856e9660SPeter Zijlstra 		close(go_pipe[0]);
605856e9660SPeter Zijlstra 		/*
606856e9660SPeter Zijlstra 		 * wait for child to settle
607856e9660SPeter Zijlstra 		 */
608856e9660SPeter Zijlstra 		if (read(child_ready_pipe[0], &buf, 1) == -1) {
609856e9660SPeter Zijlstra 			perror("unable to read pipe");
610856e9660SPeter Zijlstra 			exit(-1);
611856e9660SPeter Zijlstra 		}
612856e9660SPeter Zijlstra 		close(child_ready_pipe[0]);
613856e9660SPeter Zijlstra 	}
614856e9660SPeter Zijlstra 
615dd7927f4SArnaldo Carvalho de Melo 	open_counters(evsel_list);
61686470930SIngo Molnar 
617640c03ceSArnaldo Carvalho de Melo 	perf_session__set_sample_type(session, sample_type);
618640c03ceSArnaldo Carvalho de Melo 
619529870e3STom Zanussi 	if (pipe_output) {
620529870e3STom Zanussi 		err = perf_header__write_pipe(output);
621529870e3STom Zanussi 		if (err < 0)
622529870e3STom Zanussi 			return err;
623529870e3STom Zanussi 	} else if (file_new) {
624361c99a6SArnaldo Carvalho de Melo 		err = perf_header__write(&session->header, evsel_list,
625361c99a6SArnaldo Carvalho de Melo 					 output, false);
626d5eed904SArnaldo Carvalho de Melo 		if (err < 0)
627d5eed904SArnaldo Carvalho de Melo 			return err;
628d5eed904SArnaldo Carvalho de Melo 	}
6297c6a1c65SPeter Zijlstra 
6306122e4e4SArnaldo Carvalho de Melo 	post_processing_offset = lseek(output, 0, SEEK_CUR);
6316122e4e4SArnaldo Carvalho de Melo 
6329c90a61cSArnaldo Carvalho de Melo 	perf_session__set_sample_id_all(session, sample_id_all_avail);
6339c90a61cSArnaldo Carvalho de Melo 
6342c46dbb5STom Zanussi 	if (pipe_output) {
6352c46dbb5STom Zanussi 		err = event__synthesize_attrs(&session->header,
6362c46dbb5STom Zanussi 					      process_synthesized_event,
6372c46dbb5STom Zanussi 					      session);
6382c46dbb5STom Zanussi 		if (err < 0) {
6392c46dbb5STom Zanussi 			pr_err("Couldn't synthesize attrs.\n");
6402c46dbb5STom Zanussi 			return err;
6412c46dbb5STom Zanussi 		}
642cd19a035STom Zanussi 
643cd19a035STom Zanussi 		err = event__synthesize_event_types(process_synthesized_event,
644cd19a035STom Zanussi 						    session);
645cd19a035STom Zanussi 		if (err < 0) {
646cd19a035STom Zanussi 			pr_err("Couldn't synthesize event_types.\n");
647cd19a035STom Zanussi 			return err;
648cd19a035STom Zanussi 		}
6499215545eSTom Zanussi 
650361c99a6SArnaldo Carvalho de Melo 		if (have_tracepoints(&evsel_list->entries)) {
65163e0c771STom Zanussi 			/*
65263e0c771STom Zanussi 			 * FIXME err <= 0 here actually means that
65363e0c771STom Zanussi 			 * there were no tracepoints so its not really
65463e0c771STom Zanussi 			 * an error, just that we don't need to
65563e0c771STom Zanussi 			 * synthesize anything.  We really have to
65663e0c771STom Zanussi 			 * return this more properly and also
65763e0c771STom Zanussi 			 * propagate errors that now are calling die()
65863e0c771STom Zanussi 			 */
659361c99a6SArnaldo Carvalho de Melo 			err = event__synthesize_tracing_data(output, evsel_list,
6609215545eSTom Zanussi 							     process_synthesized_event,
6619215545eSTom Zanussi 							     session);
66263e0c771STom Zanussi 			if (err <= 0) {
66363e0c771STom Zanussi 				pr_err("Couldn't record tracing data.\n");
66463e0c771STom Zanussi 				return err;
66563e0c771STom Zanussi 			}
6669215545eSTom Zanussi 			advance_output(err);
6672c46dbb5STom Zanussi 		}
66863e0c771STom Zanussi 	}
6692c46dbb5STom Zanussi 
67023346f21SArnaldo Carvalho de Melo 	machine = perf_session__find_host_machine(session);
67123346f21SArnaldo Carvalho de Melo 	if (!machine) {
672a1645ce1SZhang, Yanmin 		pr_err("Couldn't find native kernel information.\n");
673a1645ce1SZhang, Yanmin 		return -1;
674a1645ce1SZhang, Yanmin 	}
675a1645ce1SZhang, Yanmin 
67656b03f3cSArnaldo Carvalho de Melo 	err = event__synthesize_kernel_mmap(process_synthesized_event,
67723346f21SArnaldo Carvalho de Melo 					    session, machine, "_text");
67870162138SArnaldo Carvalho de Melo 	if (err < 0)
67970162138SArnaldo Carvalho de Melo 		err = event__synthesize_kernel_mmap(process_synthesized_event,
68023346f21SArnaldo Carvalho de Melo 						    session, machine, "_stext");
681c1a3a4b9SArnaldo Carvalho de Melo 	if (err < 0)
682c1a3a4b9SArnaldo Carvalho de Melo 		pr_err("Couldn't record kernel reference relocation symbol\n"
683c1a3a4b9SArnaldo Carvalho de Melo 		       "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
684c1a3a4b9SArnaldo Carvalho de Melo 		       "Check /proc/kallsyms permission or run as root.\n");
68556b03f3cSArnaldo Carvalho de Melo 
686a1645ce1SZhang, Yanmin 	err = event__synthesize_modules(process_synthesized_event,
68723346f21SArnaldo Carvalho de Melo 					session, machine);
688c1a3a4b9SArnaldo Carvalho de Melo 	if (err < 0)
689c1a3a4b9SArnaldo Carvalho de Melo 		pr_err("Couldn't record kernel module information.\n"
690c1a3a4b9SArnaldo Carvalho de Melo 		       "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
691c1a3a4b9SArnaldo Carvalho de Melo 		       "Check /proc/modules permission or run as root.\n");
692c1a3a4b9SArnaldo Carvalho de Melo 
693a1645ce1SZhang, Yanmin 	if (perf_guest)
69423346f21SArnaldo Carvalho de Melo 		perf_session__process_machines(session, event__synthesize_guest_os);
695b7cece76SArnaldo Carvalho de Melo 
696cf103a14SStephane Eranian 	if (!system_wide)
697d6d901c2SZhang, Yanmin 		event__synthesize_thread(target_tid, process_synthesized_event,
698d8f66248SArnaldo Carvalho de Melo 					 session);
699234fbbf5SArnaldo Carvalho de Melo 	else
700d8f66248SArnaldo Carvalho de Melo 		event__synthesize_threads(process_synthesized_event, session);
7017c6a1c65SPeter Zijlstra 
70286470930SIngo Molnar 	if (realtime_prio) {
70386470930SIngo Molnar 		struct sched_param param;
70486470930SIngo Molnar 
70586470930SIngo Molnar 		param.sched_priority = realtime_prio;
70686470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
7076beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
70886470930SIngo Molnar 			exit(-1);
70986470930SIngo Molnar 		}
71086470930SIngo Molnar 	}
71186470930SIngo Molnar 
712856e9660SPeter Zijlstra 	/*
713856e9660SPeter Zijlstra 	 * Let the child rip
714856e9660SPeter Zijlstra 	 */
715d4db3f16SArnaldo Carvalho de Melo 	if (forks)
716856e9660SPeter Zijlstra 		close(go_pipe[1]);
717856e9660SPeter Zijlstra 
718649c48a9SPeter Zijlstra 	for (;;) {
71986470930SIngo Molnar 		int hits = samples;
720d6d901c2SZhang, Yanmin 		int thread;
72186470930SIngo Molnar 
72298402807SFrederic Weisbecker 		mmap_read_all();
72386470930SIngo Molnar 
724649c48a9SPeter Zijlstra 		if (hits == samples) {
725649c48a9SPeter Zijlstra 			if (done)
726649c48a9SPeter Zijlstra 				break;
7275c581041SArnaldo Carvalho de Melo 			err = poll(evsel_list->pollfd, evsel_list->nr_fds, -1);
7288b412664SPeter Zijlstra 			waking++;
7298b412664SPeter Zijlstra 		}
7308b412664SPeter Zijlstra 
7318b412664SPeter Zijlstra 		if (done) {
732dd7927f4SArnaldo Carvalho de Melo 			for (i = 0; i < cpus->nr; i++) {
73369aad6f1SArnaldo Carvalho de Melo 				struct perf_evsel *pos;
73469aad6f1SArnaldo Carvalho de Melo 
735361c99a6SArnaldo Carvalho de Melo 				list_for_each_entry(pos, &evsel_list->entries, node) {
736d6d901c2SZhang, Yanmin 					for (thread = 0;
7375c98d466SArnaldo Carvalho de Melo 						thread < threads->nr;
738d6d901c2SZhang, Yanmin 						thread++)
73969aad6f1SArnaldo Carvalho de Melo 						ioctl(FD(pos, i, thread),
740d6d901c2SZhang, Yanmin 							PERF_EVENT_IOC_DISABLE);
741d6d901c2SZhang, Yanmin 				}
74286470930SIngo Molnar 			}
743649c48a9SPeter Zijlstra 		}
7448b412664SPeter Zijlstra 	}
7458b412664SPeter Zijlstra 
74618483b81SArnaldo Carvalho de Melo 	if (quiet || signr == SIGUSR1)
747b44308f5SArnaldo Carvalho de Melo 		return 0;
748b44308f5SArnaldo Carvalho de Melo 
7498b412664SPeter Zijlstra 	fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
75086470930SIngo Molnar 
75186470930SIngo Molnar 	/*
75286470930SIngo Molnar 	 * Approximate RIP event size: 24 bytes.
75386470930SIngo Molnar 	 */
75486470930SIngo Molnar 	fprintf(stderr,
7559486aa38SArnaldo Carvalho de Melo 		"[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n",
75686470930SIngo Molnar 		(double)bytes_written / 1024.0 / 1024.0,
75786470930SIngo Molnar 		output_name,
75886470930SIngo Molnar 		bytes_written / 24);
75986470930SIngo Molnar 
76086470930SIngo Molnar 	return 0;
76139d17dacSArnaldo Carvalho de Melo 
76239d17dacSArnaldo Carvalho de Melo out_delete_session:
76339d17dacSArnaldo Carvalho de Melo 	perf_session__delete(session);
76439d17dacSArnaldo Carvalho de Melo 	return err;
76586470930SIngo Molnar }
76686470930SIngo Molnar 
76786470930SIngo Molnar static const char * const record_usage[] = {
76886470930SIngo Molnar 	"perf record [<options>] [<command>]",
76986470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
77086470930SIngo Molnar 	NULL
77186470930SIngo Molnar };
77286470930SIngo Molnar 
7737865e817SFrederic Weisbecker static bool force, append_file;
7747865e817SFrederic Weisbecker 
775bca647aaSTom Zanussi const struct option record_options[] = {
776361c99a6SArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &evsel_list, "event",
77786470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
77886470930SIngo Molnar 		     parse_events),
779361c99a6SArnaldo Carvalho de Melo 	OPT_CALLBACK(0, "filter", &evsel_list, "filter",
780c171b552SLi Zefan 		     "event filter", parse_filter),
78186470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
782d6d901c2SZhang, Yanmin 		    "record events on existing process id"),
783d6d901c2SZhang, Yanmin 	OPT_INTEGER('t', "tid", &target_tid,
784d6d901c2SZhang, Yanmin 		    "record events on existing thread id"),
78586470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
78686470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
787acac03faSKirill Smelkov 	OPT_BOOLEAN('D', "no-delay", &nodelay,
788acac03faSKirill Smelkov 		    "collect data without buffering"),
789daac07b2SFrederic Weisbecker 	OPT_BOOLEAN('R', "raw-samples", &raw_samples,
790daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
79186470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
79286470930SIngo Molnar 			    "system-wide collection from all CPUs"),
79386470930SIngo Molnar 	OPT_BOOLEAN('A', "append", &append_file,
79486470930SIngo Molnar 			    "append to the output file to do incremental profiling"),
795c45c6ea2SStephane Eranian 	OPT_STRING('C', "cpu", &cpu_list, "cpu",
796c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
79786470930SIngo Molnar 	OPT_BOOLEAN('f', "force", &force,
7987865e817SFrederic Weisbecker 			"overwrite existing data file (deprecated)"),
7993de29cabSStephane Eranian 	OPT_U64('c', "count", &user_interval, "event period to sample"),
80086470930SIngo Molnar 	OPT_STRING('o', "output", &output_name, "file",
80186470930SIngo Molnar 		    "output file name"),
8022e6cdf99SStephane Eranian 	OPT_BOOLEAN('i', "no-inherit", &no_inherit,
8032e6cdf99SStephane Eranian 		    "child tasks do not inherit counters"),
8041967936dSArnaldo Carvalho de Melo 	OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"),
8051967936dSArnaldo Carvalho de Melo 	OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
8063efa1cc9SIngo Molnar 	OPT_BOOLEAN('g', "call-graph", &call_graph,
8073efa1cc9SIngo Molnar 		    "do call-graph (stack chain/backtrace) recording"),
808c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
8093da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
810b44308f5SArnaldo Carvalho de Melo 	OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
811649c48a9SPeter Zijlstra 	OPT_BOOLEAN('s', "stat", &inherit_stat,
812649c48a9SPeter Zijlstra 		    "per thread counts"),
8134bba828dSAnton Blanchard 	OPT_BOOLEAN('d', "data", &sample_address,
8144bba828dSAnton Blanchard 		    "Sample addresses"),
8159c90a61cSArnaldo Carvalho de Melo 	OPT_BOOLEAN('T', "timestamp", &sample_time, "Sample timestamps"),
816649c48a9SPeter Zijlstra 	OPT_BOOLEAN('n', "no-samples", &no_samples,
817649c48a9SPeter Zijlstra 		    "don't sample"),
818baa2f6ceSArnaldo Carvalho de Melo 	OPT_BOOLEAN('N', "no-buildid-cache", &no_buildid_cache,
819a1ac1d3cSStephane Eranian 		    "do not update the buildid cache"),
820baa2f6ceSArnaldo Carvalho de Melo 	OPT_BOOLEAN('B', "no-buildid", &no_buildid,
821baa2f6ceSArnaldo Carvalho de Melo 		    "do not collect buildids in perf.data"),
82286470930SIngo Molnar 	OPT_END()
82386470930SIngo Molnar };
82486470930SIngo Molnar 
825f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used)
82686470930SIngo Molnar {
82769aad6f1SArnaldo Carvalho de Melo 	int err = -ENOMEM;
82869aad6f1SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
82986470930SIngo Molnar 
830361c99a6SArnaldo Carvalho de Melo 	evsel_list = perf_evlist__new();
831361c99a6SArnaldo Carvalho de Melo 	if (evsel_list == NULL)
832361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
833361c99a6SArnaldo Carvalho de Melo 
834bca647aaSTom Zanussi 	argc = parse_options(argc, argv, record_options, record_usage,
835a0541234SAnton Blanchard 			    PARSE_OPT_STOP_AT_NON_OPTION);
836d6d901c2SZhang, Yanmin 	if (!argc && target_pid == -1 && target_tid == -1 &&
837c45c6ea2SStephane Eranian 		!system_wide && !cpu_list)
838bca647aaSTom Zanussi 		usage_with_options(record_usage, record_options);
83986470930SIngo Molnar 
8407865e817SFrederic Weisbecker 	if (force && append_file) {
8417865e817SFrederic Weisbecker 		fprintf(stderr, "Can't overwrite and append at the same time."
8427865e817SFrederic Weisbecker 				" You need to choose between -f and -A");
843bca647aaSTom Zanussi 		usage_with_options(record_usage, record_options);
8447865e817SFrederic Weisbecker 	} else if (append_file) {
8457865e817SFrederic Weisbecker 		write_mode = WRITE_APPEND;
8467865e817SFrederic Weisbecker 	} else {
8477865e817SFrederic Weisbecker 		write_mode = WRITE_FORCE;
8487865e817SFrederic Weisbecker 	}
8497865e817SFrederic Weisbecker 
850655000e7SArnaldo Carvalho de Melo 	symbol__init();
851baa2f6ceSArnaldo Carvalho de Melo 
852baa2f6ceSArnaldo Carvalho de Melo 	if (no_buildid_cache || no_buildid)
853a1ac1d3cSStephane Eranian 		disable_buildid_cache();
854655000e7SArnaldo Carvalho de Melo 
855361c99a6SArnaldo Carvalho de Melo 	if (evsel_list->nr_entries == 0 &&
856361c99a6SArnaldo Carvalho de Melo 	    perf_evlist__add_default(evsel_list) < 0) {
85769aad6f1SArnaldo Carvalho de Melo 		pr_err("Not enough memory for event selector list\n");
85869aad6f1SArnaldo Carvalho de Melo 		goto out_symbol_exit;
859bbd36e5eSPeter Zijlstra 	}
86086470930SIngo Molnar 
8615c98d466SArnaldo Carvalho de Melo 	if (target_pid != -1)
862d6d901c2SZhang, Yanmin 		target_tid = target_pid;
863d6d901c2SZhang, Yanmin 
8645c98d466SArnaldo Carvalho de Melo 	threads = thread_map__new(target_pid, target_tid);
8655c98d466SArnaldo Carvalho de Melo 	if (threads == NULL) {
8665c98d466SArnaldo Carvalho de Melo 		pr_err("Problems finding threads of monitor\n");
8675c98d466SArnaldo Carvalho de Melo 		usage_with_options(record_usage, record_options);
868d6d901c2SZhang, Yanmin 	}
869d6d901c2SZhang, Yanmin 
870dd7927f4SArnaldo Carvalho de Melo 	if (target_tid != -1)
871dd7927f4SArnaldo Carvalho de Melo 		cpus = cpu_map__dummy_new();
872dd7927f4SArnaldo Carvalho de Melo 	else
87360d567e2SArnaldo Carvalho de Melo 		cpus = cpu_map__new(cpu_list);
874dd7927f4SArnaldo Carvalho de Melo 
875dd7927f4SArnaldo Carvalho de Melo 	if (cpus == NULL)
876dd7927f4SArnaldo Carvalho de Melo 		usage_with_options(record_usage, record_options);
87769aad6f1SArnaldo Carvalho de Melo 
878361c99a6SArnaldo Carvalho de Melo 	list_for_each_entry(pos, &evsel_list->entries, node) {
8795c98d466SArnaldo Carvalho de Melo 		if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
88069aad6f1SArnaldo Carvalho de Melo 			goto out_free_fd;
881ad7f4e3fSArnaldo Carvalho de Melo 		if (perf_header__push_event(pos->attr.config, event_name(pos)))
882ad7f4e3fSArnaldo Carvalho de Melo 			goto out_free_fd;
883d6d901c2SZhang, Yanmin 	}
8845c581041SArnaldo Carvalho de Melo 
8855c581041SArnaldo Carvalho de Melo 	if (perf_evlist__alloc_pollfd(evsel_list, cpus->nr, threads->nr) < 0)
88639d17dacSArnaldo Carvalho de Melo 		goto out_free_fd;
887d6d901c2SZhang, Yanmin 
8883de29cabSStephane Eranian 	if (user_interval != ULLONG_MAX)
889f9212819SFrederic Weisbecker 		default_interval = user_interval;
890f9212819SFrederic Weisbecker 	if (user_freq != UINT_MAX)
891f9212819SFrederic Weisbecker 		freq = user_freq;
892f9212819SFrederic Weisbecker 
8937e4ff9e3SMike Galbraith 	/*
8947e4ff9e3SMike Galbraith 	 * User specified count overrides default frequency.
8957e4ff9e3SMike Galbraith 	 */
8967e4ff9e3SMike Galbraith 	if (default_interval)
8977e4ff9e3SMike Galbraith 		freq = 0;
8987e4ff9e3SMike Galbraith 	else if (freq) {
8997e4ff9e3SMike Galbraith 		default_interval = freq;
9007e4ff9e3SMike Galbraith 	} else {
9017e4ff9e3SMike Galbraith 		fprintf(stderr, "frequency and count are zero, aborting\n");
90239d17dacSArnaldo Carvalho de Melo 		err = -EINVAL;
9035c581041SArnaldo Carvalho de Melo 		goto out_free_fd;
9047e4ff9e3SMike Galbraith 	}
9057e4ff9e3SMike Galbraith 
90639d17dacSArnaldo Carvalho de Melo 	err = __cmd_record(argc, argv);
90739d17dacSArnaldo Carvalho de Melo 
90839d17dacSArnaldo Carvalho de Melo out_free_fd:
9095c98d466SArnaldo Carvalho de Melo 	thread_map__delete(threads);
9105c98d466SArnaldo Carvalho de Melo 	threads = NULL;
911d65a458bSArnaldo Carvalho de Melo out_symbol_exit:
912d65a458bSArnaldo Carvalho de Melo 	symbol__exit();
91339d17dacSArnaldo Carvalho de Melo 	return err;
91486470930SIngo Molnar }
915