xref: /openbmc/linux/tools/perf/builtin-record.c (revision 991ae4eb36911fe9a99bef1c22b9578ceec3896a)
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"
50d8871ea7SWang Nan #include "asm/bug.h"
51c1a604dfSArnaldo Carvalho de Melo #include "perf.h"
527c6a1c65SPeter Zijlstra 
53a43783aeSArnaldo Carvalho de Melo #include <errno.h>
54fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
5567230479SArnaldo Carvalho de Melo #include <locale.h>
564208735dSArnaldo Carvalho de Melo #include <poll.h>
57d99c22eaSStephane Eranian #include <pthread.h>
5886470930SIngo Molnar #include <unistd.h>
5986470930SIngo Molnar #include <sched.h>
609607ad3aSArnaldo Carvalho de Melo #include <signal.h>
61da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
62da231338SAnand K Mistry #include <sys/eventfd.h>
63da231338SAnand K Mistry #endif
64a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h>
654208735dSArnaldo Carvalho de Melo #include <sys/wait.h>
66eeb399b5SAdrian Hunter #include <sys/types.h>
67eeb399b5SAdrian Hunter #include <sys/stat.h>
68eeb399b5SAdrian Hunter #include <fcntl.h>
696ef81c55SMamatha Inamdar #include <linux/err.h>
708520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
710693e680SArnaldo Carvalho de Melo #include <linux/time64.h>
72d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
738384a260SAlexey Budankov #include <linux/bitmap.h>
74d1e325cfSJiri Olsa #include <sys/time.h>
7578da39faSBernhard Rosenkraenzer 
761b43b704SJiri Olsa struct switch_output {
77dc0c6127SJiri Olsa 	bool		 enabled;
781b43b704SJiri Olsa 	bool		 signal;
79dc0c6127SJiri Olsa 	unsigned long	 size;
80bfacbe3bSJiri Olsa 	unsigned long	 time;
81cb4e1ebbSJiri Olsa 	const char	*str;
82cb4e1ebbSJiri Olsa 	bool		 set;
8303724b2eSAndi Kleen 	char		 **filenames;
8403724b2eSAndi Kleen 	int		 num_files;
8503724b2eSAndi Kleen 	int		 cur_file;
861b43b704SJiri Olsa };
871b43b704SJiri Olsa 
888c6f45a7SArnaldo Carvalho de Melo struct record {
8945694aa7SArnaldo Carvalho de Melo 	struct perf_tool	tool;
90b4006796SArnaldo Carvalho de Melo 	struct record_opts	opts;
91d20deb64SArnaldo Carvalho de Melo 	u64			bytes_written;
928ceb41d7SJiri Olsa 	struct perf_data	data;
93ef149c25SAdrian Hunter 	struct auxtrace_record	*itr;
9463503dbaSJiri Olsa 	struct evlist	*evlist;
95d20deb64SArnaldo Carvalho de Melo 	struct perf_session	*session;
96bc477d79SArnaldo Carvalho de Melo 	struct evlist		*sb_evlist;
97899e5ffbSArnaldo Carvalho de Melo 	pthread_t		thread_id;
98d20deb64SArnaldo Carvalho de Melo 	int			realtime_prio;
99899e5ffbSArnaldo Carvalho de Melo 	bool			switch_output_event_set;
100d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid;
101d2db9a98SWang Nan 	bool			no_buildid_set;
102d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid_cache;
103d2db9a98SWang Nan 	bool			no_buildid_cache_set;
1046156681bSNamhyung Kim 	bool			buildid_all;
105e29386c8SJiri Olsa 	bool			buildid_mmap;
106ecfd7a9cSWang Nan 	bool			timestamp_filename;
10768588bafSJin Yao 	bool			timestamp_boundary;
1081b43b704SJiri Olsa 	struct switch_output	switch_output;
1099f065194SYang Shi 	unsigned long long	samples;
1108384a260SAlexey Budankov 	struct mmap_cpu_mask	affinity_mask;
1116d575816SJiwei Sun 	unsigned long		output_max_size;	/* = 0: unlimited */
1120f82ebc4SArnaldo Carvalho de Melo };
11386470930SIngo Molnar 
1146d575816SJiwei Sun static volatile int done;
1156d575816SJiwei Sun 
116dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started;
117dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
118dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger);
119dc0c6127SJiri Olsa 
1209d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = {
1219d2ed645SAlexey Budankov 	"SYS", "NODE", "CPU"
1229d2ed645SAlexey Budankov };
1239d2ed645SAlexey Budankov 
124dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec)
125dc0c6127SJiri Olsa {
126dc0c6127SJiri Olsa 	return rec->switch_output.signal &&
127dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
128dc0c6127SJiri Olsa }
129dc0c6127SJiri Olsa 
130dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec)
131dc0c6127SJiri Olsa {
132dc0c6127SJiri Olsa 	return rec->switch_output.size &&
133dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger) &&
134dc0c6127SJiri Olsa 	       (rec->bytes_written >= rec->switch_output.size);
135dc0c6127SJiri Olsa }
136dc0c6127SJiri Olsa 
137bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec)
138bfacbe3bSJiri Olsa {
139bfacbe3bSJiri Olsa 	return rec->switch_output.time &&
140bfacbe3bSJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
141bfacbe3bSJiri Olsa }
142bfacbe3bSJiri Olsa 
1436d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec)
1446d575816SJiwei Sun {
1456d575816SJiwei Sun 	return rec->output_max_size &&
1466d575816SJiwei Sun 	       (rec->bytes_written >= rec->output_max_size);
1476d575816SJiwei Sun }
1486d575816SJiwei Sun 
149a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused,
150ded2b8feSJiri Olsa 			 void *bf, size_t size)
151f5970550SPeter Zijlstra {
152ded2b8feSJiri Olsa 	struct perf_data_file *file = &rec->session->data->file;
153ded2b8feSJiri Olsa 
154ded2b8feSJiri Olsa 	if (perf_data_file__write(file, bf, size) < 0) {
1554f624685SAdrian Hunter 		pr_err("failed to write perf data, error: %m\n");
1568d3eca20SDavid Ahern 		return -1;
1578d3eca20SDavid Ahern 	}
158f5970550SPeter Zijlstra 
159cf8b2e69SArnaldo Carvalho de Melo 	rec->bytes_written += size;
160dc0c6127SJiri Olsa 
1616d575816SJiwei Sun 	if (record__output_max_size_exceeded(rec) && !done) {
1626d575816SJiwei Sun 		fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
1636d575816SJiwei Sun 				" stopping session ]\n",
1646d575816SJiwei Sun 				rec->bytes_written >> 10);
1656d575816SJiwei Sun 		done = 1;
1666d575816SJiwei Sun 	}
1676d575816SJiwei Sun 
168dc0c6127SJiri Olsa 	if (switch_output_size(rec))
169dc0c6127SJiri Olsa 		trigger_hit(&switch_output_trigger);
170dc0c6127SJiri Olsa 
1718d3eca20SDavid Ahern 	return 0;
172f5970550SPeter Zijlstra }
173f5970550SPeter Zijlstra 
174ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec);
175ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec);
1765d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size,
1775d7f4116SAlexey Budankov 			    void *src, size_t src_size);
1785d7f4116SAlexey Budankov 
179d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
180d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd,
181d3d1af6fSAlexey Budankov 		void *buf, size_t size, off_t off)
182d3d1af6fSAlexey Budankov {
183d3d1af6fSAlexey Budankov 	int rc;
184d3d1af6fSAlexey Budankov 
185d3d1af6fSAlexey Budankov 	cblock->aio_fildes = trace_fd;
186d3d1af6fSAlexey Budankov 	cblock->aio_buf    = buf;
187d3d1af6fSAlexey Budankov 	cblock->aio_nbytes = size;
188d3d1af6fSAlexey Budankov 	cblock->aio_offset = off;
189d3d1af6fSAlexey Budankov 	cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
190d3d1af6fSAlexey Budankov 
191d3d1af6fSAlexey Budankov 	do {
192d3d1af6fSAlexey Budankov 		rc = aio_write(cblock);
193d3d1af6fSAlexey Budankov 		if (rc == 0) {
194d3d1af6fSAlexey Budankov 			break;
195d3d1af6fSAlexey Budankov 		} else if (errno != EAGAIN) {
196d3d1af6fSAlexey Budankov 			cblock->aio_fildes = -1;
197d3d1af6fSAlexey Budankov 			pr_err("failed to queue perf data, error: %m\n");
198d3d1af6fSAlexey Budankov 			break;
199d3d1af6fSAlexey Budankov 		}
200d3d1af6fSAlexey Budankov 	} while (1);
201d3d1af6fSAlexey Budankov 
202d3d1af6fSAlexey Budankov 	return rc;
203d3d1af6fSAlexey Budankov }
204d3d1af6fSAlexey Budankov 
205a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock)
206d3d1af6fSAlexey Budankov {
207d3d1af6fSAlexey Budankov 	void *rem_buf;
208d3d1af6fSAlexey Budankov 	off_t rem_off;
209d3d1af6fSAlexey Budankov 	size_t rem_size;
210d3d1af6fSAlexey Budankov 	int rc, aio_errno;
211d3d1af6fSAlexey Budankov 	ssize_t aio_ret, written;
212d3d1af6fSAlexey Budankov 
213d3d1af6fSAlexey Budankov 	aio_errno = aio_error(cblock);
214d3d1af6fSAlexey Budankov 	if (aio_errno == EINPROGRESS)
215d3d1af6fSAlexey Budankov 		return 0;
216d3d1af6fSAlexey Budankov 
217d3d1af6fSAlexey Budankov 	written = aio_ret = aio_return(cblock);
218d3d1af6fSAlexey Budankov 	if (aio_ret < 0) {
219d3d1af6fSAlexey Budankov 		if (aio_errno != EINTR)
220d3d1af6fSAlexey Budankov 			pr_err("failed to write perf data, error: %m\n");
221d3d1af6fSAlexey Budankov 		written = 0;
222d3d1af6fSAlexey Budankov 	}
223d3d1af6fSAlexey Budankov 
224d3d1af6fSAlexey Budankov 	rem_size = cblock->aio_nbytes - written;
225d3d1af6fSAlexey Budankov 
226d3d1af6fSAlexey Budankov 	if (rem_size == 0) {
227d3d1af6fSAlexey Budankov 		cblock->aio_fildes = -1;
228d3d1af6fSAlexey Budankov 		/*
229ef781128SAlexey Budankov 		 * md->refcount is incremented in record__aio_pushfn() for
230ef781128SAlexey Budankov 		 * every aio write request started in record__aio_push() so
231ef781128SAlexey Budankov 		 * decrement it because the request is now complete.
232d3d1af6fSAlexey Budankov 		 */
23380e53d11SJiri Olsa 		perf_mmap__put(&md->core);
234d3d1af6fSAlexey Budankov 		rc = 1;
235d3d1af6fSAlexey Budankov 	} else {
236d3d1af6fSAlexey Budankov 		/*
237d3d1af6fSAlexey Budankov 		 * aio write request may require restart with the
238d3d1af6fSAlexey Budankov 		 * reminder if the kernel didn't write whole
239d3d1af6fSAlexey Budankov 		 * chunk at once.
240d3d1af6fSAlexey Budankov 		 */
241d3d1af6fSAlexey Budankov 		rem_off = cblock->aio_offset + written;
242d3d1af6fSAlexey Budankov 		rem_buf = (void *)(cblock->aio_buf + written);
243d3d1af6fSAlexey Budankov 		record__aio_write(cblock, cblock->aio_fildes,
244d3d1af6fSAlexey Budankov 				rem_buf, rem_size, rem_off);
245d3d1af6fSAlexey Budankov 		rc = 0;
246d3d1af6fSAlexey Budankov 	}
247d3d1af6fSAlexey Budankov 
248d3d1af6fSAlexey Budankov 	return rc;
249d3d1af6fSAlexey Budankov }
250d3d1af6fSAlexey Budankov 
251a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all)
252d3d1af6fSAlexey Budankov {
25393f20c0fSAlexey Budankov 	struct aiocb **aiocb = md->aio.aiocb;
25493f20c0fSAlexey Budankov 	struct aiocb *cblocks = md->aio.cblocks;
255d3d1af6fSAlexey Budankov 	struct timespec timeout = { 0, 1000 * 1000  * 1 }; /* 1ms */
25693f20c0fSAlexey Budankov 	int i, do_suspend;
257d3d1af6fSAlexey Budankov 
258d3d1af6fSAlexey Budankov 	do {
25993f20c0fSAlexey Budankov 		do_suspend = 0;
26093f20c0fSAlexey Budankov 		for (i = 0; i < md->aio.nr_cblocks; ++i) {
26193f20c0fSAlexey Budankov 			if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
26293f20c0fSAlexey Budankov 				if (sync_all)
26393f20c0fSAlexey Budankov 					aiocb[i] = NULL;
26493f20c0fSAlexey Budankov 				else
26593f20c0fSAlexey Budankov 					return i;
26693f20c0fSAlexey Budankov 			} else {
26793f20c0fSAlexey Budankov 				/*
26893f20c0fSAlexey Budankov 				 * Started aio write is not complete yet
26993f20c0fSAlexey Budankov 				 * so it has to be waited before the
27093f20c0fSAlexey Budankov 				 * next allocation.
27193f20c0fSAlexey Budankov 				 */
27293f20c0fSAlexey Budankov 				aiocb[i] = &cblocks[i];
27393f20c0fSAlexey Budankov 				do_suspend = 1;
27493f20c0fSAlexey Budankov 			}
27593f20c0fSAlexey Budankov 		}
27693f20c0fSAlexey Budankov 		if (!do_suspend)
27793f20c0fSAlexey Budankov 			return -1;
278d3d1af6fSAlexey Budankov 
27993f20c0fSAlexey Budankov 		while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) {
280d3d1af6fSAlexey Budankov 			if (!(errno == EAGAIN || errno == EINTR))
281d3d1af6fSAlexey Budankov 				pr_err("failed to sync perf data, error: %m\n");
282d3d1af6fSAlexey Budankov 		}
283d3d1af6fSAlexey Budankov 	} while (1);
284d3d1af6fSAlexey Budankov }
285d3d1af6fSAlexey Budankov 
286ef781128SAlexey Budankov struct record_aio {
287ef781128SAlexey Budankov 	struct record	*rec;
288ef781128SAlexey Budankov 	void		*data;
289ef781128SAlexey Budankov 	size_t		size;
290ef781128SAlexey Budankov };
291ef781128SAlexey Budankov 
292a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size)
293d3d1af6fSAlexey Budankov {
294ef781128SAlexey Budankov 	struct record_aio *aio = to;
295ef781128SAlexey Budankov 
296ef781128SAlexey Budankov 	/*
297547740f7SJiri Olsa 	 * map->core.base data pointed by buf is copied into free map->aio.data[] buffer
298ef781128SAlexey Budankov 	 * to release space in the kernel buffer as fast as possible, calling
299ef781128SAlexey Budankov 	 * perf_mmap__consume() from perf_mmap__push() function.
300ef781128SAlexey Budankov 	 *
301ef781128SAlexey Budankov 	 * That lets the kernel to proceed with storing more profiling data into
302ef781128SAlexey Budankov 	 * the kernel buffer earlier than other per-cpu kernel buffers are handled.
303ef781128SAlexey Budankov 	 *
304ef781128SAlexey Budankov 	 * Coping can be done in two steps in case the chunk of profiling data
305ef781128SAlexey Budankov 	 * crosses the upper bound of the kernel buffer. In this case we first move
306ef781128SAlexey Budankov 	 * part of data from map->start till the upper bound and then the reminder
307ef781128SAlexey Budankov 	 * from the beginning of the kernel buffer till the end of the data chunk.
308ef781128SAlexey Budankov 	 */
309ef781128SAlexey Budankov 
310ef781128SAlexey Budankov 	if (record__comp_enabled(aio->rec)) {
311ef781128SAlexey Budankov 		size = zstd_compress(aio->rec->session, aio->data + aio->size,
312bf59b305SJiri Olsa 				     mmap__mmap_len(map) - aio->size,
313ef781128SAlexey Budankov 				     buf, size);
314ef781128SAlexey Budankov 	} else {
315ef781128SAlexey Budankov 		memcpy(aio->data + aio->size, buf, size);
316ef781128SAlexey Budankov 	}
317ef781128SAlexey Budankov 
318ef781128SAlexey Budankov 	if (!aio->size) {
319ef781128SAlexey Budankov 		/*
320ef781128SAlexey Budankov 		 * Increment map->refcount to guard map->aio.data[] buffer
321ef781128SAlexey Budankov 		 * from premature deallocation because map object can be
322ef781128SAlexey Budankov 		 * released earlier than aio write request started on
323ef781128SAlexey Budankov 		 * map->aio.data[] buffer is complete.
324ef781128SAlexey Budankov 		 *
325ef781128SAlexey Budankov 		 * perf_mmap__put() is done at record__aio_complete()
326ef781128SAlexey Budankov 		 * after started aio request completion or at record__aio_push()
327ef781128SAlexey Budankov 		 * if the request failed to start.
328ef781128SAlexey Budankov 		 */
329e75710f0SJiri Olsa 		perf_mmap__get(&map->core);
330ef781128SAlexey Budankov 	}
331ef781128SAlexey Budankov 
332ef781128SAlexey Budankov 	aio->size += size;
333ef781128SAlexey Budankov 
334ef781128SAlexey Budankov 	return size;
335ef781128SAlexey Budankov }
336ef781128SAlexey Budankov 
337a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off)
338ef781128SAlexey Budankov {
339ef781128SAlexey Budankov 	int ret, idx;
340ef781128SAlexey Budankov 	int trace_fd = rec->session->data->file.fd;
341ef781128SAlexey Budankov 	struct record_aio aio = { .rec = rec, .size = 0 };
342ef781128SAlexey Budankov 
343ef781128SAlexey Budankov 	/*
344ef781128SAlexey Budankov 	 * Call record__aio_sync() to wait till map->aio.data[] buffer
345ef781128SAlexey Budankov 	 * becomes available after previous aio write operation.
346ef781128SAlexey Budankov 	 */
347ef781128SAlexey Budankov 
348ef781128SAlexey Budankov 	idx = record__aio_sync(map, false);
349ef781128SAlexey Budankov 	aio.data = map->aio.data[idx];
350ef781128SAlexey Budankov 	ret = perf_mmap__push(map, &aio, record__aio_pushfn);
351ef781128SAlexey Budankov 	if (ret != 0) /* ret > 0 - no data, ret < 0 - error */
352ef781128SAlexey Budankov 		return ret;
353d3d1af6fSAlexey Budankov 
354d3d1af6fSAlexey Budankov 	rec->samples++;
355ef781128SAlexey Budankov 	ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off);
356d3d1af6fSAlexey Budankov 	if (!ret) {
357ef781128SAlexey Budankov 		*off += aio.size;
358ef781128SAlexey Budankov 		rec->bytes_written += aio.size;
359d3d1af6fSAlexey Budankov 		if (switch_output_size(rec))
360d3d1af6fSAlexey Budankov 			trigger_hit(&switch_output_trigger);
361ef781128SAlexey Budankov 	} else {
362ef781128SAlexey Budankov 		/*
363ef781128SAlexey Budankov 		 * Decrement map->refcount incremented in record__aio_pushfn()
364ef781128SAlexey Budankov 		 * back if record__aio_write() operation failed to start, otherwise
365ef781128SAlexey Budankov 		 * map->refcount is decremented in record__aio_complete() after
366ef781128SAlexey Budankov 		 * aio write operation finishes successfully.
367ef781128SAlexey Budankov 		 */
36880e53d11SJiri Olsa 		perf_mmap__put(&map->core);
369d3d1af6fSAlexey Budankov 	}
370d3d1af6fSAlexey Budankov 
371d3d1af6fSAlexey Budankov 	return ret;
372d3d1af6fSAlexey Budankov }
373d3d1af6fSAlexey Budankov 
374d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd)
375d3d1af6fSAlexey Budankov {
376d3d1af6fSAlexey Budankov 	return lseek(trace_fd, 0, SEEK_CUR);
377d3d1af6fSAlexey Budankov }
378d3d1af6fSAlexey Budankov 
379d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos)
380d3d1af6fSAlexey Budankov {
381d3d1af6fSAlexey Budankov 	lseek(trace_fd, pos, SEEK_SET);
382d3d1af6fSAlexey Budankov }
383d3d1af6fSAlexey Budankov 
384d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec)
385d3d1af6fSAlexey Budankov {
386d3d1af6fSAlexey Budankov 	int i;
38763503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
388a5830532SJiri Olsa 	struct mmap *maps = evlist->mmap;
389d3d1af6fSAlexey Budankov 
390ef781128SAlexey Budankov 	if (!record__aio_enabled(rec))
391d3d1af6fSAlexey Budankov 		return;
392d3d1af6fSAlexey Budankov 
393c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
394a5830532SJiri Olsa 		struct mmap *map = &maps[i];
395d3d1af6fSAlexey Budankov 
396547740f7SJiri Olsa 		if (map->core.base)
39793f20c0fSAlexey Budankov 			record__aio_sync(map, true);
398d3d1af6fSAlexey Budankov 	}
399d3d1af6fSAlexey Budankov }
400d3d1af6fSAlexey Budankov 
401d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1;
40293f20c0fSAlexey Budankov static int nr_cblocks_max = 4;
403d3d1af6fSAlexey Budankov 
404d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt,
40593f20c0fSAlexey Budankov 			     const char *str,
406d3d1af6fSAlexey Budankov 			     int unset)
407d3d1af6fSAlexey Budankov {
408d3d1af6fSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
409d3d1af6fSAlexey Budankov 
41093f20c0fSAlexey Budankov 	if (unset) {
411d3d1af6fSAlexey Budankov 		opts->nr_cblocks = 0;
41293f20c0fSAlexey Budankov 	} else {
41393f20c0fSAlexey Budankov 		if (str)
41493f20c0fSAlexey Budankov 			opts->nr_cblocks = strtol(str, NULL, 0);
41593f20c0fSAlexey Budankov 		if (!opts->nr_cblocks)
416d3d1af6fSAlexey Budankov 			opts->nr_cblocks = nr_cblocks_default;
41793f20c0fSAlexey Budankov 	}
418d3d1af6fSAlexey Budankov 
419d3d1af6fSAlexey Budankov 	return 0;
420d3d1af6fSAlexey Budankov }
421d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */
42293f20c0fSAlexey Budankov static int nr_cblocks_max = 0;
42393f20c0fSAlexey Budankov 
424a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused,
425ef781128SAlexey Budankov 			    off_t *off __maybe_unused)
426d3d1af6fSAlexey Budankov {
427d3d1af6fSAlexey Budankov 	return -1;
428d3d1af6fSAlexey Budankov }
429d3d1af6fSAlexey Budankov 
430d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused)
431d3d1af6fSAlexey Budankov {
432d3d1af6fSAlexey Budankov 	return -1;
433d3d1af6fSAlexey Budankov }
434d3d1af6fSAlexey Budankov 
435d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused)
436d3d1af6fSAlexey Budankov {
437d3d1af6fSAlexey Budankov }
438d3d1af6fSAlexey Budankov 
439d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused)
440d3d1af6fSAlexey Budankov {
441d3d1af6fSAlexey Budankov }
442d3d1af6fSAlexey Budankov #endif
443d3d1af6fSAlexey Budankov 
444d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec)
445d3d1af6fSAlexey Budankov {
446d3d1af6fSAlexey Budankov 	return rec->opts.nr_cblocks > 0;
447d3d1af6fSAlexey Budankov }
448d3d1af6fSAlexey Budankov 
449470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1
450470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt,
451470530bbSAlexey Budankov 				    const char *str,
452470530bbSAlexey Budankov 				    int unset)
453470530bbSAlexey Budankov {
454470530bbSAlexey Budankov 	int flush_max;
455470530bbSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
456470530bbSAlexey Budankov 	static struct parse_tag tags[] = {
457470530bbSAlexey Budankov 			{ .tag  = 'B', .mult = 1       },
458470530bbSAlexey Budankov 			{ .tag  = 'K', .mult = 1 << 10 },
459470530bbSAlexey Budankov 			{ .tag  = 'M', .mult = 1 << 20 },
460470530bbSAlexey Budankov 			{ .tag  = 'G', .mult = 1 << 30 },
461470530bbSAlexey Budankov 			{ .tag  = 0 },
462470530bbSAlexey Budankov 	};
463470530bbSAlexey Budankov 
464470530bbSAlexey Budankov 	if (unset)
465470530bbSAlexey Budankov 		return 0;
466470530bbSAlexey Budankov 
467470530bbSAlexey Budankov 	if (str) {
468470530bbSAlexey Budankov 		opts->mmap_flush = parse_tag_value(str, tags);
469470530bbSAlexey Budankov 		if (opts->mmap_flush == (int)-1)
470470530bbSAlexey Budankov 			opts->mmap_flush = strtol(str, NULL, 0);
471470530bbSAlexey Budankov 	}
472470530bbSAlexey Budankov 
473470530bbSAlexey Budankov 	if (!opts->mmap_flush)
474470530bbSAlexey Budankov 		opts->mmap_flush = MMAP_FLUSH_DEFAULT;
475470530bbSAlexey Budankov 
4769521b5f2SJiri Olsa 	flush_max = evlist__mmap_size(opts->mmap_pages);
477470530bbSAlexey Budankov 	flush_max /= 4;
478470530bbSAlexey Budankov 	if (opts->mmap_flush > flush_max)
479470530bbSAlexey Budankov 		opts->mmap_flush = flush_max;
480470530bbSAlexey Budankov 
481470530bbSAlexey Budankov 	return 0;
482470530bbSAlexey Budankov }
483470530bbSAlexey Budankov 
484504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
485504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1;
486504c1ad1SAlexey Budankov 
487504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset)
488504c1ad1SAlexey Budankov {
489504c1ad1SAlexey Budankov 	struct record_opts *opts = opt->value;
490504c1ad1SAlexey Budankov 
491504c1ad1SAlexey Budankov 	if (unset) {
492504c1ad1SAlexey Budankov 		opts->comp_level = 0;
493504c1ad1SAlexey Budankov 	} else {
494504c1ad1SAlexey Budankov 		if (str)
495504c1ad1SAlexey Budankov 			opts->comp_level = strtol(str, NULL, 0);
496504c1ad1SAlexey Budankov 		if (!opts->comp_level)
497504c1ad1SAlexey Budankov 			opts->comp_level = comp_level_default;
498504c1ad1SAlexey Budankov 	}
499504c1ad1SAlexey Budankov 
500504c1ad1SAlexey Budankov 	return 0;
501504c1ad1SAlexey Budankov }
502504c1ad1SAlexey Budankov #endif
50351255a8aSAlexey Budankov static unsigned int comp_level_max = 22;
50451255a8aSAlexey Budankov 
50542e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec)
50642e1fd80SAlexey Budankov {
50742e1fd80SAlexey Budankov 	return rec->opts.comp_level > 0;
50842e1fd80SAlexey Budankov }
50942e1fd80SAlexey Budankov 
51045694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool,
511d20deb64SArnaldo Carvalho de Melo 				     union perf_event *event,
5121d037ca1SIrina Tirdea 				     struct perf_sample *sample __maybe_unused,
5131d037ca1SIrina Tirdea 				     struct machine *machine __maybe_unused)
514234fbbf5SArnaldo Carvalho de Melo {
5158c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = container_of(tool, struct record, tool);
516ded2b8feSJiri Olsa 	return record__write(rec, NULL, event, event->header.size);
517234fbbf5SArnaldo Carvalho de Melo }
518234fbbf5SArnaldo Carvalho de Melo 
519d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool,
520d99c22eaSStephane Eranian 				     union perf_event *event,
521d99c22eaSStephane Eranian 				     struct perf_sample *sample __maybe_unused,
522d99c22eaSStephane Eranian 				     struct machine *machine __maybe_unused)
523d99c22eaSStephane Eranian {
524d99c22eaSStephane Eranian 	static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER;
525d99c22eaSStephane Eranian 	int ret;
526d99c22eaSStephane Eranian 
527d99c22eaSStephane Eranian 	pthread_mutex_lock(&synth_lock);
528d99c22eaSStephane Eranian 	ret = process_synthesized_event(tool, event, sample, machine);
529d99c22eaSStephane Eranian 	pthread_mutex_unlock(&synth_lock);
530d99c22eaSStephane Eranian 	return ret;
531d99c22eaSStephane Eranian }
532d99c22eaSStephane Eranian 
533a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
534d37f1586SArnaldo Carvalho de Melo {
535d37f1586SArnaldo Carvalho de Melo 	struct record *rec = to;
536d37f1586SArnaldo Carvalho de Melo 
5375d7f4116SAlexey Budankov 	if (record__comp_enabled(rec)) {
538bf59b305SJiri Olsa 		size = zstd_compress(rec->session, map->data, mmap__mmap_len(map), bf, size);
5395d7f4116SAlexey Budankov 		bf   = map->data;
5405d7f4116SAlexey Budankov 	}
5415d7f4116SAlexey Budankov 
542d37f1586SArnaldo Carvalho de Melo 	rec->samples++;
543ded2b8feSJiri Olsa 	return record__write(rec, map, bf, size);
544d37f1586SArnaldo Carvalho de Melo }
545d37f1586SArnaldo Carvalho de Melo 
5462dd6d8a1SAdrian Hunter static volatile int signr = -1;
5472dd6d8a1SAdrian Hunter static volatile int child_finished;
548da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
549da231338SAnand K Mistry static int done_fd = -1;
550da231338SAnand K Mistry #endif
551c0bdc1c4SWang Nan 
5522dd6d8a1SAdrian Hunter static void sig_handler(int sig)
5532dd6d8a1SAdrian Hunter {
5542dd6d8a1SAdrian Hunter 	if (sig == SIGCHLD)
5552dd6d8a1SAdrian Hunter 		child_finished = 1;
5562dd6d8a1SAdrian Hunter 	else
5572dd6d8a1SAdrian Hunter 		signr = sig;
5582dd6d8a1SAdrian Hunter 
5592dd6d8a1SAdrian Hunter 	done = 1;
560da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
561da231338SAnand K Mistry {
562da231338SAnand K Mistry 	u64 tmp = 1;
563da231338SAnand K Mistry 	/*
564da231338SAnand K Mistry 	 * It is possible for this signal handler to run after done is checked
565da231338SAnand K Mistry 	 * in the main loop, but before the perf counter fds are polled. If this
566da231338SAnand K Mistry 	 * happens, the poll() will continue to wait even though done is set,
567da231338SAnand K Mistry 	 * and will only break out if either another signal is received, or the
568da231338SAnand K Mistry 	 * counters are ready for read. To ensure the poll() doesn't sleep when
569da231338SAnand K Mistry 	 * done is set, use an eventfd (done_fd) to wake up the poll().
570da231338SAnand K Mistry 	 */
571da231338SAnand K Mistry 	if (write(done_fd, &tmp, sizeof(tmp)) < 0)
572da231338SAnand K Mistry 		pr_err("failed to signal wakeup fd, error: %m\n");
573da231338SAnand K Mistry }
574da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT
5752dd6d8a1SAdrian Hunter }
5762dd6d8a1SAdrian Hunter 
577a074865eSWang Nan static void sigsegv_handler(int sig)
578a074865eSWang Nan {
579a074865eSWang Nan 	perf_hooks__recover();
580a074865eSWang Nan 	sighandler_dump_stack(sig);
581a074865eSWang Nan }
582a074865eSWang Nan 
5832dd6d8a1SAdrian Hunter static void record__sig_exit(void)
5842dd6d8a1SAdrian Hunter {
5852dd6d8a1SAdrian Hunter 	if (signr == -1)
5862dd6d8a1SAdrian Hunter 		return;
5872dd6d8a1SAdrian Hunter 
5882dd6d8a1SAdrian Hunter 	signal(signr, SIG_DFL);
5892dd6d8a1SAdrian Hunter 	raise(signr);
5902dd6d8a1SAdrian Hunter }
5912dd6d8a1SAdrian Hunter 
592e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT
593e31f0d01SAdrian Hunter 
594ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool,
595a5830532SJiri Olsa 				    struct mmap *map,
596ef149c25SAdrian Hunter 				    union perf_event *event, void *data1,
597ef149c25SAdrian Hunter 				    size_t len1, void *data2, size_t len2)
598ef149c25SAdrian Hunter {
599ef149c25SAdrian Hunter 	struct record *rec = container_of(tool, struct record, tool);
6008ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
601ef149c25SAdrian Hunter 	size_t padding;
602ef149c25SAdrian Hunter 	u8 pad[8] = {0};
603ef149c25SAdrian Hunter 
60446e201efSAdrian Hunter 	if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) {
60599fa2984SAdrian Hunter 		off_t file_offset;
6068ceb41d7SJiri Olsa 		int fd = perf_data__fd(data);
60799fa2984SAdrian Hunter 		int err;
60899fa2984SAdrian Hunter 
60999fa2984SAdrian Hunter 		file_offset = lseek(fd, 0, SEEK_CUR);
61099fa2984SAdrian Hunter 		if (file_offset == -1)
61199fa2984SAdrian Hunter 			return -1;
61299fa2984SAdrian Hunter 		err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
61399fa2984SAdrian Hunter 						     event, file_offset);
61499fa2984SAdrian Hunter 		if (err)
61599fa2984SAdrian Hunter 			return err;
61699fa2984SAdrian Hunter 	}
61799fa2984SAdrian Hunter 
618ef149c25SAdrian Hunter 	/* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
619ef149c25SAdrian Hunter 	padding = (len1 + len2) & 7;
620ef149c25SAdrian Hunter 	if (padding)
621ef149c25SAdrian Hunter 		padding = 8 - padding;
622ef149c25SAdrian Hunter 
623ded2b8feSJiri Olsa 	record__write(rec, map, event, event->header.size);
624ded2b8feSJiri Olsa 	record__write(rec, map, data1, len1);
625ef149c25SAdrian Hunter 	if (len2)
626ded2b8feSJiri Olsa 		record__write(rec, map, data2, len2);
627ded2b8feSJiri Olsa 	record__write(rec, map, &pad, padding);
628ef149c25SAdrian Hunter 
629ef149c25SAdrian Hunter 	return 0;
630ef149c25SAdrian Hunter }
631ef149c25SAdrian Hunter 
632ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec,
633a5830532SJiri Olsa 				      struct mmap *map)
634ef149c25SAdrian Hunter {
635ef149c25SAdrian Hunter 	int ret;
636ef149c25SAdrian Hunter 
637e035f4caSJiri Olsa 	ret = auxtrace_mmap__read(map, rec->itr, &rec->tool,
638ef149c25SAdrian Hunter 				  record__process_auxtrace);
639ef149c25SAdrian Hunter 	if (ret < 0)
640ef149c25SAdrian Hunter 		return ret;
641ef149c25SAdrian Hunter 
642ef149c25SAdrian Hunter 	if (ret)
643ef149c25SAdrian Hunter 		rec->samples++;
644ef149c25SAdrian Hunter 
645ef149c25SAdrian Hunter 	return 0;
646ef149c25SAdrian Hunter }
647ef149c25SAdrian Hunter 
6482dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec,
649a5830532SJiri Olsa 					       struct mmap *map)
6502dd6d8a1SAdrian Hunter {
6512dd6d8a1SAdrian Hunter 	int ret;
6522dd6d8a1SAdrian Hunter 
653e035f4caSJiri Olsa 	ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool,
6542dd6d8a1SAdrian Hunter 					   record__process_auxtrace,
6552dd6d8a1SAdrian Hunter 					   rec->opts.auxtrace_snapshot_size);
6562dd6d8a1SAdrian Hunter 	if (ret < 0)
6572dd6d8a1SAdrian Hunter 		return ret;
6582dd6d8a1SAdrian Hunter 
6592dd6d8a1SAdrian Hunter 	if (ret)
6602dd6d8a1SAdrian Hunter 		rec->samples++;
6612dd6d8a1SAdrian Hunter 
6622dd6d8a1SAdrian Hunter 	return 0;
6632dd6d8a1SAdrian Hunter }
6642dd6d8a1SAdrian Hunter 
6652dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec)
6662dd6d8a1SAdrian Hunter {
6672dd6d8a1SAdrian Hunter 	int i;
6682dd6d8a1SAdrian Hunter 	int rc = 0;
6692dd6d8a1SAdrian Hunter 
670c976ee11SJiri Olsa 	for (i = 0; i < rec->evlist->core.nr_mmaps; i++) {
671a5830532SJiri Olsa 		struct mmap *map = &rec->evlist->mmap[i];
6722dd6d8a1SAdrian Hunter 
673e035f4caSJiri Olsa 		if (!map->auxtrace_mmap.base)
6742dd6d8a1SAdrian Hunter 			continue;
6752dd6d8a1SAdrian Hunter 
676e035f4caSJiri Olsa 		if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) {
6772dd6d8a1SAdrian Hunter 			rc = -1;
6782dd6d8a1SAdrian Hunter 			goto out;
6792dd6d8a1SAdrian Hunter 		}
6802dd6d8a1SAdrian Hunter 	}
6812dd6d8a1SAdrian Hunter out:
6822dd6d8a1SAdrian Hunter 	return rc;
6832dd6d8a1SAdrian Hunter }
6842dd6d8a1SAdrian Hunter 
685ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit)
6862dd6d8a1SAdrian Hunter {
6872dd6d8a1SAdrian Hunter 	pr_debug("Recording AUX area tracing snapshot\n");
6882dd6d8a1SAdrian Hunter 	if (record__auxtrace_read_snapshot_all(rec) < 0) {
6895f9cf599SWang Nan 		trigger_error(&auxtrace_snapshot_trigger);
6902dd6d8a1SAdrian Hunter 	} else {
691ce7b0e42SAlexander Shishkin 		if (auxtrace_record__snapshot_finish(rec->itr, on_exit))
6925f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
6935f9cf599SWang Nan 		else
6945f9cf599SWang Nan 			trigger_ready(&auxtrace_snapshot_trigger);
6952dd6d8a1SAdrian Hunter 	}
6962dd6d8a1SAdrian Hunter }
6972dd6d8a1SAdrian Hunter 
698ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec)
699ce7b0e42SAlexander Shishkin {
700ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
701ce7b0e42SAlexander Shishkin 		return 0;
702ce7b0e42SAlexander Shishkin 
703ce7b0e42SAlexander Shishkin 	if (!auxtrace_record__snapshot_started &&
704ce7b0e42SAlexander Shishkin 	    auxtrace_record__snapshot_start(rec->itr))
705ce7b0e42SAlexander Shishkin 		return -1;
706ce7b0e42SAlexander Shishkin 
707ce7b0e42SAlexander Shishkin 	record__read_auxtrace_snapshot(rec, true);
708ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
709ce7b0e42SAlexander Shishkin 		return -1;
710ce7b0e42SAlexander Shishkin 
711ce7b0e42SAlexander Shishkin 	return 0;
712ce7b0e42SAlexander Shishkin }
713ce7b0e42SAlexander Shishkin 
7144b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec)
7154b5ea3bdSAdrian Hunter {
7164b5ea3bdSAdrian Hunter 	int err;
7174b5ea3bdSAdrian Hunter 
7184b5ea3bdSAdrian Hunter 	if (!rec->itr) {
7194b5ea3bdSAdrian Hunter 		rec->itr = auxtrace_record__init(rec->evlist, &err);
7204b5ea3bdSAdrian Hunter 		if (err)
7214b5ea3bdSAdrian Hunter 			return err;
7224b5ea3bdSAdrian Hunter 	}
7234b5ea3bdSAdrian Hunter 
7244b5ea3bdSAdrian Hunter 	err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
7254b5ea3bdSAdrian Hunter 					      rec->opts.auxtrace_snapshot_opts);
7264b5ea3bdSAdrian Hunter 	if (err)
7274b5ea3bdSAdrian Hunter 		return err;
7284b5ea3bdSAdrian Hunter 
729c0a6de06SAdrian Hunter 	err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts,
730c0a6de06SAdrian Hunter 					    rec->opts.auxtrace_sample_opts);
731c0a6de06SAdrian Hunter 	if (err)
732c0a6de06SAdrian Hunter 		return err;
733c0a6de06SAdrian Hunter 
7344b5ea3bdSAdrian Hunter 	return auxtrace_parse_filters(rec->evlist);
7354b5ea3bdSAdrian Hunter }
7364b5ea3bdSAdrian Hunter 
737e31f0d01SAdrian Hunter #else
738e31f0d01SAdrian Hunter 
739e31f0d01SAdrian Hunter static inline
740e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
741a5830532SJiri Olsa 			       struct mmap *map __maybe_unused)
742e31f0d01SAdrian Hunter {
743e31f0d01SAdrian Hunter 	return 0;
744e31f0d01SAdrian Hunter }
745e31f0d01SAdrian Hunter 
7462dd6d8a1SAdrian Hunter static inline
747ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused,
748ce7b0e42SAlexander Shishkin 				    bool on_exit __maybe_unused)
7492dd6d8a1SAdrian Hunter {
7502dd6d8a1SAdrian Hunter }
7512dd6d8a1SAdrian Hunter 
7522dd6d8a1SAdrian Hunter static inline
7532dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
7542dd6d8a1SAdrian Hunter {
7552dd6d8a1SAdrian Hunter 	return 0;
7562dd6d8a1SAdrian Hunter }
7572dd6d8a1SAdrian Hunter 
758ce7b0e42SAlexander Shishkin static inline
759ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused)
760ce7b0e42SAlexander Shishkin {
761ce7b0e42SAlexander Shishkin 	return 0;
762ce7b0e42SAlexander Shishkin }
763ce7b0e42SAlexander Shishkin 
7644b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused)
7654b5ea3bdSAdrian Hunter {
7664b5ea3bdSAdrian Hunter 	return 0;
7674b5ea3bdSAdrian Hunter }
7684b5ea3bdSAdrian Hunter 
769e31f0d01SAdrian Hunter #endif
770e31f0d01SAdrian Hunter 
771246eba8eSAdrian Hunter static int record__config_text_poke(struct evlist *evlist)
772246eba8eSAdrian Hunter {
773246eba8eSAdrian Hunter 	struct evsel *evsel;
774246eba8eSAdrian Hunter 	int err;
775246eba8eSAdrian Hunter 
776246eba8eSAdrian Hunter 	/* Nothing to do if text poke is already configured */
777246eba8eSAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
778246eba8eSAdrian Hunter 		if (evsel->core.attr.text_poke)
779246eba8eSAdrian Hunter 			return 0;
780246eba8eSAdrian Hunter 	}
781246eba8eSAdrian Hunter 
782246eba8eSAdrian Hunter 	err = parse_events(evlist, "dummy:u", NULL);
783246eba8eSAdrian Hunter 	if (err)
784246eba8eSAdrian Hunter 		return err;
785246eba8eSAdrian Hunter 
786246eba8eSAdrian Hunter 	evsel = evlist__last(evlist);
787246eba8eSAdrian Hunter 
788246eba8eSAdrian Hunter 	evsel->core.attr.freq = 0;
789246eba8eSAdrian Hunter 	evsel->core.attr.sample_period = 1;
790246eba8eSAdrian Hunter 	evsel->core.attr.text_poke = 1;
791246eba8eSAdrian Hunter 	evsel->core.attr.ksymbol = 1;
792246eba8eSAdrian Hunter 
793246eba8eSAdrian Hunter 	evsel->core.system_wide = true;
794246eba8eSAdrian Hunter 	evsel->no_aux_samples = true;
795246eba8eSAdrian Hunter 	evsel->immediate = true;
796246eba8eSAdrian Hunter 
797246eba8eSAdrian Hunter 	/* Text poke must be collected on all CPUs */
798246eba8eSAdrian Hunter 	perf_cpu_map__put(evsel->core.own_cpus);
799246eba8eSAdrian Hunter 	evsel->core.own_cpus = perf_cpu_map__new(NULL);
800246eba8eSAdrian Hunter 	perf_cpu_map__put(evsel->core.cpus);
801246eba8eSAdrian Hunter 	evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus);
802246eba8eSAdrian Hunter 
803246eba8eSAdrian Hunter 	evsel__set_sample_bit(evsel, TIME);
804246eba8eSAdrian Hunter 
805246eba8eSAdrian Hunter 	return 0;
806246eba8eSAdrian Hunter }
807246eba8eSAdrian Hunter 
808eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine)
809eeb399b5SAdrian Hunter {
810eeb399b5SAdrian Hunter 	char kcore[PATH_MAX];
811eeb399b5SAdrian Hunter 	int fd;
812eeb399b5SAdrian Hunter 
813eeb399b5SAdrian Hunter 	scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir);
814eeb399b5SAdrian Hunter 
815eeb399b5SAdrian Hunter 	fd = open(kcore, O_RDONLY);
816eeb399b5SAdrian Hunter 	if (fd < 0)
817eeb399b5SAdrian Hunter 		return false;
818eeb399b5SAdrian Hunter 
819eeb399b5SAdrian Hunter 	close(fd);
820eeb399b5SAdrian Hunter 
821eeb399b5SAdrian Hunter 	return true;
822eeb399b5SAdrian Hunter }
823eeb399b5SAdrian Hunter 
824eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data)
825eeb399b5SAdrian Hunter {
826eeb399b5SAdrian Hunter 	char from_dir[PATH_MAX];
827eeb399b5SAdrian Hunter 	char kcore_dir[PATH_MAX];
828eeb399b5SAdrian Hunter 	int ret;
829eeb399b5SAdrian Hunter 
830eeb399b5SAdrian Hunter 	snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir);
831eeb399b5SAdrian Hunter 
832eeb399b5SAdrian Hunter 	ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir));
833eeb399b5SAdrian Hunter 	if (ret)
834eeb399b5SAdrian Hunter 		return ret;
835eeb399b5SAdrian Hunter 
836eeb399b5SAdrian Hunter 	return kcore_copy(from_dir, kcore_dir);
837eeb399b5SAdrian Hunter }
838eeb399b5SAdrian Hunter 
839cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec,
84063503dbaSJiri Olsa 			       struct evlist *evlist)
841cda57a8cSWang Nan {
842cda57a8cSWang Nan 	struct record_opts *opts = &rec->opts;
843c0a6de06SAdrian Hunter 	bool auxtrace_overwrite = opts->auxtrace_snapshot_mode ||
844c0a6de06SAdrian Hunter 				  opts->auxtrace_sample_mode;
845cda57a8cSWang Nan 	char msg[512];
846cda57a8cSWang Nan 
847f13de660SAlexey Budankov 	if (opts->affinity != PERF_AFFINITY_SYS)
848f13de660SAlexey Budankov 		cpu__setup_cpunode_map();
849f13de660SAlexey Budankov 
8509521b5f2SJiri Olsa 	if (evlist__mmap_ex(evlist, opts->mmap_pages,
851cda57a8cSWang Nan 				 opts->auxtrace_mmap_pages,
852c0a6de06SAdrian Hunter 				 auxtrace_overwrite,
853470530bbSAlexey Budankov 				 opts->nr_cblocks, opts->affinity,
85451255a8aSAlexey Budankov 				 opts->mmap_flush, opts->comp_level) < 0) {
855cda57a8cSWang Nan 		if (errno == EPERM) {
856cda57a8cSWang Nan 			pr_err("Permission error mapping pages.\n"
857cda57a8cSWang Nan 			       "Consider increasing "
858cda57a8cSWang Nan 			       "/proc/sys/kernel/perf_event_mlock_kb,\n"
859cda57a8cSWang Nan 			       "or try again with a smaller value of -m/--mmap_pages.\n"
860cda57a8cSWang Nan 			       "(current value: %u,%u)\n",
861cda57a8cSWang Nan 			       opts->mmap_pages, opts->auxtrace_mmap_pages);
862cda57a8cSWang Nan 			return -errno;
863cda57a8cSWang Nan 		} else {
864cda57a8cSWang Nan 			pr_err("failed to mmap with %d (%s)\n", errno,
865c8b5f2c9SArnaldo Carvalho de Melo 				str_error_r(errno, msg, sizeof(msg)));
866cda57a8cSWang Nan 			if (errno)
867cda57a8cSWang Nan 				return -errno;
868cda57a8cSWang Nan 			else
869cda57a8cSWang Nan 				return -EINVAL;
870cda57a8cSWang Nan 		}
871cda57a8cSWang Nan 	}
872cda57a8cSWang Nan 	return 0;
873cda57a8cSWang Nan }
874cda57a8cSWang Nan 
875cda57a8cSWang Nan static int record__mmap(struct record *rec)
876cda57a8cSWang Nan {
877cda57a8cSWang Nan 	return record__mmap_evlist(rec, rec->evlist);
878cda57a8cSWang Nan }
879cda57a8cSWang Nan 
8808c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec)
881dd7927f4SArnaldo Carvalho de Melo {
882d6195a6aSArnaldo Carvalho de Melo 	char msg[BUFSIZ];
88332dcd021SJiri Olsa 	struct evsel *pos;
88463503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
885d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session = rec->session;
886b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
8878d3eca20SDavid Ahern 	int rc = 0;
888dd7927f4SArnaldo Carvalho de Melo 
889d3dbf43cSArnaldo Carvalho de Melo 	/*
8900a892c1cSIan Rogers 	 * For initial_delay or system wide, we need to add a dummy event so
8910a892c1cSIan Rogers 	 * that we can track PERF_RECORD_MMAP to cover the delay of waiting or
8920a892c1cSIan Rogers 	 * event synthesis.
893d3dbf43cSArnaldo Carvalho de Melo 	 */
8940a892c1cSIan Rogers 	if (opts->initial_delay || target__has_cpu(&opts->target)) {
895e80db255SArnaldo Carvalho de Melo 		pos = evlist__get_tracking_event(evlist);
896442ad225SAdrian Hunter 		if (!evsel__is_dummy_event(pos)) {
897442ad225SAdrian Hunter 			/* Set up dummy event. */
898e251abeeSArnaldo Carvalho de Melo 			if (evlist__add_dummy(evlist))
899d3dbf43cSArnaldo Carvalho de Melo 				return -ENOMEM;
900515dbe48SJiri Olsa 			pos = evlist__last(evlist);
901e80db255SArnaldo Carvalho de Melo 			evlist__set_tracking_event(evlist, pos);
902442ad225SAdrian Hunter 		}
903442ad225SAdrian Hunter 
9040a892c1cSIan Rogers 		/*
9050a892c1cSIan Rogers 		 * Enable the dummy event when the process is forked for
9060a892c1cSIan Rogers 		 * initial_delay, immediately for system wide.
9070a892c1cSIan Rogers 		 */
908442ad225SAdrian Hunter 		if (opts->initial_delay && !pos->immediate)
9091fc632ceSJiri Olsa 			pos->core.attr.enable_on_exec = 1;
9100a892c1cSIan Rogers 		else
9110a892c1cSIan Rogers 			pos->immediate = 1;
912d3dbf43cSArnaldo Carvalho de Melo 	}
913d3dbf43cSArnaldo Carvalho de Melo 
91478e1bc25SArnaldo Carvalho de Melo 	evlist__config(evlist, opts, &callchain_param);
915cac21425SJiri Olsa 
916e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
9173da297a6SIngo Molnar try_again:
918af663bd0SJiri Olsa 		if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
919ae430892SArnaldo Carvalho de Melo 			if (evsel__fallback(pos, errno, msg, sizeof(msg))) {
920bb963e16SNamhyung Kim 				if (verbose > 0)
921c0a54341SArnaldo Carvalho de Melo 					ui__warning("%s\n", msg);
9223da297a6SIngo Molnar 				goto try_again;
9233da297a6SIngo Molnar 			}
924cf99ad14SAndi Kleen 			if ((errno == EINVAL || errno == EBADF) &&
925cf99ad14SAndi Kleen 			    pos->leader != pos &&
926cf99ad14SAndi Kleen 			    pos->weak_group) {
92764b4778bSArnaldo Carvalho de Melo 			        pos = evlist__reset_weak_group(evlist, pos, true);
928cf99ad14SAndi Kleen 				goto try_again;
929cf99ad14SAndi Kleen 			}
93056e52e85SArnaldo Carvalho de Melo 			rc = -errno;
9312bb72dbbSArnaldo Carvalho de Melo 			evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg));
93256e52e85SArnaldo Carvalho de Melo 			ui__error("%s\n", msg);
9338d3eca20SDavid Ahern 			goto out;
9347c6a1c65SPeter Zijlstra 		}
935bfd8f72cSAndi Kleen 
936bfd8f72cSAndi Kleen 		pos->supported = true;
9377c6a1c65SPeter Zijlstra 	}
9387c6a1c65SPeter Zijlstra 
93978e1bc25SArnaldo Carvalho de Melo 	if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) {
940c8b567c8SArnaldo Carvalho de Melo 		pr_warning(
941c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
942c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
943c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
944c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n"
945c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n"
946c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
947c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n");
948c8b567c8SArnaldo Carvalho de Melo 	}
949c8b567c8SArnaldo Carvalho de Melo 
95024bf91a7SArnaldo Carvalho de Melo 	if (evlist__apply_filters(evlist, &pos)) {
95162d94b00SArnaldo Carvalho de Melo 		pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
9528ab2e96dSArnaldo Carvalho de Melo 			pos->filter, evsel__name(pos), errno,
953c8b5f2c9SArnaldo Carvalho de Melo 			str_error_r(errno, msg, sizeof(msg)));
9548d3eca20SDavid Ahern 		rc = -1;
9558d3eca20SDavid Ahern 		goto out;
9560a102479SFrederic Weisbecker 	}
9570a102479SFrederic Weisbecker 
958cda57a8cSWang Nan 	rc = record__mmap(rec);
959cda57a8cSWang Nan 	if (rc)
9608d3eca20SDavid Ahern 		goto out;
9610a27d7f9SArnaldo Carvalho de Melo 
962a91e5431SArnaldo Carvalho de Melo 	session->evlist = evlist;
9637b56cce2SArnaldo Carvalho de Melo 	perf_session__set_id_hdr_size(session);
9648d3eca20SDavid Ahern out:
9658d3eca20SDavid Ahern 	return rc;
966a91e5431SArnaldo Carvalho de Melo }
967a91e5431SArnaldo Carvalho de Melo 
968e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool,
969e3d59112SNamhyung Kim 				union perf_event *event,
970e3d59112SNamhyung Kim 				struct perf_sample *sample,
97132dcd021SJiri Olsa 				struct evsel *evsel,
972e3d59112SNamhyung Kim 				struct machine *machine)
973e3d59112SNamhyung Kim {
974e3d59112SNamhyung Kim 	struct record *rec = container_of(tool, struct record, tool);
975e3d59112SNamhyung Kim 
97668588bafSJin Yao 	if (rec->evlist->first_sample_time == 0)
97768588bafSJin Yao 		rec->evlist->first_sample_time = sample->time;
978e3d59112SNamhyung Kim 
97968588bafSJin Yao 	rec->evlist->last_sample_time = sample->time;
98068588bafSJin Yao 
98168588bafSJin Yao 	if (rec->buildid_all)
98268588bafSJin Yao 		return 0;
98368588bafSJin Yao 
98468588bafSJin Yao 	rec->samples++;
985e3d59112SNamhyung Kim 	return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
986e3d59112SNamhyung Kim }
987e3d59112SNamhyung Kim 
9888c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec)
9896122e4e4SArnaldo Carvalho de Melo {
990f5fc1412SJiri Olsa 	struct perf_session *session = rec->session;
9916122e4e4SArnaldo Carvalho de Melo 
99245112e89SJiri Olsa 	if (perf_data__size(&rec->data) == 0)
9939f591fd7SArnaldo Carvalho de Melo 		return 0;
9949f591fd7SArnaldo Carvalho de Melo 
99500dc8657SNamhyung Kim 	/*
99600dc8657SNamhyung Kim 	 * During this process, it'll load kernel map and replace the
99700dc8657SNamhyung Kim 	 * dso->long_name to a real pathname it found.  In this case
99800dc8657SNamhyung Kim 	 * we prefer the vmlinux path like
99900dc8657SNamhyung Kim 	 *   /lib/modules/3.16.4/build/vmlinux
100000dc8657SNamhyung Kim 	 *
100100dc8657SNamhyung Kim 	 * rather than build-id path (in debug directory).
100200dc8657SNamhyung Kim 	 *   $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
100300dc8657SNamhyung Kim 	 */
100400dc8657SNamhyung Kim 	symbol_conf.ignore_vmlinux_buildid = true;
100500dc8657SNamhyung Kim 
10066156681bSNamhyung Kim 	/*
10076156681bSNamhyung Kim 	 * If --buildid-all is given, it marks all DSO regardless of hits,
100868588bafSJin Yao 	 * so no need to process samples. But if timestamp_boundary is enabled,
100968588bafSJin Yao 	 * it still needs to walk on all samples to get the timestamps of
101068588bafSJin Yao 	 * first/last samples.
10116156681bSNamhyung Kim 	 */
101268588bafSJin Yao 	if (rec->buildid_all && !rec->timestamp_boundary)
10136156681bSNamhyung Kim 		rec->tool.sample = NULL;
10146156681bSNamhyung Kim 
1015b7b61cbeSArnaldo Carvalho de Melo 	return perf_session__process_events(session);
10166122e4e4SArnaldo Carvalho de Melo }
10176122e4e4SArnaldo Carvalho de Melo 
10188115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
1019a1645ce1SZhang, Yanmin {
1020a1645ce1SZhang, Yanmin 	int err;
102145694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = data;
1022a1645ce1SZhang, Yanmin 	/*
1023a1645ce1SZhang, Yanmin 	 *As for guest kernel when processing subcommand record&report,
1024a1645ce1SZhang, Yanmin 	 *we arrange module mmap prior to guest kernel mmap and trigger
1025a1645ce1SZhang, Yanmin 	 *a preload dso because default guest module symbols are loaded
1026a1645ce1SZhang, Yanmin 	 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
1027a1645ce1SZhang, Yanmin 	 *method is used to avoid symbol missing when the first addr is
1028a1645ce1SZhang, Yanmin 	 *in module instead of in guest kernel.
1029a1645ce1SZhang, Yanmin 	 */
103045694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_modules(tool, process_synthesized_event,
1031743eb868SArnaldo Carvalho de Melo 					     machine);
1032a1645ce1SZhang, Yanmin 	if (err < 0)
1033a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
103423346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
1035a1645ce1SZhang, Yanmin 
1036a1645ce1SZhang, Yanmin 	/*
1037a1645ce1SZhang, Yanmin 	 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
1038a1645ce1SZhang, Yanmin 	 * have no _text sometimes.
1039a1645ce1SZhang, Yanmin 	 */
104045694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
10410ae617beSAdrian Hunter 						 machine);
1042a1645ce1SZhang, Yanmin 	if (err < 0)
1043a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
104423346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
1045a1645ce1SZhang, Yanmin }
1046a1645ce1SZhang, Yanmin 
104798402807SFrederic Weisbecker static struct perf_event_header finished_round_event = {
104898402807SFrederic Weisbecker 	.size = sizeof(struct perf_event_header),
104998402807SFrederic Weisbecker 	.type = PERF_RECORD_FINISHED_ROUND,
105098402807SFrederic Weisbecker };
105198402807SFrederic Weisbecker 
1052a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map)
1053f13de660SAlexey Budankov {
1054f13de660SAlexey Budankov 	if (rec->opts.affinity != PERF_AFFINITY_SYS &&
10558384a260SAlexey Budankov 	    !bitmap_equal(rec->affinity_mask.bits, map->affinity_mask.bits,
10568384a260SAlexey Budankov 			  rec->affinity_mask.nbits)) {
10578384a260SAlexey Budankov 		bitmap_zero(rec->affinity_mask.bits, rec->affinity_mask.nbits);
10588384a260SAlexey Budankov 		bitmap_or(rec->affinity_mask.bits, rec->affinity_mask.bits,
10598384a260SAlexey Budankov 			  map->affinity_mask.bits, rec->affinity_mask.nbits);
10608384a260SAlexey Budankov 		sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&rec->affinity_mask),
10618384a260SAlexey Budankov 				  (cpu_set_t *)rec->affinity_mask.bits);
10628384a260SAlexey Budankov 		if (verbose == 2)
10638384a260SAlexey Budankov 			mmap_cpu_mask__scnprintf(&rec->affinity_mask, "thread");
1064f13de660SAlexey Budankov 	}
1065f13de660SAlexey Budankov }
1066f13de660SAlexey Budankov 
10675d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment)
10685d7f4116SAlexey Budankov {
106972932371SJiri Olsa 	struct perf_record_compressed *event = record;
10705d7f4116SAlexey Budankov 	size_t size = sizeof(*event);
10715d7f4116SAlexey Budankov 
10725d7f4116SAlexey Budankov 	if (increment) {
10735d7f4116SAlexey Budankov 		event->header.size += increment;
10745d7f4116SAlexey Budankov 		return increment;
10755d7f4116SAlexey Budankov 	}
10765d7f4116SAlexey Budankov 
10775d7f4116SAlexey Budankov 	event->header.type = PERF_RECORD_COMPRESSED;
10785d7f4116SAlexey Budankov 	event->header.size = size;
10795d7f4116SAlexey Budankov 
10805d7f4116SAlexey Budankov 	return size;
10815d7f4116SAlexey Budankov }
10825d7f4116SAlexey Budankov 
10835d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size,
10845d7f4116SAlexey Budankov 			    void *src, size_t src_size)
10855d7f4116SAlexey Budankov {
10865d7f4116SAlexey Budankov 	size_t compressed;
108772932371SJiri Olsa 	size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
10885d7f4116SAlexey Budankov 
10895d7f4116SAlexey Budankov 	compressed = zstd_compress_stream_to_records(&session->zstd_data, dst, dst_size, src, src_size,
10905d7f4116SAlexey Budankov 						     max_record_size, process_comp_header);
10915d7f4116SAlexey Budankov 
10925d7f4116SAlexey Budankov 	session->bytes_transferred += src_size;
10935d7f4116SAlexey Budankov 	session->bytes_compressed  += compressed;
10945d7f4116SAlexey Budankov 
10955d7f4116SAlexey Budankov 	return compressed;
10965d7f4116SAlexey Budankov }
10975d7f4116SAlexey Budankov 
109863503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist,
1099470530bbSAlexey Budankov 				    bool overwrite, bool synch)
110098402807SFrederic Weisbecker {
1101dcabb507SJiri Olsa 	u64 bytes_written = rec->bytes_written;
11020e2e63ddSPeter Zijlstra 	int i;
11038d3eca20SDavid Ahern 	int rc = 0;
1104a5830532SJiri Olsa 	struct mmap *maps;
1105d3d1af6fSAlexey Budankov 	int trace_fd = rec->data.file.fd;
1106ef781128SAlexey Budankov 	off_t off = 0;
110798402807SFrederic Weisbecker 
1108cb21686bSWang Nan 	if (!evlist)
1109cb21686bSWang Nan 		return 0;
1110ef149c25SAdrian Hunter 
11110b72d69aSWang Nan 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
1112a4ea0ec4SWang Nan 	if (!maps)
1113a4ea0ec4SWang Nan 		return 0;
1114cb21686bSWang Nan 
11150b72d69aSWang Nan 	if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
111654cc54deSWang Nan 		return 0;
111754cc54deSWang Nan 
1118d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1119d3d1af6fSAlexey Budankov 		off = record__aio_get_pos(trace_fd);
1120d3d1af6fSAlexey Budankov 
1121c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
1122470530bbSAlexey Budankov 		u64 flush = 0;
1123a5830532SJiri Olsa 		struct mmap *map = &maps[i];
1124a4ea0ec4SWang Nan 
1125547740f7SJiri Olsa 		if (map->core.base) {
1126f13de660SAlexey Budankov 			record__adjust_affinity(rec, map);
1127470530bbSAlexey Budankov 			if (synch) {
112865aa2e6bSJiri Olsa 				flush = map->core.flush;
112965aa2e6bSJiri Olsa 				map->core.flush = 1;
1130470530bbSAlexey Budankov 			}
1131d3d1af6fSAlexey Budankov 			if (!record__aio_enabled(rec)) {
1132ef781128SAlexey Budankov 				if (perf_mmap__push(map, rec, record__pushfn) < 0) {
1133470530bbSAlexey Budankov 					if (synch)
113465aa2e6bSJiri Olsa 						map->core.flush = flush;
11358d3eca20SDavid Ahern 					rc = -1;
11368d3eca20SDavid Ahern 					goto out;
11378d3eca20SDavid Ahern 				}
1138d3d1af6fSAlexey Budankov 			} else {
1139ef781128SAlexey Budankov 				if (record__aio_push(rec, map, &off) < 0) {
1140d3d1af6fSAlexey Budankov 					record__aio_set_pos(trace_fd, off);
1141470530bbSAlexey Budankov 					if (synch)
114265aa2e6bSJiri Olsa 						map->core.flush = flush;
1143d3d1af6fSAlexey Budankov 					rc = -1;
1144d3d1af6fSAlexey Budankov 					goto out;
1145d3d1af6fSAlexey Budankov 				}
1146d3d1af6fSAlexey Budankov 			}
1147470530bbSAlexey Budankov 			if (synch)
114865aa2e6bSJiri Olsa 				map->core.flush = flush;
11498d3eca20SDavid Ahern 		}
1150ef149c25SAdrian Hunter 
1151e035f4caSJiri Olsa 		if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
1152c0a6de06SAdrian Hunter 		    !rec->opts.auxtrace_sample_mode &&
1153e035f4caSJiri Olsa 		    record__auxtrace_mmap_read(rec, map) != 0) {
1154ef149c25SAdrian Hunter 			rc = -1;
1155ef149c25SAdrian Hunter 			goto out;
1156ef149c25SAdrian Hunter 		}
115798402807SFrederic Weisbecker 	}
115898402807SFrederic Weisbecker 
1159d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1160d3d1af6fSAlexey Budankov 		record__aio_set_pos(trace_fd, off);
1161d3d1af6fSAlexey Budankov 
1162dcabb507SJiri Olsa 	/*
1163dcabb507SJiri Olsa 	 * Mark the round finished in case we wrote
1164dcabb507SJiri Olsa 	 * at least one event.
1165dcabb507SJiri Olsa 	 */
1166dcabb507SJiri Olsa 	if (bytes_written != rec->bytes_written)
1167ded2b8feSJiri Olsa 		rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event));
11688d3eca20SDavid Ahern 
11690b72d69aSWang Nan 	if (overwrite)
1170ade9d208SArnaldo Carvalho de Melo 		evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
11718d3eca20SDavid Ahern out:
11728d3eca20SDavid Ahern 	return rc;
117398402807SFrederic Weisbecker }
117498402807SFrederic Weisbecker 
1175470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch)
1176cb21686bSWang Nan {
1177cb21686bSWang Nan 	int err;
1178cb21686bSWang Nan 
1179470530bbSAlexey Budankov 	err = record__mmap_read_evlist(rec, rec->evlist, false, synch);
1180cb21686bSWang Nan 	if (err)
1181cb21686bSWang Nan 		return err;
1182cb21686bSWang Nan 
1183470530bbSAlexey Budankov 	return record__mmap_read_evlist(rec, rec->evlist, true, synch);
1184cb21686bSWang Nan }
1185cb21686bSWang Nan 
11868c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec)
118757706abcSDavid Ahern {
118857706abcSDavid Ahern 	struct perf_session *session = rec->session;
118957706abcSDavid Ahern 	int feat;
119057706abcSDavid Ahern 
119157706abcSDavid Ahern 	for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
119257706abcSDavid Ahern 		perf_header__set_feat(&session->header, feat);
119357706abcSDavid Ahern 
119457706abcSDavid Ahern 	if (rec->no_buildid)
119557706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
119657706abcSDavid Ahern 
1197ce9036a6SJiri Olsa 	if (!have_tracepoints(&rec->evlist->core.entries))
119857706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
119957706abcSDavid Ahern 
120057706abcSDavid Ahern 	if (!rec->opts.branch_stack)
120157706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1202ef149c25SAdrian Hunter 
1203ef149c25SAdrian Hunter 	if (!rec->opts.full_auxtrace)
1204ef149c25SAdrian Hunter 		perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1205ffa517adSJiri Olsa 
1206cf790516SAlexey Budankov 	if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns))
1207cf790516SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_CLOCKID);
1208cf790516SAlexey Budankov 
1209d1e325cfSJiri Olsa 	if (!rec->opts.use_clockid)
1210d1e325cfSJiri Olsa 		perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA);
1211d1e325cfSJiri Olsa 
1212258031c0SJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
121342e1fd80SAlexey Budankov 	if (!record__comp_enabled(rec))
121442e1fd80SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_COMPRESSED);
1215258031c0SJiri Olsa 
1216ffa517adSJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_STAT);
121757706abcSDavid Ahern }
121857706abcSDavid Ahern 
1219e1ab48baSWang Nan static void
1220e1ab48baSWang Nan record__finish_output(struct record *rec)
1221e1ab48baSWang Nan {
12228ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
12238ceb41d7SJiri Olsa 	int fd = perf_data__fd(data);
1224e1ab48baSWang Nan 
12258ceb41d7SJiri Olsa 	if (data->is_pipe)
1226e1ab48baSWang Nan 		return;
1227e1ab48baSWang Nan 
1228e1ab48baSWang Nan 	rec->session->header.data_size += rec->bytes_written;
122945112e89SJiri Olsa 	data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR);
1230e1ab48baSWang Nan 
1231e1ab48baSWang Nan 	if (!rec->no_buildid) {
1232e1ab48baSWang Nan 		process_buildids(rec);
1233e1ab48baSWang Nan 
1234e1ab48baSWang Nan 		if (rec->buildid_all)
1235e1ab48baSWang Nan 			dsos__hit_all(rec->session);
1236e1ab48baSWang Nan 	}
1237e1ab48baSWang Nan 	perf_session__write_header(rec->session, rec->evlist, fd, true);
1238e1ab48baSWang Nan 
1239e1ab48baSWang Nan 	return;
1240e1ab48baSWang Nan }
1241e1ab48baSWang Nan 
12424ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail)
1243be7b0c9eSWang Nan {
12449d6aae72SArnaldo Carvalho de Melo 	int err;
12459749b90eSJiri Olsa 	struct perf_thread_map *thread_map;
1246be7b0c9eSWang Nan 
12474ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
12484ea648aeSWang Nan 		return 0;
12494ea648aeSWang Nan 
12509d6aae72SArnaldo Carvalho de Melo 	thread_map = thread_map__new_by_tid(rec->evlist->workload.pid);
12519d6aae72SArnaldo Carvalho de Melo 	if (thread_map == NULL)
12529d6aae72SArnaldo Carvalho de Melo 		return -1;
12539d6aae72SArnaldo Carvalho de Melo 
12549d6aae72SArnaldo Carvalho de Melo 	err = perf_event__synthesize_thread_map(&rec->tool, thread_map,
1255be7b0c9eSWang Nan 						 process_synthesized_event,
1256be7b0c9eSWang Nan 						 &rec->session->machines.host,
12573fcb10e4SMark Drayton 						 rec->opts.sample_address);
12587836e52eSJiri Olsa 	perf_thread_map__put(thread_map);
12599d6aae72SArnaldo Carvalho de Melo 	return err;
1260be7b0c9eSWang Nan }
1261be7b0c9eSWang Nan 
12624ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail);
12633c1cb7e3SWang Nan 
1264ecfd7a9cSWang Nan static int
1265ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit)
1266ecfd7a9cSWang Nan {
12678ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1268ecfd7a9cSWang Nan 	int fd, err;
126903724b2eSAndi Kleen 	char *new_filename;
1270ecfd7a9cSWang Nan 
1271ecfd7a9cSWang Nan 	/* Same Size:      "2015122520103046"*/
1272ecfd7a9cSWang Nan 	char timestamp[] = "InvalidTimestamp";
1273ecfd7a9cSWang Nan 
1274d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
1275d3d1af6fSAlexey Budankov 
12764ea648aeSWang Nan 	record__synthesize(rec, true);
12774ea648aeSWang Nan 	if (target__none(&rec->opts.target))
12784ea648aeSWang Nan 		record__synthesize_workload(rec, true);
12794ea648aeSWang Nan 
1280ecfd7a9cSWang Nan 	rec->samples = 0;
1281ecfd7a9cSWang Nan 	record__finish_output(rec);
1282ecfd7a9cSWang Nan 	err = fetch_current_timestamp(timestamp, sizeof(timestamp));
1283ecfd7a9cSWang Nan 	if (err) {
1284ecfd7a9cSWang Nan 		pr_err("Failed to get current timestamp\n");
1285ecfd7a9cSWang Nan 		return -EINVAL;
1286ecfd7a9cSWang Nan 	}
1287ecfd7a9cSWang Nan 
12888ceb41d7SJiri Olsa 	fd = perf_data__switch(data, timestamp,
1289ecfd7a9cSWang Nan 				    rec->session->header.data_offset,
129003724b2eSAndi Kleen 				    at_exit, &new_filename);
1291ecfd7a9cSWang Nan 	if (fd >= 0 && !at_exit) {
1292ecfd7a9cSWang Nan 		rec->bytes_written = 0;
1293ecfd7a9cSWang Nan 		rec->session->header.data_size = 0;
1294ecfd7a9cSWang Nan 	}
1295ecfd7a9cSWang Nan 
1296ecfd7a9cSWang Nan 	if (!quiet)
1297ecfd7a9cSWang Nan 		fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
12982d4f2799SJiri Olsa 			data->path, timestamp);
12993c1cb7e3SWang Nan 
130003724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
130103724b2eSAndi Kleen 		int n = rec->switch_output.cur_file + 1;
130203724b2eSAndi Kleen 
130303724b2eSAndi Kleen 		if (n >= rec->switch_output.num_files)
130403724b2eSAndi Kleen 			n = 0;
130503724b2eSAndi Kleen 		rec->switch_output.cur_file = n;
130603724b2eSAndi Kleen 		if (rec->switch_output.filenames[n]) {
130703724b2eSAndi Kleen 			remove(rec->switch_output.filenames[n]);
1308d8f9da24SArnaldo Carvalho de Melo 			zfree(&rec->switch_output.filenames[n]);
130903724b2eSAndi Kleen 		}
131003724b2eSAndi Kleen 		rec->switch_output.filenames[n] = new_filename;
131103724b2eSAndi Kleen 	} else {
131203724b2eSAndi Kleen 		free(new_filename);
131303724b2eSAndi Kleen 	}
131403724b2eSAndi Kleen 
13153c1cb7e3SWang Nan 	/* Output tracking events */
1316be7b0c9eSWang Nan 	if (!at_exit) {
13174ea648aeSWang Nan 		record__synthesize(rec, false);
13183c1cb7e3SWang Nan 
1319be7b0c9eSWang Nan 		/*
1320be7b0c9eSWang Nan 		 * In 'perf record --switch-output' without -a,
1321be7b0c9eSWang Nan 		 * record__synthesize() in record__switch_output() won't
1322be7b0c9eSWang Nan 		 * generate tracking events because there's no thread_map
1323be7b0c9eSWang Nan 		 * in evlist. Which causes newly created perf.data doesn't
1324be7b0c9eSWang Nan 		 * contain map and comm information.
1325be7b0c9eSWang Nan 		 * Create a fake thread_map and directly call
1326be7b0c9eSWang Nan 		 * perf_event__synthesize_thread_map() for those events.
1327be7b0c9eSWang Nan 		 */
1328be7b0c9eSWang Nan 		if (target__none(&rec->opts.target))
13294ea648aeSWang Nan 			record__synthesize_workload(rec, false);
1330be7b0c9eSWang Nan 	}
1331ecfd7a9cSWang Nan 	return fd;
1332ecfd7a9cSWang Nan }
1333ecfd7a9cSWang Nan 
1334f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno;
1335f33cbe72SArnaldo Carvalho de Melo 
1336f33cbe72SArnaldo Carvalho de Melo /*
13377b392ef0SArnaldo Carvalho de Melo  * evlist__prepare_workload will send a SIGUSR1
1338f33cbe72SArnaldo Carvalho de Melo  * if the fork fails, since we asked by setting its
1339f33cbe72SArnaldo Carvalho de Melo  * want_signal to true.
1340f33cbe72SArnaldo Carvalho de Melo  */
134145604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused,
134245604710SNamhyung Kim 					siginfo_t *info,
1343f33cbe72SArnaldo Carvalho de Melo 					void *ucontext __maybe_unused)
1344f33cbe72SArnaldo Carvalho de Melo {
1345f33cbe72SArnaldo Carvalho de Melo 	workload_exec_errno = info->si_value.sival_int;
1346f33cbe72SArnaldo Carvalho de Melo 	done = 1;
1347f33cbe72SArnaldo Carvalho de Melo 	child_finished = 1;
1348f33cbe72SArnaldo Carvalho de Melo }
1349f33cbe72SArnaldo Carvalho de Melo 
13502dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig);
1351bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig);
13522dd6d8a1SAdrian Hunter 
1353db0ea13cSArnaldo Carvalho de Melo static const struct perf_event_mmap_page *evlist__pick_pc(struct evlist *evlist)
1354ee667f94SWang Nan {
1355b2cb615dSWang Nan 	if (evlist) {
1356547740f7SJiri Olsa 		if (evlist->mmap && evlist->mmap[0].core.base)
1357547740f7SJiri Olsa 			return evlist->mmap[0].core.base;
1358547740f7SJiri Olsa 		if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base)
1359547740f7SJiri Olsa 			return evlist->overwrite_mmap[0].core.base;
1360b2cb615dSWang Nan 	}
1361ee667f94SWang Nan 	return NULL;
1362ee667f94SWang Nan }
1363ee667f94SWang Nan 
1364c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
1365c45628b0SWang Nan {
1366db0ea13cSArnaldo Carvalho de Melo 	const struct perf_event_mmap_page *pc = evlist__pick_pc(rec->evlist);
1367ee667f94SWang Nan 	if (pc)
1368ee667f94SWang Nan 		return pc;
1369c45628b0SWang Nan 	return NULL;
1370c45628b0SWang Nan }
1371c45628b0SWang Nan 
13724ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail)
1373c45c86ebSWang Nan {
1374c45c86ebSWang Nan 	struct perf_session *session = rec->session;
1375c45c86ebSWang Nan 	struct machine *machine = &session->machines.host;
13768ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1377c45c86ebSWang Nan 	struct record_opts *opts = &rec->opts;
1378c45c86ebSWang Nan 	struct perf_tool *tool = &rec->tool;
13798ceb41d7SJiri Olsa 	int fd = perf_data__fd(data);
1380c45c86ebSWang Nan 	int err = 0;
1381d99c22eaSStephane Eranian 	event_op f = process_synthesized_event;
1382c45c86ebSWang Nan 
13834ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
13844ea648aeSWang Nan 		return 0;
13854ea648aeSWang Nan 
13868ceb41d7SJiri Olsa 	if (data->is_pipe) {
1387a2015516SJiri Olsa 		/*
1388a2015516SJiri Olsa 		 * We need to synthesize events first, because some
1389a2015516SJiri Olsa 		 * features works on top of them (on report side).
1390a2015516SJiri Olsa 		 */
1391318ec184SJiri Olsa 		err = perf_event__synthesize_attrs(tool, rec->evlist,
1392c45c86ebSWang Nan 						   process_synthesized_event);
1393c45c86ebSWang Nan 		if (err < 0) {
1394c45c86ebSWang Nan 			pr_err("Couldn't synthesize attrs.\n");
1395c45c86ebSWang Nan 			goto out;
1396c45c86ebSWang Nan 		}
1397c45c86ebSWang Nan 
1398a2015516SJiri Olsa 		err = perf_event__synthesize_features(tool, session, rec->evlist,
1399a2015516SJiri Olsa 						      process_synthesized_event);
1400a2015516SJiri Olsa 		if (err < 0) {
1401a2015516SJiri Olsa 			pr_err("Couldn't synthesize features.\n");
1402a2015516SJiri Olsa 			return err;
1403a2015516SJiri Olsa 		}
1404a2015516SJiri Olsa 
1405ce9036a6SJiri Olsa 		if (have_tracepoints(&rec->evlist->core.entries)) {
1406c45c86ebSWang Nan 			/*
1407c45c86ebSWang Nan 			 * FIXME err <= 0 here actually means that
1408c45c86ebSWang Nan 			 * there were no tracepoints so its not really
1409c45c86ebSWang Nan 			 * an error, just that we don't need to
1410c45c86ebSWang Nan 			 * synthesize anything.  We really have to
1411c45c86ebSWang Nan 			 * return this more properly and also
1412c45c86ebSWang Nan 			 * propagate errors that now are calling die()
1413c45c86ebSWang Nan 			 */
1414c45c86ebSWang Nan 			err = perf_event__synthesize_tracing_data(tool,	fd, rec->evlist,
1415c45c86ebSWang Nan 								  process_synthesized_event);
1416c45c86ebSWang Nan 			if (err <= 0) {
1417c45c86ebSWang Nan 				pr_err("Couldn't record tracing data.\n");
1418c45c86ebSWang Nan 				goto out;
1419c45c86ebSWang Nan 			}
1420c45c86ebSWang Nan 			rec->bytes_written += err;
1421c45c86ebSWang Nan 		}
1422c45c86ebSWang Nan 	}
1423c45c86ebSWang Nan 
1424c45628b0SWang Nan 	err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
142546bc29b9SAdrian Hunter 					  process_synthesized_event, machine);
142646bc29b9SAdrian Hunter 	if (err)
142746bc29b9SAdrian Hunter 		goto out;
142846bc29b9SAdrian Hunter 
1429c0a6de06SAdrian Hunter 	/* Synthesize id_index before auxtrace_info */
1430c0a6de06SAdrian Hunter 	if (rec->opts.auxtrace_sample_mode) {
1431c0a6de06SAdrian Hunter 		err = perf_event__synthesize_id_index(tool,
1432c0a6de06SAdrian Hunter 						      process_synthesized_event,
1433c0a6de06SAdrian Hunter 						      session->evlist, machine);
1434c0a6de06SAdrian Hunter 		if (err)
1435c0a6de06SAdrian Hunter 			goto out;
1436c0a6de06SAdrian Hunter 	}
1437c0a6de06SAdrian Hunter 
1438c45c86ebSWang Nan 	if (rec->opts.full_auxtrace) {
1439c45c86ebSWang Nan 		err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
1440c45c86ebSWang Nan 					session, process_synthesized_event);
1441c45c86ebSWang Nan 		if (err)
1442c45c86ebSWang Nan 			goto out;
1443c45c86ebSWang Nan 	}
1444c45c86ebSWang Nan 
144578e1bc25SArnaldo Carvalho de Melo 	if (!evlist__exclude_kernel(rec->evlist)) {
1446c45c86ebSWang Nan 		err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
1447c45c86ebSWang Nan 							 machine);
1448c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
1449c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1450c45c86ebSWang Nan 				   "Check /proc/kallsyms permission or run as root.\n");
1451c45c86ebSWang Nan 
1452c45c86ebSWang Nan 		err = perf_event__synthesize_modules(tool, process_synthesized_event,
1453c45c86ebSWang Nan 						     machine);
1454c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
1455c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1456c45c86ebSWang Nan 				   "Check /proc/modules permission or run as root.\n");
14576c443954SArnaldo Carvalho de Melo 	}
1458c45c86ebSWang Nan 
1459c45c86ebSWang Nan 	if (perf_guest) {
1460c45c86ebSWang Nan 		machines__process_guests(&session->machines,
1461c45c86ebSWang Nan 					 perf_event__synthesize_guest_os, tool);
1462c45c86ebSWang Nan 	}
1463c45c86ebSWang Nan 
1464bfd8f72cSAndi Kleen 	err = perf_event__synthesize_extra_attr(&rec->tool,
1465bfd8f72cSAndi Kleen 						rec->evlist,
1466bfd8f72cSAndi Kleen 						process_synthesized_event,
1467bfd8f72cSAndi Kleen 						data->is_pipe);
1468bfd8f72cSAndi Kleen 	if (err)
1469bfd8f72cSAndi Kleen 		goto out;
1470bfd8f72cSAndi Kleen 
147103617c22SJiri Olsa 	err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads,
1472373565d2SAndi Kleen 						 process_synthesized_event,
1473373565d2SAndi Kleen 						NULL);
1474373565d2SAndi Kleen 	if (err < 0) {
1475373565d2SAndi Kleen 		pr_err("Couldn't synthesize thread map.\n");
1476373565d2SAndi Kleen 		return err;
1477373565d2SAndi Kleen 	}
1478373565d2SAndi Kleen 
1479f72f901dSJiri Olsa 	err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.cpus,
1480373565d2SAndi Kleen 					     process_synthesized_event, NULL);
1481373565d2SAndi Kleen 	if (err < 0) {
1482373565d2SAndi Kleen 		pr_err("Couldn't synthesize cpu map.\n");
1483373565d2SAndi Kleen 		return err;
1484373565d2SAndi Kleen 	}
1485373565d2SAndi Kleen 
1486e5416950SSong Liu 	err = perf_event__synthesize_bpf_events(session, process_synthesized_event,
14877b612e29SSong Liu 						machine, opts);
14887b612e29SSong Liu 	if (err < 0)
14897b612e29SSong Liu 		pr_warning("Couldn't synthesize bpf events.\n");
14907b612e29SSong Liu 
1491ab64069fSNamhyung Kim 	err = perf_event__synthesize_cgroups(tool, process_synthesized_event,
1492ab64069fSNamhyung Kim 					     machine);
1493ab64069fSNamhyung Kim 	if (err < 0)
1494ab64069fSNamhyung Kim 		pr_warning("Couldn't synthesize cgroup events.\n");
1495ab64069fSNamhyung Kim 
1496d99c22eaSStephane Eranian 	if (rec->opts.nr_threads_synthesize > 1) {
1497d99c22eaSStephane Eranian 		perf_set_multithreaded();
1498d99c22eaSStephane Eranian 		f = process_locked_synthesized_event;
1499d99c22eaSStephane Eranian 	}
1500d99c22eaSStephane Eranian 
150103617c22SJiri Olsa 	err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads,
1502d99c22eaSStephane Eranian 					    f, opts->sample_address,
1503d99c22eaSStephane Eranian 					    rec->opts.nr_threads_synthesize);
1504d99c22eaSStephane Eranian 
1505d99c22eaSStephane Eranian 	if (rec->opts.nr_threads_synthesize > 1)
1506d99c22eaSStephane Eranian 		perf_set_singlethreaded();
1507d99c22eaSStephane Eranian 
1508c45c86ebSWang Nan out:
1509c45c86ebSWang Nan 	return err;
1510c45c86ebSWang Nan }
1511c45c86ebSWang Nan 
1512899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data)
1513899e5ffbSArnaldo Carvalho de Melo {
1514899e5ffbSArnaldo Carvalho de Melo 	struct record *rec = data;
1515899e5ffbSArnaldo Carvalho de Melo 	pthread_kill(rec->thread_id, SIGUSR2);
1516899e5ffbSArnaldo Carvalho de Melo 	return 0;
1517899e5ffbSArnaldo Carvalho de Melo }
1518899e5ffbSArnaldo Carvalho de Melo 
151923cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec)
152023cbb41cSArnaldo Carvalho de Melo {
152123cbb41cSArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
152223cbb41cSArnaldo Carvalho de Melo 
152323cbb41cSArnaldo Carvalho de Melo 	if (rec->sb_evlist != NULL) {
152423cbb41cSArnaldo Carvalho de Melo 		/*
152523cbb41cSArnaldo Carvalho de Melo 		 * We get here if --switch-output-event populated the
152623cbb41cSArnaldo Carvalho de Melo 		 * sb_evlist, so associate a callback that will send a SIGUSR2
152723cbb41cSArnaldo Carvalho de Melo 		 * to the main thread.
152823cbb41cSArnaldo Carvalho de Melo 		 */
152923cbb41cSArnaldo Carvalho de Melo 		evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec);
153023cbb41cSArnaldo Carvalho de Melo 		rec->thread_id = pthread_self();
153123cbb41cSArnaldo Carvalho de Melo 	}
15321101c872SJin Yao #ifdef HAVE_LIBBPF_SUPPORT
153323cbb41cSArnaldo Carvalho de Melo 	if (!opts->no_bpf_event) {
153423cbb41cSArnaldo Carvalho de Melo 		if (rec->sb_evlist == NULL) {
153523cbb41cSArnaldo Carvalho de Melo 			rec->sb_evlist = evlist__new();
153623cbb41cSArnaldo Carvalho de Melo 
153723cbb41cSArnaldo Carvalho de Melo 			if (rec->sb_evlist == NULL) {
153823cbb41cSArnaldo Carvalho de Melo 				pr_err("Couldn't create side band evlist.\n.");
153923cbb41cSArnaldo Carvalho de Melo 				return -1;
154023cbb41cSArnaldo Carvalho de Melo 			}
154123cbb41cSArnaldo Carvalho de Melo 		}
154223cbb41cSArnaldo Carvalho de Melo 
154323cbb41cSArnaldo Carvalho de Melo 		if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) {
154423cbb41cSArnaldo Carvalho de Melo 			pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
154523cbb41cSArnaldo Carvalho de Melo 			return -1;
154623cbb41cSArnaldo Carvalho de Melo 		}
154723cbb41cSArnaldo Carvalho de Melo 	}
15481101c872SJin Yao #endif
154908c83997SArnaldo Carvalho de Melo 	if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) {
155023cbb41cSArnaldo Carvalho de Melo 		pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
155123cbb41cSArnaldo Carvalho de Melo 		opts->no_bpf_event = true;
155223cbb41cSArnaldo Carvalho de Melo 	}
155323cbb41cSArnaldo Carvalho de Melo 
155423cbb41cSArnaldo Carvalho de Melo 	return 0;
155523cbb41cSArnaldo Carvalho de Melo }
155623cbb41cSArnaldo Carvalho de Melo 
1557d1e325cfSJiri Olsa static int record__init_clock(struct record *rec)
1558d1e325cfSJiri Olsa {
1559d1e325cfSJiri Olsa 	struct perf_session *session = rec->session;
1560d1e325cfSJiri Olsa 	struct timespec ref_clockid;
1561d1e325cfSJiri Olsa 	struct timeval ref_tod;
1562d1e325cfSJiri Olsa 	u64 ref;
1563d1e325cfSJiri Olsa 
1564d1e325cfSJiri Olsa 	if (!rec->opts.use_clockid)
1565d1e325cfSJiri Olsa 		return 0;
1566d1e325cfSJiri Olsa 
15679d88a1a1SJiri Olsa 	if (rec->opts.use_clockid && rec->opts.clockid_res_ns)
15689d88a1a1SJiri Olsa 		session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns;
15699d88a1a1SJiri Olsa 
1570d1e325cfSJiri Olsa 	session->header.env.clock.clockid = rec->opts.clockid;
1571d1e325cfSJiri Olsa 
1572d1e325cfSJiri Olsa 	if (gettimeofday(&ref_tod, NULL) != 0) {
1573d1e325cfSJiri Olsa 		pr_err("gettimeofday failed, cannot set reference time.\n");
1574d1e325cfSJiri Olsa 		return -1;
1575d1e325cfSJiri Olsa 	}
1576d1e325cfSJiri Olsa 
1577d1e325cfSJiri Olsa 	if (clock_gettime(rec->opts.clockid, &ref_clockid)) {
1578d1e325cfSJiri Olsa 		pr_err("clock_gettime failed, cannot set reference time.\n");
1579d1e325cfSJiri Olsa 		return -1;
1580d1e325cfSJiri Olsa 	}
1581d1e325cfSJiri Olsa 
1582d1e325cfSJiri Olsa 	ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC +
1583d1e325cfSJiri Olsa 	      (u64) ref_tod.tv_usec * NSEC_PER_USEC;
1584d1e325cfSJiri Olsa 
1585d1e325cfSJiri Olsa 	session->header.env.clock.tod_ns = ref;
1586d1e325cfSJiri Olsa 
1587d1e325cfSJiri Olsa 	ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC +
1588d1e325cfSJiri Olsa 	      (u64) ref_clockid.tv_nsec;
1589d1e325cfSJiri Olsa 
1590d1e325cfSJiri Olsa 	session->header.env.clock.clockid_ns = ref;
1591d1e325cfSJiri Olsa 	return 0;
1592d1e325cfSJiri Olsa }
1593d1e325cfSJiri Olsa 
1594d20aff15SAdrian Hunter static void hit_auxtrace_snapshot_trigger(struct record *rec)
1595d20aff15SAdrian Hunter {
1596d20aff15SAdrian Hunter 	if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
1597d20aff15SAdrian Hunter 		trigger_hit(&auxtrace_snapshot_trigger);
1598d20aff15SAdrian Hunter 		auxtrace_record__snapshot_started = 1;
1599d20aff15SAdrian Hunter 		if (auxtrace_record__snapshot_start(rec->itr))
1600d20aff15SAdrian Hunter 			trigger_error(&auxtrace_snapshot_trigger);
1601d20aff15SAdrian Hunter 	}
1602d20aff15SAdrian Hunter }
1603d20aff15SAdrian Hunter 
16048c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv)
160586470930SIngo Molnar {
160657706abcSDavid Ahern 	int err;
160745604710SNamhyung Kim 	int status = 0;
16088b412664SPeter Zijlstra 	unsigned long waking = 0;
160946be604bSZhang, Yanmin 	const bool forks = argc > 0;
161045694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = &rec->tool;
1611b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
16128ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1613d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session;
16146dcf45efSArnaldo Carvalho de Melo 	bool disabled = false, draining = false;
161542aa276fSNamhyung Kim 	int fd;
1616d3c8c08eSAlexey Budankov 	float ratio = 0;
1617acce0223SAlexey Budankov 	enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
161886470930SIngo Molnar 
161945604710SNamhyung Kim 	atexit(record__sig_exit);
1620f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
1621f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
1622804f7ac7SDavid Ahern 	signal(SIGTERM, sig_handler);
1623a074865eSWang Nan 	signal(SIGSEGV, sigsegv_handler);
1624c0bdc1c4SWang Nan 
1625f3b3614aSHari Bathini 	if (rec->opts.record_namespaces)
1626f3b3614aSHari Bathini 		tool->namespace_events = true;
1627f3b3614aSHari Bathini 
16288fb4b679SNamhyung Kim 	if (rec->opts.record_cgroup) {
16298fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE
16308fb4b679SNamhyung Kim 		tool->cgroup_events = true;
16318fb4b679SNamhyung Kim #else
16328fb4b679SNamhyung Kim 		pr_err("cgroup tracking is not supported\n");
16338fb4b679SNamhyung Kim 		return -1;
16348fb4b679SNamhyung Kim #endif
16358fb4b679SNamhyung Kim 	}
16368fb4b679SNamhyung Kim 
1637dc0c6127SJiri Olsa 	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
16382dd6d8a1SAdrian Hunter 		signal(SIGUSR2, snapshot_sig_handler);
16393c1cb7e3SWang Nan 		if (rec->opts.auxtrace_snapshot_mode)
16405f9cf599SWang Nan 			trigger_on(&auxtrace_snapshot_trigger);
1641dc0c6127SJiri Olsa 		if (rec->switch_output.enabled)
16423c1cb7e3SWang Nan 			trigger_on(&switch_output_trigger);
1643c0bdc1c4SWang Nan 	} else {
16442dd6d8a1SAdrian Hunter 		signal(SIGUSR2, SIG_IGN);
1645c0bdc1c4SWang Nan 	}
1646f5970550SPeter Zijlstra 
16478ceb41d7SJiri Olsa 	session = perf_session__new(data, false, tool);
16486ef81c55SMamatha Inamdar 	if (IS_ERR(session)) {
1649ffa91880SAdrien BAK 		pr_err("Perf session creation failed.\n");
16506ef81c55SMamatha Inamdar 		return PTR_ERR(session);
1651a9a70bbcSArnaldo Carvalho de Melo 	}
1652a9a70bbcSArnaldo Carvalho de Melo 
16538ceb41d7SJiri Olsa 	fd = perf_data__fd(data);
1654d20deb64SArnaldo Carvalho de Melo 	rec->session = session;
1655d20deb64SArnaldo Carvalho de Melo 
16565d7f4116SAlexey Budankov 	if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) {
16575d7f4116SAlexey Budankov 		pr_err("Compression initialization failed.\n");
16585d7f4116SAlexey Budankov 		return -1;
16595d7f4116SAlexey Budankov 	}
1660da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
1661da231338SAnand K Mistry 	done_fd = eventfd(0, EFD_NONBLOCK);
1662da231338SAnand K Mistry 	if (done_fd < 0) {
1663da231338SAnand K Mistry 		pr_err("Failed to create wakeup eventfd, error: %m\n");
1664da231338SAnand K Mistry 		status = -1;
1665da231338SAnand K Mistry 		goto out_delete_session;
1666da231338SAnand K Mistry 	}
1667da231338SAnand K Mistry 	err = evlist__add_pollfd(rec->evlist, done_fd);
1668da231338SAnand K Mistry 	if (err < 0) {
1669da231338SAnand K Mistry 		pr_err("Failed to add wakeup eventfd to poll list\n");
1670da231338SAnand K Mistry 		status = err;
1671da231338SAnand K Mistry 		goto out_delete_session;
1672da231338SAnand K Mistry 	}
1673da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT
16745d7f4116SAlexey Budankov 
16755d7f4116SAlexey Budankov 	session->header.env.comp_type  = PERF_COMP_ZSTD;
16765d7f4116SAlexey Budankov 	session->header.env.comp_level = rec->opts.comp_level;
16775d7f4116SAlexey Budankov 
1678eeb399b5SAdrian Hunter 	if (rec->opts.kcore &&
1679eeb399b5SAdrian Hunter 	    !record__kcore_readable(&session->machines.host)) {
1680eeb399b5SAdrian Hunter 		pr_err("ERROR: kcore is not readable.\n");
1681eeb399b5SAdrian Hunter 		return -1;
1682eeb399b5SAdrian Hunter 	}
1683eeb399b5SAdrian Hunter 
1684d1e325cfSJiri Olsa 	if (record__init_clock(rec))
1685d1e325cfSJiri Olsa 		return -1;
1686d1e325cfSJiri Olsa 
16878c6f45a7SArnaldo Carvalho de Melo 	record__init_features(rec);
1688330aa675SStephane Eranian 
1689d4db3f16SArnaldo Carvalho de Melo 	if (forks) {
16907b392ef0SArnaldo Carvalho de Melo 		err = evlist__prepare_workload(rec->evlist, &opts->target, argv, data->is_pipe,
1691735f7e0bSArnaldo Carvalho de Melo 					       workload_exec_failed_signal);
169235b9d88eSArnaldo Carvalho de Melo 		if (err < 0) {
169335b9d88eSArnaldo Carvalho de Melo 			pr_err("Couldn't run the workload!\n");
169445604710SNamhyung Kim 			status = err;
169535b9d88eSArnaldo Carvalho de Melo 			goto out_delete_session;
1696856e9660SPeter Zijlstra 		}
1697856e9660SPeter Zijlstra 	}
1698856e9660SPeter Zijlstra 
1699ad46e48cSJiri Olsa 	/*
1700ad46e48cSJiri Olsa 	 * If we have just single event and are sending data
1701ad46e48cSJiri Olsa 	 * through pipe, we need to force the ids allocation,
1702ad46e48cSJiri Olsa 	 * because we synthesize event name through the pipe
1703ad46e48cSJiri Olsa 	 * and need the id for that.
1704ad46e48cSJiri Olsa 	 */
17056484d2f9SJiri Olsa 	if (data->is_pipe && rec->evlist->core.nr_entries == 1)
1706ad46e48cSJiri Olsa 		rec->opts.sample_id = true;
1707ad46e48cSJiri Olsa 
17088c6f45a7SArnaldo Carvalho de Melo 	if (record__open(rec) != 0) {
17098d3eca20SDavid Ahern 		err = -1;
171045604710SNamhyung Kim 		goto out_child;
17118d3eca20SDavid Ahern 	}
1712f6fa4375SJiri Olsa 	session->header.env.comp_mmap_len = session->evlist->core.mmap_len;
171386470930SIngo Molnar 
1714eeb399b5SAdrian Hunter 	if (rec->opts.kcore) {
1715eeb399b5SAdrian Hunter 		err = record__kcore_copy(&session->machines.host, data);
1716eeb399b5SAdrian Hunter 		if (err) {
1717eeb399b5SAdrian Hunter 			pr_err("ERROR: Failed to copy kcore\n");
1718eeb399b5SAdrian Hunter 			goto out_child;
1719eeb399b5SAdrian Hunter 		}
1720eeb399b5SAdrian Hunter 	}
1721eeb399b5SAdrian Hunter 
17228690a2a7SWang Nan 	err = bpf__apply_obj_config();
17238690a2a7SWang Nan 	if (err) {
17248690a2a7SWang Nan 		char errbuf[BUFSIZ];
17258690a2a7SWang Nan 
17268690a2a7SWang Nan 		bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
17278690a2a7SWang Nan 		pr_err("ERROR: Apply config to BPF failed: %s\n",
17288690a2a7SWang Nan 			 errbuf);
17298690a2a7SWang Nan 		goto out_child;
17308690a2a7SWang Nan 	}
17318690a2a7SWang Nan 
1732cca8482cSAdrian Hunter 	/*
1733cca8482cSAdrian Hunter 	 * Normally perf_session__new would do this, but it doesn't have the
1734cca8482cSAdrian Hunter 	 * evlist.
1735cca8482cSAdrian Hunter 	 */
17368cedf3a5SArnaldo Carvalho de Melo 	if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) {
1737cca8482cSAdrian Hunter 		pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
1738cca8482cSAdrian Hunter 		rec->tool.ordered_events = false;
1739cca8482cSAdrian Hunter 	}
1740cca8482cSAdrian Hunter 
17413e2be2daSArnaldo Carvalho de Melo 	if (!rec->evlist->nr_groups)
1742a8bb559bSNamhyung Kim 		perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
1743a8bb559bSNamhyung Kim 
17448ceb41d7SJiri Olsa 	if (data->is_pipe) {
174542aa276fSNamhyung Kim 		err = perf_header__write_pipe(fd);
1746529870e3STom Zanussi 		if (err < 0)
174745604710SNamhyung Kim 			goto out_child;
1748563aecb2SJiri Olsa 	} else {
174942aa276fSNamhyung Kim 		err = perf_session__write_header(session, rec->evlist, fd, false);
1750d5eed904SArnaldo Carvalho de Melo 		if (err < 0)
175145604710SNamhyung Kim 			goto out_child;
1752d5eed904SArnaldo Carvalho de Melo 	}
17537c6a1c65SPeter Zijlstra 
1754b38d85efSArnaldo Carvalho de Melo 	err = -1;
1755d3665498SDavid Ahern 	if (!rec->no_buildid
1756e20960c0SRobert Richter 	    && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
1757d3665498SDavid Ahern 		pr_err("Couldn't generate buildids. "
1758e20960c0SRobert Richter 		       "Use --no-buildid to profile anyway.\n");
175945604710SNamhyung Kim 		goto out_child;
1760e20960c0SRobert Richter 	}
1761e20960c0SRobert Richter 
176223cbb41cSArnaldo Carvalho de Melo 	err = record__setup_sb_evlist(rec);
176323cbb41cSArnaldo Carvalho de Melo 	if (err)
1764b38d85efSArnaldo Carvalho de Melo 		goto out_child;
1765657ee553SSong Liu 
17664ea648aeSWang Nan 	err = record__synthesize(rec, false);
1767c45c86ebSWang Nan 	if (err < 0)
176845604710SNamhyung Kim 		goto out_child;
17698d3eca20SDavid Ahern 
1770d20deb64SArnaldo Carvalho de Melo 	if (rec->realtime_prio) {
177186470930SIngo Molnar 		struct sched_param param;
177286470930SIngo Molnar 
1773d20deb64SArnaldo Carvalho de Melo 		param.sched_priority = rec->realtime_prio;
177486470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
17756beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
17768d3eca20SDavid Ahern 			err = -1;
177745604710SNamhyung Kim 			goto out_child;
177886470930SIngo Molnar 		}
177986470930SIngo Molnar 	}
178086470930SIngo Molnar 
1781774cb499SJiri Olsa 	/*
1782774cb499SJiri Olsa 	 * When perf is starting the traced process, all the events
1783774cb499SJiri Olsa 	 * (apart from group members) have enable_on_exec=1 set,
1784774cb499SJiri Olsa 	 * so don't spoil it by prematurely enabling them.
1785774cb499SJiri Olsa 	 */
17866619a53eSAndi Kleen 	if (!target__none(&opts->target) && !opts->initial_delay)
17871c87f165SJiri Olsa 		evlist__enable(rec->evlist);
1788764e16a3SDavid Ahern 
1789856e9660SPeter Zijlstra 	/*
1790856e9660SPeter Zijlstra 	 * Let the child rip
1791856e9660SPeter Zijlstra 	 */
1792e803cf97SNamhyung Kim 	if (forks) {
179320a8a3cfSJiri Olsa 		struct machine *machine = &session->machines.host;
1794e5bed564SNamhyung Kim 		union perf_event *event;
1795e907caf3SHari Bathini 		pid_t tgid;
1796e5bed564SNamhyung Kim 
1797e5bed564SNamhyung Kim 		event = malloc(sizeof(event->comm) + machine->id_hdr_size);
1798e5bed564SNamhyung Kim 		if (event == NULL) {
1799e5bed564SNamhyung Kim 			err = -ENOMEM;
1800e5bed564SNamhyung Kim 			goto out_child;
1801e5bed564SNamhyung Kim 		}
1802e5bed564SNamhyung Kim 
1803e803cf97SNamhyung Kim 		/*
1804e803cf97SNamhyung Kim 		 * Some H/W events are generated before COMM event
1805e803cf97SNamhyung Kim 		 * which is emitted during exec(), so perf script
1806e803cf97SNamhyung Kim 		 * cannot see a correct process name for those events.
1807e803cf97SNamhyung Kim 		 * Synthesize COMM event to prevent it.
1808e803cf97SNamhyung Kim 		 */
1809e907caf3SHari Bathini 		tgid = perf_event__synthesize_comm(tool, event,
1810e803cf97SNamhyung Kim 						   rec->evlist->workload.pid,
1811e803cf97SNamhyung Kim 						   process_synthesized_event,
1812e803cf97SNamhyung Kim 						   machine);
1813e5bed564SNamhyung Kim 		free(event);
1814e803cf97SNamhyung Kim 
1815e907caf3SHari Bathini 		if (tgid == -1)
1816e907caf3SHari Bathini 			goto out_child;
1817e907caf3SHari Bathini 
1818e907caf3SHari Bathini 		event = malloc(sizeof(event->namespaces) +
1819e907caf3SHari Bathini 			       (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
1820e907caf3SHari Bathini 			       machine->id_hdr_size);
1821e907caf3SHari Bathini 		if (event == NULL) {
1822e907caf3SHari Bathini 			err = -ENOMEM;
1823e907caf3SHari Bathini 			goto out_child;
1824e907caf3SHari Bathini 		}
1825e907caf3SHari Bathini 
1826e907caf3SHari Bathini 		/*
1827e907caf3SHari Bathini 		 * Synthesize NAMESPACES event for the command specified.
1828e907caf3SHari Bathini 		 */
1829e907caf3SHari Bathini 		perf_event__synthesize_namespaces(tool, event,
1830e907caf3SHari Bathini 						  rec->evlist->workload.pid,
1831e907caf3SHari Bathini 						  tgid, process_synthesized_event,
1832e907caf3SHari Bathini 						  machine);
1833e907caf3SHari Bathini 		free(event);
1834e907caf3SHari Bathini 
18357b392ef0SArnaldo Carvalho de Melo 		evlist__start_workload(rec->evlist);
1836e803cf97SNamhyung Kim 	}
1837856e9660SPeter Zijlstra 
18381d078ccbSAlexey Budankov 	if (evlist__initialize_ctlfd(rec->evlist, opts->ctl_fd, opts->ctl_fd_ack))
18391d078ccbSAlexey Budankov 		goto out_child;
18401d078ccbSAlexey Budankov 
18416619a53eSAndi Kleen 	if (opts->initial_delay) {
184268cd3b45SAlexey Budankov 		pr_info(EVLIST_DISABLED_MSG);
184368cd3b45SAlexey Budankov 		if (opts->initial_delay > 0) {
18440693e680SArnaldo Carvalho de Melo 			usleep(opts->initial_delay * USEC_PER_MSEC);
18451c87f165SJiri Olsa 			evlist__enable(rec->evlist);
184668cd3b45SAlexey Budankov 			pr_info(EVLIST_ENABLED_MSG);
184768cd3b45SAlexey Budankov 		}
18486619a53eSAndi Kleen 	}
18496619a53eSAndi Kleen 
18505f9cf599SWang Nan 	trigger_ready(&auxtrace_snapshot_trigger);
18513c1cb7e3SWang Nan 	trigger_ready(&switch_output_trigger);
1852a074865eSWang Nan 	perf_hooks__invoke_record_start();
1853649c48a9SPeter Zijlstra 	for (;;) {
18549f065194SYang Shi 		unsigned long long hits = rec->samples;
185586470930SIngo Molnar 
185605737464SWang Nan 		/*
185705737464SWang Nan 		 * rec->evlist->bkw_mmap_state is possible to be
185805737464SWang Nan 		 * BKW_MMAP_EMPTY here: when done == true and
185905737464SWang Nan 		 * hits != rec->samples in previous round.
186005737464SWang Nan 		 *
1861ade9d208SArnaldo Carvalho de Melo 		 * evlist__toggle_bkw_mmap ensure we never
186205737464SWang Nan 		 * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
186305737464SWang Nan 		 */
186405737464SWang Nan 		if (trigger_is_hit(&switch_output_trigger) || done || draining)
1865ade9d208SArnaldo Carvalho de Melo 			evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
186605737464SWang Nan 
1867470530bbSAlexey Budankov 		if (record__mmap_read_all(rec, false) < 0) {
18685f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
18693c1cb7e3SWang Nan 			trigger_error(&switch_output_trigger);
18708d3eca20SDavid Ahern 			err = -1;
187145604710SNamhyung Kim 			goto out_child;
18728d3eca20SDavid Ahern 		}
187386470930SIngo Molnar 
18742dd6d8a1SAdrian Hunter 		if (auxtrace_record__snapshot_started) {
18752dd6d8a1SAdrian Hunter 			auxtrace_record__snapshot_started = 0;
18765f9cf599SWang Nan 			if (!trigger_is_error(&auxtrace_snapshot_trigger))
1877ce7b0e42SAlexander Shishkin 				record__read_auxtrace_snapshot(rec, false);
18785f9cf599SWang Nan 			if (trigger_is_error(&auxtrace_snapshot_trigger)) {
18792dd6d8a1SAdrian Hunter 				pr_err("AUX area tracing snapshot failed\n");
18802dd6d8a1SAdrian Hunter 				err = -1;
18812dd6d8a1SAdrian Hunter 				goto out_child;
18822dd6d8a1SAdrian Hunter 			}
18832dd6d8a1SAdrian Hunter 		}
18842dd6d8a1SAdrian Hunter 
18853c1cb7e3SWang Nan 		if (trigger_is_hit(&switch_output_trigger)) {
188605737464SWang Nan 			/*
188705737464SWang Nan 			 * If switch_output_trigger is hit, the data in
188805737464SWang Nan 			 * overwritable ring buffer should have been collected,
188905737464SWang Nan 			 * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
189005737464SWang Nan 			 *
189105737464SWang Nan 			 * If SIGUSR2 raise after or during record__mmap_read_all(),
189205737464SWang Nan 			 * record__mmap_read_all() didn't collect data from
189305737464SWang Nan 			 * overwritable ring buffer. Read again.
189405737464SWang Nan 			 */
189505737464SWang Nan 			if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
189605737464SWang Nan 				continue;
18973c1cb7e3SWang Nan 			trigger_ready(&switch_output_trigger);
18983c1cb7e3SWang Nan 
189905737464SWang Nan 			/*
190005737464SWang Nan 			 * Reenable events in overwrite ring buffer after
190105737464SWang Nan 			 * record__mmap_read_all(): we should have collected
190205737464SWang Nan 			 * data from it.
190305737464SWang Nan 			 */
1904ade9d208SArnaldo Carvalho de Melo 			evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
190505737464SWang Nan 
19063c1cb7e3SWang Nan 			if (!quiet)
19073c1cb7e3SWang Nan 				fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
19083c1cb7e3SWang Nan 					waking);
19093c1cb7e3SWang Nan 			waking = 0;
19103c1cb7e3SWang Nan 			fd = record__switch_output(rec, false);
19113c1cb7e3SWang Nan 			if (fd < 0) {
19123c1cb7e3SWang Nan 				pr_err("Failed to switch to new file\n");
19133c1cb7e3SWang Nan 				trigger_error(&switch_output_trigger);
19143c1cb7e3SWang Nan 				err = fd;
19153c1cb7e3SWang Nan 				goto out_child;
19163c1cb7e3SWang Nan 			}
1917bfacbe3bSJiri Olsa 
1918bfacbe3bSJiri Olsa 			/* re-arm the alarm */
1919bfacbe3bSJiri Olsa 			if (rec->switch_output.time)
1920bfacbe3bSJiri Olsa 				alarm(rec->switch_output.time);
19213c1cb7e3SWang Nan 		}
19223c1cb7e3SWang Nan 
1923d20deb64SArnaldo Carvalho de Melo 		if (hits == rec->samples) {
19246dcf45efSArnaldo Carvalho de Melo 			if (done || draining)
1925649c48a9SPeter Zijlstra 				break;
192680ab2987SJiri Olsa 			err = evlist__poll(rec->evlist, -1);
1927a515114fSJiri Olsa 			/*
1928a515114fSJiri Olsa 			 * Propagate error, only if there's any. Ignore positive
1929a515114fSJiri Olsa 			 * number of returned events and interrupt error.
1930a515114fSJiri Olsa 			 */
1931a515114fSJiri Olsa 			if (err > 0 || (err < 0 && errno == EINTR))
193245604710SNamhyung Kim 				err = 0;
19338b412664SPeter Zijlstra 			waking++;
19346dcf45efSArnaldo Carvalho de Melo 
1935f4009e7bSJiri Olsa 			if (evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
19366dcf45efSArnaldo Carvalho de Melo 				draining = true;
19378b412664SPeter Zijlstra 		}
19388b412664SPeter Zijlstra 
1939acce0223SAlexey Budankov 		if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) {
1940acce0223SAlexey Budankov 			switch (cmd) {
1941d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
1942d20aff15SAdrian Hunter 				hit_auxtrace_snapshot_trigger(rec);
1943d20aff15SAdrian Hunter 				evlist__ctlfd_ack(rec->evlist);
1944d20aff15SAdrian Hunter 				break;
1945acce0223SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
1946acce0223SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
1947*991ae4ebSJiri Olsa 			case EVLIST_CTL_CMD_ENABLE:
1948*991ae4ebSJiri Olsa 			case EVLIST_CTL_CMD_DISABLE:
1949acce0223SAlexey Budankov 			default:
1950acce0223SAlexey Budankov 				break;
1951acce0223SAlexey Budankov 			}
1952acce0223SAlexey Budankov 		}
1953acce0223SAlexey Budankov 
1954774cb499SJiri Olsa 		/*
1955774cb499SJiri Olsa 		 * When perf is starting the traced process, at the end events
1956774cb499SJiri Olsa 		 * die with the process and we wait for that. Thus no need to
1957774cb499SJiri Olsa 		 * disable events in this case.
1958774cb499SJiri Olsa 		 */
1959602ad878SArnaldo Carvalho de Melo 		if (done && !disabled && !target__none(&opts->target)) {
19605f9cf599SWang Nan 			trigger_off(&auxtrace_snapshot_trigger);
1961e74676deSJiri Olsa 			evlist__disable(rec->evlist);
19622711926aSJiri Olsa 			disabled = true;
19632711926aSJiri Olsa 		}
19648b412664SPeter Zijlstra 	}
1965ce7b0e42SAlexander Shishkin 
19665f9cf599SWang Nan 	trigger_off(&auxtrace_snapshot_trigger);
19673c1cb7e3SWang Nan 	trigger_off(&switch_output_trigger);
19688b412664SPeter Zijlstra 
1969ce7b0e42SAlexander Shishkin 	if (opts->auxtrace_snapshot_on_exit)
1970ce7b0e42SAlexander Shishkin 		record__auxtrace_snapshot_exit(rec);
1971ce7b0e42SAlexander Shishkin 
1972f33cbe72SArnaldo Carvalho de Melo 	if (forks && workload_exec_errno) {
197335550da3SMasami Hiramatsu 		char msg[STRERR_BUFSIZE];
1974c8b5f2c9SArnaldo Carvalho de Melo 		const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
1975f33cbe72SArnaldo Carvalho de Melo 		pr_err("Workload failed: %s\n", emsg);
1976f33cbe72SArnaldo Carvalho de Melo 		err = -1;
197745604710SNamhyung Kim 		goto out_child;
1978f33cbe72SArnaldo Carvalho de Melo 	}
1979f33cbe72SArnaldo Carvalho de Melo 
1980e3d59112SNamhyung Kim 	if (!quiet)
19818b412664SPeter Zijlstra 		fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
198286470930SIngo Molnar 
19834ea648aeSWang Nan 	if (target__none(&rec->opts.target))
19844ea648aeSWang Nan 		record__synthesize_workload(rec, true);
19854ea648aeSWang Nan 
198645604710SNamhyung Kim out_child:
19871d078ccbSAlexey Budankov 	evlist__finalize_ctlfd(rec->evlist);
1988470530bbSAlexey Budankov 	record__mmap_read_all(rec, true);
1989d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
1990d3d1af6fSAlexey Budankov 
1991d3c8c08eSAlexey Budankov 	if (rec->session->bytes_transferred && rec->session->bytes_compressed) {
1992d3c8c08eSAlexey Budankov 		ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed;
1993d3c8c08eSAlexey Budankov 		session->header.env.comp_ratio = ratio + 0.5;
1994d3c8c08eSAlexey Budankov 	}
1995d3c8c08eSAlexey Budankov 
199645604710SNamhyung Kim 	if (forks) {
199745604710SNamhyung Kim 		int exit_status;
199845604710SNamhyung Kim 
199945604710SNamhyung Kim 		if (!child_finished)
200045604710SNamhyung Kim 			kill(rec->evlist->workload.pid, SIGTERM);
200145604710SNamhyung Kim 
200245604710SNamhyung Kim 		wait(&exit_status);
200345604710SNamhyung Kim 
200445604710SNamhyung Kim 		if (err < 0)
200545604710SNamhyung Kim 			status = err;
200645604710SNamhyung Kim 		else if (WIFEXITED(exit_status))
200745604710SNamhyung Kim 			status = WEXITSTATUS(exit_status);
200845604710SNamhyung Kim 		else if (WIFSIGNALED(exit_status))
200945604710SNamhyung Kim 			signr = WTERMSIG(exit_status);
201045604710SNamhyung Kim 	} else
201145604710SNamhyung Kim 		status = err;
201245604710SNamhyung Kim 
20134ea648aeSWang Nan 	record__synthesize(rec, true);
2014e3d59112SNamhyung Kim 	/* this will be recalculated during process_buildids() */
2015e3d59112SNamhyung Kim 	rec->samples = 0;
2016e3d59112SNamhyung Kim 
2017ecfd7a9cSWang Nan 	if (!err) {
2018ecfd7a9cSWang Nan 		if (!rec->timestamp_filename) {
2019e1ab48baSWang Nan 			record__finish_output(rec);
2020ecfd7a9cSWang Nan 		} else {
2021ecfd7a9cSWang Nan 			fd = record__switch_output(rec, true);
2022ecfd7a9cSWang Nan 			if (fd < 0) {
2023ecfd7a9cSWang Nan 				status = fd;
2024ecfd7a9cSWang Nan 				goto out_delete_session;
2025ecfd7a9cSWang Nan 			}
2026ecfd7a9cSWang Nan 		}
2027ecfd7a9cSWang Nan 	}
202839d17dacSArnaldo Carvalho de Melo 
2029a074865eSWang Nan 	perf_hooks__invoke_record_end();
2030a074865eSWang Nan 
2031e3d59112SNamhyung Kim 	if (!err && !quiet) {
2032e3d59112SNamhyung Kim 		char samples[128];
2033ecfd7a9cSWang Nan 		const char *postfix = rec->timestamp_filename ?
2034ecfd7a9cSWang Nan 					".<timestamp>" : "";
2035e3d59112SNamhyung Kim 
2036ef149c25SAdrian Hunter 		if (rec->samples && !rec->opts.full_auxtrace)
2037e3d59112SNamhyung Kim 			scnprintf(samples, sizeof(samples),
2038e3d59112SNamhyung Kim 				  " (%" PRIu64 " samples)", rec->samples);
2039e3d59112SNamhyung Kim 		else
2040e3d59112SNamhyung Kim 			samples[0] = '\0';
2041e3d59112SNamhyung Kim 
2042d3c8c08eSAlexey Budankov 		fprintf(stderr,	"[ perf record: Captured and wrote %.3f MB %s%s%s",
20438ceb41d7SJiri Olsa 			perf_data__size(data) / 1024.0 / 1024.0,
20442d4f2799SJiri Olsa 			data->path, postfix, samples);
2045d3c8c08eSAlexey Budankov 		if (ratio) {
2046d3c8c08eSAlexey Budankov 			fprintf(stderr,	", compressed (original %.3f MB, ratio is %.3f)",
2047d3c8c08eSAlexey Budankov 					rec->session->bytes_transferred / 1024.0 / 1024.0,
2048d3c8c08eSAlexey Budankov 					ratio);
2049d3c8c08eSAlexey Budankov 		}
2050d3c8c08eSAlexey Budankov 		fprintf(stderr, " ]\n");
2051e3d59112SNamhyung Kim 	}
2052e3d59112SNamhyung Kim 
205339d17dacSArnaldo Carvalho de Melo out_delete_session:
2054da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT
2055da231338SAnand K Mistry 	if (done_fd >= 0)
2056da231338SAnand K Mistry 		close(done_fd);
2057da231338SAnand K Mistry #endif
20585d7f4116SAlexey Budankov 	zstd_fini(&session->zstd_data);
205939d17dacSArnaldo Carvalho de Melo 	perf_session__delete(session);
2060657ee553SSong Liu 
2061657ee553SSong Liu 	if (!opts->no_bpf_event)
206208c83997SArnaldo Carvalho de Melo 		evlist__stop_sb_thread(rec->sb_evlist);
206345604710SNamhyung Kim 	return status;
206486470930SIngo Molnar }
206586470930SIngo Molnar 
20660883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain)
206709b0fd45SJiri Olsa {
2068aad2b21cSKan Liang 	static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
2069a601fdffSJiri Olsa 
20700883e820SArnaldo Carvalho de Melo 	pr_debug("callchain: type %s\n", str[callchain->record_mode]);
207126d33022SJiri Olsa 
20720883e820SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_DWARF)
207309b0fd45SJiri Olsa 		pr_debug("callchain: stack dump size %d\n",
20740883e820SArnaldo Carvalho de Melo 			 callchain->dump_size);
20750883e820SArnaldo Carvalho de Melo }
20760883e820SArnaldo Carvalho de Melo 
20770883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record,
20780883e820SArnaldo Carvalho de Melo 				 struct callchain_param *callchain,
20790883e820SArnaldo Carvalho de Melo 				 const char *arg, bool unset)
20800883e820SArnaldo Carvalho de Melo {
20810883e820SArnaldo Carvalho de Melo 	int ret;
20820883e820SArnaldo Carvalho de Melo 	callchain->enabled = !unset;
20830883e820SArnaldo Carvalho de Melo 
20840883e820SArnaldo Carvalho de Melo 	/* --no-call-graph */
20850883e820SArnaldo Carvalho de Melo 	if (unset) {
20860883e820SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_NONE;
20870883e820SArnaldo Carvalho de Melo 		pr_debug("callchain: disabled\n");
20880883e820SArnaldo Carvalho de Melo 		return 0;
20890883e820SArnaldo Carvalho de Melo 	}
20900883e820SArnaldo Carvalho de Melo 
20910883e820SArnaldo Carvalho de Melo 	ret = parse_callchain_record_opt(arg, callchain);
20920883e820SArnaldo Carvalho de Melo 	if (!ret) {
20930883e820SArnaldo Carvalho de Melo 		/* Enable data address sampling for DWARF unwind. */
20940883e820SArnaldo Carvalho de Melo 		if (callchain->record_mode == CALLCHAIN_DWARF)
20950883e820SArnaldo Carvalho de Melo 			record->sample_address = true;
20960883e820SArnaldo Carvalho de Melo 		callchain_debug(callchain);
20970883e820SArnaldo Carvalho de Melo 	}
20980883e820SArnaldo Carvalho de Melo 
20990883e820SArnaldo Carvalho de Melo 	return ret;
210009b0fd45SJiri Olsa }
210109b0fd45SJiri Olsa 
2102c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt,
210309b0fd45SJiri Olsa 			       const char *arg,
210409b0fd45SJiri Olsa 			       int unset)
210509b0fd45SJiri Olsa {
21060883e820SArnaldo Carvalho de Melo 	return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
210726d33022SJiri Olsa }
210826d33022SJiri Olsa 
2109c421e80bSKan Liang int record_callchain_opt(const struct option *opt,
211009b0fd45SJiri Olsa 			 const char *arg __maybe_unused,
211109b0fd45SJiri Olsa 			 int unset __maybe_unused)
211209b0fd45SJiri Olsa {
21132ddd5c04SArnaldo Carvalho de Melo 	struct callchain_param *callchain = opt->value;
2114c421e80bSKan Liang 
21152ddd5c04SArnaldo Carvalho de Melo 	callchain->enabled = true;
211609b0fd45SJiri Olsa 
21172ddd5c04SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_NONE)
21182ddd5c04SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_FP;
2119eb853e80SJiri Olsa 
21202ddd5c04SArnaldo Carvalho de Melo 	callchain_debug(callchain);
212109b0fd45SJiri Olsa 	return 0;
212209b0fd45SJiri Olsa }
212309b0fd45SJiri Olsa 
2124eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb)
2125eb853e80SJiri Olsa {
21267a29c087SNamhyung Kim 	struct record *rec = cb;
21277a29c087SNamhyung Kim 
21287a29c087SNamhyung Kim 	if (!strcmp(var, "record.build-id")) {
21297a29c087SNamhyung Kim 		if (!strcmp(value, "cache"))
21307a29c087SNamhyung Kim 			rec->no_buildid_cache = false;
21317a29c087SNamhyung Kim 		else if (!strcmp(value, "no-cache"))
21327a29c087SNamhyung Kim 			rec->no_buildid_cache = true;
21337a29c087SNamhyung Kim 		else if (!strcmp(value, "skip"))
21347a29c087SNamhyung Kim 			rec->no_buildid = true;
2135e29386c8SJiri Olsa 		else if (!strcmp(value, "mmap"))
2136e29386c8SJiri Olsa 			rec->buildid_mmap = true;
21377a29c087SNamhyung Kim 		else
21387a29c087SNamhyung Kim 			return -1;
21397a29c087SNamhyung Kim 		return 0;
21407a29c087SNamhyung Kim 	}
2141cff17205SYisheng Xie 	if (!strcmp(var, "record.call-graph")) {
2142cff17205SYisheng Xie 		var = "call-graph.record-mode";
2143eb853e80SJiri Olsa 		return perf_default_config(var, value, cb);
2144eb853e80SJiri Olsa 	}
214593f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
214693f20c0fSAlexey Budankov 	if (!strcmp(var, "record.aio")) {
214793f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = strtol(value, NULL, 0);
214893f20c0fSAlexey Budankov 		if (!rec->opts.nr_cblocks)
214993f20c0fSAlexey Budankov 			rec->opts.nr_cblocks = nr_cblocks_default;
215093f20c0fSAlexey Budankov 	}
215193f20c0fSAlexey Budankov #endif
2152eb853e80SJiri Olsa 
2153cff17205SYisheng Xie 	return 0;
2154cff17205SYisheng Xie }
2155cff17205SYisheng Xie 
2156814c8c38SPeter Zijlstra 
2157f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset)
2158f4fe11b7SAlexey Budankov {
2159f4fe11b7SAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
2160f4fe11b7SAlexey Budankov 
2161f4fe11b7SAlexey Budankov 	if (unset || !str)
2162f4fe11b7SAlexey Budankov 		return 0;
2163f4fe11b7SAlexey Budankov 
2164f4fe11b7SAlexey Budankov 	if (!strcasecmp(str, "node"))
2165f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_NODE;
2166f4fe11b7SAlexey Budankov 	else if (!strcasecmp(str, "cpu"))
2167f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_CPU;
2168f4fe11b7SAlexey Budankov 
2169f4fe11b7SAlexey Budankov 	return 0;
2170f4fe11b7SAlexey Budankov }
2171f4fe11b7SAlexey Budankov 
21726d575816SJiwei Sun static int parse_output_max_size(const struct option *opt,
21736d575816SJiwei Sun 				 const char *str, int unset)
21746d575816SJiwei Sun {
21756d575816SJiwei Sun 	unsigned long *s = (unsigned long *)opt->value;
21766d575816SJiwei Sun 	static struct parse_tag tags_size[] = {
21776d575816SJiwei Sun 		{ .tag  = 'B', .mult = 1       },
21786d575816SJiwei Sun 		{ .tag  = 'K', .mult = 1 << 10 },
21796d575816SJiwei Sun 		{ .tag  = 'M', .mult = 1 << 20 },
21806d575816SJiwei Sun 		{ .tag  = 'G', .mult = 1 << 30 },
21816d575816SJiwei Sun 		{ .tag  = 0 },
21826d575816SJiwei Sun 	};
21836d575816SJiwei Sun 	unsigned long val;
21846d575816SJiwei Sun 
21856d575816SJiwei Sun 	if (unset) {
21866d575816SJiwei Sun 		*s = 0;
21876d575816SJiwei Sun 		return 0;
21886d575816SJiwei Sun 	}
21896d575816SJiwei Sun 
21906d575816SJiwei Sun 	val = parse_tag_value(str, tags_size);
21916d575816SJiwei Sun 	if (val != (unsigned long) -1) {
21926d575816SJiwei Sun 		*s = val;
21936d575816SJiwei Sun 		return 0;
21946d575816SJiwei Sun 	}
21956d575816SJiwei Sun 
21966d575816SJiwei Sun 	return -1;
21976d575816SJiwei Sun }
21986d575816SJiwei Sun 
2199e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt,
2200e9db1310SAdrian Hunter 				    const char *str,
2201e9db1310SAdrian Hunter 				    int unset __maybe_unused)
2202e9db1310SAdrian Hunter {
2203e9db1310SAdrian Hunter 	struct record_opts *opts = opt->value;
2204e9db1310SAdrian Hunter 	char *s, *p;
2205e9db1310SAdrian Hunter 	unsigned int mmap_pages;
2206e9db1310SAdrian Hunter 	int ret;
2207e9db1310SAdrian Hunter 
2208e9db1310SAdrian Hunter 	if (!str)
2209e9db1310SAdrian Hunter 		return -EINVAL;
2210e9db1310SAdrian Hunter 
2211e9db1310SAdrian Hunter 	s = strdup(str);
2212e9db1310SAdrian Hunter 	if (!s)
2213e9db1310SAdrian Hunter 		return -ENOMEM;
2214e9db1310SAdrian Hunter 
2215e9db1310SAdrian Hunter 	p = strchr(s, ',');
2216e9db1310SAdrian Hunter 	if (p)
2217e9db1310SAdrian Hunter 		*p = '\0';
2218e9db1310SAdrian Hunter 
2219e9db1310SAdrian Hunter 	if (*s) {
222025f84702SArnaldo Carvalho de Melo 		ret = __evlist__parse_mmap_pages(&mmap_pages, s);
2221e9db1310SAdrian Hunter 		if (ret)
2222e9db1310SAdrian Hunter 			goto out_free;
2223e9db1310SAdrian Hunter 		opts->mmap_pages = mmap_pages;
2224e9db1310SAdrian Hunter 	}
2225e9db1310SAdrian Hunter 
2226e9db1310SAdrian Hunter 	if (!p) {
2227e9db1310SAdrian Hunter 		ret = 0;
2228e9db1310SAdrian Hunter 		goto out_free;
2229e9db1310SAdrian Hunter 	}
2230e9db1310SAdrian Hunter 
223125f84702SArnaldo Carvalho de Melo 	ret = __evlist__parse_mmap_pages(&mmap_pages, p + 1);
2232e9db1310SAdrian Hunter 	if (ret)
2233e9db1310SAdrian Hunter 		goto out_free;
2234e9db1310SAdrian Hunter 
2235e9db1310SAdrian Hunter 	opts->auxtrace_mmap_pages = mmap_pages;
2236e9db1310SAdrian Hunter 
2237e9db1310SAdrian Hunter out_free:
2238e9db1310SAdrian Hunter 	free(s);
2239e9db1310SAdrian Hunter 	return ret;
2240e9db1310SAdrian Hunter }
2241e9db1310SAdrian Hunter 
22421d078ccbSAlexey Budankov static int parse_control_option(const struct option *opt,
22431d078ccbSAlexey Budankov 				const char *str,
22441d078ccbSAlexey Budankov 				int unset __maybe_unused)
22451d078ccbSAlexey Budankov {
22469864a66dSAdrian Hunter 	struct record_opts *opts = opt->value;
22471d078ccbSAlexey Budankov 
2248a8fcbd26SAdrian Hunter 	return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close);
2249a8fcbd26SAdrian Hunter }
2250a8fcbd26SAdrian Hunter 
22510c582449SJiri Olsa static void switch_output_size_warn(struct record *rec)
22520c582449SJiri Olsa {
22539521b5f2SJiri Olsa 	u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages);
22540c582449SJiri Olsa 	struct switch_output *s = &rec->switch_output;
22550c582449SJiri Olsa 
22560c582449SJiri Olsa 	wakeup_size /= 2;
22570c582449SJiri Olsa 
22580c582449SJiri Olsa 	if (s->size < wakeup_size) {
22590c582449SJiri Olsa 		char buf[100];
22600c582449SJiri Olsa 
22610c582449SJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
22620c582449SJiri Olsa 		pr_warning("WARNING: switch-output data size lower than "
22630c582449SJiri Olsa 			   "wakeup kernel buffer size (%s) "
22640c582449SJiri Olsa 			   "expect bigger perf.data sizes\n", buf);
22650c582449SJiri Olsa 	}
22660c582449SJiri Olsa }
22670c582449SJiri Olsa 
2268cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec)
2269cb4e1ebbSJiri Olsa {
2270cb4e1ebbSJiri Olsa 	struct switch_output *s = &rec->switch_output;
2271dc0c6127SJiri Olsa 	static struct parse_tag tags_size[] = {
2272dc0c6127SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
2273dc0c6127SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
2274dc0c6127SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
2275dc0c6127SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
2276dc0c6127SJiri Olsa 		{ .tag  = 0 },
2277dc0c6127SJiri Olsa 	};
2278bfacbe3bSJiri Olsa 	static struct parse_tag tags_time[] = {
2279bfacbe3bSJiri Olsa 		{ .tag  = 's', .mult = 1        },
2280bfacbe3bSJiri Olsa 		{ .tag  = 'm', .mult = 60       },
2281bfacbe3bSJiri Olsa 		{ .tag  = 'h', .mult = 60*60    },
2282bfacbe3bSJiri Olsa 		{ .tag  = 'd', .mult = 60*60*24 },
2283bfacbe3bSJiri Olsa 		{ .tag  = 0 },
2284bfacbe3bSJiri Olsa 	};
2285dc0c6127SJiri Olsa 	unsigned long val;
2286cb4e1ebbSJiri Olsa 
2287899e5ffbSArnaldo Carvalho de Melo 	/*
2288899e5ffbSArnaldo Carvalho de Melo 	 * If we're using --switch-output-events, then we imply its
2289899e5ffbSArnaldo Carvalho de Melo 	 * --switch-output=signal, as we'll send a SIGUSR2 from the side band
2290899e5ffbSArnaldo Carvalho de Melo 	 *  thread to its parent.
2291899e5ffbSArnaldo Carvalho de Melo 	 */
2292899e5ffbSArnaldo Carvalho de Melo 	if (rec->switch_output_event_set)
2293899e5ffbSArnaldo Carvalho de Melo 		goto do_signal;
2294899e5ffbSArnaldo Carvalho de Melo 
2295cb4e1ebbSJiri Olsa 	if (!s->set)
2296cb4e1ebbSJiri Olsa 		return 0;
2297cb4e1ebbSJiri Olsa 
2298cb4e1ebbSJiri Olsa 	if (!strcmp(s->str, "signal")) {
2299899e5ffbSArnaldo Carvalho de Melo do_signal:
2300cb4e1ebbSJiri Olsa 		s->signal = true;
2301cb4e1ebbSJiri Olsa 		pr_debug("switch-output with SIGUSR2 signal\n");
2302dc0c6127SJiri Olsa 		goto enabled;
2303dc0c6127SJiri Olsa 	}
2304dc0c6127SJiri Olsa 
2305dc0c6127SJiri Olsa 	val = parse_tag_value(s->str, tags_size);
2306dc0c6127SJiri Olsa 	if (val != (unsigned long) -1) {
2307dc0c6127SJiri Olsa 		s->size = val;
2308dc0c6127SJiri Olsa 		pr_debug("switch-output with %s size threshold\n", s->str);
2309dc0c6127SJiri Olsa 		goto enabled;
2310cb4e1ebbSJiri Olsa 	}
2311cb4e1ebbSJiri Olsa 
2312bfacbe3bSJiri Olsa 	val = parse_tag_value(s->str, tags_time);
2313bfacbe3bSJiri Olsa 	if (val != (unsigned long) -1) {
2314bfacbe3bSJiri Olsa 		s->time = val;
2315bfacbe3bSJiri Olsa 		pr_debug("switch-output with %s time threshold (%lu seconds)\n",
2316bfacbe3bSJiri Olsa 			 s->str, s->time);
2317bfacbe3bSJiri Olsa 		goto enabled;
2318bfacbe3bSJiri Olsa 	}
2319bfacbe3bSJiri Olsa 
2320cb4e1ebbSJiri Olsa 	return -1;
2321dc0c6127SJiri Olsa 
2322dc0c6127SJiri Olsa enabled:
2323dc0c6127SJiri Olsa 	rec->timestamp_filename = true;
2324dc0c6127SJiri Olsa 	s->enabled              = true;
23250c582449SJiri Olsa 
23260c582449SJiri Olsa 	if (s->size && !rec->opts.no_buffering)
23270c582449SJiri Olsa 		switch_output_size_warn(rec);
23280c582449SJiri Olsa 
2329dc0c6127SJiri Olsa 	return 0;
2330cb4e1ebbSJiri Olsa }
2331cb4e1ebbSJiri Olsa 
2332e5b2c207SNamhyung Kim static const char * const __record_usage[] = {
233386470930SIngo Molnar 	"perf record [<options>] [<command>]",
233486470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
233586470930SIngo Molnar 	NULL
233686470930SIngo Molnar };
2337e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage;
233886470930SIngo Molnar 
23396e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event,
23406e0a9b3dSArnaldo Carvalho de Melo 				  struct perf_sample *sample, struct machine *machine)
23416e0a9b3dSArnaldo Carvalho de Melo {
23426e0a9b3dSArnaldo Carvalho de Melo 	/*
23436e0a9b3dSArnaldo Carvalho de Melo 	 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
23446e0a9b3dSArnaldo Carvalho de Melo 	 * no need to add them twice.
23456e0a9b3dSArnaldo Carvalho de Melo 	 */
23466e0a9b3dSArnaldo Carvalho de Melo 	if (!(event->header.misc & PERF_RECORD_MISC_USER))
23476e0a9b3dSArnaldo Carvalho de Melo 		return 0;
23486e0a9b3dSArnaldo Carvalho de Melo 	return perf_event__process_mmap(tool, event, sample, machine);
23496e0a9b3dSArnaldo Carvalho de Melo }
23506e0a9b3dSArnaldo Carvalho de Melo 
23516e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event,
23526e0a9b3dSArnaldo Carvalho de Melo 				   struct perf_sample *sample, struct machine *machine)
23536e0a9b3dSArnaldo Carvalho de Melo {
23546e0a9b3dSArnaldo Carvalho de Melo 	/*
23556e0a9b3dSArnaldo Carvalho de Melo 	 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
23566e0a9b3dSArnaldo Carvalho de Melo 	 * no need to add them twice.
23576e0a9b3dSArnaldo Carvalho de Melo 	 */
23586e0a9b3dSArnaldo Carvalho de Melo 	if (!(event->header.misc & PERF_RECORD_MISC_USER))
23596e0a9b3dSArnaldo Carvalho de Melo 		return 0;
23606e0a9b3dSArnaldo Carvalho de Melo 
23616e0a9b3dSArnaldo Carvalho de Melo 	return perf_event__process_mmap2(tool, event, sample, machine);
23626e0a9b3dSArnaldo Carvalho de Melo }
23636e0a9b3dSArnaldo Carvalho de Melo 
2364d20deb64SArnaldo Carvalho de Melo /*
23658c6f45a7SArnaldo Carvalho de Melo  * XXX Ideally would be local to cmd_record() and passed to a record__new
23668c6f45a7SArnaldo Carvalho de Melo  * because we need to have access to it in record__exit, that is called
2367d20deb64SArnaldo Carvalho de Melo  * after cmd_record() exits, but since record_options need to be accessible to
2368d20deb64SArnaldo Carvalho de Melo  * builtin-script, leave it here.
2369d20deb64SArnaldo Carvalho de Melo  *
2370d20deb64SArnaldo Carvalho de Melo  * At least we don't ouch it in all the other functions here directly.
2371d20deb64SArnaldo Carvalho de Melo  *
2372d20deb64SArnaldo Carvalho de Melo  * Just say no to tons of global variables, sigh.
2373d20deb64SArnaldo Carvalho de Melo  */
23748c6f45a7SArnaldo Carvalho de Melo static struct record record = {
2375d20deb64SArnaldo Carvalho de Melo 	.opts = {
23768affc2b8SAndi Kleen 		.sample_time	     = true,
2377d20deb64SArnaldo Carvalho de Melo 		.mmap_pages	     = UINT_MAX,
2378d20deb64SArnaldo Carvalho de Melo 		.user_freq	     = UINT_MAX,
2379d20deb64SArnaldo Carvalho de Melo 		.user_interval	     = ULLONG_MAX,
2380447a6013SArnaldo Carvalho de Melo 		.freq		     = 4000,
2381d1cb9fceSNamhyung Kim 		.target		     = {
2382d1cb9fceSNamhyung Kim 			.uses_mmap   = true,
23833aa5939dSAdrian Hunter 			.default_per_cpu = true,
2384d1cb9fceSNamhyung Kim 		},
2385470530bbSAlexey Budankov 		.mmap_flush          = MMAP_FLUSH_DEFAULT,
2386d99c22eaSStephane Eranian 		.nr_threads_synthesize = 1,
23871d078ccbSAlexey Budankov 		.ctl_fd              = -1,
23881d078ccbSAlexey Budankov 		.ctl_fd_ack          = -1,
2389d20deb64SArnaldo Carvalho de Melo 	},
2390e3d59112SNamhyung Kim 	.tool = {
2391e3d59112SNamhyung Kim 		.sample		= process_sample_event,
2392e3d59112SNamhyung Kim 		.fork		= perf_event__process_fork,
2393cca8482cSAdrian Hunter 		.exit		= perf_event__process_exit,
2394e3d59112SNamhyung Kim 		.comm		= perf_event__process_comm,
2395f3b3614aSHari Bathini 		.namespaces	= perf_event__process_namespaces,
23966e0a9b3dSArnaldo Carvalho de Melo 		.mmap		= build_id__process_mmap,
23976e0a9b3dSArnaldo Carvalho de Melo 		.mmap2		= build_id__process_mmap2,
2398cca8482cSAdrian Hunter 		.ordered_events	= true,
2399e3d59112SNamhyung Kim 	},
2400d20deb64SArnaldo Carvalho de Melo };
24017865e817SFrederic Weisbecker 
240276a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
240376a26549SNamhyung Kim 	"\n\t\t\t\tDefault: fp";
240461eaa3beSArnaldo Carvalho de Melo 
24050aab2136SWang Nan static bool dry_run;
24060aab2136SWang Nan 
2407d20deb64SArnaldo Carvalho de Melo /*
2408d20deb64SArnaldo Carvalho de Melo  * XXX Will stay a global variable till we fix builtin-script.c to stop messing
2409d20deb64SArnaldo Carvalho de Melo  * with it and switch to use the library functions in perf_evlist that came
2410b4006796SArnaldo Carvalho de Melo  * from builtin-record.c, i.e. use record_opts,
24117b392ef0SArnaldo Carvalho de Melo  * evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
2412d20deb64SArnaldo Carvalho de Melo  * using pipes, etc.
2413d20deb64SArnaldo Carvalho de Melo  */
2414efd21307SJiri Olsa static struct option __record_options[] = {
2415d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &record.evlist, "event",
241686470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
2417f120f9d5SJiri Olsa 		     parse_events_option),
2418d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK(0, "filter", &record.evlist, "filter",
2419c171b552SLi Zefan 		     "event filter", parse_filter),
24204ba1faa1SWang Nan 	OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
24214ba1faa1SWang Nan 			   NULL, "don't record events from perf itself",
24224ba1faa1SWang Nan 			   exclude_perf),
2423bea03405SNamhyung Kim 	OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
2424d6d901c2SZhang, Yanmin 		    "record events on existing process id"),
2425bea03405SNamhyung Kim 	OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
2426d6d901c2SZhang, Yanmin 		    "record events on existing thread id"),
2427d20deb64SArnaldo Carvalho de Melo 	OPT_INTEGER('r', "realtime", &record.realtime_prio,
242886470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
2429509051eaSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
2430acac03faSKirill Smelkov 		    "collect data without buffering"),
2431d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
2432daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
2433bea03405SNamhyung Kim 	OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
243486470930SIngo Molnar 			    "system-wide collection from all CPUs"),
2435bea03405SNamhyung Kim 	OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
2436c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
2437d20deb64SArnaldo Carvalho de Melo 	OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
24382d4f2799SJiri Olsa 	OPT_STRING('o', "output", &record.data.path, "file",
243986470930SIngo Molnar 		    "output file name"),
244069e7e5b0SAdrian Hunter 	OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
244169e7e5b0SAdrian Hunter 			&record.opts.no_inherit_set,
24422e6cdf99SStephane Eranian 			"child tasks do not inherit counters"),
24434ea648aeSWang Nan 	OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
24444ea648aeSWang Nan 		    "synthesize non-sample events at the end of output"),
2445626a6b78SWang Nan 	OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
2446a060c1f1SWei Li 	OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"),
2447b09c2364SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq,
2448b09c2364SArnaldo Carvalho de Melo 		    "Fail if the specified frequency can't be used"),
244967230479SArnaldo Carvalho de Melo 	OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'",
245067230479SArnaldo Carvalho de Melo 		     "profile at this frequency",
245167230479SArnaldo Carvalho de Melo 		      record__parse_freq),
2452e9db1310SAdrian Hunter 	OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
2453e9db1310SAdrian Hunter 		     "number of mmap data pages and AUX area tracing mmap pages",
2454e9db1310SAdrian Hunter 		     record__parse_mmap_pages),
2455470530bbSAlexey Budankov 	OPT_CALLBACK(0, "mmap-flush", &record.opts, "number",
2456470530bbSAlexey Budankov 		     "Minimal number of bytes that is extracted from mmap data pages (default: 1)",
2457470530bbSAlexey Budankov 		     record__mmap_flush_parse),
2458d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "group", &record.opts.group,
245943bece79SLin Ming 		    "put the counters into a counter group"),
24602ddd5c04SArnaldo Carvalho de Melo 	OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
246109b0fd45SJiri Olsa 			   NULL, "enables call-graph recording" ,
246209b0fd45SJiri Olsa 			   &record_callchain_opt),
246309b0fd45SJiri Olsa 	OPT_CALLBACK(0, "call-graph", &record.opts,
246476a26549SNamhyung Kim 		     "record_mode[,record_size]", record_callchain_help,
246509b0fd45SJiri Olsa 		     &record_parse_callchain_opt),
2466c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
24673da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
2468b44308f5SArnaldo Carvalho de Melo 	OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
2469d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
2470649c48a9SPeter Zijlstra 		    "per thread counts"),
247156100321SPeter Zijlstra 	OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
24723b0a5daaSKan Liang 	OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr,
24733b0a5daaSKan Liang 		    "Record the sample physical addresses"),
2474542b88fdSKan Liang 	OPT_BOOLEAN(0, "data-page-size", &record.opts.sample_data_page_size,
2475542b88fdSKan Liang 		    "Record the sampled data address data page size"),
2476c1de7f3dSKan Liang 	OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size,
2477c1de7f3dSKan Liang 		    "Record the sampled code address (ip) page size"),
2478b6f35ed7SJiri Olsa 	OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
24793abebc55SAdrian Hunter 	OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
24803abebc55SAdrian Hunter 			&record.opts.sample_time_set,
24813abebc55SAdrian Hunter 			"Record the sample timestamps"),
2482f290aa1fSJiri Olsa 	OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set,
2483f290aa1fSJiri Olsa 			"Record the sample period"),
2484d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
2485649c48a9SPeter Zijlstra 		    "don't sample"),
2486d2db9a98SWang Nan 	OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
2487d2db9a98SWang Nan 			&record.no_buildid_cache_set,
2488a1ac1d3cSStephane Eranian 			"do not update the buildid cache"),
2489d2db9a98SWang Nan 	OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
2490d2db9a98SWang Nan 			&record.no_buildid_set,
2491baa2f6ceSArnaldo Carvalho de Melo 			"do not collect buildids in perf.data"),
2492d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
2493023695d9SStephane Eranian 		     "monitor event in cgroup name only",
2494023695d9SStephane Eranian 		     parse_cgroups),
249568cd3b45SAlexey Budankov 	OPT_INTEGER('D', "delay", &record.opts.initial_delay,
249668cd3b45SAlexey Budankov 		  "ms to wait before starting measurement after program start (-1: start with events disabled)"),
2497eeb399b5SAdrian Hunter 	OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"),
2498bea03405SNamhyung Kim 	OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
2499bea03405SNamhyung Kim 		   "user to profile"),
2500a5aabdacSStephane Eranian 
2501a5aabdacSStephane Eranian 	OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
2502a5aabdacSStephane Eranian 		     "branch any", "sample any taken branches",
2503a5aabdacSStephane Eranian 		     parse_branch_stack),
2504a5aabdacSStephane Eranian 
2505a5aabdacSStephane Eranian 	OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
2506a5aabdacSStephane Eranian 		     "branch filter mask", "branch stack filter modes",
2507bdfebd84SRoberto Agostino Vitillo 		     parse_branch_stack),
250805484298SAndi Kleen 	OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
250905484298SAndi Kleen 		    "sample by weight (on special events only)"),
2510475eeab9SAndi Kleen 	OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
2511475eeab9SAndi Kleen 		    "sample transaction flags (special events only)"),
25123aa5939dSAdrian Hunter 	OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
25133aa5939dSAdrian Hunter 		    "use per-thread mmaps"),
2514bcc84ec6SStephane Eranian 	OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
2515bcc84ec6SStephane Eranian 		    "sample selected machine registers on interrupt,"
2516aeea9062SKan Liang 		    " use '-I?' to list register names", parse_intr_regs),
251784c41742SAndi Kleen 	OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register",
251884c41742SAndi Kleen 		    "sample selected machine registers on interrupt,"
2519aeea9062SKan Liang 		    " use '--user-regs=?' to list register names", parse_user_regs),
252085c273d2SAndi Kleen 	OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
252185c273d2SAndi Kleen 		    "Record running/enabled time of read (:S) events"),
2522814c8c38SPeter Zijlstra 	OPT_CALLBACK('k', "clockid", &record.opts,
2523814c8c38SPeter Zijlstra 	"clockid", "clockid to use for events, see clock_gettime()",
2524814c8c38SPeter Zijlstra 	parse_clockid),
25252dd6d8a1SAdrian Hunter 	OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
25262dd6d8a1SAdrian Hunter 			  "opts", "AUX area tracing Snapshot Mode", ""),
2527c0a6de06SAdrian Hunter 	OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts,
2528c0a6de06SAdrian Hunter 			  "opts", "sample AUX area", ""),
25293fcb10e4SMark Drayton 	OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
25309d9cad76SKan Liang 			"per thread proc mmap processing timeout in ms"),
2531f3b3614aSHari Bathini 	OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces,
2532f3b3614aSHari Bathini 		    "Record namespaces events"),
25338fb4b679SNamhyung Kim 	OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup,
25348fb4b679SNamhyung Kim 		    "Record cgroup events"),
253516b4b4e1SAdrian Hunter 	OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events,
253616b4b4e1SAdrian Hunter 			&record.opts.record_switch_events_set,
2537b757bb09SAdrian Hunter 			"Record context switch events"),
253885723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
253985723885SJiri Olsa 			 "Configure all used events to run in kernel space.",
254085723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
254185723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
254285723885SJiri Olsa 			 "Configure all used events to run in user space.",
254385723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
254453651b28Syuzhoujian 	OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains,
254553651b28Syuzhoujian 		    "collect kernel callchains"),
254653651b28Syuzhoujian 	OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains,
254753651b28Syuzhoujian 		    "collect user callchains"),
254871dc2326SWang Nan 	OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
254971dc2326SWang Nan 		   "clang binary to use for compiling BPF scriptlets"),
255071dc2326SWang Nan 	OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
255171dc2326SWang Nan 		   "options passed to clang when compiling BPF scriptlets"),
25527efe0e03SHe Kuang 	OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
25537efe0e03SHe Kuang 		   "file", "vmlinux pathname"),
25546156681bSNamhyung Kim 	OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
25556156681bSNamhyung Kim 		    "Record build-id of all DSOs regardless of hits"),
2556e29386c8SJiri Olsa 	OPT_BOOLEAN(0, "buildid-mmap", &record.buildid_mmap,
2557e29386c8SJiri Olsa 		    "Record build-id in map events"),
2558ecfd7a9cSWang Nan 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
2559ecfd7a9cSWang Nan 		    "append timestamp to output filename"),
256068588bafSJin Yao 	OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary,
256168588bafSJin Yao 		    "Record timestamp boundary (time of first/last samples)"),
2562cb4e1ebbSJiri Olsa 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
2563c38dab7dSAndi Kleen 			  &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
2564c38dab7dSAndi Kleen 			  "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
2565dc0c6127SJiri Olsa 			  "signal"),
2566899e5ffbSArnaldo Carvalho de Melo 	OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event",
2567899e5ffbSArnaldo Carvalho de Melo 			 "switch output event selector. use 'perf list' to list available events",
2568899e5ffbSArnaldo Carvalho de Melo 			 parse_events_option_new_evlist),
256903724b2eSAndi Kleen 	OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
257003724b2eSAndi Kleen 		   "Limit number of switch output generated files"),
25710aab2136SWang Nan 	OPT_BOOLEAN(0, "dry-run", &dry_run,
25720aab2136SWang Nan 		    "Parse options then exit"),
2573d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
257493f20c0fSAlexey Budankov 	OPT_CALLBACK_OPTARG(0, "aio", &record.opts,
257593f20c0fSAlexey Budankov 		     &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
2576d3d1af6fSAlexey Budankov 		     record__aio_parse),
2577d3d1af6fSAlexey Budankov #endif
2578f4fe11b7SAlexey Budankov 	OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
2579f4fe11b7SAlexey Budankov 		     "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
2580f4fe11b7SAlexey Budankov 		     record__parse_affinity),
2581504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
2582504c1ad1SAlexey Budankov 	OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default,
2583504c1ad1SAlexey Budankov 			    "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
2584504c1ad1SAlexey Budankov 			    record__parse_comp_level),
2585504c1ad1SAlexey Budankov #endif
25866d575816SJiwei Sun 	OPT_CALLBACK(0, "max-size", &record.output_max_size,
25876d575816SJiwei Sun 		     "size", "Limit the maximum size of the output file", parse_output_max_size),
2588d99c22eaSStephane Eranian 	OPT_UINTEGER(0, "num-thread-synthesize",
2589d99c22eaSStephane Eranian 		     &record.opts.nr_threads_synthesize,
2590d99c22eaSStephane Eranian 		     "number of threads to run for event synthesis"),
259170943490SStephane Eranian #ifdef HAVE_LIBPFM
259270943490SStephane Eranian 	OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
259370943490SStephane Eranian 		"libpfm4 event selector. use 'perf list' to list available events",
259470943490SStephane Eranian 		parse_libpfm_events_option),
259570943490SStephane Eranian #endif
2596a8fcbd26SAdrian Hunter 	OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
2597d20aff15SAdrian Hunter 		     "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n"
2598d20aff15SAdrian Hunter 		     "\t\t\t  'snapshot': AUX area tracing snapshot).\n"
2599a8fcbd26SAdrian Hunter 		     "\t\t\t  Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
2600a8fcbd26SAdrian Hunter 		     "\t\t\t  Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
26011d078ccbSAlexey Budankov 		      parse_control_option),
260286470930SIngo Molnar 	OPT_END()
260386470930SIngo Molnar };
260486470930SIngo Molnar 
2605e5b2c207SNamhyung Kim struct option *record_options = __record_options;
2606e5b2c207SNamhyung Kim 
2607b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv)
260886470930SIngo Molnar {
2609ef149c25SAdrian Hunter 	int err;
26108c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = &record;
261116ad2ffbSNamhyung Kim 	char errbuf[BUFSIZ];
261286470930SIngo Molnar 
261367230479SArnaldo Carvalho de Melo 	setlocale(LC_ALL, "");
261467230479SArnaldo Carvalho de Melo 
261548e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT
261648e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
261748e1cab1SWang Nan 	set_nobuild('\0', "clang-path", true);
261848e1cab1SWang Nan 	set_nobuild('\0', "clang-opt", true);
261948e1cab1SWang Nan # undef set_nobuild
262048e1cab1SWang Nan #endif
262148e1cab1SWang Nan 
26227efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE
26237efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT)
26247efe0e03SHe Kuang #  define REASON  "NO_DWARF=1"
26257efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT)
26267efe0e03SHe Kuang #  define REASON  "NO_LIBBPF=1"
26277efe0e03SHe Kuang # else
26287efe0e03SHe Kuang #  define REASON  "this architecture doesn't support BPF prologue"
26297efe0e03SHe Kuang # endif
26307efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
26317efe0e03SHe Kuang 	set_nobuild('\0', "vmlinux", true);
26327efe0e03SHe Kuang # undef set_nobuild
26337efe0e03SHe Kuang # undef REASON
26347efe0e03SHe Kuang #endif
26357efe0e03SHe Kuang 
26369d2ed645SAlexey Budankov 	rec->opts.affinity = PERF_AFFINITY_SYS;
26379d2ed645SAlexey Budankov 
26380f98b11cSJiri Olsa 	rec->evlist = evlist__new();
26393e2be2daSArnaldo Carvalho de Melo 	if (rec->evlist == NULL)
2640361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
2641361c99a6SArnaldo Carvalho de Melo 
2642ecc4c561SArnaldo Carvalho de Melo 	err = perf_config(perf_record_config, rec);
2643ecc4c561SArnaldo Carvalho de Melo 	if (err)
2644ecc4c561SArnaldo Carvalho de Melo 		return err;
2645eb853e80SJiri Olsa 
2646bca647aaSTom Zanussi 	argc = parse_options(argc, argv, record_options, record_usage,
2647a0541234SAnton Blanchard 			    PARSE_OPT_STOP_AT_NON_OPTION);
264868ba3235SNamhyung Kim 	if (quiet)
264968ba3235SNamhyung Kim 		perf_quiet_option();
2650483635a9SJiri Olsa 
2651483635a9SJiri Olsa 	/* Make system wide (-a) the default target. */
2652602ad878SArnaldo Carvalho de Melo 	if (!argc && target__none(&rec->opts.target))
2653483635a9SJiri Olsa 		rec->opts.target.system_wide = true;
265486470930SIngo Molnar 
2655bea03405SNamhyung Kim 	if (nr_cgroups && !rec->opts.target.system_wide) {
2656c7118369SNamhyung Kim 		usage_with_options_msg(record_usage, record_options,
2657c7118369SNamhyung Kim 			"cgroup monitoring only available in system-wide mode");
2658c7118369SNamhyung Kim 
2659023695d9SStephane Eranian 	}
2660504c1ad1SAlexey Budankov 
2661e29386c8SJiri Olsa 	if (rec->buildid_mmap) {
2662e29386c8SJiri Olsa 		if (!perf_can_record_build_id()) {
2663e29386c8SJiri Olsa 			pr_err("Failed: no support to record build id in mmap events, update your kernel.\n");
2664e29386c8SJiri Olsa 			err = -EINVAL;
2665e29386c8SJiri Olsa 			goto out_opts;
2666e29386c8SJiri Olsa 		}
2667e29386c8SJiri Olsa 		pr_debug("Enabling build id in mmap2 events.\n");
2668e29386c8SJiri Olsa 		/* Enable mmap build id synthesizing. */
2669e29386c8SJiri Olsa 		symbol_conf.buildid_mmap2 = true;
2670e29386c8SJiri Olsa 		/* Enable perf_event_attr::build_id bit. */
2671e29386c8SJiri Olsa 		rec->opts.build_id = true;
2672e29386c8SJiri Olsa 		/* Disable build id cache. */
2673e29386c8SJiri Olsa 		rec->no_buildid = true;
2674e29386c8SJiri Olsa 	}
2675e29386c8SJiri Olsa 
2676eeb399b5SAdrian Hunter 	if (rec->opts.kcore)
2677eeb399b5SAdrian Hunter 		rec->data.is_dir = true;
2678eeb399b5SAdrian Hunter 
2679504c1ad1SAlexey Budankov 	if (rec->opts.comp_level != 0) {
2680504c1ad1SAlexey Budankov 		pr_debug("Compression enabled, disabling build id collection at the end of the session.\n");
2681504c1ad1SAlexey Budankov 		rec->no_buildid = true;
2682504c1ad1SAlexey Budankov 	}
2683504c1ad1SAlexey Budankov 
2684b757bb09SAdrian Hunter 	if (rec->opts.record_switch_events &&
2685b757bb09SAdrian Hunter 	    !perf_can_record_switch_events()) {
2686c7118369SNamhyung Kim 		ui__error("kernel does not support recording context switch events\n");
2687c7118369SNamhyung Kim 		parse_options_usage(record_usage, record_options, "switch-events", 0);
2688a8fcbd26SAdrian Hunter 		err = -EINVAL;
2689a8fcbd26SAdrian Hunter 		goto out_opts;
2690b757bb09SAdrian Hunter 	}
2691023695d9SStephane Eranian 
2692cb4e1ebbSJiri Olsa 	if (switch_output_setup(rec)) {
2693cb4e1ebbSJiri Olsa 		parse_options_usage(record_usage, record_options, "switch-output", 0);
2694a8fcbd26SAdrian Hunter 		err = -EINVAL;
2695a8fcbd26SAdrian Hunter 		goto out_opts;
2696cb4e1ebbSJiri Olsa 	}
2697cb4e1ebbSJiri Olsa 
2698bfacbe3bSJiri Olsa 	if (rec->switch_output.time) {
2699bfacbe3bSJiri Olsa 		signal(SIGALRM, alarm_sig_handler);
2700bfacbe3bSJiri Olsa 		alarm(rec->switch_output.time);
2701bfacbe3bSJiri Olsa 	}
2702bfacbe3bSJiri Olsa 
270303724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
270403724b2eSAndi Kleen 		rec->switch_output.filenames = calloc(sizeof(char *),
270503724b2eSAndi Kleen 						      rec->switch_output.num_files);
2706a8fcbd26SAdrian Hunter 		if (!rec->switch_output.filenames) {
2707a8fcbd26SAdrian Hunter 			err = -EINVAL;
2708a8fcbd26SAdrian Hunter 			goto out_opts;
2709a8fcbd26SAdrian Hunter 		}
271003724b2eSAndi Kleen 	}
271103724b2eSAndi Kleen 
27121b36c03eSAdrian Hunter 	/*
27131b36c03eSAdrian Hunter 	 * Allow aliases to facilitate the lookup of symbols for address
27141b36c03eSAdrian Hunter 	 * filters. Refer to auxtrace_parse_filters().
27151b36c03eSAdrian Hunter 	 */
27161b36c03eSAdrian Hunter 	symbol_conf.allow_aliases = true;
27171b36c03eSAdrian Hunter 
27181b36c03eSAdrian Hunter 	symbol__init(NULL);
27191b36c03eSAdrian Hunter 
27208384a260SAlexey Budankov 	if (rec->opts.affinity != PERF_AFFINITY_SYS) {
27218384a260SAlexey Budankov 		rec->affinity_mask.nbits = cpu__max_cpu();
27228384a260SAlexey Budankov 		rec->affinity_mask.bits = bitmap_alloc(rec->affinity_mask.nbits);
27238384a260SAlexey Budankov 		if (!rec->affinity_mask.bits) {
27248384a260SAlexey Budankov 			pr_err("Failed to allocate thread mask for %zd cpus\n", rec->affinity_mask.nbits);
2725a8fcbd26SAdrian Hunter 			err = -ENOMEM;
2726a8fcbd26SAdrian Hunter 			goto out_opts;
27278384a260SAlexey Budankov 		}
27288384a260SAlexey Budankov 		pr_debug2("thread mask[%zd]: empty\n", rec->affinity_mask.nbits);
27298384a260SAlexey Budankov 	}
27308384a260SAlexey Budankov 
27314b5ea3bdSAdrian Hunter 	err = record__auxtrace_init(rec);
27321b36c03eSAdrian Hunter 	if (err)
27331b36c03eSAdrian Hunter 		goto out;
27341b36c03eSAdrian Hunter 
27350aab2136SWang Nan 	if (dry_run)
27365c01ad60SAdrian Hunter 		goto out;
27370aab2136SWang Nan 
2738d7888573SWang Nan 	err = bpf__setup_stdout(rec->evlist);
2739d7888573SWang Nan 	if (err) {
2740d7888573SWang Nan 		bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
2741d7888573SWang Nan 		pr_err("ERROR: Setup BPF stdout failed: %s\n",
2742d7888573SWang Nan 			 errbuf);
27435c01ad60SAdrian Hunter 		goto out;
2744d7888573SWang Nan 	}
2745d7888573SWang Nan 
2746ef149c25SAdrian Hunter 	err = -ENOMEM;
2747ef149c25SAdrian Hunter 
27480c1d46a8SWang Nan 	if (rec->no_buildid_cache || rec->no_buildid) {
2749a1ac1d3cSStephane Eranian 		disable_buildid_cache();
2750dc0c6127SJiri Olsa 	} else if (rec->switch_output.enabled) {
27510c1d46a8SWang Nan 		/*
27520c1d46a8SWang Nan 		 * In 'perf record --switch-output', disable buildid
27530c1d46a8SWang Nan 		 * generation by default to reduce data file switching
27540c1d46a8SWang Nan 		 * overhead. Still generate buildid if they are required
27550c1d46a8SWang Nan 		 * explicitly using
27560c1d46a8SWang Nan 		 *
275760437ac0SJiri Olsa 		 *  perf record --switch-output --no-no-buildid \
27580c1d46a8SWang Nan 		 *              --no-no-buildid-cache
27590c1d46a8SWang Nan 		 *
27600c1d46a8SWang Nan 		 * Following code equals to:
27610c1d46a8SWang Nan 		 *
27620c1d46a8SWang Nan 		 * if ((rec->no_buildid || !rec->no_buildid_set) &&
27630c1d46a8SWang Nan 		 *     (rec->no_buildid_cache || !rec->no_buildid_cache_set))
27640c1d46a8SWang Nan 		 *         disable_buildid_cache();
27650c1d46a8SWang Nan 		 */
27660c1d46a8SWang Nan 		bool disable = true;
27670c1d46a8SWang Nan 
27680c1d46a8SWang Nan 		if (rec->no_buildid_set && !rec->no_buildid)
27690c1d46a8SWang Nan 			disable = false;
27700c1d46a8SWang Nan 		if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
27710c1d46a8SWang Nan 			disable = false;
27720c1d46a8SWang Nan 		if (disable) {
27730c1d46a8SWang Nan 			rec->no_buildid = true;
27740c1d46a8SWang Nan 			rec->no_buildid_cache = true;
27750c1d46a8SWang Nan 			disable_buildid_cache();
27760c1d46a8SWang Nan 		}
27770c1d46a8SWang Nan 	}
2778655000e7SArnaldo Carvalho de Melo 
27794ea648aeSWang Nan 	if (record.opts.overwrite)
27804ea648aeSWang Nan 		record.opts.tail_synthesize = true;
27814ea648aeSWang Nan 
27826484d2f9SJiri Olsa 	if (rec->evlist->core.nr_entries == 0 &&
2783e251abeeSArnaldo Carvalho de Melo 	    __evlist__add_default(rec->evlist, !record.opts.no_samples) < 0) {
278469aad6f1SArnaldo Carvalho de Melo 		pr_err("Not enough memory for event selector list\n");
2785394c01edSAdrian Hunter 		goto out;
2786bbd36e5eSPeter Zijlstra 	}
278786470930SIngo Molnar 
278869e7e5b0SAdrian Hunter 	if (rec->opts.target.tid && !rec->opts.no_inherit_set)
278969e7e5b0SAdrian Hunter 		rec->opts.no_inherit = true;
279069e7e5b0SAdrian Hunter 
2791602ad878SArnaldo Carvalho de Melo 	err = target__validate(&rec->opts.target);
279216ad2ffbSNamhyung Kim 	if (err) {
2793602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
2794c3dec27bSJiri Olsa 		ui__warning("%s\n", errbuf);
279516ad2ffbSNamhyung Kim 	}
27964bd0f2d2SNamhyung Kim 
2797602ad878SArnaldo Carvalho de Melo 	err = target__parse_uid(&rec->opts.target);
279816ad2ffbSNamhyung Kim 	if (err) {
279916ad2ffbSNamhyung Kim 		int saved_errno = errno;
280016ad2ffbSNamhyung Kim 
2801602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
28023780f488SNamhyung Kim 		ui__error("%s", errbuf);
280316ad2ffbSNamhyung Kim 
280416ad2ffbSNamhyung Kim 		err = -saved_errno;
2805394c01edSAdrian Hunter 		goto out;
280616ad2ffbSNamhyung Kim 	}
28070d37aa34SArnaldo Carvalho de Melo 
2808ca800068SMengting Zhang 	/* Enable ignoring missing threads when -u/-p option is defined. */
2809ca800068SMengting Zhang 	rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid;
281023dc4f15SJiri Olsa 
281116ad2ffbSNamhyung Kim 	err = -ENOMEM;
28127748bb71SArnaldo Carvalho de Melo 	if (evlist__create_maps(rec->evlist, &rec->opts.target) < 0)
2813dd7927f4SArnaldo Carvalho de Melo 		usage_with_options(record_usage, record_options);
281469aad6f1SArnaldo Carvalho de Melo 
2815ef149c25SAdrian Hunter 	err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
2816ef149c25SAdrian Hunter 	if (err)
2817394c01edSAdrian Hunter 		goto out;
2818ef149c25SAdrian Hunter 
28196156681bSNamhyung Kim 	/*
28206156681bSNamhyung Kim 	 * We take all buildids when the file contains
28216156681bSNamhyung Kim 	 * AUX area tracing data because we do not decode the
28226156681bSNamhyung Kim 	 * trace because it would take too long.
28236156681bSNamhyung Kim 	 */
28246156681bSNamhyung Kim 	if (rec->opts.full_auxtrace)
28256156681bSNamhyung Kim 		rec->buildid_all = true;
28266156681bSNamhyung Kim 
2827246eba8eSAdrian Hunter 	if (rec->opts.text_poke) {
2828246eba8eSAdrian Hunter 		err = record__config_text_poke(rec->evlist);
2829246eba8eSAdrian Hunter 		if (err) {
2830246eba8eSAdrian Hunter 			pr_err("record__config_text_poke failed, error %d\n", err);
2831246eba8eSAdrian Hunter 			goto out;
2832246eba8eSAdrian Hunter 		}
2833246eba8eSAdrian Hunter 	}
2834246eba8eSAdrian Hunter 
2835b4006796SArnaldo Carvalho de Melo 	if (record_opts__config(&rec->opts)) {
283639d17dacSArnaldo Carvalho de Melo 		err = -EINVAL;
2837394c01edSAdrian Hunter 		goto out;
28387e4ff9e3SMike Galbraith 	}
28397e4ff9e3SMike Galbraith 
284093f20c0fSAlexey Budankov 	if (rec->opts.nr_cblocks > nr_cblocks_max)
284193f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = nr_cblocks_max;
28425d7f4116SAlexey Budankov 	pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks);
2843d3d1af6fSAlexey Budankov 
28449d2ed645SAlexey Budankov 	pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]);
2845470530bbSAlexey Budankov 	pr_debug("mmap flush: %d\n", rec->opts.mmap_flush);
28469d2ed645SAlexey Budankov 
284751255a8aSAlexey Budankov 	if (rec->opts.comp_level > comp_level_max)
284851255a8aSAlexey Budankov 		rec->opts.comp_level = comp_level_max;
284951255a8aSAlexey Budankov 	pr_debug("comp level: %d\n", rec->opts.comp_level);
285051255a8aSAlexey Budankov 
2851d20deb64SArnaldo Carvalho de Melo 	err = __cmd_record(&record, argc, argv);
2852394c01edSAdrian Hunter out:
28538384a260SAlexey Budankov 	bitmap_free(rec->affinity_mask.bits);
2854c12995a5SJiri Olsa 	evlist__delete(rec->evlist);
2855d65a458bSArnaldo Carvalho de Melo 	symbol__exit();
2856ef149c25SAdrian Hunter 	auxtrace_record__free(rec->itr);
2857a8fcbd26SAdrian Hunter out_opts:
2858ee7fe31eSAdrian Hunter 	evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close);
285939d17dacSArnaldo Carvalho de Melo 	return err;
286086470930SIngo Molnar }
28612dd6d8a1SAdrian Hunter 
28622dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused)
28632dd6d8a1SAdrian Hunter {
2864dc0c6127SJiri Olsa 	struct record *rec = &record;
2865dc0c6127SJiri Olsa 
2866d20aff15SAdrian Hunter 	hit_auxtrace_snapshot_trigger(rec);
28673c1cb7e3SWang Nan 
2868dc0c6127SJiri Olsa 	if (switch_output_signal(rec))
28693c1cb7e3SWang Nan 		trigger_hit(&switch_output_trigger);
28702dd6d8a1SAdrian Hunter }
2871bfacbe3bSJiri Olsa 
2872bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused)
2873bfacbe3bSJiri Olsa {
2874bfacbe3bSJiri Olsa 	struct record *rec = &record;
2875bfacbe3bSJiri Olsa 
2876bfacbe3bSJiri Olsa 	if (switch_output_time(rec))
2877bfacbe3bSJiri Olsa 		trigger_hit(&switch_output_trigger);
2878bfacbe3bSJiri Olsa }
2879