1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * builtin-record.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Builtin record command: Record the profile of a workload
6*4882a593Smuzhiyun * (or a CPU, or a PID) into the perf.data output file - for
7*4882a593Smuzhiyun * later analysis via perf report.
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun #include "builtin.h"
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include "util/build-id.h"
12*4882a593Smuzhiyun #include <subcmd/parse-options.h>
13*4882a593Smuzhiyun #include "util/parse-events.h"
14*4882a593Smuzhiyun #include "util/config.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include "util/callchain.h"
17*4882a593Smuzhiyun #include "util/cgroup.h"
18*4882a593Smuzhiyun #include "util/header.h"
19*4882a593Smuzhiyun #include "util/event.h"
20*4882a593Smuzhiyun #include "util/evlist.h"
21*4882a593Smuzhiyun #include "util/evsel.h"
22*4882a593Smuzhiyun #include "util/debug.h"
23*4882a593Smuzhiyun #include "util/mmap.h"
24*4882a593Smuzhiyun #include "util/target.h"
25*4882a593Smuzhiyun #include "util/session.h"
26*4882a593Smuzhiyun #include "util/tool.h"
27*4882a593Smuzhiyun #include "util/symbol.h"
28*4882a593Smuzhiyun #include "util/record.h"
29*4882a593Smuzhiyun #include "util/cpumap.h"
30*4882a593Smuzhiyun #include "util/thread_map.h"
31*4882a593Smuzhiyun #include "util/data.h"
32*4882a593Smuzhiyun #include "util/perf_regs.h"
33*4882a593Smuzhiyun #include "util/auxtrace.h"
34*4882a593Smuzhiyun #include "util/tsc.h"
35*4882a593Smuzhiyun #include "util/parse-branch-options.h"
36*4882a593Smuzhiyun #include "util/parse-regs-options.h"
37*4882a593Smuzhiyun #include "util/perf_api_probe.h"
38*4882a593Smuzhiyun #include "util/llvm-utils.h"
39*4882a593Smuzhiyun #include "util/bpf-loader.h"
40*4882a593Smuzhiyun #include "util/trigger.h"
41*4882a593Smuzhiyun #include "util/perf-hooks.h"
42*4882a593Smuzhiyun #include "util/cpu-set-sched.h"
43*4882a593Smuzhiyun #include "util/synthetic-events.h"
44*4882a593Smuzhiyun #include "util/time-utils.h"
45*4882a593Smuzhiyun #include "util/units.h"
46*4882a593Smuzhiyun #include "util/bpf-event.h"
47*4882a593Smuzhiyun #include "util/util.h"
48*4882a593Smuzhiyun #include "util/pfm.h"
49*4882a593Smuzhiyun #include "util/clockid.h"
50*4882a593Smuzhiyun #include "asm/bug.h"
51*4882a593Smuzhiyun #include "perf.h"
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #include <errno.h>
54*4882a593Smuzhiyun #include <inttypes.h>
55*4882a593Smuzhiyun #include <locale.h>
56*4882a593Smuzhiyun #include <poll.h>
57*4882a593Smuzhiyun #include <pthread.h>
58*4882a593Smuzhiyun #include <unistd.h>
59*4882a593Smuzhiyun #include <sched.h>
60*4882a593Smuzhiyun #include <signal.h>
61*4882a593Smuzhiyun #ifdef HAVE_EVENTFD_SUPPORT
62*4882a593Smuzhiyun #include <sys/eventfd.h>
63*4882a593Smuzhiyun #endif
64*4882a593Smuzhiyun #include <sys/mman.h>
65*4882a593Smuzhiyun #include <sys/wait.h>
66*4882a593Smuzhiyun #include <sys/types.h>
67*4882a593Smuzhiyun #include <sys/stat.h>
68*4882a593Smuzhiyun #include <fcntl.h>
69*4882a593Smuzhiyun #include <linux/err.h>
70*4882a593Smuzhiyun #include <linux/string.h>
71*4882a593Smuzhiyun #include <linux/time64.h>
72*4882a593Smuzhiyun #include <linux/zalloc.h>
73*4882a593Smuzhiyun #include <linux/bitmap.h>
74*4882a593Smuzhiyun #include <sys/time.h>
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun struct switch_output {
77*4882a593Smuzhiyun bool enabled;
78*4882a593Smuzhiyun bool signal;
79*4882a593Smuzhiyun unsigned long size;
80*4882a593Smuzhiyun unsigned long time;
81*4882a593Smuzhiyun const char *str;
82*4882a593Smuzhiyun bool set;
83*4882a593Smuzhiyun char **filenames;
84*4882a593Smuzhiyun int num_files;
85*4882a593Smuzhiyun int cur_file;
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun struct record {
89*4882a593Smuzhiyun struct perf_tool tool;
90*4882a593Smuzhiyun struct record_opts opts;
91*4882a593Smuzhiyun u64 bytes_written;
92*4882a593Smuzhiyun struct perf_data data;
93*4882a593Smuzhiyun struct auxtrace_record *itr;
94*4882a593Smuzhiyun struct evlist *evlist;
95*4882a593Smuzhiyun struct perf_session *session;
96*4882a593Smuzhiyun struct evlist *sb_evlist;
97*4882a593Smuzhiyun pthread_t thread_id;
98*4882a593Smuzhiyun int realtime_prio;
99*4882a593Smuzhiyun bool switch_output_event_set;
100*4882a593Smuzhiyun bool no_buildid;
101*4882a593Smuzhiyun bool no_buildid_set;
102*4882a593Smuzhiyun bool no_buildid_cache;
103*4882a593Smuzhiyun bool no_buildid_cache_set;
104*4882a593Smuzhiyun bool buildid_all;
105*4882a593Smuzhiyun bool timestamp_filename;
106*4882a593Smuzhiyun bool timestamp_boundary;
107*4882a593Smuzhiyun struct switch_output switch_output;
108*4882a593Smuzhiyun unsigned long long samples;
109*4882a593Smuzhiyun struct mmap_cpu_mask affinity_mask;
110*4882a593Smuzhiyun unsigned long output_max_size; /* = 0: unlimited */
111*4882a593Smuzhiyun };
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun static volatile int done;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun static volatile int auxtrace_record__snapshot_started;
116*4882a593Smuzhiyun static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
117*4882a593Smuzhiyun static DEFINE_TRIGGER(switch_output_trigger);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun static const char *affinity_tags[PERF_AFFINITY_MAX] = {
120*4882a593Smuzhiyun "SYS", "NODE", "CPU"
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun
switch_output_signal(struct record * rec)123*4882a593Smuzhiyun static bool switch_output_signal(struct record *rec)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun return rec->switch_output.signal &&
126*4882a593Smuzhiyun trigger_is_ready(&switch_output_trigger);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
switch_output_size(struct record * rec)129*4882a593Smuzhiyun static bool switch_output_size(struct record *rec)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun return rec->switch_output.size &&
132*4882a593Smuzhiyun trigger_is_ready(&switch_output_trigger) &&
133*4882a593Smuzhiyun (rec->bytes_written >= rec->switch_output.size);
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
switch_output_time(struct record * rec)136*4882a593Smuzhiyun static bool switch_output_time(struct record *rec)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun return rec->switch_output.time &&
139*4882a593Smuzhiyun trigger_is_ready(&switch_output_trigger);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
record__output_max_size_exceeded(struct record * rec)142*4882a593Smuzhiyun static bool record__output_max_size_exceeded(struct record *rec)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun return rec->output_max_size &&
145*4882a593Smuzhiyun (rec->bytes_written >= rec->output_max_size);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
record__write(struct record * rec,struct mmap * map __maybe_unused,void * bf,size_t size)148*4882a593Smuzhiyun static int record__write(struct record *rec, struct mmap *map __maybe_unused,
149*4882a593Smuzhiyun void *bf, size_t size)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun struct perf_data_file *file = &rec->session->data->file;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (perf_data_file__write(file, bf, size) < 0) {
154*4882a593Smuzhiyun pr_err("failed to write perf data, error: %m\n");
155*4882a593Smuzhiyun return -1;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun rec->bytes_written += size;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun if (record__output_max_size_exceeded(rec) && !done) {
161*4882a593Smuzhiyun fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
162*4882a593Smuzhiyun " stopping session ]\n",
163*4882a593Smuzhiyun rec->bytes_written >> 10);
164*4882a593Smuzhiyun done = 1;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun if (switch_output_size(rec))
168*4882a593Smuzhiyun trigger_hit(&switch_output_trigger);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun return 0;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun static int record__aio_enabled(struct record *rec);
174*4882a593Smuzhiyun static int record__comp_enabled(struct record *rec);
175*4882a593Smuzhiyun static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size,
176*4882a593Smuzhiyun void *src, size_t src_size);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun #ifdef HAVE_AIO_SUPPORT
record__aio_write(struct aiocb * cblock,int trace_fd,void * buf,size_t size,off_t off)179*4882a593Smuzhiyun static int record__aio_write(struct aiocb *cblock, int trace_fd,
180*4882a593Smuzhiyun void *buf, size_t size, off_t off)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun int rc;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun cblock->aio_fildes = trace_fd;
185*4882a593Smuzhiyun cblock->aio_buf = buf;
186*4882a593Smuzhiyun cblock->aio_nbytes = size;
187*4882a593Smuzhiyun cblock->aio_offset = off;
188*4882a593Smuzhiyun cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun do {
191*4882a593Smuzhiyun rc = aio_write(cblock);
192*4882a593Smuzhiyun if (rc == 0) {
193*4882a593Smuzhiyun break;
194*4882a593Smuzhiyun } else if (errno != EAGAIN) {
195*4882a593Smuzhiyun cblock->aio_fildes = -1;
196*4882a593Smuzhiyun pr_err("failed to queue perf data, error: %m\n");
197*4882a593Smuzhiyun break;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun } while (1);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun return rc;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
record__aio_complete(struct mmap * md,struct aiocb * cblock)204*4882a593Smuzhiyun static int record__aio_complete(struct mmap *md, struct aiocb *cblock)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun void *rem_buf;
207*4882a593Smuzhiyun off_t rem_off;
208*4882a593Smuzhiyun size_t rem_size;
209*4882a593Smuzhiyun int rc, aio_errno;
210*4882a593Smuzhiyun ssize_t aio_ret, written;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun aio_errno = aio_error(cblock);
213*4882a593Smuzhiyun if (aio_errno == EINPROGRESS)
214*4882a593Smuzhiyun return 0;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun written = aio_ret = aio_return(cblock);
217*4882a593Smuzhiyun if (aio_ret < 0) {
218*4882a593Smuzhiyun if (aio_errno != EINTR)
219*4882a593Smuzhiyun pr_err("failed to write perf data, error: %m\n");
220*4882a593Smuzhiyun written = 0;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun rem_size = cblock->aio_nbytes - written;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun if (rem_size == 0) {
226*4882a593Smuzhiyun cblock->aio_fildes = -1;
227*4882a593Smuzhiyun /*
228*4882a593Smuzhiyun * md->refcount is incremented in record__aio_pushfn() for
229*4882a593Smuzhiyun * every aio write request started in record__aio_push() so
230*4882a593Smuzhiyun * decrement it because the request is now complete.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun perf_mmap__put(&md->core);
233*4882a593Smuzhiyun rc = 1;
234*4882a593Smuzhiyun } else {
235*4882a593Smuzhiyun /*
236*4882a593Smuzhiyun * aio write request may require restart with the
237*4882a593Smuzhiyun * reminder if the kernel didn't write whole
238*4882a593Smuzhiyun * chunk at once.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun rem_off = cblock->aio_offset + written;
241*4882a593Smuzhiyun rem_buf = (void *)(cblock->aio_buf + written);
242*4882a593Smuzhiyun record__aio_write(cblock, cblock->aio_fildes,
243*4882a593Smuzhiyun rem_buf, rem_size, rem_off);
244*4882a593Smuzhiyun rc = 0;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun return rc;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
record__aio_sync(struct mmap * md,bool sync_all)250*4882a593Smuzhiyun static int record__aio_sync(struct mmap *md, bool sync_all)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun struct aiocb **aiocb = md->aio.aiocb;
253*4882a593Smuzhiyun struct aiocb *cblocks = md->aio.cblocks;
254*4882a593Smuzhiyun struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */
255*4882a593Smuzhiyun int i, do_suspend;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun do {
258*4882a593Smuzhiyun do_suspend = 0;
259*4882a593Smuzhiyun for (i = 0; i < md->aio.nr_cblocks; ++i) {
260*4882a593Smuzhiyun if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
261*4882a593Smuzhiyun if (sync_all)
262*4882a593Smuzhiyun aiocb[i] = NULL;
263*4882a593Smuzhiyun else
264*4882a593Smuzhiyun return i;
265*4882a593Smuzhiyun } else {
266*4882a593Smuzhiyun /*
267*4882a593Smuzhiyun * Started aio write is not complete yet
268*4882a593Smuzhiyun * so it has to be waited before the
269*4882a593Smuzhiyun * next allocation.
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun aiocb[i] = &cblocks[i];
272*4882a593Smuzhiyun do_suspend = 1;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun if (!do_suspend)
276*4882a593Smuzhiyun return -1;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) {
279*4882a593Smuzhiyun if (!(errno == EAGAIN || errno == EINTR))
280*4882a593Smuzhiyun pr_err("failed to sync perf data, error: %m\n");
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun } while (1);
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun struct record_aio {
286*4882a593Smuzhiyun struct record *rec;
287*4882a593Smuzhiyun void *data;
288*4882a593Smuzhiyun size_t size;
289*4882a593Smuzhiyun };
290*4882a593Smuzhiyun
record__aio_pushfn(struct mmap * map,void * to,void * buf,size_t size)291*4882a593Smuzhiyun static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun struct record_aio *aio = to;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /*
296*4882a593Smuzhiyun * map->core.base data pointed by buf is copied into free map->aio.data[] buffer
297*4882a593Smuzhiyun * to release space in the kernel buffer as fast as possible, calling
298*4882a593Smuzhiyun * perf_mmap__consume() from perf_mmap__push() function.
299*4882a593Smuzhiyun *
300*4882a593Smuzhiyun * That lets the kernel to proceed with storing more profiling data into
301*4882a593Smuzhiyun * the kernel buffer earlier than other per-cpu kernel buffers are handled.
302*4882a593Smuzhiyun *
303*4882a593Smuzhiyun * Coping can be done in two steps in case the chunk of profiling data
304*4882a593Smuzhiyun * crosses the upper bound of the kernel buffer. In this case we first move
305*4882a593Smuzhiyun * part of data from map->start till the upper bound and then the reminder
306*4882a593Smuzhiyun * from the beginning of the kernel buffer till the end of the data chunk.
307*4882a593Smuzhiyun */
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun if (record__comp_enabled(aio->rec)) {
310*4882a593Smuzhiyun size = zstd_compress(aio->rec->session, aio->data + aio->size,
311*4882a593Smuzhiyun mmap__mmap_len(map) - aio->size,
312*4882a593Smuzhiyun buf, size);
313*4882a593Smuzhiyun } else {
314*4882a593Smuzhiyun memcpy(aio->data + aio->size, buf, size);
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun if (!aio->size) {
318*4882a593Smuzhiyun /*
319*4882a593Smuzhiyun * Increment map->refcount to guard map->aio.data[] buffer
320*4882a593Smuzhiyun * from premature deallocation because map object can be
321*4882a593Smuzhiyun * released earlier than aio write request started on
322*4882a593Smuzhiyun * map->aio.data[] buffer is complete.
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * perf_mmap__put() is done at record__aio_complete()
325*4882a593Smuzhiyun * after started aio request completion or at record__aio_push()
326*4882a593Smuzhiyun * if the request failed to start.
327*4882a593Smuzhiyun */
328*4882a593Smuzhiyun perf_mmap__get(&map->core);
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun aio->size += size;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun return size;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
record__aio_push(struct record * rec,struct mmap * map,off_t * off)336*4882a593Smuzhiyun static int record__aio_push(struct record *rec, struct mmap *map, off_t *off)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun int ret, idx;
339*4882a593Smuzhiyun int trace_fd = rec->session->data->file.fd;
340*4882a593Smuzhiyun struct record_aio aio = { .rec = rec, .size = 0 };
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun /*
343*4882a593Smuzhiyun * Call record__aio_sync() to wait till map->aio.data[] buffer
344*4882a593Smuzhiyun * becomes available after previous aio write operation.
345*4882a593Smuzhiyun */
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun idx = record__aio_sync(map, false);
348*4882a593Smuzhiyun aio.data = map->aio.data[idx];
349*4882a593Smuzhiyun ret = perf_mmap__push(map, &aio, record__aio_pushfn);
350*4882a593Smuzhiyun if (ret != 0) /* ret > 0 - no data, ret < 0 - error */
351*4882a593Smuzhiyun return ret;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun rec->samples++;
354*4882a593Smuzhiyun ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off);
355*4882a593Smuzhiyun if (!ret) {
356*4882a593Smuzhiyun *off += aio.size;
357*4882a593Smuzhiyun rec->bytes_written += aio.size;
358*4882a593Smuzhiyun if (switch_output_size(rec))
359*4882a593Smuzhiyun trigger_hit(&switch_output_trigger);
360*4882a593Smuzhiyun } else {
361*4882a593Smuzhiyun /*
362*4882a593Smuzhiyun * Decrement map->refcount incremented in record__aio_pushfn()
363*4882a593Smuzhiyun * back if record__aio_write() operation failed to start, otherwise
364*4882a593Smuzhiyun * map->refcount is decremented in record__aio_complete() after
365*4882a593Smuzhiyun * aio write operation finishes successfully.
366*4882a593Smuzhiyun */
367*4882a593Smuzhiyun perf_mmap__put(&map->core);
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun return ret;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
record__aio_get_pos(int trace_fd)373*4882a593Smuzhiyun static off_t record__aio_get_pos(int trace_fd)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun return lseek(trace_fd, 0, SEEK_CUR);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
record__aio_set_pos(int trace_fd,off_t pos)378*4882a593Smuzhiyun static void record__aio_set_pos(int trace_fd, off_t pos)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun lseek(trace_fd, pos, SEEK_SET);
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
record__aio_mmap_read_sync(struct record * rec)383*4882a593Smuzhiyun static void record__aio_mmap_read_sync(struct record *rec)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun int i;
386*4882a593Smuzhiyun struct evlist *evlist = rec->evlist;
387*4882a593Smuzhiyun struct mmap *maps = evlist->mmap;
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun if (!record__aio_enabled(rec))
390*4882a593Smuzhiyun return;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun for (i = 0; i < evlist->core.nr_mmaps; i++) {
393*4882a593Smuzhiyun struct mmap *map = &maps[i];
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (map->core.base)
396*4882a593Smuzhiyun record__aio_sync(map, true);
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun static int nr_cblocks_default = 1;
401*4882a593Smuzhiyun static int nr_cblocks_max = 4;
402*4882a593Smuzhiyun
record__aio_parse(const struct option * opt,const char * str,int unset)403*4882a593Smuzhiyun static int record__aio_parse(const struct option *opt,
404*4882a593Smuzhiyun const char *str,
405*4882a593Smuzhiyun int unset)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun struct record_opts *opts = (struct record_opts *)opt->value;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun if (unset) {
410*4882a593Smuzhiyun opts->nr_cblocks = 0;
411*4882a593Smuzhiyun } else {
412*4882a593Smuzhiyun if (str)
413*4882a593Smuzhiyun opts->nr_cblocks = strtol(str, NULL, 0);
414*4882a593Smuzhiyun if (!opts->nr_cblocks)
415*4882a593Smuzhiyun opts->nr_cblocks = nr_cblocks_default;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun return 0;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun #else /* HAVE_AIO_SUPPORT */
421*4882a593Smuzhiyun static int nr_cblocks_max = 0;
422*4882a593Smuzhiyun
record__aio_push(struct record * rec __maybe_unused,struct mmap * map __maybe_unused,off_t * off __maybe_unused)423*4882a593Smuzhiyun static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused,
424*4882a593Smuzhiyun off_t *off __maybe_unused)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun return -1;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
record__aio_get_pos(int trace_fd __maybe_unused)429*4882a593Smuzhiyun static off_t record__aio_get_pos(int trace_fd __maybe_unused)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun return -1;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
record__aio_set_pos(int trace_fd __maybe_unused,off_t pos __maybe_unused)434*4882a593Smuzhiyun static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
record__aio_mmap_read_sync(struct record * rec __maybe_unused)438*4882a593Smuzhiyun static void record__aio_mmap_read_sync(struct record *rec __maybe_unused)
439*4882a593Smuzhiyun {
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun #endif
442*4882a593Smuzhiyun
record__aio_enabled(struct record * rec)443*4882a593Smuzhiyun static int record__aio_enabled(struct record *rec)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun return rec->opts.nr_cblocks > 0;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun #define MMAP_FLUSH_DEFAULT 1
record__mmap_flush_parse(const struct option * opt,const char * str,int unset)449*4882a593Smuzhiyun static int record__mmap_flush_parse(const struct option *opt,
450*4882a593Smuzhiyun const char *str,
451*4882a593Smuzhiyun int unset)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun int flush_max;
454*4882a593Smuzhiyun struct record_opts *opts = (struct record_opts *)opt->value;
455*4882a593Smuzhiyun static struct parse_tag tags[] = {
456*4882a593Smuzhiyun { .tag = 'B', .mult = 1 },
457*4882a593Smuzhiyun { .tag = 'K', .mult = 1 << 10 },
458*4882a593Smuzhiyun { .tag = 'M', .mult = 1 << 20 },
459*4882a593Smuzhiyun { .tag = 'G', .mult = 1 << 30 },
460*4882a593Smuzhiyun { .tag = 0 },
461*4882a593Smuzhiyun };
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun if (unset)
464*4882a593Smuzhiyun return 0;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun if (str) {
467*4882a593Smuzhiyun opts->mmap_flush = parse_tag_value(str, tags);
468*4882a593Smuzhiyun if (opts->mmap_flush == (int)-1)
469*4882a593Smuzhiyun opts->mmap_flush = strtol(str, NULL, 0);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun if (!opts->mmap_flush)
473*4882a593Smuzhiyun opts->mmap_flush = MMAP_FLUSH_DEFAULT;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun flush_max = evlist__mmap_size(opts->mmap_pages);
476*4882a593Smuzhiyun flush_max /= 4;
477*4882a593Smuzhiyun if (opts->mmap_flush > flush_max)
478*4882a593Smuzhiyun opts->mmap_flush = flush_max;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun return 0;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun #ifdef HAVE_ZSTD_SUPPORT
484*4882a593Smuzhiyun static unsigned int comp_level_default = 1;
485*4882a593Smuzhiyun
record__parse_comp_level(const struct option * opt,const char * str,int unset)486*4882a593Smuzhiyun static int record__parse_comp_level(const struct option *opt, const char *str, int unset)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun struct record_opts *opts = opt->value;
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun if (unset) {
491*4882a593Smuzhiyun opts->comp_level = 0;
492*4882a593Smuzhiyun } else {
493*4882a593Smuzhiyun if (str)
494*4882a593Smuzhiyun opts->comp_level = strtol(str, NULL, 0);
495*4882a593Smuzhiyun if (!opts->comp_level)
496*4882a593Smuzhiyun opts->comp_level = comp_level_default;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun return 0;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun #endif
502*4882a593Smuzhiyun static unsigned int comp_level_max = 22;
503*4882a593Smuzhiyun
record__comp_enabled(struct record * rec)504*4882a593Smuzhiyun static int record__comp_enabled(struct record *rec)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun return rec->opts.comp_level > 0;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
process_synthesized_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)509*4882a593Smuzhiyun static int process_synthesized_event(struct perf_tool *tool,
510*4882a593Smuzhiyun union perf_event *event,
511*4882a593Smuzhiyun struct perf_sample *sample __maybe_unused,
512*4882a593Smuzhiyun struct machine *machine __maybe_unused)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun struct record *rec = container_of(tool, struct record, tool);
515*4882a593Smuzhiyun return record__write(rec, NULL, event, event->header.size);
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun
process_locked_synthesized_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)518*4882a593Smuzhiyun static int process_locked_synthesized_event(struct perf_tool *tool,
519*4882a593Smuzhiyun union perf_event *event,
520*4882a593Smuzhiyun struct perf_sample *sample __maybe_unused,
521*4882a593Smuzhiyun struct machine *machine __maybe_unused)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER;
524*4882a593Smuzhiyun int ret;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun pthread_mutex_lock(&synth_lock);
527*4882a593Smuzhiyun ret = process_synthesized_event(tool, event, sample, machine);
528*4882a593Smuzhiyun pthread_mutex_unlock(&synth_lock);
529*4882a593Smuzhiyun return ret;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
record__pushfn(struct mmap * map,void * to,void * bf,size_t size)532*4882a593Smuzhiyun static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun struct record *rec = to;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun if (record__comp_enabled(rec)) {
537*4882a593Smuzhiyun size = zstd_compress(rec->session, map->data, mmap__mmap_len(map), bf, size);
538*4882a593Smuzhiyun bf = map->data;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun rec->samples++;
542*4882a593Smuzhiyun return record__write(rec, map, bf, size);
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun static volatile int signr = -1;
546*4882a593Smuzhiyun static volatile int child_finished;
547*4882a593Smuzhiyun #ifdef HAVE_EVENTFD_SUPPORT
548*4882a593Smuzhiyun static int done_fd = -1;
549*4882a593Smuzhiyun #endif
550*4882a593Smuzhiyun
sig_handler(int sig)551*4882a593Smuzhiyun static void sig_handler(int sig)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun if (sig == SIGCHLD)
554*4882a593Smuzhiyun child_finished = 1;
555*4882a593Smuzhiyun else
556*4882a593Smuzhiyun signr = sig;
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun done = 1;
559*4882a593Smuzhiyun #ifdef HAVE_EVENTFD_SUPPORT
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun u64 tmp = 1;
562*4882a593Smuzhiyun /*
563*4882a593Smuzhiyun * It is possible for this signal handler to run after done is checked
564*4882a593Smuzhiyun * in the main loop, but before the perf counter fds are polled. If this
565*4882a593Smuzhiyun * happens, the poll() will continue to wait even though done is set,
566*4882a593Smuzhiyun * and will only break out if either another signal is received, or the
567*4882a593Smuzhiyun * counters are ready for read. To ensure the poll() doesn't sleep when
568*4882a593Smuzhiyun * done is set, use an eventfd (done_fd) to wake up the poll().
569*4882a593Smuzhiyun */
570*4882a593Smuzhiyun if (write(done_fd, &tmp, sizeof(tmp)) < 0)
571*4882a593Smuzhiyun pr_err("failed to signal wakeup fd, error: %m\n");
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun #endif // HAVE_EVENTFD_SUPPORT
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
sigsegv_handler(int sig)576*4882a593Smuzhiyun static void sigsegv_handler(int sig)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun perf_hooks__recover();
579*4882a593Smuzhiyun sighandler_dump_stack(sig);
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun
record__sig_exit(void)582*4882a593Smuzhiyun static void record__sig_exit(void)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun if (signr == -1)
585*4882a593Smuzhiyun return;
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun signal(signr, SIG_DFL);
588*4882a593Smuzhiyun raise(signr);
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun #ifdef HAVE_AUXTRACE_SUPPORT
592*4882a593Smuzhiyun
record__process_auxtrace(struct perf_tool * tool,struct mmap * map,union perf_event * event,void * data1,size_t len1,void * data2,size_t len2)593*4882a593Smuzhiyun static int record__process_auxtrace(struct perf_tool *tool,
594*4882a593Smuzhiyun struct mmap *map,
595*4882a593Smuzhiyun union perf_event *event, void *data1,
596*4882a593Smuzhiyun size_t len1, void *data2, size_t len2)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun struct record *rec = container_of(tool, struct record, tool);
599*4882a593Smuzhiyun struct perf_data *data = &rec->data;
600*4882a593Smuzhiyun size_t padding;
601*4882a593Smuzhiyun u8 pad[8] = {0};
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) {
604*4882a593Smuzhiyun off_t file_offset;
605*4882a593Smuzhiyun int fd = perf_data__fd(data);
606*4882a593Smuzhiyun int err;
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun file_offset = lseek(fd, 0, SEEK_CUR);
609*4882a593Smuzhiyun if (file_offset == -1)
610*4882a593Smuzhiyun return -1;
611*4882a593Smuzhiyun err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
612*4882a593Smuzhiyun event, file_offset);
613*4882a593Smuzhiyun if (err)
614*4882a593Smuzhiyun return err;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
618*4882a593Smuzhiyun padding = (len1 + len2) & 7;
619*4882a593Smuzhiyun if (padding)
620*4882a593Smuzhiyun padding = 8 - padding;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun record__write(rec, map, event, event->header.size);
623*4882a593Smuzhiyun record__write(rec, map, data1, len1);
624*4882a593Smuzhiyun if (len2)
625*4882a593Smuzhiyun record__write(rec, map, data2, len2);
626*4882a593Smuzhiyun record__write(rec, map, &pad, padding);
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun return 0;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
record__auxtrace_mmap_read(struct record * rec,struct mmap * map)631*4882a593Smuzhiyun static int record__auxtrace_mmap_read(struct record *rec,
632*4882a593Smuzhiyun struct mmap *map)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun int ret;
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun ret = auxtrace_mmap__read(map, rec->itr, &rec->tool,
637*4882a593Smuzhiyun record__process_auxtrace);
638*4882a593Smuzhiyun if (ret < 0)
639*4882a593Smuzhiyun return ret;
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun if (ret)
642*4882a593Smuzhiyun rec->samples++;
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun return 0;
645*4882a593Smuzhiyun }
646*4882a593Smuzhiyun
record__auxtrace_mmap_read_snapshot(struct record * rec,struct mmap * map)647*4882a593Smuzhiyun static int record__auxtrace_mmap_read_snapshot(struct record *rec,
648*4882a593Smuzhiyun struct mmap *map)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun int ret;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool,
653*4882a593Smuzhiyun record__process_auxtrace,
654*4882a593Smuzhiyun rec->opts.auxtrace_snapshot_size);
655*4882a593Smuzhiyun if (ret < 0)
656*4882a593Smuzhiyun return ret;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun if (ret)
659*4882a593Smuzhiyun rec->samples++;
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun return 0;
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun
record__auxtrace_read_snapshot_all(struct record * rec)664*4882a593Smuzhiyun static int record__auxtrace_read_snapshot_all(struct record *rec)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun int i;
667*4882a593Smuzhiyun int rc = 0;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun for (i = 0; i < rec->evlist->core.nr_mmaps; i++) {
670*4882a593Smuzhiyun struct mmap *map = &rec->evlist->mmap[i];
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun if (!map->auxtrace_mmap.base)
673*4882a593Smuzhiyun continue;
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) {
676*4882a593Smuzhiyun rc = -1;
677*4882a593Smuzhiyun goto out;
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun out:
681*4882a593Smuzhiyun return rc;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun
record__read_auxtrace_snapshot(struct record * rec,bool on_exit)684*4882a593Smuzhiyun static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun pr_debug("Recording AUX area tracing snapshot\n");
687*4882a593Smuzhiyun if (record__auxtrace_read_snapshot_all(rec) < 0) {
688*4882a593Smuzhiyun trigger_error(&auxtrace_snapshot_trigger);
689*4882a593Smuzhiyun } else {
690*4882a593Smuzhiyun if (auxtrace_record__snapshot_finish(rec->itr, on_exit))
691*4882a593Smuzhiyun trigger_error(&auxtrace_snapshot_trigger);
692*4882a593Smuzhiyun else
693*4882a593Smuzhiyun trigger_ready(&auxtrace_snapshot_trigger);
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun
record__auxtrace_snapshot_exit(struct record * rec)697*4882a593Smuzhiyun static int record__auxtrace_snapshot_exit(struct record *rec)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun if (trigger_is_error(&auxtrace_snapshot_trigger))
700*4882a593Smuzhiyun return 0;
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun if (!auxtrace_record__snapshot_started &&
703*4882a593Smuzhiyun auxtrace_record__snapshot_start(rec->itr))
704*4882a593Smuzhiyun return -1;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun record__read_auxtrace_snapshot(rec, true);
707*4882a593Smuzhiyun if (trigger_is_error(&auxtrace_snapshot_trigger))
708*4882a593Smuzhiyun return -1;
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun return 0;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun
record__auxtrace_init(struct record * rec)713*4882a593Smuzhiyun static int record__auxtrace_init(struct record *rec)
714*4882a593Smuzhiyun {
715*4882a593Smuzhiyun int err;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun if (!rec->itr) {
718*4882a593Smuzhiyun rec->itr = auxtrace_record__init(rec->evlist, &err);
719*4882a593Smuzhiyun if (err)
720*4882a593Smuzhiyun return err;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
724*4882a593Smuzhiyun rec->opts.auxtrace_snapshot_opts);
725*4882a593Smuzhiyun if (err)
726*4882a593Smuzhiyun return err;
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts,
729*4882a593Smuzhiyun rec->opts.auxtrace_sample_opts);
730*4882a593Smuzhiyun if (err)
731*4882a593Smuzhiyun return err;
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun return auxtrace_parse_filters(rec->evlist);
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun #else
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun static inline
record__auxtrace_mmap_read(struct record * rec __maybe_unused,struct mmap * map __maybe_unused)739*4882a593Smuzhiyun int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
740*4882a593Smuzhiyun struct mmap *map __maybe_unused)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun return 0;
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun static inline
record__read_auxtrace_snapshot(struct record * rec __maybe_unused,bool on_exit __maybe_unused)746*4882a593Smuzhiyun void record__read_auxtrace_snapshot(struct record *rec __maybe_unused,
747*4882a593Smuzhiyun bool on_exit __maybe_unused)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun static inline
auxtrace_record__snapshot_start(struct auxtrace_record * itr __maybe_unused)752*4882a593Smuzhiyun int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun return 0;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun static inline
record__auxtrace_snapshot_exit(struct record * rec __maybe_unused)758*4882a593Smuzhiyun int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun return 0;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun
record__auxtrace_init(struct record * rec __maybe_unused)763*4882a593Smuzhiyun static int record__auxtrace_init(struct record *rec __maybe_unused)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun return 0;
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun #endif
769*4882a593Smuzhiyun
record__config_text_poke(struct evlist * evlist)770*4882a593Smuzhiyun static int record__config_text_poke(struct evlist *evlist)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun struct evsel *evsel;
773*4882a593Smuzhiyun int err;
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun /* Nothing to do if text poke is already configured */
776*4882a593Smuzhiyun evlist__for_each_entry(evlist, evsel) {
777*4882a593Smuzhiyun if (evsel->core.attr.text_poke)
778*4882a593Smuzhiyun return 0;
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun err = parse_events(evlist, "dummy:u", NULL);
782*4882a593Smuzhiyun if (err)
783*4882a593Smuzhiyun return err;
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun evsel = evlist__last(evlist);
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun evsel->core.attr.freq = 0;
788*4882a593Smuzhiyun evsel->core.attr.sample_period = 1;
789*4882a593Smuzhiyun evsel->core.attr.text_poke = 1;
790*4882a593Smuzhiyun evsel->core.attr.ksymbol = 1;
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun evsel->core.system_wide = true;
793*4882a593Smuzhiyun evsel->no_aux_samples = true;
794*4882a593Smuzhiyun evsel->immediate = true;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /* Text poke must be collected on all CPUs */
797*4882a593Smuzhiyun perf_cpu_map__put(evsel->core.own_cpus);
798*4882a593Smuzhiyun evsel->core.own_cpus = perf_cpu_map__new(NULL);
799*4882a593Smuzhiyun perf_cpu_map__put(evsel->core.cpus);
800*4882a593Smuzhiyun evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus);
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun evsel__set_sample_bit(evsel, TIME);
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun return 0;
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun
record__kcore_readable(struct machine * machine)807*4882a593Smuzhiyun static bool record__kcore_readable(struct machine *machine)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun char kcore[PATH_MAX];
810*4882a593Smuzhiyun int fd;
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir);
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun fd = open(kcore, O_RDONLY);
815*4882a593Smuzhiyun if (fd < 0)
816*4882a593Smuzhiyun return false;
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun close(fd);
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun return true;
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun
record__kcore_copy(struct machine * machine,struct perf_data * data)823*4882a593Smuzhiyun static int record__kcore_copy(struct machine *machine, struct perf_data *data)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun char from_dir[PATH_MAX];
826*4882a593Smuzhiyun char kcore_dir[PATH_MAX];
827*4882a593Smuzhiyun int ret;
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir);
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir));
832*4882a593Smuzhiyun if (ret)
833*4882a593Smuzhiyun return ret;
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun return kcore_copy(from_dir, kcore_dir);
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun
record__mmap_evlist(struct record * rec,struct evlist * evlist)838*4882a593Smuzhiyun static int record__mmap_evlist(struct record *rec,
839*4882a593Smuzhiyun struct evlist *evlist)
840*4882a593Smuzhiyun {
841*4882a593Smuzhiyun struct record_opts *opts = &rec->opts;
842*4882a593Smuzhiyun bool auxtrace_overwrite = opts->auxtrace_snapshot_mode ||
843*4882a593Smuzhiyun opts->auxtrace_sample_mode;
844*4882a593Smuzhiyun char msg[512];
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun if (opts->affinity != PERF_AFFINITY_SYS)
847*4882a593Smuzhiyun cpu__setup_cpunode_map();
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun if (evlist__mmap_ex(evlist, opts->mmap_pages,
850*4882a593Smuzhiyun opts->auxtrace_mmap_pages,
851*4882a593Smuzhiyun auxtrace_overwrite,
852*4882a593Smuzhiyun opts->nr_cblocks, opts->affinity,
853*4882a593Smuzhiyun opts->mmap_flush, opts->comp_level) < 0) {
854*4882a593Smuzhiyun if (errno == EPERM) {
855*4882a593Smuzhiyun pr_err("Permission error mapping pages.\n"
856*4882a593Smuzhiyun "Consider increasing "
857*4882a593Smuzhiyun "/proc/sys/kernel/perf_event_mlock_kb,\n"
858*4882a593Smuzhiyun "or try again with a smaller value of -m/--mmap_pages.\n"
859*4882a593Smuzhiyun "(current value: %u,%u)\n",
860*4882a593Smuzhiyun opts->mmap_pages, opts->auxtrace_mmap_pages);
861*4882a593Smuzhiyun return -errno;
862*4882a593Smuzhiyun } else {
863*4882a593Smuzhiyun pr_err("failed to mmap with %d (%s)\n", errno,
864*4882a593Smuzhiyun str_error_r(errno, msg, sizeof(msg)));
865*4882a593Smuzhiyun if (errno)
866*4882a593Smuzhiyun return -errno;
867*4882a593Smuzhiyun else
868*4882a593Smuzhiyun return -EINVAL;
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun return 0;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun
record__mmap(struct record * rec)874*4882a593Smuzhiyun static int record__mmap(struct record *rec)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun return record__mmap_evlist(rec, rec->evlist);
877*4882a593Smuzhiyun }
878*4882a593Smuzhiyun
record__open(struct record * rec)879*4882a593Smuzhiyun static int record__open(struct record *rec)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun char msg[BUFSIZ];
882*4882a593Smuzhiyun struct evsel *pos;
883*4882a593Smuzhiyun struct evlist *evlist = rec->evlist;
884*4882a593Smuzhiyun struct perf_session *session = rec->session;
885*4882a593Smuzhiyun struct record_opts *opts = &rec->opts;
886*4882a593Smuzhiyun int rc = 0;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun /*
889*4882a593Smuzhiyun * For initial_delay or system wide, we need to add a dummy event so
890*4882a593Smuzhiyun * that we can track PERF_RECORD_MMAP to cover the delay of waiting or
891*4882a593Smuzhiyun * event synthesis.
892*4882a593Smuzhiyun */
893*4882a593Smuzhiyun if (opts->initial_delay || target__has_cpu(&opts->target)) {
894*4882a593Smuzhiyun pos = perf_evlist__get_tracking_event(evlist);
895*4882a593Smuzhiyun if (!evsel__is_dummy_event(pos)) {
896*4882a593Smuzhiyun /* Set up dummy event. */
897*4882a593Smuzhiyun if (evlist__add_dummy(evlist))
898*4882a593Smuzhiyun return -ENOMEM;
899*4882a593Smuzhiyun pos = evlist__last(evlist);
900*4882a593Smuzhiyun perf_evlist__set_tracking_event(evlist, pos);
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun /*
904*4882a593Smuzhiyun * Enable the dummy event when the process is forked for
905*4882a593Smuzhiyun * initial_delay, immediately for system wide.
906*4882a593Smuzhiyun */
907*4882a593Smuzhiyun if (opts->initial_delay && !pos->immediate)
908*4882a593Smuzhiyun pos->core.attr.enable_on_exec = 1;
909*4882a593Smuzhiyun else
910*4882a593Smuzhiyun pos->immediate = 1;
911*4882a593Smuzhiyun }
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun perf_evlist__config(evlist, opts, &callchain_param);
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun evlist__for_each_entry(evlist, pos) {
916*4882a593Smuzhiyun try_again:
917*4882a593Smuzhiyun if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
918*4882a593Smuzhiyun if (evsel__fallback(pos, errno, msg, sizeof(msg))) {
919*4882a593Smuzhiyun if (verbose > 0)
920*4882a593Smuzhiyun ui__warning("%s\n", msg);
921*4882a593Smuzhiyun goto try_again;
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun if ((errno == EINVAL || errno == EBADF) &&
924*4882a593Smuzhiyun pos->leader != pos &&
925*4882a593Smuzhiyun pos->weak_group) {
926*4882a593Smuzhiyun pos = perf_evlist__reset_weak_group(evlist, pos, true);
927*4882a593Smuzhiyun goto try_again;
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun rc = -errno;
930*4882a593Smuzhiyun evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg));
931*4882a593Smuzhiyun ui__error("%s\n", msg);
932*4882a593Smuzhiyun goto out;
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun pos->supported = true;
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun if (symbol_conf.kptr_restrict && !perf_evlist__exclude_kernel(evlist)) {
939*4882a593Smuzhiyun pr_warning(
940*4882a593Smuzhiyun "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
941*4882a593Smuzhiyun "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
942*4882a593Smuzhiyun "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
943*4882a593Smuzhiyun "file is not found in the buildid cache or in the vmlinux path.\n\n"
944*4882a593Smuzhiyun "Samples in kernel modules won't be resolved at all.\n\n"
945*4882a593Smuzhiyun "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
946*4882a593Smuzhiyun "even with a suitable vmlinux or kallsyms file.\n\n");
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun
949*4882a593Smuzhiyun if (perf_evlist__apply_filters(evlist, &pos)) {
950*4882a593Smuzhiyun pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
951*4882a593Smuzhiyun pos->filter, evsel__name(pos), errno,
952*4882a593Smuzhiyun str_error_r(errno, msg, sizeof(msg)));
953*4882a593Smuzhiyun rc = -1;
954*4882a593Smuzhiyun goto out;
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun rc = record__mmap(rec);
958*4882a593Smuzhiyun if (rc)
959*4882a593Smuzhiyun goto out;
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun session->evlist = evlist;
962*4882a593Smuzhiyun perf_session__set_id_hdr_size(session);
963*4882a593Smuzhiyun out:
964*4882a593Smuzhiyun return rc;
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun
process_sample_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)967*4882a593Smuzhiyun static int process_sample_event(struct perf_tool *tool,
968*4882a593Smuzhiyun union perf_event *event,
969*4882a593Smuzhiyun struct perf_sample *sample,
970*4882a593Smuzhiyun struct evsel *evsel,
971*4882a593Smuzhiyun struct machine *machine)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun struct record *rec = container_of(tool, struct record, tool);
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun if (rec->evlist->first_sample_time == 0)
976*4882a593Smuzhiyun rec->evlist->first_sample_time = sample->time;
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun rec->evlist->last_sample_time = sample->time;
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun if (rec->buildid_all)
981*4882a593Smuzhiyun return 0;
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun rec->samples++;
984*4882a593Smuzhiyun return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun
process_buildids(struct record * rec)987*4882a593Smuzhiyun static int process_buildids(struct record *rec)
988*4882a593Smuzhiyun {
989*4882a593Smuzhiyun struct perf_session *session = rec->session;
990*4882a593Smuzhiyun
991*4882a593Smuzhiyun if (perf_data__size(&rec->data) == 0)
992*4882a593Smuzhiyun return 0;
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun /*
995*4882a593Smuzhiyun * During this process, it'll load kernel map and replace the
996*4882a593Smuzhiyun * dso->long_name to a real pathname it found. In this case
997*4882a593Smuzhiyun * we prefer the vmlinux path like
998*4882a593Smuzhiyun * /lib/modules/3.16.4/build/vmlinux
999*4882a593Smuzhiyun *
1000*4882a593Smuzhiyun * rather than build-id path (in debug directory).
1001*4882a593Smuzhiyun * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
1002*4882a593Smuzhiyun */
1003*4882a593Smuzhiyun symbol_conf.ignore_vmlinux_buildid = true;
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun /*
1006*4882a593Smuzhiyun * If --buildid-all is given, it marks all DSO regardless of hits,
1007*4882a593Smuzhiyun * so no need to process samples. But if timestamp_boundary is enabled,
1008*4882a593Smuzhiyun * it still needs to walk on all samples to get the timestamps of
1009*4882a593Smuzhiyun * first/last samples.
1010*4882a593Smuzhiyun */
1011*4882a593Smuzhiyun if (rec->buildid_all && !rec->timestamp_boundary)
1012*4882a593Smuzhiyun rec->tool.sample = NULL;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun return perf_session__process_events(session);
1015*4882a593Smuzhiyun }
1016*4882a593Smuzhiyun
perf_event__synthesize_guest_os(struct machine * machine,void * data)1017*4882a593Smuzhiyun static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
1018*4882a593Smuzhiyun {
1019*4882a593Smuzhiyun int err;
1020*4882a593Smuzhiyun struct perf_tool *tool = data;
1021*4882a593Smuzhiyun /*
1022*4882a593Smuzhiyun *As for guest kernel when processing subcommand record&report,
1023*4882a593Smuzhiyun *we arrange module mmap prior to guest kernel mmap and trigger
1024*4882a593Smuzhiyun *a preload dso because default guest module symbols are loaded
1025*4882a593Smuzhiyun *from guest kallsyms instead of /lib/modules/XXX/XXX. This
1026*4882a593Smuzhiyun *method is used to avoid symbol missing when the first addr is
1027*4882a593Smuzhiyun *in module instead of in guest kernel.
1028*4882a593Smuzhiyun */
1029*4882a593Smuzhiyun err = perf_event__synthesize_modules(tool, process_synthesized_event,
1030*4882a593Smuzhiyun machine);
1031*4882a593Smuzhiyun if (err < 0)
1032*4882a593Smuzhiyun pr_err("Couldn't record guest kernel [%d]'s reference"
1033*4882a593Smuzhiyun " relocation symbol.\n", machine->pid);
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun /*
1036*4882a593Smuzhiyun * We use _stext for guest kernel because guest kernel's /proc/kallsyms
1037*4882a593Smuzhiyun * have no _text sometimes.
1038*4882a593Smuzhiyun */
1039*4882a593Smuzhiyun err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
1040*4882a593Smuzhiyun machine);
1041*4882a593Smuzhiyun if (err < 0)
1042*4882a593Smuzhiyun pr_err("Couldn't record guest kernel [%d]'s reference"
1043*4882a593Smuzhiyun " relocation symbol.\n", machine->pid);
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun static struct perf_event_header finished_round_event = {
1047*4882a593Smuzhiyun .size = sizeof(struct perf_event_header),
1048*4882a593Smuzhiyun .type = PERF_RECORD_FINISHED_ROUND,
1049*4882a593Smuzhiyun };
1050*4882a593Smuzhiyun
record__adjust_affinity(struct record * rec,struct mmap * map)1051*4882a593Smuzhiyun static void record__adjust_affinity(struct record *rec, struct mmap *map)
1052*4882a593Smuzhiyun {
1053*4882a593Smuzhiyun if (rec->opts.affinity != PERF_AFFINITY_SYS &&
1054*4882a593Smuzhiyun !bitmap_equal(rec->affinity_mask.bits, map->affinity_mask.bits,
1055*4882a593Smuzhiyun rec->affinity_mask.nbits)) {
1056*4882a593Smuzhiyun bitmap_zero(rec->affinity_mask.bits, rec->affinity_mask.nbits);
1057*4882a593Smuzhiyun bitmap_or(rec->affinity_mask.bits, rec->affinity_mask.bits,
1058*4882a593Smuzhiyun map->affinity_mask.bits, rec->affinity_mask.nbits);
1059*4882a593Smuzhiyun sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&rec->affinity_mask),
1060*4882a593Smuzhiyun (cpu_set_t *)rec->affinity_mask.bits);
1061*4882a593Smuzhiyun if (verbose == 2)
1062*4882a593Smuzhiyun mmap_cpu_mask__scnprintf(&rec->affinity_mask, "thread");
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun
process_comp_header(void * record,size_t increment)1066*4882a593Smuzhiyun static size_t process_comp_header(void *record, size_t increment)
1067*4882a593Smuzhiyun {
1068*4882a593Smuzhiyun struct perf_record_compressed *event = record;
1069*4882a593Smuzhiyun size_t size = sizeof(*event);
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun if (increment) {
1072*4882a593Smuzhiyun event->header.size += increment;
1073*4882a593Smuzhiyun return increment;
1074*4882a593Smuzhiyun }
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun event->header.type = PERF_RECORD_COMPRESSED;
1077*4882a593Smuzhiyun event->header.size = size;
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun return size;
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun
zstd_compress(struct perf_session * session,void * dst,size_t dst_size,void * src,size_t src_size)1082*4882a593Smuzhiyun static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size,
1083*4882a593Smuzhiyun void *src, size_t src_size)
1084*4882a593Smuzhiyun {
1085*4882a593Smuzhiyun size_t compressed;
1086*4882a593Smuzhiyun size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1;
1087*4882a593Smuzhiyun
1088*4882a593Smuzhiyun compressed = zstd_compress_stream_to_records(&session->zstd_data, dst, dst_size, src, src_size,
1089*4882a593Smuzhiyun max_record_size, process_comp_header);
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun session->bytes_transferred += src_size;
1092*4882a593Smuzhiyun session->bytes_compressed += compressed;
1093*4882a593Smuzhiyun
1094*4882a593Smuzhiyun return compressed;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun
record__mmap_read_evlist(struct record * rec,struct evlist * evlist,bool overwrite,bool synch)1097*4882a593Smuzhiyun static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist,
1098*4882a593Smuzhiyun bool overwrite, bool synch)
1099*4882a593Smuzhiyun {
1100*4882a593Smuzhiyun u64 bytes_written = rec->bytes_written;
1101*4882a593Smuzhiyun int i;
1102*4882a593Smuzhiyun int rc = 0;
1103*4882a593Smuzhiyun struct mmap *maps;
1104*4882a593Smuzhiyun int trace_fd = rec->data.file.fd;
1105*4882a593Smuzhiyun off_t off = 0;
1106*4882a593Smuzhiyun
1107*4882a593Smuzhiyun if (!evlist)
1108*4882a593Smuzhiyun return 0;
1109*4882a593Smuzhiyun
1110*4882a593Smuzhiyun maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
1111*4882a593Smuzhiyun if (!maps)
1112*4882a593Smuzhiyun return 0;
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
1115*4882a593Smuzhiyun return 0;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun if (record__aio_enabled(rec))
1118*4882a593Smuzhiyun off = record__aio_get_pos(trace_fd);
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun for (i = 0; i < evlist->core.nr_mmaps; i++) {
1121*4882a593Smuzhiyun u64 flush = 0;
1122*4882a593Smuzhiyun struct mmap *map = &maps[i];
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun if (map->core.base) {
1125*4882a593Smuzhiyun record__adjust_affinity(rec, map);
1126*4882a593Smuzhiyun if (synch) {
1127*4882a593Smuzhiyun flush = map->core.flush;
1128*4882a593Smuzhiyun map->core.flush = 1;
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun if (!record__aio_enabled(rec)) {
1131*4882a593Smuzhiyun if (perf_mmap__push(map, rec, record__pushfn) < 0) {
1132*4882a593Smuzhiyun if (synch)
1133*4882a593Smuzhiyun map->core.flush = flush;
1134*4882a593Smuzhiyun rc = -1;
1135*4882a593Smuzhiyun goto out;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun } else {
1138*4882a593Smuzhiyun if (record__aio_push(rec, map, &off) < 0) {
1139*4882a593Smuzhiyun record__aio_set_pos(trace_fd, off);
1140*4882a593Smuzhiyun if (synch)
1141*4882a593Smuzhiyun map->core.flush = flush;
1142*4882a593Smuzhiyun rc = -1;
1143*4882a593Smuzhiyun goto out;
1144*4882a593Smuzhiyun }
1145*4882a593Smuzhiyun }
1146*4882a593Smuzhiyun if (synch)
1147*4882a593Smuzhiyun map->core.flush = flush;
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
1151*4882a593Smuzhiyun !rec->opts.auxtrace_sample_mode &&
1152*4882a593Smuzhiyun record__auxtrace_mmap_read(rec, map) != 0) {
1153*4882a593Smuzhiyun rc = -1;
1154*4882a593Smuzhiyun goto out;
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun if (record__aio_enabled(rec))
1159*4882a593Smuzhiyun record__aio_set_pos(trace_fd, off);
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun /*
1162*4882a593Smuzhiyun * Mark the round finished in case we wrote
1163*4882a593Smuzhiyun * at least one event.
1164*4882a593Smuzhiyun */
1165*4882a593Smuzhiyun if (bytes_written != rec->bytes_written)
1166*4882a593Smuzhiyun rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event));
1167*4882a593Smuzhiyun
1168*4882a593Smuzhiyun if (overwrite)
1169*4882a593Smuzhiyun perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
1170*4882a593Smuzhiyun out:
1171*4882a593Smuzhiyun return rc;
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun
record__mmap_read_all(struct record * rec,bool synch)1174*4882a593Smuzhiyun static int record__mmap_read_all(struct record *rec, bool synch)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun int err;
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun err = record__mmap_read_evlist(rec, rec->evlist, false, synch);
1179*4882a593Smuzhiyun if (err)
1180*4882a593Smuzhiyun return err;
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun return record__mmap_read_evlist(rec, rec->evlist, true, synch);
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun
record__init_features(struct record * rec)1185*4882a593Smuzhiyun static void record__init_features(struct record *rec)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun struct perf_session *session = rec->session;
1188*4882a593Smuzhiyun int feat;
1189*4882a593Smuzhiyun
1190*4882a593Smuzhiyun for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1191*4882a593Smuzhiyun perf_header__set_feat(&session->header, feat);
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun if (rec->no_buildid)
1194*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun if (!have_tracepoints(&rec->evlist->core.entries))
1197*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun if (!rec->opts.branch_stack)
1200*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun if (!rec->opts.full_auxtrace)
1203*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns))
1206*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_CLOCKID);
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun if (!rec->opts.use_clockid)
1209*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA);
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
1212*4882a593Smuzhiyun if (!record__comp_enabled(rec))
1213*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_COMPRESSED);
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_STAT);
1216*4882a593Smuzhiyun }
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun static void
record__finish_output(struct record * rec)1219*4882a593Smuzhiyun record__finish_output(struct record *rec)
1220*4882a593Smuzhiyun {
1221*4882a593Smuzhiyun struct perf_data *data = &rec->data;
1222*4882a593Smuzhiyun int fd = perf_data__fd(data);
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun if (data->is_pipe)
1225*4882a593Smuzhiyun return;
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun rec->session->header.data_size += rec->bytes_written;
1228*4882a593Smuzhiyun data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR);
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun if (!rec->no_buildid) {
1231*4882a593Smuzhiyun process_buildids(rec);
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun if (rec->buildid_all)
1234*4882a593Smuzhiyun dsos__hit_all(rec->session);
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun perf_session__write_header(rec->session, rec->evlist, fd, true);
1237*4882a593Smuzhiyun
1238*4882a593Smuzhiyun return;
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun
record__synthesize_workload(struct record * rec,bool tail)1241*4882a593Smuzhiyun static int record__synthesize_workload(struct record *rec, bool tail)
1242*4882a593Smuzhiyun {
1243*4882a593Smuzhiyun int err;
1244*4882a593Smuzhiyun struct perf_thread_map *thread_map;
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun if (rec->opts.tail_synthesize != tail)
1247*4882a593Smuzhiyun return 0;
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun thread_map = thread_map__new_by_tid(rec->evlist->workload.pid);
1250*4882a593Smuzhiyun if (thread_map == NULL)
1251*4882a593Smuzhiyun return -1;
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun err = perf_event__synthesize_thread_map(&rec->tool, thread_map,
1254*4882a593Smuzhiyun process_synthesized_event,
1255*4882a593Smuzhiyun &rec->session->machines.host,
1256*4882a593Smuzhiyun rec->opts.sample_address);
1257*4882a593Smuzhiyun perf_thread_map__put(thread_map);
1258*4882a593Smuzhiyun return err;
1259*4882a593Smuzhiyun }
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun static int record__synthesize(struct record *rec, bool tail);
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun static int
record__switch_output(struct record * rec,bool at_exit)1264*4882a593Smuzhiyun record__switch_output(struct record *rec, bool at_exit)
1265*4882a593Smuzhiyun {
1266*4882a593Smuzhiyun struct perf_data *data = &rec->data;
1267*4882a593Smuzhiyun int fd, err;
1268*4882a593Smuzhiyun char *new_filename;
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun /* Same Size: "2015122520103046"*/
1271*4882a593Smuzhiyun char timestamp[] = "InvalidTimestamp";
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun record__aio_mmap_read_sync(rec);
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun record__synthesize(rec, true);
1276*4882a593Smuzhiyun if (target__none(&rec->opts.target))
1277*4882a593Smuzhiyun record__synthesize_workload(rec, true);
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun rec->samples = 0;
1280*4882a593Smuzhiyun record__finish_output(rec);
1281*4882a593Smuzhiyun err = fetch_current_timestamp(timestamp, sizeof(timestamp));
1282*4882a593Smuzhiyun if (err) {
1283*4882a593Smuzhiyun pr_err("Failed to get current timestamp\n");
1284*4882a593Smuzhiyun return -EINVAL;
1285*4882a593Smuzhiyun }
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun fd = perf_data__switch(data, timestamp,
1288*4882a593Smuzhiyun rec->session->header.data_offset,
1289*4882a593Smuzhiyun at_exit, &new_filename);
1290*4882a593Smuzhiyun if (fd >= 0 && !at_exit) {
1291*4882a593Smuzhiyun rec->bytes_written = 0;
1292*4882a593Smuzhiyun rec->session->header.data_size = 0;
1293*4882a593Smuzhiyun }
1294*4882a593Smuzhiyun
1295*4882a593Smuzhiyun if (!quiet)
1296*4882a593Smuzhiyun fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
1297*4882a593Smuzhiyun data->path, timestamp);
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun if (rec->switch_output.num_files) {
1300*4882a593Smuzhiyun int n = rec->switch_output.cur_file + 1;
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun if (n >= rec->switch_output.num_files)
1303*4882a593Smuzhiyun n = 0;
1304*4882a593Smuzhiyun rec->switch_output.cur_file = n;
1305*4882a593Smuzhiyun if (rec->switch_output.filenames[n]) {
1306*4882a593Smuzhiyun remove(rec->switch_output.filenames[n]);
1307*4882a593Smuzhiyun zfree(&rec->switch_output.filenames[n]);
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun rec->switch_output.filenames[n] = new_filename;
1310*4882a593Smuzhiyun } else {
1311*4882a593Smuzhiyun free(new_filename);
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun /* Output tracking events */
1315*4882a593Smuzhiyun if (!at_exit) {
1316*4882a593Smuzhiyun record__synthesize(rec, false);
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun /*
1319*4882a593Smuzhiyun * In 'perf record --switch-output' without -a,
1320*4882a593Smuzhiyun * record__synthesize() in record__switch_output() won't
1321*4882a593Smuzhiyun * generate tracking events because there's no thread_map
1322*4882a593Smuzhiyun * in evlist. Which causes newly created perf.data doesn't
1323*4882a593Smuzhiyun * contain map and comm information.
1324*4882a593Smuzhiyun * Create a fake thread_map and directly call
1325*4882a593Smuzhiyun * perf_event__synthesize_thread_map() for those events.
1326*4882a593Smuzhiyun */
1327*4882a593Smuzhiyun if (target__none(&rec->opts.target))
1328*4882a593Smuzhiyun record__synthesize_workload(rec, false);
1329*4882a593Smuzhiyun }
1330*4882a593Smuzhiyun return fd;
1331*4882a593Smuzhiyun }
1332*4882a593Smuzhiyun
1333*4882a593Smuzhiyun static volatile int workload_exec_errno;
1334*4882a593Smuzhiyun
1335*4882a593Smuzhiyun /*
1336*4882a593Smuzhiyun * perf_evlist__prepare_workload will send a SIGUSR1
1337*4882a593Smuzhiyun * if the fork fails, since we asked by setting its
1338*4882a593Smuzhiyun * want_signal to true.
1339*4882a593Smuzhiyun */
workload_exec_failed_signal(int signo __maybe_unused,siginfo_t * info,void * ucontext __maybe_unused)1340*4882a593Smuzhiyun static void workload_exec_failed_signal(int signo __maybe_unused,
1341*4882a593Smuzhiyun siginfo_t *info,
1342*4882a593Smuzhiyun void *ucontext __maybe_unused)
1343*4882a593Smuzhiyun {
1344*4882a593Smuzhiyun workload_exec_errno = info->si_value.sival_int;
1345*4882a593Smuzhiyun done = 1;
1346*4882a593Smuzhiyun child_finished = 1;
1347*4882a593Smuzhiyun }
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun static void snapshot_sig_handler(int sig);
1350*4882a593Smuzhiyun static void alarm_sig_handler(int sig);
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun static const struct perf_event_mmap_page *
perf_evlist__pick_pc(struct evlist * evlist)1353*4882a593Smuzhiyun perf_evlist__pick_pc(struct evlist *evlist)
1354*4882a593Smuzhiyun {
1355*4882a593Smuzhiyun if (evlist) {
1356*4882a593Smuzhiyun if (evlist->mmap && evlist->mmap[0].core.base)
1357*4882a593Smuzhiyun return evlist->mmap[0].core.base;
1358*4882a593Smuzhiyun if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base)
1359*4882a593Smuzhiyun return evlist->overwrite_mmap[0].core.base;
1360*4882a593Smuzhiyun }
1361*4882a593Smuzhiyun return NULL;
1362*4882a593Smuzhiyun }
1363*4882a593Smuzhiyun
record__pick_pc(struct record * rec)1364*4882a593Smuzhiyun static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
1365*4882a593Smuzhiyun {
1366*4882a593Smuzhiyun const struct perf_event_mmap_page *pc;
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun pc = perf_evlist__pick_pc(rec->evlist);
1369*4882a593Smuzhiyun if (pc)
1370*4882a593Smuzhiyun return pc;
1371*4882a593Smuzhiyun return NULL;
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun
record__synthesize(struct record * rec,bool tail)1374*4882a593Smuzhiyun static int record__synthesize(struct record *rec, bool tail)
1375*4882a593Smuzhiyun {
1376*4882a593Smuzhiyun struct perf_session *session = rec->session;
1377*4882a593Smuzhiyun struct machine *machine = &session->machines.host;
1378*4882a593Smuzhiyun struct perf_data *data = &rec->data;
1379*4882a593Smuzhiyun struct record_opts *opts = &rec->opts;
1380*4882a593Smuzhiyun struct perf_tool *tool = &rec->tool;
1381*4882a593Smuzhiyun int fd = perf_data__fd(data);
1382*4882a593Smuzhiyun int err = 0;
1383*4882a593Smuzhiyun event_op f = process_synthesized_event;
1384*4882a593Smuzhiyun
1385*4882a593Smuzhiyun if (rec->opts.tail_synthesize != tail)
1386*4882a593Smuzhiyun return 0;
1387*4882a593Smuzhiyun
1388*4882a593Smuzhiyun if (data->is_pipe) {
1389*4882a593Smuzhiyun /*
1390*4882a593Smuzhiyun * We need to synthesize events first, because some
1391*4882a593Smuzhiyun * features works on top of them (on report side).
1392*4882a593Smuzhiyun */
1393*4882a593Smuzhiyun err = perf_event__synthesize_attrs(tool, rec->evlist,
1394*4882a593Smuzhiyun process_synthesized_event);
1395*4882a593Smuzhiyun if (err < 0) {
1396*4882a593Smuzhiyun pr_err("Couldn't synthesize attrs.\n");
1397*4882a593Smuzhiyun goto out;
1398*4882a593Smuzhiyun }
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun err = perf_event__synthesize_features(tool, session, rec->evlist,
1401*4882a593Smuzhiyun process_synthesized_event);
1402*4882a593Smuzhiyun if (err < 0) {
1403*4882a593Smuzhiyun pr_err("Couldn't synthesize features.\n");
1404*4882a593Smuzhiyun return err;
1405*4882a593Smuzhiyun }
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun if (have_tracepoints(&rec->evlist->core.entries)) {
1408*4882a593Smuzhiyun /*
1409*4882a593Smuzhiyun * FIXME err <= 0 here actually means that
1410*4882a593Smuzhiyun * there were no tracepoints so its not really
1411*4882a593Smuzhiyun * an error, just that we don't need to
1412*4882a593Smuzhiyun * synthesize anything. We really have to
1413*4882a593Smuzhiyun * return this more properly and also
1414*4882a593Smuzhiyun * propagate errors that now are calling die()
1415*4882a593Smuzhiyun */
1416*4882a593Smuzhiyun err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist,
1417*4882a593Smuzhiyun process_synthesized_event);
1418*4882a593Smuzhiyun if (err <= 0) {
1419*4882a593Smuzhiyun pr_err("Couldn't record tracing data.\n");
1420*4882a593Smuzhiyun goto out;
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun rec->bytes_written += err;
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun }
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
1427*4882a593Smuzhiyun process_synthesized_event, machine);
1428*4882a593Smuzhiyun if (err)
1429*4882a593Smuzhiyun goto out;
1430*4882a593Smuzhiyun
1431*4882a593Smuzhiyun /* Synthesize id_index before auxtrace_info */
1432*4882a593Smuzhiyun if (rec->opts.auxtrace_sample_mode) {
1433*4882a593Smuzhiyun err = perf_event__synthesize_id_index(tool,
1434*4882a593Smuzhiyun process_synthesized_event,
1435*4882a593Smuzhiyun session->evlist, machine);
1436*4882a593Smuzhiyun if (err)
1437*4882a593Smuzhiyun goto out;
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun if (rec->opts.full_auxtrace) {
1441*4882a593Smuzhiyun err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
1442*4882a593Smuzhiyun session, process_synthesized_event);
1443*4882a593Smuzhiyun if (err)
1444*4882a593Smuzhiyun goto out;
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun if (!perf_evlist__exclude_kernel(rec->evlist)) {
1448*4882a593Smuzhiyun err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
1449*4882a593Smuzhiyun machine);
1450*4882a593Smuzhiyun WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
1451*4882a593Smuzhiyun "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1452*4882a593Smuzhiyun "Check /proc/kallsyms permission or run as root.\n");
1453*4882a593Smuzhiyun
1454*4882a593Smuzhiyun err = perf_event__synthesize_modules(tool, process_synthesized_event,
1455*4882a593Smuzhiyun machine);
1456*4882a593Smuzhiyun WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
1457*4882a593Smuzhiyun "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
1458*4882a593Smuzhiyun "Check /proc/modules permission or run as root.\n");
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun if (perf_guest) {
1462*4882a593Smuzhiyun machines__process_guests(&session->machines,
1463*4882a593Smuzhiyun perf_event__synthesize_guest_os, tool);
1464*4882a593Smuzhiyun }
1465*4882a593Smuzhiyun
1466*4882a593Smuzhiyun err = perf_event__synthesize_extra_attr(&rec->tool,
1467*4882a593Smuzhiyun rec->evlist,
1468*4882a593Smuzhiyun process_synthesized_event,
1469*4882a593Smuzhiyun data->is_pipe);
1470*4882a593Smuzhiyun if (err)
1471*4882a593Smuzhiyun goto out;
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads,
1474*4882a593Smuzhiyun process_synthesized_event,
1475*4882a593Smuzhiyun NULL);
1476*4882a593Smuzhiyun if (err < 0) {
1477*4882a593Smuzhiyun pr_err("Couldn't synthesize thread map.\n");
1478*4882a593Smuzhiyun return err;
1479*4882a593Smuzhiyun }
1480*4882a593Smuzhiyun
1481*4882a593Smuzhiyun err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.cpus,
1482*4882a593Smuzhiyun process_synthesized_event, NULL);
1483*4882a593Smuzhiyun if (err < 0) {
1484*4882a593Smuzhiyun pr_err("Couldn't synthesize cpu map.\n");
1485*4882a593Smuzhiyun return err;
1486*4882a593Smuzhiyun }
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun err = perf_event__synthesize_bpf_events(session, process_synthesized_event,
1489*4882a593Smuzhiyun machine, opts);
1490*4882a593Smuzhiyun if (err < 0)
1491*4882a593Smuzhiyun pr_warning("Couldn't synthesize bpf events.\n");
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun err = perf_event__synthesize_cgroups(tool, process_synthesized_event,
1494*4882a593Smuzhiyun machine);
1495*4882a593Smuzhiyun if (err < 0)
1496*4882a593Smuzhiyun pr_warning("Couldn't synthesize cgroup events.\n");
1497*4882a593Smuzhiyun
1498*4882a593Smuzhiyun if (rec->opts.nr_threads_synthesize > 1) {
1499*4882a593Smuzhiyun perf_set_multithreaded();
1500*4882a593Smuzhiyun f = process_locked_synthesized_event;
1501*4882a593Smuzhiyun }
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads,
1504*4882a593Smuzhiyun f, opts->sample_address,
1505*4882a593Smuzhiyun rec->opts.nr_threads_synthesize);
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun if (rec->opts.nr_threads_synthesize > 1)
1508*4882a593Smuzhiyun perf_set_singlethreaded();
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun out:
1511*4882a593Smuzhiyun return err;
1512*4882a593Smuzhiyun }
1513*4882a593Smuzhiyun
record__process_signal_event(union perf_event * event __maybe_unused,void * data)1514*4882a593Smuzhiyun static int record__process_signal_event(union perf_event *event __maybe_unused, void *data)
1515*4882a593Smuzhiyun {
1516*4882a593Smuzhiyun struct record *rec = data;
1517*4882a593Smuzhiyun pthread_kill(rec->thread_id, SIGUSR2);
1518*4882a593Smuzhiyun return 0;
1519*4882a593Smuzhiyun }
1520*4882a593Smuzhiyun
record__setup_sb_evlist(struct record * rec)1521*4882a593Smuzhiyun static int record__setup_sb_evlist(struct record *rec)
1522*4882a593Smuzhiyun {
1523*4882a593Smuzhiyun struct record_opts *opts = &rec->opts;
1524*4882a593Smuzhiyun
1525*4882a593Smuzhiyun if (rec->sb_evlist != NULL) {
1526*4882a593Smuzhiyun /*
1527*4882a593Smuzhiyun * We get here if --switch-output-event populated the
1528*4882a593Smuzhiyun * sb_evlist, so associate a callback that will send a SIGUSR2
1529*4882a593Smuzhiyun * to the main thread.
1530*4882a593Smuzhiyun */
1531*4882a593Smuzhiyun evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec);
1532*4882a593Smuzhiyun rec->thread_id = pthread_self();
1533*4882a593Smuzhiyun }
1534*4882a593Smuzhiyun #ifdef HAVE_LIBBPF_SUPPORT
1535*4882a593Smuzhiyun if (!opts->no_bpf_event) {
1536*4882a593Smuzhiyun if (rec->sb_evlist == NULL) {
1537*4882a593Smuzhiyun rec->sb_evlist = evlist__new();
1538*4882a593Smuzhiyun
1539*4882a593Smuzhiyun if (rec->sb_evlist == NULL) {
1540*4882a593Smuzhiyun pr_err("Couldn't create side band evlist.\n.");
1541*4882a593Smuzhiyun return -1;
1542*4882a593Smuzhiyun }
1543*4882a593Smuzhiyun }
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) {
1546*4882a593Smuzhiyun pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
1547*4882a593Smuzhiyun return -1;
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun }
1550*4882a593Smuzhiyun #endif
1551*4882a593Smuzhiyun if (perf_evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) {
1552*4882a593Smuzhiyun pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
1553*4882a593Smuzhiyun opts->no_bpf_event = true;
1554*4882a593Smuzhiyun }
1555*4882a593Smuzhiyun
1556*4882a593Smuzhiyun return 0;
1557*4882a593Smuzhiyun }
1558*4882a593Smuzhiyun
record__init_clock(struct record * rec)1559*4882a593Smuzhiyun static int record__init_clock(struct record *rec)
1560*4882a593Smuzhiyun {
1561*4882a593Smuzhiyun struct perf_session *session = rec->session;
1562*4882a593Smuzhiyun struct timespec ref_clockid;
1563*4882a593Smuzhiyun struct timeval ref_tod;
1564*4882a593Smuzhiyun u64 ref;
1565*4882a593Smuzhiyun
1566*4882a593Smuzhiyun if (!rec->opts.use_clockid)
1567*4882a593Smuzhiyun return 0;
1568*4882a593Smuzhiyun
1569*4882a593Smuzhiyun if (rec->opts.use_clockid && rec->opts.clockid_res_ns)
1570*4882a593Smuzhiyun session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns;
1571*4882a593Smuzhiyun
1572*4882a593Smuzhiyun session->header.env.clock.clockid = rec->opts.clockid;
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun if (gettimeofday(&ref_tod, NULL) != 0) {
1575*4882a593Smuzhiyun pr_err("gettimeofday failed, cannot set reference time.\n");
1576*4882a593Smuzhiyun return -1;
1577*4882a593Smuzhiyun }
1578*4882a593Smuzhiyun
1579*4882a593Smuzhiyun if (clock_gettime(rec->opts.clockid, &ref_clockid)) {
1580*4882a593Smuzhiyun pr_err("clock_gettime failed, cannot set reference time.\n");
1581*4882a593Smuzhiyun return -1;
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun
1584*4882a593Smuzhiyun ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC +
1585*4882a593Smuzhiyun (u64) ref_tod.tv_usec * NSEC_PER_USEC;
1586*4882a593Smuzhiyun
1587*4882a593Smuzhiyun session->header.env.clock.tod_ns = ref;
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC +
1590*4882a593Smuzhiyun (u64) ref_clockid.tv_nsec;
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun session->header.env.clock.clockid_ns = ref;
1593*4882a593Smuzhiyun return 0;
1594*4882a593Smuzhiyun }
1595*4882a593Smuzhiyun
hit_auxtrace_snapshot_trigger(struct record * rec)1596*4882a593Smuzhiyun static void hit_auxtrace_snapshot_trigger(struct record *rec)
1597*4882a593Smuzhiyun {
1598*4882a593Smuzhiyun if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
1599*4882a593Smuzhiyun trigger_hit(&auxtrace_snapshot_trigger);
1600*4882a593Smuzhiyun auxtrace_record__snapshot_started = 1;
1601*4882a593Smuzhiyun if (auxtrace_record__snapshot_start(rec->itr))
1602*4882a593Smuzhiyun trigger_error(&auxtrace_snapshot_trigger);
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun }
1605*4882a593Smuzhiyun
__cmd_record(struct record * rec,int argc,const char ** argv)1606*4882a593Smuzhiyun static int __cmd_record(struct record *rec, int argc, const char **argv)
1607*4882a593Smuzhiyun {
1608*4882a593Smuzhiyun int err;
1609*4882a593Smuzhiyun int status = 0;
1610*4882a593Smuzhiyun unsigned long waking = 0;
1611*4882a593Smuzhiyun const bool forks = argc > 0;
1612*4882a593Smuzhiyun struct perf_tool *tool = &rec->tool;
1613*4882a593Smuzhiyun struct record_opts *opts = &rec->opts;
1614*4882a593Smuzhiyun struct perf_data *data = &rec->data;
1615*4882a593Smuzhiyun struct perf_session *session;
1616*4882a593Smuzhiyun bool disabled = false, draining = false;
1617*4882a593Smuzhiyun int fd;
1618*4882a593Smuzhiyun float ratio = 0;
1619*4882a593Smuzhiyun enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1620*4882a593Smuzhiyun
1621*4882a593Smuzhiyun atexit(record__sig_exit);
1622*4882a593Smuzhiyun signal(SIGCHLD, sig_handler);
1623*4882a593Smuzhiyun signal(SIGINT, sig_handler);
1624*4882a593Smuzhiyun signal(SIGTERM, sig_handler);
1625*4882a593Smuzhiyun signal(SIGSEGV, sigsegv_handler);
1626*4882a593Smuzhiyun
1627*4882a593Smuzhiyun if (rec->opts.record_namespaces)
1628*4882a593Smuzhiyun tool->namespace_events = true;
1629*4882a593Smuzhiyun
1630*4882a593Smuzhiyun if (rec->opts.record_cgroup) {
1631*4882a593Smuzhiyun #ifdef HAVE_FILE_HANDLE
1632*4882a593Smuzhiyun tool->cgroup_events = true;
1633*4882a593Smuzhiyun #else
1634*4882a593Smuzhiyun pr_err("cgroup tracking is not supported\n");
1635*4882a593Smuzhiyun return -1;
1636*4882a593Smuzhiyun #endif
1637*4882a593Smuzhiyun }
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
1640*4882a593Smuzhiyun signal(SIGUSR2, snapshot_sig_handler);
1641*4882a593Smuzhiyun if (rec->opts.auxtrace_snapshot_mode)
1642*4882a593Smuzhiyun trigger_on(&auxtrace_snapshot_trigger);
1643*4882a593Smuzhiyun if (rec->switch_output.enabled)
1644*4882a593Smuzhiyun trigger_on(&switch_output_trigger);
1645*4882a593Smuzhiyun } else {
1646*4882a593Smuzhiyun signal(SIGUSR2, SIG_IGN);
1647*4882a593Smuzhiyun }
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun session = perf_session__new(data, false, tool);
1650*4882a593Smuzhiyun if (IS_ERR(session)) {
1651*4882a593Smuzhiyun pr_err("Perf session creation failed.\n");
1652*4882a593Smuzhiyun return PTR_ERR(session);
1653*4882a593Smuzhiyun }
1654*4882a593Smuzhiyun
1655*4882a593Smuzhiyun fd = perf_data__fd(data);
1656*4882a593Smuzhiyun rec->session = session;
1657*4882a593Smuzhiyun
1658*4882a593Smuzhiyun if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) {
1659*4882a593Smuzhiyun pr_err("Compression initialization failed.\n");
1660*4882a593Smuzhiyun return -1;
1661*4882a593Smuzhiyun }
1662*4882a593Smuzhiyun #ifdef HAVE_EVENTFD_SUPPORT
1663*4882a593Smuzhiyun done_fd = eventfd(0, EFD_NONBLOCK);
1664*4882a593Smuzhiyun if (done_fd < 0) {
1665*4882a593Smuzhiyun pr_err("Failed to create wakeup eventfd, error: %m\n");
1666*4882a593Smuzhiyun status = -1;
1667*4882a593Smuzhiyun goto out_delete_session;
1668*4882a593Smuzhiyun }
1669*4882a593Smuzhiyun err = evlist__add_wakeup_eventfd(rec->evlist, done_fd);
1670*4882a593Smuzhiyun if (err < 0) {
1671*4882a593Smuzhiyun pr_err("Failed to add wakeup eventfd to poll list\n");
1672*4882a593Smuzhiyun status = err;
1673*4882a593Smuzhiyun goto out_delete_session;
1674*4882a593Smuzhiyun }
1675*4882a593Smuzhiyun #endif // HAVE_EVENTFD_SUPPORT
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun session->header.env.comp_type = PERF_COMP_ZSTD;
1678*4882a593Smuzhiyun session->header.env.comp_level = rec->opts.comp_level;
1679*4882a593Smuzhiyun
1680*4882a593Smuzhiyun if (rec->opts.kcore &&
1681*4882a593Smuzhiyun !record__kcore_readable(&session->machines.host)) {
1682*4882a593Smuzhiyun pr_err("ERROR: kcore is not readable.\n");
1683*4882a593Smuzhiyun return -1;
1684*4882a593Smuzhiyun }
1685*4882a593Smuzhiyun
1686*4882a593Smuzhiyun if (record__init_clock(rec))
1687*4882a593Smuzhiyun return -1;
1688*4882a593Smuzhiyun
1689*4882a593Smuzhiyun record__init_features(rec);
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun if (forks) {
1692*4882a593Smuzhiyun err = perf_evlist__prepare_workload(rec->evlist, &opts->target,
1693*4882a593Smuzhiyun argv, data->is_pipe,
1694*4882a593Smuzhiyun workload_exec_failed_signal);
1695*4882a593Smuzhiyun if (err < 0) {
1696*4882a593Smuzhiyun pr_err("Couldn't run the workload!\n");
1697*4882a593Smuzhiyun status = err;
1698*4882a593Smuzhiyun goto out_delete_session;
1699*4882a593Smuzhiyun }
1700*4882a593Smuzhiyun }
1701*4882a593Smuzhiyun
1702*4882a593Smuzhiyun /*
1703*4882a593Smuzhiyun * If we have just single event and are sending data
1704*4882a593Smuzhiyun * through pipe, we need to force the ids allocation,
1705*4882a593Smuzhiyun * because we synthesize event name through the pipe
1706*4882a593Smuzhiyun * and need the id for that.
1707*4882a593Smuzhiyun */
1708*4882a593Smuzhiyun if (data->is_pipe && rec->evlist->core.nr_entries == 1)
1709*4882a593Smuzhiyun rec->opts.sample_id = true;
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun if (record__open(rec) != 0) {
1712*4882a593Smuzhiyun err = -1;
1713*4882a593Smuzhiyun goto out_child;
1714*4882a593Smuzhiyun }
1715*4882a593Smuzhiyun session->header.env.comp_mmap_len = session->evlist->core.mmap_len;
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun if (rec->opts.kcore) {
1718*4882a593Smuzhiyun err = record__kcore_copy(&session->machines.host, data);
1719*4882a593Smuzhiyun if (err) {
1720*4882a593Smuzhiyun pr_err("ERROR: Failed to copy kcore\n");
1721*4882a593Smuzhiyun goto out_child;
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun
1725*4882a593Smuzhiyun err = bpf__apply_obj_config();
1726*4882a593Smuzhiyun if (err) {
1727*4882a593Smuzhiyun char errbuf[BUFSIZ];
1728*4882a593Smuzhiyun
1729*4882a593Smuzhiyun bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
1730*4882a593Smuzhiyun pr_err("ERROR: Apply config to BPF failed: %s\n",
1731*4882a593Smuzhiyun errbuf);
1732*4882a593Smuzhiyun goto out_child;
1733*4882a593Smuzhiyun }
1734*4882a593Smuzhiyun
1735*4882a593Smuzhiyun /*
1736*4882a593Smuzhiyun * Normally perf_session__new would do this, but it doesn't have the
1737*4882a593Smuzhiyun * evlist.
1738*4882a593Smuzhiyun */
1739*4882a593Smuzhiyun if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) {
1740*4882a593Smuzhiyun pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
1741*4882a593Smuzhiyun rec->tool.ordered_events = false;
1742*4882a593Smuzhiyun }
1743*4882a593Smuzhiyun
1744*4882a593Smuzhiyun if (!rec->evlist->nr_groups)
1745*4882a593Smuzhiyun perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun if (data->is_pipe) {
1748*4882a593Smuzhiyun err = perf_header__write_pipe(fd);
1749*4882a593Smuzhiyun if (err < 0)
1750*4882a593Smuzhiyun goto out_child;
1751*4882a593Smuzhiyun } else {
1752*4882a593Smuzhiyun err = perf_session__write_header(session, rec->evlist, fd, false);
1753*4882a593Smuzhiyun if (err < 0)
1754*4882a593Smuzhiyun goto out_child;
1755*4882a593Smuzhiyun }
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun err = -1;
1758*4882a593Smuzhiyun if (!rec->no_buildid
1759*4882a593Smuzhiyun && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
1760*4882a593Smuzhiyun pr_err("Couldn't generate buildids. "
1761*4882a593Smuzhiyun "Use --no-buildid to profile anyway.\n");
1762*4882a593Smuzhiyun goto out_child;
1763*4882a593Smuzhiyun }
1764*4882a593Smuzhiyun
1765*4882a593Smuzhiyun err = record__setup_sb_evlist(rec);
1766*4882a593Smuzhiyun if (err)
1767*4882a593Smuzhiyun goto out_child;
1768*4882a593Smuzhiyun
1769*4882a593Smuzhiyun err = record__synthesize(rec, false);
1770*4882a593Smuzhiyun if (err < 0)
1771*4882a593Smuzhiyun goto out_child;
1772*4882a593Smuzhiyun
1773*4882a593Smuzhiyun if (rec->realtime_prio) {
1774*4882a593Smuzhiyun struct sched_param param;
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun param.sched_priority = rec->realtime_prio;
1777*4882a593Smuzhiyun if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
1778*4882a593Smuzhiyun pr_err("Could not set realtime priority.\n");
1779*4882a593Smuzhiyun err = -1;
1780*4882a593Smuzhiyun goto out_child;
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun }
1783*4882a593Smuzhiyun
1784*4882a593Smuzhiyun /*
1785*4882a593Smuzhiyun * When perf is starting the traced process, all the events
1786*4882a593Smuzhiyun * (apart from group members) have enable_on_exec=1 set,
1787*4882a593Smuzhiyun * so don't spoil it by prematurely enabling them.
1788*4882a593Smuzhiyun */
1789*4882a593Smuzhiyun if (!target__none(&opts->target) && !opts->initial_delay)
1790*4882a593Smuzhiyun evlist__enable(rec->evlist);
1791*4882a593Smuzhiyun
1792*4882a593Smuzhiyun /*
1793*4882a593Smuzhiyun * Let the child rip
1794*4882a593Smuzhiyun */
1795*4882a593Smuzhiyun if (forks) {
1796*4882a593Smuzhiyun struct machine *machine = &session->machines.host;
1797*4882a593Smuzhiyun union perf_event *event;
1798*4882a593Smuzhiyun pid_t tgid;
1799*4882a593Smuzhiyun
1800*4882a593Smuzhiyun event = malloc(sizeof(event->comm) + machine->id_hdr_size);
1801*4882a593Smuzhiyun if (event == NULL) {
1802*4882a593Smuzhiyun err = -ENOMEM;
1803*4882a593Smuzhiyun goto out_child;
1804*4882a593Smuzhiyun }
1805*4882a593Smuzhiyun
1806*4882a593Smuzhiyun /*
1807*4882a593Smuzhiyun * Some H/W events are generated before COMM event
1808*4882a593Smuzhiyun * which is emitted during exec(), so perf script
1809*4882a593Smuzhiyun * cannot see a correct process name for those events.
1810*4882a593Smuzhiyun * Synthesize COMM event to prevent it.
1811*4882a593Smuzhiyun */
1812*4882a593Smuzhiyun tgid = perf_event__synthesize_comm(tool, event,
1813*4882a593Smuzhiyun rec->evlist->workload.pid,
1814*4882a593Smuzhiyun process_synthesized_event,
1815*4882a593Smuzhiyun machine);
1816*4882a593Smuzhiyun free(event);
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun if (tgid == -1)
1819*4882a593Smuzhiyun goto out_child;
1820*4882a593Smuzhiyun
1821*4882a593Smuzhiyun event = malloc(sizeof(event->namespaces) +
1822*4882a593Smuzhiyun (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
1823*4882a593Smuzhiyun machine->id_hdr_size);
1824*4882a593Smuzhiyun if (event == NULL) {
1825*4882a593Smuzhiyun err = -ENOMEM;
1826*4882a593Smuzhiyun goto out_child;
1827*4882a593Smuzhiyun }
1828*4882a593Smuzhiyun
1829*4882a593Smuzhiyun /*
1830*4882a593Smuzhiyun * Synthesize NAMESPACES event for the command specified.
1831*4882a593Smuzhiyun */
1832*4882a593Smuzhiyun perf_event__synthesize_namespaces(tool, event,
1833*4882a593Smuzhiyun rec->evlist->workload.pid,
1834*4882a593Smuzhiyun tgid, process_synthesized_event,
1835*4882a593Smuzhiyun machine);
1836*4882a593Smuzhiyun free(event);
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun perf_evlist__start_workload(rec->evlist);
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun if (evlist__initialize_ctlfd(rec->evlist, opts->ctl_fd, opts->ctl_fd_ack))
1842*4882a593Smuzhiyun goto out_child;
1843*4882a593Smuzhiyun
1844*4882a593Smuzhiyun if (opts->initial_delay) {
1845*4882a593Smuzhiyun pr_info(EVLIST_DISABLED_MSG);
1846*4882a593Smuzhiyun if (opts->initial_delay > 0) {
1847*4882a593Smuzhiyun usleep(opts->initial_delay * USEC_PER_MSEC);
1848*4882a593Smuzhiyun evlist__enable(rec->evlist);
1849*4882a593Smuzhiyun pr_info(EVLIST_ENABLED_MSG);
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun }
1852*4882a593Smuzhiyun
1853*4882a593Smuzhiyun trigger_ready(&auxtrace_snapshot_trigger);
1854*4882a593Smuzhiyun trigger_ready(&switch_output_trigger);
1855*4882a593Smuzhiyun perf_hooks__invoke_record_start();
1856*4882a593Smuzhiyun for (;;) {
1857*4882a593Smuzhiyun unsigned long long hits = rec->samples;
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun /*
1860*4882a593Smuzhiyun * rec->evlist->bkw_mmap_state is possible to be
1861*4882a593Smuzhiyun * BKW_MMAP_EMPTY here: when done == true and
1862*4882a593Smuzhiyun * hits != rec->samples in previous round.
1863*4882a593Smuzhiyun *
1864*4882a593Smuzhiyun * perf_evlist__toggle_bkw_mmap ensure we never
1865*4882a593Smuzhiyun * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
1866*4882a593Smuzhiyun */
1867*4882a593Smuzhiyun if (trigger_is_hit(&switch_output_trigger) || done || draining)
1868*4882a593Smuzhiyun perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun if (record__mmap_read_all(rec, false) < 0) {
1871*4882a593Smuzhiyun trigger_error(&auxtrace_snapshot_trigger);
1872*4882a593Smuzhiyun trigger_error(&switch_output_trigger);
1873*4882a593Smuzhiyun err = -1;
1874*4882a593Smuzhiyun goto out_child;
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun
1877*4882a593Smuzhiyun if (auxtrace_record__snapshot_started) {
1878*4882a593Smuzhiyun auxtrace_record__snapshot_started = 0;
1879*4882a593Smuzhiyun if (!trigger_is_error(&auxtrace_snapshot_trigger))
1880*4882a593Smuzhiyun record__read_auxtrace_snapshot(rec, false);
1881*4882a593Smuzhiyun if (trigger_is_error(&auxtrace_snapshot_trigger)) {
1882*4882a593Smuzhiyun pr_err("AUX area tracing snapshot failed\n");
1883*4882a593Smuzhiyun err = -1;
1884*4882a593Smuzhiyun goto out_child;
1885*4882a593Smuzhiyun }
1886*4882a593Smuzhiyun }
1887*4882a593Smuzhiyun
1888*4882a593Smuzhiyun if (trigger_is_hit(&switch_output_trigger)) {
1889*4882a593Smuzhiyun /*
1890*4882a593Smuzhiyun * If switch_output_trigger is hit, the data in
1891*4882a593Smuzhiyun * overwritable ring buffer should have been collected,
1892*4882a593Smuzhiyun * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
1893*4882a593Smuzhiyun *
1894*4882a593Smuzhiyun * If SIGUSR2 raise after or during record__mmap_read_all(),
1895*4882a593Smuzhiyun * record__mmap_read_all() didn't collect data from
1896*4882a593Smuzhiyun * overwritable ring buffer. Read again.
1897*4882a593Smuzhiyun */
1898*4882a593Smuzhiyun if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
1899*4882a593Smuzhiyun continue;
1900*4882a593Smuzhiyun trigger_ready(&switch_output_trigger);
1901*4882a593Smuzhiyun
1902*4882a593Smuzhiyun /*
1903*4882a593Smuzhiyun * Reenable events in overwrite ring buffer after
1904*4882a593Smuzhiyun * record__mmap_read_all(): we should have collected
1905*4882a593Smuzhiyun * data from it.
1906*4882a593Smuzhiyun */
1907*4882a593Smuzhiyun perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
1908*4882a593Smuzhiyun
1909*4882a593Smuzhiyun if (!quiet)
1910*4882a593Smuzhiyun fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
1911*4882a593Smuzhiyun waking);
1912*4882a593Smuzhiyun waking = 0;
1913*4882a593Smuzhiyun fd = record__switch_output(rec, false);
1914*4882a593Smuzhiyun if (fd < 0) {
1915*4882a593Smuzhiyun pr_err("Failed to switch to new file\n");
1916*4882a593Smuzhiyun trigger_error(&switch_output_trigger);
1917*4882a593Smuzhiyun err = fd;
1918*4882a593Smuzhiyun goto out_child;
1919*4882a593Smuzhiyun }
1920*4882a593Smuzhiyun
1921*4882a593Smuzhiyun /* re-arm the alarm */
1922*4882a593Smuzhiyun if (rec->switch_output.time)
1923*4882a593Smuzhiyun alarm(rec->switch_output.time);
1924*4882a593Smuzhiyun }
1925*4882a593Smuzhiyun
1926*4882a593Smuzhiyun if (hits == rec->samples) {
1927*4882a593Smuzhiyun if (done || draining)
1928*4882a593Smuzhiyun break;
1929*4882a593Smuzhiyun err = evlist__poll(rec->evlist, -1);
1930*4882a593Smuzhiyun /*
1931*4882a593Smuzhiyun * Propagate error, only if there's any. Ignore positive
1932*4882a593Smuzhiyun * number of returned events and interrupt error.
1933*4882a593Smuzhiyun */
1934*4882a593Smuzhiyun if (err > 0 || (err < 0 && errno == EINTR))
1935*4882a593Smuzhiyun err = 0;
1936*4882a593Smuzhiyun waking++;
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun if (evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
1939*4882a593Smuzhiyun draining = true;
1940*4882a593Smuzhiyun }
1941*4882a593Smuzhiyun
1942*4882a593Smuzhiyun if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) {
1943*4882a593Smuzhiyun switch (cmd) {
1944*4882a593Smuzhiyun case EVLIST_CTL_CMD_ENABLE:
1945*4882a593Smuzhiyun pr_info(EVLIST_ENABLED_MSG);
1946*4882a593Smuzhiyun break;
1947*4882a593Smuzhiyun case EVLIST_CTL_CMD_DISABLE:
1948*4882a593Smuzhiyun pr_info(EVLIST_DISABLED_MSG);
1949*4882a593Smuzhiyun break;
1950*4882a593Smuzhiyun case EVLIST_CTL_CMD_SNAPSHOT:
1951*4882a593Smuzhiyun hit_auxtrace_snapshot_trigger(rec);
1952*4882a593Smuzhiyun evlist__ctlfd_ack(rec->evlist);
1953*4882a593Smuzhiyun break;
1954*4882a593Smuzhiyun case EVLIST_CTL_CMD_ACK:
1955*4882a593Smuzhiyun case EVLIST_CTL_CMD_UNSUPPORTED:
1956*4882a593Smuzhiyun default:
1957*4882a593Smuzhiyun break;
1958*4882a593Smuzhiyun }
1959*4882a593Smuzhiyun }
1960*4882a593Smuzhiyun
1961*4882a593Smuzhiyun /*
1962*4882a593Smuzhiyun * When perf is starting the traced process, at the end events
1963*4882a593Smuzhiyun * die with the process and we wait for that. Thus no need to
1964*4882a593Smuzhiyun * disable events in this case.
1965*4882a593Smuzhiyun */
1966*4882a593Smuzhiyun if (done && !disabled && !target__none(&opts->target)) {
1967*4882a593Smuzhiyun trigger_off(&auxtrace_snapshot_trigger);
1968*4882a593Smuzhiyun evlist__disable(rec->evlist);
1969*4882a593Smuzhiyun disabled = true;
1970*4882a593Smuzhiyun }
1971*4882a593Smuzhiyun }
1972*4882a593Smuzhiyun
1973*4882a593Smuzhiyun trigger_off(&auxtrace_snapshot_trigger);
1974*4882a593Smuzhiyun trigger_off(&switch_output_trigger);
1975*4882a593Smuzhiyun
1976*4882a593Smuzhiyun if (opts->auxtrace_snapshot_on_exit)
1977*4882a593Smuzhiyun record__auxtrace_snapshot_exit(rec);
1978*4882a593Smuzhiyun
1979*4882a593Smuzhiyun if (forks && workload_exec_errno) {
1980*4882a593Smuzhiyun char msg[STRERR_BUFSIZE];
1981*4882a593Smuzhiyun const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
1982*4882a593Smuzhiyun pr_err("Workload failed: %s\n", emsg);
1983*4882a593Smuzhiyun err = -1;
1984*4882a593Smuzhiyun goto out_child;
1985*4882a593Smuzhiyun }
1986*4882a593Smuzhiyun
1987*4882a593Smuzhiyun if (!quiet)
1988*4882a593Smuzhiyun fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
1989*4882a593Smuzhiyun
1990*4882a593Smuzhiyun if (target__none(&rec->opts.target))
1991*4882a593Smuzhiyun record__synthesize_workload(rec, true);
1992*4882a593Smuzhiyun
1993*4882a593Smuzhiyun out_child:
1994*4882a593Smuzhiyun evlist__finalize_ctlfd(rec->evlist);
1995*4882a593Smuzhiyun record__mmap_read_all(rec, true);
1996*4882a593Smuzhiyun record__aio_mmap_read_sync(rec);
1997*4882a593Smuzhiyun
1998*4882a593Smuzhiyun if (rec->session->bytes_transferred && rec->session->bytes_compressed) {
1999*4882a593Smuzhiyun ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed;
2000*4882a593Smuzhiyun session->header.env.comp_ratio = ratio + 0.5;
2001*4882a593Smuzhiyun }
2002*4882a593Smuzhiyun
2003*4882a593Smuzhiyun if (forks) {
2004*4882a593Smuzhiyun int exit_status;
2005*4882a593Smuzhiyun
2006*4882a593Smuzhiyun if (!child_finished)
2007*4882a593Smuzhiyun kill(rec->evlist->workload.pid, SIGTERM);
2008*4882a593Smuzhiyun
2009*4882a593Smuzhiyun wait(&exit_status);
2010*4882a593Smuzhiyun
2011*4882a593Smuzhiyun if (err < 0)
2012*4882a593Smuzhiyun status = err;
2013*4882a593Smuzhiyun else if (WIFEXITED(exit_status))
2014*4882a593Smuzhiyun status = WEXITSTATUS(exit_status);
2015*4882a593Smuzhiyun else if (WIFSIGNALED(exit_status))
2016*4882a593Smuzhiyun signr = WTERMSIG(exit_status);
2017*4882a593Smuzhiyun } else
2018*4882a593Smuzhiyun status = err;
2019*4882a593Smuzhiyun
2020*4882a593Smuzhiyun record__synthesize(rec, true);
2021*4882a593Smuzhiyun /* this will be recalculated during process_buildids() */
2022*4882a593Smuzhiyun rec->samples = 0;
2023*4882a593Smuzhiyun
2024*4882a593Smuzhiyun if (!err) {
2025*4882a593Smuzhiyun if (!rec->timestamp_filename) {
2026*4882a593Smuzhiyun record__finish_output(rec);
2027*4882a593Smuzhiyun } else {
2028*4882a593Smuzhiyun fd = record__switch_output(rec, true);
2029*4882a593Smuzhiyun if (fd < 0) {
2030*4882a593Smuzhiyun status = fd;
2031*4882a593Smuzhiyun goto out_delete_session;
2032*4882a593Smuzhiyun }
2033*4882a593Smuzhiyun }
2034*4882a593Smuzhiyun }
2035*4882a593Smuzhiyun
2036*4882a593Smuzhiyun perf_hooks__invoke_record_end();
2037*4882a593Smuzhiyun
2038*4882a593Smuzhiyun if (!err && !quiet) {
2039*4882a593Smuzhiyun char samples[128];
2040*4882a593Smuzhiyun const char *postfix = rec->timestamp_filename ?
2041*4882a593Smuzhiyun ".<timestamp>" : "";
2042*4882a593Smuzhiyun
2043*4882a593Smuzhiyun if (rec->samples && !rec->opts.full_auxtrace)
2044*4882a593Smuzhiyun scnprintf(samples, sizeof(samples),
2045*4882a593Smuzhiyun " (%" PRIu64 " samples)", rec->samples);
2046*4882a593Smuzhiyun else
2047*4882a593Smuzhiyun samples[0] = '\0';
2048*4882a593Smuzhiyun
2049*4882a593Smuzhiyun fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s",
2050*4882a593Smuzhiyun perf_data__size(data) / 1024.0 / 1024.0,
2051*4882a593Smuzhiyun data->path, postfix, samples);
2052*4882a593Smuzhiyun if (ratio) {
2053*4882a593Smuzhiyun fprintf(stderr, ", compressed (original %.3f MB, ratio is %.3f)",
2054*4882a593Smuzhiyun rec->session->bytes_transferred / 1024.0 / 1024.0,
2055*4882a593Smuzhiyun ratio);
2056*4882a593Smuzhiyun }
2057*4882a593Smuzhiyun fprintf(stderr, " ]\n");
2058*4882a593Smuzhiyun }
2059*4882a593Smuzhiyun
2060*4882a593Smuzhiyun out_delete_session:
2061*4882a593Smuzhiyun #ifdef HAVE_EVENTFD_SUPPORT
2062*4882a593Smuzhiyun if (done_fd >= 0)
2063*4882a593Smuzhiyun close(done_fd);
2064*4882a593Smuzhiyun #endif
2065*4882a593Smuzhiyun zstd_fini(&session->zstd_data);
2066*4882a593Smuzhiyun perf_session__delete(session);
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun if (!opts->no_bpf_event)
2069*4882a593Smuzhiyun perf_evlist__stop_sb_thread(rec->sb_evlist);
2070*4882a593Smuzhiyun return status;
2071*4882a593Smuzhiyun }
2072*4882a593Smuzhiyun
callchain_debug(struct callchain_param * callchain)2073*4882a593Smuzhiyun static void callchain_debug(struct callchain_param *callchain)
2074*4882a593Smuzhiyun {
2075*4882a593Smuzhiyun static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
2076*4882a593Smuzhiyun
2077*4882a593Smuzhiyun pr_debug("callchain: type %s\n", str[callchain->record_mode]);
2078*4882a593Smuzhiyun
2079*4882a593Smuzhiyun if (callchain->record_mode == CALLCHAIN_DWARF)
2080*4882a593Smuzhiyun pr_debug("callchain: stack dump size %d\n",
2081*4882a593Smuzhiyun callchain->dump_size);
2082*4882a593Smuzhiyun }
2083*4882a593Smuzhiyun
record_opts__parse_callchain(struct record_opts * record,struct callchain_param * callchain,const char * arg,bool unset)2084*4882a593Smuzhiyun int record_opts__parse_callchain(struct record_opts *record,
2085*4882a593Smuzhiyun struct callchain_param *callchain,
2086*4882a593Smuzhiyun const char *arg, bool unset)
2087*4882a593Smuzhiyun {
2088*4882a593Smuzhiyun int ret;
2089*4882a593Smuzhiyun callchain->enabled = !unset;
2090*4882a593Smuzhiyun
2091*4882a593Smuzhiyun /* --no-call-graph */
2092*4882a593Smuzhiyun if (unset) {
2093*4882a593Smuzhiyun callchain->record_mode = CALLCHAIN_NONE;
2094*4882a593Smuzhiyun pr_debug("callchain: disabled\n");
2095*4882a593Smuzhiyun return 0;
2096*4882a593Smuzhiyun }
2097*4882a593Smuzhiyun
2098*4882a593Smuzhiyun ret = parse_callchain_record_opt(arg, callchain);
2099*4882a593Smuzhiyun if (!ret) {
2100*4882a593Smuzhiyun /* Enable data address sampling for DWARF unwind. */
2101*4882a593Smuzhiyun if (callchain->record_mode == CALLCHAIN_DWARF)
2102*4882a593Smuzhiyun record->sample_address = true;
2103*4882a593Smuzhiyun callchain_debug(callchain);
2104*4882a593Smuzhiyun }
2105*4882a593Smuzhiyun
2106*4882a593Smuzhiyun return ret;
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun
record_parse_callchain_opt(const struct option * opt,const char * arg,int unset)2109*4882a593Smuzhiyun int record_parse_callchain_opt(const struct option *opt,
2110*4882a593Smuzhiyun const char *arg,
2111*4882a593Smuzhiyun int unset)
2112*4882a593Smuzhiyun {
2113*4882a593Smuzhiyun return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
2114*4882a593Smuzhiyun }
2115*4882a593Smuzhiyun
record_callchain_opt(const struct option * opt,const char * arg __maybe_unused,int unset __maybe_unused)2116*4882a593Smuzhiyun int record_callchain_opt(const struct option *opt,
2117*4882a593Smuzhiyun const char *arg __maybe_unused,
2118*4882a593Smuzhiyun int unset __maybe_unused)
2119*4882a593Smuzhiyun {
2120*4882a593Smuzhiyun struct callchain_param *callchain = opt->value;
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun callchain->enabled = true;
2123*4882a593Smuzhiyun
2124*4882a593Smuzhiyun if (callchain->record_mode == CALLCHAIN_NONE)
2125*4882a593Smuzhiyun callchain->record_mode = CALLCHAIN_FP;
2126*4882a593Smuzhiyun
2127*4882a593Smuzhiyun callchain_debug(callchain);
2128*4882a593Smuzhiyun return 0;
2129*4882a593Smuzhiyun }
2130*4882a593Smuzhiyun
perf_record_config(const char * var,const char * value,void * cb)2131*4882a593Smuzhiyun static int perf_record_config(const char *var, const char *value, void *cb)
2132*4882a593Smuzhiyun {
2133*4882a593Smuzhiyun struct record *rec = cb;
2134*4882a593Smuzhiyun
2135*4882a593Smuzhiyun if (!strcmp(var, "record.build-id")) {
2136*4882a593Smuzhiyun if (!strcmp(value, "cache"))
2137*4882a593Smuzhiyun rec->no_buildid_cache = false;
2138*4882a593Smuzhiyun else if (!strcmp(value, "no-cache"))
2139*4882a593Smuzhiyun rec->no_buildid_cache = true;
2140*4882a593Smuzhiyun else if (!strcmp(value, "skip"))
2141*4882a593Smuzhiyun rec->no_buildid = true;
2142*4882a593Smuzhiyun else
2143*4882a593Smuzhiyun return -1;
2144*4882a593Smuzhiyun return 0;
2145*4882a593Smuzhiyun }
2146*4882a593Smuzhiyun if (!strcmp(var, "record.call-graph")) {
2147*4882a593Smuzhiyun var = "call-graph.record-mode";
2148*4882a593Smuzhiyun return perf_default_config(var, value, cb);
2149*4882a593Smuzhiyun }
2150*4882a593Smuzhiyun #ifdef HAVE_AIO_SUPPORT
2151*4882a593Smuzhiyun if (!strcmp(var, "record.aio")) {
2152*4882a593Smuzhiyun rec->opts.nr_cblocks = strtol(value, NULL, 0);
2153*4882a593Smuzhiyun if (!rec->opts.nr_cblocks)
2154*4882a593Smuzhiyun rec->opts.nr_cblocks = nr_cblocks_default;
2155*4882a593Smuzhiyun }
2156*4882a593Smuzhiyun #endif
2157*4882a593Smuzhiyun
2158*4882a593Smuzhiyun return 0;
2159*4882a593Smuzhiyun }
2160*4882a593Smuzhiyun
2161*4882a593Smuzhiyun
record__parse_affinity(const struct option * opt,const char * str,int unset)2162*4882a593Smuzhiyun static int record__parse_affinity(const struct option *opt, const char *str, int unset)
2163*4882a593Smuzhiyun {
2164*4882a593Smuzhiyun struct record_opts *opts = (struct record_opts *)opt->value;
2165*4882a593Smuzhiyun
2166*4882a593Smuzhiyun if (unset || !str)
2167*4882a593Smuzhiyun return 0;
2168*4882a593Smuzhiyun
2169*4882a593Smuzhiyun if (!strcasecmp(str, "node"))
2170*4882a593Smuzhiyun opts->affinity = PERF_AFFINITY_NODE;
2171*4882a593Smuzhiyun else if (!strcasecmp(str, "cpu"))
2172*4882a593Smuzhiyun opts->affinity = PERF_AFFINITY_CPU;
2173*4882a593Smuzhiyun
2174*4882a593Smuzhiyun return 0;
2175*4882a593Smuzhiyun }
2176*4882a593Smuzhiyun
parse_output_max_size(const struct option * opt,const char * str,int unset)2177*4882a593Smuzhiyun static int parse_output_max_size(const struct option *opt,
2178*4882a593Smuzhiyun const char *str, int unset)
2179*4882a593Smuzhiyun {
2180*4882a593Smuzhiyun unsigned long *s = (unsigned long *)opt->value;
2181*4882a593Smuzhiyun static struct parse_tag tags_size[] = {
2182*4882a593Smuzhiyun { .tag = 'B', .mult = 1 },
2183*4882a593Smuzhiyun { .tag = 'K', .mult = 1 << 10 },
2184*4882a593Smuzhiyun { .tag = 'M', .mult = 1 << 20 },
2185*4882a593Smuzhiyun { .tag = 'G', .mult = 1 << 30 },
2186*4882a593Smuzhiyun { .tag = 0 },
2187*4882a593Smuzhiyun };
2188*4882a593Smuzhiyun unsigned long val;
2189*4882a593Smuzhiyun
2190*4882a593Smuzhiyun if (unset) {
2191*4882a593Smuzhiyun *s = 0;
2192*4882a593Smuzhiyun return 0;
2193*4882a593Smuzhiyun }
2194*4882a593Smuzhiyun
2195*4882a593Smuzhiyun val = parse_tag_value(str, tags_size);
2196*4882a593Smuzhiyun if (val != (unsigned long) -1) {
2197*4882a593Smuzhiyun *s = val;
2198*4882a593Smuzhiyun return 0;
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun
2201*4882a593Smuzhiyun return -1;
2202*4882a593Smuzhiyun }
2203*4882a593Smuzhiyun
record__parse_mmap_pages(const struct option * opt,const char * str,int unset __maybe_unused)2204*4882a593Smuzhiyun static int record__parse_mmap_pages(const struct option *opt,
2205*4882a593Smuzhiyun const char *str,
2206*4882a593Smuzhiyun int unset __maybe_unused)
2207*4882a593Smuzhiyun {
2208*4882a593Smuzhiyun struct record_opts *opts = opt->value;
2209*4882a593Smuzhiyun char *s, *p;
2210*4882a593Smuzhiyun unsigned int mmap_pages;
2211*4882a593Smuzhiyun int ret;
2212*4882a593Smuzhiyun
2213*4882a593Smuzhiyun if (!str)
2214*4882a593Smuzhiyun return -EINVAL;
2215*4882a593Smuzhiyun
2216*4882a593Smuzhiyun s = strdup(str);
2217*4882a593Smuzhiyun if (!s)
2218*4882a593Smuzhiyun return -ENOMEM;
2219*4882a593Smuzhiyun
2220*4882a593Smuzhiyun p = strchr(s, ',');
2221*4882a593Smuzhiyun if (p)
2222*4882a593Smuzhiyun *p = '\0';
2223*4882a593Smuzhiyun
2224*4882a593Smuzhiyun if (*s) {
2225*4882a593Smuzhiyun ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s);
2226*4882a593Smuzhiyun if (ret)
2227*4882a593Smuzhiyun goto out_free;
2228*4882a593Smuzhiyun opts->mmap_pages = mmap_pages;
2229*4882a593Smuzhiyun }
2230*4882a593Smuzhiyun
2231*4882a593Smuzhiyun if (!p) {
2232*4882a593Smuzhiyun ret = 0;
2233*4882a593Smuzhiyun goto out_free;
2234*4882a593Smuzhiyun }
2235*4882a593Smuzhiyun
2236*4882a593Smuzhiyun ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1);
2237*4882a593Smuzhiyun if (ret)
2238*4882a593Smuzhiyun goto out_free;
2239*4882a593Smuzhiyun
2240*4882a593Smuzhiyun opts->auxtrace_mmap_pages = mmap_pages;
2241*4882a593Smuzhiyun
2242*4882a593Smuzhiyun out_free:
2243*4882a593Smuzhiyun free(s);
2244*4882a593Smuzhiyun return ret;
2245*4882a593Smuzhiyun }
2246*4882a593Smuzhiyun
parse_control_option(const struct option * opt,const char * str,int unset __maybe_unused)2247*4882a593Smuzhiyun static int parse_control_option(const struct option *opt,
2248*4882a593Smuzhiyun const char *str,
2249*4882a593Smuzhiyun int unset __maybe_unused)
2250*4882a593Smuzhiyun {
2251*4882a593Smuzhiyun struct record_opts *opts = opt->value;
2252*4882a593Smuzhiyun
2253*4882a593Smuzhiyun return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close);
2254*4882a593Smuzhiyun }
2255*4882a593Smuzhiyun
switch_output_size_warn(struct record * rec)2256*4882a593Smuzhiyun static void switch_output_size_warn(struct record *rec)
2257*4882a593Smuzhiyun {
2258*4882a593Smuzhiyun u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages);
2259*4882a593Smuzhiyun struct switch_output *s = &rec->switch_output;
2260*4882a593Smuzhiyun
2261*4882a593Smuzhiyun wakeup_size /= 2;
2262*4882a593Smuzhiyun
2263*4882a593Smuzhiyun if (s->size < wakeup_size) {
2264*4882a593Smuzhiyun char buf[100];
2265*4882a593Smuzhiyun
2266*4882a593Smuzhiyun unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
2267*4882a593Smuzhiyun pr_warning("WARNING: switch-output data size lower than "
2268*4882a593Smuzhiyun "wakeup kernel buffer size (%s) "
2269*4882a593Smuzhiyun "expect bigger perf.data sizes\n", buf);
2270*4882a593Smuzhiyun }
2271*4882a593Smuzhiyun }
2272*4882a593Smuzhiyun
switch_output_setup(struct record * rec)2273*4882a593Smuzhiyun static int switch_output_setup(struct record *rec)
2274*4882a593Smuzhiyun {
2275*4882a593Smuzhiyun struct switch_output *s = &rec->switch_output;
2276*4882a593Smuzhiyun static struct parse_tag tags_size[] = {
2277*4882a593Smuzhiyun { .tag = 'B', .mult = 1 },
2278*4882a593Smuzhiyun { .tag = 'K', .mult = 1 << 10 },
2279*4882a593Smuzhiyun { .tag = 'M', .mult = 1 << 20 },
2280*4882a593Smuzhiyun { .tag = 'G', .mult = 1 << 30 },
2281*4882a593Smuzhiyun { .tag = 0 },
2282*4882a593Smuzhiyun };
2283*4882a593Smuzhiyun static struct parse_tag tags_time[] = {
2284*4882a593Smuzhiyun { .tag = 's', .mult = 1 },
2285*4882a593Smuzhiyun { .tag = 'm', .mult = 60 },
2286*4882a593Smuzhiyun { .tag = 'h', .mult = 60*60 },
2287*4882a593Smuzhiyun { .tag = 'd', .mult = 60*60*24 },
2288*4882a593Smuzhiyun { .tag = 0 },
2289*4882a593Smuzhiyun };
2290*4882a593Smuzhiyun unsigned long val;
2291*4882a593Smuzhiyun
2292*4882a593Smuzhiyun /*
2293*4882a593Smuzhiyun * If we're using --switch-output-events, then we imply its
2294*4882a593Smuzhiyun * --switch-output=signal, as we'll send a SIGUSR2 from the side band
2295*4882a593Smuzhiyun * thread to its parent.
2296*4882a593Smuzhiyun */
2297*4882a593Smuzhiyun if (rec->switch_output_event_set)
2298*4882a593Smuzhiyun goto do_signal;
2299*4882a593Smuzhiyun
2300*4882a593Smuzhiyun if (!s->set)
2301*4882a593Smuzhiyun return 0;
2302*4882a593Smuzhiyun
2303*4882a593Smuzhiyun if (!strcmp(s->str, "signal")) {
2304*4882a593Smuzhiyun do_signal:
2305*4882a593Smuzhiyun s->signal = true;
2306*4882a593Smuzhiyun pr_debug("switch-output with SIGUSR2 signal\n");
2307*4882a593Smuzhiyun goto enabled;
2308*4882a593Smuzhiyun }
2309*4882a593Smuzhiyun
2310*4882a593Smuzhiyun val = parse_tag_value(s->str, tags_size);
2311*4882a593Smuzhiyun if (val != (unsigned long) -1) {
2312*4882a593Smuzhiyun s->size = val;
2313*4882a593Smuzhiyun pr_debug("switch-output with %s size threshold\n", s->str);
2314*4882a593Smuzhiyun goto enabled;
2315*4882a593Smuzhiyun }
2316*4882a593Smuzhiyun
2317*4882a593Smuzhiyun val = parse_tag_value(s->str, tags_time);
2318*4882a593Smuzhiyun if (val != (unsigned long) -1) {
2319*4882a593Smuzhiyun s->time = val;
2320*4882a593Smuzhiyun pr_debug("switch-output with %s time threshold (%lu seconds)\n",
2321*4882a593Smuzhiyun s->str, s->time);
2322*4882a593Smuzhiyun goto enabled;
2323*4882a593Smuzhiyun }
2324*4882a593Smuzhiyun
2325*4882a593Smuzhiyun return -1;
2326*4882a593Smuzhiyun
2327*4882a593Smuzhiyun enabled:
2328*4882a593Smuzhiyun rec->timestamp_filename = true;
2329*4882a593Smuzhiyun s->enabled = true;
2330*4882a593Smuzhiyun
2331*4882a593Smuzhiyun if (s->size && !rec->opts.no_buffering)
2332*4882a593Smuzhiyun switch_output_size_warn(rec);
2333*4882a593Smuzhiyun
2334*4882a593Smuzhiyun return 0;
2335*4882a593Smuzhiyun }
2336*4882a593Smuzhiyun
2337*4882a593Smuzhiyun static const char * const __record_usage[] = {
2338*4882a593Smuzhiyun "perf record [<options>] [<command>]",
2339*4882a593Smuzhiyun "perf record [<options>] -- <command> [<options>]",
2340*4882a593Smuzhiyun NULL
2341*4882a593Smuzhiyun };
2342*4882a593Smuzhiyun const char * const *record_usage = __record_usage;
2343*4882a593Smuzhiyun
build_id__process_mmap(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2344*4882a593Smuzhiyun static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event,
2345*4882a593Smuzhiyun struct perf_sample *sample, struct machine *machine)
2346*4882a593Smuzhiyun {
2347*4882a593Smuzhiyun /*
2348*4882a593Smuzhiyun * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
2349*4882a593Smuzhiyun * no need to add them twice.
2350*4882a593Smuzhiyun */
2351*4882a593Smuzhiyun if (!(event->header.misc & PERF_RECORD_MISC_USER))
2352*4882a593Smuzhiyun return 0;
2353*4882a593Smuzhiyun return perf_event__process_mmap(tool, event, sample, machine);
2354*4882a593Smuzhiyun }
2355*4882a593Smuzhiyun
build_id__process_mmap2(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2356*4882a593Smuzhiyun static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event,
2357*4882a593Smuzhiyun struct perf_sample *sample, struct machine *machine)
2358*4882a593Smuzhiyun {
2359*4882a593Smuzhiyun /*
2360*4882a593Smuzhiyun * We already have the kernel maps, put in place via perf_session__create_kernel_maps()
2361*4882a593Smuzhiyun * no need to add them twice.
2362*4882a593Smuzhiyun */
2363*4882a593Smuzhiyun if (!(event->header.misc & PERF_RECORD_MISC_USER))
2364*4882a593Smuzhiyun return 0;
2365*4882a593Smuzhiyun
2366*4882a593Smuzhiyun return perf_event__process_mmap2(tool, event, sample, machine);
2367*4882a593Smuzhiyun }
2368*4882a593Smuzhiyun
2369*4882a593Smuzhiyun /*
2370*4882a593Smuzhiyun * XXX Ideally would be local to cmd_record() and passed to a record__new
2371*4882a593Smuzhiyun * because we need to have access to it in record__exit, that is called
2372*4882a593Smuzhiyun * after cmd_record() exits, but since record_options need to be accessible to
2373*4882a593Smuzhiyun * builtin-script, leave it here.
2374*4882a593Smuzhiyun *
2375*4882a593Smuzhiyun * At least we don't ouch it in all the other functions here directly.
2376*4882a593Smuzhiyun *
2377*4882a593Smuzhiyun * Just say no to tons of global variables, sigh.
2378*4882a593Smuzhiyun */
2379*4882a593Smuzhiyun static struct record record = {
2380*4882a593Smuzhiyun .opts = {
2381*4882a593Smuzhiyun .sample_time = true,
2382*4882a593Smuzhiyun .mmap_pages = UINT_MAX,
2383*4882a593Smuzhiyun .user_freq = UINT_MAX,
2384*4882a593Smuzhiyun .user_interval = ULLONG_MAX,
2385*4882a593Smuzhiyun .freq = 4000,
2386*4882a593Smuzhiyun .target = {
2387*4882a593Smuzhiyun .uses_mmap = true,
2388*4882a593Smuzhiyun .default_per_cpu = true,
2389*4882a593Smuzhiyun },
2390*4882a593Smuzhiyun .mmap_flush = MMAP_FLUSH_DEFAULT,
2391*4882a593Smuzhiyun .nr_threads_synthesize = 1,
2392*4882a593Smuzhiyun .ctl_fd = -1,
2393*4882a593Smuzhiyun .ctl_fd_ack = -1,
2394*4882a593Smuzhiyun },
2395*4882a593Smuzhiyun .tool = {
2396*4882a593Smuzhiyun .sample = process_sample_event,
2397*4882a593Smuzhiyun .fork = perf_event__process_fork,
2398*4882a593Smuzhiyun .exit = perf_event__process_exit,
2399*4882a593Smuzhiyun .comm = perf_event__process_comm,
2400*4882a593Smuzhiyun .namespaces = perf_event__process_namespaces,
2401*4882a593Smuzhiyun .mmap = build_id__process_mmap,
2402*4882a593Smuzhiyun .mmap2 = build_id__process_mmap2,
2403*4882a593Smuzhiyun .ordered_events = true,
2404*4882a593Smuzhiyun },
2405*4882a593Smuzhiyun };
2406*4882a593Smuzhiyun
2407*4882a593Smuzhiyun const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
2408*4882a593Smuzhiyun "\n\t\t\t\tDefault: fp";
2409*4882a593Smuzhiyun
2410*4882a593Smuzhiyun static bool dry_run;
2411*4882a593Smuzhiyun
2412*4882a593Smuzhiyun /*
2413*4882a593Smuzhiyun * XXX Will stay a global variable till we fix builtin-script.c to stop messing
2414*4882a593Smuzhiyun * with it and switch to use the library functions in perf_evlist that came
2415*4882a593Smuzhiyun * from builtin-record.c, i.e. use record_opts,
2416*4882a593Smuzhiyun * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
2417*4882a593Smuzhiyun * using pipes, etc.
2418*4882a593Smuzhiyun */
2419*4882a593Smuzhiyun static struct option __record_options[] = {
2420*4882a593Smuzhiyun OPT_CALLBACK('e', "event", &record.evlist, "event",
2421*4882a593Smuzhiyun "event selector. use 'perf list' to list available events",
2422*4882a593Smuzhiyun parse_events_option),
2423*4882a593Smuzhiyun OPT_CALLBACK(0, "filter", &record.evlist, "filter",
2424*4882a593Smuzhiyun "event filter", parse_filter),
2425*4882a593Smuzhiyun OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
2426*4882a593Smuzhiyun NULL, "don't record events from perf itself",
2427*4882a593Smuzhiyun exclude_perf),
2428*4882a593Smuzhiyun OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
2429*4882a593Smuzhiyun "record events on existing process id"),
2430*4882a593Smuzhiyun OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
2431*4882a593Smuzhiyun "record events on existing thread id"),
2432*4882a593Smuzhiyun OPT_INTEGER('r', "realtime", &record.realtime_prio,
2433*4882a593Smuzhiyun "collect data with this RT SCHED_FIFO priority"),
2434*4882a593Smuzhiyun OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
2435*4882a593Smuzhiyun "collect data without buffering"),
2436*4882a593Smuzhiyun OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
2437*4882a593Smuzhiyun "collect raw sample records from all opened counters"),
2438*4882a593Smuzhiyun OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
2439*4882a593Smuzhiyun "system-wide collection from all CPUs"),
2440*4882a593Smuzhiyun OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
2441*4882a593Smuzhiyun "list of cpus to monitor"),
2442*4882a593Smuzhiyun OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
2443*4882a593Smuzhiyun OPT_STRING('o', "output", &record.data.path, "file",
2444*4882a593Smuzhiyun "output file name"),
2445*4882a593Smuzhiyun OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
2446*4882a593Smuzhiyun &record.opts.no_inherit_set,
2447*4882a593Smuzhiyun "child tasks do not inherit counters"),
2448*4882a593Smuzhiyun OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
2449*4882a593Smuzhiyun "synthesize non-sample events at the end of output"),
2450*4882a593Smuzhiyun OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
2451*4882a593Smuzhiyun OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"),
2452*4882a593Smuzhiyun OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq,
2453*4882a593Smuzhiyun "Fail if the specified frequency can't be used"),
2454*4882a593Smuzhiyun OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'",
2455*4882a593Smuzhiyun "profile at this frequency",
2456*4882a593Smuzhiyun record__parse_freq),
2457*4882a593Smuzhiyun OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
2458*4882a593Smuzhiyun "number of mmap data pages and AUX area tracing mmap pages",
2459*4882a593Smuzhiyun record__parse_mmap_pages),
2460*4882a593Smuzhiyun OPT_CALLBACK(0, "mmap-flush", &record.opts, "number",
2461*4882a593Smuzhiyun "Minimal number of bytes that is extracted from mmap data pages (default: 1)",
2462*4882a593Smuzhiyun record__mmap_flush_parse),
2463*4882a593Smuzhiyun OPT_BOOLEAN(0, "group", &record.opts.group,
2464*4882a593Smuzhiyun "put the counters into a counter group"),
2465*4882a593Smuzhiyun OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
2466*4882a593Smuzhiyun NULL, "enables call-graph recording" ,
2467*4882a593Smuzhiyun &record_callchain_opt),
2468*4882a593Smuzhiyun OPT_CALLBACK(0, "call-graph", &record.opts,
2469*4882a593Smuzhiyun "record_mode[,record_size]", record_callchain_help,
2470*4882a593Smuzhiyun &record_parse_callchain_opt),
2471*4882a593Smuzhiyun OPT_INCR('v', "verbose", &verbose,
2472*4882a593Smuzhiyun "be more verbose (show counter open errors, etc)"),
2473*4882a593Smuzhiyun OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
2474*4882a593Smuzhiyun OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
2475*4882a593Smuzhiyun "per thread counts"),
2476*4882a593Smuzhiyun OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
2477*4882a593Smuzhiyun OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr,
2478*4882a593Smuzhiyun "Record the sample physical addresses"),
2479*4882a593Smuzhiyun OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
2480*4882a593Smuzhiyun OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
2481*4882a593Smuzhiyun &record.opts.sample_time_set,
2482*4882a593Smuzhiyun "Record the sample timestamps"),
2483*4882a593Smuzhiyun OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set,
2484*4882a593Smuzhiyun "Record the sample period"),
2485*4882a593Smuzhiyun OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
2486*4882a593Smuzhiyun "don't sample"),
2487*4882a593Smuzhiyun OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
2488*4882a593Smuzhiyun &record.no_buildid_cache_set,
2489*4882a593Smuzhiyun "do not update the buildid cache"),
2490*4882a593Smuzhiyun OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
2491*4882a593Smuzhiyun &record.no_buildid_set,
2492*4882a593Smuzhiyun "do not collect buildids in perf.data"),
2493*4882a593Smuzhiyun OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
2494*4882a593Smuzhiyun "monitor event in cgroup name only",
2495*4882a593Smuzhiyun parse_cgroups),
2496*4882a593Smuzhiyun OPT_INTEGER('D', "delay", &record.opts.initial_delay,
2497*4882a593Smuzhiyun "ms to wait before starting measurement after program start (-1: start with events disabled)"),
2498*4882a593Smuzhiyun OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"),
2499*4882a593Smuzhiyun OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
2500*4882a593Smuzhiyun "user to profile"),
2501*4882a593Smuzhiyun
2502*4882a593Smuzhiyun OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
2503*4882a593Smuzhiyun "branch any", "sample any taken branches",
2504*4882a593Smuzhiyun parse_branch_stack),
2505*4882a593Smuzhiyun
2506*4882a593Smuzhiyun OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
2507*4882a593Smuzhiyun "branch filter mask", "branch stack filter modes",
2508*4882a593Smuzhiyun parse_branch_stack),
2509*4882a593Smuzhiyun OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
2510*4882a593Smuzhiyun "sample by weight (on special events only)"),
2511*4882a593Smuzhiyun OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
2512*4882a593Smuzhiyun "sample transaction flags (special events only)"),
2513*4882a593Smuzhiyun OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
2514*4882a593Smuzhiyun "use per-thread mmaps"),
2515*4882a593Smuzhiyun OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
2516*4882a593Smuzhiyun "sample selected machine registers on interrupt,"
2517*4882a593Smuzhiyun " use '-I?' to list register names", parse_intr_regs),
2518*4882a593Smuzhiyun OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register",
2519*4882a593Smuzhiyun "sample selected machine registers on interrupt,"
2520*4882a593Smuzhiyun " use '--user-regs=?' to list register names", parse_user_regs),
2521*4882a593Smuzhiyun OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
2522*4882a593Smuzhiyun "Record running/enabled time of read (:S) events"),
2523*4882a593Smuzhiyun OPT_CALLBACK('k', "clockid", &record.opts,
2524*4882a593Smuzhiyun "clockid", "clockid to use for events, see clock_gettime()",
2525*4882a593Smuzhiyun parse_clockid),
2526*4882a593Smuzhiyun OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
2527*4882a593Smuzhiyun "opts", "AUX area tracing Snapshot Mode", ""),
2528*4882a593Smuzhiyun OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts,
2529*4882a593Smuzhiyun "opts", "sample AUX area", ""),
2530*4882a593Smuzhiyun OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
2531*4882a593Smuzhiyun "per thread proc mmap processing timeout in ms"),
2532*4882a593Smuzhiyun OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces,
2533*4882a593Smuzhiyun "Record namespaces events"),
2534*4882a593Smuzhiyun OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup,
2535*4882a593Smuzhiyun "Record cgroup events"),
2536*4882a593Smuzhiyun OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events,
2537*4882a593Smuzhiyun &record.opts.record_switch_events_set,
2538*4882a593Smuzhiyun "Record context switch events"),
2539*4882a593Smuzhiyun OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
2540*4882a593Smuzhiyun "Configure all used events to run in kernel space.",
2541*4882a593Smuzhiyun PARSE_OPT_EXCLUSIVE),
2542*4882a593Smuzhiyun OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
2543*4882a593Smuzhiyun "Configure all used events to run in user space.",
2544*4882a593Smuzhiyun PARSE_OPT_EXCLUSIVE),
2545*4882a593Smuzhiyun OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains,
2546*4882a593Smuzhiyun "collect kernel callchains"),
2547*4882a593Smuzhiyun OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains,
2548*4882a593Smuzhiyun "collect user callchains"),
2549*4882a593Smuzhiyun OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
2550*4882a593Smuzhiyun "clang binary to use for compiling BPF scriptlets"),
2551*4882a593Smuzhiyun OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
2552*4882a593Smuzhiyun "options passed to clang when compiling BPF scriptlets"),
2553*4882a593Smuzhiyun OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
2554*4882a593Smuzhiyun "file", "vmlinux pathname"),
2555*4882a593Smuzhiyun OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
2556*4882a593Smuzhiyun "Record build-id of all DSOs regardless of hits"),
2557*4882a593Smuzhiyun OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
2558*4882a593Smuzhiyun "append timestamp to output filename"),
2559*4882a593Smuzhiyun OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary,
2560*4882a593Smuzhiyun "Record timestamp boundary (time of first/last samples)"),
2561*4882a593Smuzhiyun OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
2562*4882a593Smuzhiyun &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
2563*4882a593Smuzhiyun "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
2564*4882a593Smuzhiyun "signal"),
2565*4882a593Smuzhiyun OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event",
2566*4882a593Smuzhiyun "switch output event selector. use 'perf list' to list available events",
2567*4882a593Smuzhiyun parse_events_option_new_evlist),
2568*4882a593Smuzhiyun OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
2569*4882a593Smuzhiyun "Limit number of switch output generated files"),
2570*4882a593Smuzhiyun OPT_BOOLEAN(0, "dry-run", &dry_run,
2571*4882a593Smuzhiyun "Parse options then exit"),
2572*4882a593Smuzhiyun #ifdef HAVE_AIO_SUPPORT
2573*4882a593Smuzhiyun OPT_CALLBACK_OPTARG(0, "aio", &record.opts,
2574*4882a593Smuzhiyun &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
2575*4882a593Smuzhiyun record__aio_parse),
2576*4882a593Smuzhiyun #endif
2577*4882a593Smuzhiyun OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
2578*4882a593Smuzhiyun "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
2579*4882a593Smuzhiyun record__parse_affinity),
2580*4882a593Smuzhiyun #ifdef HAVE_ZSTD_SUPPORT
2581*4882a593Smuzhiyun OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default,
2582*4882a593Smuzhiyun "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
2583*4882a593Smuzhiyun record__parse_comp_level),
2584*4882a593Smuzhiyun #endif
2585*4882a593Smuzhiyun OPT_CALLBACK(0, "max-size", &record.output_max_size,
2586*4882a593Smuzhiyun "size", "Limit the maximum size of the output file", parse_output_max_size),
2587*4882a593Smuzhiyun OPT_UINTEGER(0, "num-thread-synthesize",
2588*4882a593Smuzhiyun &record.opts.nr_threads_synthesize,
2589*4882a593Smuzhiyun "number of threads to run for event synthesis"),
2590*4882a593Smuzhiyun #ifdef HAVE_LIBPFM
2591*4882a593Smuzhiyun OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
2592*4882a593Smuzhiyun "libpfm4 event selector. use 'perf list' to list available events",
2593*4882a593Smuzhiyun parse_libpfm_events_option),
2594*4882a593Smuzhiyun #endif
2595*4882a593Smuzhiyun OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
2596*4882a593Smuzhiyun "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n"
2597*4882a593Smuzhiyun "\t\t\t 'snapshot': AUX area tracing snapshot).\n"
2598*4882a593Smuzhiyun "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
2599*4882a593Smuzhiyun "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
2600*4882a593Smuzhiyun parse_control_option),
2601*4882a593Smuzhiyun OPT_END()
2602*4882a593Smuzhiyun };
2603*4882a593Smuzhiyun
2604*4882a593Smuzhiyun struct option *record_options = __record_options;
2605*4882a593Smuzhiyun
cmd_record(int argc,const char ** argv)2606*4882a593Smuzhiyun int cmd_record(int argc, const char **argv)
2607*4882a593Smuzhiyun {
2608*4882a593Smuzhiyun int err;
2609*4882a593Smuzhiyun struct record *rec = &record;
2610*4882a593Smuzhiyun char errbuf[BUFSIZ];
2611*4882a593Smuzhiyun
2612*4882a593Smuzhiyun setlocale(LC_ALL, "");
2613*4882a593Smuzhiyun
2614*4882a593Smuzhiyun #ifndef HAVE_LIBBPF_SUPPORT
2615*4882a593Smuzhiyun # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
2616*4882a593Smuzhiyun set_nobuild('\0', "clang-path", true);
2617*4882a593Smuzhiyun set_nobuild('\0', "clang-opt", true);
2618*4882a593Smuzhiyun # undef set_nobuild
2619*4882a593Smuzhiyun #endif
2620*4882a593Smuzhiyun
2621*4882a593Smuzhiyun #ifndef HAVE_BPF_PROLOGUE
2622*4882a593Smuzhiyun # if !defined (HAVE_DWARF_SUPPORT)
2623*4882a593Smuzhiyun # define REASON "NO_DWARF=1"
2624*4882a593Smuzhiyun # elif !defined (HAVE_LIBBPF_SUPPORT)
2625*4882a593Smuzhiyun # define REASON "NO_LIBBPF=1"
2626*4882a593Smuzhiyun # else
2627*4882a593Smuzhiyun # define REASON "this architecture doesn't support BPF prologue"
2628*4882a593Smuzhiyun # endif
2629*4882a593Smuzhiyun # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
2630*4882a593Smuzhiyun set_nobuild('\0', "vmlinux", true);
2631*4882a593Smuzhiyun # undef set_nobuild
2632*4882a593Smuzhiyun # undef REASON
2633*4882a593Smuzhiyun #endif
2634*4882a593Smuzhiyun
2635*4882a593Smuzhiyun rec->opts.affinity = PERF_AFFINITY_SYS;
2636*4882a593Smuzhiyun
2637*4882a593Smuzhiyun rec->evlist = evlist__new();
2638*4882a593Smuzhiyun if (rec->evlist == NULL)
2639*4882a593Smuzhiyun return -ENOMEM;
2640*4882a593Smuzhiyun
2641*4882a593Smuzhiyun err = perf_config(perf_record_config, rec);
2642*4882a593Smuzhiyun if (err)
2643*4882a593Smuzhiyun return err;
2644*4882a593Smuzhiyun
2645*4882a593Smuzhiyun argc = parse_options(argc, argv, record_options, record_usage,
2646*4882a593Smuzhiyun PARSE_OPT_STOP_AT_NON_OPTION);
2647*4882a593Smuzhiyun if (quiet)
2648*4882a593Smuzhiyun perf_quiet_option();
2649*4882a593Smuzhiyun
2650*4882a593Smuzhiyun /* Make system wide (-a) the default target. */
2651*4882a593Smuzhiyun if (!argc && target__none(&rec->opts.target))
2652*4882a593Smuzhiyun rec->opts.target.system_wide = true;
2653*4882a593Smuzhiyun
2654*4882a593Smuzhiyun if (nr_cgroups && !rec->opts.target.system_wide) {
2655*4882a593Smuzhiyun usage_with_options_msg(record_usage, record_options,
2656*4882a593Smuzhiyun "cgroup monitoring only available in system-wide mode");
2657*4882a593Smuzhiyun
2658*4882a593Smuzhiyun }
2659*4882a593Smuzhiyun
2660*4882a593Smuzhiyun if (rec->opts.kcore)
2661*4882a593Smuzhiyun rec->data.is_dir = true;
2662*4882a593Smuzhiyun
2663*4882a593Smuzhiyun if (rec->opts.comp_level != 0) {
2664*4882a593Smuzhiyun pr_debug("Compression enabled, disabling build id collection at the end of the session.\n");
2665*4882a593Smuzhiyun rec->no_buildid = true;
2666*4882a593Smuzhiyun }
2667*4882a593Smuzhiyun
2668*4882a593Smuzhiyun if (rec->opts.record_switch_events &&
2669*4882a593Smuzhiyun !perf_can_record_switch_events()) {
2670*4882a593Smuzhiyun ui__error("kernel does not support recording context switch events\n");
2671*4882a593Smuzhiyun parse_options_usage(record_usage, record_options, "switch-events", 0);
2672*4882a593Smuzhiyun err = -EINVAL;
2673*4882a593Smuzhiyun goto out_opts;
2674*4882a593Smuzhiyun }
2675*4882a593Smuzhiyun
2676*4882a593Smuzhiyun if (switch_output_setup(rec)) {
2677*4882a593Smuzhiyun parse_options_usage(record_usage, record_options, "switch-output", 0);
2678*4882a593Smuzhiyun err = -EINVAL;
2679*4882a593Smuzhiyun goto out_opts;
2680*4882a593Smuzhiyun }
2681*4882a593Smuzhiyun
2682*4882a593Smuzhiyun if (rec->switch_output.time) {
2683*4882a593Smuzhiyun signal(SIGALRM, alarm_sig_handler);
2684*4882a593Smuzhiyun alarm(rec->switch_output.time);
2685*4882a593Smuzhiyun }
2686*4882a593Smuzhiyun
2687*4882a593Smuzhiyun if (rec->switch_output.num_files) {
2688*4882a593Smuzhiyun rec->switch_output.filenames = calloc(sizeof(char *),
2689*4882a593Smuzhiyun rec->switch_output.num_files);
2690*4882a593Smuzhiyun if (!rec->switch_output.filenames) {
2691*4882a593Smuzhiyun err = -EINVAL;
2692*4882a593Smuzhiyun goto out_opts;
2693*4882a593Smuzhiyun }
2694*4882a593Smuzhiyun }
2695*4882a593Smuzhiyun
2696*4882a593Smuzhiyun /*
2697*4882a593Smuzhiyun * Allow aliases to facilitate the lookup of symbols for address
2698*4882a593Smuzhiyun * filters. Refer to auxtrace_parse_filters().
2699*4882a593Smuzhiyun */
2700*4882a593Smuzhiyun symbol_conf.allow_aliases = true;
2701*4882a593Smuzhiyun
2702*4882a593Smuzhiyun symbol__init(NULL);
2703*4882a593Smuzhiyun
2704*4882a593Smuzhiyun if (rec->opts.affinity != PERF_AFFINITY_SYS) {
2705*4882a593Smuzhiyun rec->affinity_mask.nbits = cpu__max_cpu();
2706*4882a593Smuzhiyun rec->affinity_mask.bits = bitmap_alloc(rec->affinity_mask.nbits);
2707*4882a593Smuzhiyun if (!rec->affinity_mask.bits) {
2708*4882a593Smuzhiyun pr_err("Failed to allocate thread mask for %zd cpus\n", rec->affinity_mask.nbits);
2709*4882a593Smuzhiyun err = -ENOMEM;
2710*4882a593Smuzhiyun goto out_opts;
2711*4882a593Smuzhiyun }
2712*4882a593Smuzhiyun pr_debug2("thread mask[%zd]: empty\n", rec->affinity_mask.nbits);
2713*4882a593Smuzhiyun }
2714*4882a593Smuzhiyun
2715*4882a593Smuzhiyun err = record__auxtrace_init(rec);
2716*4882a593Smuzhiyun if (err)
2717*4882a593Smuzhiyun goto out;
2718*4882a593Smuzhiyun
2719*4882a593Smuzhiyun if (dry_run)
2720*4882a593Smuzhiyun goto out;
2721*4882a593Smuzhiyun
2722*4882a593Smuzhiyun err = bpf__setup_stdout(rec->evlist);
2723*4882a593Smuzhiyun if (err) {
2724*4882a593Smuzhiyun bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
2725*4882a593Smuzhiyun pr_err("ERROR: Setup BPF stdout failed: %s\n",
2726*4882a593Smuzhiyun errbuf);
2727*4882a593Smuzhiyun goto out;
2728*4882a593Smuzhiyun }
2729*4882a593Smuzhiyun
2730*4882a593Smuzhiyun err = -ENOMEM;
2731*4882a593Smuzhiyun
2732*4882a593Smuzhiyun if (rec->no_buildid_cache || rec->no_buildid) {
2733*4882a593Smuzhiyun disable_buildid_cache();
2734*4882a593Smuzhiyun } else if (rec->switch_output.enabled) {
2735*4882a593Smuzhiyun /*
2736*4882a593Smuzhiyun * In 'perf record --switch-output', disable buildid
2737*4882a593Smuzhiyun * generation by default to reduce data file switching
2738*4882a593Smuzhiyun * overhead. Still generate buildid if they are required
2739*4882a593Smuzhiyun * explicitly using
2740*4882a593Smuzhiyun *
2741*4882a593Smuzhiyun * perf record --switch-output --no-no-buildid \
2742*4882a593Smuzhiyun * --no-no-buildid-cache
2743*4882a593Smuzhiyun *
2744*4882a593Smuzhiyun * Following code equals to:
2745*4882a593Smuzhiyun *
2746*4882a593Smuzhiyun * if ((rec->no_buildid || !rec->no_buildid_set) &&
2747*4882a593Smuzhiyun * (rec->no_buildid_cache || !rec->no_buildid_cache_set))
2748*4882a593Smuzhiyun * disable_buildid_cache();
2749*4882a593Smuzhiyun */
2750*4882a593Smuzhiyun bool disable = true;
2751*4882a593Smuzhiyun
2752*4882a593Smuzhiyun if (rec->no_buildid_set && !rec->no_buildid)
2753*4882a593Smuzhiyun disable = false;
2754*4882a593Smuzhiyun if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
2755*4882a593Smuzhiyun disable = false;
2756*4882a593Smuzhiyun if (disable) {
2757*4882a593Smuzhiyun rec->no_buildid = true;
2758*4882a593Smuzhiyun rec->no_buildid_cache = true;
2759*4882a593Smuzhiyun disable_buildid_cache();
2760*4882a593Smuzhiyun }
2761*4882a593Smuzhiyun }
2762*4882a593Smuzhiyun
2763*4882a593Smuzhiyun if (record.opts.overwrite)
2764*4882a593Smuzhiyun record.opts.tail_synthesize = true;
2765*4882a593Smuzhiyun
2766*4882a593Smuzhiyun if (rec->evlist->core.nr_entries == 0 &&
2767*4882a593Smuzhiyun __evlist__add_default(rec->evlist, !record.opts.no_samples) < 0) {
2768*4882a593Smuzhiyun pr_err("Not enough memory for event selector list\n");
2769*4882a593Smuzhiyun goto out;
2770*4882a593Smuzhiyun }
2771*4882a593Smuzhiyun
2772*4882a593Smuzhiyun if (rec->opts.target.tid && !rec->opts.no_inherit_set)
2773*4882a593Smuzhiyun rec->opts.no_inherit = true;
2774*4882a593Smuzhiyun
2775*4882a593Smuzhiyun err = target__validate(&rec->opts.target);
2776*4882a593Smuzhiyun if (err) {
2777*4882a593Smuzhiyun target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
2778*4882a593Smuzhiyun ui__warning("%s\n", errbuf);
2779*4882a593Smuzhiyun }
2780*4882a593Smuzhiyun
2781*4882a593Smuzhiyun err = target__parse_uid(&rec->opts.target);
2782*4882a593Smuzhiyun if (err) {
2783*4882a593Smuzhiyun int saved_errno = errno;
2784*4882a593Smuzhiyun
2785*4882a593Smuzhiyun target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
2786*4882a593Smuzhiyun ui__error("%s", errbuf);
2787*4882a593Smuzhiyun
2788*4882a593Smuzhiyun err = -saved_errno;
2789*4882a593Smuzhiyun goto out;
2790*4882a593Smuzhiyun }
2791*4882a593Smuzhiyun
2792*4882a593Smuzhiyun /* Enable ignoring missing threads when -u/-p option is defined. */
2793*4882a593Smuzhiyun rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid;
2794*4882a593Smuzhiyun
2795*4882a593Smuzhiyun err = -ENOMEM;
2796*4882a593Smuzhiyun if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0)
2797*4882a593Smuzhiyun usage_with_options(record_usage, record_options);
2798*4882a593Smuzhiyun
2799*4882a593Smuzhiyun err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
2800*4882a593Smuzhiyun if (err)
2801*4882a593Smuzhiyun goto out;
2802*4882a593Smuzhiyun
2803*4882a593Smuzhiyun /*
2804*4882a593Smuzhiyun * We take all buildids when the file contains
2805*4882a593Smuzhiyun * AUX area tracing data because we do not decode the
2806*4882a593Smuzhiyun * trace because it would take too long.
2807*4882a593Smuzhiyun */
2808*4882a593Smuzhiyun if (rec->opts.full_auxtrace)
2809*4882a593Smuzhiyun rec->buildid_all = true;
2810*4882a593Smuzhiyun
2811*4882a593Smuzhiyun if (rec->opts.text_poke) {
2812*4882a593Smuzhiyun err = record__config_text_poke(rec->evlist);
2813*4882a593Smuzhiyun if (err) {
2814*4882a593Smuzhiyun pr_err("record__config_text_poke failed, error %d\n", err);
2815*4882a593Smuzhiyun goto out;
2816*4882a593Smuzhiyun }
2817*4882a593Smuzhiyun }
2818*4882a593Smuzhiyun
2819*4882a593Smuzhiyun if (record_opts__config(&rec->opts)) {
2820*4882a593Smuzhiyun err = -EINVAL;
2821*4882a593Smuzhiyun goto out;
2822*4882a593Smuzhiyun }
2823*4882a593Smuzhiyun
2824*4882a593Smuzhiyun if (rec->opts.nr_cblocks > nr_cblocks_max)
2825*4882a593Smuzhiyun rec->opts.nr_cblocks = nr_cblocks_max;
2826*4882a593Smuzhiyun pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks);
2827*4882a593Smuzhiyun
2828*4882a593Smuzhiyun pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]);
2829*4882a593Smuzhiyun pr_debug("mmap flush: %d\n", rec->opts.mmap_flush);
2830*4882a593Smuzhiyun
2831*4882a593Smuzhiyun if (rec->opts.comp_level > comp_level_max)
2832*4882a593Smuzhiyun rec->opts.comp_level = comp_level_max;
2833*4882a593Smuzhiyun pr_debug("comp level: %d\n", rec->opts.comp_level);
2834*4882a593Smuzhiyun
2835*4882a593Smuzhiyun err = __cmd_record(&record, argc, argv);
2836*4882a593Smuzhiyun out:
2837*4882a593Smuzhiyun bitmap_free(rec->affinity_mask.bits);
2838*4882a593Smuzhiyun evlist__delete(rec->evlist);
2839*4882a593Smuzhiyun symbol__exit();
2840*4882a593Smuzhiyun auxtrace_record__free(rec->itr);
2841*4882a593Smuzhiyun out_opts:
2842*4882a593Smuzhiyun evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close);
2843*4882a593Smuzhiyun return err;
2844*4882a593Smuzhiyun }
2845*4882a593Smuzhiyun
snapshot_sig_handler(int sig __maybe_unused)2846*4882a593Smuzhiyun static void snapshot_sig_handler(int sig __maybe_unused)
2847*4882a593Smuzhiyun {
2848*4882a593Smuzhiyun struct record *rec = &record;
2849*4882a593Smuzhiyun
2850*4882a593Smuzhiyun hit_auxtrace_snapshot_trigger(rec);
2851*4882a593Smuzhiyun
2852*4882a593Smuzhiyun if (switch_output_signal(rec))
2853*4882a593Smuzhiyun trigger_hit(&switch_output_trigger);
2854*4882a593Smuzhiyun }
2855*4882a593Smuzhiyun
alarm_sig_handler(int sig __maybe_unused)2856*4882a593Smuzhiyun static void alarm_sig_handler(int sig __maybe_unused)
2857*4882a593Smuzhiyun {
2858*4882a593Smuzhiyun struct record *rec = &record;
2859*4882a593Smuzhiyun
2860*4882a593Smuzhiyun if (switch_output_time(rec))
2861*4882a593Smuzhiyun trigger_hit(&switch_output_trigger);
2862*4882a593Smuzhiyun }
2863