blob: efa03e4ac2c9f3379ea4ca5507eb0bb6c0109834 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnarabaff322009-06-02 22:59:57 +02002/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02003 * builtin-record.c
4 *
5 * Builtin record command: Record the profile of a workload
6 * (or a CPU, or a PID) into the perf.data output file - for
7 * later analysis via perf report.
Ingo Molnarabaff322009-06-02 22:59:57 +02008 */
Ingo Molnar16f762a2009-05-27 09:10:38 +02009#include "builtin.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020010
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -020011#include "util/build-id.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060012#include <subcmd/parse-options.h>
Namhyung Kime3a23262022-09-01 12:57:37 -070013#include <internal/xyarray.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +020014#include "util/parse-events.h"
Taeung Song41840d22016-06-23 17:55:17 +090015#include "util/config.h"
Thomas Gleixner6eda5832009-05-01 18:29:57 +020016
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030017#include "util/callchain.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030018#include "util/cgroup.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019#include "util/header.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020020#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020021#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020022#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020023#include "util/debug.h"
Arnaldo Carvalho de Meloe0fcfb02019-09-23 12:20:38 -030024#include "util/mmap.h"
Ian Rogers49c670b172022-08-26 09:42:31 -070025#include "util/mutex.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030026#include "util/target.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020027#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020028#include "util/tool.h"
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020029#include "util/symbol.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030030#include "util/record.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110031#include "util/cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020032#include "util/thread_map.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020033#include "util/data.h"
Stephane Eranianbcc84ec2015-08-31 18:41:12 +020034#include "util/perf_regs.h"
Adrian Hunteref149c22015-04-09 18:53:45 +030035#include "util/auxtrace.h"
Adrian Hunter46bc29b2016-03-08 10:38:44 +020036#include "util/tsc.h"
Andi Kleenf00898f2015-05-27 10:51:51 -070037#include "util/parse-branch-options.h"
Stephane Eranianbcc84ec2015-08-31 18:41:12 +020038#include "util/parse-regs-options.h"
Arnaldo Carvalho de Melo40c7d242020-05-05 11:49:08 -030039#include "util/perf_api_probe.h"
Wang Nan71dc23262015-10-14 12:41:19 +000040#include "util/llvm-utils.h"
Wang Nan8690a2a2016-02-22 09:10:32 +000041#include "util/bpf-loader.h"
Wang Nan5f9cf592016-04-20 18:59:49 +000042#include "util/trigger.h"
Wang Nana0748652016-11-26 07:03:28 +000043#include "util/perf-hooks.h"
Alexey Budankovf13de662019-01-22 20:50:57 +030044#include "util/cpu-set-sched.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030045#include "util/synthetic-events.h"
Arnaldo Carvalho de Meloc5e40272017-04-19 16:12:39 -030046#include "util/time-utils.h"
Arnaldo Carvalho de Melo58db1d62017-04-19 16:05:56 -030047#include "util/units.h"
Song Liu7b612e22019-01-17 08:15:19 -080048#include "util/bpf-event.h"
Stephane Eraniand99c22e2020-04-22 08:50:38 -070049#include "util/util.h"
Stephane Eranian70943492020-05-05 11:29:43 -070050#include "util/pfm.h"
Jiri Olsa6953beb2020-08-05 11:34:38 +020051#include "util/clockid.h"
Jin Yaob53a0752021-04-27 15:01:26 +080052#include "util/pmu-hybrid.h"
53#include "util/evlist-hybrid.h"
Namhyung Kimedc41a12022-05-18 15:47:21 -070054#include "util/off_cpu.h"
Namhyung Kim27c6f242023-03-14 16:42:31 -070055#include "util/bpf-filter.h"
Wang Nand8871ea2016-02-26 09:32:06 +000056#include "asm/bug.h"
Arnaldo Carvalho de Meloc1a604d2019-08-29 15:20:59 -030057#include "perf.h"
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +030058#include "cputopo.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030060#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030061#include <inttypes.h>
Arnaldo Carvalho de Melo67230472018-03-01 13:46:23 -030062#include <locale.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030063#include <poll.h>
Stephane Eraniand99c22e2020-04-22 08:50:38 -070064#include <pthread.h>
Peter Zijlstra97124d5e2009-06-02 15:52:24 +020065#include <unistd.h>
Alexey Bayduraev415ccb52022-01-17 21:34:23 +030066#ifndef HAVE_GETTID
67#include <syscall.h>
68#endif
Peter Zijlstrade9ac072009-04-08 15:01:31 +020069#include <sched.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030070#include <signal.h>
Anand K Mistryda231332020-05-13 12:20:23 +100071#ifdef HAVE_EVENTFD_SUPPORT
72#include <sys/eventfd.h>
73#endif
Arnaldo Carvalho de Meloa41794c2010-05-18 18:29:23 -030074#include <sys/mman.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030075#include <sys/wait.h>
Adrian Huntereeb399b2019-10-04 11:31:21 +030076#include <sys/types.h>
77#include <sys/stat.h>
78#include <fcntl.h>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053079#include <linux/err.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030080#include <linux/string.h>
Arnaldo Carvalho de Melo0693e682016-08-08 15:05:46 -030081#include <linux/time64.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -030082#include <linux/zalloc.h>
Alexey Budankov8384a262019-12-03 14:45:27 +030083#include <linux/bitmap.h>
Jiri Olsad1e325c2020-08-05 11:34:40 +020084#include <sys/time.h>
Bernhard Rosenkraenzer78da39f2012-10-08 09:43:26 +030085
Jiri Olsa1b43b702017-01-09 10:51:56 +010086struct switch_output {
Jiri Olsadc0c6122017-01-09 10:51:58 +010087 bool enabled;
Jiri Olsa1b43b702017-01-09 10:51:56 +010088 bool signal;
Jiri Olsadc0c6122017-01-09 10:51:58 +010089 unsigned long size;
Jiri Olsabfacbe32017-01-09 10:52:00 +010090 unsigned long time;
Jiri Olsacb4e1eb2017-01-09 10:51:57 +010091 const char *str;
92 bool set;
Andi Kleen03724b22019-03-14 15:49:55 -070093 char **filenames;
94 int num_files;
95 int cur_file;
Jiri Olsa1b43b702017-01-09 10:51:56 +010096};
97
Alexey Bayduraev7954f712022-01-17 21:34:21 +030098struct thread_mask {
99 struct mmap_cpu_mask maps;
100 struct mmap_cpu_mask affinity;
101};
102
Alexey Bayduraev415ccb52022-01-17 21:34:23 +0300103struct record_thread {
104 pid_t tid;
105 struct thread_mask *mask;
106 struct {
107 int msg[2];
108 int ack[2];
109 } pipes;
110 struct fdarray pollfd;
111 int ctlfd_pos;
112 int nr_mmaps;
113 struct mmap **maps;
114 struct mmap **overwrite_maps;
115 struct record *rec;
Alexey Bayduraev396b6262022-01-17 21:34:25 +0300116 unsigned long long samples;
117 unsigned long waking;
Alexey Bayduraevae9c7242b2022-01-17 21:34:29 +0300118 u64 bytes_written;
Alexey Bayduraev610fbc02022-01-17 21:34:31 +0300119 u64 bytes_transferred;
120 u64 bytes_compressed;
Alexey Bayduraev415ccb52022-01-17 21:34:23 +0300121};
122
Alexey Bayduraev396b6262022-01-17 21:34:25 +0300123static __thread struct record_thread *thread;
124
Alexey Bayduraev1e5de7d2022-01-17 21:34:26 +0300125enum thread_msg {
126 THREAD_MSG__UNDEFINED = 0,
127 THREAD_MSG__READY,
128 THREAD_MSG__MAX,
129};
130
131static const char *thread_msg_tags[THREAD_MSG__MAX] = {
132 "UNDEFINED", "READY"
133};
134
Alexey Bayduraev06380a82022-01-17 21:34:32 +0300135enum thread_spec {
136 THREAD_SPEC__UNDEFINED = 0,
137 THREAD_SPEC__CPU,
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +0300138 THREAD_SPEC__CORE,
139 THREAD_SPEC__PACKAGE,
140 THREAD_SPEC__NUMA,
141 THREAD_SPEC__USER,
142 THREAD_SPEC__MAX,
143};
144
145static const char *thread_spec_tags[THREAD_SPEC__MAX] = {
146 "undefined", "cpu", "core", "package", "numa", "user"
Alexey Bayduraev06380a82022-01-17 21:34:32 +0300147};
148
Adrian Hunter6562c9a2022-08-24 10:28:10 +0300149struct pollfd_index_map {
150 int evlist_pollfd_index;
151 int thread_pollfd_index;
152};
153
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -0300154struct record {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200155 struct perf_tool tool;
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300156 struct record_opts opts;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200157 u64 bytes_written;
Yang Jihong91621be2023-02-15 12:23:24 +0000158 u64 thread_bytes_written;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100159 struct perf_data data;
Adrian Hunteref149c22015-04-09 18:53:45 +0300160 struct auxtrace_record *itr;
Jiri Olsa63503db2019-07-21 13:23:52 +0200161 struct evlist *evlist;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200162 struct perf_session *session;
Arnaldo Carvalho de Melobc477d72020-04-24 10:24:04 -0300163 struct evlist *sb_evlist;
Arnaldo Carvalho de Melo899e5ff2020-04-27 17:56:37 -0300164 pthread_t thread_id;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200165 int realtime_prio;
Arnaldo Carvalho de Melo899e5ff2020-04-27 17:56:37 -0300166 bool switch_output_event_set;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200167 bool no_buildid;
Wang Nand2db9a92016-01-25 09:56:19 +0000168 bool no_buildid_set;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200169 bool no_buildid_cache;
Wang Nand2db9a92016-01-25 09:56:19 +0000170 bool no_buildid_cache_set;
Namhyung Kim61566812016-01-11 22:37:09 +0900171 bool buildid_all;
Jiri Olsae29386c2020-12-14 11:54:57 +0100172 bool buildid_mmap;
Wang Nanecfd7a92016-04-13 08:21:07 +0000173 bool timestamp_filename;
Jin Yao68588ba2017-12-08 21:13:42 +0800174 bool timestamp_boundary;
Namhyung Kimedc41a12022-05-18 15:47:21 -0700175 bool off_cpu;
Jiri Olsa1b43b702017-01-09 10:51:56 +0100176 struct switch_output switch_output;
Yang Shi9f065192015-09-29 14:49:43 -0700177 unsigned long long samples;
Jiwei Sun6d575812019-10-22 16:09:01 +0800178 unsigned long output_max_size; /* = 0: unlimited */
Jiri Olsa9bce13e2021-12-09 21:04:25 +0100179 struct perf_debuginfod debuginfod;
Alexey Bayduraev7954f712022-01-17 21:34:21 +0300180 int nr_threads;
181 struct thread_mask *thread_masks;
Alexey Bayduraev415ccb52022-01-17 21:34:23 +0300182 struct record_thread *thread_data;
Adrian Hunter6562c9a2022-08-24 10:28:10 +0300183 struct pollfd_index_map *index_map;
184 size_t index_map_sz;
185 size_t index_map_cnt;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200186};
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200187
Jiwei Sun6d575812019-10-22 16:09:01 +0800188static volatile int done;
189
Jiri Olsadc0c6122017-01-09 10:51:58 +0100190static volatile int auxtrace_record__snapshot_started;
191static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
192static DEFINE_TRIGGER(switch_output_trigger);
193
Alexey Budankov9d2ed642019-01-22 20:47:43 +0300194static const char *affinity_tags[PERF_AFFINITY_MAX] = {
195 "SYS", "NODE", "CPU"
196};
197
Alexey Bayduraev415ccb52022-01-17 21:34:23 +0300198#ifndef HAVE_GETTID
199static inline pid_t gettid(void)
200{
201 return (pid_t)syscall(__NR_gettid);
202}
203#endif
204
Alexey Bayduraev3217e9f2022-01-17 21:34:27 +0300205static int record__threads_enabled(struct record *rec)
206{
207 return rec->opts.threads_spec;
208}
209
Jiri Olsadc0c6122017-01-09 10:51:58 +0100210static bool switch_output_signal(struct record *rec)
211{
212 return rec->switch_output.signal &&
213 trigger_is_ready(&switch_output_trigger);
214}
215
216static bool switch_output_size(struct record *rec)
217{
218 return rec->switch_output.size &&
219 trigger_is_ready(&switch_output_trigger) &&
220 (rec->bytes_written >= rec->switch_output.size);
221}
222
Jiri Olsabfacbe32017-01-09 10:52:00 +0100223static bool switch_output_time(struct record *rec)
224{
225 return rec->switch_output.time &&
226 trigger_is_ready(&switch_output_trigger);
227}
228
Alexey Bayduraevae9c7242b2022-01-17 21:34:29 +0300229static u64 record__bytes_written(struct record *rec)
230{
Yang Jihong91621be2023-02-15 12:23:24 +0000231 return rec->bytes_written + rec->thread_bytes_written;
Alexey Bayduraevae9c7242b2022-01-17 21:34:29 +0300232}
233
Jiwei Sun6d575812019-10-22 16:09:01 +0800234static bool record__output_max_size_exceeded(struct record *rec)
235{
236 return rec->output_max_size &&
Alexey Bayduraevae9c7242b2022-01-17 21:34:29 +0300237 (record__bytes_written(rec) >= rec->output_max_size);
Jiwei Sun6d575812019-10-22 16:09:01 +0800238}
239
Jiri Olsaa5830532019-07-27 20:30:53 +0200240static int record__write(struct record *rec, struct mmap *map __maybe_unused,
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200241 void *bf, size_t size)
Peter Zijlstraf5970552009-06-18 23:22:55 +0200242{
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200243 struct perf_data_file *file = &rec->session->data->file;
244
Alexey Bayduraev56f735f2022-01-17 21:34:28 +0300245 if (map && map->file)
246 file = map->file;
247
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200248 if (perf_data_file__write(file, bf, size) < 0) {
Jiri Olsa50a9b862013-11-22 13:11:24 +0100249 pr_err("failed to write perf data, error: %m\n");
250 return -1;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200251 }
David Ahern8d3eca22012-08-26 12:24:47 -0600252
Yang Jihong91621be2023-02-15 12:23:24 +0000253 if (map && map->file) {
Alexey Bayduraevae9c7242b2022-01-17 21:34:29 +0300254 thread->bytes_written += size;
Yang Jihong91621be2023-02-15 12:23:24 +0000255 rec->thread_bytes_written += size;
256 } else {
Alexey Bayduraev56f735f2022-01-17 21:34:28 +0300257 rec->bytes_written += size;
Yang Jihong91621be2023-02-15 12:23:24 +0000258 }
Jiri Olsadc0c6122017-01-09 10:51:58 +0100259
Jiwei Sun6d575812019-10-22 16:09:01 +0800260 if (record__output_max_size_exceeded(rec) && !done) {
261 fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
262 " stopping session ]\n",
Alexey Bayduraevae9c7242b2022-01-17 21:34:29 +0300263 record__bytes_written(rec) >> 10);
Jiwei Sun6d575812019-10-22 16:09:01 +0800264 done = 1;
265 }
266
Jiri Olsadc0c6122017-01-09 10:51:58 +0100267 if (switch_output_size(rec))
268 trigger_hit(&switch_output_trigger);
269
David Ahern8d3eca22012-08-26 12:24:47 -0600270 return 0;
Peter Zijlstraf5970552009-06-18 23:22:55 +0200271}
272
Alexey Budankovef781122019-03-18 20:44:12 +0300273static int record__aio_enabled(struct record *rec);
274static int record__comp_enabled(struct record *rec);
Alexey Bayduraev75f5f1f2022-01-17 21:34:30 +0300275static size_t zstd_compress(struct perf_session *session, struct mmap *map,
276 void *dst, size_t dst_size, void *src, size_t src_size);
Alexey Budankov5d7f4112019-03-18 20:43:35 +0300277
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300278#ifdef HAVE_AIO_SUPPORT
279static int record__aio_write(struct aiocb *cblock, int trace_fd,
280 void *buf, size_t size, off_t off)
281{
282 int rc;
283
284 cblock->aio_fildes = trace_fd;
285 cblock->aio_buf = buf;
286 cblock->aio_nbytes = size;
287 cblock->aio_offset = off;
288 cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
289
290 do {
291 rc = aio_write(cblock);
292 if (rc == 0) {
293 break;
294 } else if (errno != EAGAIN) {
295 cblock->aio_fildes = -1;
296 pr_err("failed to queue perf data, error: %m\n");
297 break;
298 }
299 } while (1);
300
301 return rc;
302}
303
Jiri Olsaa5830532019-07-27 20:30:53 +0200304static int record__aio_complete(struct mmap *md, struct aiocb *cblock)
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300305{
306 void *rem_buf;
307 off_t rem_off;
308 size_t rem_size;
309 int rc, aio_errno;
310 ssize_t aio_ret, written;
311
312 aio_errno = aio_error(cblock);
313 if (aio_errno == EINPROGRESS)
314 return 0;
315
316 written = aio_ret = aio_return(cblock);
317 if (aio_ret < 0) {
318 if (aio_errno != EINTR)
319 pr_err("failed to write perf data, error: %m\n");
320 written = 0;
321 }
322
323 rem_size = cblock->aio_nbytes - written;
324
325 if (rem_size == 0) {
326 cblock->aio_fildes = -1;
327 /*
Alexey Budankovef781122019-03-18 20:44:12 +0300328 * md->refcount is incremented in record__aio_pushfn() for
329 * every aio write request started in record__aio_push() so
330 * decrement it because the request is now complete.
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300331 */
Jiri Olsa80e53d12019-10-07 14:53:15 +0200332 perf_mmap__put(&md->core);
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300333 rc = 1;
334 } else {
335 /*
336 * aio write request may require restart with the
337 * reminder if the kernel didn't write whole
338 * chunk at once.
339 */
340 rem_off = cblock->aio_offset + written;
341 rem_buf = (void *)(cblock->aio_buf + written);
342 record__aio_write(cblock, cblock->aio_fildes,
343 rem_buf, rem_size, rem_off);
344 rc = 0;
345 }
346
347 return rc;
348}
349
Jiri Olsaa5830532019-07-27 20:30:53 +0200350static int record__aio_sync(struct mmap *md, bool sync_all)
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300351{
Alexey Budankov93f20c02018-11-06 12:07:19 +0300352 struct aiocb **aiocb = md->aio.aiocb;
353 struct aiocb *cblocks = md->aio.cblocks;
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300354 struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */
Alexey Budankov93f20c02018-11-06 12:07:19 +0300355 int i, do_suspend;
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300356
357 do {
Alexey Budankov93f20c02018-11-06 12:07:19 +0300358 do_suspend = 0;
359 for (i = 0; i < md->aio.nr_cblocks; ++i) {
360 if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
361 if (sync_all)
362 aiocb[i] = NULL;
363 else
364 return i;
365 } else {
366 /*
367 * Started aio write is not complete yet
368 * so it has to be waited before the
369 * next allocation.
370 */
371 aiocb[i] = &cblocks[i];
372 do_suspend = 1;
373 }
374 }
375 if (!do_suspend)
376 return -1;
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300377
Alexey Budankov93f20c02018-11-06 12:07:19 +0300378 while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) {
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300379 if (!(errno == EAGAIN || errno == EINTR))
380 pr_err("failed to sync perf data, error: %m\n");
381 }
382 } while (1);
383}
384
Alexey Budankovef781122019-03-18 20:44:12 +0300385struct record_aio {
386 struct record *rec;
387 void *data;
388 size_t size;
389};
390
Jiri Olsaa5830532019-07-27 20:30:53 +0200391static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size)
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300392{
Alexey Budankovef781122019-03-18 20:44:12 +0300393 struct record_aio *aio = to;
394
395 /*
Jiri Olsa547740f2019-07-27 22:07:44 +0200396 * map->core.base data pointed by buf is copied into free map->aio.data[] buffer
Alexey Budankovef781122019-03-18 20:44:12 +0300397 * to release space in the kernel buffer as fast as possible, calling
398 * perf_mmap__consume() from perf_mmap__push() function.
399 *
400 * That lets the kernel to proceed with storing more profiling data into
401 * the kernel buffer earlier than other per-cpu kernel buffers are handled.
402 *
403 * Coping can be done in two steps in case the chunk of profiling data
404 * crosses the upper bound of the kernel buffer. In this case we first move
405 * part of data from map->start till the upper bound and then the reminder
406 * from the beginning of the kernel buffer till the end of the data chunk.
407 */
408
409 if (record__comp_enabled(aio->rec)) {
Alexey Bayduraev75f5f1f2022-01-17 21:34:30 +0300410 size = zstd_compress(aio->rec->session, NULL, aio->data + aio->size,
Jiri Olsabf59b302019-10-07 14:53:11 +0200411 mmap__mmap_len(map) - aio->size,
Alexey Budankovef781122019-03-18 20:44:12 +0300412 buf, size);
413 } else {
414 memcpy(aio->data + aio->size, buf, size);
415 }
416
417 if (!aio->size) {
418 /*
419 * Increment map->refcount to guard map->aio.data[] buffer
420 * from premature deallocation because map object can be
421 * released earlier than aio write request started on
422 * map->aio.data[] buffer is complete.
423 *
424 * perf_mmap__put() is done at record__aio_complete()
425 * after started aio request completion or at record__aio_push()
426 * if the request failed to start.
427 */
Jiri Olsae75710f2019-10-07 14:53:13 +0200428 perf_mmap__get(&map->core);
Alexey Budankovef781122019-03-18 20:44:12 +0300429 }
430
431 aio->size += size;
432
433 return size;
434}
435
Jiri Olsaa5830532019-07-27 20:30:53 +0200436static int record__aio_push(struct record *rec, struct mmap *map, off_t *off)
Alexey Budankovef781122019-03-18 20:44:12 +0300437{
438 int ret, idx;
439 int trace_fd = rec->session->data->file.fd;
440 struct record_aio aio = { .rec = rec, .size = 0 };
441
442 /*
443 * Call record__aio_sync() to wait till map->aio.data[] buffer
444 * becomes available after previous aio write operation.
445 */
446
447 idx = record__aio_sync(map, false);
448 aio.data = map->aio.data[idx];
449 ret = perf_mmap__push(map, &aio, record__aio_pushfn);
450 if (ret != 0) /* ret > 0 - no data, ret < 0 - error */
451 return ret;
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300452
453 rec->samples++;
Alexey Budankovef781122019-03-18 20:44:12 +0300454 ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off);
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300455 if (!ret) {
Alexey Budankovef781122019-03-18 20:44:12 +0300456 *off += aio.size;
457 rec->bytes_written += aio.size;
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300458 if (switch_output_size(rec))
459 trigger_hit(&switch_output_trigger);
Alexey Budankovef781122019-03-18 20:44:12 +0300460 } else {
461 /*
462 * Decrement map->refcount incremented in record__aio_pushfn()
463 * back if record__aio_write() operation failed to start, otherwise
464 * map->refcount is decremented in record__aio_complete() after
465 * aio write operation finishes successfully.
466 */
Jiri Olsa80e53d12019-10-07 14:53:15 +0200467 perf_mmap__put(&map->core);
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300468 }
469
470 return ret;
471}
472
473static off_t record__aio_get_pos(int trace_fd)
474{
475 return lseek(trace_fd, 0, SEEK_CUR);
476}
477
478static void record__aio_set_pos(int trace_fd, off_t pos)
479{
480 lseek(trace_fd, pos, SEEK_SET);
481}
482
483static void record__aio_mmap_read_sync(struct record *rec)
484{
485 int i;
Jiri Olsa63503db2019-07-21 13:23:52 +0200486 struct evlist *evlist = rec->evlist;
Jiri Olsaa5830532019-07-27 20:30:53 +0200487 struct mmap *maps = evlist->mmap;
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300488
Alexey Budankovef781122019-03-18 20:44:12 +0300489 if (!record__aio_enabled(rec))
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300490 return;
491
Jiri Olsac976ee12019-07-30 13:04:59 +0200492 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Jiri Olsaa5830532019-07-27 20:30:53 +0200493 struct mmap *map = &maps[i];
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300494
Jiri Olsa547740f2019-07-27 22:07:44 +0200495 if (map->core.base)
Alexey Budankov93f20c02018-11-06 12:07:19 +0300496 record__aio_sync(map, true);
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300497 }
498}
499
500static int nr_cblocks_default = 1;
Alexey Budankov93f20c02018-11-06 12:07:19 +0300501static int nr_cblocks_max = 4;
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300502
503static int record__aio_parse(const struct option *opt,
Alexey Budankov93f20c02018-11-06 12:07:19 +0300504 const char *str,
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300505 int unset)
506{
507 struct record_opts *opts = (struct record_opts *)opt->value;
508
Alexey Budankov93f20c02018-11-06 12:07:19 +0300509 if (unset) {
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300510 opts->nr_cblocks = 0;
Alexey Budankov93f20c02018-11-06 12:07:19 +0300511 } else {
512 if (str)
513 opts->nr_cblocks = strtol(str, NULL, 0);
514 if (!opts->nr_cblocks)
515 opts->nr_cblocks = nr_cblocks_default;
516 }
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300517
518 return 0;
519}
520#else /* HAVE_AIO_SUPPORT */
Alexey Budankov93f20c02018-11-06 12:07:19 +0300521static int nr_cblocks_max = 0;
522
Jiri Olsaa5830532019-07-27 20:30:53 +0200523static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused,
Alexey Budankovef781122019-03-18 20:44:12 +0300524 off_t *off __maybe_unused)
Alexey Budankovd3d1af62018-11-06 12:04:58 +0300525{
526 return -1;
527}
528
529static off_t record__aio_get_pos(int trace_fd __maybe_unused)
530{
531 return -1;
532}
533
534static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused)
535{
536}
537
538static void record__aio_mmap_read_sync(struct record *rec __maybe_unused)
539{
540}
541#endif
542
543static int record__aio_enabled(struct record *rec)
544{
545 return rec->opts.nr_cblocks > 0;
546}
547
Alexey Budankov470530b2019-03-18 20:40:26 +0300548#define MMAP_FLUSH_DEFAULT 1
549static int record__mmap_flush_parse(const struct option *opt,
550 const char *str,
551 int unset)
552{
553 int flush_max;
554 struct record_opts *opts = (struct record_opts *)opt->value;
555 static struct parse_tag tags[] = {
556 { .tag = 'B', .mult = 1 },
557 { .tag = 'K', .mult = 1 << 10 },
558 { .tag = 'M', .mult = 1 << 20 },
559 { .tag = 'G', .mult = 1 << 30 },
560 { .tag = 0 },
561 };
562
563 if (unset)
564 return 0;
565
566 if (str) {
567 opts->mmap_flush = parse_tag_value(str, tags);
568 if (opts->mmap_flush == (int)-1)
569 opts->mmap_flush = strtol(str, NULL, 0);
570 }
571
572 if (!opts->mmap_flush)
573 opts->mmap_flush = MMAP_FLUSH_DEFAULT;
574
Jiri Olsa9521b5f2019-07-28 12:45:35 +0200575 flush_max = evlist__mmap_size(opts->mmap_pages);
Alexey Budankov470530b2019-03-18 20:40:26 +0300576 flush_max /= 4;
577 if (opts->mmap_flush > flush_max)
578 opts->mmap_flush = flush_max;
579
580 return 0;
581}
582
Alexey Budankov504c1ad2019-03-18 20:44:42 +0300583#ifdef HAVE_ZSTD_SUPPORT
584static unsigned int comp_level_default = 1;
585
586static int record__parse_comp_level(const struct option *opt, const char *str, int unset)
587{
588 struct record_opts *opts = opt->value;
589
590 if (unset) {
591 opts->comp_level = 0;
592 } else {
593 if (str)
594 opts->comp_level = strtol(str, NULL, 0);
595 if (!opts->comp_level)
596 opts->comp_level = comp_level_default;
597 }
598
599 return 0;
600}
601#endif
Alexey Budankov51255a82019-03-18 20:42:19 +0300602static unsigned int comp_level_max = 22;
603
Alexey Budankov42e1fd82019-03-18 20:41:33 +0300604static int record__comp_enabled(struct record *rec)
605{
606 return rec->opts.comp_level > 0;
607}
608
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200609static int process_synthesized_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200610 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300611 struct perf_sample *sample __maybe_unused,
612 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200613{
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -0300614 struct record *rec = container_of(tool, struct record, tool);
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200615 return record__write(rec, NULL, event, event->header.size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200616}
617
Ian Rogers49c670b172022-08-26 09:42:31 -0700618static struct mutex synth_lock;
619
Stephane Eraniand99c22e2020-04-22 08:50:38 -0700620static int process_locked_synthesized_event(struct perf_tool *tool,
621 union perf_event *event,
622 struct perf_sample *sample __maybe_unused,
623 struct machine *machine __maybe_unused)
624{
Stephane Eraniand99c22e2020-04-22 08:50:38 -0700625 int ret;
626
Ian Rogers49c670b172022-08-26 09:42:31 -0700627 mutex_lock(&synth_lock);
Stephane Eraniand99c22e2020-04-22 08:50:38 -0700628 ret = process_synthesized_event(tool, event, sample, machine);
Ian Rogers49c670b172022-08-26 09:42:31 -0700629 mutex_unlock(&synth_lock);
Stephane Eraniand99c22e2020-04-22 08:50:38 -0700630 return ret;
631}
632
Jiri Olsaa5830532019-07-27 20:30:53 +0200633static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
Arnaldo Carvalho de Melod37f1582017-10-05 16:39:55 -0300634{
635 struct record *rec = to;
636
Alexey Budankov5d7f4112019-03-18 20:43:35 +0300637 if (record__comp_enabled(rec)) {
Alexey Bayduraev75f5f1f2022-01-17 21:34:30 +0300638 size = zstd_compress(rec->session, map, map->data, mmap__mmap_len(map), bf, size);
Alexey Budankov5d7f4112019-03-18 20:43:35 +0300639 bf = map->data;
640 }
641
Alexey Bayduraev396b6262022-01-17 21:34:25 +0300642 thread->samples++;
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200643 return record__write(rec, map, bf, size);
Arnaldo Carvalho de Melod37f1582017-10-05 16:39:55 -0300644}
645
Ian Rogers8ed28c22022-10-24 11:19:07 -0700646static volatile sig_atomic_t signr = -1;
647static volatile sig_atomic_t child_finished;
Anand K Mistryda231332020-05-13 12:20:23 +1000648#ifdef HAVE_EVENTFD_SUPPORT
Ian Rogers8ed28c22022-10-24 11:19:07 -0700649static volatile sig_atomic_t done_fd = -1;
Anand K Mistryda231332020-05-13 12:20:23 +1000650#endif
Wang Nanc0bdc1c2016-04-13 08:21:06 +0000651
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300652static void sig_handler(int sig)
653{
654 if (sig == SIGCHLD)
655 child_finished = 1;
656 else
657 signr = sig;
658
659 done = 1;
Anand K Mistryda231332020-05-13 12:20:23 +1000660#ifdef HAVE_EVENTFD_SUPPORT
Ian Rogers304f0a22022-10-23 18:10:24 -0700661 if (done_fd >= 0) {
662 u64 tmp = 1;
663 int orig_errno = errno;
664
665 /*
666 * It is possible for this signal handler to run after done is
667 * checked in the main loop, but before the perf counter fds are
668 * polled. If this happens, the poll() will continue to wait
669 * even though done is set, and will only break out if either
670 * another signal is received, or the counters are ready for
671 * read. To ensure the poll() doesn't sleep when done is set,
672 * use an eventfd (done_fd) to wake up the poll().
673 */
674 if (write(done_fd, &tmp, sizeof(tmp)) < 0)
675 pr_err("failed to signal wakeup fd, error: %m\n");
676
677 errno = orig_errno;
678 }
Anand K Mistryda231332020-05-13 12:20:23 +1000679#endif // HAVE_EVENTFD_SUPPORT
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300680}
681
Wang Nana0748652016-11-26 07:03:28 +0000682static void sigsegv_handler(int sig)
683{
684 perf_hooks__recover();
685 sighandler_dump_stack(sig);
686}
687
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300688static void record__sig_exit(void)
689{
690 if (signr == -1)
691 return;
692
693 signal(signr, SIG_DFL);
694 raise(signr);
695}
696
Adrian Huntere31f0d02015-04-30 17:37:27 +0300697#ifdef HAVE_AUXTRACE_SUPPORT
698
Adrian Hunteref149c22015-04-09 18:53:45 +0300699static int record__process_auxtrace(struct perf_tool *tool,
Jiri Olsaa5830532019-07-27 20:30:53 +0200700 struct mmap *map,
Adrian Hunteref149c22015-04-09 18:53:45 +0300701 union perf_event *event, void *data1,
702 size_t len1, void *data2, size_t len2)
703{
704 struct record *rec = container_of(tool, struct record, tool);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100705 struct perf_data *data = &rec->data;
Adrian Hunteref149c22015-04-09 18:53:45 +0300706 size_t padding;
707 u8 pad[8] = {0};
708
Adrian Hunter46e201e2019-10-04 11:31:20 +0300709 if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) {
Adrian Hunter99fa2982015-04-30 17:37:25 +0300710 off_t file_offset;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100711 int fd = perf_data__fd(data);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300712 int err;
713
714 file_offset = lseek(fd, 0, SEEK_CUR);
715 if (file_offset == -1)
716 return -1;
717 err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
718 event, file_offset);
719 if (err)
720 return err;
721 }
722
Adrian Hunteref149c22015-04-09 18:53:45 +0300723 /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
724 padding = (len1 + len2) & 7;
725 if (padding)
726 padding = 8 - padding;
727
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200728 record__write(rec, map, event, event->header.size);
729 record__write(rec, map, data1, len1);
Adrian Hunteref149c22015-04-09 18:53:45 +0300730 if (len2)
Jiri Olsaded2b8f2018-09-13 14:54:06 +0200731 record__write(rec, map, data2, len2);
732 record__write(rec, map, &pad, padding);
Adrian Hunteref149c22015-04-09 18:53:45 +0300733
734 return 0;
735}
736
737static int record__auxtrace_mmap_read(struct record *rec,
Jiri Olsaa5830532019-07-27 20:30:53 +0200738 struct mmap *map)
Adrian Hunteref149c22015-04-09 18:53:45 +0300739{
740 int ret;
741
Jiri Olsae035f4c2018-09-13 14:54:05 +0200742 ret = auxtrace_mmap__read(map, rec->itr, &rec->tool,
Adrian Hunteref149c22015-04-09 18:53:45 +0300743 record__process_auxtrace);
744 if (ret < 0)
745 return ret;
746
747 if (ret)
748 rec->samples++;
749
750 return 0;
751}
752
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300753static int record__auxtrace_mmap_read_snapshot(struct record *rec,
Jiri Olsaa5830532019-07-27 20:30:53 +0200754 struct mmap *map)
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300755{
756 int ret;
757
Jiri Olsae035f4c2018-09-13 14:54:05 +0200758 ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool,
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300759 record__process_auxtrace,
760 rec->opts.auxtrace_snapshot_size);
761 if (ret < 0)
762 return ret;
763
764 if (ret)
765 rec->samples++;
766
767 return 0;
768}
769
770static int record__auxtrace_read_snapshot_all(struct record *rec)
771{
772 int i;
773 int rc = 0;
774
Jiri Olsac976ee12019-07-30 13:04:59 +0200775 for (i = 0; i < rec->evlist->core.nr_mmaps; i++) {
Jiri Olsaa5830532019-07-27 20:30:53 +0200776 struct mmap *map = &rec->evlist->mmap[i];
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300777
Jiri Olsae035f4c2018-09-13 14:54:05 +0200778 if (!map->auxtrace_mmap.base)
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300779 continue;
780
Jiri Olsae035f4c2018-09-13 14:54:05 +0200781 if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) {
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300782 rc = -1;
783 goto out;
784 }
785 }
786out:
787 return rc;
788}
789
Alexander Shishkince7b0e42019-08-06 17:41:01 +0300790static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit)
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300791{
792 pr_debug("Recording AUX area tracing snapshot\n");
793 if (record__auxtrace_read_snapshot_all(rec) < 0) {
Wang Nan5f9cf592016-04-20 18:59:49 +0000794 trigger_error(&auxtrace_snapshot_trigger);
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300795 } else {
Alexander Shishkince7b0e42019-08-06 17:41:01 +0300796 if (auxtrace_record__snapshot_finish(rec->itr, on_exit))
Wang Nan5f9cf592016-04-20 18:59:49 +0000797 trigger_error(&auxtrace_snapshot_trigger);
798 else
799 trigger_ready(&auxtrace_snapshot_trigger);
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300800 }
801}
802
Alexander Shishkince7b0e42019-08-06 17:41:01 +0300803static int record__auxtrace_snapshot_exit(struct record *rec)
804{
805 if (trigger_is_error(&auxtrace_snapshot_trigger))
806 return 0;
807
808 if (!auxtrace_record__snapshot_started &&
809 auxtrace_record__snapshot_start(rec->itr))
810 return -1;
811
812 record__read_auxtrace_snapshot(rec, true);
813 if (trigger_is_error(&auxtrace_snapshot_trigger))
814 return -1;
815
816 return 0;
817}
818
Adrian Hunter4b5ea3b2018-03-06 11:13:12 +0200819static int record__auxtrace_init(struct record *rec)
820{
821 int err;
822
Alexey Bayduraevb5f25112022-01-17 21:34:34 +0300823 if ((rec->opts.auxtrace_snapshot_opts || rec->opts.auxtrace_sample_opts)
824 && record__threads_enabled(rec)) {
825 pr_err("AUX area tracing options are not available in parallel streaming mode.\n");
826 return -EINVAL;
827 }
828
Adrian Hunter4b5ea3b2018-03-06 11:13:12 +0200829 if (!rec->itr) {
830 rec->itr = auxtrace_record__init(rec->evlist, &err);
831 if (err)
832 return err;
833 }
834
835 err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
836 rec->opts.auxtrace_snapshot_opts);
837 if (err)
838 return err;
839
Adrian Hunterc0a6de02019-11-15 14:42:16 +0200840 err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts,
841 rec->opts.auxtrace_sample_opts);
842 if (err)
843 return err;
844
Adrian Hunterd58b3f72021-01-21 16:04:18 +0200845 auxtrace_regroup_aux_output(rec->evlist);
846
Adrian Hunter4b5ea3b2018-03-06 11:13:12 +0200847 return auxtrace_parse_filters(rec->evlist);
848}
849
Adrian Huntere31f0d02015-04-30 17:37:27 +0300850#else
851
852static inline
853int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
Jiri Olsaa5830532019-07-27 20:30:53 +0200854 struct mmap *map __maybe_unused)
Adrian Huntere31f0d02015-04-30 17:37:27 +0300855{
856 return 0;
857}
858
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300859static inline
Alexander Shishkince7b0e42019-08-06 17:41:01 +0300860void record__read_auxtrace_snapshot(struct record *rec __maybe_unused,
861 bool on_exit __maybe_unused)
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +0300862{
863}
864
865static inline
866int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
867{
868 return 0;
869}
870
Alexander Shishkince7b0e42019-08-06 17:41:01 +0300871static inline
872int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused)
873{
874 return 0;
875}
876
Adrian Hunter4b5ea3b2018-03-06 11:13:12 +0200877static int record__auxtrace_init(struct record *rec __maybe_unused)
878{
879 return 0;
880}
881
Adrian Huntere31f0d02015-04-30 17:37:27 +0300882#endif
883
Adrian Hunter246eba82020-05-12 15:19:18 +0300884static int record__config_text_poke(struct evlist *evlist)
885{
886 struct evsel *evsel;
Adrian Hunter246eba82020-05-12 15:19:18 +0300887
888 /* Nothing to do if text poke is already configured */
889 evlist__for_each_entry(evlist, evsel) {
890 if (evsel->core.attr.text_poke)
891 return 0;
892 }
893
Adrian Hunter921e3be2022-05-24 10:54:27 +0300894 evsel = evlist__add_dummy_on_all_cpus(evlist);
895 if (!evsel)
896 return -ENOMEM;
Adrian Hunter246eba82020-05-12 15:19:18 +0300897
Adrian Hunter246eba82020-05-12 15:19:18 +0300898 evsel->core.attr.text_poke = 1;
899 evsel->core.attr.ksymbol = 1;
Adrian Hunter246eba82020-05-12 15:19:18 +0300900 evsel->immediate = true;
Adrian Hunter246eba82020-05-12 15:19:18 +0300901 evsel__set_sample_bit(evsel, TIME);
902
903 return 0;
904}
905
Namhyung Kimedc41a12022-05-18 15:47:21 -0700906static int record__config_off_cpu(struct record *rec)
907{
Namhyung Kim685439a2022-05-18 15:47:24 -0700908 return off_cpu_prepare(rec->evlist, &rec->opts.target, &rec->opts);
Namhyung Kimedc41a12022-05-18 15:47:21 -0700909}
910
Adrian Huntereeb399b2019-10-04 11:31:21 +0300911static bool record__kcore_readable(struct machine *machine)
912{
913 char kcore[PATH_MAX];
914 int fd;
915
916 scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir);
917
918 fd = open(kcore, O_RDONLY);
919 if (fd < 0)
920 return false;
921
922 close(fd);
923
924 return true;
925}
926
927static int record__kcore_copy(struct machine *machine, struct perf_data *data)
928{
929 char from_dir[PATH_MAX];
930 char kcore_dir[PATH_MAX];
931 int ret;
932
933 snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir);
934
935 ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir));
936 if (ret)
937 return ret;
938
939 return kcore_copy(from_dir, kcore_dir);
940}
941
Alexey Bayduraev415ccb52022-01-17 21:34:23 +0300942static void record__thread_data_init_pipes(struct record_thread *thread_data)
943{
944 thread_data->pipes.msg[0] = -1;
945 thread_data->pipes.msg[1] = -1;
946 thread_data->pipes.ack[0] = -1;
947 thread_data->pipes.ack[1] = -1;
948}
949
950static int record__thread_data_open_pipes(struct record_thread *thread_data)
951{
952 if (pipe(thread_data->pipes.msg))
953 return -EINVAL;
954
955 if (pipe(thread_data->pipes.ack)) {
956 close(thread_data->pipes.msg[0]);
957 thread_data->pipes.msg[0] = -1;
958 close(thread_data->pipes.msg[1]);
959 thread_data->pipes.msg[1] = -1;
960 return -EINVAL;
961 }
962
963 pr_debug2("thread_data[%p]: msg=[%d,%d], ack=[%d,%d]\n", thread_data,
964 thread_data->pipes.msg[0], thread_data->pipes.msg[1],
965 thread_data->pipes.ack[0], thread_data->pipes.ack[1]);
966
967 return 0;
968}
969
970static void record__thread_data_close_pipes(struct record_thread *thread_data)
971{
972 if (thread_data->pipes.msg[0] != -1) {
973 close(thread_data->pipes.msg[0]);
974 thread_data->pipes.msg[0] = -1;
975 }
976 if (thread_data->pipes.msg[1] != -1) {
977 close(thread_data->pipes.msg[1]);
978 thread_data->pipes.msg[1] = -1;
979 }
980 if (thread_data->pipes.ack[0] != -1) {
981 close(thread_data->pipes.ack[0]);
982 thread_data->pipes.ack[0] = -1;
983 }
984 if (thread_data->pipes.ack[1] != -1) {
985 close(thread_data->pipes.ack[1]);
986 thread_data->pipes.ack[1] = -1;
987 }
988}
989
Adrian Hunter7be1fed2022-05-24 10:54:30 +0300990static bool evlist__per_thread(struct evlist *evlist)
991{
992 return cpu_map__is_dummy(evlist->core.user_requested_cpus);
993}
994
Alexey Bayduraev415ccb52022-01-17 21:34:23 +0300995static int record__thread_data_init_maps(struct record_thread *thread_data, struct evlist *evlist)
996{
997 int m, tm, nr_mmaps = evlist->core.nr_mmaps;
998 struct mmap *mmap = evlist->mmap;
999 struct mmap *overwrite_mmap = evlist->overwrite_mmap;
Adrian Hunter7be1fed2022-05-24 10:54:30 +03001000 struct perf_cpu_map *cpus = evlist->core.all_cpus;
1001 bool per_thread = evlist__per_thread(evlist);
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03001002
Adrian Hunter7be1fed2022-05-24 10:54:30 +03001003 if (per_thread)
Alexey Bayduraev23380e42022-04-13 18:46:40 -07001004 thread_data->nr_mmaps = nr_mmaps;
1005 else
1006 thread_data->nr_mmaps = bitmap_weight(thread_data->mask->maps.bits,
1007 thread_data->mask->maps.nbits);
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03001008 if (mmap) {
1009 thread_data->maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
1010 if (!thread_data->maps)
1011 return -ENOMEM;
1012 }
1013 if (overwrite_mmap) {
1014 thread_data->overwrite_maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *));
1015 if (!thread_data->overwrite_maps) {
1016 zfree(&thread_data->maps);
1017 return -ENOMEM;
1018 }
1019 }
1020 pr_debug2("thread_data[%p]: nr_mmaps=%d, maps=%p, ow_maps=%p\n", thread_data,
1021 thread_data->nr_mmaps, thread_data->maps, thread_data->overwrite_maps);
1022
1023 for (m = 0, tm = 0; m < nr_mmaps && tm < thread_data->nr_mmaps; m++) {
Adrian Hunter7be1fed2022-05-24 10:54:30 +03001024 if (per_thread ||
Ian Rogers02555712022-05-02 21:17:52 -07001025 test_bit(perf_cpu_map__cpu(cpus, m).cpu, thread_data->mask->maps.bits)) {
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03001026 if (thread_data->maps) {
1027 thread_data->maps[tm] = &mmap[m];
1028 pr_debug2("thread_data[%p]: cpu%d: maps[%d] -> mmap[%d]\n",
Alexey Bayduraev23380e42022-04-13 18:46:40 -07001029 thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m);
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03001030 }
1031 if (thread_data->overwrite_maps) {
1032 thread_data->overwrite_maps[tm] = &overwrite_mmap[m];
1033 pr_debug2("thread_data[%p]: cpu%d: ow_maps[%d] -> ow_mmap[%d]\n",
Alexey Bayduraev23380e42022-04-13 18:46:40 -07001034 thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m);
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03001035 }
1036 tm++;
1037 }
1038 }
1039
1040 return 0;
1041}
1042
1043static int record__thread_data_init_pollfd(struct record_thread *thread_data, struct evlist *evlist)
1044{
1045 int f, tm, pos;
1046 struct mmap *map, *overwrite_map;
1047
1048 fdarray__init(&thread_data->pollfd, 64);
1049
1050 for (tm = 0; tm < thread_data->nr_mmaps; tm++) {
1051 map = thread_data->maps ? thread_data->maps[tm] : NULL;
1052 overwrite_map = thread_data->overwrite_maps ?
1053 thread_data->overwrite_maps[tm] : NULL;
1054
1055 for (f = 0; f < evlist->core.pollfd.nr; f++) {
1056 void *ptr = evlist->core.pollfd.priv[f].ptr;
1057
1058 if ((map && ptr == map) || (overwrite_map && ptr == overwrite_map)) {
1059 pos = fdarray__dup_entry_from(&thread_data->pollfd, f,
1060 &evlist->core.pollfd);
1061 if (pos < 0)
1062 return pos;
1063 pr_debug2("thread_data[%p]: pollfd[%d] <- event_fd=%d\n",
1064 thread_data, pos, evlist->core.pollfd.entries[f].fd);
1065 }
1066 }
1067 }
1068
1069 return 0;
1070}
1071
1072static void record__free_thread_data(struct record *rec)
1073{
1074 int t;
1075 struct record_thread *thread_data = rec->thread_data;
1076
1077 if (thread_data == NULL)
1078 return;
1079
1080 for (t = 0; t < rec->nr_threads; t++) {
1081 record__thread_data_close_pipes(&thread_data[t]);
1082 zfree(&thread_data[t].maps);
1083 zfree(&thread_data[t].overwrite_maps);
1084 fdarray__exit(&thread_data[t].pollfd);
1085 }
1086
1087 zfree(&rec->thread_data);
1088}
1089
Adrian Hunter6562c9a2022-08-24 10:28:10 +03001090static int record__map_thread_evlist_pollfd_indexes(struct record *rec,
1091 int evlist_pollfd_index,
1092 int thread_pollfd_index)
1093{
1094 size_t x = rec->index_map_cnt;
1095
1096 if (realloc_array_as_needed(rec->index_map, rec->index_map_sz, x, NULL))
1097 return -ENOMEM;
1098 rec->index_map[x].evlist_pollfd_index = evlist_pollfd_index;
1099 rec->index_map[x].thread_pollfd_index = thread_pollfd_index;
1100 rec->index_map_cnt += 1;
1101 return 0;
1102}
1103
1104static int record__update_evlist_pollfd_from_thread(struct record *rec,
1105 struct evlist *evlist,
1106 struct record_thread *thread_data)
1107{
1108 struct pollfd *e_entries = evlist->core.pollfd.entries;
1109 struct pollfd *t_entries = thread_data->pollfd.entries;
1110 int err = 0;
1111 size_t i;
1112
1113 for (i = 0; i < rec->index_map_cnt; i++) {
1114 int e_pos = rec->index_map[i].evlist_pollfd_index;
1115 int t_pos = rec->index_map[i].thread_pollfd_index;
1116
1117 if (e_entries[e_pos].fd != t_entries[t_pos].fd ||
1118 e_entries[e_pos].events != t_entries[t_pos].events) {
1119 pr_err("Thread and evlist pollfd index mismatch\n");
1120 err = -EINVAL;
1121 continue;
1122 }
1123 e_entries[e_pos].revents = t_entries[t_pos].revents;
1124 }
1125 return err;
1126}
1127
1128static int record__dup_non_perf_events(struct record *rec,
1129 struct evlist *evlist,
1130 struct record_thread *thread_data)
1131{
1132 struct fdarray *fda = &evlist->core.pollfd;
1133 int i, ret;
1134
1135 for (i = 0; i < fda->nr; i++) {
1136 if (!(fda->priv[i].flags & fdarray_flag__non_perf_event))
1137 continue;
1138 ret = fdarray__dup_entry_from(&thread_data->pollfd, i, fda);
1139 if (ret < 0) {
1140 pr_err("Failed to duplicate descriptor in main thread pollfd\n");
1141 return ret;
1142 }
1143 pr_debug2("thread_data[%p]: pollfd[%d] <- non_perf_event fd=%d\n",
1144 thread_data, ret, fda->entries[i].fd);
1145 ret = record__map_thread_evlist_pollfd_indexes(rec, i, ret);
1146 if (ret < 0) {
1147 pr_err("Failed to map thread and evlist pollfd indexes\n");
1148 return ret;
1149 }
1150 }
1151 return 0;
1152}
1153
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03001154static int record__alloc_thread_data(struct record *rec, struct evlist *evlist)
1155{
1156 int t, ret;
1157 struct record_thread *thread_data;
1158
1159 rec->thread_data = zalloc(rec->nr_threads * sizeof(*(rec->thread_data)));
1160 if (!rec->thread_data) {
1161 pr_err("Failed to allocate thread data\n");
1162 return -ENOMEM;
1163 }
1164 thread_data = rec->thread_data;
1165
1166 for (t = 0; t < rec->nr_threads; t++)
1167 record__thread_data_init_pipes(&thread_data[t]);
1168
1169 for (t = 0; t < rec->nr_threads; t++) {
1170 thread_data[t].rec = rec;
1171 thread_data[t].mask = &rec->thread_masks[t];
1172 ret = record__thread_data_init_maps(&thread_data[t], evlist);
1173 if (ret) {
1174 pr_err("Failed to initialize thread[%d] maps\n", t);
1175 goto out_free;
1176 }
1177 ret = record__thread_data_init_pollfd(&thread_data[t], evlist);
1178 if (ret) {
1179 pr_err("Failed to initialize thread[%d] pollfd\n", t);
1180 goto out_free;
1181 }
1182 if (t) {
1183 thread_data[t].tid = -1;
1184 ret = record__thread_data_open_pipes(&thread_data[t]);
1185 if (ret) {
1186 pr_err("Failed to open thread[%d] communication pipes\n", t);
1187 goto out_free;
1188 }
1189 ret = fdarray__add(&thread_data[t].pollfd, thread_data[t].pipes.msg[0],
1190 POLLIN | POLLERR | POLLHUP, fdarray_flag__nonfilterable);
1191 if (ret < 0) {
1192 pr_err("Failed to add descriptor to thread[%d] pollfd\n", t);
1193 goto out_free;
1194 }
1195 thread_data[t].ctlfd_pos = ret;
1196 pr_debug2("thread_data[%p]: pollfd[%d] <- ctl_fd=%d\n",
1197 thread_data, thread_data[t].ctlfd_pos,
1198 thread_data[t].pipes.msg[0]);
1199 } else {
1200 thread_data[t].tid = gettid();
Adrian Hunter6562c9a2022-08-24 10:28:10 +03001201
1202 ret = record__dup_non_perf_events(rec, evlist, &thread_data[t]);
1203 if (ret < 0)
1204 goto out_free;
1205
Adrian Hunterfeff0b62022-08-24 10:28:12 +03001206 thread_data[t].ctlfd_pos = -1; /* Not used */
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03001207 }
1208 }
1209
1210 return 0;
1211
1212out_free:
1213 record__free_thread_data(rec);
1214
1215 return ret;
1216}
1217
Wang Nancda57a82016-06-27 10:24:03 +00001218static int record__mmap_evlist(struct record *rec,
Jiri Olsa63503db2019-07-21 13:23:52 +02001219 struct evlist *evlist)
Wang Nancda57a82016-06-27 10:24:03 +00001220{
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001221 int i, ret;
Wang Nancda57a82016-06-27 10:24:03 +00001222 struct record_opts *opts = &rec->opts;
Adrian Hunterc0a6de02019-11-15 14:42:16 +02001223 bool auxtrace_overwrite = opts->auxtrace_snapshot_mode ||
1224 opts->auxtrace_sample_mode;
Wang Nancda57a82016-06-27 10:24:03 +00001225 char msg[512];
1226
Alexey Budankovf13de662019-01-22 20:50:57 +03001227 if (opts->affinity != PERF_AFFINITY_SYS)
1228 cpu__setup_cpunode_map();
1229
Jiri Olsa9521b5f2019-07-28 12:45:35 +02001230 if (evlist__mmap_ex(evlist, opts->mmap_pages,
Wang Nancda57a82016-06-27 10:24:03 +00001231 opts->auxtrace_mmap_pages,
Adrian Hunterc0a6de02019-11-15 14:42:16 +02001232 auxtrace_overwrite,
Alexey Budankov470530b2019-03-18 20:40:26 +03001233 opts->nr_cblocks, opts->affinity,
Alexey Budankov51255a82019-03-18 20:42:19 +03001234 opts->mmap_flush, opts->comp_level) < 0) {
Wang Nancda57a82016-06-27 10:24:03 +00001235 if (errno == EPERM) {
1236 pr_err("Permission error mapping pages.\n"
1237 "Consider increasing "
1238 "/proc/sys/kernel/perf_event_mlock_kb,\n"
1239 "or try again with a smaller value of -m/--mmap_pages.\n"
1240 "(current value: %u,%u)\n",
1241 opts->mmap_pages, opts->auxtrace_mmap_pages);
1242 return -errno;
1243 } else {
1244 pr_err("failed to mmap with %d (%s)\n", errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001245 str_error_r(errno, msg, sizeof(msg)));
Wang Nancda57a82016-06-27 10:24:03 +00001246 if (errno)
1247 return -errno;
1248 else
1249 return -EINVAL;
1250 }
1251 }
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03001252
1253 if (evlist__initialize_ctlfd(evlist, opts->ctl_fd, opts->ctl_fd_ack))
1254 return -1;
1255
1256 ret = record__alloc_thread_data(rec, evlist);
1257 if (ret)
1258 return ret;
1259
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001260 if (record__threads_enabled(rec)) {
1261 ret = perf_data__create_dir(&rec->data, evlist->core.nr_mmaps);
Alexey Bayduraev65e7c962022-02-22 12:14:17 +03001262 if (ret) {
1263 pr_err("Failed to create data directory: %s\n", strerror(-ret));
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001264 return ret;
Alexey Bayduraev65e7c962022-02-22 12:14:17 +03001265 }
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001266 for (i = 0; i < evlist->core.nr_mmaps; i++) {
1267 if (evlist->mmap)
1268 evlist->mmap[i].file = &rec->data.dir.files[i];
1269 if (evlist->overwrite_mmap)
1270 evlist->overwrite_mmap[i].file = &rec->data.dir.files[i];
1271 }
1272 }
1273
Wang Nancda57a82016-06-27 10:24:03 +00001274 return 0;
1275}
1276
1277static int record__mmap(struct record *rec)
1278{
1279 return record__mmap_evlist(rec, rec->evlist);
1280}
1281
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03001282static int record__open(struct record *rec)
Arnaldo Carvalho de Melodd7927f2011-01-12 14:28:51 -02001283{
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -03001284 char msg[BUFSIZ];
Jiri Olsa32dcd022019-07-21 13:23:51 +02001285 struct evsel *pos;
Jiri Olsa63503db2019-07-21 13:23:52 +02001286 struct evlist *evlist = rec->evlist;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001287 struct perf_session *session = rec->session;
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03001288 struct record_opts *opts = &rec->opts;
David Ahern8d3eca22012-08-26 12:24:47 -06001289 int rc = 0;
Arnaldo Carvalho de Melodd7927f2011-01-12 14:28:51 -02001290
Arnaldo Carvalho de Melod3dbf432017-11-03 15:34:34 -03001291 /*
Kan Liangb91e5492021-07-08 09:03:32 -07001292 * For initial_delay, system wide or a hybrid system, we need to add a
1293 * dummy event so that we can track PERF_RECORD_MMAP to cover the delay
1294 * of waiting or event synthesis.
Arnaldo Carvalho de Melod3dbf432017-11-03 15:34:34 -03001295 */
Changbin Ducb4b9e62023-03-02 11:11:45 +08001296 if (opts->target.initial_delay || target__has_cpu(&opts->target) ||
Kan Liangb91e5492021-07-08 09:03:32 -07001297 perf_pmu__has_hybrid()) {
Arnaldo Carvalho de Meloe80db252020-11-30 14:39:41 -03001298 pos = evlist__get_tracking_event(evlist);
Adrian Hunter442ad2252020-06-29 12:19:51 +03001299 if (!evsel__is_dummy_event(pos)) {
1300 /* Set up dummy event. */
Arnaldo Carvalho de Melofacbf0b2020-07-08 13:49:15 -03001301 if (evlist__add_dummy(evlist))
Adrian Hunter442ad2252020-06-29 12:19:51 +03001302 return -ENOMEM;
1303 pos = evlist__last(evlist);
Arnaldo Carvalho de Meloe80db252020-11-30 14:39:41 -03001304 evlist__set_tracking_event(evlist, pos);
Adrian Hunter442ad2252020-06-29 12:19:51 +03001305 }
Arnaldo Carvalho de Melod3dbf432017-11-03 15:34:34 -03001306
Ian Rogers0a892c12020-04-22 10:36:15 -07001307 /*
1308 * Enable the dummy event when the process is forked for
1309 * initial_delay, immediately for system wide.
1310 */
Changbin Ducb4b9e62023-03-02 11:11:45 +08001311 if (opts->target.initial_delay && !pos->immediate &&
Namhyung Kimbb07d622021-08-27 16:32:12 -07001312 !target__has_cpu(&opts->target))
Ian Rogers0a892c12020-04-22 10:36:15 -07001313 pos->core.attr.enable_on_exec = 1;
1314 else
1315 pos->immediate = 1;
Arnaldo Carvalho de Melod3dbf432017-11-03 15:34:34 -03001316 }
1317
Arnaldo Carvalho de Melo78e1bc22020-11-30 15:07:49 -03001318 evlist__config(evlist, opts, &callchain_param);
Jiri Olsacac21422012-11-12 18:34:00 +01001319
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001320 evlist__for_each_entry(evlist, pos) {
Ingo Molnar3da297a2009-06-07 17:39:02 +02001321try_again:
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001322 if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
Arnaldo Carvalho de Meloae430892020-04-30 11:46:15 -03001323 if (evsel__fallback(pos, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001324 if (verbose > 0)
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001325 ui__warning("%s\n", msg);
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001326 goto try_again;
1327 }
Andi Kleencf99ad12018-10-01 12:59:27 -07001328 if ((errno == EINVAL || errno == EBADF) &&
Jiri Olsafba7c862021-07-06 17:17:00 +02001329 pos->core.leader != &pos->core &&
Andi Kleencf99ad12018-10-01 12:59:27 -07001330 pos->weak_group) {
Arnaldo Carvalho de Melo64b47782020-11-30 14:58:32 -03001331 pos = evlist__reset_weak_group(evlist, pos, true);
Andi Kleencf99ad12018-10-01 12:59:27 -07001332 goto try_again;
1333 }
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001334 rc = -errno;
Arnaldo Carvalho de Melo2bb72db2020-05-04 13:43:03 -03001335 evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001336 ui__error("%s\n", msg);
David Ahern8d3eca22012-08-26 12:24:47 -06001337 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001338 }
Andi Kleenbfd8f722017-11-17 13:42:58 -08001339
1340 pos->supported = true;
Li Zefanc171b552009-10-15 11:22:07 +08001341 }
Arnaldo Carvalho de Meloa43d3f02010-12-25 12:12:25 -02001342
Arnaldo Carvalho de Melo78e1bc22020-11-30 15:07:49 -03001343 if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) {
Arnaldo Carvalho de Meloc8b567c2019-09-23 11:07:29 -03001344 pr_warning(
1345"WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
1346"check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
1347"Samples in kernel functions may not be resolved if a suitable vmlinux\n"
1348"file is not found in the buildid cache or in the vmlinux path.\n\n"
1349"Samples in kernel modules won't be resolved at all.\n\n"
1350"If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
1351"even with a suitable vmlinux or kallsyms file.\n\n");
1352 }
1353
Arnaldo Carvalho de Melo24bf91a2020-11-30 09:38:02 -03001354 if (evlist__apply_filters(evlist, &pos)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001355 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Namhyung Kim4310551b2023-03-14 16:42:36 -07001356 pos->filter ?: "BPF", evsel__name(pos), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001357 str_error_r(errno, msg, sizeof(msg)));
David Ahern8d3eca22012-08-26 12:24:47 -06001358 rc = -1;
1359 goto out;
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001360 }
1361
Wang Nancda57a82016-06-27 10:24:03 +00001362 rc = record__mmap(rec);
1363 if (rc)
David Ahern8d3eca22012-08-26 12:24:47 -06001364 goto out;
Arnaldo Carvalho de Melo0a27d7f2011-01-14 15:50:51 -02001365
Jiri Olsa563aecb2013-06-05 13:35:06 +02001366 session->evlist = evlist;
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001367 perf_session__set_id_hdr_size(session);
David Ahern8d3eca22012-08-26 12:24:47 -06001368out:
1369 return rc;
Peter Zijlstra16c8a102009-05-05 17:50:27 +02001370}
1371
Adrian Hunter66286ed2021-05-03 09:42:22 +03001372static void set_timestamp_boundary(struct record *rec, u64 sample_time)
1373{
1374 if (rec->evlist->first_sample_time == 0)
1375 rec->evlist->first_sample_time = sample_time;
1376
1377 if (sample_time)
1378 rec->evlist->last_sample_time = sample_time;
1379}
1380
Namhyung Kime3d59112015-01-29 17:06:44 +09001381static int process_sample_event(struct perf_tool *tool,
1382 union perf_event *event,
1383 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001384 struct evsel *evsel,
Namhyung Kime3d59112015-01-29 17:06:44 +09001385 struct machine *machine)
1386{
1387 struct record *rec = container_of(tool, struct record, tool);
1388
Adrian Hunter66286ed2021-05-03 09:42:22 +03001389 set_timestamp_boundary(rec, sample->time);
Jin Yao68588ba2017-12-08 21:13:42 +08001390
1391 if (rec->buildid_all)
1392 return 0;
1393
1394 rec->samples++;
Namhyung Kime3d59112015-01-29 17:06:44 +09001395 return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
1396}
1397
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03001398static int process_buildids(struct record *rec)
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001399{
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001400 struct perf_session *session = rec->session;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001401
Jiri Olsa45112e82019-02-21 10:41:29 +01001402 if (perf_data__size(&rec->data) == 0)
Arnaldo Carvalho de Melo9f591fd2010-03-11 15:53:11 -03001403 return 0;
1404
Namhyung Kim00dc8652014-11-04 10:14:32 +09001405 /*
1406 * During this process, it'll load kernel map and replace the
1407 * dso->long_name to a real pathname it found. In this case
1408 * we prefer the vmlinux path like
1409 * /lib/modules/3.16.4/build/vmlinux
1410 *
1411 * rather than build-id path (in debug directory).
1412 * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
1413 */
1414 symbol_conf.ignore_vmlinux_buildid = true;
1415
Namhyung Kim61566812016-01-11 22:37:09 +09001416 /*
1417 * If --buildid-all is given, it marks all DSO regardless of hits,
Jin Yao68588ba2017-12-08 21:13:42 +08001418 * so no need to process samples. But if timestamp_boundary is enabled,
1419 * it still needs to walk on all samples to get the timestamps of
1420 * first/last samples.
Namhyung Kim61566812016-01-11 22:37:09 +09001421 */
Jin Yao68588ba2017-12-08 21:13:42 +08001422 if (rec->buildid_all && !rec->timestamp_boundary)
Namhyung Kim61566812016-01-11 22:37:09 +09001423 rec->tool.sample = NULL;
1424
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001425 return perf_session__process_events(session);
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001426}
1427
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001428static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001429{
1430 int err;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001431 struct perf_tool *tool = data;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001432 /*
1433 *As for guest kernel when processing subcommand record&report,
1434 *we arrange module mmap prior to guest kernel mmap and trigger
1435 *a preload dso because default guest module symbols are loaded
1436 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
1437 *method is used to avoid symbol missing when the first addr is
1438 *in module instead of in guest kernel.
1439 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001440 err = perf_event__synthesize_modules(tool, process_synthesized_event,
Arnaldo Carvalho de Melo743eb8682011-11-28 07:56:39 -02001441 machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001442 if (err < 0)
1443 pr_err("Couldn't record guest kernel [%d]'s reference"
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001444 " relocation symbol.\n", machine->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001445
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001446 /*
1447 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
1448 * have no _text sometimes.
1449 */
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001450 err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
Adrian Hunter0ae617b2014-01-29 16:14:40 +02001451 machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001452 if (err < 0)
1453 pr_err("Couldn't record guest kernel [%d]'s reference"
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001454 " relocation symbol.\n", machine->pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001455}
1456
Frederic Weisbecker98402802010-05-02 22:05:29 +02001457static struct perf_event_header finished_round_event = {
1458 .size = sizeof(struct perf_event_header),
1459 .type = PERF_RECORD_FINISHED_ROUND,
1460};
1461
Adrian Hunter3812d292022-06-10 14:33:15 +03001462static struct perf_event_header finished_init_event = {
1463 .size = sizeof(struct perf_event_header),
1464 .type = PERF_RECORD_FINISHED_INIT,
1465};
1466
Jiri Olsaa5830532019-07-27 20:30:53 +02001467static void record__adjust_affinity(struct record *rec, struct mmap *map)
Alexey Budankovf13de662019-01-22 20:50:57 +03001468{
1469 if (rec->opts.affinity != PERF_AFFINITY_SYS &&
Alexey Bayduraev396b6262022-01-17 21:34:25 +03001470 !bitmap_equal(thread->mask->affinity.bits, map->affinity_mask.bits,
1471 thread->mask->affinity.nbits)) {
1472 bitmap_zero(thread->mask->affinity.bits, thread->mask->affinity.nbits);
1473 bitmap_or(thread->mask->affinity.bits, thread->mask->affinity.bits,
1474 map->affinity_mask.bits, thread->mask->affinity.nbits);
1475 sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity),
1476 (cpu_set_t *)thread->mask->affinity.bits);
1477 if (verbose == 2) {
1478 pr_debug("threads[%d]: running on cpu%d: ", thread->tid, sched_getcpu());
1479 mmap_cpu_mask__scnprintf(&thread->mask->affinity, "affinity");
1480 }
Alexey Budankovf13de662019-01-22 20:50:57 +03001481 }
1482}
1483
Alexey Budankov5d7f4112019-03-18 20:43:35 +03001484static size_t process_comp_header(void *record, size_t increment)
1485{
Jiri Olsa72932372019-08-28 15:57:16 +02001486 struct perf_record_compressed *event = record;
Alexey Budankov5d7f4112019-03-18 20:43:35 +03001487 size_t size = sizeof(*event);
1488
1489 if (increment) {
1490 event->header.size += increment;
1491 return increment;
1492 }
1493
1494 event->header.type = PERF_RECORD_COMPRESSED;
1495 event->header.size = size;
1496
1497 return size;
1498}
1499
Alexey Bayduraev75f5f1f2022-01-17 21:34:30 +03001500static size_t zstd_compress(struct perf_session *session, struct mmap *map,
1501 void *dst, size_t dst_size, void *src, size_t src_size)
Alexey Budankov5d7f4112019-03-18 20:43:35 +03001502{
1503 size_t compressed;
Jiri Olsa72932372019-08-28 15:57:16 +02001504 size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
Alexey Bayduraev75f5f1f2022-01-17 21:34:30 +03001505 struct zstd_data *zstd_data = &session->zstd_data;
Alexey Budankov5d7f4112019-03-18 20:43:35 +03001506
Alexey Bayduraev75f5f1f2022-01-17 21:34:30 +03001507 if (map && map->file)
1508 zstd_data = &map->zstd_data;
1509
1510 compressed = zstd_compress_stream_to_records(zstd_data, dst, dst_size, src, src_size,
Alexey Budankov5d7f4112019-03-18 20:43:35 +03001511 max_record_size, process_comp_header);
1512
Alexey Bayduraev610fbc02022-01-17 21:34:31 +03001513 if (map && map->file) {
1514 thread->bytes_transferred += src_size;
1515 thread->bytes_compressed += compressed;
1516 } else {
1517 session->bytes_transferred += src_size;
1518 session->bytes_compressed += compressed;
1519 }
Alexey Budankov5d7f4112019-03-18 20:43:35 +03001520
1521 return compressed;
1522}
1523
Jiri Olsa63503db2019-07-21 13:23:52 +02001524static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist,
Alexey Budankov470530b2019-03-18 20:40:26 +03001525 bool overwrite, bool synch)
Frederic Weisbecker98402802010-05-02 22:05:29 +02001526{
Jiri Olsadcabb502014-07-25 16:56:16 +02001527 u64 bytes_written = rec->bytes_written;
Peter Zijlstra0e2e63d2010-05-20 14:45:26 +02001528 int i;
David Ahern8d3eca22012-08-26 12:24:47 -06001529 int rc = 0;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03001530 int nr_mmaps;
1531 struct mmap **maps;
Alexey Budankovd3d1af62018-11-06 12:04:58 +03001532 int trace_fd = rec->data.file.fd;
Alexey Budankovef781122019-03-18 20:44:12 +03001533 off_t off = 0;
Frederic Weisbecker98402802010-05-02 22:05:29 +02001534
Wang Nancb216862016-06-27 10:24:04 +00001535 if (!evlist)
1536 return 0;
Adrian Hunteref149c22015-04-09 18:53:45 +03001537
Alexey Bayduraev396b6262022-01-17 21:34:25 +03001538 nr_mmaps = thread->nr_mmaps;
1539 maps = overwrite ? thread->overwrite_maps : thread->maps;
1540
Wang Nana4ea0ec2016-07-14 08:34:36 +00001541 if (!maps)
1542 return 0;
Wang Nancb216862016-06-27 10:24:04 +00001543
Wang Nan0b72d692017-12-04 16:51:07 +00001544 if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
Wang Nan54cc54d2016-07-14 08:34:42 +00001545 return 0;
1546
Alexey Budankovd3d1af62018-11-06 12:04:58 +03001547 if (record__aio_enabled(rec))
1548 off = record__aio_get_pos(trace_fd);
1549
Alexey Bayduraev396b6262022-01-17 21:34:25 +03001550 for (i = 0; i < nr_mmaps; i++) {
Alexey Budankov470530b2019-03-18 20:40:26 +03001551 u64 flush = 0;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03001552 struct mmap *map = maps[i];
Wang Nana4ea0ec2016-07-14 08:34:36 +00001553
Jiri Olsa547740f2019-07-27 22:07:44 +02001554 if (map->core.base) {
Alexey Budankovf13de662019-01-22 20:50:57 +03001555 record__adjust_affinity(rec, map);
Alexey Budankov470530b2019-03-18 20:40:26 +03001556 if (synch) {
Jiri Olsa65aa2e62019-08-27 16:05:18 +02001557 flush = map->core.flush;
1558 map->core.flush = 1;
Alexey Budankov470530b2019-03-18 20:40:26 +03001559 }
Alexey Budankovd3d1af62018-11-06 12:04:58 +03001560 if (!record__aio_enabled(rec)) {
Alexey Budankovef781122019-03-18 20:44:12 +03001561 if (perf_mmap__push(map, rec, record__pushfn) < 0) {
Alexey Budankov470530b2019-03-18 20:40:26 +03001562 if (synch)
Jiri Olsa65aa2e62019-08-27 16:05:18 +02001563 map->core.flush = flush;
Alexey Budankovd3d1af62018-11-06 12:04:58 +03001564 rc = -1;
1565 goto out;
1566 }
1567 } else {
Alexey Budankovef781122019-03-18 20:44:12 +03001568 if (record__aio_push(rec, map, &off) < 0) {
Alexey Budankovd3d1af62018-11-06 12:04:58 +03001569 record__aio_set_pos(trace_fd, off);
Alexey Budankov470530b2019-03-18 20:40:26 +03001570 if (synch)
Jiri Olsa65aa2e62019-08-27 16:05:18 +02001571 map->core.flush = flush;
Alexey Budankovd3d1af62018-11-06 12:04:58 +03001572 rc = -1;
1573 goto out;
1574 }
David Ahern8d3eca22012-08-26 12:24:47 -06001575 }
Alexey Budankov470530b2019-03-18 20:40:26 +03001576 if (synch)
Jiri Olsa65aa2e62019-08-27 16:05:18 +02001577 map->core.flush = flush;
David Ahern8d3eca22012-08-26 12:24:47 -06001578 }
Adrian Hunteref149c22015-04-09 18:53:45 +03001579
Jiri Olsae035f4c2018-09-13 14:54:05 +02001580 if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
Adrian Hunterc0a6de02019-11-15 14:42:16 +02001581 !rec->opts.auxtrace_sample_mode &&
Jiri Olsae035f4c2018-09-13 14:54:05 +02001582 record__auxtrace_mmap_read(rec, map) != 0) {
Adrian Hunteref149c22015-04-09 18:53:45 +03001583 rc = -1;
1584 goto out;
1585 }
Frederic Weisbecker98402802010-05-02 22:05:29 +02001586 }
1587
Alexey Budankovd3d1af62018-11-06 12:04:58 +03001588 if (record__aio_enabled(rec))
1589 record__aio_set_pos(trace_fd, off);
1590
Jiri Olsadcabb502014-07-25 16:56:16 +02001591 /*
1592 * Mark the round finished in case we wrote
1593 * at least one event.
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001594 *
1595 * No need for round events in directory mode,
1596 * because per-cpu maps and files have data
1597 * sorted by kernel.
Jiri Olsadcabb502014-07-25 16:56:16 +02001598 */
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001599 if (!record__threads_enabled(rec) && bytes_written != rec->bytes_written)
Jiri Olsaded2b8f2018-09-13 14:54:06 +02001600 rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event));
David Ahern8d3eca22012-08-26 12:24:47 -06001601
Wang Nan0b72d692017-12-04 16:51:07 +00001602 if (overwrite)
Arnaldo Carvalho de Meloade9d202020-11-30 09:33:55 -03001603 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
David Ahern8d3eca22012-08-26 12:24:47 -06001604out:
1605 return rc;
Frederic Weisbecker98402802010-05-02 22:05:29 +02001606}
1607
Alexey Budankov470530b2019-03-18 20:40:26 +03001608static int record__mmap_read_all(struct record *rec, bool synch)
Wang Nancb216862016-06-27 10:24:04 +00001609{
1610 int err;
1611
Alexey Budankov470530b2019-03-18 20:40:26 +03001612 err = record__mmap_read_evlist(rec, rec->evlist, false, synch);
Wang Nancb216862016-06-27 10:24:04 +00001613 if (err)
1614 return err;
1615
Alexey Budankov470530b2019-03-18 20:40:26 +03001616 return record__mmap_read_evlist(rec, rec->evlist, true, synch);
Wang Nancb216862016-06-27 10:24:04 +00001617}
1618
Alexey Bayduraev396b6262022-01-17 21:34:25 +03001619static void record__thread_munmap_filtered(struct fdarray *fda, int fd,
1620 void *arg __maybe_unused)
1621{
1622 struct perf_mmap *map = fda->priv[fd].ptr;
1623
1624 if (map)
1625 perf_mmap__put(map);
1626}
1627
Alexey Bayduraev3217e9f2022-01-17 21:34:27 +03001628static void *record__thread(void *arg)
1629{
1630 enum thread_msg msg = THREAD_MSG__READY;
1631 bool terminate = false;
1632 struct fdarray *pollfd;
1633 int err, ctlfd_pos;
1634
1635 thread = arg;
1636 thread->tid = gettid();
1637
1638 err = write(thread->pipes.ack[1], &msg, sizeof(msg));
1639 if (err == -1)
1640 pr_warning("threads[%d]: failed to notify on start: %s\n",
1641 thread->tid, strerror(errno));
1642
1643 pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu());
1644
1645 pollfd = &thread->pollfd;
1646 ctlfd_pos = thread->ctlfd_pos;
1647
1648 for (;;) {
1649 unsigned long long hits = thread->samples;
1650
1651 if (record__mmap_read_all(thread->rec, false) < 0 || terminate)
1652 break;
1653
1654 if (hits == thread->samples) {
1655
1656 err = fdarray__poll(pollfd, -1);
1657 /*
1658 * Propagate error, only if there's any. Ignore positive
1659 * number of returned events and interrupt error.
1660 */
1661 if (err > 0 || (err < 0 && errno == EINTR))
1662 err = 0;
1663 thread->waking++;
1664
1665 if (fdarray__filter(pollfd, POLLERR | POLLHUP,
1666 record__thread_munmap_filtered, NULL) == 0)
1667 break;
1668 }
1669
1670 if (pollfd->entries[ctlfd_pos].revents & POLLHUP) {
1671 terminate = true;
1672 close(thread->pipes.msg[0]);
1673 thread->pipes.msg[0] = -1;
1674 pollfd->entries[ctlfd_pos].fd = -1;
1675 pollfd->entries[ctlfd_pos].events = 0;
1676 }
1677
1678 pollfd->entries[ctlfd_pos].revents = 0;
1679 }
1680 record__mmap_read_all(thread->rec, true);
1681
1682 err = write(thread->pipes.ack[1], &msg, sizeof(msg));
1683 if (err == -1)
1684 pr_warning("threads[%d]: failed to notify on termination: %s\n",
1685 thread->tid, strerror(errno));
1686
1687 return NULL;
1688}
1689
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03001690static void record__init_features(struct record *rec)
David Ahern57706ab2013-11-06 11:41:34 -07001691{
David Ahern57706ab2013-11-06 11:41:34 -07001692 struct perf_session *session = rec->session;
1693 int feat;
1694
1695 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1696 perf_header__set_feat(&session->header, feat);
1697
1698 if (rec->no_buildid)
1699 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1700
Ian Rogers378ef0f2022-12-05 14:59:39 -08001701#ifdef HAVE_LIBTRACEEVENT
Jiri Olsace9036a2019-07-21 13:24:23 +02001702 if (!have_tracepoints(&rec->evlist->core.entries))
David Ahern57706ab2013-11-06 11:41:34 -07001703 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
Ian Rogers378ef0f2022-12-05 14:59:39 -08001704#endif
David Ahern57706ab2013-11-06 11:41:34 -07001705
1706 if (!rec->opts.branch_stack)
1707 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
Adrian Hunteref149c22015-04-09 18:53:45 +03001708
1709 if (!rec->opts.full_auxtrace)
1710 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
Jiri Olsaffa517a2015-10-25 15:51:43 +01001711
Alexey Budankovcf790512018-10-09 17:36:24 +03001712 if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns))
1713 perf_header__clear_feat(&session->header, HEADER_CLOCKID);
1714
Jiri Olsad1e325c2020-08-05 11:34:40 +02001715 if (!rec->opts.use_clockid)
1716 perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA);
1717
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001718 if (!record__threads_enabled(rec))
1719 perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
1720
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001721 if (!record__comp_enabled(rec))
1722 perf_header__clear_feat(&session->header, HEADER_COMPRESSED);
Jiri Olsa258031c2019-03-08 14:47:39 +01001723
Jiri Olsaffa517a2015-10-25 15:51:43 +01001724 perf_header__clear_feat(&session->header, HEADER_STAT);
David Ahern57706ab2013-11-06 11:41:34 -07001725}
1726
Wang Nane1ab48b2016-02-26 09:32:10 +00001727static void
1728record__finish_output(struct record *rec)
1729{
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001730 int i;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001731 struct perf_data *data = &rec->data;
1732 int fd = perf_data__fd(data);
Wang Nane1ab48b2016-02-26 09:32:10 +00001733
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001734 if (data->is_pipe)
Wang Nane1ab48b2016-02-26 09:32:10 +00001735 return;
1736
1737 rec->session->header.data_size += rec->bytes_written;
Jiri Olsa45112e82019-02-21 10:41:29 +01001738 data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR);
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03001739 if (record__threads_enabled(rec)) {
1740 for (i = 0; i < data->dir.nr; i++)
1741 data->dir.files[i].size = lseek(data->dir.files[i].fd, 0, SEEK_CUR);
1742 }
Wang Nane1ab48b2016-02-26 09:32:10 +00001743
1744 if (!rec->no_buildid) {
1745 process_buildids(rec);
1746
1747 if (rec->buildid_all)
1748 dsos__hit_all(rec->session);
1749 }
1750 perf_session__write_header(rec->session, rec->evlist, fd, true);
1751
1752 return;
1753}
1754
Wang Nan4ea648a2016-07-14 08:34:47 +00001755static int record__synthesize_workload(struct record *rec, bool tail)
Wang Nanbe7b0c92016-04-20 18:59:54 +00001756{
Arnaldo Carvalho de Melo9d6aae72017-02-14 10:59:04 -03001757 int err;
Jiri Olsa9749b902019-07-21 13:23:50 +02001758 struct perf_thread_map *thread_map;
Namhyung Kim41b740b2021-08-10 21:46:58 -07001759 bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP;
Wang Nanbe7b0c92016-04-20 18:59:54 +00001760
Wang Nan4ea648a2016-07-14 08:34:47 +00001761 if (rec->opts.tail_synthesize != tail)
1762 return 0;
1763
Arnaldo Carvalho de Melo9d6aae72017-02-14 10:59:04 -03001764 thread_map = thread_map__new_by_tid(rec->evlist->workload.pid);
1765 if (thread_map == NULL)
1766 return -1;
1767
1768 err = perf_event__synthesize_thread_map(&rec->tool, thread_map,
Wang Nanbe7b0c92016-04-20 18:59:54 +00001769 process_synthesized_event,
1770 &rec->session->machines.host,
Namhyung Kim41b740b2021-08-10 21:46:58 -07001771 needs_mmap,
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001772 rec->opts.sample_address);
Jiri Olsa7836e522019-07-21 13:24:20 +02001773 perf_thread_map__put(thread_map);
Arnaldo Carvalho de Melo9d6aae72017-02-14 10:59:04 -03001774 return err;
Wang Nanbe7b0c92016-04-20 18:59:54 +00001775}
1776
Adrian Hunter3812d292022-06-10 14:33:15 +03001777static int write_finished_init(struct record *rec, bool tail)
1778{
1779 if (rec->opts.tail_synthesize != tail)
1780 return 0;
1781
1782 return record__write(rec, NULL, &finished_init_event, sizeof(finished_init_event));
1783}
1784
Wang Nan4ea648a2016-07-14 08:34:47 +00001785static int record__synthesize(struct record *rec, bool tail);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00001786
Wang Nanecfd7a92016-04-13 08:21:07 +00001787static int
1788record__switch_output(struct record *rec, bool at_exit)
1789{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001790 struct perf_data *data = &rec->data;
Wang Nanecfd7a92016-04-13 08:21:07 +00001791 int fd, err;
Andi Kleen03724b22019-03-14 15:49:55 -07001792 char *new_filename;
Wang Nanecfd7a92016-04-13 08:21:07 +00001793
1794 /* Same Size: "2015122520103046"*/
1795 char timestamp[] = "InvalidTimestamp";
1796
Alexey Budankovd3d1af62018-11-06 12:04:58 +03001797 record__aio_mmap_read_sync(rec);
1798
Adrian Hunter3812d292022-06-10 14:33:15 +03001799 write_finished_init(rec, true);
1800
Wang Nan4ea648a2016-07-14 08:34:47 +00001801 record__synthesize(rec, true);
1802 if (target__none(&rec->opts.target))
1803 record__synthesize_workload(rec, true);
1804
Wang Nanecfd7a92016-04-13 08:21:07 +00001805 rec->samples = 0;
1806 record__finish_output(rec);
1807 err = fetch_current_timestamp(timestamp, sizeof(timestamp));
1808 if (err) {
1809 pr_err("Failed to get current timestamp\n");
1810 return -EINVAL;
1811 }
1812
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001813 fd = perf_data__switch(data, timestamp,
Wang Nanecfd7a92016-04-13 08:21:07 +00001814 rec->session->header.data_offset,
Andi Kleen03724b22019-03-14 15:49:55 -07001815 at_exit, &new_filename);
Wang Nanecfd7a92016-04-13 08:21:07 +00001816 if (fd >= 0 && !at_exit) {
1817 rec->bytes_written = 0;
1818 rec->session->header.data_size = 0;
1819 }
1820
1821 if (!quiet)
1822 fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001823 data->path, timestamp);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00001824
Andi Kleen03724b22019-03-14 15:49:55 -07001825 if (rec->switch_output.num_files) {
1826 int n = rec->switch_output.cur_file + 1;
1827
1828 if (n >= rec->switch_output.num_files)
1829 n = 0;
1830 rec->switch_output.cur_file = n;
1831 if (rec->switch_output.filenames[n]) {
1832 remove(rec->switch_output.filenames[n]);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001833 zfree(&rec->switch_output.filenames[n]);
Andi Kleen03724b22019-03-14 15:49:55 -07001834 }
1835 rec->switch_output.filenames[n] = new_filename;
1836 } else {
1837 free(new_filename);
1838 }
1839
Wang Nan3c1cb7e2016-04-20 18:59:50 +00001840 /* Output tracking events */
Wang Nanbe7b0c92016-04-20 18:59:54 +00001841 if (!at_exit) {
Wang Nan4ea648a2016-07-14 08:34:47 +00001842 record__synthesize(rec, false);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00001843
Wang Nanbe7b0c92016-04-20 18:59:54 +00001844 /*
1845 * In 'perf record --switch-output' without -a,
1846 * record__synthesize() in record__switch_output() won't
1847 * generate tracking events because there's no thread_map
1848 * in evlist. Which causes newly created perf.data doesn't
1849 * contain map and comm information.
1850 * Create a fake thread_map and directly call
1851 * perf_event__synthesize_thread_map() for those events.
1852 */
1853 if (target__none(&rec->opts.target))
Wang Nan4ea648a2016-07-14 08:34:47 +00001854 record__synthesize_workload(rec, false);
Adrian Hunter3812d292022-06-10 14:33:15 +03001855 write_finished_init(rec, false);
Wang Nanbe7b0c92016-04-20 18:59:54 +00001856 }
Wang Nanecfd7a92016-04-13 08:21:07 +00001857 return fd;
1858}
1859
Namhyung Kim27c6f242023-03-14 16:42:31 -07001860static void __record__save_lost_samples(struct record *rec, struct evsel *evsel,
Namhyung Kime3a23262022-09-01 12:57:37 -07001861 struct perf_record_lost_samples *lost,
Namhyung Kim27c6f242023-03-14 16:42:31 -07001862 int cpu_idx, int thread_idx, u64 lost_count,
1863 u16 misc_flag)
Namhyung Kime3a23262022-09-01 12:57:37 -07001864{
Namhyung Kime3a23262022-09-01 12:57:37 -07001865 struct perf_sample_id *sid;
1866 struct perf_sample sample = {};
1867 int id_hdr_size;
1868
Namhyung Kim27c6f242023-03-14 16:42:31 -07001869 lost->lost = lost_count;
Namhyung Kime3a23262022-09-01 12:57:37 -07001870 if (evsel->core.ids) {
1871 sid = xyarray__entry(evsel->core.sample_id, cpu_idx, thread_idx);
1872 sample.id = sid->id;
1873 }
1874
1875 id_hdr_size = perf_event__synthesize_id_sample((void *)(lost + 1),
1876 evsel->core.attr.sample_type, &sample);
1877 lost->header.size = sizeof(*lost) + id_hdr_size;
Namhyung Kim27c6f242023-03-14 16:42:31 -07001878 lost->header.misc = misc_flag;
Namhyung Kime3a23262022-09-01 12:57:37 -07001879 record__write(rec, NULL, lost, lost->header.size);
1880}
1881
1882static void record__read_lost_samples(struct record *rec)
1883{
1884 struct perf_session *session = rec->session;
1885 struct perf_record_lost_samples *lost;
1886 struct evsel *evsel;
1887
Namhyung Kimd031a002022-09-09 16:50:24 -07001888 /* there was an error during record__open */
1889 if (session->evlist == NULL)
1890 return;
1891
Namhyung Kime3a23262022-09-01 12:57:37 -07001892 lost = zalloc(PERF_SAMPLE_MAX_SIZE);
1893 if (lost == NULL) {
1894 pr_debug("Memory allocation failed\n");
1895 return;
1896 }
1897
1898 lost->header.type = PERF_RECORD_LOST_SAMPLES;
1899
1900 evlist__for_each_entry(session->evlist, evsel) {
1901 struct xyarray *xy = evsel->core.sample_id;
Namhyung Kim27c6f242023-03-14 16:42:31 -07001902 u64 lost_count;
Namhyung Kime3a23262022-09-01 12:57:37 -07001903
Namhyung Kimd031a002022-09-09 16:50:24 -07001904 if (xy == NULL || evsel->core.fd == NULL)
1905 continue;
Namhyung Kime3a23262022-09-01 12:57:37 -07001906 if (xyarray__max_x(evsel->core.fd) != xyarray__max_x(xy) ||
1907 xyarray__max_y(evsel->core.fd) != xyarray__max_y(xy)) {
1908 pr_debug("Unmatched FD vs. sample ID: skip reading LOST count\n");
1909 continue;
1910 }
1911
1912 for (int x = 0; x < xyarray__max_x(xy); x++) {
1913 for (int y = 0; y < xyarray__max_y(xy); y++) {
Namhyung Kim27c6f242023-03-14 16:42:31 -07001914 struct perf_counts_values count;
1915
1916 if (perf_evsel__read(&evsel->core, x, y, &count) < 0) {
1917 pr_debug("read LOST count failed\n");
1918 goto out;
1919 }
1920
1921 if (count.lost) {
1922 __record__save_lost_samples(rec, evsel, lost,
1923 x, y, count.lost, 0);
1924 }
Namhyung Kime3a23262022-09-01 12:57:37 -07001925 }
1926 }
Namhyung Kime3a23262022-09-01 12:57:37 -07001927
Namhyung Kim27c6f242023-03-14 16:42:31 -07001928 lost_count = perf_bpf_filter__lost_count(evsel);
1929 if (lost_count)
1930 __record__save_lost_samples(rec, evsel, lost, 0, 0, lost_count,
1931 PERF_RECORD_MISC_LOST_SAMPLES_BPF);
1932 }
1933out:
1934 free(lost);
Namhyung Kime3a23262022-09-01 12:57:37 -07001935}
1936
Ian Rogers8ed28c22022-10-24 11:19:07 -07001937static volatile sig_atomic_t workload_exec_errno;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001938
1939/*
Arnaldo Carvalho de Melo7b392ef2020-11-30 09:26:54 -03001940 * evlist__prepare_workload will send a SIGUSR1
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001941 * if the fork fails, since we asked by setting its
1942 * want_signal to true.
1943 */
Namhyung Kim45604712014-05-12 09:47:24 +09001944static void workload_exec_failed_signal(int signo __maybe_unused,
1945 siginfo_t *info,
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001946 void *ucontext __maybe_unused)
1947{
1948 workload_exec_errno = info->si_value.sival_int;
1949 done = 1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001950 child_finished = 1;
1951}
1952
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03001953static void snapshot_sig_handler(int sig);
Jiri Olsabfacbe32017-01-09 10:52:00 +01001954static void alarm_sig_handler(int sig);
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03001955
Arnaldo Carvalho de Melodb0ea13c2020-11-30 15:19:40 -03001956static const struct perf_event_mmap_page *evlist__pick_pc(struct evlist *evlist)
Wang Nanee667f92016-06-27 10:24:05 +00001957{
Wang Nanb2cb6152016-07-14 08:34:39 +00001958 if (evlist) {
Jiri Olsa547740f2019-07-27 22:07:44 +02001959 if (evlist->mmap && evlist->mmap[0].core.base)
1960 return evlist->mmap[0].core.base;
1961 if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base)
1962 return evlist->overwrite_mmap[0].core.base;
Wang Nanb2cb6152016-07-14 08:34:39 +00001963 }
Wang Nanee667f92016-06-27 10:24:05 +00001964 return NULL;
1965}
1966
Wang Nanc45628b2016-05-24 02:28:59 +00001967static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
1968{
Arnaldo Carvalho de Melodb0ea13c2020-11-30 15:19:40 -03001969 const struct perf_event_mmap_page *pc = evlist__pick_pc(rec->evlist);
Wang Nanee667f92016-06-27 10:24:05 +00001970 if (pc)
1971 return pc;
Wang Nanc45628b2016-05-24 02:28:59 +00001972 return NULL;
1973}
1974
Wang Nan4ea648a2016-07-14 08:34:47 +00001975static int record__synthesize(struct record *rec, bool tail)
Wang Nanc45c86e2016-02-26 09:32:07 +00001976{
1977 struct perf_session *session = rec->session;
1978 struct machine *machine = &session->machines.host;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001979 struct perf_data *data = &rec->data;
Wang Nanc45c86e2016-02-26 09:32:07 +00001980 struct record_opts *opts = &rec->opts;
1981 struct perf_tool *tool = &rec->tool;
Wang Nanc45c86e2016-02-26 09:32:07 +00001982 int err = 0;
Stephane Eraniand99c22e2020-04-22 08:50:38 -07001983 event_op f = process_synthesized_event;
Wang Nanc45c86e2016-02-26 09:32:07 +00001984
Wang Nan4ea648a2016-07-14 08:34:47 +00001985 if (rec->opts.tail_synthesize != tail)
1986 return 0;
1987
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001988 if (data->is_pipe) {
Namhyung Kimc3a057d2021-07-19 15:31:52 -07001989 err = perf_event__synthesize_for_pipe(tool, session, data,
Jiri Olsaa2015512018-03-14 10:22:04 +01001990 process_synthesized_event);
Namhyung Kimc3a057d2021-07-19 15:31:52 -07001991 if (err < 0)
1992 goto out;
Jiri Olsaa2015512018-03-14 10:22:04 +01001993
Namhyung Kimc3a057d2021-07-19 15:31:52 -07001994 rec->bytes_written += err;
Wang Nanc45c86e2016-02-26 09:32:07 +00001995 }
1996
Wang Nanc45628b2016-05-24 02:28:59 +00001997 err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
Adrian Hunter46bc29b2016-03-08 10:38:44 +02001998 process_synthesized_event, machine);
1999 if (err)
2000 goto out;
2001
Adrian Hunterc0a6de02019-11-15 14:42:16 +02002002 /* Synthesize id_index before auxtrace_info */
Adrian Hunter6b080312022-06-10 14:33:13 +03002003 err = perf_event__synthesize_id_index(tool,
2004 process_synthesized_event,
2005 session->evlist, machine);
2006 if (err)
2007 goto out;
Adrian Hunterc0a6de02019-11-15 14:42:16 +02002008
Wang Nanc45c86e2016-02-26 09:32:07 +00002009 if (rec->opts.full_auxtrace) {
2010 err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
2011 session, process_synthesized_event);
2012 if (err)
2013 goto out;
2014 }
2015
Arnaldo Carvalho de Melo78e1bc22020-11-30 15:07:49 -03002016 if (!evlist__exclude_kernel(rec->evlist)) {
Arnaldo Carvalho de Melo6c443952017-11-14 11:03:19 -03002017 err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
2018 machine);
2019 WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
2020 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
2021 "Check /proc/kallsyms permission or run as root.\n");
Wang Nanc45c86e2016-02-26 09:32:07 +00002022
Arnaldo Carvalho de Melo6c443952017-11-14 11:03:19 -03002023 err = perf_event__synthesize_modules(tool, process_synthesized_event,
2024 machine);
2025 WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
2026 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
2027 "Check /proc/modules permission or run as root.\n");
2028 }
Wang Nanc45c86e2016-02-26 09:32:07 +00002029
2030 if (perf_guest) {
2031 machines__process_guests(&session->machines,
2032 perf_event__synthesize_guest_os, tool);
2033 }
2034
Andi Kleenbfd8f722017-11-17 13:42:58 -08002035 err = perf_event__synthesize_extra_attr(&rec->tool,
2036 rec->evlist,
2037 process_synthesized_event,
2038 data->is_pipe);
2039 if (err)
2040 goto out;
2041
Jiri Olsa03617c22019-07-21 13:24:42 +02002042 err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads,
Andi Kleen373565d2017-11-17 13:42:59 -08002043 process_synthesized_event,
2044 NULL);
2045 if (err < 0) {
2046 pr_err("Couldn't synthesize thread map.\n");
2047 return err;
2048 }
2049
Adrian Hunter7be1fed2022-05-24 10:54:30 +03002050 err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.all_cpus,
Andi Kleen373565d2017-11-17 13:42:59 -08002051 process_synthesized_event, NULL);
2052 if (err < 0) {
2053 pr_err("Couldn't synthesize cpu map.\n");
2054 return err;
2055 }
2056
Song Liue5416952019-03-11 22:30:41 -07002057 err = perf_event__synthesize_bpf_events(session, process_synthesized_event,
Song Liu7b612e22019-01-17 08:15:19 -08002058 machine, opts);
Adrian Hunterfaf59ec2022-09-07 19:24:58 +03002059 if (err < 0) {
Song Liu7b612e22019-01-17 08:15:19 -08002060 pr_warning("Couldn't synthesize bpf events.\n");
Adrian Hunterfaf59ec2022-09-07 19:24:58 +03002061 err = 0;
2062 }
Song Liu7b612e22019-01-17 08:15:19 -08002063
Namhyung Kim41b740b2021-08-10 21:46:58 -07002064 if (rec->opts.synth & PERF_SYNTH_CGROUP) {
2065 err = perf_event__synthesize_cgroups(tool, process_synthesized_event,
2066 machine);
Adrian Hunterfaf59ec2022-09-07 19:24:58 +03002067 if (err < 0) {
Namhyung Kim41b740b2021-08-10 21:46:58 -07002068 pr_warning("Couldn't synthesize cgroup events.\n");
Adrian Hunterfaf59ec2022-09-07 19:24:58 +03002069 err = 0;
2070 }
Namhyung Kim41b740b2021-08-10 21:46:58 -07002071 }
Namhyung Kimab640692020-03-25 21:45:33 +09002072
Stephane Eraniand99c22e2020-04-22 08:50:38 -07002073 if (rec->opts.nr_threads_synthesize > 1) {
Ian Rogers49c670b172022-08-26 09:42:31 -07002074 mutex_init(&synth_lock);
Stephane Eraniand99c22e2020-04-22 08:50:38 -07002075 perf_set_multithreaded();
2076 f = process_locked_synthesized_event;
2077 }
2078
Namhyung Kim41b740b2021-08-10 21:46:58 -07002079 if (rec->opts.synth & PERF_SYNTH_TASK) {
2080 bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP;
2081
2082 err = __machine__synthesize_threads(machine, tool, &opts->target,
2083 rec->evlist->core.threads,
2084 f, needs_mmap, opts->sample_address,
2085 rec->opts.nr_threads_synthesize);
2086 }
Stephane Eraniand99c22e2020-04-22 08:50:38 -07002087
Ian Rogers49c670b172022-08-26 09:42:31 -07002088 if (rec->opts.nr_threads_synthesize > 1) {
Stephane Eraniand99c22e2020-04-22 08:50:38 -07002089 perf_set_singlethreaded();
Ian Rogers49c670b172022-08-26 09:42:31 -07002090 mutex_destroy(&synth_lock);
2091 }
Stephane Eraniand99c22e2020-04-22 08:50:38 -07002092
Wang Nanc45c86e2016-02-26 09:32:07 +00002093out:
2094 return err;
2095}
2096
Arnaldo Carvalho de Melo899e5ff2020-04-27 17:56:37 -03002097static int record__process_signal_event(union perf_event *event __maybe_unused, void *data)
2098{
2099 struct record *rec = data;
2100 pthread_kill(rec->thread_id, SIGUSR2);
2101 return 0;
2102}
2103
Arnaldo Carvalho de Melo23cbb412020-04-28 14:58:29 -03002104static int record__setup_sb_evlist(struct record *rec)
2105{
2106 struct record_opts *opts = &rec->opts;
2107
2108 if (rec->sb_evlist != NULL) {
2109 /*
2110 * We get here if --switch-output-event populated the
2111 * sb_evlist, so associate a callback that will send a SIGUSR2
2112 * to the main thread.
2113 */
2114 evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec);
2115 rec->thread_id = pthread_self();
2116 }
Jin Yao1101c872020-08-05 10:29:37 +08002117#ifdef HAVE_LIBBPF_SUPPORT
Arnaldo Carvalho de Melo23cbb412020-04-28 14:58:29 -03002118 if (!opts->no_bpf_event) {
2119 if (rec->sb_evlist == NULL) {
2120 rec->sb_evlist = evlist__new();
2121
2122 if (rec->sb_evlist == NULL) {
2123 pr_err("Couldn't create side band evlist.\n.");
2124 return -1;
2125 }
2126 }
2127
2128 if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) {
2129 pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
2130 return -1;
2131 }
2132 }
Jin Yao1101c872020-08-05 10:29:37 +08002133#endif
Arnaldo Carvalho de Melo08c83992020-11-30 09:40:10 -03002134 if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) {
Arnaldo Carvalho de Melo23cbb412020-04-28 14:58:29 -03002135 pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
2136 opts->no_bpf_event = true;
2137 }
2138
2139 return 0;
2140}
2141
Jiri Olsad1e325c2020-08-05 11:34:40 +02002142static int record__init_clock(struct record *rec)
2143{
2144 struct perf_session *session = rec->session;
2145 struct timespec ref_clockid;
2146 struct timeval ref_tod;
2147 u64 ref;
2148
2149 if (!rec->opts.use_clockid)
2150 return 0;
2151
Jiri Olsa9d88a1a12020-08-05 11:34:41 +02002152 if (rec->opts.use_clockid && rec->opts.clockid_res_ns)
2153 session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns;
2154
Jiri Olsad1e325c2020-08-05 11:34:40 +02002155 session->header.env.clock.clockid = rec->opts.clockid;
2156
2157 if (gettimeofday(&ref_tod, NULL) != 0) {
2158 pr_err("gettimeofday failed, cannot set reference time.\n");
2159 return -1;
2160 }
2161
2162 if (clock_gettime(rec->opts.clockid, &ref_clockid)) {
2163 pr_err("clock_gettime failed, cannot set reference time.\n");
2164 return -1;
2165 }
2166
2167 ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC +
2168 (u64) ref_tod.tv_usec * NSEC_PER_USEC;
2169
2170 session->header.env.clock.tod_ns = ref;
2171
2172 ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC +
2173 (u64) ref_clockid.tv_nsec;
2174
2175 session->header.env.clock.clockid_ns = ref;
2176 return 0;
2177}
2178
Adrian Hunterd20aff12020-09-01 12:37:57 +03002179static void hit_auxtrace_snapshot_trigger(struct record *rec)
2180{
2181 if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
2182 trigger_hit(&auxtrace_snapshot_trigger);
2183 auxtrace_record__snapshot_started = 1;
2184 if (auxtrace_record__snapshot_start(rec->itr))
2185 trigger_error(&auxtrace_snapshot_trigger);
2186 }
2187}
2188
Jin Yao91c0f5e2021-04-27 15:01:30 +08002189static void record__uniquify_name(struct record *rec)
2190{
2191 struct evsel *pos;
2192 struct evlist *evlist = rec->evlist;
2193 char *new_name;
2194 int ret;
2195
2196 if (!perf_pmu__has_hybrid())
2197 return;
2198
2199 evlist__for_each_entry(evlist, pos) {
2200 if (!evsel__is_hybrid(pos))
2201 continue;
2202
2203 if (strchr(pos->name, '/'))
2204 continue;
2205
2206 ret = asprintf(&new_name, "%s/%s/",
2207 pos->pmu_name, pos->name);
2208 if (ret) {
2209 free(pos->name);
2210 pos->name = new_name;
2211 }
2212 }
2213}
2214
Alexey Bayduraev1e5de7d2022-01-17 21:34:26 +03002215static int record__terminate_thread(struct record_thread *thread_data)
2216{
2217 int err;
2218 enum thread_msg ack = THREAD_MSG__UNDEFINED;
2219 pid_t tid = thread_data->tid;
2220
2221 close(thread_data->pipes.msg[1]);
2222 thread_data->pipes.msg[1] = -1;
2223 err = read(thread_data->pipes.ack[0], &ack, sizeof(ack));
2224 if (err > 0)
2225 pr_debug2("threads[%d]: sent %s\n", tid, thread_msg_tags[ack]);
2226 else
2227 pr_warning("threads[%d]: failed to receive termination notification from %d\n",
2228 thread->tid, tid);
2229
2230 return 0;
2231}
2232
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002233static int record__start_threads(struct record *rec)
2234{
Alexey Bayduraev3217e9f2022-01-17 21:34:27 +03002235 int t, tt, err, ret = 0, nr_threads = rec->nr_threads;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002236 struct record_thread *thread_data = rec->thread_data;
Alexey Bayduraev3217e9f2022-01-17 21:34:27 +03002237 sigset_t full, mask;
2238 pthread_t handle;
2239 pthread_attr_t attrs;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002240
2241 thread = &thread_data[0];
2242
Alexey Bayduraev3217e9f2022-01-17 21:34:27 +03002243 if (!record__threads_enabled(rec))
2244 return 0;
2245
2246 sigfillset(&full);
2247 if (sigprocmask(SIG_SETMASK, &full, &mask)) {
2248 pr_err("Failed to block signals on threads start: %s\n", strerror(errno));
2249 return -1;
2250 }
2251
2252 pthread_attr_init(&attrs);
2253 pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
2254
2255 for (t = 1; t < nr_threads; t++) {
2256 enum thread_msg msg = THREAD_MSG__UNDEFINED;
2257
2258#ifdef HAVE_PTHREAD_ATTR_SETAFFINITY_NP
2259 pthread_attr_setaffinity_np(&attrs,
2260 MMAP_CPU_MASK_BYTES(&(thread_data[t].mask->affinity)),
2261 (cpu_set_t *)(thread_data[t].mask->affinity.bits));
2262#endif
2263 if (pthread_create(&handle, &attrs, record__thread, &thread_data[t])) {
2264 for (tt = 1; tt < t; tt++)
2265 record__terminate_thread(&thread_data[t]);
2266 pr_err("Failed to start threads: %s\n", strerror(errno));
2267 ret = -1;
2268 goto out_err;
2269 }
2270
2271 err = read(thread_data[t].pipes.ack[0], &msg, sizeof(msg));
2272 if (err > 0)
2273 pr_debug2("threads[%d]: sent %s\n", rec->thread_data[t].tid,
2274 thread_msg_tags[msg]);
2275 else
2276 pr_warning("threads[%d]: failed to receive start notification from %d\n",
2277 thread->tid, rec->thread_data[t].tid);
2278 }
2279
2280 sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity),
2281 (cpu_set_t *)thread->mask->affinity.bits);
2282
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002283 pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu());
2284
Alexey Bayduraev3217e9f2022-01-17 21:34:27 +03002285out_err:
2286 pthread_attr_destroy(&attrs);
2287
2288 if (sigprocmask(SIG_SETMASK, &mask, NULL)) {
2289 pr_err("Failed to unblock signals on threads start: %s\n", strerror(errno));
2290 ret = -1;
2291 }
2292
2293 return ret;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002294}
2295
2296static int record__stop_threads(struct record *rec)
2297{
2298 int t;
2299 struct record_thread *thread_data = rec->thread_data;
2300
Alexey Bayduraev1e5de7d2022-01-17 21:34:26 +03002301 for (t = 1; t < rec->nr_threads; t++)
2302 record__terminate_thread(&thread_data[t]);
2303
Alexey Bayduraev610fbc02022-01-17 21:34:31 +03002304 for (t = 0; t < rec->nr_threads; t++) {
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002305 rec->samples += thread_data[t].samples;
Alexey Bayduraev610fbc02022-01-17 21:34:31 +03002306 if (!record__threads_enabled(rec))
2307 continue;
2308 rec->session->bytes_transferred += thread_data[t].bytes_transferred;
2309 rec->session->bytes_compressed += thread_data[t].bytes_compressed;
2310 pr_debug("threads[%d]: samples=%lld, wakes=%ld, ", thread_data[t].tid,
2311 thread_data[t].samples, thread_data[t].waking);
2312 if (thread_data[t].bytes_transferred && thread_data[t].bytes_compressed)
2313 pr_debug("transferred=%" PRIu64 ", compressed=%" PRIu64 "\n",
2314 thread_data[t].bytes_transferred, thread_data[t].bytes_compressed);
2315 else
2316 pr_debug("written=%" PRIu64 "\n", thread_data[t].bytes_written);
2317 }
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002318
2319 return 0;
2320}
2321
2322static unsigned long record__waking(struct record *rec)
2323{
2324 int t;
2325 unsigned long waking = 0;
2326 struct record_thread *thread_data = rec->thread_data;
2327
2328 for (t = 0; t < rec->nr_threads; t++)
2329 waking += thread_data[t].waking;
2330
2331 return waking;
2332}
2333
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03002334static int __cmd_record(struct record *rec, int argc, const char **argv)
Peter Zijlstra16c8a102009-05-05 17:50:27 +02002335{
David Ahern57706ab2013-11-06 11:41:34 -07002336 int err;
Namhyung Kim45604712014-05-12 09:47:24 +09002337 int status = 0;
Zhang, Yanmin46be6042010-03-18 11:36:04 -03002338 const bool forks = argc > 0;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002339 struct perf_tool *tool = &rec->tool;
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03002340 struct record_opts *opts = &rec->opts;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002341 struct perf_data *data = &rec->data;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002342 struct perf_session *session;
Arnaldo Carvalho de Melo6dcf45ef2014-08-13 11:33:59 -03002343 bool disabled = false, draining = false;
Namhyung Kim42aa2762015-01-29 17:06:48 +09002344 int fd;
Alexey Budankovd3c8c082019-03-18 20:41:02 +03002345 float ratio = 0;
Alexey Budankovacce0222020-07-17 10:07:50 +03002346 enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002347
Namhyung Kim45604712014-05-12 09:47:24 +09002348 atexit(record__sig_exit);
Peter Zijlstraf5970552009-06-18 23:22:55 +02002349 signal(SIGCHLD, sig_handler);
2350 signal(SIGINT, sig_handler);
David Ahern804f7ac2013-05-06 12:24:23 -06002351 signal(SIGTERM, sig_handler);
Wang Nana0748652016-11-26 07:03:28 +00002352 signal(SIGSEGV, sigsegv_handler);
Wang Nanc0bdc1c2016-04-13 08:21:06 +00002353
Hari Bathinif3b36142017-03-08 02:11:43 +05302354 if (rec->opts.record_namespaces)
2355 tool->namespace_events = true;
2356
Namhyung Kim8fb4b672020-03-25 21:45:34 +09002357 if (rec->opts.record_cgroup) {
2358#ifdef HAVE_FILE_HANDLE
2359 tool->cgroup_events = true;
2360#else
2361 pr_err("cgroup tracking is not supported\n");
2362 return -1;
2363#endif
2364 }
2365
Jiri Olsadc0c6122017-01-09 10:51:58 +01002366 if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03002367 signal(SIGUSR2, snapshot_sig_handler);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002368 if (rec->opts.auxtrace_snapshot_mode)
2369 trigger_on(&auxtrace_snapshot_trigger);
Jiri Olsadc0c6122017-01-09 10:51:58 +01002370 if (rec->switch_output.enabled)
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002371 trigger_on(&switch_output_trigger);
Wang Nanc0bdc1c2016-04-13 08:21:06 +00002372 } else {
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03002373 signal(SIGUSR2, SIG_IGN);
Wang Nanc0bdc1c2016-04-13 08:21:06 +00002374 }
Peter Zijlstraf5970552009-06-18 23:22:55 +02002375
Namhyung Kim2681bd82021-07-19 15:31:49 -07002376 session = perf_session__new(data, tool);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05302377 if (IS_ERR(session)) {
Adrien BAKffa91882014-04-18 11:00:43 +09002378 pr_err("Perf session creation failed.\n");
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05302379 return PTR_ERR(session);
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -02002380 }
2381
Alexey Bayduraevb5f25112022-01-17 21:34:34 +03002382 if (record__threads_enabled(rec)) {
2383 if (perf_data__is_pipe(&rec->data)) {
2384 pr_err("Parallel trace streaming is not available in pipe mode.\n");
2385 return -1;
2386 }
2387 if (rec->opts.full_auxtrace) {
2388 pr_err("Parallel trace streaming is not available in AUX area tracing mode.\n");
2389 return -1;
2390 }
2391 }
2392
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002393 fd = perf_data__fd(data);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002394 rec->session = session;
2395
Alexey Budankov5d7f4112019-03-18 20:43:35 +03002396 if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) {
2397 pr_err("Compression initialization failed.\n");
2398 return -1;
2399 }
Anand K Mistryda231332020-05-13 12:20:23 +10002400#ifdef HAVE_EVENTFD_SUPPORT
2401 done_fd = eventfd(0, EFD_NONBLOCK);
2402 if (done_fd < 0) {
2403 pr_err("Failed to create wakeup eventfd, error: %m\n");
2404 status = -1;
2405 goto out_delete_session;
2406 }
Yang Jihonge16c2ce2021-02-05 14:50:01 +08002407 err = evlist__add_wakeup_eventfd(rec->evlist, done_fd);
Anand K Mistryda231332020-05-13 12:20:23 +10002408 if (err < 0) {
2409 pr_err("Failed to add wakeup eventfd to poll list\n");
2410 status = err;
2411 goto out_delete_session;
2412 }
2413#endif // HAVE_EVENTFD_SUPPORT
Alexey Budankov5d7f4112019-03-18 20:43:35 +03002414
2415 session->header.env.comp_type = PERF_COMP_ZSTD;
2416 session->header.env.comp_level = rec->opts.comp_level;
2417
Adrian Huntereeb399b2019-10-04 11:31:21 +03002418 if (rec->opts.kcore &&
2419 !record__kcore_readable(&session->machines.host)) {
2420 pr_err("ERROR: kcore is not readable.\n");
2421 return -1;
2422 }
2423
Jiri Olsad1e325c2020-08-05 11:34:40 +02002424 if (record__init_clock(rec))
2425 return -1;
2426
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03002427 record__init_features(rec);
Stephane Eranian330aa672012-03-08 23:47:46 +01002428
Arnaldo Carvalho de Melod4db3f12009-12-27 21:36:57 -02002429 if (forks) {
Arnaldo Carvalho de Melo7b392ef2020-11-30 09:26:54 -03002430 err = evlist__prepare_workload(rec->evlist, &opts->target, argv, data->is_pipe,
2431 workload_exec_failed_signal);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02002432 if (err < 0) {
2433 pr_err("Couldn't run the workload!\n");
Namhyung Kim45604712014-05-12 09:47:24 +09002434 status = err;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02002435 goto out_delete_session;
Jens Axboe0a5ac842009-08-12 11:18:01 +02002436 }
Peter Zijlstra856e9662009-12-16 17:55:55 +01002437 }
2438
Jiri Olsaad46e48c2018-03-02 17:13:54 +01002439 /*
2440 * If we have just single event and are sending data
2441 * through pipe, we need to force the ids allocation,
2442 * because we synthesize event name through the pipe
2443 * and need the id for that.
2444 */
Jiri Olsa6484d2f2019-07-21 13:24:28 +02002445 if (data->is_pipe && rec->evlist->core.nr_entries == 1)
Jiri Olsaad46e48c2018-03-02 17:13:54 +01002446 rec->opts.sample_id = true;
2447
Jin Yao91c0f5e2021-04-27 15:01:30 +08002448 record__uniquify_name(rec);
2449
Adrian Hunterda406202022-09-12 11:34:11 +03002450 /* Debug message used by test scripts */
2451 pr_debug3("perf record opening and mmapping events\n");
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03002452 if (record__open(rec) != 0) {
David Ahern8d3eca22012-08-26 12:24:47 -06002453 err = -1;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002454 goto out_free_threads;
David Ahern8d3eca22012-08-26 12:24:47 -06002455 }
Adrian Hunterda406202022-09-12 11:34:11 +03002456 /* Debug message used by test scripts */
2457 pr_debug3("perf record done opening and mmapping events\n");
Jiri Olsaf6fa4372019-08-06 15:14:05 +02002458 session->header.env.comp_mmap_len = session->evlist->core.mmap_len;
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002459
Adrian Huntereeb399b2019-10-04 11:31:21 +03002460 if (rec->opts.kcore) {
2461 err = record__kcore_copy(&session->machines.host, data);
2462 if (err) {
2463 pr_err("ERROR: Failed to copy kcore\n");
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002464 goto out_free_threads;
Adrian Huntereeb399b2019-10-04 11:31:21 +03002465 }
2466 }
2467
Wang Nan8690a2a2016-02-22 09:10:32 +00002468 err = bpf__apply_obj_config();
2469 if (err) {
2470 char errbuf[BUFSIZ];
2471
2472 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2473 pr_err("ERROR: Apply config to BPF failed: %s\n",
2474 errbuf);
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002475 goto out_free_threads;
Wang Nan8690a2a2016-02-22 09:10:32 +00002476 }
2477
Adrian Huntercca84822015-08-19 17:29:21 +03002478 /*
2479 * Normally perf_session__new would do this, but it doesn't have the
2480 * evlist.
2481 */
Arnaldo Carvalho de Melo8cedf3a52020-06-17 09:29:48 -03002482 if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) {
Adrian Huntercca84822015-08-19 17:29:21 +03002483 pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
2484 rec->tool.ordered_events = false;
2485 }
2486
Ian Rogers9d2dc632023-03-11 18:15:42 -08002487 if (evlist__nr_groups(rec->evlist) == 0)
Namhyung Kima8bb5592013-01-22 18:09:31 +09002488 perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
2489
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002490 if (data->is_pipe) {
Namhyung Kim42aa2762015-01-29 17:06:48 +09002491 err = perf_header__write_pipe(fd);
Tom Zanussi529870e2010-04-01 23:59:16 -05002492 if (err < 0)
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002493 goto out_free_threads;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002494 } else {
Namhyung Kim42aa2762015-01-29 17:06:48 +09002495 err = perf_session__write_header(session, rec->evlist, fd, false);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002496 if (err < 0)
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002497 goto out_free_threads;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002498 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002499
Arnaldo Carvalho de Melob38d85e2020-04-24 12:24:51 -03002500 err = -1;
David Ahernd3665492012-02-06 15:27:52 -07002501 if (!rec->no_buildid
Robert Richtere20960c2011-12-07 10:02:55 +01002502 && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
David Ahernd3665492012-02-06 15:27:52 -07002503 pr_err("Couldn't generate buildids. "
Robert Richtere20960c2011-12-07 10:02:55 +01002504 "Use --no-buildid to profile anyway.\n");
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002505 goto out_free_threads;
Robert Richtere20960c2011-12-07 10:02:55 +01002506 }
2507
Arnaldo Carvalho de Melo23cbb412020-04-28 14:58:29 -03002508 err = record__setup_sb_evlist(rec);
2509 if (err)
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002510 goto out_free_threads;
Song Liu657ee552019-03-11 22:30:50 -07002511
Wang Nan4ea648a2016-07-14 08:34:47 +00002512 err = record__synthesize(rec, false);
Wang Nanc45c86e2016-02-26 09:32:07 +00002513 if (err < 0)
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002514 goto out_free_threads;
David Ahern8d3eca22012-08-26 12:24:47 -06002515
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002516 if (rec->realtime_prio) {
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002517 struct sched_param param;
2518
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002519 param.sched_priority = rec->realtime_prio;
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002520 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02002521 pr_err("Could not set realtime priority.\n");
David Ahern8d3eca22012-08-26 12:24:47 -06002522 err = -1;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002523 goto out_free_threads;
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002524 }
2525 }
2526
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002527 if (record__start_threads(rec))
2528 goto out_free_threads;
2529
Jiri Olsa774cb492012-11-12 18:34:01 +01002530 /*
2531 * When perf is starting the traced process, all the events
2532 * (apart from group members) have enable_on_exec=1 set,
2533 * so don't spoil it by prematurely enabling them.
2534 */
Changbin Ducb4b9e62023-03-02 11:11:45 +08002535 if (!target__none(&opts->target) && !opts->target.initial_delay)
Jiri Olsa1c87f162019-07-21 13:24:08 +02002536 evlist__enable(rec->evlist);
David Ahern764e16a32011-08-25 10:17:55 -06002537
Peter Zijlstra856e9662009-12-16 17:55:55 +01002538 /*
2539 * Let the child rip
2540 */
Namhyung Kime803cf92015-09-22 09:24:55 +09002541 if (forks) {
Jiri Olsa20a8a3c2018-03-07 16:50:04 +01002542 struct machine *machine = &session->machines.host;
Namhyung Kime5bed5642015-09-30 10:45:24 +09002543 union perf_event *event;
Hari Bathinie907caf2017-03-08 02:11:51 +05302544 pid_t tgid;
Namhyung Kime5bed5642015-09-30 10:45:24 +09002545
2546 event = malloc(sizeof(event->comm) + machine->id_hdr_size);
2547 if (event == NULL) {
2548 err = -ENOMEM;
2549 goto out_child;
2550 }
2551
Namhyung Kime803cf92015-09-22 09:24:55 +09002552 /*
2553 * Some H/W events are generated before COMM event
2554 * which is emitted during exec(), so perf script
2555 * cannot see a correct process name for those events.
2556 * Synthesize COMM event to prevent it.
2557 */
Hari Bathinie907caf2017-03-08 02:11:51 +05302558 tgid = perf_event__synthesize_comm(tool, event,
2559 rec->evlist->workload.pid,
2560 process_synthesized_event,
2561 machine);
2562 free(event);
2563
2564 if (tgid == -1)
2565 goto out_child;
2566
2567 event = malloc(sizeof(event->namespaces) +
2568 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
2569 machine->id_hdr_size);
2570 if (event == NULL) {
2571 err = -ENOMEM;
2572 goto out_child;
2573 }
2574
2575 /*
2576 * Synthesize NAMESPACES event for the command specified.
2577 */
2578 perf_event__synthesize_namespaces(tool, event,
2579 rec->evlist->workload.pid,
2580 tgid, process_synthesized_event,
2581 machine);
Namhyung Kime5bed5642015-09-30 10:45:24 +09002582 free(event);
Namhyung Kime803cf92015-09-22 09:24:55 +09002583
Arnaldo Carvalho de Melo7b392ef2020-11-30 09:26:54 -03002584 evlist__start_workload(rec->evlist);
Namhyung Kime803cf92015-09-22 09:24:55 +09002585 }
Peter Zijlstra856e9662009-12-16 17:55:55 +01002586
Changbin Ducb4b9e62023-03-02 11:11:45 +08002587 if (opts->target.initial_delay) {
Alexey Budankov68cd3b42020-07-17 10:07:03 +03002588 pr_info(EVLIST_DISABLED_MSG);
Changbin Ducb4b9e62023-03-02 11:11:45 +08002589 if (opts->target.initial_delay > 0) {
2590 usleep(opts->target.initial_delay * USEC_PER_MSEC);
Alexey Budankov68cd3b42020-07-17 10:07:03 +03002591 evlist__enable(rec->evlist);
2592 pr_info(EVLIST_ENABLED_MSG);
2593 }
Andi Kleen6619a532014-01-11 13:38:27 -08002594 }
2595
Adrian Hunter6657a092022-08-24 10:28:14 +03002596 err = event_enable_timer__start(rec->evlist->eet);
2597 if (err)
2598 goto out_child;
2599
Adrian Hunterda406202022-09-12 11:34:11 +03002600 /* Debug message used by test scripts */
2601 pr_debug3("perf record has started\n");
2602 fflush(stderr);
2603
Wang Nan5f9cf592016-04-20 18:59:49 +00002604 trigger_ready(&auxtrace_snapshot_trigger);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002605 trigger_ready(&switch_output_trigger);
Wang Nana0748652016-11-26 07:03:28 +00002606 perf_hooks__invoke_record_start();
Adrian Hunter3812d292022-06-10 14:33:15 +03002607
2608 /*
2609 * Must write FINISHED_INIT so it will be seen after all other
2610 * synthesized user events, but before any regular events.
2611 */
2612 err = write_finished_init(rec, false);
2613 if (err < 0)
2614 goto out_child;
2615
Peter Zijlstra649c48a2009-06-24 21:12:48 +02002616 for (;;) {
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002617 unsigned long long hits = thread->samples;
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002618
Wang Nan057374642016-07-14 08:34:43 +00002619 /*
2620 * rec->evlist->bkw_mmap_state is possible to be
2621 * BKW_MMAP_EMPTY here: when done == true and
2622 * hits != rec->samples in previous round.
2623 *
Arnaldo Carvalho de Meloade9d202020-11-30 09:33:55 -03002624 * evlist__toggle_bkw_mmap ensure we never
Wang Nan057374642016-07-14 08:34:43 +00002625 * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
2626 */
2627 if (trigger_is_hit(&switch_output_trigger) || done || draining)
Arnaldo Carvalho de Meloade9d202020-11-30 09:33:55 -03002628 evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
Wang Nan057374642016-07-14 08:34:43 +00002629
Alexey Budankov470530b2019-03-18 20:40:26 +03002630 if (record__mmap_read_all(rec, false) < 0) {
Wang Nan5f9cf592016-04-20 18:59:49 +00002631 trigger_error(&auxtrace_snapshot_trigger);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002632 trigger_error(&switch_output_trigger);
David Ahern8d3eca22012-08-26 12:24:47 -06002633 err = -1;
Namhyung Kim45604712014-05-12 09:47:24 +09002634 goto out_child;
David Ahern8d3eca22012-08-26 12:24:47 -06002635 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002636
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03002637 if (auxtrace_record__snapshot_started) {
2638 auxtrace_record__snapshot_started = 0;
Wang Nan5f9cf592016-04-20 18:59:49 +00002639 if (!trigger_is_error(&auxtrace_snapshot_trigger))
Alexander Shishkince7b0e42019-08-06 17:41:01 +03002640 record__read_auxtrace_snapshot(rec, false);
Wang Nan5f9cf592016-04-20 18:59:49 +00002641 if (trigger_is_error(&auxtrace_snapshot_trigger)) {
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03002642 pr_err("AUX area tracing snapshot failed\n");
2643 err = -1;
2644 goto out_child;
2645 }
2646 }
2647
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002648 if (trigger_is_hit(&switch_output_trigger)) {
Wang Nan057374642016-07-14 08:34:43 +00002649 /*
2650 * If switch_output_trigger is hit, the data in
2651 * overwritable ring buffer should have been collected,
2652 * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
2653 *
2654 * If SIGUSR2 raise after or during record__mmap_read_all(),
2655 * record__mmap_read_all() didn't collect data from
2656 * overwritable ring buffer. Read again.
2657 */
2658 if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
2659 continue;
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002660 trigger_ready(&switch_output_trigger);
2661
Wang Nan057374642016-07-14 08:34:43 +00002662 /*
2663 * Reenable events in overwrite ring buffer after
2664 * record__mmap_read_all(): we should have collected
2665 * data from it.
2666 */
Arnaldo Carvalho de Meloade9d202020-11-30 09:33:55 -03002667 evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
Wang Nan057374642016-07-14 08:34:43 +00002668
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002669 if (!quiet)
2670 fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002671 record__waking(rec));
2672 thread->waking = 0;
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002673 fd = record__switch_output(rec, false);
2674 if (fd < 0) {
2675 pr_err("Failed to switch to new file\n");
2676 trigger_error(&switch_output_trigger);
2677 err = fd;
2678 goto out_child;
2679 }
Jiri Olsabfacbe32017-01-09 10:52:00 +01002680
2681 /* re-arm the alarm */
2682 if (rec->switch_output.time)
2683 alarm(rec->switch_output.time);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002684 }
2685
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002686 if (hits == thread->samples) {
Arnaldo Carvalho de Melo6dcf45ef2014-08-13 11:33:59 -03002687 if (done || draining)
Peter Zijlstra649c48a2009-06-24 21:12:48 +02002688 break;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002689 err = fdarray__poll(&thread->pollfd, -1);
Jiri Olsaa5151142014-06-02 13:44:23 -04002690 /*
2691 * Propagate error, only if there's any. Ignore positive
2692 * number of returned events and interrupt error.
2693 */
2694 if (err > 0 || (err < 0 && errno == EINTR))
Namhyung Kim45604712014-05-12 09:47:24 +09002695 err = 0;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002696 thread->waking++;
Arnaldo Carvalho de Melo6dcf45ef2014-08-13 11:33:59 -03002697
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002698 if (fdarray__filter(&thread->pollfd, POLLERR | POLLHUP,
2699 record__thread_munmap_filtered, NULL) == 0)
Arnaldo Carvalho de Melo6dcf45ef2014-08-13 11:33:59 -03002700 draining = true;
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002701
Adrian Hunter6562c9a2022-08-24 10:28:10 +03002702 err = record__update_evlist_pollfd_from_thread(rec, rec->evlist, thread);
2703 if (err)
2704 goto out_child;
Peter Zijlstra8b412662009-09-17 19:59:05 +02002705 }
2706
Alexey Budankovacce0222020-07-17 10:07:50 +03002707 if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) {
2708 switch (cmd) {
Adrian Hunterd20aff12020-09-01 12:37:57 +03002709 case EVLIST_CTL_CMD_SNAPSHOT:
2710 hit_auxtrace_snapshot_trigger(rec);
2711 evlist__ctlfd_ack(rec->evlist);
2712 break;
Jiri Olsaf186cd62020-12-27 00:20:37 +01002713 case EVLIST_CTL_CMD_STOP:
2714 done = 1;
2715 break;
Alexey Budankovacce0222020-07-17 10:07:50 +03002716 case EVLIST_CTL_CMD_ACK:
2717 case EVLIST_CTL_CMD_UNSUPPORTED:
Jiri Olsa991ae4e2020-12-27 00:20:35 +01002718 case EVLIST_CTL_CMD_ENABLE:
2719 case EVLIST_CTL_CMD_DISABLE:
Jiri Olsa142544a2020-12-27 00:20:36 +01002720 case EVLIST_CTL_CMD_EVLIST:
Jiri Olsa47fddcb2020-12-27 00:20:38 +01002721 case EVLIST_CTL_CMD_PING:
Alexey Budankovacce0222020-07-17 10:07:50 +03002722 default:
2723 break;
2724 }
2725 }
2726
Adrian Hunter6657a092022-08-24 10:28:14 +03002727 err = event_enable_timer__process(rec->evlist->eet);
2728 if (err < 0)
2729 goto out_child;
2730 if (err) {
2731 err = 0;
2732 done = 1;
2733 }
2734
Jiri Olsa774cb492012-11-12 18:34:01 +01002735 /*
2736 * When perf is starting the traced process, at the end events
2737 * die with the process and we wait for that. Thus no need to
2738 * disable events in this case.
2739 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002740 if (done && !disabled && !target__none(&opts->target)) {
Wang Nan5f9cf592016-04-20 18:59:49 +00002741 trigger_off(&auxtrace_snapshot_trigger);
Jiri Olsae74676d2019-07-21 13:24:09 +02002742 evlist__disable(rec->evlist);
Jiri Olsa27119262012-11-12 18:34:02 +01002743 disabled = true;
2744 }
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002745 }
Alexander Shishkince7b0e42019-08-06 17:41:01 +03002746
Wang Nan5f9cf592016-04-20 18:59:49 +00002747 trigger_off(&auxtrace_snapshot_trigger);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00002748 trigger_off(&switch_output_trigger);
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002749
Alexander Shishkince7b0e42019-08-06 17:41:01 +03002750 if (opts->auxtrace_snapshot_on_exit)
2751 record__auxtrace_snapshot_exit(rec);
2752
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03002753 if (forks && workload_exec_errno) {
Arnaldo Carvalho de Melo3535a692021-04-14 09:32:14 -03002754 char msg[STRERR_BUFSIZE], strevsels[2048];
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002755 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melo3535a692021-04-14 09:32:14 -03002756
2757 evlist__scnprintf_evsels(rec->evlist, sizeof(strevsels), strevsels);
2758
2759 pr_err("Failed to collect '%s' for the '%s' workload: %s\n",
2760 strevsels, argv[0], emsg);
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03002761 err = -1;
Namhyung Kim45604712014-05-12 09:47:24 +09002762 goto out_child;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03002763 }
2764
Namhyung Kime3d59112015-01-29 17:06:44 +09002765 if (!quiet)
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002766 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n",
2767 record__waking(rec));
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -02002768
Adrian Hunter3812d292022-06-10 14:33:15 +03002769 write_finished_init(rec, true);
2770
Wang Nan4ea648a2016-07-14 08:34:47 +00002771 if (target__none(&rec->opts.target))
2772 record__synthesize_workload(rec, true);
2773
Namhyung Kim45604712014-05-12 09:47:24 +09002774out_child:
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002775 record__stop_threads(rec);
Alexey Budankov470530b2019-03-18 20:40:26 +03002776 record__mmap_read_all(rec, true);
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002777out_free_threads:
Alexey Bayduraev415ccb52022-01-17 21:34:23 +03002778 record__free_thread_data(rec);
Alexey Bayduraev396b6262022-01-17 21:34:25 +03002779 evlist__finalize_ctlfd(rec->evlist);
Alexey Budankovd3d1af62018-11-06 12:04:58 +03002780 record__aio_mmap_read_sync(rec);
2781
Alexey Budankovd3c8c082019-03-18 20:41:02 +03002782 if (rec->session->bytes_transferred && rec->session->bytes_compressed) {
2783 ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed;
2784 session->header.env.comp_ratio = ratio + 0.5;
2785 }
2786
Namhyung Kim45604712014-05-12 09:47:24 +09002787 if (forks) {
2788 int exit_status;
Ingo Molnaraddc2782009-06-02 23:43:11 +02002789
Namhyung Kim45604712014-05-12 09:47:24 +09002790 if (!child_finished)
2791 kill(rec->evlist->workload.pid, SIGTERM);
2792
2793 wait(&exit_status);
2794
2795 if (err < 0)
2796 status = err;
2797 else if (WIFEXITED(exit_status))
2798 status = WEXITSTATUS(exit_status);
2799 else if (WIFSIGNALED(exit_status))
2800 signr = WTERMSIG(exit_status);
2801 } else
2802 status = err;
2803
Namhyung Kimedc41a12022-05-18 15:47:21 -07002804 if (rec->off_cpu)
2805 rec->bytes_written += off_cpu_write(rec->session);
2806
Namhyung Kime3a23262022-09-01 12:57:37 -07002807 record__read_lost_samples(rec);
Wang Nan4ea648a2016-07-14 08:34:47 +00002808 record__synthesize(rec, true);
Namhyung Kime3d59112015-01-29 17:06:44 +09002809 /* this will be recalculated during process_buildids() */
2810 rec->samples = 0;
2811
Wang Nanecfd7a92016-04-13 08:21:07 +00002812 if (!err) {
2813 if (!rec->timestamp_filename) {
2814 record__finish_output(rec);
2815 } else {
2816 fd = record__switch_output(rec, true);
2817 if (fd < 0) {
2818 status = fd;
2819 goto out_delete_session;
2820 }
2821 }
2822 }
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -03002823
Wang Nana0748652016-11-26 07:03:28 +00002824 perf_hooks__invoke_record_end();
2825
Namhyung Kime3d59112015-01-29 17:06:44 +09002826 if (!err && !quiet) {
2827 char samples[128];
Wang Nanecfd7a92016-04-13 08:21:07 +00002828 const char *postfix = rec->timestamp_filename ?
2829 ".<timestamp>" : "";
Namhyung Kime3d59112015-01-29 17:06:44 +09002830
Adrian Hunteref149c22015-04-09 18:53:45 +03002831 if (rec->samples && !rec->opts.full_auxtrace)
Namhyung Kime3d59112015-01-29 17:06:44 +09002832 scnprintf(samples, sizeof(samples),
2833 " (%" PRIu64 " samples)", rec->samples);
2834 else
2835 samples[0] = '\0';
2836
Alexey Budankovd3c8c082019-03-18 20:41:02 +03002837 fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s",
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002838 perf_data__size(data) / 1024.0 / 1024.0,
Jiri Olsa2d4f2792019-02-21 10:41:30 +01002839 data->path, postfix, samples);
Alexey Budankovd3c8c082019-03-18 20:41:02 +03002840 if (ratio) {
2841 fprintf(stderr, ", compressed (original %.3f MB, ratio is %.3f)",
2842 rec->session->bytes_transferred / 1024.0 / 1024.0,
2843 ratio);
2844 }
2845 fprintf(stderr, " ]\n");
Namhyung Kime3d59112015-01-29 17:06:44 +09002846 }
2847
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -03002848out_delete_session:
Anand K Mistryda231332020-05-13 12:20:23 +10002849#ifdef HAVE_EVENTFD_SUPPORT
Ian Rogers304f0a22022-10-23 18:10:24 -07002850 if (done_fd >= 0) {
2851 fd = done_fd;
2852 done_fd = -1;
2853
2854 close(fd);
2855 }
Anand K Mistryda231332020-05-13 12:20:23 +10002856#endif
Alexey Budankov5d7f4112019-03-18 20:43:35 +03002857 zstd_fini(&session->zstd_data);
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -03002858 perf_session__delete(session);
Song Liu657ee552019-03-11 22:30:50 -07002859
2860 if (!opts->no_bpf_event)
Arnaldo Carvalho de Melo08c83992020-11-30 09:40:10 -03002861 evlist__stop_sb_thread(rec->sb_evlist);
Namhyung Kim45604712014-05-12 09:47:24 +09002862 return status;
Peter Zijlstrade9ac072009-04-08 15:01:31 +02002863}
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02002864
Arnaldo Carvalho de Melo0883e822016-04-15 16:37:17 -03002865static void callchain_debug(struct callchain_param *callchain)
Jiri Olsa09b0fd42013-10-26 16:25:33 +02002866{
Kan Liangaad2b212015-01-05 13:23:04 -05002867 static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
Jiri Olsaa601fdf2014-02-03 12:44:43 +01002868
Arnaldo Carvalho de Melo0883e822016-04-15 16:37:17 -03002869 pr_debug("callchain: type %s\n", str[callchain->record_mode]);
Jiri Olsa09b0fd42013-10-26 16:25:33 +02002870
Arnaldo Carvalho de Melo0883e822016-04-15 16:37:17 -03002871 if (callchain->record_mode == CALLCHAIN_DWARF)
Jiri Olsa09b0fd42013-10-26 16:25:33 +02002872 pr_debug("callchain: stack dump size %d\n",
Arnaldo Carvalho de Melo0883e822016-04-15 16:37:17 -03002873 callchain->dump_size);
2874}
2875
2876int record_opts__parse_callchain(struct record_opts *record,
2877 struct callchain_param *callchain,
2878 const char *arg, bool unset)
2879{
2880 int ret;
Arnaldo Carvalho de Melo0883e822016-04-15 16:37:17 -03002881 callchain->enabled = !unset;
2882
2883 /* --no-call-graph */
2884 if (unset) {
2885 callchain->record_mode = CALLCHAIN_NONE;
2886 pr_debug("callchain: disabled\n");
2887 return 0;
2888 }
2889
2890 ret = parse_callchain_record_opt(arg, callchain);
2891 if (!ret) {
2892 /* Enable data address sampling for DWARF unwind. */
2893 if (callchain->record_mode == CALLCHAIN_DWARF)
2894 record->sample_address = true;
2895 callchain_debug(callchain);
2896 }
2897
2898 return ret;
Jiri Olsa09b0fd42013-10-26 16:25:33 +02002899}
2900
Kan Liangc421e802015-07-29 05:42:12 -04002901int record_parse_callchain_opt(const struct option *opt,
Jiri Olsa09b0fd42013-10-26 16:25:33 +02002902 const char *arg,
2903 int unset)
2904{
Arnaldo Carvalho de Melo0883e822016-04-15 16:37:17 -03002905 return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
Jiri Olsa26d33022012-08-07 15:20:47 +02002906}
2907
Kan Liangc421e802015-07-29 05:42:12 -04002908int record_callchain_opt(const struct option *opt,
Jiri Olsa09b0fd42013-10-26 16:25:33 +02002909 const char *arg __maybe_unused,
2910 int unset __maybe_unused)
2911{
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002912 struct callchain_param *callchain = opt->value;
Kan Liangc421e802015-07-29 05:42:12 -04002913
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002914 callchain->enabled = true;
Jiri Olsa09b0fd42013-10-26 16:25:33 +02002915
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002916 if (callchain->record_mode == CALLCHAIN_NONE)
2917 callchain->record_mode = CALLCHAIN_FP;
Jiri Olsaeb853e82014-02-03 12:44:42 +01002918
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002919 callchain_debug(callchain);
Jiri Olsa09b0fd42013-10-26 16:25:33 +02002920 return 0;
2921}
2922
Jiri Olsaeb853e82014-02-03 12:44:42 +01002923static int perf_record_config(const char *var, const char *value, void *cb)
2924{
Namhyung Kim7a29c082015-12-15 10:49:56 +09002925 struct record *rec = cb;
2926
2927 if (!strcmp(var, "record.build-id")) {
2928 if (!strcmp(value, "cache"))
2929 rec->no_buildid_cache = false;
2930 else if (!strcmp(value, "no-cache"))
2931 rec->no_buildid_cache = true;
2932 else if (!strcmp(value, "skip"))
2933 rec->no_buildid = true;
Jiri Olsae29386c2020-12-14 11:54:57 +01002934 else if (!strcmp(value, "mmap"))
2935 rec->buildid_mmap = true;
Namhyung Kim7a29c082015-12-15 10:49:56 +09002936 else
2937 return -1;
2938 return 0;
2939 }
Yisheng Xiecff17202018-03-12 19:25:57 +08002940 if (!strcmp(var, "record.call-graph")) {
2941 var = "call-graph.record-mode";
2942 return perf_default_config(var, value, cb);
2943 }
Alexey Budankov93f20c02018-11-06 12:07:19 +03002944#ifdef HAVE_AIO_SUPPORT
2945 if (!strcmp(var, "record.aio")) {
2946 rec->opts.nr_cblocks = strtol(value, NULL, 0);
2947 if (!rec->opts.nr_cblocks)
2948 rec->opts.nr_cblocks = nr_cblocks_default;
2949 }
2950#endif
Jiri Olsa9bce13e2021-12-09 21:04:25 +01002951 if (!strcmp(var, "record.debuginfod")) {
2952 rec->debuginfod.urls = strdup(value);
2953 if (!rec->debuginfod.urls)
2954 return -ENOMEM;
2955 rec->debuginfod.set = true;
2956 }
Jiri Olsaeb853e82014-02-03 12:44:42 +01002957
Yisheng Xiecff17202018-03-12 19:25:57 +08002958 return 0;
Jiri Olsaeb853e82014-02-03 12:44:42 +01002959}
2960
Adrian Hunter6657a092022-08-24 10:28:14 +03002961static int record__parse_event_enable_time(const struct option *opt, const char *str, int unset)
2962{
2963 struct record *rec = (struct record *)opt->value;
2964
2965 return evlist__parse_event_enable_time(rec->evlist, &rec->opts, str, unset);
2966}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002967
Alexey Budankovf4fe11b2019-01-22 20:52:03 +03002968static int record__parse_affinity(const struct option *opt, const char *str, int unset)
2969{
2970 struct record_opts *opts = (struct record_opts *)opt->value;
2971
2972 if (unset || !str)
2973 return 0;
2974
2975 if (!strcasecmp(str, "node"))
2976 opts->affinity = PERF_AFFINITY_NODE;
2977 else if (!strcasecmp(str, "cpu"))
2978 opts->affinity = PERF_AFFINITY_CPU;
2979
2980 return 0;
2981}
2982
Alexey Bayduraev7954f712022-01-17 21:34:21 +03002983static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits)
2984{
2985 mask->nbits = nr_bits;
2986 mask->bits = bitmap_zalloc(mask->nbits);
2987 if (!mask->bits)
2988 return -ENOMEM;
2989
2990 return 0;
2991}
2992
2993static void record__mmap_cpu_mask_free(struct mmap_cpu_mask *mask)
2994{
2995 bitmap_free(mask->bits);
2996 mask->nbits = 0;
2997}
2998
2999static int record__thread_mask_alloc(struct thread_mask *mask, int nr_bits)
3000{
3001 int ret;
3002
3003 ret = record__mmap_cpu_mask_alloc(&mask->maps, nr_bits);
3004 if (ret) {
3005 mask->affinity.bits = NULL;
3006 return ret;
3007 }
3008
3009 ret = record__mmap_cpu_mask_alloc(&mask->affinity, nr_bits);
3010 if (ret) {
3011 record__mmap_cpu_mask_free(&mask->maps);
3012 mask->maps.bits = NULL;
3013 }
3014
3015 return ret;
3016}
3017
3018static void record__thread_mask_free(struct thread_mask *mask)
3019{
3020 record__mmap_cpu_mask_free(&mask->maps);
3021 record__mmap_cpu_mask_free(&mask->affinity);
3022}
3023
Alexey Bayduraev06380a82022-01-17 21:34:32 +03003024static int record__parse_threads(const struct option *opt, const char *str, int unset)
3025{
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003026 int s;
Alexey Bayduraev06380a82022-01-17 21:34:32 +03003027 struct record_opts *opts = opt->value;
3028
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003029 if (unset || !str || !strlen(str)) {
Alexey Bayduraev06380a82022-01-17 21:34:32 +03003030 opts->threads_spec = THREAD_SPEC__CPU;
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003031 } else {
3032 for (s = 1; s < THREAD_SPEC__MAX; s++) {
3033 if (s == THREAD_SPEC__USER) {
3034 opts->threads_user_spec = strdup(str);
3035 if (!opts->threads_user_spec)
3036 return -ENOMEM;
3037 opts->threads_spec = THREAD_SPEC__USER;
3038 break;
3039 }
3040 if (!strncasecmp(str, thread_spec_tags[s], strlen(thread_spec_tags[s]))) {
3041 opts->threads_spec = s;
3042 break;
3043 }
3044 }
3045 }
3046
3047 if (opts->threads_spec == THREAD_SPEC__USER)
3048 pr_debug("threads_spec: %s\n", opts->threads_user_spec);
3049 else
3050 pr_debug("threads_spec: %s\n", thread_spec_tags[opts->threads_spec]);
Alexey Bayduraev06380a82022-01-17 21:34:32 +03003051
3052 return 0;
3053}
3054
Jiwei Sun6d575812019-10-22 16:09:01 +08003055static int parse_output_max_size(const struct option *opt,
3056 const char *str, int unset)
3057{
3058 unsigned long *s = (unsigned long *)opt->value;
3059 static struct parse_tag tags_size[] = {
3060 { .tag = 'B', .mult = 1 },
3061 { .tag = 'K', .mult = 1 << 10 },
3062 { .tag = 'M', .mult = 1 << 20 },
3063 { .tag = 'G', .mult = 1 << 30 },
3064 { .tag = 0 },
3065 };
3066 unsigned long val;
3067
3068 if (unset) {
3069 *s = 0;
3070 return 0;
3071 }
3072
3073 val = parse_tag_value(str, tags_size);
3074 if (val != (unsigned long) -1) {
3075 *s = val;
3076 return 0;
3077 }
3078
3079 return -1;
3080}
3081
Adrian Huntere9db1312015-04-09 18:53:46 +03003082static int record__parse_mmap_pages(const struct option *opt,
3083 const char *str,
3084 int unset __maybe_unused)
3085{
3086 struct record_opts *opts = opt->value;
3087 char *s, *p;
3088 unsigned int mmap_pages;
3089 int ret;
3090
3091 if (!str)
3092 return -EINVAL;
3093
3094 s = strdup(str);
3095 if (!s)
3096 return -ENOMEM;
3097
3098 p = strchr(s, ',');
3099 if (p)
3100 *p = '\0';
3101
3102 if (*s) {
Arnaldo Carvalho de Melo25f847022020-11-30 15:09:45 -03003103 ret = __evlist__parse_mmap_pages(&mmap_pages, s);
Adrian Huntere9db1312015-04-09 18:53:46 +03003104 if (ret)
3105 goto out_free;
3106 opts->mmap_pages = mmap_pages;
3107 }
3108
3109 if (!p) {
3110 ret = 0;
3111 goto out_free;
3112 }
3113
Arnaldo Carvalho de Melo25f847022020-11-30 15:09:45 -03003114 ret = __evlist__parse_mmap_pages(&mmap_pages, p + 1);
Adrian Huntere9db1312015-04-09 18:53:46 +03003115 if (ret)
3116 goto out_free;
3117
3118 opts->auxtrace_mmap_pages = mmap_pages;
3119
3120out_free:
3121 free(s);
3122 return ret;
3123}
3124
Alexandre Truong7248e302021-12-17 15:45:15 +00003125void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __maybe_unused)
3126{
3127}
3128
Alexey Budankov1d078cc2020-07-17 10:08:23 +03003129static int parse_control_option(const struct option *opt,
3130 const char *str,
3131 int unset __maybe_unused)
3132{
Adrian Hunter9864a662020-09-01 12:37:53 +03003133 struct record_opts *opts = opt->value;
Alexey Budankov1d078cc2020-07-17 10:08:23 +03003134
Adrian Huntera8fcbd22020-09-02 13:57:07 +03003135 return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close);
3136}
3137
Jiri Olsa0c582442017-01-09 10:51:59 +01003138static void switch_output_size_warn(struct record *rec)
3139{
Jiri Olsa9521b5f2019-07-28 12:45:35 +02003140 u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages);
Jiri Olsa0c582442017-01-09 10:51:59 +01003141 struct switch_output *s = &rec->switch_output;
3142
3143 wakeup_size /= 2;
3144
3145 if (s->size < wakeup_size) {
3146 char buf[100];
3147
3148 unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
3149 pr_warning("WARNING: switch-output data size lower than "
3150 "wakeup kernel buffer size (%s) "
3151 "expect bigger perf.data sizes\n", buf);
3152 }
3153}
3154
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003155static int switch_output_setup(struct record *rec)
3156{
3157 struct switch_output *s = &rec->switch_output;
Jiri Olsadc0c6122017-01-09 10:51:58 +01003158 static struct parse_tag tags_size[] = {
3159 { .tag = 'B', .mult = 1 },
3160 { .tag = 'K', .mult = 1 << 10 },
3161 { .tag = 'M', .mult = 1 << 20 },
3162 { .tag = 'G', .mult = 1 << 30 },
3163 { .tag = 0 },
3164 };
Jiri Olsabfacbe32017-01-09 10:52:00 +01003165 static struct parse_tag tags_time[] = {
3166 { .tag = 's', .mult = 1 },
3167 { .tag = 'm', .mult = 60 },
3168 { .tag = 'h', .mult = 60*60 },
3169 { .tag = 'd', .mult = 60*60*24 },
3170 { .tag = 0 },
3171 };
Jiri Olsadc0c6122017-01-09 10:51:58 +01003172 unsigned long val;
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003173
Arnaldo Carvalho de Melo899e5ff2020-04-27 17:56:37 -03003174 /*
3175 * If we're using --switch-output-events, then we imply its
3176 * --switch-output=signal, as we'll send a SIGUSR2 from the side band
3177 * thread to its parent.
3178 */
Alexey Bayduraevb5f25112022-01-17 21:34:34 +03003179 if (rec->switch_output_event_set) {
3180 if (record__threads_enabled(rec)) {
3181 pr_warning("WARNING: --switch-output-event option is not available in parallel streaming mode.\n");
3182 return 0;
3183 }
Arnaldo Carvalho de Melo899e5ff2020-04-27 17:56:37 -03003184 goto do_signal;
Alexey Bayduraevb5f25112022-01-17 21:34:34 +03003185 }
Arnaldo Carvalho de Melo899e5ff2020-04-27 17:56:37 -03003186
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003187 if (!s->set)
3188 return 0;
3189
Alexey Bayduraevb5f25112022-01-17 21:34:34 +03003190 if (record__threads_enabled(rec)) {
3191 pr_warning("WARNING: --switch-output option is not available in parallel streaming mode.\n");
3192 return 0;
3193 }
3194
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003195 if (!strcmp(s->str, "signal")) {
Arnaldo Carvalho de Melo899e5ff2020-04-27 17:56:37 -03003196do_signal:
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003197 s->signal = true;
3198 pr_debug("switch-output with SIGUSR2 signal\n");
Jiri Olsadc0c6122017-01-09 10:51:58 +01003199 goto enabled;
3200 }
3201
3202 val = parse_tag_value(s->str, tags_size);
3203 if (val != (unsigned long) -1) {
3204 s->size = val;
3205 pr_debug("switch-output with %s size threshold\n", s->str);
3206 goto enabled;
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003207 }
3208
Jiri Olsabfacbe32017-01-09 10:52:00 +01003209 val = parse_tag_value(s->str, tags_time);
3210 if (val != (unsigned long) -1) {
3211 s->time = val;
3212 pr_debug("switch-output with %s time threshold (%lu seconds)\n",
3213 s->str, s->time);
3214 goto enabled;
3215 }
3216
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003217 return -1;
Jiri Olsadc0c6122017-01-09 10:51:58 +01003218
3219enabled:
3220 rec->timestamp_filename = true;
3221 s->enabled = true;
Jiri Olsa0c582442017-01-09 10:51:59 +01003222
3223 if (s->size && !rec->opts.no_buffering)
3224 switch_output_size_warn(rec);
3225
Jiri Olsadc0c6122017-01-09 10:51:58 +01003226 return 0;
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003227}
3228
Namhyung Kime5b2c202014-10-23 00:15:46 +09003229static const char * const __record_usage[] = {
Mike Galbraith9e0967532009-05-28 16:25:34 +02003230 "perf record [<options>] [<command>]",
3231 "perf record [<options>] -- <command> [<options>]",
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02003232 NULL
3233};
Namhyung Kime5b2c202014-10-23 00:15:46 +09003234const char * const *record_usage = __record_usage;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02003235
Arnaldo Carvalho de Melo6e0a9b3d2019-11-14 12:15:34 -03003236static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event,
3237 struct perf_sample *sample, struct machine *machine)
3238{
3239 /*
3240 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
3241 * no need to add them twice.
3242 */
3243 if (!(event->header.misc & PERF_RECORD_MISC_USER))
3244 return 0;
3245 return perf_event__process_mmap(tool, event, sample, machine);
3246}
3247
3248static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event,
3249 struct perf_sample *sample, struct machine *machine)
3250{
3251 /*
3252 * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
3253 * no need to add them twice.
3254 */
3255 if (!(event->header.misc & PERF_RECORD_MISC_USER))
3256 return 0;
3257
3258 return perf_event__process_mmap2(tool, event, sample, machine);
3259}
3260
Adrian Hunter66286ed2021-05-03 09:42:22 +03003261static int process_timestamp_boundary(struct perf_tool *tool,
3262 union perf_event *event __maybe_unused,
3263 struct perf_sample *sample,
3264 struct machine *machine __maybe_unused)
3265{
3266 struct record *rec = container_of(tool, struct record, tool);
3267
3268 set_timestamp_boundary(rec, sample->time);
3269 return 0;
3270}
3271
Namhyung Kim41b740b2021-08-10 21:46:58 -07003272static int parse_record_synth_option(const struct option *opt,
3273 const char *str,
3274 int unset __maybe_unused)
3275{
3276 struct record_opts *opts = opt->value;
3277 char *p = strdup(str);
3278
3279 if (p == NULL)
3280 return -1;
3281
3282 opts->synth = parse_synth_opt(p);
3283 free(p);
3284
3285 if (opts->synth < 0) {
3286 pr_err("Invalid synth option: %s\n", str);
3287 return -1;
3288 }
3289 return 0;
3290}
3291
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003292/*
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03003293 * XXX Ideally would be local to cmd_record() and passed to a record__new
3294 * because we need to have access to it in record__exit, that is called
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003295 * after cmd_record() exits, but since record_options need to be accessible to
3296 * builtin-script, leave it here.
3297 *
3298 * At least we don't ouch it in all the other functions here directly.
3299 *
3300 * Just say no to tons of global variables, sigh.
3301 */
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03003302static struct record record = {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003303 .opts = {
Andi Kleen8affc2b2014-07-31 14:45:04 +08003304 .sample_time = true,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003305 .mmap_pages = UINT_MAX,
3306 .user_freq = UINT_MAX,
3307 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo447a6012012-05-22 13:14:18 -03003308 .freq = 4000,
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +09003309 .target = {
3310 .uses_mmap = true,
Adrian Hunter3aa59392013-11-15 15:52:29 +02003311 .default_per_cpu = true,
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +09003312 },
Alexey Budankov470530b2019-03-18 20:40:26 +03003313 .mmap_flush = MMAP_FLUSH_DEFAULT,
Stephane Eraniand99c22e2020-04-22 08:50:38 -07003314 .nr_threads_synthesize = 1,
Alexey Budankov1d078cc2020-07-17 10:08:23 +03003315 .ctl_fd = -1,
3316 .ctl_fd_ack = -1,
Namhyung Kim41b740b2021-08-10 21:46:58 -07003317 .synth = PERF_SYNTH_ALL,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003318 },
Namhyung Kime3d59112015-01-29 17:06:44 +09003319 .tool = {
3320 .sample = process_sample_event,
3321 .fork = perf_event__process_fork,
Adrian Huntercca84822015-08-19 17:29:21 +03003322 .exit = perf_event__process_exit,
Namhyung Kime3d59112015-01-29 17:06:44 +09003323 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303324 .namespaces = perf_event__process_namespaces,
Arnaldo Carvalho de Melo6e0a9b3d2019-11-14 12:15:34 -03003325 .mmap = build_id__process_mmap,
3326 .mmap2 = build_id__process_mmap2,
Adrian Hunter66286ed2021-05-03 09:42:22 +03003327 .itrace_start = process_timestamp_boundary,
3328 .aux = process_timestamp_boundary,
Adrian Huntercca84822015-08-19 17:29:21 +03003329 .ordered_events = true,
Namhyung Kime3d59112015-01-29 17:06:44 +09003330 },
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003331};
Frederic Weisbecker7865e812010-04-14 19:42:07 +02003332
Namhyung Kim76a26542015-10-22 23:28:32 +09003333const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
3334 "\n\t\t\t\tDefault: fp";
Arnaldo Carvalho de Melo61eaa3b2012-10-01 15:20:58 -03003335
Wang Nan0aab2132016-06-16 08:02:41 +00003336static bool dry_run;
3337
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003338/*
3339 * XXX Will stay a global variable till we fix builtin-script.c to stop messing
3340 * with it and switch to use the library functions in perf_evlist that came
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03003341 * from builtin-record.c, i.e. use record_opts,
Arnaldo Carvalho de Melo7b392ef2020-11-30 09:26:54 -03003342 * evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003343 * using pipes, etc.
3344 */
Jiri Olsaefd21302017-01-03 09:19:55 +01003345static struct option __record_options[] = {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003346 OPT_CALLBACK('e', "event", &record.evlist, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02003347 "event selector. use 'perf list' to list available events",
Jiri Olsaf120f9d2011-07-14 11:25:32 +02003348 parse_events_option),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003349 OPT_CALLBACK(0, "filter", &record.evlist, "filter",
Li Zefanc171b552009-10-15 11:22:07 +08003350 "event filter", parse_filter),
Wang Nan4ba1faa2015-07-10 07:36:10 +00003351 OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
3352 NULL, "don't record events from perf itself",
3353 exclude_perf),
Namhyung Kimbea03402012-04-26 14:15:15 +09003354 OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03003355 "record events on existing process id"),
Namhyung Kimbea03402012-04-26 14:15:15 +09003356 OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03003357 "record events on existing thread id"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003358 OPT_INTEGER('r', "realtime", &record.realtime_prio,
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02003359 "collect data with this RT SCHED_FIFO priority"),
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03003360 OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
Kirill Smelkovacac03f2011-01-12 17:59:36 +03003361 "collect data without buffering"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003362 OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
Frederic Weisbeckerdaac07b2009-08-13 10:27:19 +02003363 "collect raw sample records from all opened counters"),
Namhyung Kimbea03402012-04-26 14:15:15 +09003364 OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02003365 "system-wide collection from all CPUs"),
Namhyung Kimbea03402012-04-26 14:15:15 +09003366 OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02003367 "list of cpus to monitor"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003368 OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003369 OPT_STRING('o', "output", &record.data.path, "file",
Ingo Molnarabaff322009-06-02 22:59:57 +02003370 "output file name"),
Adrian Hunter69e7e5b2013-11-18 11:55:57 +02003371 OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
3372 &record.opts.no_inherit_set,
3373 "child tasks do not inherit counters"),
Wang Nan4ea648a2016-07-14 08:34:47 +00003374 OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
3375 "synthesize non-sample events at the end of output"),
Wang Nan626a6b72016-07-14 08:34:45 +00003376 OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
Wei Lia060c1f2020-08-19 11:19:47 +08003377 OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"),
Arnaldo Carvalho de Melob09c2362018-03-01 14:52:50 -03003378 OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq,
3379 "Fail if the specified frequency can't be used"),
Arnaldo Carvalho de Melo67230472018-03-01 13:46:23 -03003380 OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'",
3381 "profile at this frequency",
3382 record__parse_freq),
Adrian Huntere9db1312015-04-09 18:53:46 +03003383 OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
3384 "number of mmap data pages and AUX area tracing mmap pages",
3385 record__parse_mmap_pages),
Alexey Budankov470530b2019-03-18 20:40:26 +03003386 OPT_CALLBACK(0, "mmap-flush", &record.opts, "number",
3387 "Minimal number of bytes that is extracted from mmap data pages (default: 1)",
3388 record__mmap_flush_parse),
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03003389 OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
Jiri Olsa09b0fd42013-10-26 16:25:33 +02003390 NULL, "enables call-graph recording" ,
3391 &record_callchain_opt),
3392 OPT_CALLBACK(0, "call-graph", &record.opts,
Namhyung Kim76a26542015-10-22 23:28:32 +09003393 "record_mode[,record_size]", record_callchain_help,
Jiri Olsa09b0fd42013-10-26 16:25:33 +02003394 &record_parse_callchain_opt),
Ian Munsiec0555642010-04-13 18:37:33 +10003395 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +02003396 "be more verbose (show counter open errors, etc)"),
James Clarka527c2c2022-10-18 10:41:36 +01003397 OPT_BOOLEAN('q', "quiet", &quiet, "don't print any warnings or messages"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003398 OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
Peter Zijlstra649c48a2009-06-24 21:12:48 +02003399 "per thread counts"),
Peter Zijlstra56100322015-06-10 16:48:50 +02003400 OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
Kan Liang3b0a5da2017-08-29 13:11:08 -04003401 OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr,
3402 "Record the sample physical addresses"),
Kan Liang542b88f2020-11-30 09:27:53 -08003403 OPT_BOOLEAN(0, "data-page-size", &record.opts.sample_data_page_size,
3404 "Record the sampled data address data page size"),
Kan Liangc1de7f32021-01-05 11:57:49 -08003405 OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size,
3406 "Record the sampled code address (ip) page size"),
Jiri Olsab6f35ed2016-08-01 20:02:35 +02003407 OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
Adrian Hunter61110882022-06-15 08:25:11 +03003408 OPT_BOOLEAN(0, "sample-identifier", &record.opts.sample_identifier,
3409 "Record the sample identifier"),
Adrian Hunter3abebc552015-07-06 14:51:01 +03003410 OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
3411 &record.opts.sample_time_set,
3412 "Record the sample timestamps"),
Jiri Olsaf290aa12018-02-01 09:38:11 +01003413 OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set,
3414 "Record the sample period"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003415 OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
Peter Zijlstra649c48a2009-06-24 21:12:48 +02003416 "don't sample"),
Wang Nand2db9a92016-01-25 09:56:19 +00003417 OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
3418 &record.no_buildid_cache_set,
3419 "do not update the buildid cache"),
3420 OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
3421 &record.no_buildid_set,
3422 "do not collect buildids in perf.data"),
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003423 OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
Stephane Eranian023695d2011-02-14 11:20:01 +02003424 "monitor event in cgroup name only",
3425 parse_cgroups),
Adrian Hunter6657a092022-08-24 10:28:14 +03003426 OPT_CALLBACK('D', "delay", &record, "ms",
3427 "ms to wait before starting measurement after program start (-1: start with events disabled), "
3428 "or ranges of time to enable events e.g. '-D 10-20,30-40'",
3429 record__parse_event_enable_time),
Adrian Huntereeb399b2019-10-04 11:31:21 +03003430 OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"),
Namhyung Kimbea03402012-04-26 14:15:15 +09003431 OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
3432 "user to profile"),
Stephane Eraniana5aabda2012-03-08 23:47:45 +01003433
3434 OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
3435 "branch any", "sample any taken branches",
3436 parse_branch_stack),
3437
3438 OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
3439 "branch filter mask", "branch stack filter modes",
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +01003440 parse_branch_stack),
Andi Kleen05484292013-01-24 16:10:29 +01003441 OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
3442 "sample by weight (on special events only)"),
Andi Kleen475eeab2013-09-20 07:40:43 -07003443 OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
3444 "sample transaction flags (special events only)"),
Adrian Hunter3aa59392013-11-15 15:52:29 +02003445 OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
3446 "use per-thread mmaps"),
Stephane Eranianbcc84ec2015-08-31 18:41:12 +02003447 OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
3448 "sample selected machine registers on interrupt,"
Kan Liangaeea9062019-05-14 13:19:32 -07003449 " use '-I?' to list register names", parse_intr_regs),
Andi Kleen84c41742017-09-05 10:00:28 -07003450 OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register",
3451 "sample selected machine registers on interrupt,"
Kan Liangaeea9062019-05-14 13:19:32 -07003452 " use '--user-regs=?' to list register names", parse_user_regs),
Andi Kleen85c273d2015-02-24 15:13:40 -08003453 OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
3454 "Record running/enabled time of read (:S) events"),
Peter Zijlstra814c8c32015-03-31 00:19:31 +02003455 OPT_CALLBACK('k', "clockid", &record.opts,
3456 "clockid", "clockid to use for events, see clock_gettime()",
3457 parse_clockid),
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03003458 OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
3459 "opts", "AUX area tracing Snapshot Mode", ""),
Adrian Hunterc0a6de02019-11-15 14:42:16 +02003460 OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts,
3461 "opts", "sample AUX area", ""),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08003462 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04003463 "per thread proc mmap processing timeout in ms"),
Hari Bathinif3b36142017-03-08 02:11:43 +05303464 OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces,
3465 "Record namespaces events"),
Namhyung Kim8fb4b672020-03-25 21:45:34 +09003466 OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup,
3467 "Record cgroup events"),
Adrian Hunter16b4b4e2020-05-28 15:08:58 +03003468 OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events,
3469 &record.opts.record_switch_events_set,
3470 "Record context switch events"),
Jiri Olsa85723882016-02-15 09:34:31 +01003471 OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
3472 "Configure all used events to run in kernel space.",
3473 PARSE_OPT_EXCLUSIVE),
3474 OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
3475 "Configure all used events to run in user space.",
3476 PARSE_OPT_EXCLUSIVE),
yuzhoujian53651b22019-05-30 14:29:22 +01003477 OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains,
3478 "collect kernel callchains"),
3479 OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains,
3480 "collect user callchains"),
Wang Nan71dc23262015-10-14 12:41:19 +00003481 OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
3482 "clang binary to use for compiling BPF scriptlets"),
3483 OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
3484 "options passed to clang when compiling BPF scriptlets"),
He Kuang7efe0e02015-12-14 10:39:23 +00003485 OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
3486 "file", "vmlinux pathname"),
Namhyung Kim61566812016-01-11 22:37:09 +09003487 OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
3488 "Record build-id of all DSOs regardless of hits"),
Jiri Olsae29386c2020-12-14 11:54:57 +01003489 OPT_BOOLEAN(0, "buildid-mmap", &record.buildid_mmap,
3490 "Record build-id in map events"),
Wang Nanecfd7a92016-04-13 08:21:07 +00003491 OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
3492 "append timestamp to output filename"),
Jin Yao68588ba2017-12-08 21:13:42 +08003493 OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary,
3494 "Record timestamp boundary (time of first/last samples)"),
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01003495 OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
Andi Kleenc38dab72019-03-14 15:49:56 -07003496 &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
3497 "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
Jiri Olsadc0c6122017-01-09 10:51:58 +01003498 "signal"),
Arnaldo Carvalho de Melo899e5ff2020-04-27 17:56:37 -03003499 OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event",
3500 "switch output event selector. use 'perf list' to list available events",
3501 parse_events_option_new_evlist),
Andi Kleen03724b22019-03-14 15:49:55 -07003502 OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
3503 "Limit number of switch output generated files"),
Wang Nan0aab2132016-06-16 08:02:41 +00003504 OPT_BOOLEAN(0, "dry-run", &dry_run,
3505 "Parse options then exit"),
Alexey Budankovd3d1af62018-11-06 12:04:58 +03003506#ifdef HAVE_AIO_SUPPORT
Alexey Budankov93f20c02018-11-06 12:07:19 +03003507 OPT_CALLBACK_OPTARG(0, "aio", &record.opts,
3508 &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
Alexey Budankovd3d1af62018-11-06 12:04:58 +03003509 record__aio_parse),
3510#endif
Alexey Budankovf4fe11b2019-01-22 20:52:03 +03003511 OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
3512 "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
3513 record__parse_affinity),
Alexey Budankov504c1ad2019-03-18 20:44:42 +03003514#ifdef HAVE_ZSTD_SUPPORT
Alexey Bayduraevb5f25112022-01-17 21:34:34 +03003515 OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, "n",
3516 "Compress records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
Alexey Budankov504c1ad2019-03-18 20:44:42 +03003517 record__parse_comp_level),
3518#endif
Jiwei Sun6d575812019-10-22 16:09:01 +08003519 OPT_CALLBACK(0, "max-size", &record.output_max_size,
3520 "size", "Limit the maximum size of the output file", parse_output_max_size),
Stephane Eraniand99c22e2020-04-22 08:50:38 -07003521 OPT_UINTEGER(0, "num-thread-synthesize",
3522 &record.opts.nr_threads_synthesize,
3523 "number of threads to run for event synthesis"),
Stephane Eranian70943492020-05-05 11:29:43 -07003524#ifdef HAVE_LIBPFM
3525 OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
3526 "libpfm4 event selector. use 'perf list' to list available events",
3527 parse_libpfm_events_option),
3528#endif
Adrian Huntera8fcbd22020-09-02 13:57:07 +03003529 OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
Adrian Hunterd20aff12020-09-01 12:37:57 +03003530 "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n"
3531 "\t\t\t 'snapshot': AUX area tracing snapshot).\n"
Adrian Huntera8fcbd22020-09-02 13:57:07 +03003532 "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
3533 "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
Alexey Budankov1d078cc2020-07-17 10:08:23 +03003534 parse_control_option),
Namhyung Kim41b740b2021-08-10 21:46:58 -07003535 OPT_CALLBACK(0, "synth", &record.opts, "no|all|task|mmap|cgroup",
3536 "Fine-tune event synthesis: default=all", parse_record_synth_option),
Jiri Olsa9bce13e2021-12-09 21:04:25 +01003537 OPT_STRING_OPTARG_SET(0, "debuginfod", &record.debuginfod.urls,
3538 &record.debuginfod.set, "debuginfod urls",
3539 "Enable debuginfod data retrieval from DEBUGINFOD_URLS or specified urls",
3540 "system"),
Alexey Bayduraev06380a82022-01-17 21:34:32 +03003541 OPT_CALLBACK_OPTARG(0, "threads", &record.opts, NULL, "spec",
3542 "write collected trace data into several data files using parallel threads",
3543 record__parse_threads),
Namhyung Kimedc41a12022-05-18 15:47:21 -07003544 OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"),
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02003545 OPT_END()
3546};
3547
Namhyung Kime5b2c202014-10-23 00:15:46 +09003548struct option *record_options = __record_options;
3549
Athira Rajeevcbd7bfc2022-09-05 19:49:29 +05303550static int record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpu_map *cpus)
Alexey Bayduraev7954f712022-01-17 21:34:21 +03003551{
Ian Rogers02555712022-05-02 21:17:52 -07003552 struct perf_cpu cpu;
3553 int idx;
Alexey Bayduraev7954f712022-01-17 21:34:21 +03003554
Alexey Bayduraev23380e42022-04-13 18:46:40 -07003555 if (cpu_map__is_dummy(cpus))
Athira Rajeevcbd7bfc2022-09-05 19:49:29 +05303556 return 0;
Alexey Bayduraev23380e42022-04-13 18:46:40 -07003557
Athira Rajeevcbd7bfc2022-09-05 19:49:29 +05303558 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Adrian Hunterca76d7d2022-09-15 15:26:11 +03003559 if (cpu.cpu == -1)
3560 continue;
Athira Rajeevcbd7bfc2022-09-05 19:49:29 +05303561 /* Return ENODEV is input cpu is greater than max cpu */
3562 if ((unsigned long)cpu.cpu > mask->nbits)
3563 return -ENODEV;
Sean Christopherson49bd97c2022-11-19 01:34:46 +00003564 __set_bit(cpu.cpu, mask->bits);
Athira Rajeevcbd7bfc2022-09-05 19:49:29 +05303565 }
3566
3567 return 0;
Alexey Bayduraev7954f712022-01-17 21:34:21 +03003568}
3569
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003570static int record__mmap_cpu_mask_init_spec(struct mmap_cpu_mask *mask, const char *mask_spec)
3571{
3572 struct perf_cpu_map *cpus;
3573
3574 cpus = perf_cpu_map__new(mask_spec);
3575 if (!cpus)
3576 return -ENOMEM;
3577
3578 bitmap_zero(mask->bits, mask->nbits);
Athira Rajeevcbd7bfc2022-09-05 19:49:29 +05303579 if (record__mmap_cpu_mask_init(mask, cpus))
3580 return -ENODEV;
3581
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003582 perf_cpu_map__put(cpus);
3583
3584 return 0;
3585}
3586
Alexey Bayduraev7954f712022-01-17 21:34:21 +03003587static void record__free_thread_masks(struct record *rec, int nr_threads)
3588{
3589 int t;
3590
3591 if (rec->thread_masks)
3592 for (t = 0; t < nr_threads; t++)
3593 record__thread_mask_free(&rec->thread_masks[t]);
3594
3595 zfree(&rec->thread_masks);
3596}
3597
3598static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr_bits)
3599{
3600 int t, ret;
3601
3602 rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks)));
3603 if (!rec->thread_masks) {
3604 pr_err("Failed to allocate thread masks\n");
3605 return -ENOMEM;
3606 }
3607
3608 for (t = 0; t < nr_threads; t++) {
3609 ret = record__thread_mask_alloc(&rec->thread_masks[t], nr_bits);
3610 if (ret) {
3611 pr_err("Failed to allocate thread masks[%d]\n", t);
3612 goto out_free;
3613 }
3614 }
3615
3616 return 0;
3617
3618out_free:
3619 record__free_thread_masks(rec, nr_threads);
3620
3621 return ret;
3622}
3623
Alexey Bayduraev06380a82022-01-17 21:34:32 +03003624static int record__init_thread_cpu_masks(struct record *rec, struct perf_cpu_map *cpus)
3625{
3626 int t, ret, nr_cpus = perf_cpu_map__nr(cpus);
3627
3628 ret = record__alloc_thread_masks(rec, nr_cpus, cpu__max_cpu().cpu);
3629 if (ret)
3630 return ret;
3631
3632 rec->nr_threads = nr_cpus;
3633 pr_debug("nr_threads: %d\n", rec->nr_threads);
3634
3635 for (t = 0; t < rec->nr_threads; t++) {
Sean Christopherson49bd97c2022-11-19 01:34:46 +00003636 __set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].maps.bits);
3637 __set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].affinity.bits);
Yang Jihong7c0a6142022-12-20 11:57:01 +08003638 if (verbose > 0) {
Alexey Bayduraev06380a82022-01-17 21:34:32 +03003639 pr_debug("thread_masks[%d]: ", t);
3640 mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps");
3641 pr_debug("thread_masks[%d]: ", t);
3642 mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity");
3643 }
3644 }
3645
3646 return 0;
3647}
3648
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003649static int record__init_thread_masks_spec(struct record *rec, struct perf_cpu_map *cpus,
3650 const char **maps_spec, const char **affinity_spec,
3651 u32 nr_spec)
3652{
3653 u32 s;
3654 int ret = 0, t = 0;
3655 struct mmap_cpu_mask cpus_mask;
3656 struct thread_mask thread_mask, full_mask, *thread_masks;
3657
3658 ret = record__mmap_cpu_mask_alloc(&cpus_mask, cpu__max_cpu().cpu);
3659 if (ret) {
3660 pr_err("Failed to allocate CPUs mask\n");
3661 return ret;
3662 }
Athira Rajeevcbd7bfc2022-09-05 19:49:29 +05303663
3664 ret = record__mmap_cpu_mask_init(&cpus_mask, cpus);
3665 if (ret) {
3666 pr_err("Failed to init cpu mask\n");
3667 goto out_free_cpu_mask;
3668 }
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003669
3670 ret = record__thread_mask_alloc(&full_mask, cpu__max_cpu().cpu);
3671 if (ret) {
3672 pr_err("Failed to allocate full mask\n");
3673 goto out_free_cpu_mask;
3674 }
3675
3676 ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu);
3677 if (ret) {
3678 pr_err("Failed to allocate thread mask\n");
3679 goto out_free_full_and_cpu_masks;
3680 }
3681
3682 for (s = 0; s < nr_spec; s++) {
3683 ret = record__mmap_cpu_mask_init_spec(&thread_mask.maps, maps_spec[s]);
3684 if (ret) {
3685 pr_err("Failed to initialize maps thread mask\n");
3686 goto out_free;
3687 }
3688 ret = record__mmap_cpu_mask_init_spec(&thread_mask.affinity, affinity_spec[s]);
3689 if (ret) {
3690 pr_err("Failed to initialize affinity thread mask\n");
3691 goto out_free;
3692 }
3693
3694 /* ignore invalid CPUs but do not allow empty masks */
3695 if (!bitmap_and(thread_mask.maps.bits, thread_mask.maps.bits,
3696 cpus_mask.bits, thread_mask.maps.nbits)) {
3697 pr_err("Empty maps mask: %s\n", maps_spec[s]);
3698 ret = -EINVAL;
3699 goto out_free;
3700 }
3701 if (!bitmap_and(thread_mask.affinity.bits, thread_mask.affinity.bits,
3702 cpus_mask.bits, thread_mask.affinity.nbits)) {
3703 pr_err("Empty affinity mask: %s\n", affinity_spec[s]);
3704 ret = -EINVAL;
3705 goto out_free;
3706 }
3707
3708 /* do not allow intersection with other masks (full_mask) */
3709 if (bitmap_intersects(thread_mask.maps.bits, full_mask.maps.bits,
3710 thread_mask.maps.nbits)) {
3711 pr_err("Intersecting maps mask: %s\n", maps_spec[s]);
3712 ret = -EINVAL;
3713 goto out_free;
3714 }
3715 if (bitmap_intersects(thread_mask.affinity.bits, full_mask.affinity.bits,
3716 thread_mask.affinity.nbits)) {
3717 pr_err("Intersecting affinity mask: %s\n", affinity_spec[s]);
3718 ret = -EINVAL;
3719 goto out_free;
3720 }
3721
3722 bitmap_or(full_mask.maps.bits, full_mask.maps.bits,
3723 thread_mask.maps.bits, full_mask.maps.nbits);
3724 bitmap_or(full_mask.affinity.bits, full_mask.affinity.bits,
3725 thread_mask.affinity.bits, full_mask.maps.nbits);
3726
3727 thread_masks = realloc(rec->thread_masks, (t + 1) * sizeof(struct thread_mask));
3728 if (!thread_masks) {
3729 pr_err("Failed to reallocate thread masks\n");
3730 ret = -ENOMEM;
3731 goto out_free;
3732 }
3733 rec->thread_masks = thread_masks;
3734 rec->thread_masks[t] = thread_mask;
Yang Jihong7c0a6142022-12-20 11:57:01 +08003735 if (verbose > 0) {
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003736 pr_debug("thread_masks[%d]: ", t);
3737 mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps");
3738 pr_debug("thread_masks[%d]: ", t);
3739 mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity");
3740 }
3741 t++;
3742 ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu);
3743 if (ret) {
3744 pr_err("Failed to allocate thread mask\n");
3745 goto out_free_full_and_cpu_masks;
3746 }
3747 }
3748 rec->nr_threads = t;
3749 pr_debug("nr_threads: %d\n", rec->nr_threads);
3750 if (!rec->nr_threads)
3751 ret = -EINVAL;
3752
3753out_free:
3754 record__thread_mask_free(&thread_mask);
3755out_free_full_and_cpu_masks:
3756 record__thread_mask_free(&full_mask);
3757out_free_cpu_mask:
3758 record__mmap_cpu_mask_free(&cpus_mask);
3759
3760 return ret;
3761}
3762
3763static int record__init_thread_core_masks(struct record *rec, struct perf_cpu_map *cpus)
3764{
3765 int ret;
3766 struct cpu_topology *topo;
3767
3768 topo = cpu_topology__new();
3769 if (!topo) {
3770 pr_err("Failed to allocate CPU topology\n");
3771 return -ENOMEM;
3772 }
3773
3774 ret = record__init_thread_masks_spec(rec, cpus, topo->core_cpus_list,
3775 topo->core_cpus_list, topo->core_cpus_lists);
3776 cpu_topology__delete(topo);
3777
3778 return ret;
3779}
3780
3781static int record__init_thread_package_masks(struct record *rec, struct perf_cpu_map *cpus)
3782{
3783 int ret;
3784 struct cpu_topology *topo;
3785
3786 topo = cpu_topology__new();
3787 if (!topo) {
3788 pr_err("Failed to allocate CPU topology\n");
3789 return -ENOMEM;
3790 }
3791
3792 ret = record__init_thread_masks_spec(rec, cpus, topo->package_cpus_list,
3793 topo->package_cpus_list, topo->package_cpus_lists);
3794 cpu_topology__delete(topo);
3795
3796 return ret;
3797}
3798
3799static int record__init_thread_numa_masks(struct record *rec, struct perf_cpu_map *cpus)
3800{
3801 u32 s;
3802 int ret;
3803 const char **spec;
3804 struct numa_topology *topo;
3805
3806 topo = numa_topology__new();
3807 if (!topo) {
3808 pr_err("Failed to allocate NUMA topology\n");
3809 return -ENOMEM;
3810 }
3811
3812 spec = zalloc(topo->nr * sizeof(char *));
3813 if (!spec) {
3814 pr_err("Failed to allocate NUMA spec\n");
3815 ret = -ENOMEM;
3816 goto out_delete_topo;
3817 }
3818 for (s = 0; s < topo->nr; s++)
3819 spec[s] = topo->nodes[s].cpus;
3820
3821 ret = record__init_thread_masks_spec(rec, cpus, spec, spec, topo->nr);
3822
3823 zfree(&spec);
3824
3825out_delete_topo:
3826 numa_topology__delete(topo);
3827
3828 return ret;
3829}
3830
3831static int record__init_thread_user_masks(struct record *rec, struct perf_cpu_map *cpus)
3832{
3833 int t, ret;
3834 u32 s, nr_spec = 0;
3835 char **maps_spec = NULL, **affinity_spec = NULL, **tmp_spec;
3836 char *user_spec, *spec, *spec_ptr, *mask, *mask_ptr, *dup_mask = NULL;
3837
3838 for (t = 0, user_spec = (char *)rec->opts.threads_user_spec; ; t++, user_spec = NULL) {
3839 spec = strtok_r(user_spec, ":", &spec_ptr);
3840 if (spec == NULL)
3841 break;
3842 pr_debug2("threads_spec[%d]: %s\n", t, spec);
3843 mask = strtok_r(spec, "/", &mask_ptr);
3844 if (mask == NULL)
3845 break;
3846 pr_debug2(" maps mask: %s\n", mask);
3847 tmp_spec = realloc(maps_spec, (nr_spec + 1) * sizeof(char *));
3848 if (!tmp_spec) {
3849 pr_err("Failed to reallocate maps spec\n");
3850 ret = -ENOMEM;
3851 goto out_free;
3852 }
3853 maps_spec = tmp_spec;
3854 maps_spec[nr_spec] = dup_mask = strdup(mask);
3855 if (!maps_spec[nr_spec]) {
3856 pr_err("Failed to allocate maps spec[%d]\n", nr_spec);
3857 ret = -ENOMEM;
3858 goto out_free;
3859 }
3860 mask = strtok_r(NULL, "/", &mask_ptr);
3861 if (mask == NULL) {
3862 pr_err("Invalid thread maps or affinity specs\n");
3863 ret = -EINVAL;
3864 goto out_free;
3865 }
3866 pr_debug2(" affinity mask: %s\n", mask);
3867 tmp_spec = realloc(affinity_spec, (nr_spec + 1) * sizeof(char *));
3868 if (!tmp_spec) {
3869 pr_err("Failed to reallocate affinity spec\n");
3870 ret = -ENOMEM;
3871 goto out_free;
3872 }
3873 affinity_spec = tmp_spec;
3874 affinity_spec[nr_spec] = strdup(mask);
3875 if (!affinity_spec[nr_spec]) {
3876 pr_err("Failed to allocate affinity spec[%d]\n", nr_spec);
3877 ret = -ENOMEM;
3878 goto out_free;
3879 }
3880 dup_mask = NULL;
3881 nr_spec++;
3882 }
3883
3884 ret = record__init_thread_masks_spec(rec, cpus, (const char **)maps_spec,
3885 (const char **)affinity_spec, nr_spec);
3886
3887out_free:
3888 free(dup_mask);
3889 for (s = 0; s < nr_spec; s++) {
3890 if (maps_spec)
3891 free(maps_spec[s]);
3892 if (affinity_spec)
3893 free(affinity_spec[s]);
3894 }
3895 free(affinity_spec);
3896 free(maps_spec);
3897
3898 return ret;
3899}
3900
Alexey Bayduraev7954f712022-01-17 21:34:21 +03003901static int record__init_thread_default_masks(struct record *rec, struct perf_cpu_map *cpus)
3902{
3903 int ret;
3904
3905 ret = record__alloc_thread_masks(rec, 1, cpu__max_cpu().cpu);
3906 if (ret)
3907 return ret;
3908
Athira Rajeevcbd7bfc2022-09-05 19:49:29 +05303909 if (record__mmap_cpu_mask_init(&rec->thread_masks->maps, cpus))
3910 return -ENODEV;
Alexey Bayduraev7954f712022-01-17 21:34:21 +03003911
3912 rec->nr_threads = 1;
3913
3914 return 0;
3915}
3916
3917static int record__init_thread_masks(struct record *rec)
3918{
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003919 int ret = 0;
Adrian Hunter7be1fed2022-05-24 10:54:30 +03003920 struct perf_cpu_map *cpus = rec->evlist->core.all_cpus;
Alexey Bayduraev7954f712022-01-17 21:34:21 +03003921
Alexey Bayduraev06380a82022-01-17 21:34:32 +03003922 if (!record__threads_enabled(rec))
3923 return record__init_thread_default_masks(rec, cpus);
3924
Adrian Hunter7be1fed2022-05-24 10:54:30 +03003925 if (evlist__per_thread(rec->evlist)) {
Alexey Bayduraev23380e42022-04-13 18:46:40 -07003926 pr_err("--per-thread option is mutually exclusive to parallel streaming mode.\n");
3927 return -EINVAL;
3928 }
3929
Alexey Bayduraevf466e5e2022-01-17 21:34:33 +03003930 switch (rec->opts.threads_spec) {
3931 case THREAD_SPEC__CPU:
3932 ret = record__init_thread_cpu_masks(rec, cpus);
3933 break;
3934 case THREAD_SPEC__CORE:
3935 ret = record__init_thread_core_masks(rec, cpus);
3936 break;
3937 case THREAD_SPEC__PACKAGE:
3938 ret = record__init_thread_package_masks(rec, cpus);
3939 break;
3940 case THREAD_SPEC__NUMA:
3941 ret = record__init_thread_numa_masks(rec, cpus);
3942 break;
3943 case THREAD_SPEC__USER:
3944 ret = record__init_thread_user_masks(rec, cpus);
3945 break;
3946 default:
3947 break;
3948 }
3949
3950 return ret;
Alexey Bayduraev7954f712022-01-17 21:34:21 +03003951}
3952
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003953int cmd_record(int argc, const char **argv)
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02003954{
Adrian Hunteref149c22015-04-09 18:53:45 +03003955 int err;
Arnaldo Carvalho de Melo8c6f45a2013-12-19 14:38:03 -03003956 struct record *rec = &record;
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09003957 char errbuf[BUFSIZ];
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02003958
Arnaldo Carvalho de Melo67230472018-03-01 13:46:23 -03003959 setlocale(LC_ALL, "");
3960
Wang Nan48e1cab2015-12-14 10:39:22 +00003961#ifndef HAVE_LIBBPF_SUPPORT
3962# define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
3963 set_nobuild('\0', "clang-path", true);
3964 set_nobuild('\0', "clang-opt", true);
3965# undef set_nobuild
3966#endif
3967
He Kuang7efe0e02015-12-14 10:39:23 +00003968#ifndef HAVE_BPF_PROLOGUE
3969# if !defined (HAVE_DWARF_SUPPORT)
3970# define REASON "NO_DWARF=1"
3971# elif !defined (HAVE_LIBBPF_SUPPORT)
3972# define REASON "NO_LIBBPF=1"
3973# else
3974# define REASON "this architecture doesn't support BPF prologue"
3975# endif
3976# define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
3977 set_nobuild('\0', "vmlinux", true);
3978# undef set_nobuild
3979# undef REASON
3980#endif
3981
Namhyung Kimedc41a12022-05-18 15:47:21 -07003982#ifndef HAVE_BPF_SKEL
3983# define set_nobuild(s, l, m, c) set_option_nobuild(record_options, s, l, m, c)
Arnaldo Carvalho de Melo9a2d5172023-05-06 18:07:37 -03003984 set_nobuild('\0', "off-cpu", "no BUILD_BPF_SKEL=1", true);
Namhyung Kimedc41a12022-05-18 15:47:21 -07003985# undef set_nobuild
3986#endif
3987
Alexey Budankov9d2ed642019-01-22 20:47:43 +03003988 rec->opts.affinity = PERF_AFFINITY_SYS;
3989
Jiri Olsa0f98b112019-07-21 13:23:55 +02003990 rec->evlist = evlist__new();
Arnaldo Carvalho de Melo3e2be2d2014-01-03 15:03:26 -03003991 if (rec->evlist == NULL)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02003992 return -ENOMEM;
3993
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03003994 err = perf_config(perf_record_config, rec);
3995 if (err)
3996 return err;
Jiri Olsaeb853e82014-02-03 12:44:42 +01003997
Tom Zanussibca647a2010-11-10 08:11:30 -06003998 argc = parse_options(argc, argv, record_options, record_usage,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02003999 PARSE_OPT_STOP_AT_NON_OPTION);
Namhyung Kim68ba3232017-02-17 17:17:42 +09004000 if (quiet)
4001 perf_quiet_option();
Jiri Olsa483635a2017-02-17 18:00:18 +01004002
James Clark7cc72552021-10-18 14:48:42 +01004003 err = symbol__validate_sym_arguments();
4004 if (err)
4005 return err;
4006
Jiri Olsa9bce13e2021-12-09 21:04:25 +01004007 perf_debuginfod_setup(&record.debuginfod);
4008
Jiri Olsa483635a2017-02-17 18:00:18 +01004009 /* Make system wide (-a) the default target. */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004010 if (!argc && target__none(&rec->opts.target))
Jiri Olsa483635a2017-02-17 18:00:18 +01004011 rec->opts.target.system_wide = true;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02004012
Namhyung Kimbea03402012-04-26 14:15:15 +09004013 if (nr_cgroups && !rec->opts.target.system_wide) {
Namhyung Kimc7118362015-10-25 00:49:27 +09004014 usage_with_options_msg(record_usage, record_options,
4015 "cgroup monitoring only available in system-wide mode");
4016
Stephane Eranian023695d2011-02-14 11:20:01 +02004017 }
Alexey Budankov504c1ad2019-03-18 20:44:42 +03004018
Jiri Olsae29386c2020-12-14 11:54:57 +01004019 if (rec->buildid_mmap) {
4020 if (!perf_can_record_build_id()) {
4021 pr_err("Failed: no support to record build id in mmap events, update your kernel.\n");
4022 err = -EINVAL;
4023 goto out_opts;
4024 }
4025 pr_debug("Enabling build id in mmap2 events.\n");
4026 /* Enable mmap build id synthesizing. */
4027 symbol_conf.buildid_mmap2 = true;
4028 /* Enable perf_event_attr::build_id bit. */
4029 rec->opts.build_id = true;
4030 /* Disable build id cache. */
4031 rec->no_buildid = true;
4032 }
4033
Namhyung Kim4f2abe92021-05-27 11:28:35 -07004034 if (rec->opts.record_cgroup && !perf_can_record_cgroup()) {
4035 pr_err("Kernel has no cgroup sampling support.\n");
4036 err = -EINVAL;
4037 goto out_opts;
4038 }
4039
Adrian Hunterf42c0ce2022-06-10 14:33:12 +03004040 if (rec->opts.kcore)
4041 rec->opts.text_poke = true;
4042
Alexey Bayduraev56f735f2022-01-17 21:34:28 +03004043 if (rec->opts.kcore || record__threads_enabled(rec))
Adrian Huntereeb399b2019-10-04 11:31:21 +03004044 rec->data.is_dir = true;
4045
Alexey Bayduraevb5f25112022-01-17 21:34:34 +03004046 if (record__threads_enabled(rec)) {
4047 if (rec->opts.affinity != PERF_AFFINITY_SYS) {
4048 pr_err("--affinity option is mutually exclusive to parallel streaming mode.\n");
4049 goto out_opts;
4050 }
4051 if (record__aio_enabled(rec)) {
4052 pr_err("Asynchronous streaming mode (--aio) is mutually exclusive to parallel streaming mode.\n");
4053 goto out_opts;
4054 }
4055 }
4056
Alexey Budankov504c1ad2019-03-18 20:44:42 +03004057 if (rec->opts.comp_level != 0) {
4058 pr_debug("Compression enabled, disabling build id collection at the end of the session.\n");
4059 rec->no_buildid = true;
4060 }
4061
Adrian Hunterb757bb02015-07-21 12:44:04 +03004062 if (rec->opts.record_switch_events &&
4063 !perf_can_record_switch_events()) {
Namhyung Kimc7118362015-10-25 00:49:27 +09004064 ui__error("kernel does not support recording context switch events\n");
4065 parse_options_usage(record_usage, record_options, "switch-events", 0);
Adrian Huntera8fcbd22020-09-02 13:57:07 +03004066 err = -EINVAL;
4067 goto out_opts;
Adrian Hunterb757bb02015-07-21 12:44:04 +03004068 }
Stephane Eranian023695d2011-02-14 11:20:01 +02004069
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01004070 if (switch_output_setup(rec)) {
4071 parse_options_usage(record_usage, record_options, "switch-output", 0);
Adrian Huntera8fcbd22020-09-02 13:57:07 +03004072 err = -EINVAL;
4073 goto out_opts;
Jiri Olsacb4e1eb2017-01-09 10:51:57 +01004074 }
4075
Jiri Olsabfacbe32017-01-09 10:52:00 +01004076 if (rec->switch_output.time) {
4077 signal(SIGALRM, alarm_sig_handler);
4078 alarm(rec->switch_output.time);
4079 }
4080
Andi Kleen03724b22019-03-14 15:49:55 -07004081 if (rec->switch_output.num_files) {
4082 rec->switch_output.filenames = calloc(sizeof(char *),
4083 rec->switch_output.num_files);
Adrian Huntera8fcbd22020-09-02 13:57:07 +03004084 if (!rec->switch_output.filenames) {
4085 err = -EINVAL;
4086 goto out_opts;
4087 }
Andi Kleen03724b22019-03-14 15:49:55 -07004088 }
4089
Alexey Bayduraevb5f25112022-01-17 21:34:34 +03004090 if (rec->timestamp_filename && record__threads_enabled(rec)) {
4091 rec->timestamp_filename = false;
4092 pr_warning("WARNING: --timestamp-filename option is not available in parallel streaming mode.\n");
4093 }
4094
Adrian Hunter1b36c032016-09-23 17:38:39 +03004095 /*
4096 * Allow aliases to facilitate the lookup of symbols for address
4097 * filters. Refer to auxtrace_parse_filters().
4098 */
4099 symbol_conf.allow_aliases = true;
4100
4101 symbol__init(NULL);
4102
Adrian Hunter4b5ea3b2018-03-06 11:13:12 +02004103 err = record__auxtrace_init(rec);
Adrian Hunter1b36c032016-09-23 17:38:39 +03004104 if (err)
4105 goto out;
4106
Wang Nan0aab2132016-06-16 08:02:41 +00004107 if (dry_run)
Adrian Hunter5c01ad602016-09-23 17:38:37 +03004108 goto out;
Wang Nan0aab2132016-06-16 08:02:41 +00004109
Wang Nand7888572016-04-08 15:07:24 +00004110 err = bpf__setup_stdout(rec->evlist);
4111 if (err) {
4112 bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
4113 pr_err("ERROR: Setup BPF stdout failed: %s\n",
4114 errbuf);
Adrian Hunter5c01ad602016-09-23 17:38:37 +03004115 goto out;
Wang Nand7888572016-04-08 15:07:24 +00004116 }
4117
Adrian Hunteref149c22015-04-09 18:53:45 +03004118 err = -ENOMEM;
4119
Wang Nan0c1d46a2016-04-20 18:59:52 +00004120 if (rec->no_buildid_cache || rec->no_buildid) {
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02004121 disable_buildid_cache();
Jiri Olsadc0c6122017-01-09 10:51:58 +01004122 } else if (rec->switch_output.enabled) {
Wang Nan0c1d46a2016-04-20 18:59:52 +00004123 /*
4124 * In 'perf record --switch-output', disable buildid
4125 * generation by default to reduce data file switching
4126 * overhead. Still generate buildid if they are required
4127 * explicitly using
4128 *
Jiri Olsa60437ac2017-01-03 09:19:56 +01004129 * perf record --switch-output --no-no-buildid \
Wang Nan0c1d46a2016-04-20 18:59:52 +00004130 * --no-no-buildid-cache
4131 *
4132 * Following code equals to:
4133 *
4134 * if ((rec->no_buildid || !rec->no_buildid_set) &&
4135 * (rec->no_buildid_cache || !rec->no_buildid_cache_set))
4136 * disable_buildid_cache();
4137 */
4138 bool disable = true;
4139
4140 if (rec->no_buildid_set && !rec->no_buildid)
4141 disable = false;
4142 if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
4143 disable = false;
4144 if (disable) {
4145 rec->no_buildid = true;
4146 rec->no_buildid_cache = true;
4147 disable_buildid_cache();
4148 }
4149 }
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02004150
Wang Nan4ea648a2016-07-14 08:34:47 +00004151 if (record.opts.overwrite)
4152 record.opts.tail_synthesize = true;
4153
Jin Yaob53a0752021-04-27 15:01:26 +08004154 if (rec->evlist->core.nr_entries == 0) {
4155 if (perf_pmu__has_hybrid()) {
4156 err = evlist__add_default_hybrid(rec->evlist,
4157 !record.opts.no_samples);
4158 } else {
4159 err = __evlist__add_default(rec->evlist,
4160 !record.opts.no_samples);
4161 }
4162
4163 if (err < 0) {
4164 pr_err("Not enough memory for event selector list\n");
4165 goto out;
4166 }
Peter Zijlstrabbd36e52009-06-11 23:11:50 +02004167 }
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02004168
Adrian Hunter69e7e5b2013-11-18 11:55:57 +02004169 if (rec->opts.target.tid && !rec->opts.no_inherit_set)
4170 rec->opts.no_inherit = true;
4171
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004172 err = target__validate(&rec->opts.target);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09004173 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004174 target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
Jiri Olsac3dec272018-02-06 19:17:58 +01004175 ui__warning("%s\n", errbuf);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09004176 }
Namhyung Kim4bd0f2d2012-04-26 14:15:18 +09004177
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004178 err = target__parse_uid(&rec->opts.target);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09004179 if (err) {
4180 int saved_errno = errno;
4181
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004182 target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
Namhyung Kim3780f482012-05-29 13:22:57 +09004183 ui__error("%s", errbuf);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09004184
4185 err = -saved_errno;
Adrian Hunter394c01e2016-09-23 17:38:36 +03004186 goto out;
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09004187 }
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02004188
Mengting Zhangca800062017-12-13 15:01:53 +08004189 /* Enable ignoring missing threads when -u/-p option is defined. */
4190 rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid;
Jiri Olsa23dc4f12016-12-12 11:35:43 +01004191
Jin Yao1d3351e2021-07-23 14:34:33 +08004192 if (evlist__fix_hybrid_cpus(rec->evlist, rec->opts.target.cpu_list)) {
4193 pr_err("failed to use cpu list %s\n",
4194 rec->opts.target.cpu_list);
4195 goto out;
4196 }
4197
4198 rec->opts.target.hybrid = perf_pmu__has_hybrid();
Alexandre Truong7248e302021-12-17 15:45:15 +00004199
4200 if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP)
4201 arch__add_leaf_frame_record_opts(&rec->opts);
4202
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09004203 err = -ENOMEM;
Martin Liška1bf7d832022-08-12 13:40:49 +02004204 if (evlist__create_maps(rec->evlist, &rec->opts.target) < 0) {
4205 if (rec->opts.target.pid != NULL) {
4206 pr_err("Couldn't create thread/CPU maps: %s\n",
4207 errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
4208 goto out;
4209 }
4210 else
4211 usage_with_options(record_usage, record_options);
4212 }
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02004213
Adrian Hunteref149c22015-04-09 18:53:45 +03004214 err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
4215 if (err)
Adrian Hunter394c01e2016-09-23 17:38:36 +03004216 goto out;
Adrian Hunteref149c22015-04-09 18:53:45 +03004217
Namhyung Kim61566812016-01-11 22:37:09 +09004218 /*
4219 * We take all buildids when the file contains
4220 * AUX area tracing data because we do not decode the
4221 * trace because it would take too long.
4222 */
4223 if (rec->opts.full_auxtrace)
4224 rec->buildid_all = true;
4225
Adrian Hunter246eba82020-05-12 15:19:18 +03004226 if (rec->opts.text_poke) {
4227 err = record__config_text_poke(rec->evlist);
4228 if (err) {
4229 pr_err("record__config_text_poke failed, error %d\n", err);
4230 goto out;
4231 }
4232 }
4233
Namhyung Kimedc41a12022-05-18 15:47:21 -07004234 if (rec->off_cpu) {
4235 err = record__config_off_cpu(rec);
4236 if (err) {
4237 pr_err("record__config_off_cpu failed, error %d\n", err);
4238 goto out;
4239 }
4240 }
4241
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03004242 if (record_opts__config(&rec->opts)) {
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -03004243 err = -EINVAL;
Adrian Hunter394c01e2016-09-23 17:38:36 +03004244 goto out;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02004245 }
4246
Alexey Bayduraev7954f712022-01-17 21:34:21 +03004247 err = record__init_thread_masks(rec);
4248 if (err) {
4249 pr_err("Failed to initialize parallel data streaming masks\n");
4250 goto out;
4251 }
4252
Alexey Budankov93f20c02018-11-06 12:07:19 +03004253 if (rec->opts.nr_cblocks > nr_cblocks_max)
4254 rec->opts.nr_cblocks = nr_cblocks_max;
Alexey Budankov5d7f4112019-03-18 20:43:35 +03004255 pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks);
Alexey Budankovd3d1af62018-11-06 12:04:58 +03004256
Alexey Budankov9d2ed642019-01-22 20:47:43 +03004257 pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]);
Alexey Budankov470530b2019-03-18 20:40:26 +03004258 pr_debug("mmap flush: %d\n", rec->opts.mmap_flush);
Alexey Budankov9d2ed642019-01-22 20:47:43 +03004259
Alexey Budankov51255a82019-03-18 20:42:19 +03004260 if (rec->opts.comp_level > comp_level_max)
4261 rec->opts.comp_level = comp_level_max;
4262 pr_debug("comp level: %d\n", rec->opts.comp_level);
4263
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02004264 err = __cmd_record(&record, argc, argv);
Adrian Hunter394c01e2016-09-23 17:38:36 +03004265out:
Jiri Olsac12995a2019-07-21 13:23:56 +02004266 evlist__delete(rec->evlist);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03004267 symbol__exit();
Adrian Hunteref149c22015-04-09 18:53:45 +03004268 auxtrace_record__free(rec->itr);
Adrian Huntera8fcbd22020-09-02 13:57:07 +03004269out_opts:
Alexey Bayduraev7954f712022-01-17 21:34:21 +03004270 record__free_thread_masks(rec, rec->nr_threads);
4271 rec->nr_threads = 0;
Adrian Hunteree7fe312020-09-03 15:29:37 +03004272 evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close);
Arnaldo Carvalho de Melo39d17da2010-07-29 14:08:55 -03004273 return err;
Ingo Molnar0e9b20b2009-05-26 09:17:18 +02004274}
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03004275
4276static void snapshot_sig_handler(int sig __maybe_unused)
4277{
Jiri Olsadc0c6122017-01-09 10:51:58 +01004278 struct record *rec = &record;
4279
Adrian Hunterd20aff12020-09-01 12:37:57 +03004280 hit_auxtrace_snapshot_trigger(rec);
Wang Nan3c1cb7e2016-04-20 18:59:50 +00004281
Jiri Olsadc0c6122017-01-09 10:51:58 +01004282 if (switch_output_signal(rec))
Wang Nan3c1cb7e2016-04-20 18:59:50 +00004283 trigger_hit(&switch_output_trigger);
Adrian Hunter2dd6d8a2015-04-30 17:37:32 +03004284}
Jiri Olsabfacbe32017-01-09 10:52:00 +01004285
4286static void alarm_sig_handler(int sig __maybe_unused)
4287{
4288 struct record *rec = &record;
4289
4290 if (switch_output_time(rec))
4291 trigger_hit(&switch_output_trigger);
4292}