xref: /openbmc/linux/tools/perf/builtin-record.c (revision feff0b61ffd831dbe4a7f28cfc8064b59c9f90c1)
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>
1386470930SIngo Molnar #include "util/parse-events.h"
1441840d21STaeung Song #include "util/config.h"
1586470930SIngo Molnar 
168f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h"
17f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h"
187c6a1c65SPeter Zijlstra #include "util/header.h"
1966e274f3SFrederic Weisbecker #include "util/event.h"
20361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h"
2169aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h"
228f28827aSFrederic Weisbecker #include "util/debug.h"
23e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
24aeb00b1aSArnaldo Carvalho de Melo #include "util/target.h"
2594c744b6SArnaldo Carvalho de Melo #include "util/session.h"
2645694aa7SArnaldo Carvalho de Melo #include "util/tool.h"
278d06367fSArnaldo Carvalho de Melo #include "util/symbol.h"
28aeb00b1aSArnaldo Carvalho de Melo #include "util/record.h"
29a12b51c4SPaul Mackerras #include "util/cpumap.h"
30fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h"
31f5fc1412SJiri Olsa #include "util/data.h"
32bcc84ec6SStephane Eranian #include "util/perf_regs.h"
33ef149c25SAdrian Hunter #include "util/auxtrace.h"
3446bc29b9SAdrian Hunter #include "util/tsc.h"
35f00898f4SAndi Kleen #include "util/parse-branch-options.h"
36bcc84ec6SStephane Eranian #include "util/parse-regs-options.h"
3740c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
3871dc2326SWang Nan #include "util/llvm-utils.h"
398690a2a7SWang Nan #include "util/bpf-loader.h"
405f9cf599SWang Nan #include "util/trigger.h"
41a074865eSWang Nan #include "util/perf-hooks.h"
42f13de660SAlexey Budankov #include "util/cpu-set-sched.h"
43ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h"
44c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h"
4558db1d6eSArnaldo Carvalho de Melo #include "util/units.h"
467b612e29SSong Liu #include "util/bpf-event.h"
47d99c22eaSStephane Eranian #include "util/util.h"
4870943490SStephane Eranian #include "util/pfm.h"
496953beb4SJiri Olsa #include "util/clockid.h"
50b53a0755SJin Yao #include "util/pmu-hybrid.h"
51b53a0755SJin Yao #include "util/evlist-hybrid.h"
52edc41a10SNamhyung Kim #include "util/off_cpu.h"
53d8871ea7SWang Nan #include "asm/bug.h"
54c1a604dfSArnaldo Carvalho de Melo #include "perf.h"
55f466e5edSAlexey Bayduraev #include "cputopo.h"
567c6a1c65SPeter Zijlstra 
57a43783aeSArnaldo Carvalho de Melo #include <errno.h>
58fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
5967230479SArnaldo Carvalho de Melo #include <locale.h>
604208735dSArnaldo Carvalho de Melo #include <poll.h>
61d99c22eaSStephane Eranian #include <pthread.h>
6286470930SIngo Molnar #include <unistd.h>
63415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID
64415ccb58SAlexey Bayduraev #include <syscall.h>
65415ccb58SAlexey Bayduraev #endif
6686470930SIngo Molnar #include <sched.h>
679607ad3aSArnaldo Carvalho de Melo #include <signal.h>
68da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
69da231338SAnand K Mistry #include <sys/eventfd.h>
70da231338SAnand K Mistry #endif
71a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h>
724208735dSArnaldo Carvalho de Melo #include <sys/wait.h>
73eeb399b5SAdrian Hunter #include <sys/types.h>
74eeb399b5SAdrian Hunter #include <sys/stat.h>
75eeb399b5SAdrian Hunter #include <fcntl.h>
766ef81c55SMamatha Inamdar #include <linux/err.h>
778520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
780693e680SArnaldo Carvalho de Melo #include <linux/time64.h>
79d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
808384a260SAlexey Budankov #include <linux/bitmap.h>
81d1e325cfSJiri Olsa #include <sys/time.h>
8278da39faSBernhard Rosenkraenzer 
831b43b704SJiri Olsa struct switch_output {
84dc0c6127SJiri Olsa 	bool		 enabled;
851b43b704SJiri Olsa 	bool		 signal;
86dc0c6127SJiri Olsa 	unsigned long	 size;
87bfacbe3bSJiri Olsa 	unsigned long	 time;
88cb4e1ebbSJiri Olsa 	const char	*str;
89cb4e1ebbSJiri Olsa 	bool		 set;
9003724b2eSAndi Kleen 	char		 **filenames;
9103724b2eSAndi Kleen 	int		 num_files;
9203724b2eSAndi Kleen 	int		 cur_file;
931b43b704SJiri Olsa };
941b43b704SJiri Olsa 
957954f716SAlexey Bayduraev struct thread_mask {
967954f716SAlexey Bayduraev 	struct mmap_cpu_mask	maps;
977954f716SAlexey Bayduraev 	struct mmap_cpu_mask	affinity;
987954f716SAlexey Bayduraev };
997954f716SAlexey Bayduraev 
100415ccb58SAlexey Bayduraev struct record_thread {
101415ccb58SAlexey Bayduraev 	pid_t			tid;
102415ccb58SAlexey Bayduraev 	struct thread_mask	*mask;
103415ccb58SAlexey Bayduraev 	struct {
104415ccb58SAlexey Bayduraev 		int		msg[2];
105415ccb58SAlexey Bayduraev 		int		ack[2];
106415ccb58SAlexey Bayduraev 	} pipes;
107415ccb58SAlexey Bayduraev 	struct fdarray		pollfd;
108415ccb58SAlexey Bayduraev 	int			ctlfd_pos;
109415ccb58SAlexey Bayduraev 	int			nr_mmaps;
110415ccb58SAlexey Bayduraev 	struct mmap		**maps;
111415ccb58SAlexey Bayduraev 	struct mmap		**overwrite_maps;
112415ccb58SAlexey Bayduraev 	struct record		*rec;
113396b626bSAlexey Bayduraev 	unsigned long long	samples;
114396b626bSAlexey Bayduraev 	unsigned long		waking;
115ae9c7242SAlexey Bayduraev 	u64			bytes_written;
116610fbc01SAlexey Bayduraev 	u64			bytes_transferred;
117610fbc01SAlexey Bayduraev 	u64			bytes_compressed;
118415ccb58SAlexey Bayduraev };
119415ccb58SAlexey Bayduraev 
120396b626bSAlexey Bayduraev static __thread struct record_thread *thread;
121396b626bSAlexey Bayduraev 
1221e5de7d9SAlexey Bayduraev enum thread_msg {
1231e5de7d9SAlexey Bayduraev 	THREAD_MSG__UNDEFINED = 0,
1241e5de7d9SAlexey Bayduraev 	THREAD_MSG__READY,
1251e5de7d9SAlexey Bayduraev 	THREAD_MSG__MAX,
1261e5de7d9SAlexey Bayduraev };
1271e5de7d9SAlexey Bayduraev 
1281e5de7d9SAlexey Bayduraev static const char *thread_msg_tags[THREAD_MSG__MAX] = {
1291e5de7d9SAlexey Bayduraev 	"UNDEFINED", "READY"
1301e5de7d9SAlexey Bayduraev };
1311e5de7d9SAlexey Bayduraev 
13206380a84SAlexey Bayduraev enum thread_spec {
13306380a84SAlexey Bayduraev 	THREAD_SPEC__UNDEFINED = 0,
13406380a84SAlexey Bayduraev 	THREAD_SPEC__CPU,
135f466e5edSAlexey Bayduraev 	THREAD_SPEC__CORE,
136f466e5edSAlexey Bayduraev 	THREAD_SPEC__PACKAGE,
137f466e5edSAlexey Bayduraev 	THREAD_SPEC__NUMA,
138f466e5edSAlexey Bayduraev 	THREAD_SPEC__USER,
139f466e5edSAlexey Bayduraev 	THREAD_SPEC__MAX,
140f466e5edSAlexey Bayduraev };
141f466e5edSAlexey Bayduraev 
142f466e5edSAlexey Bayduraev static const char *thread_spec_tags[THREAD_SPEC__MAX] = {
143f466e5edSAlexey Bayduraev 	"undefined", "cpu", "core", "package", "numa", "user"
14406380a84SAlexey Bayduraev };
14506380a84SAlexey Bayduraev 
1466562c9acSAdrian Hunter struct pollfd_index_map {
1476562c9acSAdrian Hunter 	int evlist_pollfd_index;
1486562c9acSAdrian Hunter 	int thread_pollfd_index;
1496562c9acSAdrian Hunter };
1506562c9acSAdrian Hunter 
1518c6f45a7SArnaldo Carvalho de Melo struct record {
15245694aa7SArnaldo Carvalho de Melo 	struct perf_tool	tool;
153b4006796SArnaldo Carvalho de Melo 	struct record_opts	opts;
154d20deb64SArnaldo Carvalho de Melo 	u64			bytes_written;
1558ceb41d7SJiri Olsa 	struct perf_data	data;
156ef149c25SAdrian Hunter 	struct auxtrace_record	*itr;
15763503dbaSJiri Olsa 	struct evlist	*evlist;
158d20deb64SArnaldo Carvalho de Melo 	struct perf_session	*session;
159bc477d79SArnaldo Carvalho de Melo 	struct evlist		*sb_evlist;
160899e5ffbSArnaldo Carvalho de Melo 	pthread_t		thread_id;
161d20deb64SArnaldo Carvalho de Melo 	int			realtime_prio;
162899e5ffbSArnaldo Carvalho de Melo 	bool			switch_output_event_set;
163d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid;
164d2db9a98SWang Nan 	bool			no_buildid_set;
165d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid_cache;
166d2db9a98SWang Nan 	bool			no_buildid_cache_set;
1676156681bSNamhyung Kim 	bool			buildid_all;
168e29386c8SJiri Olsa 	bool			buildid_mmap;
169ecfd7a9cSWang Nan 	bool			timestamp_filename;
17068588bafSJin Yao 	bool			timestamp_boundary;
171edc41a10SNamhyung Kim 	bool			off_cpu;
1721b43b704SJiri Olsa 	struct switch_output	switch_output;
1739f065194SYang Shi 	unsigned long long	samples;
1746d575816SJiwei Sun 	unsigned long		output_max_size;	/* = 0: unlimited */
1759bce13eaSJiri Olsa 	struct perf_debuginfod	debuginfod;
1767954f716SAlexey Bayduraev 	int			nr_threads;
1777954f716SAlexey Bayduraev 	struct thread_mask	*thread_masks;
178415ccb58SAlexey Bayduraev 	struct record_thread	*thread_data;
1796562c9acSAdrian Hunter 	struct pollfd_index_map	*index_map;
1806562c9acSAdrian Hunter 	size_t			index_map_sz;
1816562c9acSAdrian Hunter 	size_t			index_map_cnt;
1820f82ebc4SArnaldo Carvalho de Melo };
18386470930SIngo Molnar 
1846d575816SJiwei Sun static volatile int done;
1856d575816SJiwei Sun 
186dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started;
187dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
188dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger);
189dc0c6127SJiri Olsa 
1909d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = {
1919d2ed645SAlexey Budankov 	"SYS", "NODE", "CPU"
1929d2ed645SAlexey Budankov };
1939d2ed645SAlexey Budankov 
194415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID
195415ccb58SAlexey Bayduraev static inline pid_t gettid(void)
196415ccb58SAlexey Bayduraev {
197415ccb58SAlexey Bayduraev 	return (pid_t)syscall(__NR_gettid);
198415ccb58SAlexey Bayduraev }
199415ccb58SAlexey Bayduraev #endif
200415ccb58SAlexey Bayduraev 
2013217e9feSAlexey Bayduraev static int record__threads_enabled(struct record *rec)
2023217e9feSAlexey Bayduraev {
2033217e9feSAlexey Bayduraev 	return rec->opts.threads_spec;
2043217e9feSAlexey Bayduraev }
2053217e9feSAlexey Bayduraev 
206dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec)
207dc0c6127SJiri Olsa {
208dc0c6127SJiri Olsa 	return rec->switch_output.signal &&
209dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
210dc0c6127SJiri Olsa }
211dc0c6127SJiri Olsa 
212dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec)
213dc0c6127SJiri Olsa {
214dc0c6127SJiri Olsa 	return rec->switch_output.size &&
215dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger) &&
216dc0c6127SJiri Olsa 	       (rec->bytes_written >= rec->switch_output.size);
217dc0c6127SJiri Olsa }
218dc0c6127SJiri Olsa 
219bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec)
220bfacbe3bSJiri Olsa {
221bfacbe3bSJiri Olsa 	return rec->switch_output.time &&
222bfacbe3bSJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
223bfacbe3bSJiri Olsa }
224bfacbe3bSJiri Olsa 
225ae9c7242SAlexey Bayduraev static u64 record__bytes_written(struct record *rec)
226ae9c7242SAlexey Bayduraev {
227ae9c7242SAlexey Bayduraev 	int t;
228ae9c7242SAlexey Bayduraev 	u64 bytes_written = rec->bytes_written;
229ae9c7242SAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
230ae9c7242SAlexey Bayduraev 
231ae9c7242SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++)
232ae9c7242SAlexey Bayduraev 		bytes_written += thread_data[t].bytes_written;
233ae9c7242SAlexey Bayduraev 
234ae9c7242SAlexey Bayduraev 	return bytes_written;
235ae9c7242SAlexey Bayduraev }
236ae9c7242SAlexey Bayduraev 
2376d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec)
2386d575816SJiwei Sun {
2396d575816SJiwei Sun 	return rec->output_max_size &&
240ae9c7242SAlexey Bayduraev 	       (record__bytes_written(rec) >= rec->output_max_size);
2416d575816SJiwei Sun }
2426d575816SJiwei Sun 
243a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused,
244ded2b8feSJiri Olsa 			 void *bf, size_t size)
245f5970550SPeter Zijlstra {
246ded2b8feSJiri Olsa 	struct perf_data_file *file = &rec->session->data->file;
247ded2b8feSJiri Olsa 
24856f735ffSAlexey Bayduraev 	if (map && map->file)
24956f735ffSAlexey Bayduraev 		file = map->file;
25056f735ffSAlexey Bayduraev 
251ded2b8feSJiri Olsa 	if (perf_data_file__write(file, bf, size) < 0) {
2524f624685SAdrian Hunter 		pr_err("failed to write perf data, error: %m\n");
2538d3eca20SDavid Ahern 		return -1;
2548d3eca20SDavid Ahern 	}
255f5970550SPeter Zijlstra 
256ae9c7242SAlexey Bayduraev 	if (map && map->file)
257ae9c7242SAlexey Bayduraev 		thread->bytes_written += size;
258ae9c7242SAlexey Bayduraev 	else
259cf8b2e69SArnaldo Carvalho de Melo 		rec->bytes_written += size;
260dc0c6127SJiri Olsa 
2616d575816SJiwei Sun 	if (record__output_max_size_exceeded(rec) && !done) {
2626d575816SJiwei Sun 		fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
2636d575816SJiwei Sun 				" stopping session ]\n",
264ae9c7242SAlexey Bayduraev 				record__bytes_written(rec) >> 10);
2656d575816SJiwei Sun 		done = 1;
2666d575816SJiwei Sun 	}
2676d575816SJiwei Sun 
268dc0c6127SJiri Olsa 	if (switch_output_size(rec))
269dc0c6127SJiri Olsa 		trigger_hit(&switch_output_trigger);
270dc0c6127SJiri Olsa 
2718d3eca20SDavid Ahern 	return 0;
272f5970550SPeter Zijlstra }
273f5970550SPeter Zijlstra 
274ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec);
275ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec);
27675f5f1fcSAlexey Bayduraev static size_t zstd_compress(struct perf_session *session, struct mmap *map,
27775f5f1fcSAlexey Bayduraev 			    void *dst, size_t dst_size, void *src, size_t src_size);
2785d7f4116SAlexey Budankov 
279d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
280d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd,
281d3d1af6fSAlexey Budankov 		void *buf, size_t size, off_t off)
282d3d1af6fSAlexey Budankov {
283d3d1af6fSAlexey Budankov 	int rc;
284d3d1af6fSAlexey Budankov 
285d3d1af6fSAlexey Budankov 	cblock->aio_fildes = trace_fd;
286d3d1af6fSAlexey Budankov 	cblock->aio_buf    = buf;
287d3d1af6fSAlexey Budankov 	cblock->aio_nbytes = size;
288d3d1af6fSAlexey Budankov 	cblock->aio_offset = off;
289d3d1af6fSAlexey Budankov 	cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
290d3d1af6fSAlexey Budankov 
291d3d1af6fSAlexey Budankov 	do {
292d3d1af6fSAlexey Budankov 		rc = aio_write(cblock);
293d3d1af6fSAlexey Budankov 		if (rc == 0) {
294d3d1af6fSAlexey Budankov 			break;
295d3d1af6fSAlexey Budankov 		} else if (errno != EAGAIN) {
296d3d1af6fSAlexey Budankov 			cblock->aio_fildes = -1;
297d3d1af6fSAlexey Budankov 			pr_err("failed to queue perf data, error: %m\n");
298d3d1af6fSAlexey Budankov 			break;
299d3d1af6fSAlexey Budankov 		}
300d3d1af6fSAlexey Budankov 	} while (1);
301d3d1af6fSAlexey Budankov 
302d3d1af6fSAlexey Budankov 	return rc;
303d3d1af6fSAlexey Budankov }
304d3d1af6fSAlexey Budankov 
305a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock)
306d3d1af6fSAlexey Budankov {
307d3d1af6fSAlexey Budankov 	void *rem_buf;
308d3d1af6fSAlexey Budankov 	off_t rem_off;
309d3d1af6fSAlexey Budankov 	size_t rem_size;
310d3d1af6fSAlexey Budankov 	int rc, aio_errno;
311d3d1af6fSAlexey Budankov 	ssize_t aio_ret, written;
312d3d1af6fSAlexey Budankov 
313d3d1af6fSAlexey Budankov 	aio_errno = aio_error(cblock);
314d3d1af6fSAlexey Budankov 	if (aio_errno == EINPROGRESS)
315d3d1af6fSAlexey Budankov 		return 0;
316d3d1af6fSAlexey Budankov 
317d3d1af6fSAlexey Budankov 	written = aio_ret = aio_return(cblock);
318d3d1af6fSAlexey Budankov 	if (aio_ret < 0) {
319d3d1af6fSAlexey Budankov 		if (aio_errno != EINTR)
320d3d1af6fSAlexey Budankov 			pr_err("failed to write perf data, error: %m\n");
321d3d1af6fSAlexey Budankov 		written = 0;
322d3d1af6fSAlexey Budankov 	}
323d3d1af6fSAlexey Budankov 
324d3d1af6fSAlexey Budankov 	rem_size = cblock->aio_nbytes - written;
325d3d1af6fSAlexey Budankov 
326d3d1af6fSAlexey Budankov 	if (rem_size == 0) {
327d3d1af6fSAlexey Budankov 		cblock->aio_fildes = -1;
328d3d1af6fSAlexey Budankov 		/*
329ef781128SAlexey Budankov 		 * md->refcount is incremented in record__aio_pushfn() for
330ef781128SAlexey Budankov 		 * every aio write request started in record__aio_push() so
331ef781128SAlexey Budankov 		 * decrement it because the request is now complete.
332d3d1af6fSAlexey Budankov 		 */
33380e53d11SJiri Olsa 		perf_mmap__put(&md->core);
334d3d1af6fSAlexey Budankov 		rc = 1;
335d3d1af6fSAlexey Budankov 	} else {
336d3d1af6fSAlexey Budankov 		/*
337d3d1af6fSAlexey Budankov 		 * aio write request may require restart with the
338d3d1af6fSAlexey Budankov 		 * reminder if the kernel didn't write whole
339d3d1af6fSAlexey Budankov 		 * chunk at once.
340d3d1af6fSAlexey Budankov 		 */
341d3d1af6fSAlexey Budankov 		rem_off = cblock->aio_offset + written;
342d3d1af6fSAlexey Budankov 		rem_buf = (void *)(cblock->aio_buf + written);
343d3d1af6fSAlexey Budankov 		record__aio_write(cblock, cblock->aio_fildes,
344d3d1af6fSAlexey Budankov 				rem_buf, rem_size, rem_off);
345d3d1af6fSAlexey Budankov 		rc = 0;
346d3d1af6fSAlexey Budankov 	}
347d3d1af6fSAlexey Budankov 
348d3d1af6fSAlexey Budankov 	return rc;
349d3d1af6fSAlexey Budankov }
350d3d1af6fSAlexey Budankov 
351a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all)
352d3d1af6fSAlexey Budankov {
35393f20c0fSAlexey Budankov 	struct aiocb **aiocb = md->aio.aiocb;
35493f20c0fSAlexey Budankov 	struct aiocb *cblocks = md->aio.cblocks;
355d3d1af6fSAlexey Budankov 	struct timespec timeout = { 0, 1000 * 1000  * 1 }; /* 1ms */
35693f20c0fSAlexey Budankov 	int i, do_suspend;
357d3d1af6fSAlexey Budankov 
358d3d1af6fSAlexey Budankov 	do {
35993f20c0fSAlexey Budankov 		do_suspend = 0;
36093f20c0fSAlexey Budankov 		for (i = 0; i < md->aio.nr_cblocks; ++i) {
36193f20c0fSAlexey Budankov 			if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
36293f20c0fSAlexey Budankov 				if (sync_all)
36393f20c0fSAlexey Budankov 					aiocb[i] = NULL;
36493f20c0fSAlexey Budankov 				else
36593f20c0fSAlexey Budankov 					return i;
36693f20c0fSAlexey Budankov 			} else {
36793f20c0fSAlexey Budankov 				/*
36893f20c0fSAlexey Budankov 				 * Started aio write is not complete yet
36993f20c0fSAlexey Budankov 				 * so it has to be waited before the
37093f20c0fSAlexey Budankov 				 * next allocation.
37193f20c0fSAlexey Budankov 				 */
37293f20c0fSAlexey Budankov 				aiocb[i] = &cblocks[i];
37393f20c0fSAlexey Budankov 				do_suspend = 1;
37493f20c0fSAlexey Budankov 			}
37593f20c0fSAlexey Budankov 		}
37693f20c0fSAlexey Budankov 		if (!do_suspend)
37793f20c0fSAlexey Budankov 			return -1;
378d3d1af6fSAlexey Budankov 
37993f20c0fSAlexey Budankov 		while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) {
380d3d1af6fSAlexey Budankov 			if (!(errno == EAGAIN || errno == EINTR))
381d3d1af6fSAlexey Budankov 				pr_err("failed to sync perf data, error: %m\n");
382d3d1af6fSAlexey Budankov 		}
383d3d1af6fSAlexey Budankov 	} while (1);
384d3d1af6fSAlexey Budankov }
385d3d1af6fSAlexey Budankov 
386ef781128SAlexey Budankov struct record_aio {
387ef781128SAlexey Budankov 	struct record	*rec;
388ef781128SAlexey Budankov 	void		*data;
389ef781128SAlexey Budankov 	size_t		size;
390ef781128SAlexey Budankov };
391ef781128SAlexey Budankov 
392a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size)
393d3d1af6fSAlexey Budankov {
394ef781128SAlexey Budankov 	struct record_aio *aio = to;
395ef781128SAlexey Budankov 
396ef781128SAlexey Budankov 	/*
397547740f7SJiri Olsa 	 * map->core.base data pointed by buf is copied into free map->aio.data[] buffer
398ef781128SAlexey Budankov 	 * to release space in the kernel buffer as fast as possible, calling
399ef781128SAlexey Budankov 	 * perf_mmap__consume() from perf_mmap__push() function.
400ef781128SAlexey Budankov 	 *
401ef781128SAlexey Budankov 	 * That lets the kernel to proceed with storing more profiling data into
402ef781128SAlexey Budankov 	 * the kernel buffer earlier than other per-cpu kernel buffers are handled.
403ef781128SAlexey Budankov 	 *
404ef781128SAlexey Budankov 	 * Coping can be done in two steps in case the chunk of profiling data
405ef781128SAlexey Budankov 	 * crosses the upper bound of the kernel buffer. In this case we first move
406ef781128SAlexey Budankov 	 * part of data from map->start till the upper bound and then the reminder
407ef781128SAlexey Budankov 	 * from the beginning of the kernel buffer till the end of the data chunk.
408ef781128SAlexey Budankov 	 */
409ef781128SAlexey Budankov 
410ef781128SAlexey Budankov 	if (record__comp_enabled(aio->rec)) {
41175f5f1fcSAlexey Bayduraev 		size = zstd_compress(aio->rec->session, NULL, aio->data + aio->size,
412bf59b305SJiri Olsa 				     mmap__mmap_len(map) - aio->size,
413ef781128SAlexey Budankov 				     buf, size);
414ef781128SAlexey Budankov 	} else {
415ef781128SAlexey Budankov 		memcpy(aio->data + aio->size, buf, size);
416ef781128SAlexey Budankov 	}
417ef781128SAlexey Budankov 
418ef781128SAlexey Budankov 	if (!aio->size) {
419ef781128SAlexey Budankov 		/*
420ef781128SAlexey Budankov 		 * Increment map->refcount to guard map->aio.data[] buffer
421ef781128SAlexey Budankov 		 * from premature deallocation because map object can be
422ef781128SAlexey Budankov 		 * released earlier than aio write request started on
423ef781128SAlexey Budankov 		 * map->aio.data[] buffer is complete.
424ef781128SAlexey Budankov 		 *
425ef781128SAlexey Budankov 		 * perf_mmap__put() is done at record__aio_complete()
426ef781128SAlexey Budankov 		 * after started aio request completion or at record__aio_push()
427ef781128SAlexey Budankov 		 * if the request failed to start.
428ef781128SAlexey Budankov 		 */
429e75710f0SJiri Olsa 		perf_mmap__get(&map->core);
430ef781128SAlexey Budankov 	}
431ef781128SAlexey Budankov 
432ef781128SAlexey Budankov 	aio->size += size;
433ef781128SAlexey Budankov 
434ef781128SAlexey Budankov 	return size;
435ef781128SAlexey Budankov }
436ef781128SAlexey Budankov 
437a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off)
438ef781128SAlexey Budankov {
439ef781128SAlexey Budankov 	int ret, idx;
440ef781128SAlexey Budankov 	int trace_fd = rec->session->data->file.fd;
441ef781128SAlexey Budankov 	struct record_aio aio = { .rec = rec, .size = 0 };
442ef781128SAlexey Budankov 
443ef781128SAlexey Budankov 	/*
444ef781128SAlexey Budankov 	 * Call record__aio_sync() to wait till map->aio.data[] buffer
445ef781128SAlexey Budankov 	 * becomes available after previous aio write operation.
446ef781128SAlexey Budankov 	 */
447ef781128SAlexey Budankov 
448ef781128SAlexey Budankov 	idx = record__aio_sync(map, false);
449ef781128SAlexey Budankov 	aio.data = map->aio.data[idx];
450ef781128SAlexey Budankov 	ret = perf_mmap__push(map, &aio, record__aio_pushfn);
451ef781128SAlexey Budankov 	if (ret != 0) /* ret > 0 - no data, ret < 0 - error */
452ef781128SAlexey Budankov 		return ret;
453d3d1af6fSAlexey Budankov 
454d3d1af6fSAlexey Budankov 	rec->samples++;
455ef781128SAlexey Budankov 	ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off);
456d3d1af6fSAlexey Budankov 	if (!ret) {
457ef781128SAlexey Budankov 		*off += aio.size;
458ef781128SAlexey Budankov 		rec->bytes_written += aio.size;
459d3d1af6fSAlexey Budankov 		if (switch_output_size(rec))
460d3d1af6fSAlexey Budankov 			trigger_hit(&switch_output_trigger);
461ef781128SAlexey Budankov 	} else {
462ef781128SAlexey Budankov 		/*
463ef781128SAlexey Budankov 		 * Decrement map->refcount incremented in record__aio_pushfn()
464ef781128SAlexey Budankov 		 * back if record__aio_write() operation failed to start, otherwise
465ef781128SAlexey Budankov 		 * map->refcount is decremented in record__aio_complete() after
466ef781128SAlexey Budankov 		 * aio write operation finishes successfully.
467ef781128SAlexey Budankov 		 */
46880e53d11SJiri Olsa 		perf_mmap__put(&map->core);
469d3d1af6fSAlexey Budankov 	}
470d3d1af6fSAlexey Budankov 
471d3d1af6fSAlexey Budankov 	return ret;
472d3d1af6fSAlexey Budankov }
473d3d1af6fSAlexey Budankov 
474d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd)
475d3d1af6fSAlexey Budankov {
476d3d1af6fSAlexey Budankov 	return lseek(trace_fd, 0, SEEK_CUR);
477d3d1af6fSAlexey Budankov }
478d3d1af6fSAlexey Budankov 
479d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos)
480d3d1af6fSAlexey Budankov {
481d3d1af6fSAlexey Budankov 	lseek(trace_fd, pos, SEEK_SET);
482d3d1af6fSAlexey Budankov }
483d3d1af6fSAlexey Budankov 
484d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec)
485d3d1af6fSAlexey Budankov {
486d3d1af6fSAlexey Budankov 	int i;
48763503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
488a5830532SJiri Olsa 	struct mmap *maps = evlist->mmap;
489d3d1af6fSAlexey Budankov 
490ef781128SAlexey Budankov 	if (!record__aio_enabled(rec))
491d3d1af6fSAlexey Budankov 		return;
492d3d1af6fSAlexey Budankov 
493c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
494a5830532SJiri Olsa 		struct mmap *map = &maps[i];
495d3d1af6fSAlexey Budankov 
496547740f7SJiri Olsa 		if (map->core.base)
49793f20c0fSAlexey Budankov 			record__aio_sync(map, true);
498d3d1af6fSAlexey Budankov 	}
499d3d1af6fSAlexey Budankov }
500d3d1af6fSAlexey Budankov 
501d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1;
50293f20c0fSAlexey Budankov static int nr_cblocks_max = 4;
503d3d1af6fSAlexey Budankov 
504d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt,
50593f20c0fSAlexey Budankov 			     const char *str,
506d3d1af6fSAlexey Budankov 			     int unset)
507d3d1af6fSAlexey Budankov {
508d3d1af6fSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
509d3d1af6fSAlexey Budankov 
51093f20c0fSAlexey Budankov 	if (unset) {
511d3d1af6fSAlexey Budankov 		opts->nr_cblocks = 0;
51293f20c0fSAlexey Budankov 	} else {
51393f20c0fSAlexey Budankov 		if (str)
51493f20c0fSAlexey Budankov 			opts->nr_cblocks = strtol(str, NULL, 0);
51593f20c0fSAlexey Budankov 		if (!opts->nr_cblocks)
516d3d1af6fSAlexey Budankov 			opts->nr_cblocks = nr_cblocks_default;
51793f20c0fSAlexey Budankov 	}
518d3d1af6fSAlexey Budankov 
519d3d1af6fSAlexey Budankov 	return 0;
520d3d1af6fSAlexey Budankov }
521d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */
52293f20c0fSAlexey Budankov static int nr_cblocks_max = 0;
52393f20c0fSAlexey Budankov 
524a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused,
525ef781128SAlexey Budankov 			    off_t *off __maybe_unused)
526d3d1af6fSAlexey Budankov {
527d3d1af6fSAlexey Budankov 	return -1;
528d3d1af6fSAlexey Budankov }
529d3d1af6fSAlexey Budankov 
530d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused)
531d3d1af6fSAlexey Budankov {
532d3d1af6fSAlexey Budankov 	return -1;
533d3d1af6fSAlexey Budankov }
534d3d1af6fSAlexey Budankov 
535d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused)
536d3d1af6fSAlexey Budankov {
537d3d1af6fSAlexey Budankov }
538d3d1af6fSAlexey Budankov 
539d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused)
540d3d1af6fSAlexey Budankov {
541d3d1af6fSAlexey Budankov }
542d3d1af6fSAlexey Budankov #endif
543d3d1af6fSAlexey Budankov 
544d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec)
545d3d1af6fSAlexey Budankov {
546d3d1af6fSAlexey Budankov 	return rec->opts.nr_cblocks > 0;
547d3d1af6fSAlexey Budankov }
548d3d1af6fSAlexey Budankov 
549470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1
550470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt,
551470530bbSAlexey Budankov 				    const char *str,
552470530bbSAlexey Budankov 				    int unset)
553470530bbSAlexey Budankov {
554470530bbSAlexey Budankov 	int flush_max;
555470530bbSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
556470530bbSAlexey Budankov 	static struct parse_tag tags[] = {
557470530bbSAlexey Budankov 			{ .tag  = 'B', .mult = 1       },
558470530bbSAlexey Budankov 			{ .tag  = 'K', .mult = 1 << 10 },
559470530bbSAlexey Budankov 			{ .tag  = 'M', .mult = 1 << 20 },
560470530bbSAlexey Budankov 			{ .tag  = 'G', .mult = 1 << 30 },
561470530bbSAlexey Budankov 			{ .tag  = 0 },
562470530bbSAlexey Budankov 	};
563470530bbSAlexey Budankov 
564470530bbSAlexey Budankov 	if (unset)
565470530bbSAlexey Budankov 		return 0;
566470530bbSAlexey Budankov 
567470530bbSAlexey Budankov 	if (str) {
568470530bbSAlexey Budankov 		opts->mmap_flush = parse_tag_value(str, tags);
569470530bbSAlexey Budankov 		if (opts->mmap_flush == (int)-1)
570470530bbSAlexey Budankov 			opts->mmap_flush = strtol(str, NULL, 0);
571470530bbSAlexey Budankov 	}
572470530bbSAlexey Budankov 
573470530bbSAlexey Budankov 	if (!opts->mmap_flush)
574470530bbSAlexey Budankov 		opts->mmap_flush = MMAP_FLUSH_DEFAULT;
575470530bbSAlexey Budankov 
5769521b5f2SJiri Olsa 	flush_max = evlist__mmap_size(opts->mmap_pages);
577470530bbSAlexey Budankov 	flush_max /= 4;
578470530bbSAlexey Budankov 	if (opts->mmap_flush > flush_max)
579470530bbSAlexey Budankov 		opts->mmap_flush = flush_max;
580470530bbSAlexey Budankov 
581470530bbSAlexey Budankov 	return 0;
582470530bbSAlexey Budankov }
583470530bbSAlexey Budankov 
584504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
585504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1;
586504c1ad1SAlexey Budankov 
587504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset)
588504c1ad1SAlexey Budankov {
589504c1ad1SAlexey Budankov 	struct record_opts *opts = opt->value;
590504c1ad1SAlexey Budankov 
591504c1ad1SAlexey Budankov 	if (unset) {
592504c1ad1SAlexey Budankov 		opts->comp_level = 0;
593504c1ad1SAlexey Budankov 	} else {
594504c1ad1SAlexey Budankov 		if (str)
595504c1ad1SAlexey Budankov 			opts->comp_level = strtol(str, NULL, 0);
596504c1ad1SAlexey Budankov 		if (!opts->comp_level)
597504c1ad1SAlexey Budankov 			opts->comp_level = comp_level_default;
598504c1ad1SAlexey Budankov 	}
599504c1ad1SAlexey Budankov 
600504c1ad1SAlexey Budankov 	return 0;
601504c1ad1SAlexey Budankov }
602504c1ad1SAlexey Budankov #endif
60351255a8aSAlexey Budankov static unsigned int comp_level_max = 22;
60451255a8aSAlexey Budankov 
60542e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec)
60642e1fd80SAlexey Budankov {
60742e1fd80SAlexey Budankov 	return rec->opts.comp_level > 0;
60842e1fd80SAlexey Budankov }
60942e1fd80SAlexey Budankov 
61045694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool,
611d20deb64SArnaldo Carvalho de Melo 				     union perf_event *event,
6121d037ca1SIrina Tirdea 				     struct perf_sample *sample __maybe_unused,
6131d037ca1SIrina Tirdea 				     struct machine *machine __maybe_unused)
614234fbbf5SArnaldo Carvalho de Melo {
6158c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = container_of(tool, struct record, tool);
616ded2b8feSJiri Olsa 	return record__write(rec, NULL, event, event->header.size);
617234fbbf5SArnaldo Carvalho de Melo }
618234fbbf5SArnaldo Carvalho de Melo 
619d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool,
620d99c22eaSStephane Eranian 				     union perf_event *event,
621d99c22eaSStephane Eranian 				     struct perf_sample *sample __maybe_unused,
622d99c22eaSStephane Eranian 				     struct machine *machine __maybe_unused)
623d99c22eaSStephane Eranian {
624d99c22eaSStephane Eranian 	static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER;
625d99c22eaSStephane Eranian 	int ret;
626d99c22eaSStephane Eranian 
627d99c22eaSStephane Eranian 	pthread_mutex_lock(&synth_lock);
628d99c22eaSStephane Eranian 	ret = process_synthesized_event(tool, event, sample, machine);
629d99c22eaSStephane Eranian 	pthread_mutex_unlock(&synth_lock);
630d99c22eaSStephane Eranian 	return ret;
631d99c22eaSStephane Eranian }
632d99c22eaSStephane Eranian 
633a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
634d37f1586SArnaldo Carvalho de Melo {
635d37f1586SArnaldo Carvalho de Melo 	struct record *rec = to;
636d37f1586SArnaldo Carvalho de Melo 
6375d7f4116SAlexey Budankov 	if (record__comp_enabled(rec)) {
63875f5f1fcSAlexey Bayduraev 		size = zstd_compress(rec->session, map, map->data, mmap__mmap_len(map), bf, size);
6395d7f4116SAlexey Budankov 		bf   = map->data;
6405d7f4116SAlexey Budankov 	}
6415d7f4116SAlexey Budankov 
642396b626bSAlexey Bayduraev 	thread->samples++;
643ded2b8feSJiri Olsa 	return record__write(rec, map, bf, size);
644d37f1586SArnaldo Carvalho de Melo }
645d37f1586SArnaldo Carvalho de Melo 
6462dd6d8a1SAdrian Hunter static volatile int signr = -1;
6472dd6d8a1SAdrian Hunter static volatile int child_finished;
648da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
649da231338SAnand K Mistry static int done_fd = -1;
650da231338SAnand K Mistry #endif
651c0bdc1c4SWang Nan 
6522dd6d8a1SAdrian Hunter static void sig_handler(int sig)
6532dd6d8a1SAdrian Hunter {
6542dd6d8a1SAdrian Hunter 	if (sig == SIGCHLD)
6552dd6d8a1SAdrian Hunter 		child_finished = 1;
6562dd6d8a1SAdrian Hunter 	else
6572dd6d8a1SAdrian Hunter 		signr = sig;
6582dd6d8a1SAdrian Hunter 
6592dd6d8a1SAdrian Hunter 	done = 1;
660da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
661da231338SAnand K Mistry {
662da231338SAnand K Mistry 	u64 tmp = 1;
663da231338SAnand K Mistry 	/*
664da231338SAnand K Mistry 	 * It is possible for this signal handler to run after done is checked
665da231338SAnand K Mistry 	 * in the main loop, but before the perf counter fds are polled. If this
666da231338SAnand K Mistry 	 * happens, the poll() will continue to wait even though done is set,
667da231338SAnand K Mistry 	 * and will only break out if either another signal is received, or the
668da231338SAnand K Mistry 	 * counters are ready for read. To ensure the poll() doesn't sleep when
669da231338SAnand K Mistry 	 * done is set, use an eventfd (done_fd) to wake up the poll().
670da231338SAnand K Mistry 	 */
671da231338SAnand K Mistry 	if (write(done_fd, &tmp, sizeof(tmp)) < 0)
672da231338SAnand K Mistry 		pr_err("failed to signal wakeup fd, error: %m\n");
673da231338SAnand K Mistry }
674da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT
6752dd6d8a1SAdrian Hunter }
6762dd6d8a1SAdrian Hunter 
677a074865eSWang Nan static void sigsegv_handler(int sig)
678a074865eSWang Nan {
679a074865eSWang Nan 	perf_hooks__recover();
680a074865eSWang Nan 	sighandler_dump_stack(sig);
681a074865eSWang Nan }
682a074865eSWang Nan 
6832dd6d8a1SAdrian Hunter static void record__sig_exit(void)
6842dd6d8a1SAdrian Hunter {
6852dd6d8a1SAdrian Hunter 	if (signr == -1)
6862dd6d8a1SAdrian Hunter 		return;
6872dd6d8a1SAdrian Hunter 
6882dd6d8a1SAdrian Hunter 	signal(signr, SIG_DFL);
6892dd6d8a1SAdrian Hunter 	raise(signr);
6902dd6d8a1SAdrian Hunter }
6912dd6d8a1SAdrian Hunter 
692e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT
693e31f0d01SAdrian Hunter 
694ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool,
695a5830532SJiri Olsa 				    struct mmap *map,
696ef149c25SAdrian Hunter 				    union perf_event *event, void *data1,
697ef149c25SAdrian Hunter 				    size_t len1, void *data2, size_t len2)
698ef149c25SAdrian Hunter {
699ef149c25SAdrian Hunter 	struct record *rec = container_of(tool, struct record, tool);
7008ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
701ef149c25SAdrian Hunter 	size_t padding;
702ef149c25SAdrian Hunter 	u8 pad[8] = {0};
703ef149c25SAdrian Hunter 
70446e201efSAdrian Hunter 	if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) {
70599fa2984SAdrian Hunter 		off_t file_offset;
7068ceb41d7SJiri Olsa 		int fd = perf_data__fd(data);
70799fa2984SAdrian Hunter 		int err;
70899fa2984SAdrian Hunter 
70999fa2984SAdrian Hunter 		file_offset = lseek(fd, 0, SEEK_CUR);
71099fa2984SAdrian Hunter 		if (file_offset == -1)
71199fa2984SAdrian Hunter 			return -1;
71299fa2984SAdrian Hunter 		err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
71399fa2984SAdrian Hunter 						     event, file_offset);
71499fa2984SAdrian Hunter 		if (err)
71599fa2984SAdrian Hunter 			return err;
71699fa2984SAdrian Hunter 	}
71799fa2984SAdrian Hunter 
718ef149c25SAdrian Hunter 	/* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
719ef149c25SAdrian Hunter 	padding = (len1 + len2) & 7;
720ef149c25SAdrian Hunter 	if (padding)
721ef149c25SAdrian Hunter 		padding = 8 - padding;
722ef149c25SAdrian Hunter 
723ded2b8feSJiri Olsa 	record__write(rec, map, event, event->header.size);
724ded2b8feSJiri Olsa 	record__write(rec, map, data1, len1);
725ef149c25SAdrian Hunter 	if (len2)
726ded2b8feSJiri Olsa 		record__write(rec, map, data2, len2);
727ded2b8feSJiri Olsa 	record__write(rec, map, &pad, padding);
728ef149c25SAdrian Hunter 
729ef149c25SAdrian Hunter 	return 0;
730ef149c25SAdrian Hunter }
731ef149c25SAdrian Hunter 
732ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec,
733a5830532SJiri Olsa 				      struct mmap *map)
734ef149c25SAdrian Hunter {
735ef149c25SAdrian Hunter 	int ret;
736ef149c25SAdrian Hunter 
737e035f4caSJiri Olsa 	ret = auxtrace_mmap__read(map, rec->itr, &rec->tool,
738ef149c25SAdrian Hunter 				  record__process_auxtrace);
739ef149c25SAdrian Hunter 	if (ret < 0)
740ef149c25SAdrian Hunter 		return ret;
741ef149c25SAdrian Hunter 
742ef149c25SAdrian Hunter 	if (ret)
743ef149c25SAdrian Hunter 		rec->samples++;
744ef149c25SAdrian Hunter 
745ef149c25SAdrian Hunter 	return 0;
746ef149c25SAdrian Hunter }
747ef149c25SAdrian Hunter 
7482dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec,
749a5830532SJiri Olsa 					       struct mmap *map)
7502dd6d8a1SAdrian Hunter {
7512dd6d8a1SAdrian Hunter 	int ret;
7522dd6d8a1SAdrian Hunter 
753e035f4caSJiri Olsa 	ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool,
7542dd6d8a1SAdrian Hunter 					   record__process_auxtrace,
7552dd6d8a1SAdrian Hunter 					   rec->opts.auxtrace_snapshot_size);
7562dd6d8a1SAdrian Hunter 	if (ret < 0)
7572dd6d8a1SAdrian Hunter 		return ret;
7582dd6d8a1SAdrian Hunter 
7592dd6d8a1SAdrian Hunter 	if (ret)
7602dd6d8a1SAdrian Hunter 		rec->samples++;
7612dd6d8a1SAdrian Hunter 
7622dd6d8a1SAdrian Hunter 	return 0;
7632dd6d8a1SAdrian Hunter }
7642dd6d8a1SAdrian Hunter 
7652dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec)
7662dd6d8a1SAdrian Hunter {
7672dd6d8a1SAdrian Hunter 	int i;
7682dd6d8a1SAdrian Hunter 	int rc = 0;
7692dd6d8a1SAdrian Hunter 
770c976ee11SJiri Olsa 	for (i = 0; i < rec->evlist->core.nr_mmaps; i++) {
771a5830532SJiri Olsa 		struct mmap *map = &rec->evlist->mmap[i];
7722dd6d8a1SAdrian Hunter 
773e035f4caSJiri Olsa 		if (!map->auxtrace_mmap.base)
7742dd6d8a1SAdrian Hunter 			continue;
7752dd6d8a1SAdrian Hunter 
776e035f4caSJiri Olsa 		if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) {
7772dd6d8a1SAdrian Hunter 			rc = -1;
7782dd6d8a1SAdrian Hunter 			goto out;
7792dd6d8a1SAdrian Hunter 		}
7802dd6d8a1SAdrian Hunter 	}
7812dd6d8a1SAdrian Hunter out:
7822dd6d8a1SAdrian Hunter 	return rc;
7832dd6d8a1SAdrian Hunter }
7842dd6d8a1SAdrian Hunter 
785ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit)
7862dd6d8a1SAdrian Hunter {
7872dd6d8a1SAdrian Hunter 	pr_debug("Recording AUX area tracing snapshot\n");
7882dd6d8a1SAdrian Hunter 	if (record__auxtrace_read_snapshot_all(rec) < 0) {
7895f9cf599SWang Nan 		trigger_error(&auxtrace_snapshot_trigger);
7902dd6d8a1SAdrian Hunter 	} else {
791ce7b0e42SAlexander Shishkin 		if (auxtrace_record__snapshot_finish(rec->itr, on_exit))
7925f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
7935f9cf599SWang Nan 		else
7945f9cf599SWang Nan 			trigger_ready(&auxtrace_snapshot_trigger);
7952dd6d8a1SAdrian Hunter 	}
7962dd6d8a1SAdrian Hunter }
7972dd6d8a1SAdrian Hunter 
798ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec)
799ce7b0e42SAlexander Shishkin {
800ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
801ce7b0e42SAlexander Shishkin 		return 0;
802ce7b0e42SAlexander Shishkin 
803ce7b0e42SAlexander Shishkin 	if (!auxtrace_record__snapshot_started &&
804ce7b0e42SAlexander Shishkin 	    auxtrace_record__snapshot_start(rec->itr))
805ce7b0e42SAlexander Shishkin 		return -1;
806ce7b0e42SAlexander Shishkin 
807ce7b0e42SAlexander Shishkin 	record__read_auxtrace_snapshot(rec, true);
808ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
809ce7b0e42SAlexander Shishkin 		return -1;
810ce7b0e42SAlexander Shishkin 
811ce7b0e42SAlexander Shishkin 	return 0;
812ce7b0e42SAlexander Shishkin }
813ce7b0e42SAlexander Shishkin 
8144b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec)
8154b5ea3bdSAdrian Hunter {
8164b5ea3bdSAdrian Hunter 	int err;
8174b5ea3bdSAdrian Hunter 
818b5f2511dSAlexey Bayduraev 	if ((rec->opts.auxtrace_snapshot_opts || rec->opts.auxtrace_sample_opts)
819b5f2511dSAlexey Bayduraev 	    && record__threads_enabled(rec)) {
820b5f2511dSAlexey Bayduraev 		pr_err("AUX area tracing options are not available in parallel streaming mode.\n");
821b5f2511dSAlexey Bayduraev 		return -EINVAL;
822b5f2511dSAlexey Bayduraev 	}
823b5f2511dSAlexey Bayduraev 
8244b5ea3bdSAdrian Hunter 	if (!rec->itr) {
8254b5ea3bdSAdrian Hunter 		rec->itr = auxtrace_record__init(rec->evlist, &err);
8264b5ea3bdSAdrian Hunter 		if (err)
8274b5ea3bdSAdrian Hunter 			return err;
8284b5ea3bdSAdrian Hunter 	}
8294b5ea3bdSAdrian Hunter 
8304b5ea3bdSAdrian Hunter 	err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
8314b5ea3bdSAdrian Hunter 					      rec->opts.auxtrace_snapshot_opts);
8324b5ea3bdSAdrian Hunter 	if (err)
8334b5ea3bdSAdrian Hunter 		return err;
8344b5ea3bdSAdrian Hunter 
835c0a6de06SAdrian Hunter 	err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts,
836c0a6de06SAdrian Hunter 					    rec->opts.auxtrace_sample_opts);
837c0a6de06SAdrian Hunter 	if (err)
838c0a6de06SAdrian Hunter 		return err;
839c0a6de06SAdrian Hunter 
840d58b3f7eSAdrian Hunter 	auxtrace_regroup_aux_output(rec->evlist);
841d58b3f7eSAdrian Hunter 
8424b5ea3bdSAdrian Hunter 	return auxtrace_parse_filters(rec->evlist);
8434b5ea3bdSAdrian Hunter }
8444b5ea3bdSAdrian Hunter 
845e31f0d01SAdrian Hunter #else
846e31f0d01SAdrian Hunter 
847e31f0d01SAdrian Hunter static inline
848e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
849a5830532SJiri Olsa 			       struct mmap *map __maybe_unused)
850e31f0d01SAdrian Hunter {
851e31f0d01SAdrian Hunter 	return 0;
852e31f0d01SAdrian Hunter }
853e31f0d01SAdrian Hunter 
8542dd6d8a1SAdrian Hunter static inline
855ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused,
856ce7b0e42SAlexander Shishkin 				    bool on_exit __maybe_unused)
8572dd6d8a1SAdrian Hunter {
8582dd6d8a1SAdrian Hunter }
8592dd6d8a1SAdrian Hunter 
8602dd6d8a1SAdrian Hunter static inline
8612dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
8622dd6d8a1SAdrian Hunter {
8632dd6d8a1SAdrian Hunter 	return 0;
8642dd6d8a1SAdrian Hunter }
8652dd6d8a1SAdrian Hunter 
866ce7b0e42SAlexander Shishkin static inline
867ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused)
868ce7b0e42SAlexander Shishkin {
869ce7b0e42SAlexander Shishkin 	return 0;
870ce7b0e42SAlexander Shishkin }
871ce7b0e42SAlexander Shishkin 
8724b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused)
8734b5ea3bdSAdrian Hunter {
8744b5ea3bdSAdrian Hunter 	return 0;
8754b5ea3bdSAdrian Hunter }
8764b5ea3bdSAdrian Hunter 
877e31f0d01SAdrian Hunter #endif
878e31f0d01SAdrian Hunter 
879246eba8eSAdrian Hunter static int record__config_text_poke(struct evlist *evlist)
880246eba8eSAdrian Hunter {
881246eba8eSAdrian Hunter 	struct evsel *evsel;
882246eba8eSAdrian Hunter 
883246eba8eSAdrian Hunter 	/* Nothing to do if text poke is already configured */
884246eba8eSAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
885246eba8eSAdrian Hunter 		if (evsel->core.attr.text_poke)
886246eba8eSAdrian Hunter 			return 0;
887246eba8eSAdrian Hunter 	}
888246eba8eSAdrian Hunter 
889921e3be5SAdrian Hunter 	evsel = evlist__add_dummy_on_all_cpus(evlist);
890921e3be5SAdrian Hunter 	if (!evsel)
891921e3be5SAdrian Hunter 		return -ENOMEM;
892246eba8eSAdrian Hunter 
893246eba8eSAdrian Hunter 	evsel->core.attr.text_poke = 1;
894246eba8eSAdrian Hunter 	evsel->core.attr.ksymbol = 1;
895246eba8eSAdrian Hunter 	evsel->immediate = true;
896246eba8eSAdrian Hunter 	evsel__set_sample_bit(evsel, TIME);
897246eba8eSAdrian Hunter 
898246eba8eSAdrian Hunter 	return 0;
899246eba8eSAdrian Hunter }
900246eba8eSAdrian Hunter 
901edc41a10SNamhyung Kim static int record__config_off_cpu(struct record *rec)
902edc41a10SNamhyung Kim {
903685439a7SNamhyung Kim 	return off_cpu_prepare(rec->evlist, &rec->opts.target, &rec->opts);
904edc41a10SNamhyung Kim }
905edc41a10SNamhyung Kim 
906eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine)
907eeb399b5SAdrian Hunter {
908eeb399b5SAdrian Hunter 	char kcore[PATH_MAX];
909eeb399b5SAdrian Hunter 	int fd;
910eeb399b5SAdrian Hunter 
911eeb399b5SAdrian Hunter 	scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir);
912eeb399b5SAdrian Hunter 
913eeb399b5SAdrian Hunter 	fd = open(kcore, O_RDONLY);
914eeb399b5SAdrian Hunter 	if (fd < 0)
915eeb399b5SAdrian Hunter 		return false;
916eeb399b5SAdrian Hunter 
917eeb399b5SAdrian Hunter 	close(fd);
918eeb399b5SAdrian Hunter 
919eeb399b5SAdrian Hunter 	return true;
920eeb399b5SAdrian Hunter }
921eeb399b5SAdrian Hunter 
922eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data)
923eeb399b5SAdrian Hunter {
924eeb399b5SAdrian Hunter 	char from_dir[PATH_MAX];
925eeb399b5SAdrian Hunter 	char kcore_dir[PATH_MAX];
926eeb399b5SAdrian Hunter 	int ret;
927eeb399b5SAdrian Hunter 
928eeb399b5SAdrian Hunter 	snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir);
929eeb399b5SAdrian Hunter 
930eeb399b5SAdrian Hunter 	ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir));
931eeb399b5SAdrian Hunter 	if (ret)
932eeb399b5SAdrian Hunter 		return ret;
933eeb399b5SAdrian Hunter 
934eeb399b5SAdrian Hunter 	return kcore_copy(from_dir, kcore_dir);
935eeb399b5SAdrian Hunter }
936eeb399b5SAdrian Hunter 
937415ccb58SAlexey Bayduraev static void record__thread_data_init_pipes(struct record_thread *thread_data)
938415ccb58SAlexey Bayduraev {
939415ccb58SAlexey Bayduraev 	thread_data->pipes.msg[0] = -1;
940415ccb58SAlexey Bayduraev 	thread_data->pipes.msg[1] = -1;
941415ccb58SAlexey Bayduraev 	thread_data->pipes.ack[0] = -1;
942415ccb58SAlexey Bayduraev 	thread_data->pipes.ack[1] = -1;
943415ccb58SAlexey Bayduraev }
944415ccb58SAlexey Bayduraev 
945415ccb58SAlexey Bayduraev static int record__thread_data_open_pipes(struct record_thread *thread_data)
946415ccb58SAlexey Bayduraev {
947415ccb58SAlexey Bayduraev 	if (pipe(thread_data->pipes.msg))
948415ccb58SAlexey Bayduraev 		return -EINVAL;
949415ccb58SAlexey Bayduraev 
950415ccb58SAlexey Bayduraev 	if (pipe(thread_data->pipes.ack)) {
951415ccb58SAlexey Bayduraev 		close(thread_data->pipes.msg[0]);
952415ccb58SAlexey Bayduraev 		thread_data->pipes.msg[0] = -1;
953415ccb58SAlexey Bayduraev 		close(thread_data->pipes.msg[1]);
954415ccb58SAlexey Bayduraev 		thread_data->pipes.msg[1] = -1;
955415ccb58SAlexey Bayduraev 		return -EINVAL;
956415ccb58SAlexey Bayduraev 	}
957415ccb58SAlexey Bayduraev 
958415ccb58SAlexey Bayduraev 	pr_debug2("thread_data[%p]: msg=[%d,%d], ack=[%d,%d]\n", thread_data,
959415ccb58SAlexey Bayduraev 		 thread_data->pipes.msg[0], thread_data->pipes.msg[1],
960415ccb58SAlexey Bayduraev 		 thread_data->pipes.ack[0], thread_data->pipes.ack[1]);
961415ccb58SAlexey Bayduraev 
962415ccb58SAlexey Bayduraev 	return 0;
963415ccb58SAlexey Bayduraev }
964415ccb58SAlexey Bayduraev 
965415ccb58SAlexey Bayduraev static void record__thread_data_close_pipes(struct record_thread *thread_data)
966415ccb58SAlexey Bayduraev {
967415ccb58SAlexey Bayduraev 	if (thread_data->pipes.msg[0] != -1) {
968415ccb58SAlexey Bayduraev 		close(thread_data->pipes.msg[0]);
969415ccb58SAlexey Bayduraev 		thread_data->pipes.msg[0] = -1;
970415ccb58SAlexey Bayduraev 	}
971415ccb58SAlexey Bayduraev 	if (thread_data->pipes.msg[1] != -1) {
972415ccb58SAlexey Bayduraev 		close(thread_data->pipes.msg[1]);
973415ccb58SAlexey Bayduraev 		thread_data->pipes.msg[1] = -1;
974415ccb58SAlexey Bayduraev 	}
975415ccb58SAlexey Bayduraev 	if (thread_data->pipes.ack[0] != -1) {
976415ccb58SAlexey Bayduraev 		close(thread_data->pipes.ack[0]);
977415ccb58SAlexey Bayduraev 		thread_data->pipes.ack[0] = -1;
978415ccb58SAlexey Bayduraev 	}
979415ccb58SAlexey Bayduraev 	if (thread_data->pipes.ack[1] != -1) {
980415ccb58SAlexey Bayduraev 		close(thread_data->pipes.ack[1]);
981415ccb58SAlexey Bayduraev 		thread_data->pipes.ack[1] = -1;
982415ccb58SAlexey Bayduraev 	}
983415ccb58SAlexey Bayduraev }
984415ccb58SAlexey Bayduraev 
9857be1feddSAdrian Hunter static bool evlist__per_thread(struct evlist *evlist)
9867be1feddSAdrian Hunter {
9877be1feddSAdrian Hunter 	return cpu_map__is_dummy(evlist->core.user_requested_cpus);
9887be1feddSAdrian Hunter }
9897be1feddSAdrian Hunter 
990415ccb58SAlexey Bayduraev static int record__thread_data_init_maps(struct record_thread *thread_data, struct evlist *evlist)
991415ccb58SAlexey Bayduraev {
992415ccb58SAlexey Bayduraev 	int m, tm, nr_mmaps = evlist->core.nr_mmaps;
993415ccb58SAlexey Bayduraev 	struct mmap *mmap = evlist->mmap;
994415ccb58SAlexey Bayduraev 	struct mmap *overwrite_mmap = evlist->overwrite_mmap;
9957be1feddSAdrian Hunter 	struct perf_cpu_map *cpus = evlist->core.all_cpus;
9967be1feddSAdrian Hunter 	bool per_thread = evlist__per_thread(evlist);
997415ccb58SAlexey Bayduraev 
9987be1feddSAdrian Hunter 	if (per_thread)
99923380e4dSAlexey Bayduraev 		thread_data->nr_mmaps = nr_mmaps;
100023380e4dSAlexey Bayduraev 	else
1001415ccb58SAlexey Bayduraev 		thread_data->nr_mmaps = bitmap_weight(thread_data->mask->maps.bits,
1002415ccb58SAlexey Bayduraev 						      thread_data->mask->maps.nbits);
1003415ccb58SAlexey Bayduraev 	if (mmap) {
1004415ccb58SAlexey Bayduraev 		thread_data->maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
1005415ccb58SAlexey Bayduraev 		if (!thread_data->maps)
1006415ccb58SAlexey Bayduraev 			return -ENOMEM;
1007415ccb58SAlexey Bayduraev 	}
1008415ccb58SAlexey Bayduraev 	if (overwrite_mmap) {
1009415ccb58SAlexey Bayduraev 		thread_data->overwrite_maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
1010415ccb58SAlexey Bayduraev 		if (!thread_data->overwrite_maps) {
1011415ccb58SAlexey Bayduraev 			zfree(&thread_data->maps);
1012415ccb58SAlexey Bayduraev 			return -ENOMEM;
1013415ccb58SAlexey Bayduraev 		}
1014415ccb58SAlexey Bayduraev 	}
1015415ccb58SAlexey Bayduraev 	pr_debug2("thread_data[%p]: nr_mmaps=%d, maps=%p, ow_maps=%p\n", thread_data,
1016415ccb58SAlexey Bayduraev 		 thread_data->nr_mmaps, thread_data->maps, thread_data->overwrite_maps);
1017415ccb58SAlexey Bayduraev 
1018415ccb58SAlexey Bayduraev 	for (m = 0, tm = 0; m < nr_mmaps && tm < thread_data->nr_mmaps; m++) {
10197be1feddSAdrian Hunter 		if (per_thread ||
10200255571aSIan Rogers 		    test_bit(perf_cpu_map__cpu(cpus, m).cpu, thread_data->mask->maps.bits)) {
1021415ccb58SAlexey Bayduraev 			if (thread_data->maps) {
1022415ccb58SAlexey Bayduraev 				thread_data->maps[tm] = &mmap[m];
1023415ccb58SAlexey Bayduraev 				pr_debug2("thread_data[%p]: cpu%d: maps[%d] -> mmap[%d]\n",
102423380e4dSAlexey Bayduraev 					  thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m);
1025415ccb58SAlexey Bayduraev 			}
1026415ccb58SAlexey Bayduraev 			if (thread_data->overwrite_maps) {
1027415ccb58SAlexey Bayduraev 				thread_data->overwrite_maps[tm] = &overwrite_mmap[m];
1028415ccb58SAlexey Bayduraev 				pr_debug2("thread_data[%p]: cpu%d: ow_maps[%d] -> ow_mmap[%d]\n",
102923380e4dSAlexey Bayduraev 					  thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m);
1030415ccb58SAlexey Bayduraev 			}
1031415ccb58SAlexey Bayduraev 			tm++;
1032415ccb58SAlexey Bayduraev 		}
1033415ccb58SAlexey Bayduraev 	}
1034415ccb58SAlexey Bayduraev 
1035415ccb58SAlexey Bayduraev 	return 0;
1036415ccb58SAlexey Bayduraev }
1037415ccb58SAlexey Bayduraev 
1038415ccb58SAlexey Bayduraev static int record__thread_data_init_pollfd(struct record_thread *thread_data, struct evlist *evlist)
1039415ccb58SAlexey Bayduraev {
1040415ccb58SAlexey Bayduraev 	int f, tm, pos;
1041415ccb58SAlexey Bayduraev 	struct mmap *map, *overwrite_map;
1042415ccb58SAlexey Bayduraev 
1043415ccb58SAlexey Bayduraev 	fdarray__init(&thread_data->pollfd, 64);
1044415ccb58SAlexey Bayduraev 
1045415ccb58SAlexey Bayduraev 	for (tm = 0; tm < thread_data->nr_mmaps; tm++) {
1046415ccb58SAlexey Bayduraev 		map = thread_data->maps ? thread_data->maps[tm] : NULL;
1047415ccb58SAlexey Bayduraev 		overwrite_map = thread_data->overwrite_maps ?
1048415ccb58SAlexey Bayduraev 				thread_data->overwrite_maps[tm] : NULL;
1049415ccb58SAlexey Bayduraev 
1050415ccb58SAlexey Bayduraev 		for (f = 0; f < evlist->core.pollfd.nr; f++) {
1051415ccb58SAlexey Bayduraev 			void *ptr = evlist->core.pollfd.priv[f].ptr;
1052415ccb58SAlexey Bayduraev 
1053415ccb58SAlexey Bayduraev 			if ((map && ptr == map) || (overwrite_map && ptr == overwrite_map)) {
1054415ccb58SAlexey Bayduraev 				pos = fdarray__dup_entry_from(&thread_data->pollfd, f,
1055415ccb58SAlexey Bayduraev 							      &evlist->core.pollfd);
1056415ccb58SAlexey Bayduraev 				if (pos < 0)
1057415ccb58SAlexey Bayduraev 					return pos;
1058415ccb58SAlexey Bayduraev 				pr_debug2("thread_data[%p]: pollfd[%d] <- event_fd=%d\n",
1059415ccb58SAlexey Bayduraev 					 thread_data, pos, evlist->core.pollfd.entries[f].fd);
1060415ccb58SAlexey Bayduraev 			}
1061415ccb58SAlexey Bayduraev 		}
1062415ccb58SAlexey Bayduraev 	}
1063415ccb58SAlexey Bayduraev 
1064415ccb58SAlexey Bayduraev 	return 0;
1065415ccb58SAlexey Bayduraev }
1066415ccb58SAlexey Bayduraev 
1067415ccb58SAlexey Bayduraev static void record__free_thread_data(struct record *rec)
1068415ccb58SAlexey Bayduraev {
1069415ccb58SAlexey Bayduraev 	int t;
1070415ccb58SAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
1071415ccb58SAlexey Bayduraev 
1072415ccb58SAlexey Bayduraev 	if (thread_data == NULL)
1073415ccb58SAlexey Bayduraev 		return;
1074415ccb58SAlexey Bayduraev 
1075415ccb58SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++) {
1076415ccb58SAlexey Bayduraev 		record__thread_data_close_pipes(&thread_data[t]);
1077415ccb58SAlexey Bayduraev 		zfree(&thread_data[t].maps);
1078415ccb58SAlexey Bayduraev 		zfree(&thread_data[t].overwrite_maps);
1079415ccb58SAlexey Bayduraev 		fdarray__exit(&thread_data[t].pollfd);
1080415ccb58SAlexey Bayduraev 	}
1081415ccb58SAlexey Bayduraev 
1082415ccb58SAlexey Bayduraev 	zfree(&rec->thread_data);
1083415ccb58SAlexey Bayduraev }
1084415ccb58SAlexey Bayduraev 
10856562c9acSAdrian Hunter static int record__map_thread_evlist_pollfd_indexes(struct record *rec,
10866562c9acSAdrian Hunter 						    int evlist_pollfd_index,
10876562c9acSAdrian Hunter 						    int thread_pollfd_index)
10886562c9acSAdrian Hunter {
10896562c9acSAdrian Hunter 	size_t x = rec->index_map_cnt;
10906562c9acSAdrian Hunter 
10916562c9acSAdrian Hunter 	if (realloc_array_as_needed(rec->index_map, rec->index_map_sz, x, NULL))
10926562c9acSAdrian Hunter 		return -ENOMEM;
10936562c9acSAdrian Hunter 	rec->index_map[x].evlist_pollfd_index = evlist_pollfd_index;
10946562c9acSAdrian Hunter 	rec->index_map[x].thread_pollfd_index = thread_pollfd_index;
10956562c9acSAdrian Hunter 	rec->index_map_cnt += 1;
10966562c9acSAdrian Hunter 	return 0;
10976562c9acSAdrian Hunter }
10986562c9acSAdrian Hunter 
10996562c9acSAdrian Hunter static int record__update_evlist_pollfd_from_thread(struct record *rec,
11006562c9acSAdrian Hunter 						    struct evlist *evlist,
11016562c9acSAdrian Hunter 						    struct record_thread *thread_data)
11026562c9acSAdrian Hunter {
11036562c9acSAdrian Hunter 	struct pollfd *e_entries = evlist->core.pollfd.entries;
11046562c9acSAdrian Hunter 	struct pollfd *t_entries = thread_data->pollfd.entries;
11056562c9acSAdrian Hunter 	int err = 0;
11066562c9acSAdrian Hunter 	size_t i;
11076562c9acSAdrian Hunter 
11086562c9acSAdrian Hunter 	for (i = 0; i < rec->index_map_cnt; i++) {
11096562c9acSAdrian Hunter 		int e_pos = rec->index_map[i].evlist_pollfd_index;
11106562c9acSAdrian Hunter 		int t_pos = rec->index_map[i].thread_pollfd_index;
11116562c9acSAdrian Hunter 
11126562c9acSAdrian Hunter 		if (e_entries[e_pos].fd != t_entries[t_pos].fd ||
11136562c9acSAdrian Hunter 		    e_entries[e_pos].events != t_entries[t_pos].events) {
11146562c9acSAdrian Hunter 			pr_err("Thread and evlist pollfd index mismatch\n");
11156562c9acSAdrian Hunter 			err = -EINVAL;
11166562c9acSAdrian Hunter 			continue;
11176562c9acSAdrian Hunter 		}
11186562c9acSAdrian Hunter 		e_entries[e_pos].revents = t_entries[t_pos].revents;
11196562c9acSAdrian Hunter 	}
11206562c9acSAdrian Hunter 	return err;
11216562c9acSAdrian Hunter }
11226562c9acSAdrian Hunter 
11236562c9acSAdrian Hunter static int record__dup_non_perf_events(struct record *rec,
11246562c9acSAdrian Hunter 				       struct evlist *evlist,
11256562c9acSAdrian Hunter 				       struct record_thread *thread_data)
11266562c9acSAdrian Hunter {
11276562c9acSAdrian Hunter 	struct fdarray *fda = &evlist->core.pollfd;
11286562c9acSAdrian Hunter 	int i, ret;
11296562c9acSAdrian Hunter 
11306562c9acSAdrian Hunter 	for (i = 0; i < fda->nr; i++) {
11316562c9acSAdrian Hunter 		if (!(fda->priv[i].flags & fdarray_flag__non_perf_event))
11326562c9acSAdrian Hunter 			continue;
11336562c9acSAdrian Hunter 		ret = fdarray__dup_entry_from(&thread_data->pollfd, i, fda);
11346562c9acSAdrian Hunter 		if (ret < 0) {
11356562c9acSAdrian Hunter 			pr_err("Failed to duplicate descriptor in main thread pollfd\n");
11366562c9acSAdrian Hunter 			return ret;
11376562c9acSAdrian Hunter 		}
11386562c9acSAdrian Hunter 		pr_debug2("thread_data[%p]: pollfd[%d] <- non_perf_event fd=%d\n",
11396562c9acSAdrian Hunter 			  thread_data, ret, fda->entries[i].fd);
11406562c9acSAdrian Hunter 		ret = record__map_thread_evlist_pollfd_indexes(rec, i, ret);
11416562c9acSAdrian Hunter 		if (ret < 0) {
11426562c9acSAdrian Hunter 			pr_err("Failed to map thread and evlist pollfd indexes\n");
11436562c9acSAdrian Hunter 			return ret;
11446562c9acSAdrian Hunter 		}
11456562c9acSAdrian Hunter 	}
11466562c9acSAdrian Hunter 	return 0;
11476562c9acSAdrian Hunter }
11486562c9acSAdrian Hunter 
1149415ccb58SAlexey Bayduraev static int record__alloc_thread_data(struct record *rec, struct evlist *evlist)
1150415ccb58SAlexey Bayduraev {
1151415ccb58SAlexey Bayduraev 	int t, ret;
1152415ccb58SAlexey Bayduraev 	struct record_thread *thread_data;
1153415ccb58SAlexey Bayduraev 
1154415ccb58SAlexey Bayduraev 	rec->thread_data = zalloc(rec->nr_threads * sizeof(*(rec->thread_data)));
1155415ccb58SAlexey Bayduraev 	if (!rec->thread_data) {
1156415ccb58SAlexey Bayduraev 		pr_err("Failed to allocate thread data\n");
1157415ccb58SAlexey Bayduraev 		return -ENOMEM;
1158415ccb58SAlexey Bayduraev 	}
1159415ccb58SAlexey Bayduraev 	thread_data = rec->thread_data;
1160415ccb58SAlexey Bayduraev 
1161415ccb58SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++)
1162415ccb58SAlexey Bayduraev 		record__thread_data_init_pipes(&thread_data[t]);
1163415ccb58SAlexey Bayduraev 
1164415ccb58SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++) {
1165415ccb58SAlexey Bayduraev 		thread_data[t].rec = rec;
1166415ccb58SAlexey Bayduraev 		thread_data[t].mask = &rec->thread_masks[t];
1167415ccb58SAlexey Bayduraev 		ret = record__thread_data_init_maps(&thread_data[t], evlist);
1168415ccb58SAlexey Bayduraev 		if (ret) {
1169415ccb58SAlexey Bayduraev 			pr_err("Failed to initialize thread[%d] maps\n", t);
1170415ccb58SAlexey Bayduraev 			goto out_free;
1171415ccb58SAlexey Bayduraev 		}
1172415ccb58SAlexey Bayduraev 		ret = record__thread_data_init_pollfd(&thread_data[t], evlist);
1173415ccb58SAlexey Bayduraev 		if (ret) {
1174415ccb58SAlexey Bayduraev 			pr_err("Failed to initialize thread[%d] pollfd\n", t);
1175415ccb58SAlexey Bayduraev 			goto out_free;
1176415ccb58SAlexey Bayduraev 		}
1177415ccb58SAlexey Bayduraev 		if (t) {
1178415ccb58SAlexey Bayduraev 			thread_data[t].tid = -1;
1179415ccb58SAlexey Bayduraev 			ret = record__thread_data_open_pipes(&thread_data[t]);
1180415ccb58SAlexey Bayduraev 			if (ret) {
1181415ccb58SAlexey Bayduraev 				pr_err("Failed to open thread[%d] communication pipes\n", t);
1182415ccb58SAlexey Bayduraev 				goto out_free;
1183415ccb58SAlexey Bayduraev 			}
1184415ccb58SAlexey Bayduraev 			ret = fdarray__add(&thread_data[t].pollfd, thread_data[t].pipes.msg[0],
1185415ccb58SAlexey Bayduraev 					   POLLIN | POLLERR | POLLHUP, fdarray_flag__nonfilterable);
1186415ccb58SAlexey Bayduraev 			if (ret < 0) {
1187415ccb58SAlexey Bayduraev 				pr_err("Failed to add descriptor to thread[%d] pollfd\n", t);
1188415ccb58SAlexey Bayduraev 				goto out_free;
1189415ccb58SAlexey Bayduraev 			}
1190415ccb58SAlexey Bayduraev 			thread_data[t].ctlfd_pos = ret;
1191415ccb58SAlexey Bayduraev 			pr_debug2("thread_data[%p]: pollfd[%d] <- ctl_fd=%d\n",
1192415ccb58SAlexey Bayduraev 				 thread_data, thread_data[t].ctlfd_pos,
1193415ccb58SAlexey Bayduraev 				 thread_data[t].pipes.msg[0]);
1194415ccb58SAlexey Bayduraev 		} else {
1195415ccb58SAlexey Bayduraev 			thread_data[t].tid = gettid();
11966562c9acSAdrian Hunter 
11976562c9acSAdrian Hunter 			ret = record__dup_non_perf_events(rec, evlist, &thread_data[t]);
11986562c9acSAdrian Hunter 			if (ret < 0)
11996562c9acSAdrian Hunter 				goto out_free;
12006562c9acSAdrian Hunter 
1201*feff0b61SAdrian Hunter 			thread_data[t].ctlfd_pos = -1; /* Not used */
1202415ccb58SAlexey Bayduraev 		}
1203415ccb58SAlexey Bayduraev 	}
1204415ccb58SAlexey Bayduraev 
1205415ccb58SAlexey Bayduraev 	return 0;
1206415ccb58SAlexey Bayduraev 
1207415ccb58SAlexey Bayduraev out_free:
1208415ccb58SAlexey Bayduraev 	record__free_thread_data(rec);
1209415ccb58SAlexey Bayduraev 
1210415ccb58SAlexey Bayduraev 	return ret;
1211415ccb58SAlexey Bayduraev }
1212415ccb58SAlexey Bayduraev 
1213cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec,
121463503dbaSJiri Olsa 			       struct evlist *evlist)
1215cda57a8cSWang Nan {
121656f735ffSAlexey Bayduraev 	int i, ret;
1217cda57a8cSWang Nan 	struct record_opts *opts = &rec->opts;
1218c0a6de06SAdrian Hunter 	bool auxtrace_overwrite = opts->auxtrace_snapshot_mode ||
1219c0a6de06SAdrian Hunter 				  opts->auxtrace_sample_mode;
1220cda57a8cSWang Nan 	char msg[512];
1221cda57a8cSWang Nan 
1222f13de660SAlexey Budankov 	if (opts->affinity != PERF_AFFINITY_SYS)
1223f13de660SAlexey Budankov 		cpu__setup_cpunode_map();
1224f13de660SAlexey Budankov 
12259521b5f2SJiri Olsa 	if (evlist__mmap_ex(evlist, opts->mmap_pages,
1226cda57a8cSWang Nan 				 opts->auxtrace_mmap_pages,
1227c0a6de06SAdrian Hunter 				 auxtrace_overwrite,
1228470530bbSAlexey Budankov 				 opts->nr_cblocks, opts->affinity,
122951255a8aSAlexey Budankov 				 opts->mmap_flush, opts->comp_level) < 0) {
1230cda57a8cSWang Nan 		if (errno == EPERM) {
1231cda57a8cSWang Nan 			pr_err("Permission error mapping pages.\n"
1232cda57a8cSWang Nan 			       "Consider increasing "
1233cda57a8cSWang Nan 			       "/proc/sys/kernel/perf_event_mlock_kb,\n"
1234cda57a8cSWang Nan 			       "or try again with a smaller value of -m/--mmap_pages.\n"
1235cda57a8cSWang Nan 			       "(current value: %u,%u)\n",
1236cda57a8cSWang Nan 			       opts->mmap_pages, opts->auxtrace_mmap_pages);
1237cda57a8cSWang Nan 			return -errno;
1238cda57a8cSWang Nan 		} else {
1239cda57a8cSWang Nan 			pr_err("failed to mmap with %d (%s)\n", errno,
1240c8b5f2c9SArnaldo Carvalho de Melo 				str_error_r(errno, msg, sizeof(msg)));
1241cda57a8cSWang Nan 			if (errno)
1242cda57a8cSWang Nan 				return -errno;
1243cda57a8cSWang Nan 			else
1244cda57a8cSWang Nan 				return -EINVAL;
1245cda57a8cSWang Nan 		}
1246cda57a8cSWang Nan 	}
1247415ccb58SAlexey Bayduraev 
1248415ccb58SAlexey Bayduraev 	if (evlist__initialize_ctlfd(evlist, opts->ctl_fd, opts->ctl_fd_ack))
1249415ccb58SAlexey Bayduraev 		return -1;
1250415ccb58SAlexey Bayduraev 
1251415ccb58SAlexey Bayduraev 	ret = record__alloc_thread_data(rec, evlist);
1252415ccb58SAlexey Bayduraev 	if (ret)
1253415ccb58SAlexey Bayduraev 		return ret;
1254415ccb58SAlexey Bayduraev 
125556f735ffSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
125656f735ffSAlexey Bayduraev 		ret = perf_data__create_dir(&rec->data, evlist->core.nr_mmaps);
125765e7c963SAlexey Bayduraev 		if (ret) {
125865e7c963SAlexey Bayduraev 			pr_err("Failed to create data directory: %s\n", strerror(-ret));
125956f735ffSAlexey Bayduraev 			return ret;
126065e7c963SAlexey Bayduraev 		}
126156f735ffSAlexey Bayduraev 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
126256f735ffSAlexey Bayduraev 			if (evlist->mmap)
126356f735ffSAlexey Bayduraev 				evlist->mmap[i].file = &rec->data.dir.files[i];
126456f735ffSAlexey Bayduraev 			if (evlist->overwrite_mmap)
126556f735ffSAlexey Bayduraev 				evlist->overwrite_mmap[i].file = &rec->data.dir.files[i];
126656f735ffSAlexey Bayduraev 		}
126756f735ffSAlexey Bayduraev 	}
126856f735ffSAlexey Bayduraev 
1269cda57a8cSWang Nan 	return 0;
1270cda57a8cSWang Nan }
1271cda57a8cSWang Nan 
1272cda57a8cSWang Nan static int record__mmap(struct record *rec)
1273cda57a8cSWang Nan {
1274cda57a8cSWang Nan 	return record__mmap_evlist(rec, rec->evlist);
1275cda57a8cSWang Nan }
1276cda57a8cSWang Nan 
12778c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec)
1278dd7927f4SArnaldo Carvalho de Melo {
1279d6195a6aSArnaldo Carvalho de Melo 	char msg[BUFSIZ];
128032dcd021SJiri Olsa 	struct evsel *pos;
128163503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
1282d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session = rec->session;
1283b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
12848d3eca20SDavid Ahern 	int rc = 0;
1285dd7927f4SArnaldo Carvalho de Melo 
1286d3dbf43cSArnaldo Carvalho de Melo 	/*
1287b91e5492SKan Liang 	 * For initial_delay, system wide or a hybrid system, we need to add a
1288b91e5492SKan Liang 	 * dummy event so that we can track PERF_RECORD_MMAP to cover the delay
1289b91e5492SKan Liang 	 * of waiting or event synthesis.
1290d3dbf43cSArnaldo Carvalho de Melo 	 */
1291b91e5492SKan Liang 	if (opts->initial_delay || target__has_cpu(&opts->target) ||
1292b91e5492SKan Liang 	    perf_pmu__has_hybrid()) {
1293e80db255SArnaldo Carvalho de Melo 		pos = evlist__get_tracking_event(evlist);
1294442ad225SAdrian Hunter 		if (!evsel__is_dummy_event(pos)) {
1295442ad225SAdrian Hunter 			/* Set up dummy event. */
1296e251abeeSArnaldo Carvalho de Melo 			if (evlist__add_dummy(evlist))
1297d3dbf43cSArnaldo Carvalho de Melo 				return -ENOMEM;
1298515dbe48SJiri Olsa 			pos = evlist__last(evlist);
1299e80db255SArnaldo Carvalho de Melo 			evlist__set_tracking_event(evlist, pos);
1300442ad225SAdrian Hunter 		}
1301442ad225SAdrian Hunter 
13020a892c1cSIan Rogers 		/*
13030a892c1cSIan Rogers 		 * Enable the dummy event when the process is forked for
13040a892c1cSIan Rogers 		 * initial_delay, immediately for system wide.
13050a892c1cSIan Rogers 		 */
1306bb07d62eSNamhyung Kim 		if (opts->initial_delay && !pos->immediate &&
1307bb07d62eSNamhyung Kim 		    !target__has_cpu(&opts->target))
13081fc632ceSJiri Olsa 			pos->core.attr.enable_on_exec = 1;
13090a892c1cSIan Rogers 		else
13100a892c1cSIan Rogers 			pos->immediate = 1;
1311d3dbf43cSArnaldo Carvalho de Melo 	}
1312d3dbf43cSArnaldo Carvalho de Melo 
131378e1bc25SArnaldo Carvalho de Melo 	evlist__config(evlist, opts, &callchain_param);
1314cac21425SJiri Olsa 
1315e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
13163da297a6SIngo Molnar try_again:
1317af663bd0SJiri Olsa 		if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
1318ae430892SArnaldo Carvalho de Melo 			if (evsel__fallback(pos, errno, msg, sizeof(msg))) {
1319bb963e16SNamhyung Kim 				if (verbose > 0)
1320c0a54341SArnaldo Carvalho de Melo 					ui__warning("%s\n", msg);
13213da297a6SIngo Molnar 				goto try_again;
13223da297a6SIngo Molnar 			}
1323cf99ad14SAndi Kleen 			if ((errno == EINVAL || errno == EBADF) &&
1324fba7c866SJiri Olsa 			    pos->core.leader != &pos->core &&
1325cf99ad14SAndi Kleen 			    pos->weak_group) {
132664b4778bSArnaldo Carvalho de Melo 			        pos = evlist__reset_weak_group(evlist, pos, true);
1327cf99ad14SAndi Kleen 				goto try_again;
1328cf99ad14SAndi Kleen 			}
132956e52e85SArnaldo Carvalho de Melo 			rc = -errno;
13302bb72dbbSArnaldo Carvalho de Melo 			evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg));
133156e52e85SArnaldo Carvalho de Melo 			ui__error("%s\n", msg);
13328d3eca20SDavid Ahern 			goto out;
13337c6a1c65SPeter Zijlstra 		}
1334bfd8f72cSAndi Kleen 
1335bfd8f72cSAndi Kleen 		pos->supported = true;
13367c6a1c65SPeter Zijlstra 	}
13377c6a1c65SPeter Zijlstra 
133878e1bc25SArnaldo Carvalho de Melo 	if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) {
1339c8b567c8SArnaldo Carvalho de Melo 		pr_warning(
1340c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
1341c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
1342c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
1343c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n"
1344c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n"
1345c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
1346c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n");
1347c8b567c8SArnaldo Carvalho de Melo 	}
1348c8b567c8SArnaldo Carvalho de Melo 
134924bf91a7SArnaldo Carvalho de Melo 	if (evlist__apply_filters(evlist, &pos)) {
135062d94b00SArnaldo Carvalho de Melo 		pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
13518ab2e96dSArnaldo Carvalho de Melo 			pos->filter, evsel__name(pos), errno,
1352c8b5f2c9SArnaldo Carvalho de Melo 			str_error_r(errno, msg, sizeof(msg)));
13538d3eca20SDavid Ahern 		rc = -1;
13548d3eca20SDavid Ahern 		goto out;
13550a102479SFrederic Weisbecker 	}
13560a102479SFrederic Weisbecker 
1357cda57a8cSWang Nan 	rc = record__mmap(rec);
1358cda57a8cSWang Nan 	if (rc)
13598d3eca20SDavid Ahern 		goto out;
13600a27d7f9SArnaldo Carvalho de Melo 
1361a91e5431SArnaldo Carvalho de Melo 	session->evlist = evlist;
13627b56cce2SArnaldo Carvalho de Melo 	perf_session__set_id_hdr_size(session);
13638d3eca20SDavid Ahern out:
13648d3eca20SDavid Ahern 	return rc;
1365a91e5431SArnaldo Carvalho de Melo }
1366a91e5431SArnaldo Carvalho de Melo 
136766286ed3SAdrian Hunter static void set_timestamp_boundary(struct record *rec, u64 sample_time)
136866286ed3SAdrian Hunter {
136966286ed3SAdrian Hunter 	if (rec->evlist->first_sample_time == 0)
137066286ed3SAdrian Hunter 		rec->evlist->first_sample_time = sample_time;
137166286ed3SAdrian Hunter 
137266286ed3SAdrian Hunter 	if (sample_time)
137366286ed3SAdrian Hunter 		rec->evlist->last_sample_time = sample_time;
137466286ed3SAdrian Hunter }
137566286ed3SAdrian Hunter 
1376e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool,
1377e3d59112SNamhyung Kim 				union perf_event *event,
1378e3d59112SNamhyung Kim 				struct perf_sample *sample,
137932dcd021SJiri Olsa 				struct evsel *evsel,
1380e3d59112SNamhyung Kim 				struct machine *machine)
1381e3d59112SNamhyung Kim {
1382e3d59112SNamhyung Kim 	struct record *rec = container_of(tool, struct record, tool);
1383e3d59112SNamhyung Kim 
138466286ed3SAdrian Hunter 	set_timestamp_boundary(rec, sample->time);
138568588bafSJin Yao 
138668588bafSJin Yao 	if (rec->buildid_all)
138768588bafSJin Yao 		return 0;
138868588bafSJin Yao 
138968588bafSJin Yao 	rec->samples++;
1390e3d59112SNamhyung Kim 	return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
1391e3d59112SNamhyung Kim }
1392e3d59112SNamhyung Kim 
13938c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec)
13946122e4e4SArnaldo Carvalho de Melo {
1395f5fc1412SJiri Olsa 	struct perf_session *session = rec->session;
13966122e4e4SArnaldo Carvalho de Melo 
139745112e89SJiri Olsa 	if (perf_data__size(&rec->data) == 0)
13989f591fd7SArnaldo Carvalho de Melo 		return 0;
13999f591fd7SArnaldo Carvalho de Melo 
140000dc8657SNamhyung Kim 	/*
140100dc8657SNamhyung Kim 	 * During this process, it'll load kernel map and replace the
140200dc8657SNamhyung Kim 	 * dso->long_name to a real pathname it found.  In this case
140300dc8657SNamhyung Kim 	 * we prefer the vmlinux path like
140400dc8657SNamhyung Kim 	 *   /lib/modules/3.16.4/build/vmlinux
140500dc8657SNamhyung Kim 	 *
140600dc8657SNamhyung Kim 	 * rather than build-id path (in debug directory).
140700dc8657SNamhyung Kim 	 *   $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
140800dc8657SNamhyung Kim 	 */
140900dc8657SNamhyung Kim 	symbol_conf.ignore_vmlinux_buildid = true;
141000dc8657SNamhyung Kim 
14116156681bSNamhyung Kim 	/*
14126156681bSNamhyung Kim 	 * If --buildid-all is given, it marks all DSO regardless of hits,
141368588bafSJin Yao 	 * so no need to process samples. But if timestamp_boundary is enabled,
141468588bafSJin Yao 	 * it still needs to walk on all samples to get the timestamps of
141568588bafSJin Yao 	 * first/last samples.
14166156681bSNamhyung Kim 	 */
141768588bafSJin Yao 	if (rec->buildid_all && !rec->timestamp_boundary)
14186156681bSNamhyung Kim 		rec->tool.sample = NULL;
14196156681bSNamhyung Kim 
1420b7b61cbeSArnaldo Carvalho de Melo 	return perf_session__process_events(session);
14216122e4e4SArnaldo Carvalho de Melo }
14226122e4e4SArnaldo Carvalho de Melo 
14238115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
1424a1645ce1SZhang, Yanmin {
1425a1645ce1SZhang, Yanmin 	int err;
142645694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = data;
1427a1645ce1SZhang, Yanmin 	/*
1428a1645ce1SZhang, Yanmin 	 *As for guest kernel when processing subcommand record&report,
1429a1645ce1SZhang, Yanmin 	 *we arrange module mmap prior to guest kernel mmap and trigger
1430a1645ce1SZhang, Yanmin 	 *a preload dso because default guest module symbols are loaded
1431a1645ce1SZhang, Yanmin 	 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
1432a1645ce1SZhang, Yanmin 	 *method is used to avoid symbol missing when the first addr is
1433a1645ce1SZhang, Yanmin 	 *in module instead of in guest kernel.
1434a1645ce1SZhang, Yanmin 	 */
143545694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_modules(tool, process_synthesized_event,
1436743eb868SArnaldo Carvalho de Melo 					     machine);
1437a1645ce1SZhang, Yanmin 	if (err < 0)
1438a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
143923346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
1440a1645ce1SZhang, Yanmin 
1441a1645ce1SZhang, Yanmin 	/*
1442a1645ce1SZhang, Yanmin 	 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
1443a1645ce1SZhang, Yanmin 	 * have no _text sometimes.
1444a1645ce1SZhang, Yanmin 	 */
144545694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
14460ae617beSAdrian Hunter 						 machine);
1447a1645ce1SZhang, Yanmin 	if (err < 0)
1448a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
144923346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
1450a1645ce1SZhang, Yanmin }
1451a1645ce1SZhang, Yanmin 
145298402807SFrederic Weisbecker static struct perf_event_header finished_round_event = {
145398402807SFrederic Weisbecker 	.size = sizeof(struct perf_event_header),
145498402807SFrederic Weisbecker 	.type = PERF_RECORD_FINISHED_ROUND,
145598402807SFrederic Weisbecker };
145698402807SFrederic Weisbecker 
14573812d298SAdrian Hunter static struct perf_event_header finished_init_event = {
14583812d298SAdrian Hunter 	.size = sizeof(struct perf_event_header),
14593812d298SAdrian Hunter 	.type = PERF_RECORD_FINISHED_INIT,
14603812d298SAdrian Hunter };
14613812d298SAdrian Hunter 
1462a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map)
1463f13de660SAlexey Budankov {
1464f13de660SAlexey Budankov 	if (rec->opts.affinity != PERF_AFFINITY_SYS &&
1465396b626bSAlexey Bayduraev 	    !bitmap_equal(thread->mask->affinity.bits, map->affinity_mask.bits,
1466396b626bSAlexey Bayduraev 			  thread->mask->affinity.nbits)) {
1467396b626bSAlexey Bayduraev 		bitmap_zero(thread->mask->affinity.bits, thread->mask->affinity.nbits);
1468396b626bSAlexey Bayduraev 		bitmap_or(thread->mask->affinity.bits, thread->mask->affinity.bits,
1469396b626bSAlexey Bayduraev 			  map->affinity_mask.bits, thread->mask->affinity.nbits);
1470396b626bSAlexey Bayduraev 		sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity),
1471396b626bSAlexey Bayduraev 					(cpu_set_t *)thread->mask->affinity.bits);
1472396b626bSAlexey Bayduraev 		if (verbose == 2) {
1473396b626bSAlexey Bayduraev 			pr_debug("threads[%d]: running on cpu%d: ", thread->tid, sched_getcpu());
1474396b626bSAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&thread->mask->affinity, "affinity");
1475396b626bSAlexey Bayduraev 		}
1476f13de660SAlexey Budankov 	}
1477f13de660SAlexey Budankov }
1478f13de660SAlexey Budankov 
14795d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment)
14805d7f4116SAlexey Budankov {
148172932371SJiri Olsa 	struct perf_record_compressed *event = record;
14825d7f4116SAlexey Budankov 	size_t size = sizeof(*event);
14835d7f4116SAlexey Budankov 
14845d7f4116SAlexey Budankov 	if (increment) {
14855d7f4116SAlexey Budankov 		event->header.size += increment;
14865d7f4116SAlexey Budankov 		return increment;
14875d7f4116SAlexey Budankov 	}
14885d7f4116SAlexey Budankov 
14895d7f4116SAlexey Budankov 	event->header.type = PERF_RECORD_COMPRESSED;
14905d7f4116SAlexey Budankov 	event->header.size = size;
14915d7f4116SAlexey Budankov 
14925d7f4116SAlexey Budankov 	return size;
14935d7f4116SAlexey Budankov }
14945d7f4116SAlexey Budankov 
149575f5f1fcSAlexey Bayduraev static size_t zstd_compress(struct perf_session *session, struct mmap *map,
149675f5f1fcSAlexey Bayduraev 			    void *dst, size_t dst_size, void *src, size_t src_size)
14975d7f4116SAlexey Budankov {
14985d7f4116SAlexey Budankov 	size_t compressed;
149972932371SJiri Olsa 	size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
150075f5f1fcSAlexey Bayduraev 	struct zstd_data *zstd_data = &session->zstd_data;
15015d7f4116SAlexey Budankov 
150275f5f1fcSAlexey Bayduraev 	if (map && map->file)
150375f5f1fcSAlexey Bayduraev 		zstd_data = &map->zstd_data;
150475f5f1fcSAlexey Bayduraev 
150575f5f1fcSAlexey Bayduraev 	compressed = zstd_compress_stream_to_records(zstd_data, dst, dst_size, src, src_size,
15065d7f4116SAlexey Budankov 						     max_record_size, process_comp_header);
15075d7f4116SAlexey Budankov 
1508610fbc01SAlexey Bayduraev 	if (map && map->file) {
1509610fbc01SAlexey Bayduraev 		thread->bytes_transferred += src_size;
1510610fbc01SAlexey Bayduraev 		thread->bytes_compressed  += compressed;
1511610fbc01SAlexey Bayduraev 	} else {
15125d7f4116SAlexey Budankov 		session->bytes_transferred += src_size;
15135d7f4116SAlexey Budankov 		session->bytes_compressed  += compressed;
1514610fbc01SAlexey Bayduraev 	}
15155d7f4116SAlexey Budankov 
15165d7f4116SAlexey Budankov 	return compressed;
15175d7f4116SAlexey Budankov }
15185d7f4116SAlexey Budankov 
151963503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist,
1520470530bbSAlexey Budankov 				    bool overwrite, bool synch)
152198402807SFrederic Weisbecker {
1522dcabb507SJiri Olsa 	u64 bytes_written = rec->bytes_written;
15230e2e63ddSPeter Zijlstra 	int i;
15248d3eca20SDavid Ahern 	int rc = 0;
1525396b626bSAlexey Bayduraev 	int nr_mmaps;
1526396b626bSAlexey Bayduraev 	struct mmap **maps;
1527d3d1af6fSAlexey Budankov 	int trace_fd = rec->data.file.fd;
1528ef781128SAlexey Budankov 	off_t off = 0;
152998402807SFrederic Weisbecker 
1530cb21686bSWang Nan 	if (!evlist)
1531cb21686bSWang Nan 		return 0;
1532ef149c25SAdrian Hunter 
1533396b626bSAlexey Bayduraev 	nr_mmaps = thread->nr_mmaps;
1534396b626bSAlexey Bayduraev 	maps = overwrite ? thread->overwrite_maps : thread->maps;
1535396b626bSAlexey Bayduraev 
1536a4ea0ec4SWang Nan 	if (!maps)
1537a4ea0ec4SWang Nan 		return 0;
1538cb21686bSWang Nan 
15390b72d69aSWang Nan 	if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
154054cc54deSWang Nan 		return 0;
154154cc54deSWang Nan 
1542d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1543d3d1af6fSAlexey Budankov 		off = record__aio_get_pos(trace_fd);
1544d3d1af6fSAlexey Budankov 
1545396b626bSAlexey Bayduraev 	for (i = 0; i < nr_mmaps; i++) {
1546470530bbSAlexey Budankov 		u64 flush = 0;
1547396b626bSAlexey Bayduraev 		struct mmap *map = maps[i];
1548a4ea0ec4SWang Nan 
1549547740f7SJiri Olsa 		if (map->core.base) {
1550f13de660SAlexey Budankov 			record__adjust_affinity(rec, map);
1551470530bbSAlexey Budankov 			if (synch) {
155265aa2e6bSJiri Olsa 				flush = map->core.flush;
155365aa2e6bSJiri Olsa 				map->core.flush = 1;
1554470530bbSAlexey Budankov 			}
1555d3d1af6fSAlexey Budankov 			if (!record__aio_enabled(rec)) {
1556ef781128SAlexey Budankov 				if (perf_mmap__push(map, rec, record__pushfn) < 0) {
1557470530bbSAlexey Budankov 					if (synch)
155865aa2e6bSJiri Olsa 						map->core.flush = flush;
15598d3eca20SDavid Ahern 					rc = -1;
15608d3eca20SDavid Ahern 					goto out;
15618d3eca20SDavid Ahern 				}
1562d3d1af6fSAlexey Budankov 			} else {
1563ef781128SAlexey Budankov 				if (record__aio_push(rec, map, &off) < 0) {
1564d3d1af6fSAlexey Budankov 					record__aio_set_pos(trace_fd, off);
1565470530bbSAlexey Budankov 					if (synch)
156665aa2e6bSJiri Olsa 						map->core.flush = flush;
1567d3d1af6fSAlexey Budankov 					rc = -1;
1568d3d1af6fSAlexey Budankov 					goto out;
1569d3d1af6fSAlexey Budankov 				}
1570d3d1af6fSAlexey Budankov 			}
1571470530bbSAlexey Budankov 			if (synch)
157265aa2e6bSJiri Olsa 				map->core.flush = flush;
15738d3eca20SDavid Ahern 		}
1574ef149c25SAdrian Hunter 
1575e035f4caSJiri Olsa 		if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
1576c0a6de06SAdrian Hunter 		    !rec->opts.auxtrace_sample_mode &&
1577e035f4caSJiri Olsa 		    record__auxtrace_mmap_read(rec, map) != 0) {
1578ef149c25SAdrian Hunter 			rc = -1;
1579ef149c25SAdrian Hunter 			goto out;
1580ef149c25SAdrian Hunter 		}
158198402807SFrederic Weisbecker 	}
158298402807SFrederic Weisbecker 
1583d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1584d3d1af6fSAlexey Budankov 		record__aio_set_pos(trace_fd, off);
1585d3d1af6fSAlexey Budankov 
1586dcabb507SJiri Olsa 	/*
1587dcabb507SJiri Olsa 	 * Mark the round finished in case we wrote
1588dcabb507SJiri Olsa 	 * at least one event.
158956f735ffSAlexey Bayduraev 	 *
159056f735ffSAlexey Bayduraev 	 * No need for round events in directory mode,
159156f735ffSAlexey Bayduraev 	 * because per-cpu maps and files have data
159256f735ffSAlexey Bayduraev 	 * sorted by kernel.
1593dcabb507SJiri Olsa 	 */
159456f735ffSAlexey Bayduraev 	if (!record__threads_enabled(rec) && bytes_written != rec->bytes_written)
1595ded2b8feSJiri Olsa 		rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event));
15968d3eca20SDavid Ahern 
15970b72d69aSWang Nan 	if (overwrite)
1598ade9d208SArnaldo Carvalho de Melo 		evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
15998d3eca20SDavid Ahern out:
16008d3eca20SDavid Ahern 	return rc;
160198402807SFrederic Weisbecker }
160298402807SFrederic Weisbecker 
1603470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch)
1604cb21686bSWang Nan {
1605cb21686bSWang Nan 	int err;
1606cb21686bSWang Nan 
1607470530bbSAlexey Budankov 	err = record__mmap_read_evlist(rec, rec->evlist, false, synch);
1608cb21686bSWang Nan 	if (err)
1609cb21686bSWang Nan 		return err;
1610cb21686bSWang Nan 
1611470530bbSAlexey Budankov 	return record__mmap_read_evlist(rec, rec->evlist, true, synch);
1612cb21686bSWang Nan }
1613cb21686bSWang Nan 
1614396b626bSAlexey Bayduraev static void record__thread_munmap_filtered(struct fdarray *fda, int fd,
1615396b626bSAlexey Bayduraev 					   void *arg __maybe_unused)
1616396b626bSAlexey Bayduraev {
1617396b626bSAlexey Bayduraev 	struct perf_mmap *map = fda->priv[fd].ptr;
1618396b626bSAlexey Bayduraev 
1619396b626bSAlexey Bayduraev 	if (map)
1620396b626bSAlexey Bayduraev 		perf_mmap__put(map);
1621396b626bSAlexey Bayduraev }
1622396b626bSAlexey Bayduraev 
16233217e9feSAlexey Bayduraev static void *record__thread(void *arg)
16243217e9feSAlexey Bayduraev {
16253217e9feSAlexey Bayduraev 	enum thread_msg msg = THREAD_MSG__READY;
16263217e9feSAlexey Bayduraev 	bool terminate = false;
16273217e9feSAlexey Bayduraev 	struct fdarray *pollfd;
16283217e9feSAlexey Bayduraev 	int err, ctlfd_pos;
16293217e9feSAlexey Bayduraev 
16303217e9feSAlexey Bayduraev 	thread = arg;
16313217e9feSAlexey Bayduraev 	thread->tid = gettid();
16323217e9feSAlexey Bayduraev 
16333217e9feSAlexey Bayduraev 	err = write(thread->pipes.ack[1], &msg, sizeof(msg));
16343217e9feSAlexey Bayduraev 	if (err == -1)
16353217e9feSAlexey Bayduraev 		pr_warning("threads[%d]: failed to notify on start: %s\n",
16363217e9feSAlexey Bayduraev 			   thread->tid, strerror(errno));
16373217e9feSAlexey Bayduraev 
16383217e9feSAlexey Bayduraev 	pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu());
16393217e9feSAlexey Bayduraev 
16403217e9feSAlexey Bayduraev 	pollfd = &thread->pollfd;
16413217e9feSAlexey Bayduraev 	ctlfd_pos = thread->ctlfd_pos;
16423217e9feSAlexey Bayduraev 
16433217e9feSAlexey Bayduraev 	for (;;) {
16443217e9feSAlexey Bayduraev 		unsigned long long hits = thread->samples;
16453217e9feSAlexey Bayduraev 
16463217e9feSAlexey Bayduraev 		if (record__mmap_read_all(thread->rec, false) < 0 || terminate)
16473217e9feSAlexey Bayduraev 			break;
16483217e9feSAlexey Bayduraev 
16493217e9feSAlexey Bayduraev 		if (hits == thread->samples) {
16503217e9feSAlexey Bayduraev 
16513217e9feSAlexey Bayduraev 			err = fdarray__poll(pollfd, -1);
16523217e9feSAlexey Bayduraev 			/*
16533217e9feSAlexey Bayduraev 			 * Propagate error, only if there's any. Ignore positive
16543217e9feSAlexey Bayduraev 			 * number of returned events and interrupt error.
16553217e9feSAlexey Bayduraev 			 */
16563217e9feSAlexey Bayduraev 			if (err > 0 || (err < 0 && errno == EINTR))
16573217e9feSAlexey Bayduraev 				err = 0;
16583217e9feSAlexey Bayduraev 			thread->waking++;
16593217e9feSAlexey Bayduraev 
16603217e9feSAlexey Bayduraev 			if (fdarray__filter(pollfd, POLLERR | POLLHUP,
16613217e9feSAlexey Bayduraev 					    record__thread_munmap_filtered, NULL) == 0)
16623217e9feSAlexey Bayduraev 				break;
16633217e9feSAlexey Bayduraev 		}
16643217e9feSAlexey Bayduraev 
16653217e9feSAlexey Bayduraev 		if (pollfd->entries[ctlfd_pos].revents & POLLHUP) {
16663217e9feSAlexey Bayduraev 			terminate = true;
16673217e9feSAlexey Bayduraev 			close(thread->pipes.msg[0]);
16683217e9feSAlexey Bayduraev 			thread->pipes.msg[0] = -1;
16693217e9feSAlexey Bayduraev 			pollfd->entries[ctlfd_pos].fd = -1;
16703217e9feSAlexey Bayduraev 			pollfd->entries[ctlfd_pos].events = 0;
16713217e9feSAlexey Bayduraev 		}
16723217e9feSAlexey Bayduraev 
16733217e9feSAlexey Bayduraev 		pollfd->entries[ctlfd_pos].revents = 0;
16743217e9feSAlexey Bayduraev 	}
16753217e9feSAlexey Bayduraev 	record__mmap_read_all(thread->rec, true);
16763217e9feSAlexey Bayduraev 
16773217e9feSAlexey Bayduraev 	err = write(thread->pipes.ack[1], &msg, sizeof(msg));
16783217e9feSAlexey Bayduraev 	if (err == -1)
16793217e9feSAlexey Bayduraev 		pr_warning("threads[%d]: failed to notify on termination: %s\n",
16803217e9feSAlexey Bayduraev 			   thread->tid, strerror(errno));
16813217e9feSAlexey Bayduraev 
16823217e9feSAlexey Bayduraev 	return NULL;
16833217e9feSAlexey Bayduraev }
16843217e9feSAlexey Bayduraev 
16858c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec)
168657706abcSDavid Ahern {
168757706abcSDavid Ahern 	struct perf_session *session = rec->session;
168857706abcSDavid Ahern 	int feat;
168957706abcSDavid Ahern 
169057706abcSDavid Ahern 	for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
169157706abcSDavid Ahern 		perf_header__set_feat(&session->header, feat);
169257706abcSDavid Ahern 
169357706abcSDavid Ahern 	if (rec->no_buildid)
169457706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
169557706abcSDavid Ahern 
1696ce9036a6SJiri Olsa 	if (!have_tracepoints(&rec->evlist->core.entries))
169757706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
169857706abcSDavid Ahern 
169957706abcSDavid Ahern 	if (!rec->opts.branch_stack)
170057706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1701ef149c25SAdrian Hunter 
1702ef149c25SAdrian Hunter 	if (!rec->opts.full_auxtrace)
1703ef149c25SAdrian Hunter 		perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1704ffa517adSJiri Olsa 
1705cf790516SAlexey Budankov 	if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns))
1706cf790516SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_CLOCKID);
1707cf790516SAlexey Budankov 
1708d1e325cfSJiri Olsa 	if (!rec->opts.use_clockid)
1709d1e325cfSJiri Olsa 		perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA);
1710d1e325cfSJiri Olsa 
171156f735ffSAlexey Bayduraev 	if (!record__threads_enabled(rec))
1712258031c0SJiri Olsa 		perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
171356f735ffSAlexey Bayduraev 
171442e1fd80SAlexey Budankov 	if (!record__comp_enabled(rec))
171542e1fd80SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_COMPRESSED);
1716258031c0SJiri Olsa 
1717ffa517adSJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_STAT);
171857706abcSDavid Ahern }
171957706abcSDavid Ahern 
1720e1ab48baSWang Nan static void
1721e1ab48baSWang Nan record__finish_output(struct record *rec)
1722e1ab48baSWang Nan {
172356f735ffSAlexey Bayduraev 	int i;
17248ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
17258ceb41d7SJiri Olsa 	int fd = perf_data__fd(data);
1726e1ab48baSWang Nan 
17278ceb41d7SJiri Olsa 	if (data->is_pipe)
1728e1ab48baSWang Nan 		return;
1729e1ab48baSWang Nan 
1730e1ab48baSWang Nan 	rec->session->header.data_size += rec->bytes_written;
173145112e89SJiri Olsa 	data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR);
173256f735ffSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
173356f735ffSAlexey Bayduraev 		for (i = 0; i < data->dir.nr; i++)
173456f735ffSAlexey Bayduraev 			data->dir.files[i].size = lseek(data->dir.files[i].fd, 0, SEEK_CUR);
173556f735ffSAlexey Bayduraev 	}
1736e1ab48baSWang Nan 
1737e1ab48baSWang Nan 	if (!rec->no_buildid) {
1738e1ab48baSWang Nan 		process_buildids(rec);
1739e1ab48baSWang Nan 
1740e1ab48baSWang Nan 		if (rec->buildid_all)
1741e1ab48baSWang Nan 			dsos__hit_all(rec->session);
1742e1ab48baSWang Nan 	}
1743e1ab48baSWang Nan 	perf_session__write_header(rec->session, rec->evlist, fd, true);
1744e1ab48baSWang Nan 
1745e1ab48baSWang Nan 	return;
1746e1ab48baSWang Nan }
1747e1ab48baSWang Nan 
17484ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail)
1749be7b0c9eSWang Nan {
17509d6aae72SArnaldo Carvalho de Melo 	int err;
17519749b90eSJiri Olsa 	struct perf_thread_map *thread_map;
175241b740b6SNamhyung Kim 	bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP;
1753be7b0c9eSWang Nan 
17544ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
17554ea648aeSWang Nan 		return 0;
17564ea648aeSWang Nan 
17579d6aae72SArnaldo Carvalho de Melo 	thread_map = thread_map__new_by_tid(rec->evlist->workload.pid);
17589d6aae72SArnaldo Carvalho de Melo 	if (thread_map == NULL)
17599d6aae72SArnaldo Carvalho de Melo 		return -1;
17609d6aae72SArnaldo Carvalho de Melo 
17619d6aae72SArnaldo Carvalho de Melo 	err = perf_event__synthesize_thread_map(&rec->tool, thread_map,
1762be7b0c9eSWang Nan 						 process_synthesized_event,
1763be7b0c9eSWang Nan 						 &rec->session->machines.host,
176441b740b6SNamhyung Kim 						 needs_mmap,
17653fcb10e4SMark Drayton 						 rec->opts.sample_address);
17667836e52eSJiri Olsa 	perf_thread_map__put(thread_map);
17679d6aae72SArnaldo Carvalho de Melo 	return err;
1768be7b0c9eSWang Nan }
1769be7b0c9eSWang Nan 
17703812d298SAdrian Hunter static int write_finished_init(struct record *rec, bool tail)
17713812d298SAdrian Hunter {
17723812d298SAdrian Hunter 	if (rec->opts.tail_synthesize != tail)
17733812d298SAdrian Hunter 		return 0;
17743812d298SAdrian Hunter 
17753812d298SAdrian Hunter 	return record__write(rec, NULL, &finished_init_event, sizeof(finished_init_event));
17763812d298SAdrian Hunter }
17773812d298SAdrian Hunter 
17784ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail);
17793c1cb7e3SWang Nan 
1780ecfd7a9cSWang Nan static int
1781ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit)
1782ecfd7a9cSWang Nan {
17838ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1784ecfd7a9cSWang Nan 	int fd, err;
178503724b2eSAndi Kleen 	char *new_filename;
1786ecfd7a9cSWang Nan 
1787ecfd7a9cSWang Nan 	/* Same Size:      "2015122520103046"*/
1788ecfd7a9cSWang Nan 	char timestamp[] = "InvalidTimestamp";
1789ecfd7a9cSWang Nan 
1790d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
1791d3d1af6fSAlexey Budankov 
17923812d298SAdrian Hunter 	write_finished_init(rec, true);
17933812d298SAdrian Hunter 
17944ea648aeSWang Nan 	record__synthesize(rec, true);
17954ea648aeSWang Nan 	if (target__none(&rec->opts.target))
17964ea648aeSWang Nan 		record__synthesize_workload(rec, true);
17974ea648aeSWang Nan 
1798ecfd7a9cSWang Nan 	rec->samples = 0;
1799ecfd7a9cSWang Nan 	record__finish_output(rec);
1800ecfd7a9cSWang Nan 	err = fetch_current_timestamp(timestamp, sizeof(timestamp));
1801ecfd7a9cSWang Nan 	if (err) {
1802ecfd7a9cSWang Nan 		pr_err("Failed to get current timestamp\n");
1803ecfd7a9cSWang Nan 		return -EINVAL;
1804ecfd7a9cSWang Nan 	}
1805ecfd7a9cSWang Nan 
18068ceb41d7SJiri Olsa 	fd = perf_data__switch(data, timestamp,
1807ecfd7a9cSWang Nan 				    rec->session->header.data_offset,
180803724b2eSAndi Kleen 				    at_exit, &new_filename);
1809ecfd7a9cSWang Nan 	if (fd >= 0 && !at_exit) {
1810ecfd7a9cSWang Nan 		rec->bytes_written = 0;
1811ecfd7a9cSWang Nan 		rec->session->header.data_size = 0;
1812ecfd7a9cSWang Nan 	}
1813ecfd7a9cSWang Nan 
1814ecfd7a9cSWang Nan 	if (!quiet)
1815ecfd7a9cSWang Nan 		fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
18162d4f2799SJiri Olsa 			data->path, timestamp);
18173c1cb7e3SWang Nan 
181803724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
181903724b2eSAndi Kleen 		int n = rec->switch_output.cur_file + 1;
182003724b2eSAndi Kleen 
182103724b2eSAndi Kleen 		if (n >= rec->switch_output.num_files)
182203724b2eSAndi Kleen 			n = 0;
182303724b2eSAndi Kleen 		rec->switch_output.cur_file = n;
182403724b2eSAndi Kleen 		if (rec->switch_output.filenames[n]) {
182503724b2eSAndi Kleen 			remove(rec->switch_output.filenames[n]);
1826d8f9da24SArnaldo Carvalho de Melo 			zfree(&rec->switch_output.filenames[n]);
182703724b2eSAndi Kleen 		}
182803724b2eSAndi Kleen 		rec->switch_output.filenames[n] = new_filename;
182903724b2eSAndi Kleen 	} else {
183003724b2eSAndi Kleen 		free(new_filename);
183103724b2eSAndi Kleen 	}
183203724b2eSAndi Kleen 
18333c1cb7e3SWang Nan 	/* Output tracking events */
1834be7b0c9eSWang Nan 	if (!at_exit) {
18354ea648aeSWang Nan 		record__synthesize(rec, false);
18363c1cb7e3SWang Nan 
1837be7b0c9eSWang Nan 		/*
1838be7b0c9eSWang Nan 		 * In 'perf record --switch-output' without -a,
1839be7b0c9eSWang Nan 		 * record__synthesize() in record__switch_output() won't
1840be7b0c9eSWang Nan 		 * generate tracking events because there's no thread_map
1841be7b0c9eSWang Nan 		 * in evlist. Which causes newly created perf.data doesn't
1842be7b0c9eSWang Nan 		 * contain map and comm information.
1843be7b0c9eSWang Nan 		 * Create a fake thread_map and directly call
1844be7b0c9eSWang Nan 		 * perf_event__synthesize_thread_map() for those events.
1845be7b0c9eSWang Nan 		 */
1846be7b0c9eSWang Nan 		if (target__none(&rec->opts.target))
18474ea648aeSWang Nan 			record__synthesize_workload(rec, false);
18483812d298SAdrian Hunter 		write_finished_init(rec, false);
1849be7b0c9eSWang Nan 	}
1850ecfd7a9cSWang Nan 	return fd;
1851ecfd7a9cSWang Nan }
1852ecfd7a9cSWang Nan 
1853f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno;
1854f33cbe72SArnaldo Carvalho de Melo 
1855f33cbe72SArnaldo Carvalho de Melo /*
18567b392ef0SArnaldo Carvalho de Melo  * evlist__prepare_workload will send a SIGUSR1
1857f33cbe72SArnaldo Carvalho de Melo  * if the fork fails, since we asked by setting its
1858f33cbe72SArnaldo Carvalho de Melo  * want_signal to true.
1859f33cbe72SArnaldo Carvalho de Melo  */
186045604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused,
186145604710SNamhyung Kim 					siginfo_t *info,
1862f33cbe72SArnaldo Carvalho de Melo 					void *ucontext __maybe_unused)
1863f33cbe72SArnaldo Carvalho de Melo {
1864f33cbe72SArnaldo Carvalho de Melo 	workload_exec_errno = info->si_value.sival_int;
1865f33cbe72SArnaldo Carvalho de Melo 	done = 1;
1866f33cbe72SArnaldo Carvalho de Melo 	child_finished = 1;
1867f33cbe72SArnaldo Carvalho de Melo }
1868f33cbe72SArnaldo Carvalho de Melo 
18692dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig);
1870bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig);
18712dd6d8a1SAdrian Hunter 
1872db0ea13cSArnaldo Carvalho de Melo static const struct perf_event_mmap_page *evlist__pick_pc(struct evlist *evlist)
1873ee667f94SWang Nan {
1874b2cb615dSWang Nan 	if (evlist) {
1875547740f7SJiri Olsa 		if (evlist->mmap && evlist->mmap[0].core.base)
1876547740f7SJiri Olsa 			return evlist->mmap[0].core.base;
1877547740f7SJiri Olsa 		if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base)
1878547740f7SJiri Olsa 			return evlist->overwrite_mmap[0].core.base;
1879b2cb615dSWang Nan 	}
1880ee667f94SWang Nan 	return NULL;
1881ee667f94SWang Nan }
1882ee667f94SWang Nan 
1883c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
1884c45628b0SWang Nan {
1885db0ea13cSArnaldo Carvalho de Melo 	const struct perf_event_mmap_page *pc = evlist__pick_pc(rec->evlist);
1886ee667f94SWang Nan 	if (pc)
1887ee667f94SWang Nan 		return pc;
1888c45628b0SWang Nan 	return NULL;
1889c45628b0SWang Nan }
1890c45628b0SWang Nan 
18914ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail)
1892c45c86ebSWang Nan {
1893c45c86ebSWang Nan 	struct perf_session *session = rec->session;
1894c45c86ebSWang Nan 	struct machine *machine = &session->machines.host;
18958ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1896c45c86ebSWang Nan 	struct record_opts *opts = &rec->opts;
1897c45c86ebSWang Nan 	struct perf_tool *tool = &rec->tool;
1898c45c86ebSWang Nan 	int err = 0;
1899d99c22eaSStephane Eranian 	event_op f = process_synthesized_event;
1900c45c86ebSWang Nan 
19014ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
19024ea648aeSWang Nan 		return 0;
19034ea648aeSWang Nan 
19048ceb41d7SJiri Olsa 	if (data->is_pipe) {
1905c3a057dcSNamhyung Kim 		err = perf_event__synthesize_for_pipe(tool, session, data,
1906c45c86ebSWang Nan 						      process_synthesized_event);
1907c3a057dcSNamhyung Kim 		if (err < 0)
1908c45c86ebSWang Nan 			goto out;
1909c45c86ebSWang Nan 
1910c45c86ebSWang Nan 		rec->bytes_written += err;
1911c45c86ebSWang Nan 	}
1912c45c86ebSWang Nan 
1913c45628b0SWang Nan 	err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
191446bc29b9SAdrian Hunter 					  process_synthesized_event, machine);
191546bc29b9SAdrian Hunter 	if (err)
191646bc29b9SAdrian Hunter 		goto out;
191746bc29b9SAdrian Hunter 
1918c0a6de06SAdrian Hunter 	/* Synthesize id_index before auxtrace_info */
1919c0a6de06SAdrian Hunter 	err = perf_event__synthesize_id_index(tool,
1920c0a6de06SAdrian Hunter 					      process_synthesized_event,
1921c0a6de06SAdrian Hunter 					      session->evlist, machine);
1922c0a6de06SAdrian Hunter 	if (err)
1923c0a6de06SAdrian Hunter 		goto out;
1924c0a6de06SAdrian Hunter 
1925c45c86ebSWang Nan 	if (rec->opts.full_auxtrace) {
1926c45c86ebSWang Nan 		err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
1927c45c86ebSWang Nan 					session, process_synthesized_event);
1928c45c86ebSWang Nan 		if (err)
1929c45c86ebSWang Nan 			goto out;
1930c45c86ebSWang Nan 	}
1931c45c86ebSWang Nan 
193278e1bc25SArnaldo Carvalho de Melo 	if (!evlist__exclude_kernel(rec->evlist)) {
1933c45c86ebSWang Nan 		err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
1934c45c86ebSWang Nan 							 machine);
1935c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
1936c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1937c45c86ebSWang Nan 				   "Check /proc/kallsyms permission or run as root.\n");
1938c45c86ebSWang Nan 
1939c45c86ebSWang Nan 		err = perf_event__synthesize_modules(tool, process_synthesized_event,
1940c45c86ebSWang Nan 						     machine);
1941c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
1942c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1943c45c86ebSWang Nan 				   "Check /proc/modules permission or run as root.\n");
19446c443954SArnaldo Carvalho de Melo 	}
1945c45c86ebSWang Nan 
1946c45c86ebSWang Nan 	if (perf_guest) {
1947c45c86ebSWang Nan 		machines__process_guests(&session->machines,
1948c45c86ebSWang Nan 					 perf_event__synthesize_guest_os, tool);
1949c45c86ebSWang Nan 	}
1950c45c86ebSWang Nan 
1951bfd8f72cSAndi Kleen 	err = perf_event__synthesize_extra_attr(&rec->tool,
1952bfd8f72cSAndi Kleen 						rec->evlist,
1953bfd8f72cSAndi Kleen 						process_synthesized_event,
1954bfd8f72cSAndi Kleen 						data->is_pipe);
1955bfd8f72cSAndi Kleen 	if (err)
1956bfd8f72cSAndi Kleen 		goto out;
1957bfd8f72cSAndi Kleen 
195803617c22SJiri Olsa 	err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads,
1959373565d2SAndi Kleen 						 process_synthesized_event,
1960373565d2SAndi Kleen 						NULL);
1961373565d2SAndi Kleen 	if (err < 0) {
1962373565d2SAndi Kleen 		pr_err("Couldn't synthesize thread map.\n");
1963373565d2SAndi Kleen 		return err;
1964373565d2SAndi Kleen 	}
1965373565d2SAndi Kleen 
19667be1feddSAdrian Hunter 	err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.all_cpus,
1967373565d2SAndi Kleen 					     process_synthesized_event, NULL);
1968373565d2SAndi Kleen 	if (err < 0) {
1969373565d2SAndi Kleen 		pr_err("Couldn't synthesize cpu map.\n");
1970373565d2SAndi Kleen 		return err;
1971373565d2SAndi Kleen 	}
1972373565d2SAndi Kleen 
1973e5416950SSong Liu 	err = perf_event__synthesize_bpf_events(session, process_synthesized_event,
19747b612e29SSong Liu 						machine, opts);
1975faf59ec8SAdrian Hunter 	if (err < 0) {
19767b612e29SSong Liu 		pr_warning("Couldn't synthesize bpf events.\n");
1977faf59ec8SAdrian Hunter 		err = 0;
1978faf59ec8SAdrian Hunter 	}
19797b612e29SSong Liu 
198041b740b6SNamhyung Kim 	if (rec->opts.synth & PERF_SYNTH_CGROUP) {
1981ab64069fSNamhyung Kim 		err = perf_event__synthesize_cgroups(tool, process_synthesized_event,
1982ab64069fSNamhyung Kim 						     machine);
1983faf59ec8SAdrian Hunter 		if (err < 0) {
1984ab64069fSNamhyung Kim 			pr_warning("Couldn't synthesize cgroup events.\n");
1985faf59ec8SAdrian Hunter 			err = 0;
1986faf59ec8SAdrian Hunter 		}
198741b740b6SNamhyung Kim 	}
1988ab64069fSNamhyung Kim 
1989d99c22eaSStephane Eranian 	if (rec->opts.nr_threads_synthesize > 1) {
1990d99c22eaSStephane Eranian 		perf_set_multithreaded();
1991d99c22eaSStephane Eranian 		f = process_locked_synthesized_event;
1992d99c22eaSStephane Eranian 	}
1993d99c22eaSStephane Eranian 
199441b740b6SNamhyung Kim 	if (rec->opts.synth & PERF_SYNTH_TASK) {
199541b740b6SNamhyung Kim 		bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP;
199641b740b6SNamhyung Kim 
199784111b9cSNamhyung Kim 		err = __machine__synthesize_threads(machine, tool, &opts->target,
199884111b9cSNamhyung Kim 						    rec->evlist->core.threads,
199941b740b6SNamhyung Kim 						    f, needs_mmap, opts->sample_address,
2000d99c22eaSStephane Eranian 						    rec->opts.nr_threads_synthesize);
200141b740b6SNamhyung Kim 	}
2002d99c22eaSStephane Eranian 
2003d99c22eaSStephane Eranian 	if (rec->opts.nr_threads_synthesize > 1)
2004d99c22eaSStephane Eranian 		perf_set_singlethreaded();
2005d99c22eaSStephane Eranian 
2006c45c86ebSWang Nan out:
2007c45c86ebSWang Nan 	return err;
2008c45c86ebSWang Nan }
2009c45c86ebSWang Nan 
2010899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data)
2011899e5ffbSArnaldo Carvalho de Melo {
2012899e5ffbSArnaldo Carvalho de Melo 	struct record *rec = data;
2013899e5ffbSArnaldo Carvalho de Melo 	pthread_kill(rec->thread_id, SIGUSR2);
2014899e5ffbSArnaldo Carvalho de Melo 	return 0;
2015899e5ffbSArnaldo Carvalho de Melo }
2016899e5ffbSArnaldo Carvalho de Melo 
201723cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec)
201823cbb41cSArnaldo Carvalho de Melo {
201923cbb41cSArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
202023cbb41cSArnaldo Carvalho de Melo 
202123cbb41cSArnaldo Carvalho de Melo 	if (rec->sb_evlist != NULL) {
202223cbb41cSArnaldo Carvalho de Melo 		/*
202323cbb41cSArnaldo Carvalho de Melo 		 * We get here if --switch-output-event populated the
202423cbb41cSArnaldo Carvalho de Melo 		 * sb_evlist, so associate a callback that will send a SIGUSR2
202523cbb41cSArnaldo Carvalho de Melo 		 * to the main thread.
202623cbb41cSArnaldo Carvalho de Melo 		 */
202723cbb41cSArnaldo Carvalho de Melo 		evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec);
202823cbb41cSArnaldo Carvalho de Melo 		rec->thread_id = pthread_self();
202923cbb41cSArnaldo Carvalho de Melo 	}
20301101c872SJin Yao #ifdef HAVE_LIBBPF_SUPPORT
203123cbb41cSArnaldo Carvalho de Melo 	if (!opts->no_bpf_event) {
203223cbb41cSArnaldo Carvalho de Melo 		if (rec->sb_evlist == NULL) {
203323cbb41cSArnaldo Carvalho de Melo 			rec->sb_evlist = evlist__new();
203423cbb41cSArnaldo Carvalho de Melo 
203523cbb41cSArnaldo Carvalho de Melo 			if (rec->sb_evlist == NULL) {
203623cbb41cSArnaldo Carvalho de Melo 				pr_err("Couldn't create side band evlist.\n.");
203723cbb41cSArnaldo Carvalho de Melo 				return -1;
203823cbb41cSArnaldo Carvalho de Melo 			}
203923cbb41cSArnaldo Carvalho de Melo 		}
204023cbb41cSArnaldo Carvalho de Melo 
204123cbb41cSArnaldo Carvalho de Melo 		if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) {
204223cbb41cSArnaldo Carvalho de Melo 			pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
204323cbb41cSArnaldo Carvalho de Melo 			return -1;
204423cbb41cSArnaldo Carvalho de Melo 		}
204523cbb41cSArnaldo Carvalho de Melo 	}
20461101c872SJin Yao #endif
204708c83997SArnaldo Carvalho de Melo 	if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) {
204823cbb41cSArnaldo Carvalho de Melo 		pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
204923cbb41cSArnaldo Carvalho de Melo 		opts->no_bpf_event = true;
205023cbb41cSArnaldo Carvalho de Melo 	}
205123cbb41cSArnaldo Carvalho de Melo 
205223cbb41cSArnaldo Carvalho de Melo 	return 0;
205323cbb41cSArnaldo Carvalho de Melo }
205423cbb41cSArnaldo Carvalho de Melo 
2055d1e325cfSJiri Olsa static int record__init_clock(struct record *rec)
2056d1e325cfSJiri Olsa {
2057d1e325cfSJiri Olsa 	struct perf_session *session = rec->session;
2058d1e325cfSJiri Olsa 	struct timespec ref_clockid;
2059d1e325cfSJiri Olsa 	struct timeval ref_tod;
2060d1e325cfSJiri Olsa 	u64 ref;
2061d1e325cfSJiri Olsa 
2062d1e325cfSJiri Olsa 	if (!rec->opts.use_clockid)
2063d1e325cfSJiri Olsa 		return 0;
2064d1e325cfSJiri Olsa 
20659d88a1a1SJiri Olsa 	if (rec->opts.use_clockid && rec->opts.clockid_res_ns)
20669d88a1a1SJiri Olsa 		session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns;
20679d88a1a1SJiri Olsa 
2068d1e325cfSJiri Olsa 	session->header.env.clock.clockid = rec->opts.clockid;
2069d1e325cfSJiri Olsa 
2070d1e325cfSJiri Olsa 	if (gettimeofday(&ref_tod, NULL) != 0) {
2071d1e325cfSJiri Olsa 		pr_err("gettimeofday failed, cannot set reference time.\n");
2072d1e325cfSJiri Olsa 		return -1;
2073d1e325cfSJiri Olsa 	}
2074d1e325cfSJiri Olsa 
2075d1e325cfSJiri Olsa 	if (clock_gettime(rec->opts.clockid, &ref_clockid)) {
2076d1e325cfSJiri Olsa 		pr_err("clock_gettime failed, cannot set reference time.\n");
2077d1e325cfSJiri Olsa 		return -1;
2078d1e325cfSJiri Olsa 	}
2079d1e325cfSJiri Olsa 
2080d1e325cfSJiri Olsa 	ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC +
2081d1e325cfSJiri Olsa 	      (u64) ref_tod.tv_usec * NSEC_PER_USEC;
2082d1e325cfSJiri Olsa 
2083d1e325cfSJiri Olsa 	session->header.env.clock.tod_ns = ref;
2084d1e325cfSJiri Olsa 
2085d1e325cfSJiri Olsa 	ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC +
2086d1e325cfSJiri Olsa 	      (u64) ref_clockid.tv_nsec;
2087d1e325cfSJiri Olsa 
2088d1e325cfSJiri Olsa 	session->header.env.clock.clockid_ns = ref;
2089d1e325cfSJiri Olsa 	return 0;
2090d1e325cfSJiri Olsa }
2091d1e325cfSJiri Olsa 
2092d20aff15SAdrian Hunter static void hit_auxtrace_snapshot_trigger(struct record *rec)
2093d20aff15SAdrian Hunter {
2094d20aff15SAdrian Hunter 	if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
2095d20aff15SAdrian Hunter 		trigger_hit(&auxtrace_snapshot_trigger);
2096d20aff15SAdrian Hunter 		auxtrace_record__snapshot_started = 1;
2097d20aff15SAdrian Hunter 		if (auxtrace_record__snapshot_start(rec->itr))
2098d20aff15SAdrian Hunter 			trigger_error(&auxtrace_snapshot_trigger);
2099d20aff15SAdrian Hunter 	}
2100d20aff15SAdrian Hunter }
2101d20aff15SAdrian Hunter 
210291c0f5ecSJin Yao static void record__uniquify_name(struct record *rec)
210391c0f5ecSJin Yao {
210491c0f5ecSJin Yao 	struct evsel *pos;
210591c0f5ecSJin Yao 	struct evlist *evlist = rec->evlist;
210691c0f5ecSJin Yao 	char *new_name;
210791c0f5ecSJin Yao 	int ret;
210891c0f5ecSJin Yao 
210991c0f5ecSJin Yao 	if (!perf_pmu__has_hybrid())
211091c0f5ecSJin Yao 		return;
211191c0f5ecSJin Yao 
211291c0f5ecSJin Yao 	evlist__for_each_entry(evlist, pos) {
211391c0f5ecSJin Yao 		if (!evsel__is_hybrid(pos))
211491c0f5ecSJin Yao 			continue;
211591c0f5ecSJin Yao 
211691c0f5ecSJin Yao 		if (strchr(pos->name, '/'))
211791c0f5ecSJin Yao 			continue;
211891c0f5ecSJin Yao 
211991c0f5ecSJin Yao 		ret = asprintf(&new_name, "%s/%s/",
212091c0f5ecSJin Yao 			       pos->pmu_name, pos->name);
212191c0f5ecSJin Yao 		if (ret) {
212291c0f5ecSJin Yao 			free(pos->name);
212391c0f5ecSJin Yao 			pos->name = new_name;
212491c0f5ecSJin Yao 		}
212591c0f5ecSJin Yao 	}
212691c0f5ecSJin Yao }
212791c0f5ecSJin Yao 
21281e5de7d9SAlexey Bayduraev static int record__terminate_thread(struct record_thread *thread_data)
21291e5de7d9SAlexey Bayduraev {
21301e5de7d9SAlexey Bayduraev 	int err;
21311e5de7d9SAlexey Bayduraev 	enum thread_msg ack = THREAD_MSG__UNDEFINED;
21321e5de7d9SAlexey Bayduraev 	pid_t tid = thread_data->tid;
21331e5de7d9SAlexey Bayduraev 
21341e5de7d9SAlexey Bayduraev 	close(thread_data->pipes.msg[1]);
21351e5de7d9SAlexey Bayduraev 	thread_data->pipes.msg[1] = -1;
21361e5de7d9SAlexey Bayduraev 	err = read(thread_data->pipes.ack[0], &ack, sizeof(ack));
21371e5de7d9SAlexey Bayduraev 	if (err > 0)
21381e5de7d9SAlexey Bayduraev 		pr_debug2("threads[%d]: sent %s\n", tid, thread_msg_tags[ack]);
21391e5de7d9SAlexey Bayduraev 	else
21401e5de7d9SAlexey Bayduraev 		pr_warning("threads[%d]: failed to receive termination notification from %d\n",
21411e5de7d9SAlexey Bayduraev 			   thread->tid, tid);
21421e5de7d9SAlexey Bayduraev 
21431e5de7d9SAlexey Bayduraev 	return 0;
21441e5de7d9SAlexey Bayduraev }
21451e5de7d9SAlexey Bayduraev 
2146396b626bSAlexey Bayduraev static int record__start_threads(struct record *rec)
2147396b626bSAlexey Bayduraev {
21483217e9feSAlexey Bayduraev 	int t, tt, err, ret = 0, nr_threads = rec->nr_threads;
2149396b626bSAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
21503217e9feSAlexey Bayduraev 	sigset_t full, mask;
21513217e9feSAlexey Bayduraev 	pthread_t handle;
21523217e9feSAlexey Bayduraev 	pthread_attr_t attrs;
2153396b626bSAlexey Bayduraev 
2154396b626bSAlexey Bayduraev 	thread = &thread_data[0];
2155396b626bSAlexey Bayduraev 
21563217e9feSAlexey Bayduraev 	if (!record__threads_enabled(rec))
21573217e9feSAlexey Bayduraev 		return 0;
21583217e9feSAlexey Bayduraev 
21593217e9feSAlexey Bayduraev 	sigfillset(&full);
21603217e9feSAlexey Bayduraev 	if (sigprocmask(SIG_SETMASK, &full, &mask)) {
21613217e9feSAlexey Bayduraev 		pr_err("Failed to block signals on threads start: %s\n", strerror(errno));
21623217e9feSAlexey Bayduraev 		return -1;
21633217e9feSAlexey Bayduraev 	}
21643217e9feSAlexey Bayduraev 
21653217e9feSAlexey Bayduraev 	pthread_attr_init(&attrs);
21663217e9feSAlexey Bayduraev 	pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
21673217e9feSAlexey Bayduraev 
21683217e9feSAlexey Bayduraev 	for (t = 1; t < nr_threads; t++) {
21693217e9feSAlexey Bayduraev 		enum thread_msg msg = THREAD_MSG__UNDEFINED;
21703217e9feSAlexey Bayduraev 
21713217e9feSAlexey Bayduraev #ifdef HAVE_PTHREAD_ATTR_SETAFFINITY_NP
21723217e9feSAlexey Bayduraev 		pthread_attr_setaffinity_np(&attrs,
21733217e9feSAlexey Bayduraev 					    MMAP_CPU_MASK_BYTES(&(thread_data[t].mask->affinity)),
21743217e9feSAlexey Bayduraev 					    (cpu_set_t *)(thread_data[t].mask->affinity.bits));
21753217e9feSAlexey Bayduraev #endif
21763217e9feSAlexey Bayduraev 		if (pthread_create(&handle, &attrs, record__thread, &thread_data[t])) {
21773217e9feSAlexey Bayduraev 			for (tt = 1; tt < t; tt++)
21783217e9feSAlexey Bayduraev 				record__terminate_thread(&thread_data[t]);
21793217e9feSAlexey Bayduraev 			pr_err("Failed to start threads: %s\n", strerror(errno));
21803217e9feSAlexey Bayduraev 			ret = -1;
21813217e9feSAlexey Bayduraev 			goto out_err;
21823217e9feSAlexey Bayduraev 		}
21833217e9feSAlexey Bayduraev 
21843217e9feSAlexey Bayduraev 		err = read(thread_data[t].pipes.ack[0], &msg, sizeof(msg));
21853217e9feSAlexey Bayduraev 		if (err > 0)
21863217e9feSAlexey Bayduraev 			pr_debug2("threads[%d]: sent %s\n", rec->thread_data[t].tid,
21873217e9feSAlexey Bayduraev 				  thread_msg_tags[msg]);
21883217e9feSAlexey Bayduraev 		else
21893217e9feSAlexey Bayduraev 			pr_warning("threads[%d]: failed to receive start notification from %d\n",
21903217e9feSAlexey Bayduraev 				   thread->tid, rec->thread_data[t].tid);
21913217e9feSAlexey Bayduraev 	}
21923217e9feSAlexey Bayduraev 
21933217e9feSAlexey Bayduraev 	sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity),
21943217e9feSAlexey Bayduraev 			(cpu_set_t *)thread->mask->affinity.bits);
21953217e9feSAlexey Bayduraev 
2196396b626bSAlexey Bayduraev 	pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu());
2197396b626bSAlexey Bayduraev 
21983217e9feSAlexey Bayduraev out_err:
21993217e9feSAlexey Bayduraev 	pthread_attr_destroy(&attrs);
22003217e9feSAlexey Bayduraev 
22013217e9feSAlexey Bayduraev 	if (sigprocmask(SIG_SETMASK, &mask, NULL)) {
22023217e9feSAlexey Bayduraev 		pr_err("Failed to unblock signals on threads start: %s\n", strerror(errno));
22033217e9feSAlexey Bayduraev 		ret = -1;
22043217e9feSAlexey Bayduraev 	}
22053217e9feSAlexey Bayduraev 
22063217e9feSAlexey Bayduraev 	return ret;
2207396b626bSAlexey Bayduraev }
2208396b626bSAlexey Bayduraev 
2209396b626bSAlexey Bayduraev static int record__stop_threads(struct record *rec)
2210396b626bSAlexey Bayduraev {
2211396b626bSAlexey Bayduraev 	int t;
2212396b626bSAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
2213396b626bSAlexey Bayduraev 
22141e5de7d9SAlexey Bayduraev 	for (t = 1; t < rec->nr_threads; t++)
22151e5de7d9SAlexey Bayduraev 		record__terminate_thread(&thread_data[t]);
22161e5de7d9SAlexey Bayduraev 
2217610fbc01SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++) {
2218396b626bSAlexey Bayduraev 		rec->samples += thread_data[t].samples;
2219610fbc01SAlexey Bayduraev 		if (!record__threads_enabled(rec))
2220610fbc01SAlexey Bayduraev 			continue;
2221610fbc01SAlexey Bayduraev 		rec->session->bytes_transferred += thread_data[t].bytes_transferred;
2222610fbc01SAlexey Bayduraev 		rec->session->bytes_compressed += thread_data[t].bytes_compressed;
2223610fbc01SAlexey Bayduraev 		pr_debug("threads[%d]: samples=%lld, wakes=%ld, ", thread_data[t].tid,
2224610fbc01SAlexey Bayduraev 			 thread_data[t].samples, thread_data[t].waking);
2225610fbc01SAlexey Bayduraev 		if (thread_data[t].bytes_transferred && thread_data[t].bytes_compressed)
2226610fbc01SAlexey Bayduraev 			pr_debug("transferred=%" PRIu64 ", compressed=%" PRIu64 "\n",
2227610fbc01SAlexey Bayduraev 				 thread_data[t].bytes_transferred, thread_data[t].bytes_compressed);
2228610fbc01SAlexey Bayduraev 		else
2229610fbc01SAlexey Bayduraev 			pr_debug("written=%" PRIu64 "\n", thread_data[t].bytes_written);
2230610fbc01SAlexey Bayduraev 	}
2231396b626bSAlexey Bayduraev 
2232396b626bSAlexey Bayduraev 	return 0;
2233396b626bSAlexey Bayduraev }
2234396b626bSAlexey Bayduraev 
2235396b626bSAlexey Bayduraev static unsigned long record__waking(struct record *rec)
2236396b626bSAlexey Bayduraev {
2237396b626bSAlexey Bayduraev 	int t;
2238396b626bSAlexey Bayduraev 	unsigned long waking = 0;
2239396b626bSAlexey Bayduraev 	struct record_thread *thread_data = rec->thread_data;
2240396b626bSAlexey Bayduraev 
2241396b626bSAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++)
2242396b626bSAlexey Bayduraev 		waking += thread_data[t].waking;
2243396b626bSAlexey Bayduraev 
2244396b626bSAlexey Bayduraev 	return waking;
2245396b626bSAlexey Bayduraev }
2246396b626bSAlexey Bayduraev 
22478c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv)
224886470930SIngo Molnar {
224957706abcSDavid Ahern 	int err;
225045604710SNamhyung Kim 	int status = 0;
225146be604bSZhang, Yanmin 	const bool forks = argc > 0;
225245694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = &rec->tool;
2253b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
22548ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
2255d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session;
22566dcf45efSArnaldo Carvalho de Melo 	bool disabled = false, draining = false;
225742aa276fSNamhyung Kim 	int fd;
2258d3c8c08eSAlexey Budankov 	float ratio = 0;
2259acce0223SAlexey Budankov 	enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
226086470930SIngo Molnar 
226145604710SNamhyung Kim 	atexit(record__sig_exit);
2262f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
2263f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
2264804f7ac7SDavid Ahern 	signal(SIGTERM, sig_handler);
2265a074865eSWang Nan 	signal(SIGSEGV, sigsegv_handler);
2266c0bdc1c4SWang Nan 
2267f3b3614aSHari Bathini 	if (rec->opts.record_namespaces)
2268f3b3614aSHari Bathini 		tool->namespace_events = true;
2269f3b3614aSHari Bathini 
22708fb4b679SNamhyung Kim 	if (rec->opts.record_cgroup) {
22718fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE
22728fb4b679SNamhyung Kim 		tool->cgroup_events = true;
22738fb4b679SNamhyung Kim #else
22748fb4b679SNamhyung Kim 		pr_err("cgroup tracking is not supported\n");
22758fb4b679SNamhyung Kim 		return -1;
22768fb4b679SNamhyung Kim #endif
22778fb4b679SNamhyung Kim 	}
22788fb4b679SNamhyung Kim 
2279dc0c6127SJiri Olsa 	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
22802dd6d8a1SAdrian Hunter 		signal(SIGUSR2, snapshot_sig_handler);
22813c1cb7e3SWang Nan 		if (rec->opts.auxtrace_snapshot_mode)
22825f9cf599SWang Nan 			trigger_on(&auxtrace_snapshot_trigger);
2283dc0c6127SJiri Olsa 		if (rec->switch_output.enabled)
22843c1cb7e3SWang Nan 			trigger_on(&switch_output_trigger);
2285c0bdc1c4SWang Nan 	} else {
22862dd6d8a1SAdrian Hunter 		signal(SIGUSR2, SIG_IGN);
2287c0bdc1c4SWang Nan 	}
2288f5970550SPeter Zijlstra 
22892681bd85SNamhyung Kim 	session = perf_session__new(data, tool);
22906ef81c55SMamatha Inamdar 	if (IS_ERR(session)) {
2291ffa91880SAdrien BAK 		pr_err("Perf session creation failed.\n");
22926ef81c55SMamatha Inamdar 		return PTR_ERR(session);
2293a9a70bbcSArnaldo Carvalho de Melo 	}
2294a9a70bbcSArnaldo Carvalho de Melo 
2295b5f2511dSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
2296b5f2511dSAlexey Bayduraev 		if (perf_data__is_pipe(&rec->data)) {
2297b5f2511dSAlexey Bayduraev 			pr_err("Parallel trace streaming is not available in pipe mode.\n");
2298b5f2511dSAlexey Bayduraev 			return -1;
2299b5f2511dSAlexey Bayduraev 		}
2300b5f2511dSAlexey Bayduraev 		if (rec->opts.full_auxtrace) {
2301b5f2511dSAlexey Bayduraev 			pr_err("Parallel trace streaming is not available in AUX area tracing mode.\n");
2302b5f2511dSAlexey Bayduraev 			return -1;
2303b5f2511dSAlexey Bayduraev 		}
2304b5f2511dSAlexey Bayduraev 	}
2305b5f2511dSAlexey Bayduraev 
23068ceb41d7SJiri Olsa 	fd = perf_data__fd(data);
2307d20deb64SArnaldo Carvalho de Melo 	rec->session = session;
2308d20deb64SArnaldo Carvalho de Melo 
23095d7f4116SAlexey Budankov 	if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) {
23105d7f4116SAlexey Budankov 		pr_err("Compression initialization failed.\n");
23115d7f4116SAlexey Budankov 		return -1;
23125d7f4116SAlexey Budankov 	}
2313da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
2314da231338SAnand K Mistry 	done_fd = eventfd(0, EFD_NONBLOCK);
2315da231338SAnand K Mistry 	if (done_fd < 0) {
2316da231338SAnand K Mistry 		pr_err("Failed to create wakeup eventfd, error: %m\n");
2317da231338SAnand K Mistry 		status = -1;
2318da231338SAnand K Mistry 		goto out_delete_session;
2319da231338SAnand K Mistry 	}
2320e16c2ce7SYang Jihong 	err = evlist__add_wakeup_eventfd(rec->evlist, done_fd);
2321da231338SAnand K Mistry 	if (err < 0) {
2322da231338SAnand K Mistry 		pr_err("Failed to add wakeup eventfd to poll list\n");
2323da231338SAnand K Mistry 		status = err;
2324da231338SAnand K Mistry 		goto out_delete_session;
2325da231338SAnand K Mistry 	}
2326da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT
23275d7f4116SAlexey Budankov 
23285d7f4116SAlexey Budankov 	session->header.env.comp_type  = PERF_COMP_ZSTD;
23295d7f4116SAlexey Budankov 	session->header.env.comp_level = rec->opts.comp_level;
23305d7f4116SAlexey Budankov 
2331eeb399b5SAdrian Hunter 	if (rec->opts.kcore &&
2332eeb399b5SAdrian Hunter 	    !record__kcore_readable(&session->machines.host)) {
2333eeb399b5SAdrian Hunter 		pr_err("ERROR: kcore is not readable.\n");
2334eeb399b5SAdrian Hunter 		return -1;
2335eeb399b5SAdrian Hunter 	}
2336eeb399b5SAdrian Hunter 
2337d1e325cfSJiri Olsa 	if (record__init_clock(rec))
2338d1e325cfSJiri Olsa 		return -1;
2339d1e325cfSJiri Olsa 
23408c6f45a7SArnaldo Carvalho de Melo 	record__init_features(rec);
2341330aa675SStephane Eranian 
2342d4db3f16SArnaldo Carvalho de Melo 	if (forks) {
23437b392ef0SArnaldo Carvalho de Melo 		err = evlist__prepare_workload(rec->evlist, &opts->target, argv, data->is_pipe,
2344735f7e0bSArnaldo Carvalho de Melo 					       workload_exec_failed_signal);
234535b9d88eSArnaldo Carvalho de Melo 		if (err < 0) {
234635b9d88eSArnaldo Carvalho de Melo 			pr_err("Couldn't run the workload!\n");
234745604710SNamhyung Kim 			status = err;
234835b9d88eSArnaldo Carvalho de Melo 			goto out_delete_session;
2349856e9660SPeter Zijlstra 		}
2350856e9660SPeter Zijlstra 	}
2351856e9660SPeter Zijlstra 
2352ad46e48cSJiri Olsa 	/*
2353ad46e48cSJiri Olsa 	 * If we have just single event and are sending data
2354ad46e48cSJiri Olsa 	 * through pipe, we need to force the ids allocation,
2355ad46e48cSJiri Olsa 	 * because we synthesize event name through the pipe
2356ad46e48cSJiri Olsa 	 * and need the id for that.
2357ad46e48cSJiri Olsa 	 */
23586484d2f9SJiri Olsa 	if (data->is_pipe && rec->evlist->core.nr_entries == 1)
2359ad46e48cSJiri Olsa 		rec->opts.sample_id = true;
2360ad46e48cSJiri Olsa 
236191c0f5ecSJin Yao 	record__uniquify_name(rec);
236291c0f5ecSJin Yao 
23638c6f45a7SArnaldo Carvalho de Melo 	if (record__open(rec) != 0) {
23648d3eca20SDavid Ahern 		err = -1;
2365396b626bSAlexey Bayduraev 		goto out_free_threads;
23668d3eca20SDavid Ahern 	}
2367f6fa4375SJiri Olsa 	session->header.env.comp_mmap_len = session->evlist->core.mmap_len;
236886470930SIngo Molnar 
2369eeb399b5SAdrian Hunter 	if (rec->opts.kcore) {
2370eeb399b5SAdrian Hunter 		err = record__kcore_copy(&session->machines.host, data);
2371eeb399b5SAdrian Hunter 		if (err) {
2372eeb399b5SAdrian Hunter 			pr_err("ERROR: Failed to copy kcore\n");
2373396b626bSAlexey Bayduraev 			goto out_free_threads;
2374eeb399b5SAdrian Hunter 		}
2375eeb399b5SAdrian Hunter 	}
2376eeb399b5SAdrian Hunter 
23778690a2a7SWang Nan 	err = bpf__apply_obj_config();
23788690a2a7SWang Nan 	if (err) {
23798690a2a7SWang Nan 		char errbuf[BUFSIZ];
23808690a2a7SWang Nan 
23818690a2a7SWang Nan 		bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
23828690a2a7SWang Nan 		pr_err("ERROR: Apply config to BPF failed: %s\n",
23838690a2a7SWang Nan 			 errbuf);
2384396b626bSAlexey Bayduraev 		goto out_free_threads;
23858690a2a7SWang Nan 	}
23868690a2a7SWang Nan 
2387cca8482cSAdrian Hunter 	/*
2388cca8482cSAdrian Hunter 	 * Normally perf_session__new would do this, but it doesn't have the
2389cca8482cSAdrian Hunter 	 * evlist.
2390cca8482cSAdrian Hunter 	 */
23918cedf3a5SArnaldo Carvalho de Melo 	if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) {
2392cca8482cSAdrian Hunter 		pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
2393cca8482cSAdrian Hunter 		rec->tool.ordered_events = false;
2394cca8482cSAdrian Hunter 	}
2395cca8482cSAdrian Hunter 
23963a683120SJiri Olsa 	if (!rec->evlist->core.nr_groups)
2397a8bb559bSNamhyung Kim 		perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
2398a8bb559bSNamhyung Kim 
23998ceb41d7SJiri Olsa 	if (data->is_pipe) {
240042aa276fSNamhyung Kim 		err = perf_header__write_pipe(fd);
2401529870e3STom Zanussi 		if (err < 0)
2402396b626bSAlexey Bayduraev 			goto out_free_threads;
2403563aecb2SJiri Olsa 	} else {
240442aa276fSNamhyung Kim 		err = perf_session__write_header(session, rec->evlist, fd, false);
2405d5eed904SArnaldo Carvalho de Melo 		if (err < 0)
2406396b626bSAlexey Bayduraev 			goto out_free_threads;
2407d5eed904SArnaldo Carvalho de Melo 	}
24087c6a1c65SPeter Zijlstra 
2409b38d85efSArnaldo Carvalho de Melo 	err = -1;
2410d3665498SDavid Ahern 	if (!rec->no_buildid
2411e20960c0SRobert Richter 	    && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
2412d3665498SDavid Ahern 		pr_err("Couldn't generate buildids. "
2413e20960c0SRobert Richter 		       "Use --no-buildid to profile anyway.\n");
2414396b626bSAlexey Bayduraev 		goto out_free_threads;
2415e20960c0SRobert Richter 	}
2416e20960c0SRobert Richter 
241723cbb41cSArnaldo Carvalho de Melo 	err = record__setup_sb_evlist(rec);
241823cbb41cSArnaldo Carvalho de Melo 	if (err)
2419396b626bSAlexey Bayduraev 		goto out_free_threads;
2420657ee553SSong Liu 
24214ea648aeSWang Nan 	err = record__synthesize(rec, false);
2422c45c86ebSWang Nan 	if (err < 0)
2423396b626bSAlexey Bayduraev 		goto out_free_threads;
24248d3eca20SDavid Ahern 
2425d20deb64SArnaldo Carvalho de Melo 	if (rec->realtime_prio) {
242686470930SIngo Molnar 		struct sched_param param;
242786470930SIngo Molnar 
2428d20deb64SArnaldo Carvalho de Melo 		param.sched_priority = rec->realtime_prio;
242986470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
24306beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
24318d3eca20SDavid Ahern 			err = -1;
2432396b626bSAlexey Bayduraev 			goto out_free_threads;
243386470930SIngo Molnar 		}
243486470930SIngo Molnar 	}
243586470930SIngo Molnar 
2436396b626bSAlexey Bayduraev 	if (record__start_threads(rec))
2437396b626bSAlexey Bayduraev 		goto out_free_threads;
2438396b626bSAlexey Bayduraev 
2439774cb499SJiri Olsa 	/*
2440774cb499SJiri Olsa 	 * When perf is starting the traced process, all the events
2441774cb499SJiri Olsa 	 * (apart from group members) have enable_on_exec=1 set,
2442774cb499SJiri Olsa 	 * so don't spoil it by prematurely enabling them.
2443774cb499SJiri Olsa 	 */
24446619a53eSAndi Kleen 	if (!target__none(&opts->target) && !opts->initial_delay)
24451c87f165SJiri Olsa 		evlist__enable(rec->evlist);
2446764e16a3SDavid Ahern 
2447856e9660SPeter Zijlstra 	/*
2448856e9660SPeter Zijlstra 	 * Let the child rip
2449856e9660SPeter Zijlstra 	 */
2450e803cf97SNamhyung Kim 	if (forks) {
245120a8a3cfSJiri Olsa 		struct machine *machine = &session->machines.host;
2452e5bed564SNamhyung Kim 		union perf_event *event;
2453e907caf3SHari Bathini 		pid_t tgid;
2454e5bed564SNamhyung Kim 
2455e5bed564SNamhyung Kim 		event = malloc(sizeof(event->comm) + machine->id_hdr_size);
2456e5bed564SNamhyung Kim 		if (event == NULL) {
2457e5bed564SNamhyung Kim 			err = -ENOMEM;
2458e5bed564SNamhyung Kim 			goto out_child;
2459e5bed564SNamhyung Kim 		}
2460e5bed564SNamhyung Kim 
2461e803cf97SNamhyung Kim 		/*
2462e803cf97SNamhyung Kim 		 * Some H/W events are generated before COMM event
2463e803cf97SNamhyung Kim 		 * which is emitted during exec(), so perf script
2464e803cf97SNamhyung Kim 		 * cannot see a correct process name for those events.
2465e803cf97SNamhyung Kim 		 * Synthesize COMM event to prevent it.
2466e803cf97SNamhyung Kim 		 */
2467e907caf3SHari Bathini 		tgid = perf_event__synthesize_comm(tool, event,
2468e803cf97SNamhyung Kim 						   rec->evlist->workload.pid,
2469e803cf97SNamhyung Kim 						   process_synthesized_event,
2470e803cf97SNamhyung Kim 						   machine);
2471e5bed564SNamhyung Kim 		free(event);
2472e803cf97SNamhyung Kim 
2473e907caf3SHari Bathini 		if (tgid == -1)
2474e907caf3SHari Bathini 			goto out_child;
2475e907caf3SHari Bathini 
2476e907caf3SHari Bathini 		event = malloc(sizeof(event->namespaces) +
2477e907caf3SHari Bathini 			       (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
2478e907caf3SHari Bathini 			       machine->id_hdr_size);
2479e907caf3SHari Bathini 		if (event == NULL) {
2480e907caf3SHari Bathini 			err = -ENOMEM;
2481e907caf3SHari Bathini 			goto out_child;
2482e907caf3SHari Bathini 		}
2483e907caf3SHari Bathini 
2484e907caf3SHari Bathini 		/*
2485e907caf3SHari Bathini 		 * Synthesize NAMESPACES event for the command specified.
2486e907caf3SHari Bathini 		 */
2487e907caf3SHari Bathini 		perf_event__synthesize_namespaces(tool, event,
2488e907caf3SHari Bathini 						  rec->evlist->workload.pid,
2489e907caf3SHari Bathini 						  tgid, process_synthesized_event,
2490e907caf3SHari Bathini 						  machine);
2491e907caf3SHari Bathini 		free(event);
2492e907caf3SHari Bathini 
24937b392ef0SArnaldo Carvalho de Melo 		evlist__start_workload(rec->evlist);
2494e803cf97SNamhyung Kim 	}
2495856e9660SPeter Zijlstra 
24966619a53eSAndi Kleen 	if (opts->initial_delay) {
249768cd3b45SAlexey Budankov 		pr_info(EVLIST_DISABLED_MSG);
249868cd3b45SAlexey Budankov 		if (opts->initial_delay > 0) {
24990693e680SArnaldo Carvalho de Melo 			usleep(opts->initial_delay * USEC_PER_MSEC);
25001c87f165SJiri Olsa 			evlist__enable(rec->evlist);
250168cd3b45SAlexey Budankov 			pr_info(EVLIST_ENABLED_MSG);
250268cd3b45SAlexey Budankov 		}
25036619a53eSAndi Kleen 	}
25046619a53eSAndi Kleen 
25055f9cf599SWang Nan 	trigger_ready(&auxtrace_snapshot_trigger);
25063c1cb7e3SWang Nan 	trigger_ready(&switch_output_trigger);
2507a074865eSWang Nan 	perf_hooks__invoke_record_start();
25083812d298SAdrian Hunter 
25093812d298SAdrian Hunter 	/*
25103812d298SAdrian Hunter 	 * Must write FINISHED_INIT so it will be seen after all other
25113812d298SAdrian Hunter 	 * synthesized user events, but before any regular events.
25123812d298SAdrian Hunter 	 */
25133812d298SAdrian Hunter 	err = write_finished_init(rec, false);
25143812d298SAdrian Hunter 	if (err < 0)
25153812d298SAdrian Hunter 		goto out_child;
25163812d298SAdrian Hunter 
2517649c48a9SPeter Zijlstra 	for (;;) {
2518396b626bSAlexey Bayduraev 		unsigned long long hits = thread->samples;
251986470930SIngo Molnar 
252005737464SWang Nan 		/*
252105737464SWang Nan 		 * rec->evlist->bkw_mmap_state is possible to be
252205737464SWang Nan 		 * BKW_MMAP_EMPTY here: when done == true and
252305737464SWang Nan 		 * hits != rec->samples in previous round.
252405737464SWang Nan 		 *
2525ade9d208SArnaldo Carvalho de Melo 		 * evlist__toggle_bkw_mmap ensure we never
252605737464SWang Nan 		 * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
252705737464SWang Nan 		 */
252805737464SWang Nan 		if (trigger_is_hit(&switch_output_trigger) || done || draining)
2529ade9d208SArnaldo Carvalho de Melo 			evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
253005737464SWang Nan 
2531470530bbSAlexey Budankov 		if (record__mmap_read_all(rec, false) < 0) {
25325f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
25333c1cb7e3SWang Nan 			trigger_error(&switch_output_trigger);
25348d3eca20SDavid Ahern 			err = -1;
253545604710SNamhyung Kim 			goto out_child;
25368d3eca20SDavid Ahern 		}
253786470930SIngo Molnar 
25382dd6d8a1SAdrian Hunter 		if (auxtrace_record__snapshot_started) {
25392dd6d8a1SAdrian Hunter 			auxtrace_record__snapshot_started = 0;
25405f9cf599SWang Nan 			if (!trigger_is_error(&auxtrace_snapshot_trigger))
2541ce7b0e42SAlexander Shishkin 				record__read_auxtrace_snapshot(rec, false);
25425f9cf599SWang Nan 			if (trigger_is_error(&auxtrace_snapshot_trigger)) {
25432dd6d8a1SAdrian Hunter 				pr_err("AUX area tracing snapshot failed\n");
25442dd6d8a1SAdrian Hunter 				err = -1;
25452dd6d8a1SAdrian Hunter 				goto out_child;
25462dd6d8a1SAdrian Hunter 			}
25472dd6d8a1SAdrian Hunter 		}
25482dd6d8a1SAdrian Hunter 
25493c1cb7e3SWang Nan 		if (trigger_is_hit(&switch_output_trigger)) {
255005737464SWang Nan 			/*
255105737464SWang Nan 			 * If switch_output_trigger is hit, the data in
255205737464SWang Nan 			 * overwritable ring buffer should have been collected,
255305737464SWang Nan 			 * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
255405737464SWang Nan 			 *
255505737464SWang Nan 			 * If SIGUSR2 raise after or during record__mmap_read_all(),
255605737464SWang Nan 			 * record__mmap_read_all() didn't collect data from
255705737464SWang Nan 			 * overwritable ring buffer. Read again.
255805737464SWang Nan 			 */
255905737464SWang Nan 			if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
256005737464SWang Nan 				continue;
25613c1cb7e3SWang Nan 			trigger_ready(&switch_output_trigger);
25623c1cb7e3SWang Nan 
256305737464SWang Nan 			/*
256405737464SWang Nan 			 * Reenable events in overwrite ring buffer after
256505737464SWang Nan 			 * record__mmap_read_all(): we should have collected
256605737464SWang Nan 			 * data from it.
256705737464SWang Nan 			 */
2568ade9d208SArnaldo Carvalho de Melo 			evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
256905737464SWang Nan 
25703c1cb7e3SWang Nan 			if (!quiet)
25713c1cb7e3SWang Nan 				fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
2572396b626bSAlexey Bayduraev 					record__waking(rec));
2573396b626bSAlexey Bayduraev 			thread->waking = 0;
25743c1cb7e3SWang Nan 			fd = record__switch_output(rec, false);
25753c1cb7e3SWang Nan 			if (fd < 0) {
25763c1cb7e3SWang Nan 				pr_err("Failed to switch to new file\n");
25773c1cb7e3SWang Nan 				trigger_error(&switch_output_trigger);
25783c1cb7e3SWang Nan 				err = fd;
25793c1cb7e3SWang Nan 				goto out_child;
25803c1cb7e3SWang Nan 			}
2581bfacbe3bSJiri Olsa 
2582bfacbe3bSJiri Olsa 			/* re-arm the alarm */
2583bfacbe3bSJiri Olsa 			if (rec->switch_output.time)
2584bfacbe3bSJiri Olsa 				alarm(rec->switch_output.time);
25853c1cb7e3SWang Nan 		}
25863c1cb7e3SWang Nan 
2587396b626bSAlexey Bayduraev 		if (hits == thread->samples) {
25886dcf45efSArnaldo Carvalho de Melo 			if (done || draining)
2589649c48a9SPeter Zijlstra 				break;
2590396b626bSAlexey Bayduraev 			err = fdarray__poll(&thread->pollfd, -1);
2591a515114fSJiri Olsa 			/*
2592a515114fSJiri Olsa 			 * Propagate error, only if there's any. Ignore positive
2593a515114fSJiri Olsa 			 * number of returned events and interrupt error.
2594a515114fSJiri Olsa 			 */
2595a515114fSJiri Olsa 			if (err > 0 || (err < 0 && errno == EINTR))
259645604710SNamhyung Kim 				err = 0;
2597396b626bSAlexey Bayduraev 			thread->waking++;
25986dcf45efSArnaldo Carvalho de Melo 
2599396b626bSAlexey Bayduraev 			if (fdarray__filter(&thread->pollfd, POLLERR | POLLHUP,
2600396b626bSAlexey Bayduraev 					    record__thread_munmap_filtered, NULL) == 0)
26016dcf45efSArnaldo Carvalho de Melo 				draining = true;
2602396b626bSAlexey Bayduraev 
26036562c9acSAdrian Hunter 			err = record__update_evlist_pollfd_from_thread(rec, rec->evlist, thread);
26046562c9acSAdrian Hunter 			if (err)
26056562c9acSAdrian Hunter 				goto out_child;
26068b412664SPeter Zijlstra 		}
26078b412664SPeter Zijlstra 
2608acce0223SAlexey Budankov 		if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) {
2609acce0223SAlexey Budankov 			switch (cmd) {
2610d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2611d20aff15SAdrian Hunter 				hit_auxtrace_snapshot_trigger(rec);
2612d20aff15SAdrian Hunter 				evlist__ctlfd_ack(rec->evlist);
2613d20aff15SAdrian Hunter 				break;
2614f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
2615f186cd61SJiri Olsa 				done = 1;
2616f186cd61SJiri Olsa 				break;
2617acce0223SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2618acce0223SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2619991ae4ebSJiri Olsa 			case EVLIST_CTL_CMD_ENABLE:
2620991ae4ebSJiri Olsa 			case EVLIST_CTL_CMD_DISABLE:
2621142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
262247fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2623acce0223SAlexey Budankov 			default:
2624acce0223SAlexey Budankov 				break;
2625acce0223SAlexey Budankov 			}
2626acce0223SAlexey Budankov 		}
2627acce0223SAlexey Budankov 
2628774cb499SJiri Olsa 		/*
2629774cb499SJiri Olsa 		 * When perf is starting the traced process, at the end events
2630774cb499SJiri Olsa 		 * die with the process and we wait for that. Thus no need to
2631774cb499SJiri Olsa 		 * disable events in this case.
2632774cb499SJiri Olsa 		 */
2633602ad878SArnaldo Carvalho de Melo 		if (done && !disabled && !target__none(&opts->target)) {
26345f9cf599SWang Nan 			trigger_off(&auxtrace_snapshot_trigger);
2635e74676deSJiri Olsa 			evlist__disable(rec->evlist);
26362711926aSJiri Olsa 			disabled = true;
26372711926aSJiri Olsa 		}
26388b412664SPeter Zijlstra 	}
2639ce7b0e42SAlexander Shishkin 
26405f9cf599SWang Nan 	trigger_off(&auxtrace_snapshot_trigger);
26413c1cb7e3SWang Nan 	trigger_off(&switch_output_trigger);
26428b412664SPeter Zijlstra 
2643ce7b0e42SAlexander Shishkin 	if (opts->auxtrace_snapshot_on_exit)
2644ce7b0e42SAlexander Shishkin 		record__auxtrace_snapshot_exit(rec);
2645ce7b0e42SAlexander Shishkin 
2646f33cbe72SArnaldo Carvalho de Melo 	if (forks && workload_exec_errno) {
26473535a696SArnaldo Carvalho de Melo 		char msg[STRERR_BUFSIZE], strevsels[2048];
2648c8b5f2c9SArnaldo Carvalho de Melo 		const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
26493535a696SArnaldo Carvalho de Melo 
26503535a696SArnaldo Carvalho de Melo 		evlist__scnprintf_evsels(rec->evlist, sizeof(strevsels), strevsels);
26513535a696SArnaldo Carvalho de Melo 
26523535a696SArnaldo Carvalho de Melo 		pr_err("Failed to collect '%s' for the '%s' workload: %s\n",
26533535a696SArnaldo Carvalho de Melo 			strevsels, argv[0], emsg);
2654f33cbe72SArnaldo Carvalho de Melo 		err = -1;
265545604710SNamhyung Kim 		goto out_child;
2656f33cbe72SArnaldo Carvalho de Melo 	}
2657f33cbe72SArnaldo Carvalho de Melo 
2658e3d59112SNamhyung Kim 	if (!quiet)
2659396b626bSAlexey Bayduraev 		fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n",
2660396b626bSAlexey Bayduraev 			record__waking(rec));
266186470930SIngo Molnar 
26623812d298SAdrian Hunter 	write_finished_init(rec, true);
26633812d298SAdrian Hunter 
26644ea648aeSWang Nan 	if (target__none(&rec->opts.target))
26654ea648aeSWang Nan 		record__synthesize_workload(rec, true);
26664ea648aeSWang Nan 
266745604710SNamhyung Kim out_child:
2668396b626bSAlexey Bayduraev 	record__stop_threads(rec);
2669470530bbSAlexey Budankov 	record__mmap_read_all(rec, true);
2670396b626bSAlexey Bayduraev out_free_threads:
2671415ccb58SAlexey Bayduraev 	record__free_thread_data(rec);
2672396b626bSAlexey Bayduraev 	evlist__finalize_ctlfd(rec->evlist);
2673d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
2674d3d1af6fSAlexey Budankov 
2675d3c8c08eSAlexey Budankov 	if (rec->session->bytes_transferred && rec->session->bytes_compressed) {
2676d3c8c08eSAlexey Budankov 		ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed;
2677d3c8c08eSAlexey Budankov 		session->header.env.comp_ratio = ratio + 0.5;
2678d3c8c08eSAlexey Budankov 	}
2679d3c8c08eSAlexey Budankov 
268045604710SNamhyung Kim 	if (forks) {
268145604710SNamhyung Kim 		int exit_status;
268245604710SNamhyung Kim 
268345604710SNamhyung Kim 		if (!child_finished)
268445604710SNamhyung Kim 			kill(rec->evlist->workload.pid, SIGTERM);
268545604710SNamhyung Kim 
268645604710SNamhyung Kim 		wait(&exit_status);
268745604710SNamhyung Kim 
268845604710SNamhyung Kim 		if (err < 0)
268945604710SNamhyung Kim 			status = err;
269045604710SNamhyung Kim 		else if (WIFEXITED(exit_status))
269145604710SNamhyung Kim 			status = WEXITSTATUS(exit_status);
269245604710SNamhyung Kim 		else if (WIFSIGNALED(exit_status))
269345604710SNamhyung Kim 			signr = WTERMSIG(exit_status);
269445604710SNamhyung Kim 	} else
269545604710SNamhyung Kim 		status = err;
269645604710SNamhyung Kim 
2697edc41a10SNamhyung Kim 	if (rec->off_cpu)
2698edc41a10SNamhyung Kim 		rec->bytes_written += off_cpu_write(rec->session);
2699edc41a10SNamhyung Kim 
27004ea648aeSWang Nan 	record__synthesize(rec, true);
2701e3d59112SNamhyung Kim 	/* this will be recalculated during process_buildids() */
2702e3d59112SNamhyung Kim 	rec->samples = 0;
2703e3d59112SNamhyung Kim 
2704ecfd7a9cSWang Nan 	if (!err) {
2705ecfd7a9cSWang Nan 		if (!rec->timestamp_filename) {
2706e1ab48baSWang Nan 			record__finish_output(rec);
2707ecfd7a9cSWang Nan 		} else {
2708ecfd7a9cSWang Nan 			fd = record__switch_output(rec, true);
2709ecfd7a9cSWang Nan 			if (fd < 0) {
2710ecfd7a9cSWang Nan 				status = fd;
2711ecfd7a9cSWang Nan 				goto out_delete_session;
2712ecfd7a9cSWang Nan 			}
2713ecfd7a9cSWang Nan 		}
2714ecfd7a9cSWang Nan 	}
271539d17dacSArnaldo Carvalho de Melo 
2716a074865eSWang Nan 	perf_hooks__invoke_record_end();
2717a074865eSWang Nan 
2718e3d59112SNamhyung Kim 	if (!err && !quiet) {
2719e3d59112SNamhyung Kim 		char samples[128];
2720ecfd7a9cSWang Nan 		const char *postfix = rec->timestamp_filename ?
2721ecfd7a9cSWang Nan 					".<timestamp>" : "";
2722e3d59112SNamhyung Kim 
2723ef149c25SAdrian Hunter 		if (rec->samples && !rec->opts.full_auxtrace)
2724e3d59112SNamhyung Kim 			scnprintf(samples, sizeof(samples),
2725e3d59112SNamhyung Kim 				  " (%" PRIu64 " samples)", rec->samples);
2726e3d59112SNamhyung Kim 		else
2727e3d59112SNamhyung Kim 			samples[0] = '\0';
2728e3d59112SNamhyung Kim 
2729d3c8c08eSAlexey Budankov 		fprintf(stderr,	"[ perf record: Captured and wrote %.3f MB %s%s%s",
27308ceb41d7SJiri Olsa 			perf_data__size(data) / 1024.0 / 1024.0,
27312d4f2799SJiri Olsa 			data->path, postfix, samples);
2732d3c8c08eSAlexey Budankov 		if (ratio) {
2733d3c8c08eSAlexey Budankov 			fprintf(stderr,	", compressed (original %.3f MB, ratio is %.3f)",
2734d3c8c08eSAlexey Budankov 					rec->session->bytes_transferred / 1024.0 / 1024.0,
2735d3c8c08eSAlexey Budankov 					ratio);
2736d3c8c08eSAlexey Budankov 		}
2737d3c8c08eSAlexey Budankov 		fprintf(stderr, " ]\n");
2738e3d59112SNamhyung Kim 	}
2739e3d59112SNamhyung Kim 
274039d17dacSArnaldo Carvalho de Melo out_delete_session:
2741da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
2742da231338SAnand K Mistry 	if (done_fd >= 0)
2743da231338SAnand K Mistry 		close(done_fd);
2744da231338SAnand K Mistry #endif
27455d7f4116SAlexey Budankov 	zstd_fini(&session->zstd_data);
274639d17dacSArnaldo Carvalho de Melo 	perf_session__delete(session);
2747657ee553SSong Liu 
2748657ee553SSong Liu 	if (!opts->no_bpf_event)
274908c83997SArnaldo Carvalho de Melo 		evlist__stop_sb_thread(rec->sb_evlist);
275045604710SNamhyung Kim 	return status;
275186470930SIngo Molnar }
275286470930SIngo Molnar 
27530883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain)
275409b0fd45SJiri Olsa {
2755aad2b21cSKan Liang 	static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
2756a601fdffSJiri Olsa 
27570883e820SArnaldo Carvalho de Melo 	pr_debug("callchain: type %s\n", str[callchain->record_mode]);
275826d33022SJiri Olsa 
27590883e820SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_DWARF)
276009b0fd45SJiri Olsa 		pr_debug("callchain: stack dump size %d\n",
27610883e820SArnaldo Carvalho de Melo 			 callchain->dump_size);
27620883e820SArnaldo Carvalho de Melo }
27630883e820SArnaldo Carvalho de Melo 
27640883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record,
27650883e820SArnaldo Carvalho de Melo 				 struct callchain_param *callchain,
27660883e820SArnaldo Carvalho de Melo 				 const char *arg, bool unset)
27670883e820SArnaldo Carvalho de Melo {
27680883e820SArnaldo Carvalho de Melo 	int ret;
27690883e820SArnaldo Carvalho de Melo 	callchain->enabled = !unset;
27700883e820SArnaldo Carvalho de Melo 
27710883e820SArnaldo Carvalho de Melo 	/* --no-call-graph */
27720883e820SArnaldo Carvalho de Melo 	if (unset) {
27730883e820SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_NONE;
27740883e820SArnaldo Carvalho de Melo 		pr_debug("callchain: disabled\n");
27750883e820SArnaldo Carvalho de Melo 		return 0;
27760883e820SArnaldo Carvalho de Melo 	}
27770883e820SArnaldo Carvalho de Melo 
27780883e820SArnaldo Carvalho de Melo 	ret = parse_callchain_record_opt(arg, callchain);
27790883e820SArnaldo Carvalho de Melo 	if (!ret) {
27800883e820SArnaldo Carvalho de Melo 		/* Enable data address sampling for DWARF unwind. */
27810883e820SArnaldo Carvalho de Melo 		if (callchain->record_mode == CALLCHAIN_DWARF)
27820883e820SArnaldo Carvalho de Melo 			record->sample_address = true;
27830883e820SArnaldo Carvalho de Melo 		callchain_debug(callchain);
27840883e820SArnaldo Carvalho de Melo 	}
27850883e820SArnaldo Carvalho de Melo 
27860883e820SArnaldo Carvalho de Melo 	return ret;
278709b0fd45SJiri Olsa }
278809b0fd45SJiri Olsa 
2789c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt,
279009b0fd45SJiri Olsa 			       const char *arg,
279109b0fd45SJiri Olsa 			       int unset)
279209b0fd45SJiri Olsa {
27930883e820SArnaldo Carvalho de Melo 	return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
279426d33022SJiri Olsa }
279526d33022SJiri Olsa 
2796c421e80bSKan Liang int record_callchain_opt(const struct option *opt,
279709b0fd45SJiri Olsa 			 const char *arg __maybe_unused,
279809b0fd45SJiri Olsa 			 int unset __maybe_unused)
279909b0fd45SJiri Olsa {
28002ddd5c04SArnaldo Carvalho de Melo 	struct callchain_param *callchain = opt->value;
2801c421e80bSKan Liang 
28022ddd5c04SArnaldo Carvalho de Melo 	callchain->enabled = true;
280309b0fd45SJiri Olsa 
28042ddd5c04SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_NONE)
28052ddd5c04SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_FP;
2806eb853e80SJiri Olsa 
28072ddd5c04SArnaldo Carvalho de Melo 	callchain_debug(callchain);
280809b0fd45SJiri Olsa 	return 0;
280909b0fd45SJiri Olsa }
281009b0fd45SJiri Olsa 
2811eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb)
2812eb853e80SJiri Olsa {
28137a29c087SNamhyung Kim 	struct record *rec = cb;
28147a29c087SNamhyung Kim 
28157a29c087SNamhyung Kim 	if (!strcmp(var, "record.build-id")) {
28167a29c087SNamhyung Kim 		if (!strcmp(value, "cache"))
28177a29c087SNamhyung Kim 			rec->no_buildid_cache = false;
28187a29c087SNamhyung Kim 		else if (!strcmp(value, "no-cache"))
28197a29c087SNamhyung Kim 			rec->no_buildid_cache = true;
28207a29c087SNamhyung Kim 		else if (!strcmp(value, "skip"))
28217a29c087SNamhyung Kim 			rec->no_buildid = true;
2822e29386c8SJiri Olsa 		else if (!strcmp(value, "mmap"))
2823e29386c8SJiri Olsa 			rec->buildid_mmap = true;
28247a29c087SNamhyung Kim 		else
28257a29c087SNamhyung Kim 			return -1;
28267a29c087SNamhyung Kim 		return 0;
28277a29c087SNamhyung Kim 	}
2828cff17205SYisheng Xie 	if (!strcmp(var, "record.call-graph")) {
2829cff17205SYisheng Xie 		var = "call-graph.record-mode";
2830eb853e80SJiri Olsa 		return perf_default_config(var, value, cb);
2831eb853e80SJiri Olsa 	}
283293f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
283393f20c0fSAlexey Budankov 	if (!strcmp(var, "record.aio")) {
283493f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = strtol(value, NULL, 0);
283593f20c0fSAlexey Budankov 		if (!rec->opts.nr_cblocks)
283693f20c0fSAlexey Budankov 			rec->opts.nr_cblocks = nr_cblocks_default;
283793f20c0fSAlexey Budankov 	}
283893f20c0fSAlexey Budankov #endif
28399bce13eaSJiri Olsa 	if (!strcmp(var, "record.debuginfod")) {
28409bce13eaSJiri Olsa 		rec->debuginfod.urls = strdup(value);
28419bce13eaSJiri Olsa 		if (!rec->debuginfod.urls)
28429bce13eaSJiri Olsa 			return -ENOMEM;
28439bce13eaSJiri Olsa 		rec->debuginfod.set = true;
28449bce13eaSJiri Olsa 	}
2845eb853e80SJiri Olsa 
2846cff17205SYisheng Xie 	return 0;
2847cff17205SYisheng Xie }
2848cff17205SYisheng Xie 
2849814c8c38SPeter Zijlstra 
2850f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset)
2851f4fe11b7SAlexey Budankov {
2852f4fe11b7SAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
2853f4fe11b7SAlexey Budankov 
2854f4fe11b7SAlexey Budankov 	if (unset || !str)
2855f4fe11b7SAlexey Budankov 		return 0;
2856f4fe11b7SAlexey Budankov 
2857f4fe11b7SAlexey Budankov 	if (!strcasecmp(str, "node"))
2858f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_NODE;
2859f4fe11b7SAlexey Budankov 	else if (!strcasecmp(str, "cpu"))
2860f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_CPU;
2861f4fe11b7SAlexey Budankov 
2862f4fe11b7SAlexey Budankov 	return 0;
2863f4fe11b7SAlexey Budankov }
2864f4fe11b7SAlexey Budankov 
28657954f716SAlexey Bayduraev static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits)
28667954f716SAlexey Bayduraev {
28677954f716SAlexey Bayduraev 	mask->nbits = nr_bits;
28687954f716SAlexey Bayduraev 	mask->bits = bitmap_zalloc(mask->nbits);
28697954f716SAlexey Bayduraev 	if (!mask->bits)
28707954f716SAlexey Bayduraev 		return -ENOMEM;
28717954f716SAlexey Bayduraev 
28727954f716SAlexey Bayduraev 	return 0;
28737954f716SAlexey Bayduraev }
28747954f716SAlexey Bayduraev 
28757954f716SAlexey Bayduraev static void record__mmap_cpu_mask_free(struct mmap_cpu_mask *mask)
28767954f716SAlexey Bayduraev {
28777954f716SAlexey Bayduraev 	bitmap_free(mask->bits);
28787954f716SAlexey Bayduraev 	mask->nbits = 0;
28797954f716SAlexey Bayduraev }
28807954f716SAlexey Bayduraev 
28817954f716SAlexey Bayduraev static int record__thread_mask_alloc(struct thread_mask *mask, int nr_bits)
28827954f716SAlexey Bayduraev {
28837954f716SAlexey Bayduraev 	int ret;
28847954f716SAlexey Bayduraev 
28857954f716SAlexey Bayduraev 	ret = record__mmap_cpu_mask_alloc(&mask->maps, nr_bits);
28867954f716SAlexey Bayduraev 	if (ret) {
28877954f716SAlexey Bayduraev 		mask->affinity.bits = NULL;
28887954f716SAlexey Bayduraev 		return ret;
28897954f716SAlexey Bayduraev 	}
28907954f716SAlexey Bayduraev 
28917954f716SAlexey Bayduraev 	ret = record__mmap_cpu_mask_alloc(&mask->affinity, nr_bits);
28927954f716SAlexey Bayduraev 	if (ret) {
28937954f716SAlexey Bayduraev 		record__mmap_cpu_mask_free(&mask->maps);
28947954f716SAlexey Bayduraev 		mask->maps.bits = NULL;
28957954f716SAlexey Bayduraev 	}
28967954f716SAlexey Bayduraev 
28977954f716SAlexey Bayduraev 	return ret;
28987954f716SAlexey Bayduraev }
28997954f716SAlexey Bayduraev 
29007954f716SAlexey Bayduraev static void record__thread_mask_free(struct thread_mask *mask)
29017954f716SAlexey Bayduraev {
29027954f716SAlexey Bayduraev 	record__mmap_cpu_mask_free(&mask->maps);
29037954f716SAlexey Bayduraev 	record__mmap_cpu_mask_free(&mask->affinity);
29047954f716SAlexey Bayduraev }
29057954f716SAlexey Bayduraev 
290606380a84SAlexey Bayduraev static int record__parse_threads(const struct option *opt, const char *str, int unset)
290706380a84SAlexey Bayduraev {
2908f466e5edSAlexey Bayduraev 	int s;
290906380a84SAlexey Bayduraev 	struct record_opts *opts = opt->value;
291006380a84SAlexey Bayduraev 
2911f466e5edSAlexey Bayduraev 	if (unset || !str || !strlen(str)) {
291206380a84SAlexey Bayduraev 		opts->threads_spec = THREAD_SPEC__CPU;
2913f466e5edSAlexey Bayduraev 	} else {
2914f466e5edSAlexey Bayduraev 		for (s = 1; s < THREAD_SPEC__MAX; s++) {
2915f466e5edSAlexey Bayduraev 			if (s == THREAD_SPEC__USER) {
2916f466e5edSAlexey Bayduraev 				opts->threads_user_spec = strdup(str);
2917f466e5edSAlexey Bayduraev 				if (!opts->threads_user_spec)
2918f466e5edSAlexey Bayduraev 					return -ENOMEM;
2919f466e5edSAlexey Bayduraev 				opts->threads_spec = THREAD_SPEC__USER;
2920f466e5edSAlexey Bayduraev 				break;
2921f466e5edSAlexey Bayduraev 			}
2922f466e5edSAlexey Bayduraev 			if (!strncasecmp(str, thread_spec_tags[s], strlen(thread_spec_tags[s]))) {
2923f466e5edSAlexey Bayduraev 				opts->threads_spec = s;
2924f466e5edSAlexey Bayduraev 				break;
2925f466e5edSAlexey Bayduraev 			}
2926f466e5edSAlexey Bayduraev 		}
2927f466e5edSAlexey Bayduraev 	}
2928f466e5edSAlexey Bayduraev 
2929f466e5edSAlexey Bayduraev 	if (opts->threads_spec == THREAD_SPEC__USER)
2930f466e5edSAlexey Bayduraev 		pr_debug("threads_spec: %s\n", opts->threads_user_spec);
2931f466e5edSAlexey Bayduraev 	else
2932f466e5edSAlexey Bayduraev 		pr_debug("threads_spec: %s\n", thread_spec_tags[opts->threads_spec]);
293306380a84SAlexey Bayduraev 
293406380a84SAlexey Bayduraev 	return 0;
293506380a84SAlexey Bayduraev }
293606380a84SAlexey Bayduraev 
29376d575816SJiwei Sun static int parse_output_max_size(const struct option *opt,
29386d575816SJiwei Sun 				 const char *str, int unset)
29396d575816SJiwei Sun {
29406d575816SJiwei Sun 	unsigned long *s = (unsigned long *)opt->value;
29416d575816SJiwei Sun 	static struct parse_tag tags_size[] = {
29426d575816SJiwei Sun 		{ .tag  = 'B', .mult = 1       },
29436d575816SJiwei Sun 		{ .tag  = 'K', .mult = 1 << 10 },
29446d575816SJiwei Sun 		{ .tag  = 'M', .mult = 1 << 20 },
29456d575816SJiwei Sun 		{ .tag  = 'G', .mult = 1 << 30 },
29466d575816SJiwei Sun 		{ .tag  = 0 },
29476d575816SJiwei Sun 	};
29486d575816SJiwei Sun 	unsigned long val;
29496d575816SJiwei Sun 
29506d575816SJiwei Sun 	if (unset) {
29516d575816SJiwei Sun 		*s = 0;
29526d575816SJiwei Sun 		return 0;
29536d575816SJiwei Sun 	}
29546d575816SJiwei Sun 
29556d575816SJiwei Sun 	val = parse_tag_value(str, tags_size);
29566d575816SJiwei Sun 	if (val != (unsigned long) -1) {
29576d575816SJiwei Sun 		*s = val;
29586d575816SJiwei Sun 		return 0;
29596d575816SJiwei Sun 	}
29606d575816SJiwei Sun 
29616d575816SJiwei Sun 	return -1;
29626d575816SJiwei Sun }
29636d575816SJiwei Sun 
2964e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt,
2965e9db1310SAdrian Hunter 				    const char *str,
2966e9db1310SAdrian Hunter 				    int unset __maybe_unused)
2967e9db1310SAdrian Hunter {
2968e9db1310SAdrian Hunter 	struct record_opts *opts = opt->value;
2969e9db1310SAdrian Hunter 	char *s, *p;
2970e9db1310SAdrian Hunter 	unsigned int mmap_pages;
2971e9db1310SAdrian Hunter 	int ret;
2972e9db1310SAdrian Hunter 
2973e9db1310SAdrian Hunter 	if (!str)
2974e9db1310SAdrian Hunter 		return -EINVAL;
2975e9db1310SAdrian Hunter 
2976e9db1310SAdrian Hunter 	s = strdup(str);
2977e9db1310SAdrian Hunter 	if (!s)
2978e9db1310SAdrian Hunter 		return -ENOMEM;
2979e9db1310SAdrian Hunter 
2980e9db1310SAdrian Hunter 	p = strchr(s, ',');
2981e9db1310SAdrian Hunter 	if (p)
2982e9db1310SAdrian Hunter 		*p = '\0';
2983e9db1310SAdrian Hunter 
2984e9db1310SAdrian Hunter 	if (*s) {
298525f84702SArnaldo Carvalho de Melo 		ret = __evlist__parse_mmap_pages(&mmap_pages, s);
2986e9db1310SAdrian Hunter 		if (ret)
2987e9db1310SAdrian Hunter 			goto out_free;
2988e9db1310SAdrian Hunter 		opts->mmap_pages = mmap_pages;
2989e9db1310SAdrian Hunter 	}
2990e9db1310SAdrian Hunter 
2991e9db1310SAdrian Hunter 	if (!p) {
2992e9db1310SAdrian Hunter 		ret = 0;
2993e9db1310SAdrian Hunter 		goto out_free;
2994e9db1310SAdrian Hunter 	}
2995e9db1310SAdrian Hunter 
299625f84702SArnaldo Carvalho de Melo 	ret = __evlist__parse_mmap_pages(&mmap_pages, p + 1);
2997e9db1310SAdrian Hunter 	if (ret)
2998e9db1310SAdrian Hunter 		goto out_free;
2999e9db1310SAdrian Hunter 
3000e9db1310SAdrian Hunter 	opts->auxtrace_mmap_pages = mmap_pages;
3001e9db1310SAdrian Hunter 
3002e9db1310SAdrian Hunter out_free:
3003e9db1310SAdrian Hunter 	free(s);
3004e9db1310SAdrian Hunter 	return ret;
3005e9db1310SAdrian Hunter }
3006e9db1310SAdrian Hunter 
30077248e308SAlexandre Truong void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __maybe_unused)
30087248e308SAlexandre Truong {
30097248e308SAlexandre Truong }
30107248e308SAlexandre Truong 
30111d078ccbSAlexey Budankov static int parse_control_option(const struct option *opt,
30121d078ccbSAlexey Budankov 				const char *str,
30131d078ccbSAlexey Budankov 				int unset __maybe_unused)
30141d078ccbSAlexey Budankov {
30159864a66dSAdrian Hunter 	struct record_opts *opts = opt->value;
30161d078ccbSAlexey Budankov 
3017a8fcbd26SAdrian Hunter 	return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close);
3018a8fcbd26SAdrian Hunter }
3019a8fcbd26SAdrian Hunter 
30200c582449SJiri Olsa static void switch_output_size_warn(struct record *rec)
30210c582449SJiri Olsa {
30229521b5f2SJiri Olsa 	u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages);
30230c582449SJiri Olsa 	struct switch_output *s = &rec->switch_output;
30240c582449SJiri Olsa 
30250c582449SJiri Olsa 	wakeup_size /= 2;
30260c582449SJiri Olsa 
30270c582449SJiri Olsa 	if (s->size < wakeup_size) {
30280c582449SJiri Olsa 		char buf[100];
30290c582449SJiri Olsa 
30300c582449SJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
30310c582449SJiri Olsa 		pr_warning("WARNING: switch-output data size lower than "
30320c582449SJiri Olsa 			   "wakeup kernel buffer size (%s) "
30330c582449SJiri Olsa 			   "expect bigger perf.data sizes\n", buf);
30340c582449SJiri Olsa 	}
30350c582449SJiri Olsa }
30360c582449SJiri Olsa 
3037cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec)
3038cb4e1ebbSJiri Olsa {
3039cb4e1ebbSJiri Olsa 	struct switch_output *s = &rec->switch_output;
3040dc0c6127SJiri Olsa 	static struct parse_tag tags_size[] = {
3041dc0c6127SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
3042dc0c6127SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
3043dc0c6127SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
3044dc0c6127SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
3045dc0c6127SJiri Olsa 		{ .tag  = 0 },
3046dc0c6127SJiri Olsa 	};
3047bfacbe3bSJiri Olsa 	static struct parse_tag tags_time[] = {
3048bfacbe3bSJiri Olsa 		{ .tag  = 's', .mult = 1        },
3049bfacbe3bSJiri Olsa 		{ .tag  = 'm', .mult = 60       },
3050bfacbe3bSJiri Olsa 		{ .tag  = 'h', .mult = 60*60    },
3051bfacbe3bSJiri Olsa 		{ .tag  = 'd', .mult = 60*60*24 },
3052bfacbe3bSJiri Olsa 		{ .tag  = 0 },
3053bfacbe3bSJiri Olsa 	};
3054dc0c6127SJiri Olsa 	unsigned long val;
3055cb4e1ebbSJiri Olsa 
3056899e5ffbSArnaldo Carvalho de Melo 	/*
3057899e5ffbSArnaldo Carvalho de Melo 	 * If we're using --switch-output-events, then we imply its
3058899e5ffbSArnaldo Carvalho de Melo 	 * --switch-output=signal, as we'll send a SIGUSR2 from the side band
3059899e5ffbSArnaldo Carvalho de Melo 	 *  thread to its parent.
3060899e5ffbSArnaldo Carvalho de Melo 	 */
3061b5f2511dSAlexey Bayduraev 	if (rec->switch_output_event_set) {
3062b5f2511dSAlexey Bayduraev 		if (record__threads_enabled(rec)) {
3063b5f2511dSAlexey Bayduraev 			pr_warning("WARNING: --switch-output-event option is not available in parallel streaming mode.\n");
3064b5f2511dSAlexey Bayduraev 			return 0;
3065b5f2511dSAlexey Bayduraev 		}
3066899e5ffbSArnaldo Carvalho de Melo 		goto do_signal;
3067b5f2511dSAlexey Bayduraev 	}
3068899e5ffbSArnaldo Carvalho de Melo 
3069cb4e1ebbSJiri Olsa 	if (!s->set)
3070cb4e1ebbSJiri Olsa 		return 0;
3071cb4e1ebbSJiri Olsa 
3072b5f2511dSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
3073b5f2511dSAlexey Bayduraev 		pr_warning("WARNING: --switch-output option is not available in parallel streaming mode.\n");
3074b5f2511dSAlexey Bayduraev 		return 0;
3075b5f2511dSAlexey Bayduraev 	}
3076b5f2511dSAlexey Bayduraev 
3077cb4e1ebbSJiri Olsa 	if (!strcmp(s->str, "signal")) {
3078899e5ffbSArnaldo Carvalho de Melo do_signal:
3079cb4e1ebbSJiri Olsa 		s->signal = true;
3080cb4e1ebbSJiri Olsa 		pr_debug("switch-output with SIGUSR2 signal\n");
3081dc0c6127SJiri Olsa 		goto enabled;
3082dc0c6127SJiri Olsa 	}
3083dc0c6127SJiri Olsa 
3084dc0c6127SJiri Olsa 	val = parse_tag_value(s->str, tags_size);
3085dc0c6127SJiri Olsa 	if (val != (unsigned long) -1) {
3086dc0c6127SJiri Olsa 		s->size = val;
3087dc0c6127SJiri Olsa 		pr_debug("switch-output with %s size threshold\n", s->str);
3088dc0c6127SJiri Olsa 		goto enabled;
3089cb4e1ebbSJiri Olsa 	}
3090cb4e1ebbSJiri Olsa 
3091bfacbe3bSJiri Olsa 	val = parse_tag_value(s->str, tags_time);
3092bfacbe3bSJiri Olsa 	if (val != (unsigned long) -1) {
3093bfacbe3bSJiri Olsa 		s->time = val;
3094bfacbe3bSJiri Olsa 		pr_debug("switch-output with %s time threshold (%lu seconds)\n",
3095bfacbe3bSJiri Olsa 			 s->str, s->time);
3096bfacbe3bSJiri Olsa 		goto enabled;
3097bfacbe3bSJiri Olsa 	}
3098bfacbe3bSJiri Olsa 
3099cb4e1ebbSJiri Olsa 	return -1;
3100dc0c6127SJiri Olsa 
3101dc0c6127SJiri Olsa enabled:
3102dc0c6127SJiri Olsa 	rec->timestamp_filename = true;
3103dc0c6127SJiri Olsa 	s->enabled              = true;
31040c582449SJiri Olsa 
31050c582449SJiri Olsa 	if (s->size && !rec->opts.no_buffering)
31060c582449SJiri Olsa 		switch_output_size_warn(rec);
31070c582449SJiri Olsa 
3108dc0c6127SJiri Olsa 	return 0;
3109cb4e1ebbSJiri Olsa }
3110cb4e1ebbSJiri Olsa 
3111e5b2c207SNamhyung Kim static const char * const __record_usage[] = {
311286470930SIngo Molnar 	"perf record [<options>] [<command>]",
311386470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
311486470930SIngo Molnar 	NULL
311586470930SIngo Molnar };
3116e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage;
311786470930SIngo Molnar 
31186e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event,
31196e0a9b3dSArnaldo Carvalho de Melo 				  struct perf_sample *sample, struct machine *machine)
31206e0a9b3dSArnaldo Carvalho de Melo {
31216e0a9b3dSArnaldo Carvalho de Melo 	/*
31226e0a9b3dSArnaldo Carvalho de Melo 	 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
31236e0a9b3dSArnaldo Carvalho de Melo 	 * no need to add them twice.
31246e0a9b3dSArnaldo Carvalho de Melo 	 */
31256e0a9b3dSArnaldo Carvalho de Melo 	if (!(event->header.misc & PERF_RECORD_MISC_USER))
31266e0a9b3dSArnaldo Carvalho de Melo 		return 0;
31276e0a9b3dSArnaldo Carvalho de Melo 	return perf_event__process_mmap(tool, event, sample, machine);
31286e0a9b3dSArnaldo Carvalho de Melo }
31296e0a9b3dSArnaldo Carvalho de Melo 
31306e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event,
31316e0a9b3dSArnaldo Carvalho de Melo 				   struct perf_sample *sample, struct machine *machine)
31326e0a9b3dSArnaldo Carvalho de Melo {
31336e0a9b3dSArnaldo Carvalho de Melo 	/*
31346e0a9b3dSArnaldo Carvalho de Melo 	 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
31356e0a9b3dSArnaldo Carvalho de Melo 	 * no need to add them twice.
31366e0a9b3dSArnaldo Carvalho de Melo 	 */
31376e0a9b3dSArnaldo Carvalho de Melo 	if (!(event->header.misc & PERF_RECORD_MISC_USER))
31386e0a9b3dSArnaldo Carvalho de Melo 		return 0;
31396e0a9b3dSArnaldo Carvalho de Melo 
31406e0a9b3dSArnaldo Carvalho de Melo 	return perf_event__process_mmap2(tool, event, sample, machine);
31416e0a9b3dSArnaldo Carvalho de Melo }
31426e0a9b3dSArnaldo Carvalho de Melo 
314366286ed3SAdrian Hunter static int process_timestamp_boundary(struct perf_tool *tool,
314466286ed3SAdrian Hunter 				      union perf_event *event __maybe_unused,
314566286ed3SAdrian Hunter 				      struct perf_sample *sample,
314666286ed3SAdrian Hunter 				      struct machine *machine __maybe_unused)
314766286ed3SAdrian Hunter {
314866286ed3SAdrian Hunter 	struct record *rec = container_of(tool, struct record, tool);
314966286ed3SAdrian Hunter 
315066286ed3SAdrian Hunter 	set_timestamp_boundary(rec, sample->time);
315166286ed3SAdrian Hunter 	return 0;
315266286ed3SAdrian Hunter }
315366286ed3SAdrian Hunter 
315441b740b6SNamhyung Kim static int parse_record_synth_option(const struct option *opt,
315541b740b6SNamhyung Kim 				     const char *str,
315641b740b6SNamhyung Kim 				     int unset __maybe_unused)
315741b740b6SNamhyung Kim {
315841b740b6SNamhyung Kim 	struct record_opts *opts = opt->value;
315941b740b6SNamhyung Kim 	char *p = strdup(str);
316041b740b6SNamhyung Kim 
316141b740b6SNamhyung Kim 	if (p == NULL)
316241b740b6SNamhyung Kim 		return -1;
316341b740b6SNamhyung Kim 
316441b740b6SNamhyung Kim 	opts->synth = parse_synth_opt(p);
316541b740b6SNamhyung Kim 	free(p);
316641b740b6SNamhyung Kim 
316741b740b6SNamhyung Kim 	if (opts->synth < 0) {
316841b740b6SNamhyung Kim 		pr_err("Invalid synth option: %s\n", str);
316941b740b6SNamhyung Kim 		return -1;
317041b740b6SNamhyung Kim 	}
317141b740b6SNamhyung Kim 	return 0;
317241b740b6SNamhyung Kim }
317341b740b6SNamhyung Kim 
3174d20deb64SArnaldo Carvalho de Melo /*
31758c6f45a7SArnaldo Carvalho de Melo  * XXX Ideally would be local to cmd_record() and passed to a record__new
31768c6f45a7SArnaldo Carvalho de Melo  * because we need to have access to it in record__exit, that is called
3177d20deb64SArnaldo Carvalho de Melo  * after cmd_record() exits, but since record_options need to be accessible to
3178d20deb64SArnaldo Carvalho de Melo  * builtin-script, leave it here.
3179d20deb64SArnaldo Carvalho de Melo  *
3180d20deb64SArnaldo Carvalho de Melo  * At least we don't ouch it in all the other functions here directly.
3181d20deb64SArnaldo Carvalho de Melo  *
3182d20deb64SArnaldo Carvalho de Melo  * Just say no to tons of global variables, sigh.
3183d20deb64SArnaldo Carvalho de Melo  */
31848c6f45a7SArnaldo Carvalho de Melo static struct record record = {
3185d20deb64SArnaldo Carvalho de Melo 	.opts = {
31868affc2b8SAndi Kleen 		.sample_time	     = true,
3187d20deb64SArnaldo Carvalho de Melo 		.mmap_pages	     = UINT_MAX,
3188d20deb64SArnaldo Carvalho de Melo 		.user_freq	     = UINT_MAX,
3189d20deb64SArnaldo Carvalho de Melo 		.user_interval	     = ULLONG_MAX,
3190447a6013SArnaldo Carvalho de Melo 		.freq		     = 4000,
3191d1cb9fceSNamhyung Kim 		.target		     = {
3192d1cb9fceSNamhyung Kim 			.uses_mmap   = true,
31933aa5939dSAdrian Hunter 			.default_per_cpu = true,
3194d1cb9fceSNamhyung Kim 		},
3195470530bbSAlexey Budankov 		.mmap_flush          = MMAP_FLUSH_DEFAULT,
3196d99c22eaSStephane Eranian 		.nr_threads_synthesize = 1,
31971d078ccbSAlexey Budankov 		.ctl_fd              = -1,
31981d078ccbSAlexey Budankov 		.ctl_fd_ack          = -1,
319941b740b6SNamhyung Kim 		.synth               = PERF_SYNTH_ALL,
3200d20deb64SArnaldo Carvalho de Melo 	},
3201e3d59112SNamhyung Kim 	.tool = {
3202e3d59112SNamhyung Kim 		.sample		= process_sample_event,
3203e3d59112SNamhyung Kim 		.fork		= perf_event__process_fork,
3204cca8482cSAdrian Hunter 		.exit		= perf_event__process_exit,
3205e3d59112SNamhyung Kim 		.comm		= perf_event__process_comm,
3206f3b3614aSHari Bathini 		.namespaces	= perf_event__process_namespaces,
32076e0a9b3dSArnaldo Carvalho de Melo 		.mmap		= build_id__process_mmap,
32086e0a9b3dSArnaldo Carvalho de Melo 		.mmap2		= build_id__process_mmap2,
320966286ed3SAdrian Hunter 		.itrace_start	= process_timestamp_boundary,
321066286ed3SAdrian Hunter 		.aux		= process_timestamp_boundary,
3211cca8482cSAdrian Hunter 		.ordered_events	= true,
3212e3d59112SNamhyung Kim 	},
3213d20deb64SArnaldo Carvalho de Melo };
32147865e817SFrederic Weisbecker 
321576a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
321676a26549SNamhyung Kim 	"\n\t\t\t\tDefault: fp";
321761eaa3beSArnaldo Carvalho de Melo 
32180aab2136SWang Nan static bool dry_run;
32190aab2136SWang Nan 
3220d20deb64SArnaldo Carvalho de Melo /*
3221d20deb64SArnaldo Carvalho de Melo  * XXX Will stay a global variable till we fix builtin-script.c to stop messing
3222d20deb64SArnaldo Carvalho de Melo  * with it and switch to use the library functions in perf_evlist that came
3223b4006796SArnaldo Carvalho de Melo  * from builtin-record.c, i.e. use record_opts,
32247b392ef0SArnaldo Carvalho de Melo  * evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
3225d20deb64SArnaldo Carvalho de Melo  * using pipes, etc.
3226d20deb64SArnaldo Carvalho de Melo  */
3227efd21307SJiri Olsa static struct option __record_options[] = {
3228d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &record.evlist, "event",
322986470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
3230f120f9d5SJiri Olsa 		     parse_events_option),
3231d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK(0, "filter", &record.evlist, "filter",
3232c171b552SLi Zefan 		     "event filter", parse_filter),
32334ba1faa1SWang Nan 	OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
32344ba1faa1SWang Nan 			   NULL, "don't record events from perf itself",
32354ba1faa1SWang Nan 			   exclude_perf),
3236bea03405SNamhyung Kim 	OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
3237d6d901c2SZhang, Yanmin 		    "record events on existing process id"),
3238bea03405SNamhyung Kim 	OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
3239d6d901c2SZhang, Yanmin 		    "record events on existing thread id"),
3240d20deb64SArnaldo Carvalho de Melo 	OPT_INTEGER('r', "realtime", &record.realtime_prio,
324186470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
3242509051eaSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
3243acac03faSKirill Smelkov 		    "collect data without buffering"),
3244d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
3245daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
3246bea03405SNamhyung Kim 	OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
324786470930SIngo Molnar 			    "system-wide collection from all CPUs"),
3248bea03405SNamhyung Kim 	OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
3249c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
3250d20deb64SArnaldo Carvalho de Melo 	OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
32512d4f2799SJiri Olsa 	OPT_STRING('o', "output", &record.data.path, "file",
325286470930SIngo Molnar 		    "output file name"),
325369e7e5b0SAdrian Hunter 	OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
325469e7e5b0SAdrian Hunter 			&record.opts.no_inherit_set,
32552e6cdf99SStephane Eranian 			"child tasks do not inherit counters"),
32564ea648aeSWang Nan 	OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
32574ea648aeSWang Nan 		    "synthesize non-sample events at the end of output"),
3258626a6b78SWang Nan 	OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
3259a060c1f1SWei Li 	OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"),
3260b09c2364SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq,
3261b09c2364SArnaldo Carvalho de Melo 		    "Fail if the specified frequency can't be used"),
326267230479SArnaldo Carvalho de Melo 	OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'",
326367230479SArnaldo Carvalho de Melo 		     "profile at this frequency",
326467230479SArnaldo Carvalho de Melo 		      record__parse_freq),
3265e9db1310SAdrian Hunter 	OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
3266e9db1310SAdrian Hunter 		     "number of mmap data pages and AUX area tracing mmap pages",
3267e9db1310SAdrian Hunter 		     record__parse_mmap_pages),
3268470530bbSAlexey Budankov 	OPT_CALLBACK(0, "mmap-flush", &record.opts, "number",
3269470530bbSAlexey Budankov 		     "Minimal number of bytes that is extracted from mmap data pages (default: 1)",
3270470530bbSAlexey Budankov 		     record__mmap_flush_parse),
3271d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "group", &record.opts.group,
327243bece79SLin Ming 		    "put the counters into a counter group"),
32732ddd5c04SArnaldo Carvalho de Melo 	OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
327409b0fd45SJiri Olsa 			   NULL, "enables call-graph recording" ,
327509b0fd45SJiri Olsa 			   &record_callchain_opt),
327609b0fd45SJiri Olsa 	OPT_CALLBACK(0, "call-graph", &record.opts,
327776a26549SNamhyung Kim 		     "record_mode[,record_size]", record_callchain_help,
327809b0fd45SJiri Olsa 		     &record_parse_callchain_opt),
3279c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
32803da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
3281b44308f5SArnaldo Carvalho de Melo 	OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
3282d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
3283649c48a9SPeter Zijlstra 		    "per thread counts"),
328456100321SPeter Zijlstra 	OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
32853b0a5daaSKan Liang 	OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr,
32863b0a5daaSKan Liang 		    "Record the sample physical addresses"),
3287542b88fdSKan Liang 	OPT_BOOLEAN(0, "data-page-size", &record.opts.sample_data_page_size,
3288542b88fdSKan Liang 		    "Record the sampled data address data page size"),
3289c1de7f3dSKan Liang 	OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size,
3290c1de7f3dSKan Liang 		    "Record the sampled code address (ip) page size"),
3291b6f35ed7SJiri Olsa 	OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
329261110883SAdrian Hunter 	OPT_BOOLEAN(0, "sample-identifier", &record.opts.sample_identifier,
329361110883SAdrian Hunter 		    "Record the sample identifier"),
32943abebc55SAdrian Hunter 	OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
32953abebc55SAdrian Hunter 			&record.opts.sample_time_set,
32963abebc55SAdrian Hunter 			"Record the sample timestamps"),
3297f290aa1fSJiri Olsa 	OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set,
3298f290aa1fSJiri Olsa 			"Record the sample period"),
3299d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
3300649c48a9SPeter Zijlstra 		    "don't sample"),
3301d2db9a98SWang Nan 	OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
3302d2db9a98SWang Nan 			&record.no_buildid_cache_set,
3303a1ac1d3cSStephane Eranian 			"do not update the buildid cache"),
3304d2db9a98SWang Nan 	OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
3305d2db9a98SWang Nan 			&record.no_buildid_set,
3306baa2f6ceSArnaldo Carvalho de Melo 			"do not collect buildids in perf.data"),
3307d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
3308023695d9SStephane Eranian 		     "monitor event in cgroup name only",
3309023695d9SStephane Eranian 		     parse_cgroups),
331068cd3b45SAlexey Budankov 	OPT_INTEGER('D', "delay", &record.opts.initial_delay,
331168cd3b45SAlexey Budankov 		  "ms to wait before starting measurement after program start (-1: start with events disabled)"),
3312eeb399b5SAdrian Hunter 	OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"),
3313bea03405SNamhyung Kim 	OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
3314bea03405SNamhyung Kim 		   "user to profile"),
3315a5aabdacSStephane Eranian 
3316a5aabdacSStephane Eranian 	OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
3317a5aabdacSStephane Eranian 		     "branch any", "sample any taken branches",
3318a5aabdacSStephane Eranian 		     parse_branch_stack),
3319a5aabdacSStephane Eranian 
3320a5aabdacSStephane Eranian 	OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
3321a5aabdacSStephane Eranian 		     "branch filter mask", "branch stack filter modes",
3322bdfebd84SRoberto Agostino Vitillo 		     parse_branch_stack),
332305484298SAndi Kleen 	OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
332405484298SAndi Kleen 		    "sample by weight (on special events only)"),
3325475eeab9SAndi Kleen 	OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
3326475eeab9SAndi Kleen 		    "sample transaction flags (special events only)"),
33273aa5939dSAdrian Hunter 	OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
33283aa5939dSAdrian Hunter 		    "use per-thread mmaps"),
3329bcc84ec6SStephane Eranian 	OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
3330bcc84ec6SStephane Eranian 		    "sample selected machine registers on interrupt,"
3331aeea9062SKan Liang 		    " use '-I?' to list register names", parse_intr_regs),
333284c41742SAndi Kleen 	OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register",
333384c41742SAndi Kleen 		    "sample selected machine registers on interrupt,"
3334aeea9062SKan Liang 		    " use '--user-regs=?' to list register names", parse_user_regs),
333585c273d2SAndi Kleen 	OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
333685c273d2SAndi Kleen 		    "Record running/enabled time of read (:S) events"),
3337814c8c38SPeter Zijlstra 	OPT_CALLBACK('k', "clockid", &record.opts,
3338814c8c38SPeter Zijlstra 	"clockid", "clockid to use for events, see clock_gettime()",
3339814c8c38SPeter Zijlstra 	parse_clockid),
33402dd6d8a1SAdrian Hunter 	OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
33412dd6d8a1SAdrian Hunter 			  "opts", "AUX area tracing Snapshot Mode", ""),
3342c0a6de06SAdrian Hunter 	OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts,
3343c0a6de06SAdrian Hunter 			  "opts", "sample AUX area", ""),
33443fcb10e4SMark Drayton 	OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
33459d9cad76SKan Liang 			"per thread proc mmap processing timeout in ms"),
3346f3b3614aSHari Bathini 	OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces,
3347f3b3614aSHari Bathini 		    "Record namespaces events"),
33488fb4b679SNamhyung Kim 	OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup,
33498fb4b679SNamhyung Kim 		    "Record cgroup events"),
335016b4b4e1SAdrian Hunter 	OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events,
335116b4b4e1SAdrian Hunter 			&record.opts.record_switch_events_set,
3352b757bb09SAdrian Hunter 			"Record context switch events"),
335385723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
335485723885SJiri Olsa 			 "Configure all used events to run in kernel space.",
335585723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
335685723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
335785723885SJiri Olsa 			 "Configure all used events to run in user space.",
335885723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
335953651b28Syuzhoujian 	OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains,
336053651b28Syuzhoujian 		    "collect kernel callchains"),
336153651b28Syuzhoujian 	OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains,
336253651b28Syuzhoujian 		    "collect user callchains"),
336371dc2326SWang Nan 	OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
336471dc2326SWang Nan 		   "clang binary to use for compiling BPF scriptlets"),
336571dc2326SWang Nan 	OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
336671dc2326SWang Nan 		   "options passed to clang when compiling BPF scriptlets"),
33677efe0e03SHe Kuang 	OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
33687efe0e03SHe Kuang 		   "file", "vmlinux pathname"),
33696156681bSNamhyung Kim 	OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
33706156681bSNamhyung Kim 		    "Record build-id of all DSOs regardless of hits"),
3371e29386c8SJiri Olsa 	OPT_BOOLEAN(0, "buildid-mmap", &record.buildid_mmap,
3372e29386c8SJiri Olsa 		    "Record build-id in map events"),
3373ecfd7a9cSWang Nan 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
3374ecfd7a9cSWang Nan 		    "append timestamp to output filename"),
337568588bafSJin Yao 	OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary,
337668588bafSJin Yao 		    "Record timestamp boundary (time of first/last samples)"),
3377cb4e1ebbSJiri Olsa 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
3378c38dab7dSAndi Kleen 			  &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
3379c38dab7dSAndi Kleen 			  "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
3380dc0c6127SJiri Olsa 			  "signal"),
3381899e5ffbSArnaldo Carvalho de Melo 	OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event",
3382899e5ffbSArnaldo Carvalho de Melo 			 "switch output event selector. use 'perf list' to list available events",
3383899e5ffbSArnaldo Carvalho de Melo 			 parse_events_option_new_evlist),
338403724b2eSAndi Kleen 	OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
338503724b2eSAndi Kleen 		   "Limit number of switch output generated files"),
33860aab2136SWang Nan 	OPT_BOOLEAN(0, "dry-run", &dry_run,
33870aab2136SWang Nan 		    "Parse options then exit"),
3388d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
338993f20c0fSAlexey Budankov 	OPT_CALLBACK_OPTARG(0, "aio", &record.opts,
339093f20c0fSAlexey Budankov 		     &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
3391d3d1af6fSAlexey Budankov 		     record__aio_parse),
3392d3d1af6fSAlexey Budankov #endif
3393f4fe11b7SAlexey Budankov 	OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
3394f4fe11b7SAlexey Budankov 		     "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
3395f4fe11b7SAlexey Budankov 		     record__parse_affinity),
3396504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
3397b5f2511dSAlexey Bayduraev 	OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, "n",
3398b5f2511dSAlexey Bayduraev 			    "Compress records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
3399504c1ad1SAlexey Budankov 			    record__parse_comp_level),
3400504c1ad1SAlexey Budankov #endif
34016d575816SJiwei Sun 	OPT_CALLBACK(0, "max-size", &record.output_max_size,
34026d575816SJiwei Sun 		     "size", "Limit the maximum size of the output file", parse_output_max_size),
3403d99c22eaSStephane Eranian 	OPT_UINTEGER(0, "num-thread-synthesize",
3404d99c22eaSStephane Eranian 		     &record.opts.nr_threads_synthesize,
3405d99c22eaSStephane Eranian 		     "number of threads to run for event synthesis"),
340670943490SStephane Eranian #ifdef HAVE_LIBPFM
340770943490SStephane Eranian 	OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
340870943490SStephane Eranian 		"libpfm4 event selector. use 'perf list' to list available events",
340970943490SStephane Eranian 		parse_libpfm_events_option),
341070943490SStephane Eranian #endif
3411a8fcbd26SAdrian Hunter 	OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
3412d20aff15SAdrian Hunter 		     "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n"
3413d20aff15SAdrian Hunter 		     "\t\t\t  'snapshot': AUX area tracing snapshot).\n"
3414a8fcbd26SAdrian Hunter 		     "\t\t\t  Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
3415a8fcbd26SAdrian Hunter 		     "\t\t\t  Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
34161d078ccbSAlexey Budankov 		      parse_control_option),
341741b740b6SNamhyung Kim 	OPT_CALLBACK(0, "synth", &record.opts, "no|all|task|mmap|cgroup",
341841b740b6SNamhyung Kim 		     "Fine-tune event synthesis: default=all", parse_record_synth_option),
34199bce13eaSJiri Olsa 	OPT_STRING_OPTARG_SET(0, "debuginfod", &record.debuginfod.urls,
34209bce13eaSJiri Olsa 			  &record.debuginfod.set, "debuginfod urls",
34219bce13eaSJiri Olsa 			  "Enable debuginfod data retrieval from DEBUGINFOD_URLS or specified urls",
34229bce13eaSJiri Olsa 			  "system"),
342306380a84SAlexey Bayduraev 	OPT_CALLBACK_OPTARG(0, "threads", &record.opts, NULL, "spec",
342406380a84SAlexey Bayduraev 			    "write collected trace data into several data files using parallel threads",
342506380a84SAlexey Bayduraev 			    record__parse_threads),
3426edc41a10SNamhyung Kim 	OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"),
342786470930SIngo Molnar 	OPT_END()
342886470930SIngo Molnar };
342986470930SIngo Molnar 
3430e5b2c207SNamhyung Kim struct option *record_options = __record_options;
3431e5b2c207SNamhyung Kim 
3432cbd7bfc7SAthira Rajeev static int record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpu_map *cpus)
34337954f716SAlexey Bayduraev {
34340255571aSIan Rogers 	struct perf_cpu cpu;
34350255571aSIan Rogers 	int idx;
34367954f716SAlexey Bayduraev 
343723380e4dSAlexey Bayduraev 	if (cpu_map__is_dummy(cpus))
3438cbd7bfc7SAthira Rajeev 		return 0;
343923380e4dSAlexey Bayduraev 
3440cbd7bfc7SAthira Rajeev 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
3441ca76d7d2SAdrian Hunter 		if (cpu.cpu == -1)
3442ca76d7d2SAdrian Hunter 			continue;
3443cbd7bfc7SAthira Rajeev 		/* Return ENODEV is input cpu is greater than max cpu */
3444cbd7bfc7SAthira Rajeev 		if ((unsigned long)cpu.cpu > mask->nbits)
3445cbd7bfc7SAthira Rajeev 			return -ENODEV;
34460255571aSIan Rogers 		set_bit(cpu.cpu, mask->bits);
34477954f716SAlexey Bayduraev 	}
34487954f716SAlexey Bayduraev 
3449cbd7bfc7SAthira Rajeev 	return 0;
3450cbd7bfc7SAthira Rajeev }
3451cbd7bfc7SAthira Rajeev 
3452f466e5edSAlexey Bayduraev static int record__mmap_cpu_mask_init_spec(struct mmap_cpu_mask *mask, const char *mask_spec)
3453f466e5edSAlexey Bayduraev {
3454f466e5edSAlexey Bayduraev 	struct perf_cpu_map *cpus;
3455f466e5edSAlexey Bayduraev 
3456f466e5edSAlexey Bayduraev 	cpus = perf_cpu_map__new(mask_spec);
3457f466e5edSAlexey Bayduraev 	if (!cpus)
3458f466e5edSAlexey Bayduraev 		return -ENOMEM;
3459f466e5edSAlexey Bayduraev 
3460f466e5edSAlexey Bayduraev 	bitmap_zero(mask->bits, mask->nbits);
3461cbd7bfc7SAthira Rajeev 	if (record__mmap_cpu_mask_init(mask, cpus))
3462cbd7bfc7SAthira Rajeev 		return -ENODEV;
3463cbd7bfc7SAthira Rajeev 
3464f466e5edSAlexey Bayduraev 	perf_cpu_map__put(cpus);
3465f466e5edSAlexey Bayduraev 
3466f466e5edSAlexey Bayduraev 	return 0;
3467f466e5edSAlexey Bayduraev }
3468f466e5edSAlexey Bayduraev 
34697954f716SAlexey Bayduraev static void record__free_thread_masks(struct record *rec, int nr_threads)
34707954f716SAlexey Bayduraev {
34717954f716SAlexey Bayduraev 	int t;
34727954f716SAlexey Bayduraev 
34737954f716SAlexey Bayduraev 	if (rec->thread_masks)
34747954f716SAlexey Bayduraev 		for (t = 0; t < nr_threads; t++)
34757954f716SAlexey Bayduraev 			record__thread_mask_free(&rec->thread_masks[t]);
34767954f716SAlexey Bayduraev 
34777954f716SAlexey Bayduraev 	zfree(&rec->thread_masks);
34787954f716SAlexey Bayduraev }
34797954f716SAlexey Bayduraev 
34807954f716SAlexey Bayduraev static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr_bits)
34817954f716SAlexey Bayduraev {
34827954f716SAlexey Bayduraev 	int t, ret;
34837954f716SAlexey Bayduraev 
34847954f716SAlexey Bayduraev 	rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks)));
34857954f716SAlexey Bayduraev 	if (!rec->thread_masks) {
34867954f716SAlexey Bayduraev 		pr_err("Failed to allocate thread masks\n");
34877954f716SAlexey Bayduraev 		return -ENOMEM;
34887954f716SAlexey Bayduraev 	}
34897954f716SAlexey Bayduraev 
34907954f716SAlexey Bayduraev 	for (t = 0; t < nr_threads; t++) {
34917954f716SAlexey Bayduraev 		ret = record__thread_mask_alloc(&rec->thread_masks[t], nr_bits);
34927954f716SAlexey Bayduraev 		if (ret) {
34937954f716SAlexey Bayduraev 			pr_err("Failed to allocate thread masks[%d]\n", t);
34947954f716SAlexey Bayduraev 			goto out_free;
34957954f716SAlexey Bayduraev 		}
34967954f716SAlexey Bayduraev 	}
34977954f716SAlexey Bayduraev 
34987954f716SAlexey Bayduraev 	return 0;
34997954f716SAlexey Bayduraev 
35007954f716SAlexey Bayduraev out_free:
35017954f716SAlexey Bayduraev 	record__free_thread_masks(rec, nr_threads);
35027954f716SAlexey Bayduraev 
35037954f716SAlexey Bayduraev 	return ret;
35047954f716SAlexey Bayduraev }
35057954f716SAlexey Bayduraev 
350606380a84SAlexey Bayduraev static int record__init_thread_cpu_masks(struct record *rec, struct perf_cpu_map *cpus)
350706380a84SAlexey Bayduraev {
350806380a84SAlexey Bayduraev 	int t, ret, nr_cpus = perf_cpu_map__nr(cpus);
350906380a84SAlexey Bayduraev 
351006380a84SAlexey Bayduraev 	ret = record__alloc_thread_masks(rec, nr_cpus, cpu__max_cpu().cpu);
351106380a84SAlexey Bayduraev 	if (ret)
351206380a84SAlexey Bayduraev 		return ret;
351306380a84SAlexey Bayduraev 
351406380a84SAlexey Bayduraev 	rec->nr_threads = nr_cpus;
351506380a84SAlexey Bayduraev 	pr_debug("nr_threads: %d\n", rec->nr_threads);
351606380a84SAlexey Bayduraev 
351706380a84SAlexey Bayduraev 	for (t = 0; t < rec->nr_threads; t++) {
35180255571aSIan Rogers 		set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].maps.bits);
35190255571aSIan Rogers 		set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].affinity.bits);
352006380a84SAlexey Bayduraev 		if (verbose) {
352106380a84SAlexey Bayduraev 			pr_debug("thread_masks[%d]: ", t);
352206380a84SAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps");
352306380a84SAlexey Bayduraev 			pr_debug("thread_masks[%d]: ", t);
352406380a84SAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity");
352506380a84SAlexey Bayduraev 		}
352606380a84SAlexey Bayduraev 	}
352706380a84SAlexey Bayduraev 
352806380a84SAlexey Bayduraev 	return 0;
352906380a84SAlexey Bayduraev }
353006380a84SAlexey Bayduraev 
3531f466e5edSAlexey Bayduraev static int record__init_thread_masks_spec(struct record *rec, struct perf_cpu_map *cpus,
3532f466e5edSAlexey Bayduraev 					  const char **maps_spec, const char **affinity_spec,
3533f466e5edSAlexey Bayduraev 					  u32 nr_spec)
3534f466e5edSAlexey Bayduraev {
3535f466e5edSAlexey Bayduraev 	u32 s;
3536f466e5edSAlexey Bayduraev 	int ret = 0, t = 0;
3537f466e5edSAlexey Bayduraev 	struct mmap_cpu_mask cpus_mask;
3538f466e5edSAlexey Bayduraev 	struct thread_mask thread_mask, full_mask, *thread_masks;
3539f466e5edSAlexey Bayduraev 
3540f466e5edSAlexey Bayduraev 	ret = record__mmap_cpu_mask_alloc(&cpus_mask, cpu__max_cpu().cpu);
3541f466e5edSAlexey Bayduraev 	if (ret) {
3542f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate CPUs mask\n");
3543f466e5edSAlexey Bayduraev 		return ret;
3544f466e5edSAlexey Bayduraev 	}
3545cbd7bfc7SAthira Rajeev 
3546cbd7bfc7SAthira Rajeev 	ret = record__mmap_cpu_mask_init(&cpus_mask, cpus);
3547cbd7bfc7SAthira Rajeev 	if (ret) {
3548cbd7bfc7SAthira Rajeev 		pr_err("Failed to init cpu mask\n");
3549cbd7bfc7SAthira Rajeev 		goto out_free_cpu_mask;
3550cbd7bfc7SAthira Rajeev 	}
3551f466e5edSAlexey Bayduraev 
3552f466e5edSAlexey Bayduraev 	ret = record__thread_mask_alloc(&full_mask, cpu__max_cpu().cpu);
3553f466e5edSAlexey Bayduraev 	if (ret) {
3554f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate full mask\n");
3555f466e5edSAlexey Bayduraev 		goto out_free_cpu_mask;
3556f466e5edSAlexey Bayduraev 	}
3557f466e5edSAlexey Bayduraev 
3558f466e5edSAlexey Bayduraev 	ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu);
3559f466e5edSAlexey Bayduraev 	if (ret) {
3560f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate thread mask\n");
3561f466e5edSAlexey Bayduraev 		goto out_free_full_and_cpu_masks;
3562f466e5edSAlexey Bayduraev 	}
3563f466e5edSAlexey Bayduraev 
3564f466e5edSAlexey Bayduraev 	for (s = 0; s < nr_spec; s++) {
3565f466e5edSAlexey Bayduraev 		ret = record__mmap_cpu_mask_init_spec(&thread_mask.maps, maps_spec[s]);
3566f466e5edSAlexey Bayduraev 		if (ret) {
3567f466e5edSAlexey Bayduraev 			pr_err("Failed to initialize maps thread mask\n");
3568f466e5edSAlexey Bayduraev 			goto out_free;
3569f466e5edSAlexey Bayduraev 		}
3570f466e5edSAlexey Bayduraev 		ret = record__mmap_cpu_mask_init_spec(&thread_mask.affinity, affinity_spec[s]);
3571f466e5edSAlexey Bayduraev 		if (ret) {
3572f466e5edSAlexey Bayduraev 			pr_err("Failed to initialize affinity thread mask\n");
3573f466e5edSAlexey Bayduraev 			goto out_free;
3574f466e5edSAlexey Bayduraev 		}
3575f466e5edSAlexey Bayduraev 
3576f466e5edSAlexey Bayduraev 		/* ignore invalid CPUs but do not allow empty masks */
3577f466e5edSAlexey Bayduraev 		if (!bitmap_and(thread_mask.maps.bits, thread_mask.maps.bits,
3578f466e5edSAlexey Bayduraev 				cpus_mask.bits, thread_mask.maps.nbits)) {
3579f466e5edSAlexey Bayduraev 			pr_err("Empty maps mask: %s\n", maps_spec[s]);
3580f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3581f466e5edSAlexey Bayduraev 			goto out_free;
3582f466e5edSAlexey Bayduraev 		}
3583f466e5edSAlexey Bayduraev 		if (!bitmap_and(thread_mask.affinity.bits, thread_mask.affinity.bits,
3584f466e5edSAlexey Bayduraev 				cpus_mask.bits, thread_mask.affinity.nbits)) {
3585f466e5edSAlexey Bayduraev 			pr_err("Empty affinity mask: %s\n", affinity_spec[s]);
3586f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3587f466e5edSAlexey Bayduraev 			goto out_free;
3588f466e5edSAlexey Bayduraev 		}
3589f466e5edSAlexey Bayduraev 
3590f466e5edSAlexey Bayduraev 		/* do not allow intersection with other masks (full_mask) */
3591f466e5edSAlexey Bayduraev 		if (bitmap_intersects(thread_mask.maps.bits, full_mask.maps.bits,
3592f466e5edSAlexey Bayduraev 				      thread_mask.maps.nbits)) {
3593f466e5edSAlexey Bayduraev 			pr_err("Intersecting maps mask: %s\n", maps_spec[s]);
3594f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3595f466e5edSAlexey Bayduraev 			goto out_free;
3596f466e5edSAlexey Bayduraev 		}
3597f466e5edSAlexey Bayduraev 		if (bitmap_intersects(thread_mask.affinity.bits, full_mask.affinity.bits,
3598f466e5edSAlexey Bayduraev 				      thread_mask.affinity.nbits)) {
3599f466e5edSAlexey Bayduraev 			pr_err("Intersecting affinity mask: %s\n", affinity_spec[s]);
3600f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3601f466e5edSAlexey Bayduraev 			goto out_free;
3602f466e5edSAlexey Bayduraev 		}
3603f466e5edSAlexey Bayduraev 
3604f466e5edSAlexey Bayduraev 		bitmap_or(full_mask.maps.bits, full_mask.maps.bits,
3605f466e5edSAlexey Bayduraev 			  thread_mask.maps.bits, full_mask.maps.nbits);
3606f466e5edSAlexey Bayduraev 		bitmap_or(full_mask.affinity.bits, full_mask.affinity.bits,
3607f466e5edSAlexey Bayduraev 			  thread_mask.affinity.bits, full_mask.maps.nbits);
3608f466e5edSAlexey Bayduraev 
3609f466e5edSAlexey Bayduraev 		thread_masks = realloc(rec->thread_masks, (t + 1) * sizeof(struct thread_mask));
3610f466e5edSAlexey Bayduraev 		if (!thread_masks) {
3611f466e5edSAlexey Bayduraev 			pr_err("Failed to reallocate thread masks\n");
3612f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3613f466e5edSAlexey Bayduraev 			goto out_free;
3614f466e5edSAlexey Bayduraev 		}
3615f466e5edSAlexey Bayduraev 		rec->thread_masks = thread_masks;
3616f466e5edSAlexey Bayduraev 		rec->thread_masks[t] = thread_mask;
3617f466e5edSAlexey Bayduraev 		if (verbose) {
3618f466e5edSAlexey Bayduraev 			pr_debug("thread_masks[%d]: ", t);
3619f466e5edSAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps");
3620f466e5edSAlexey Bayduraev 			pr_debug("thread_masks[%d]: ", t);
3621f466e5edSAlexey Bayduraev 			mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity");
3622f466e5edSAlexey Bayduraev 		}
3623f466e5edSAlexey Bayduraev 		t++;
3624f466e5edSAlexey Bayduraev 		ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu);
3625f466e5edSAlexey Bayduraev 		if (ret) {
3626f466e5edSAlexey Bayduraev 			pr_err("Failed to allocate thread mask\n");
3627f466e5edSAlexey Bayduraev 			goto out_free_full_and_cpu_masks;
3628f466e5edSAlexey Bayduraev 		}
3629f466e5edSAlexey Bayduraev 	}
3630f466e5edSAlexey Bayduraev 	rec->nr_threads = t;
3631f466e5edSAlexey Bayduraev 	pr_debug("nr_threads: %d\n", rec->nr_threads);
3632f466e5edSAlexey Bayduraev 	if (!rec->nr_threads)
3633f466e5edSAlexey Bayduraev 		ret = -EINVAL;
3634f466e5edSAlexey Bayduraev 
3635f466e5edSAlexey Bayduraev out_free:
3636f466e5edSAlexey Bayduraev 	record__thread_mask_free(&thread_mask);
3637f466e5edSAlexey Bayduraev out_free_full_and_cpu_masks:
3638f466e5edSAlexey Bayduraev 	record__thread_mask_free(&full_mask);
3639f466e5edSAlexey Bayduraev out_free_cpu_mask:
3640f466e5edSAlexey Bayduraev 	record__mmap_cpu_mask_free(&cpus_mask);
3641f466e5edSAlexey Bayduraev 
3642f466e5edSAlexey Bayduraev 	return ret;
3643f466e5edSAlexey Bayduraev }
3644f466e5edSAlexey Bayduraev 
3645f466e5edSAlexey Bayduraev static int record__init_thread_core_masks(struct record *rec, struct perf_cpu_map *cpus)
3646f466e5edSAlexey Bayduraev {
3647f466e5edSAlexey Bayduraev 	int ret;
3648f466e5edSAlexey Bayduraev 	struct cpu_topology *topo;
3649f466e5edSAlexey Bayduraev 
3650f466e5edSAlexey Bayduraev 	topo = cpu_topology__new();
3651f466e5edSAlexey Bayduraev 	if (!topo) {
3652f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate CPU topology\n");
3653f466e5edSAlexey Bayduraev 		return -ENOMEM;
3654f466e5edSAlexey Bayduraev 	}
3655f466e5edSAlexey Bayduraev 
3656f466e5edSAlexey Bayduraev 	ret = record__init_thread_masks_spec(rec, cpus, topo->core_cpus_list,
3657f466e5edSAlexey Bayduraev 					     topo->core_cpus_list, topo->core_cpus_lists);
3658f466e5edSAlexey Bayduraev 	cpu_topology__delete(topo);
3659f466e5edSAlexey Bayduraev 
3660f466e5edSAlexey Bayduraev 	return ret;
3661f466e5edSAlexey Bayduraev }
3662f466e5edSAlexey Bayduraev 
3663f466e5edSAlexey Bayduraev static int record__init_thread_package_masks(struct record *rec, struct perf_cpu_map *cpus)
3664f466e5edSAlexey Bayduraev {
3665f466e5edSAlexey Bayduraev 	int ret;
3666f466e5edSAlexey Bayduraev 	struct cpu_topology *topo;
3667f466e5edSAlexey Bayduraev 
3668f466e5edSAlexey Bayduraev 	topo = cpu_topology__new();
3669f466e5edSAlexey Bayduraev 	if (!topo) {
3670f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate CPU topology\n");
3671f466e5edSAlexey Bayduraev 		return -ENOMEM;
3672f466e5edSAlexey Bayduraev 	}
3673f466e5edSAlexey Bayduraev 
3674f466e5edSAlexey Bayduraev 	ret = record__init_thread_masks_spec(rec, cpus, topo->package_cpus_list,
3675f466e5edSAlexey Bayduraev 					     topo->package_cpus_list, topo->package_cpus_lists);
3676f466e5edSAlexey Bayduraev 	cpu_topology__delete(topo);
3677f466e5edSAlexey Bayduraev 
3678f466e5edSAlexey Bayduraev 	return ret;
3679f466e5edSAlexey Bayduraev }
3680f466e5edSAlexey Bayduraev 
3681f466e5edSAlexey Bayduraev static int record__init_thread_numa_masks(struct record *rec, struct perf_cpu_map *cpus)
3682f466e5edSAlexey Bayduraev {
3683f466e5edSAlexey Bayduraev 	u32 s;
3684f466e5edSAlexey Bayduraev 	int ret;
3685f466e5edSAlexey Bayduraev 	const char **spec;
3686f466e5edSAlexey Bayduraev 	struct numa_topology *topo;
3687f466e5edSAlexey Bayduraev 
3688f466e5edSAlexey Bayduraev 	topo = numa_topology__new();
3689f466e5edSAlexey Bayduraev 	if (!topo) {
3690f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate NUMA topology\n");
3691f466e5edSAlexey Bayduraev 		return -ENOMEM;
3692f466e5edSAlexey Bayduraev 	}
3693f466e5edSAlexey Bayduraev 
3694f466e5edSAlexey Bayduraev 	spec = zalloc(topo->nr * sizeof(char *));
3695f466e5edSAlexey Bayduraev 	if (!spec) {
3696f466e5edSAlexey Bayduraev 		pr_err("Failed to allocate NUMA spec\n");
3697f466e5edSAlexey Bayduraev 		ret = -ENOMEM;
3698f466e5edSAlexey Bayduraev 		goto out_delete_topo;
3699f466e5edSAlexey Bayduraev 	}
3700f466e5edSAlexey Bayduraev 	for (s = 0; s < topo->nr; s++)
3701f466e5edSAlexey Bayduraev 		spec[s] = topo->nodes[s].cpus;
3702f466e5edSAlexey Bayduraev 
3703f466e5edSAlexey Bayduraev 	ret = record__init_thread_masks_spec(rec, cpus, spec, spec, topo->nr);
3704f466e5edSAlexey Bayduraev 
3705f466e5edSAlexey Bayduraev 	zfree(&spec);
3706f466e5edSAlexey Bayduraev 
3707f466e5edSAlexey Bayduraev out_delete_topo:
3708f466e5edSAlexey Bayduraev 	numa_topology__delete(topo);
3709f466e5edSAlexey Bayduraev 
3710f466e5edSAlexey Bayduraev 	return ret;
3711f466e5edSAlexey Bayduraev }
3712f466e5edSAlexey Bayduraev 
3713f466e5edSAlexey Bayduraev static int record__init_thread_user_masks(struct record *rec, struct perf_cpu_map *cpus)
3714f466e5edSAlexey Bayduraev {
3715f466e5edSAlexey Bayduraev 	int t, ret;
3716f466e5edSAlexey Bayduraev 	u32 s, nr_spec = 0;
3717f466e5edSAlexey Bayduraev 	char **maps_spec = NULL, **affinity_spec = NULL, **tmp_spec;
3718f466e5edSAlexey Bayduraev 	char *user_spec, *spec, *spec_ptr, *mask, *mask_ptr, *dup_mask = NULL;
3719f466e5edSAlexey Bayduraev 
3720f466e5edSAlexey Bayduraev 	for (t = 0, user_spec = (char *)rec->opts.threads_user_spec; ; t++, user_spec = NULL) {
3721f466e5edSAlexey Bayduraev 		spec = strtok_r(user_spec, ":", &spec_ptr);
3722f466e5edSAlexey Bayduraev 		if (spec == NULL)
3723f466e5edSAlexey Bayduraev 			break;
3724f466e5edSAlexey Bayduraev 		pr_debug2("threads_spec[%d]: %s\n", t, spec);
3725f466e5edSAlexey Bayduraev 		mask = strtok_r(spec, "/", &mask_ptr);
3726f466e5edSAlexey Bayduraev 		if (mask == NULL)
3727f466e5edSAlexey Bayduraev 			break;
3728f466e5edSAlexey Bayduraev 		pr_debug2("  maps mask: %s\n", mask);
3729f466e5edSAlexey Bayduraev 		tmp_spec = realloc(maps_spec, (nr_spec + 1) * sizeof(char *));
3730f466e5edSAlexey Bayduraev 		if (!tmp_spec) {
3731f466e5edSAlexey Bayduraev 			pr_err("Failed to reallocate maps spec\n");
3732f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3733f466e5edSAlexey Bayduraev 			goto out_free;
3734f466e5edSAlexey Bayduraev 		}
3735f466e5edSAlexey Bayduraev 		maps_spec = tmp_spec;
3736f466e5edSAlexey Bayduraev 		maps_spec[nr_spec] = dup_mask = strdup(mask);
3737f466e5edSAlexey Bayduraev 		if (!maps_spec[nr_spec]) {
3738f466e5edSAlexey Bayduraev 			pr_err("Failed to allocate maps spec[%d]\n", nr_spec);
3739f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3740f466e5edSAlexey Bayduraev 			goto out_free;
3741f466e5edSAlexey Bayduraev 		}
3742f466e5edSAlexey Bayduraev 		mask = strtok_r(NULL, "/", &mask_ptr);
3743f466e5edSAlexey Bayduraev 		if (mask == NULL) {
3744f466e5edSAlexey Bayduraev 			pr_err("Invalid thread maps or affinity specs\n");
3745f466e5edSAlexey Bayduraev 			ret = -EINVAL;
3746f466e5edSAlexey Bayduraev 			goto out_free;
3747f466e5edSAlexey Bayduraev 		}
3748f466e5edSAlexey Bayduraev 		pr_debug2("  affinity mask: %s\n", mask);
3749f466e5edSAlexey Bayduraev 		tmp_spec = realloc(affinity_spec, (nr_spec + 1) * sizeof(char *));
3750f466e5edSAlexey Bayduraev 		if (!tmp_spec) {
3751f466e5edSAlexey Bayduraev 			pr_err("Failed to reallocate affinity spec\n");
3752f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3753f466e5edSAlexey Bayduraev 			goto out_free;
3754f466e5edSAlexey Bayduraev 		}
3755f466e5edSAlexey Bayduraev 		affinity_spec = tmp_spec;
3756f466e5edSAlexey Bayduraev 		affinity_spec[nr_spec] = strdup(mask);
3757f466e5edSAlexey Bayduraev 		if (!affinity_spec[nr_spec]) {
3758f466e5edSAlexey Bayduraev 			pr_err("Failed to allocate affinity spec[%d]\n", nr_spec);
3759f466e5edSAlexey Bayduraev 			ret = -ENOMEM;
3760f466e5edSAlexey Bayduraev 			goto out_free;
3761f466e5edSAlexey Bayduraev 		}
3762f466e5edSAlexey Bayduraev 		dup_mask = NULL;
3763f466e5edSAlexey Bayduraev 		nr_spec++;
3764f466e5edSAlexey Bayduraev 	}
3765f466e5edSAlexey Bayduraev 
3766f466e5edSAlexey Bayduraev 	ret = record__init_thread_masks_spec(rec, cpus, (const char **)maps_spec,
3767f466e5edSAlexey Bayduraev 					     (const char **)affinity_spec, nr_spec);
3768f466e5edSAlexey Bayduraev 
3769f466e5edSAlexey Bayduraev out_free:
3770f466e5edSAlexey Bayduraev 	free(dup_mask);
3771f466e5edSAlexey Bayduraev 	for (s = 0; s < nr_spec; s++) {
3772f466e5edSAlexey Bayduraev 		if (maps_spec)
3773f466e5edSAlexey Bayduraev 			free(maps_spec[s]);
3774f466e5edSAlexey Bayduraev 		if (affinity_spec)
3775f466e5edSAlexey Bayduraev 			free(affinity_spec[s]);
3776f466e5edSAlexey Bayduraev 	}
3777f466e5edSAlexey Bayduraev 	free(affinity_spec);
3778f466e5edSAlexey Bayduraev 	free(maps_spec);
3779f466e5edSAlexey Bayduraev 
3780f466e5edSAlexey Bayduraev 	return ret;
3781f466e5edSAlexey Bayduraev }
3782f466e5edSAlexey Bayduraev 
37837954f716SAlexey Bayduraev static int record__init_thread_default_masks(struct record *rec, struct perf_cpu_map *cpus)
37847954f716SAlexey Bayduraev {
37857954f716SAlexey Bayduraev 	int ret;
37867954f716SAlexey Bayduraev 
37877954f716SAlexey Bayduraev 	ret = record__alloc_thread_masks(rec, 1, cpu__max_cpu().cpu);
37887954f716SAlexey Bayduraev 	if (ret)
37897954f716SAlexey Bayduraev 		return ret;
37907954f716SAlexey Bayduraev 
3791cbd7bfc7SAthira Rajeev 	if (record__mmap_cpu_mask_init(&rec->thread_masks->maps, cpus))
3792cbd7bfc7SAthira Rajeev 		return -ENODEV;
37937954f716SAlexey Bayduraev 
37947954f716SAlexey Bayduraev 	rec->nr_threads = 1;
37957954f716SAlexey Bayduraev 
37967954f716SAlexey Bayduraev 	return 0;
37977954f716SAlexey Bayduraev }
37987954f716SAlexey Bayduraev 
37997954f716SAlexey Bayduraev static int record__init_thread_masks(struct record *rec)
38007954f716SAlexey Bayduraev {
3801f466e5edSAlexey Bayduraev 	int ret = 0;
38027be1feddSAdrian Hunter 	struct perf_cpu_map *cpus = rec->evlist->core.all_cpus;
38037954f716SAlexey Bayduraev 
380406380a84SAlexey Bayduraev 	if (!record__threads_enabled(rec))
38057954f716SAlexey Bayduraev 		return record__init_thread_default_masks(rec, cpus);
380606380a84SAlexey Bayduraev 
38077be1feddSAdrian Hunter 	if (evlist__per_thread(rec->evlist)) {
380823380e4dSAlexey Bayduraev 		pr_err("--per-thread option is mutually exclusive to parallel streaming mode.\n");
380923380e4dSAlexey Bayduraev 		return -EINVAL;
381023380e4dSAlexey Bayduraev 	}
381123380e4dSAlexey Bayduraev 
3812f466e5edSAlexey Bayduraev 	switch (rec->opts.threads_spec) {
3813f466e5edSAlexey Bayduraev 	case THREAD_SPEC__CPU:
3814f466e5edSAlexey Bayduraev 		ret = record__init_thread_cpu_masks(rec, cpus);
3815f466e5edSAlexey Bayduraev 		break;
3816f466e5edSAlexey Bayduraev 	case THREAD_SPEC__CORE:
3817f466e5edSAlexey Bayduraev 		ret = record__init_thread_core_masks(rec, cpus);
3818f466e5edSAlexey Bayduraev 		break;
3819f466e5edSAlexey Bayduraev 	case THREAD_SPEC__PACKAGE:
3820f466e5edSAlexey Bayduraev 		ret = record__init_thread_package_masks(rec, cpus);
3821f466e5edSAlexey Bayduraev 		break;
3822f466e5edSAlexey Bayduraev 	case THREAD_SPEC__NUMA:
3823f466e5edSAlexey Bayduraev 		ret = record__init_thread_numa_masks(rec, cpus);
3824f466e5edSAlexey Bayduraev 		break;
3825f466e5edSAlexey Bayduraev 	case THREAD_SPEC__USER:
3826f466e5edSAlexey Bayduraev 		ret = record__init_thread_user_masks(rec, cpus);
3827f466e5edSAlexey Bayduraev 		break;
3828f466e5edSAlexey Bayduraev 	default:
3829f466e5edSAlexey Bayduraev 		break;
3830f466e5edSAlexey Bayduraev 	}
3831f466e5edSAlexey Bayduraev 
3832f466e5edSAlexey Bayduraev 	return ret;
38337954f716SAlexey Bayduraev }
38347954f716SAlexey Bayduraev 
3835b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv)
383686470930SIngo Molnar {
3837ef149c25SAdrian Hunter 	int err;
38388c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = &record;
383916ad2ffbSNamhyung Kim 	char errbuf[BUFSIZ];
384086470930SIngo Molnar 
384167230479SArnaldo Carvalho de Melo 	setlocale(LC_ALL, "");
384267230479SArnaldo Carvalho de Melo 
384348e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT
384448e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
384548e1cab1SWang Nan 	set_nobuild('\0', "clang-path", true);
384648e1cab1SWang Nan 	set_nobuild('\0', "clang-opt", true);
384748e1cab1SWang Nan # undef set_nobuild
384848e1cab1SWang Nan #endif
384948e1cab1SWang Nan 
38507efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE
38517efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT)
38527efe0e03SHe Kuang #  define REASON  "NO_DWARF=1"
38537efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT)
38547efe0e03SHe Kuang #  define REASON  "NO_LIBBPF=1"
38557efe0e03SHe Kuang # else
38567efe0e03SHe Kuang #  define REASON  "this architecture doesn't support BPF prologue"
38577efe0e03SHe Kuang # endif
38587efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
38597efe0e03SHe Kuang 	set_nobuild('\0', "vmlinux", true);
38607efe0e03SHe Kuang # undef set_nobuild
38617efe0e03SHe Kuang # undef REASON
38627efe0e03SHe Kuang #endif
38637efe0e03SHe Kuang 
3864edc41a10SNamhyung Kim #ifndef HAVE_BPF_SKEL
3865edc41a10SNamhyung Kim # define set_nobuild(s, l, m, c) set_option_nobuild(record_options, s, l, m, c)
3866edc41a10SNamhyung Kim 	set_nobuild('\0', "off-cpu", "no BUILD_BPF_SKEL=1", true);
3867edc41a10SNamhyung Kim # undef set_nobuild
3868edc41a10SNamhyung Kim #endif
3869edc41a10SNamhyung Kim 
38709d2ed645SAlexey Budankov 	rec->opts.affinity = PERF_AFFINITY_SYS;
38719d2ed645SAlexey Budankov 
38720f98b11cSJiri Olsa 	rec->evlist = evlist__new();
38733e2be2daSArnaldo Carvalho de Melo 	if (rec->evlist == NULL)
3874361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
3875361c99a6SArnaldo Carvalho de Melo 
3876ecc4c561SArnaldo Carvalho de Melo 	err = perf_config(perf_record_config, rec);
3877ecc4c561SArnaldo Carvalho de Melo 	if (err)
3878ecc4c561SArnaldo Carvalho de Melo 		return err;
3879eb853e80SJiri Olsa 
3880bca647aaSTom Zanussi 	argc = parse_options(argc, argv, record_options, record_usage,
3881a0541234SAnton Blanchard 			    PARSE_OPT_STOP_AT_NON_OPTION);
388268ba3235SNamhyung Kim 	if (quiet)
388368ba3235SNamhyung Kim 		perf_quiet_option();
3884483635a9SJiri Olsa 
38857cc72553SJames Clark 	err = symbol__validate_sym_arguments();
38867cc72553SJames Clark 	if (err)
38877cc72553SJames Clark 		return err;
38887cc72553SJames Clark 
38899bce13eaSJiri Olsa 	perf_debuginfod_setup(&record.debuginfod);
38909bce13eaSJiri Olsa 
3891483635a9SJiri Olsa 	/* Make system wide (-a) the default target. */
3892602ad878SArnaldo Carvalho de Melo 	if (!argc && target__none(&rec->opts.target))
3893483635a9SJiri Olsa 		rec->opts.target.system_wide = true;
389486470930SIngo Molnar 
3895bea03405SNamhyung Kim 	if (nr_cgroups && !rec->opts.target.system_wide) {
3896c7118369SNamhyung Kim 		usage_with_options_msg(record_usage, record_options,
3897c7118369SNamhyung Kim 			"cgroup monitoring only available in system-wide mode");
3898c7118369SNamhyung Kim 
3899023695d9SStephane Eranian 	}
3900504c1ad1SAlexey Budankov 
3901e29386c8SJiri Olsa 	if (rec->buildid_mmap) {
3902e29386c8SJiri Olsa 		if (!perf_can_record_build_id()) {
3903e29386c8SJiri Olsa 			pr_err("Failed: no support to record build id in mmap events, update your kernel.\n");
3904e29386c8SJiri Olsa 			err = -EINVAL;
3905e29386c8SJiri Olsa 			goto out_opts;
3906e29386c8SJiri Olsa 		}
3907e29386c8SJiri Olsa 		pr_debug("Enabling build id in mmap2 events.\n");
3908e29386c8SJiri Olsa 		/* Enable mmap build id synthesizing. */
3909e29386c8SJiri Olsa 		symbol_conf.buildid_mmap2 = true;
3910e29386c8SJiri Olsa 		/* Enable perf_event_attr::build_id bit. */
3911e29386c8SJiri Olsa 		rec->opts.build_id = true;
3912e29386c8SJiri Olsa 		/* Disable build id cache. */
3913e29386c8SJiri Olsa 		rec->no_buildid = true;
3914e29386c8SJiri Olsa 	}
3915e29386c8SJiri Olsa 
39164f2abe91SNamhyung Kim 	if (rec->opts.record_cgroup && !perf_can_record_cgroup()) {
39174f2abe91SNamhyung Kim 		pr_err("Kernel has no cgroup sampling support.\n");
39184f2abe91SNamhyung Kim 		err = -EINVAL;
39194f2abe91SNamhyung Kim 		goto out_opts;
39204f2abe91SNamhyung Kim 	}
39214f2abe91SNamhyung Kim 
3922f42c0ce5SAdrian Hunter 	if (rec->opts.kcore)
3923f42c0ce5SAdrian Hunter 		rec->opts.text_poke = true;
3924f42c0ce5SAdrian Hunter 
392556f735ffSAlexey Bayduraev 	if (rec->opts.kcore || record__threads_enabled(rec))
3926eeb399b5SAdrian Hunter 		rec->data.is_dir = true;
3927eeb399b5SAdrian Hunter 
3928b5f2511dSAlexey Bayduraev 	if (record__threads_enabled(rec)) {
3929b5f2511dSAlexey Bayduraev 		if (rec->opts.affinity != PERF_AFFINITY_SYS) {
3930b5f2511dSAlexey Bayduraev 			pr_err("--affinity option is mutually exclusive to parallel streaming mode.\n");
3931b5f2511dSAlexey Bayduraev 			goto out_opts;
3932b5f2511dSAlexey Bayduraev 		}
3933b5f2511dSAlexey Bayduraev 		if (record__aio_enabled(rec)) {
3934b5f2511dSAlexey Bayduraev 			pr_err("Asynchronous streaming mode (--aio) is mutually exclusive to parallel streaming mode.\n");
3935b5f2511dSAlexey Bayduraev 			goto out_opts;
3936b5f2511dSAlexey Bayduraev 		}
3937b5f2511dSAlexey Bayduraev 	}
3938b5f2511dSAlexey Bayduraev 
3939504c1ad1SAlexey Budankov 	if (rec->opts.comp_level != 0) {
3940504c1ad1SAlexey Budankov 		pr_debug("Compression enabled, disabling build id collection at the end of the session.\n");
3941504c1ad1SAlexey Budankov 		rec->no_buildid = true;
3942504c1ad1SAlexey Budankov 	}
3943504c1ad1SAlexey Budankov 
3944b757bb09SAdrian Hunter 	if (rec->opts.record_switch_events &&
3945b757bb09SAdrian Hunter 	    !perf_can_record_switch_events()) {
3946c7118369SNamhyung Kim 		ui__error("kernel does not support recording context switch events\n");
3947c7118369SNamhyung Kim 		parse_options_usage(record_usage, record_options, "switch-events", 0);
3948a8fcbd26SAdrian Hunter 		err = -EINVAL;
3949a8fcbd26SAdrian Hunter 		goto out_opts;
3950b757bb09SAdrian Hunter 	}
3951023695d9SStephane Eranian 
3952cb4e1ebbSJiri Olsa 	if (switch_output_setup(rec)) {
3953cb4e1ebbSJiri Olsa 		parse_options_usage(record_usage, record_options, "switch-output", 0);
3954a8fcbd26SAdrian Hunter 		err = -EINVAL;
3955a8fcbd26SAdrian Hunter 		goto out_opts;
3956cb4e1ebbSJiri Olsa 	}
3957cb4e1ebbSJiri Olsa 
3958bfacbe3bSJiri Olsa 	if (rec->switch_output.time) {
3959bfacbe3bSJiri Olsa 		signal(SIGALRM, alarm_sig_handler);
3960bfacbe3bSJiri Olsa 		alarm(rec->switch_output.time);
3961bfacbe3bSJiri Olsa 	}
3962bfacbe3bSJiri Olsa 
396303724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
396403724b2eSAndi Kleen 		rec->switch_output.filenames = calloc(sizeof(char *),
396503724b2eSAndi Kleen 						      rec->switch_output.num_files);
3966a8fcbd26SAdrian Hunter 		if (!rec->switch_output.filenames) {
3967a8fcbd26SAdrian Hunter 			err = -EINVAL;
3968a8fcbd26SAdrian Hunter 			goto out_opts;
3969a8fcbd26SAdrian Hunter 		}
397003724b2eSAndi Kleen 	}
397103724b2eSAndi Kleen 
3972b5f2511dSAlexey Bayduraev 	if (rec->timestamp_filename && record__threads_enabled(rec)) {
3973b5f2511dSAlexey Bayduraev 		rec->timestamp_filename = false;
3974b5f2511dSAlexey Bayduraev 		pr_warning("WARNING: --timestamp-filename option is not available in parallel streaming mode.\n");
3975b5f2511dSAlexey Bayduraev 	}
3976b5f2511dSAlexey Bayduraev 
39771b36c03eSAdrian Hunter 	/*
39781b36c03eSAdrian Hunter 	 * Allow aliases to facilitate the lookup of symbols for address
39791b36c03eSAdrian Hunter 	 * filters. Refer to auxtrace_parse_filters().
39801b36c03eSAdrian Hunter 	 */
39811b36c03eSAdrian Hunter 	symbol_conf.allow_aliases = true;
39821b36c03eSAdrian Hunter 
39831b36c03eSAdrian Hunter 	symbol__init(NULL);
39841b36c03eSAdrian Hunter 
39854b5ea3bdSAdrian Hunter 	err = record__auxtrace_init(rec);
39861b36c03eSAdrian Hunter 	if (err)
39871b36c03eSAdrian Hunter 		goto out;
39881b36c03eSAdrian Hunter 
39890aab2136SWang Nan 	if (dry_run)
39905c01ad60SAdrian Hunter 		goto out;
39910aab2136SWang Nan 
3992d7888573SWang Nan 	err = bpf__setup_stdout(rec->evlist);
3993d7888573SWang Nan 	if (err) {
3994d7888573SWang Nan 		bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
3995d7888573SWang Nan 		pr_err("ERROR: Setup BPF stdout failed: %s\n",
3996d7888573SWang Nan 			 errbuf);
39975c01ad60SAdrian Hunter 		goto out;
3998d7888573SWang Nan 	}
3999d7888573SWang Nan 
4000ef149c25SAdrian Hunter 	err = -ENOMEM;
4001ef149c25SAdrian Hunter 
40020c1d46a8SWang Nan 	if (rec->no_buildid_cache || rec->no_buildid) {
4003a1ac1d3cSStephane Eranian 		disable_buildid_cache();
4004dc0c6127SJiri Olsa 	} else if (rec->switch_output.enabled) {
40050c1d46a8SWang Nan 		/*
40060c1d46a8SWang Nan 		 * In 'perf record --switch-output', disable buildid
40070c1d46a8SWang Nan 		 * generation by default to reduce data file switching
40080c1d46a8SWang Nan 		 * overhead. Still generate buildid if they are required
40090c1d46a8SWang Nan 		 * explicitly using
40100c1d46a8SWang Nan 		 *
401160437ac0SJiri Olsa 		 *  perf record --switch-output --no-no-buildid \
40120c1d46a8SWang Nan 		 *              --no-no-buildid-cache
40130c1d46a8SWang Nan 		 *
40140c1d46a8SWang Nan 		 * Following code equals to:
40150c1d46a8SWang Nan 		 *
40160c1d46a8SWang Nan 		 * if ((rec->no_buildid || !rec->no_buildid_set) &&
40170c1d46a8SWang Nan 		 *     (rec->no_buildid_cache || !rec->no_buildid_cache_set))
40180c1d46a8SWang Nan 		 *         disable_buildid_cache();
40190c1d46a8SWang Nan 		 */
40200c1d46a8SWang Nan 		bool disable = true;
40210c1d46a8SWang Nan 
40220c1d46a8SWang Nan 		if (rec->no_buildid_set && !rec->no_buildid)
40230c1d46a8SWang Nan 			disable = false;
40240c1d46a8SWang Nan 		if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
40250c1d46a8SWang Nan 			disable = false;
40260c1d46a8SWang Nan 		if (disable) {
40270c1d46a8SWang Nan 			rec->no_buildid = true;
40280c1d46a8SWang Nan 			rec->no_buildid_cache = true;
40290c1d46a8SWang Nan 			disable_buildid_cache();
40300c1d46a8SWang Nan 		}
40310c1d46a8SWang Nan 	}
4032655000e7SArnaldo Carvalho de Melo 
40334ea648aeSWang Nan 	if (record.opts.overwrite)
40344ea648aeSWang Nan 		record.opts.tail_synthesize = true;
40354ea648aeSWang Nan 
4036b53a0755SJin Yao 	if (rec->evlist->core.nr_entries == 0) {
4037b53a0755SJin Yao 		if (perf_pmu__has_hybrid()) {
4038b53a0755SJin Yao 			err = evlist__add_default_hybrid(rec->evlist,
4039b53a0755SJin Yao 							 !record.opts.no_samples);
4040b53a0755SJin Yao 		} else {
4041b53a0755SJin Yao 			err = __evlist__add_default(rec->evlist,
4042b53a0755SJin Yao 						    !record.opts.no_samples);
4043b53a0755SJin Yao 		}
4044b53a0755SJin Yao 
4045b53a0755SJin Yao 		if (err < 0) {
404669aad6f1SArnaldo Carvalho de Melo 			pr_err("Not enough memory for event selector list\n");
4047394c01edSAdrian Hunter 			goto out;
4048bbd36e5eSPeter Zijlstra 		}
4049b53a0755SJin Yao 	}
405086470930SIngo Molnar 
405169e7e5b0SAdrian Hunter 	if (rec->opts.target.tid && !rec->opts.no_inherit_set)
405269e7e5b0SAdrian Hunter 		rec->opts.no_inherit = true;
405369e7e5b0SAdrian Hunter 
4054602ad878SArnaldo Carvalho de Melo 	err = target__validate(&rec->opts.target);
405516ad2ffbSNamhyung Kim 	if (err) {
4056602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
4057c3dec27bSJiri Olsa 		ui__warning("%s\n", errbuf);
405816ad2ffbSNamhyung Kim 	}
40594bd0f2d2SNamhyung Kim 
4060602ad878SArnaldo Carvalho de Melo 	err = target__parse_uid(&rec->opts.target);
406116ad2ffbSNamhyung Kim 	if (err) {
406216ad2ffbSNamhyung Kim 		int saved_errno = errno;
406316ad2ffbSNamhyung Kim 
4064602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
40653780f488SNamhyung Kim 		ui__error("%s", errbuf);
406616ad2ffbSNamhyung Kim 
406716ad2ffbSNamhyung Kim 		err = -saved_errno;
4068394c01edSAdrian Hunter 		goto out;
406916ad2ffbSNamhyung Kim 	}
40700d37aa34SArnaldo Carvalho de Melo 
4071ca800068SMengting Zhang 	/* Enable ignoring missing threads when -u/-p option is defined. */
4072ca800068SMengting Zhang 	rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid;
407323dc4f15SJiri Olsa 
40741d3351e6SJin Yao 	if (evlist__fix_hybrid_cpus(rec->evlist, rec->opts.target.cpu_list)) {
40751d3351e6SJin Yao 		pr_err("failed to use cpu list %s\n",
40761d3351e6SJin Yao 		       rec->opts.target.cpu_list);
40771d3351e6SJin Yao 		goto out;
40781d3351e6SJin Yao 	}
40791d3351e6SJin Yao 
40801d3351e6SJin Yao 	rec->opts.target.hybrid = perf_pmu__has_hybrid();
40817248e308SAlexandre Truong 
40827248e308SAlexandre Truong 	if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP)
40837248e308SAlexandre Truong 		arch__add_leaf_frame_record_opts(&rec->opts);
40847248e308SAlexandre Truong 
408516ad2ffbSNamhyung Kim 	err = -ENOMEM;
40861bf7d836SMartin Liška 	if (evlist__create_maps(rec->evlist, &rec->opts.target) < 0) {
40871bf7d836SMartin Liška 		if (rec->opts.target.pid != NULL) {
40881bf7d836SMartin Liška 			pr_err("Couldn't create thread/CPU maps: %s\n",
40891bf7d836SMartin Liška 				errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
40901bf7d836SMartin Liška 			goto out;
40911bf7d836SMartin Liška 		}
40921bf7d836SMartin Liška 		else
4093dd7927f4SArnaldo Carvalho de Melo 			usage_with_options(record_usage, record_options);
40941bf7d836SMartin Liška 	}
409569aad6f1SArnaldo Carvalho de Melo 
4096ef149c25SAdrian Hunter 	err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
4097ef149c25SAdrian Hunter 	if (err)
4098394c01edSAdrian Hunter 		goto out;
4099ef149c25SAdrian Hunter 
41006156681bSNamhyung Kim 	/*
41016156681bSNamhyung Kim 	 * We take all buildids when the file contains
41026156681bSNamhyung Kim 	 * AUX area tracing data because we do not decode the
41036156681bSNamhyung Kim 	 * trace because it would take too long.
41046156681bSNamhyung Kim 	 */
41056156681bSNamhyung Kim 	if (rec->opts.full_auxtrace)
41066156681bSNamhyung Kim 		rec->buildid_all = true;
41076156681bSNamhyung Kim 
4108246eba8eSAdrian Hunter 	if (rec->opts.text_poke) {
4109246eba8eSAdrian Hunter 		err = record__config_text_poke(rec->evlist);
4110246eba8eSAdrian Hunter 		if (err) {
4111246eba8eSAdrian Hunter 			pr_err("record__config_text_poke failed, error %d\n", err);
4112246eba8eSAdrian Hunter 			goto out;
4113246eba8eSAdrian Hunter 		}
4114246eba8eSAdrian Hunter 	}
4115246eba8eSAdrian Hunter 
4116edc41a10SNamhyung Kim 	if (rec->off_cpu) {
4117edc41a10SNamhyung Kim 		err = record__config_off_cpu(rec);
4118edc41a10SNamhyung Kim 		if (err) {
4119edc41a10SNamhyung Kim 			pr_err("record__config_off_cpu failed, error %d\n", err);
4120edc41a10SNamhyung Kim 			goto out;
4121edc41a10SNamhyung Kim 		}
4122edc41a10SNamhyung Kim 	}
4123edc41a10SNamhyung Kim 
4124b4006796SArnaldo Carvalho de Melo 	if (record_opts__config(&rec->opts)) {
412539d17dacSArnaldo Carvalho de Melo 		err = -EINVAL;
4126394c01edSAdrian Hunter 		goto out;
41277e4ff9e3SMike Galbraith 	}
41287e4ff9e3SMike Galbraith 
41297954f716SAlexey Bayduraev 	err = record__init_thread_masks(rec);
41307954f716SAlexey Bayduraev 	if (err) {
41317954f716SAlexey Bayduraev 		pr_err("Failed to initialize parallel data streaming masks\n");
41327954f716SAlexey Bayduraev 		goto out;
41337954f716SAlexey Bayduraev 	}
41347954f716SAlexey Bayduraev 
413593f20c0fSAlexey Budankov 	if (rec->opts.nr_cblocks > nr_cblocks_max)
413693f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = nr_cblocks_max;
41375d7f4116SAlexey Budankov 	pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks);
4138d3d1af6fSAlexey Budankov 
41399d2ed645SAlexey Budankov 	pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]);
4140470530bbSAlexey Budankov 	pr_debug("mmap flush: %d\n", rec->opts.mmap_flush);
41419d2ed645SAlexey Budankov 
414251255a8aSAlexey Budankov 	if (rec->opts.comp_level > comp_level_max)
414351255a8aSAlexey Budankov 		rec->opts.comp_level = comp_level_max;
414451255a8aSAlexey Budankov 	pr_debug("comp level: %d\n", rec->opts.comp_level);
414551255a8aSAlexey Budankov 
4146d20deb64SArnaldo Carvalho de Melo 	err = __cmd_record(&record, argc, argv);
4147394c01edSAdrian Hunter out:
4148c12995a5SJiri Olsa 	evlist__delete(rec->evlist);
4149d65a458bSArnaldo Carvalho de Melo 	symbol__exit();
4150ef149c25SAdrian Hunter 	auxtrace_record__free(rec->itr);
4151a8fcbd26SAdrian Hunter out_opts:
41527954f716SAlexey Bayduraev 	record__free_thread_masks(rec, rec->nr_threads);
41537954f716SAlexey Bayduraev 	rec->nr_threads = 0;
4154ee7fe31eSAdrian Hunter 	evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close);
415539d17dacSArnaldo Carvalho de Melo 	return err;
415686470930SIngo Molnar }
41572dd6d8a1SAdrian Hunter 
41582dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused)
41592dd6d8a1SAdrian Hunter {
4160dc0c6127SJiri Olsa 	struct record *rec = &record;
4161dc0c6127SJiri Olsa 
4162d20aff15SAdrian Hunter 	hit_auxtrace_snapshot_trigger(rec);
41633c1cb7e3SWang Nan 
4164dc0c6127SJiri Olsa 	if (switch_output_signal(rec))
41653c1cb7e3SWang Nan 		trigger_hit(&switch_output_trigger);
41662dd6d8a1SAdrian Hunter }
4167bfacbe3bSJiri Olsa 
4168bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused)
4169bfacbe3bSJiri Olsa {
4170bfacbe3bSJiri Olsa 	struct record *rec = &record;
4171bfacbe3bSJiri Olsa 
4172bfacbe3bSJiri Olsa 	if (switch_output_time(rec))
4173bfacbe3bSJiri Olsa 		trigger_hit(&switch_output_trigger);
4174bfacbe3bSJiri Olsa }
4175