xref: /openbmc/linux/tools/perf/builtin-record.c (revision ae4308927e488435073a6aaf601a842ff7e5738f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
286470930SIngo Molnar /*
386470930SIngo Molnar  * builtin-record.c
486470930SIngo Molnar  *
586470930SIngo Molnar  * Builtin record command: Record the profile of a workload
686470930SIngo Molnar  * (or a CPU, or a PID) into the perf.data output file - for
786470930SIngo Molnar  * later analysis via perf report.
886470930SIngo Molnar  */
986470930SIngo Molnar #include "builtin.h"
1086470930SIngo Molnar 
116122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h"
124b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1386470930SIngo Molnar #include "util/parse-events.h"
1441840d21STaeung Song #include "util/config.h"
1586470930SIngo Molnar 
168f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h"
17f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h"
187c6a1c65SPeter Zijlstra #include "util/header.h"
1966e274f3SFrederic Weisbecker #include "util/event.h"
20361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h"
2169aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h"
228f28827aSFrederic Weisbecker #include "util/debug.h"
23e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
24aeb00b1aSArnaldo Carvalho de Melo #include "util/target.h"
2594c744b6SArnaldo Carvalho de Melo #include "util/session.h"
2645694aa7SArnaldo Carvalho de Melo #include "util/tool.h"
278d06367fSArnaldo Carvalho de Melo #include "util/symbol.h"
28aeb00b1aSArnaldo Carvalho de Melo #include "util/record.h"
29a12b51c4SPaul Mackerras #include "util/cpumap.h"
30fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h"
31f5fc1412SJiri Olsa #include "util/data.h"
32bcc84ec6SStephane Eranian #include "util/perf_regs.h"
33ef149c25SAdrian Hunter #include "util/auxtrace.h"
3446bc29b9SAdrian Hunter #include "util/tsc.h"
35f00898f4SAndi Kleen #include "util/parse-branch-options.h"
36bcc84ec6SStephane Eranian #include "util/parse-regs-options.h"
3740c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
3871dc2326SWang Nan #include "util/llvm-utils.h"
398690a2a7SWang Nan #include "util/bpf-loader.h"
405f9cf599SWang Nan #include "util/trigger.h"
41a074865eSWang Nan #include "util/perf-hooks.h"
42f13de660SAlexey Budankov #include "util/cpu-set-sched.h"
43ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h"
44c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h"
4558db1d6eSArnaldo Carvalho de Melo #include "util/units.h"
467b612e29SSong Liu #include "util/bpf-event.h"
47d99c22eaSStephane Eranian #include "util/util.h"
48d8871ea7SWang Nan #include "asm/bug.h"
49c1a604dfSArnaldo Carvalho de Melo #include "perf.h"
507c6a1c65SPeter Zijlstra 
51a43783aeSArnaldo Carvalho de Melo #include <errno.h>
52fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
5367230479SArnaldo Carvalho de Melo #include <locale.h>
544208735dSArnaldo Carvalho de Melo #include <poll.h>
55d99c22eaSStephane Eranian #include <pthread.h>
5686470930SIngo Molnar #include <unistd.h>
5786470930SIngo Molnar #include <sched.h>
589607ad3aSArnaldo Carvalho de Melo #include <signal.h>
59a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h>
604208735dSArnaldo Carvalho de Melo #include <sys/wait.h>
61eeb399b5SAdrian Hunter #include <sys/types.h>
62eeb399b5SAdrian Hunter #include <sys/stat.h>
63eeb399b5SAdrian Hunter #include <fcntl.h>
646ef81c55SMamatha Inamdar #include <linux/err.h>
658520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
660693e680SArnaldo Carvalho de Melo #include <linux/time64.h>
67d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
688384a260SAlexey Budankov #include <linux/bitmap.h>
6978da39faSBernhard Rosenkraenzer 
701b43b704SJiri Olsa struct switch_output {
71dc0c6127SJiri Olsa 	bool		 enabled;
721b43b704SJiri Olsa 	bool		 signal;
73dc0c6127SJiri Olsa 	unsigned long	 size;
74bfacbe3bSJiri Olsa 	unsigned long	 time;
75cb4e1ebbSJiri Olsa 	const char	*str;
76cb4e1ebbSJiri Olsa 	bool		 set;
7703724b2eSAndi Kleen 	char		 **filenames;
7803724b2eSAndi Kleen 	int		 num_files;
7903724b2eSAndi Kleen 	int		 cur_file;
801b43b704SJiri Olsa };
811b43b704SJiri Olsa 
828c6f45a7SArnaldo Carvalho de Melo struct record {
8345694aa7SArnaldo Carvalho de Melo 	struct perf_tool	tool;
84b4006796SArnaldo Carvalho de Melo 	struct record_opts	opts;
85d20deb64SArnaldo Carvalho de Melo 	u64			bytes_written;
868ceb41d7SJiri Olsa 	struct perf_data	data;
87ef149c25SAdrian Hunter 	struct auxtrace_record	*itr;
8863503dbaSJiri Olsa 	struct evlist	*evlist;
89d20deb64SArnaldo Carvalho de Melo 	struct perf_session	*session;
90bc477d79SArnaldo Carvalho de Melo 	struct evlist		*sb_evlist;
91899e5ffbSArnaldo Carvalho de Melo 	pthread_t		thread_id;
92d20deb64SArnaldo Carvalho de Melo 	int			realtime_prio;
93899e5ffbSArnaldo Carvalho de Melo 	bool			switch_output_event_set;
94d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid;
95d2db9a98SWang Nan 	bool			no_buildid_set;
96d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid_cache;
97d2db9a98SWang Nan 	bool			no_buildid_cache_set;
986156681bSNamhyung Kim 	bool			buildid_all;
99ecfd7a9cSWang Nan 	bool			timestamp_filename;
10068588bafSJin Yao 	bool			timestamp_boundary;
1011b43b704SJiri Olsa 	struct switch_output	switch_output;
1029f065194SYang Shi 	unsigned long long	samples;
1038384a260SAlexey Budankov 	struct mmap_cpu_mask	affinity_mask;
1046d575816SJiwei Sun 	unsigned long		output_max_size;	/* = 0: unlimited */
1050f82ebc4SArnaldo Carvalho de Melo };
10686470930SIngo Molnar 
1076d575816SJiwei Sun static volatile int done;
1086d575816SJiwei Sun 
109dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started;
110dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
111dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger);
112dc0c6127SJiri Olsa 
1139d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = {
1149d2ed645SAlexey Budankov 	"SYS", "NODE", "CPU"
1159d2ed645SAlexey Budankov };
1169d2ed645SAlexey Budankov 
117dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec)
118dc0c6127SJiri Olsa {
119dc0c6127SJiri Olsa 	return rec->switch_output.signal &&
120dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
121dc0c6127SJiri Olsa }
122dc0c6127SJiri Olsa 
123dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec)
124dc0c6127SJiri Olsa {
125dc0c6127SJiri Olsa 	return rec->switch_output.size &&
126dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger) &&
127dc0c6127SJiri Olsa 	       (rec->bytes_written >= rec->switch_output.size);
128dc0c6127SJiri Olsa }
129dc0c6127SJiri Olsa 
130bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec)
131bfacbe3bSJiri Olsa {
132bfacbe3bSJiri Olsa 	return rec->switch_output.time &&
133bfacbe3bSJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
134bfacbe3bSJiri Olsa }
135bfacbe3bSJiri Olsa 
1366d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec)
1376d575816SJiwei Sun {
1386d575816SJiwei Sun 	return rec->output_max_size &&
1396d575816SJiwei Sun 	       (rec->bytes_written >= rec->output_max_size);
1406d575816SJiwei Sun }
1416d575816SJiwei Sun 
142a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused,
143ded2b8feSJiri Olsa 			 void *bf, size_t size)
144f5970550SPeter Zijlstra {
145ded2b8feSJiri Olsa 	struct perf_data_file *file = &rec->session->data->file;
146ded2b8feSJiri Olsa 
147ded2b8feSJiri Olsa 	if (perf_data_file__write(file, bf, size) < 0) {
1484f624685SAdrian Hunter 		pr_err("failed to write perf data, error: %m\n");
1498d3eca20SDavid Ahern 		return -1;
1508d3eca20SDavid Ahern 	}
151f5970550SPeter Zijlstra 
152cf8b2e69SArnaldo Carvalho de Melo 	rec->bytes_written += size;
153dc0c6127SJiri Olsa 
1546d575816SJiwei Sun 	if (record__output_max_size_exceeded(rec) && !done) {
1556d575816SJiwei Sun 		fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
1566d575816SJiwei Sun 				" stopping session ]\n",
1576d575816SJiwei Sun 				rec->bytes_written >> 10);
1586d575816SJiwei Sun 		done = 1;
1596d575816SJiwei Sun 	}
1606d575816SJiwei Sun 
161dc0c6127SJiri Olsa 	if (switch_output_size(rec))
162dc0c6127SJiri Olsa 		trigger_hit(&switch_output_trigger);
163dc0c6127SJiri Olsa 
1648d3eca20SDavid Ahern 	return 0;
165f5970550SPeter Zijlstra }
166f5970550SPeter Zijlstra 
167ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec);
168ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec);
1695d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size,
1705d7f4116SAlexey Budankov 			    void *src, size_t src_size);
1715d7f4116SAlexey Budankov 
172d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
173d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd,
174d3d1af6fSAlexey Budankov 		void *buf, size_t size, off_t off)
175d3d1af6fSAlexey Budankov {
176d3d1af6fSAlexey Budankov 	int rc;
177d3d1af6fSAlexey Budankov 
178d3d1af6fSAlexey Budankov 	cblock->aio_fildes = trace_fd;
179d3d1af6fSAlexey Budankov 	cblock->aio_buf    = buf;
180d3d1af6fSAlexey Budankov 	cblock->aio_nbytes = size;
181d3d1af6fSAlexey Budankov 	cblock->aio_offset = off;
182d3d1af6fSAlexey Budankov 	cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
183d3d1af6fSAlexey Budankov 
184d3d1af6fSAlexey Budankov 	do {
185d3d1af6fSAlexey Budankov 		rc = aio_write(cblock);
186d3d1af6fSAlexey Budankov 		if (rc == 0) {
187d3d1af6fSAlexey Budankov 			break;
188d3d1af6fSAlexey Budankov 		} else if (errno != EAGAIN) {
189d3d1af6fSAlexey Budankov 			cblock->aio_fildes = -1;
190d3d1af6fSAlexey Budankov 			pr_err("failed to queue perf data, error: %m\n");
191d3d1af6fSAlexey Budankov 			break;
192d3d1af6fSAlexey Budankov 		}
193d3d1af6fSAlexey Budankov 	} while (1);
194d3d1af6fSAlexey Budankov 
195d3d1af6fSAlexey Budankov 	return rc;
196d3d1af6fSAlexey Budankov }
197d3d1af6fSAlexey Budankov 
198a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock)
199d3d1af6fSAlexey Budankov {
200d3d1af6fSAlexey Budankov 	void *rem_buf;
201d3d1af6fSAlexey Budankov 	off_t rem_off;
202d3d1af6fSAlexey Budankov 	size_t rem_size;
203d3d1af6fSAlexey Budankov 	int rc, aio_errno;
204d3d1af6fSAlexey Budankov 	ssize_t aio_ret, written;
205d3d1af6fSAlexey Budankov 
206d3d1af6fSAlexey Budankov 	aio_errno = aio_error(cblock);
207d3d1af6fSAlexey Budankov 	if (aio_errno == EINPROGRESS)
208d3d1af6fSAlexey Budankov 		return 0;
209d3d1af6fSAlexey Budankov 
210d3d1af6fSAlexey Budankov 	written = aio_ret = aio_return(cblock);
211d3d1af6fSAlexey Budankov 	if (aio_ret < 0) {
212d3d1af6fSAlexey Budankov 		if (aio_errno != EINTR)
213d3d1af6fSAlexey Budankov 			pr_err("failed to write perf data, error: %m\n");
214d3d1af6fSAlexey Budankov 		written = 0;
215d3d1af6fSAlexey Budankov 	}
216d3d1af6fSAlexey Budankov 
217d3d1af6fSAlexey Budankov 	rem_size = cblock->aio_nbytes - written;
218d3d1af6fSAlexey Budankov 
219d3d1af6fSAlexey Budankov 	if (rem_size == 0) {
220d3d1af6fSAlexey Budankov 		cblock->aio_fildes = -1;
221d3d1af6fSAlexey Budankov 		/*
222ef781128SAlexey Budankov 		 * md->refcount is incremented in record__aio_pushfn() for
223ef781128SAlexey Budankov 		 * every aio write request started in record__aio_push() so
224ef781128SAlexey Budankov 		 * decrement it because the request is now complete.
225d3d1af6fSAlexey Budankov 		 */
22680e53d11SJiri Olsa 		perf_mmap__put(&md->core);
227d3d1af6fSAlexey Budankov 		rc = 1;
228d3d1af6fSAlexey Budankov 	} else {
229d3d1af6fSAlexey Budankov 		/*
230d3d1af6fSAlexey Budankov 		 * aio write request may require restart with the
231d3d1af6fSAlexey Budankov 		 * reminder if the kernel didn't write whole
232d3d1af6fSAlexey Budankov 		 * chunk at once.
233d3d1af6fSAlexey Budankov 		 */
234d3d1af6fSAlexey Budankov 		rem_off = cblock->aio_offset + written;
235d3d1af6fSAlexey Budankov 		rem_buf = (void *)(cblock->aio_buf + written);
236d3d1af6fSAlexey Budankov 		record__aio_write(cblock, cblock->aio_fildes,
237d3d1af6fSAlexey Budankov 				rem_buf, rem_size, rem_off);
238d3d1af6fSAlexey Budankov 		rc = 0;
239d3d1af6fSAlexey Budankov 	}
240d3d1af6fSAlexey Budankov 
241d3d1af6fSAlexey Budankov 	return rc;
242d3d1af6fSAlexey Budankov }
243d3d1af6fSAlexey Budankov 
244a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all)
245d3d1af6fSAlexey Budankov {
24693f20c0fSAlexey Budankov 	struct aiocb **aiocb = md->aio.aiocb;
24793f20c0fSAlexey Budankov 	struct aiocb *cblocks = md->aio.cblocks;
248d3d1af6fSAlexey Budankov 	struct timespec timeout = { 0, 1000 * 1000  * 1 }; /* 1ms */
24993f20c0fSAlexey Budankov 	int i, do_suspend;
250d3d1af6fSAlexey Budankov 
251d3d1af6fSAlexey Budankov 	do {
25293f20c0fSAlexey Budankov 		do_suspend = 0;
25393f20c0fSAlexey Budankov 		for (i = 0; i < md->aio.nr_cblocks; ++i) {
25493f20c0fSAlexey Budankov 			if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
25593f20c0fSAlexey Budankov 				if (sync_all)
25693f20c0fSAlexey Budankov 					aiocb[i] = NULL;
25793f20c0fSAlexey Budankov 				else
25893f20c0fSAlexey Budankov 					return i;
25993f20c0fSAlexey Budankov 			} else {
26093f20c0fSAlexey Budankov 				/*
26193f20c0fSAlexey Budankov 				 * Started aio write is not complete yet
26293f20c0fSAlexey Budankov 				 * so it has to be waited before the
26393f20c0fSAlexey Budankov 				 * next allocation.
26493f20c0fSAlexey Budankov 				 */
26593f20c0fSAlexey Budankov 				aiocb[i] = &cblocks[i];
26693f20c0fSAlexey Budankov 				do_suspend = 1;
26793f20c0fSAlexey Budankov 			}
26893f20c0fSAlexey Budankov 		}
26993f20c0fSAlexey Budankov 		if (!do_suspend)
27093f20c0fSAlexey Budankov 			return -1;
271d3d1af6fSAlexey Budankov 
27293f20c0fSAlexey Budankov 		while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) {
273d3d1af6fSAlexey Budankov 			if (!(errno == EAGAIN || errno == EINTR))
274d3d1af6fSAlexey Budankov 				pr_err("failed to sync perf data, error: %m\n");
275d3d1af6fSAlexey Budankov 		}
276d3d1af6fSAlexey Budankov 	} while (1);
277d3d1af6fSAlexey Budankov }
278d3d1af6fSAlexey Budankov 
279ef781128SAlexey Budankov struct record_aio {
280ef781128SAlexey Budankov 	struct record	*rec;
281ef781128SAlexey Budankov 	void		*data;
282ef781128SAlexey Budankov 	size_t		size;
283ef781128SAlexey Budankov };
284ef781128SAlexey Budankov 
285a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size)
286d3d1af6fSAlexey Budankov {
287ef781128SAlexey Budankov 	struct record_aio *aio = to;
288ef781128SAlexey Budankov 
289ef781128SAlexey Budankov 	/*
290547740f7SJiri Olsa 	 * map->core.base data pointed by buf is copied into free map->aio.data[] buffer
291ef781128SAlexey Budankov 	 * to release space in the kernel buffer as fast as possible, calling
292ef781128SAlexey Budankov 	 * perf_mmap__consume() from perf_mmap__push() function.
293ef781128SAlexey Budankov 	 *
294ef781128SAlexey Budankov 	 * That lets the kernel to proceed with storing more profiling data into
295ef781128SAlexey Budankov 	 * the kernel buffer earlier than other per-cpu kernel buffers are handled.
296ef781128SAlexey Budankov 	 *
297ef781128SAlexey Budankov 	 * Coping can be done in two steps in case the chunk of profiling data
298ef781128SAlexey Budankov 	 * crosses the upper bound of the kernel buffer. In this case we first move
299ef781128SAlexey Budankov 	 * part of data from map->start till the upper bound and then the reminder
300ef781128SAlexey Budankov 	 * from the beginning of the kernel buffer till the end of the data chunk.
301ef781128SAlexey Budankov 	 */
302ef781128SAlexey Budankov 
303ef781128SAlexey Budankov 	if (record__comp_enabled(aio->rec)) {
304ef781128SAlexey Budankov 		size = zstd_compress(aio->rec->session, aio->data + aio->size,
305bf59b305SJiri Olsa 				     mmap__mmap_len(map) - aio->size,
306ef781128SAlexey Budankov 				     buf, size);
307ef781128SAlexey Budankov 	} else {
308ef781128SAlexey Budankov 		memcpy(aio->data + aio->size, buf, size);
309ef781128SAlexey Budankov 	}
310ef781128SAlexey Budankov 
311ef781128SAlexey Budankov 	if (!aio->size) {
312ef781128SAlexey Budankov 		/*
313ef781128SAlexey Budankov 		 * Increment map->refcount to guard map->aio.data[] buffer
314ef781128SAlexey Budankov 		 * from premature deallocation because map object can be
315ef781128SAlexey Budankov 		 * released earlier than aio write request started on
316ef781128SAlexey Budankov 		 * map->aio.data[] buffer is complete.
317ef781128SAlexey Budankov 		 *
318ef781128SAlexey Budankov 		 * perf_mmap__put() is done at record__aio_complete()
319ef781128SAlexey Budankov 		 * after started aio request completion or at record__aio_push()
320ef781128SAlexey Budankov 		 * if the request failed to start.
321ef781128SAlexey Budankov 		 */
322e75710f0SJiri Olsa 		perf_mmap__get(&map->core);
323ef781128SAlexey Budankov 	}
324ef781128SAlexey Budankov 
325ef781128SAlexey Budankov 	aio->size += size;
326ef781128SAlexey Budankov 
327ef781128SAlexey Budankov 	return size;
328ef781128SAlexey Budankov }
329ef781128SAlexey Budankov 
330a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off)
331ef781128SAlexey Budankov {
332ef781128SAlexey Budankov 	int ret, idx;
333ef781128SAlexey Budankov 	int trace_fd = rec->session->data->file.fd;
334ef781128SAlexey Budankov 	struct record_aio aio = { .rec = rec, .size = 0 };
335ef781128SAlexey Budankov 
336ef781128SAlexey Budankov 	/*
337ef781128SAlexey Budankov 	 * Call record__aio_sync() to wait till map->aio.data[] buffer
338ef781128SAlexey Budankov 	 * becomes available after previous aio write operation.
339ef781128SAlexey Budankov 	 */
340ef781128SAlexey Budankov 
341ef781128SAlexey Budankov 	idx = record__aio_sync(map, false);
342ef781128SAlexey Budankov 	aio.data = map->aio.data[idx];
343ef781128SAlexey Budankov 	ret = perf_mmap__push(map, &aio, record__aio_pushfn);
344ef781128SAlexey Budankov 	if (ret != 0) /* ret > 0 - no data, ret < 0 - error */
345ef781128SAlexey Budankov 		return ret;
346d3d1af6fSAlexey Budankov 
347d3d1af6fSAlexey Budankov 	rec->samples++;
348ef781128SAlexey Budankov 	ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off);
349d3d1af6fSAlexey Budankov 	if (!ret) {
350ef781128SAlexey Budankov 		*off += aio.size;
351ef781128SAlexey Budankov 		rec->bytes_written += aio.size;
352d3d1af6fSAlexey Budankov 		if (switch_output_size(rec))
353d3d1af6fSAlexey Budankov 			trigger_hit(&switch_output_trigger);
354ef781128SAlexey Budankov 	} else {
355ef781128SAlexey Budankov 		/*
356ef781128SAlexey Budankov 		 * Decrement map->refcount incremented in record__aio_pushfn()
357ef781128SAlexey Budankov 		 * back if record__aio_write() operation failed to start, otherwise
358ef781128SAlexey Budankov 		 * map->refcount is decremented in record__aio_complete() after
359ef781128SAlexey Budankov 		 * aio write operation finishes successfully.
360ef781128SAlexey Budankov 		 */
36180e53d11SJiri Olsa 		perf_mmap__put(&map->core);
362d3d1af6fSAlexey Budankov 	}
363d3d1af6fSAlexey Budankov 
364d3d1af6fSAlexey Budankov 	return ret;
365d3d1af6fSAlexey Budankov }
366d3d1af6fSAlexey Budankov 
367d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd)
368d3d1af6fSAlexey Budankov {
369d3d1af6fSAlexey Budankov 	return lseek(trace_fd, 0, SEEK_CUR);
370d3d1af6fSAlexey Budankov }
371d3d1af6fSAlexey Budankov 
372d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos)
373d3d1af6fSAlexey Budankov {
374d3d1af6fSAlexey Budankov 	lseek(trace_fd, pos, SEEK_SET);
375d3d1af6fSAlexey Budankov }
376d3d1af6fSAlexey Budankov 
377d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec)
378d3d1af6fSAlexey Budankov {
379d3d1af6fSAlexey Budankov 	int i;
38063503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
381a5830532SJiri Olsa 	struct mmap *maps = evlist->mmap;
382d3d1af6fSAlexey Budankov 
383ef781128SAlexey Budankov 	if (!record__aio_enabled(rec))
384d3d1af6fSAlexey Budankov 		return;
385d3d1af6fSAlexey Budankov 
386c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
387a5830532SJiri Olsa 		struct mmap *map = &maps[i];
388d3d1af6fSAlexey Budankov 
389547740f7SJiri Olsa 		if (map->core.base)
39093f20c0fSAlexey Budankov 			record__aio_sync(map, true);
391d3d1af6fSAlexey Budankov 	}
392d3d1af6fSAlexey Budankov }
393d3d1af6fSAlexey Budankov 
394d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1;
39593f20c0fSAlexey Budankov static int nr_cblocks_max = 4;
396d3d1af6fSAlexey Budankov 
397d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt,
39893f20c0fSAlexey Budankov 			     const char *str,
399d3d1af6fSAlexey Budankov 			     int unset)
400d3d1af6fSAlexey Budankov {
401d3d1af6fSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
402d3d1af6fSAlexey Budankov 
40393f20c0fSAlexey Budankov 	if (unset) {
404d3d1af6fSAlexey Budankov 		opts->nr_cblocks = 0;
40593f20c0fSAlexey Budankov 	} else {
40693f20c0fSAlexey Budankov 		if (str)
40793f20c0fSAlexey Budankov 			opts->nr_cblocks = strtol(str, NULL, 0);
40893f20c0fSAlexey Budankov 		if (!opts->nr_cblocks)
409d3d1af6fSAlexey Budankov 			opts->nr_cblocks = nr_cblocks_default;
41093f20c0fSAlexey Budankov 	}
411d3d1af6fSAlexey Budankov 
412d3d1af6fSAlexey Budankov 	return 0;
413d3d1af6fSAlexey Budankov }
414d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */
41593f20c0fSAlexey Budankov static int nr_cblocks_max = 0;
41693f20c0fSAlexey Budankov 
417a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused,
418ef781128SAlexey Budankov 			    off_t *off __maybe_unused)
419d3d1af6fSAlexey Budankov {
420d3d1af6fSAlexey Budankov 	return -1;
421d3d1af6fSAlexey Budankov }
422d3d1af6fSAlexey Budankov 
423d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused)
424d3d1af6fSAlexey Budankov {
425d3d1af6fSAlexey Budankov 	return -1;
426d3d1af6fSAlexey Budankov }
427d3d1af6fSAlexey Budankov 
428d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused)
429d3d1af6fSAlexey Budankov {
430d3d1af6fSAlexey Budankov }
431d3d1af6fSAlexey Budankov 
432d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused)
433d3d1af6fSAlexey Budankov {
434d3d1af6fSAlexey Budankov }
435d3d1af6fSAlexey Budankov #endif
436d3d1af6fSAlexey Budankov 
437d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec)
438d3d1af6fSAlexey Budankov {
439d3d1af6fSAlexey Budankov 	return rec->opts.nr_cblocks > 0;
440d3d1af6fSAlexey Budankov }
441d3d1af6fSAlexey Budankov 
442470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1
443470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt,
444470530bbSAlexey Budankov 				    const char *str,
445470530bbSAlexey Budankov 				    int unset)
446470530bbSAlexey Budankov {
447470530bbSAlexey Budankov 	int flush_max;
448470530bbSAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
449470530bbSAlexey Budankov 	static struct parse_tag tags[] = {
450470530bbSAlexey Budankov 			{ .tag  = 'B', .mult = 1       },
451470530bbSAlexey Budankov 			{ .tag  = 'K', .mult = 1 << 10 },
452470530bbSAlexey Budankov 			{ .tag  = 'M', .mult = 1 << 20 },
453470530bbSAlexey Budankov 			{ .tag  = 'G', .mult = 1 << 30 },
454470530bbSAlexey Budankov 			{ .tag  = 0 },
455470530bbSAlexey Budankov 	};
456470530bbSAlexey Budankov 
457470530bbSAlexey Budankov 	if (unset)
458470530bbSAlexey Budankov 		return 0;
459470530bbSAlexey Budankov 
460470530bbSAlexey Budankov 	if (str) {
461470530bbSAlexey Budankov 		opts->mmap_flush = parse_tag_value(str, tags);
462470530bbSAlexey Budankov 		if (opts->mmap_flush == (int)-1)
463470530bbSAlexey Budankov 			opts->mmap_flush = strtol(str, NULL, 0);
464470530bbSAlexey Budankov 	}
465470530bbSAlexey Budankov 
466470530bbSAlexey Budankov 	if (!opts->mmap_flush)
467470530bbSAlexey Budankov 		opts->mmap_flush = MMAP_FLUSH_DEFAULT;
468470530bbSAlexey Budankov 
4699521b5f2SJiri Olsa 	flush_max = evlist__mmap_size(opts->mmap_pages);
470470530bbSAlexey Budankov 	flush_max /= 4;
471470530bbSAlexey Budankov 	if (opts->mmap_flush > flush_max)
472470530bbSAlexey Budankov 		opts->mmap_flush = flush_max;
473470530bbSAlexey Budankov 
474470530bbSAlexey Budankov 	return 0;
475470530bbSAlexey Budankov }
476470530bbSAlexey Budankov 
477504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
478504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1;
479504c1ad1SAlexey Budankov 
480504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset)
481504c1ad1SAlexey Budankov {
482504c1ad1SAlexey Budankov 	struct record_opts *opts = opt->value;
483504c1ad1SAlexey Budankov 
484504c1ad1SAlexey Budankov 	if (unset) {
485504c1ad1SAlexey Budankov 		opts->comp_level = 0;
486504c1ad1SAlexey Budankov 	} else {
487504c1ad1SAlexey Budankov 		if (str)
488504c1ad1SAlexey Budankov 			opts->comp_level = strtol(str, NULL, 0);
489504c1ad1SAlexey Budankov 		if (!opts->comp_level)
490504c1ad1SAlexey Budankov 			opts->comp_level = comp_level_default;
491504c1ad1SAlexey Budankov 	}
492504c1ad1SAlexey Budankov 
493504c1ad1SAlexey Budankov 	return 0;
494504c1ad1SAlexey Budankov }
495504c1ad1SAlexey Budankov #endif
49651255a8aSAlexey Budankov static unsigned int comp_level_max = 22;
49751255a8aSAlexey Budankov 
49842e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec)
49942e1fd80SAlexey Budankov {
50042e1fd80SAlexey Budankov 	return rec->opts.comp_level > 0;
50142e1fd80SAlexey Budankov }
50242e1fd80SAlexey Budankov 
50345694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool,
504d20deb64SArnaldo Carvalho de Melo 				     union perf_event *event,
5051d037ca1SIrina Tirdea 				     struct perf_sample *sample __maybe_unused,
5061d037ca1SIrina Tirdea 				     struct machine *machine __maybe_unused)
507234fbbf5SArnaldo Carvalho de Melo {
5088c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = container_of(tool, struct record, tool);
509ded2b8feSJiri Olsa 	return record__write(rec, NULL, event, event->header.size);
510234fbbf5SArnaldo Carvalho de Melo }
511234fbbf5SArnaldo Carvalho de Melo 
512d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool,
513d99c22eaSStephane Eranian 				     union perf_event *event,
514d99c22eaSStephane Eranian 				     struct perf_sample *sample __maybe_unused,
515d99c22eaSStephane Eranian 				     struct machine *machine __maybe_unused)
516d99c22eaSStephane Eranian {
517d99c22eaSStephane Eranian 	static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER;
518d99c22eaSStephane Eranian 	int ret;
519d99c22eaSStephane Eranian 
520d99c22eaSStephane Eranian 	pthread_mutex_lock(&synth_lock);
521d99c22eaSStephane Eranian 	ret = process_synthesized_event(tool, event, sample, machine);
522d99c22eaSStephane Eranian 	pthread_mutex_unlock(&synth_lock);
523d99c22eaSStephane Eranian 	return ret;
524d99c22eaSStephane Eranian }
525d99c22eaSStephane Eranian 
526a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
527d37f1586SArnaldo Carvalho de Melo {
528d37f1586SArnaldo Carvalho de Melo 	struct record *rec = to;
529d37f1586SArnaldo Carvalho de Melo 
5305d7f4116SAlexey Budankov 	if (record__comp_enabled(rec)) {
531bf59b305SJiri Olsa 		size = zstd_compress(rec->session, map->data, mmap__mmap_len(map), bf, size);
5325d7f4116SAlexey Budankov 		bf   = map->data;
5335d7f4116SAlexey Budankov 	}
5345d7f4116SAlexey Budankov 
535d37f1586SArnaldo Carvalho de Melo 	rec->samples++;
536ded2b8feSJiri Olsa 	return record__write(rec, map, bf, size);
537d37f1586SArnaldo Carvalho de Melo }
538d37f1586SArnaldo Carvalho de Melo 
5392dd6d8a1SAdrian Hunter static volatile int signr = -1;
5402dd6d8a1SAdrian Hunter static volatile int child_finished;
541c0bdc1c4SWang Nan 
5422dd6d8a1SAdrian Hunter static void sig_handler(int sig)
5432dd6d8a1SAdrian Hunter {
5442dd6d8a1SAdrian Hunter 	if (sig == SIGCHLD)
5452dd6d8a1SAdrian Hunter 		child_finished = 1;
5462dd6d8a1SAdrian Hunter 	else
5472dd6d8a1SAdrian Hunter 		signr = sig;
5482dd6d8a1SAdrian Hunter 
5492dd6d8a1SAdrian Hunter 	done = 1;
5502dd6d8a1SAdrian Hunter }
5512dd6d8a1SAdrian Hunter 
552a074865eSWang Nan static void sigsegv_handler(int sig)
553a074865eSWang Nan {
554a074865eSWang Nan 	perf_hooks__recover();
555a074865eSWang Nan 	sighandler_dump_stack(sig);
556a074865eSWang Nan }
557a074865eSWang Nan 
5582dd6d8a1SAdrian Hunter static void record__sig_exit(void)
5592dd6d8a1SAdrian Hunter {
5602dd6d8a1SAdrian Hunter 	if (signr == -1)
5612dd6d8a1SAdrian Hunter 		return;
5622dd6d8a1SAdrian Hunter 
5632dd6d8a1SAdrian Hunter 	signal(signr, SIG_DFL);
5642dd6d8a1SAdrian Hunter 	raise(signr);
5652dd6d8a1SAdrian Hunter }
5662dd6d8a1SAdrian Hunter 
567e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT
568e31f0d01SAdrian Hunter 
569ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool,
570a5830532SJiri Olsa 				    struct mmap *map,
571ef149c25SAdrian Hunter 				    union perf_event *event, void *data1,
572ef149c25SAdrian Hunter 				    size_t len1, void *data2, size_t len2)
573ef149c25SAdrian Hunter {
574ef149c25SAdrian Hunter 	struct record *rec = container_of(tool, struct record, tool);
5758ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
576ef149c25SAdrian Hunter 	size_t padding;
577ef149c25SAdrian Hunter 	u8 pad[8] = {0};
578ef149c25SAdrian Hunter 
57946e201efSAdrian Hunter 	if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) {
58099fa2984SAdrian Hunter 		off_t file_offset;
5818ceb41d7SJiri Olsa 		int fd = perf_data__fd(data);
58299fa2984SAdrian Hunter 		int err;
58399fa2984SAdrian Hunter 
58499fa2984SAdrian Hunter 		file_offset = lseek(fd, 0, SEEK_CUR);
58599fa2984SAdrian Hunter 		if (file_offset == -1)
58699fa2984SAdrian Hunter 			return -1;
58799fa2984SAdrian Hunter 		err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
58899fa2984SAdrian Hunter 						     event, file_offset);
58999fa2984SAdrian Hunter 		if (err)
59099fa2984SAdrian Hunter 			return err;
59199fa2984SAdrian Hunter 	}
59299fa2984SAdrian Hunter 
593ef149c25SAdrian Hunter 	/* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
594ef149c25SAdrian Hunter 	padding = (len1 + len2) & 7;
595ef149c25SAdrian Hunter 	if (padding)
596ef149c25SAdrian Hunter 		padding = 8 - padding;
597ef149c25SAdrian Hunter 
598ded2b8feSJiri Olsa 	record__write(rec, map, event, event->header.size);
599ded2b8feSJiri Olsa 	record__write(rec, map, data1, len1);
600ef149c25SAdrian Hunter 	if (len2)
601ded2b8feSJiri Olsa 		record__write(rec, map, data2, len2);
602ded2b8feSJiri Olsa 	record__write(rec, map, &pad, padding);
603ef149c25SAdrian Hunter 
604ef149c25SAdrian Hunter 	return 0;
605ef149c25SAdrian Hunter }
606ef149c25SAdrian Hunter 
607ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec,
608a5830532SJiri Olsa 				      struct mmap *map)
609ef149c25SAdrian Hunter {
610ef149c25SAdrian Hunter 	int ret;
611ef149c25SAdrian Hunter 
612e035f4caSJiri Olsa 	ret = auxtrace_mmap__read(map, rec->itr, &rec->tool,
613ef149c25SAdrian Hunter 				  record__process_auxtrace);
614ef149c25SAdrian Hunter 	if (ret < 0)
615ef149c25SAdrian Hunter 		return ret;
616ef149c25SAdrian Hunter 
617ef149c25SAdrian Hunter 	if (ret)
618ef149c25SAdrian Hunter 		rec->samples++;
619ef149c25SAdrian Hunter 
620ef149c25SAdrian Hunter 	return 0;
621ef149c25SAdrian Hunter }
622ef149c25SAdrian Hunter 
6232dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec,
624a5830532SJiri Olsa 					       struct mmap *map)
6252dd6d8a1SAdrian Hunter {
6262dd6d8a1SAdrian Hunter 	int ret;
6272dd6d8a1SAdrian Hunter 
628e035f4caSJiri Olsa 	ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool,
6292dd6d8a1SAdrian Hunter 					   record__process_auxtrace,
6302dd6d8a1SAdrian Hunter 					   rec->opts.auxtrace_snapshot_size);
6312dd6d8a1SAdrian Hunter 	if (ret < 0)
6322dd6d8a1SAdrian Hunter 		return ret;
6332dd6d8a1SAdrian Hunter 
6342dd6d8a1SAdrian Hunter 	if (ret)
6352dd6d8a1SAdrian Hunter 		rec->samples++;
6362dd6d8a1SAdrian Hunter 
6372dd6d8a1SAdrian Hunter 	return 0;
6382dd6d8a1SAdrian Hunter }
6392dd6d8a1SAdrian Hunter 
6402dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec)
6412dd6d8a1SAdrian Hunter {
6422dd6d8a1SAdrian Hunter 	int i;
6432dd6d8a1SAdrian Hunter 	int rc = 0;
6442dd6d8a1SAdrian Hunter 
645c976ee11SJiri Olsa 	for (i = 0; i < rec->evlist->core.nr_mmaps; i++) {
646a5830532SJiri Olsa 		struct mmap *map = &rec->evlist->mmap[i];
6472dd6d8a1SAdrian Hunter 
648e035f4caSJiri Olsa 		if (!map->auxtrace_mmap.base)
6492dd6d8a1SAdrian Hunter 			continue;
6502dd6d8a1SAdrian Hunter 
651e035f4caSJiri Olsa 		if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) {
6522dd6d8a1SAdrian Hunter 			rc = -1;
6532dd6d8a1SAdrian Hunter 			goto out;
6542dd6d8a1SAdrian Hunter 		}
6552dd6d8a1SAdrian Hunter 	}
6562dd6d8a1SAdrian Hunter out:
6572dd6d8a1SAdrian Hunter 	return rc;
6582dd6d8a1SAdrian Hunter }
6592dd6d8a1SAdrian Hunter 
660ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit)
6612dd6d8a1SAdrian Hunter {
6622dd6d8a1SAdrian Hunter 	pr_debug("Recording AUX area tracing snapshot\n");
6632dd6d8a1SAdrian Hunter 	if (record__auxtrace_read_snapshot_all(rec) < 0) {
6645f9cf599SWang Nan 		trigger_error(&auxtrace_snapshot_trigger);
6652dd6d8a1SAdrian Hunter 	} else {
666ce7b0e42SAlexander Shishkin 		if (auxtrace_record__snapshot_finish(rec->itr, on_exit))
6675f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
6685f9cf599SWang Nan 		else
6695f9cf599SWang Nan 			trigger_ready(&auxtrace_snapshot_trigger);
6702dd6d8a1SAdrian Hunter 	}
6712dd6d8a1SAdrian Hunter }
6722dd6d8a1SAdrian Hunter 
673ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec)
674ce7b0e42SAlexander Shishkin {
675ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
676ce7b0e42SAlexander Shishkin 		return 0;
677ce7b0e42SAlexander Shishkin 
678ce7b0e42SAlexander Shishkin 	if (!auxtrace_record__snapshot_started &&
679ce7b0e42SAlexander Shishkin 	    auxtrace_record__snapshot_start(rec->itr))
680ce7b0e42SAlexander Shishkin 		return -1;
681ce7b0e42SAlexander Shishkin 
682ce7b0e42SAlexander Shishkin 	record__read_auxtrace_snapshot(rec, true);
683ce7b0e42SAlexander Shishkin 	if (trigger_is_error(&auxtrace_snapshot_trigger))
684ce7b0e42SAlexander Shishkin 		return -1;
685ce7b0e42SAlexander Shishkin 
686ce7b0e42SAlexander Shishkin 	return 0;
687ce7b0e42SAlexander Shishkin }
688ce7b0e42SAlexander Shishkin 
6894b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec)
6904b5ea3bdSAdrian Hunter {
6914b5ea3bdSAdrian Hunter 	int err;
6924b5ea3bdSAdrian Hunter 
6934b5ea3bdSAdrian Hunter 	if (!rec->itr) {
6944b5ea3bdSAdrian Hunter 		rec->itr = auxtrace_record__init(rec->evlist, &err);
6954b5ea3bdSAdrian Hunter 		if (err)
6964b5ea3bdSAdrian Hunter 			return err;
6974b5ea3bdSAdrian Hunter 	}
6984b5ea3bdSAdrian Hunter 
6994b5ea3bdSAdrian Hunter 	err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
7004b5ea3bdSAdrian Hunter 					      rec->opts.auxtrace_snapshot_opts);
7014b5ea3bdSAdrian Hunter 	if (err)
7024b5ea3bdSAdrian Hunter 		return err;
7034b5ea3bdSAdrian Hunter 
704c0a6de06SAdrian Hunter 	err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts,
705c0a6de06SAdrian Hunter 					    rec->opts.auxtrace_sample_opts);
706c0a6de06SAdrian Hunter 	if (err)
707c0a6de06SAdrian Hunter 		return err;
708c0a6de06SAdrian Hunter 
7094b5ea3bdSAdrian Hunter 	return auxtrace_parse_filters(rec->evlist);
7104b5ea3bdSAdrian Hunter }
7114b5ea3bdSAdrian Hunter 
712e31f0d01SAdrian Hunter #else
713e31f0d01SAdrian Hunter 
714e31f0d01SAdrian Hunter static inline
715e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
716a5830532SJiri Olsa 			       struct mmap *map __maybe_unused)
717e31f0d01SAdrian Hunter {
718e31f0d01SAdrian Hunter 	return 0;
719e31f0d01SAdrian Hunter }
720e31f0d01SAdrian Hunter 
7212dd6d8a1SAdrian Hunter static inline
722ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused,
723ce7b0e42SAlexander Shishkin 				    bool on_exit __maybe_unused)
7242dd6d8a1SAdrian Hunter {
7252dd6d8a1SAdrian Hunter }
7262dd6d8a1SAdrian Hunter 
7272dd6d8a1SAdrian Hunter static inline
7282dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
7292dd6d8a1SAdrian Hunter {
7302dd6d8a1SAdrian Hunter 	return 0;
7312dd6d8a1SAdrian Hunter }
7322dd6d8a1SAdrian Hunter 
733ce7b0e42SAlexander Shishkin static inline
734ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused)
735ce7b0e42SAlexander Shishkin {
736ce7b0e42SAlexander Shishkin 	return 0;
737ce7b0e42SAlexander Shishkin }
738ce7b0e42SAlexander Shishkin 
7394b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused)
7404b5ea3bdSAdrian Hunter {
7414b5ea3bdSAdrian Hunter 	return 0;
7424b5ea3bdSAdrian Hunter }
7434b5ea3bdSAdrian Hunter 
744e31f0d01SAdrian Hunter #endif
745e31f0d01SAdrian Hunter 
746eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine)
747eeb399b5SAdrian Hunter {
748eeb399b5SAdrian Hunter 	char kcore[PATH_MAX];
749eeb399b5SAdrian Hunter 	int fd;
750eeb399b5SAdrian Hunter 
751eeb399b5SAdrian Hunter 	scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir);
752eeb399b5SAdrian Hunter 
753eeb399b5SAdrian Hunter 	fd = open(kcore, O_RDONLY);
754eeb399b5SAdrian Hunter 	if (fd < 0)
755eeb399b5SAdrian Hunter 		return false;
756eeb399b5SAdrian Hunter 
757eeb399b5SAdrian Hunter 	close(fd);
758eeb399b5SAdrian Hunter 
759eeb399b5SAdrian Hunter 	return true;
760eeb399b5SAdrian Hunter }
761eeb399b5SAdrian Hunter 
762eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data)
763eeb399b5SAdrian Hunter {
764eeb399b5SAdrian Hunter 	char from_dir[PATH_MAX];
765eeb399b5SAdrian Hunter 	char kcore_dir[PATH_MAX];
766eeb399b5SAdrian Hunter 	int ret;
767eeb399b5SAdrian Hunter 
768eeb399b5SAdrian Hunter 	snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir);
769eeb399b5SAdrian Hunter 
770eeb399b5SAdrian Hunter 	ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir));
771eeb399b5SAdrian Hunter 	if (ret)
772eeb399b5SAdrian Hunter 		return ret;
773eeb399b5SAdrian Hunter 
774eeb399b5SAdrian Hunter 	return kcore_copy(from_dir, kcore_dir);
775eeb399b5SAdrian Hunter }
776eeb399b5SAdrian Hunter 
777cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec,
77863503dbaSJiri Olsa 			       struct evlist *evlist)
779cda57a8cSWang Nan {
780cda57a8cSWang Nan 	struct record_opts *opts = &rec->opts;
781c0a6de06SAdrian Hunter 	bool auxtrace_overwrite = opts->auxtrace_snapshot_mode ||
782c0a6de06SAdrian Hunter 				  opts->auxtrace_sample_mode;
783cda57a8cSWang Nan 	char msg[512];
784cda57a8cSWang Nan 
785f13de660SAlexey Budankov 	if (opts->affinity != PERF_AFFINITY_SYS)
786f13de660SAlexey Budankov 		cpu__setup_cpunode_map();
787f13de660SAlexey Budankov 
7889521b5f2SJiri Olsa 	if (evlist__mmap_ex(evlist, opts->mmap_pages,
789cda57a8cSWang Nan 				 opts->auxtrace_mmap_pages,
790c0a6de06SAdrian Hunter 				 auxtrace_overwrite,
791470530bbSAlexey Budankov 				 opts->nr_cblocks, opts->affinity,
79251255a8aSAlexey Budankov 				 opts->mmap_flush, opts->comp_level) < 0) {
793cda57a8cSWang Nan 		if (errno == EPERM) {
794cda57a8cSWang Nan 			pr_err("Permission error mapping pages.\n"
795cda57a8cSWang Nan 			       "Consider increasing "
796cda57a8cSWang Nan 			       "/proc/sys/kernel/perf_event_mlock_kb,\n"
797cda57a8cSWang Nan 			       "or try again with a smaller value of -m/--mmap_pages.\n"
798cda57a8cSWang Nan 			       "(current value: %u,%u)\n",
799cda57a8cSWang Nan 			       opts->mmap_pages, opts->auxtrace_mmap_pages);
800cda57a8cSWang Nan 			return -errno;
801cda57a8cSWang Nan 		} else {
802cda57a8cSWang Nan 			pr_err("failed to mmap with %d (%s)\n", errno,
803c8b5f2c9SArnaldo Carvalho de Melo 				str_error_r(errno, msg, sizeof(msg)));
804cda57a8cSWang Nan 			if (errno)
805cda57a8cSWang Nan 				return -errno;
806cda57a8cSWang Nan 			else
807cda57a8cSWang Nan 				return -EINVAL;
808cda57a8cSWang Nan 		}
809cda57a8cSWang Nan 	}
810cda57a8cSWang Nan 	return 0;
811cda57a8cSWang Nan }
812cda57a8cSWang Nan 
813cda57a8cSWang Nan static int record__mmap(struct record *rec)
814cda57a8cSWang Nan {
815cda57a8cSWang Nan 	return record__mmap_evlist(rec, rec->evlist);
816cda57a8cSWang Nan }
817cda57a8cSWang Nan 
8188c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec)
819dd7927f4SArnaldo Carvalho de Melo {
820d6195a6aSArnaldo Carvalho de Melo 	char msg[BUFSIZ];
82132dcd021SJiri Olsa 	struct evsel *pos;
82263503dbaSJiri Olsa 	struct evlist *evlist = rec->evlist;
823d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session = rec->session;
824b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
8258d3eca20SDavid Ahern 	int rc = 0;
826dd7927f4SArnaldo Carvalho de Melo 
827d3dbf43cSArnaldo Carvalho de Melo 	/*
828d3dbf43cSArnaldo Carvalho de Melo 	 * For initial_delay we need to add a dummy event so that we can track
829d3dbf43cSArnaldo Carvalho de Melo 	 * PERF_RECORD_MMAP while we wait for the initial delay to enable the
830d3dbf43cSArnaldo Carvalho de Melo 	 * real events, the ones asked by the user.
831d3dbf43cSArnaldo Carvalho de Melo 	 */
832d3dbf43cSArnaldo Carvalho de Melo 	if (opts->initial_delay) {
833d3dbf43cSArnaldo Carvalho de Melo 		if (perf_evlist__add_dummy(evlist))
834d3dbf43cSArnaldo Carvalho de Melo 			return -ENOMEM;
835d3dbf43cSArnaldo Carvalho de Melo 
836515dbe48SJiri Olsa 		pos = evlist__first(evlist);
837d3dbf43cSArnaldo Carvalho de Melo 		pos->tracking = 0;
838515dbe48SJiri Olsa 		pos = evlist__last(evlist);
839d3dbf43cSArnaldo Carvalho de Melo 		pos->tracking = 1;
8401fc632ceSJiri Olsa 		pos->core.attr.enable_on_exec = 1;
841d3dbf43cSArnaldo Carvalho de Melo 	}
842d3dbf43cSArnaldo Carvalho de Melo 
843e68ae9cfSArnaldo Carvalho de Melo 	perf_evlist__config(evlist, opts, &callchain_param);
844cac21425SJiri Olsa 
845e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
8463da297a6SIngo Molnar try_again:
847af663bd0SJiri Olsa 		if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
848*ae430892SArnaldo Carvalho de Melo 			if (evsel__fallback(pos, errno, msg, sizeof(msg))) {
849bb963e16SNamhyung Kim 				if (verbose > 0)
850c0a54341SArnaldo Carvalho de Melo 					ui__warning("%s\n", msg);
8513da297a6SIngo Molnar 				goto try_again;
8523da297a6SIngo Molnar 			}
853cf99ad14SAndi Kleen 			if ((errno == EINVAL || errno == EBADF) &&
854cf99ad14SAndi Kleen 			    pos->leader != pos &&
855cf99ad14SAndi Kleen 			    pos->weak_group) {
8564804e011SAndi Kleen 			        pos = perf_evlist__reset_weak_group(evlist, pos, true);
857cf99ad14SAndi Kleen 				goto try_again;
858cf99ad14SAndi Kleen 			}
85956e52e85SArnaldo Carvalho de Melo 			rc = -errno;
86056e52e85SArnaldo Carvalho de Melo 			perf_evsel__open_strerror(pos, &opts->target,
86156e52e85SArnaldo Carvalho de Melo 						  errno, msg, sizeof(msg));
86256e52e85SArnaldo Carvalho de Melo 			ui__error("%s\n", msg);
8638d3eca20SDavid Ahern 			goto out;
8647c6a1c65SPeter Zijlstra 		}
865bfd8f72cSAndi Kleen 
866bfd8f72cSAndi Kleen 		pos->supported = true;
8677c6a1c65SPeter Zijlstra 	}
8687c6a1c65SPeter Zijlstra 
869c8b567c8SArnaldo Carvalho de Melo 	if (symbol_conf.kptr_restrict && !perf_evlist__exclude_kernel(evlist)) {
870c8b567c8SArnaldo Carvalho de Melo 		pr_warning(
871c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
872c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
873c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
874c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n"
875c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n"
876c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
877c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n");
878c8b567c8SArnaldo Carvalho de Melo 	}
879c8b567c8SArnaldo Carvalho de Melo 
88023d4aad4SArnaldo Carvalho de Melo 	if (perf_evlist__apply_filters(evlist, &pos)) {
88162d94b00SArnaldo Carvalho de Melo 		pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
8828ab2e96dSArnaldo Carvalho de Melo 			pos->filter, evsel__name(pos), errno,
883c8b5f2c9SArnaldo Carvalho de Melo 			str_error_r(errno, msg, sizeof(msg)));
8848d3eca20SDavid Ahern 		rc = -1;
8858d3eca20SDavid Ahern 		goto out;
8860a102479SFrederic Weisbecker 	}
8870a102479SFrederic Weisbecker 
888cda57a8cSWang Nan 	rc = record__mmap(rec);
889cda57a8cSWang Nan 	if (rc)
8908d3eca20SDavid Ahern 		goto out;
8910a27d7f9SArnaldo Carvalho de Melo 
892a91e5431SArnaldo Carvalho de Melo 	session->evlist = evlist;
8937b56cce2SArnaldo Carvalho de Melo 	perf_session__set_id_hdr_size(session);
8948d3eca20SDavid Ahern out:
8958d3eca20SDavid Ahern 	return rc;
896a91e5431SArnaldo Carvalho de Melo }
897a91e5431SArnaldo Carvalho de Melo 
898e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool,
899e3d59112SNamhyung Kim 				union perf_event *event,
900e3d59112SNamhyung Kim 				struct perf_sample *sample,
90132dcd021SJiri Olsa 				struct evsel *evsel,
902e3d59112SNamhyung Kim 				struct machine *machine)
903e3d59112SNamhyung Kim {
904e3d59112SNamhyung Kim 	struct record *rec = container_of(tool, struct record, tool);
905e3d59112SNamhyung Kim 
90668588bafSJin Yao 	if (rec->evlist->first_sample_time == 0)
90768588bafSJin Yao 		rec->evlist->first_sample_time = sample->time;
908e3d59112SNamhyung Kim 
90968588bafSJin Yao 	rec->evlist->last_sample_time = sample->time;
91068588bafSJin Yao 
91168588bafSJin Yao 	if (rec->buildid_all)
91268588bafSJin Yao 		return 0;
91368588bafSJin Yao 
91468588bafSJin Yao 	rec->samples++;
915e3d59112SNamhyung Kim 	return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
916e3d59112SNamhyung Kim }
917e3d59112SNamhyung Kim 
9188c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec)
9196122e4e4SArnaldo Carvalho de Melo {
920f5fc1412SJiri Olsa 	struct perf_session *session = rec->session;
9216122e4e4SArnaldo Carvalho de Melo 
92245112e89SJiri Olsa 	if (perf_data__size(&rec->data) == 0)
9239f591fd7SArnaldo Carvalho de Melo 		return 0;
9249f591fd7SArnaldo Carvalho de Melo 
92500dc8657SNamhyung Kim 	/*
92600dc8657SNamhyung Kim 	 * During this process, it'll load kernel map and replace the
92700dc8657SNamhyung Kim 	 * dso->long_name to a real pathname it found.  In this case
92800dc8657SNamhyung Kim 	 * we prefer the vmlinux path like
92900dc8657SNamhyung Kim 	 *   /lib/modules/3.16.4/build/vmlinux
93000dc8657SNamhyung Kim 	 *
93100dc8657SNamhyung Kim 	 * rather than build-id path (in debug directory).
93200dc8657SNamhyung Kim 	 *   $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
93300dc8657SNamhyung Kim 	 */
93400dc8657SNamhyung Kim 	symbol_conf.ignore_vmlinux_buildid = true;
93500dc8657SNamhyung Kim 
9366156681bSNamhyung Kim 	/*
9376156681bSNamhyung Kim 	 * If --buildid-all is given, it marks all DSO regardless of hits,
93868588bafSJin Yao 	 * so no need to process samples. But if timestamp_boundary is enabled,
93968588bafSJin Yao 	 * it still needs to walk on all samples to get the timestamps of
94068588bafSJin Yao 	 * first/last samples.
9416156681bSNamhyung Kim 	 */
94268588bafSJin Yao 	if (rec->buildid_all && !rec->timestamp_boundary)
9436156681bSNamhyung Kim 		rec->tool.sample = NULL;
9446156681bSNamhyung Kim 
945b7b61cbeSArnaldo Carvalho de Melo 	return perf_session__process_events(session);
9466122e4e4SArnaldo Carvalho de Melo }
9476122e4e4SArnaldo Carvalho de Melo 
9488115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
949a1645ce1SZhang, Yanmin {
950a1645ce1SZhang, Yanmin 	int err;
95145694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = data;
952a1645ce1SZhang, Yanmin 	/*
953a1645ce1SZhang, Yanmin 	 *As for guest kernel when processing subcommand record&report,
954a1645ce1SZhang, Yanmin 	 *we arrange module mmap prior to guest kernel mmap and trigger
955a1645ce1SZhang, Yanmin 	 *a preload dso because default guest module symbols are loaded
956a1645ce1SZhang, Yanmin 	 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
957a1645ce1SZhang, Yanmin 	 *method is used to avoid symbol missing when the first addr is
958a1645ce1SZhang, Yanmin 	 *in module instead of in guest kernel.
959a1645ce1SZhang, Yanmin 	 */
96045694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_modules(tool, process_synthesized_event,
961743eb868SArnaldo Carvalho de Melo 					     machine);
962a1645ce1SZhang, Yanmin 	if (err < 0)
963a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
96423346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
965a1645ce1SZhang, Yanmin 
966a1645ce1SZhang, Yanmin 	/*
967a1645ce1SZhang, Yanmin 	 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
968a1645ce1SZhang, Yanmin 	 * have no _text sometimes.
969a1645ce1SZhang, Yanmin 	 */
97045694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
9710ae617beSAdrian Hunter 						 machine);
972a1645ce1SZhang, Yanmin 	if (err < 0)
973a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
97423346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
975a1645ce1SZhang, Yanmin }
976a1645ce1SZhang, Yanmin 
97798402807SFrederic Weisbecker static struct perf_event_header finished_round_event = {
97898402807SFrederic Weisbecker 	.size = sizeof(struct perf_event_header),
97998402807SFrederic Weisbecker 	.type = PERF_RECORD_FINISHED_ROUND,
98098402807SFrederic Weisbecker };
98198402807SFrederic Weisbecker 
982a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map)
983f13de660SAlexey Budankov {
984f13de660SAlexey Budankov 	if (rec->opts.affinity != PERF_AFFINITY_SYS &&
9858384a260SAlexey Budankov 	    !bitmap_equal(rec->affinity_mask.bits, map->affinity_mask.bits,
9868384a260SAlexey Budankov 			  rec->affinity_mask.nbits)) {
9878384a260SAlexey Budankov 		bitmap_zero(rec->affinity_mask.bits, rec->affinity_mask.nbits);
9888384a260SAlexey Budankov 		bitmap_or(rec->affinity_mask.bits, rec->affinity_mask.bits,
9898384a260SAlexey Budankov 			  map->affinity_mask.bits, rec->affinity_mask.nbits);
9908384a260SAlexey Budankov 		sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&rec->affinity_mask),
9918384a260SAlexey Budankov 				  (cpu_set_t *)rec->affinity_mask.bits);
9928384a260SAlexey Budankov 		if (verbose == 2)
9938384a260SAlexey Budankov 			mmap_cpu_mask__scnprintf(&rec->affinity_mask, "thread");
994f13de660SAlexey Budankov 	}
995f13de660SAlexey Budankov }
996f13de660SAlexey Budankov 
9975d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment)
9985d7f4116SAlexey Budankov {
99972932371SJiri Olsa 	struct perf_record_compressed *event = record;
10005d7f4116SAlexey Budankov 	size_t size = sizeof(*event);
10015d7f4116SAlexey Budankov 
10025d7f4116SAlexey Budankov 	if (increment) {
10035d7f4116SAlexey Budankov 		event->header.size += increment;
10045d7f4116SAlexey Budankov 		return increment;
10055d7f4116SAlexey Budankov 	}
10065d7f4116SAlexey Budankov 
10075d7f4116SAlexey Budankov 	event->header.type = PERF_RECORD_COMPRESSED;
10085d7f4116SAlexey Budankov 	event->header.size = size;
10095d7f4116SAlexey Budankov 
10105d7f4116SAlexey Budankov 	return size;
10115d7f4116SAlexey Budankov }
10125d7f4116SAlexey Budankov 
10135d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size,
10145d7f4116SAlexey Budankov 			    void *src, size_t src_size)
10155d7f4116SAlexey Budankov {
10165d7f4116SAlexey Budankov 	size_t compressed;
101772932371SJiri Olsa 	size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
10185d7f4116SAlexey Budankov 
10195d7f4116SAlexey Budankov 	compressed = zstd_compress_stream_to_records(&session->zstd_data, dst, dst_size, src, src_size,
10205d7f4116SAlexey Budankov 						     max_record_size, process_comp_header);
10215d7f4116SAlexey Budankov 
10225d7f4116SAlexey Budankov 	session->bytes_transferred += src_size;
10235d7f4116SAlexey Budankov 	session->bytes_compressed  += compressed;
10245d7f4116SAlexey Budankov 
10255d7f4116SAlexey Budankov 	return compressed;
10265d7f4116SAlexey Budankov }
10275d7f4116SAlexey Budankov 
102863503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist,
1029470530bbSAlexey Budankov 				    bool overwrite, bool synch)
103098402807SFrederic Weisbecker {
1031dcabb507SJiri Olsa 	u64 bytes_written = rec->bytes_written;
10320e2e63ddSPeter Zijlstra 	int i;
10338d3eca20SDavid Ahern 	int rc = 0;
1034a5830532SJiri Olsa 	struct mmap *maps;
1035d3d1af6fSAlexey Budankov 	int trace_fd = rec->data.file.fd;
1036ef781128SAlexey Budankov 	off_t off = 0;
103798402807SFrederic Weisbecker 
1038cb21686bSWang Nan 	if (!evlist)
1039cb21686bSWang Nan 		return 0;
1040ef149c25SAdrian Hunter 
10410b72d69aSWang Nan 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
1042a4ea0ec4SWang Nan 	if (!maps)
1043a4ea0ec4SWang Nan 		return 0;
1044cb21686bSWang Nan 
10450b72d69aSWang Nan 	if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
104654cc54deSWang Nan 		return 0;
104754cc54deSWang Nan 
1048d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1049d3d1af6fSAlexey Budankov 		off = record__aio_get_pos(trace_fd);
1050d3d1af6fSAlexey Budankov 
1051c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
1052470530bbSAlexey Budankov 		u64 flush = 0;
1053a5830532SJiri Olsa 		struct mmap *map = &maps[i];
1054a4ea0ec4SWang Nan 
1055547740f7SJiri Olsa 		if (map->core.base) {
1056f13de660SAlexey Budankov 			record__adjust_affinity(rec, map);
1057470530bbSAlexey Budankov 			if (synch) {
105865aa2e6bSJiri Olsa 				flush = map->core.flush;
105965aa2e6bSJiri Olsa 				map->core.flush = 1;
1060470530bbSAlexey Budankov 			}
1061d3d1af6fSAlexey Budankov 			if (!record__aio_enabled(rec)) {
1062ef781128SAlexey Budankov 				if (perf_mmap__push(map, rec, record__pushfn) < 0) {
1063470530bbSAlexey Budankov 					if (synch)
106465aa2e6bSJiri Olsa 						map->core.flush = flush;
10658d3eca20SDavid Ahern 					rc = -1;
10668d3eca20SDavid Ahern 					goto out;
10678d3eca20SDavid Ahern 				}
1068d3d1af6fSAlexey Budankov 			} else {
1069ef781128SAlexey Budankov 				if (record__aio_push(rec, map, &off) < 0) {
1070d3d1af6fSAlexey Budankov 					record__aio_set_pos(trace_fd, off);
1071470530bbSAlexey Budankov 					if (synch)
107265aa2e6bSJiri Olsa 						map->core.flush = flush;
1073d3d1af6fSAlexey Budankov 					rc = -1;
1074d3d1af6fSAlexey Budankov 					goto out;
1075d3d1af6fSAlexey Budankov 				}
1076d3d1af6fSAlexey Budankov 			}
1077470530bbSAlexey Budankov 			if (synch)
107865aa2e6bSJiri Olsa 				map->core.flush = flush;
10798d3eca20SDavid Ahern 		}
1080ef149c25SAdrian Hunter 
1081e035f4caSJiri Olsa 		if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
1082c0a6de06SAdrian Hunter 		    !rec->opts.auxtrace_sample_mode &&
1083e035f4caSJiri Olsa 		    record__auxtrace_mmap_read(rec, map) != 0) {
1084ef149c25SAdrian Hunter 			rc = -1;
1085ef149c25SAdrian Hunter 			goto out;
1086ef149c25SAdrian Hunter 		}
108798402807SFrederic Weisbecker 	}
108898402807SFrederic Weisbecker 
1089d3d1af6fSAlexey Budankov 	if (record__aio_enabled(rec))
1090d3d1af6fSAlexey Budankov 		record__aio_set_pos(trace_fd, off);
1091d3d1af6fSAlexey Budankov 
1092dcabb507SJiri Olsa 	/*
1093dcabb507SJiri Olsa 	 * Mark the round finished in case we wrote
1094dcabb507SJiri Olsa 	 * at least one event.
1095dcabb507SJiri Olsa 	 */
1096dcabb507SJiri Olsa 	if (bytes_written != rec->bytes_written)
1097ded2b8feSJiri Olsa 		rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event));
10988d3eca20SDavid Ahern 
10990b72d69aSWang Nan 	if (overwrite)
110054cc54deSWang Nan 		perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
11018d3eca20SDavid Ahern out:
11028d3eca20SDavid Ahern 	return rc;
110398402807SFrederic Weisbecker }
110498402807SFrederic Weisbecker 
1105470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch)
1106cb21686bSWang Nan {
1107cb21686bSWang Nan 	int err;
1108cb21686bSWang Nan 
1109470530bbSAlexey Budankov 	err = record__mmap_read_evlist(rec, rec->evlist, false, synch);
1110cb21686bSWang Nan 	if (err)
1111cb21686bSWang Nan 		return err;
1112cb21686bSWang Nan 
1113470530bbSAlexey Budankov 	return record__mmap_read_evlist(rec, rec->evlist, true, synch);
1114cb21686bSWang Nan }
1115cb21686bSWang Nan 
11168c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec)
111757706abcSDavid Ahern {
111857706abcSDavid Ahern 	struct perf_session *session = rec->session;
111957706abcSDavid Ahern 	int feat;
112057706abcSDavid Ahern 
112157706abcSDavid Ahern 	for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
112257706abcSDavid Ahern 		perf_header__set_feat(&session->header, feat);
112357706abcSDavid Ahern 
112457706abcSDavid Ahern 	if (rec->no_buildid)
112557706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
112657706abcSDavid Ahern 
1127ce9036a6SJiri Olsa 	if (!have_tracepoints(&rec->evlist->core.entries))
112857706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
112957706abcSDavid Ahern 
113057706abcSDavid Ahern 	if (!rec->opts.branch_stack)
113157706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1132ef149c25SAdrian Hunter 
1133ef149c25SAdrian Hunter 	if (!rec->opts.full_auxtrace)
1134ef149c25SAdrian Hunter 		perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1135ffa517adSJiri Olsa 
1136cf790516SAlexey Budankov 	if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns))
1137cf790516SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_CLOCKID);
1138cf790516SAlexey Budankov 
1139258031c0SJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
114042e1fd80SAlexey Budankov 	if (!record__comp_enabled(rec))
114142e1fd80SAlexey Budankov 		perf_header__clear_feat(&session->header, HEADER_COMPRESSED);
1142258031c0SJiri Olsa 
1143ffa517adSJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_STAT);
114457706abcSDavid Ahern }
114557706abcSDavid Ahern 
1146e1ab48baSWang Nan static void
1147e1ab48baSWang Nan record__finish_output(struct record *rec)
1148e1ab48baSWang Nan {
11498ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
11508ceb41d7SJiri Olsa 	int fd = perf_data__fd(data);
1151e1ab48baSWang Nan 
11528ceb41d7SJiri Olsa 	if (data->is_pipe)
1153e1ab48baSWang Nan 		return;
1154e1ab48baSWang Nan 
1155e1ab48baSWang Nan 	rec->session->header.data_size += rec->bytes_written;
115645112e89SJiri Olsa 	data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR);
1157e1ab48baSWang Nan 
1158e1ab48baSWang Nan 	if (!rec->no_buildid) {
1159e1ab48baSWang Nan 		process_buildids(rec);
1160e1ab48baSWang Nan 
1161e1ab48baSWang Nan 		if (rec->buildid_all)
1162e1ab48baSWang Nan 			dsos__hit_all(rec->session);
1163e1ab48baSWang Nan 	}
1164e1ab48baSWang Nan 	perf_session__write_header(rec->session, rec->evlist, fd, true);
1165e1ab48baSWang Nan 
1166e1ab48baSWang Nan 	return;
1167e1ab48baSWang Nan }
1168e1ab48baSWang Nan 
11694ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail)
1170be7b0c9eSWang Nan {
11719d6aae72SArnaldo Carvalho de Melo 	int err;
11729749b90eSJiri Olsa 	struct perf_thread_map *thread_map;
1173be7b0c9eSWang Nan 
11744ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
11754ea648aeSWang Nan 		return 0;
11764ea648aeSWang Nan 
11779d6aae72SArnaldo Carvalho de Melo 	thread_map = thread_map__new_by_tid(rec->evlist->workload.pid);
11789d6aae72SArnaldo Carvalho de Melo 	if (thread_map == NULL)
11799d6aae72SArnaldo Carvalho de Melo 		return -1;
11809d6aae72SArnaldo Carvalho de Melo 
11819d6aae72SArnaldo Carvalho de Melo 	err = perf_event__synthesize_thread_map(&rec->tool, thread_map,
1182be7b0c9eSWang Nan 						 process_synthesized_event,
1183be7b0c9eSWang Nan 						 &rec->session->machines.host,
11843fcb10e4SMark Drayton 						 rec->opts.sample_address);
11857836e52eSJiri Olsa 	perf_thread_map__put(thread_map);
11869d6aae72SArnaldo Carvalho de Melo 	return err;
1187be7b0c9eSWang Nan }
1188be7b0c9eSWang Nan 
11894ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail);
11903c1cb7e3SWang Nan 
1191ecfd7a9cSWang Nan static int
1192ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit)
1193ecfd7a9cSWang Nan {
11948ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1195ecfd7a9cSWang Nan 	int fd, err;
119603724b2eSAndi Kleen 	char *new_filename;
1197ecfd7a9cSWang Nan 
1198ecfd7a9cSWang Nan 	/* Same Size:      "2015122520103046"*/
1199ecfd7a9cSWang Nan 	char timestamp[] = "InvalidTimestamp";
1200ecfd7a9cSWang Nan 
1201d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
1202d3d1af6fSAlexey Budankov 
12034ea648aeSWang Nan 	record__synthesize(rec, true);
12044ea648aeSWang Nan 	if (target__none(&rec->opts.target))
12054ea648aeSWang Nan 		record__synthesize_workload(rec, true);
12064ea648aeSWang Nan 
1207ecfd7a9cSWang Nan 	rec->samples = 0;
1208ecfd7a9cSWang Nan 	record__finish_output(rec);
1209ecfd7a9cSWang Nan 	err = fetch_current_timestamp(timestamp, sizeof(timestamp));
1210ecfd7a9cSWang Nan 	if (err) {
1211ecfd7a9cSWang Nan 		pr_err("Failed to get current timestamp\n");
1212ecfd7a9cSWang Nan 		return -EINVAL;
1213ecfd7a9cSWang Nan 	}
1214ecfd7a9cSWang Nan 
12158ceb41d7SJiri Olsa 	fd = perf_data__switch(data, timestamp,
1216ecfd7a9cSWang Nan 				    rec->session->header.data_offset,
121703724b2eSAndi Kleen 				    at_exit, &new_filename);
1218ecfd7a9cSWang Nan 	if (fd >= 0 && !at_exit) {
1219ecfd7a9cSWang Nan 		rec->bytes_written = 0;
1220ecfd7a9cSWang Nan 		rec->session->header.data_size = 0;
1221ecfd7a9cSWang Nan 	}
1222ecfd7a9cSWang Nan 
1223ecfd7a9cSWang Nan 	if (!quiet)
1224ecfd7a9cSWang Nan 		fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
12252d4f2799SJiri Olsa 			data->path, timestamp);
12263c1cb7e3SWang Nan 
122703724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
122803724b2eSAndi Kleen 		int n = rec->switch_output.cur_file + 1;
122903724b2eSAndi Kleen 
123003724b2eSAndi Kleen 		if (n >= rec->switch_output.num_files)
123103724b2eSAndi Kleen 			n = 0;
123203724b2eSAndi Kleen 		rec->switch_output.cur_file = n;
123303724b2eSAndi Kleen 		if (rec->switch_output.filenames[n]) {
123403724b2eSAndi Kleen 			remove(rec->switch_output.filenames[n]);
1235d8f9da24SArnaldo Carvalho de Melo 			zfree(&rec->switch_output.filenames[n]);
123603724b2eSAndi Kleen 		}
123703724b2eSAndi Kleen 		rec->switch_output.filenames[n] = new_filename;
123803724b2eSAndi Kleen 	} else {
123903724b2eSAndi Kleen 		free(new_filename);
124003724b2eSAndi Kleen 	}
124103724b2eSAndi Kleen 
12423c1cb7e3SWang Nan 	/* Output tracking events */
1243be7b0c9eSWang Nan 	if (!at_exit) {
12444ea648aeSWang Nan 		record__synthesize(rec, false);
12453c1cb7e3SWang Nan 
1246be7b0c9eSWang Nan 		/*
1247be7b0c9eSWang Nan 		 * In 'perf record --switch-output' without -a,
1248be7b0c9eSWang Nan 		 * record__synthesize() in record__switch_output() won't
1249be7b0c9eSWang Nan 		 * generate tracking events because there's no thread_map
1250be7b0c9eSWang Nan 		 * in evlist. Which causes newly created perf.data doesn't
1251be7b0c9eSWang Nan 		 * contain map and comm information.
1252be7b0c9eSWang Nan 		 * Create a fake thread_map and directly call
1253be7b0c9eSWang Nan 		 * perf_event__synthesize_thread_map() for those events.
1254be7b0c9eSWang Nan 		 */
1255be7b0c9eSWang Nan 		if (target__none(&rec->opts.target))
12564ea648aeSWang Nan 			record__synthesize_workload(rec, false);
1257be7b0c9eSWang Nan 	}
1258ecfd7a9cSWang Nan 	return fd;
1259ecfd7a9cSWang Nan }
1260ecfd7a9cSWang Nan 
1261f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno;
1262f33cbe72SArnaldo Carvalho de Melo 
1263f33cbe72SArnaldo Carvalho de Melo /*
1264f33cbe72SArnaldo Carvalho de Melo  * perf_evlist__prepare_workload will send a SIGUSR1
1265f33cbe72SArnaldo Carvalho de Melo  * if the fork fails, since we asked by setting its
1266f33cbe72SArnaldo Carvalho de Melo  * want_signal to true.
1267f33cbe72SArnaldo Carvalho de Melo  */
126845604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused,
126945604710SNamhyung Kim 					siginfo_t *info,
1270f33cbe72SArnaldo Carvalho de Melo 					void *ucontext __maybe_unused)
1271f33cbe72SArnaldo Carvalho de Melo {
1272f33cbe72SArnaldo Carvalho de Melo 	workload_exec_errno = info->si_value.sival_int;
1273f33cbe72SArnaldo Carvalho de Melo 	done = 1;
1274f33cbe72SArnaldo Carvalho de Melo 	child_finished = 1;
1275f33cbe72SArnaldo Carvalho de Melo }
1276f33cbe72SArnaldo Carvalho de Melo 
12772dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig);
1278bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig);
12792dd6d8a1SAdrian Hunter 
1280ee667f94SWang Nan static const struct perf_event_mmap_page *
128163503dbaSJiri Olsa perf_evlist__pick_pc(struct evlist *evlist)
1282ee667f94SWang Nan {
1283b2cb615dSWang Nan 	if (evlist) {
1284547740f7SJiri Olsa 		if (evlist->mmap && evlist->mmap[0].core.base)
1285547740f7SJiri Olsa 			return evlist->mmap[0].core.base;
1286547740f7SJiri Olsa 		if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base)
1287547740f7SJiri Olsa 			return evlist->overwrite_mmap[0].core.base;
1288b2cb615dSWang Nan 	}
1289ee667f94SWang Nan 	return NULL;
1290ee667f94SWang Nan }
1291ee667f94SWang Nan 
1292c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
1293c45628b0SWang Nan {
1294ee667f94SWang Nan 	const struct perf_event_mmap_page *pc;
1295ee667f94SWang Nan 
1296ee667f94SWang Nan 	pc = perf_evlist__pick_pc(rec->evlist);
1297ee667f94SWang Nan 	if (pc)
1298ee667f94SWang Nan 		return pc;
1299c45628b0SWang Nan 	return NULL;
1300c45628b0SWang Nan }
1301c45628b0SWang Nan 
13024ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail)
1303c45c86ebSWang Nan {
1304c45c86ebSWang Nan 	struct perf_session *session = rec->session;
1305c45c86ebSWang Nan 	struct machine *machine = &session->machines.host;
13068ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1307c45c86ebSWang Nan 	struct record_opts *opts = &rec->opts;
1308c45c86ebSWang Nan 	struct perf_tool *tool = &rec->tool;
13098ceb41d7SJiri Olsa 	int fd = perf_data__fd(data);
1310c45c86ebSWang Nan 	int err = 0;
1311d99c22eaSStephane Eranian 	event_op f = process_synthesized_event;
1312c45c86ebSWang Nan 
13134ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
13144ea648aeSWang Nan 		return 0;
13154ea648aeSWang Nan 
13168ceb41d7SJiri Olsa 	if (data->is_pipe) {
1317a2015516SJiri Olsa 		/*
1318a2015516SJiri Olsa 		 * We need to synthesize events first, because some
1319a2015516SJiri Olsa 		 * features works on top of them (on report side).
1320a2015516SJiri Olsa 		 */
1321318ec184SJiri Olsa 		err = perf_event__synthesize_attrs(tool, rec->evlist,
1322c45c86ebSWang Nan 						   process_synthesized_event);
1323c45c86ebSWang Nan 		if (err < 0) {
1324c45c86ebSWang Nan 			pr_err("Couldn't synthesize attrs.\n");
1325c45c86ebSWang Nan 			goto out;
1326c45c86ebSWang Nan 		}
1327c45c86ebSWang Nan 
1328a2015516SJiri Olsa 		err = perf_event__synthesize_features(tool, session, rec->evlist,
1329a2015516SJiri Olsa 						      process_synthesized_event);
1330a2015516SJiri Olsa 		if (err < 0) {
1331a2015516SJiri Olsa 			pr_err("Couldn't synthesize features.\n");
1332a2015516SJiri Olsa 			return err;
1333a2015516SJiri Olsa 		}
1334a2015516SJiri Olsa 
1335ce9036a6SJiri Olsa 		if (have_tracepoints(&rec->evlist->core.entries)) {
1336c45c86ebSWang Nan 			/*
1337c45c86ebSWang Nan 			 * FIXME err <= 0 here actually means that
1338c45c86ebSWang Nan 			 * there were no tracepoints so its not really
1339c45c86ebSWang Nan 			 * an error, just that we don't need to
1340c45c86ebSWang Nan 			 * synthesize anything.  We really have to
1341c45c86ebSWang Nan 			 * return this more properly and also
1342c45c86ebSWang Nan 			 * propagate errors that now are calling die()
1343c45c86ebSWang Nan 			 */
1344c45c86ebSWang Nan 			err = perf_event__synthesize_tracing_data(tool,	fd, rec->evlist,
1345c45c86ebSWang Nan 								  process_synthesized_event);
1346c45c86ebSWang Nan 			if (err <= 0) {
1347c45c86ebSWang Nan 				pr_err("Couldn't record tracing data.\n");
1348c45c86ebSWang Nan 				goto out;
1349c45c86ebSWang Nan 			}
1350c45c86ebSWang Nan 			rec->bytes_written += err;
1351c45c86ebSWang Nan 		}
1352c45c86ebSWang Nan 	}
1353c45c86ebSWang Nan 
1354c45628b0SWang Nan 	err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
135546bc29b9SAdrian Hunter 					  process_synthesized_event, machine);
135646bc29b9SAdrian Hunter 	if (err)
135746bc29b9SAdrian Hunter 		goto out;
135846bc29b9SAdrian Hunter 
1359c0a6de06SAdrian Hunter 	/* Synthesize id_index before auxtrace_info */
1360c0a6de06SAdrian Hunter 	if (rec->opts.auxtrace_sample_mode) {
1361c0a6de06SAdrian Hunter 		err = perf_event__synthesize_id_index(tool,
1362c0a6de06SAdrian Hunter 						      process_synthesized_event,
1363c0a6de06SAdrian Hunter 						      session->evlist, machine);
1364c0a6de06SAdrian Hunter 		if (err)
1365c0a6de06SAdrian Hunter 			goto out;
1366c0a6de06SAdrian Hunter 	}
1367c0a6de06SAdrian Hunter 
1368c45c86ebSWang Nan 	if (rec->opts.full_auxtrace) {
1369c45c86ebSWang Nan 		err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
1370c45c86ebSWang Nan 					session, process_synthesized_event);
1371c45c86ebSWang Nan 		if (err)
1372c45c86ebSWang Nan 			goto out;
1373c45c86ebSWang Nan 	}
1374c45c86ebSWang Nan 
13756c443954SArnaldo Carvalho de Melo 	if (!perf_evlist__exclude_kernel(rec->evlist)) {
1376c45c86ebSWang Nan 		err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
1377c45c86ebSWang Nan 							 machine);
1378c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
1379c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1380c45c86ebSWang Nan 				   "Check /proc/kallsyms permission or run as root.\n");
1381c45c86ebSWang Nan 
1382c45c86ebSWang Nan 		err = perf_event__synthesize_modules(tool, process_synthesized_event,
1383c45c86ebSWang Nan 						     machine);
1384c45c86ebSWang Nan 		WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
1385c45c86ebSWang Nan 				   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1386c45c86ebSWang Nan 				   "Check /proc/modules permission or run as root.\n");
13876c443954SArnaldo Carvalho de Melo 	}
1388c45c86ebSWang Nan 
1389c45c86ebSWang Nan 	if (perf_guest) {
1390c45c86ebSWang Nan 		machines__process_guests(&session->machines,
1391c45c86ebSWang Nan 					 perf_event__synthesize_guest_os, tool);
1392c45c86ebSWang Nan 	}
1393c45c86ebSWang Nan 
1394bfd8f72cSAndi Kleen 	err = perf_event__synthesize_extra_attr(&rec->tool,
1395bfd8f72cSAndi Kleen 						rec->evlist,
1396bfd8f72cSAndi Kleen 						process_synthesized_event,
1397bfd8f72cSAndi Kleen 						data->is_pipe);
1398bfd8f72cSAndi Kleen 	if (err)
1399bfd8f72cSAndi Kleen 		goto out;
1400bfd8f72cSAndi Kleen 
140103617c22SJiri Olsa 	err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads,
1402373565d2SAndi Kleen 						 process_synthesized_event,
1403373565d2SAndi Kleen 						NULL);
1404373565d2SAndi Kleen 	if (err < 0) {
1405373565d2SAndi Kleen 		pr_err("Couldn't synthesize thread map.\n");
1406373565d2SAndi Kleen 		return err;
1407373565d2SAndi Kleen 	}
1408373565d2SAndi Kleen 
1409f72f901dSJiri Olsa 	err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.cpus,
1410373565d2SAndi Kleen 					     process_synthesized_event, NULL);
1411373565d2SAndi Kleen 	if (err < 0) {
1412373565d2SAndi Kleen 		pr_err("Couldn't synthesize cpu map.\n");
1413373565d2SAndi Kleen 		return err;
1414373565d2SAndi Kleen 	}
1415373565d2SAndi Kleen 
1416e5416950SSong Liu 	err = perf_event__synthesize_bpf_events(session, process_synthesized_event,
14177b612e29SSong Liu 						machine, opts);
14187b612e29SSong Liu 	if (err < 0)
14197b612e29SSong Liu 		pr_warning("Couldn't synthesize bpf events.\n");
14207b612e29SSong Liu 
1421ab64069fSNamhyung Kim 	err = perf_event__synthesize_cgroups(tool, process_synthesized_event,
1422ab64069fSNamhyung Kim 					     machine);
1423ab64069fSNamhyung Kim 	if (err < 0)
1424ab64069fSNamhyung Kim 		pr_warning("Couldn't synthesize cgroup events.\n");
1425ab64069fSNamhyung Kim 
1426d99c22eaSStephane Eranian 	if (rec->opts.nr_threads_synthesize > 1) {
1427d99c22eaSStephane Eranian 		perf_set_multithreaded();
1428d99c22eaSStephane Eranian 		f = process_locked_synthesized_event;
1429d99c22eaSStephane Eranian 	}
1430d99c22eaSStephane Eranian 
143103617c22SJiri Olsa 	err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads,
1432d99c22eaSStephane Eranian 					    f, opts->sample_address,
1433d99c22eaSStephane Eranian 					    rec->opts.nr_threads_synthesize);
1434d99c22eaSStephane Eranian 
1435d99c22eaSStephane Eranian 	if (rec->opts.nr_threads_synthesize > 1)
1436d99c22eaSStephane Eranian 		perf_set_singlethreaded();
1437d99c22eaSStephane Eranian 
1438c45c86ebSWang Nan out:
1439c45c86ebSWang Nan 	return err;
1440c45c86ebSWang Nan }
1441c45c86ebSWang Nan 
1442899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data)
1443899e5ffbSArnaldo Carvalho de Melo {
1444899e5ffbSArnaldo Carvalho de Melo 	struct record *rec = data;
1445899e5ffbSArnaldo Carvalho de Melo 	pthread_kill(rec->thread_id, SIGUSR2);
1446899e5ffbSArnaldo Carvalho de Melo 	return 0;
1447899e5ffbSArnaldo Carvalho de Melo }
1448899e5ffbSArnaldo Carvalho de Melo 
144923cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec)
145023cbb41cSArnaldo Carvalho de Melo {
145123cbb41cSArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
145223cbb41cSArnaldo Carvalho de Melo 
145323cbb41cSArnaldo Carvalho de Melo 	if (rec->sb_evlist != NULL) {
145423cbb41cSArnaldo Carvalho de Melo 		/*
145523cbb41cSArnaldo Carvalho de Melo 		 * We get here if --switch-output-event populated the
145623cbb41cSArnaldo Carvalho de Melo 		 * sb_evlist, so associate a callback that will send a SIGUSR2
145723cbb41cSArnaldo Carvalho de Melo 		 * to the main thread.
145823cbb41cSArnaldo Carvalho de Melo 		 */
145923cbb41cSArnaldo Carvalho de Melo 		evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec);
146023cbb41cSArnaldo Carvalho de Melo 		rec->thread_id = pthread_self();
146123cbb41cSArnaldo Carvalho de Melo 	}
146223cbb41cSArnaldo Carvalho de Melo 
146323cbb41cSArnaldo Carvalho de Melo 	if (!opts->no_bpf_event) {
146423cbb41cSArnaldo Carvalho de Melo 		if (rec->sb_evlist == NULL) {
146523cbb41cSArnaldo Carvalho de Melo 			rec->sb_evlist = evlist__new();
146623cbb41cSArnaldo Carvalho de Melo 
146723cbb41cSArnaldo Carvalho de Melo 			if (rec->sb_evlist == NULL) {
146823cbb41cSArnaldo Carvalho de Melo 				pr_err("Couldn't create side band evlist.\n.");
146923cbb41cSArnaldo Carvalho de Melo 				return -1;
147023cbb41cSArnaldo Carvalho de Melo 			}
147123cbb41cSArnaldo Carvalho de Melo 		}
147223cbb41cSArnaldo Carvalho de Melo 
147323cbb41cSArnaldo Carvalho de Melo 		if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) {
147423cbb41cSArnaldo Carvalho de Melo 			pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
147523cbb41cSArnaldo Carvalho de Melo 			return -1;
147623cbb41cSArnaldo Carvalho de Melo 		}
147723cbb41cSArnaldo Carvalho de Melo 	}
147823cbb41cSArnaldo Carvalho de Melo 
147923cbb41cSArnaldo Carvalho de Melo 	if (perf_evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) {
148023cbb41cSArnaldo Carvalho de Melo 		pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
148123cbb41cSArnaldo Carvalho de Melo 		opts->no_bpf_event = true;
148223cbb41cSArnaldo Carvalho de Melo 	}
148323cbb41cSArnaldo Carvalho de Melo 
148423cbb41cSArnaldo Carvalho de Melo 	return 0;
148523cbb41cSArnaldo Carvalho de Melo }
148623cbb41cSArnaldo Carvalho de Melo 
14878c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv)
148886470930SIngo Molnar {
148957706abcSDavid Ahern 	int err;
149045604710SNamhyung Kim 	int status = 0;
14918b412664SPeter Zijlstra 	unsigned long waking = 0;
149246be604bSZhang, Yanmin 	const bool forks = argc > 0;
149345694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = &rec->tool;
1494b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
14958ceb41d7SJiri Olsa 	struct perf_data *data = &rec->data;
1496d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session;
14976dcf45efSArnaldo Carvalho de Melo 	bool disabled = false, draining = false;
149842aa276fSNamhyung Kim 	int fd;
1499d3c8c08eSAlexey Budankov 	float ratio = 0;
150086470930SIngo Molnar 
150145604710SNamhyung Kim 	atexit(record__sig_exit);
1502f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
1503f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
1504804f7ac7SDavid Ahern 	signal(SIGTERM, sig_handler);
1505a074865eSWang Nan 	signal(SIGSEGV, sigsegv_handler);
1506c0bdc1c4SWang Nan 
1507f3b3614aSHari Bathini 	if (rec->opts.record_namespaces)
1508f3b3614aSHari Bathini 		tool->namespace_events = true;
1509f3b3614aSHari Bathini 
15108fb4b679SNamhyung Kim 	if (rec->opts.record_cgroup) {
15118fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE
15128fb4b679SNamhyung Kim 		tool->cgroup_events = true;
15138fb4b679SNamhyung Kim #else
15148fb4b679SNamhyung Kim 		pr_err("cgroup tracking is not supported\n");
15158fb4b679SNamhyung Kim 		return -1;
15168fb4b679SNamhyung Kim #endif
15178fb4b679SNamhyung Kim 	}
15188fb4b679SNamhyung Kim 
1519dc0c6127SJiri Olsa 	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
15202dd6d8a1SAdrian Hunter 		signal(SIGUSR2, snapshot_sig_handler);
15213c1cb7e3SWang Nan 		if (rec->opts.auxtrace_snapshot_mode)
15225f9cf599SWang Nan 			trigger_on(&auxtrace_snapshot_trigger);
1523dc0c6127SJiri Olsa 		if (rec->switch_output.enabled)
15243c1cb7e3SWang Nan 			trigger_on(&switch_output_trigger);
1525c0bdc1c4SWang Nan 	} else {
15262dd6d8a1SAdrian Hunter 		signal(SIGUSR2, SIG_IGN);
1527c0bdc1c4SWang Nan 	}
1528f5970550SPeter Zijlstra 
15298ceb41d7SJiri Olsa 	session = perf_session__new(data, false, tool);
15306ef81c55SMamatha Inamdar 	if (IS_ERR(session)) {
1531ffa91880SAdrien BAK 		pr_err("Perf session creation failed.\n");
15326ef81c55SMamatha Inamdar 		return PTR_ERR(session);
1533a9a70bbcSArnaldo Carvalho de Melo 	}
1534a9a70bbcSArnaldo Carvalho de Melo 
15358ceb41d7SJiri Olsa 	fd = perf_data__fd(data);
1536d20deb64SArnaldo Carvalho de Melo 	rec->session = session;
1537d20deb64SArnaldo Carvalho de Melo 
15385d7f4116SAlexey Budankov 	if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) {
15395d7f4116SAlexey Budankov 		pr_err("Compression initialization failed.\n");
15405d7f4116SAlexey Budankov 		return -1;
15415d7f4116SAlexey Budankov 	}
15425d7f4116SAlexey Budankov 
15435d7f4116SAlexey Budankov 	session->header.env.comp_type  = PERF_COMP_ZSTD;
15445d7f4116SAlexey Budankov 	session->header.env.comp_level = rec->opts.comp_level;
15455d7f4116SAlexey Budankov 
1546eeb399b5SAdrian Hunter 	if (rec->opts.kcore &&
1547eeb399b5SAdrian Hunter 	    !record__kcore_readable(&session->machines.host)) {
1548eeb399b5SAdrian Hunter 		pr_err("ERROR: kcore is not readable.\n");
1549eeb399b5SAdrian Hunter 		return -1;
1550eeb399b5SAdrian Hunter 	}
1551eeb399b5SAdrian Hunter 
15528c6f45a7SArnaldo Carvalho de Melo 	record__init_features(rec);
1553330aa675SStephane Eranian 
1554cf790516SAlexey Budankov 	if (rec->opts.use_clockid && rec->opts.clockid_res_ns)
1555cf790516SAlexey Budankov 		session->header.env.clockid_res_ns = rec->opts.clockid_res_ns;
1556cf790516SAlexey Budankov 
1557d4db3f16SArnaldo Carvalho de Melo 	if (forks) {
15583e2be2daSArnaldo Carvalho de Melo 		err = perf_evlist__prepare_workload(rec->evlist, &opts->target,
15598ceb41d7SJiri Olsa 						    argv, data->is_pipe,
1560735f7e0bSArnaldo Carvalho de Melo 						    workload_exec_failed_signal);
156135b9d88eSArnaldo Carvalho de Melo 		if (err < 0) {
156235b9d88eSArnaldo Carvalho de Melo 			pr_err("Couldn't run the workload!\n");
156345604710SNamhyung Kim 			status = err;
156435b9d88eSArnaldo Carvalho de Melo 			goto out_delete_session;
1565856e9660SPeter Zijlstra 		}
1566856e9660SPeter Zijlstra 	}
1567856e9660SPeter Zijlstra 
1568ad46e48cSJiri Olsa 	/*
1569ad46e48cSJiri Olsa 	 * If we have just single event and are sending data
1570ad46e48cSJiri Olsa 	 * through pipe, we need to force the ids allocation,
1571ad46e48cSJiri Olsa 	 * because we synthesize event name through the pipe
1572ad46e48cSJiri Olsa 	 * and need the id for that.
1573ad46e48cSJiri Olsa 	 */
15746484d2f9SJiri Olsa 	if (data->is_pipe && rec->evlist->core.nr_entries == 1)
1575ad46e48cSJiri Olsa 		rec->opts.sample_id = true;
1576ad46e48cSJiri Olsa 
15778c6f45a7SArnaldo Carvalho de Melo 	if (record__open(rec) != 0) {
15788d3eca20SDavid Ahern 		err = -1;
157945604710SNamhyung Kim 		goto out_child;
15808d3eca20SDavid Ahern 	}
1581f6fa4375SJiri Olsa 	session->header.env.comp_mmap_len = session->evlist->core.mmap_len;
158286470930SIngo Molnar 
1583eeb399b5SAdrian Hunter 	if (rec->opts.kcore) {
1584eeb399b5SAdrian Hunter 		err = record__kcore_copy(&session->machines.host, data);
1585eeb399b5SAdrian Hunter 		if (err) {
1586eeb399b5SAdrian Hunter 			pr_err("ERROR: Failed to copy kcore\n");
1587eeb399b5SAdrian Hunter 			goto out_child;
1588eeb399b5SAdrian Hunter 		}
1589eeb399b5SAdrian Hunter 	}
1590eeb399b5SAdrian Hunter 
15918690a2a7SWang Nan 	err = bpf__apply_obj_config();
15928690a2a7SWang Nan 	if (err) {
15938690a2a7SWang Nan 		char errbuf[BUFSIZ];
15948690a2a7SWang Nan 
15958690a2a7SWang Nan 		bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
15968690a2a7SWang Nan 		pr_err("ERROR: Apply config to BPF failed: %s\n",
15978690a2a7SWang Nan 			 errbuf);
15988690a2a7SWang Nan 		goto out_child;
15998690a2a7SWang Nan 	}
16008690a2a7SWang Nan 
1601cca8482cSAdrian Hunter 	/*
1602cca8482cSAdrian Hunter 	 * Normally perf_session__new would do this, but it doesn't have the
1603cca8482cSAdrian Hunter 	 * evlist.
1604cca8482cSAdrian Hunter 	 */
1605cca8482cSAdrian Hunter 	if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) {
1606cca8482cSAdrian Hunter 		pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
1607cca8482cSAdrian Hunter 		rec->tool.ordered_events = false;
1608cca8482cSAdrian Hunter 	}
1609cca8482cSAdrian Hunter 
16103e2be2daSArnaldo Carvalho de Melo 	if (!rec->evlist->nr_groups)
1611a8bb559bSNamhyung Kim 		perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
1612a8bb559bSNamhyung Kim 
16138ceb41d7SJiri Olsa 	if (data->is_pipe) {
161442aa276fSNamhyung Kim 		err = perf_header__write_pipe(fd);
1615529870e3STom Zanussi 		if (err < 0)
161645604710SNamhyung Kim 			goto out_child;
1617563aecb2SJiri Olsa 	} else {
161842aa276fSNamhyung Kim 		err = perf_session__write_header(session, rec->evlist, fd, false);
1619d5eed904SArnaldo Carvalho de Melo 		if (err < 0)
162045604710SNamhyung Kim 			goto out_child;
1621d5eed904SArnaldo Carvalho de Melo 	}
16227c6a1c65SPeter Zijlstra 
1623b38d85efSArnaldo Carvalho de Melo 	err = -1;
1624d3665498SDavid Ahern 	if (!rec->no_buildid
1625e20960c0SRobert Richter 	    && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
1626d3665498SDavid Ahern 		pr_err("Couldn't generate buildids. "
1627e20960c0SRobert Richter 		       "Use --no-buildid to profile anyway.\n");
162845604710SNamhyung Kim 		goto out_child;
1629e20960c0SRobert Richter 	}
1630e20960c0SRobert Richter 
163123cbb41cSArnaldo Carvalho de Melo 	err = record__setup_sb_evlist(rec);
163223cbb41cSArnaldo Carvalho de Melo 	if (err)
1633b38d85efSArnaldo Carvalho de Melo 		goto out_child;
1634657ee553SSong Liu 
16354ea648aeSWang Nan 	err = record__synthesize(rec, false);
1636c45c86ebSWang Nan 	if (err < 0)
163745604710SNamhyung Kim 		goto out_child;
16388d3eca20SDavid Ahern 
1639d20deb64SArnaldo Carvalho de Melo 	if (rec->realtime_prio) {
164086470930SIngo Molnar 		struct sched_param param;
164186470930SIngo Molnar 
1642d20deb64SArnaldo Carvalho de Melo 		param.sched_priority = rec->realtime_prio;
164386470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
16446beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
16458d3eca20SDavid Ahern 			err = -1;
164645604710SNamhyung Kim 			goto out_child;
164786470930SIngo Molnar 		}
164886470930SIngo Molnar 	}
164986470930SIngo Molnar 
1650774cb499SJiri Olsa 	/*
1651774cb499SJiri Olsa 	 * When perf is starting the traced process, all the events
1652774cb499SJiri Olsa 	 * (apart from group members) have enable_on_exec=1 set,
1653774cb499SJiri Olsa 	 * so don't spoil it by prematurely enabling them.
1654774cb499SJiri Olsa 	 */
16556619a53eSAndi Kleen 	if (!target__none(&opts->target) && !opts->initial_delay)
16561c87f165SJiri Olsa 		evlist__enable(rec->evlist);
1657764e16a3SDavid Ahern 
1658856e9660SPeter Zijlstra 	/*
1659856e9660SPeter Zijlstra 	 * Let the child rip
1660856e9660SPeter Zijlstra 	 */
1661e803cf97SNamhyung Kim 	if (forks) {
166220a8a3cfSJiri Olsa 		struct machine *machine = &session->machines.host;
1663e5bed564SNamhyung Kim 		union perf_event *event;
1664e907caf3SHari Bathini 		pid_t tgid;
1665e5bed564SNamhyung Kim 
1666e5bed564SNamhyung Kim 		event = malloc(sizeof(event->comm) + machine->id_hdr_size);
1667e5bed564SNamhyung Kim 		if (event == NULL) {
1668e5bed564SNamhyung Kim 			err = -ENOMEM;
1669e5bed564SNamhyung Kim 			goto out_child;
1670e5bed564SNamhyung Kim 		}
1671e5bed564SNamhyung Kim 
1672e803cf97SNamhyung Kim 		/*
1673e803cf97SNamhyung Kim 		 * Some H/W events are generated before COMM event
1674e803cf97SNamhyung Kim 		 * which is emitted during exec(), so perf script
1675e803cf97SNamhyung Kim 		 * cannot see a correct process name for those events.
1676e803cf97SNamhyung Kim 		 * Synthesize COMM event to prevent it.
1677e803cf97SNamhyung Kim 		 */
1678e907caf3SHari Bathini 		tgid = perf_event__synthesize_comm(tool, event,
1679e803cf97SNamhyung Kim 						   rec->evlist->workload.pid,
1680e803cf97SNamhyung Kim 						   process_synthesized_event,
1681e803cf97SNamhyung Kim 						   machine);
1682e5bed564SNamhyung Kim 		free(event);
1683e803cf97SNamhyung Kim 
1684e907caf3SHari Bathini 		if (tgid == -1)
1685e907caf3SHari Bathini 			goto out_child;
1686e907caf3SHari Bathini 
1687e907caf3SHari Bathini 		event = malloc(sizeof(event->namespaces) +
1688e907caf3SHari Bathini 			       (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
1689e907caf3SHari Bathini 			       machine->id_hdr_size);
1690e907caf3SHari Bathini 		if (event == NULL) {
1691e907caf3SHari Bathini 			err = -ENOMEM;
1692e907caf3SHari Bathini 			goto out_child;
1693e907caf3SHari Bathini 		}
1694e907caf3SHari Bathini 
1695e907caf3SHari Bathini 		/*
1696e907caf3SHari Bathini 		 * Synthesize NAMESPACES event for the command specified.
1697e907caf3SHari Bathini 		 */
1698e907caf3SHari Bathini 		perf_event__synthesize_namespaces(tool, event,
1699e907caf3SHari Bathini 						  rec->evlist->workload.pid,
1700e907caf3SHari Bathini 						  tgid, process_synthesized_event,
1701e907caf3SHari Bathini 						  machine);
1702e907caf3SHari Bathini 		free(event);
1703e907caf3SHari Bathini 
17043e2be2daSArnaldo Carvalho de Melo 		perf_evlist__start_workload(rec->evlist);
1705e803cf97SNamhyung Kim 	}
1706856e9660SPeter Zijlstra 
17076619a53eSAndi Kleen 	if (opts->initial_delay) {
17080693e680SArnaldo Carvalho de Melo 		usleep(opts->initial_delay * USEC_PER_MSEC);
17091c87f165SJiri Olsa 		evlist__enable(rec->evlist);
17106619a53eSAndi Kleen 	}
17116619a53eSAndi Kleen 
17125f9cf599SWang Nan 	trigger_ready(&auxtrace_snapshot_trigger);
17133c1cb7e3SWang Nan 	trigger_ready(&switch_output_trigger);
1714a074865eSWang Nan 	perf_hooks__invoke_record_start();
1715649c48a9SPeter Zijlstra 	for (;;) {
17169f065194SYang Shi 		unsigned long long hits = rec->samples;
171786470930SIngo Molnar 
171805737464SWang Nan 		/*
171905737464SWang Nan 		 * rec->evlist->bkw_mmap_state is possible to be
172005737464SWang Nan 		 * BKW_MMAP_EMPTY here: when done == true and
172105737464SWang Nan 		 * hits != rec->samples in previous round.
172205737464SWang Nan 		 *
172305737464SWang Nan 		 * perf_evlist__toggle_bkw_mmap ensure we never
172405737464SWang Nan 		 * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
172505737464SWang Nan 		 */
172605737464SWang Nan 		if (trigger_is_hit(&switch_output_trigger) || done || draining)
172705737464SWang Nan 			perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
172805737464SWang Nan 
1729470530bbSAlexey Budankov 		if (record__mmap_read_all(rec, false) < 0) {
17305f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
17313c1cb7e3SWang Nan 			trigger_error(&switch_output_trigger);
17328d3eca20SDavid Ahern 			err = -1;
173345604710SNamhyung Kim 			goto out_child;
17348d3eca20SDavid Ahern 		}
173586470930SIngo Molnar 
17362dd6d8a1SAdrian Hunter 		if (auxtrace_record__snapshot_started) {
17372dd6d8a1SAdrian Hunter 			auxtrace_record__snapshot_started = 0;
17385f9cf599SWang Nan 			if (!trigger_is_error(&auxtrace_snapshot_trigger))
1739ce7b0e42SAlexander Shishkin 				record__read_auxtrace_snapshot(rec, false);
17405f9cf599SWang Nan 			if (trigger_is_error(&auxtrace_snapshot_trigger)) {
17412dd6d8a1SAdrian Hunter 				pr_err("AUX area tracing snapshot failed\n");
17422dd6d8a1SAdrian Hunter 				err = -1;
17432dd6d8a1SAdrian Hunter 				goto out_child;
17442dd6d8a1SAdrian Hunter 			}
17452dd6d8a1SAdrian Hunter 		}
17462dd6d8a1SAdrian Hunter 
17473c1cb7e3SWang Nan 		if (trigger_is_hit(&switch_output_trigger)) {
174805737464SWang Nan 			/*
174905737464SWang Nan 			 * If switch_output_trigger is hit, the data in
175005737464SWang Nan 			 * overwritable ring buffer should have been collected,
175105737464SWang Nan 			 * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
175205737464SWang Nan 			 *
175305737464SWang Nan 			 * If SIGUSR2 raise after or during record__mmap_read_all(),
175405737464SWang Nan 			 * record__mmap_read_all() didn't collect data from
175505737464SWang Nan 			 * overwritable ring buffer. Read again.
175605737464SWang Nan 			 */
175705737464SWang Nan 			if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
175805737464SWang Nan 				continue;
17593c1cb7e3SWang Nan 			trigger_ready(&switch_output_trigger);
17603c1cb7e3SWang Nan 
176105737464SWang Nan 			/*
176205737464SWang Nan 			 * Reenable events in overwrite ring buffer after
176305737464SWang Nan 			 * record__mmap_read_all(): we should have collected
176405737464SWang Nan 			 * data from it.
176505737464SWang Nan 			 */
176605737464SWang Nan 			perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
176705737464SWang Nan 
17683c1cb7e3SWang Nan 			if (!quiet)
17693c1cb7e3SWang Nan 				fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
17703c1cb7e3SWang Nan 					waking);
17713c1cb7e3SWang Nan 			waking = 0;
17723c1cb7e3SWang Nan 			fd = record__switch_output(rec, false);
17733c1cb7e3SWang Nan 			if (fd < 0) {
17743c1cb7e3SWang Nan 				pr_err("Failed to switch to new file\n");
17753c1cb7e3SWang Nan 				trigger_error(&switch_output_trigger);
17763c1cb7e3SWang Nan 				err = fd;
17773c1cb7e3SWang Nan 				goto out_child;
17783c1cb7e3SWang Nan 			}
1779bfacbe3bSJiri Olsa 
1780bfacbe3bSJiri Olsa 			/* re-arm the alarm */
1781bfacbe3bSJiri Olsa 			if (rec->switch_output.time)
1782bfacbe3bSJiri Olsa 				alarm(rec->switch_output.time);
17833c1cb7e3SWang Nan 		}
17843c1cb7e3SWang Nan 
1785d20deb64SArnaldo Carvalho de Melo 		if (hits == rec->samples) {
17866dcf45efSArnaldo Carvalho de Melo 			if (done || draining)
1787649c48a9SPeter Zijlstra 				break;
178880ab2987SJiri Olsa 			err = evlist__poll(rec->evlist, -1);
1789a515114fSJiri Olsa 			/*
1790a515114fSJiri Olsa 			 * Propagate error, only if there's any. Ignore positive
1791a515114fSJiri Olsa 			 * number of returned events and interrupt error.
1792a515114fSJiri Olsa 			 */
1793a515114fSJiri Olsa 			if (err > 0 || (err < 0 && errno == EINTR))
179445604710SNamhyung Kim 				err = 0;
17958b412664SPeter Zijlstra 			waking++;
17966dcf45efSArnaldo Carvalho de Melo 
1797f4009e7bSJiri Olsa 			if (evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
17986dcf45efSArnaldo Carvalho de Melo 				draining = true;
17998b412664SPeter Zijlstra 		}
18008b412664SPeter Zijlstra 
1801774cb499SJiri Olsa 		/*
1802774cb499SJiri Olsa 		 * When perf is starting the traced process, at the end events
1803774cb499SJiri Olsa 		 * die with the process and we wait for that. Thus no need to
1804774cb499SJiri Olsa 		 * disable events in this case.
1805774cb499SJiri Olsa 		 */
1806602ad878SArnaldo Carvalho de Melo 		if (done && !disabled && !target__none(&opts->target)) {
18075f9cf599SWang Nan 			trigger_off(&auxtrace_snapshot_trigger);
1808e74676deSJiri Olsa 			evlist__disable(rec->evlist);
18092711926aSJiri Olsa 			disabled = true;
18102711926aSJiri Olsa 		}
18118b412664SPeter Zijlstra 	}
1812ce7b0e42SAlexander Shishkin 
18135f9cf599SWang Nan 	trigger_off(&auxtrace_snapshot_trigger);
18143c1cb7e3SWang Nan 	trigger_off(&switch_output_trigger);
18158b412664SPeter Zijlstra 
1816ce7b0e42SAlexander Shishkin 	if (opts->auxtrace_snapshot_on_exit)
1817ce7b0e42SAlexander Shishkin 		record__auxtrace_snapshot_exit(rec);
1818ce7b0e42SAlexander Shishkin 
1819f33cbe72SArnaldo Carvalho de Melo 	if (forks && workload_exec_errno) {
182035550da3SMasami Hiramatsu 		char msg[STRERR_BUFSIZE];
1821c8b5f2c9SArnaldo Carvalho de Melo 		const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
1822f33cbe72SArnaldo Carvalho de Melo 		pr_err("Workload failed: %s\n", emsg);
1823f33cbe72SArnaldo Carvalho de Melo 		err = -1;
182445604710SNamhyung Kim 		goto out_child;
1825f33cbe72SArnaldo Carvalho de Melo 	}
1826f33cbe72SArnaldo Carvalho de Melo 
1827e3d59112SNamhyung Kim 	if (!quiet)
18288b412664SPeter Zijlstra 		fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
182986470930SIngo Molnar 
18304ea648aeSWang Nan 	if (target__none(&rec->opts.target))
18314ea648aeSWang Nan 		record__synthesize_workload(rec, true);
18324ea648aeSWang Nan 
183345604710SNamhyung Kim out_child:
1834470530bbSAlexey Budankov 	record__mmap_read_all(rec, true);
1835d3d1af6fSAlexey Budankov 	record__aio_mmap_read_sync(rec);
1836d3d1af6fSAlexey Budankov 
1837d3c8c08eSAlexey Budankov 	if (rec->session->bytes_transferred && rec->session->bytes_compressed) {
1838d3c8c08eSAlexey Budankov 		ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed;
1839d3c8c08eSAlexey Budankov 		session->header.env.comp_ratio = ratio + 0.5;
1840d3c8c08eSAlexey Budankov 	}
1841d3c8c08eSAlexey Budankov 
184245604710SNamhyung Kim 	if (forks) {
184345604710SNamhyung Kim 		int exit_status;
184445604710SNamhyung Kim 
184545604710SNamhyung Kim 		if (!child_finished)
184645604710SNamhyung Kim 			kill(rec->evlist->workload.pid, SIGTERM);
184745604710SNamhyung Kim 
184845604710SNamhyung Kim 		wait(&exit_status);
184945604710SNamhyung Kim 
185045604710SNamhyung Kim 		if (err < 0)
185145604710SNamhyung Kim 			status = err;
185245604710SNamhyung Kim 		else if (WIFEXITED(exit_status))
185345604710SNamhyung Kim 			status = WEXITSTATUS(exit_status);
185445604710SNamhyung Kim 		else if (WIFSIGNALED(exit_status))
185545604710SNamhyung Kim 			signr = WTERMSIG(exit_status);
185645604710SNamhyung Kim 	} else
185745604710SNamhyung Kim 		status = err;
185845604710SNamhyung Kim 
18594ea648aeSWang Nan 	record__synthesize(rec, true);
1860e3d59112SNamhyung Kim 	/* this will be recalculated during process_buildids() */
1861e3d59112SNamhyung Kim 	rec->samples = 0;
1862e3d59112SNamhyung Kim 
1863ecfd7a9cSWang Nan 	if (!err) {
1864ecfd7a9cSWang Nan 		if (!rec->timestamp_filename) {
1865e1ab48baSWang Nan 			record__finish_output(rec);
1866ecfd7a9cSWang Nan 		} else {
1867ecfd7a9cSWang Nan 			fd = record__switch_output(rec, true);
1868ecfd7a9cSWang Nan 			if (fd < 0) {
1869ecfd7a9cSWang Nan 				status = fd;
1870ecfd7a9cSWang Nan 				goto out_delete_session;
1871ecfd7a9cSWang Nan 			}
1872ecfd7a9cSWang Nan 		}
1873ecfd7a9cSWang Nan 	}
187439d17dacSArnaldo Carvalho de Melo 
1875a074865eSWang Nan 	perf_hooks__invoke_record_end();
1876a074865eSWang Nan 
1877e3d59112SNamhyung Kim 	if (!err && !quiet) {
1878e3d59112SNamhyung Kim 		char samples[128];
1879ecfd7a9cSWang Nan 		const char *postfix = rec->timestamp_filename ?
1880ecfd7a9cSWang Nan 					".<timestamp>" : "";
1881e3d59112SNamhyung Kim 
1882ef149c25SAdrian Hunter 		if (rec->samples && !rec->opts.full_auxtrace)
1883e3d59112SNamhyung Kim 			scnprintf(samples, sizeof(samples),
1884e3d59112SNamhyung Kim 				  " (%" PRIu64 " samples)", rec->samples);
1885e3d59112SNamhyung Kim 		else
1886e3d59112SNamhyung Kim 			samples[0] = '\0';
1887e3d59112SNamhyung Kim 
1888d3c8c08eSAlexey Budankov 		fprintf(stderr,	"[ perf record: Captured and wrote %.3f MB %s%s%s",
18898ceb41d7SJiri Olsa 			perf_data__size(data) / 1024.0 / 1024.0,
18902d4f2799SJiri Olsa 			data->path, postfix, samples);
1891d3c8c08eSAlexey Budankov 		if (ratio) {
1892d3c8c08eSAlexey Budankov 			fprintf(stderr,	", compressed (original %.3f MB, ratio is %.3f)",
1893d3c8c08eSAlexey Budankov 					rec->session->bytes_transferred / 1024.0 / 1024.0,
1894d3c8c08eSAlexey Budankov 					ratio);
1895d3c8c08eSAlexey Budankov 		}
1896d3c8c08eSAlexey Budankov 		fprintf(stderr, " ]\n");
1897e3d59112SNamhyung Kim 	}
1898e3d59112SNamhyung Kim 
189939d17dacSArnaldo Carvalho de Melo out_delete_session:
19005d7f4116SAlexey Budankov 	zstd_fini(&session->zstd_data);
190139d17dacSArnaldo Carvalho de Melo 	perf_session__delete(session);
1902657ee553SSong Liu 
1903657ee553SSong Liu 	if (!opts->no_bpf_event)
1904bc477d79SArnaldo Carvalho de Melo 		perf_evlist__stop_sb_thread(rec->sb_evlist);
190545604710SNamhyung Kim 	return status;
190686470930SIngo Molnar }
190786470930SIngo Molnar 
19080883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain)
190909b0fd45SJiri Olsa {
1910aad2b21cSKan Liang 	static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
1911a601fdffSJiri Olsa 
19120883e820SArnaldo Carvalho de Melo 	pr_debug("callchain: type %s\n", str[callchain->record_mode]);
191326d33022SJiri Olsa 
19140883e820SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_DWARF)
191509b0fd45SJiri Olsa 		pr_debug("callchain: stack dump size %d\n",
19160883e820SArnaldo Carvalho de Melo 			 callchain->dump_size);
19170883e820SArnaldo Carvalho de Melo }
19180883e820SArnaldo Carvalho de Melo 
19190883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record,
19200883e820SArnaldo Carvalho de Melo 				 struct callchain_param *callchain,
19210883e820SArnaldo Carvalho de Melo 				 const char *arg, bool unset)
19220883e820SArnaldo Carvalho de Melo {
19230883e820SArnaldo Carvalho de Melo 	int ret;
19240883e820SArnaldo Carvalho de Melo 	callchain->enabled = !unset;
19250883e820SArnaldo Carvalho de Melo 
19260883e820SArnaldo Carvalho de Melo 	/* --no-call-graph */
19270883e820SArnaldo Carvalho de Melo 	if (unset) {
19280883e820SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_NONE;
19290883e820SArnaldo Carvalho de Melo 		pr_debug("callchain: disabled\n");
19300883e820SArnaldo Carvalho de Melo 		return 0;
19310883e820SArnaldo Carvalho de Melo 	}
19320883e820SArnaldo Carvalho de Melo 
19330883e820SArnaldo Carvalho de Melo 	ret = parse_callchain_record_opt(arg, callchain);
19340883e820SArnaldo Carvalho de Melo 	if (!ret) {
19350883e820SArnaldo Carvalho de Melo 		/* Enable data address sampling for DWARF unwind. */
19360883e820SArnaldo Carvalho de Melo 		if (callchain->record_mode == CALLCHAIN_DWARF)
19370883e820SArnaldo Carvalho de Melo 			record->sample_address = true;
19380883e820SArnaldo Carvalho de Melo 		callchain_debug(callchain);
19390883e820SArnaldo Carvalho de Melo 	}
19400883e820SArnaldo Carvalho de Melo 
19410883e820SArnaldo Carvalho de Melo 	return ret;
194209b0fd45SJiri Olsa }
194309b0fd45SJiri Olsa 
1944c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt,
194509b0fd45SJiri Olsa 			       const char *arg,
194609b0fd45SJiri Olsa 			       int unset)
194709b0fd45SJiri Olsa {
19480883e820SArnaldo Carvalho de Melo 	return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
194926d33022SJiri Olsa }
195026d33022SJiri Olsa 
1951c421e80bSKan Liang int record_callchain_opt(const struct option *opt,
195209b0fd45SJiri Olsa 			 const char *arg __maybe_unused,
195309b0fd45SJiri Olsa 			 int unset __maybe_unused)
195409b0fd45SJiri Olsa {
19552ddd5c04SArnaldo Carvalho de Melo 	struct callchain_param *callchain = opt->value;
1956c421e80bSKan Liang 
19572ddd5c04SArnaldo Carvalho de Melo 	callchain->enabled = true;
195809b0fd45SJiri Olsa 
19592ddd5c04SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_NONE)
19602ddd5c04SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_FP;
1961eb853e80SJiri Olsa 
19622ddd5c04SArnaldo Carvalho de Melo 	callchain_debug(callchain);
196309b0fd45SJiri Olsa 	return 0;
196409b0fd45SJiri Olsa }
196509b0fd45SJiri Olsa 
1966eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb)
1967eb853e80SJiri Olsa {
19687a29c087SNamhyung Kim 	struct record *rec = cb;
19697a29c087SNamhyung Kim 
19707a29c087SNamhyung Kim 	if (!strcmp(var, "record.build-id")) {
19717a29c087SNamhyung Kim 		if (!strcmp(value, "cache"))
19727a29c087SNamhyung Kim 			rec->no_buildid_cache = false;
19737a29c087SNamhyung Kim 		else if (!strcmp(value, "no-cache"))
19747a29c087SNamhyung Kim 			rec->no_buildid_cache = true;
19757a29c087SNamhyung Kim 		else if (!strcmp(value, "skip"))
19767a29c087SNamhyung Kim 			rec->no_buildid = true;
19777a29c087SNamhyung Kim 		else
19787a29c087SNamhyung Kim 			return -1;
19797a29c087SNamhyung Kim 		return 0;
19807a29c087SNamhyung Kim 	}
1981cff17205SYisheng Xie 	if (!strcmp(var, "record.call-graph")) {
1982cff17205SYisheng Xie 		var = "call-graph.record-mode";
1983eb853e80SJiri Olsa 		return perf_default_config(var, value, cb);
1984eb853e80SJiri Olsa 	}
198593f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
198693f20c0fSAlexey Budankov 	if (!strcmp(var, "record.aio")) {
198793f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = strtol(value, NULL, 0);
198893f20c0fSAlexey Budankov 		if (!rec->opts.nr_cblocks)
198993f20c0fSAlexey Budankov 			rec->opts.nr_cblocks = nr_cblocks_default;
199093f20c0fSAlexey Budankov 	}
199193f20c0fSAlexey Budankov #endif
1992eb853e80SJiri Olsa 
1993cff17205SYisheng Xie 	return 0;
1994cff17205SYisheng Xie }
1995cff17205SYisheng Xie 
1996814c8c38SPeter Zijlstra struct clockid_map {
1997814c8c38SPeter Zijlstra 	const char *name;
1998814c8c38SPeter Zijlstra 	int clockid;
1999814c8c38SPeter Zijlstra };
2000814c8c38SPeter Zijlstra 
2001814c8c38SPeter Zijlstra #define CLOCKID_MAP(n, c)	\
2002814c8c38SPeter Zijlstra 	{ .name = n, .clockid = (c), }
2003814c8c38SPeter Zijlstra 
2004814c8c38SPeter Zijlstra #define CLOCKID_END	{ .name = NULL, }
2005814c8c38SPeter Zijlstra 
2006814c8c38SPeter Zijlstra 
2007814c8c38SPeter Zijlstra /*
2008814c8c38SPeter Zijlstra  * Add the missing ones, we need to build on many distros...
2009814c8c38SPeter Zijlstra  */
2010814c8c38SPeter Zijlstra #ifndef CLOCK_MONOTONIC_RAW
2011814c8c38SPeter Zijlstra #define CLOCK_MONOTONIC_RAW 4
2012814c8c38SPeter Zijlstra #endif
2013814c8c38SPeter Zijlstra #ifndef CLOCK_BOOTTIME
2014814c8c38SPeter Zijlstra #define CLOCK_BOOTTIME 7
2015814c8c38SPeter Zijlstra #endif
2016814c8c38SPeter Zijlstra #ifndef CLOCK_TAI
2017814c8c38SPeter Zijlstra #define CLOCK_TAI 11
2018814c8c38SPeter Zijlstra #endif
2019814c8c38SPeter Zijlstra 
2020814c8c38SPeter Zijlstra static const struct clockid_map clockids[] = {
2021814c8c38SPeter Zijlstra 	/* available for all events, NMI safe */
2022814c8c38SPeter Zijlstra 	CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
2023814c8c38SPeter Zijlstra 	CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
2024814c8c38SPeter Zijlstra 
2025814c8c38SPeter Zijlstra 	/* available for some events */
2026814c8c38SPeter Zijlstra 	CLOCKID_MAP("realtime", CLOCK_REALTIME),
2027814c8c38SPeter Zijlstra 	CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
2028814c8c38SPeter Zijlstra 	CLOCKID_MAP("tai", CLOCK_TAI),
2029814c8c38SPeter Zijlstra 
2030814c8c38SPeter Zijlstra 	/* available for the lazy */
2031814c8c38SPeter Zijlstra 	CLOCKID_MAP("mono", CLOCK_MONOTONIC),
2032814c8c38SPeter Zijlstra 	CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
2033814c8c38SPeter Zijlstra 	CLOCKID_MAP("real", CLOCK_REALTIME),
2034814c8c38SPeter Zijlstra 	CLOCKID_MAP("boot", CLOCK_BOOTTIME),
2035814c8c38SPeter Zijlstra 
2036814c8c38SPeter Zijlstra 	CLOCKID_END,
2037814c8c38SPeter Zijlstra };
2038814c8c38SPeter Zijlstra 
2039cf790516SAlexey Budankov static int get_clockid_res(clockid_t clk_id, u64 *res_ns)
2040cf790516SAlexey Budankov {
2041cf790516SAlexey Budankov 	struct timespec res;
2042cf790516SAlexey Budankov 
2043cf790516SAlexey Budankov 	*res_ns = 0;
2044cf790516SAlexey Budankov 	if (!clock_getres(clk_id, &res))
2045cf790516SAlexey Budankov 		*res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC;
2046cf790516SAlexey Budankov 	else
2047cf790516SAlexey Budankov 		pr_warning("WARNING: Failed to determine specified clock resolution.\n");
2048cf790516SAlexey Budankov 
2049cf790516SAlexey Budankov 	return 0;
2050cf790516SAlexey Budankov }
2051cf790516SAlexey Budankov 
2052814c8c38SPeter Zijlstra static int parse_clockid(const struct option *opt, const char *str, int unset)
2053814c8c38SPeter Zijlstra {
2054814c8c38SPeter Zijlstra 	struct record_opts *opts = (struct record_opts *)opt->value;
2055814c8c38SPeter Zijlstra 	const struct clockid_map *cm;
2056814c8c38SPeter Zijlstra 	const char *ostr = str;
2057814c8c38SPeter Zijlstra 
2058814c8c38SPeter Zijlstra 	if (unset) {
2059814c8c38SPeter Zijlstra 		opts->use_clockid = 0;
2060814c8c38SPeter Zijlstra 		return 0;
2061814c8c38SPeter Zijlstra 	}
2062814c8c38SPeter Zijlstra 
2063814c8c38SPeter Zijlstra 	/* no arg passed */
2064814c8c38SPeter Zijlstra 	if (!str)
2065814c8c38SPeter Zijlstra 		return 0;
2066814c8c38SPeter Zijlstra 
2067814c8c38SPeter Zijlstra 	/* no setting it twice */
2068814c8c38SPeter Zijlstra 	if (opts->use_clockid)
2069814c8c38SPeter Zijlstra 		return -1;
2070814c8c38SPeter Zijlstra 
2071814c8c38SPeter Zijlstra 	opts->use_clockid = true;
2072814c8c38SPeter Zijlstra 
2073814c8c38SPeter Zijlstra 	/* if its a number, we're done */
2074814c8c38SPeter Zijlstra 	if (sscanf(str, "%d", &opts->clockid) == 1)
2075cf790516SAlexey Budankov 		return get_clockid_res(opts->clockid, &opts->clockid_res_ns);
2076814c8c38SPeter Zijlstra 
2077814c8c38SPeter Zijlstra 	/* allow a "CLOCK_" prefix to the name */
2078814c8c38SPeter Zijlstra 	if (!strncasecmp(str, "CLOCK_", 6))
2079814c8c38SPeter Zijlstra 		str += 6;
2080814c8c38SPeter Zijlstra 
2081814c8c38SPeter Zijlstra 	for (cm = clockids; cm->name; cm++) {
2082814c8c38SPeter Zijlstra 		if (!strcasecmp(str, cm->name)) {
2083814c8c38SPeter Zijlstra 			opts->clockid = cm->clockid;
2084cf790516SAlexey Budankov 			return get_clockid_res(opts->clockid,
2085cf790516SAlexey Budankov 					       &opts->clockid_res_ns);
2086814c8c38SPeter Zijlstra 		}
2087814c8c38SPeter Zijlstra 	}
2088814c8c38SPeter Zijlstra 
2089814c8c38SPeter Zijlstra 	opts->use_clockid = false;
2090814c8c38SPeter Zijlstra 	ui__warning("unknown clockid %s, check man page\n", ostr);
2091814c8c38SPeter Zijlstra 	return -1;
2092814c8c38SPeter Zijlstra }
2093814c8c38SPeter Zijlstra 
2094f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset)
2095f4fe11b7SAlexey Budankov {
2096f4fe11b7SAlexey Budankov 	struct record_opts *opts = (struct record_opts *)opt->value;
2097f4fe11b7SAlexey Budankov 
2098f4fe11b7SAlexey Budankov 	if (unset || !str)
2099f4fe11b7SAlexey Budankov 		return 0;
2100f4fe11b7SAlexey Budankov 
2101f4fe11b7SAlexey Budankov 	if (!strcasecmp(str, "node"))
2102f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_NODE;
2103f4fe11b7SAlexey Budankov 	else if (!strcasecmp(str, "cpu"))
2104f4fe11b7SAlexey Budankov 		opts->affinity = PERF_AFFINITY_CPU;
2105f4fe11b7SAlexey Budankov 
2106f4fe11b7SAlexey Budankov 	return 0;
2107f4fe11b7SAlexey Budankov }
2108f4fe11b7SAlexey Budankov 
21096d575816SJiwei Sun static int parse_output_max_size(const struct option *opt,
21106d575816SJiwei Sun 				 const char *str, int unset)
21116d575816SJiwei Sun {
21126d575816SJiwei Sun 	unsigned long *s = (unsigned long *)opt->value;
21136d575816SJiwei Sun 	static struct parse_tag tags_size[] = {
21146d575816SJiwei Sun 		{ .tag  = 'B', .mult = 1       },
21156d575816SJiwei Sun 		{ .tag  = 'K', .mult = 1 << 10 },
21166d575816SJiwei Sun 		{ .tag  = 'M', .mult = 1 << 20 },
21176d575816SJiwei Sun 		{ .tag  = 'G', .mult = 1 << 30 },
21186d575816SJiwei Sun 		{ .tag  = 0 },
21196d575816SJiwei Sun 	};
21206d575816SJiwei Sun 	unsigned long val;
21216d575816SJiwei Sun 
21226d575816SJiwei Sun 	if (unset) {
21236d575816SJiwei Sun 		*s = 0;
21246d575816SJiwei Sun 		return 0;
21256d575816SJiwei Sun 	}
21266d575816SJiwei Sun 
21276d575816SJiwei Sun 	val = parse_tag_value(str, tags_size);
21286d575816SJiwei Sun 	if (val != (unsigned long) -1) {
21296d575816SJiwei Sun 		*s = val;
21306d575816SJiwei Sun 		return 0;
21316d575816SJiwei Sun 	}
21326d575816SJiwei Sun 
21336d575816SJiwei Sun 	return -1;
21346d575816SJiwei Sun }
21356d575816SJiwei Sun 
2136e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt,
2137e9db1310SAdrian Hunter 				    const char *str,
2138e9db1310SAdrian Hunter 				    int unset __maybe_unused)
2139e9db1310SAdrian Hunter {
2140e9db1310SAdrian Hunter 	struct record_opts *opts = opt->value;
2141e9db1310SAdrian Hunter 	char *s, *p;
2142e9db1310SAdrian Hunter 	unsigned int mmap_pages;
2143e9db1310SAdrian Hunter 	int ret;
2144e9db1310SAdrian Hunter 
2145e9db1310SAdrian Hunter 	if (!str)
2146e9db1310SAdrian Hunter 		return -EINVAL;
2147e9db1310SAdrian Hunter 
2148e9db1310SAdrian Hunter 	s = strdup(str);
2149e9db1310SAdrian Hunter 	if (!s)
2150e9db1310SAdrian Hunter 		return -ENOMEM;
2151e9db1310SAdrian Hunter 
2152e9db1310SAdrian Hunter 	p = strchr(s, ',');
2153e9db1310SAdrian Hunter 	if (p)
2154e9db1310SAdrian Hunter 		*p = '\0';
2155e9db1310SAdrian Hunter 
2156e9db1310SAdrian Hunter 	if (*s) {
2157e9db1310SAdrian Hunter 		ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s);
2158e9db1310SAdrian Hunter 		if (ret)
2159e9db1310SAdrian Hunter 			goto out_free;
2160e9db1310SAdrian Hunter 		opts->mmap_pages = mmap_pages;
2161e9db1310SAdrian Hunter 	}
2162e9db1310SAdrian Hunter 
2163e9db1310SAdrian Hunter 	if (!p) {
2164e9db1310SAdrian Hunter 		ret = 0;
2165e9db1310SAdrian Hunter 		goto out_free;
2166e9db1310SAdrian Hunter 	}
2167e9db1310SAdrian Hunter 
2168e9db1310SAdrian Hunter 	ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1);
2169e9db1310SAdrian Hunter 	if (ret)
2170e9db1310SAdrian Hunter 		goto out_free;
2171e9db1310SAdrian Hunter 
2172e9db1310SAdrian Hunter 	opts->auxtrace_mmap_pages = mmap_pages;
2173e9db1310SAdrian Hunter 
2174e9db1310SAdrian Hunter out_free:
2175e9db1310SAdrian Hunter 	free(s);
2176e9db1310SAdrian Hunter 	return ret;
2177e9db1310SAdrian Hunter }
2178e9db1310SAdrian Hunter 
21790c582449SJiri Olsa static void switch_output_size_warn(struct record *rec)
21800c582449SJiri Olsa {
21819521b5f2SJiri Olsa 	u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages);
21820c582449SJiri Olsa 	struct switch_output *s = &rec->switch_output;
21830c582449SJiri Olsa 
21840c582449SJiri Olsa 	wakeup_size /= 2;
21850c582449SJiri Olsa 
21860c582449SJiri Olsa 	if (s->size < wakeup_size) {
21870c582449SJiri Olsa 		char buf[100];
21880c582449SJiri Olsa 
21890c582449SJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
21900c582449SJiri Olsa 		pr_warning("WARNING: switch-output data size lower than "
21910c582449SJiri Olsa 			   "wakeup kernel buffer size (%s) "
21920c582449SJiri Olsa 			   "expect bigger perf.data sizes\n", buf);
21930c582449SJiri Olsa 	}
21940c582449SJiri Olsa }
21950c582449SJiri Olsa 
2196cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec)
2197cb4e1ebbSJiri Olsa {
2198cb4e1ebbSJiri Olsa 	struct switch_output *s = &rec->switch_output;
2199dc0c6127SJiri Olsa 	static struct parse_tag tags_size[] = {
2200dc0c6127SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
2201dc0c6127SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
2202dc0c6127SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
2203dc0c6127SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
2204dc0c6127SJiri Olsa 		{ .tag  = 0 },
2205dc0c6127SJiri Olsa 	};
2206bfacbe3bSJiri Olsa 	static struct parse_tag tags_time[] = {
2207bfacbe3bSJiri Olsa 		{ .tag  = 's', .mult = 1        },
2208bfacbe3bSJiri Olsa 		{ .tag  = 'm', .mult = 60       },
2209bfacbe3bSJiri Olsa 		{ .tag  = 'h', .mult = 60*60    },
2210bfacbe3bSJiri Olsa 		{ .tag  = 'd', .mult = 60*60*24 },
2211bfacbe3bSJiri Olsa 		{ .tag  = 0 },
2212bfacbe3bSJiri Olsa 	};
2213dc0c6127SJiri Olsa 	unsigned long val;
2214cb4e1ebbSJiri Olsa 
2215899e5ffbSArnaldo Carvalho de Melo 	/*
2216899e5ffbSArnaldo Carvalho de Melo 	 * If we're using --switch-output-events, then we imply its
2217899e5ffbSArnaldo Carvalho de Melo 	 * --switch-output=signal, as we'll send a SIGUSR2 from the side band
2218899e5ffbSArnaldo Carvalho de Melo 	 *  thread to its parent.
2219899e5ffbSArnaldo Carvalho de Melo 	 */
2220899e5ffbSArnaldo Carvalho de Melo 	if (rec->switch_output_event_set)
2221899e5ffbSArnaldo Carvalho de Melo 		goto do_signal;
2222899e5ffbSArnaldo Carvalho de Melo 
2223cb4e1ebbSJiri Olsa 	if (!s->set)
2224cb4e1ebbSJiri Olsa 		return 0;
2225cb4e1ebbSJiri Olsa 
2226cb4e1ebbSJiri Olsa 	if (!strcmp(s->str, "signal")) {
2227899e5ffbSArnaldo Carvalho de Melo do_signal:
2228cb4e1ebbSJiri Olsa 		s->signal = true;
2229cb4e1ebbSJiri Olsa 		pr_debug("switch-output with SIGUSR2 signal\n");
2230dc0c6127SJiri Olsa 		goto enabled;
2231dc0c6127SJiri Olsa 	}
2232dc0c6127SJiri Olsa 
2233dc0c6127SJiri Olsa 	val = parse_tag_value(s->str, tags_size);
2234dc0c6127SJiri Olsa 	if (val != (unsigned long) -1) {
2235dc0c6127SJiri Olsa 		s->size = val;
2236dc0c6127SJiri Olsa 		pr_debug("switch-output with %s size threshold\n", s->str);
2237dc0c6127SJiri Olsa 		goto enabled;
2238cb4e1ebbSJiri Olsa 	}
2239cb4e1ebbSJiri Olsa 
2240bfacbe3bSJiri Olsa 	val = parse_tag_value(s->str, tags_time);
2241bfacbe3bSJiri Olsa 	if (val != (unsigned long) -1) {
2242bfacbe3bSJiri Olsa 		s->time = val;
2243bfacbe3bSJiri Olsa 		pr_debug("switch-output with %s time threshold (%lu seconds)\n",
2244bfacbe3bSJiri Olsa 			 s->str, s->time);
2245bfacbe3bSJiri Olsa 		goto enabled;
2246bfacbe3bSJiri Olsa 	}
2247bfacbe3bSJiri Olsa 
2248cb4e1ebbSJiri Olsa 	return -1;
2249dc0c6127SJiri Olsa 
2250dc0c6127SJiri Olsa enabled:
2251dc0c6127SJiri Olsa 	rec->timestamp_filename = true;
2252dc0c6127SJiri Olsa 	s->enabled              = true;
22530c582449SJiri Olsa 
22540c582449SJiri Olsa 	if (s->size && !rec->opts.no_buffering)
22550c582449SJiri Olsa 		switch_output_size_warn(rec);
22560c582449SJiri Olsa 
2257dc0c6127SJiri Olsa 	return 0;
2258cb4e1ebbSJiri Olsa }
2259cb4e1ebbSJiri Olsa 
2260e5b2c207SNamhyung Kim static const char * const __record_usage[] = {
226186470930SIngo Molnar 	"perf record [<options>] [<command>]",
226286470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
226386470930SIngo Molnar 	NULL
226486470930SIngo Molnar };
2265e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage;
226686470930SIngo Molnar 
22676e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event,
22686e0a9b3dSArnaldo Carvalho de Melo 				  struct perf_sample *sample, struct machine *machine)
22696e0a9b3dSArnaldo Carvalho de Melo {
22706e0a9b3dSArnaldo Carvalho de Melo 	/*
22716e0a9b3dSArnaldo Carvalho de Melo 	 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
22726e0a9b3dSArnaldo Carvalho de Melo 	 * no need to add them twice.
22736e0a9b3dSArnaldo Carvalho de Melo 	 */
22746e0a9b3dSArnaldo Carvalho de Melo 	if (!(event->header.misc & PERF_RECORD_MISC_USER))
22756e0a9b3dSArnaldo Carvalho de Melo 		return 0;
22766e0a9b3dSArnaldo Carvalho de Melo 	return perf_event__process_mmap(tool, event, sample, machine);
22776e0a9b3dSArnaldo Carvalho de Melo }
22786e0a9b3dSArnaldo Carvalho de Melo 
22796e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event,
22806e0a9b3dSArnaldo Carvalho de Melo 				   struct perf_sample *sample, struct machine *machine)
22816e0a9b3dSArnaldo Carvalho de Melo {
22826e0a9b3dSArnaldo Carvalho de Melo 	/*
22836e0a9b3dSArnaldo Carvalho de Melo 	 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
22846e0a9b3dSArnaldo Carvalho de Melo 	 * no need to add them twice.
22856e0a9b3dSArnaldo Carvalho de Melo 	 */
22866e0a9b3dSArnaldo Carvalho de Melo 	if (!(event->header.misc & PERF_RECORD_MISC_USER))
22876e0a9b3dSArnaldo Carvalho de Melo 		return 0;
22886e0a9b3dSArnaldo Carvalho de Melo 
22896e0a9b3dSArnaldo Carvalho de Melo 	return perf_event__process_mmap2(tool, event, sample, machine);
22906e0a9b3dSArnaldo Carvalho de Melo }
22916e0a9b3dSArnaldo Carvalho de Melo 
2292d20deb64SArnaldo Carvalho de Melo /*
22938c6f45a7SArnaldo Carvalho de Melo  * XXX Ideally would be local to cmd_record() and passed to a record__new
22948c6f45a7SArnaldo Carvalho de Melo  * because we need to have access to it in record__exit, that is called
2295d20deb64SArnaldo Carvalho de Melo  * after cmd_record() exits, but since record_options need to be accessible to
2296d20deb64SArnaldo Carvalho de Melo  * builtin-script, leave it here.
2297d20deb64SArnaldo Carvalho de Melo  *
2298d20deb64SArnaldo Carvalho de Melo  * At least we don't ouch it in all the other functions here directly.
2299d20deb64SArnaldo Carvalho de Melo  *
2300d20deb64SArnaldo Carvalho de Melo  * Just say no to tons of global variables, sigh.
2301d20deb64SArnaldo Carvalho de Melo  */
23028c6f45a7SArnaldo Carvalho de Melo static struct record record = {
2303d20deb64SArnaldo Carvalho de Melo 	.opts = {
23048affc2b8SAndi Kleen 		.sample_time	     = true,
2305d20deb64SArnaldo Carvalho de Melo 		.mmap_pages	     = UINT_MAX,
2306d20deb64SArnaldo Carvalho de Melo 		.user_freq	     = UINT_MAX,
2307d20deb64SArnaldo Carvalho de Melo 		.user_interval	     = ULLONG_MAX,
2308447a6013SArnaldo Carvalho de Melo 		.freq		     = 4000,
2309d1cb9fceSNamhyung Kim 		.target		     = {
2310d1cb9fceSNamhyung Kim 			.uses_mmap   = true,
23113aa5939dSAdrian Hunter 			.default_per_cpu = true,
2312d1cb9fceSNamhyung Kim 		},
2313470530bbSAlexey Budankov 		.mmap_flush          = MMAP_FLUSH_DEFAULT,
2314d99c22eaSStephane Eranian 		.nr_threads_synthesize = 1,
2315d20deb64SArnaldo Carvalho de Melo 	},
2316e3d59112SNamhyung Kim 	.tool = {
2317e3d59112SNamhyung Kim 		.sample		= process_sample_event,
2318e3d59112SNamhyung Kim 		.fork		= perf_event__process_fork,
2319cca8482cSAdrian Hunter 		.exit		= perf_event__process_exit,
2320e3d59112SNamhyung Kim 		.comm		= perf_event__process_comm,
2321f3b3614aSHari Bathini 		.namespaces	= perf_event__process_namespaces,
23226e0a9b3dSArnaldo Carvalho de Melo 		.mmap		= build_id__process_mmap,
23236e0a9b3dSArnaldo Carvalho de Melo 		.mmap2		= build_id__process_mmap2,
2324cca8482cSAdrian Hunter 		.ordered_events	= true,
2325e3d59112SNamhyung Kim 	},
2326d20deb64SArnaldo Carvalho de Melo };
23277865e817SFrederic Weisbecker 
232876a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
232976a26549SNamhyung Kim 	"\n\t\t\t\tDefault: fp";
233061eaa3beSArnaldo Carvalho de Melo 
23310aab2136SWang Nan static bool dry_run;
23320aab2136SWang Nan 
2333d20deb64SArnaldo Carvalho de Melo /*
2334d20deb64SArnaldo Carvalho de Melo  * XXX Will stay a global variable till we fix builtin-script.c to stop messing
2335d20deb64SArnaldo Carvalho de Melo  * with it and switch to use the library functions in perf_evlist that came
2336b4006796SArnaldo Carvalho de Melo  * from builtin-record.c, i.e. use record_opts,
2337d20deb64SArnaldo Carvalho de Melo  * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
2338d20deb64SArnaldo Carvalho de Melo  * using pipes, etc.
2339d20deb64SArnaldo Carvalho de Melo  */
2340efd21307SJiri Olsa static struct option __record_options[] = {
2341d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &record.evlist, "event",
234286470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
2343f120f9d5SJiri Olsa 		     parse_events_option),
2344d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK(0, "filter", &record.evlist, "filter",
2345c171b552SLi Zefan 		     "event filter", parse_filter),
23464ba1faa1SWang Nan 	OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
23474ba1faa1SWang Nan 			   NULL, "don't record events from perf itself",
23484ba1faa1SWang Nan 			   exclude_perf),
2349bea03405SNamhyung Kim 	OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
2350d6d901c2SZhang, Yanmin 		    "record events on existing process id"),
2351bea03405SNamhyung Kim 	OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
2352d6d901c2SZhang, Yanmin 		    "record events on existing thread id"),
2353d20deb64SArnaldo Carvalho de Melo 	OPT_INTEGER('r', "realtime", &record.realtime_prio,
235486470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
2355509051eaSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
2356acac03faSKirill Smelkov 		    "collect data without buffering"),
2357d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
2358daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
2359bea03405SNamhyung Kim 	OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
236086470930SIngo Molnar 			    "system-wide collection from all CPUs"),
2361bea03405SNamhyung Kim 	OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
2362c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
2363d20deb64SArnaldo Carvalho de Melo 	OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
23642d4f2799SJiri Olsa 	OPT_STRING('o', "output", &record.data.path, "file",
236586470930SIngo Molnar 		    "output file name"),
236669e7e5b0SAdrian Hunter 	OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
236769e7e5b0SAdrian Hunter 			&record.opts.no_inherit_set,
23682e6cdf99SStephane Eranian 			"child tasks do not inherit counters"),
23694ea648aeSWang Nan 	OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
23704ea648aeSWang Nan 		    "synthesize non-sample events at the end of output"),
2371626a6b78SWang Nan 	OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
237271184c6aSSong Liu 	OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "record bpf events"),
2373b09c2364SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq,
2374b09c2364SArnaldo Carvalho de Melo 		    "Fail if the specified frequency can't be used"),
237567230479SArnaldo Carvalho de Melo 	OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'",
237667230479SArnaldo Carvalho de Melo 		     "profile at this frequency",
237767230479SArnaldo Carvalho de Melo 		      record__parse_freq),
2378e9db1310SAdrian Hunter 	OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
2379e9db1310SAdrian Hunter 		     "number of mmap data pages and AUX area tracing mmap pages",
2380e9db1310SAdrian Hunter 		     record__parse_mmap_pages),
2381470530bbSAlexey Budankov 	OPT_CALLBACK(0, "mmap-flush", &record.opts, "number",
2382470530bbSAlexey Budankov 		     "Minimal number of bytes that is extracted from mmap data pages (default: 1)",
2383470530bbSAlexey Budankov 		     record__mmap_flush_parse),
2384d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "group", &record.opts.group,
238543bece79SLin Ming 		    "put the counters into a counter group"),
23862ddd5c04SArnaldo Carvalho de Melo 	OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
238709b0fd45SJiri Olsa 			   NULL, "enables call-graph recording" ,
238809b0fd45SJiri Olsa 			   &record_callchain_opt),
238909b0fd45SJiri Olsa 	OPT_CALLBACK(0, "call-graph", &record.opts,
239076a26549SNamhyung Kim 		     "record_mode[,record_size]", record_callchain_help,
239109b0fd45SJiri Olsa 		     &record_parse_callchain_opt),
2392c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
23933da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
2394b44308f5SArnaldo Carvalho de Melo 	OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
2395d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
2396649c48a9SPeter Zijlstra 		    "per thread counts"),
239756100321SPeter Zijlstra 	OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
23983b0a5daaSKan Liang 	OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr,
23993b0a5daaSKan Liang 		    "Record the sample physical addresses"),
2400b6f35ed7SJiri Olsa 	OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
24013abebc55SAdrian Hunter 	OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
24023abebc55SAdrian Hunter 			&record.opts.sample_time_set,
24033abebc55SAdrian Hunter 			"Record the sample timestamps"),
2404f290aa1fSJiri Olsa 	OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set,
2405f290aa1fSJiri Olsa 			"Record the sample period"),
2406d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
2407649c48a9SPeter Zijlstra 		    "don't sample"),
2408d2db9a98SWang Nan 	OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
2409d2db9a98SWang Nan 			&record.no_buildid_cache_set,
2410a1ac1d3cSStephane Eranian 			"do not update the buildid cache"),
2411d2db9a98SWang Nan 	OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
2412d2db9a98SWang Nan 			&record.no_buildid_set,
2413baa2f6ceSArnaldo Carvalho de Melo 			"do not collect buildids in perf.data"),
2414d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
2415023695d9SStephane Eranian 		     "monitor event in cgroup name only",
2416023695d9SStephane Eranian 		     parse_cgroups),
2417a6205a35SArnaldo Carvalho de Melo 	OPT_UINTEGER('D', "delay", &record.opts.initial_delay,
24186619a53eSAndi Kleen 		  "ms to wait before starting measurement after program start"),
2419eeb399b5SAdrian Hunter 	OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"),
2420bea03405SNamhyung Kim 	OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
2421bea03405SNamhyung Kim 		   "user to profile"),
2422a5aabdacSStephane Eranian 
2423a5aabdacSStephane Eranian 	OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
2424a5aabdacSStephane Eranian 		     "branch any", "sample any taken branches",
2425a5aabdacSStephane Eranian 		     parse_branch_stack),
2426a5aabdacSStephane Eranian 
2427a5aabdacSStephane Eranian 	OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
2428a5aabdacSStephane Eranian 		     "branch filter mask", "branch stack filter modes",
2429bdfebd84SRoberto Agostino Vitillo 		     parse_branch_stack),
243005484298SAndi Kleen 	OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
243105484298SAndi Kleen 		    "sample by weight (on special events only)"),
2432475eeab9SAndi Kleen 	OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
2433475eeab9SAndi Kleen 		    "sample transaction flags (special events only)"),
24343aa5939dSAdrian Hunter 	OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
24353aa5939dSAdrian Hunter 		    "use per-thread mmaps"),
2436bcc84ec6SStephane Eranian 	OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
2437bcc84ec6SStephane Eranian 		    "sample selected machine registers on interrupt,"
2438aeea9062SKan Liang 		    " use '-I?' to list register names", parse_intr_regs),
243984c41742SAndi Kleen 	OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register",
244084c41742SAndi Kleen 		    "sample selected machine registers on interrupt,"
2441aeea9062SKan Liang 		    " use '--user-regs=?' to list register names", parse_user_regs),
244285c273d2SAndi Kleen 	OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
244385c273d2SAndi Kleen 		    "Record running/enabled time of read (:S) events"),
2444814c8c38SPeter Zijlstra 	OPT_CALLBACK('k', "clockid", &record.opts,
2445814c8c38SPeter Zijlstra 	"clockid", "clockid to use for events, see clock_gettime()",
2446814c8c38SPeter Zijlstra 	parse_clockid),
24472dd6d8a1SAdrian Hunter 	OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
24482dd6d8a1SAdrian Hunter 			  "opts", "AUX area tracing Snapshot Mode", ""),
2449c0a6de06SAdrian Hunter 	OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts,
2450c0a6de06SAdrian Hunter 			  "opts", "sample AUX area", ""),
24513fcb10e4SMark Drayton 	OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
24529d9cad76SKan Liang 			"per thread proc mmap processing timeout in ms"),
2453f3b3614aSHari Bathini 	OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces,
2454f3b3614aSHari Bathini 		    "Record namespaces events"),
24558fb4b679SNamhyung Kim 	OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup,
24568fb4b679SNamhyung Kim 		    "Record cgroup events"),
2457b757bb09SAdrian Hunter 	OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events,
2458b757bb09SAdrian Hunter 		    "Record context switch events"),
245985723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
246085723885SJiri Olsa 			 "Configure all used events to run in kernel space.",
246185723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
246285723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
246385723885SJiri Olsa 			 "Configure all used events to run in user space.",
246485723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
246553651b28Syuzhoujian 	OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains,
246653651b28Syuzhoujian 		    "collect kernel callchains"),
246753651b28Syuzhoujian 	OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains,
246853651b28Syuzhoujian 		    "collect user callchains"),
246971dc2326SWang Nan 	OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
247071dc2326SWang Nan 		   "clang binary to use for compiling BPF scriptlets"),
247171dc2326SWang Nan 	OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
247271dc2326SWang Nan 		   "options passed to clang when compiling BPF scriptlets"),
24737efe0e03SHe Kuang 	OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
24747efe0e03SHe Kuang 		   "file", "vmlinux pathname"),
24756156681bSNamhyung Kim 	OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
24766156681bSNamhyung Kim 		    "Record build-id of all DSOs regardless of hits"),
2477ecfd7a9cSWang Nan 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
2478ecfd7a9cSWang Nan 		    "append timestamp to output filename"),
247968588bafSJin Yao 	OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary,
248068588bafSJin Yao 		    "Record timestamp boundary (time of first/last samples)"),
2481cb4e1ebbSJiri Olsa 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
2482c38dab7dSAndi Kleen 			  &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
2483c38dab7dSAndi Kleen 			  "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
2484dc0c6127SJiri Olsa 			  "signal"),
2485899e5ffbSArnaldo Carvalho de Melo 	OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event",
2486899e5ffbSArnaldo Carvalho de Melo 			 "switch output event selector. use 'perf list' to list available events",
2487899e5ffbSArnaldo Carvalho de Melo 			 parse_events_option_new_evlist),
248803724b2eSAndi Kleen 	OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
248903724b2eSAndi Kleen 		   "Limit number of switch output generated files"),
24900aab2136SWang Nan 	OPT_BOOLEAN(0, "dry-run", &dry_run,
24910aab2136SWang Nan 		    "Parse options then exit"),
2492d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT
249393f20c0fSAlexey Budankov 	OPT_CALLBACK_OPTARG(0, "aio", &record.opts,
249493f20c0fSAlexey Budankov 		     &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
2495d3d1af6fSAlexey Budankov 		     record__aio_parse),
2496d3d1af6fSAlexey Budankov #endif
2497f4fe11b7SAlexey Budankov 	OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
2498f4fe11b7SAlexey Budankov 		     "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
2499f4fe11b7SAlexey Budankov 		     record__parse_affinity),
2500504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT
2501504c1ad1SAlexey Budankov 	OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default,
2502504c1ad1SAlexey Budankov 			    "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
2503504c1ad1SAlexey Budankov 			    record__parse_comp_level),
2504504c1ad1SAlexey Budankov #endif
25056d575816SJiwei Sun 	OPT_CALLBACK(0, "max-size", &record.output_max_size,
25066d575816SJiwei Sun 		     "size", "Limit the maximum size of the output file", parse_output_max_size),
2507d99c22eaSStephane Eranian 	OPT_UINTEGER(0, "num-thread-synthesize",
2508d99c22eaSStephane Eranian 		     &record.opts.nr_threads_synthesize,
2509d99c22eaSStephane Eranian 		     "number of threads to run for event synthesis"),
251086470930SIngo Molnar 	OPT_END()
251186470930SIngo Molnar };
251286470930SIngo Molnar 
2513e5b2c207SNamhyung Kim struct option *record_options = __record_options;
2514e5b2c207SNamhyung Kim 
2515b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv)
251686470930SIngo Molnar {
2517ef149c25SAdrian Hunter 	int err;
25188c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = &record;
251916ad2ffbSNamhyung Kim 	char errbuf[BUFSIZ];
252086470930SIngo Molnar 
252167230479SArnaldo Carvalho de Melo 	setlocale(LC_ALL, "");
252267230479SArnaldo Carvalho de Melo 
252348e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT
252448e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
252548e1cab1SWang Nan 	set_nobuild('\0', "clang-path", true);
252648e1cab1SWang Nan 	set_nobuild('\0', "clang-opt", true);
252748e1cab1SWang Nan # undef set_nobuild
252848e1cab1SWang Nan #endif
252948e1cab1SWang Nan 
25307efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE
25317efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT)
25327efe0e03SHe Kuang #  define REASON  "NO_DWARF=1"
25337efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT)
25347efe0e03SHe Kuang #  define REASON  "NO_LIBBPF=1"
25357efe0e03SHe Kuang # else
25367efe0e03SHe Kuang #  define REASON  "this architecture doesn't support BPF prologue"
25377efe0e03SHe Kuang # endif
25387efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
25397efe0e03SHe Kuang 	set_nobuild('\0', "vmlinux", true);
25407efe0e03SHe Kuang # undef set_nobuild
25417efe0e03SHe Kuang # undef REASON
25427efe0e03SHe Kuang #endif
25437efe0e03SHe Kuang 
25449d2ed645SAlexey Budankov 	rec->opts.affinity = PERF_AFFINITY_SYS;
25459d2ed645SAlexey Budankov 
25460f98b11cSJiri Olsa 	rec->evlist = evlist__new();
25473e2be2daSArnaldo Carvalho de Melo 	if (rec->evlist == NULL)
2548361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
2549361c99a6SArnaldo Carvalho de Melo 
2550ecc4c561SArnaldo Carvalho de Melo 	err = perf_config(perf_record_config, rec);
2551ecc4c561SArnaldo Carvalho de Melo 	if (err)
2552ecc4c561SArnaldo Carvalho de Melo 		return err;
2553eb853e80SJiri Olsa 
2554bca647aaSTom Zanussi 	argc = parse_options(argc, argv, record_options, record_usage,
2555a0541234SAnton Blanchard 			    PARSE_OPT_STOP_AT_NON_OPTION);
255668ba3235SNamhyung Kim 	if (quiet)
255768ba3235SNamhyung Kim 		perf_quiet_option();
2558483635a9SJiri Olsa 
2559483635a9SJiri Olsa 	/* Make system wide (-a) the default target. */
2560602ad878SArnaldo Carvalho de Melo 	if (!argc && target__none(&rec->opts.target))
2561483635a9SJiri Olsa 		rec->opts.target.system_wide = true;
256286470930SIngo Molnar 
2563bea03405SNamhyung Kim 	if (nr_cgroups && !rec->opts.target.system_wide) {
2564c7118369SNamhyung Kim 		usage_with_options_msg(record_usage, record_options,
2565c7118369SNamhyung Kim 			"cgroup monitoring only available in system-wide mode");
2566c7118369SNamhyung Kim 
2567023695d9SStephane Eranian 	}
2568504c1ad1SAlexey Budankov 
2569eeb399b5SAdrian Hunter 	if (rec->opts.kcore)
2570eeb399b5SAdrian Hunter 		rec->data.is_dir = true;
2571eeb399b5SAdrian Hunter 
2572504c1ad1SAlexey Budankov 	if (rec->opts.comp_level != 0) {
2573504c1ad1SAlexey Budankov 		pr_debug("Compression enabled, disabling build id collection at the end of the session.\n");
2574504c1ad1SAlexey Budankov 		rec->no_buildid = true;
2575504c1ad1SAlexey Budankov 	}
2576504c1ad1SAlexey Budankov 
2577b757bb09SAdrian Hunter 	if (rec->opts.record_switch_events &&
2578b757bb09SAdrian Hunter 	    !perf_can_record_switch_events()) {
2579c7118369SNamhyung Kim 		ui__error("kernel does not support recording context switch events\n");
2580c7118369SNamhyung Kim 		parse_options_usage(record_usage, record_options, "switch-events", 0);
2581c7118369SNamhyung Kim 		return -EINVAL;
2582b757bb09SAdrian Hunter 	}
2583023695d9SStephane Eranian 
2584cb4e1ebbSJiri Olsa 	if (switch_output_setup(rec)) {
2585cb4e1ebbSJiri Olsa 		parse_options_usage(record_usage, record_options, "switch-output", 0);
2586cb4e1ebbSJiri Olsa 		return -EINVAL;
2587cb4e1ebbSJiri Olsa 	}
2588cb4e1ebbSJiri Olsa 
2589bfacbe3bSJiri Olsa 	if (rec->switch_output.time) {
2590bfacbe3bSJiri Olsa 		signal(SIGALRM, alarm_sig_handler);
2591bfacbe3bSJiri Olsa 		alarm(rec->switch_output.time);
2592bfacbe3bSJiri Olsa 	}
2593bfacbe3bSJiri Olsa 
259403724b2eSAndi Kleen 	if (rec->switch_output.num_files) {
259503724b2eSAndi Kleen 		rec->switch_output.filenames = calloc(sizeof(char *),
259603724b2eSAndi Kleen 						      rec->switch_output.num_files);
259703724b2eSAndi Kleen 		if (!rec->switch_output.filenames)
259803724b2eSAndi Kleen 			return -EINVAL;
259903724b2eSAndi Kleen 	}
260003724b2eSAndi Kleen 
26011b36c03eSAdrian Hunter 	/*
26021b36c03eSAdrian Hunter 	 * Allow aliases to facilitate the lookup of symbols for address
26031b36c03eSAdrian Hunter 	 * filters. Refer to auxtrace_parse_filters().
26041b36c03eSAdrian Hunter 	 */
26051b36c03eSAdrian Hunter 	symbol_conf.allow_aliases = true;
26061b36c03eSAdrian Hunter 
26071b36c03eSAdrian Hunter 	symbol__init(NULL);
26081b36c03eSAdrian Hunter 
26098384a260SAlexey Budankov 	if (rec->opts.affinity != PERF_AFFINITY_SYS) {
26108384a260SAlexey Budankov 		rec->affinity_mask.nbits = cpu__max_cpu();
26118384a260SAlexey Budankov 		rec->affinity_mask.bits = bitmap_alloc(rec->affinity_mask.nbits);
26128384a260SAlexey Budankov 		if (!rec->affinity_mask.bits) {
26138384a260SAlexey Budankov 			pr_err("Failed to allocate thread mask for %zd cpus\n", rec->affinity_mask.nbits);
26148384a260SAlexey Budankov 			return -ENOMEM;
26158384a260SAlexey Budankov 		}
26168384a260SAlexey Budankov 		pr_debug2("thread mask[%zd]: empty\n", rec->affinity_mask.nbits);
26178384a260SAlexey Budankov 	}
26188384a260SAlexey Budankov 
26194b5ea3bdSAdrian Hunter 	err = record__auxtrace_init(rec);
26201b36c03eSAdrian Hunter 	if (err)
26211b36c03eSAdrian Hunter 		goto out;
26221b36c03eSAdrian Hunter 
26230aab2136SWang Nan 	if (dry_run)
26245c01ad60SAdrian Hunter 		goto out;
26250aab2136SWang Nan 
2626d7888573SWang Nan 	err = bpf__setup_stdout(rec->evlist);
2627d7888573SWang Nan 	if (err) {
2628d7888573SWang Nan 		bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
2629d7888573SWang Nan 		pr_err("ERROR: Setup BPF stdout failed: %s\n",
2630d7888573SWang Nan 			 errbuf);
26315c01ad60SAdrian Hunter 		goto out;
2632d7888573SWang Nan 	}
2633d7888573SWang Nan 
2634ef149c25SAdrian Hunter 	err = -ENOMEM;
2635ef149c25SAdrian Hunter 
26360c1d46a8SWang Nan 	if (rec->no_buildid_cache || rec->no_buildid) {
2637a1ac1d3cSStephane Eranian 		disable_buildid_cache();
2638dc0c6127SJiri Olsa 	} else if (rec->switch_output.enabled) {
26390c1d46a8SWang Nan 		/*
26400c1d46a8SWang Nan 		 * In 'perf record --switch-output', disable buildid
26410c1d46a8SWang Nan 		 * generation by default to reduce data file switching
26420c1d46a8SWang Nan 		 * overhead. Still generate buildid if they are required
26430c1d46a8SWang Nan 		 * explicitly using
26440c1d46a8SWang Nan 		 *
264560437ac0SJiri Olsa 		 *  perf record --switch-output --no-no-buildid \
26460c1d46a8SWang Nan 		 *              --no-no-buildid-cache
26470c1d46a8SWang Nan 		 *
26480c1d46a8SWang Nan 		 * Following code equals to:
26490c1d46a8SWang Nan 		 *
26500c1d46a8SWang Nan 		 * if ((rec->no_buildid || !rec->no_buildid_set) &&
26510c1d46a8SWang Nan 		 *     (rec->no_buildid_cache || !rec->no_buildid_cache_set))
26520c1d46a8SWang Nan 		 *         disable_buildid_cache();
26530c1d46a8SWang Nan 		 */
26540c1d46a8SWang Nan 		bool disable = true;
26550c1d46a8SWang Nan 
26560c1d46a8SWang Nan 		if (rec->no_buildid_set && !rec->no_buildid)
26570c1d46a8SWang Nan 			disable = false;
26580c1d46a8SWang Nan 		if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
26590c1d46a8SWang Nan 			disable = false;
26600c1d46a8SWang Nan 		if (disable) {
26610c1d46a8SWang Nan 			rec->no_buildid = true;
26620c1d46a8SWang Nan 			rec->no_buildid_cache = true;
26630c1d46a8SWang Nan 			disable_buildid_cache();
26640c1d46a8SWang Nan 		}
26650c1d46a8SWang Nan 	}
2666655000e7SArnaldo Carvalho de Melo 
26674ea648aeSWang Nan 	if (record.opts.overwrite)
26684ea648aeSWang Nan 		record.opts.tail_synthesize = true;
26694ea648aeSWang Nan 
26706484d2f9SJiri Olsa 	if (rec->evlist->core.nr_entries == 0 &&
26714b4cd503SArnaldo Carvalho de Melo 	    __perf_evlist__add_default(rec->evlist, !record.opts.no_samples) < 0) {
267269aad6f1SArnaldo Carvalho de Melo 		pr_err("Not enough memory for event selector list\n");
2673394c01edSAdrian Hunter 		goto out;
2674bbd36e5eSPeter Zijlstra 	}
267586470930SIngo Molnar 
267669e7e5b0SAdrian Hunter 	if (rec->opts.target.tid && !rec->opts.no_inherit_set)
267769e7e5b0SAdrian Hunter 		rec->opts.no_inherit = true;
267869e7e5b0SAdrian Hunter 
2679602ad878SArnaldo Carvalho de Melo 	err = target__validate(&rec->opts.target);
268016ad2ffbSNamhyung Kim 	if (err) {
2681602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
2682c3dec27bSJiri Olsa 		ui__warning("%s\n", errbuf);
268316ad2ffbSNamhyung Kim 	}
26844bd0f2d2SNamhyung Kim 
2685602ad878SArnaldo Carvalho de Melo 	err = target__parse_uid(&rec->opts.target);
268616ad2ffbSNamhyung Kim 	if (err) {
268716ad2ffbSNamhyung Kim 		int saved_errno = errno;
268816ad2ffbSNamhyung Kim 
2689602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
26903780f488SNamhyung Kim 		ui__error("%s", errbuf);
269116ad2ffbSNamhyung Kim 
269216ad2ffbSNamhyung Kim 		err = -saved_errno;
2693394c01edSAdrian Hunter 		goto out;
269416ad2ffbSNamhyung Kim 	}
26950d37aa34SArnaldo Carvalho de Melo 
2696ca800068SMengting Zhang 	/* Enable ignoring missing threads when -u/-p option is defined. */
2697ca800068SMengting Zhang 	rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid;
269823dc4f15SJiri Olsa 
269916ad2ffbSNamhyung Kim 	err = -ENOMEM;
27003e2be2daSArnaldo Carvalho de Melo 	if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0)
2701dd7927f4SArnaldo Carvalho de Melo 		usage_with_options(record_usage, record_options);
270269aad6f1SArnaldo Carvalho de Melo 
2703ef149c25SAdrian Hunter 	err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
2704ef149c25SAdrian Hunter 	if (err)
2705394c01edSAdrian Hunter 		goto out;
2706ef149c25SAdrian Hunter 
27076156681bSNamhyung Kim 	/*
27086156681bSNamhyung Kim 	 * We take all buildids when the file contains
27096156681bSNamhyung Kim 	 * AUX area tracing data because we do not decode the
27106156681bSNamhyung Kim 	 * trace because it would take too long.
27116156681bSNamhyung Kim 	 */
27126156681bSNamhyung Kim 	if (rec->opts.full_auxtrace)
27136156681bSNamhyung Kim 		rec->buildid_all = true;
27146156681bSNamhyung Kim 
2715b4006796SArnaldo Carvalho de Melo 	if (record_opts__config(&rec->opts)) {
271639d17dacSArnaldo Carvalho de Melo 		err = -EINVAL;
2717394c01edSAdrian Hunter 		goto out;
27187e4ff9e3SMike Galbraith 	}
27197e4ff9e3SMike Galbraith 
272093f20c0fSAlexey Budankov 	if (rec->opts.nr_cblocks > nr_cblocks_max)
272193f20c0fSAlexey Budankov 		rec->opts.nr_cblocks = nr_cblocks_max;
27225d7f4116SAlexey Budankov 	pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks);
2723d3d1af6fSAlexey Budankov 
27249d2ed645SAlexey Budankov 	pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]);
2725470530bbSAlexey Budankov 	pr_debug("mmap flush: %d\n", rec->opts.mmap_flush);
27269d2ed645SAlexey Budankov 
272751255a8aSAlexey Budankov 	if (rec->opts.comp_level > comp_level_max)
272851255a8aSAlexey Budankov 		rec->opts.comp_level = comp_level_max;
272951255a8aSAlexey Budankov 	pr_debug("comp level: %d\n", rec->opts.comp_level);
273051255a8aSAlexey Budankov 
2731d20deb64SArnaldo Carvalho de Melo 	err = __cmd_record(&record, argc, argv);
2732394c01edSAdrian Hunter out:
27338384a260SAlexey Budankov 	bitmap_free(rec->affinity_mask.bits);
2734c12995a5SJiri Olsa 	evlist__delete(rec->evlist);
2735d65a458bSArnaldo Carvalho de Melo 	symbol__exit();
2736ef149c25SAdrian Hunter 	auxtrace_record__free(rec->itr);
273739d17dacSArnaldo Carvalho de Melo 	return err;
273886470930SIngo Molnar }
27392dd6d8a1SAdrian Hunter 
27402dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused)
27412dd6d8a1SAdrian Hunter {
2742dc0c6127SJiri Olsa 	struct record *rec = &record;
2743dc0c6127SJiri Olsa 
27445f9cf599SWang Nan 	if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
27455f9cf599SWang Nan 		trigger_hit(&auxtrace_snapshot_trigger);
27462dd6d8a1SAdrian Hunter 		auxtrace_record__snapshot_started = 1;
27475f9cf599SWang Nan 		if (auxtrace_record__snapshot_start(record.itr))
27485f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
27495f9cf599SWang Nan 	}
27503c1cb7e3SWang Nan 
2751dc0c6127SJiri Olsa 	if (switch_output_signal(rec))
27523c1cb7e3SWang Nan 		trigger_hit(&switch_output_trigger);
27532dd6d8a1SAdrian Hunter }
2754bfacbe3bSJiri Olsa 
2755bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused)
2756bfacbe3bSJiri Olsa {
2757bfacbe3bSJiri Olsa 	struct record *rec = &record;
2758bfacbe3bSJiri Olsa 
2759bfacbe3bSJiri Olsa 	if (switch_output_time(rec))
2760bfacbe3bSJiri Olsa 		trigger_hit(&switch_output_trigger);
2761bfacbe3bSJiri Olsa }
2762