xref: /openbmc/linux/tools/perf/builtin-record.c (revision 65aa2e6bae3658cbc84c2e628a5c0ca163686204)
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"
3771dc2326SWang Nan #include "util/llvm-utils.h"
388690a2a7SWang Nan #include "util/bpf-loader.h"
395f9cf599SWang Nan #include "util/trigger.h"
40a074865eSWang Nan #include "util/perf-hooks.h"
41f13de660SAlexey Budankov #include "util/cpu-set-sched.h"
42ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h"
43c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h"
4458db1d6eSArnaldo Carvalho de Melo #include "util/units.h"
457b612e29SSong Liu #include "util/bpf-event.h"
46d8871ea7SWang Nan #include "asm/bug.h"
47c1a604dfSArnaldo Carvalho de Melo #include "perf.h"
487c6a1c65SPeter Zijlstra 
49a43783aeSArnaldo Carvalho de Melo #include <errno.h>
50fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
5167230479SArnaldo Carvalho de Melo #include <locale.h>
524208735dSArnaldo Carvalho de Melo #include <poll.h>
5386470930SIngo Molnar #include <unistd.h>
5486470930SIngo Molnar #include <sched.h>
559607ad3aSArnaldo Carvalho de Melo #include <signal.h>
56a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h>
574208735dSArnaldo Carvalho de Melo #include <sys/wait.h>
586ef81c55SMamatha Inamdar #include <linux/err.h>
598520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
600693e680SArnaldo Carvalho de Melo #include <linux/time64.h>
61d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
6278da39faSBernhard Rosenkraenzer 
631b43b704SJiri Olsa struct switch_output {
64dc0c6127SJiri Olsa 	bool		 enabled;
651b43b704SJiri Olsa 	bool		 signal;
66dc0c6127SJiri Olsa 	unsigned long	 size;
67bfacbe3bSJiri Olsa 	unsigned long	 time;
68cb4e1ebbSJiri Olsa 	const char	*str;
69cb4e1ebbSJiri Olsa 	bool		 set;
7003724b2eSAndi Kleen 	char		 **filenames;
7103724b2eSAndi Kleen 	int		 num_files;
7203724b2eSAndi Kleen 	int		 cur_file;
731b43b704SJiri Olsa };
741b43b704SJiri Olsa 
758c6f45a7SArnaldo Carvalho de Melo struct record {
7645694aa7SArnaldo Carvalho de Melo 	struct perf_tool	tool;
77b4006796SArnaldo Carvalho de Melo 	struct record_opts	opts;
78d20deb64SArnaldo Carvalho de Melo 	u64			bytes_written;
798ceb41d7SJiri Olsa 	struct perf_data	data;
80ef149c25SAdrian Hunter 	struct auxtrace_record	*itr;
8163503dbaSJiri Olsa 	struct evlist	*evlist;
82d20deb64SArnaldo Carvalho de Melo 	struct perf_session	*session;
83d20deb64SArnaldo Carvalho de Melo 	int			realtime_prio;
84d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid;
85d2db9a98SWang Nan 	bool			no_buildid_set;
86d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid_cache;
87d2db9a98SWang Nan 	bool			no_buildid_cache_set;
886156681bSNamhyung Kim 	bool			buildid_all;
89ecfd7a9cSWang Nan 	bool			timestamp_filename;
9068588bafSJin Yao 	bool			timestamp_boundary;
911b43b704SJiri Olsa 	struct switch_output	switch_output;
929f065194SYang Shi 	unsigned long long	samples;
939d2ed645SAlexey Budankov 	cpu_set_t		affinity_mask;
940f82ebc4SArnaldo Carvalho de Melo };
9586470930SIngo Molnar 
96dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started;
97dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
98dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger);
99dc0c6127SJiri Olsa 
1009d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = {
1019d2ed645SAlexey Budankov 	"SYS", "NODE", "CPU"
1029d2ed645SAlexey Budankov };
1039d2ed645SAlexey Budankov 
104dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec)
105dc0c6127SJiri Olsa {
106dc0c6127SJiri Olsa 	return rec->switch_output.signal &&
107dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
108dc0c6127SJiri Olsa }
109dc0c6127SJiri Olsa 
110dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec)
111dc0c6127SJiri Olsa {
112dc0c6127SJiri Olsa 	return rec->switch_output.size &&
113dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger) &&
114dc0c6127SJiri Olsa 	       (rec->bytes_written >= rec->switch_output.size);
115dc0c6127SJiri Olsa }
116dc0c6127SJiri Olsa 
117bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec)
118bfacbe3bSJiri Olsa {
119bfacbe3bSJiri Olsa 	return rec->switch_output.time &&
120bfacbe3bSJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
121bfacbe3bSJiri Olsa }
122bfacbe3bSJiri Olsa 
123a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused,
124ded2b8feSJiri Olsa 			 void *bf, size_t size)
125f5970550SPeter Zijlstra {
126ded2b8feSJiri Olsa 	struct perf_data_file *file = &rec->session->data->file;
127ded2b8feSJiri Olsa 
128ded2b8feSJiri Olsa 	if (perf_data_file__write(file, bf, size) < 0) {
1294f624685SAdrian Hunter 		pr_err("failed to write perf data, error: %m\n");
1308d3eca20SDavid Ahern 		return -1;
1318d3eca20SDavid Ahern 	}
132f5970550SPeter Zijlstra 
133cf8b2e69SArnaldo Carvalho de Melo 	rec->bytes_written += size;
134dc0c6127SJiri Olsa 
135dc0c6127SJiri Olsa 	if (switch_output_size(rec))
136dc0c6127SJiri Olsa 		trigger_hit(&switch_output_trigger);
137dc0c6127SJiri Olsa 
1388d3eca20SDavid Ahern 	return 0;
139f5970550SPeter Zijlstra }
140f5970550SPeter Zijlstra 
141ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec);
142ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec);
1435d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size,
1445d7f4116SAlexey Budankov 			    void *src, size_t src_size);
1455d7f4116SAlexey Budankov 
146d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
147d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd,
148d3d1af6fSAlexey Budankov 		void *buf, size_t size, off_t off)
149d3d1af6fSAlexey Budankov {
150d3d1af6fSAlexey Budankov 	int rc;
151d3d1af6fSAlexey Budankov 
152d3d1af6fSAlexey Budankov 	cblock->aio_fildes = trace_fd;
153d3d1af6fSAlexey Budankov 	cblock->aio_buf    = buf;
154d3d1af6fSAlexey Budankov 	cblock->aio_nbytes = size;
155d3d1af6fSAlexey Budankov 	cblock->aio_offset = off;
156d3d1af6fSAlexey Budankov 	cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
157d3d1af6fSAlexey Budankov 
158d3d1af6fSAlexey Budankov 	do {
159d3d1af6fSAlexey Budankov 		rc = aio_write(cblock);
160d3d1af6fSAlexey Budankov 		if (rc == 0) {
161d3d1af6fSAlexey Budankov 			break;
162d3d1af6fSAlexey Budankov 		} else if (errno != EAGAIN) {
163d3d1af6fSAlexey Budankov 			cblock->aio_fildes = -1;
164d3d1af6fSAlexey Budankov 			pr_err("failed to queue perf data, error: %m\n");
165d3d1af6fSAlexey Budankov 			break;
166d3d1af6fSAlexey Budankov 		}
167d3d1af6fSAlexey Budankov 	} while (1);
168d3d1af6fSAlexey Budankov 
169d3d1af6fSAlexey Budankov 	return rc;
170d3d1af6fSAlexey Budankov }
171d3d1af6fSAlexey Budankov 
172a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock)
173d3d1af6fSAlexey Budankov {
174d3d1af6fSAlexey Budankov 	void *rem_buf;
175d3d1af6fSAlexey Budankov 	off_t rem_off;
176d3d1af6fSAlexey Budankov 	size_t rem_size;
177d3d1af6fSAlexey Budankov 	int rc, aio_errno;
178d3d1af6fSAlexey Budankov 	ssize_t aio_ret, written;
179d3d1af6fSAlexey Budankov 
180d3d1af6fSAlexey Budankov 	aio_errno = aio_error(cblock);
181d3d1af6fSAlexey Budankov 	if (aio_errno == EINPROGRESS)
182d3d1af6fSAlexey Budankov 		return 0;
183d3d1af6fSAlexey Budankov 
184d3d1af6fSAlexey Budankov 	written = aio_ret = aio_return(cblock);
185d3d1af6fSAlexey Budankov 	if (aio_ret < 0) {
186d3d1af6fSAlexey Budankov 		if (aio_errno != EINTR)
187d3d1af6fSAlexey Budankov 			pr_err("failed to write perf data, error: %m\n");
188d3d1af6fSAlexey Budankov 		written = 0;
189d3d1af6fSAlexey Budankov 	}
190d3d1af6fSAlexey Budankov 
191d3d1af6fSAlexey Budankov 	rem_size = cblock->aio_nbytes - written;
192d3d1af6fSAlexey Budankov 
193d3d1af6fSAlexey Budankov 	if (rem_size == 0) {
194d3d1af6fSAlexey Budankov 		cblock->aio_fildes = -1;
195d3d1af6fSAlexey Budankov 		/*
196ef781128SAlexey Budankov 		 * md->refcount is incremented in record__aio_pushfn() for
197ef781128SAlexey Budankov 		 * every aio write request started in record__aio_push() so
198ef781128SAlexey Budankov 		 * decrement it because the request is now complete.
199d3d1af6fSAlexey Budankov 		 */
200d3d1af6fSAlexey Budankov 		perf_mmap__put(md);
201d3d1af6fSAlexey Budankov 		rc = 1;
202d3d1af6fSAlexey Budankov 	} else {
203d3d1af6fSAlexey Budankov 		/*
204d3d1af6fSAlexey Budankov 		 * aio write request may require restart with the
205d3d1af6fSAlexey Budankov 		 * reminder if the kernel didn't write whole
206d3d1af6fSAlexey Budankov 		 * chunk at once.
207d3d1af6fSAlexey Budankov 		 */
208d3d1af6fSAlexey Budankov 		rem_off = cblock->aio_offset + written;
209d3d1af6fSAlexey Budankov 		rem_buf = (void *)(cblock->aio_buf + written);
210d3d1af6fSAlexey Budankov 		record__aio_write(cblock, cblock->aio_fildes,
211d3d1af6fSAlexey Budankov 				rem_buf, rem_size, rem_off);
212d3d1af6fSAlexey Budankov 		rc = 0;
213d3d1af6fSAlexey Budankov 	}
214d3d1af6fSAlexey Budankov 
215d3d1af6fSAlexey Budankov 	return rc;
216d3d1af6fSAlexey Budankov }
217d3d1af6fSAlexey Budankov 
218a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all)
219d3d1af6fSAlexey Budankov {
22093f20c0fSAlexey Budankov 	struct aiocb **aiocb = md->aio.aiocb;
22193f20c0fSAlexey Budankov 	struct aiocb *cblocks = md->aio.cblocks;
222d3d1af6fSAlexey Budankov 	struct timespec timeout = { 0, 1000 * 1000  * 1 }; /* 1ms */
22393f20c0fSAlexey Budankov 	int i, do_suspend;
224d3d1af6fSAlexey Budankov 
225d3d1af6fSAlexey Budankov 	do {
22693f20c0fSAlexey Budankov 		do_suspend = 0;
22793f20c0fSAlexey Budankov 		for (i = 0; i < md->aio.nr_cblocks; ++i) {
22893f20c0fSAlexey Budankov 			if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
22993f20c0fSAlexey Budankov 				if (sync_all)
23093f20c0fSAlexey Budankov 					aiocb[i] = NULL;
23193f20c0fSAlexey Budankov 				else
23293f20c0fSAlexey Budankov 					return i;
23393f20c0fSAlexey Budankov 			} else {
23493f20c0fSAlexey Budankov 				/*
23593f20c0fSAlexey Budankov 				 * Started aio write is not complete yet
23693f20c0fSAlexey Budankov 				 * so it has to be waited before the
23793f20c0fSAlexey Budankov 				 * next allocation.
23893f20c0fSAlexey Budankov 				 */
23993f20c0fSAlexey Budankov 				aiocb[i] = &cblocks[i];
24093f20c0fSAlexey Budankov 				do_suspend = 1;
24193f20c0fSAlexey Budankov 			}
24293f20c0fSAlexey Budankov 		}
24393f20c0fSAlexey Budankov 		if (!do_suspend)
24493f20c0fSAlexey Budankov 			return -1;
245d3d1af6fSAlexey Budankov 
24693f20c0fSAlexey Budankov 		while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) {
247d3d1af6fSAlexey Budankov 			if (!(errno == EAGAIN || errno == EINTR))
248d3d1af6fSAlexey Budankov 				pr_err("failed to sync perf data, error: %m\n");
249d3d1af6fSAlexey Budankov 		}
250d3d1af6fSAlexey Budankov 	} while (1);
251d3d1af6fSAlexey Budankov }
252d3d1af6fSAlexey Budankov 
253ef781128SAlexey Budankov struct record_aio {
254ef781128SAlexey Budankov 	struct record	*rec;
255ef781128SAlexey Budankov 	void		*data;
256ef781128SAlexey Budankov 	size_t		size;
257ef781128SAlexey Budankov };
258ef781128SAlexey Budankov 
259a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size)
260d3d1af6fSAlexey Budankov {
261ef781128SAlexey Budankov 	struct record_aio *aio = to;
262ef781128SAlexey Budankov 
263ef781128SAlexey Budankov 	/*
264547740f7SJiri Olsa 	 * map->core.base data pointed by buf is copied into free map->aio.data[] buffer
265ef781128SAlexey Budankov 	 * to release space in the kernel buffer as fast as possible, calling
266ef781128SAlexey Budankov 	 * perf_mmap__consume() from perf_mmap__push() function.
267ef781128SAlexey Budankov 	 *
268ef781128SAlexey Budankov 	 * That lets the kernel to proceed with storing more profiling data into
269ef781128SAlexey Budankov 	 * the kernel buffer earlier than other per-cpu kernel buffers are handled.
270ef781128SAlexey Budankov 	 *
271ef781128SAlexey Budankov 	 * Coping can be done in two steps in case the chunk of profiling data
272ef781128SAlexey Budankov 	 * crosses the upper bound of the kernel buffer. In this case we first move
273ef781128SAlexey Budankov 	 * part of data from map->start till the upper bound and then the reminder
274ef781128SAlexey Budankov 	 * from the beginning of the kernel buffer till the end of the data chunk.
275ef781128SAlexey Budankov 	 */
276ef781128SAlexey Budankov 
277ef781128SAlexey Budankov 	if (record__comp_enabled(aio->rec)) {
278ef781128SAlexey Budankov 		size = zstd_compress(aio->rec->session, aio->data + aio->size,
279ef781128SAlexey Budankov 				     perf_mmap__mmap_len(map) - aio->size,
280ef781128SAlexey Budankov 				     buf, size);
281ef781128SAlexey Budankov 	} else {
282ef781128SAlexey Budankov 		memcpy(aio->data + aio->size, buf, size);
283ef781128SAlexey Budankov 	}
284ef781128SAlexey Budankov 
285ef781128SAlexey Budankov 	if (!aio->size) {
286ef781128SAlexey Budankov 		/*
287ef781128SAlexey Budankov 		 * Increment map->refcount to guard map->aio.data[] buffer
288ef781128SAlexey Budankov 		 * from premature deallocation because map object can be
289ef781128SAlexey Budankov 		 * released earlier than aio write request started on
290ef781128SAlexey Budankov 		 * map->aio.data[] buffer is complete.
291ef781128SAlexey Budankov 		 *
292ef781128SAlexey Budankov 		 * perf_mmap__put() is done at record__aio_complete()
293ef781128SAlexey Budankov 		 * after started aio request completion or at record__aio_push()
294ef781128SAlexey Budankov 		 * if the request failed to start.
295ef781128SAlexey Budankov 		 */
296ef781128SAlexey Budankov 		perf_mmap__get(map);
297ef781128SAlexey Budankov 	}
298ef781128SAlexey Budankov 
299ef781128SAlexey Budankov 	aio->size += size;
300ef781128SAlexey Budankov 
301ef781128SAlexey Budankov 	return size;
302ef781128SAlexey Budankov }
303ef781128SAlexey Budankov 
304a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off)
305ef781128SAlexey Budankov {
306ef781128SAlexey Budankov 	int ret, idx;
307ef781128SAlexey Budankov 	int trace_fd = rec->session->data->file.fd;
308ef781128SAlexey Budankov 	struct record_aio aio = { .rec = rec, .size = 0 };
309ef781128SAlexey Budankov 
310ef781128SAlexey Budankov 	/*
311ef781128SAlexey Budankov 	 * Call record__aio_sync() to wait till map->aio.data[] buffer
312ef781128SAlexey Budankov 	 * becomes available after previous aio write operation.
313ef781128SAlexey Budankov 	 */
314ef781128SAlexey Budankov 
315ef781128SAlexey Budankov 	idx = record__aio_sync(map, false);
316ef781128SAlexey Budankov 	aio.data = map->aio.data[idx];
317ef781128SAlexey Budankov 	ret = perf_mmap__push(map, &aio, record__aio_pushfn);
318ef781128SAlexey Budankov 	if (ret != 0) /* ret > 0 - no data, ret < 0 - error */
319ef781128SAlexey Budankov 		return ret;
320d3d1af6fSAlexey Budankov 
321d3d1af6fSAlexey Budankov 	rec->samples++;
322ef781128SAlexey Budankov 	ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off);
323d3d1af6fSAlexey Budankov 	if (!ret) {
324ef781128SAlexey Budankov 		*off += aio.size;
325ef781128SAlexey Budankov 		rec->bytes_written += aio.size;
326d3d1af6fSAlexey Budankov 		if (switch_output_size(rec))
327d3d1af6fSAlexey Budankov 			trigger_hit(&switch_output_trigger);
328ef781128SAlexey Budankov 	} else {
329ef781128SAlexey Budankov 		/*
330ef781128SAlexey Budankov 		 * Decrement map->refcount incremented in record__aio_pushfn()
331ef781128SAlexey Budankov 		 * back if record__aio_write() operation failed to start, otherwise
332ef781128SAlexey Budankov 		 * map->refcount is decremented in record__aio_complete() after
333ef781128SAlexey Budankov 		 * aio write operation finishes successfully.
334ef781128SAlexey Budankov 		 */
335ef781128SAlexey Budankov 		perf_mmap__put(map);
336d3d1af6fSAlexey Budankov 	}
337d3d1af6fSAlexey Budankov 
338d3d1af6fSAlexey Budankov 	return ret;
339d3d1af6fSAlexey Budankov }
340d3d1af6fSAlexey Budankov 
341d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd)
342d3d1af6fSAlexey Budankov {
343d3d1af6fSAlexey Budankov 	return lseek(trace_fd, 0, SEEK_CUR);
344d3d1af6fSAlexey Budankov }
345d3d1af6fSAlexey Budankov 
346d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos)
347d3d1af6fSAlexey Budankov {
348d3d1af6fSAlexey Budankov 	lseek(trace_fd, pos, SEEK_SET);
349d3d1af6fSAlexey Budankov }
350d3d1af6fSAlexey Budankov 
351d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec)
352d3d1af6fSAlexey Budankov {
353d3d1af6fSAlexey Budankov 	int i;
35463503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
355a5830532SJiri Olsa 	struct mmap *maps = evlist->mmap;
356d3d1af6fSAlexey Budankov 
357ef781128SAlexey Budankov 	if (!record__aio_enabled(rec))
358d3d1af6fSAlexey Budankov 		return;
359d3d1af6fSAlexey Budankov 
360d3d1af6fSAlexey Budankov 	for (i = 0; i < evlist->nr_mmaps; i++) {
361a5830532SJiri Olsa 		struct mmap *map = &maps[i];
362d3d1af6fSAlexey Budankov 
363547740f7SJiri Olsa 		if (map->core.base)
36493f20c0fSAlexey Budankov 			record__aio_sync(map, true);
365d3d1af6fSAlexey Budankov 	}
366d3d1af6fSAlexey Budankov }
367d3d1af6fSAlexey Budankov 
368d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1;
36993f20c0fSAlexey Budankov static int nr_cblocks_max = 4;
370d3d1af6fSAlexey Budankov 
371d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt,
37293f20c0fSAlexey Budankov 			     const char *str,
373d3d1af6fSAlexey Budankov 			     int unset)
374d3d1af6fSAlexey Budankov {
375d3d1af6fSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
376d3d1af6fSAlexey Budankov 
37793f20c0fSAlexey Budankov 	if (unset) {
378d3d1af6fSAlexey Budankov 		opts->nr_cblocks = 0;
37993f20c0fSAlexey Budankov 	} else {
38093f20c0fSAlexey Budankov 		if (str)
38193f20c0fSAlexey Budankov 			opts->nr_cblocks = strtol(str, NULL, 0);
38293f20c0fSAlexey Budankov 		if (!opts->nr_cblocks)
383d3d1af6fSAlexey Budankov 			opts->nr_cblocks = nr_cblocks_default;
38493f20c0fSAlexey Budankov 	}
385d3d1af6fSAlexey Budankov 
386d3d1af6fSAlexey Budankov 	return 0;
387d3d1af6fSAlexey Budankov }
388d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */
38993f20c0fSAlexey Budankov static int nr_cblocks_max = 0;
39093f20c0fSAlexey Budankov 
391a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused,
392ef781128SAlexey Budankov 			    off_t *off __maybe_unused)
393d3d1af6fSAlexey Budankov {
394d3d1af6fSAlexey Budankov 	return -1;
395d3d1af6fSAlexey Budankov }
396d3d1af6fSAlexey Budankov 
397d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused)
398d3d1af6fSAlexey Budankov {
399d3d1af6fSAlexey Budankov 	return -1;
400d3d1af6fSAlexey Budankov }
401d3d1af6fSAlexey Budankov 
402d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused)
403d3d1af6fSAlexey Budankov {
404d3d1af6fSAlexey Budankov }
405d3d1af6fSAlexey Budankov 
406d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused)
407d3d1af6fSAlexey Budankov {
408d3d1af6fSAlexey Budankov }
409d3d1af6fSAlexey Budankov #endif
410d3d1af6fSAlexey Budankov 
411d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec)
412d3d1af6fSAlexey Budankov {
413d3d1af6fSAlexey Budankov 	return rec->opts.nr_cblocks > 0;
414d3d1af6fSAlexey Budankov }
415d3d1af6fSAlexey Budankov 
416470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1
417470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt,
418470530bbSAlexey Budankov 				    const char *str,
419470530bbSAlexey Budankov 				    int unset)
420470530bbSAlexey Budankov {
421470530bbSAlexey Budankov 	int flush_max;
422470530bbSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
423470530bbSAlexey Budankov 	static struct parse_tag tags[] = {
424470530bbSAlexey Budankov 			{ .tag  = 'B', .mult = 1       },
425470530bbSAlexey Budankov 			{ .tag  = 'K', .mult = 1 << 10 },
426470530bbSAlexey Budankov 			{ .tag  = 'M', .mult = 1 << 20 },
427470530bbSAlexey Budankov 			{ .tag  = 'G', .mult = 1 << 30 },
428470530bbSAlexey Budankov 			{ .tag  = 0 },
429470530bbSAlexey Budankov 	};
430470530bbSAlexey Budankov 
431470530bbSAlexey Budankov 	if (unset)
432470530bbSAlexey Budankov 		return 0;
433470530bbSAlexey Budankov 
434470530bbSAlexey Budankov 	if (str) {
435470530bbSAlexey Budankov 		opts->mmap_flush = parse_tag_value(str, tags);
436470530bbSAlexey Budankov 		if (opts->mmap_flush == (int)-1)
437470530bbSAlexey Budankov 			opts->mmap_flush = strtol(str, NULL, 0);
438470530bbSAlexey Budankov 	}
439470530bbSAlexey Budankov 
440470530bbSAlexey Budankov 	if (!opts->mmap_flush)
441470530bbSAlexey Budankov 		opts->mmap_flush = MMAP_FLUSH_DEFAULT;
442470530bbSAlexey Budankov 
4439521b5f2SJiri Olsa 	flush_max = evlist__mmap_size(opts->mmap_pages);
444470530bbSAlexey Budankov 	flush_max /= 4;
445470530bbSAlexey Budankov 	if (opts->mmap_flush > flush_max)
446470530bbSAlexey Budankov 		opts->mmap_flush = flush_max;
447470530bbSAlexey Budankov 
448470530bbSAlexey Budankov 	return 0;
449470530bbSAlexey Budankov }
450470530bbSAlexey Budankov 
451504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
452504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1;
453504c1ad1SAlexey Budankov 
454504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset)
455504c1ad1SAlexey Budankov {
456504c1ad1SAlexey Budankov 	struct record_opts *opts = opt->value;
457504c1ad1SAlexey Budankov 
458504c1ad1SAlexey Budankov 	if (unset) {
459504c1ad1SAlexey Budankov 		opts->comp_level = 0;
460504c1ad1SAlexey Budankov 	} else {
461504c1ad1SAlexey Budankov 		if (str)
462504c1ad1SAlexey Budankov 			opts->comp_level = strtol(str, NULL, 0);
463504c1ad1SAlexey Budankov 		if (!opts->comp_level)
464504c1ad1SAlexey Budankov 			opts->comp_level = comp_level_default;
465504c1ad1SAlexey Budankov 	}
466504c1ad1SAlexey Budankov 
467504c1ad1SAlexey Budankov 	return 0;
468504c1ad1SAlexey Budankov }
469504c1ad1SAlexey Budankov #endif
47051255a8aSAlexey Budankov static unsigned int comp_level_max = 22;
47151255a8aSAlexey Budankov 
47242e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec)
47342e1fd80SAlexey Budankov {
47442e1fd80SAlexey Budankov 	return rec->opts.comp_level > 0;
47542e1fd80SAlexey Budankov }
47642e1fd80SAlexey Budankov 
47745694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool,
478d20deb64SArnaldo Carvalho de Melo 				     union perf_event *event,
4791d037ca1SIrina Tirdea 				     struct perf_sample *sample __maybe_unused,
4801d037ca1SIrina Tirdea 				     struct machine *machine __maybe_unused)
481234fbbf5SArnaldo Carvalho de Melo {
4828c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = container_of(tool, struct record, tool);
483ded2b8feSJiri Olsa 	return record__write(rec, NULL, event, event->header.size);
484234fbbf5SArnaldo Carvalho de Melo }
485234fbbf5SArnaldo Carvalho de Melo 
486a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
487d37f1586SArnaldo Carvalho de Melo {
488d37f1586SArnaldo Carvalho de Melo 	struct record *rec = to;
489d37f1586SArnaldo Carvalho de Melo 
4905d7f4116SAlexey Budankov 	if (record__comp_enabled(rec)) {
4915d7f4116SAlexey Budankov 		size = zstd_compress(rec->session, map->data, perf_mmap__mmap_len(map), bf, size);
4925d7f4116SAlexey Budankov 		bf   = map->data;
4935d7f4116SAlexey Budankov 	}
4945d7f4116SAlexey Budankov 
495d37f1586SArnaldo Carvalho de Melo 	rec->samples++;
496ded2b8feSJiri Olsa 	return record__write(rec, map, bf, size);
497d37f1586SArnaldo Carvalho de Melo }
498d37f1586SArnaldo Carvalho de Melo 
4992dd6d8a1SAdrian Hunter static volatile int done;
5002dd6d8a1SAdrian Hunter static volatile int signr = -1;
5012dd6d8a1SAdrian Hunter static volatile int child_finished;
502c0bdc1c4SWang Nan 
5032dd6d8a1SAdrian Hunter static void sig_handler(int sig)
5042dd6d8a1SAdrian Hunter {
5052dd6d8a1SAdrian Hunter 	if (sig == SIGCHLD)
5062dd6d8a1SAdrian Hunter 		child_finished = 1;
5072dd6d8a1SAdrian Hunter 	else
5082dd6d8a1SAdrian Hunter 		signr = sig;
5092dd6d8a1SAdrian Hunter 
5102dd6d8a1SAdrian Hunter 	done = 1;
5112dd6d8a1SAdrian Hunter }
5122dd6d8a1SAdrian Hunter 
513a074865eSWang Nan static void sigsegv_handler(int sig)
514a074865eSWang Nan {
515a074865eSWang Nan 	perf_hooks__recover();
516a074865eSWang Nan 	sighandler_dump_stack(sig);
517a074865eSWang Nan }
518a074865eSWang Nan 
5192dd6d8a1SAdrian Hunter static void record__sig_exit(void)
5202dd6d8a1SAdrian Hunter {
5212dd6d8a1SAdrian Hunter 	if (signr == -1)
5222dd6d8a1SAdrian Hunter 		return;
5232dd6d8a1SAdrian Hunter 
5242dd6d8a1SAdrian Hunter 	signal(signr, SIG_DFL);
5252dd6d8a1SAdrian Hunter 	raise(signr);
5262dd6d8a1SAdrian Hunter }
5272dd6d8a1SAdrian Hunter 
528e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT
529e31f0d01SAdrian Hunter 
530ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool,
531a5830532SJiri Olsa 				    struct mmap *map,
532ef149c25SAdrian Hunter 				    union perf_event *event, void *data1,
533ef149c25SAdrian Hunter 				    size_t len1, void *data2, size_t len2)
534ef149c25SAdrian Hunter {
535ef149c25SAdrian Hunter 	struct record *rec = container_of(tool, struct record, tool);
5368ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
537ef149c25SAdrian Hunter 	size_t padding;
538ef149c25SAdrian Hunter 	u8 pad[8] = {0};
539ef149c25SAdrian Hunter 
540cd3dd8ddSJiri Olsa 	if (!perf_data__is_pipe(data) && !perf_data__is_dir(data)) {
54199fa2984SAdrian Hunter 		off_t file_offset;
5428ceb41d7SJiri Olsa 		int fd = perf_data__fd(data);
54399fa2984SAdrian Hunter 		int err;
54499fa2984SAdrian Hunter 
54599fa2984SAdrian Hunter 		file_offset = lseek(fd, 0, SEEK_CUR);
54699fa2984SAdrian Hunter 		if (file_offset == -1)
54799fa2984SAdrian Hunter 			return -1;
54899fa2984SAdrian Hunter 		err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
54999fa2984SAdrian Hunter 						     event, file_offset);
55099fa2984SAdrian Hunter 		if (err)
55199fa2984SAdrian Hunter 			return err;
55299fa2984SAdrian Hunter 	}
55399fa2984SAdrian Hunter 
554ef149c25SAdrian Hunter 	/* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
555ef149c25SAdrian Hunter 	padding = (len1 + len2) & 7;
556ef149c25SAdrian Hunter 	if (padding)
557ef149c25SAdrian Hunter 		padding = 8 - padding;
558ef149c25SAdrian Hunter 
559ded2b8feSJiri Olsa 	record__write(rec, map, event, event->header.size);
560ded2b8feSJiri Olsa 	record__write(rec, map, data1, len1);
561ef149c25SAdrian Hunter 	if (len2)
562ded2b8feSJiri Olsa 		record__write(rec, map, data2, len2);
563ded2b8feSJiri Olsa 	record__write(rec, map, &pad, padding);
564ef149c25SAdrian Hunter 
565ef149c25SAdrian Hunter 	return 0;
566ef149c25SAdrian Hunter }
567ef149c25SAdrian Hunter 
568ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec,
569a5830532SJiri Olsa 				      struct mmap *map)
570ef149c25SAdrian Hunter {
571ef149c25SAdrian Hunter 	int ret;
572ef149c25SAdrian Hunter 
573e035f4caSJiri Olsa 	ret = auxtrace_mmap__read(map, rec->itr, &rec->tool,
574ef149c25SAdrian Hunter 				  record__process_auxtrace);
575ef149c25SAdrian Hunter 	if (ret < 0)
576ef149c25SAdrian Hunter 		return ret;
577ef149c25SAdrian Hunter 
578ef149c25SAdrian Hunter 	if (ret)
579ef149c25SAdrian Hunter 		rec->samples++;
580ef149c25SAdrian Hunter 
581ef149c25SAdrian Hunter 	return 0;
582ef149c25SAdrian Hunter }
583ef149c25SAdrian Hunter 
5842dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec,
585a5830532SJiri Olsa 					       struct mmap *map)
5862dd6d8a1SAdrian Hunter {
5872dd6d8a1SAdrian Hunter 	int ret;
5882dd6d8a1SAdrian Hunter 
589e035f4caSJiri Olsa 	ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool,
5902dd6d8a1SAdrian Hunter 					   record__process_auxtrace,
5912dd6d8a1SAdrian Hunter 					   rec->opts.auxtrace_snapshot_size);
5922dd6d8a1SAdrian Hunter 	if (ret < 0)
5932dd6d8a1SAdrian Hunter 		return ret;
5942dd6d8a1SAdrian Hunter 
5952dd6d8a1SAdrian Hunter 	if (ret)
5962dd6d8a1SAdrian Hunter 		rec->samples++;
5972dd6d8a1SAdrian Hunter 
5982dd6d8a1SAdrian Hunter 	return 0;
5992dd6d8a1SAdrian Hunter }
6002dd6d8a1SAdrian Hunter 
6012dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec)
6022dd6d8a1SAdrian Hunter {
6032dd6d8a1SAdrian Hunter 	int i;
6042dd6d8a1SAdrian Hunter 	int rc = 0;
6052dd6d8a1SAdrian Hunter 
6062dd6d8a1SAdrian Hunter 	for (i = 0; i < rec->evlist->nr_mmaps; i++) {
607a5830532SJiri Olsa 		struct mmap *map = &rec->evlist->mmap[i];
6082dd6d8a1SAdrian Hunter 
609e035f4caSJiri Olsa 		if (!map->auxtrace_mmap.base)
6102dd6d8a1SAdrian Hunter 			continue;
6112dd6d8a1SAdrian Hunter 
612e035f4caSJiri Olsa 		if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) {
6132dd6d8a1SAdrian Hunter 			rc = -1;
6142dd6d8a1SAdrian Hunter 			goto out;
6152dd6d8a1SAdrian Hunter 		}
6162dd6d8a1SAdrian Hunter 	}
6172dd6d8a1SAdrian Hunter out:
6182dd6d8a1SAdrian Hunter 	return rc;
6192dd6d8a1SAdrian Hunter }
6202dd6d8a1SAdrian Hunter 
621ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit)
6222dd6d8a1SAdrian Hunter {
6232dd6d8a1SAdrian Hunter 	pr_debug("Recording AUX area tracing snapshot\n");
6242dd6d8a1SAdrian Hunter 	if (record__auxtrace_read_snapshot_all(rec) < 0) {
6255f9cf599SWang Nan 		trigger_error(&auxtrace_snapshot_trigger);
6262dd6d8a1SAdrian Hunter 	} else {
627ce7b0e42SAlexander Shishkin 		if (auxtrace_record__snapshot_finish(rec->itr, on_exit))
6285f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
6295f9cf599SWang Nan 		else
6305f9cf599SWang Nan 			trigger_ready(&auxtrace_snapshot_trigger);
6312dd6d8a1SAdrian Hunter 	}
6322dd6d8a1SAdrian Hunter }
6332dd6d8a1SAdrian Hunter 
634ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec)
635ce7b0e42SAlexander Shishkin {
636ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
637ce7b0e42SAlexander Shishkin 		return 0;
638ce7b0e42SAlexander Shishkin 
639ce7b0e42SAlexander Shishkin 	if (!auxtrace_record__snapshot_started &&
640ce7b0e42SAlexander Shishkin 	    auxtrace_record__snapshot_start(rec->itr))
641ce7b0e42SAlexander Shishkin 		return -1;
642ce7b0e42SAlexander Shishkin 
643ce7b0e42SAlexander Shishkin 	record__read_auxtrace_snapshot(rec, true);
644ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
645ce7b0e42SAlexander Shishkin 		return -1;
646ce7b0e42SAlexander Shishkin 
647ce7b0e42SAlexander Shishkin 	return 0;
648ce7b0e42SAlexander Shishkin }
649ce7b0e42SAlexander Shishkin 
6504b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec)
6514b5ea3bdSAdrian Hunter {
6524b5ea3bdSAdrian Hunter 	int err;
6534b5ea3bdSAdrian Hunter 
6544b5ea3bdSAdrian Hunter 	if (!rec->itr) {
6554b5ea3bdSAdrian Hunter 		rec->itr = auxtrace_record__init(rec->evlist, &err);
6564b5ea3bdSAdrian Hunter 		if (err)
6574b5ea3bdSAdrian Hunter 			return err;
6584b5ea3bdSAdrian Hunter 	}
6594b5ea3bdSAdrian Hunter 
6604b5ea3bdSAdrian Hunter 	err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
6614b5ea3bdSAdrian Hunter 					      rec->opts.auxtrace_snapshot_opts);
6624b5ea3bdSAdrian Hunter 	if (err)
6634b5ea3bdSAdrian Hunter 		return err;
6644b5ea3bdSAdrian Hunter 
6654b5ea3bdSAdrian Hunter 	return auxtrace_parse_filters(rec->evlist);
6664b5ea3bdSAdrian Hunter }
6674b5ea3bdSAdrian Hunter 
668e31f0d01SAdrian Hunter #else
669e31f0d01SAdrian Hunter 
670e31f0d01SAdrian Hunter static inline
671e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
672a5830532SJiri Olsa 			       struct mmap *map __maybe_unused)
673e31f0d01SAdrian Hunter {
674e31f0d01SAdrian Hunter 	return 0;
675e31f0d01SAdrian Hunter }
676e31f0d01SAdrian Hunter 
6772dd6d8a1SAdrian Hunter static inline
678ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused,
679ce7b0e42SAlexander Shishkin 				    bool on_exit __maybe_unused)
6802dd6d8a1SAdrian Hunter {
6812dd6d8a1SAdrian Hunter }
6822dd6d8a1SAdrian Hunter 
6832dd6d8a1SAdrian Hunter static inline
6842dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
6852dd6d8a1SAdrian Hunter {
6862dd6d8a1SAdrian Hunter 	return 0;
6872dd6d8a1SAdrian Hunter }
6882dd6d8a1SAdrian Hunter 
689ce7b0e42SAlexander Shishkin static inline
690ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused)
691ce7b0e42SAlexander Shishkin {
692ce7b0e42SAlexander Shishkin 	return 0;
693ce7b0e42SAlexander Shishkin }
694ce7b0e42SAlexander Shishkin 
6954b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused)
6964b5ea3bdSAdrian Hunter {
6974b5ea3bdSAdrian Hunter 	return 0;
6984b5ea3bdSAdrian Hunter }
6994b5ea3bdSAdrian Hunter 
700e31f0d01SAdrian Hunter #endif
701e31f0d01SAdrian Hunter 
702cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec,
70363503dbaSJiri Olsa 			       struct evlist *evlist)
704cda57a8cSWang Nan {
705cda57a8cSWang Nan 	struct record_opts *opts = &rec->opts;
706cda57a8cSWang Nan 	char msg[512];
707cda57a8cSWang Nan 
708f13de660SAlexey Budankov 	if (opts->affinity != PERF_AFFINITY_SYS)
709f13de660SAlexey Budankov 		cpu__setup_cpunode_map();
710f13de660SAlexey Budankov 
7119521b5f2SJiri Olsa 	if (evlist__mmap_ex(evlist, opts->mmap_pages,
712cda57a8cSWang Nan 				 opts->auxtrace_mmap_pages,
7139d2ed645SAlexey Budankov 				 opts->auxtrace_snapshot_mode,
714470530bbSAlexey Budankov 				 opts->nr_cblocks, opts->affinity,
71551255a8aSAlexey Budankov 				 opts->mmap_flush, opts->comp_level) < 0) {
716cda57a8cSWang Nan 		if (errno == EPERM) {
717cda57a8cSWang Nan 			pr_err("Permission error mapping pages.\n"
718cda57a8cSWang Nan 			       "Consider increasing "
719cda57a8cSWang Nan 			       "/proc/sys/kernel/perf_event_mlock_kb,\n"
720cda57a8cSWang Nan 			       "or try again with a smaller value of -m/--mmap_pages.\n"
721cda57a8cSWang Nan 			       "(current value: %u,%u)\n",
722cda57a8cSWang Nan 			       opts->mmap_pages, opts->auxtrace_mmap_pages);
723cda57a8cSWang Nan 			return -errno;
724cda57a8cSWang Nan 		} else {
725cda57a8cSWang Nan 			pr_err("failed to mmap with %d (%s)\n", errno,
726c8b5f2c9SArnaldo Carvalho de Melo 				str_error_r(errno, msg, sizeof(msg)));
727cda57a8cSWang Nan 			if (errno)
728cda57a8cSWang Nan 				return -errno;
729cda57a8cSWang Nan 			else
730cda57a8cSWang Nan 				return -EINVAL;
731cda57a8cSWang Nan 		}
732cda57a8cSWang Nan 	}
733cda57a8cSWang Nan 	return 0;
734cda57a8cSWang Nan }
735cda57a8cSWang Nan 
736cda57a8cSWang Nan static int record__mmap(struct record *rec)
737cda57a8cSWang Nan {
738cda57a8cSWang Nan 	return record__mmap_evlist(rec, rec->evlist);
739cda57a8cSWang Nan }
740cda57a8cSWang Nan 
7418c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec)
742dd7927f4SArnaldo Carvalho de Melo {
743d6195a6aSArnaldo Carvalho de Melo 	char msg[BUFSIZ];
74432dcd021SJiri Olsa 	struct evsel *pos;
74563503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
746d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session = rec->session;
747b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
7488d3eca20SDavid Ahern 	int rc = 0;
749dd7927f4SArnaldo Carvalho de Melo 
750d3dbf43cSArnaldo Carvalho de Melo 	/*
751d3dbf43cSArnaldo Carvalho de Melo 	 * For initial_delay we need to add a dummy event so that we can track
752d3dbf43cSArnaldo Carvalho de Melo 	 * PERF_RECORD_MMAP while we wait for the initial delay to enable the
753d3dbf43cSArnaldo Carvalho de Melo 	 * real events, the ones asked by the user.
754d3dbf43cSArnaldo Carvalho de Melo 	 */
755d3dbf43cSArnaldo Carvalho de Melo 	if (opts->initial_delay) {
756d3dbf43cSArnaldo Carvalho de Melo 		if (perf_evlist__add_dummy(evlist))
757d3dbf43cSArnaldo Carvalho de Melo 			return -ENOMEM;
758d3dbf43cSArnaldo Carvalho de Melo 
759d3dbf43cSArnaldo Carvalho de Melo 		pos = perf_evlist__first(evlist);
760d3dbf43cSArnaldo Carvalho de Melo 		pos->tracking = 0;
761d3dbf43cSArnaldo Carvalho de Melo 		pos = perf_evlist__last(evlist);
762d3dbf43cSArnaldo Carvalho de Melo 		pos->tracking = 1;
7631fc632ceSJiri Olsa 		pos->core.attr.enable_on_exec = 1;
764d3dbf43cSArnaldo Carvalho de Melo 	}
765d3dbf43cSArnaldo Carvalho de Melo 
766e68ae9cfSArnaldo Carvalho de Melo 	perf_evlist__config(evlist, opts, &callchain_param);
767cac21425SJiri Olsa 
768e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
7693da297a6SIngo Molnar try_again:
770af663bd0SJiri Olsa 		if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
77156e52e85SArnaldo Carvalho de Melo 			if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) {
772bb963e16SNamhyung Kim 				if (verbose > 0)
773c0a54341SArnaldo Carvalho de Melo 					ui__warning("%s\n", msg);
7743da297a6SIngo Molnar 				goto try_again;
7753da297a6SIngo Molnar 			}
776cf99ad14SAndi Kleen 			if ((errno == EINVAL || errno == EBADF) &&
777cf99ad14SAndi Kleen 			    pos->leader != pos &&
778cf99ad14SAndi Kleen 			    pos->weak_group) {
779cf99ad14SAndi Kleen 			        pos = perf_evlist__reset_weak_group(evlist, pos);
780cf99ad14SAndi Kleen 				goto try_again;
781cf99ad14SAndi Kleen 			}
78256e52e85SArnaldo Carvalho de Melo 			rc = -errno;
78356e52e85SArnaldo Carvalho de Melo 			perf_evsel__open_strerror(pos, &opts->target,
78456e52e85SArnaldo Carvalho de Melo 						  errno, msg, sizeof(msg));
78556e52e85SArnaldo Carvalho de Melo 			ui__error("%s\n", msg);
7868d3eca20SDavid Ahern 			goto out;
7877c6a1c65SPeter Zijlstra 		}
788bfd8f72cSAndi Kleen 
789bfd8f72cSAndi Kleen 		pos->supported = true;
7907c6a1c65SPeter Zijlstra 	}
7917c6a1c65SPeter Zijlstra 
792c8b567c8SArnaldo Carvalho de Melo 	if (symbol_conf.kptr_restrict && !perf_evlist__exclude_kernel(evlist)) {
793c8b567c8SArnaldo Carvalho de Melo 		pr_warning(
794c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
795c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
796c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
797c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n"
798c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n"
799c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
800c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n");
801c8b567c8SArnaldo Carvalho de Melo 	}
802c8b567c8SArnaldo Carvalho de Melo 
80323d4aad4SArnaldo Carvalho de Melo 	if (perf_evlist__apply_filters(evlist, &pos)) {
80462d94b00SArnaldo Carvalho de Melo 		pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
80523d4aad4SArnaldo Carvalho de Melo 			pos->filter, perf_evsel__name(pos), errno,
806c8b5f2c9SArnaldo Carvalho de Melo 			str_error_r(errno, msg, sizeof(msg)));
8078d3eca20SDavid Ahern 		rc = -1;
8088d3eca20SDavid Ahern 		goto out;
8090a102479SFrederic Weisbecker 	}
8100a102479SFrederic Weisbecker 
811cda57a8cSWang Nan 	rc = record__mmap(rec);
812cda57a8cSWang Nan 	if (rc)
8138d3eca20SDavid Ahern 		goto out;
8140a27d7f9SArnaldo Carvalho de Melo 
815a91e5431SArnaldo Carvalho de Melo 	session->evlist = evlist;
8167b56cce2SArnaldo Carvalho de Melo 	perf_session__set_id_hdr_size(session);
8178d3eca20SDavid Ahern out:
8188d3eca20SDavid Ahern 	return rc;
819a91e5431SArnaldo Carvalho de Melo }
820a91e5431SArnaldo Carvalho de Melo 
821e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool,
822e3d59112SNamhyung Kim 				union perf_event *event,
823e3d59112SNamhyung Kim 				struct perf_sample *sample,
82432dcd021SJiri Olsa 				struct evsel *evsel,
825e3d59112SNamhyung Kim 				struct machine *machine)
826e3d59112SNamhyung Kim {
827e3d59112SNamhyung Kim 	struct record *rec = container_of(tool, struct record, tool);
828e3d59112SNamhyung Kim 
82968588bafSJin Yao 	if (rec->evlist->first_sample_time == 0)
83068588bafSJin Yao 		rec->evlist->first_sample_time = sample->time;
831e3d59112SNamhyung Kim 
83268588bafSJin Yao 	rec->evlist->last_sample_time = sample->time;
83368588bafSJin Yao 
83468588bafSJin Yao 	if (rec->buildid_all)
83568588bafSJin Yao 		return 0;
83668588bafSJin Yao 
83768588bafSJin Yao 	rec->samples++;
838e3d59112SNamhyung Kim 	return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
839e3d59112SNamhyung Kim }
840e3d59112SNamhyung Kim 
8418c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec)
8426122e4e4SArnaldo Carvalho de Melo {
843f5fc1412SJiri Olsa 	struct perf_session *session = rec->session;
8446122e4e4SArnaldo Carvalho de Melo 
84545112e89SJiri Olsa 	if (perf_data__size(&rec->data) == 0)
8469f591fd7SArnaldo Carvalho de Melo 		return 0;
8479f591fd7SArnaldo Carvalho de Melo 
84800dc8657SNamhyung Kim 	/*
84900dc8657SNamhyung Kim 	 * During this process, it'll load kernel map and replace the
85000dc8657SNamhyung Kim 	 * dso->long_name to a real pathname it found.  In this case
85100dc8657SNamhyung Kim 	 * we prefer the vmlinux path like
85200dc8657SNamhyung Kim 	 *   /lib/modules/3.16.4/build/vmlinux
85300dc8657SNamhyung Kim 	 *
85400dc8657SNamhyung Kim 	 * rather than build-id path (in debug directory).
85500dc8657SNamhyung Kim 	 *   $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
85600dc8657SNamhyung Kim 	 */
85700dc8657SNamhyung Kim 	symbol_conf.ignore_vmlinux_buildid = true;
85800dc8657SNamhyung Kim 
8596156681bSNamhyung Kim 	/*
8606156681bSNamhyung Kim 	 * If --buildid-all is given, it marks all DSO regardless of hits,
86168588bafSJin Yao 	 * so no need to process samples. But if timestamp_boundary is enabled,
86268588bafSJin Yao 	 * it still needs to walk on all samples to get the timestamps of
86368588bafSJin Yao 	 * first/last samples.
8646156681bSNamhyung Kim 	 */
86568588bafSJin Yao 	if (rec->buildid_all && !rec->timestamp_boundary)
8666156681bSNamhyung Kim 		rec->tool.sample = NULL;
8676156681bSNamhyung Kim 
868b7b61cbeSArnaldo Carvalho de Melo 	return perf_session__process_events(session);
8696122e4e4SArnaldo Carvalho de Melo }
8706122e4e4SArnaldo Carvalho de Melo 
8718115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
872a1645ce1SZhang, Yanmin {
873a1645ce1SZhang, Yanmin 	int err;
87445694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = data;
875a1645ce1SZhang, Yanmin 	/*
876a1645ce1SZhang, Yanmin 	 *As for guest kernel when processing subcommand record&report,
877a1645ce1SZhang, Yanmin 	 *we arrange module mmap prior to guest kernel mmap and trigger
878a1645ce1SZhang, Yanmin 	 *a preload dso because default guest module symbols are loaded
879a1645ce1SZhang, Yanmin 	 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
880a1645ce1SZhang, Yanmin 	 *method is used to avoid symbol missing when the first addr is
881a1645ce1SZhang, Yanmin 	 *in module instead of in guest kernel.
882a1645ce1SZhang, Yanmin 	 */
88345694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_modules(tool, process_synthesized_event,
884743eb868SArnaldo Carvalho de Melo 					     machine);
885a1645ce1SZhang, Yanmin 	if (err < 0)
886a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
88723346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
888a1645ce1SZhang, Yanmin 
889a1645ce1SZhang, Yanmin 	/*
890a1645ce1SZhang, Yanmin 	 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
891a1645ce1SZhang, Yanmin 	 * have no _text sometimes.
892a1645ce1SZhang, Yanmin 	 */
89345694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
8940ae617beSAdrian Hunter 						 machine);
895a1645ce1SZhang, Yanmin 	if (err < 0)
896a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
89723346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
898a1645ce1SZhang, Yanmin }
899a1645ce1SZhang, Yanmin 
90098402807SFrederic Weisbecker static struct perf_event_header finished_round_event = {
90198402807SFrederic Weisbecker 	.size = sizeof(struct perf_event_header),
90298402807SFrederic Weisbecker 	.type = PERF_RECORD_FINISHED_ROUND,
90398402807SFrederic Weisbecker };
90498402807SFrederic Weisbecker 
905a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map)
906f13de660SAlexey Budankov {
907f13de660SAlexey Budankov 	if (rec->opts.affinity != PERF_AFFINITY_SYS &&
908f13de660SAlexey Budankov 	    !CPU_EQUAL(&rec->affinity_mask, &map->affinity_mask)) {
909f13de660SAlexey Budankov 		CPU_ZERO(&rec->affinity_mask);
910f13de660SAlexey Budankov 		CPU_OR(&rec->affinity_mask, &rec->affinity_mask, &map->affinity_mask);
911f13de660SAlexey Budankov 		sched_setaffinity(0, sizeof(rec->affinity_mask), &rec->affinity_mask);
912f13de660SAlexey Budankov 	}
913f13de660SAlexey Budankov }
914f13de660SAlexey Budankov 
9155d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment)
9165d7f4116SAlexey Budankov {
91772932371SJiri Olsa 	struct perf_record_compressed *event = record;
9185d7f4116SAlexey Budankov 	size_t size = sizeof(*event);
9195d7f4116SAlexey Budankov 
9205d7f4116SAlexey Budankov 	if (increment) {
9215d7f4116SAlexey Budankov 		event->header.size += increment;
9225d7f4116SAlexey Budankov 		return increment;
9235d7f4116SAlexey Budankov 	}
9245d7f4116SAlexey Budankov 
9255d7f4116SAlexey Budankov 	event->header.type = PERF_RECORD_COMPRESSED;
9265d7f4116SAlexey Budankov 	event->header.size = size;
9275d7f4116SAlexey Budankov 
9285d7f4116SAlexey Budankov 	return size;
9295d7f4116SAlexey Budankov }
9305d7f4116SAlexey Budankov 
9315d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size,
9325d7f4116SAlexey Budankov 			    void *src, size_t src_size)
9335d7f4116SAlexey Budankov {
9345d7f4116SAlexey Budankov 	size_t compressed;
93572932371SJiri Olsa 	size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
9365d7f4116SAlexey Budankov 
9375d7f4116SAlexey Budankov 	compressed = zstd_compress_stream_to_records(&session->zstd_data, dst, dst_size, src, src_size,
9385d7f4116SAlexey Budankov 						     max_record_size, process_comp_header);
9395d7f4116SAlexey Budankov 
9405d7f4116SAlexey Budankov 	session->bytes_transferred += src_size;
9415d7f4116SAlexey Budankov 	session->bytes_compressed  += compressed;
9425d7f4116SAlexey Budankov 
9435d7f4116SAlexey Budankov 	return compressed;
9445d7f4116SAlexey Budankov }
9455d7f4116SAlexey Budankov 
94663503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist,
947470530bbSAlexey Budankov 				    bool overwrite, bool synch)
94898402807SFrederic Weisbecker {
949dcabb507SJiri Olsa 	u64 bytes_written = rec->bytes_written;
9500e2e63ddSPeter Zijlstra 	int i;
9518d3eca20SDavid Ahern 	int rc = 0;
952a5830532SJiri Olsa 	struct mmap *maps;
953d3d1af6fSAlexey Budankov 	int trace_fd = rec->data.file.fd;
954ef781128SAlexey Budankov 	off_t off = 0;
95598402807SFrederic Weisbecker 
956cb21686bSWang Nan 	if (!evlist)
957cb21686bSWang Nan 		return 0;
958ef149c25SAdrian Hunter 
9590b72d69aSWang Nan 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
960a4ea0ec4SWang Nan 	if (!maps)
961a4ea0ec4SWang Nan 		return 0;
962cb21686bSWang Nan 
9630b72d69aSWang Nan 	if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
96454cc54deSWang Nan 		return 0;
96554cc54deSWang Nan 
966d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
967d3d1af6fSAlexey Budankov 		off = record__aio_get_pos(trace_fd);
968d3d1af6fSAlexey Budankov 
969a4ea0ec4SWang Nan 	for (i = 0; i < evlist->nr_mmaps; i++) {
970470530bbSAlexey Budankov 		u64 flush = 0;
971a5830532SJiri Olsa 		struct mmap *map = &maps[i];
972a4ea0ec4SWang Nan 
973547740f7SJiri Olsa 		if (map->core.base) {
974f13de660SAlexey Budankov 			record__adjust_affinity(rec, map);
975470530bbSAlexey Budankov 			if (synch) {
976*65aa2e6bSJiri Olsa 				flush = map->core.flush;
977*65aa2e6bSJiri Olsa 				map->core.flush = 1;
978470530bbSAlexey Budankov 			}
979d3d1af6fSAlexey Budankov 			if (!record__aio_enabled(rec)) {
980ef781128SAlexey Budankov 				if (perf_mmap__push(map, rec, record__pushfn) < 0) {
981470530bbSAlexey Budankov 					if (synch)
982*65aa2e6bSJiri Olsa 						map->core.flush = flush;
9838d3eca20SDavid Ahern 					rc = -1;
9848d3eca20SDavid Ahern 					goto out;
9858d3eca20SDavid Ahern 				}
986d3d1af6fSAlexey Budankov 			} else {
987ef781128SAlexey Budankov 				if (record__aio_push(rec, map, &off) < 0) {
988d3d1af6fSAlexey Budankov 					record__aio_set_pos(trace_fd, off);
989470530bbSAlexey Budankov 					if (synch)
990*65aa2e6bSJiri Olsa 						map->core.flush = flush;
991d3d1af6fSAlexey Budankov 					rc = -1;
992d3d1af6fSAlexey Budankov 					goto out;
993d3d1af6fSAlexey Budankov 				}
994d3d1af6fSAlexey Budankov 			}
995470530bbSAlexey Budankov 			if (synch)
996*65aa2e6bSJiri Olsa 				map->core.flush = flush;
9978d3eca20SDavid Ahern 		}
998ef149c25SAdrian Hunter 
999e035f4caSJiri Olsa 		if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
1000e035f4caSJiri Olsa 		    record__auxtrace_mmap_read(rec, map) != 0) {
1001ef149c25SAdrian Hunter 			rc = -1;
1002ef149c25SAdrian Hunter 			goto out;
1003ef149c25SAdrian Hunter 		}
100498402807SFrederic Weisbecker 	}
100598402807SFrederic Weisbecker 
1006d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1007d3d1af6fSAlexey Budankov 		record__aio_set_pos(trace_fd, off);
1008d3d1af6fSAlexey Budankov 
1009dcabb507SJiri Olsa 	/*
1010dcabb507SJiri Olsa 	 * Mark the round finished in case we wrote
1011dcabb507SJiri Olsa 	 * at least one event.
1012dcabb507SJiri Olsa 	 */
1013dcabb507SJiri Olsa 	if (bytes_written != rec->bytes_written)
1014ded2b8feSJiri Olsa 		rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event));
10158d3eca20SDavid Ahern 
10160b72d69aSWang Nan 	if (overwrite)
101754cc54deSWang Nan 		perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
10188d3eca20SDavid Ahern out:
10198d3eca20SDavid Ahern 	return rc;
102098402807SFrederic Weisbecker }
102198402807SFrederic Weisbecker 
1022470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch)
1023cb21686bSWang Nan {
1024cb21686bSWang Nan 	int err;
1025cb21686bSWang Nan 
1026470530bbSAlexey Budankov 	err = record__mmap_read_evlist(rec, rec->evlist, false, synch);
1027cb21686bSWang Nan 	if (err)
1028cb21686bSWang Nan 		return err;
1029cb21686bSWang Nan 
1030470530bbSAlexey Budankov 	return record__mmap_read_evlist(rec, rec->evlist, true, synch);
1031cb21686bSWang Nan }
1032cb21686bSWang Nan 
10338c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec)
103457706abcSDavid Ahern {
103557706abcSDavid Ahern 	struct perf_session *session = rec->session;
103657706abcSDavid Ahern 	int feat;
103757706abcSDavid Ahern 
103857706abcSDavid Ahern 	for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
103957706abcSDavid Ahern 		perf_header__set_feat(&session->header, feat);
104057706abcSDavid Ahern 
104157706abcSDavid Ahern 	if (rec->no_buildid)
104257706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
104357706abcSDavid Ahern 
1044ce9036a6SJiri Olsa 	if (!have_tracepoints(&rec->evlist->core.entries))
104557706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
104657706abcSDavid Ahern 
104757706abcSDavid Ahern 	if (!rec->opts.branch_stack)
104857706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1049ef149c25SAdrian Hunter 
1050ef149c25SAdrian Hunter 	if (!rec->opts.full_auxtrace)
1051ef149c25SAdrian Hunter 		perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1052ffa517adSJiri Olsa 
1053cf790516SAlexey Budankov 	if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns))
1054cf790516SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_CLOCKID);
1055cf790516SAlexey Budankov 
1056258031c0SJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
105742e1fd80SAlexey Budankov 	if (!record__comp_enabled(rec))
105842e1fd80SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_COMPRESSED);
1059258031c0SJiri Olsa 
1060ffa517adSJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_STAT);
106157706abcSDavid Ahern }
106257706abcSDavid Ahern 
1063e1ab48baSWang Nan static void
1064e1ab48baSWang Nan record__finish_output(struct record *rec)
1065e1ab48baSWang Nan {
10668ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
10678ceb41d7SJiri Olsa 	int fd = perf_data__fd(data);
1068e1ab48baSWang Nan 
10698ceb41d7SJiri Olsa 	if (data->is_pipe)
1070e1ab48baSWang Nan 		return;
1071e1ab48baSWang Nan 
1072e1ab48baSWang Nan 	rec->session->header.data_size += rec->bytes_written;
107345112e89SJiri Olsa 	data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR);
1074e1ab48baSWang Nan 
1075e1ab48baSWang Nan 	if (!rec->no_buildid) {
1076e1ab48baSWang Nan 		process_buildids(rec);
1077e1ab48baSWang Nan 
1078e1ab48baSWang Nan 		if (rec->buildid_all)
1079e1ab48baSWang Nan 			dsos__hit_all(rec->session);
1080e1ab48baSWang Nan 	}
1081e1ab48baSWang Nan 	perf_session__write_header(rec->session, rec->evlist, fd, true);
1082e1ab48baSWang Nan 
1083e1ab48baSWang Nan 	return;
1084e1ab48baSWang Nan }
1085e1ab48baSWang Nan 
10864ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail)
1087be7b0c9eSWang Nan {
10889d6aae72SArnaldo Carvalho de Melo 	int err;
10899749b90eSJiri Olsa 	struct perf_thread_map *thread_map;
1090be7b0c9eSWang Nan 
10914ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
10924ea648aeSWang Nan 		return 0;
10934ea648aeSWang Nan 
10949d6aae72SArnaldo Carvalho de Melo 	thread_map = thread_map__new_by_tid(rec->evlist->workload.pid);
10959d6aae72SArnaldo Carvalho de Melo 	if (thread_map == NULL)
10969d6aae72SArnaldo Carvalho de Melo 		return -1;
10979d6aae72SArnaldo Carvalho de Melo 
10989d6aae72SArnaldo Carvalho de Melo 	err = perf_event__synthesize_thread_map(&rec->tool, thread_map,
1099be7b0c9eSWang Nan 						 process_synthesized_event,
1100be7b0c9eSWang Nan 						 &rec->session->machines.host,
11013fcb10e4SMark Drayton 						 rec->opts.sample_address);
11027836e52eSJiri Olsa 	perf_thread_map__put(thread_map);
11039d6aae72SArnaldo Carvalho de Melo 	return err;
1104be7b0c9eSWang Nan }
1105be7b0c9eSWang Nan 
11064ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail);
11073c1cb7e3SWang Nan 
1108ecfd7a9cSWang Nan static int
1109ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit)
1110ecfd7a9cSWang Nan {
11118ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1112ecfd7a9cSWang Nan 	int fd, err;
111303724b2eSAndi Kleen 	char *new_filename;
1114ecfd7a9cSWang Nan 
1115ecfd7a9cSWang Nan 	/* Same Size:      "2015122520103046"*/
1116ecfd7a9cSWang Nan 	char timestamp[] = "InvalidTimestamp";
1117ecfd7a9cSWang Nan 
1118d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
1119d3d1af6fSAlexey Budankov 
11204ea648aeSWang Nan 	record__synthesize(rec, true);
11214ea648aeSWang Nan 	if (target__none(&rec->opts.target))
11224ea648aeSWang Nan 		record__synthesize_workload(rec, true);
11234ea648aeSWang Nan 
1124ecfd7a9cSWang Nan 	rec->samples = 0;
1125ecfd7a9cSWang Nan 	record__finish_output(rec);
1126ecfd7a9cSWang Nan 	err = fetch_current_timestamp(timestamp, sizeof(timestamp));
1127ecfd7a9cSWang Nan 	if (err) {
1128ecfd7a9cSWang Nan 		pr_err("Failed to get current timestamp\n");
1129ecfd7a9cSWang Nan 		return -EINVAL;
1130ecfd7a9cSWang Nan 	}
1131ecfd7a9cSWang Nan 
11328ceb41d7SJiri Olsa 	fd = perf_data__switch(data, timestamp,
1133ecfd7a9cSWang Nan 				    rec->session->header.data_offset,
113403724b2eSAndi Kleen 				    at_exit, &new_filename);
1135ecfd7a9cSWang Nan 	if (fd >= 0 && !at_exit) {
1136ecfd7a9cSWang Nan 		rec->bytes_written = 0;
1137ecfd7a9cSWang Nan 		rec->session->header.data_size = 0;
1138ecfd7a9cSWang Nan 	}
1139ecfd7a9cSWang Nan 
1140ecfd7a9cSWang Nan 	if (!quiet)
1141ecfd7a9cSWang Nan 		fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
11422d4f2799SJiri Olsa 			data->path, timestamp);
11433c1cb7e3SWang Nan 
114403724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
114503724b2eSAndi Kleen 		int n = rec->switch_output.cur_file + 1;
114603724b2eSAndi Kleen 
114703724b2eSAndi Kleen 		if (n >= rec->switch_output.num_files)
114803724b2eSAndi Kleen 			n = 0;
114903724b2eSAndi Kleen 		rec->switch_output.cur_file = n;
115003724b2eSAndi Kleen 		if (rec->switch_output.filenames[n]) {
115103724b2eSAndi Kleen 			remove(rec->switch_output.filenames[n]);
1152d8f9da24SArnaldo Carvalho de Melo 			zfree(&rec->switch_output.filenames[n]);
115303724b2eSAndi Kleen 		}
115403724b2eSAndi Kleen 		rec->switch_output.filenames[n] = new_filename;
115503724b2eSAndi Kleen 	} else {
115603724b2eSAndi Kleen 		free(new_filename);
115703724b2eSAndi Kleen 	}
115803724b2eSAndi Kleen 
11593c1cb7e3SWang Nan 	/* Output tracking events */
1160be7b0c9eSWang Nan 	if (!at_exit) {
11614ea648aeSWang Nan 		record__synthesize(rec, false);
11623c1cb7e3SWang Nan 
1163be7b0c9eSWang Nan 		/*
1164be7b0c9eSWang Nan 		 * In 'perf record --switch-output' without -a,
1165be7b0c9eSWang Nan 		 * record__synthesize() in record__switch_output() won't
1166be7b0c9eSWang Nan 		 * generate tracking events because there's no thread_map
1167be7b0c9eSWang Nan 		 * in evlist. Which causes newly created perf.data doesn't
1168be7b0c9eSWang Nan 		 * contain map and comm information.
1169be7b0c9eSWang Nan 		 * Create a fake thread_map and directly call
1170be7b0c9eSWang Nan 		 * perf_event__synthesize_thread_map() for those events.
1171be7b0c9eSWang Nan 		 */
1172be7b0c9eSWang Nan 		if (target__none(&rec->opts.target))
11734ea648aeSWang Nan 			record__synthesize_workload(rec, false);
1174be7b0c9eSWang Nan 	}
1175ecfd7a9cSWang Nan 	return fd;
1176ecfd7a9cSWang Nan }
1177ecfd7a9cSWang Nan 
1178f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno;
1179f33cbe72SArnaldo Carvalho de Melo 
1180f33cbe72SArnaldo Carvalho de Melo /*
1181f33cbe72SArnaldo Carvalho de Melo  * perf_evlist__prepare_workload will send a SIGUSR1
1182f33cbe72SArnaldo Carvalho de Melo  * if the fork fails, since we asked by setting its
1183f33cbe72SArnaldo Carvalho de Melo  * want_signal to true.
1184f33cbe72SArnaldo Carvalho de Melo  */
118545604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused,
118645604710SNamhyung Kim 					siginfo_t *info,
1187f33cbe72SArnaldo Carvalho de Melo 					void *ucontext __maybe_unused)
1188f33cbe72SArnaldo Carvalho de Melo {
1189f33cbe72SArnaldo Carvalho de Melo 	workload_exec_errno = info->si_value.sival_int;
1190f33cbe72SArnaldo Carvalho de Melo 	done = 1;
1191f33cbe72SArnaldo Carvalho de Melo 	child_finished = 1;
1192f33cbe72SArnaldo Carvalho de Melo }
1193f33cbe72SArnaldo Carvalho de Melo 
11942dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig);
1195bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig);
11962dd6d8a1SAdrian Hunter 
1197ee667f94SWang Nan static const struct perf_event_mmap_page *
119863503dbaSJiri Olsa perf_evlist__pick_pc(struct evlist *evlist)
1199ee667f94SWang Nan {
1200b2cb615dSWang Nan 	if (evlist) {
1201547740f7SJiri Olsa 		if (evlist->mmap && evlist->mmap[0].core.base)
1202547740f7SJiri Olsa 			return evlist->mmap[0].core.base;
1203547740f7SJiri Olsa 		if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base)
1204547740f7SJiri Olsa 			return evlist->overwrite_mmap[0].core.base;
1205b2cb615dSWang Nan 	}
1206ee667f94SWang Nan 	return NULL;
1207ee667f94SWang Nan }
1208ee667f94SWang Nan 
1209c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
1210c45628b0SWang Nan {
1211ee667f94SWang Nan 	const struct perf_event_mmap_page *pc;
1212ee667f94SWang Nan 
1213ee667f94SWang Nan 	pc = perf_evlist__pick_pc(rec->evlist);
1214ee667f94SWang Nan 	if (pc)
1215ee667f94SWang Nan 		return pc;
1216c45628b0SWang Nan 	return NULL;
1217c45628b0SWang Nan }
1218c45628b0SWang Nan 
12194ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail)
1220c45c86ebSWang Nan {
1221c45c86ebSWang Nan 	struct perf_session *session = rec->session;
1222c45c86ebSWang Nan 	struct machine *machine = &session->machines.host;
12238ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1224c45c86ebSWang Nan 	struct record_opts *opts = &rec->opts;
1225c45c86ebSWang Nan 	struct perf_tool *tool = &rec->tool;
12268ceb41d7SJiri Olsa 	int fd = perf_data__fd(data);
1227c45c86ebSWang Nan 	int err = 0;
1228c45c86ebSWang Nan 
12294ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
12304ea648aeSWang Nan 		return 0;
12314ea648aeSWang Nan 
12328ceb41d7SJiri Olsa 	if (data->is_pipe) {
1233a2015516SJiri Olsa 		/*
1234a2015516SJiri Olsa 		 * We need to synthesize events first, because some
1235a2015516SJiri Olsa 		 * features works on top of them (on report side).
1236a2015516SJiri Olsa 		 */
1237318ec184SJiri Olsa 		err = perf_event__synthesize_attrs(tool, rec->evlist,
1238c45c86ebSWang Nan 						   process_synthesized_event);
1239c45c86ebSWang Nan 		if (err < 0) {
1240c45c86ebSWang Nan 			pr_err("Couldn't synthesize attrs.\n");
1241c45c86ebSWang Nan 			goto out;
1242c45c86ebSWang Nan 		}
1243c45c86ebSWang Nan 
1244a2015516SJiri Olsa 		err = perf_event__synthesize_features(tool, session, rec->evlist,
1245a2015516SJiri Olsa 						      process_synthesized_event);
1246a2015516SJiri Olsa 		if (err < 0) {
1247a2015516SJiri Olsa 			pr_err("Couldn't synthesize features.\n");
1248a2015516SJiri Olsa 			return err;
1249a2015516SJiri Olsa 		}
1250a2015516SJiri Olsa 
1251ce9036a6SJiri Olsa 		if (have_tracepoints(&rec->evlist->core.entries)) {
1252c45c86ebSWang Nan 			/*
1253c45c86ebSWang Nan 			 * FIXME err <= 0 here actually means that
1254c45c86ebSWang Nan 			 * there were no tracepoints so its not really
1255c45c86ebSWang Nan 			 * an error, just that we don't need to
1256c45c86ebSWang Nan 			 * synthesize anything.  We really have to
1257c45c86ebSWang Nan 			 * return this more properly and also
1258c45c86ebSWang Nan 			 * propagate errors that now are calling die()
1259c45c86ebSWang Nan 			 */
1260c45c86ebSWang Nan 			err = perf_event__synthesize_tracing_data(tool,	fd, rec->evlist,
1261c45c86ebSWang Nan 								  process_synthesized_event);
1262c45c86ebSWang Nan 			if (err <= 0) {
1263c45c86ebSWang Nan 				pr_err("Couldn't record tracing data.\n");
1264c45c86ebSWang Nan 				goto out;
1265c45c86ebSWang Nan 			}
1266c45c86ebSWang Nan 			rec->bytes_written += err;
1267c45c86ebSWang Nan 		}
1268c45c86ebSWang Nan 	}
1269c45c86ebSWang Nan 
1270c45628b0SWang Nan 	err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
127146bc29b9SAdrian Hunter 					  process_synthesized_event, machine);
127246bc29b9SAdrian Hunter 	if (err)
127346bc29b9SAdrian Hunter 		goto out;
127446bc29b9SAdrian Hunter 
1275c45c86ebSWang Nan 	if (rec->opts.full_auxtrace) {
1276c45c86ebSWang Nan 		err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
1277c45c86ebSWang Nan 					session, process_synthesized_event);
1278c45c86ebSWang Nan 		if (err)
1279c45c86ebSWang Nan 			goto out;
1280c45c86ebSWang Nan 	}
1281c45c86ebSWang Nan 
12826c443954SArnaldo Carvalho de Melo 	if (!perf_evlist__exclude_kernel(rec->evlist)) {
1283c45c86ebSWang Nan 		err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
1284c45c86ebSWang Nan 							 machine);
1285c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
1286c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1287c45c86ebSWang Nan 				   "Check /proc/kallsyms permission or run as root.\n");
1288c45c86ebSWang Nan 
1289c45c86ebSWang Nan 		err = perf_event__synthesize_modules(tool, process_synthesized_event,
1290c45c86ebSWang Nan 						     machine);
1291c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
1292c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1293c45c86ebSWang Nan 				   "Check /proc/modules permission or run as root.\n");
12946c443954SArnaldo Carvalho de Melo 	}
1295c45c86ebSWang Nan 
1296c45c86ebSWang Nan 	if (perf_guest) {
1297c45c86ebSWang Nan 		machines__process_guests(&session->machines,
1298c45c86ebSWang Nan 					 perf_event__synthesize_guest_os, tool);
1299c45c86ebSWang Nan 	}
1300c45c86ebSWang Nan 
1301bfd8f72cSAndi Kleen 	err = perf_event__synthesize_extra_attr(&rec->tool,
1302bfd8f72cSAndi Kleen 						rec->evlist,
1303bfd8f72cSAndi Kleen 						process_synthesized_event,
1304bfd8f72cSAndi Kleen 						data->is_pipe);
1305bfd8f72cSAndi Kleen 	if (err)
1306bfd8f72cSAndi Kleen 		goto out;
1307bfd8f72cSAndi Kleen 
130803617c22SJiri Olsa 	err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads,
1309373565d2SAndi Kleen 						 process_synthesized_event,
1310373565d2SAndi Kleen 						NULL);
1311373565d2SAndi Kleen 	if (err < 0) {
1312373565d2SAndi Kleen 		pr_err("Couldn't synthesize thread map.\n");
1313373565d2SAndi Kleen 		return err;
1314373565d2SAndi Kleen 	}
1315373565d2SAndi Kleen 
1316f72f901dSJiri Olsa 	err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.cpus,
1317373565d2SAndi Kleen 					     process_synthesized_event, NULL);
1318373565d2SAndi Kleen 	if (err < 0) {
1319373565d2SAndi Kleen 		pr_err("Couldn't synthesize cpu map.\n");
1320373565d2SAndi Kleen 		return err;
1321373565d2SAndi Kleen 	}
1322373565d2SAndi Kleen 
1323e5416950SSong Liu 	err = perf_event__synthesize_bpf_events(session, process_synthesized_event,
13247b612e29SSong Liu 						machine, opts);
13257b612e29SSong Liu 	if (err < 0)
13267b612e29SSong Liu 		pr_warning("Couldn't synthesize bpf events.\n");
13277b612e29SSong Liu 
132803617c22SJiri Olsa 	err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads,
1329c45c86ebSWang Nan 					    process_synthesized_event, opts->sample_address,
13303fcb10e4SMark Drayton 					    1);
1331c45c86ebSWang Nan out:
1332c45c86ebSWang Nan 	return err;
1333c45c86ebSWang Nan }
1334c45c86ebSWang Nan 
13358c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv)
133686470930SIngo Molnar {
133757706abcSDavid Ahern 	int err;
133845604710SNamhyung Kim 	int status = 0;
13398b412664SPeter Zijlstra 	unsigned long waking = 0;
134046be604bSZhang, Yanmin 	const bool forks = argc > 0;
134145694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = &rec->tool;
1342b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
13438ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1344d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session;
13456dcf45efSArnaldo Carvalho de Melo 	bool disabled = false, draining = false;
134663503dbaSJiri Olsa 	struct evlist *sb_evlist = NULL;
134742aa276fSNamhyung Kim 	int fd;
1348d3c8c08eSAlexey Budankov 	float ratio = 0;
134986470930SIngo Molnar 
135045604710SNamhyung Kim 	atexit(record__sig_exit);
1351f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
1352f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
1353804f7ac7SDavid Ahern 	signal(SIGTERM, sig_handler);
1354a074865eSWang Nan 	signal(SIGSEGV, sigsegv_handler);
1355c0bdc1c4SWang Nan 
1356f3b3614aSHari Bathini 	if (rec->opts.record_namespaces)
1357f3b3614aSHari Bathini 		tool->namespace_events = true;
1358f3b3614aSHari Bathini 
1359dc0c6127SJiri Olsa 	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
13602dd6d8a1SAdrian Hunter 		signal(SIGUSR2, snapshot_sig_handler);
13613c1cb7e3SWang Nan 		if (rec->opts.auxtrace_snapshot_mode)
13625f9cf599SWang Nan 			trigger_on(&auxtrace_snapshot_trigger);
1363dc0c6127SJiri Olsa 		if (rec->switch_output.enabled)
13643c1cb7e3SWang Nan 			trigger_on(&switch_output_trigger);
1365c0bdc1c4SWang Nan 	} else {
13662dd6d8a1SAdrian Hunter 		signal(SIGUSR2, SIG_IGN);
1367c0bdc1c4SWang Nan 	}
1368f5970550SPeter Zijlstra 
13698ceb41d7SJiri Olsa 	session = perf_session__new(data, false, tool);
13706ef81c55SMamatha Inamdar 	if (IS_ERR(session)) {
1371ffa91880SAdrien BAK 		pr_err("Perf session creation failed.\n");
13726ef81c55SMamatha Inamdar 		return PTR_ERR(session);
1373a9a70bbcSArnaldo Carvalho de Melo 	}
1374a9a70bbcSArnaldo Carvalho de Melo 
13758ceb41d7SJiri Olsa 	fd = perf_data__fd(data);
1376d20deb64SArnaldo Carvalho de Melo 	rec->session = session;
1377d20deb64SArnaldo Carvalho de Melo 
13785d7f4116SAlexey Budankov 	if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) {
13795d7f4116SAlexey Budankov 		pr_err("Compression initialization failed.\n");
13805d7f4116SAlexey Budankov 		return -1;
13815d7f4116SAlexey Budankov 	}
13825d7f4116SAlexey Budankov 
13835d7f4116SAlexey Budankov 	session->header.env.comp_type  = PERF_COMP_ZSTD;
13845d7f4116SAlexey Budankov 	session->header.env.comp_level = rec->opts.comp_level;
13855d7f4116SAlexey Budankov 
13868c6f45a7SArnaldo Carvalho de Melo 	record__init_features(rec);
1387330aa675SStephane Eranian 
1388cf790516SAlexey Budankov 	if (rec->opts.use_clockid && rec->opts.clockid_res_ns)
1389cf790516SAlexey Budankov 		session->header.env.clockid_res_ns = rec->opts.clockid_res_ns;
1390cf790516SAlexey Budankov 
1391d4db3f16SArnaldo Carvalho de Melo 	if (forks) {
13923e2be2daSArnaldo Carvalho de Melo 		err = perf_evlist__prepare_workload(rec->evlist, &opts->target,
13938ceb41d7SJiri Olsa 						    argv, data->is_pipe,
1394735f7e0bSArnaldo Carvalho de Melo 						    workload_exec_failed_signal);
139535b9d88eSArnaldo Carvalho de Melo 		if (err < 0) {
139635b9d88eSArnaldo Carvalho de Melo 			pr_err("Couldn't run the workload!\n");
139745604710SNamhyung Kim 			status = err;
139835b9d88eSArnaldo Carvalho de Melo 			goto out_delete_session;
1399856e9660SPeter Zijlstra 		}
1400856e9660SPeter Zijlstra 	}
1401856e9660SPeter Zijlstra 
1402ad46e48cSJiri Olsa 	/*
1403ad46e48cSJiri Olsa 	 * If we have just single event and are sending data
1404ad46e48cSJiri Olsa 	 * through pipe, we need to force the ids allocation,
1405ad46e48cSJiri Olsa 	 * because we synthesize event name through the pipe
1406ad46e48cSJiri Olsa 	 * and need the id for that.
1407ad46e48cSJiri Olsa 	 */
14086484d2f9SJiri Olsa 	if (data->is_pipe && rec->evlist->core.nr_entries == 1)
1409ad46e48cSJiri Olsa 		rec->opts.sample_id = true;
1410ad46e48cSJiri Olsa 
14118c6f45a7SArnaldo Carvalho de Melo 	if (record__open(rec) != 0) {
14128d3eca20SDavid Ahern 		err = -1;
141345604710SNamhyung Kim 		goto out_child;
14148d3eca20SDavid Ahern 	}
141542e1fd80SAlexey Budankov 	session->header.env.comp_mmap_len = session->evlist->mmap_len;
141686470930SIngo Molnar 
14178690a2a7SWang Nan 	err = bpf__apply_obj_config();
14188690a2a7SWang Nan 	if (err) {
14198690a2a7SWang Nan 		char errbuf[BUFSIZ];
14208690a2a7SWang Nan 
14218690a2a7SWang Nan 		bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
14228690a2a7SWang Nan 		pr_err("ERROR: Apply config to BPF failed: %s\n",
14238690a2a7SWang Nan 			 errbuf);
14248690a2a7SWang Nan 		goto out_child;
14258690a2a7SWang Nan 	}
14268690a2a7SWang Nan 
1427cca8482cSAdrian Hunter 	/*
1428cca8482cSAdrian Hunter 	 * Normally perf_session__new would do this, but it doesn't have the
1429cca8482cSAdrian Hunter 	 * evlist.
1430cca8482cSAdrian Hunter 	 */
1431cca8482cSAdrian Hunter 	if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) {
1432cca8482cSAdrian Hunter 		pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
1433cca8482cSAdrian Hunter 		rec->tool.ordered_events = false;
1434cca8482cSAdrian Hunter 	}
1435cca8482cSAdrian Hunter 
14363e2be2daSArnaldo Carvalho de Melo 	if (!rec->evlist->nr_groups)
1437a8bb559bSNamhyung Kim 		perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
1438a8bb559bSNamhyung Kim 
14398ceb41d7SJiri Olsa 	if (data->is_pipe) {
144042aa276fSNamhyung Kim 		err = perf_header__write_pipe(fd);
1441529870e3STom Zanussi 		if (err < 0)
144245604710SNamhyung Kim 			goto out_child;
1443563aecb2SJiri Olsa 	} else {
144442aa276fSNamhyung Kim 		err = perf_session__write_header(session, rec->evlist, fd, false);
1445d5eed904SArnaldo Carvalho de Melo 		if (err < 0)
144645604710SNamhyung Kim 			goto out_child;
1447d5eed904SArnaldo Carvalho de Melo 	}
14487c6a1c65SPeter Zijlstra 
1449d3665498SDavid Ahern 	if (!rec->no_buildid
1450e20960c0SRobert Richter 	    && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
1451d3665498SDavid Ahern 		pr_err("Couldn't generate buildids. "
1452e20960c0SRobert Richter 		       "Use --no-buildid to profile anyway.\n");
14538d3eca20SDavid Ahern 		err = -1;
145445604710SNamhyung Kim 		goto out_child;
1455e20960c0SRobert Richter 	}
1456e20960c0SRobert Richter 
1457d56354dcSSong Liu 	if (!opts->no_bpf_event)
1458d56354dcSSong Liu 		bpf_event__add_sb_event(&sb_evlist, &session->header.env);
1459d56354dcSSong Liu 
1460657ee553SSong Liu 	if (perf_evlist__start_sb_thread(sb_evlist, &rec->opts.target)) {
1461657ee553SSong Liu 		pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
1462657ee553SSong Liu 		opts->no_bpf_event = true;
1463657ee553SSong Liu 	}
1464657ee553SSong Liu 
14654ea648aeSWang Nan 	err = record__synthesize(rec, false);
1466c45c86ebSWang Nan 	if (err < 0)
146745604710SNamhyung Kim 		goto out_child;
14688d3eca20SDavid Ahern 
1469d20deb64SArnaldo Carvalho de Melo 	if (rec->realtime_prio) {
147086470930SIngo Molnar 		struct sched_param param;
147186470930SIngo Molnar 
1472d20deb64SArnaldo Carvalho de Melo 		param.sched_priority = rec->realtime_prio;
147386470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
14746beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
14758d3eca20SDavid Ahern 			err = -1;
147645604710SNamhyung Kim 			goto out_child;
147786470930SIngo Molnar 		}
147886470930SIngo Molnar 	}
147986470930SIngo Molnar 
1480774cb499SJiri Olsa 	/*
1481774cb499SJiri Olsa 	 * When perf is starting the traced process, all the events
1482774cb499SJiri Olsa 	 * (apart from group members) have enable_on_exec=1 set,
1483774cb499SJiri Olsa 	 * so don't spoil it by prematurely enabling them.
1484774cb499SJiri Olsa 	 */
14856619a53eSAndi Kleen 	if (!target__none(&opts->target) && !opts->initial_delay)
14861c87f165SJiri Olsa 		evlist__enable(rec->evlist);
1487764e16a3SDavid Ahern 
1488856e9660SPeter Zijlstra 	/*
1489856e9660SPeter Zijlstra 	 * Let the child rip
1490856e9660SPeter Zijlstra 	 */
1491e803cf97SNamhyung Kim 	if (forks) {
149220a8a3cfSJiri Olsa 		struct machine *machine = &session->machines.host;
1493e5bed564SNamhyung Kim 		union perf_event *event;
1494e907caf3SHari Bathini 		pid_t tgid;
1495e5bed564SNamhyung Kim 
1496e5bed564SNamhyung Kim 		event = malloc(sizeof(event->comm) + machine->id_hdr_size);
1497e5bed564SNamhyung Kim 		if (event == NULL) {
1498e5bed564SNamhyung Kim 			err = -ENOMEM;
1499e5bed564SNamhyung Kim 			goto out_child;
1500e5bed564SNamhyung Kim 		}
1501e5bed564SNamhyung Kim 
1502e803cf97SNamhyung Kim 		/*
1503e803cf97SNamhyung Kim 		 * Some H/W events are generated before COMM event
1504e803cf97SNamhyung Kim 		 * which is emitted during exec(), so perf script
1505e803cf97SNamhyung Kim 		 * cannot see a correct process name for those events.
1506e803cf97SNamhyung Kim 		 * Synthesize COMM event to prevent it.
1507e803cf97SNamhyung Kim 		 */
1508e907caf3SHari Bathini 		tgid = perf_event__synthesize_comm(tool, event,
1509e803cf97SNamhyung Kim 						   rec->evlist->workload.pid,
1510e803cf97SNamhyung Kim 						   process_synthesized_event,
1511e803cf97SNamhyung Kim 						   machine);
1512e5bed564SNamhyung Kim 		free(event);
1513e803cf97SNamhyung Kim 
1514e907caf3SHari Bathini 		if (tgid == -1)
1515e907caf3SHari Bathini 			goto out_child;
1516e907caf3SHari Bathini 
1517e907caf3SHari Bathini 		event = malloc(sizeof(event->namespaces) +
1518e907caf3SHari Bathini 			       (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
1519e907caf3SHari Bathini 			       machine->id_hdr_size);
1520e907caf3SHari Bathini 		if (event == NULL) {
1521e907caf3SHari Bathini 			err = -ENOMEM;
1522e907caf3SHari Bathini 			goto out_child;
1523e907caf3SHari Bathini 		}
1524e907caf3SHari Bathini 
1525e907caf3SHari Bathini 		/*
1526e907caf3SHari Bathini 		 * Synthesize NAMESPACES event for the command specified.
1527e907caf3SHari Bathini 		 */
1528e907caf3SHari Bathini 		perf_event__synthesize_namespaces(tool, event,
1529e907caf3SHari Bathini 						  rec->evlist->workload.pid,
1530e907caf3SHari Bathini 						  tgid, process_synthesized_event,
1531e907caf3SHari Bathini 						  machine);
1532e907caf3SHari Bathini 		free(event);
1533e907caf3SHari Bathini 
15343e2be2daSArnaldo Carvalho de Melo 		perf_evlist__start_workload(rec->evlist);
1535e803cf97SNamhyung Kim 	}
1536856e9660SPeter Zijlstra 
15376619a53eSAndi Kleen 	if (opts->initial_delay) {
15380693e680SArnaldo Carvalho de Melo 		usleep(opts->initial_delay * USEC_PER_MSEC);
15391c87f165SJiri Olsa 		evlist__enable(rec->evlist);
15406619a53eSAndi Kleen 	}
15416619a53eSAndi Kleen 
15425f9cf599SWang Nan 	trigger_ready(&auxtrace_snapshot_trigger);
15433c1cb7e3SWang Nan 	trigger_ready(&switch_output_trigger);
1544a074865eSWang Nan 	perf_hooks__invoke_record_start();
1545649c48a9SPeter Zijlstra 	for (;;) {
15469f065194SYang Shi 		unsigned long long hits = rec->samples;
154786470930SIngo Molnar 
154805737464SWang Nan 		/*
154905737464SWang Nan 		 * rec->evlist->bkw_mmap_state is possible to be
155005737464SWang Nan 		 * BKW_MMAP_EMPTY here: when done == true and
155105737464SWang Nan 		 * hits != rec->samples in previous round.
155205737464SWang Nan 		 *
155305737464SWang Nan 		 * perf_evlist__toggle_bkw_mmap ensure we never
155405737464SWang Nan 		 * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
155505737464SWang Nan 		 */
155605737464SWang Nan 		if (trigger_is_hit(&switch_output_trigger) || done || draining)
155705737464SWang Nan 			perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
155805737464SWang Nan 
1559470530bbSAlexey Budankov 		if (record__mmap_read_all(rec, false) < 0) {
15605f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
15613c1cb7e3SWang Nan 			trigger_error(&switch_output_trigger);
15628d3eca20SDavid Ahern 			err = -1;
156345604710SNamhyung Kim 			goto out_child;
15648d3eca20SDavid Ahern 		}
156586470930SIngo Molnar 
15662dd6d8a1SAdrian Hunter 		if (auxtrace_record__snapshot_started) {
15672dd6d8a1SAdrian Hunter 			auxtrace_record__snapshot_started = 0;
15685f9cf599SWang Nan 			if (!trigger_is_error(&auxtrace_snapshot_trigger))
1569ce7b0e42SAlexander Shishkin 				record__read_auxtrace_snapshot(rec, false);
15705f9cf599SWang Nan 			if (trigger_is_error(&auxtrace_snapshot_trigger)) {
15712dd6d8a1SAdrian Hunter 				pr_err("AUX area tracing snapshot failed\n");
15722dd6d8a1SAdrian Hunter 				err = -1;
15732dd6d8a1SAdrian Hunter 				goto out_child;
15742dd6d8a1SAdrian Hunter 			}
15752dd6d8a1SAdrian Hunter 		}
15762dd6d8a1SAdrian Hunter 
15773c1cb7e3SWang Nan 		if (trigger_is_hit(&switch_output_trigger)) {
157805737464SWang Nan 			/*
157905737464SWang Nan 			 * If switch_output_trigger is hit, the data in
158005737464SWang Nan 			 * overwritable ring buffer should have been collected,
158105737464SWang Nan 			 * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
158205737464SWang Nan 			 *
158305737464SWang Nan 			 * If SIGUSR2 raise after or during record__mmap_read_all(),
158405737464SWang Nan 			 * record__mmap_read_all() didn't collect data from
158505737464SWang Nan 			 * overwritable ring buffer. Read again.
158605737464SWang Nan 			 */
158705737464SWang Nan 			if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
158805737464SWang Nan 				continue;
15893c1cb7e3SWang Nan 			trigger_ready(&switch_output_trigger);
15903c1cb7e3SWang Nan 
159105737464SWang Nan 			/*
159205737464SWang Nan 			 * Reenable events in overwrite ring buffer after
159305737464SWang Nan 			 * record__mmap_read_all(): we should have collected
159405737464SWang Nan 			 * data from it.
159505737464SWang Nan 			 */
159605737464SWang Nan 			perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
159705737464SWang Nan 
15983c1cb7e3SWang Nan 			if (!quiet)
15993c1cb7e3SWang Nan 				fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
16003c1cb7e3SWang Nan 					waking);
16013c1cb7e3SWang Nan 			waking = 0;
16023c1cb7e3SWang Nan 			fd = record__switch_output(rec, false);
16033c1cb7e3SWang Nan 			if (fd < 0) {
16043c1cb7e3SWang Nan 				pr_err("Failed to switch to new file\n");
16053c1cb7e3SWang Nan 				trigger_error(&switch_output_trigger);
16063c1cb7e3SWang Nan 				err = fd;
16073c1cb7e3SWang Nan 				goto out_child;
16083c1cb7e3SWang Nan 			}
1609bfacbe3bSJiri Olsa 
1610bfacbe3bSJiri Olsa 			/* re-arm the alarm */
1611bfacbe3bSJiri Olsa 			if (rec->switch_output.time)
1612bfacbe3bSJiri Olsa 				alarm(rec->switch_output.time);
16133c1cb7e3SWang Nan 		}
16143c1cb7e3SWang Nan 
1615d20deb64SArnaldo Carvalho de Melo 		if (hits == rec->samples) {
16166dcf45efSArnaldo Carvalho de Melo 			if (done || draining)
1617649c48a9SPeter Zijlstra 				break;
1618f66a889dSArnaldo Carvalho de Melo 			err = perf_evlist__poll(rec->evlist, -1);
1619a515114fSJiri Olsa 			/*
1620a515114fSJiri Olsa 			 * Propagate error, only if there's any. Ignore positive
1621a515114fSJiri Olsa 			 * number of returned events and interrupt error.
1622a515114fSJiri Olsa 			 */
1623a515114fSJiri Olsa 			if (err > 0 || (err < 0 && errno == EINTR))
162445604710SNamhyung Kim 				err = 0;
16258b412664SPeter Zijlstra 			waking++;
16266dcf45efSArnaldo Carvalho de Melo 
16276dcf45efSArnaldo Carvalho de Melo 			if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
16286dcf45efSArnaldo Carvalho de Melo 				draining = true;
16298b412664SPeter Zijlstra 		}
16308b412664SPeter Zijlstra 
1631774cb499SJiri Olsa 		/*
1632774cb499SJiri Olsa 		 * When perf is starting the traced process, at the end events
1633774cb499SJiri Olsa 		 * die with the process and we wait for that. Thus no need to
1634774cb499SJiri Olsa 		 * disable events in this case.
1635774cb499SJiri Olsa 		 */
1636602ad878SArnaldo Carvalho de Melo 		if (done && !disabled && !target__none(&opts->target)) {
16375f9cf599SWang Nan 			trigger_off(&auxtrace_snapshot_trigger);
1638e74676deSJiri Olsa 			evlist__disable(rec->evlist);
16392711926aSJiri Olsa 			disabled = true;
16402711926aSJiri Olsa 		}
16418b412664SPeter Zijlstra 	}
1642ce7b0e42SAlexander Shishkin 
16435f9cf599SWang Nan 	trigger_off(&auxtrace_snapshot_trigger);
16443c1cb7e3SWang Nan 	trigger_off(&switch_output_trigger);
16458b412664SPeter Zijlstra 
1646ce7b0e42SAlexander Shishkin 	if (opts->auxtrace_snapshot_on_exit)
1647ce7b0e42SAlexander Shishkin 		record__auxtrace_snapshot_exit(rec);
1648ce7b0e42SAlexander Shishkin 
1649f33cbe72SArnaldo Carvalho de Melo 	if (forks && workload_exec_errno) {
165035550da3SMasami Hiramatsu 		char msg[STRERR_BUFSIZE];
1651c8b5f2c9SArnaldo Carvalho de Melo 		const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
1652f33cbe72SArnaldo Carvalho de Melo 		pr_err("Workload failed: %s\n", emsg);
1653f33cbe72SArnaldo Carvalho de Melo 		err = -1;
165445604710SNamhyung Kim 		goto out_child;
1655f33cbe72SArnaldo Carvalho de Melo 	}
1656f33cbe72SArnaldo Carvalho de Melo 
1657e3d59112SNamhyung Kim 	if (!quiet)
16588b412664SPeter Zijlstra 		fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
165986470930SIngo Molnar 
16604ea648aeSWang Nan 	if (target__none(&rec->opts.target))
16614ea648aeSWang Nan 		record__synthesize_workload(rec, true);
16624ea648aeSWang Nan 
166345604710SNamhyung Kim out_child:
1664470530bbSAlexey Budankov 	record__mmap_read_all(rec, true);
1665d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
1666d3d1af6fSAlexey Budankov 
1667d3c8c08eSAlexey Budankov 	if (rec->session->bytes_transferred && rec->session->bytes_compressed) {
1668d3c8c08eSAlexey Budankov 		ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed;
1669d3c8c08eSAlexey Budankov 		session->header.env.comp_ratio = ratio + 0.5;
1670d3c8c08eSAlexey Budankov 	}
1671d3c8c08eSAlexey Budankov 
167245604710SNamhyung Kim 	if (forks) {
167345604710SNamhyung Kim 		int exit_status;
167445604710SNamhyung Kim 
167545604710SNamhyung Kim 		if (!child_finished)
167645604710SNamhyung Kim 			kill(rec->evlist->workload.pid, SIGTERM);
167745604710SNamhyung Kim 
167845604710SNamhyung Kim 		wait(&exit_status);
167945604710SNamhyung Kim 
168045604710SNamhyung Kim 		if (err < 0)
168145604710SNamhyung Kim 			status = err;
168245604710SNamhyung Kim 		else if (WIFEXITED(exit_status))
168345604710SNamhyung Kim 			status = WEXITSTATUS(exit_status);
168445604710SNamhyung Kim 		else if (WIFSIGNALED(exit_status))
168545604710SNamhyung Kim 			signr = WTERMSIG(exit_status);
168645604710SNamhyung Kim 	} else
168745604710SNamhyung Kim 		status = err;
168845604710SNamhyung Kim 
16894ea648aeSWang Nan 	record__synthesize(rec, true);
1690e3d59112SNamhyung Kim 	/* this will be recalculated during process_buildids() */
1691e3d59112SNamhyung Kim 	rec->samples = 0;
1692e3d59112SNamhyung Kim 
1693ecfd7a9cSWang Nan 	if (!err) {
1694ecfd7a9cSWang Nan 		if (!rec->timestamp_filename) {
1695e1ab48baSWang Nan 			record__finish_output(rec);
1696ecfd7a9cSWang Nan 		} else {
1697ecfd7a9cSWang Nan 			fd = record__switch_output(rec, true);
1698ecfd7a9cSWang Nan 			if (fd < 0) {
1699ecfd7a9cSWang Nan 				status = fd;
1700ecfd7a9cSWang Nan 				goto out_delete_session;
1701ecfd7a9cSWang Nan 			}
1702ecfd7a9cSWang Nan 		}
1703ecfd7a9cSWang Nan 	}
170439d17dacSArnaldo Carvalho de Melo 
1705a074865eSWang Nan 	perf_hooks__invoke_record_end();
1706a074865eSWang Nan 
1707e3d59112SNamhyung Kim 	if (!err && !quiet) {
1708e3d59112SNamhyung Kim 		char samples[128];
1709ecfd7a9cSWang Nan 		const char *postfix = rec->timestamp_filename ?
1710ecfd7a9cSWang Nan 					".<timestamp>" : "";
1711e3d59112SNamhyung Kim 
1712ef149c25SAdrian Hunter 		if (rec->samples && !rec->opts.full_auxtrace)
1713e3d59112SNamhyung Kim 			scnprintf(samples, sizeof(samples),
1714e3d59112SNamhyung Kim 				  " (%" PRIu64 " samples)", rec->samples);
1715e3d59112SNamhyung Kim 		else
1716e3d59112SNamhyung Kim 			samples[0] = '\0';
1717e3d59112SNamhyung Kim 
1718d3c8c08eSAlexey Budankov 		fprintf(stderr,	"[ perf record: Captured and wrote %.3f MB %s%s%s",
17198ceb41d7SJiri Olsa 			perf_data__size(data) / 1024.0 / 1024.0,
17202d4f2799SJiri Olsa 			data->path, postfix, samples);
1721d3c8c08eSAlexey Budankov 		if (ratio) {
1722d3c8c08eSAlexey Budankov 			fprintf(stderr,	", compressed (original %.3f MB, ratio is %.3f)",
1723d3c8c08eSAlexey Budankov 					rec->session->bytes_transferred / 1024.0 / 1024.0,
1724d3c8c08eSAlexey Budankov 					ratio);
1725d3c8c08eSAlexey Budankov 		}
1726d3c8c08eSAlexey Budankov 		fprintf(stderr, " ]\n");
1727e3d59112SNamhyung Kim 	}
1728e3d59112SNamhyung Kim 
172939d17dacSArnaldo Carvalho de Melo out_delete_session:
17305d7f4116SAlexey Budankov 	zstd_fini(&session->zstd_data);
173139d17dacSArnaldo Carvalho de Melo 	perf_session__delete(session);
1732657ee553SSong Liu 
1733657ee553SSong Liu 	if (!opts->no_bpf_event)
1734657ee553SSong Liu 		perf_evlist__stop_sb_thread(sb_evlist);
173545604710SNamhyung Kim 	return status;
173686470930SIngo Molnar }
173786470930SIngo Molnar 
17380883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain)
173909b0fd45SJiri Olsa {
1740aad2b21cSKan Liang 	static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
1741a601fdffSJiri Olsa 
17420883e820SArnaldo Carvalho de Melo 	pr_debug("callchain: type %s\n", str[callchain->record_mode]);
174326d33022SJiri Olsa 
17440883e820SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_DWARF)
174509b0fd45SJiri Olsa 		pr_debug("callchain: stack dump size %d\n",
17460883e820SArnaldo Carvalho de Melo 			 callchain->dump_size);
17470883e820SArnaldo Carvalho de Melo }
17480883e820SArnaldo Carvalho de Melo 
17490883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record,
17500883e820SArnaldo Carvalho de Melo 				 struct callchain_param *callchain,
17510883e820SArnaldo Carvalho de Melo 				 const char *arg, bool unset)
17520883e820SArnaldo Carvalho de Melo {
17530883e820SArnaldo Carvalho de Melo 	int ret;
17540883e820SArnaldo Carvalho de Melo 	callchain->enabled = !unset;
17550883e820SArnaldo Carvalho de Melo 
17560883e820SArnaldo Carvalho de Melo 	/* --no-call-graph */
17570883e820SArnaldo Carvalho de Melo 	if (unset) {
17580883e820SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_NONE;
17590883e820SArnaldo Carvalho de Melo 		pr_debug("callchain: disabled\n");
17600883e820SArnaldo Carvalho de Melo 		return 0;
17610883e820SArnaldo Carvalho de Melo 	}
17620883e820SArnaldo Carvalho de Melo 
17630883e820SArnaldo Carvalho de Melo 	ret = parse_callchain_record_opt(arg, callchain);
17640883e820SArnaldo Carvalho de Melo 	if (!ret) {
17650883e820SArnaldo Carvalho de Melo 		/* Enable data address sampling for DWARF unwind. */
17660883e820SArnaldo Carvalho de Melo 		if (callchain->record_mode == CALLCHAIN_DWARF)
17670883e820SArnaldo Carvalho de Melo 			record->sample_address = true;
17680883e820SArnaldo Carvalho de Melo 		callchain_debug(callchain);
17690883e820SArnaldo Carvalho de Melo 	}
17700883e820SArnaldo Carvalho de Melo 
17710883e820SArnaldo Carvalho de Melo 	return ret;
177209b0fd45SJiri Olsa }
177309b0fd45SJiri Olsa 
1774c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt,
177509b0fd45SJiri Olsa 			       const char *arg,
177609b0fd45SJiri Olsa 			       int unset)
177709b0fd45SJiri Olsa {
17780883e820SArnaldo Carvalho de Melo 	return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
177926d33022SJiri Olsa }
178026d33022SJiri Olsa 
1781c421e80bSKan Liang int record_callchain_opt(const struct option *opt,
178209b0fd45SJiri Olsa 			 const char *arg __maybe_unused,
178309b0fd45SJiri Olsa 			 int unset __maybe_unused)
178409b0fd45SJiri Olsa {
17852ddd5c04SArnaldo Carvalho de Melo 	struct callchain_param *callchain = opt->value;
1786c421e80bSKan Liang 
17872ddd5c04SArnaldo Carvalho de Melo 	callchain->enabled = true;
178809b0fd45SJiri Olsa 
17892ddd5c04SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_NONE)
17902ddd5c04SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_FP;
1791eb853e80SJiri Olsa 
17922ddd5c04SArnaldo Carvalho de Melo 	callchain_debug(callchain);
179309b0fd45SJiri Olsa 	return 0;
179409b0fd45SJiri Olsa }
179509b0fd45SJiri Olsa 
1796eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb)
1797eb853e80SJiri Olsa {
17987a29c087SNamhyung Kim 	struct record *rec = cb;
17997a29c087SNamhyung Kim 
18007a29c087SNamhyung Kim 	if (!strcmp(var, "record.build-id")) {
18017a29c087SNamhyung Kim 		if (!strcmp(value, "cache"))
18027a29c087SNamhyung Kim 			rec->no_buildid_cache = false;
18037a29c087SNamhyung Kim 		else if (!strcmp(value, "no-cache"))
18047a29c087SNamhyung Kim 			rec->no_buildid_cache = true;
18057a29c087SNamhyung Kim 		else if (!strcmp(value, "skip"))
18067a29c087SNamhyung Kim 			rec->no_buildid = true;
18077a29c087SNamhyung Kim 		else
18087a29c087SNamhyung Kim 			return -1;
18097a29c087SNamhyung Kim 		return 0;
18107a29c087SNamhyung Kim 	}
1811cff17205SYisheng Xie 	if (!strcmp(var, "record.call-graph")) {
1812cff17205SYisheng Xie 		var = "call-graph.record-mode";
1813eb853e80SJiri Olsa 		return perf_default_config(var, value, cb);
1814eb853e80SJiri Olsa 	}
181593f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
181693f20c0fSAlexey Budankov 	if (!strcmp(var, "record.aio")) {
181793f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = strtol(value, NULL, 0);
181893f20c0fSAlexey Budankov 		if (!rec->opts.nr_cblocks)
181993f20c0fSAlexey Budankov 			rec->opts.nr_cblocks = nr_cblocks_default;
182093f20c0fSAlexey Budankov 	}
182193f20c0fSAlexey Budankov #endif
1822eb853e80SJiri Olsa 
1823cff17205SYisheng Xie 	return 0;
1824cff17205SYisheng Xie }
1825cff17205SYisheng Xie 
1826814c8c38SPeter Zijlstra struct clockid_map {
1827814c8c38SPeter Zijlstra 	const char *name;
1828814c8c38SPeter Zijlstra 	int clockid;
1829814c8c38SPeter Zijlstra };
1830814c8c38SPeter Zijlstra 
1831814c8c38SPeter Zijlstra #define CLOCKID_MAP(n, c)	\
1832814c8c38SPeter Zijlstra 	{ .name = n, .clockid = (c), }
1833814c8c38SPeter Zijlstra 
1834814c8c38SPeter Zijlstra #define CLOCKID_END	{ .name = NULL, }
1835814c8c38SPeter Zijlstra 
1836814c8c38SPeter Zijlstra 
1837814c8c38SPeter Zijlstra /*
1838814c8c38SPeter Zijlstra  * Add the missing ones, we need to build on many distros...
1839814c8c38SPeter Zijlstra  */
1840814c8c38SPeter Zijlstra #ifndef CLOCK_MONOTONIC_RAW
1841814c8c38SPeter Zijlstra #define CLOCK_MONOTONIC_RAW 4
1842814c8c38SPeter Zijlstra #endif
1843814c8c38SPeter Zijlstra #ifndef CLOCK_BOOTTIME
1844814c8c38SPeter Zijlstra #define CLOCK_BOOTTIME 7
1845814c8c38SPeter Zijlstra #endif
1846814c8c38SPeter Zijlstra #ifndef CLOCK_TAI
1847814c8c38SPeter Zijlstra #define CLOCK_TAI 11
1848814c8c38SPeter Zijlstra #endif
1849814c8c38SPeter Zijlstra 
1850814c8c38SPeter Zijlstra static const struct clockid_map clockids[] = {
1851814c8c38SPeter Zijlstra 	/* available for all events, NMI safe */
1852814c8c38SPeter Zijlstra 	CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
1853814c8c38SPeter Zijlstra 	CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
1854814c8c38SPeter Zijlstra 
1855814c8c38SPeter Zijlstra 	/* available for some events */
1856814c8c38SPeter Zijlstra 	CLOCKID_MAP("realtime", CLOCK_REALTIME),
1857814c8c38SPeter Zijlstra 	CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
1858814c8c38SPeter Zijlstra 	CLOCKID_MAP("tai", CLOCK_TAI),
1859814c8c38SPeter Zijlstra 
1860814c8c38SPeter Zijlstra 	/* available for the lazy */
1861814c8c38SPeter Zijlstra 	CLOCKID_MAP("mono", CLOCK_MONOTONIC),
1862814c8c38SPeter Zijlstra 	CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
1863814c8c38SPeter Zijlstra 	CLOCKID_MAP("real", CLOCK_REALTIME),
1864814c8c38SPeter Zijlstra 	CLOCKID_MAP("boot", CLOCK_BOOTTIME),
1865814c8c38SPeter Zijlstra 
1866814c8c38SPeter Zijlstra 	CLOCKID_END,
1867814c8c38SPeter Zijlstra };
1868814c8c38SPeter Zijlstra 
1869cf790516SAlexey Budankov static int get_clockid_res(clockid_t clk_id, u64 *res_ns)
1870cf790516SAlexey Budankov {
1871cf790516SAlexey Budankov 	struct timespec res;
1872cf790516SAlexey Budankov 
1873cf790516SAlexey Budankov 	*res_ns = 0;
1874cf790516SAlexey Budankov 	if (!clock_getres(clk_id, &res))
1875cf790516SAlexey Budankov 		*res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC;
1876cf790516SAlexey Budankov 	else
1877cf790516SAlexey Budankov 		pr_warning("WARNING: Failed to determine specified clock resolution.\n");
1878cf790516SAlexey Budankov 
1879cf790516SAlexey Budankov 	return 0;
1880cf790516SAlexey Budankov }
1881cf790516SAlexey Budankov 
1882814c8c38SPeter Zijlstra static int parse_clockid(const struct option *opt, const char *str, int unset)
1883814c8c38SPeter Zijlstra {
1884814c8c38SPeter Zijlstra 	struct record_opts *opts = (struct record_opts *)opt->value;
1885814c8c38SPeter Zijlstra 	const struct clockid_map *cm;
1886814c8c38SPeter Zijlstra 	const char *ostr = str;
1887814c8c38SPeter Zijlstra 
1888814c8c38SPeter Zijlstra 	if (unset) {
1889814c8c38SPeter Zijlstra 		opts->use_clockid = 0;
1890814c8c38SPeter Zijlstra 		return 0;
1891814c8c38SPeter Zijlstra 	}
1892814c8c38SPeter Zijlstra 
1893814c8c38SPeter Zijlstra 	/* no arg passed */
1894814c8c38SPeter Zijlstra 	if (!str)
1895814c8c38SPeter Zijlstra 		return 0;
1896814c8c38SPeter Zijlstra 
1897814c8c38SPeter Zijlstra 	/* no setting it twice */
1898814c8c38SPeter Zijlstra 	if (opts->use_clockid)
1899814c8c38SPeter Zijlstra 		return -1;
1900814c8c38SPeter Zijlstra 
1901814c8c38SPeter Zijlstra 	opts->use_clockid = true;
1902814c8c38SPeter Zijlstra 
1903814c8c38SPeter Zijlstra 	/* if its a number, we're done */
1904814c8c38SPeter Zijlstra 	if (sscanf(str, "%d", &opts->clockid) == 1)
1905cf790516SAlexey Budankov 		return get_clockid_res(opts->clockid, &opts->clockid_res_ns);
1906814c8c38SPeter Zijlstra 
1907814c8c38SPeter Zijlstra 	/* allow a "CLOCK_" prefix to the name */
1908814c8c38SPeter Zijlstra 	if (!strncasecmp(str, "CLOCK_", 6))
1909814c8c38SPeter Zijlstra 		str += 6;
1910814c8c38SPeter Zijlstra 
1911814c8c38SPeter Zijlstra 	for (cm = clockids; cm->name; cm++) {
1912814c8c38SPeter Zijlstra 		if (!strcasecmp(str, cm->name)) {
1913814c8c38SPeter Zijlstra 			opts->clockid = cm->clockid;
1914cf790516SAlexey Budankov 			return get_clockid_res(opts->clockid,
1915cf790516SAlexey Budankov 					       &opts->clockid_res_ns);
1916814c8c38SPeter Zijlstra 		}
1917814c8c38SPeter Zijlstra 	}
1918814c8c38SPeter Zijlstra 
1919814c8c38SPeter Zijlstra 	opts->use_clockid = false;
1920814c8c38SPeter Zijlstra 	ui__warning("unknown clockid %s, check man page\n", ostr);
1921814c8c38SPeter Zijlstra 	return -1;
1922814c8c38SPeter Zijlstra }
1923814c8c38SPeter Zijlstra 
1924f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset)
1925f4fe11b7SAlexey Budankov {
1926f4fe11b7SAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
1927f4fe11b7SAlexey Budankov 
1928f4fe11b7SAlexey Budankov 	if (unset || !str)
1929f4fe11b7SAlexey Budankov 		return 0;
1930f4fe11b7SAlexey Budankov 
1931f4fe11b7SAlexey Budankov 	if (!strcasecmp(str, "node"))
1932f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_NODE;
1933f4fe11b7SAlexey Budankov 	else if (!strcasecmp(str, "cpu"))
1934f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_CPU;
1935f4fe11b7SAlexey Budankov 
1936f4fe11b7SAlexey Budankov 	return 0;
1937f4fe11b7SAlexey Budankov }
1938f4fe11b7SAlexey Budankov 
1939e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt,
1940e9db1310SAdrian Hunter 				    const char *str,
1941e9db1310SAdrian Hunter 				    int unset __maybe_unused)
1942e9db1310SAdrian Hunter {
1943e9db1310SAdrian Hunter 	struct record_opts *opts = opt->value;
1944e9db1310SAdrian Hunter 	char *s, *p;
1945e9db1310SAdrian Hunter 	unsigned int mmap_pages;
1946e9db1310SAdrian Hunter 	int ret;
1947e9db1310SAdrian Hunter 
1948e9db1310SAdrian Hunter 	if (!str)
1949e9db1310SAdrian Hunter 		return -EINVAL;
1950e9db1310SAdrian Hunter 
1951e9db1310SAdrian Hunter 	s = strdup(str);
1952e9db1310SAdrian Hunter 	if (!s)
1953e9db1310SAdrian Hunter 		return -ENOMEM;
1954e9db1310SAdrian Hunter 
1955e9db1310SAdrian Hunter 	p = strchr(s, ',');
1956e9db1310SAdrian Hunter 	if (p)
1957e9db1310SAdrian Hunter 		*p = '\0';
1958e9db1310SAdrian Hunter 
1959e9db1310SAdrian Hunter 	if (*s) {
1960e9db1310SAdrian Hunter 		ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s);
1961e9db1310SAdrian Hunter 		if (ret)
1962e9db1310SAdrian Hunter 			goto out_free;
1963e9db1310SAdrian Hunter 		opts->mmap_pages = mmap_pages;
1964e9db1310SAdrian Hunter 	}
1965e9db1310SAdrian Hunter 
1966e9db1310SAdrian Hunter 	if (!p) {
1967e9db1310SAdrian Hunter 		ret = 0;
1968e9db1310SAdrian Hunter 		goto out_free;
1969e9db1310SAdrian Hunter 	}
1970e9db1310SAdrian Hunter 
1971e9db1310SAdrian Hunter 	ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1);
1972e9db1310SAdrian Hunter 	if (ret)
1973e9db1310SAdrian Hunter 		goto out_free;
1974e9db1310SAdrian Hunter 
1975e9db1310SAdrian Hunter 	opts->auxtrace_mmap_pages = mmap_pages;
1976e9db1310SAdrian Hunter 
1977e9db1310SAdrian Hunter out_free:
1978e9db1310SAdrian Hunter 	free(s);
1979e9db1310SAdrian Hunter 	return ret;
1980e9db1310SAdrian Hunter }
1981e9db1310SAdrian Hunter 
19820c582449SJiri Olsa static void switch_output_size_warn(struct record *rec)
19830c582449SJiri Olsa {
19849521b5f2SJiri Olsa 	u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages);
19850c582449SJiri Olsa 	struct switch_output *s = &rec->switch_output;
19860c582449SJiri Olsa 
19870c582449SJiri Olsa 	wakeup_size /= 2;
19880c582449SJiri Olsa 
19890c582449SJiri Olsa 	if (s->size < wakeup_size) {
19900c582449SJiri Olsa 		char buf[100];
19910c582449SJiri Olsa 
19920c582449SJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
19930c582449SJiri Olsa 		pr_warning("WARNING: switch-output data size lower than "
19940c582449SJiri Olsa 			   "wakeup kernel buffer size (%s) "
19950c582449SJiri Olsa 			   "expect bigger perf.data sizes\n", buf);
19960c582449SJiri Olsa 	}
19970c582449SJiri Olsa }
19980c582449SJiri Olsa 
1999cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec)
2000cb4e1ebbSJiri Olsa {
2001cb4e1ebbSJiri Olsa 	struct switch_output *s = &rec->switch_output;
2002dc0c6127SJiri Olsa 	static struct parse_tag tags_size[] = {
2003dc0c6127SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
2004dc0c6127SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
2005dc0c6127SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
2006dc0c6127SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
2007dc0c6127SJiri Olsa 		{ .tag  = 0 },
2008dc0c6127SJiri Olsa 	};
2009bfacbe3bSJiri Olsa 	static struct parse_tag tags_time[] = {
2010bfacbe3bSJiri Olsa 		{ .tag  = 's', .mult = 1        },
2011bfacbe3bSJiri Olsa 		{ .tag  = 'm', .mult = 60       },
2012bfacbe3bSJiri Olsa 		{ .tag  = 'h', .mult = 60*60    },
2013bfacbe3bSJiri Olsa 		{ .tag  = 'd', .mult = 60*60*24 },
2014bfacbe3bSJiri Olsa 		{ .tag  = 0 },
2015bfacbe3bSJiri Olsa 	};
2016dc0c6127SJiri Olsa 	unsigned long val;
2017cb4e1ebbSJiri Olsa 
2018cb4e1ebbSJiri Olsa 	if (!s->set)
2019cb4e1ebbSJiri Olsa 		return 0;
2020cb4e1ebbSJiri Olsa 
2021cb4e1ebbSJiri Olsa 	if (!strcmp(s->str, "signal")) {
2022cb4e1ebbSJiri Olsa 		s->signal = true;
2023cb4e1ebbSJiri Olsa 		pr_debug("switch-output with SIGUSR2 signal\n");
2024dc0c6127SJiri Olsa 		goto enabled;
2025dc0c6127SJiri Olsa 	}
2026dc0c6127SJiri Olsa 
2027dc0c6127SJiri Olsa 	val = parse_tag_value(s->str, tags_size);
2028dc0c6127SJiri Olsa 	if (val != (unsigned long) -1) {
2029dc0c6127SJiri Olsa 		s->size = val;
2030dc0c6127SJiri Olsa 		pr_debug("switch-output with %s size threshold\n", s->str);
2031dc0c6127SJiri Olsa 		goto enabled;
2032cb4e1ebbSJiri Olsa 	}
2033cb4e1ebbSJiri Olsa 
2034bfacbe3bSJiri Olsa 	val = parse_tag_value(s->str, tags_time);
2035bfacbe3bSJiri Olsa 	if (val != (unsigned long) -1) {
2036bfacbe3bSJiri Olsa 		s->time = val;
2037bfacbe3bSJiri Olsa 		pr_debug("switch-output with %s time threshold (%lu seconds)\n",
2038bfacbe3bSJiri Olsa 			 s->str, s->time);
2039bfacbe3bSJiri Olsa 		goto enabled;
2040bfacbe3bSJiri Olsa 	}
2041bfacbe3bSJiri Olsa 
2042cb4e1ebbSJiri Olsa 	return -1;
2043dc0c6127SJiri Olsa 
2044dc0c6127SJiri Olsa enabled:
2045dc0c6127SJiri Olsa 	rec->timestamp_filename = true;
2046dc0c6127SJiri Olsa 	s->enabled              = true;
20470c582449SJiri Olsa 
20480c582449SJiri Olsa 	if (s->size && !rec->opts.no_buffering)
20490c582449SJiri Olsa 		switch_output_size_warn(rec);
20500c582449SJiri Olsa 
2051dc0c6127SJiri Olsa 	return 0;
2052cb4e1ebbSJiri Olsa }
2053cb4e1ebbSJiri Olsa 
2054e5b2c207SNamhyung Kim static const char * const __record_usage[] = {
205586470930SIngo Molnar 	"perf record [<options>] [<command>]",
205686470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
205786470930SIngo Molnar 	NULL
205886470930SIngo Molnar };
2059e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage;
206086470930SIngo Molnar 
2061d20deb64SArnaldo Carvalho de Melo /*
20628c6f45a7SArnaldo Carvalho de Melo  * XXX Ideally would be local to cmd_record() and passed to a record__new
20638c6f45a7SArnaldo Carvalho de Melo  * because we need to have access to it in record__exit, that is called
2064d20deb64SArnaldo Carvalho de Melo  * after cmd_record() exits, but since record_options need to be accessible to
2065d20deb64SArnaldo Carvalho de Melo  * builtin-script, leave it here.
2066d20deb64SArnaldo Carvalho de Melo  *
2067d20deb64SArnaldo Carvalho de Melo  * At least we don't ouch it in all the other functions here directly.
2068d20deb64SArnaldo Carvalho de Melo  *
2069d20deb64SArnaldo Carvalho de Melo  * Just say no to tons of global variables, sigh.
2070d20deb64SArnaldo Carvalho de Melo  */
20718c6f45a7SArnaldo Carvalho de Melo static struct record record = {
2072d20deb64SArnaldo Carvalho de Melo 	.opts = {
20738affc2b8SAndi Kleen 		.sample_time	     = true,
2074d20deb64SArnaldo Carvalho de Melo 		.mmap_pages	     = UINT_MAX,
2075d20deb64SArnaldo Carvalho de Melo 		.user_freq	     = UINT_MAX,
2076d20deb64SArnaldo Carvalho de Melo 		.user_interval	     = ULLONG_MAX,
2077447a6013SArnaldo Carvalho de Melo 		.freq		     = 4000,
2078d1cb9fceSNamhyung Kim 		.target		     = {
2079d1cb9fceSNamhyung Kim 			.uses_mmap   = true,
20803aa5939dSAdrian Hunter 			.default_per_cpu = true,
2081d1cb9fceSNamhyung Kim 		},
2082470530bbSAlexey Budankov 		.mmap_flush          = MMAP_FLUSH_DEFAULT,
2083d20deb64SArnaldo Carvalho de Melo 	},
2084e3d59112SNamhyung Kim 	.tool = {
2085e3d59112SNamhyung Kim 		.sample		= process_sample_event,
2086e3d59112SNamhyung Kim 		.fork		= perf_event__process_fork,
2087cca8482cSAdrian Hunter 		.exit		= perf_event__process_exit,
2088e3d59112SNamhyung Kim 		.comm		= perf_event__process_comm,
2089f3b3614aSHari Bathini 		.namespaces	= perf_event__process_namespaces,
2090e3d59112SNamhyung Kim 		.mmap		= perf_event__process_mmap,
2091e3d59112SNamhyung Kim 		.mmap2		= perf_event__process_mmap2,
2092cca8482cSAdrian Hunter 		.ordered_events	= true,
2093e3d59112SNamhyung Kim 	},
2094d20deb64SArnaldo Carvalho de Melo };
20957865e817SFrederic Weisbecker 
209676a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
209776a26549SNamhyung Kim 	"\n\t\t\t\tDefault: fp";
209861eaa3beSArnaldo Carvalho de Melo 
20990aab2136SWang Nan static bool dry_run;
21000aab2136SWang Nan 
2101d20deb64SArnaldo Carvalho de Melo /*
2102d20deb64SArnaldo Carvalho de Melo  * XXX Will stay a global variable till we fix builtin-script.c to stop messing
2103d20deb64SArnaldo Carvalho de Melo  * with it and switch to use the library functions in perf_evlist that came
2104b4006796SArnaldo Carvalho de Melo  * from builtin-record.c, i.e. use record_opts,
2105d20deb64SArnaldo Carvalho de Melo  * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
2106d20deb64SArnaldo Carvalho de Melo  * using pipes, etc.
2107d20deb64SArnaldo Carvalho de Melo  */
2108efd21307SJiri Olsa static struct option __record_options[] = {
2109d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &record.evlist, "event",
211086470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
2111f120f9d5SJiri Olsa 		     parse_events_option),
2112d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK(0, "filter", &record.evlist, "filter",
2113c171b552SLi Zefan 		     "event filter", parse_filter),
21144ba1faa1SWang Nan 	OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
21154ba1faa1SWang Nan 			   NULL, "don't record events from perf itself",
21164ba1faa1SWang Nan 			   exclude_perf),
2117bea03405SNamhyung Kim 	OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
2118d6d901c2SZhang, Yanmin 		    "record events on existing process id"),
2119bea03405SNamhyung Kim 	OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
2120d6d901c2SZhang, Yanmin 		    "record events on existing thread id"),
2121d20deb64SArnaldo Carvalho de Melo 	OPT_INTEGER('r', "realtime", &record.realtime_prio,
212286470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
2123509051eaSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
2124acac03faSKirill Smelkov 		    "collect data without buffering"),
2125d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
2126daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
2127bea03405SNamhyung Kim 	OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
212886470930SIngo Molnar 			    "system-wide collection from all CPUs"),
2129bea03405SNamhyung Kim 	OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
2130c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
2131d20deb64SArnaldo Carvalho de Melo 	OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
21322d4f2799SJiri Olsa 	OPT_STRING('o', "output", &record.data.path, "file",
213386470930SIngo Molnar 		    "output file name"),
213469e7e5b0SAdrian Hunter 	OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
213569e7e5b0SAdrian Hunter 			&record.opts.no_inherit_set,
21362e6cdf99SStephane Eranian 			"child tasks do not inherit counters"),
21374ea648aeSWang Nan 	OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
21384ea648aeSWang Nan 		    "synthesize non-sample events at the end of output"),
2139626a6b78SWang Nan 	OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
214071184c6aSSong Liu 	OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "record bpf events"),
2141b09c2364SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq,
2142b09c2364SArnaldo Carvalho de Melo 		    "Fail if the specified frequency can't be used"),
214367230479SArnaldo Carvalho de Melo 	OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'",
214467230479SArnaldo Carvalho de Melo 		     "profile at this frequency",
214567230479SArnaldo Carvalho de Melo 		      record__parse_freq),
2146e9db1310SAdrian Hunter 	OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
2147e9db1310SAdrian Hunter 		     "number of mmap data pages and AUX area tracing mmap pages",
2148e9db1310SAdrian Hunter 		     record__parse_mmap_pages),
2149470530bbSAlexey Budankov 	OPT_CALLBACK(0, "mmap-flush", &record.opts, "number",
2150470530bbSAlexey Budankov 		     "Minimal number of bytes that is extracted from mmap data pages (default: 1)",
2151470530bbSAlexey Budankov 		     record__mmap_flush_parse),
2152d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "group", &record.opts.group,
215343bece79SLin Ming 		    "put the counters into a counter group"),
21542ddd5c04SArnaldo Carvalho de Melo 	OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
215509b0fd45SJiri Olsa 			   NULL, "enables call-graph recording" ,
215609b0fd45SJiri Olsa 			   &record_callchain_opt),
215709b0fd45SJiri Olsa 	OPT_CALLBACK(0, "call-graph", &record.opts,
215876a26549SNamhyung Kim 		     "record_mode[,record_size]", record_callchain_help,
215909b0fd45SJiri Olsa 		     &record_parse_callchain_opt),
2160c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
21613da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
2162b44308f5SArnaldo Carvalho de Melo 	OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
2163d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
2164649c48a9SPeter Zijlstra 		    "per thread counts"),
216556100321SPeter Zijlstra 	OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
21663b0a5daaSKan Liang 	OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr,
21673b0a5daaSKan Liang 		    "Record the sample physical addresses"),
2168b6f35ed7SJiri Olsa 	OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
21693abebc55SAdrian Hunter 	OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
21703abebc55SAdrian Hunter 			&record.opts.sample_time_set,
21713abebc55SAdrian Hunter 			"Record the sample timestamps"),
2172f290aa1fSJiri Olsa 	OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set,
2173f290aa1fSJiri Olsa 			"Record the sample period"),
2174d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
2175649c48a9SPeter Zijlstra 		    "don't sample"),
2176d2db9a98SWang Nan 	OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
2177d2db9a98SWang Nan 			&record.no_buildid_cache_set,
2178a1ac1d3cSStephane Eranian 			"do not update the buildid cache"),
2179d2db9a98SWang Nan 	OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
2180d2db9a98SWang Nan 			&record.no_buildid_set,
2181baa2f6ceSArnaldo Carvalho de Melo 			"do not collect buildids in perf.data"),
2182d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
2183023695d9SStephane Eranian 		     "monitor event in cgroup name only",
2184023695d9SStephane Eranian 		     parse_cgroups),
2185a6205a35SArnaldo Carvalho de Melo 	OPT_UINTEGER('D', "delay", &record.opts.initial_delay,
21866619a53eSAndi Kleen 		  "ms to wait before starting measurement after program start"),
2187bea03405SNamhyung Kim 	OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
2188bea03405SNamhyung Kim 		   "user to profile"),
2189a5aabdacSStephane Eranian 
2190a5aabdacSStephane Eranian 	OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
2191a5aabdacSStephane Eranian 		     "branch any", "sample any taken branches",
2192a5aabdacSStephane Eranian 		     parse_branch_stack),
2193a5aabdacSStephane Eranian 
2194a5aabdacSStephane Eranian 	OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
2195a5aabdacSStephane Eranian 		     "branch filter mask", "branch stack filter modes",
2196bdfebd84SRoberto Agostino Vitillo 		     parse_branch_stack),
219705484298SAndi Kleen 	OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
219805484298SAndi Kleen 		    "sample by weight (on special events only)"),
2199475eeab9SAndi Kleen 	OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
2200475eeab9SAndi Kleen 		    "sample transaction flags (special events only)"),
22013aa5939dSAdrian Hunter 	OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
22023aa5939dSAdrian Hunter 		    "use per-thread mmaps"),
2203bcc84ec6SStephane Eranian 	OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
2204bcc84ec6SStephane Eranian 		    "sample selected machine registers on interrupt,"
2205aeea9062SKan Liang 		    " use '-I?' to list register names", parse_intr_regs),
220684c41742SAndi Kleen 	OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register",
220784c41742SAndi Kleen 		    "sample selected machine registers on interrupt,"
2208aeea9062SKan Liang 		    " use '--user-regs=?' to list register names", parse_user_regs),
220985c273d2SAndi Kleen 	OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
221085c273d2SAndi Kleen 		    "Record running/enabled time of read (:S) events"),
2211814c8c38SPeter Zijlstra 	OPT_CALLBACK('k', "clockid", &record.opts,
2212814c8c38SPeter Zijlstra 	"clockid", "clockid to use for events, see clock_gettime()",
2213814c8c38SPeter Zijlstra 	parse_clockid),
22142dd6d8a1SAdrian Hunter 	OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
22152dd6d8a1SAdrian Hunter 			  "opts", "AUX area tracing Snapshot Mode", ""),
22163fcb10e4SMark Drayton 	OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
22179d9cad76SKan Liang 			"per thread proc mmap processing timeout in ms"),
2218f3b3614aSHari Bathini 	OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces,
2219f3b3614aSHari Bathini 		    "Record namespaces events"),
2220b757bb09SAdrian Hunter 	OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events,
2221b757bb09SAdrian Hunter 		    "Record context switch events"),
222285723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
222385723885SJiri Olsa 			 "Configure all used events to run in kernel space.",
222485723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
222585723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
222685723885SJiri Olsa 			 "Configure all used events to run in user space.",
222785723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
222853651b28Syuzhoujian 	OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains,
222953651b28Syuzhoujian 		    "collect kernel callchains"),
223053651b28Syuzhoujian 	OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains,
223153651b28Syuzhoujian 		    "collect user callchains"),
223271dc2326SWang Nan 	OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
223371dc2326SWang Nan 		   "clang binary to use for compiling BPF scriptlets"),
223471dc2326SWang Nan 	OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
223571dc2326SWang Nan 		   "options passed to clang when compiling BPF scriptlets"),
22367efe0e03SHe Kuang 	OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
22377efe0e03SHe Kuang 		   "file", "vmlinux pathname"),
22386156681bSNamhyung Kim 	OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
22396156681bSNamhyung Kim 		    "Record build-id of all DSOs regardless of hits"),
2240ecfd7a9cSWang Nan 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
2241ecfd7a9cSWang Nan 		    "append timestamp to output filename"),
224268588bafSJin Yao 	OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary,
224368588bafSJin Yao 		    "Record timestamp boundary (time of first/last samples)"),
2244cb4e1ebbSJiri Olsa 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
2245c38dab7dSAndi Kleen 			  &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
2246c38dab7dSAndi Kleen 			  "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
2247dc0c6127SJiri Olsa 			  "signal"),
224803724b2eSAndi Kleen 	OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
224903724b2eSAndi Kleen 		   "Limit number of switch output generated files"),
22500aab2136SWang Nan 	OPT_BOOLEAN(0, "dry-run", &dry_run,
22510aab2136SWang Nan 		    "Parse options then exit"),
2252d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
225393f20c0fSAlexey Budankov 	OPT_CALLBACK_OPTARG(0, "aio", &record.opts,
225493f20c0fSAlexey Budankov 		     &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
2255d3d1af6fSAlexey Budankov 		     record__aio_parse),
2256d3d1af6fSAlexey Budankov #endif
2257f4fe11b7SAlexey Budankov 	OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
2258f4fe11b7SAlexey Budankov 		     "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
2259f4fe11b7SAlexey Budankov 		     record__parse_affinity),
2260504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
2261504c1ad1SAlexey Budankov 	OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default,
2262504c1ad1SAlexey Budankov 			    "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
2263504c1ad1SAlexey Budankov 			    record__parse_comp_level),
2264504c1ad1SAlexey Budankov #endif
226586470930SIngo Molnar 	OPT_END()
226686470930SIngo Molnar };
226786470930SIngo Molnar 
2268e5b2c207SNamhyung Kim struct option *record_options = __record_options;
2269e5b2c207SNamhyung Kim 
2270b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv)
227186470930SIngo Molnar {
2272ef149c25SAdrian Hunter 	int err;
22738c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = &record;
227416ad2ffbSNamhyung Kim 	char errbuf[BUFSIZ];
227586470930SIngo Molnar 
227667230479SArnaldo Carvalho de Melo 	setlocale(LC_ALL, "");
227767230479SArnaldo Carvalho de Melo 
227848e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT
227948e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
228048e1cab1SWang Nan 	set_nobuild('\0', "clang-path", true);
228148e1cab1SWang Nan 	set_nobuild('\0', "clang-opt", true);
228248e1cab1SWang Nan # undef set_nobuild
228348e1cab1SWang Nan #endif
228448e1cab1SWang Nan 
22857efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE
22867efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT)
22877efe0e03SHe Kuang #  define REASON  "NO_DWARF=1"
22887efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT)
22897efe0e03SHe Kuang #  define REASON  "NO_LIBBPF=1"
22907efe0e03SHe Kuang # else
22917efe0e03SHe Kuang #  define REASON  "this architecture doesn't support BPF prologue"
22927efe0e03SHe Kuang # endif
22937efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
22947efe0e03SHe Kuang 	set_nobuild('\0', "vmlinux", true);
22957efe0e03SHe Kuang # undef set_nobuild
22967efe0e03SHe Kuang # undef REASON
22977efe0e03SHe Kuang #endif
22987efe0e03SHe Kuang 
22999d2ed645SAlexey Budankov 	CPU_ZERO(&rec->affinity_mask);
23009d2ed645SAlexey Budankov 	rec->opts.affinity = PERF_AFFINITY_SYS;
23019d2ed645SAlexey Budankov 
23020f98b11cSJiri Olsa 	rec->evlist = evlist__new();
23033e2be2daSArnaldo Carvalho de Melo 	if (rec->evlist == NULL)
2304361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
2305361c99a6SArnaldo Carvalho de Melo 
2306ecc4c561SArnaldo Carvalho de Melo 	err = perf_config(perf_record_config, rec);
2307ecc4c561SArnaldo Carvalho de Melo 	if (err)
2308ecc4c561SArnaldo Carvalho de Melo 		return err;
2309eb853e80SJiri Olsa 
2310bca647aaSTom Zanussi 	argc = parse_options(argc, argv, record_options, record_usage,
2311a0541234SAnton Blanchard 			    PARSE_OPT_STOP_AT_NON_OPTION);
231268ba3235SNamhyung Kim 	if (quiet)
231368ba3235SNamhyung Kim 		perf_quiet_option();
2314483635a9SJiri Olsa 
2315483635a9SJiri Olsa 	/* Make system wide (-a) the default target. */
2316602ad878SArnaldo Carvalho de Melo 	if (!argc && target__none(&rec->opts.target))
2317483635a9SJiri Olsa 		rec->opts.target.system_wide = true;
231886470930SIngo Molnar 
2319bea03405SNamhyung Kim 	if (nr_cgroups && !rec->opts.target.system_wide) {
2320c7118369SNamhyung Kim 		usage_with_options_msg(record_usage, record_options,
2321c7118369SNamhyung Kim 			"cgroup monitoring only available in system-wide mode");
2322c7118369SNamhyung Kim 
2323023695d9SStephane Eranian 	}
2324504c1ad1SAlexey Budankov 
2325504c1ad1SAlexey Budankov 	if (rec->opts.comp_level != 0) {
2326504c1ad1SAlexey Budankov 		pr_debug("Compression enabled, disabling build id collection at the end of the session.\n");
2327504c1ad1SAlexey Budankov 		rec->no_buildid = true;
2328504c1ad1SAlexey Budankov 	}
2329504c1ad1SAlexey Budankov 
2330b757bb09SAdrian Hunter 	if (rec->opts.record_switch_events &&
2331b757bb09SAdrian Hunter 	    !perf_can_record_switch_events()) {
2332c7118369SNamhyung Kim 		ui__error("kernel does not support recording context switch events\n");
2333c7118369SNamhyung Kim 		parse_options_usage(record_usage, record_options, "switch-events", 0);
2334c7118369SNamhyung Kim 		return -EINVAL;
2335b757bb09SAdrian Hunter 	}
2336023695d9SStephane Eranian 
2337cb4e1ebbSJiri Olsa 	if (switch_output_setup(rec)) {
2338cb4e1ebbSJiri Olsa 		parse_options_usage(record_usage, record_options, "switch-output", 0);
2339cb4e1ebbSJiri Olsa 		return -EINVAL;
2340cb4e1ebbSJiri Olsa 	}
2341cb4e1ebbSJiri Olsa 
2342bfacbe3bSJiri Olsa 	if (rec->switch_output.time) {
2343bfacbe3bSJiri Olsa 		signal(SIGALRM, alarm_sig_handler);
2344bfacbe3bSJiri Olsa 		alarm(rec->switch_output.time);
2345bfacbe3bSJiri Olsa 	}
2346bfacbe3bSJiri Olsa 
234703724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
234803724b2eSAndi Kleen 		rec->switch_output.filenames = calloc(sizeof(char *),
234903724b2eSAndi Kleen 						      rec->switch_output.num_files);
235003724b2eSAndi Kleen 		if (!rec->switch_output.filenames)
235103724b2eSAndi Kleen 			return -EINVAL;
235203724b2eSAndi Kleen 	}
235303724b2eSAndi Kleen 
23541b36c03eSAdrian Hunter 	/*
23551b36c03eSAdrian Hunter 	 * Allow aliases to facilitate the lookup of symbols for address
23561b36c03eSAdrian Hunter 	 * filters. Refer to auxtrace_parse_filters().
23571b36c03eSAdrian Hunter 	 */
23581b36c03eSAdrian Hunter 	symbol_conf.allow_aliases = true;
23591b36c03eSAdrian Hunter 
23601b36c03eSAdrian Hunter 	symbol__init(NULL);
23611b36c03eSAdrian Hunter 
23624b5ea3bdSAdrian Hunter 	err = record__auxtrace_init(rec);
23631b36c03eSAdrian Hunter 	if (err)
23641b36c03eSAdrian Hunter 		goto out;
23651b36c03eSAdrian Hunter 
23660aab2136SWang Nan 	if (dry_run)
23675c01ad60SAdrian Hunter 		goto out;
23680aab2136SWang Nan 
2369d7888573SWang Nan 	err = bpf__setup_stdout(rec->evlist);
2370d7888573SWang Nan 	if (err) {
2371d7888573SWang Nan 		bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
2372d7888573SWang Nan 		pr_err("ERROR: Setup BPF stdout failed: %s\n",
2373d7888573SWang Nan 			 errbuf);
23745c01ad60SAdrian Hunter 		goto out;
2375d7888573SWang Nan 	}
2376d7888573SWang Nan 
2377ef149c25SAdrian Hunter 	err = -ENOMEM;
2378ef149c25SAdrian Hunter 
23790c1d46a8SWang Nan 	if (rec->no_buildid_cache || rec->no_buildid) {
2380a1ac1d3cSStephane Eranian 		disable_buildid_cache();
2381dc0c6127SJiri Olsa 	} else if (rec->switch_output.enabled) {
23820c1d46a8SWang Nan 		/*
23830c1d46a8SWang Nan 		 * In 'perf record --switch-output', disable buildid
23840c1d46a8SWang Nan 		 * generation by default to reduce data file switching
23850c1d46a8SWang Nan 		 * overhead. Still generate buildid if they are required
23860c1d46a8SWang Nan 		 * explicitly using
23870c1d46a8SWang Nan 		 *
238860437ac0SJiri Olsa 		 *  perf record --switch-output --no-no-buildid \
23890c1d46a8SWang Nan 		 *              --no-no-buildid-cache
23900c1d46a8SWang Nan 		 *
23910c1d46a8SWang Nan 		 * Following code equals to:
23920c1d46a8SWang Nan 		 *
23930c1d46a8SWang Nan 		 * if ((rec->no_buildid || !rec->no_buildid_set) &&
23940c1d46a8SWang Nan 		 *     (rec->no_buildid_cache || !rec->no_buildid_cache_set))
23950c1d46a8SWang Nan 		 *         disable_buildid_cache();
23960c1d46a8SWang Nan 		 */
23970c1d46a8SWang Nan 		bool disable = true;
23980c1d46a8SWang Nan 
23990c1d46a8SWang Nan 		if (rec->no_buildid_set && !rec->no_buildid)
24000c1d46a8SWang Nan 			disable = false;
24010c1d46a8SWang Nan 		if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
24020c1d46a8SWang Nan 			disable = false;
24030c1d46a8SWang Nan 		if (disable) {
24040c1d46a8SWang Nan 			rec->no_buildid = true;
24050c1d46a8SWang Nan 			rec->no_buildid_cache = true;
24060c1d46a8SWang Nan 			disable_buildid_cache();
24070c1d46a8SWang Nan 		}
24080c1d46a8SWang Nan 	}
2409655000e7SArnaldo Carvalho de Melo 
24104ea648aeSWang Nan 	if (record.opts.overwrite)
24114ea648aeSWang Nan 		record.opts.tail_synthesize = true;
24124ea648aeSWang Nan 
24136484d2f9SJiri Olsa 	if (rec->evlist->core.nr_entries == 0 &&
24144b4cd503SArnaldo Carvalho de Melo 	    __perf_evlist__add_default(rec->evlist, !record.opts.no_samples) < 0) {
241569aad6f1SArnaldo Carvalho de Melo 		pr_err("Not enough memory for event selector list\n");
2416394c01edSAdrian Hunter 		goto out;
2417bbd36e5eSPeter Zijlstra 	}
241886470930SIngo Molnar 
241969e7e5b0SAdrian Hunter 	if (rec->opts.target.tid && !rec->opts.no_inherit_set)
242069e7e5b0SAdrian Hunter 		rec->opts.no_inherit = true;
242169e7e5b0SAdrian Hunter 
2422602ad878SArnaldo Carvalho de Melo 	err = target__validate(&rec->opts.target);
242316ad2ffbSNamhyung Kim 	if (err) {
2424602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
2425c3dec27bSJiri Olsa 		ui__warning("%s\n", errbuf);
242616ad2ffbSNamhyung Kim 	}
24274bd0f2d2SNamhyung Kim 
2428602ad878SArnaldo Carvalho de Melo 	err = target__parse_uid(&rec->opts.target);
242916ad2ffbSNamhyung Kim 	if (err) {
243016ad2ffbSNamhyung Kim 		int saved_errno = errno;
243116ad2ffbSNamhyung Kim 
2432602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
24333780f488SNamhyung Kim 		ui__error("%s", errbuf);
243416ad2ffbSNamhyung Kim 
243516ad2ffbSNamhyung Kim 		err = -saved_errno;
2436394c01edSAdrian Hunter 		goto out;
243716ad2ffbSNamhyung Kim 	}
24380d37aa34SArnaldo Carvalho de Melo 
2439ca800068SMengting Zhang 	/* Enable ignoring missing threads when -u/-p option is defined. */
2440ca800068SMengting Zhang 	rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid;
244123dc4f15SJiri Olsa 
244216ad2ffbSNamhyung Kim 	err = -ENOMEM;
24433e2be2daSArnaldo Carvalho de Melo 	if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0)
2444dd7927f4SArnaldo Carvalho de Melo 		usage_with_options(record_usage, record_options);
244569aad6f1SArnaldo Carvalho de Melo 
2446ef149c25SAdrian Hunter 	err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
2447ef149c25SAdrian Hunter 	if (err)
2448394c01edSAdrian Hunter 		goto out;
2449ef149c25SAdrian Hunter 
24506156681bSNamhyung Kim 	/*
24516156681bSNamhyung Kim 	 * We take all buildids when the file contains
24526156681bSNamhyung Kim 	 * AUX area tracing data because we do not decode the
24536156681bSNamhyung Kim 	 * trace because it would take too long.
24546156681bSNamhyung Kim 	 */
24556156681bSNamhyung Kim 	if (rec->opts.full_auxtrace)
24566156681bSNamhyung Kim 		rec->buildid_all = true;
24576156681bSNamhyung Kim 
2458b4006796SArnaldo Carvalho de Melo 	if (record_opts__config(&rec->opts)) {
245939d17dacSArnaldo Carvalho de Melo 		err = -EINVAL;
2460394c01edSAdrian Hunter 		goto out;
24617e4ff9e3SMike Galbraith 	}
24627e4ff9e3SMike Galbraith 
246393f20c0fSAlexey Budankov 	if (rec->opts.nr_cblocks > nr_cblocks_max)
246493f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = nr_cblocks_max;
24655d7f4116SAlexey Budankov 	pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks);
2466d3d1af6fSAlexey Budankov 
24679d2ed645SAlexey Budankov 	pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]);
2468470530bbSAlexey Budankov 	pr_debug("mmap flush: %d\n", rec->opts.mmap_flush);
24699d2ed645SAlexey Budankov 
247051255a8aSAlexey Budankov 	if (rec->opts.comp_level > comp_level_max)
247151255a8aSAlexey Budankov 		rec->opts.comp_level = comp_level_max;
247251255a8aSAlexey Budankov 	pr_debug("comp level: %d\n", rec->opts.comp_level);
247351255a8aSAlexey Budankov 
2474d20deb64SArnaldo Carvalho de Melo 	err = __cmd_record(&record, argc, argv);
2475394c01edSAdrian Hunter out:
2476c12995a5SJiri Olsa 	evlist__delete(rec->evlist);
2477d65a458bSArnaldo Carvalho de Melo 	symbol__exit();
2478ef149c25SAdrian Hunter 	auxtrace_record__free(rec->itr);
247939d17dacSArnaldo Carvalho de Melo 	return err;
248086470930SIngo Molnar }
24812dd6d8a1SAdrian Hunter 
24822dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused)
24832dd6d8a1SAdrian Hunter {
2484dc0c6127SJiri Olsa 	struct record *rec = &record;
2485dc0c6127SJiri Olsa 
24865f9cf599SWang Nan 	if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
24875f9cf599SWang Nan 		trigger_hit(&auxtrace_snapshot_trigger);
24882dd6d8a1SAdrian Hunter 		auxtrace_record__snapshot_started = 1;
24895f9cf599SWang Nan 		if (auxtrace_record__snapshot_start(record.itr))
24905f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
24915f9cf599SWang Nan 	}
24923c1cb7e3SWang Nan 
2493dc0c6127SJiri Olsa 	if (switch_output_signal(rec))
24943c1cb7e3SWang Nan 		trigger_hit(&switch_output_trigger);
24952dd6d8a1SAdrian Hunter }
2496bfacbe3bSJiri Olsa 
2497bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused)
2498bfacbe3bSJiri Olsa {
2499bfacbe3bSJiri Olsa 	struct record *rec = &record;
2500bfacbe3bSJiri Olsa 
2501bfacbe3bSJiri Olsa 	if (switch_output_time(rec))
2502bfacbe3bSJiri Olsa 		trigger_hit(&switch_output_trigger);
2503bfacbe3bSJiri Olsa }
2504