xref: /openbmc/linux/tools/perf/builtin-record.c (revision 7c0a6144f9a6a53b1cf2f78f09ca35d59d267f1e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
286470930SIngo Molnar /*
386470930SIngo Molnar  * builtin-record.c
486470930SIngo Molnar  *
586470930SIngo Molnar  * Builtin record command: Record the profile of a workload
686470930SIngo Molnar  * (or a CPU, or a PID) into the perf.data output file - for
786470930SIngo Molnar  * later analysis via perf report.
886470930SIngo Molnar  */
986470930SIngo Molnar #include "builtin.h"
1086470930SIngo Molnar 
116122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h"
124b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
13e3a23261SNamhyung Kim #include <internal/xyarray.h>
1486470930SIngo Molnar #include "util/parse-events.h"
1541840d21STaeung Song #include "util/config.h"
1686470930SIngo Molnar 
178f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h"
18f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h"
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"
24e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
2549c670b1SIan Rogers #include "util/mutex.h"
26aeb00b1aSArnaldo Carvalho de Melo #include "util/target.h"
2794c744b6SArnaldo Carvalho de Melo #include "util/session.h"
2845694aa7SArnaldo Carvalho de Melo #include "util/tool.h"
298d06367fSArnaldo Carvalho de Melo #include "util/symbol.h"
30aeb00b1aSArnaldo Carvalho de Melo #include "util/record.h"
31a12b51c4SPaul Mackerras #include "util/cpumap.h"
32fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h"
33f5fc1412SJiri Olsa #include "util/data.h"
34bcc84ec6SStephane Eranian #include "util/perf_regs.h"
35ef149c25SAdrian Hunter #include "util/auxtrace.h"
3646bc29b9SAdrian Hunter #include "util/tsc.h"
37f00898f4SAndi Kleen #include "util/parse-branch-options.h"
38bcc84ec6SStephane Eranian #include "util/parse-regs-options.h"
3940c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
4071dc2326SWang Nan #include "util/llvm-utils.h"
418690a2a7SWang Nan #include "util/bpf-loader.h"
425f9cf599SWang Nan #include "util/trigger.h"
43a074865eSWang Nan #include "util/perf-hooks.h"
44f13de660SAlexey Budankov #include "util/cpu-set-sched.h"
45ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h"
46c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h"
4758db1d6eSArnaldo Carvalho de Melo #include "util/units.h"
487b612e29SSong Liu #include "util/bpf-event.h"
49d99c22eaSStephane Eranian #include "util/util.h"
5070943490SStephane Eranian #include "util/pfm.h"
516953beb4SJiri Olsa #include "util/clockid.h"
52b53a0755SJin Yao #include "util/pmu-hybrid.h"
53b53a0755SJin Yao #include "util/evlist-hybrid.h"
54edc41a10SNamhyung Kim #include "util/off_cpu.h"
55d8871ea7SWang Nan #include "asm/bug.h"
56c1a604dfSArnaldo Carvalho de Melo #include "perf.h"
57f466e5edSAlexey Bayduraev #include "cputopo.h"
587c6a1c65SPeter Zijlstra 
59a43783aeSArnaldo Carvalho de Melo #include <errno.h>
60fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
6167230479SArnaldo Carvalho de Melo #include <locale.h>
624208735dSArnaldo Carvalho de Melo #include <poll.h>
63d99c22eaSStephane Eranian #include <pthread.h>
6486470930SIngo Molnar #include <unistd.h>
65415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID
66415ccb58SAlexey Bayduraev #include <syscall.h>
67415ccb58SAlexey Bayduraev #endif
6886470930SIngo Molnar #include <sched.h>
699607ad3aSArnaldo Carvalho de Melo #include <signal.h>
70da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
71da231338SAnand K Mistry #include <sys/eventfd.h>
72da231338SAnand K Mistry #endif
73a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h>
744208735dSArnaldo Carvalho de Melo #include <sys/wait.h>
75eeb399b5SAdrian Hunter #include <sys/types.h>
76eeb399b5SAdrian Hunter #include <sys/stat.h>
77eeb399b5SAdrian Hunter #include <fcntl.h>
786ef81c55SMamatha Inamdar #include <linux/err.h>
798520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
800693e680SArnaldo Carvalho de Melo #include <linux/time64.h>
81d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
828384a260SAlexey Budankov #include <linux/bitmap.h>
83d1e325cfSJiri Olsa #include <sys/time.h>
8478da39faSBernhard Rosenkraenzer 
851b43b704SJiri Olsa struct switch_output {
86dc0c6127SJiri Olsa 	bool		 enabled;
871b43b704SJiri Olsa 	bool		 signal;
88dc0c6127SJiri Olsa 	unsigned long	 size;
89bfacbe3bSJiri Olsa 	unsigned long	 time;
90cb4e1ebbSJiri Olsa 	const char	*str;
91cb4e1ebbSJiri Olsa 	bool		 set;
9203724b2eSAndi Kleen 	char		 **filenames;
9303724b2eSAndi Kleen 	int		 num_files;
9403724b2eSAndi Kleen 	int		 cur_file;
951b43b704SJiri Olsa };
961b43b704SJiri Olsa 
977954f716SAlexey Bayduraev struct thread_mask {
987954f716SAlexey Bayduraev 	struct mmap_cpu_mask	maps;
997954f716SAlexey Bayduraev 	struct mmap_cpu_mask	affinity;
1007954f716SAlexey Bayduraev };
1017954f716SAlexey Bayduraev 
102415ccb58SAlexey Bayduraev struct record_thread {
103415ccb58SAlexey Bayduraev 	pid_t			tid;
104415ccb58SAlexey Bayduraev 	struct thread_mask	*mask;
105415ccb58SAlexey Bayduraev 	struct {
106415ccb58SAlexey Bayduraev 		int		msg[2];
107415ccb58SAlexey Bayduraev 		int		ack[2];
108415ccb58SAlexey Bayduraev 	} pipes;
109415ccb58SAlexey Bayduraev 	struct fdarray		pollfd;
110415ccb58SAlexey Bayduraev 	int			ctlfd_pos;
111415ccb58SAlexey Bayduraev 	int			nr_mmaps;
112415ccb58SAlexey Bayduraev 	struct mmap		**maps;
113415ccb58SAlexey Bayduraev 	struct mmap		**overwrite_maps;
114415ccb58SAlexey Bayduraev 	struct record		*rec;
115396b626bSAlexey Bayduraev 	unsigned long long	samples;
116396b626bSAlexey Bayduraev 	unsigned long		waking;
117ae9c7242SAlexey Bayduraev 	u64			bytes_written;
118610fbc01SAlexey Bayduraev 	u64			bytes_transferred;
119610fbc01SAlexey Bayduraev 	u64			bytes_compressed;
120415ccb58SAlexey Bayduraev };
121415ccb58SAlexey Bayduraev 
122396b626bSAlexey Bayduraev static __thread struct record_thread *thread;
123396b626bSAlexey Bayduraev 
1241e5de7d9SAlexey Bayduraev enum thread_msg {
1251e5de7d9SAlexey Bayduraev 	THREAD_MSG__UNDEFINED = 0,
1261e5de7d9SAlexey Bayduraev 	THREAD_MSG__READY,
1271e5de7d9SAlexey Bayduraev 	THREAD_MSG__MAX,
1281e5de7d9SAlexey Bayduraev };
1291e5de7d9SAlexey Bayduraev 
1301e5de7d9SAlexey Bayduraev static const char *thread_msg_tags[THREAD_MSG__MAX] = {
1311e5de7d9SAlexey Bayduraev 	"UNDEFINED", "READY"
1321e5de7d9SAlexey Bayduraev };
1331e5de7d9SAlexey Bayduraev 
13406380a84SAlexey Bayduraev enum thread_spec {
13506380a84SAlexey Bayduraev 	THREAD_SPEC__UNDEFINED = 0,
13606380a84SAlexey Bayduraev 	THREAD_SPEC__CPU,
137f466e5edSAlexey Bayduraev 	THREAD_SPEC__CORE,
138f466e5edSAlexey Bayduraev 	THREAD_SPEC__PACKAGE,
139f466e5edSAlexey Bayduraev 	THREAD_SPEC__NUMA,
140f466e5edSAlexey Bayduraev 	THREAD_SPEC__USER,
141f466e5edSAlexey Bayduraev 	THREAD_SPEC__MAX,
142f466e5edSAlexey Bayduraev };
143f466e5edSAlexey Bayduraev 
144f466e5edSAlexey Bayduraev static const char *thread_spec_tags[THREAD_SPEC__MAX] = {
145f466e5edSAlexey Bayduraev 	"undefined", "cpu", "core", "package", "numa", "user"
14606380a84SAlexey Bayduraev };
14706380a84SAlexey Bayduraev 
1486562c9acSAdrian Hunter struct pollfd_index_map {
1496562c9acSAdrian Hunter 	int evlist_pollfd_index;
1506562c9acSAdrian Hunter 	int thread_pollfd_index;
1516562c9acSAdrian Hunter };
1526562c9acSAdrian Hunter 
1538c6f45a7SArnaldo Carvalho de Melo struct record {
15445694aa7SArnaldo Carvalho de Melo 	struct perf_tool	tool;
155b4006796SArnaldo Carvalho de Melo 	struct record_opts	opts;
156d20deb64SArnaldo Carvalho de Melo 	u64			bytes_written;
1578ceb41d7SJiri Olsa 	struct perf_data	data;
158ef149c25SAdrian Hunter 	struct auxtrace_record	*itr;
15963503dbaSJiri Olsa 	struct evlist	*evlist;
160d20deb64SArnaldo Carvalho de Melo 	struct perf_session	*session;
161bc477d79SArnaldo Carvalho de Melo 	struct evlist		*sb_evlist;
162899e5ffbSArnaldo Carvalho de Melo 	pthread_t		thread_id;
163d20deb64SArnaldo Carvalho de Melo 	int			realtime_prio;
164899e5ffbSArnaldo Carvalho de Melo 	bool			switch_output_event_set;
165d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid;
166d2db9a98SWang Nan 	bool			no_buildid_set;
167d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid_cache;
168d2db9a98SWang Nan 	bool			no_buildid_cache_set;
1696156681bSNamhyung Kim 	bool			buildid_all;
170e29386c8SJiri Olsa 	bool			buildid_mmap;
171ecfd7a9cSWang Nan 	bool			timestamp_filename;
17268588bafSJin Yao 	bool			timestamp_boundary;
173edc41a10SNamhyung Kim 	bool			off_cpu;
1741b43b704SJiri Olsa 	struct switch_output	switch_output;
1759f065194SYang Shi 	unsigned long long	samples;
1766d575816SJiwei Sun 	unsigned long		output_max_size;	/* = 0: unlimited */
1779bce13eaSJiri Olsa 	struct perf_debuginfod	debuginfod;
1787954f716SAlexey Bayduraev 	int			nr_threads;
1797954f716SAlexey Bayduraev 	struct thread_mask	*thread_masks;
180415ccb58SAlexey Bayduraev 	struct record_thread	*thread_data;
1816562c9acSAdrian Hunter 	struct pollfd_index_map	*index_map;
1826562c9acSAdrian Hunter 	size_t			index_map_sz;
1836562c9acSAdrian Hunter 	size_t			index_map_cnt;
1840f82ebc4SArnaldo Carvalho de Melo };
18586470930SIngo Molnar 
1866d575816SJiwei Sun static volatile int done;
1876d575816SJiwei Sun 
188dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started;
189dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
190dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger);
191dc0c6127SJiri Olsa 
1929d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = {
1939d2ed645SAlexey Budankov 	"SYS", "NODE", "CPU"
1949d2ed645SAlexey Budankov };
1959d2ed645SAlexey Budankov 
196415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID
197415ccb58SAlexey Bayduraev static inline pid_t gettid(void)
198415ccb58SAlexey Bayduraev {
199415ccb58SAlexey Bayduraev 	return (pid_t)syscall(__NR_gettid);
200415ccb58SAlexey Bayduraev }
201415ccb58SAlexey Bayduraev #endif
202415ccb58SAlexey Bayduraev 
2033217e9feSAlexey Bayduraev static int record__threads_enabled(struct record *rec)
2043217e9feSAlexey Bayduraev {
2053217e9feSAlexey Bayduraev 	return rec->opts.threads_spec;
2063217e9feSAlexey Bayduraev }
2073217e9feSAlexey Bayduraev 
208dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec)
209dc0c6127SJiri Olsa {
210dc0c6127SJiri Olsa 	return rec->switch_output.signal &&
211dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
212dc0c6127SJiri Olsa }
213dc0c6127SJiri Olsa 
214dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec)
215dc0c6127SJiri Olsa {
216dc0c6127SJiri Olsa 	return rec->switch_output.size &&
217dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger) &&
218dc0c6127SJiri Olsa 	       (rec->bytes_written >= rec->switch_output.size);
219dc0c6127SJiri Olsa }
220dc0c6127SJiri Olsa 
221bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec)
222bfacbe3bSJiri Olsa {
223bfacbe3bSJiri Olsa 	return rec->switch_output.time &&
224bfacbe3bSJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
225bfacbe3bSJiri Olsa }
226bfacbe3bSJiri Olsa 
227ae9c7242SAlexey Bayduraev static u64 record__bytes_written(struct record *rec)
228ae9c7242SAlexey Bayduraev {
229ae9c7242SAlexey Bayduraev 	int t;
230ae9c7242SAlexey Bayduraev 	u64 bytes_written = rec->bytes_written;
231ae9c7242SAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
232ae9c7242SAlexey Bayduraev 
233ae9c7242SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++)
234ae9c7242SAlexey Bayduraev 		bytes_written += thread_data[t].bytes_written;
235ae9c7242SAlexey Bayduraev 
236ae9c7242SAlexey Bayduraev 	return bytes_written;
237ae9c7242SAlexey Bayduraev }
238ae9c7242SAlexey Bayduraev 
2396d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec)
2406d575816SJiwei Sun {
2416d575816SJiwei Sun 	return rec->output_max_size &&
242ae9c7242SAlexey Bayduraev 	       (record__bytes_written(rec) >= rec->output_max_size);
2436d575816SJiwei Sun }
2446d575816SJiwei Sun 
245a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused,
246ded2b8feSJiri Olsa 			 void *bf, size_t size)
247f5970550SPeter Zijlstra {
248ded2b8feSJiri Olsa 	struct perf_data_file *file = &rec->session->data->file;
249ded2b8feSJiri Olsa 
25056f735ffSAlexey Bayduraev 	if (map && map->file)
25156f735ffSAlexey Bayduraev 		file = map->file;
25256f735ffSAlexey Bayduraev 
253ded2b8feSJiri Olsa 	if (perf_data_file__write(file, bf, size) < 0) {
2544f624685SAdrian Hunter 		pr_err("failed to write perf data, error: %m\n");
2558d3eca20SDavid Ahern 		return -1;
2568d3eca20SDavid Ahern 	}
257f5970550SPeter Zijlstra 
258ae9c7242SAlexey Bayduraev 	if (map && map->file)
259ae9c7242SAlexey Bayduraev 		thread->bytes_written += size;
260ae9c7242SAlexey Bayduraev 	else
261cf8b2e69SArnaldo Carvalho de Melo 		rec->bytes_written += size;
262dc0c6127SJiri Olsa 
2636d575816SJiwei Sun 	if (record__output_max_size_exceeded(rec) && !done) {
2646d575816SJiwei Sun 		fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
2656d575816SJiwei Sun 				" stopping session ]\n",
266ae9c7242SAlexey Bayduraev 				record__bytes_written(rec) >> 10);
2676d575816SJiwei Sun 		done = 1;
2686d575816SJiwei Sun 	}
2696d575816SJiwei Sun 
270dc0c6127SJiri Olsa 	if (switch_output_size(rec))
271dc0c6127SJiri Olsa 		trigger_hit(&switch_output_trigger);
272dc0c6127SJiri Olsa 
2738d3eca20SDavid Ahern 	return 0;
274f5970550SPeter Zijlstra }
275f5970550SPeter Zijlstra 
276ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec);
277ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec);
27875f5f1fcSAlexey Bayduraev static size_t zstd_compress(struct perf_session *session, struct mmap *map,
27975f5f1fcSAlexey Bayduraev 			    void *dst, size_t dst_size, void *src, size_t src_size);
2805d7f4116SAlexey Budankov 
281d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
282d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd,
283d3d1af6fSAlexey Budankov 		void *buf, size_t size, off_t off)
284d3d1af6fSAlexey Budankov {
285d3d1af6fSAlexey Budankov 	int rc;
286d3d1af6fSAlexey Budankov 
287d3d1af6fSAlexey Budankov 	cblock->aio_fildes = trace_fd;
288d3d1af6fSAlexey Budankov 	cblock->aio_buf    = buf;
289d3d1af6fSAlexey Budankov 	cblock->aio_nbytes = size;
290d3d1af6fSAlexey Budankov 	cblock->aio_offset = off;
291d3d1af6fSAlexey Budankov 	cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
292d3d1af6fSAlexey Budankov 
293d3d1af6fSAlexey Budankov 	do {
294d3d1af6fSAlexey Budankov 		rc = aio_write(cblock);
295d3d1af6fSAlexey Budankov 		if (rc == 0) {
296d3d1af6fSAlexey Budankov 			break;
297d3d1af6fSAlexey Budankov 		} else if (errno != EAGAIN) {
298d3d1af6fSAlexey Budankov 			cblock->aio_fildes = -1;
299d3d1af6fSAlexey Budankov 			pr_err("failed to queue perf data, error: %m\n");
300d3d1af6fSAlexey Budankov 			break;
301d3d1af6fSAlexey Budankov 		}
302d3d1af6fSAlexey Budankov 	} while (1);
303d3d1af6fSAlexey Budankov 
304d3d1af6fSAlexey Budankov 	return rc;
305d3d1af6fSAlexey Budankov }
306d3d1af6fSAlexey Budankov 
307a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock)
308d3d1af6fSAlexey Budankov {
309d3d1af6fSAlexey Budankov 	void *rem_buf;
310d3d1af6fSAlexey Budankov 	off_t rem_off;
311d3d1af6fSAlexey Budankov 	size_t rem_size;
312d3d1af6fSAlexey Budankov 	int rc, aio_errno;
313d3d1af6fSAlexey Budankov 	ssize_t aio_ret, written;
314d3d1af6fSAlexey Budankov 
315d3d1af6fSAlexey Budankov 	aio_errno = aio_error(cblock);
316d3d1af6fSAlexey Budankov 	if (aio_errno == EINPROGRESS)
317d3d1af6fSAlexey Budankov 		return 0;
318d3d1af6fSAlexey Budankov 
319d3d1af6fSAlexey Budankov 	written = aio_ret = aio_return(cblock);
320d3d1af6fSAlexey Budankov 	if (aio_ret < 0) {
321d3d1af6fSAlexey Budankov 		if (aio_errno != EINTR)
322d3d1af6fSAlexey Budankov 			pr_err("failed to write perf data, error: %m\n");
323d3d1af6fSAlexey Budankov 		written = 0;
324d3d1af6fSAlexey Budankov 	}
325d3d1af6fSAlexey Budankov 
326d3d1af6fSAlexey Budankov 	rem_size = cblock->aio_nbytes - written;
327d3d1af6fSAlexey Budankov 
328d3d1af6fSAlexey Budankov 	if (rem_size == 0) {
329d3d1af6fSAlexey Budankov 		cblock->aio_fildes = -1;
330d3d1af6fSAlexey Budankov 		/*
331ef781128SAlexey Budankov 		 * md->refcount is incremented in record__aio_pushfn() for
332ef781128SAlexey Budankov 		 * every aio write request started in record__aio_push() so
333ef781128SAlexey Budankov 		 * decrement it because the request is now complete.
334d3d1af6fSAlexey Budankov 		 */
33580e53d11SJiri Olsa 		perf_mmap__put(&md->core);
336d3d1af6fSAlexey Budankov 		rc = 1;
337d3d1af6fSAlexey Budankov 	} else {
338d3d1af6fSAlexey Budankov 		/*
339d3d1af6fSAlexey Budankov 		 * aio write request may require restart with the
340d3d1af6fSAlexey Budankov 		 * reminder if the kernel didn't write whole
341d3d1af6fSAlexey Budankov 		 * chunk at once.
342d3d1af6fSAlexey Budankov 		 */
343d3d1af6fSAlexey Budankov 		rem_off = cblock->aio_offset + written;
344d3d1af6fSAlexey Budankov 		rem_buf = (void *)(cblock->aio_buf + written);
345d3d1af6fSAlexey Budankov 		record__aio_write(cblock, cblock->aio_fildes,
346d3d1af6fSAlexey Budankov 				rem_buf, rem_size, rem_off);
347d3d1af6fSAlexey Budankov 		rc = 0;
348d3d1af6fSAlexey Budankov 	}
349d3d1af6fSAlexey Budankov 
350d3d1af6fSAlexey Budankov 	return rc;
351d3d1af6fSAlexey Budankov }
352d3d1af6fSAlexey Budankov 
353a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all)
354d3d1af6fSAlexey Budankov {
35593f20c0fSAlexey Budankov 	struct aiocb **aiocb = md->aio.aiocb;
35693f20c0fSAlexey Budankov 	struct aiocb *cblocks = md->aio.cblocks;
357d3d1af6fSAlexey Budankov 	struct timespec timeout = { 0, 1000 * 1000  * 1 }; /* 1ms */
35893f20c0fSAlexey Budankov 	int i, do_suspend;
359d3d1af6fSAlexey Budankov 
360d3d1af6fSAlexey Budankov 	do {
36193f20c0fSAlexey Budankov 		do_suspend = 0;
36293f20c0fSAlexey Budankov 		for (i = 0; i < md->aio.nr_cblocks; ++i) {
36393f20c0fSAlexey Budankov 			if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
36493f20c0fSAlexey Budankov 				if (sync_all)
36593f20c0fSAlexey Budankov 					aiocb[i] = NULL;
36693f20c0fSAlexey Budankov 				else
36793f20c0fSAlexey Budankov 					return i;
36893f20c0fSAlexey Budankov 			} else {
36993f20c0fSAlexey Budankov 				/*
37093f20c0fSAlexey Budankov 				 * Started aio write is not complete yet
37193f20c0fSAlexey Budankov 				 * so it has to be waited before the
37293f20c0fSAlexey Budankov 				 * next allocation.
37393f20c0fSAlexey Budankov 				 */
37493f20c0fSAlexey Budankov 				aiocb[i] = &cblocks[i];
37593f20c0fSAlexey Budankov 				do_suspend = 1;
37693f20c0fSAlexey Budankov 			}
37793f20c0fSAlexey Budankov 		}
37893f20c0fSAlexey Budankov 		if (!do_suspend)
37993f20c0fSAlexey Budankov 			return -1;
380d3d1af6fSAlexey Budankov 
38193f20c0fSAlexey Budankov 		while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) {
382d3d1af6fSAlexey Budankov 			if (!(errno == EAGAIN || errno == EINTR))
383d3d1af6fSAlexey Budankov 				pr_err("failed to sync perf data, error: %m\n");
384d3d1af6fSAlexey Budankov 		}
385d3d1af6fSAlexey Budankov 	} while (1);
386d3d1af6fSAlexey Budankov }
387d3d1af6fSAlexey Budankov 
388ef781128SAlexey Budankov struct record_aio {
389ef781128SAlexey Budankov 	struct record	*rec;
390ef781128SAlexey Budankov 	void		*data;
391ef781128SAlexey Budankov 	size_t		size;
392ef781128SAlexey Budankov };
393ef781128SAlexey Budankov 
394a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size)
395d3d1af6fSAlexey Budankov {
396ef781128SAlexey Budankov 	struct record_aio *aio = to;
397ef781128SAlexey Budankov 
398ef781128SAlexey Budankov 	/*
399547740f7SJiri Olsa 	 * map->core.base data pointed by buf is copied into free map->aio.data[] buffer
400ef781128SAlexey Budankov 	 * to release space in the kernel buffer as fast as possible, calling
401ef781128SAlexey Budankov 	 * perf_mmap__consume() from perf_mmap__push() function.
402ef781128SAlexey Budankov 	 *
403ef781128SAlexey Budankov 	 * That lets the kernel to proceed with storing more profiling data into
404ef781128SAlexey Budankov 	 * the kernel buffer earlier than other per-cpu kernel buffers are handled.
405ef781128SAlexey Budankov 	 *
406ef781128SAlexey Budankov 	 * Coping can be done in two steps in case the chunk of profiling data
407ef781128SAlexey Budankov 	 * crosses the upper bound of the kernel buffer. In this case we first move
408ef781128SAlexey Budankov 	 * part of data from map->start till the upper bound and then the reminder
409ef781128SAlexey Budankov 	 * from the beginning of the kernel buffer till the end of the data chunk.
410ef781128SAlexey Budankov 	 */
411ef781128SAlexey Budankov 
412ef781128SAlexey Budankov 	if (record__comp_enabled(aio->rec)) {
41375f5f1fcSAlexey Bayduraev 		size = zstd_compress(aio->rec->session, NULL, aio->data + aio->size,
414bf59b305SJiri Olsa 				     mmap__mmap_len(map) - aio->size,
415ef781128SAlexey Budankov 				     buf, size);
416ef781128SAlexey Budankov 	} else {
417ef781128SAlexey Budankov 		memcpy(aio->data + aio->size, buf, size);
418ef781128SAlexey Budankov 	}
419ef781128SAlexey Budankov 
420ef781128SAlexey Budankov 	if (!aio->size) {
421ef781128SAlexey Budankov 		/*
422ef781128SAlexey Budankov 		 * Increment map->refcount to guard map->aio.data[] buffer
423ef781128SAlexey Budankov 		 * from premature deallocation because map object can be
424ef781128SAlexey Budankov 		 * released earlier than aio write request started on
425ef781128SAlexey Budankov 		 * map->aio.data[] buffer is complete.
426ef781128SAlexey Budankov 		 *
427ef781128SAlexey Budankov 		 * perf_mmap__put() is done at record__aio_complete()
428ef781128SAlexey Budankov 		 * after started aio request completion or at record__aio_push()
429ef781128SAlexey Budankov 		 * if the request failed to start.
430ef781128SAlexey Budankov 		 */
431e75710f0SJiri Olsa 		perf_mmap__get(&map->core);
432ef781128SAlexey Budankov 	}
433ef781128SAlexey Budankov 
434ef781128SAlexey Budankov 	aio->size += size;
435ef781128SAlexey Budankov 
436ef781128SAlexey Budankov 	return size;
437ef781128SAlexey Budankov }
438ef781128SAlexey Budankov 
439a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off)
440ef781128SAlexey Budankov {
441ef781128SAlexey Budankov 	int ret, idx;
442ef781128SAlexey Budankov 	int trace_fd = rec->session->data->file.fd;
443ef781128SAlexey Budankov 	struct record_aio aio = { .rec = rec, .size = 0 };
444ef781128SAlexey Budankov 
445ef781128SAlexey Budankov 	/*
446ef781128SAlexey Budankov 	 * Call record__aio_sync() to wait till map->aio.data[] buffer
447ef781128SAlexey Budankov 	 * becomes available after previous aio write operation.
448ef781128SAlexey Budankov 	 */
449ef781128SAlexey Budankov 
450ef781128SAlexey Budankov 	idx = record__aio_sync(map, false);
451ef781128SAlexey Budankov 	aio.data = map->aio.data[idx];
452ef781128SAlexey Budankov 	ret = perf_mmap__push(map, &aio, record__aio_pushfn);
453ef781128SAlexey Budankov 	if (ret != 0) /* ret > 0 - no data, ret < 0 - error */
454ef781128SAlexey Budankov 		return ret;
455d3d1af6fSAlexey Budankov 
456d3d1af6fSAlexey Budankov 	rec->samples++;
457ef781128SAlexey Budankov 	ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off);
458d3d1af6fSAlexey Budankov 	if (!ret) {
459ef781128SAlexey Budankov 		*off += aio.size;
460ef781128SAlexey Budankov 		rec->bytes_written += aio.size;
461d3d1af6fSAlexey Budankov 		if (switch_output_size(rec))
462d3d1af6fSAlexey Budankov 			trigger_hit(&switch_output_trigger);
463ef781128SAlexey Budankov 	} else {
464ef781128SAlexey Budankov 		/*
465ef781128SAlexey Budankov 		 * Decrement map->refcount incremented in record__aio_pushfn()
466ef781128SAlexey Budankov 		 * back if record__aio_write() operation failed to start, otherwise
467ef781128SAlexey Budankov 		 * map->refcount is decremented in record__aio_complete() after
468ef781128SAlexey Budankov 		 * aio write operation finishes successfully.
469ef781128SAlexey Budankov 		 */
47080e53d11SJiri Olsa 		perf_mmap__put(&map->core);
471d3d1af6fSAlexey Budankov 	}
472d3d1af6fSAlexey Budankov 
473d3d1af6fSAlexey Budankov 	return ret;
474d3d1af6fSAlexey Budankov }
475d3d1af6fSAlexey Budankov 
476d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd)
477d3d1af6fSAlexey Budankov {
478d3d1af6fSAlexey Budankov 	return lseek(trace_fd, 0, SEEK_CUR);
479d3d1af6fSAlexey Budankov }
480d3d1af6fSAlexey Budankov 
481d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos)
482d3d1af6fSAlexey Budankov {
483d3d1af6fSAlexey Budankov 	lseek(trace_fd, pos, SEEK_SET);
484d3d1af6fSAlexey Budankov }
485d3d1af6fSAlexey Budankov 
486d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec)
487d3d1af6fSAlexey Budankov {
488d3d1af6fSAlexey Budankov 	int i;
48963503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
490a5830532SJiri Olsa 	struct mmap *maps = evlist->mmap;
491d3d1af6fSAlexey Budankov 
492ef781128SAlexey Budankov 	if (!record__aio_enabled(rec))
493d3d1af6fSAlexey Budankov 		return;
494d3d1af6fSAlexey Budankov 
495c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
496a5830532SJiri Olsa 		struct mmap *map = &maps[i];
497d3d1af6fSAlexey Budankov 
498547740f7SJiri Olsa 		if (map->core.base)
49993f20c0fSAlexey Budankov 			record__aio_sync(map, true);
500d3d1af6fSAlexey Budankov 	}
501d3d1af6fSAlexey Budankov }
502d3d1af6fSAlexey Budankov 
503d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1;
50493f20c0fSAlexey Budankov static int nr_cblocks_max = 4;
505d3d1af6fSAlexey Budankov 
506d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt,
50793f20c0fSAlexey Budankov 			     const char *str,
508d3d1af6fSAlexey Budankov 			     int unset)
509d3d1af6fSAlexey Budankov {
510d3d1af6fSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
511d3d1af6fSAlexey Budankov 
51293f20c0fSAlexey Budankov 	if (unset) {
513d3d1af6fSAlexey Budankov 		opts->nr_cblocks = 0;
51493f20c0fSAlexey Budankov 	} else {
51593f20c0fSAlexey Budankov 		if (str)
51693f20c0fSAlexey Budankov 			opts->nr_cblocks = strtol(str, NULL, 0);
51793f20c0fSAlexey Budankov 		if (!opts->nr_cblocks)
518d3d1af6fSAlexey Budankov 			opts->nr_cblocks = nr_cblocks_default;
51993f20c0fSAlexey Budankov 	}
520d3d1af6fSAlexey Budankov 
521d3d1af6fSAlexey Budankov 	return 0;
522d3d1af6fSAlexey Budankov }
523d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */
52493f20c0fSAlexey Budankov static int nr_cblocks_max = 0;
52593f20c0fSAlexey Budankov 
526a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused,
527ef781128SAlexey Budankov 			    off_t *off __maybe_unused)
528d3d1af6fSAlexey Budankov {
529d3d1af6fSAlexey Budankov 	return -1;
530d3d1af6fSAlexey Budankov }
531d3d1af6fSAlexey Budankov 
532d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused)
533d3d1af6fSAlexey Budankov {
534d3d1af6fSAlexey Budankov 	return -1;
535d3d1af6fSAlexey Budankov }
536d3d1af6fSAlexey Budankov 
537d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused)
538d3d1af6fSAlexey Budankov {
539d3d1af6fSAlexey Budankov }
540d3d1af6fSAlexey Budankov 
541d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused)
542d3d1af6fSAlexey Budankov {
543d3d1af6fSAlexey Budankov }
544d3d1af6fSAlexey Budankov #endif
545d3d1af6fSAlexey Budankov 
546d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec)
547d3d1af6fSAlexey Budankov {
548d3d1af6fSAlexey Budankov 	return rec->opts.nr_cblocks > 0;
549d3d1af6fSAlexey Budankov }
550d3d1af6fSAlexey Budankov 
551470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1
552470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt,
553470530bbSAlexey Budankov 				    const char *str,
554470530bbSAlexey Budankov 				    int unset)
555470530bbSAlexey Budankov {
556470530bbSAlexey Budankov 	int flush_max;
557470530bbSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
558470530bbSAlexey Budankov 	static struct parse_tag tags[] = {
559470530bbSAlexey Budankov 			{ .tag  = 'B', .mult = 1       },
560470530bbSAlexey Budankov 			{ .tag  = 'K', .mult = 1 << 10 },
561470530bbSAlexey Budankov 			{ .tag  = 'M', .mult = 1 << 20 },
562470530bbSAlexey Budankov 			{ .tag  = 'G', .mult = 1 << 30 },
563470530bbSAlexey Budankov 			{ .tag  = 0 },
564470530bbSAlexey Budankov 	};
565470530bbSAlexey Budankov 
566470530bbSAlexey Budankov 	if (unset)
567470530bbSAlexey Budankov 		return 0;
568470530bbSAlexey Budankov 
569470530bbSAlexey Budankov 	if (str) {
570470530bbSAlexey Budankov 		opts->mmap_flush = parse_tag_value(str, tags);
571470530bbSAlexey Budankov 		if (opts->mmap_flush == (int)-1)
572470530bbSAlexey Budankov 			opts->mmap_flush = strtol(str, NULL, 0);
573470530bbSAlexey Budankov 	}
574470530bbSAlexey Budankov 
575470530bbSAlexey Budankov 	if (!opts->mmap_flush)
576470530bbSAlexey Budankov 		opts->mmap_flush = MMAP_FLUSH_DEFAULT;
577470530bbSAlexey Budankov 
5789521b5f2SJiri Olsa 	flush_max = evlist__mmap_size(opts->mmap_pages);
579470530bbSAlexey Budankov 	flush_max /= 4;
580470530bbSAlexey Budankov 	if (opts->mmap_flush > flush_max)
581470530bbSAlexey Budankov 		opts->mmap_flush = flush_max;
582470530bbSAlexey Budankov 
583470530bbSAlexey Budankov 	return 0;
584470530bbSAlexey Budankov }
585470530bbSAlexey Budankov 
586504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
587504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1;
588504c1ad1SAlexey Budankov 
589504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset)
590504c1ad1SAlexey Budankov {
591504c1ad1SAlexey Budankov 	struct record_opts *opts = opt->value;
592504c1ad1SAlexey Budankov 
593504c1ad1SAlexey Budankov 	if (unset) {
594504c1ad1SAlexey Budankov 		opts->comp_level = 0;
595504c1ad1SAlexey Budankov 	} else {
596504c1ad1SAlexey Budankov 		if (str)
597504c1ad1SAlexey Budankov 			opts->comp_level = strtol(str, NULL, 0);
598504c1ad1SAlexey Budankov 		if (!opts->comp_level)
599504c1ad1SAlexey Budankov 			opts->comp_level = comp_level_default;
600504c1ad1SAlexey Budankov 	}
601504c1ad1SAlexey Budankov 
602504c1ad1SAlexey Budankov 	return 0;
603504c1ad1SAlexey Budankov }
604504c1ad1SAlexey Budankov #endif
60551255a8aSAlexey Budankov static unsigned int comp_level_max = 22;
60651255a8aSAlexey Budankov 
60742e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec)
60842e1fd80SAlexey Budankov {
60942e1fd80SAlexey Budankov 	return rec->opts.comp_level > 0;
61042e1fd80SAlexey Budankov }
61142e1fd80SAlexey Budankov 
61245694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool,
613d20deb64SArnaldo Carvalho de Melo 				     union perf_event *event,
6141d037ca1SIrina Tirdea 				     struct perf_sample *sample __maybe_unused,
6151d037ca1SIrina Tirdea 				     struct machine *machine __maybe_unused)
616234fbbf5SArnaldo Carvalho de Melo {
6178c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = container_of(tool, struct record, tool);
618ded2b8feSJiri Olsa 	return record__write(rec, NULL, event, event->header.size);
619234fbbf5SArnaldo Carvalho de Melo }
620234fbbf5SArnaldo Carvalho de Melo 
62149c670b1SIan Rogers static struct mutex synth_lock;
62249c670b1SIan Rogers 
623d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool,
624d99c22eaSStephane Eranian 				     union perf_event *event,
625d99c22eaSStephane Eranian 				     struct perf_sample *sample __maybe_unused,
626d99c22eaSStephane Eranian 				     struct machine *machine __maybe_unused)
627d99c22eaSStephane Eranian {
628d99c22eaSStephane Eranian 	int ret;
629d99c22eaSStephane Eranian 
63049c670b1SIan Rogers 	mutex_lock(&synth_lock);
631d99c22eaSStephane Eranian 	ret = process_synthesized_event(tool, event, sample, machine);
63249c670b1SIan Rogers 	mutex_unlock(&synth_lock);
633d99c22eaSStephane Eranian 	return ret;
634d99c22eaSStephane Eranian }
635d99c22eaSStephane Eranian 
636a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
637d37f1586SArnaldo Carvalho de Melo {
638d37f1586SArnaldo Carvalho de Melo 	struct record *rec = to;
639d37f1586SArnaldo Carvalho de Melo 
6405d7f4116SAlexey Budankov 	if (record__comp_enabled(rec)) {
64175f5f1fcSAlexey Bayduraev 		size = zstd_compress(rec->session, map, map->data, mmap__mmap_len(map), bf, size);
6425d7f4116SAlexey Budankov 		bf   = map->data;
6435d7f4116SAlexey Budankov 	}
6445d7f4116SAlexey Budankov 
645396b626bSAlexey Bayduraev 	thread->samples++;
646ded2b8feSJiri Olsa 	return record__write(rec, map, bf, size);
647d37f1586SArnaldo Carvalho de Melo }
648d37f1586SArnaldo Carvalho de Melo 
6498ed28c2bSIan Rogers static volatile sig_atomic_t signr = -1;
6508ed28c2bSIan Rogers static volatile sig_atomic_t child_finished;
651da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
6528ed28c2bSIan Rogers static volatile sig_atomic_t done_fd = -1;
653da231338SAnand K Mistry #endif
654c0bdc1c4SWang Nan 
6552dd6d8a1SAdrian Hunter static void sig_handler(int sig)
6562dd6d8a1SAdrian Hunter {
6572dd6d8a1SAdrian Hunter 	if (sig == SIGCHLD)
6582dd6d8a1SAdrian Hunter 		child_finished = 1;
6592dd6d8a1SAdrian Hunter 	else
6602dd6d8a1SAdrian Hunter 		signr = sig;
6612dd6d8a1SAdrian Hunter 
6622dd6d8a1SAdrian Hunter 	done = 1;
663da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
664304f0a2fSIan Rogers 	if (done_fd >= 0) {
665da231338SAnand K Mistry 		u64 tmp = 1;
666304f0a2fSIan Rogers 		int orig_errno = errno;
667304f0a2fSIan Rogers 
668da231338SAnand K Mistry 		/*
669304f0a2fSIan Rogers 		 * It is possible for this signal handler to run after done is
670304f0a2fSIan Rogers 		 * checked in the main loop, but before the perf counter fds are
671304f0a2fSIan Rogers 		 * polled. If this happens, the poll() will continue to wait
672304f0a2fSIan Rogers 		 * even though done is set, and will only break out if either
673304f0a2fSIan Rogers 		 * another signal is received, or the counters are ready for
674304f0a2fSIan Rogers 		 * read. To ensure the poll() doesn't sleep when done is set,
675304f0a2fSIan Rogers 		 * use an eventfd (done_fd) to wake up the poll().
676da231338SAnand K Mistry 		 */
677da231338SAnand K Mistry 		if (write(done_fd, &tmp, sizeof(tmp)) < 0)
678da231338SAnand K Mistry 			pr_err("failed to signal wakeup fd, error: %m\n");
679304f0a2fSIan Rogers 
680304f0a2fSIan Rogers 		errno = orig_errno;
681da231338SAnand K Mistry 	}
682da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT
6832dd6d8a1SAdrian Hunter }
6842dd6d8a1SAdrian Hunter 
685a074865eSWang Nan static void sigsegv_handler(int sig)
686a074865eSWang Nan {
687a074865eSWang Nan 	perf_hooks__recover();
688a074865eSWang Nan 	sighandler_dump_stack(sig);
689a074865eSWang Nan }
690a074865eSWang Nan 
6912dd6d8a1SAdrian Hunter static void record__sig_exit(void)
6922dd6d8a1SAdrian Hunter {
6932dd6d8a1SAdrian Hunter 	if (signr == -1)
6942dd6d8a1SAdrian Hunter 		return;
6952dd6d8a1SAdrian Hunter 
6962dd6d8a1SAdrian Hunter 	signal(signr, SIG_DFL);
6972dd6d8a1SAdrian Hunter 	raise(signr);
6982dd6d8a1SAdrian Hunter }
6992dd6d8a1SAdrian Hunter 
700e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT
701e31f0d01SAdrian Hunter 
702ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool,
703a5830532SJiri Olsa 				    struct mmap *map,
704ef149c25SAdrian Hunter 				    union perf_event *event, void *data1,
705ef149c25SAdrian Hunter 				    size_t len1, void *data2, size_t len2)
706ef149c25SAdrian Hunter {
707ef149c25SAdrian Hunter 	struct record *rec = container_of(tool, struct record, tool);
7088ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
709ef149c25SAdrian Hunter 	size_t padding;
710ef149c25SAdrian Hunter 	u8 pad[8] = {0};
711ef149c25SAdrian Hunter 
71246e201efSAdrian Hunter 	if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) {
71399fa2984SAdrian Hunter 		off_t file_offset;
7148ceb41d7SJiri Olsa 		int fd = perf_data__fd(data);
71599fa2984SAdrian Hunter 		int err;
71699fa2984SAdrian Hunter 
71799fa2984SAdrian Hunter 		file_offset = lseek(fd, 0, SEEK_CUR);
71899fa2984SAdrian Hunter 		if (file_offset == -1)
71999fa2984SAdrian Hunter 			return -1;
72099fa2984SAdrian Hunter 		err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
72199fa2984SAdrian Hunter 						     event, file_offset);
72299fa2984SAdrian Hunter 		if (err)
72399fa2984SAdrian Hunter 			return err;
72499fa2984SAdrian Hunter 	}
72599fa2984SAdrian Hunter 
726ef149c25SAdrian Hunter 	/* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
727ef149c25SAdrian Hunter 	padding = (len1 + len2) & 7;
728ef149c25SAdrian Hunter 	if (padding)
729ef149c25SAdrian Hunter 		padding = 8 - padding;
730ef149c25SAdrian Hunter 
731ded2b8feSJiri Olsa 	record__write(rec, map, event, event->header.size);
732ded2b8feSJiri Olsa 	record__write(rec, map, data1, len1);
733ef149c25SAdrian Hunter 	if (len2)
734ded2b8feSJiri Olsa 		record__write(rec, map, data2, len2);
735ded2b8feSJiri Olsa 	record__write(rec, map, &pad, padding);
736ef149c25SAdrian Hunter 
737ef149c25SAdrian Hunter 	return 0;
738ef149c25SAdrian Hunter }
739ef149c25SAdrian Hunter 
740ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec,
741a5830532SJiri Olsa 				      struct mmap *map)
742ef149c25SAdrian Hunter {
743ef149c25SAdrian Hunter 	int ret;
744ef149c25SAdrian Hunter 
745e035f4caSJiri Olsa 	ret = auxtrace_mmap__read(map, rec->itr, &rec->tool,
746ef149c25SAdrian Hunter 				  record__process_auxtrace);
747ef149c25SAdrian Hunter 	if (ret < 0)
748ef149c25SAdrian Hunter 		return ret;
749ef149c25SAdrian Hunter 
750ef149c25SAdrian Hunter 	if (ret)
751ef149c25SAdrian Hunter 		rec->samples++;
752ef149c25SAdrian Hunter 
753ef149c25SAdrian Hunter 	return 0;
754ef149c25SAdrian Hunter }
755ef149c25SAdrian Hunter 
7562dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec,
757a5830532SJiri Olsa 					       struct mmap *map)
7582dd6d8a1SAdrian Hunter {
7592dd6d8a1SAdrian Hunter 	int ret;
7602dd6d8a1SAdrian Hunter 
761e035f4caSJiri Olsa 	ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool,
7622dd6d8a1SAdrian Hunter 					   record__process_auxtrace,
7632dd6d8a1SAdrian Hunter 					   rec->opts.auxtrace_snapshot_size);
7642dd6d8a1SAdrian Hunter 	if (ret < 0)
7652dd6d8a1SAdrian Hunter 		return ret;
7662dd6d8a1SAdrian Hunter 
7672dd6d8a1SAdrian Hunter 	if (ret)
7682dd6d8a1SAdrian Hunter 		rec->samples++;
7692dd6d8a1SAdrian Hunter 
7702dd6d8a1SAdrian Hunter 	return 0;
7712dd6d8a1SAdrian Hunter }
7722dd6d8a1SAdrian Hunter 
7732dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec)
7742dd6d8a1SAdrian Hunter {
7752dd6d8a1SAdrian Hunter 	int i;
7762dd6d8a1SAdrian Hunter 	int rc = 0;
7772dd6d8a1SAdrian Hunter 
778c976ee11SJiri Olsa 	for (i = 0; i < rec->evlist->core.nr_mmaps; i++) {
779a5830532SJiri Olsa 		struct mmap *map = &rec->evlist->mmap[i];
7802dd6d8a1SAdrian Hunter 
781e035f4caSJiri Olsa 		if (!map->auxtrace_mmap.base)
7822dd6d8a1SAdrian Hunter 			continue;
7832dd6d8a1SAdrian Hunter 
784e035f4caSJiri Olsa 		if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) {
7852dd6d8a1SAdrian Hunter 			rc = -1;
7862dd6d8a1SAdrian Hunter 			goto out;
7872dd6d8a1SAdrian Hunter 		}
7882dd6d8a1SAdrian Hunter 	}
7892dd6d8a1SAdrian Hunter out:
7902dd6d8a1SAdrian Hunter 	return rc;
7912dd6d8a1SAdrian Hunter }
7922dd6d8a1SAdrian Hunter 
793ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit)
7942dd6d8a1SAdrian Hunter {
7952dd6d8a1SAdrian Hunter 	pr_debug("Recording AUX area tracing snapshot\n");
7962dd6d8a1SAdrian Hunter 	if (record__auxtrace_read_snapshot_all(rec) < 0) {
7975f9cf599SWang Nan 		trigger_error(&auxtrace_snapshot_trigger);
7982dd6d8a1SAdrian Hunter 	} else {
799ce7b0e42SAlexander Shishkin 		if (auxtrace_record__snapshot_finish(rec->itr, on_exit))
8005f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
8015f9cf599SWang Nan 		else
8025f9cf599SWang Nan 			trigger_ready(&auxtrace_snapshot_trigger);
8032dd6d8a1SAdrian Hunter 	}
8042dd6d8a1SAdrian Hunter }
8052dd6d8a1SAdrian Hunter 
806ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec)
807ce7b0e42SAlexander Shishkin {
808ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
809ce7b0e42SAlexander Shishkin 		return 0;
810ce7b0e42SAlexander Shishkin 
811ce7b0e42SAlexander Shishkin 	if (!auxtrace_record__snapshot_started &&
812ce7b0e42SAlexander Shishkin 	    auxtrace_record__snapshot_start(rec->itr))
813ce7b0e42SAlexander Shishkin 		return -1;
814ce7b0e42SAlexander Shishkin 
815ce7b0e42SAlexander Shishkin 	record__read_auxtrace_snapshot(rec, true);
816ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
817ce7b0e42SAlexander Shishkin 		return -1;
818ce7b0e42SAlexander Shishkin 
819ce7b0e42SAlexander Shishkin 	return 0;
820ce7b0e42SAlexander Shishkin }
821ce7b0e42SAlexander Shishkin 
8224b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec)
8234b5ea3bdSAdrian Hunter {
8244b5ea3bdSAdrian Hunter 	int err;
8254b5ea3bdSAdrian Hunter 
826b5f2511dSAlexey Bayduraev 	if ((rec->opts.auxtrace_snapshot_opts || rec->opts.auxtrace_sample_opts)
827b5f2511dSAlexey Bayduraev 	    && record__threads_enabled(rec)) {
828b5f2511dSAlexey Bayduraev 		pr_err("AUX area tracing options are not available in parallel streaming mode.\n");
829b5f2511dSAlexey Bayduraev 		return -EINVAL;
830b5f2511dSAlexey Bayduraev 	}
831b5f2511dSAlexey Bayduraev 
8324b5ea3bdSAdrian Hunter 	if (!rec->itr) {
8334b5ea3bdSAdrian Hunter 		rec->itr = auxtrace_record__init(rec->evlist, &err);
8344b5ea3bdSAdrian Hunter 		if (err)
8354b5ea3bdSAdrian Hunter 			return err;
8364b5ea3bdSAdrian Hunter 	}
8374b5ea3bdSAdrian Hunter 
8384b5ea3bdSAdrian Hunter 	err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
8394b5ea3bdSAdrian Hunter 					      rec->opts.auxtrace_snapshot_opts);
8404b5ea3bdSAdrian Hunter 	if (err)
8414b5ea3bdSAdrian Hunter 		return err;
8424b5ea3bdSAdrian Hunter 
843c0a6de06SAdrian Hunter 	err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts,
844c0a6de06SAdrian Hunter 					    rec->opts.auxtrace_sample_opts);
845c0a6de06SAdrian Hunter 	if (err)
846c0a6de06SAdrian Hunter 		return err;
847c0a6de06SAdrian Hunter 
848d58b3f7eSAdrian Hunter 	auxtrace_regroup_aux_output(rec->evlist);
849d58b3f7eSAdrian Hunter 
8504b5ea3bdSAdrian Hunter 	return auxtrace_parse_filters(rec->evlist);
8514b5ea3bdSAdrian Hunter }
8524b5ea3bdSAdrian Hunter 
853e31f0d01SAdrian Hunter #else
854e31f0d01SAdrian Hunter 
855e31f0d01SAdrian Hunter static inline
856e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
857a5830532SJiri Olsa 			       struct mmap *map __maybe_unused)
858e31f0d01SAdrian Hunter {
859e31f0d01SAdrian Hunter 	return 0;
860e31f0d01SAdrian Hunter }
861e31f0d01SAdrian Hunter 
8622dd6d8a1SAdrian Hunter static inline
863ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused,
864ce7b0e42SAlexander Shishkin 				    bool on_exit __maybe_unused)
8652dd6d8a1SAdrian Hunter {
8662dd6d8a1SAdrian Hunter }
8672dd6d8a1SAdrian Hunter 
8682dd6d8a1SAdrian Hunter static inline
8692dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
8702dd6d8a1SAdrian Hunter {
8712dd6d8a1SAdrian Hunter 	return 0;
8722dd6d8a1SAdrian Hunter }
8732dd6d8a1SAdrian Hunter 
874ce7b0e42SAlexander Shishkin static inline
875ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused)
876ce7b0e42SAlexander Shishkin {
877ce7b0e42SAlexander Shishkin 	return 0;
878ce7b0e42SAlexander Shishkin }
879ce7b0e42SAlexander Shishkin 
8804b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused)
8814b5ea3bdSAdrian Hunter {
8824b5ea3bdSAdrian Hunter 	return 0;
8834b5ea3bdSAdrian Hunter }
8844b5ea3bdSAdrian Hunter 
885e31f0d01SAdrian Hunter #endif
886e31f0d01SAdrian Hunter 
887246eba8eSAdrian Hunter static int record__config_text_poke(struct evlist *evlist)
888246eba8eSAdrian Hunter {
889246eba8eSAdrian Hunter 	struct evsel *evsel;
890246eba8eSAdrian Hunter 
891246eba8eSAdrian Hunter 	/* Nothing to do if text poke is already configured */
892246eba8eSAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
893246eba8eSAdrian Hunter 		if (evsel->core.attr.text_poke)
894246eba8eSAdrian Hunter 			return 0;
895246eba8eSAdrian Hunter 	}
896246eba8eSAdrian Hunter 
897921e3be5SAdrian Hunter 	evsel = evlist__add_dummy_on_all_cpus(evlist);
898921e3be5SAdrian Hunter 	if (!evsel)
899921e3be5SAdrian Hunter 		return -ENOMEM;
900246eba8eSAdrian Hunter 
901246eba8eSAdrian Hunter 	evsel->core.attr.text_poke = 1;
902246eba8eSAdrian Hunter 	evsel->core.attr.ksymbol = 1;
903246eba8eSAdrian Hunter 	evsel->immediate = true;
904246eba8eSAdrian Hunter 	evsel__set_sample_bit(evsel, TIME);
905246eba8eSAdrian Hunter 
906246eba8eSAdrian Hunter 	return 0;
907246eba8eSAdrian Hunter }
908246eba8eSAdrian Hunter 
909edc41a10SNamhyung Kim static int record__config_off_cpu(struct record *rec)
910edc41a10SNamhyung Kim {
911685439a7SNamhyung Kim 	return off_cpu_prepare(rec->evlist, &rec->opts.target, &rec->opts);
912edc41a10SNamhyung Kim }
913edc41a10SNamhyung Kim 
914eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine)
915eeb399b5SAdrian Hunter {
916eeb399b5SAdrian Hunter 	char kcore[PATH_MAX];
917eeb399b5SAdrian Hunter 	int fd;
918eeb399b5SAdrian Hunter 
919eeb399b5SAdrian Hunter 	scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir);
920eeb399b5SAdrian Hunter 
921eeb399b5SAdrian Hunter 	fd = open(kcore, O_RDONLY);
922eeb399b5SAdrian Hunter 	if (fd < 0)
923eeb399b5SAdrian Hunter 		return false;
924eeb399b5SAdrian Hunter 
925eeb399b5SAdrian Hunter 	close(fd);
926eeb399b5SAdrian Hunter 
927eeb399b5SAdrian Hunter 	return true;
928eeb399b5SAdrian Hunter }
929eeb399b5SAdrian Hunter 
930eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data)
931eeb399b5SAdrian Hunter {
932eeb399b5SAdrian Hunter 	char from_dir[PATH_MAX];
933eeb399b5SAdrian Hunter 	char kcore_dir[PATH_MAX];
934eeb399b5SAdrian Hunter 	int ret;
935eeb399b5SAdrian Hunter 
936eeb399b5SAdrian Hunter 	snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir);
937eeb399b5SAdrian Hunter 
938eeb399b5SAdrian Hunter 	ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir));
939eeb399b5SAdrian Hunter 	if (ret)
940eeb399b5SAdrian Hunter 		return ret;
941eeb399b5SAdrian Hunter 
942eeb399b5SAdrian Hunter 	return kcore_copy(from_dir, kcore_dir);
943eeb399b5SAdrian Hunter }
944eeb399b5SAdrian Hunter 
945415ccb58SAlexey Bayduraev static void record__thread_data_init_pipes(struct record_thread *thread_data)
946415ccb58SAlexey Bayduraev {
947415ccb58SAlexey Bayduraev 	thread_data->pipes.msg[0] = -1;
948415ccb58SAlexey Bayduraev 	thread_data->pipes.msg[1] = -1;
949415ccb58SAlexey Bayduraev 	thread_data->pipes.ack[0] = -1;
950415ccb58SAlexey Bayduraev 	thread_data->pipes.ack[1] = -1;
951415ccb58SAlexey Bayduraev }
952415ccb58SAlexey Bayduraev 
953415ccb58SAlexey Bayduraev static int record__thread_data_open_pipes(struct record_thread *thread_data)
954415ccb58SAlexey Bayduraev {
955415ccb58SAlexey Bayduraev 	if (pipe(thread_data->pipes.msg))
956415ccb58SAlexey Bayduraev 		return -EINVAL;
957415ccb58SAlexey Bayduraev 
958415ccb58SAlexey Bayduraev 	if (pipe(thread_data->pipes.ack)) {
959415ccb58SAlexey Bayduraev 		close(thread_data->pipes.msg[0]);
960415ccb58SAlexey Bayduraev 		thread_data->pipes.msg[0] = -1;
961415ccb58SAlexey Bayduraev 		close(thread_data->pipes.msg[1]);
962415ccb58SAlexey Bayduraev 		thread_data->pipes.msg[1] = -1;
963415ccb58SAlexey Bayduraev 		return -EINVAL;
964415ccb58SAlexey Bayduraev 	}
965415ccb58SAlexey Bayduraev 
966415ccb58SAlexey Bayduraev 	pr_debug2("thread_data[%p]: msg=[%d,%d], ack=[%d,%d]\n", thread_data,
967415ccb58SAlexey Bayduraev 		 thread_data->pipes.msg[0], thread_data->pipes.msg[1],
968415ccb58SAlexey Bayduraev 		 thread_data->pipes.ack[0], thread_data->pipes.ack[1]);
969415ccb58SAlexey Bayduraev 
970415ccb58SAlexey Bayduraev 	return 0;
971415ccb58SAlexey Bayduraev }
972415ccb58SAlexey Bayduraev 
973415ccb58SAlexey Bayduraev static void record__thread_data_close_pipes(struct record_thread *thread_data)
974415ccb58SAlexey Bayduraev {
975415ccb58SAlexey Bayduraev 	if (thread_data->pipes.msg[0] != -1) {
976415ccb58SAlexey Bayduraev 		close(thread_data->pipes.msg[0]);
977415ccb58SAlexey Bayduraev 		thread_data->pipes.msg[0] = -1;
978415ccb58SAlexey Bayduraev 	}
979415ccb58SAlexey Bayduraev 	if (thread_data->pipes.msg[1] != -1) {
980415ccb58SAlexey Bayduraev 		close(thread_data->pipes.msg[1]);
981415ccb58SAlexey Bayduraev 		thread_data->pipes.msg[1] = -1;
982415ccb58SAlexey Bayduraev 	}
983415ccb58SAlexey Bayduraev 	if (thread_data->pipes.ack[0] != -1) {
984415ccb58SAlexey Bayduraev 		close(thread_data->pipes.ack[0]);
985415ccb58SAlexey Bayduraev 		thread_data->pipes.ack[0] = -1;
986415ccb58SAlexey Bayduraev 	}
987415ccb58SAlexey Bayduraev 	if (thread_data->pipes.ack[1] != -1) {
988415ccb58SAlexey Bayduraev 		close(thread_data->pipes.ack[1]);
989415ccb58SAlexey Bayduraev 		thread_data->pipes.ack[1] = -1;
990415ccb58SAlexey Bayduraev 	}
991415ccb58SAlexey Bayduraev }
992415ccb58SAlexey Bayduraev 
9937be1feddSAdrian Hunter static bool evlist__per_thread(struct evlist *evlist)
9947be1feddSAdrian Hunter {
9957be1feddSAdrian Hunter 	return cpu_map__is_dummy(evlist->core.user_requested_cpus);
9967be1feddSAdrian Hunter }
9977be1feddSAdrian Hunter 
998415ccb58SAlexey Bayduraev static int record__thread_data_init_maps(struct record_thread *thread_data, struct evlist *evlist)
999415ccb58SAlexey Bayduraev {
1000415ccb58SAlexey Bayduraev 	int m, tm, nr_mmaps = evlist->core.nr_mmaps;
1001415ccb58SAlexey Bayduraev 	struct mmap *mmap = evlist->mmap;
1002415ccb58SAlexey Bayduraev 	struct mmap *overwrite_mmap = evlist->overwrite_mmap;
10037be1feddSAdrian Hunter 	struct perf_cpu_map *cpus = evlist->core.all_cpus;
10047be1feddSAdrian Hunter 	bool per_thread = evlist__per_thread(evlist);
1005415ccb58SAlexey Bayduraev 
10067be1feddSAdrian Hunter 	if (per_thread)
100723380e4dSAlexey Bayduraev 		thread_data->nr_mmaps = nr_mmaps;
100823380e4dSAlexey Bayduraev 	else
1009415ccb58SAlexey Bayduraev 		thread_data->nr_mmaps = bitmap_weight(thread_data->mask->maps.bits,
1010415ccb58SAlexey Bayduraev 						      thread_data->mask->maps.nbits);
1011415ccb58SAlexey Bayduraev 	if (mmap) {
1012415ccb58SAlexey Bayduraev 		thread_data->maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
1013415ccb58SAlexey Bayduraev 		if (!thread_data->maps)
1014415ccb58SAlexey Bayduraev 			return -ENOMEM;
1015415ccb58SAlexey Bayduraev 	}
1016415ccb58SAlexey Bayduraev 	if (overwrite_mmap) {
1017415ccb58SAlexey Bayduraev 		thread_data->overwrite_maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
1018415ccb58SAlexey Bayduraev 		if (!thread_data->overwrite_maps) {
1019415ccb58SAlexey Bayduraev 			zfree(&thread_data->maps);
1020415ccb58SAlexey Bayduraev 			return -ENOMEM;
1021415ccb58SAlexey Bayduraev 		}
1022415ccb58SAlexey Bayduraev 	}
1023415ccb58SAlexey Bayduraev 	pr_debug2("thread_data[%p]: nr_mmaps=%d, maps=%p, ow_maps=%p\n", thread_data,
1024415ccb58SAlexey Bayduraev 		 thread_data->nr_mmaps, thread_data->maps, thread_data->overwrite_maps);
1025415ccb58SAlexey Bayduraev 
1026415ccb58SAlexey Bayduraev 	for (m = 0, tm = 0; m < nr_mmaps && tm < thread_data->nr_mmaps; m++) {
10277be1feddSAdrian Hunter 		if (per_thread ||
10280255571aSIan Rogers 		    test_bit(perf_cpu_map__cpu(cpus, m).cpu, thread_data->mask->maps.bits)) {
1029415ccb58SAlexey Bayduraev 			if (thread_data->maps) {
1030415ccb58SAlexey Bayduraev 				thread_data->maps[tm] = &mmap[m];
1031415ccb58SAlexey Bayduraev 				pr_debug2("thread_data[%p]: cpu%d: maps[%d] -> mmap[%d]\n",
103223380e4dSAlexey Bayduraev 					  thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m);
1033415ccb58SAlexey Bayduraev 			}
1034415ccb58SAlexey Bayduraev 			if (thread_data->overwrite_maps) {
1035415ccb58SAlexey Bayduraev 				thread_data->overwrite_maps[tm] = &overwrite_mmap[m];
1036415ccb58SAlexey Bayduraev 				pr_debug2("thread_data[%p]: cpu%d: ow_maps[%d] -> ow_mmap[%d]\n",
103723380e4dSAlexey Bayduraev 					  thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m);
1038415ccb58SAlexey Bayduraev 			}
1039415ccb58SAlexey Bayduraev 			tm++;
1040415ccb58SAlexey Bayduraev 		}
1041415ccb58SAlexey Bayduraev 	}
1042415ccb58SAlexey Bayduraev 
1043415ccb58SAlexey Bayduraev 	return 0;
1044415ccb58SAlexey Bayduraev }
1045415ccb58SAlexey Bayduraev 
1046415ccb58SAlexey Bayduraev static int record__thread_data_init_pollfd(struct record_thread *thread_data, struct evlist *evlist)
1047415ccb58SAlexey Bayduraev {
1048415ccb58SAlexey Bayduraev 	int f, tm, pos;
1049415ccb58SAlexey Bayduraev 	struct mmap *map, *overwrite_map;
1050415ccb58SAlexey Bayduraev 
1051415ccb58SAlexey Bayduraev 	fdarray__init(&thread_data->pollfd, 64);
1052415ccb58SAlexey Bayduraev 
1053415ccb58SAlexey Bayduraev 	for (tm = 0; tm < thread_data->nr_mmaps; tm++) {
1054415ccb58SAlexey Bayduraev 		map = thread_data->maps ? thread_data->maps[tm] : NULL;
1055415ccb58SAlexey Bayduraev 		overwrite_map = thread_data->overwrite_maps ?
1056415ccb58SAlexey Bayduraev 				thread_data->overwrite_maps[tm] : NULL;
1057415ccb58SAlexey Bayduraev 
1058415ccb58SAlexey Bayduraev 		for (f = 0; f < evlist->core.pollfd.nr; f++) {
1059415ccb58SAlexey Bayduraev 			void *ptr = evlist->core.pollfd.priv[f].ptr;
1060415ccb58SAlexey Bayduraev 
1061415ccb58SAlexey Bayduraev 			if ((map && ptr == map) || (overwrite_map && ptr == overwrite_map)) {
1062415ccb58SAlexey Bayduraev 				pos = fdarray__dup_entry_from(&thread_data->pollfd, f,
1063415ccb58SAlexey Bayduraev 							      &evlist->core.pollfd);
1064415ccb58SAlexey Bayduraev 				if (pos < 0)
1065415ccb58SAlexey Bayduraev 					return pos;
1066415ccb58SAlexey Bayduraev 				pr_debug2("thread_data[%p]: pollfd[%d] <- event_fd=%d\n",
1067415ccb58SAlexey Bayduraev 					 thread_data, pos, evlist->core.pollfd.entries[f].fd);
1068415ccb58SAlexey Bayduraev 			}
1069415ccb58SAlexey Bayduraev 		}
1070415ccb58SAlexey Bayduraev 	}
1071415ccb58SAlexey Bayduraev 
1072415ccb58SAlexey Bayduraev 	return 0;
1073415ccb58SAlexey Bayduraev }
1074415ccb58SAlexey Bayduraev 
1075415ccb58SAlexey Bayduraev static void record__free_thread_data(struct record *rec)
1076415ccb58SAlexey Bayduraev {
1077415ccb58SAlexey Bayduraev 	int t;
1078415ccb58SAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
1079415ccb58SAlexey Bayduraev 
1080415ccb58SAlexey Bayduraev 	if (thread_data == NULL)
1081415ccb58SAlexey Bayduraev 		return;
1082415ccb58SAlexey Bayduraev 
1083415ccb58SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++) {
1084415ccb58SAlexey Bayduraev 		record__thread_data_close_pipes(&thread_data[t]);
1085415ccb58SAlexey Bayduraev 		zfree(&thread_data[t].maps);
1086415ccb58SAlexey Bayduraev 		zfree(&thread_data[t].overwrite_maps);
1087415ccb58SAlexey Bayduraev 		fdarray__exit(&thread_data[t].pollfd);
1088415ccb58SAlexey Bayduraev 	}
1089415ccb58SAlexey Bayduraev 
1090415ccb58SAlexey Bayduraev 	zfree(&rec->thread_data);
1091415ccb58SAlexey Bayduraev }
1092415ccb58SAlexey Bayduraev 
10936562c9acSAdrian Hunter static int record__map_thread_evlist_pollfd_indexes(struct record *rec,
10946562c9acSAdrian Hunter 						    int evlist_pollfd_index,
10956562c9acSAdrian Hunter 						    int thread_pollfd_index)
10966562c9acSAdrian Hunter {
10976562c9acSAdrian Hunter 	size_t x = rec->index_map_cnt;
10986562c9acSAdrian Hunter 
10996562c9acSAdrian Hunter 	if (realloc_array_as_needed(rec->index_map, rec->index_map_sz, x, NULL))
11006562c9acSAdrian Hunter 		return -ENOMEM;
11016562c9acSAdrian Hunter 	rec->index_map[x].evlist_pollfd_index = evlist_pollfd_index;
11026562c9acSAdrian Hunter 	rec->index_map[x].thread_pollfd_index = thread_pollfd_index;
11036562c9acSAdrian Hunter 	rec->index_map_cnt += 1;
11046562c9acSAdrian Hunter 	return 0;
11056562c9acSAdrian Hunter }
11066562c9acSAdrian Hunter 
11076562c9acSAdrian Hunter static int record__update_evlist_pollfd_from_thread(struct record *rec,
11086562c9acSAdrian Hunter 						    struct evlist *evlist,
11096562c9acSAdrian Hunter 						    struct record_thread *thread_data)
11106562c9acSAdrian Hunter {
11116562c9acSAdrian Hunter 	struct pollfd *e_entries = evlist->core.pollfd.entries;
11126562c9acSAdrian Hunter 	struct pollfd *t_entries = thread_data->pollfd.entries;
11136562c9acSAdrian Hunter 	int err = 0;
11146562c9acSAdrian Hunter 	size_t i;
11156562c9acSAdrian Hunter 
11166562c9acSAdrian Hunter 	for (i = 0; i < rec->index_map_cnt; i++) {
11176562c9acSAdrian Hunter 		int e_pos = rec->index_map[i].evlist_pollfd_index;
11186562c9acSAdrian Hunter 		int t_pos = rec->index_map[i].thread_pollfd_index;
11196562c9acSAdrian Hunter 
11206562c9acSAdrian Hunter 		if (e_entries[e_pos].fd != t_entries[t_pos].fd ||
11216562c9acSAdrian Hunter 		    e_entries[e_pos].events != t_entries[t_pos].events) {
11226562c9acSAdrian Hunter 			pr_err("Thread and evlist pollfd index mismatch\n");
11236562c9acSAdrian Hunter 			err = -EINVAL;
11246562c9acSAdrian Hunter 			continue;
11256562c9acSAdrian Hunter 		}
11266562c9acSAdrian Hunter 		e_entries[e_pos].revents = t_entries[t_pos].revents;
11276562c9acSAdrian Hunter 	}
11286562c9acSAdrian Hunter 	return err;
11296562c9acSAdrian Hunter }
11306562c9acSAdrian Hunter 
11316562c9acSAdrian Hunter static int record__dup_non_perf_events(struct record *rec,
11326562c9acSAdrian Hunter 				       struct evlist *evlist,
11336562c9acSAdrian Hunter 				       struct record_thread *thread_data)
11346562c9acSAdrian Hunter {
11356562c9acSAdrian Hunter 	struct fdarray *fda = &evlist->core.pollfd;
11366562c9acSAdrian Hunter 	int i, ret;
11376562c9acSAdrian Hunter 
11386562c9acSAdrian Hunter 	for (i = 0; i < fda->nr; i++) {
11396562c9acSAdrian Hunter 		if (!(fda->priv[i].flags & fdarray_flag__non_perf_event))
11406562c9acSAdrian Hunter 			continue;
11416562c9acSAdrian Hunter 		ret = fdarray__dup_entry_from(&thread_data->pollfd, i, fda);
11426562c9acSAdrian Hunter 		if (ret < 0) {
11436562c9acSAdrian Hunter 			pr_err("Failed to duplicate descriptor in main thread pollfd\n");
11446562c9acSAdrian Hunter 			return ret;
11456562c9acSAdrian Hunter 		}
11466562c9acSAdrian Hunter 		pr_debug2("thread_data[%p]: pollfd[%d] <- non_perf_event fd=%d\n",
11476562c9acSAdrian Hunter 			  thread_data, ret, fda->entries[i].fd);
11486562c9acSAdrian Hunter 		ret = record__map_thread_evlist_pollfd_indexes(rec, i, ret);
11496562c9acSAdrian Hunter 		if (ret < 0) {
11506562c9acSAdrian Hunter 			pr_err("Failed to map thread and evlist pollfd indexes\n");
11516562c9acSAdrian Hunter 			return ret;
11526562c9acSAdrian Hunter 		}
11536562c9acSAdrian Hunter 	}
11546562c9acSAdrian Hunter 	return 0;
11556562c9acSAdrian Hunter }
11566562c9acSAdrian Hunter 
1157415ccb58SAlexey Bayduraev static int record__alloc_thread_data(struct record *rec, struct evlist *evlist)
1158415ccb58SAlexey Bayduraev {
1159415ccb58SAlexey Bayduraev 	int t, ret;
1160415ccb58SAlexey Bayduraev 	struct record_thread *thread_data;
1161415ccb58SAlexey Bayduraev 
1162415ccb58SAlexey Bayduraev 	rec->thread_data = zalloc(rec->nr_threads * sizeof(*(rec->thread_data)));
1163415ccb58SAlexey Bayduraev 	if (!rec->thread_data) {
1164415ccb58SAlexey Bayduraev 		pr_err("Failed to allocate thread data\n");
1165415ccb58SAlexey Bayduraev 		return -ENOMEM;
1166415ccb58SAlexey Bayduraev 	}
1167415ccb58SAlexey Bayduraev 	thread_data = rec->thread_data;
1168415ccb58SAlexey Bayduraev 
1169415ccb58SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++)
1170415ccb58SAlexey Bayduraev 		record__thread_data_init_pipes(&thread_data[t]);
1171415ccb58SAlexey Bayduraev 
1172415ccb58SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++) {
1173415ccb58SAlexey Bayduraev 		thread_data[t].rec = rec;
1174415ccb58SAlexey Bayduraev 		thread_data[t].mask = &rec->thread_masks[t];
1175415ccb58SAlexey Bayduraev 		ret = record__thread_data_init_maps(&thread_data[t], evlist);
1176415ccb58SAlexey Bayduraev 		if (ret) {
1177415ccb58SAlexey Bayduraev 			pr_err("Failed to initialize thread[%d] maps\n", t);
1178415ccb58SAlexey Bayduraev 			goto out_free;
1179415ccb58SAlexey Bayduraev 		}
1180415ccb58SAlexey Bayduraev 		ret = record__thread_data_init_pollfd(&thread_data[t], evlist);
1181415ccb58SAlexey Bayduraev 		if (ret) {
1182415ccb58SAlexey Bayduraev 			pr_err("Failed to initialize thread[%d] pollfd\n", t);
1183415ccb58SAlexey Bayduraev 			goto out_free;
1184415ccb58SAlexey Bayduraev 		}
1185415ccb58SAlexey Bayduraev 		if (t) {
1186415ccb58SAlexey Bayduraev 			thread_data[t].tid = -1;
1187415ccb58SAlexey Bayduraev 			ret = record__thread_data_open_pipes(&thread_data[t]);
1188415ccb58SAlexey Bayduraev 			if (ret) {
1189415ccb58SAlexey Bayduraev 				pr_err("Failed to open thread[%d] communication pipes\n", t);
1190415ccb58SAlexey Bayduraev 				goto out_free;
1191415ccb58SAlexey Bayduraev 			}
1192415ccb58SAlexey Bayduraev 			ret = fdarray__add(&thread_data[t].pollfd, thread_data[t].pipes.msg[0],
1193415ccb58SAlexey Bayduraev 					   POLLIN | POLLERR | POLLHUP, fdarray_flag__nonfilterable);
1194415ccb58SAlexey Bayduraev 			if (ret < 0) {
1195415ccb58SAlexey Bayduraev 				pr_err("Failed to add descriptor to thread[%d] pollfd\n", t);
1196415ccb58SAlexey Bayduraev 				goto out_free;
1197415ccb58SAlexey Bayduraev 			}
1198415ccb58SAlexey Bayduraev 			thread_data[t].ctlfd_pos = ret;
1199415ccb58SAlexey Bayduraev 			pr_debug2("thread_data[%p]: pollfd[%d] <- ctl_fd=%d\n",
1200415ccb58SAlexey Bayduraev 				 thread_data, thread_data[t].ctlfd_pos,
1201415ccb58SAlexey Bayduraev 				 thread_data[t].pipes.msg[0]);
1202415ccb58SAlexey Bayduraev 		} else {
1203415ccb58SAlexey Bayduraev 			thread_data[t].tid = gettid();
12046562c9acSAdrian Hunter 
12056562c9acSAdrian Hunter 			ret = record__dup_non_perf_events(rec, evlist, &thread_data[t]);
12066562c9acSAdrian Hunter 			if (ret < 0)
12076562c9acSAdrian Hunter 				goto out_free;
12086562c9acSAdrian Hunter 
1209feff0b61SAdrian Hunter 			thread_data[t].ctlfd_pos = -1; /* Not used */
1210415ccb58SAlexey Bayduraev 		}
1211415ccb58SAlexey Bayduraev 	}
1212415ccb58SAlexey Bayduraev 
1213415ccb58SAlexey Bayduraev 	return 0;
1214415ccb58SAlexey Bayduraev 
1215415ccb58SAlexey Bayduraev out_free:
1216415ccb58SAlexey Bayduraev 	record__free_thread_data(rec);
1217415ccb58SAlexey Bayduraev 
1218415ccb58SAlexey Bayduraev 	return ret;
1219415ccb58SAlexey Bayduraev }
1220415ccb58SAlexey Bayduraev 
1221cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec,
122263503dbaSJiri Olsa 			       struct evlist *evlist)
1223cda57a8cSWang Nan {
122456f735ffSAlexey Bayduraev 	int i, ret;
1225cda57a8cSWang Nan 	struct record_opts *opts = &rec->opts;
1226c0a6de06SAdrian Hunter 	bool auxtrace_overwrite = opts->auxtrace_snapshot_mode ||
1227c0a6de06SAdrian Hunter 				  opts->auxtrace_sample_mode;
1228cda57a8cSWang Nan 	char msg[512];
1229cda57a8cSWang Nan 
1230f13de660SAlexey Budankov 	if (opts->affinity != PERF_AFFINITY_SYS)
1231f13de660SAlexey Budankov 		cpu__setup_cpunode_map();
1232f13de660SAlexey Budankov 
12339521b5f2SJiri Olsa 	if (evlist__mmap_ex(evlist, opts->mmap_pages,
1234cda57a8cSWang Nan 				 opts->auxtrace_mmap_pages,
1235c0a6de06SAdrian Hunter 				 auxtrace_overwrite,
1236470530bbSAlexey Budankov 				 opts->nr_cblocks, opts->affinity,
123751255a8aSAlexey Budankov 				 opts->mmap_flush, opts->comp_level) < 0) {
1238cda57a8cSWang Nan 		if (errno == EPERM) {
1239cda57a8cSWang Nan 			pr_err("Permission error mapping pages.\n"
1240cda57a8cSWang Nan 			       "Consider increasing "
1241cda57a8cSWang Nan 			       "/proc/sys/kernel/perf_event_mlock_kb,\n"
1242cda57a8cSWang Nan 			       "or try again with a smaller value of -m/--mmap_pages.\n"
1243cda57a8cSWang Nan 			       "(current value: %u,%u)\n",
1244cda57a8cSWang Nan 			       opts->mmap_pages, opts->auxtrace_mmap_pages);
1245cda57a8cSWang Nan 			return -errno;
1246cda57a8cSWang Nan 		} else {
1247cda57a8cSWang Nan 			pr_err("failed to mmap with %d (%s)\n", errno,
1248c8b5f2c9SArnaldo Carvalho de Melo 				str_error_r(errno, msg, sizeof(msg)));
1249cda57a8cSWang Nan 			if (errno)
1250cda57a8cSWang Nan 				return -errno;
1251cda57a8cSWang Nan 			else
1252cda57a8cSWang Nan 				return -EINVAL;
1253cda57a8cSWang Nan 		}
1254cda57a8cSWang Nan 	}
1255415ccb58SAlexey Bayduraev 
1256415ccb58SAlexey Bayduraev 	if (evlist__initialize_ctlfd(evlist, opts->ctl_fd, opts->ctl_fd_ack))
1257415ccb58SAlexey Bayduraev 		return -1;
1258415ccb58SAlexey Bayduraev 
1259415ccb58SAlexey Bayduraev 	ret = record__alloc_thread_data(rec, evlist);
1260415ccb58SAlexey Bayduraev 	if (ret)
1261415ccb58SAlexey Bayduraev 		return ret;
1262415ccb58SAlexey Bayduraev 
126356f735ffSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
126456f735ffSAlexey Bayduraev 		ret = perf_data__create_dir(&rec->data, evlist->core.nr_mmaps);
126565e7c963SAlexey Bayduraev 		if (ret) {
126665e7c963SAlexey Bayduraev 			pr_err("Failed to create data directory: %s\n", strerror(-ret));
126756f735ffSAlexey Bayduraev 			return ret;
126865e7c963SAlexey Bayduraev 		}
126956f735ffSAlexey Bayduraev 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
127056f735ffSAlexey Bayduraev 			if (evlist->mmap)
127156f735ffSAlexey Bayduraev 				evlist->mmap[i].file = &rec->data.dir.files[i];
127256f735ffSAlexey Bayduraev 			if (evlist->overwrite_mmap)
127356f735ffSAlexey Bayduraev 				evlist->overwrite_mmap[i].file = &rec->data.dir.files[i];
127456f735ffSAlexey Bayduraev 		}
127556f735ffSAlexey Bayduraev 	}
127656f735ffSAlexey Bayduraev 
1277cda57a8cSWang Nan 	return 0;
1278cda57a8cSWang Nan }
1279cda57a8cSWang Nan 
1280cda57a8cSWang Nan static int record__mmap(struct record *rec)
1281cda57a8cSWang Nan {
1282cda57a8cSWang Nan 	return record__mmap_evlist(rec, rec->evlist);
1283cda57a8cSWang Nan }
1284cda57a8cSWang Nan 
12858c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec)
1286dd7927f4SArnaldo Carvalho de Melo {
1287d6195a6aSArnaldo Carvalho de Melo 	char msg[BUFSIZ];
128832dcd021SJiri Olsa 	struct evsel *pos;
128963503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
1290d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session = rec->session;
1291b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
12928d3eca20SDavid Ahern 	int rc = 0;
1293dd7927f4SArnaldo Carvalho de Melo 
1294d3dbf43cSArnaldo Carvalho de Melo 	/*
1295b91e5492SKan Liang 	 * For initial_delay, system wide or a hybrid system, we need to add a
1296b91e5492SKan Liang 	 * dummy event so that we can track PERF_RECORD_MMAP to cover the delay
1297b91e5492SKan Liang 	 * of waiting or event synthesis.
1298d3dbf43cSArnaldo Carvalho de Melo 	 */
1299b91e5492SKan Liang 	if (opts->initial_delay || target__has_cpu(&opts->target) ||
1300b91e5492SKan Liang 	    perf_pmu__has_hybrid()) {
1301e80db255SArnaldo Carvalho de Melo 		pos = evlist__get_tracking_event(evlist);
1302442ad225SAdrian Hunter 		if (!evsel__is_dummy_event(pos)) {
1303442ad225SAdrian Hunter 			/* Set up dummy event. */
1304e251abeeSArnaldo Carvalho de Melo 			if (evlist__add_dummy(evlist))
1305d3dbf43cSArnaldo Carvalho de Melo 				return -ENOMEM;
1306515dbe48SJiri Olsa 			pos = evlist__last(evlist);
1307e80db255SArnaldo Carvalho de Melo 			evlist__set_tracking_event(evlist, pos);
1308442ad225SAdrian Hunter 		}
1309442ad225SAdrian Hunter 
13100a892c1cSIan Rogers 		/*
13110a892c1cSIan Rogers 		 * Enable the dummy event when the process is forked for
13120a892c1cSIan Rogers 		 * initial_delay, immediately for system wide.
13130a892c1cSIan Rogers 		 */
1314bb07d62eSNamhyung Kim 		if (opts->initial_delay && !pos->immediate &&
1315bb07d62eSNamhyung Kim 		    !target__has_cpu(&opts->target))
13161fc632ceSJiri Olsa 			pos->core.attr.enable_on_exec = 1;
13170a892c1cSIan Rogers 		else
13180a892c1cSIan Rogers 			pos->immediate = 1;
1319d3dbf43cSArnaldo Carvalho de Melo 	}
1320d3dbf43cSArnaldo Carvalho de Melo 
132178e1bc25SArnaldo Carvalho de Melo 	evlist__config(evlist, opts, &callchain_param);
1322cac21425SJiri Olsa 
1323e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
13243da297a6SIngo Molnar try_again:
1325af663bd0SJiri Olsa 		if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
1326ae430892SArnaldo Carvalho de Melo 			if (evsel__fallback(pos, errno, msg, sizeof(msg))) {
1327bb963e16SNamhyung Kim 				if (verbose > 0)
1328c0a54341SArnaldo Carvalho de Melo 					ui__warning("%s\n", msg);
13293da297a6SIngo Molnar 				goto try_again;
13303da297a6SIngo Molnar 			}
1331cf99ad14SAndi Kleen 			if ((errno == EINVAL || errno == EBADF) &&
1332fba7c866SJiri Olsa 			    pos->core.leader != &pos->core &&
1333cf99ad14SAndi Kleen 			    pos->weak_group) {
133464b4778bSArnaldo Carvalho de Melo 			        pos = evlist__reset_weak_group(evlist, pos, true);
1335cf99ad14SAndi Kleen 				goto try_again;
1336cf99ad14SAndi Kleen 			}
133756e52e85SArnaldo Carvalho de Melo 			rc = -errno;
13382bb72dbbSArnaldo Carvalho de Melo 			evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg));
133956e52e85SArnaldo Carvalho de Melo 			ui__error("%s\n", msg);
13408d3eca20SDavid Ahern 			goto out;
13417c6a1c65SPeter Zijlstra 		}
1342bfd8f72cSAndi Kleen 
1343bfd8f72cSAndi Kleen 		pos->supported = true;
13447c6a1c65SPeter Zijlstra 	}
13457c6a1c65SPeter Zijlstra 
134678e1bc25SArnaldo Carvalho de Melo 	if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) {
1347c8b567c8SArnaldo Carvalho de Melo 		pr_warning(
1348c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
1349c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
1350c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
1351c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n"
1352c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n"
1353c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
1354c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n");
1355c8b567c8SArnaldo Carvalho de Melo 	}
1356c8b567c8SArnaldo Carvalho de Melo 
135724bf91a7SArnaldo Carvalho de Melo 	if (evlist__apply_filters(evlist, &pos)) {
135862d94b00SArnaldo Carvalho de Melo 		pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
13598ab2e96dSArnaldo Carvalho de Melo 			pos->filter, evsel__name(pos), errno,
1360c8b5f2c9SArnaldo Carvalho de Melo 			str_error_r(errno, msg, sizeof(msg)));
13618d3eca20SDavid Ahern 		rc = -1;
13628d3eca20SDavid Ahern 		goto out;
13630a102479SFrederic Weisbecker 	}
13640a102479SFrederic Weisbecker 
1365cda57a8cSWang Nan 	rc = record__mmap(rec);
1366cda57a8cSWang Nan 	if (rc)
13678d3eca20SDavid Ahern 		goto out;
13680a27d7f9SArnaldo Carvalho de Melo 
1369a91e5431SArnaldo Carvalho de Melo 	session->evlist = evlist;
13707b56cce2SArnaldo Carvalho de Melo 	perf_session__set_id_hdr_size(session);
13718d3eca20SDavid Ahern out:
13728d3eca20SDavid Ahern 	return rc;
1373a91e5431SArnaldo Carvalho de Melo }
1374a91e5431SArnaldo Carvalho de Melo 
137566286ed3SAdrian Hunter static void set_timestamp_boundary(struct record *rec, u64 sample_time)
137666286ed3SAdrian Hunter {
137766286ed3SAdrian Hunter 	if (rec->evlist->first_sample_time == 0)
137866286ed3SAdrian Hunter 		rec->evlist->first_sample_time = sample_time;
137966286ed3SAdrian Hunter 
138066286ed3SAdrian Hunter 	if (sample_time)
138166286ed3SAdrian Hunter 		rec->evlist->last_sample_time = sample_time;
138266286ed3SAdrian Hunter }
138366286ed3SAdrian Hunter 
1384e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool,
1385e3d59112SNamhyung Kim 				union perf_event *event,
1386e3d59112SNamhyung Kim 				struct perf_sample *sample,
138732dcd021SJiri Olsa 				struct evsel *evsel,
1388e3d59112SNamhyung Kim 				struct machine *machine)
1389e3d59112SNamhyung Kim {
1390e3d59112SNamhyung Kim 	struct record *rec = container_of(tool, struct record, tool);
1391e3d59112SNamhyung Kim 
139266286ed3SAdrian Hunter 	set_timestamp_boundary(rec, sample->time);
139368588bafSJin Yao 
139468588bafSJin Yao 	if (rec->buildid_all)
139568588bafSJin Yao 		return 0;
139668588bafSJin Yao 
139768588bafSJin Yao 	rec->samples++;
1398e3d59112SNamhyung Kim 	return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
1399e3d59112SNamhyung Kim }
1400e3d59112SNamhyung Kim 
14018c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec)
14026122e4e4SArnaldo Carvalho de Melo {
1403f5fc1412SJiri Olsa 	struct perf_session *session = rec->session;
14046122e4e4SArnaldo Carvalho de Melo 
140545112e89SJiri Olsa 	if (perf_data__size(&rec->data) == 0)
14069f591fd7SArnaldo Carvalho de Melo 		return 0;
14079f591fd7SArnaldo Carvalho de Melo 
140800dc8657SNamhyung Kim 	/*
140900dc8657SNamhyung Kim 	 * During this process, it'll load kernel map and replace the
141000dc8657SNamhyung Kim 	 * dso->long_name to a real pathname it found.  In this case
141100dc8657SNamhyung Kim 	 * we prefer the vmlinux path like
141200dc8657SNamhyung Kim 	 *   /lib/modules/3.16.4/build/vmlinux
141300dc8657SNamhyung Kim 	 *
141400dc8657SNamhyung Kim 	 * rather than build-id path (in debug directory).
141500dc8657SNamhyung Kim 	 *   $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
141600dc8657SNamhyung Kim 	 */
141700dc8657SNamhyung Kim 	symbol_conf.ignore_vmlinux_buildid = true;
141800dc8657SNamhyung Kim 
14196156681bSNamhyung Kim 	/*
14206156681bSNamhyung Kim 	 * If --buildid-all is given, it marks all DSO regardless of hits,
142168588bafSJin Yao 	 * so no need to process samples. But if timestamp_boundary is enabled,
142268588bafSJin Yao 	 * it still needs to walk on all samples to get the timestamps of
142368588bafSJin Yao 	 * first/last samples.
14246156681bSNamhyung Kim 	 */
142568588bafSJin Yao 	if (rec->buildid_all && !rec->timestamp_boundary)
14266156681bSNamhyung Kim 		rec->tool.sample = NULL;
14276156681bSNamhyung Kim 
1428b7b61cbeSArnaldo Carvalho de Melo 	return perf_session__process_events(session);
14296122e4e4SArnaldo Carvalho de Melo }
14306122e4e4SArnaldo Carvalho de Melo 
14318115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
1432a1645ce1SZhang, Yanmin {
1433a1645ce1SZhang, Yanmin 	int err;
143445694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = data;
1435a1645ce1SZhang, Yanmin 	/*
1436a1645ce1SZhang, Yanmin 	 *As for guest kernel when processing subcommand record&report,
1437a1645ce1SZhang, Yanmin 	 *we arrange module mmap prior to guest kernel mmap and trigger
1438a1645ce1SZhang, Yanmin 	 *a preload dso because default guest module symbols are loaded
1439a1645ce1SZhang, Yanmin 	 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
1440a1645ce1SZhang, Yanmin 	 *method is used to avoid symbol missing when the first addr is
1441a1645ce1SZhang, Yanmin 	 *in module instead of in guest kernel.
1442a1645ce1SZhang, Yanmin 	 */
144345694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_modules(tool, process_synthesized_event,
1444743eb868SArnaldo Carvalho de Melo 					     machine);
1445a1645ce1SZhang, Yanmin 	if (err < 0)
1446a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
144723346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
1448a1645ce1SZhang, Yanmin 
1449a1645ce1SZhang, Yanmin 	/*
1450a1645ce1SZhang, Yanmin 	 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
1451a1645ce1SZhang, Yanmin 	 * have no _text sometimes.
1452a1645ce1SZhang, Yanmin 	 */
145345694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
14540ae617beSAdrian Hunter 						 machine);
1455a1645ce1SZhang, Yanmin 	if (err < 0)
1456a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
145723346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
1458a1645ce1SZhang, Yanmin }
1459a1645ce1SZhang, Yanmin 
146098402807SFrederic Weisbecker static struct perf_event_header finished_round_event = {
146198402807SFrederic Weisbecker 	.size = sizeof(struct perf_event_header),
146298402807SFrederic Weisbecker 	.type = PERF_RECORD_FINISHED_ROUND,
146398402807SFrederic Weisbecker };
146498402807SFrederic Weisbecker 
14653812d298SAdrian Hunter static struct perf_event_header finished_init_event = {
14663812d298SAdrian Hunter 	.size = sizeof(struct perf_event_header),
14673812d298SAdrian Hunter 	.type = PERF_RECORD_FINISHED_INIT,
14683812d298SAdrian Hunter };
14693812d298SAdrian Hunter 
1470a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map)
1471f13de660SAlexey Budankov {
1472f13de660SAlexey Budankov 	if (rec->opts.affinity != PERF_AFFINITY_SYS &&
1473396b626bSAlexey Bayduraev 	    !bitmap_equal(thread->mask->affinity.bits, map->affinity_mask.bits,
1474396b626bSAlexey Bayduraev 			  thread->mask->affinity.nbits)) {
1475396b626bSAlexey Bayduraev 		bitmap_zero(thread->mask->affinity.bits, thread->mask->affinity.nbits);
1476396b626bSAlexey Bayduraev 		bitmap_or(thread->mask->affinity.bits, thread->mask->affinity.bits,
1477396b626bSAlexey Bayduraev 			  map->affinity_mask.bits, thread->mask->affinity.nbits);
1478396b626bSAlexey Bayduraev 		sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity),
1479396b626bSAlexey Bayduraev 					(cpu_set_t *)thread->mask->affinity.bits);
1480396b626bSAlexey Bayduraev 		if (verbose == 2) {
1481396b626bSAlexey Bayduraev 			pr_debug("threads[%d]: running on cpu%d: ", thread->tid, sched_getcpu());
1482396b626bSAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&thread->mask->affinity, "affinity");
1483396b626bSAlexey Bayduraev 		}
1484f13de660SAlexey Budankov 	}
1485f13de660SAlexey Budankov }
1486f13de660SAlexey Budankov 
14875d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment)
14885d7f4116SAlexey Budankov {
148972932371SJiri Olsa 	struct perf_record_compressed *event = record;
14905d7f4116SAlexey Budankov 	size_t size = sizeof(*event);
14915d7f4116SAlexey Budankov 
14925d7f4116SAlexey Budankov 	if (increment) {
14935d7f4116SAlexey Budankov 		event->header.size += increment;
14945d7f4116SAlexey Budankov 		return increment;
14955d7f4116SAlexey Budankov 	}
14965d7f4116SAlexey Budankov 
14975d7f4116SAlexey Budankov 	event->header.type = PERF_RECORD_COMPRESSED;
14985d7f4116SAlexey Budankov 	event->header.size = size;
14995d7f4116SAlexey Budankov 
15005d7f4116SAlexey Budankov 	return size;
15015d7f4116SAlexey Budankov }
15025d7f4116SAlexey Budankov 
150375f5f1fcSAlexey Bayduraev static size_t zstd_compress(struct perf_session *session, struct mmap *map,
150475f5f1fcSAlexey Bayduraev 			    void *dst, size_t dst_size, void *src, size_t src_size)
15055d7f4116SAlexey Budankov {
15065d7f4116SAlexey Budankov 	size_t compressed;
150772932371SJiri Olsa 	size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
150875f5f1fcSAlexey Bayduraev 	struct zstd_data *zstd_data = &session->zstd_data;
15095d7f4116SAlexey Budankov 
151075f5f1fcSAlexey Bayduraev 	if (map && map->file)
151175f5f1fcSAlexey Bayduraev 		zstd_data = &map->zstd_data;
151275f5f1fcSAlexey Bayduraev 
151375f5f1fcSAlexey Bayduraev 	compressed = zstd_compress_stream_to_records(zstd_data, dst, dst_size, src, src_size,
15145d7f4116SAlexey Budankov 						     max_record_size, process_comp_header);
15155d7f4116SAlexey Budankov 
1516610fbc01SAlexey Bayduraev 	if (map && map->file) {
1517610fbc01SAlexey Bayduraev 		thread->bytes_transferred += src_size;
1518610fbc01SAlexey Bayduraev 		thread->bytes_compressed  += compressed;
1519610fbc01SAlexey Bayduraev 	} else {
15205d7f4116SAlexey Budankov 		session->bytes_transferred += src_size;
15215d7f4116SAlexey Budankov 		session->bytes_compressed  += compressed;
1522610fbc01SAlexey Bayduraev 	}
15235d7f4116SAlexey Budankov 
15245d7f4116SAlexey Budankov 	return compressed;
15255d7f4116SAlexey Budankov }
15265d7f4116SAlexey Budankov 
152763503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist,
1528470530bbSAlexey Budankov 				    bool overwrite, bool synch)
152998402807SFrederic Weisbecker {
1530dcabb507SJiri Olsa 	u64 bytes_written = rec->bytes_written;
15310e2e63ddSPeter Zijlstra 	int i;
15328d3eca20SDavid Ahern 	int rc = 0;
1533396b626bSAlexey Bayduraev 	int nr_mmaps;
1534396b626bSAlexey Bayduraev 	struct mmap **maps;
1535d3d1af6fSAlexey Budankov 	int trace_fd = rec->data.file.fd;
1536ef781128SAlexey Budankov 	off_t off = 0;
153798402807SFrederic Weisbecker 
1538cb21686bSWang Nan 	if (!evlist)
1539cb21686bSWang Nan 		return 0;
1540ef149c25SAdrian Hunter 
1541396b626bSAlexey Bayduraev 	nr_mmaps = thread->nr_mmaps;
1542396b626bSAlexey Bayduraev 	maps = overwrite ? thread->overwrite_maps : thread->maps;
1543396b626bSAlexey Bayduraev 
1544a4ea0ec4SWang Nan 	if (!maps)
1545a4ea0ec4SWang Nan 		return 0;
1546cb21686bSWang Nan 
15470b72d69aSWang Nan 	if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
154854cc54deSWang Nan 		return 0;
154954cc54deSWang Nan 
1550d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1551d3d1af6fSAlexey Budankov 		off = record__aio_get_pos(trace_fd);
1552d3d1af6fSAlexey Budankov 
1553396b626bSAlexey Bayduraev 	for (i = 0; i < nr_mmaps; i++) {
1554470530bbSAlexey Budankov 		u64 flush = 0;
1555396b626bSAlexey Bayduraev 		struct mmap *map = maps[i];
1556a4ea0ec4SWang Nan 
1557547740f7SJiri Olsa 		if (map->core.base) {
1558f13de660SAlexey Budankov 			record__adjust_affinity(rec, map);
1559470530bbSAlexey Budankov 			if (synch) {
156065aa2e6bSJiri Olsa 				flush = map->core.flush;
156165aa2e6bSJiri Olsa 				map->core.flush = 1;
1562470530bbSAlexey Budankov 			}
1563d3d1af6fSAlexey Budankov 			if (!record__aio_enabled(rec)) {
1564ef781128SAlexey Budankov 				if (perf_mmap__push(map, rec, record__pushfn) < 0) {
1565470530bbSAlexey Budankov 					if (synch)
156665aa2e6bSJiri Olsa 						map->core.flush = flush;
15678d3eca20SDavid Ahern 					rc = -1;
15688d3eca20SDavid Ahern 					goto out;
15698d3eca20SDavid Ahern 				}
1570d3d1af6fSAlexey Budankov 			} else {
1571ef781128SAlexey Budankov 				if (record__aio_push(rec, map, &off) < 0) {
1572d3d1af6fSAlexey Budankov 					record__aio_set_pos(trace_fd, off);
1573470530bbSAlexey Budankov 					if (synch)
157465aa2e6bSJiri Olsa 						map->core.flush = flush;
1575d3d1af6fSAlexey Budankov 					rc = -1;
1576d3d1af6fSAlexey Budankov 					goto out;
1577d3d1af6fSAlexey Budankov 				}
1578d3d1af6fSAlexey Budankov 			}
1579470530bbSAlexey Budankov 			if (synch)
158065aa2e6bSJiri Olsa 				map->core.flush = flush;
15818d3eca20SDavid Ahern 		}
1582ef149c25SAdrian Hunter 
1583e035f4caSJiri Olsa 		if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
1584c0a6de06SAdrian Hunter 		    !rec->opts.auxtrace_sample_mode &&
1585e035f4caSJiri Olsa 		    record__auxtrace_mmap_read(rec, map) != 0) {
1586ef149c25SAdrian Hunter 			rc = -1;
1587ef149c25SAdrian Hunter 			goto out;
1588ef149c25SAdrian Hunter 		}
158998402807SFrederic Weisbecker 	}
159098402807SFrederic Weisbecker 
1591d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1592d3d1af6fSAlexey Budankov 		record__aio_set_pos(trace_fd, off);
1593d3d1af6fSAlexey Budankov 
1594dcabb507SJiri Olsa 	/*
1595dcabb507SJiri Olsa 	 * Mark the round finished in case we wrote
1596dcabb507SJiri Olsa 	 * at least one event.
159756f735ffSAlexey Bayduraev 	 *
159856f735ffSAlexey Bayduraev 	 * No need for round events in directory mode,
159956f735ffSAlexey Bayduraev 	 * because per-cpu maps and files have data
160056f735ffSAlexey Bayduraev 	 * sorted by kernel.
1601dcabb507SJiri Olsa 	 */
160256f735ffSAlexey Bayduraev 	if (!record__threads_enabled(rec) && bytes_written != rec->bytes_written)
1603ded2b8feSJiri Olsa 		rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event));
16048d3eca20SDavid Ahern 
16050b72d69aSWang Nan 	if (overwrite)
1606ade9d208SArnaldo Carvalho de Melo 		evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
16078d3eca20SDavid Ahern out:
16088d3eca20SDavid Ahern 	return rc;
160998402807SFrederic Weisbecker }
161098402807SFrederic Weisbecker 
1611470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch)
1612cb21686bSWang Nan {
1613cb21686bSWang Nan 	int err;
1614cb21686bSWang Nan 
1615470530bbSAlexey Budankov 	err = record__mmap_read_evlist(rec, rec->evlist, false, synch);
1616cb21686bSWang Nan 	if (err)
1617cb21686bSWang Nan 		return err;
1618cb21686bSWang Nan 
1619470530bbSAlexey Budankov 	return record__mmap_read_evlist(rec, rec->evlist, true, synch);
1620cb21686bSWang Nan }
1621cb21686bSWang Nan 
1622396b626bSAlexey Bayduraev static void record__thread_munmap_filtered(struct fdarray *fda, int fd,
1623396b626bSAlexey Bayduraev 					   void *arg __maybe_unused)
1624396b626bSAlexey Bayduraev {
1625396b626bSAlexey Bayduraev 	struct perf_mmap *map = fda->priv[fd].ptr;
1626396b626bSAlexey Bayduraev 
1627396b626bSAlexey Bayduraev 	if (map)
1628396b626bSAlexey Bayduraev 		perf_mmap__put(map);
1629396b626bSAlexey Bayduraev }
1630396b626bSAlexey Bayduraev 
16313217e9feSAlexey Bayduraev static void *record__thread(void *arg)
16323217e9feSAlexey Bayduraev {
16333217e9feSAlexey Bayduraev 	enum thread_msg msg = THREAD_MSG__READY;
16343217e9feSAlexey Bayduraev 	bool terminate = false;
16353217e9feSAlexey Bayduraev 	struct fdarray *pollfd;
16363217e9feSAlexey Bayduraev 	int err, ctlfd_pos;
16373217e9feSAlexey Bayduraev 
16383217e9feSAlexey Bayduraev 	thread = arg;
16393217e9feSAlexey Bayduraev 	thread->tid = gettid();
16403217e9feSAlexey Bayduraev 
16413217e9feSAlexey Bayduraev 	err = write(thread->pipes.ack[1], &msg, sizeof(msg));
16423217e9feSAlexey Bayduraev 	if (err == -1)
16433217e9feSAlexey Bayduraev 		pr_warning("threads[%d]: failed to notify on start: %s\n",
16443217e9feSAlexey Bayduraev 			   thread->tid, strerror(errno));
16453217e9feSAlexey Bayduraev 
16463217e9feSAlexey Bayduraev 	pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu());
16473217e9feSAlexey Bayduraev 
16483217e9feSAlexey Bayduraev 	pollfd = &thread->pollfd;
16493217e9feSAlexey Bayduraev 	ctlfd_pos = thread->ctlfd_pos;
16503217e9feSAlexey Bayduraev 
16513217e9feSAlexey Bayduraev 	for (;;) {
16523217e9feSAlexey Bayduraev 		unsigned long long hits = thread->samples;
16533217e9feSAlexey Bayduraev 
16543217e9feSAlexey Bayduraev 		if (record__mmap_read_all(thread->rec, false) < 0 || terminate)
16553217e9feSAlexey Bayduraev 			break;
16563217e9feSAlexey Bayduraev 
16573217e9feSAlexey Bayduraev 		if (hits == thread->samples) {
16583217e9feSAlexey Bayduraev 
16593217e9feSAlexey Bayduraev 			err = fdarray__poll(pollfd, -1);
16603217e9feSAlexey Bayduraev 			/*
16613217e9feSAlexey Bayduraev 			 * Propagate error, only if there's any. Ignore positive
16623217e9feSAlexey Bayduraev 			 * number of returned events and interrupt error.
16633217e9feSAlexey Bayduraev 			 */
16643217e9feSAlexey Bayduraev 			if (err > 0 || (err < 0 && errno == EINTR))
16653217e9feSAlexey Bayduraev 				err = 0;
16663217e9feSAlexey Bayduraev 			thread->waking++;
16673217e9feSAlexey Bayduraev 
16683217e9feSAlexey Bayduraev 			if (fdarray__filter(pollfd, POLLERR | POLLHUP,
16693217e9feSAlexey Bayduraev 					    record__thread_munmap_filtered, NULL) == 0)
16703217e9feSAlexey Bayduraev 				break;
16713217e9feSAlexey Bayduraev 		}
16723217e9feSAlexey Bayduraev 
16733217e9feSAlexey Bayduraev 		if (pollfd->entries[ctlfd_pos].revents & POLLHUP) {
16743217e9feSAlexey Bayduraev 			terminate = true;
16753217e9feSAlexey Bayduraev 			close(thread->pipes.msg[0]);
16763217e9feSAlexey Bayduraev 			thread->pipes.msg[0] = -1;
16773217e9feSAlexey Bayduraev 			pollfd->entries[ctlfd_pos].fd = -1;
16783217e9feSAlexey Bayduraev 			pollfd->entries[ctlfd_pos].events = 0;
16793217e9feSAlexey Bayduraev 		}
16803217e9feSAlexey Bayduraev 
16813217e9feSAlexey Bayduraev 		pollfd->entries[ctlfd_pos].revents = 0;
16823217e9feSAlexey Bayduraev 	}
16833217e9feSAlexey Bayduraev 	record__mmap_read_all(thread->rec, true);
16843217e9feSAlexey Bayduraev 
16853217e9feSAlexey Bayduraev 	err = write(thread->pipes.ack[1], &msg, sizeof(msg));
16863217e9feSAlexey Bayduraev 	if (err == -1)
16873217e9feSAlexey Bayduraev 		pr_warning("threads[%d]: failed to notify on termination: %s\n",
16883217e9feSAlexey Bayduraev 			   thread->tid, strerror(errno));
16893217e9feSAlexey Bayduraev 
16903217e9feSAlexey Bayduraev 	return NULL;
16913217e9feSAlexey Bayduraev }
16923217e9feSAlexey Bayduraev 
16938c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec)
169457706abcSDavid Ahern {
169557706abcSDavid Ahern 	struct perf_session *session = rec->session;
169657706abcSDavid Ahern 	int feat;
169757706abcSDavid Ahern 
169857706abcSDavid Ahern 	for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
169957706abcSDavid Ahern 		perf_header__set_feat(&session->header, feat);
170057706abcSDavid Ahern 
170157706abcSDavid Ahern 	if (rec->no_buildid)
170257706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
170357706abcSDavid Ahern 
1704378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
1705ce9036a6SJiri Olsa 	if (!have_tracepoints(&rec->evlist->core.entries))
170657706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1707378ef0f5SIan Rogers #endif
170857706abcSDavid Ahern 
170957706abcSDavid Ahern 	if (!rec->opts.branch_stack)
171057706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1711ef149c25SAdrian Hunter 
1712ef149c25SAdrian Hunter 	if (!rec->opts.full_auxtrace)
1713ef149c25SAdrian Hunter 		perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1714ffa517adSJiri Olsa 
1715cf790516SAlexey Budankov 	if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns))
1716cf790516SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_CLOCKID);
1717cf790516SAlexey Budankov 
1718d1e325cfSJiri Olsa 	if (!rec->opts.use_clockid)
1719d1e325cfSJiri Olsa 		perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA);
1720d1e325cfSJiri Olsa 
172156f735ffSAlexey Bayduraev 	if (!record__threads_enabled(rec))
1722258031c0SJiri Olsa 		perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
172356f735ffSAlexey Bayduraev 
172442e1fd80SAlexey Budankov 	if (!record__comp_enabled(rec))
172542e1fd80SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_COMPRESSED);
1726258031c0SJiri Olsa 
1727ffa517adSJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_STAT);
172857706abcSDavid Ahern }
172957706abcSDavid Ahern 
1730e1ab48baSWang Nan static void
1731e1ab48baSWang Nan record__finish_output(struct record *rec)
1732e1ab48baSWang Nan {
173356f735ffSAlexey Bayduraev 	int i;
17348ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
17358ceb41d7SJiri Olsa 	int fd = perf_data__fd(data);
1736e1ab48baSWang Nan 
17378ceb41d7SJiri Olsa 	if (data->is_pipe)
1738e1ab48baSWang Nan 		return;
1739e1ab48baSWang Nan 
1740e1ab48baSWang Nan 	rec->session->header.data_size += rec->bytes_written;
174145112e89SJiri Olsa 	data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR);
174256f735ffSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
174356f735ffSAlexey Bayduraev 		for (i = 0; i < data->dir.nr; i++)
174456f735ffSAlexey Bayduraev 			data->dir.files[i].size = lseek(data->dir.files[i].fd, 0, SEEK_CUR);
174556f735ffSAlexey Bayduraev 	}
1746e1ab48baSWang Nan 
1747e1ab48baSWang Nan 	if (!rec->no_buildid) {
1748e1ab48baSWang Nan 		process_buildids(rec);
1749e1ab48baSWang Nan 
1750e1ab48baSWang Nan 		if (rec->buildid_all)
1751e1ab48baSWang Nan 			dsos__hit_all(rec->session);
1752e1ab48baSWang Nan 	}
1753e1ab48baSWang Nan 	perf_session__write_header(rec->session, rec->evlist, fd, true);
1754e1ab48baSWang Nan 
1755e1ab48baSWang Nan 	return;
1756e1ab48baSWang Nan }
1757e1ab48baSWang Nan 
17584ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail)
1759be7b0c9eSWang Nan {
17609d6aae72SArnaldo Carvalho de Melo 	int err;
17619749b90eSJiri Olsa 	struct perf_thread_map *thread_map;
176241b740b6SNamhyung Kim 	bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP;
1763be7b0c9eSWang Nan 
17644ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
17654ea648aeSWang Nan 		return 0;
17664ea648aeSWang Nan 
17679d6aae72SArnaldo Carvalho de Melo 	thread_map = thread_map__new_by_tid(rec->evlist->workload.pid);
17689d6aae72SArnaldo Carvalho de Melo 	if (thread_map == NULL)
17699d6aae72SArnaldo Carvalho de Melo 		return -1;
17709d6aae72SArnaldo Carvalho de Melo 
17719d6aae72SArnaldo Carvalho de Melo 	err = perf_event__synthesize_thread_map(&rec->tool, thread_map,
1772be7b0c9eSWang Nan 						 process_synthesized_event,
1773be7b0c9eSWang Nan 						 &rec->session->machines.host,
177441b740b6SNamhyung Kim 						 needs_mmap,
17753fcb10e4SMark Drayton 						 rec->opts.sample_address);
17767836e52eSJiri Olsa 	perf_thread_map__put(thread_map);
17779d6aae72SArnaldo Carvalho de Melo 	return err;
1778be7b0c9eSWang Nan }
1779be7b0c9eSWang Nan 
17803812d298SAdrian Hunter static int write_finished_init(struct record *rec, bool tail)
17813812d298SAdrian Hunter {
17823812d298SAdrian Hunter 	if (rec->opts.tail_synthesize != tail)
17833812d298SAdrian Hunter 		return 0;
17843812d298SAdrian Hunter 
17853812d298SAdrian Hunter 	return record__write(rec, NULL, &finished_init_event, sizeof(finished_init_event));
17863812d298SAdrian Hunter }
17873812d298SAdrian Hunter 
17884ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail);
17893c1cb7e3SWang Nan 
1790ecfd7a9cSWang Nan static int
1791ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit)
1792ecfd7a9cSWang Nan {
17938ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1794ecfd7a9cSWang Nan 	int fd, err;
179503724b2eSAndi Kleen 	char *new_filename;
1796ecfd7a9cSWang Nan 
1797ecfd7a9cSWang Nan 	/* Same Size:      "2015122520103046"*/
1798ecfd7a9cSWang Nan 	char timestamp[] = "InvalidTimestamp";
1799ecfd7a9cSWang Nan 
1800d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
1801d3d1af6fSAlexey Budankov 
18023812d298SAdrian Hunter 	write_finished_init(rec, true);
18033812d298SAdrian Hunter 
18044ea648aeSWang Nan 	record__synthesize(rec, true);
18054ea648aeSWang Nan 	if (target__none(&rec->opts.target))
18064ea648aeSWang Nan 		record__synthesize_workload(rec, true);
18074ea648aeSWang Nan 
1808ecfd7a9cSWang Nan 	rec->samples = 0;
1809ecfd7a9cSWang Nan 	record__finish_output(rec);
1810ecfd7a9cSWang Nan 	err = fetch_current_timestamp(timestamp, sizeof(timestamp));
1811ecfd7a9cSWang Nan 	if (err) {
1812ecfd7a9cSWang Nan 		pr_err("Failed to get current timestamp\n");
1813ecfd7a9cSWang Nan 		return -EINVAL;
1814ecfd7a9cSWang Nan 	}
1815ecfd7a9cSWang Nan 
18168ceb41d7SJiri Olsa 	fd = perf_data__switch(data, timestamp,
1817ecfd7a9cSWang Nan 				    rec->session->header.data_offset,
181803724b2eSAndi Kleen 				    at_exit, &new_filename);
1819ecfd7a9cSWang Nan 	if (fd >= 0 && !at_exit) {
1820ecfd7a9cSWang Nan 		rec->bytes_written = 0;
1821ecfd7a9cSWang Nan 		rec->session->header.data_size = 0;
1822ecfd7a9cSWang Nan 	}
1823ecfd7a9cSWang Nan 
1824ecfd7a9cSWang Nan 	if (!quiet)
1825ecfd7a9cSWang Nan 		fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
18262d4f2799SJiri Olsa 			data->path, timestamp);
18273c1cb7e3SWang Nan 
182803724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
182903724b2eSAndi Kleen 		int n = rec->switch_output.cur_file + 1;
183003724b2eSAndi Kleen 
183103724b2eSAndi Kleen 		if (n >= rec->switch_output.num_files)
183203724b2eSAndi Kleen 			n = 0;
183303724b2eSAndi Kleen 		rec->switch_output.cur_file = n;
183403724b2eSAndi Kleen 		if (rec->switch_output.filenames[n]) {
183503724b2eSAndi Kleen 			remove(rec->switch_output.filenames[n]);
1836d8f9da24SArnaldo Carvalho de Melo 			zfree(&rec->switch_output.filenames[n]);
183703724b2eSAndi Kleen 		}
183803724b2eSAndi Kleen 		rec->switch_output.filenames[n] = new_filename;
183903724b2eSAndi Kleen 	} else {
184003724b2eSAndi Kleen 		free(new_filename);
184103724b2eSAndi Kleen 	}
184203724b2eSAndi Kleen 
18433c1cb7e3SWang Nan 	/* Output tracking events */
1844be7b0c9eSWang Nan 	if (!at_exit) {
18454ea648aeSWang Nan 		record__synthesize(rec, false);
18463c1cb7e3SWang Nan 
1847be7b0c9eSWang Nan 		/*
1848be7b0c9eSWang Nan 		 * In 'perf record --switch-output' without -a,
1849be7b0c9eSWang Nan 		 * record__synthesize() in record__switch_output() won't
1850be7b0c9eSWang Nan 		 * generate tracking events because there's no thread_map
1851be7b0c9eSWang Nan 		 * in evlist. Which causes newly created perf.data doesn't
1852be7b0c9eSWang Nan 		 * contain map and comm information.
1853be7b0c9eSWang Nan 		 * Create a fake thread_map and directly call
1854be7b0c9eSWang Nan 		 * perf_event__synthesize_thread_map() for those events.
1855be7b0c9eSWang Nan 		 */
1856be7b0c9eSWang Nan 		if (target__none(&rec->opts.target))
18574ea648aeSWang Nan 			record__synthesize_workload(rec, false);
18583812d298SAdrian Hunter 		write_finished_init(rec, false);
1859be7b0c9eSWang Nan 	}
1860ecfd7a9cSWang Nan 	return fd;
1861ecfd7a9cSWang Nan }
1862ecfd7a9cSWang Nan 
1863e3a23261SNamhyung Kim static void __record__read_lost_samples(struct record *rec, struct evsel *evsel,
1864e3a23261SNamhyung Kim 					struct perf_record_lost_samples *lost,
1865e3a23261SNamhyung Kim 					int cpu_idx, int thread_idx)
1866e3a23261SNamhyung Kim {
1867e3a23261SNamhyung Kim 	struct perf_counts_values count;
1868e3a23261SNamhyung Kim 	struct perf_sample_id *sid;
1869e3a23261SNamhyung Kim 	struct perf_sample sample = {};
1870e3a23261SNamhyung Kim 	int id_hdr_size;
1871e3a23261SNamhyung Kim 
1872e3a23261SNamhyung Kim 	if (perf_evsel__read(&evsel->core, cpu_idx, thread_idx, &count) < 0) {
1873e3a23261SNamhyung Kim 		pr_err("read LOST count failed\n");
1874e3a23261SNamhyung Kim 		return;
1875e3a23261SNamhyung Kim 	}
1876e3a23261SNamhyung Kim 
1877e3a23261SNamhyung Kim 	if (count.lost == 0)
1878e3a23261SNamhyung Kim 		return;
1879e3a23261SNamhyung Kim 
1880e3a23261SNamhyung Kim 	lost->lost = count.lost;
1881e3a23261SNamhyung Kim 	if (evsel->core.ids) {
1882e3a23261SNamhyung Kim 		sid = xyarray__entry(evsel->core.sample_id, cpu_idx, thread_idx);
1883e3a23261SNamhyung Kim 		sample.id = sid->id;
1884e3a23261SNamhyung Kim 	}
1885e3a23261SNamhyung Kim 
1886e3a23261SNamhyung Kim 	id_hdr_size = perf_event__synthesize_id_sample((void *)(lost + 1),
1887e3a23261SNamhyung Kim 						       evsel->core.attr.sample_type, &sample);
1888e3a23261SNamhyung Kim 	lost->header.size = sizeof(*lost) + id_hdr_size;
1889e3a23261SNamhyung Kim 	record__write(rec, NULL, lost, lost->header.size);
1890e3a23261SNamhyung Kim }
1891e3a23261SNamhyung Kim 
1892e3a23261SNamhyung Kim static void record__read_lost_samples(struct record *rec)
1893e3a23261SNamhyung Kim {
1894e3a23261SNamhyung Kim 	struct perf_session *session = rec->session;
1895e3a23261SNamhyung Kim 	struct perf_record_lost_samples *lost;
1896e3a23261SNamhyung Kim 	struct evsel *evsel;
1897e3a23261SNamhyung Kim 
1898d031a00aSNamhyung Kim 	/* there was an error during record__open */
1899d031a00aSNamhyung Kim 	if (session->evlist == NULL)
1900d031a00aSNamhyung Kim 		return;
1901d031a00aSNamhyung Kim 
1902e3a23261SNamhyung Kim 	lost = zalloc(PERF_SAMPLE_MAX_SIZE);
1903e3a23261SNamhyung Kim 	if (lost == NULL) {
1904e3a23261SNamhyung Kim 		pr_debug("Memory allocation failed\n");
1905e3a23261SNamhyung Kim 		return;
1906e3a23261SNamhyung Kim 	}
1907e3a23261SNamhyung Kim 
1908e3a23261SNamhyung Kim 	lost->header.type = PERF_RECORD_LOST_SAMPLES;
1909e3a23261SNamhyung Kim 
1910e3a23261SNamhyung Kim 	evlist__for_each_entry(session->evlist, evsel) {
1911e3a23261SNamhyung Kim 		struct xyarray *xy = evsel->core.sample_id;
1912e3a23261SNamhyung Kim 
1913d031a00aSNamhyung Kim 		if (xy == NULL || evsel->core.fd == NULL)
1914d031a00aSNamhyung Kim 			continue;
1915e3a23261SNamhyung Kim 		if (xyarray__max_x(evsel->core.fd) != xyarray__max_x(xy) ||
1916e3a23261SNamhyung Kim 		    xyarray__max_y(evsel->core.fd) != xyarray__max_y(xy)) {
1917e3a23261SNamhyung Kim 			pr_debug("Unmatched FD vs. sample ID: skip reading LOST count\n");
1918e3a23261SNamhyung Kim 			continue;
1919e3a23261SNamhyung Kim 		}
1920e3a23261SNamhyung Kim 
1921e3a23261SNamhyung Kim 		for (int x = 0; x < xyarray__max_x(xy); x++) {
1922e3a23261SNamhyung Kim 			for (int y = 0; y < xyarray__max_y(xy); y++) {
1923e3a23261SNamhyung Kim 				__record__read_lost_samples(rec, evsel, lost, x, y);
1924e3a23261SNamhyung Kim 			}
1925e3a23261SNamhyung Kim 		}
1926e3a23261SNamhyung Kim 	}
1927e3a23261SNamhyung Kim 	free(lost);
1928e3a23261SNamhyung Kim 
1929e3a23261SNamhyung Kim }
1930e3a23261SNamhyung Kim 
19318ed28c2bSIan Rogers static volatile sig_atomic_t workload_exec_errno;
1932f33cbe72SArnaldo Carvalho de Melo 
1933f33cbe72SArnaldo Carvalho de Melo /*
19347b392ef0SArnaldo Carvalho de Melo  * evlist__prepare_workload will send a SIGUSR1
1935f33cbe72SArnaldo Carvalho de Melo  * if the fork fails, since we asked by setting its
1936f33cbe72SArnaldo Carvalho de Melo  * want_signal to true.
1937f33cbe72SArnaldo Carvalho de Melo  */
193845604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused,
193945604710SNamhyung Kim 					siginfo_t *info,
1940f33cbe72SArnaldo Carvalho de Melo 					void *ucontext __maybe_unused)
1941f33cbe72SArnaldo Carvalho de Melo {
1942f33cbe72SArnaldo Carvalho de Melo 	workload_exec_errno = info->si_value.sival_int;
1943f33cbe72SArnaldo Carvalho de Melo 	done = 1;
1944f33cbe72SArnaldo Carvalho de Melo 	child_finished = 1;
1945f33cbe72SArnaldo Carvalho de Melo }
1946f33cbe72SArnaldo Carvalho de Melo 
19472dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig);
1948bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig);
19492dd6d8a1SAdrian Hunter 
1950db0ea13cSArnaldo Carvalho de Melo static const struct perf_event_mmap_page *evlist__pick_pc(struct evlist *evlist)
1951ee667f94SWang Nan {
1952b2cb615dSWang Nan 	if (evlist) {
1953547740f7SJiri Olsa 		if (evlist->mmap && evlist->mmap[0].core.base)
1954547740f7SJiri Olsa 			return evlist->mmap[0].core.base;
1955547740f7SJiri Olsa 		if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base)
1956547740f7SJiri Olsa 			return evlist->overwrite_mmap[0].core.base;
1957b2cb615dSWang Nan 	}
1958ee667f94SWang Nan 	return NULL;
1959ee667f94SWang Nan }
1960ee667f94SWang Nan 
1961c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
1962c45628b0SWang Nan {
1963db0ea13cSArnaldo Carvalho de Melo 	const struct perf_event_mmap_page *pc = evlist__pick_pc(rec->evlist);
1964ee667f94SWang Nan 	if (pc)
1965ee667f94SWang Nan 		return pc;
1966c45628b0SWang Nan 	return NULL;
1967c45628b0SWang Nan }
1968c45628b0SWang Nan 
19694ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail)
1970c45c86ebSWang Nan {
1971c45c86ebSWang Nan 	struct perf_session *session = rec->session;
1972c45c86ebSWang Nan 	struct machine *machine = &session->machines.host;
19738ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1974c45c86ebSWang Nan 	struct record_opts *opts = &rec->opts;
1975c45c86ebSWang Nan 	struct perf_tool *tool = &rec->tool;
1976c45c86ebSWang Nan 	int err = 0;
1977d99c22eaSStephane Eranian 	event_op f = process_synthesized_event;
1978c45c86ebSWang Nan 
19794ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
19804ea648aeSWang Nan 		return 0;
19814ea648aeSWang Nan 
19828ceb41d7SJiri Olsa 	if (data->is_pipe) {
1983c3a057dcSNamhyung Kim 		err = perf_event__synthesize_for_pipe(tool, session, data,
1984c45c86ebSWang Nan 						      process_synthesized_event);
1985c3a057dcSNamhyung Kim 		if (err < 0)
1986c45c86ebSWang Nan 			goto out;
1987c45c86ebSWang Nan 
1988c45c86ebSWang Nan 		rec->bytes_written += err;
1989c45c86ebSWang Nan 	}
1990c45c86ebSWang Nan 
1991c45628b0SWang Nan 	err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
199246bc29b9SAdrian Hunter 					  process_synthesized_event, machine);
199346bc29b9SAdrian Hunter 	if (err)
199446bc29b9SAdrian Hunter 		goto out;
199546bc29b9SAdrian Hunter 
1996c0a6de06SAdrian Hunter 	/* Synthesize id_index before auxtrace_info */
1997c0a6de06SAdrian Hunter 	err = perf_event__synthesize_id_index(tool,
1998c0a6de06SAdrian Hunter 					      process_synthesized_event,
1999c0a6de06SAdrian Hunter 					      session->evlist, machine);
2000c0a6de06SAdrian Hunter 	if (err)
2001c0a6de06SAdrian Hunter 		goto out;
2002c0a6de06SAdrian Hunter 
2003c45c86ebSWang Nan 	if (rec->opts.full_auxtrace) {
2004c45c86ebSWang Nan 		err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
2005c45c86ebSWang Nan 					session, process_synthesized_event);
2006c45c86ebSWang Nan 		if (err)
2007c45c86ebSWang Nan 			goto out;
2008c45c86ebSWang Nan 	}
2009c45c86ebSWang Nan 
201078e1bc25SArnaldo Carvalho de Melo 	if (!evlist__exclude_kernel(rec->evlist)) {
2011c45c86ebSWang Nan 		err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
2012c45c86ebSWang Nan 							 machine);
2013c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
2014c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
2015c45c86ebSWang Nan 				   "Check /proc/kallsyms permission or run as root.\n");
2016c45c86ebSWang Nan 
2017c45c86ebSWang Nan 		err = perf_event__synthesize_modules(tool, process_synthesized_event,
2018c45c86ebSWang Nan 						     machine);
2019c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
2020c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
2021c45c86ebSWang Nan 				   "Check /proc/modules permission or run as root.\n");
20226c443954SArnaldo Carvalho de Melo 	}
2023c45c86ebSWang Nan 
2024c45c86ebSWang Nan 	if (perf_guest) {
2025c45c86ebSWang Nan 		machines__process_guests(&session->machines,
2026c45c86ebSWang Nan 					 perf_event__synthesize_guest_os, tool);
2027c45c86ebSWang Nan 	}
2028c45c86ebSWang Nan 
2029bfd8f72cSAndi Kleen 	err = perf_event__synthesize_extra_attr(&rec->tool,
2030bfd8f72cSAndi Kleen 						rec->evlist,
2031bfd8f72cSAndi Kleen 						process_synthesized_event,
2032bfd8f72cSAndi Kleen 						data->is_pipe);
2033bfd8f72cSAndi Kleen 	if (err)
2034bfd8f72cSAndi Kleen 		goto out;
2035bfd8f72cSAndi Kleen 
203603617c22SJiri Olsa 	err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads,
2037373565d2SAndi Kleen 						 process_synthesized_event,
2038373565d2SAndi Kleen 						NULL);
2039373565d2SAndi Kleen 	if (err < 0) {
2040373565d2SAndi Kleen 		pr_err("Couldn't synthesize thread map.\n");
2041373565d2SAndi Kleen 		return err;
2042373565d2SAndi Kleen 	}
2043373565d2SAndi Kleen 
20447be1feddSAdrian Hunter 	err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.all_cpus,
2045373565d2SAndi Kleen 					     process_synthesized_event, NULL);
2046373565d2SAndi Kleen 	if (err < 0) {
2047373565d2SAndi Kleen 		pr_err("Couldn't synthesize cpu map.\n");
2048373565d2SAndi Kleen 		return err;
2049373565d2SAndi Kleen 	}
2050373565d2SAndi Kleen 
2051e5416950SSong Liu 	err = perf_event__synthesize_bpf_events(session, process_synthesized_event,
20527b612e29SSong Liu 						machine, opts);
2053faf59ec8SAdrian Hunter 	if (err < 0) {
20547b612e29SSong Liu 		pr_warning("Couldn't synthesize bpf events.\n");
2055faf59ec8SAdrian Hunter 		err = 0;
2056faf59ec8SAdrian Hunter 	}
20577b612e29SSong Liu 
205841b740b6SNamhyung Kim 	if (rec->opts.synth & PERF_SYNTH_CGROUP) {
2059ab64069fSNamhyung Kim 		err = perf_event__synthesize_cgroups(tool, process_synthesized_event,
2060ab64069fSNamhyung Kim 						     machine);
2061faf59ec8SAdrian Hunter 		if (err < 0) {
2062ab64069fSNamhyung Kim 			pr_warning("Couldn't synthesize cgroup events.\n");
2063faf59ec8SAdrian Hunter 			err = 0;
2064faf59ec8SAdrian Hunter 		}
206541b740b6SNamhyung Kim 	}
2066ab64069fSNamhyung Kim 
2067d99c22eaSStephane Eranian 	if (rec->opts.nr_threads_synthesize > 1) {
206849c670b1SIan Rogers 		mutex_init(&synth_lock);
2069d99c22eaSStephane Eranian 		perf_set_multithreaded();
2070d99c22eaSStephane Eranian 		f = process_locked_synthesized_event;
2071d99c22eaSStephane Eranian 	}
2072d99c22eaSStephane Eranian 
207341b740b6SNamhyung Kim 	if (rec->opts.synth & PERF_SYNTH_TASK) {
207441b740b6SNamhyung Kim 		bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP;
207541b740b6SNamhyung Kim 
207684111b9cSNamhyung Kim 		err = __machine__synthesize_threads(machine, tool, &opts->target,
207784111b9cSNamhyung Kim 						    rec->evlist->core.threads,
207841b740b6SNamhyung Kim 						    f, needs_mmap, opts->sample_address,
2079d99c22eaSStephane Eranian 						    rec->opts.nr_threads_synthesize);
208041b740b6SNamhyung Kim 	}
2081d99c22eaSStephane Eranian 
208249c670b1SIan Rogers 	if (rec->opts.nr_threads_synthesize > 1) {
2083d99c22eaSStephane Eranian 		perf_set_singlethreaded();
208449c670b1SIan Rogers 		mutex_destroy(&synth_lock);
208549c670b1SIan Rogers 	}
2086d99c22eaSStephane Eranian 
2087c45c86ebSWang Nan out:
2088c45c86ebSWang Nan 	return err;
2089c45c86ebSWang Nan }
2090c45c86ebSWang Nan 
2091899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data)
2092899e5ffbSArnaldo Carvalho de Melo {
2093899e5ffbSArnaldo Carvalho de Melo 	struct record *rec = data;
2094899e5ffbSArnaldo Carvalho de Melo 	pthread_kill(rec->thread_id, SIGUSR2);
2095899e5ffbSArnaldo Carvalho de Melo 	return 0;
2096899e5ffbSArnaldo Carvalho de Melo }
2097899e5ffbSArnaldo Carvalho de Melo 
209823cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec)
209923cbb41cSArnaldo Carvalho de Melo {
210023cbb41cSArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
210123cbb41cSArnaldo Carvalho de Melo 
210223cbb41cSArnaldo Carvalho de Melo 	if (rec->sb_evlist != NULL) {
210323cbb41cSArnaldo Carvalho de Melo 		/*
210423cbb41cSArnaldo Carvalho de Melo 		 * We get here if --switch-output-event populated the
210523cbb41cSArnaldo Carvalho de Melo 		 * sb_evlist, so associate a callback that will send a SIGUSR2
210623cbb41cSArnaldo Carvalho de Melo 		 * to the main thread.
210723cbb41cSArnaldo Carvalho de Melo 		 */
210823cbb41cSArnaldo Carvalho de Melo 		evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec);
210923cbb41cSArnaldo Carvalho de Melo 		rec->thread_id = pthread_self();
211023cbb41cSArnaldo Carvalho de Melo 	}
21111101c872SJin Yao #ifdef HAVE_LIBBPF_SUPPORT
211223cbb41cSArnaldo Carvalho de Melo 	if (!opts->no_bpf_event) {
211323cbb41cSArnaldo Carvalho de Melo 		if (rec->sb_evlist == NULL) {
211423cbb41cSArnaldo Carvalho de Melo 			rec->sb_evlist = evlist__new();
211523cbb41cSArnaldo Carvalho de Melo 
211623cbb41cSArnaldo Carvalho de Melo 			if (rec->sb_evlist == NULL) {
211723cbb41cSArnaldo Carvalho de Melo 				pr_err("Couldn't create side band evlist.\n.");
211823cbb41cSArnaldo Carvalho de Melo 				return -1;
211923cbb41cSArnaldo Carvalho de Melo 			}
212023cbb41cSArnaldo Carvalho de Melo 		}
212123cbb41cSArnaldo Carvalho de Melo 
212223cbb41cSArnaldo Carvalho de Melo 		if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) {
212323cbb41cSArnaldo Carvalho de Melo 			pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
212423cbb41cSArnaldo Carvalho de Melo 			return -1;
212523cbb41cSArnaldo Carvalho de Melo 		}
212623cbb41cSArnaldo Carvalho de Melo 	}
21271101c872SJin Yao #endif
212808c83997SArnaldo Carvalho de Melo 	if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) {
212923cbb41cSArnaldo Carvalho de Melo 		pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
213023cbb41cSArnaldo Carvalho de Melo 		opts->no_bpf_event = true;
213123cbb41cSArnaldo Carvalho de Melo 	}
213223cbb41cSArnaldo Carvalho de Melo 
213323cbb41cSArnaldo Carvalho de Melo 	return 0;
213423cbb41cSArnaldo Carvalho de Melo }
213523cbb41cSArnaldo Carvalho de Melo 
2136d1e325cfSJiri Olsa static int record__init_clock(struct record *rec)
2137d1e325cfSJiri Olsa {
2138d1e325cfSJiri Olsa 	struct perf_session *session = rec->session;
2139d1e325cfSJiri Olsa 	struct timespec ref_clockid;
2140d1e325cfSJiri Olsa 	struct timeval ref_tod;
2141d1e325cfSJiri Olsa 	u64 ref;
2142d1e325cfSJiri Olsa 
2143d1e325cfSJiri Olsa 	if (!rec->opts.use_clockid)
2144d1e325cfSJiri Olsa 		return 0;
2145d1e325cfSJiri Olsa 
21469d88a1a1SJiri Olsa 	if (rec->opts.use_clockid && rec->opts.clockid_res_ns)
21479d88a1a1SJiri Olsa 		session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns;
21489d88a1a1SJiri Olsa 
2149d1e325cfSJiri Olsa 	session->header.env.clock.clockid = rec->opts.clockid;
2150d1e325cfSJiri Olsa 
2151d1e325cfSJiri Olsa 	if (gettimeofday(&ref_tod, NULL) != 0) {
2152d1e325cfSJiri Olsa 		pr_err("gettimeofday failed, cannot set reference time.\n");
2153d1e325cfSJiri Olsa 		return -1;
2154d1e325cfSJiri Olsa 	}
2155d1e325cfSJiri Olsa 
2156d1e325cfSJiri Olsa 	if (clock_gettime(rec->opts.clockid, &ref_clockid)) {
2157d1e325cfSJiri Olsa 		pr_err("clock_gettime failed, cannot set reference time.\n");
2158d1e325cfSJiri Olsa 		return -1;
2159d1e325cfSJiri Olsa 	}
2160d1e325cfSJiri Olsa 
2161d1e325cfSJiri Olsa 	ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC +
2162d1e325cfSJiri Olsa 	      (u64) ref_tod.tv_usec * NSEC_PER_USEC;
2163d1e325cfSJiri Olsa 
2164d1e325cfSJiri Olsa 	session->header.env.clock.tod_ns = ref;
2165d1e325cfSJiri Olsa 
2166d1e325cfSJiri Olsa 	ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC +
2167d1e325cfSJiri Olsa 	      (u64) ref_clockid.tv_nsec;
2168d1e325cfSJiri Olsa 
2169d1e325cfSJiri Olsa 	session->header.env.clock.clockid_ns = ref;
2170d1e325cfSJiri Olsa 	return 0;
2171d1e325cfSJiri Olsa }
2172d1e325cfSJiri Olsa 
2173d20aff15SAdrian Hunter static void hit_auxtrace_snapshot_trigger(struct record *rec)
2174d20aff15SAdrian Hunter {
2175d20aff15SAdrian Hunter 	if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
2176d20aff15SAdrian Hunter 		trigger_hit(&auxtrace_snapshot_trigger);
2177d20aff15SAdrian Hunter 		auxtrace_record__snapshot_started = 1;
2178d20aff15SAdrian Hunter 		if (auxtrace_record__snapshot_start(rec->itr))
2179d20aff15SAdrian Hunter 			trigger_error(&auxtrace_snapshot_trigger);
2180d20aff15SAdrian Hunter 	}
2181d20aff15SAdrian Hunter }
2182d20aff15SAdrian Hunter 
218391c0f5ecSJin Yao static void record__uniquify_name(struct record *rec)
218491c0f5ecSJin Yao {
218591c0f5ecSJin Yao 	struct evsel *pos;
218691c0f5ecSJin Yao 	struct evlist *evlist = rec->evlist;
218791c0f5ecSJin Yao 	char *new_name;
218891c0f5ecSJin Yao 	int ret;
218991c0f5ecSJin Yao 
219091c0f5ecSJin Yao 	if (!perf_pmu__has_hybrid())
219191c0f5ecSJin Yao 		return;
219291c0f5ecSJin Yao 
219391c0f5ecSJin Yao 	evlist__for_each_entry(evlist, pos) {
219491c0f5ecSJin Yao 		if (!evsel__is_hybrid(pos))
219591c0f5ecSJin Yao 			continue;
219691c0f5ecSJin Yao 
219791c0f5ecSJin Yao 		if (strchr(pos->name, '/'))
219891c0f5ecSJin Yao 			continue;
219991c0f5ecSJin Yao 
220091c0f5ecSJin Yao 		ret = asprintf(&new_name, "%s/%s/",
220191c0f5ecSJin Yao 			       pos->pmu_name, pos->name);
220291c0f5ecSJin Yao 		if (ret) {
220391c0f5ecSJin Yao 			free(pos->name);
220491c0f5ecSJin Yao 			pos->name = new_name;
220591c0f5ecSJin Yao 		}
220691c0f5ecSJin Yao 	}
220791c0f5ecSJin Yao }
220891c0f5ecSJin Yao 
22091e5de7d9SAlexey Bayduraev static int record__terminate_thread(struct record_thread *thread_data)
22101e5de7d9SAlexey Bayduraev {
22111e5de7d9SAlexey Bayduraev 	int err;
22121e5de7d9SAlexey Bayduraev 	enum thread_msg ack = THREAD_MSG__UNDEFINED;
22131e5de7d9SAlexey Bayduraev 	pid_t tid = thread_data->tid;
22141e5de7d9SAlexey Bayduraev 
22151e5de7d9SAlexey Bayduraev 	close(thread_data->pipes.msg[1]);
22161e5de7d9SAlexey Bayduraev 	thread_data->pipes.msg[1] = -1;
22171e5de7d9SAlexey Bayduraev 	err = read(thread_data->pipes.ack[0], &ack, sizeof(ack));
22181e5de7d9SAlexey Bayduraev 	if (err > 0)
22191e5de7d9SAlexey Bayduraev 		pr_debug2("threads[%d]: sent %s\n", tid, thread_msg_tags[ack]);
22201e5de7d9SAlexey Bayduraev 	else
22211e5de7d9SAlexey Bayduraev 		pr_warning("threads[%d]: failed to receive termination notification from %d\n",
22221e5de7d9SAlexey Bayduraev 			   thread->tid, tid);
22231e5de7d9SAlexey Bayduraev 
22241e5de7d9SAlexey Bayduraev 	return 0;
22251e5de7d9SAlexey Bayduraev }
22261e5de7d9SAlexey Bayduraev 
2227396b626bSAlexey Bayduraev static int record__start_threads(struct record *rec)
2228396b626bSAlexey Bayduraev {
22293217e9feSAlexey Bayduraev 	int t, tt, err, ret = 0, nr_threads = rec->nr_threads;
2230396b626bSAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
22313217e9feSAlexey Bayduraev 	sigset_t full, mask;
22323217e9feSAlexey Bayduraev 	pthread_t handle;
22333217e9feSAlexey Bayduraev 	pthread_attr_t attrs;
2234396b626bSAlexey Bayduraev 
2235396b626bSAlexey Bayduraev 	thread = &thread_data[0];
2236396b626bSAlexey Bayduraev 
22373217e9feSAlexey Bayduraev 	if (!record__threads_enabled(rec))
22383217e9feSAlexey Bayduraev 		return 0;
22393217e9feSAlexey Bayduraev 
22403217e9feSAlexey Bayduraev 	sigfillset(&full);
22413217e9feSAlexey Bayduraev 	if (sigprocmask(SIG_SETMASK, &full, &mask)) {
22423217e9feSAlexey Bayduraev 		pr_err("Failed to block signals on threads start: %s\n", strerror(errno));
22433217e9feSAlexey Bayduraev 		return -1;
22443217e9feSAlexey Bayduraev 	}
22453217e9feSAlexey Bayduraev 
22463217e9feSAlexey Bayduraev 	pthread_attr_init(&attrs);
22473217e9feSAlexey Bayduraev 	pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
22483217e9feSAlexey Bayduraev 
22493217e9feSAlexey Bayduraev 	for (t = 1; t < nr_threads; t++) {
22503217e9feSAlexey Bayduraev 		enum thread_msg msg = THREAD_MSG__UNDEFINED;
22513217e9feSAlexey Bayduraev 
22523217e9feSAlexey Bayduraev #ifdef HAVE_PTHREAD_ATTR_SETAFFINITY_NP
22533217e9feSAlexey Bayduraev 		pthread_attr_setaffinity_np(&attrs,
22543217e9feSAlexey Bayduraev 					    MMAP_CPU_MASK_BYTES(&(thread_data[t].mask->affinity)),
22553217e9feSAlexey Bayduraev 					    (cpu_set_t *)(thread_data[t].mask->affinity.bits));
22563217e9feSAlexey Bayduraev #endif
22573217e9feSAlexey Bayduraev 		if (pthread_create(&handle, &attrs, record__thread, &thread_data[t])) {
22583217e9feSAlexey Bayduraev 			for (tt = 1; tt < t; tt++)
22593217e9feSAlexey Bayduraev 				record__terminate_thread(&thread_data[t]);
22603217e9feSAlexey Bayduraev 			pr_err("Failed to start threads: %s\n", strerror(errno));
22613217e9feSAlexey Bayduraev 			ret = -1;
22623217e9feSAlexey Bayduraev 			goto out_err;
22633217e9feSAlexey Bayduraev 		}
22643217e9feSAlexey Bayduraev 
22653217e9feSAlexey Bayduraev 		err = read(thread_data[t].pipes.ack[0], &msg, sizeof(msg));
22663217e9feSAlexey Bayduraev 		if (err > 0)
22673217e9feSAlexey Bayduraev 			pr_debug2("threads[%d]: sent %s\n", rec->thread_data[t].tid,
22683217e9feSAlexey Bayduraev 				  thread_msg_tags[msg]);
22693217e9feSAlexey Bayduraev 		else
22703217e9feSAlexey Bayduraev 			pr_warning("threads[%d]: failed to receive start notification from %d\n",
22713217e9feSAlexey Bayduraev 				   thread->tid, rec->thread_data[t].tid);
22723217e9feSAlexey Bayduraev 	}
22733217e9feSAlexey Bayduraev 
22743217e9feSAlexey Bayduraev 	sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity),
22753217e9feSAlexey Bayduraev 			(cpu_set_t *)thread->mask->affinity.bits);
22763217e9feSAlexey Bayduraev 
2277396b626bSAlexey Bayduraev 	pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu());
2278396b626bSAlexey Bayduraev 
22793217e9feSAlexey Bayduraev out_err:
22803217e9feSAlexey Bayduraev 	pthread_attr_destroy(&attrs);
22813217e9feSAlexey Bayduraev 
22823217e9feSAlexey Bayduraev 	if (sigprocmask(SIG_SETMASK, &mask, NULL)) {
22833217e9feSAlexey Bayduraev 		pr_err("Failed to unblock signals on threads start: %s\n", strerror(errno));
22843217e9feSAlexey Bayduraev 		ret = -1;
22853217e9feSAlexey Bayduraev 	}
22863217e9feSAlexey Bayduraev 
22873217e9feSAlexey Bayduraev 	return ret;
2288396b626bSAlexey Bayduraev }
2289396b626bSAlexey Bayduraev 
2290396b626bSAlexey Bayduraev static int record__stop_threads(struct record *rec)
2291396b626bSAlexey Bayduraev {
2292396b626bSAlexey Bayduraev 	int t;
2293396b626bSAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
2294396b626bSAlexey Bayduraev 
22951e5de7d9SAlexey Bayduraev 	for (t = 1; t < rec->nr_threads; t++)
22961e5de7d9SAlexey Bayduraev 		record__terminate_thread(&thread_data[t]);
22971e5de7d9SAlexey Bayduraev 
2298610fbc01SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++) {
2299396b626bSAlexey Bayduraev 		rec->samples += thread_data[t].samples;
2300610fbc01SAlexey Bayduraev 		if (!record__threads_enabled(rec))
2301610fbc01SAlexey Bayduraev 			continue;
2302610fbc01SAlexey Bayduraev 		rec->session->bytes_transferred += thread_data[t].bytes_transferred;
2303610fbc01SAlexey Bayduraev 		rec->session->bytes_compressed += thread_data[t].bytes_compressed;
2304610fbc01SAlexey Bayduraev 		pr_debug("threads[%d]: samples=%lld, wakes=%ld, ", thread_data[t].tid,
2305610fbc01SAlexey Bayduraev 			 thread_data[t].samples, thread_data[t].waking);
2306610fbc01SAlexey Bayduraev 		if (thread_data[t].bytes_transferred && thread_data[t].bytes_compressed)
2307610fbc01SAlexey Bayduraev 			pr_debug("transferred=%" PRIu64 ", compressed=%" PRIu64 "\n",
2308610fbc01SAlexey Bayduraev 				 thread_data[t].bytes_transferred, thread_data[t].bytes_compressed);
2309610fbc01SAlexey Bayduraev 		else
2310610fbc01SAlexey Bayduraev 			pr_debug("written=%" PRIu64 "\n", thread_data[t].bytes_written);
2311610fbc01SAlexey Bayduraev 	}
2312396b626bSAlexey Bayduraev 
2313396b626bSAlexey Bayduraev 	return 0;
2314396b626bSAlexey Bayduraev }
2315396b626bSAlexey Bayduraev 
2316396b626bSAlexey Bayduraev static unsigned long record__waking(struct record *rec)
2317396b626bSAlexey Bayduraev {
2318396b626bSAlexey Bayduraev 	int t;
2319396b626bSAlexey Bayduraev 	unsigned long waking = 0;
2320396b626bSAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
2321396b626bSAlexey Bayduraev 
2322396b626bSAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++)
2323396b626bSAlexey Bayduraev 		waking += thread_data[t].waking;
2324396b626bSAlexey Bayduraev 
2325396b626bSAlexey Bayduraev 	return waking;
2326396b626bSAlexey Bayduraev }
2327396b626bSAlexey Bayduraev 
23288c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv)
232986470930SIngo Molnar {
233057706abcSDavid Ahern 	int err;
233145604710SNamhyung Kim 	int status = 0;
233246be604bSZhang, Yanmin 	const bool forks = argc > 0;
233345694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = &rec->tool;
2334b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
23358ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
2336d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session;
23376dcf45efSArnaldo Carvalho de Melo 	bool disabled = false, draining = false;
233842aa276fSNamhyung Kim 	int fd;
2339d3c8c08eSAlexey Budankov 	float ratio = 0;
2340acce0223SAlexey Budankov 	enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
234186470930SIngo Molnar 
234245604710SNamhyung Kim 	atexit(record__sig_exit);
2343f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
2344f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
2345804f7ac7SDavid Ahern 	signal(SIGTERM, sig_handler);
2346a074865eSWang Nan 	signal(SIGSEGV, sigsegv_handler);
2347c0bdc1c4SWang Nan 
2348f3b3614aSHari Bathini 	if (rec->opts.record_namespaces)
2349f3b3614aSHari Bathini 		tool->namespace_events = true;
2350f3b3614aSHari Bathini 
23518fb4b679SNamhyung Kim 	if (rec->opts.record_cgroup) {
23528fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE
23538fb4b679SNamhyung Kim 		tool->cgroup_events = true;
23548fb4b679SNamhyung Kim #else
23558fb4b679SNamhyung Kim 		pr_err("cgroup tracking is not supported\n");
23568fb4b679SNamhyung Kim 		return -1;
23578fb4b679SNamhyung Kim #endif
23588fb4b679SNamhyung Kim 	}
23598fb4b679SNamhyung Kim 
2360dc0c6127SJiri Olsa 	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
23612dd6d8a1SAdrian Hunter 		signal(SIGUSR2, snapshot_sig_handler);
23623c1cb7e3SWang Nan 		if (rec->opts.auxtrace_snapshot_mode)
23635f9cf599SWang Nan 			trigger_on(&auxtrace_snapshot_trigger);
2364dc0c6127SJiri Olsa 		if (rec->switch_output.enabled)
23653c1cb7e3SWang Nan 			trigger_on(&switch_output_trigger);
2366c0bdc1c4SWang Nan 	} else {
23672dd6d8a1SAdrian Hunter 		signal(SIGUSR2, SIG_IGN);
2368c0bdc1c4SWang Nan 	}
2369f5970550SPeter Zijlstra 
23702681bd85SNamhyung Kim 	session = perf_session__new(data, tool);
23716ef81c55SMamatha Inamdar 	if (IS_ERR(session)) {
2372ffa91880SAdrien BAK 		pr_err("Perf session creation failed.\n");
23736ef81c55SMamatha Inamdar 		return PTR_ERR(session);
2374a9a70bbcSArnaldo Carvalho de Melo 	}
2375a9a70bbcSArnaldo Carvalho de Melo 
2376b5f2511dSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
2377b5f2511dSAlexey Bayduraev 		if (perf_data__is_pipe(&rec->data)) {
2378b5f2511dSAlexey Bayduraev 			pr_err("Parallel trace streaming is not available in pipe mode.\n");
2379b5f2511dSAlexey Bayduraev 			return -1;
2380b5f2511dSAlexey Bayduraev 		}
2381b5f2511dSAlexey Bayduraev 		if (rec->opts.full_auxtrace) {
2382b5f2511dSAlexey Bayduraev 			pr_err("Parallel trace streaming is not available in AUX area tracing mode.\n");
2383b5f2511dSAlexey Bayduraev 			return -1;
2384b5f2511dSAlexey Bayduraev 		}
2385b5f2511dSAlexey Bayduraev 	}
2386b5f2511dSAlexey Bayduraev 
23878ceb41d7SJiri Olsa 	fd = perf_data__fd(data);
2388d20deb64SArnaldo Carvalho de Melo 	rec->session = session;
2389d20deb64SArnaldo Carvalho de Melo 
23905d7f4116SAlexey Budankov 	if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) {
23915d7f4116SAlexey Budankov 		pr_err("Compression initialization failed.\n");
23925d7f4116SAlexey Budankov 		return -1;
23935d7f4116SAlexey Budankov 	}
2394da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
2395da231338SAnand K Mistry 	done_fd = eventfd(0, EFD_NONBLOCK);
2396da231338SAnand K Mistry 	if (done_fd < 0) {
2397da231338SAnand K Mistry 		pr_err("Failed to create wakeup eventfd, error: %m\n");
2398da231338SAnand K Mistry 		status = -1;
2399da231338SAnand K Mistry 		goto out_delete_session;
2400da231338SAnand K Mistry 	}
2401e16c2ce7SYang Jihong 	err = evlist__add_wakeup_eventfd(rec->evlist, done_fd);
2402da231338SAnand K Mistry 	if (err < 0) {
2403da231338SAnand K Mistry 		pr_err("Failed to add wakeup eventfd to poll list\n");
2404da231338SAnand K Mistry 		status = err;
2405da231338SAnand K Mistry 		goto out_delete_session;
2406da231338SAnand K Mistry 	}
2407da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT
24085d7f4116SAlexey Budankov 
24095d7f4116SAlexey Budankov 	session->header.env.comp_type  = PERF_COMP_ZSTD;
24105d7f4116SAlexey Budankov 	session->header.env.comp_level = rec->opts.comp_level;
24115d7f4116SAlexey Budankov 
2412eeb399b5SAdrian Hunter 	if (rec->opts.kcore &&
2413eeb399b5SAdrian Hunter 	    !record__kcore_readable(&session->machines.host)) {
2414eeb399b5SAdrian Hunter 		pr_err("ERROR: kcore is not readable.\n");
2415eeb399b5SAdrian Hunter 		return -1;
2416eeb399b5SAdrian Hunter 	}
2417eeb399b5SAdrian Hunter 
2418d1e325cfSJiri Olsa 	if (record__init_clock(rec))
2419d1e325cfSJiri Olsa 		return -1;
2420d1e325cfSJiri Olsa 
24218c6f45a7SArnaldo Carvalho de Melo 	record__init_features(rec);
2422330aa675SStephane Eranian 
2423d4db3f16SArnaldo Carvalho de Melo 	if (forks) {
24247b392ef0SArnaldo Carvalho de Melo 		err = evlist__prepare_workload(rec->evlist, &opts->target, argv, data->is_pipe,
2425735f7e0bSArnaldo Carvalho de Melo 					       workload_exec_failed_signal);
242635b9d88eSArnaldo Carvalho de Melo 		if (err < 0) {
242735b9d88eSArnaldo Carvalho de Melo 			pr_err("Couldn't run the workload!\n");
242845604710SNamhyung Kim 			status = err;
242935b9d88eSArnaldo Carvalho de Melo 			goto out_delete_session;
2430856e9660SPeter Zijlstra 		}
2431856e9660SPeter Zijlstra 	}
2432856e9660SPeter Zijlstra 
2433ad46e48cSJiri Olsa 	/*
2434ad46e48cSJiri Olsa 	 * If we have just single event and are sending data
2435ad46e48cSJiri Olsa 	 * through pipe, we need to force the ids allocation,
2436ad46e48cSJiri Olsa 	 * because we synthesize event name through the pipe
2437ad46e48cSJiri Olsa 	 * and need the id for that.
2438ad46e48cSJiri Olsa 	 */
24396484d2f9SJiri Olsa 	if (data->is_pipe && rec->evlist->core.nr_entries == 1)
2440ad46e48cSJiri Olsa 		rec->opts.sample_id = true;
2441ad46e48cSJiri Olsa 
244291c0f5ecSJin Yao 	record__uniquify_name(rec);
244391c0f5ecSJin Yao 
2444da406202SAdrian Hunter 	/* Debug message used by test scripts */
2445da406202SAdrian Hunter 	pr_debug3("perf record opening and mmapping events\n");
24468c6f45a7SArnaldo Carvalho de Melo 	if (record__open(rec) != 0) {
24478d3eca20SDavid Ahern 		err = -1;
2448396b626bSAlexey Bayduraev 		goto out_free_threads;
24498d3eca20SDavid Ahern 	}
2450da406202SAdrian Hunter 	/* Debug message used by test scripts */
2451da406202SAdrian Hunter 	pr_debug3("perf record done opening and mmapping events\n");
2452f6fa4375SJiri Olsa 	session->header.env.comp_mmap_len = session->evlist->core.mmap_len;
245386470930SIngo Molnar 
2454eeb399b5SAdrian Hunter 	if (rec->opts.kcore) {
2455eeb399b5SAdrian Hunter 		err = record__kcore_copy(&session->machines.host, data);
2456eeb399b5SAdrian Hunter 		if (err) {
2457eeb399b5SAdrian Hunter 			pr_err("ERROR: Failed to copy kcore\n");
2458396b626bSAlexey Bayduraev 			goto out_free_threads;
2459eeb399b5SAdrian Hunter 		}
2460eeb399b5SAdrian Hunter 	}
2461eeb399b5SAdrian Hunter 
24628690a2a7SWang Nan 	err = bpf__apply_obj_config();
24638690a2a7SWang Nan 	if (err) {
24648690a2a7SWang Nan 		char errbuf[BUFSIZ];
24658690a2a7SWang Nan 
24668690a2a7SWang Nan 		bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
24678690a2a7SWang Nan 		pr_err("ERROR: Apply config to BPF failed: %s\n",
24688690a2a7SWang Nan 			 errbuf);
2469396b626bSAlexey Bayduraev 		goto out_free_threads;
24708690a2a7SWang Nan 	}
24718690a2a7SWang Nan 
2472cca8482cSAdrian Hunter 	/*
2473cca8482cSAdrian Hunter 	 * Normally perf_session__new would do this, but it doesn't have the
2474cca8482cSAdrian Hunter 	 * evlist.
2475cca8482cSAdrian Hunter 	 */
24768cedf3a5SArnaldo Carvalho de Melo 	if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) {
2477cca8482cSAdrian Hunter 		pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
2478cca8482cSAdrian Hunter 		rec->tool.ordered_events = false;
2479cca8482cSAdrian Hunter 	}
2480cca8482cSAdrian Hunter 
24813a683120SJiri Olsa 	if (!rec->evlist->core.nr_groups)
2482a8bb559bSNamhyung Kim 		perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
2483a8bb559bSNamhyung Kim 
24848ceb41d7SJiri Olsa 	if (data->is_pipe) {
248542aa276fSNamhyung Kim 		err = perf_header__write_pipe(fd);
2486529870e3STom Zanussi 		if (err < 0)
2487396b626bSAlexey Bayduraev 			goto out_free_threads;
2488563aecb2SJiri Olsa 	} else {
248942aa276fSNamhyung Kim 		err = perf_session__write_header(session, rec->evlist, fd, false);
2490d5eed904SArnaldo Carvalho de Melo 		if (err < 0)
2491396b626bSAlexey Bayduraev 			goto out_free_threads;
2492d5eed904SArnaldo Carvalho de Melo 	}
24937c6a1c65SPeter Zijlstra 
2494b38d85efSArnaldo Carvalho de Melo 	err = -1;
2495d3665498SDavid Ahern 	if (!rec->no_buildid
2496e20960c0SRobert Richter 	    && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
2497d3665498SDavid Ahern 		pr_err("Couldn't generate buildids. "
2498e20960c0SRobert Richter 		       "Use --no-buildid to profile anyway.\n");
2499396b626bSAlexey Bayduraev 		goto out_free_threads;
2500e20960c0SRobert Richter 	}
2501e20960c0SRobert Richter 
250223cbb41cSArnaldo Carvalho de Melo 	err = record__setup_sb_evlist(rec);
250323cbb41cSArnaldo Carvalho de Melo 	if (err)
2504396b626bSAlexey Bayduraev 		goto out_free_threads;
2505657ee553SSong Liu 
25064ea648aeSWang Nan 	err = record__synthesize(rec, false);
2507c45c86ebSWang Nan 	if (err < 0)
2508396b626bSAlexey Bayduraev 		goto out_free_threads;
25098d3eca20SDavid Ahern 
2510d20deb64SArnaldo Carvalho de Melo 	if (rec->realtime_prio) {
251186470930SIngo Molnar 		struct sched_param param;
251286470930SIngo Molnar 
2513d20deb64SArnaldo Carvalho de Melo 		param.sched_priority = rec->realtime_prio;
251486470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
25156beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
25168d3eca20SDavid Ahern 			err = -1;
2517396b626bSAlexey Bayduraev 			goto out_free_threads;
251886470930SIngo Molnar 		}
251986470930SIngo Molnar 	}
252086470930SIngo Molnar 
2521396b626bSAlexey Bayduraev 	if (record__start_threads(rec))
2522396b626bSAlexey Bayduraev 		goto out_free_threads;
2523396b626bSAlexey Bayduraev 
2524774cb499SJiri Olsa 	/*
2525774cb499SJiri Olsa 	 * When perf is starting the traced process, all the events
2526774cb499SJiri Olsa 	 * (apart from group members) have enable_on_exec=1 set,
2527774cb499SJiri Olsa 	 * so don't spoil it by prematurely enabling them.
2528774cb499SJiri Olsa 	 */
25296619a53eSAndi Kleen 	if (!target__none(&opts->target) && !opts->initial_delay)
25301c87f165SJiri Olsa 		evlist__enable(rec->evlist);
2531764e16a3SDavid Ahern 
2532856e9660SPeter Zijlstra 	/*
2533856e9660SPeter Zijlstra 	 * Let the child rip
2534856e9660SPeter Zijlstra 	 */
2535e803cf97SNamhyung Kim 	if (forks) {
253620a8a3cfSJiri Olsa 		struct machine *machine = &session->machines.host;
2537e5bed564SNamhyung Kim 		union perf_event *event;
2538e907caf3SHari Bathini 		pid_t tgid;
2539e5bed564SNamhyung Kim 
2540e5bed564SNamhyung Kim 		event = malloc(sizeof(event->comm) + machine->id_hdr_size);
2541e5bed564SNamhyung Kim 		if (event == NULL) {
2542e5bed564SNamhyung Kim 			err = -ENOMEM;
2543e5bed564SNamhyung Kim 			goto out_child;
2544e5bed564SNamhyung Kim 		}
2545e5bed564SNamhyung Kim 
2546e803cf97SNamhyung Kim 		/*
2547e803cf97SNamhyung Kim 		 * Some H/W events are generated before COMM event
2548e803cf97SNamhyung Kim 		 * which is emitted during exec(), so perf script
2549e803cf97SNamhyung Kim 		 * cannot see a correct process name for those events.
2550e803cf97SNamhyung Kim 		 * Synthesize COMM event to prevent it.
2551e803cf97SNamhyung Kim 		 */
2552e907caf3SHari Bathini 		tgid = perf_event__synthesize_comm(tool, event,
2553e803cf97SNamhyung Kim 						   rec->evlist->workload.pid,
2554e803cf97SNamhyung Kim 						   process_synthesized_event,
2555e803cf97SNamhyung Kim 						   machine);
2556e5bed564SNamhyung Kim 		free(event);
2557e803cf97SNamhyung Kim 
2558e907caf3SHari Bathini 		if (tgid == -1)
2559e907caf3SHari Bathini 			goto out_child;
2560e907caf3SHari Bathini 
2561e907caf3SHari Bathini 		event = malloc(sizeof(event->namespaces) +
2562e907caf3SHari Bathini 			       (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
2563e907caf3SHari Bathini 			       machine->id_hdr_size);
2564e907caf3SHari Bathini 		if (event == NULL) {
2565e907caf3SHari Bathini 			err = -ENOMEM;
2566e907caf3SHari Bathini 			goto out_child;
2567e907caf3SHari Bathini 		}
2568e907caf3SHari Bathini 
2569e907caf3SHari Bathini 		/*
2570e907caf3SHari Bathini 		 * Synthesize NAMESPACES event for the command specified.
2571e907caf3SHari Bathini 		 */
2572e907caf3SHari Bathini 		perf_event__synthesize_namespaces(tool, event,
2573e907caf3SHari Bathini 						  rec->evlist->workload.pid,
2574e907caf3SHari Bathini 						  tgid, process_synthesized_event,
2575e907caf3SHari Bathini 						  machine);
2576e907caf3SHari Bathini 		free(event);
2577e907caf3SHari Bathini 
25787b392ef0SArnaldo Carvalho de Melo 		evlist__start_workload(rec->evlist);
2579e803cf97SNamhyung Kim 	}
2580856e9660SPeter Zijlstra 
25816619a53eSAndi Kleen 	if (opts->initial_delay) {
258268cd3b45SAlexey Budankov 		pr_info(EVLIST_DISABLED_MSG);
258368cd3b45SAlexey Budankov 		if (opts->initial_delay > 0) {
25840693e680SArnaldo Carvalho de Melo 			usleep(opts->initial_delay * USEC_PER_MSEC);
25851c87f165SJiri Olsa 			evlist__enable(rec->evlist);
258668cd3b45SAlexey Budankov 			pr_info(EVLIST_ENABLED_MSG);
258768cd3b45SAlexey Budankov 		}
25886619a53eSAndi Kleen 	}
25896619a53eSAndi Kleen 
25906657a099SAdrian Hunter 	err = event_enable_timer__start(rec->evlist->eet);
25916657a099SAdrian Hunter 	if (err)
25926657a099SAdrian Hunter 		goto out_child;
25936657a099SAdrian Hunter 
2594da406202SAdrian Hunter 	/* Debug message used by test scripts */
2595da406202SAdrian Hunter 	pr_debug3("perf record has started\n");
2596da406202SAdrian Hunter 	fflush(stderr);
2597da406202SAdrian Hunter 
25985f9cf599SWang Nan 	trigger_ready(&auxtrace_snapshot_trigger);
25993c1cb7e3SWang Nan 	trigger_ready(&switch_output_trigger);
2600a074865eSWang Nan 	perf_hooks__invoke_record_start();
26013812d298SAdrian Hunter 
26023812d298SAdrian Hunter 	/*
26033812d298SAdrian Hunter 	 * Must write FINISHED_INIT so it will be seen after all other
26043812d298SAdrian Hunter 	 * synthesized user events, but before any regular events.
26053812d298SAdrian Hunter 	 */
26063812d298SAdrian Hunter 	err = write_finished_init(rec, false);
26073812d298SAdrian Hunter 	if (err < 0)
26083812d298SAdrian Hunter 		goto out_child;
26093812d298SAdrian Hunter 
2610649c48a9SPeter Zijlstra 	for (;;) {
2611396b626bSAlexey Bayduraev 		unsigned long long hits = thread->samples;
261286470930SIngo Molnar 
261305737464SWang Nan 		/*
261405737464SWang Nan 		 * rec->evlist->bkw_mmap_state is possible to be
261505737464SWang Nan 		 * BKW_MMAP_EMPTY here: when done == true and
261605737464SWang Nan 		 * hits != rec->samples in previous round.
261705737464SWang Nan 		 *
2618ade9d208SArnaldo Carvalho de Melo 		 * evlist__toggle_bkw_mmap ensure we never
261905737464SWang Nan 		 * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
262005737464SWang Nan 		 */
262105737464SWang Nan 		if (trigger_is_hit(&switch_output_trigger) || done || draining)
2622ade9d208SArnaldo Carvalho de Melo 			evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
262305737464SWang Nan 
2624470530bbSAlexey Budankov 		if (record__mmap_read_all(rec, false) < 0) {
26255f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
26263c1cb7e3SWang Nan 			trigger_error(&switch_output_trigger);
26278d3eca20SDavid Ahern 			err = -1;
262845604710SNamhyung Kim 			goto out_child;
26298d3eca20SDavid Ahern 		}
263086470930SIngo Molnar 
26312dd6d8a1SAdrian Hunter 		if (auxtrace_record__snapshot_started) {
26322dd6d8a1SAdrian Hunter 			auxtrace_record__snapshot_started = 0;
26335f9cf599SWang Nan 			if (!trigger_is_error(&auxtrace_snapshot_trigger))
2634ce7b0e42SAlexander Shishkin 				record__read_auxtrace_snapshot(rec, false);
26355f9cf599SWang Nan 			if (trigger_is_error(&auxtrace_snapshot_trigger)) {
26362dd6d8a1SAdrian Hunter 				pr_err("AUX area tracing snapshot failed\n");
26372dd6d8a1SAdrian Hunter 				err = -1;
26382dd6d8a1SAdrian Hunter 				goto out_child;
26392dd6d8a1SAdrian Hunter 			}
26402dd6d8a1SAdrian Hunter 		}
26412dd6d8a1SAdrian Hunter 
26423c1cb7e3SWang Nan 		if (trigger_is_hit(&switch_output_trigger)) {
264305737464SWang Nan 			/*
264405737464SWang Nan 			 * If switch_output_trigger is hit, the data in
264505737464SWang Nan 			 * overwritable ring buffer should have been collected,
264605737464SWang Nan 			 * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
264705737464SWang Nan 			 *
264805737464SWang Nan 			 * If SIGUSR2 raise after or during record__mmap_read_all(),
264905737464SWang Nan 			 * record__mmap_read_all() didn't collect data from
265005737464SWang Nan 			 * overwritable ring buffer. Read again.
265105737464SWang Nan 			 */
265205737464SWang Nan 			if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
265305737464SWang Nan 				continue;
26543c1cb7e3SWang Nan 			trigger_ready(&switch_output_trigger);
26553c1cb7e3SWang Nan 
265605737464SWang Nan 			/*
265705737464SWang Nan 			 * Reenable events in overwrite ring buffer after
265805737464SWang Nan 			 * record__mmap_read_all(): we should have collected
265905737464SWang Nan 			 * data from it.
266005737464SWang Nan 			 */
2661ade9d208SArnaldo Carvalho de Melo 			evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
266205737464SWang Nan 
26633c1cb7e3SWang Nan 			if (!quiet)
26643c1cb7e3SWang Nan 				fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
2665396b626bSAlexey Bayduraev 					record__waking(rec));
2666396b626bSAlexey Bayduraev 			thread->waking = 0;
26673c1cb7e3SWang Nan 			fd = record__switch_output(rec, false);
26683c1cb7e3SWang Nan 			if (fd < 0) {
26693c1cb7e3SWang Nan 				pr_err("Failed to switch to new file\n");
26703c1cb7e3SWang Nan 				trigger_error(&switch_output_trigger);
26713c1cb7e3SWang Nan 				err = fd;
26723c1cb7e3SWang Nan 				goto out_child;
26733c1cb7e3SWang Nan 			}
2674bfacbe3bSJiri Olsa 
2675bfacbe3bSJiri Olsa 			/* re-arm the alarm */
2676bfacbe3bSJiri Olsa 			if (rec->switch_output.time)
2677bfacbe3bSJiri Olsa 				alarm(rec->switch_output.time);
26783c1cb7e3SWang Nan 		}
26793c1cb7e3SWang Nan 
2680396b626bSAlexey Bayduraev 		if (hits == thread->samples) {
26816dcf45efSArnaldo Carvalho de Melo 			if (done || draining)
2682649c48a9SPeter Zijlstra 				break;
2683396b626bSAlexey Bayduraev 			err = fdarray__poll(&thread->pollfd, -1);
2684a515114fSJiri Olsa 			/*
2685a515114fSJiri Olsa 			 * Propagate error, only if there's any. Ignore positive
2686a515114fSJiri Olsa 			 * number of returned events and interrupt error.
2687a515114fSJiri Olsa 			 */
2688a515114fSJiri Olsa 			if (err > 0 || (err < 0 && errno == EINTR))
268945604710SNamhyung Kim 				err = 0;
2690396b626bSAlexey Bayduraev 			thread->waking++;
26916dcf45efSArnaldo Carvalho de Melo 
2692396b626bSAlexey Bayduraev 			if (fdarray__filter(&thread->pollfd, POLLERR | POLLHUP,
2693396b626bSAlexey Bayduraev 					    record__thread_munmap_filtered, NULL) == 0)
26946dcf45efSArnaldo Carvalho de Melo 				draining = true;
2695396b626bSAlexey Bayduraev 
26966562c9acSAdrian Hunter 			err = record__update_evlist_pollfd_from_thread(rec, rec->evlist, thread);
26976562c9acSAdrian Hunter 			if (err)
26986562c9acSAdrian Hunter 				goto out_child;
26998b412664SPeter Zijlstra 		}
27008b412664SPeter Zijlstra 
2701acce0223SAlexey Budankov 		if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) {
2702acce0223SAlexey Budankov 			switch (cmd) {
2703d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2704d20aff15SAdrian Hunter 				hit_auxtrace_snapshot_trigger(rec);
2705d20aff15SAdrian Hunter 				evlist__ctlfd_ack(rec->evlist);
2706d20aff15SAdrian Hunter 				break;
2707f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
2708f186cd61SJiri Olsa 				done = 1;
2709f186cd61SJiri Olsa 				break;
2710acce0223SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2711acce0223SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2712991ae4ebSJiri Olsa 			case EVLIST_CTL_CMD_ENABLE:
2713991ae4ebSJiri Olsa 			case EVLIST_CTL_CMD_DISABLE:
2714142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
271547fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2716acce0223SAlexey Budankov 			default:
2717acce0223SAlexey Budankov 				break;
2718acce0223SAlexey Budankov 			}
2719acce0223SAlexey Budankov 		}
2720acce0223SAlexey Budankov 
27216657a099SAdrian Hunter 		err = event_enable_timer__process(rec->evlist->eet);
27226657a099SAdrian Hunter 		if (err < 0)
27236657a099SAdrian Hunter 			goto out_child;
27246657a099SAdrian Hunter 		if (err) {
27256657a099SAdrian Hunter 			err = 0;
27266657a099SAdrian Hunter 			done = 1;
27276657a099SAdrian Hunter 		}
27286657a099SAdrian Hunter 
2729774cb499SJiri Olsa 		/*
2730774cb499SJiri Olsa 		 * When perf is starting the traced process, at the end events
2731774cb499SJiri Olsa 		 * die with the process and we wait for that. Thus no need to
2732774cb499SJiri Olsa 		 * disable events in this case.
2733774cb499SJiri Olsa 		 */
2734602ad878SArnaldo Carvalho de Melo 		if (done && !disabled && !target__none(&opts->target)) {
27355f9cf599SWang Nan 			trigger_off(&auxtrace_snapshot_trigger);
2736e74676deSJiri Olsa 			evlist__disable(rec->evlist);
27372711926aSJiri Olsa 			disabled = true;
27382711926aSJiri Olsa 		}
27398b412664SPeter Zijlstra 	}
2740ce7b0e42SAlexander Shishkin 
27415f9cf599SWang Nan 	trigger_off(&auxtrace_snapshot_trigger);
27423c1cb7e3SWang Nan 	trigger_off(&switch_output_trigger);
27438b412664SPeter Zijlstra 
2744ce7b0e42SAlexander Shishkin 	if (opts->auxtrace_snapshot_on_exit)
2745ce7b0e42SAlexander Shishkin 		record__auxtrace_snapshot_exit(rec);
2746ce7b0e42SAlexander Shishkin 
2747f33cbe72SArnaldo Carvalho de Melo 	if (forks && workload_exec_errno) {
27483535a696SArnaldo Carvalho de Melo 		char msg[STRERR_BUFSIZE], strevsels[2048];
2749c8b5f2c9SArnaldo Carvalho de Melo 		const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
27503535a696SArnaldo Carvalho de Melo 
27513535a696SArnaldo Carvalho de Melo 		evlist__scnprintf_evsels(rec->evlist, sizeof(strevsels), strevsels);
27523535a696SArnaldo Carvalho de Melo 
27533535a696SArnaldo Carvalho de Melo 		pr_err("Failed to collect '%s' for the '%s' workload: %s\n",
27543535a696SArnaldo Carvalho de Melo 			strevsels, argv[0], emsg);
2755f33cbe72SArnaldo Carvalho de Melo 		err = -1;
275645604710SNamhyung Kim 		goto out_child;
2757f33cbe72SArnaldo Carvalho de Melo 	}
2758f33cbe72SArnaldo Carvalho de Melo 
2759e3d59112SNamhyung Kim 	if (!quiet)
2760396b626bSAlexey Bayduraev 		fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n",
2761396b626bSAlexey Bayduraev 			record__waking(rec));
276286470930SIngo Molnar 
27633812d298SAdrian Hunter 	write_finished_init(rec, true);
27643812d298SAdrian Hunter 
27654ea648aeSWang Nan 	if (target__none(&rec->opts.target))
27664ea648aeSWang Nan 		record__synthesize_workload(rec, true);
27674ea648aeSWang Nan 
276845604710SNamhyung Kim out_child:
2769396b626bSAlexey Bayduraev 	record__stop_threads(rec);
2770470530bbSAlexey Budankov 	record__mmap_read_all(rec, true);
2771396b626bSAlexey Bayduraev out_free_threads:
2772415ccb58SAlexey Bayduraev 	record__free_thread_data(rec);
2773396b626bSAlexey Bayduraev 	evlist__finalize_ctlfd(rec->evlist);
2774d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
2775d3d1af6fSAlexey Budankov 
2776d3c8c08eSAlexey Budankov 	if (rec->session->bytes_transferred && rec->session->bytes_compressed) {
2777d3c8c08eSAlexey Budankov 		ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed;
2778d3c8c08eSAlexey Budankov 		session->header.env.comp_ratio = ratio + 0.5;
2779d3c8c08eSAlexey Budankov 	}
2780d3c8c08eSAlexey Budankov 
278145604710SNamhyung Kim 	if (forks) {
278245604710SNamhyung Kim 		int exit_status;
278345604710SNamhyung Kim 
278445604710SNamhyung Kim 		if (!child_finished)
278545604710SNamhyung Kim 			kill(rec->evlist->workload.pid, SIGTERM);
278645604710SNamhyung Kim 
278745604710SNamhyung Kim 		wait(&exit_status);
278845604710SNamhyung Kim 
278945604710SNamhyung Kim 		if (err < 0)
279045604710SNamhyung Kim 			status = err;
279145604710SNamhyung Kim 		else if (WIFEXITED(exit_status))
279245604710SNamhyung Kim 			status = WEXITSTATUS(exit_status);
279345604710SNamhyung Kim 		else if (WIFSIGNALED(exit_status))
279445604710SNamhyung Kim 			signr = WTERMSIG(exit_status);
279545604710SNamhyung Kim 	} else
279645604710SNamhyung Kim 		status = err;
279745604710SNamhyung Kim 
2798edc41a10SNamhyung Kim 	if (rec->off_cpu)
2799edc41a10SNamhyung Kim 		rec->bytes_written += off_cpu_write(rec->session);
2800edc41a10SNamhyung Kim 
2801e3a23261SNamhyung Kim 	record__read_lost_samples(rec);
28024ea648aeSWang Nan 	record__synthesize(rec, true);
2803e3d59112SNamhyung Kim 	/* this will be recalculated during process_buildids() */
2804e3d59112SNamhyung Kim 	rec->samples = 0;
2805e3d59112SNamhyung Kim 
2806ecfd7a9cSWang Nan 	if (!err) {
2807ecfd7a9cSWang Nan 		if (!rec->timestamp_filename) {
2808e1ab48baSWang Nan 			record__finish_output(rec);
2809ecfd7a9cSWang Nan 		} else {
2810ecfd7a9cSWang Nan 			fd = record__switch_output(rec, true);
2811ecfd7a9cSWang Nan 			if (fd < 0) {
2812ecfd7a9cSWang Nan 				status = fd;
2813ecfd7a9cSWang Nan 				goto out_delete_session;
2814ecfd7a9cSWang Nan 			}
2815ecfd7a9cSWang Nan 		}
2816ecfd7a9cSWang Nan 	}
281739d17dacSArnaldo Carvalho de Melo 
2818a074865eSWang Nan 	perf_hooks__invoke_record_end();
2819a074865eSWang Nan 
2820e3d59112SNamhyung Kim 	if (!err && !quiet) {
2821e3d59112SNamhyung Kim 		char samples[128];
2822ecfd7a9cSWang Nan 		const char *postfix = rec->timestamp_filename ?
2823ecfd7a9cSWang Nan 					".<timestamp>" : "";
2824e3d59112SNamhyung Kim 
2825ef149c25SAdrian Hunter 		if (rec->samples && !rec->opts.full_auxtrace)
2826e3d59112SNamhyung Kim 			scnprintf(samples, sizeof(samples),
2827e3d59112SNamhyung Kim 				  " (%" PRIu64 " samples)", rec->samples);
2828e3d59112SNamhyung Kim 		else
2829e3d59112SNamhyung Kim 			samples[0] = '\0';
2830e3d59112SNamhyung Kim 
2831d3c8c08eSAlexey Budankov 		fprintf(stderr,	"[ perf record: Captured and wrote %.3f MB %s%s%s",
28328ceb41d7SJiri Olsa 			perf_data__size(data) / 1024.0 / 1024.0,
28332d4f2799SJiri Olsa 			data->path, postfix, samples);
2834d3c8c08eSAlexey Budankov 		if (ratio) {
2835d3c8c08eSAlexey Budankov 			fprintf(stderr,	", compressed (original %.3f MB, ratio is %.3f)",
2836d3c8c08eSAlexey Budankov 					rec->session->bytes_transferred / 1024.0 / 1024.0,
2837d3c8c08eSAlexey Budankov 					ratio);
2838d3c8c08eSAlexey Budankov 		}
2839d3c8c08eSAlexey Budankov 		fprintf(stderr, " ]\n");
2840e3d59112SNamhyung Kim 	}
2841e3d59112SNamhyung Kim 
284239d17dacSArnaldo Carvalho de Melo out_delete_session:
2843da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
2844304f0a2fSIan Rogers 	if (done_fd >= 0) {
2845304f0a2fSIan Rogers 		fd = done_fd;
2846304f0a2fSIan Rogers 		done_fd = -1;
2847304f0a2fSIan Rogers 
2848304f0a2fSIan Rogers 		close(fd);
2849304f0a2fSIan Rogers 	}
2850da231338SAnand K Mistry #endif
28515d7f4116SAlexey Budankov 	zstd_fini(&session->zstd_data);
285239d17dacSArnaldo Carvalho de Melo 	perf_session__delete(session);
2853657ee553SSong Liu 
2854657ee553SSong Liu 	if (!opts->no_bpf_event)
285508c83997SArnaldo Carvalho de Melo 		evlist__stop_sb_thread(rec->sb_evlist);
285645604710SNamhyung Kim 	return status;
285786470930SIngo Molnar }
285886470930SIngo Molnar 
28590883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain)
286009b0fd45SJiri Olsa {
2861aad2b21cSKan Liang 	static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
2862a601fdffSJiri Olsa 
28630883e820SArnaldo Carvalho de Melo 	pr_debug("callchain: type %s\n", str[callchain->record_mode]);
286426d33022SJiri Olsa 
28650883e820SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_DWARF)
286609b0fd45SJiri Olsa 		pr_debug("callchain: stack dump size %d\n",
28670883e820SArnaldo Carvalho de Melo 			 callchain->dump_size);
28680883e820SArnaldo Carvalho de Melo }
28690883e820SArnaldo Carvalho de Melo 
28700883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record,
28710883e820SArnaldo Carvalho de Melo 				 struct callchain_param *callchain,
28720883e820SArnaldo Carvalho de Melo 				 const char *arg, bool unset)
28730883e820SArnaldo Carvalho de Melo {
28740883e820SArnaldo Carvalho de Melo 	int ret;
28750883e820SArnaldo Carvalho de Melo 	callchain->enabled = !unset;
28760883e820SArnaldo Carvalho de Melo 
28770883e820SArnaldo Carvalho de Melo 	/* --no-call-graph */
28780883e820SArnaldo Carvalho de Melo 	if (unset) {
28790883e820SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_NONE;
28800883e820SArnaldo Carvalho de Melo 		pr_debug("callchain: disabled\n");
28810883e820SArnaldo Carvalho de Melo 		return 0;
28820883e820SArnaldo Carvalho de Melo 	}
28830883e820SArnaldo Carvalho de Melo 
28840883e820SArnaldo Carvalho de Melo 	ret = parse_callchain_record_opt(arg, callchain);
28850883e820SArnaldo Carvalho de Melo 	if (!ret) {
28860883e820SArnaldo Carvalho de Melo 		/* Enable data address sampling for DWARF unwind. */
28870883e820SArnaldo Carvalho de Melo 		if (callchain->record_mode == CALLCHAIN_DWARF)
28880883e820SArnaldo Carvalho de Melo 			record->sample_address = true;
28890883e820SArnaldo Carvalho de Melo 		callchain_debug(callchain);
28900883e820SArnaldo Carvalho de Melo 	}
28910883e820SArnaldo Carvalho de Melo 
28920883e820SArnaldo Carvalho de Melo 	return ret;
289309b0fd45SJiri Olsa }
289409b0fd45SJiri Olsa 
2895c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt,
289609b0fd45SJiri Olsa 			       const char *arg,
289709b0fd45SJiri Olsa 			       int unset)
289809b0fd45SJiri Olsa {
28990883e820SArnaldo Carvalho de Melo 	return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
290026d33022SJiri Olsa }
290126d33022SJiri Olsa 
2902c421e80bSKan Liang int record_callchain_opt(const struct option *opt,
290309b0fd45SJiri Olsa 			 const char *arg __maybe_unused,
290409b0fd45SJiri Olsa 			 int unset __maybe_unused)
290509b0fd45SJiri Olsa {
29062ddd5c04SArnaldo Carvalho de Melo 	struct callchain_param *callchain = opt->value;
2907c421e80bSKan Liang 
29082ddd5c04SArnaldo Carvalho de Melo 	callchain->enabled = true;
290909b0fd45SJiri Olsa 
29102ddd5c04SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_NONE)
29112ddd5c04SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_FP;
2912eb853e80SJiri Olsa 
29132ddd5c04SArnaldo Carvalho de Melo 	callchain_debug(callchain);
291409b0fd45SJiri Olsa 	return 0;
291509b0fd45SJiri Olsa }
291609b0fd45SJiri Olsa 
2917eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb)
2918eb853e80SJiri Olsa {
29197a29c087SNamhyung Kim 	struct record *rec = cb;
29207a29c087SNamhyung Kim 
29217a29c087SNamhyung Kim 	if (!strcmp(var, "record.build-id")) {
29227a29c087SNamhyung Kim 		if (!strcmp(value, "cache"))
29237a29c087SNamhyung Kim 			rec->no_buildid_cache = false;
29247a29c087SNamhyung Kim 		else if (!strcmp(value, "no-cache"))
29257a29c087SNamhyung Kim 			rec->no_buildid_cache = true;
29267a29c087SNamhyung Kim 		else if (!strcmp(value, "skip"))
29277a29c087SNamhyung Kim 			rec->no_buildid = true;
2928e29386c8SJiri Olsa 		else if (!strcmp(value, "mmap"))
2929e29386c8SJiri Olsa 			rec->buildid_mmap = true;
29307a29c087SNamhyung Kim 		else
29317a29c087SNamhyung Kim 			return -1;
29327a29c087SNamhyung Kim 		return 0;
29337a29c087SNamhyung Kim 	}
2934cff17205SYisheng Xie 	if (!strcmp(var, "record.call-graph")) {
2935cff17205SYisheng Xie 		var = "call-graph.record-mode";
2936eb853e80SJiri Olsa 		return perf_default_config(var, value, cb);
2937eb853e80SJiri Olsa 	}
293893f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
293993f20c0fSAlexey Budankov 	if (!strcmp(var, "record.aio")) {
294093f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = strtol(value, NULL, 0);
294193f20c0fSAlexey Budankov 		if (!rec->opts.nr_cblocks)
294293f20c0fSAlexey Budankov 			rec->opts.nr_cblocks = nr_cblocks_default;
294393f20c0fSAlexey Budankov 	}
294493f20c0fSAlexey Budankov #endif
29459bce13eaSJiri Olsa 	if (!strcmp(var, "record.debuginfod")) {
29469bce13eaSJiri Olsa 		rec->debuginfod.urls = strdup(value);
29479bce13eaSJiri Olsa 		if (!rec->debuginfod.urls)
29489bce13eaSJiri Olsa 			return -ENOMEM;
29499bce13eaSJiri Olsa 		rec->debuginfod.set = true;
29509bce13eaSJiri Olsa 	}
2951eb853e80SJiri Olsa 
2952cff17205SYisheng Xie 	return 0;
2953cff17205SYisheng Xie }
2954cff17205SYisheng Xie 
29556657a099SAdrian Hunter static int record__parse_event_enable_time(const struct option *opt, const char *str, int unset)
29566657a099SAdrian Hunter {
29576657a099SAdrian Hunter 	struct record *rec = (struct record *)opt->value;
29586657a099SAdrian Hunter 
29596657a099SAdrian Hunter 	return evlist__parse_event_enable_time(rec->evlist, &rec->opts, str, unset);
29606657a099SAdrian Hunter }
2961814c8c38SPeter Zijlstra 
2962f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset)
2963f4fe11b7SAlexey Budankov {
2964f4fe11b7SAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
2965f4fe11b7SAlexey Budankov 
2966f4fe11b7SAlexey Budankov 	if (unset || !str)
2967f4fe11b7SAlexey Budankov 		return 0;
2968f4fe11b7SAlexey Budankov 
2969f4fe11b7SAlexey Budankov 	if (!strcasecmp(str, "node"))
2970f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_NODE;
2971f4fe11b7SAlexey Budankov 	else if (!strcasecmp(str, "cpu"))
2972f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_CPU;
2973f4fe11b7SAlexey Budankov 
2974f4fe11b7SAlexey Budankov 	return 0;
2975f4fe11b7SAlexey Budankov }
2976f4fe11b7SAlexey Budankov 
29777954f716SAlexey Bayduraev static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits)
29787954f716SAlexey Bayduraev {
29797954f716SAlexey Bayduraev 	mask->nbits = nr_bits;
29807954f716SAlexey Bayduraev 	mask->bits = bitmap_zalloc(mask->nbits);
29817954f716SAlexey Bayduraev 	if (!mask->bits)
29827954f716SAlexey Bayduraev 		return -ENOMEM;
29837954f716SAlexey Bayduraev 
29847954f716SAlexey Bayduraev 	return 0;
29857954f716SAlexey Bayduraev }
29867954f716SAlexey Bayduraev 
29877954f716SAlexey Bayduraev static void record__mmap_cpu_mask_free(struct mmap_cpu_mask *mask)
29887954f716SAlexey Bayduraev {
29897954f716SAlexey Bayduraev 	bitmap_free(mask->bits);
29907954f716SAlexey Bayduraev 	mask->nbits = 0;
29917954f716SAlexey Bayduraev }
29927954f716SAlexey Bayduraev 
29937954f716SAlexey Bayduraev static int record__thread_mask_alloc(struct thread_mask *mask, int nr_bits)
29947954f716SAlexey Bayduraev {
29957954f716SAlexey Bayduraev 	int ret;
29967954f716SAlexey Bayduraev 
29977954f716SAlexey Bayduraev 	ret = record__mmap_cpu_mask_alloc(&mask->maps, nr_bits);
29987954f716SAlexey Bayduraev 	if (ret) {
29997954f716SAlexey Bayduraev 		mask->affinity.bits = NULL;
30007954f716SAlexey Bayduraev 		return ret;
30017954f716SAlexey Bayduraev 	}
30027954f716SAlexey Bayduraev 
30037954f716SAlexey Bayduraev 	ret = record__mmap_cpu_mask_alloc(&mask->affinity, nr_bits);
30047954f716SAlexey Bayduraev 	if (ret) {
30057954f716SAlexey Bayduraev 		record__mmap_cpu_mask_free(&mask->maps);
30067954f716SAlexey Bayduraev 		mask->maps.bits = NULL;
30077954f716SAlexey Bayduraev 	}
30087954f716SAlexey Bayduraev 
30097954f716SAlexey Bayduraev 	return ret;
30107954f716SAlexey Bayduraev }
30117954f716SAlexey Bayduraev 
30127954f716SAlexey Bayduraev static void record__thread_mask_free(struct thread_mask *mask)
30137954f716SAlexey Bayduraev {
30147954f716SAlexey Bayduraev 	record__mmap_cpu_mask_free(&mask->maps);
30157954f716SAlexey Bayduraev 	record__mmap_cpu_mask_free(&mask->affinity);
30167954f716SAlexey Bayduraev }
30177954f716SAlexey Bayduraev 
301806380a84SAlexey Bayduraev static int record__parse_threads(const struct option *opt, const char *str, int unset)
301906380a84SAlexey Bayduraev {
3020f466e5edSAlexey Bayduraev 	int s;
302106380a84SAlexey Bayduraev 	struct record_opts *opts = opt->value;
302206380a84SAlexey Bayduraev 
3023f466e5edSAlexey Bayduraev 	if (unset || !str || !strlen(str)) {
302406380a84SAlexey Bayduraev 		opts->threads_spec = THREAD_SPEC__CPU;
3025f466e5edSAlexey Bayduraev 	} else {
3026f466e5edSAlexey Bayduraev 		for (s = 1; s < THREAD_SPEC__MAX; s++) {
3027f466e5edSAlexey Bayduraev 			if (s == THREAD_SPEC__USER) {
3028f466e5edSAlexey Bayduraev 				opts->threads_user_spec = strdup(str);
3029f466e5edSAlexey Bayduraev 				if (!opts->threads_user_spec)
3030f466e5edSAlexey Bayduraev 					return -ENOMEM;
3031f466e5edSAlexey Bayduraev 				opts->threads_spec = THREAD_SPEC__USER;
3032f466e5edSAlexey Bayduraev 				break;
3033f466e5edSAlexey Bayduraev 			}
3034f466e5edSAlexey Bayduraev 			if (!strncasecmp(str, thread_spec_tags[s], strlen(thread_spec_tags[s]))) {
3035f466e5edSAlexey Bayduraev 				opts->threads_spec = s;
3036f466e5edSAlexey Bayduraev 				break;
3037f466e5edSAlexey Bayduraev 			}
3038f466e5edSAlexey Bayduraev 		}
3039f466e5edSAlexey Bayduraev 	}
3040f466e5edSAlexey Bayduraev 
3041f466e5edSAlexey Bayduraev 	if (opts->threads_spec == THREAD_SPEC__USER)
3042f466e5edSAlexey Bayduraev 		pr_debug("threads_spec: %s\n", opts->threads_user_spec);
3043f466e5edSAlexey Bayduraev 	else
3044f466e5edSAlexey Bayduraev 		pr_debug("threads_spec: %s\n", thread_spec_tags[opts->threads_spec]);
304506380a84SAlexey Bayduraev 
304606380a84SAlexey Bayduraev 	return 0;
304706380a84SAlexey Bayduraev }
304806380a84SAlexey Bayduraev 
30496d575816SJiwei Sun static int parse_output_max_size(const struct option *opt,
30506d575816SJiwei Sun 				 const char *str, int unset)
30516d575816SJiwei Sun {
30526d575816SJiwei Sun 	unsigned long *s = (unsigned long *)opt->value;
30536d575816SJiwei Sun 	static struct parse_tag tags_size[] = {
30546d575816SJiwei Sun 		{ .tag  = 'B', .mult = 1       },
30556d575816SJiwei Sun 		{ .tag  = 'K', .mult = 1 << 10 },
30566d575816SJiwei Sun 		{ .tag  = 'M', .mult = 1 << 20 },
30576d575816SJiwei Sun 		{ .tag  = 'G', .mult = 1 << 30 },
30586d575816SJiwei Sun 		{ .tag  = 0 },
30596d575816SJiwei Sun 	};
30606d575816SJiwei Sun 	unsigned long val;
30616d575816SJiwei Sun 
30626d575816SJiwei Sun 	if (unset) {
30636d575816SJiwei Sun 		*s = 0;
30646d575816SJiwei Sun 		return 0;
30656d575816SJiwei Sun 	}
30666d575816SJiwei Sun 
30676d575816SJiwei Sun 	val = parse_tag_value(str, tags_size);
30686d575816SJiwei Sun 	if (val != (unsigned long) -1) {
30696d575816SJiwei Sun 		*s = val;
30706d575816SJiwei Sun 		return 0;
30716d575816SJiwei Sun 	}
30726d575816SJiwei Sun 
30736d575816SJiwei Sun 	return -1;
30746d575816SJiwei Sun }
30756d575816SJiwei Sun 
3076e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt,
3077e9db1310SAdrian Hunter 				    const char *str,
3078e9db1310SAdrian Hunter 				    int unset __maybe_unused)
3079e9db1310SAdrian Hunter {
3080e9db1310SAdrian Hunter 	struct record_opts *opts = opt->value;
3081e9db1310SAdrian Hunter 	char *s, *p;
3082e9db1310SAdrian Hunter 	unsigned int mmap_pages;
3083e9db1310SAdrian Hunter 	int ret;
3084e9db1310SAdrian Hunter 
3085e9db1310SAdrian Hunter 	if (!str)
3086e9db1310SAdrian Hunter 		return -EINVAL;
3087e9db1310SAdrian Hunter 
3088e9db1310SAdrian Hunter 	s = strdup(str);
3089e9db1310SAdrian Hunter 	if (!s)
3090e9db1310SAdrian Hunter 		return -ENOMEM;
3091e9db1310SAdrian Hunter 
3092e9db1310SAdrian Hunter 	p = strchr(s, ',');
3093e9db1310SAdrian Hunter 	if (p)
3094e9db1310SAdrian Hunter 		*p = '\0';
3095e9db1310SAdrian Hunter 
3096e9db1310SAdrian Hunter 	if (*s) {
309725f84702SArnaldo Carvalho de Melo 		ret = __evlist__parse_mmap_pages(&mmap_pages, s);
3098e9db1310SAdrian Hunter 		if (ret)
3099e9db1310SAdrian Hunter 			goto out_free;
3100e9db1310SAdrian Hunter 		opts->mmap_pages = mmap_pages;
3101e9db1310SAdrian Hunter 	}
3102e9db1310SAdrian Hunter 
3103e9db1310SAdrian Hunter 	if (!p) {
3104e9db1310SAdrian Hunter 		ret = 0;
3105e9db1310SAdrian Hunter 		goto out_free;
3106e9db1310SAdrian Hunter 	}
3107e9db1310SAdrian Hunter 
310825f84702SArnaldo Carvalho de Melo 	ret = __evlist__parse_mmap_pages(&mmap_pages, p + 1);
3109e9db1310SAdrian Hunter 	if (ret)
3110e9db1310SAdrian Hunter 		goto out_free;
3111e9db1310SAdrian Hunter 
3112e9db1310SAdrian Hunter 	opts->auxtrace_mmap_pages = mmap_pages;
3113e9db1310SAdrian Hunter 
3114e9db1310SAdrian Hunter out_free:
3115e9db1310SAdrian Hunter 	free(s);
3116e9db1310SAdrian Hunter 	return ret;
3117e9db1310SAdrian Hunter }
3118e9db1310SAdrian Hunter 
31197248e308SAlexandre Truong void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __maybe_unused)
31207248e308SAlexandre Truong {
31217248e308SAlexandre Truong }
31227248e308SAlexandre Truong 
31231d078ccbSAlexey Budankov static int parse_control_option(const struct option *opt,
31241d078ccbSAlexey Budankov 				const char *str,
31251d078ccbSAlexey Budankov 				int unset __maybe_unused)
31261d078ccbSAlexey Budankov {
31279864a66dSAdrian Hunter 	struct record_opts *opts = opt->value;
31281d078ccbSAlexey Budankov 
3129a8fcbd26SAdrian Hunter 	return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close);
3130a8fcbd26SAdrian Hunter }
3131a8fcbd26SAdrian Hunter 
31320c582449SJiri Olsa static void switch_output_size_warn(struct record *rec)
31330c582449SJiri Olsa {
31349521b5f2SJiri Olsa 	u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages);
31350c582449SJiri Olsa 	struct switch_output *s = &rec->switch_output;
31360c582449SJiri Olsa 
31370c582449SJiri Olsa 	wakeup_size /= 2;
31380c582449SJiri Olsa 
31390c582449SJiri Olsa 	if (s->size < wakeup_size) {
31400c582449SJiri Olsa 		char buf[100];
31410c582449SJiri Olsa 
31420c582449SJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
31430c582449SJiri Olsa 		pr_warning("WARNING: switch-output data size lower than "
31440c582449SJiri Olsa 			   "wakeup kernel buffer size (%s) "
31450c582449SJiri Olsa 			   "expect bigger perf.data sizes\n", buf);
31460c582449SJiri Olsa 	}
31470c582449SJiri Olsa }
31480c582449SJiri Olsa 
3149cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec)
3150cb4e1ebbSJiri Olsa {
3151cb4e1ebbSJiri Olsa 	struct switch_output *s = &rec->switch_output;
3152dc0c6127SJiri Olsa 	static struct parse_tag tags_size[] = {
3153dc0c6127SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
3154dc0c6127SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
3155dc0c6127SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
3156dc0c6127SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
3157dc0c6127SJiri Olsa 		{ .tag  = 0 },
3158dc0c6127SJiri Olsa 	};
3159bfacbe3bSJiri Olsa 	static struct parse_tag tags_time[] = {
3160bfacbe3bSJiri Olsa 		{ .tag  = 's', .mult = 1        },
3161bfacbe3bSJiri Olsa 		{ .tag  = 'm', .mult = 60       },
3162bfacbe3bSJiri Olsa 		{ .tag  = 'h', .mult = 60*60    },
3163bfacbe3bSJiri Olsa 		{ .tag  = 'd', .mult = 60*60*24 },
3164bfacbe3bSJiri Olsa 		{ .tag  = 0 },
3165bfacbe3bSJiri Olsa 	};
3166dc0c6127SJiri Olsa 	unsigned long val;
3167cb4e1ebbSJiri Olsa 
3168899e5ffbSArnaldo Carvalho de Melo 	/*
3169899e5ffbSArnaldo Carvalho de Melo 	 * If we're using --switch-output-events, then we imply its
3170899e5ffbSArnaldo Carvalho de Melo 	 * --switch-output=signal, as we'll send a SIGUSR2 from the side band
3171899e5ffbSArnaldo Carvalho de Melo 	 *  thread to its parent.
3172899e5ffbSArnaldo Carvalho de Melo 	 */
3173b5f2511dSAlexey Bayduraev 	if (rec->switch_output_event_set) {
3174b5f2511dSAlexey Bayduraev 		if (record__threads_enabled(rec)) {
3175b5f2511dSAlexey Bayduraev 			pr_warning("WARNING: --switch-output-event option is not available in parallel streaming mode.\n");
3176b5f2511dSAlexey Bayduraev 			return 0;
3177b5f2511dSAlexey Bayduraev 		}
3178899e5ffbSArnaldo Carvalho de Melo 		goto do_signal;
3179b5f2511dSAlexey Bayduraev 	}
3180899e5ffbSArnaldo Carvalho de Melo 
3181cb4e1ebbSJiri Olsa 	if (!s->set)
3182cb4e1ebbSJiri Olsa 		return 0;
3183cb4e1ebbSJiri Olsa 
3184b5f2511dSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
3185b5f2511dSAlexey Bayduraev 		pr_warning("WARNING: --switch-output option is not available in parallel streaming mode.\n");
3186b5f2511dSAlexey Bayduraev 		return 0;
3187b5f2511dSAlexey Bayduraev 	}
3188b5f2511dSAlexey Bayduraev 
3189cb4e1ebbSJiri Olsa 	if (!strcmp(s->str, "signal")) {
3190899e5ffbSArnaldo Carvalho de Melo do_signal:
3191cb4e1ebbSJiri Olsa 		s->signal = true;
3192cb4e1ebbSJiri Olsa 		pr_debug("switch-output with SIGUSR2 signal\n");
3193dc0c6127SJiri Olsa 		goto enabled;
3194dc0c6127SJiri Olsa 	}
3195dc0c6127SJiri Olsa 
3196dc0c6127SJiri Olsa 	val = parse_tag_value(s->str, tags_size);
3197dc0c6127SJiri Olsa 	if (val != (unsigned long) -1) {
3198dc0c6127SJiri Olsa 		s->size = val;
3199dc0c6127SJiri Olsa 		pr_debug("switch-output with %s size threshold\n", s->str);
3200dc0c6127SJiri Olsa 		goto enabled;
3201cb4e1ebbSJiri Olsa 	}
3202cb4e1ebbSJiri Olsa 
3203bfacbe3bSJiri Olsa 	val = parse_tag_value(s->str, tags_time);
3204bfacbe3bSJiri Olsa 	if (val != (unsigned long) -1) {
3205bfacbe3bSJiri Olsa 		s->time = val;
3206bfacbe3bSJiri Olsa 		pr_debug("switch-output with %s time threshold (%lu seconds)\n",
3207bfacbe3bSJiri Olsa 			 s->str, s->time);
3208bfacbe3bSJiri Olsa 		goto enabled;
3209bfacbe3bSJiri Olsa 	}
3210bfacbe3bSJiri Olsa 
3211cb4e1ebbSJiri Olsa 	return -1;
3212dc0c6127SJiri Olsa 
3213dc0c6127SJiri Olsa enabled:
3214dc0c6127SJiri Olsa 	rec->timestamp_filename = true;
3215dc0c6127SJiri Olsa 	s->enabled              = true;
32160c582449SJiri Olsa 
32170c582449SJiri Olsa 	if (s->size && !rec->opts.no_buffering)
32180c582449SJiri Olsa 		switch_output_size_warn(rec);
32190c582449SJiri Olsa 
3220dc0c6127SJiri Olsa 	return 0;
3221cb4e1ebbSJiri Olsa }
3222cb4e1ebbSJiri Olsa 
3223e5b2c207SNamhyung Kim static const char * const __record_usage[] = {
322486470930SIngo Molnar 	"perf record [<options>] [<command>]",
322586470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
322686470930SIngo Molnar 	NULL
322786470930SIngo Molnar };
3228e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage;
322986470930SIngo Molnar 
32306e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event,
32316e0a9b3dSArnaldo Carvalho de Melo 				  struct perf_sample *sample, struct machine *machine)
32326e0a9b3dSArnaldo Carvalho de Melo {
32336e0a9b3dSArnaldo Carvalho de Melo 	/*
32346e0a9b3dSArnaldo Carvalho de Melo 	 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
32356e0a9b3dSArnaldo Carvalho de Melo 	 * no need to add them twice.
32366e0a9b3dSArnaldo Carvalho de Melo 	 */
32376e0a9b3dSArnaldo Carvalho de Melo 	if (!(event->header.misc & PERF_RECORD_MISC_USER))
32386e0a9b3dSArnaldo Carvalho de Melo 		return 0;
32396e0a9b3dSArnaldo Carvalho de Melo 	return perf_event__process_mmap(tool, event, sample, machine);
32406e0a9b3dSArnaldo Carvalho de Melo }
32416e0a9b3dSArnaldo Carvalho de Melo 
32426e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event,
32436e0a9b3dSArnaldo Carvalho de Melo 				   struct perf_sample *sample, struct machine *machine)
32446e0a9b3dSArnaldo Carvalho de Melo {
32456e0a9b3dSArnaldo Carvalho de Melo 	/*
32466e0a9b3dSArnaldo Carvalho de Melo 	 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
32476e0a9b3dSArnaldo Carvalho de Melo 	 * no need to add them twice.
32486e0a9b3dSArnaldo Carvalho de Melo 	 */
32496e0a9b3dSArnaldo Carvalho de Melo 	if (!(event->header.misc & PERF_RECORD_MISC_USER))
32506e0a9b3dSArnaldo Carvalho de Melo 		return 0;
32516e0a9b3dSArnaldo Carvalho de Melo 
32526e0a9b3dSArnaldo Carvalho de Melo 	return perf_event__process_mmap2(tool, event, sample, machine);
32536e0a9b3dSArnaldo Carvalho de Melo }
32546e0a9b3dSArnaldo Carvalho de Melo 
325566286ed3SAdrian Hunter static int process_timestamp_boundary(struct perf_tool *tool,
325666286ed3SAdrian Hunter 				      union perf_event *event __maybe_unused,
325766286ed3SAdrian Hunter 				      struct perf_sample *sample,
325866286ed3SAdrian Hunter 				      struct machine *machine __maybe_unused)
325966286ed3SAdrian Hunter {
326066286ed3SAdrian Hunter 	struct record *rec = container_of(tool, struct record, tool);
326166286ed3SAdrian Hunter 
326266286ed3SAdrian Hunter 	set_timestamp_boundary(rec, sample->time);
326366286ed3SAdrian Hunter 	return 0;
326466286ed3SAdrian Hunter }
326566286ed3SAdrian Hunter 
326641b740b6SNamhyung Kim static int parse_record_synth_option(const struct option *opt,
326741b740b6SNamhyung Kim 				     const char *str,
326841b740b6SNamhyung Kim 				     int unset __maybe_unused)
326941b740b6SNamhyung Kim {
327041b740b6SNamhyung Kim 	struct record_opts *opts = opt->value;
327141b740b6SNamhyung Kim 	char *p = strdup(str);
327241b740b6SNamhyung Kim 
327341b740b6SNamhyung Kim 	if (p == NULL)
327441b740b6SNamhyung Kim 		return -1;
327541b740b6SNamhyung Kim 
327641b740b6SNamhyung Kim 	opts->synth = parse_synth_opt(p);
327741b740b6SNamhyung Kim 	free(p);
327841b740b6SNamhyung Kim 
327941b740b6SNamhyung Kim 	if (opts->synth < 0) {
328041b740b6SNamhyung Kim 		pr_err("Invalid synth option: %s\n", str);
328141b740b6SNamhyung Kim 		return -1;
328241b740b6SNamhyung Kim 	}
328341b740b6SNamhyung Kim 	return 0;
328441b740b6SNamhyung Kim }
328541b740b6SNamhyung Kim 
3286d20deb64SArnaldo Carvalho de Melo /*
32878c6f45a7SArnaldo Carvalho de Melo  * XXX Ideally would be local to cmd_record() and passed to a record__new
32888c6f45a7SArnaldo Carvalho de Melo  * because we need to have access to it in record__exit, that is called
3289d20deb64SArnaldo Carvalho de Melo  * after cmd_record() exits, but since record_options need to be accessible to
3290d20deb64SArnaldo Carvalho de Melo  * builtin-script, leave it here.
3291d20deb64SArnaldo Carvalho de Melo  *
3292d20deb64SArnaldo Carvalho de Melo  * At least we don't ouch it in all the other functions here directly.
3293d20deb64SArnaldo Carvalho de Melo  *
3294d20deb64SArnaldo Carvalho de Melo  * Just say no to tons of global variables, sigh.
3295d20deb64SArnaldo Carvalho de Melo  */
32968c6f45a7SArnaldo Carvalho de Melo static struct record record = {
3297d20deb64SArnaldo Carvalho de Melo 	.opts = {
32988affc2b8SAndi Kleen 		.sample_time	     = true,
3299d20deb64SArnaldo Carvalho de Melo 		.mmap_pages	     = UINT_MAX,
3300d20deb64SArnaldo Carvalho de Melo 		.user_freq	     = UINT_MAX,
3301d20deb64SArnaldo Carvalho de Melo 		.user_interval	     = ULLONG_MAX,
3302447a6013SArnaldo Carvalho de Melo 		.freq		     = 4000,
3303d1cb9fceSNamhyung Kim 		.target		     = {
3304d1cb9fceSNamhyung Kim 			.uses_mmap   = true,
33053aa5939dSAdrian Hunter 			.default_per_cpu = true,
3306d1cb9fceSNamhyung Kim 		},
3307470530bbSAlexey Budankov 		.mmap_flush          = MMAP_FLUSH_DEFAULT,
3308d99c22eaSStephane Eranian 		.nr_threads_synthesize = 1,
33091d078ccbSAlexey Budankov 		.ctl_fd              = -1,
33101d078ccbSAlexey Budankov 		.ctl_fd_ack          = -1,
331141b740b6SNamhyung Kim 		.synth               = PERF_SYNTH_ALL,
3312d20deb64SArnaldo Carvalho de Melo 	},
3313e3d59112SNamhyung Kim 	.tool = {
3314e3d59112SNamhyung Kim 		.sample		= process_sample_event,
3315e3d59112SNamhyung Kim 		.fork		= perf_event__process_fork,
3316cca8482cSAdrian Hunter 		.exit		= perf_event__process_exit,
3317e3d59112SNamhyung Kim 		.comm		= perf_event__process_comm,
3318f3b3614aSHari Bathini 		.namespaces	= perf_event__process_namespaces,
33196e0a9b3dSArnaldo Carvalho de Melo 		.mmap		= build_id__process_mmap,
33206e0a9b3dSArnaldo Carvalho de Melo 		.mmap2		= build_id__process_mmap2,
332166286ed3SAdrian Hunter 		.itrace_start	= process_timestamp_boundary,
332266286ed3SAdrian Hunter 		.aux		= process_timestamp_boundary,
3323cca8482cSAdrian Hunter 		.ordered_events	= true,
3324e3d59112SNamhyung Kim 	},
3325d20deb64SArnaldo Carvalho de Melo };
33267865e817SFrederic Weisbecker 
332776a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
332876a26549SNamhyung Kim 	"\n\t\t\t\tDefault: fp";
332961eaa3beSArnaldo Carvalho de Melo 
33300aab2136SWang Nan static bool dry_run;
33310aab2136SWang Nan 
3332d20deb64SArnaldo Carvalho de Melo /*
3333d20deb64SArnaldo Carvalho de Melo  * XXX Will stay a global variable till we fix builtin-script.c to stop messing
3334d20deb64SArnaldo Carvalho de Melo  * with it and switch to use the library functions in perf_evlist that came
3335b4006796SArnaldo Carvalho de Melo  * from builtin-record.c, i.e. use record_opts,
33367b392ef0SArnaldo Carvalho de Melo  * evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
3337d20deb64SArnaldo Carvalho de Melo  * using pipes, etc.
3338d20deb64SArnaldo Carvalho de Melo  */
3339efd21307SJiri Olsa static struct option __record_options[] = {
3340d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &record.evlist, "event",
334186470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
3342f120f9d5SJiri Olsa 		     parse_events_option),
3343d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK(0, "filter", &record.evlist, "filter",
3344c171b552SLi Zefan 		     "event filter", parse_filter),
33454ba1faa1SWang Nan 	OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
33464ba1faa1SWang Nan 			   NULL, "don't record events from perf itself",
33474ba1faa1SWang Nan 			   exclude_perf),
3348bea03405SNamhyung Kim 	OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
3349d6d901c2SZhang, Yanmin 		    "record events on existing process id"),
3350bea03405SNamhyung Kim 	OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
3351d6d901c2SZhang, Yanmin 		    "record events on existing thread id"),
3352d20deb64SArnaldo Carvalho de Melo 	OPT_INTEGER('r', "realtime", &record.realtime_prio,
335386470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
3354509051eaSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
3355acac03faSKirill Smelkov 		    "collect data without buffering"),
3356d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
3357daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
3358bea03405SNamhyung Kim 	OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
335986470930SIngo Molnar 			    "system-wide collection from all CPUs"),
3360bea03405SNamhyung Kim 	OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
3361c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
3362d20deb64SArnaldo Carvalho de Melo 	OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
33632d4f2799SJiri Olsa 	OPT_STRING('o', "output", &record.data.path, "file",
336486470930SIngo Molnar 		    "output file name"),
336569e7e5b0SAdrian Hunter 	OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
336669e7e5b0SAdrian Hunter 			&record.opts.no_inherit_set,
33672e6cdf99SStephane Eranian 			"child tasks do not inherit counters"),
33684ea648aeSWang Nan 	OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
33694ea648aeSWang Nan 		    "synthesize non-sample events at the end of output"),
3370626a6b78SWang Nan 	OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
3371a060c1f1SWei Li 	OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"),
3372b09c2364SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq,
3373b09c2364SArnaldo Carvalho de Melo 		    "Fail if the specified frequency can't be used"),
337467230479SArnaldo Carvalho de Melo 	OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'",
337567230479SArnaldo Carvalho de Melo 		     "profile at this frequency",
337667230479SArnaldo Carvalho de Melo 		      record__parse_freq),
3377e9db1310SAdrian Hunter 	OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
3378e9db1310SAdrian Hunter 		     "number of mmap data pages and AUX area tracing mmap pages",
3379e9db1310SAdrian Hunter 		     record__parse_mmap_pages),
3380470530bbSAlexey Budankov 	OPT_CALLBACK(0, "mmap-flush", &record.opts, "number",
3381470530bbSAlexey Budankov 		     "Minimal number of bytes that is extracted from mmap data pages (default: 1)",
3382470530bbSAlexey Budankov 		     record__mmap_flush_parse),
33832ddd5c04SArnaldo Carvalho de Melo 	OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
338409b0fd45SJiri Olsa 			   NULL, "enables call-graph recording" ,
338509b0fd45SJiri Olsa 			   &record_callchain_opt),
338609b0fd45SJiri Olsa 	OPT_CALLBACK(0, "call-graph", &record.opts,
338776a26549SNamhyung Kim 		     "record_mode[,record_size]", record_callchain_help,
338809b0fd45SJiri Olsa 		     &record_parse_callchain_opt),
3389c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
33903da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
3391a527c2c1SJames Clark 	OPT_BOOLEAN('q', "quiet", &quiet, "don't print any warnings or messages"),
3392d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
3393649c48a9SPeter Zijlstra 		    "per thread counts"),
339456100321SPeter Zijlstra 	OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
33953b0a5daaSKan Liang 	OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr,
33963b0a5daaSKan Liang 		    "Record the sample physical addresses"),
3397542b88fdSKan Liang 	OPT_BOOLEAN(0, "data-page-size", &record.opts.sample_data_page_size,
3398542b88fdSKan Liang 		    "Record the sampled data address data page size"),
3399c1de7f3dSKan Liang 	OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size,
3400c1de7f3dSKan Liang 		    "Record the sampled code address (ip) page size"),
3401b6f35ed7SJiri Olsa 	OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
340261110883SAdrian Hunter 	OPT_BOOLEAN(0, "sample-identifier", &record.opts.sample_identifier,
340361110883SAdrian Hunter 		    "Record the sample identifier"),
34043abebc55SAdrian Hunter 	OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
34053abebc55SAdrian Hunter 			&record.opts.sample_time_set,
34063abebc55SAdrian Hunter 			"Record the sample timestamps"),
3407f290aa1fSJiri Olsa 	OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set,
3408f290aa1fSJiri Olsa 			"Record the sample period"),
3409d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
3410649c48a9SPeter Zijlstra 		    "don't sample"),
3411d2db9a98SWang Nan 	OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
3412d2db9a98SWang Nan 			&record.no_buildid_cache_set,
3413a1ac1d3cSStephane Eranian 			"do not update the buildid cache"),
3414d2db9a98SWang Nan 	OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
3415d2db9a98SWang Nan 			&record.no_buildid_set,
3416baa2f6ceSArnaldo Carvalho de Melo 			"do not collect buildids in perf.data"),
3417d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
3418023695d9SStephane Eranian 		     "monitor event in cgroup name only",
3419023695d9SStephane Eranian 		     parse_cgroups),
34206657a099SAdrian Hunter 	OPT_CALLBACK('D', "delay", &record, "ms",
34216657a099SAdrian Hunter 		     "ms to wait before starting measurement after program start (-1: start with events disabled), "
34226657a099SAdrian Hunter 		     "or ranges of time to enable events e.g. '-D 10-20,30-40'",
34236657a099SAdrian Hunter 		     record__parse_event_enable_time),
3424eeb399b5SAdrian Hunter 	OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"),
3425bea03405SNamhyung Kim 	OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
3426bea03405SNamhyung Kim 		   "user to profile"),
3427a5aabdacSStephane Eranian 
3428a5aabdacSStephane Eranian 	OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
3429a5aabdacSStephane Eranian 		     "branch any", "sample any taken branches",
3430a5aabdacSStephane Eranian 		     parse_branch_stack),
3431a5aabdacSStephane Eranian 
3432a5aabdacSStephane Eranian 	OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
3433a5aabdacSStephane Eranian 		     "branch filter mask", "branch stack filter modes",
3434bdfebd84SRoberto Agostino Vitillo 		     parse_branch_stack),
343505484298SAndi Kleen 	OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
343605484298SAndi Kleen 		    "sample by weight (on special events only)"),
3437475eeab9SAndi Kleen 	OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
3438475eeab9SAndi Kleen 		    "sample transaction flags (special events only)"),
34393aa5939dSAdrian Hunter 	OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
34403aa5939dSAdrian Hunter 		    "use per-thread mmaps"),
3441bcc84ec6SStephane Eranian 	OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
3442bcc84ec6SStephane Eranian 		    "sample selected machine registers on interrupt,"
3443aeea9062SKan Liang 		    " use '-I?' to list register names", parse_intr_regs),
344484c41742SAndi Kleen 	OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register",
344584c41742SAndi Kleen 		    "sample selected machine registers on interrupt,"
3446aeea9062SKan Liang 		    " use '--user-regs=?' to list register names", parse_user_regs),
344785c273d2SAndi Kleen 	OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
344885c273d2SAndi Kleen 		    "Record running/enabled time of read (:S) events"),
3449814c8c38SPeter Zijlstra 	OPT_CALLBACK('k', "clockid", &record.opts,
3450814c8c38SPeter Zijlstra 	"clockid", "clockid to use for events, see clock_gettime()",
3451814c8c38SPeter Zijlstra 	parse_clockid),
34522dd6d8a1SAdrian Hunter 	OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
34532dd6d8a1SAdrian Hunter 			  "opts", "AUX area tracing Snapshot Mode", ""),
3454c0a6de06SAdrian Hunter 	OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts,
3455c0a6de06SAdrian Hunter 			  "opts", "sample AUX area", ""),
34563fcb10e4SMark Drayton 	OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
34579d9cad76SKan Liang 			"per thread proc mmap processing timeout in ms"),
3458f3b3614aSHari Bathini 	OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces,
3459f3b3614aSHari Bathini 		    "Record namespaces events"),
34608fb4b679SNamhyung Kim 	OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup,
34618fb4b679SNamhyung Kim 		    "Record cgroup events"),
346216b4b4e1SAdrian Hunter 	OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events,
346316b4b4e1SAdrian Hunter 			&record.opts.record_switch_events_set,
3464b757bb09SAdrian Hunter 			"Record context switch events"),
346585723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
346685723885SJiri Olsa 			 "Configure all used events to run in kernel space.",
346785723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
346885723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
346985723885SJiri Olsa 			 "Configure all used events to run in user space.",
347085723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
347153651b28Syuzhoujian 	OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains,
347253651b28Syuzhoujian 		    "collect kernel callchains"),
347353651b28Syuzhoujian 	OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains,
347453651b28Syuzhoujian 		    "collect user callchains"),
347571dc2326SWang Nan 	OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
347671dc2326SWang Nan 		   "clang binary to use for compiling BPF scriptlets"),
347771dc2326SWang Nan 	OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
347871dc2326SWang Nan 		   "options passed to clang when compiling BPF scriptlets"),
34797efe0e03SHe Kuang 	OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
34807efe0e03SHe Kuang 		   "file", "vmlinux pathname"),
34816156681bSNamhyung Kim 	OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
34826156681bSNamhyung Kim 		    "Record build-id of all DSOs regardless of hits"),
3483e29386c8SJiri Olsa 	OPT_BOOLEAN(0, "buildid-mmap", &record.buildid_mmap,
3484e29386c8SJiri Olsa 		    "Record build-id in map events"),
3485ecfd7a9cSWang Nan 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
3486ecfd7a9cSWang Nan 		    "append timestamp to output filename"),
348768588bafSJin Yao 	OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary,
348868588bafSJin Yao 		    "Record timestamp boundary (time of first/last samples)"),
3489cb4e1ebbSJiri Olsa 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
3490c38dab7dSAndi Kleen 			  &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
3491c38dab7dSAndi Kleen 			  "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
3492dc0c6127SJiri Olsa 			  "signal"),
3493899e5ffbSArnaldo Carvalho de Melo 	OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event",
3494899e5ffbSArnaldo Carvalho de Melo 			 "switch output event selector. use 'perf list' to list available events",
3495899e5ffbSArnaldo Carvalho de Melo 			 parse_events_option_new_evlist),
349603724b2eSAndi Kleen 	OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
349703724b2eSAndi Kleen 		   "Limit number of switch output generated files"),
34980aab2136SWang Nan 	OPT_BOOLEAN(0, "dry-run", &dry_run,
34990aab2136SWang Nan 		    "Parse options then exit"),
3500d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
350193f20c0fSAlexey Budankov 	OPT_CALLBACK_OPTARG(0, "aio", &record.opts,
350293f20c0fSAlexey Budankov 		     &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
3503d3d1af6fSAlexey Budankov 		     record__aio_parse),
3504d3d1af6fSAlexey Budankov #endif
3505f4fe11b7SAlexey Budankov 	OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
3506f4fe11b7SAlexey Budankov 		     "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
3507f4fe11b7SAlexey Budankov 		     record__parse_affinity),
3508504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
3509b5f2511dSAlexey Bayduraev 	OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, "n",
3510b5f2511dSAlexey Bayduraev 			    "Compress records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
3511504c1ad1SAlexey Budankov 			    record__parse_comp_level),
3512504c1ad1SAlexey Budankov #endif
35136d575816SJiwei Sun 	OPT_CALLBACK(0, "max-size", &record.output_max_size,
35146d575816SJiwei Sun 		     "size", "Limit the maximum size of the output file", parse_output_max_size),
3515d99c22eaSStephane Eranian 	OPT_UINTEGER(0, "num-thread-synthesize",
3516d99c22eaSStephane Eranian 		     &record.opts.nr_threads_synthesize,
3517d99c22eaSStephane Eranian 		     "number of threads to run for event synthesis"),
351870943490SStephane Eranian #ifdef HAVE_LIBPFM
351970943490SStephane Eranian 	OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
352070943490SStephane Eranian 		"libpfm4 event selector. use 'perf list' to list available events",
352170943490SStephane Eranian 		parse_libpfm_events_option),
352270943490SStephane Eranian #endif
3523a8fcbd26SAdrian Hunter 	OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
3524d20aff15SAdrian Hunter 		     "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n"
3525d20aff15SAdrian Hunter 		     "\t\t\t  'snapshot': AUX area tracing snapshot).\n"
3526a8fcbd26SAdrian Hunter 		     "\t\t\t  Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
3527a8fcbd26SAdrian Hunter 		     "\t\t\t  Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
35281d078ccbSAlexey Budankov 		      parse_control_option),
352941b740b6SNamhyung Kim 	OPT_CALLBACK(0, "synth", &record.opts, "no|all|task|mmap|cgroup",
353041b740b6SNamhyung Kim 		     "Fine-tune event synthesis: default=all", parse_record_synth_option),
35319bce13eaSJiri Olsa 	OPT_STRING_OPTARG_SET(0, "debuginfod", &record.debuginfod.urls,
35329bce13eaSJiri Olsa 			  &record.debuginfod.set, "debuginfod urls",
35339bce13eaSJiri Olsa 			  "Enable debuginfod data retrieval from DEBUGINFOD_URLS or specified urls",
35349bce13eaSJiri Olsa 			  "system"),
353506380a84SAlexey Bayduraev 	OPT_CALLBACK_OPTARG(0, "threads", &record.opts, NULL, "spec",
353606380a84SAlexey Bayduraev 			    "write collected trace data into several data files using parallel threads",
353706380a84SAlexey Bayduraev 			    record__parse_threads),
3538edc41a10SNamhyung Kim 	OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"),
353986470930SIngo Molnar 	OPT_END()
354086470930SIngo Molnar };
354186470930SIngo Molnar 
3542e5b2c207SNamhyung Kim struct option *record_options = __record_options;
3543e5b2c207SNamhyung Kim 
3544cbd7bfc7SAthira Rajeev static int record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpu_map *cpus)
35457954f716SAlexey Bayduraev {
35460255571aSIan Rogers 	struct perf_cpu cpu;
35470255571aSIan Rogers 	int idx;
35487954f716SAlexey Bayduraev 
354923380e4dSAlexey Bayduraev 	if (cpu_map__is_dummy(cpus))
3550cbd7bfc7SAthira Rajeev 		return 0;
355123380e4dSAlexey Bayduraev 
3552cbd7bfc7SAthira Rajeev 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
3553ca76d7d2SAdrian Hunter 		if (cpu.cpu == -1)
3554ca76d7d2SAdrian Hunter 			continue;
3555cbd7bfc7SAthira Rajeev 		/* Return ENODEV is input cpu is greater than max cpu */
3556cbd7bfc7SAthira Rajeev 		if ((unsigned long)cpu.cpu > mask->nbits)
3557cbd7bfc7SAthira Rajeev 			return -ENODEV;
355849bd97c2SSean Christopherson 		__set_bit(cpu.cpu, mask->bits);
35597954f716SAlexey Bayduraev 	}
35607954f716SAlexey Bayduraev 
3561cbd7bfc7SAthira Rajeev 	return 0;
3562cbd7bfc7SAthira Rajeev }
3563cbd7bfc7SAthira Rajeev 
3564f466e5edSAlexey Bayduraev static int record__mmap_cpu_mask_init_spec(struct mmap_cpu_mask *mask, const char *mask_spec)
3565f466e5edSAlexey Bayduraev {
3566f466e5edSAlexey Bayduraev 	struct perf_cpu_map *cpus;
3567f466e5edSAlexey Bayduraev 
3568f466e5edSAlexey Bayduraev 	cpus = perf_cpu_map__new(mask_spec);
3569f466e5edSAlexey Bayduraev 	if (!cpus)
3570f466e5edSAlexey Bayduraev 		return -ENOMEM;
3571f466e5edSAlexey Bayduraev 
3572f466e5edSAlexey Bayduraev 	bitmap_zero(mask->bits, mask->nbits);
3573cbd7bfc7SAthira Rajeev 	if (record__mmap_cpu_mask_init(mask, cpus))
3574cbd7bfc7SAthira Rajeev 		return -ENODEV;
3575cbd7bfc7SAthira Rajeev 
3576f466e5edSAlexey Bayduraev 	perf_cpu_map__put(cpus);
3577f466e5edSAlexey Bayduraev 
3578f466e5edSAlexey Bayduraev 	return 0;
3579f466e5edSAlexey Bayduraev }
3580f466e5edSAlexey Bayduraev 
35817954f716SAlexey Bayduraev static void record__free_thread_masks(struct record *rec, int nr_threads)
35827954f716SAlexey Bayduraev {
35837954f716SAlexey Bayduraev 	int t;
35847954f716SAlexey Bayduraev 
35857954f716SAlexey Bayduraev 	if (rec->thread_masks)
35867954f716SAlexey Bayduraev 		for (t = 0; t < nr_threads; t++)
35877954f716SAlexey Bayduraev 			record__thread_mask_free(&rec->thread_masks[t]);
35887954f716SAlexey Bayduraev 
35897954f716SAlexey Bayduraev 	zfree(&rec->thread_masks);
35907954f716SAlexey Bayduraev }
35917954f716SAlexey Bayduraev 
35927954f716SAlexey Bayduraev static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr_bits)
35937954f716SAlexey Bayduraev {
35947954f716SAlexey Bayduraev 	int t, ret;
35957954f716SAlexey Bayduraev 
35967954f716SAlexey Bayduraev 	rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks)));
35977954f716SAlexey Bayduraev 	if (!rec->thread_masks) {
35987954f716SAlexey Bayduraev 		pr_err("Failed to allocate thread masks\n");
35997954f716SAlexey Bayduraev 		return -ENOMEM;
36007954f716SAlexey Bayduraev 	}
36017954f716SAlexey Bayduraev 
36027954f716SAlexey Bayduraev 	for (t = 0; t < nr_threads; t++) {
36037954f716SAlexey Bayduraev 		ret = record__thread_mask_alloc(&rec->thread_masks[t], nr_bits);
36047954f716SAlexey Bayduraev 		if (ret) {
36057954f716SAlexey Bayduraev 			pr_err("Failed to allocate thread masks[%d]\n", t);
36067954f716SAlexey Bayduraev 			goto out_free;
36077954f716SAlexey Bayduraev 		}
36087954f716SAlexey Bayduraev 	}
36097954f716SAlexey Bayduraev 
36107954f716SAlexey Bayduraev 	return 0;
36117954f716SAlexey Bayduraev 
36127954f716SAlexey Bayduraev out_free:
36137954f716SAlexey Bayduraev 	record__free_thread_masks(rec, nr_threads);
36147954f716SAlexey Bayduraev 
36157954f716SAlexey Bayduraev 	return ret;
36167954f716SAlexey Bayduraev }
36177954f716SAlexey Bayduraev 
361806380a84SAlexey Bayduraev static int record__init_thread_cpu_masks(struct record *rec, struct perf_cpu_map *cpus)
361906380a84SAlexey Bayduraev {
362006380a84SAlexey Bayduraev 	int t, ret, nr_cpus = perf_cpu_map__nr(cpus);
362106380a84SAlexey Bayduraev 
362206380a84SAlexey Bayduraev 	ret = record__alloc_thread_masks(rec, nr_cpus, cpu__max_cpu().cpu);
362306380a84SAlexey Bayduraev 	if (ret)
362406380a84SAlexey Bayduraev 		return ret;
362506380a84SAlexey Bayduraev 
362606380a84SAlexey Bayduraev 	rec->nr_threads = nr_cpus;
362706380a84SAlexey Bayduraev 	pr_debug("nr_threads: %d\n", rec->nr_threads);
362806380a84SAlexey Bayduraev 
362906380a84SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++) {
363049bd97c2SSean Christopherson 		__set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].maps.bits);
363149bd97c2SSean Christopherson 		__set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].affinity.bits);
3632*7c0a6144SYang Jihong 		if (verbose > 0) {
363306380a84SAlexey Bayduraev 			pr_debug("thread_masks[%d]: ", t);
363406380a84SAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps");
363506380a84SAlexey Bayduraev 			pr_debug("thread_masks[%d]: ", t);
363606380a84SAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity");
363706380a84SAlexey Bayduraev 		}
363806380a84SAlexey Bayduraev 	}
363906380a84SAlexey Bayduraev 
364006380a84SAlexey Bayduraev 	return 0;
364106380a84SAlexey Bayduraev }
364206380a84SAlexey Bayduraev 
3643f466e5edSAlexey Bayduraev static int record__init_thread_masks_spec(struct record *rec, struct perf_cpu_map *cpus,
3644f466e5edSAlexey Bayduraev 					  const char **maps_spec, const char **affinity_spec,
3645f466e5edSAlexey Bayduraev 					  u32 nr_spec)
3646f466e5edSAlexey Bayduraev {
3647f466e5edSAlexey Bayduraev 	u32 s;
3648f466e5edSAlexey Bayduraev 	int ret = 0, t = 0;
3649f466e5edSAlexey Bayduraev 	struct mmap_cpu_mask cpus_mask;
3650f466e5edSAlexey Bayduraev 	struct thread_mask thread_mask, full_mask, *thread_masks;
3651f466e5edSAlexey Bayduraev 
3652f466e5edSAlexey Bayduraev 	ret = record__mmap_cpu_mask_alloc(&cpus_mask, cpu__max_cpu().cpu);
3653f466e5edSAlexey Bayduraev 	if (ret) {
3654f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate CPUs mask\n");
3655f466e5edSAlexey Bayduraev 		return ret;
3656f466e5edSAlexey Bayduraev 	}
3657cbd7bfc7SAthira Rajeev 
3658cbd7bfc7SAthira Rajeev 	ret = record__mmap_cpu_mask_init(&cpus_mask, cpus);
3659cbd7bfc7SAthira Rajeev 	if (ret) {
3660cbd7bfc7SAthira Rajeev 		pr_err("Failed to init cpu mask\n");
3661cbd7bfc7SAthira Rajeev 		goto out_free_cpu_mask;
3662cbd7bfc7SAthira Rajeev 	}
3663f466e5edSAlexey Bayduraev 
3664f466e5edSAlexey Bayduraev 	ret = record__thread_mask_alloc(&full_mask, cpu__max_cpu().cpu);
3665f466e5edSAlexey Bayduraev 	if (ret) {
3666f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate full mask\n");
3667f466e5edSAlexey Bayduraev 		goto out_free_cpu_mask;
3668f466e5edSAlexey Bayduraev 	}
3669f466e5edSAlexey Bayduraev 
3670f466e5edSAlexey Bayduraev 	ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu);
3671f466e5edSAlexey Bayduraev 	if (ret) {
3672f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate thread mask\n");
3673f466e5edSAlexey Bayduraev 		goto out_free_full_and_cpu_masks;
3674f466e5edSAlexey Bayduraev 	}
3675f466e5edSAlexey Bayduraev 
3676f466e5edSAlexey Bayduraev 	for (s = 0; s < nr_spec; s++) {
3677f466e5edSAlexey Bayduraev 		ret = record__mmap_cpu_mask_init_spec(&thread_mask.maps, maps_spec[s]);
3678f466e5edSAlexey Bayduraev 		if (ret) {
3679f466e5edSAlexey Bayduraev 			pr_err("Failed to initialize maps thread mask\n");
3680f466e5edSAlexey Bayduraev 			goto out_free;
3681f466e5edSAlexey Bayduraev 		}
3682f466e5edSAlexey Bayduraev 		ret = record__mmap_cpu_mask_init_spec(&thread_mask.affinity, affinity_spec[s]);
3683f466e5edSAlexey Bayduraev 		if (ret) {
3684f466e5edSAlexey Bayduraev 			pr_err("Failed to initialize affinity thread mask\n");
3685f466e5edSAlexey Bayduraev 			goto out_free;
3686f466e5edSAlexey Bayduraev 		}
3687f466e5edSAlexey Bayduraev 
3688f466e5edSAlexey Bayduraev 		/* ignore invalid CPUs but do not allow empty masks */
3689f466e5edSAlexey Bayduraev 		if (!bitmap_and(thread_mask.maps.bits, thread_mask.maps.bits,
3690f466e5edSAlexey Bayduraev 				cpus_mask.bits, thread_mask.maps.nbits)) {
3691f466e5edSAlexey Bayduraev 			pr_err("Empty maps mask: %s\n", maps_spec[s]);
3692f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3693f466e5edSAlexey Bayduraev 			goto out_free;
3694f466e5edSAlexey Bayduraev 		}
3695f466e5edSAlexey Bayduraev 		if (!bitmap_and(thread_mask.affinity.bits, thread_mask.affinity.bits,
3696f466e5edSAlexey Bayduraev 				cpus_mask.bits, thread_mask.affinity.nbits)) {
3697f466e5edSAlexey Bayduraev 			pr_err("Empty affinity mask: %s\n", affinity_spec[s]);
3698f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3699f466e5edSAlexey Bayduraev 			goto out_free;
3700f466e5edSAlexey Bayduraev 		}
3701f466e5edSAlexey Bayduraev 
3702f466e5edSAlexey Bayduraev 		/* do not allow intersection with other masks (full_mask) */
3703f466e5edSAlexey Bayduraev 		if (bitmap_intersects(thread_mask.maps.bits, full_mask.maps.bits,
3704f466e5edSAlexey Bayduraev 				      thread_mask.maps.nbits)) {
3705f466e5edSAlexey Bayduraev 			pr_err("Intersecting maps mask: %s\n", maps_spec[s]);
3706f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3707f466e5edSAlexey Bayduraev 			goto out_free;
3708f466e5edSAlexey Bayduraev 		}
3709f466e5edSAlexey Bayduraev 		if (bitmap_intersects(thread_mask.affinity.bits, full_mask.affinity.bits,
3710f466e5edSAlexey Bayduraev 				      thread_mask.affinity.nbits)) {
3711f466e5edSAlexey Bayduraev 			pr_err("Intersecting affinity mask: %s\n", affinity_spec[s]);
3712f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3713f466e5edSAlexey Bayduraev 			goto out_free;
3714f466e5edSAlexey Bayduraev 		}
3715f466e5edSAlexey Bayduraev 
3716f466e5edSAlexey Bayduraev 		bitmap_or(full_mask.maps.bits, full_mask.maps.bits,
3717f466e5edSAlexey Bayduraev 			  thread_mask.maps.bits, full_mask.maps.nbits);
3718f466e5edSAlexey Bayduraev 		bitmap_or(full_mask.affinity.bits, full_mask.affinity.bits,
3719f466e5edSAlexey Bayduraev 			  thread_mask.affinity.bits, full_mask.maps.nbits);
3720f466e5edSAlexey Bayduraev 
3721f466e5edSAlexey Bayduraev 		thread_masks = realloc(rec->thread_masks, (t + 1) * sizeof(struct thread_mask));
3722f466e5edSAlexey Bayduraev 		if (!thread_masks) {
3723f466e5edSAlexey Bayduraev 			pr_err("Failed to reallocate thread masks\n");
3724f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3725f466e5edSAlexey Bayduraev 			goto out_free;
3726f466e5edSAlexey Bayduraev 		}
3727f466e5edSAlexey Bayduraev 		rec->thread_masks = thread_masks;
3728f466e5edSAlexey Bayduraev 		rec->thread_masks[t] = thread_mask;
3729*7c0a6144SYang Jihong 		if (verbose > 0) {
3730f466e5edSAlexey Bayduraev 			pr_debug("thread_masks[%d]: ", t);
3731f466e5edSAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps");
3732f466e5edSAlexey Bayduraev 			pr_debug("thread_masks[%d]: ", t);
3733f466e5edSAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity");
3734f466e5edSAlexey Bayduraev 		}
3735f466e5edSAlexey Bayduraev 		t++;
3736f466e5edSAlexey Bayduraev 		ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu);
3737f466e5edSAlexey Bayduraev 		if (ret) {
3738f466e5edSAlexey Bayduraev 			pr_err("Failed to allocate thread mask\n");
3739f466e5edSAlexey Bayduraev 			goto out_free_full_and_cpu_masks;
3740f466e5edSAlexey Bayduraev 		}
3741f466e5edSAlexey Bayduraev 	}
3742f466e5edSAlexey Bayduraev 	rec->nr_threads = t;
3743f466e5edSAlexey Bayduraev 	pr_debug("nr_threads: %d\n", rec->nr_threads);
3744f466e5edSAlexey Bayduraev 	if (!rec->nr_threads)
3745f466e5edSAlexey Bayduraev 		ret = -EINVAL;
3746f466e5edSAlexey Bayduraev 
3747f466e5edSAlexey Bayduraev out_free:
3748f466e5edSAlexey Bayduraev 	record__thread_mask_free(&thread_mask);
3749f466e5edSAlexey Bayduraev out_free_full_and_cpu_masks:
3750f466e5edSAlexey Bayduraev 	record__thread_mask_free(&full_mask);
3751f466e5edSAlexey Bayduraev out_free_cpu_mask:
3752f466e5edSAlexey Bayduraev 	record__mmap_cpu_mask_free(&cpus_mask);
3753f466e5edSAlexey Bayduraev 
3754f466e5edSAlexey Bayduraev 	return ret;
3755f466e5edSAlexey Bayduraev }
3756f466e5edSAlexey Bayduraev 
3757f466e5edSAlexey Bayduraev static int record__init_thread_core_masks(struct record *rec, struct perf_cpu_map *cpus)
3758f466e5edSAlexey Bayduraev {
3759f466e5edSAlexey Bayduraev 	int ret;
3760f466e5edSAlexey Bayduraev 	struct cpu_topology *topo;
3761f466e5edSAlexey Bayduraev 
3762f466e5edSAlexey Bayduraev 	topo = cpu_topology__new();
3763f466e5edSAlexey Bayduraev 	if (!topo) {
3764f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate CPU topology\n");
3765f466e5edSAlexey Bayduraev 		return -ENOMEM;
3766f466e5edSAlexey Bayduraev 	}
3767f466e5edSAlexey Bayduraev 
3768f466e5edSAlexey Bayduraev 	ret = record__init_thread_masks_spec(rec, cpus, topo->core_cpus_list,
3769f466e5edSAlexey Bayduraev 					     topo->core_cpus_list, topo->core_cpus_lists);
3770f466e5edSAlexey Bayduraev 	cpu_topology__delete(topo);
3771f466e5edSAlexey Bayduraev 
3772f466e5edSAlexey Bayduraev 	return ret;
3773f466e5edSAlexey Bayduraev }
3774f466e5edSAlexey Bayduraev 
3775f466e5edSAlexey Bayduraev static int record__init_thread_package_masks(struct record *rec, struct perf_cpu_map *cpus)
3776f466e5edSAlexey Bayduraev {
3777f466e5edSAlexey Bayduraev 	int ret;
3778f466e5edSAlexey Bayduraev 	struct cpu_topology *topo;
3779f466e5edSAlexey Bayduraev 
3780f466e5edSAlexey Bayduraev 	topo = cpu_topology__new();
3781f466e5edSAlexey Bayduraev 	if (!topo) {
3782f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate CPU topology\n");
3783f466e5edSAlexey Bayduraev 		return -ENOMEM;
3784f466e5edSAlexey Bayduraev 	}
3785f466e5edSAlexey Bayduraev 
3786f466e5edSAlexey Bayduraev 	ret = record__init_thread_masks_spec(rec, cpus, topo->package_cpus_list,
3787f466e5edSAlexey Bayduraev 					     topo->package_cpus_list, topo->package_cpus_lists);
3788f466e5edSAlexey Bayduraev 	cpu_topology__delete(topo);
3789f466e5edSAlexey Bayduraev 
3790f466e5edSAlexey Bayduraev 	return ret;
3791f466e5edSAlexey Bayduraev }
3792f466e5edSAlexey Bayduraev 
3793f466e5edSAlexey Bayduraev static int record__init_thread_numa_masks(struct record *rec, struct perf_cpu_map *cpus)
3794f466e5edSAlexey Bayduraev {
3795f466e5edSAlexey Bayduraev 	u32 s;
3796f466e5edSAlexey Bayduraev 	int ret;
3797f466e5edSAlexey Bayduraev 	const char **spec;
3798f466e5edSAlexey Bayduraev 	struct numa_topology *topo;
3799f466e5edSAlexey Bayduraev 
3800f466e5edSAlexey Bayduraev 	topo = numa_topology__new();
3801f466e5edSAlexey Bayduraev 	if (!topo) {
3802f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate NUMA topology\n");
3803f466e5edSAlexey Bayduraev 		return -ENOMEM;
3804f466e5edSAlexey Bayduraev 	}
3805f466e5edSAlexey Bayduraev 
3806f466e5edSAlexey Bayduraev 	spec = zalloc(topo->nr * sizeof(char *));
3807f466e5edSAlexey Bayduraev 	if (!spec) {
3808f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate NUMA spec\n");
3809f466e5edSAlexey Bayduraev 		ret = -ENOMEM;
3810f466e5edSAlexey Bayduraev 		goto out_delete_topo;
3811f466e5edSAlexey Bayduraev 	}
3812f466e5edSAlexey Bayduraev 	for (s = 0; s < topo->nr; s++)
3813f466e5edSAlexey Bayduraev 		spec[s] = topo->nodes[s].cpus;
3814f466e5edSAlexey Bayduraev 
3815f466e5edSAlexey Bayduraev 	ret = record__init_thread_masks_spec(rec, cpus, spec, spec, topo->nr);
3816f466e5edSAlexey Bayduraev 
3817f466e5edSAlexey Bayduraev 	zfree(&spec);
3818f466e5edSAlexey Bayduraev 
3819f466e5edSAlexey Bayduraev out_delete_topo:
3820f466e5edSAlexey Bayduraev 	numa_topology__delete(topo);
3821f466e5edSAlexey Bayduraev 
3822f466e5edSAlexey Bayduraev 	return ret;
3823f466e5edSAlexey Bayduraev }
3824f466e5edSAlexey Bayduraev 
3825f466e5edSAlexey Bayduraev static int record__init_thread_user_masks(struct record *rec, struct perf_cpu_map *cpus)
3826f466e5edSAlexey Bayduraev {
3827f466e5edSAlexey Bayduraev 	int t, ret;
3828f466e5edSAlexey Bayduraev 	u32 s, nr_spec = 0;
3829f466e5edSAlexey Bayduraev 	char **maps_spec = NULL, **affinity_spec = NULL, **tmp_spec;
3830f466e5edSAlexey Bayduraev 	char *user_spec, *spec, *spec_ptr, *mask, *mask_ptr, *dup_mask = NULL;
3831f466e5edSAlexey Bayduraev 
3832f466e5edSAlexey Bayduraev 	for (t = 0, user_spec = (char *)rec->opts.threads_user_spec; ; t++, user_spec = NULL) {
3833f466e5edSAlexey Bayduraev 		spec = strtok_r(user_spec, ":", &spec_ptr);
3834f466e5edSAlexey Bayduraev 		if (spec == NULL)
3835f466e5edSAlexey Bayduraev 			break;
3836f466e5edSAlexey Bayduraev 		pr_debug2("threads_spec[%d]: %s\n", t, spec);
3837f466e5edSAlexey Bayduraev 		mask = strtok_r(spec, "/", &mask_ptr);
3838f466e5edSAlexey Bayduraev 		if (mask == NULL)
3839f466e5edSAlexey Bayduraev 			break;
3840f466e5edSAlexey Bayduraev 		pr_debug2("  maps mask: %s\n", mask);
3841f466e5edSAlexey Bayduraev 		tmp_spec = realloc(maps_spec, (nr_spec + 1) * sizeof(char *));
3842f466e5edSAlexey Bayduraev 		if (!tmp_spec) {
3843f466e5edSAlexey Bayduraev 			pr_err("Failed to reallocate maps spec\n");
3844f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3845f466e5edSAlexey Bayduraev 			goto out_free;
3846f466e5edSAlexey Bayduraev 		}
3847f466e5edSAlexey Bayduraev 		maps_spec = tmp_spec;
3848f466e5edSAlexey Bayduraev 		maps_spec[nr_spec] = dup_mask = strdup(mask);
3849f466e5edSAlexey Bayduraev 		if (!maps_spec[nr_spec]) {
3850f466e5edSAlexey Bayduraev 			pr_err("Failed to allocate maps spec[%d]\n", nr_spec);
3851f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3852f466e5edSAlexey Bayduraev 			goto out_free;
3853f466e5edSAlexey Bayduraev 		}
3854f466e5edSAlexey Bayduraev 		mask = strtok_r(NULL, "/", &mask_ptr);
3855f466e5edSAlexey Bayduraev 		if (mask == NULL) {
3856f466e5edSAlexey Bayduraev 			pr_err("Invalid thread maps or affinity specs\n");
3857f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3858f466e5edSAlexey Bayduraev 			goto out_free;
3859f466e5edSAlexey Bayduraev 		}
3860f466e5edSAlexey Bayduraev 		pr_debug2("  affinity mask: %s\n", mask);
3861f466e5edSAlexey Bayduraev 		tmp_spec = realloc(affinity_spec, (nr_spec + 1) * sizeof(char *));
3862f466e5edSAlexey Bayduraev 		if (!tmp_spec) {
3863f466e5edSAlexey Bayduraev 			pr_err("Failed to reallocate affinity spec\n");
3864f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3865f466e5edSAlexey Bayduraev 			goto out_free;
3866f466e5edSAlexey Bayduraev 		}
3867f466e5edSAlexey Bayduraev 		affinity_spec = tmp_spec;
3868f466e5edSAlexey Bayduraev 		affinity_spec[nr_spec] = strdup(mask);
3869f466e5edSAlexey Bayduraev 		if (!affinity_spec[nr_spec]) {
3870f466e5edSAlexey Bayduraev 			pr_err("Failed to allocate affinity spec[%d]\n", nr_spec);
3871f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3872f466e5edSAlexey Bayduraev 			goto out_free;
3873f466e5edSAlexey Bayduraev 		}
3874f466e5edSAlexey Bayduraev 		dup_mask = NULL;
3875f466e5edSAlexey Bayduraev 		nr_spec++;
3876f466e5edSAlexey Bayduraev 	}
3877f466e5edSAlexey Bayduraev 
3878f466e5edSAlexey Bayduraev 	ret = record__init_thread_masks_spec(rec, cpus, (const char **)maps_spec,
3879f466e5edSAlexey Bayduraev 					     (const char **)affinity_spec, nr_spec);
3880f466e5edSAlexey Bayduraev 
3881f466e5edSAlexey Bayduraev out_free:
3882f466e5edSAlexey Bayduraev 	free(dup_mask);
3883f466e5edSAlexey Bayduraev 	for (s = 0; s < nr_spec; s++) {
3884f466e5edSAlexey Bayduraev 		if (maps_spec)
3885f466e5edSAlexey Bayduraev 			free(maps_spec[s]);
3886f466e5edSAlexey Bayduraev 		if (affinity_spec)
3887f466e5edSAlexey Bayduraev 			free(affinity_spec[s]);
3888f466e5edSAlexey Bayduraev 	}
3889f466e5edSAlexey Bayduraev 	free(affinity_spec);
3890f466e5edSAlexey Bayduraev 	free(maps_spec);
3891f466e5edSAlexey Bayduraev 
3892f466e5edSAlexey Bayduraev 	return ret;
3893f466e5edSAlexey Bayduraev }
3894f466e5edSAlexey Bayduraev 
38957954f716SAlexey Bayduraev static int record__init_thread_default_masks(struct record *rec, struct perf_cpu_map *cpus)
38967954f716SAlexey Bayduraev {
38977954f716SAlexey Bayduraev 	int ret;
38987954f716SAlexey Bayduraev 
38997954f716SAlexey Bayduraev 	ret = record__alloc_thread_masks(rec, 1, cpu__max_cpu().cpu);
39007954f716SAlexey Bayduraev 	if (ret)
39017954f716SAlexey Bayduraev 		return ret;
39027954f716SAlexey Bayduraev 
3903cbd7bfc7SAthira Rajeev 	if (record__mmap_cpu_mask_init(&rec->thread_masks->maps, cpus))
3904cbd7bfc7SAthira Rajeev 		return -ENODEV;
39057954f716SAlexey Bayduraev 
39067954f716SAlexey Bayduraev 	rec->nr_threads = 1;
39077954f716SAlexey Bayduraev 
39087954f716SAlexey Bayduraev 	return 0;
39097954f716SAlexey Bayduraev }
39107954f716SAlexey Bayduraev 
39117954f716SAlexey Bayduraev static int record__init_thread_masks(struct record *rec)
39127954f716SAlexey Bayduraev {
3913f466e5edSAlexey Bayduraev 	int ret = 0;
39147be1feddSAdrian Hunter 	struct perf_cpu_map *cpus = rec->evlist->core.all_cpus;
39157954f716SAlexey Bayduraev 
391606380a84SAlexey Bayduraev 	if (!record__threads_enabled(rec))
39177954f716SAlexey Bayduraev 		return record__init_thread_default_masks(rec, cpus);
391806380a84SAlexey Bayduraev 
39197be1feddSAdrian Hunter 	if (evlist__per_thread(rec->evlist)) {
392023380e4dSAlexey Bayduraev 		pr_err("--per-thread option is mutually exclusive to parallel streaming mode.\n");
392123380e4dSAlexey Bayduraev 		return -EINVAL;
392223380e4dSAlexey Bayduraev 	}
392323380e4dSAlexey Bayduraev 
3924f466e5edSAlexey Bayduraev 	switch (rec->opts.threads_spec) {
3925f466e5edSAlexey Bayduraev 	case THREAD_SPEC__CPU:
3926f466e5edSAlexey Bayduraev 		ret = record__init_thread_cpu_masks(rec, cpus);
3927f466e5edSAlexey Bayduraev 		break;
3928f466e5edSAlexey Bayduraev 	case THREAD_SPEC__CORE:
3929f466e5edSAlexey Bayduraev 		ret = record__init_thread_core_masks(rec, cpus);
3930f466e5edSAlexey Bayduraev 		break;
3931f466e5edSAlexey Bayduraev 	case THREAD_SPEC__PACKAGE:
3932f466e5edSAlexey Bayduraev 		ret = record__init_thread_package_masks(rec, cpus);
3933f466e5edSAlexey Bayduraev 		break;
3934f466e5edSAlexey Bayduraev 	case THREAD_SPEC__NUMA:
3935f466e5edSAlexey Bayduraev 		ret = record__init_thread_numa_masks(rec, cpus);
3936f466e5edSAlexey Bayduraev 		break;
3937f466e5edSAlexey Bayduraev 	case THREAD_SPEC__USER:
3938f466e5edSAlexey Bayduraev 		ret = record__init_thread_user_masks(rec, cpus);
3939f466e5edSAlexey Bayduraev 		break;
3940f466e5edSAlexey Bayduraev 	default:
3941f466e5edSAlexey Bayduraev 		break;
3942f466e5edSAlexey Bayduraev 	}
3943f466e5edSAlexey Bayduraev 
3944f466e5edSAlexey Bayduraev 	return ret;
39457954f716SAlexey Bayduraev }
39467954f716SAlexey Bayduraev 
3947b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv)
394886470930SIngo Molnar {
3949ef149c25SAdrian Hunter 	int err;
39508c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = &record;
395116ad2ffbSNamhyung Kim 	char errbuf[BUFSIZ];
395286470930SIngo Molnar 
395367230479SArnaldo Carvalho de Melo 	setlocale(LC_ALL, "");
395467230479SArnaldo Carvalho de Melo 
395548e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT
395648e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
395748e1cab1SWang Nan 	set_nobuild('\0', "clang-path", true);
395848e1cab1SWang Nan 	set_nobuild('\0', "clang-opt", true);
395948e1cab1SWang Nan # undef set_nobuild
396048e1cab1SWang Nan #endif
396148e1cab1SWang Nan 
39627efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE
39637efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT)
39647efe0e03SHe Kuang #  define REASON  "NO_DWARF=1"
39657efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT)
39667efe0e03SHe Kuang #  define REASON  "NO_LIBBPF=1"
39677efe0e03SHe Kuang # else
39687efe0e03SHe Kuang #  define REASON  "this architecture doesn't support BPF prologue"
39697efe0e03SHe Kuang # endif
39707efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
39717efe0e03SHe Kuang 	set_nobuild('\0', "vmlinux", true);
39727efe0e03SHe Kuang # undef set_nobuild
39737efe0e03SHe Kuang # undef REASON
39747efe0e03SHe Kuang #endif
39757efe0e03SHe Kuang 
3976edc41a10SNamhyung Kim #ifndef HAVE_BPF_SKEL
3977edc41a10SNamhyung Kim # define set_nobuild(s, l, m, c) set_option_nobuild(record_options, s, l, m, c)
3978edc41a10SNamhyung Kim 	set_nobuild('\0', "off-cpu", "no BUILD_BPF_SKEL=1", true);
3979edc41a10SNamhyung Kim # undef set_nobuild
3980edc41a10SNamhyung Kim #endif
3981edc41a10SNamhyung Kim 
39829d2ed645SAlexey Budankov 	rec->opts.affinity = PERF_AFFINITY_SYS;
39839d2ed645SAlexey Budankov 
39840f98b11cSJiri Olsa 	rec->evlist = evlist__new();
39853e2be2daSArnaldo Carvalho de Melo 	if (rec->evlist == NULL)
3986361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
3987361c99a6SArnaldo Carvalho de Melo 
3988ecc4c561SArnaldo Carvalho de Melo 	err = perf_config(perf_record_config, rec);
3989ecc4c561SArnaldo Carvalho de Melo 	if (err)
3990ecc4c561SArnaldo Carvalho de Melo 		return err;
3991eb853e80SJiri Olsa 
3992bca647aaSTom Zanussi 	argc = parse_options(argc, argv, record_options, record_usage,
3993a0541234SAnton Blanchard 			    PARSE_OPT_STOP_AT_NON_OPTION);
399468ba3235SNamhyung Kim 	if (quiet)
399568ba3235SNamhyung Kim 		perf_quiet_option();
3996483635a9SJiri Olsa 
39977cc72553SJames Clark 	err = symbol__validate_sym_arguments();
39987cc72553SJames Clark 	if (err)
39997cc72553SJames Clark 		return err;
40007cc72553SJames Clark 
40019bce13eaSJiri Olsa 	perf_debuginfod_setup(&record.debuginfod);
40029bce13eaSJiri Olsa 
4003483635a9SJiri Olsa 	/* Make system wide (-a) the default target. */
4004602ad878SArnaldo Carvalho de Melo 	if (!argc && target__none(&rec->opts.target))
4005483635a9SJiri Olsa 		rec->opts.target.system_wide = true;
400686470930SIngo Molnar 
4007bea03405SNamhyung Kim 	if (nr_cgroups && !rec->opts.target.system_wide) {
4008c7118369SNamhyung Kim 		usage_with_options_msg(record_usage, record_options,
4009c7118369SNamhyung Kim 			"cgroup monitoring only available in system-wide mode");
4010c7118369SNamhyung Kim 
4011023695d9SStephane Eranian 	}
4012504c1ad1SAlexey Budankov 
4013e29386c8SJiri Olsa 	if (rec->buildid_mmap) {
4014e29386c8SJiri Olsa 		if (!perf_can_record_build_id()) {
4015e29386c8SJiri Olsa 			pr_err("Failed: no support to record build id in mmap events, update your kernel.\n");
4016e29386c8SJiri Olsa 			err = -EINVAL;
4017e29386c8SJiri Olsa 			goto out_opts;
4018e29386c8SJiri Olsa 		}
4019e29386c8SJiri Olsa 		pr_debug("Enabling build id in mmap2 events.\n");
4020e29386c8SJiri Olsa 		/* Enable mmap build id synthesizing. */
4021e29386c8SJiri Olsa 		symbol_conf.buildid_mmap2 = true;
4022e29386c8SJiri Olsa 		/* Enable perf_event_attr::build_id bit. */
4023e29386c8SJiri Olsa 		rec->opts.build_id = true;
4024e29386c8SJiri Olsa 		/* Disable build id cache. */
4025e29386c8SJiri Olsa 		rec->no_buildid = true;
4026e29386c8SJiri Olsa 	}
4027e29386c8SJiri Olsa 
40284f2abe91SNamhyung Kim 	if (rec->opts.record_cgroup && !perf_can_record_cgroup()) {
40294f2abe91SNamhyung Kim 		pr_err("Kernel has no cgroup sampling support.\n");
40304f2abe91SNamhyung Kim 		err = -EINVAL;
40314f2abe91SNamhyung Kim 		goto out_opts;
40324f2abe91SNamhyung Kim 	}
40334f2abe91SNamhyung Kim 
4034f42c0ce5SAdrian Hunter 	if (rec->opts.kcore)
4035f42c0ce5SAdrian Hunter 		rec->opts.text_poke = true;
4036f42c0ce5SAdrian Hunter 
403756f735ffSAlexey Bayduraev 	if (rec->opts.kcore || record__threads_enabled(rec))
4038eeb399b5SAdrian Hunter 		rec->data.is_dir = true;
4039eeb399b5SAdrian Hunter 
4040b5f2511dSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
4041b5f2511dSAlexey Bayduraev 		if (rec->opts.affinity != PERF_AFFINITY_SYS) {
4042b5f2511dSAlexey Bayduraev 			pr_err("--affinity option is mutually exclusive to parallel streaming mode.\n");
4043b5f2511dSAlexey Bayduraev 			goto out_opts;
4044b5f2511dSAlexey Bayduraev 		}
4045b5f2511dSAlexey Bayduraev 		if (record__aio_enabled(rec)) {
4046b5f2511dSAlexey Bayduraev 			pr_err("Asynchronous streaming mode (--aio) is mutually exclusive to parallel streaming mode.\n");
4047b5f2511dSAlexey Bayduraev 			goto out_opts;
4048b5f2511dSAlexey Bayduraev 		}
4049b5f2511dSAlexey Bayduraev 	}
4050b5f2511dSAlexey Bayduraev 
4051504c1ad1SAlexey Budankov 	if (rec->opts.comp_level != 0) {
4052504c1ad1SAlexey Budankov 		pr_debug("Compression enabled, disabling build id collection at the end of the session.\n");
4053504c1ad1SAlexey Budankov 		rec->no_buildid = true;
4054504c1ad1SAlexey Budankov 	}
4055504c1ad1SAlexey Budankov 
4056b757bb09SAdrian Hunter 	if (rec->opts.record_switch_events &&
4057b757bb09SAdrian Hunter 	    !perf_can_record_switch_events()) {
4058c7118369SNamhyung Kim 		ui__error("kernel does not support recording context switch events\n");
4059c7118369SNamhyung Kim 		parse_options_usage(record_usage, record_options, "switch-events", 0);
4060a8fcbd26SAdrian Hunter 		err = -EINVAL;
4061a8fcbd26SAdrian Hunter 		goto out_opts;
4062b757bb09SAdrian Hunter 	}
4063023695d9SStephane Eranian 
4064cb4e1ebbSJiri Olsa 	if (switch_output_setup(rec)) {
4065cb4e1ebbSJiri Olsa 		parse_options_usage(record_usage, record_options, "switch-output", 0);
4066a8fcbd26SAdrian Hunter 		err = -EINVAL;
4067a8fcbd26SAdrian Hunter 		goto out_opts;
4068cb4e1ebbSJiri Olsa 	}
4069cb4e1ebbSJiri Olsa 
4070bfacbe3bSJiri Olsa 	if (rec->switch_output.time) {
4071bfacbe3bSJiri Olsa 		signal(SIGALRM, alarm_sig_handler);
4072bfacbe3bSJiri Olsa 		alarm(rec->switch_output.time);
4073bfacbe3bSJiri Olsa 	}
4074bfacbe3bSJiri Olsa 
407503724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
407603724b2eSAndi Kleen 		rec->switch_output.filenames = calloc(sizeof(char *),
407703724b2eSAndi Kleen 						      rec->switch_output.num_files);
4078a8fcbd26SAdrian Hunter 		if (!rec->switch_output.filenames) {
4079a8fcbd26SAdrian Hunter 			err = -EINVAL;
4080a8fcbd26SAdrian Hunter 			goto out_opts;
4081a8fcbd26SAdrian Hunter 		}
408203724b2eSAndi Kleen 	}
408303724b2eSAndi Kleen 
4084b5f2511dSAlexey Bayduraev 	if (rec->timestamp_filename && record__threads_enabled(rec)) {
4085b5f2511dSAlexey Bayduraev 		rec->timestamp_filename = false;
4086b5f2511dSAlexey Bayduraev 		pr_warning("WARNING: --timestamp-filename option is not available in parallel streaming mode.\n");
4087b5f2511dSAlexey Bayduraev 	}
4088b5f2511dSAlexey Bayduraev 
40891b36c03eSAdrian Hunter 	/*
40901b36c03eSAdrian Hunter 	 * Allow aliases to facilitate the lookup of symbols for address
40911b36c03eSAdrian Hunter 	 * filters. Refer to auxtrace_parse_filters().
40921b36c03eSAdrian Hunter 	 */
40931b36c03eSAdrian Hunter 	symbol_conf.allow_aliases = true;
40941b36c03eSAdrian Hunter 
40951b36c03eSAdrian Hunter 	symbol__init(NULL);
40961b36c03eSAdrian Hunter 
40974b5ea3bdSAdrian Hunter 	err = record__auxtrace_init(rec);
40981b36c03eSAdrian Hunter 	if (err)
40991b36c03eSAdrian Hunter 		goto out;
41001b36c03eSAdrian Hunter 
41010aab2136SWang Nan 	if (dry_run)
41025c01ad60SAdrian Hunter 		goto out;
41030aab2136SWang Nan 
4104d7888573SWang Nan 	err = bpf__setup_stdout(rec->evlist);
4105d7888573SWang Nan 	if (err) {
4106d7888573SWang Nan 		bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
4107d7888573SWang Nan 		pr_err("ERROR: Setup BPF stdout failed: %s\n",
4108d7888573SWang Nan 			 errbuf);
41095c01ad60SAdrian Hunter 		goto out;
4110d7888573SWang Nan 	}
4111d7888573SWang Nan 
4112ef149c25SAdrian Hunter 	err = -ENOMEM;
4113ef149c25SAdrian Hunter 
41140c1d46a8SWang Nan 	if (rec->no_buildid_cache || rec->no_buildid) {
4115a1ac1d3cSStephane Eranian 		disable_buildid_cache();
4116dc0c6127SJiri Olsa 	} else if (rec->switch_output.enabled) {
41170c1d46a8SWang Nan 		/*
41180c1d46a8SWang Nan 		 * In 'perf record --switch-output', disable buildid
41190c1d46a8SWang Nan 		 * generation by default to reduce data file switching
41200c1d46a8SWang Nan 		 * overhead. Still generate buildid if they are required
41210c1d46a8SWang Nan 		 * explicitly using
41220c1d46a8SWang Nan 		 *
412360437ac0SJiri Olsa 		 *  perf record --switch-output --no-no-buildid \
41240c1d46a8SWang Nan 		 *              --no-no-buildid-cache
41250c1d46a8SWang Nan 		 *
41260c1d46a8SWang Nan 		 * Following code equals to:
41270c1d46a8SWang Nan 		 *
41280c1d46a8SWang Nan 		 * if ((rec->no_buildid || !rec->no_buildid_set) &&
41290c1d46a8SWang Nan 		 *     (rec->no_buildid_cache || !rec->no_buildid_cache_set))
41300c1d46a8SWang Nan 		 *         disable_buildid_cache();
41310c1d46a8SWang Nan 		 */
41320c1d46a8SWang Nan 		bool disable = true;
41330c1d46a8SWang Nan 
41340c1d46a8SWang Nan 		if (rec->no_buildid_set && !rec->no_buildid)
41350c1d46a8SWang Nan 			disable = false;
41360c1d46a8SWang Nan 		if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
41370c1d46a8SWang Nan 			disable = false;
41380c1d46a8SWang Nan 		if (disable) {
41390c1d46a8SWang Nan 			rec->no_buildid = true;
41400c1d46a8SWang Nan 			rec->no_buildid_cache = true;
41410c1d46a8SWang Nan 			disable_buildid_cache();
41420c1d46a8SWang Nan 		}
41430c1d46a8SWang Nan 	}
4144655000e7SArnaldo Carvalho de Melo 
41454ea648aeSWang Nan 	if (record.opts.overwrite)
41464ea648aeSWang Nan 		record.opts.tail_synthesize = true;
41474ea648aeSWang Nan 
4148b53a0755SJin Yao 	if (rec->evlist->core.nr_entries == 0) {
4149b53a0755SJin Yao 		if (perf_pmu__has_hybrid()) {
4150b53a0755SJin Yao 			err = evlist__add_default_hybrid(rec->evlist,
4151b53a0755SJin Yao 							 !record.opts.no_samples);
4152b53a0755SJin Yao 		} else {
4153b53a0755SJin Yao 			err = __evlist__add_default(rec->evlist,
4154b53a0755SJin Yao 						    !record.opts.no_samples);
4155b53a0755SJin Yao 		}
4156b53a0755SJin Yao 
4157b53a0755SJin Yao 		if (err < 0) {
415869aad6f1SArnaldo Carvalho de Melo 			pr_err("Not enough memory for event selector list\n");
4159394c01edSAdrian Hunter 			goto out;
4160bbd36e5eSPeter Zijlstra 		}
4161b53a0755SJin Yao 	}
416286470930SIngo Molnar 
416369e7e5b0SAdrian Hunter 	if (rec->opts.target.tid && !rec->opts.no_inherit_set)
416469e7e5b0SAdrian Hunter 		rec->opts.no_inherit = true;
416569e7e5b0SAdrian Hunter 
4166602ad878SArnaldo Carvalho de Melo 	err = target__validate(&rec->opts.target);
416716ad2ffbSNamhyung Kim 	if (err) {
4168602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
4169c3dec27bSJiri Olsa 		ui__warning("%s\n", errbuf);
417016ad2ffbSNamhyung Kim 	}
41714bd0f2d2SNamhyung Kim 
4172602ad878SArnaldo Carvalho de Melo 	err = target__parse_uid(&rec->opts.target);
417316ad2ffbSNamhyung Kim 	if (err) {
417416ad2ffbSNamhyung Kim 		int saved_errno = errno;
417516ad2ffbSNamhyung Kim 
4176602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
41773780f488SNamhyung Kim 		ui__error("%s", errbuf);
417816ad2ffbSNamhyung Kim 
417916ad2ffbSNamhyung Kim 		err = -saved_errno;
4180394c01edSAdrian Hunter 		goto out;
418116ad2ffbSNamhyung Kim 	}
41820d37aa34SArnaldo Carvalho de Melo 
4183ca800068SMengting Zhang 	/* Enable ignoring missing threads when -u/-p option is defined. */
4184ca800068SMengting Zhang 	rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid;
418523dc4f15SJiri Olsa 
41861d3351e6SJin Yao 	if (evlist__fix_hybrid_cpus(rec->evlist, rec->opts.target.cpu_list)) {
41871d3351e6SJin Yao 		pr_err("failed to use cpu list %s\n",
41881d3351e6SJin Yao 		       rec->opts.target.cpu_list);
41891d3351e6SJin Yao 		goto out;
41901d3351e6SJin Yao 	}
41911d3351e6SJin Yao 
41921d3351e6SJin Yao 	rec->opts.target.hybrid = perf_pmu__has_hybrid();
41937248e308SAlexandre Truong 
41947248e308SAlexandre Truong 	if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP)
41957248e308SAlexandre Truong 		arch__add_leaf_frame_record_opts(&rec->opts);
41967248e308SAlexandre Truong 
419716ad2ffbSNamhyung Kim 	err = -ENOMEM;
41981bf7d836SMartin Liška 	if (evlist__create_maps(rec->evlist, &rec->opts.target) < 0) {
41991bf7d836SMartin Liška 		if (rec->opts.target.pid != NULL) {
42001bf7d836SMartin Liška 			pr_err("Couldn't create thread/CPU maps: %s\n",
42011bf7d836SMartin Liška 				errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
42021bf7d836SMartin Liška 			goto out;
42031bf7d836SMartin Liška 		}
42041bf7d836SMartin Liška 		else
4205dd7927f4SArnaldo Carvalho de Melo 			usage_with_options(record_usage, record_options);
42061bf7d836SMartin Liška 	}
420769aad6f1SArnaldo Carvalho de Melo 
4208ef149c25SAdrian Hunter 	err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
4209ef149c25SAdrian Hunter 	if (err)
4210394c01edSAdrian Hunter 		goto out;
4211ef149c25SAdrian Hunter 
42126156681bSNamhyung Kim 	/*
42136156681bSNamhyung Kim 	 * We take all buildids when the file contains
42146156681bSNamhyung Kim 	 * AUX area tracing data because we do not decode the
42156156681bSNamhyung Kim 	 * trace because it would take too long.
42166156681bSNamhyung Kim 	 */
42176156681bSNamhyung Kim 	if (rec->opts.full_auxtrace)
42186156681bSNamhyung Kim 		rec->buildid_all = true;
42196156681bSNamhyung Kim 
4220246eba8eSAdrian Hunter 	if (rec->opts.text_poke) {
4221246eba8eSAdrian Hunter 		err = record__config_text_poke(rec->evlist);
4222246eba8eSAdrian Hunter 		if (err) {
4223246eba8eSAdrian Hunter 			pr_err("record__config_text_poke failed, error %d\n", err);
4224246eba8eSAdrian Hunter 			goto out;
4225246eba8eSAdrian Hunter 		}
4226246eba8eSAdrian Hunter 	}
4227246eba8eSAdrian Hunter 
4228edc41a10SNamhyung Kim 	if (rec->off_cpu) {
4229edc41a10SNamhyung Kim 		err = record__config_off_cpu(rec);
4230edc41a10SNamhyung Kim 		if (err) {
4231edc41a10SNamhyung Kim 			pr_err("record__config_off_cpu failed, error %d\n", err);
4232edc41a10SNamhyung Kim 			goto out;
4233edc41a10SNamhyung Kim 		}
4234edc41a10SNamhyung Kim 	}
4235edc41a10SNamhyung Kim 
4236b4006796SArnaldo Carvalho de Melo 	if (record_opts__config(&rec->opts)) {
423739d17dacSArnaldo Carvalho de Melo 		err = -EINVAL;
4238394c01edSAdrian Hunter 		goto out;
42397e4ff9e3SMike Galbraith 	}
42407e4ff9e3SMike Galbraith 
42417954f716SAlexey Bayduraev 	err = record__init_thread_masks(rec);
42427954f716SAlexey Bayduraev 	if (err) {
42437954f716SAlexey Bayduraev 		pr_err("Failed to initialize parallel data streaming masks\n");
42447954f716SAlexey Bayduraev 		goto out;
42457954f716SAlexey Bayduraev 	}
42467954f716SAlexey Bayduraev 
424793f20c0fSAlexey Budankov 	if (rec->opts.nr_cblocks > nr_cblocks_max)
424893f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = nr_cblocks_max;
42495d7f4116SAlexey Budankov 	pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks);
4250d3d1af6fSAlexey Budankov 
42519d2ed645SAlexey Budankov 	pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]);
4252470530bbSAlexey Budankov 	pr_debug("mmap flush: %d\n", rec->opts.mmap_flush);
42539d2ed645SAlexey Budankov 
425451255a8aSAlexey Budankov 	if (rec->opts.comp_level > comp_level_max)
425551255a8aSAlexey Budankov 		rec->opts.comp_level = comp_level_max;
425651255a8aSAlexey Budankov 	pr_debug("comp level: %d\n", rec->opts.comp_level);
425751255a8aSAlexey Budankov 
4258d20deb64SArnaldo Carvalho de Melo 	err = __cmd_record(&record, argc, argv);
4259394c01edSAdrian Hunter out:
4260c12995a5SJiri Olsa 	evlist__delete(rec->evlist);
4261d65a458bSArnaldo Carvalho de Melo 	symbol__exit();
4262ef149c25SAdrian Hunter 	auxtrace_record__free(rec->itr);
4263a8fcbd26SAdrian Hunter out_opts:
42647954f716SAlexey Bayduraev 	record__free_thread_masks(rec, rec->nr_threads);
42657954f716SAlexey Bayduraev 	rec->nr_threads = 0;
4266ee7fe31eSAdrian Hunter 	evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close);
426739d17dacSArnaldo Carvalho de Melo 	return err;
426886470930SIngo Molnar }
42692dd6d8a1SAdrian Hunter 
42702dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused)
42712dd6d8a1SAdrian Hunter {
4272dc0c6127SJiri Olsa 	struct record *rec = &record;
4273dc0c6127SJiri Olsa 
4274d20aff15SAdrian Hunter 	hit_auxtrace_snapshot_trigger(rec);
42753c1cb7e3SWang Nan 
4276dc0c6127SJiri Olsa 	if (switch_output_signal(rec))
42773c1cb7e3SWang Nan 		trigger_hit(&switch_output_trigger);
42782dd6d8a1SAdrian Hunter }
4279bfacbe3bSJiri Olsa 
4280bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused)
4281bfacbe3bSJiri Olsa {
4282bfacbe3bSJiri Olsa 	struct record *rec = &record;
4283bfacbe3bSJiri Olsa 
4284bfacbe3bSJiri Olsa 	if (switch_output_time(rec))
4285bfacbe3bSJiri Olsa 		trigger_hit(&switch_output_trigger);
4286bfacbe3bSJiri Olsa }
4287