1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 286470930SIngo Molnar /* 386470930SIngo Molnar * builtin-record.c 486470930SIngo Molnar * 586470930SIngo Molnar * Builtin record command: Record the profile of a workload 686470930SIngo Molnar * (or a CPU, or a PID) into the perf.data output file - for 786470930SIngo Molnar * later analysis via perf report. 886470930SIngo Molnar */ 986470930SIngo Molnar #include "builtin.h" 1086470930SIngo Molnar 116122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h" 124b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1386470930SIngo Molnar #include "util/parse-events.h" 1441840d21STaeung Song #include "util/config.h" 1586470930SIngo Molnar 168f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h" 17f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h" 187c6a1c65SPeter Zijlstra #include "util/header.h" 1966e274f3SFrederic Weisbecker #include "util/event.h" 20361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h" 2169aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 228f28827aSFrederic Weisbecker #include "util/debug.h" 23e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 24aeb00b1aSArnaldo Carvalho de Melo #include "util/target.h" 2594c744b6SArnaldo Carvalho de Melo #include "util/session.h" 2645694aa7SArnaldo Carvalho de Melo #include "util/tool.h" 278d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 28aeb00b1aSArnaldo Carvalho de Melo #include "util/record.h" 29a12b51c4SPaul Mackerras #include "util/cpumap.h" 30fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h" 31f5fc1412SJiri Olsa #include "util/data.h" 32bcc84ec6SStephane Eranian #include "util/perf_regs.h" 33ef149c25SAdrian Hunter #include "util/auxtrace.h" 3446bc29b9SAdrian Hunter #include "util/tsc.h" 35f00898f4SAndi Kleen #include "util/parse-branch-options.h" 36bcc84ec6SStephane Eranian #include "util/parse-regs-options.h" 3740c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h" 3871dc2326SWang Nan #include "util/llvm-utils.h" 398690a2a7SWang Nan #include "util/bpf-loader.h" 405f9cf599SWang Nan #include "util/trigger.h" 41a074865eSWang Nan #include "util/perf-hooks.h" 42f13de660SAlexey Budankov #include "util/cpu-set-sched.h" 43ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h" 44c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h" 4558db1d6eSArnaldo Carvalho de Melo #include "util/units.h" 467b612e29SSong Liu #include "util/bpf-event.h" 47d99c22eaSStephane Eranian #include "util/util.h" 4870943490SStephane Eranian #include "util/pfm.h" 496953beb4SJiri Olsa #include "util/clockid.h" 50b53a0755SJin Yao #include "util/pmu-hybrid.h" 51b53a0755SJin Yao #include "util/evlist-hybrid.h" 52d8871ea7SWang Nan #include "asm/bug.h" 53c1a604dfSArnaldo Carvalho de Melo #include "perf.h" 547c6a1c65SPeter Zijlstra 55a43783aeSArnaldo Carvalho de Melo #include <errno.h> 56fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 5767230479SArnaldo Carvalho de Melo #include <locale.h> 584208735dSArnaldo Carvalho de Melo #include <poll.h> 59d99c22eaSStephane Eranian #include <pthread.h> 6086470930SIngo Molnar #include <unistd.h> 61415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID 62415ccb58SAlexey Bayduraev #include <syscall.h> 63415ccb58SAlexey Bayduraev #endif 6486470930SIngo Molnar #include <sched.h> 659607ad3aSArnaldo Carvalho de Melo #include <signal.h> 66da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 67da231338SAnand K Mistry #include <sys/eventfd.h> 68da231338SAnand K Mistry #endif 69a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 704208735dSArnaldo Carvalho de Melo #include <sys/wait.h> 71eeb399b5SAdrian Hunter #include <sys/types.h> 72eeb399b5SAdrian Hunter #include <sys/stat.h> 73eeb399b5SAdrian Hunter #include <fcntl.h> 746ef81c55SMamatha Inamdar #include <linux/err.h> 758520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 760693e680SArnaldo Carvalho de Melo #include <linux/time64.h> 77d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h> 788384a260SAlexey Budankov #include <linux/bitmap.h> 79d1e325cfSJiri Olsa #include <sys/time.h> 8078da39faSBernhard Rosenkraenzer 811b43b704SJiri Olsa struct switch_output { 82dc0c6127SJiri Olsa bool enabled; 831b43b704SJiri Olsa bool signal; 84dc0c6127SJiri Olsa unsigned long size; 85bfacbe3bSJiri Olsa unsigned long time; 86cb4e1ebbSJiri Olsa const char *str; 87cb4e1ebbSJiri Olsa bool set; 8803724b2eSAndi Kleen char **filenames; 8903724b2eSAndi Kleen int num_files; 9003724b2eSAndi Kleen int cur_file; 911b43b704SJiri Olsa }; 921b43b704SJiri Olsa 937954f716SAlexey Bayduraev struct thread_mask { 947954f716SAlexey Bayduraev struct mmap_cpu_mask maps; 957954f716SAlexey Bayduraev struct mmap_cpu_mask affinity; 967954f716SAlexey Bayduraev }; 977954f716SAlexey Bayduraev 98415ccb58SAlexey Bayduraev struct record_thread { 99415ccb58SAlexey Bayduraev pid_t tid; 100415ccb58SAlexey Bayduraev struct thread_mask *mask; 101415ccb58SAlexey Bayduraev struct { 102415ccb58SAlexey Bayduraev int msg[2]; 103415ccb58SAlexey Bayduraev int ack[2]; 104415ccb58SAlexey Bayduraev } pipes; 105415ccb58SAlexey Bayduraev struct fdarray pollfd; 106415ccb58SAlexey Bayduraev int ctlfd_pos; 107415ccb58SAlexey Bayduraev int nr_mmaps; 108415ccb58SAlexey Bayduraev struct mmap **maps; 109415ccb58SAlexey Bayduraev struct mmap **overwrite_maps; 110415ccb58SAlexey Bayduraev struct record *rec; 111396b626bSAlexey Bayduraev unsigned long long samples; 112396b626bSAlexey Bayduraev unsigned long waking; 113*ae9c7242SAlexey Bayduraev u64 bytes_written; 114415ccb58SAlexey Bayduraev }; 115415ccb58SAlexey Bayduraev 116396b626bSAlexey Bayduraev static __thread struct record_thread *thread; 117396b626bSAlexey Bayduraev 1181e5de7d9SAlexey Bayduraev enum thread_msg { 1191e5de7d9SAlexey Bayduraev THREAD_MSG__UNDEFINED = 0, 1201e5de7d9SAlexey Bayduraev THREAD_MSG__READY, 1211e5de7d9SAlexey Bayduraev THREAD_MSG__MAX, 1221e5de7d9SAlexey Bayduraev }; 1231e5de7d9SAlexey Bayduraev 1241e5de7d9SAlexey Bayduraev static const char *thread_msg_tags[THREAD_MSG__MAX] = { 1251e5de7d9SAlexey Bayduraev "UNDEFINED", "READY" 1261e5de7d9SAlexey Bayduraev }; 1271e5de7d9SAlexey Bayduraev 1288c6f45a7SArnaldo Carvalho de Melo struct record { 12945694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 130b4006796SArnaldo Carvalho de Melo struct record_opts opts; 131d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 1328ceb41d7SJiri Olsa struct perf_data data; 133ef149c25SAdrian Hunter struct auxtrace_record *itr; 13463503dbaSJiri Olsa struct evlist *evlist; 135d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 136bc477d79SArnaldo Carvalho de Melo struct evlist *sb_evlist; 137899e5ffbSArnaldo Carvalho de Melo pthread_t thread_id; 138d20deb64SArnaldo Carvalho de Melo int realtime_prio; 139899e5ffbSArnaldo Carvalho de Melo bool switch_output_event_set; 140d20deb64SArnaldo Carvalho de Melo bool no_buildid; 141d2db9a98SWang Nan bool no_buildid_set; 142d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 143d2db9a98SWang Nan bool no_buildid_cache_set; 1446156681bSNamhyung Kim bool buildid_all; 145e29386c8SJiri Olsa bool buildid_mmap; 146ecfd7a9cSWang Nan bool timestamp_filename; 14768588bafSJin Yao bool timestamp_boundary; 1481b43b704SJiri Olsa struct switch_output switch_output; 1499f065194SYang Shi unsigned long long samples; 1506d575816SJiwei Sun unsigned long output_max_size; /* = 0: unlimited */ 1519bce13eaSJiri Olsa struct perf_debuginfod debuginfod; 1527954f716SAlexey Bayduraev int nr_threads; 1537954f716SAlexey Bayduraev struct thread_mask *thread_masks; 154415ccb58SAlexey Bayduraev struct record_thread *thread_data; 1550f82ebc4SArnaldo Carvalho de Melo }; 15686470930SIngo Molnar 1576d575816SJiwei Sun static volatile int done; 1586d575816SJiwei Sun 159dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 160dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 161dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 162dc0c6127SJiri Olsa 1639d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = { 1649d2ed645SAlexey Budankov "SYS", "NODE", "CPU" 1659d2ed645SAlexey Budankov }; 1669d2ed645SAlexey Budankov 167415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID 168415ccb58SAlexey Bayduraev static inline pid_t gettid(void) 169415ccb58SAlexey Bayduraev { 170415ccb58SAlexey Bayduraev return (pid_t)syscall(__NR_gettid); 171415ccb58SAlexey Bayduraev } 172415ccb58SAlexey Bayduraev #endif 173415ccb58SAlexey Bayduraev 1743217e9feSAlexey Bayduraev static int record__threads_enabled(struct record *rec) 1753217e9feSAlexey Bayduraev { 1763217e9feSAlexey Bayduraev return rec->opts.threads_spec; 1773217e9feSAlexey Bayduraev } 1783217e9feSAlexey Bayduraev 179dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 180dc0c6127SJiri Olsa { 181dc0c6127SJiri Olsa return rec->switch_output.signal && 182dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 183dc0c6127SJiri Olsa } 184dc0c6127SJiri Olsa 185dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 186dc0c6127SJiri Olsa { 187dc0c6127SJiri Olsa return rec->switch_output.size && 188dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 189dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 190dc0c6127SJiri Olsa } 191dc0c6127SJiri Olsa 192bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 193bfacbe3bSJiri Olsa { 194bfacbe3bSJiri Olsa return rec->switch_output.time && 195bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 196bfacbe3bSJiri Olsa } 197bfacbe3bSJiri Olsa 198*ae9c7242SAlexey Bayduraev static u64 record__bytes_written(struct record *rec) 199*ae9c7242SAlexey Bayduraev { 200*ae9c7242SAlexey Bayduraev int t; 201*ae9c7242SAlexey Bayduraev u64 bytes_written = rec->bytes_written; 202*ae9c7242SAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 203*ae9c7242SAlexey Bayduraev 204*ae9c7242SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 205*ae9c7242SAlexey Bayduraev bytes_written += thread_data[t].bytes_written; 206*ae9c7242SAlexey Bayduraev 207*ae9c7242SAlexey Bayduraev return bytes_written; 208*ae9c7242SAlexey Bayduraev } 209*ae9c7242SAlexey Bayduraev 2106d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec) 2116d575816SJiwei Sun { 2126d575816SJiwei Sun return rec->output_max_size && 213*ae9c7242SAlexey Bayduraev (record__bytes_written(rec) >= rec->output_max_size); 2146d575816SJiwei Sun } 2156d575816SJiwei Sun 216a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused, 217ded2b8feSJiri Olsa void *bf, size_t size) 218f5970550SPeter Zijlstra { 219ded2b8feSJiri Olsa struct perf_data_file *file = &rec->session->data->file; 220ded2b8feSJiri Olsa 22156f735ffSAlexey Bayduraev if (map && map->file) 22256f735ffSAlexey Bayduraev file = map->file; 22356f735ffSAlexey Bayduraev 224ded2b8feSJiri Olsa if (perf_data_file__write(file, bf, size) < 0) { 2254f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 2268d3eca20SDavid Ahern return -1; 2278d3eca20SDavid Ahern } 228f5970550SPeter Zijlstra 229*ae9c7242SAlexey Bayduraev if (map && map->file) 230*ae9c7242SAlexey Bayduraev thread->bytes_written += size; 231*ae9c7242SAlexey Bayduraev else 232cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 233dc0c6127SJiri Olsa 2346d575816SJiwei Sun if (record__output_max_size_exceeded(rec) && !done) { 2356d575816SJiwei Sun fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB)," 2366d575816SJiwei Sun " stopping session ]\n", 237*ae9c7242SAlexey Bayduraev record__bytes_written(rec) >> 10); 2386d575816SJiwei Sun done = 1; 2396d575816SJiwei Sun } 2406d575816SJiwei Sun 241dc0c6127SJiri Olsa if (switch_output_size(rec)) 242dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 243dc0c6127SJiri Olsa 2448d3eca20SDavid Ahern return 0; 245f5970550SPeter Zijlstra } 246f5970550SPeter Zijlstra 247ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec); 248ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec); 2495d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size, 2505d7f4116SAlexey Budankov void *src, size_t src_size); 2515d7f4116SAlexey Budankov 252d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 253d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd, 254d3d1af6fSAlexey Budankov void *buf, size_t size, off_t off) 255d3d1af6fSAlexey Budankov { 256d3d1af6fSAlexey Budankov int rc; 257d3d1af6fSAlexey Budankov 258d3d1af6fSAlexey Budankov cblock->aio_fildes = trace_fd; 259d3d1af6fSAlexey Budankov cblock->aio_buf = buf; 260d3d1af6fSAlexey Budankov cblock->aio_nbytes = size; 261d3d1af6fSAlexey Budankov cblock->aio_offset = off; 262d3d1af6fSAlexey Budankov cblock->aio_sigevent.sigev_notify = SIGEV_NONE; 263d3d1af6fSAlexey Budankov 264d3d1af6fSAlexey Budankov do { 265d3d1af6fSAlexey Budankov rc = aio_write(cblock); 266d3d1af6fSAlexey Budankov if (rc == 0) { 267d3d1af6fSAlexey Budankov break; 268d3d1af6fSAlexey Budankov } else if (errno != EAGAIN) { 269d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 270d3d1af6fSAlexey Budankov pr_err("failed to queue perf data, error: %m\n"); 271d3d1af6fSAlexey Budankov break; 272d3d1af6fSAlexey Budankov } 273d3d1af6fSAlexey Budankov } while (1); 274d3d1af6fSAlexey Budankov 275d3d1af6fSAlexey Budankov return rc; 276d3d1af6fSAlexey Budankov } 277d3d1af6fSAlexey Budankov 278a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock) 279d3d1af6fSAlexey Budankov { 280d3d1af6fSAlexey Budankov void *rem_buf; 281d3d1af6fSAlexey Budankov off_t rem_off; 282d3d1af6fSAlexey Budankov size_t rem_size; 283d3d1af6fSAlexey Budankov int rc, aio_errno; 284d3d1af6fSAlexey Budankov ssize_t aio_ret, written; 285d3d1af6fSAlexey Budankov 286d3d1af6fSAlexey Budankov aio_errno = aio_error(cblock); 287d3d1af6fSAlexey Budankov if (aio_errno == EINPROGRESS) 288d3d1af6fSAlexey Budankov return 0; 289d3d1af6fSAlexey Budankov 290d3d1af6fSAlexey Budankov written = aio_ret = aio_return(cblock); 291d3d1af6fSAlexey Budankov if (aio_ret < 0) { 292d3d1af6fSAlexey Budankov if (aio_errno != EINTR) 293d3d1af6fSAlexey Budankov pr_err("failed to write perf data, error: %m\n"); 294d3d1af6fSAlexey Budankov written = 0; 295d3d1af6fSAlexey Budankov } 296d3d1af6fSAlexey Budankov 297d3d1af6fSAlexey Budankov rem_size = cblock->aio_nbytes - written; 298d3d1af6fSAlexey Budankov 299d3d1af6fSAlexey Budankov if (rem_size == 0) { 300d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 301d3d1af6fSAlexey Budankov /* 302ef781128SAlexey Budankov * md->refcount is incremented in record__aio_pushfn() for 303ef781128SAlexey Budankov * every aio write request started in record__aio_push() so 304ef781128SAlexey Budankov * decrement it because the request is now complete. 305d3d1af6fSAlexey Budankov */ 30680e53d11SJiri Olsa perf_mmap__put(&md->core); 307d3d1af6fSAlexey Budankov rc = 1; 308d3d1af6fSAlexey Budankov } else { 309d3d1af6fSAlexey Budankov /* 310d3d1af6fSAlexey Budankov * aio write request may require restart with the 311d3d1af6fSAlexey Budankov * reminder if the kernel didn't write whole 312d3d1af6fSAlexey Budankov * chunk at once. 313d3d1af6fSAlexey Budankov */ 314d3d1af6fSAlexey Budankov rem_off = cblock->aio_offset + written; 315d3d1af6fSAlexey Budankov rem_buf = (void *)(cblock->aio_buf + written); 316d3d1af6fSAlexey Budankov record__aio_write(cblock, cblock->aio_fildes, 317d3d1af6fSAlexey Budankov rem_buf, rem_size, rem_off); 318d3d1af6fSAlexey Budankov rc = 0; 319d3d1af6fSAlexey Budankov } 320d3d1af6fSAlexey Budankov 321d3d1af6fSAlexey Budankov return rc; 322d3d1af6fSAlexey Budankov } 323d3d1af6fSAlexey Budankov 324a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all) 325d3d1af6fSAlexey Budankov { 32693f20c0fSAlexey Budankov struct aiocb **aiocb = md->aio.aiocb; 32793f20c0fSAlexey Budankov struct aiocb *cblocks = md->aio.cblocks; 328d3d1af6fSAlexey Budankov struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */ 32993f20c0fSAlexey Budankov int i, do_suspend; 330d3d1af6fSAlexey Budankov 331d3d1af6fSAlexey Budankov do { 33293f20c0fSAlexey Budankov do_suspend = 0; 33393f20c0fSAlexey Budankov for (i = 0; i < md->aio.nr_cblocks; ++i) { 33493f20c0fSAlexey Budankov if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) { 33593f20c0fSAlexey Budankov if (sync_all) 33693f20c0fSAlexey Budankov aiocb[i] = NULL; 33793f20c0fSAlexey Budankov else 33893f20c0fSAlexey Budankov return i; 33993f20c0fSAlexey Budankov } else { 34093f20c0fSAlexey Budankov /* 34193f20c0fSAlexey Budankov * Started aio write is not complete yet 34293f20c0fSAlexey Budankov * so it has to be waited before the 34393f20c0fSAlexey Budankov * next allocation. 34493f20c0fSAlexey Budankov */ 34593f20c0fSAlexey Budankov aiocb[i] = &cblocks[i]; 34693f20c0fSAlexey Budankov do_suspend = 1; 34793f20c0fSAlexey Budankov } 34893f20c0fSAlexey Budankov } 34993f20c0fSAlexey Budankov if (!do_suspend) 35093f20c0fSAlexey Budankov return -1; 351d3d1af6fSAlexey Budankov 35293f20c0fSAlexey Budankov while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) { 353d3d1af6fSAlexey Budankov if (!(errno == EAGAIN || errno == EINTR)) 354d3d1af6fSAlexey Budankov pr_err("failed to sync perf data, error: %m\n"); 355d3d1af6fSAlexey Budankov } 356d3d1af6fSAlexey Budankov } while (1); 357d3d1af6fSAlexey Budankov } 358d3d1af6fSAlexey Budankov 359ef781128SAlexey Budankov struct record_aio { 360ef781128SAlexey Budankov struct record *rec; 361ef781128SAlexey Budankov void *data; 362ef781128SAlexey Budankov size_t size; 363ef781128SAlexey Budankov }; 364ef781128SAlexey Budankov 365a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size) 366d3d1af6fSAlexey Budankov { 367ef781128SAlexey Budankov struct record_aio *aio = to; 368ef781128SAlexey Budankov 369ef781128SAlexey Budankov /* 370547740f7SJiri Olsa * map->core.base data pointed by buf is copied into free map->aio.data[] buffer 371ef781128SAlexey Budankov * to release space in the kernel buffer as fast as possible, calling 372ef781128SAlexey Budankov * perf_mmap__consume() from perf_mmap__push() function. 373ef781128SAlexey Budankov * 374ef781128SAlexey Budankov * That lets the kernel to proceed with storing more profiling data into 375ef781128SAlexey Budankov * the kernel buffer earlier than other per-cpu kernel buffers are handled. 376ef781128SAlexey Budankov * 377ef781128SAlexey Budankov * Coping can be done in two steps in case the chunk of profiling data 378ef781128SAlexey Budankov * crosses the upper bound of the kernel buffer. In this case we first move 379ef781128SAlexey Budankov * part of data from map->start till the upper bound and then the reminder 380ef781128SAlexey Budankov * from the beginning of the kernel buffer till the end of the data chunk. 381ef781128SAlexey Budankov */ 382ef781128SAlexey Budankov 383ef781128SAlexey Budankov if (record__comp_enabled(aio->rec)) { 384ef781128SAlexey Budankov size = zstd_compress(aio->rec->session, aio->data + aio->size, 385bf59b305SJiri Olsa mmap__mmap_len(map) - aio->size, 386ef781128SAlexey Budankov buf, size); 387ef781128SAlexey Budankov } else { 388ef781128SAlexey Budankov memcpy(aio->data + aio->size, buf, size); 389ef781128SAlexey Budankov } 390ef781128SAlexey Budankov 391ef781128SAlexey Budankov if (!aio->size) { 392ef781128SAlexey Budankov /* 393ef781128SAlexey Budankov * Increment map->refcount to guard map->aio.data[] buffer 394ef781128SAlexey Budankov * from premature deallocation because map object can be 395ef781128SAlexey Budankov * released earlier than aio write request started on 396ef781128SAlexey Budankov * map->aio.data[] buffer is complete. 397ef781128SAlexey Budankov * 398ef781128SAlexey Budankov * perf_mmap__put() is done at record__aio_complete() 399ef781128SAlexey Budankov * after started aio request completion or at record__aio_push() 400ef781128SAlexey Budankov * if the request failed to start. 401ef781128SAlexey Budankov */ 402e75710f0SJiri Olsa perf_mmap__get(&map->core); 403ef781128SAlexey Budankov } 404ef781128SAlexey Budankov 405ef781128SAlexey Budankov aio->size += size; 406ef781128SAlexey Budankov 407ef781128SAlexey Budankov return size; 408ef781128SAlexey Budankov } 409ef781128SAlexey Budankov 410a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off) 411ef781128SAlexey Budankov { 412ef781128SAlexey Budankov int ret, idx; 413ef781128SAlexey Budankov int trace_fd = rec->session->data->file.fd; 414ef781128SAlexey Budankov struct record_aio aio = { .rec = rec, .size = 0 }; 415ef781128SAlexey Budankov 416ef781128SAlexey Budankov /* 417ef781128SAlexey Budankov * Call record__aio_sync() to wait till map->aio.data[] buffer 418ef781128SAlexey Budankov * becomes available after previous aio write operation. 419ef781128SAlexey Budankov */ 420ef781128SAlexey Budankov 421ef781128SAlexey Budankov idx = record__aio_sync(map, false); 422ef781128SAlexey Budankov aio.data = map->aio.data[idx]; 423ef781128SAlexey Budankov ret = perf_mmap__push(map, &aio, record__aio_pushfn); 424ef781128SAlexey Budankov if (ret != 0) /* ret > 0 - no data, ret < 0 - error */ 425ef781128SAlexey Budankov return ret; 426d3d1af6fSAlexey Budankov 427d3d1af6fSAlexey Budankov rec->samples++; 428ef781128SAlexey Budankov ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off); 429d3d1af6fSAlexey Budankov if (!ret) { 430ef781128SAlexey Budankov *off += aio.size; 431ef781128SAlexey Budankov rec->bytes_written += aio.size; 432d3d1af6fSAlexey Budankov if (switch_output_size(rec)) 433d3d1af6fSAlexey Budankov trigger_hit(&switch_output_trigger); 434ef781128SAlexey Budankov } else { 435ef781128SAlexey Budankov /* 436ef781128SAlexey Budankov * Decrement map->refcount incremented in record__aio_pushfn() 437ef781128SAlexey Budankov * back if record__aio_write() operation failed to start, otherwise 438ef781128SAlexey Budankov * map->refcount is decremented in record__aio_complete() after 439ef781128SAlexey Budankov * aio write operation finishes successfully. 440ef781128SAlexey Budankov */ 44180e53d11SJiri Olsa perf_mmap__put(&map->core); 442d3d1af6fSAlexey Budankov } 443d3d1af6fSAlexey Budankov 444d3d1af6fSAlexey Budankov return ret; 445d3d1af6fSAlexey Budankov } 446d3d1af6fSAlexey Budankov 447d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd) 448d3d1af6fSAlexey Budankov { 449d3d1af6fSAlexey Budankov return lseek(trace_fd, 0, SEEK_CUR); 450d3d1af6fSAlexey Budankov } 451d3d1af6fSAlexey Budankov 452d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos) 453d3d1af6fSAlexey Budankov { 454d3d1af6fSAlexey Budankov lseek(trace_fd, pos, SEEK_SET); 455d3d1af6fSAlexey Budankov } 456d3d1af6fSAlexey Budankov 457d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec) 458d3d1af6fSAlexey Budankov { 459d3d1af6fSAlexey Budankov int i; 46063503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 461a5830532SJiri Olsa struct mmap *maps = evlist->mmap; 462d3d1af6fSAlexey Budankov 463ef781128SAlexey Budankov if (!record__aio_enabled(rec)) 464d3d1af6fSAlexey Budankov return; 465d3d1af6fSAlexey Budankov 466c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 467a5830532SJiri Olsa struct mmap *map = &maps[i]; 468d3d1af6fSAlexey Budankov 469547740f7SJiri Olsa if (map->core.base) 47093f20c0fSAlexey Budankov record__aio_sync(map, true); 471d3d1af6fSAlexey Budankov } 472d3d1af6fSAlexey Budankov } 473d3d1af6fSAlexey Budankov 474d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1; 47593f20c0fSAlexey Budankov static int nr_cblocks_max = 4; 476d3d1af6fSAlexey Budankov 477d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt, 47893f20c0fSAlexey Budankov const char *str, 479d3d1af6fSAlexey Budankov int unset) 480d3d1af6fSAlexey Budankov { 481d3d1af6fSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 482d3d1af6fSAlexey Budankov 48393f20c0fSAlexey Budankov if (unset) { 484d3d1af6fSAlexey Budankov opts->nr_cblocks = 0; 48593f20c0fSAlexey Budankov } else { 48693f20c0fSAlexey Budankov if (str) 48793f20c0fSAlexey Budankov opts->nr_cblocks = strtol(str, NULL, 0); 48893f20c0fSAlexey Budankov if (!opts->nr_cblocks) 489d3d1af6fSAlexey Budankov opts->nr_cblocks = nr_cblocks_default; 49093f20c0fSAlexey Budankov } 491d3d1af6fSAlexey Budankov 492d3d1af6fSAlexey Budankov return 0; 493d3d1af6fSAlexey Budankov } 494d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */ 49593f20c0fSAlexey Budankov static int nr_cblocks_max = 0; 49693f20c0fSAlexey Budankov 497a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused, 498ef781128SAlexey Budankov off_t *off __maybe_unused) 499d3d1af6fSAlexey Budankov { 500d3d1af6fSAlexey Budankov return -1; 501d3d1af6fSAlexey Budankov } 502d3d1af6fSAlexey Budankov 503d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused) 504d3d1af6fSAlexey Budankov { 505d3d1af6fSAlexey Budankov return -1; 506d3d1af6fSAlexey Budankov } 507d3d1af6fSAlexey Budankov 508d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused) 509d3d1af6fSAlexey Budankov { 510d3d1af6fSAlexey Budankov } 511d3d1af6fSAlexey Budankov 512d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused) 513d3d1af6fSAlexey Budankov { 514d3d1af6fSAlexey Budankov } 515d3d1af6fSAlexey Budankov #endif 516d3d1af6fSAlexey Budankov 517d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec) 518d3d1af6fSAlexey Budankov { 519d3d1af6fSAlexey Budankov return rec->opts.nr_cblocks > 0; 520d3d1af6fSAlexey Budankov } 521d3d1af6fSAlexey Budankov 522470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1 523470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt, 524470530bbSAlexey Budankov const char *str, 525470530bbSAlexey Budankov int unset) 526470530bbSAlexey Budankov { 527470530bbSAlexey Budankov int flush_max; 528470530bbSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 529470530bbSAlexey Budankov static struct parse_tag tags[] = { 530470530bbSAlexey Budankov { .tag = 'B', .mult = 1 }, 531470530bbSAlexey Budankov { .tag = 'K', .mult = 1 << 10 }, 532470530bbSAlexey Budankov { .tag = 'M', .mult = 1 << 20 }, 533470530bbSAlexey Budankov { .tag = 'G', .mult = 1 << 30 }, 534470530bbSAlexey Budankov { .tag = 0 }, 535470530bbSAlexey Budankov }; 536470530bbSAlexey Budankov 537470530bbSAlexey Budankov if (unset) 538470530bbSAlexey Budankov return 0; 539470530bbSAlexey Budankov 540470530bbSAlexey Budankov if (str) { 541470530bbSAlexey Budankov opts->mmap_flush = parse_tag_value(str, tags); 542470530bbSAlexey Budankov if (opts->mmap_flush == (int)-1) 543470530bbSAlexey Budankov opts->mmap_flush = strtol(str, NULL, 0); 544470530bbSAlexey Budankov } 545470530bbSAlexey Budankov 546470530bbSAlexey Budankov if (!opts->mmap_flush) 547470530bbSAlexey Budankov opts->mmap_flush = MMAP_FLUSH_DEFAULT; 548470530bbSAlexey Budankov 5499521b5f2SJiri Olsa flush_max = evlist__mmap_size(opts->mmap_pages); 550470530bbSAlexey Budankov flush_max /= 4; 551470530bbSAlexey Budankov if (opts->mmap_flush > flush_max) 552470530bbSAlexey Budankov opts->mmap_flush = flush_max; 553470530bbSAlexey Budankov 554470530bbSAlexey Budankov return 0; 555470530bbSAlexey Budankov } 556470530bbSAlexey Budankov 557504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 558504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1; 559504c1ad1SAlexey Budankov 560504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset) 561504c1ad1SAlexey Budankov { 562504c1ad1SAlexey Budankov struct record_opts *opts = opt->value; 563504c1ad1SAlexey Budankov 564504c1ad1SAlexey Budankov if (unset) { 565504c1ad1SAlexey Budankov opts->comp_level = 0; 566504c1ad1SAlexey Budankov } else { 567504c1ad1SAlexey Budankov if (str) 568504c1ad1SAlexey Budankov opts->comp_level = strtol(str, NULL, 0); 569504c1ad1SAlexey Budankov if (!opts->comp_level) 570504c1ad1SAlexey Budankov opts->comp_level = comp_level_default; 571504c1ad1SAlexey Budankov } 572504c1ad1SAlexey Budankov 573504c1ad1SAlexey Budankov return 0; 574504c1ad1SAlexey Budankov } 575504c1ad1SAlexey Budankov #endif 57651255a8aSAlexey Budankov static unsigned int comp_level_max = 22; 57751255a8aSAlexey Budankov 57842e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec) 57942e1fd80SAlexey Budankov { 58042e1fd80SAlexey Budankov return rec->opts.comp_level > 0; 58142e1fd80SAlexey Budankov } 58242e1fd80SAlexey Budankov 58345694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 584d20deb64SArnaldo Carvalho de Melo union perf_event *event, 5851d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 5861d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 587234fbbf5SArnaldo Carvalho de Melo { 5888c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 589ded2b8feSJiri Olsa return record__write(rec, NULL, event, event->header.size); 590234fbbf5SArnaldo Carvalho de Melo } 591234fbbf5SArnaldo Carvalho de Melo 592d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool, 593d99c22eaSStephane Eranian union perf_event *event, 594d99c22eaSStephane Eranian struct perf_sample *sample __maybe_unused, 595d99c22eaSStephane Eranian struct machine *machine __maybe_unused) 596d99c22eaSStephane Eranian { 597d99c22eaSStephane Eranian static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER; 598d99c22eaSStephane Eranian int ret; 599d99c22eaSStephane Eranian 600d99c22eaSStephane Eranian pthread_mutex_lock(&synth_lock); 601d99c22eaSStephane Eranian ret = process_synthesized_event(tool, event, sample, machine); 602d99c22eaSStephane Eranian pthread_mutex_unlock(&synth_lock); 603d99c22eaSStephane Eranian return ret; 604d99c22eaSStephane Eranian } 605d99c22eaSStephane Eranian 606a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size) 607d37f1586SArnaldo Carvalho de Melo { 608d37f1586SArnaldo Carvalho de Melo struct record *rec = to; 609d37f1586SArnaldo Carvalho de Melo 6105d7f4116SAlexey Budankov if (record__comp_enabled(rec)) { 611bf59b305SJiri Olsa size = zstd_compress(rec->session, map->data, mmap__mmap_len(map), bf, size); 6125d7f4116SAlexey Budankov bf = map->data; 6135d7f4116SAlexey Budankov } 6145d7f4116SAlexey Budankov 615396b626bSAlexey Bayduraev thread->samples++; 616ded2b8feSJiri Olsa return record__write(rec, map, bf, size); 617d37f1586SArnaldo Carvalho de Melo } 618d37f1586SArnaldo Carvalho de Melo 6192dd6d8a1SAdrian Hunter static volatile int signr = -1; 6202dd6d8a1SAdrian Hunter static volatile int child_finished; 621da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 622da231338SAnand K Mistry static int done_fd = -1; 623da231338SAnand K Mistry #endif 624c0bdc1c4SWang Nan 6252dd6d8a1SAdrian Hunter static void sig_handler(int sig) 6262dd6d8a1SAdrian Hunter { 6272dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 6282dd6d8a1SAdrian Hunter child_finished = 1; 6292dd6d8a1SAdrian Hunter else 6302dd6d8a1SAdrian Hunter signr = sig; 6312dd6d8a1SAdrian Hunter 6322dd6d8a1SAdrian Hunter done = 1; 633da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 634da231338SAnand K Mistry { 635da231338SAnand K Mistry u64 tmp = 1; 636da231338SAnand K Mistry /* 637da231338SAnand K Mistry * It is possible for this signal handler to run after done is checked 638da231338SAnand K Mistry * in the main loop, but before the perf counter fds are polled. If this 639da231338SAnand K Mistry * happens, the poll() will continue to wait even though done is set, 640da231338SAnand K Mistry * and will only break out if either another signal is received, or the 641da231338SAnand K Mistry * counters are ready for read. To ensure the poll() doesn't sleep when 642da231338SAnand K Mistry * done is set, use an eventfd (done_fd) to wake up the poll(). 643da231338SAnand K Mistry */ 644da231338SAnand K Mistry if (write(done_fd, &tmp, sizeof(tmp)) < 0) 645da231338SAnand K Mistry pr_err("failed to signal wakeup fd, error: %m\n"); 646da231338SAnand K Mistry } 647da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT 6482dd6d8a1SAdrian Hunter } 6492dd6d8a1SAdrian Hunter 650a074865eSWang Nan static void sigsegv_handler(int sig) 651a074865eSWang Nan { 652a074865eSWang Nan perf_hooks__recover(); 653a074865eSWang Nan sighandler_dump_stack(sig); 654a074865eSWang Nan } 655a074865eSWang Nan 6562dd6d8a1SAdrian Hunter static void record__sig_exit(void) 6572dd6d8a1SAdrian Hunter { 6582dd6d8a1SAdrian Hunter if (signr == -1) 6592dd6d8a1SAdrian Hunter return; 6602dd6d8a1SAdrian Hunter 6612dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 6622dd6d8a1SAdrian Hunter raise(signr); 6632dd6d8a1SAdrian Hunter } 6642dd6d8a1SAdrian Hunter 665e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 666e31f0d01SAdrian Hunter 667ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 668a5830532SJiri Olsa struct mmap *map, 669ef149c25SAdrian Hunter union perf_event *event, void *data1, 670ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 671ef149c25SAdrian Hunter { 672ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 6738ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 674ef149c25SAdrian Hunter size_t padding; 675ef149c25SAdrian Hunter u8 pad[8] = {0}; 676ef149c25SAdrian Hunter 67746e201efSAdrian Hunter if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) { 67899fa2984SAdrian Hunter off_t file_offset; 6798ceb41d7SJiri Olsa int fd = perf_data__fd(data); 68099fa2984SAdrian Hunter int err; 68199fa2984SAdrian Hunter 68299fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 68399fa2984SAdrian Hunter if (file_offset == -1) 68499fa2984SAdrian Hunter return -1; 68599fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 68699fa2984SAdrian Hunter event, file_offset); 68799fa2984SAdrian Hunter if (err) 68899fa2984SAdrian Hunter return err; 68999fa2984SAdrian Hunter } 69099fa2984SAdrian Hunter 691ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 692ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 693ef149c25SAdrian Hunter if (padding) 694ef149c25SAdrian Hunter padding = 8 - padding; 695ef149c25SAdrian Hunter 696ded2b8feSJiri Olsa record__write(rec, map, event, event->header.size); 697ded2b8feSJiri Olsa record__write(rec, map, data1, len1); 698ef149c25SAdrian Hunter if (len2) 699ded2b8feSJiri Olsa record__write(rec, map, data2, len2); 700ded2b8feSJiri Olsa record__write(rec, map, &pad, padding); 701ef149c25SAdrian Hunter 702ef149c25SAdrian Hunter return 0; 703ef149c25SAdrian Hunter } 704ef149c25SAdrian Hunter 705ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 706a5830532SJiri Olsa struct mmap *map) 707ef149c25SAdrian Hunter { 708ef149c25SAdrian Hunter int ret; 709ef149c25SAdrian Hunter 710e035f4caSJiri Olsa ret = auxtrace_mmap__read(map, rec->itr, &rec->tool, 711ef149c25SAdrian Hunter record__process_auxtrace); 712ef149c25SAdrian Hunter if (ret < 0) 713ef149c25SAdrian Hunter return ret; 714ef149c25SAdrian Hunter 715ef149c25SAdrian Hunter if (ret) 716ef149c25SAdrian Hunter rec->samples++; 717ef149c25SAdrian Hunter 718ef149c25SAdrian Hunter return 0; 719ef149c25SAdrian Hunter } 720ef149c25SAdrian Hunter 7212dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 722a5830532SJiri Olsa struct mmap *map) 7232dd6d8a1SAdrian Hunter { 7242dd6d8a1SAdrian Hunter int ret; 7252dd6d8a1SAdrian Hunter 726e035f4caSJiri Olsa ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool, 7272dd6d8a1SAdrian Hunter record__process_auxtrace, 7282dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 7292dd6d8a1SAdrian Hunter if (ret < 0) 7302dd6d8a1SAdrian Hunter return ret; 7312dd6d8a1SAdrian Hunter 7322dd6d8a1SAdrian Hunter if (ret) 7332dd6d8a1SAdrian Hunter rec->samples++; 7342dd6d8a1SAdrian Hunter 7352dd6d8a1SAdrian Hunter return 0; 7362dd6d8a1SAdrian Hunter } 7372dd6d8a1SAdrian Hunter 7382dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 7392dd6d8a1SAdrian Hunter { 7402dd6d8a1SAdrian Hunter int i; 7412dd6d8a1SAdrian Hunter int rc = 0; 7422dd6d8a1SAdrian Hunter 743c976ee11SJiri Olsa for (i = 0; i < rec->evlist->core.nr_mmaps; i++) { 744a5830532SJiri Olsa struct mmap *map = &rec->evlist->mmap[i]; 7452dd6d8a1SAdrian Hunter 746e035f4caSJiri Olsa if (!map->auxtrace_mmap.base) 7472dd6d8a1SAdrian Hunter continue; 7482dd6d8a1SAdrian Hunter 749e035f4caSJiri Olsa if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) { 7502dd6d8a1SAdrian Hunter rc = -1; 7512dd6d8a1SAdrian Hunter goto out; 7522dd6d8a1SAdrian Hunter } 7532dd6d8a1SAdrian Hunter } 7542dd6d8a1SAdrian Hunter out: 7552dd6d8a1SAdrian Hunter return rc; 7562dd6d8a1SAdrian Hunter } 7572dd6d8a1SAdrian Hunter 758ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit) 7592dd6d8a1SAdrian Hunter { 7602dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 7612dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 7625f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 7632dd6d8a1SAdrian Hunter } else { 764ce7b0e42SAlexander Shishkin if (auxtrace_record__snapshot_finish(rec->itr, on_exit)) 7655f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 7665f9cf599SWang Nan else 7675f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 7682dd6d8a1SAdrian Hunter } 7692dd6d8a1SAdrian Hunter } 7702dd6d8a1SAdrian Hunter 771ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec) 772ce7b0e42SAlexander Shishkin { 773ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 774ce7b0e42SAlexander Shishkin return 0; 775ce7b0e42SAlexander Shishkin 776ce7b0e42SAlexander Shishkin if (!auxtrace_record__snapshot_started && 777ce7b0e42SAlexander Shishkin auxtrace_record__snapshot_start(rec->itr)) 778ce7b0e42SAlexander Shishkin return -1; 779ce7b0e42SAlexander Shishkin 780ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, true); 781ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 782ce7b0e42SAlexander Shishkin return -1; 783ce7b0e42SAlexander Shishkin 784ce7b0e42SAlexander Shishkin return 0; 785ce7b0e42SAlexander Shishkin } 786ce7b0e42SAlexander Shishkin 7874b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec) 7884b5ea3bdSAdrian Hunter { 7894b5ea3bdSAdrian Hunter int err; 7904b5ea3bdSAdrian Hunter 7914b5ea3bdSAdrian Hunter if (!rec->itr) { 7924b5ea3bdSAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 7934b5ea3bdSAdrian Hunter if (err) 7944b5ea3bdSAdrian Hunter return err; 7954b5ea3bdSAdrian Hunter } 7964b5ea3bdSAdrian Hunter 7974b5ea3bdSAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 7984b5ea3bdSAdrian Hunter rec->opts.auxtrace_snapshot_opts); 7994b5ea3bdSAdrian Hunter if (err) 8004b5ea3bdSAdrian Hunter return err; 8014b5ea3bdSAdrian Hunter 802c0a6de06SAdrian Hunter err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts, 803c0a6de06SAdrian Hunter rec->opts.auxtrace_sample_opts); 804c0a6de06SAdrian Hunter if (err) 805c0a6de06SAdrian Hunter return err; 806c0a6de06SAdrian Hunter 807d58b3f7eSAdrian Hunter auxtrace_regroup_aux_output(rec->evlist); 808d58b3f7eSAdrian Hunter 8094b5ea3bdSAdrian Hunter return auxtrace_parse_filters(rec->evlist); 8104b5ea3bdSAdrian Hunter } 8114b5ea3bdSAdrian Hunter 812e31f0d01SAdrian Hunter #else 813e31f0d01SAdrian Hunter 814e31f0d01SAdrian Hunter static inline 815e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 816a5830532SJiri Olsa struct mmap *map __maybe_unused) 817e31f0d01SAdrian Hunter { 818e31f0d01SAdrian Hunter return 0; 819e31f0d01SAdrian Hunter } 820e31f0d01SAdrian Hunter 8212dd6d8a1SAdrian Hunter static inline 822ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused, 823ce7b0e42SAlexander Shishkin bool on_exit __maybe_unused) 8242dd6d8a1SAdrian Hunter { 8252dd6d8a1SAdrian Hunter } 8262dd6d8a1SAdrian Hunter 8272dd6d8a1SAdrian Hunter static inline 8282dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 8292dd6d8a1SAdrian Hunter { 8302dd6d8a1SAdrian Hunter return 0; 8312dd6d8a1SAdrian Hunter } 8322dd6d8a1SAdrian Hunter 833ce7b0e42SAlexander Shishkin static inline 834ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused) 835ce7b0e42SAlexander Shishkin { 836ce7b0e42SAlexander Shishkin return 0; 837ce7b0e42SAlexander Shishkin } 838ce7b0e42SAlexander Shishkin 8394b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused) 8404b5ea3bdSAdrian Hunter { 8414b5ea3bdSAdrian Hunter return 0; 8424b5ea3bdSAdrian Hunter } 8434b5ea3bdSAdrian Hunter 844e31f0d01SAdrian Hunter #endif 845e31f0d01SAdrian Hunter 846246eba8eSAdrian Hunter static int record__config_text_poke(struct evlist *evlist) 847246eba8eSAdrian Hunter { 848246eba8eSAdrian Hunter struct evsel *evsel; 849246eba8eSAdrian Hunter int err; 850246eba8eSAdrian Hunter 851246eba8eSAdrian Hunter /* Nothing to do if text poke is already configured */ 852246eba8eSAdrian Hunter evlist__for_each_entry(evlist, evsel) { 853246eba8eSAdrian Hunter if (evsel->core.attr.text_poke) 854246eba8eSAdrian Hunter return 0; 855246eba8eSAdrian Hunter } 856246eba8eSAdrian Hunter 857246eba8eSAdrian Hunter err = parse_events(evlist, "dummy:u", NULL); 858246eba8eSAdrian Hunter if (err) 859246eba8eSAdrian Hunter return err; 860246eba8eSAdrian Hunter 861246eba8eSAdrian Hunter evsel = evlist__last(evlist); 862246eba8eSAdrian Hunter 863246eba8eSAdrian Hunter evsel->core.attr.freq = 0; 864246eba8eSAdrian Hunter evsel->core.attr.sample_period = 1; 865246eba8eSAdrian Hunter evsel->core.attr.text_poke = 1; 866246eba8eSAdrian Hunter evsel->core.attr.ksymbol = 1; 867246eba8eSAdrian Hunter 868246eba8eSAdrian Hunter evsel->core.system_wide = true; 869246eba8eSAdrian Hunter evsel->no_aux_samples = true; 870246eba8eSAdrian Hunter evsel->immediate = true; 871246eba8eSAdrian Hunter 872246eba8eSAdrian Hunter /* Text poke must be collected on all CPUs */ 873246eba8eSAdrian Hunter perf_cpu_map__put(evsel->core.own_cpus); 874246eba8eSAdrian Hunter evsel->core.own_cpus = perf_cpu_map__new(NULL); 875246eba8eSAdrian Hunter perf_cpu_map__put(evsel->core.cpus); 876246eba8eSAdrian Hunter evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus); 877246eba8eSAdrian Hunter 878246eba8eSAdrian Hunter evsel__set_sample_bit(evsel, TIME); 879246eba8eSAdrian Hunter 880246eba8eSAdrian Hunter return 0; 881246eba8eSAdrian Hunter } 882246eba8eSAdrian Hunter 883eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine) 884eeb399b5SAdrian Hunter { 885eeb399b5SAdrian Hunter char kcore[PATH_MAX]; 886eeb399b5SAdrian Hunter int fd; 887eeb399b5SAdrian Hunter 888eeb399b5SAdrian Hunter scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir); 889eeb399b5SAdrian Hunter 890eeb399b5SAdrian Hunter fd = open(kcore, O_RDONLY); 891eeb399b5SAdrian Hunter if (fd < 0) 892eeb399b5SAdrian Hunter return false; 893eeb399b5SAdrian Hunter 894eeb399b5SAdrian Hunter close(fd); 895eeb399b5SAdrian Hunter 896eeb399b5SAdrian Hunter return true; 897eeb399b5SAdrian Hunter } 898eeb399b5SAdrian Hunter 899eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data) 900eeb399b5SAdrian Hunter { 901eeb399b5SAdrian Hunter char from_dir[PATH_MAX]; 902eeb399b5SAdrian Hunter char kcore_dir[PATH_MAX]; 903eeb399b5SAdrian Hunter int ret; 904eeb399b5SAdrian Hunter 905eeb399b5SAdrian Hunter snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir); 906eeb399b5SAdrian Hunter 907eeb399b5SAdrian Hunter ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir)); 908eeb399b5SAdrian Hunter if (ret) 909eeb399b5SAdrian Hunter return ret; 910eeb399b5SAdrian Hunter 911eeb399b5SAdrian Hunter return kcore_copy(from_dir, kcore_dir); 912eeb399b5SAdrian Hunter } 913eeb399b5SAdrian Hunter 914415ccb58SAlexey Bayduraev static void record__thread_data_init_pipes(struct record_thread *thread_data) 915415ccb58SAlexey Bayduraev { 916415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 917415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 918415ccb58SAlexey Bayduraev thread_data->pipes.ack[0] = -1; 919415ccb58SAlexey Bayduraev thread_data->pipes.ack[1] = -1; 920415ccb58SAlexey Bayduraev } 921415ccb58SAlexey Bayduraev 922415ccb58SAlexey Bayduraev static int record__thread_data_open_pipes(struct record_thread *thread_data) 923415ccb58SAlexey Bayduraev { 924415ccb58SAlexey Bayduraev if (pipe(thread_data->pipes.msg)) 925415ccb58SAlexey Bayduraev return -EINVAL; 926415ccb58SAlexey Bayduraev 927415ccb58SAlexey Bayduraev if (pipe(thread_data->pipes.ack)) { 928415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[0]); 929415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 930415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[1]); 931415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 932415ccb58SAlexey Bayduraev return -EINVAL; 933415ccb58SAlexey Bayduraev } 934415ccb58SAlexey Bayduraev 935415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: msg=[%d,%d], ack=[%d,%d]\n", thread_data, 936415ccb58SAlexey Bayduraev thread_data->pipes.msg[0], thread_data->pipes.msg[1], 937415ccb58SAlexey Bayduraev thread_data->pipes.ack[0], thread_data->pipes.ack[1]); 938415ccb58SAlexey Bayduraev 939415ccb58SAlexey Bayduraev return 0; 940415ccb58SAlexey Bayduraev } 941415ccb58SAlexey Bayduraev 942415ccb58SAlexey Bayduraev static void record__thread_data_close_pipes(struct record_thread *thread_data) 943415ccb58SAlexey Bayduraev { 944415ccb58SAlexey Bayduraev if (thread_data->pipes.msg[0] != -1) { 945415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[0]); 946415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 947415ccb58SAlexey Bayduraev } 948415ccb58SAlexey Bayduraev if (thread_data->pipes.msg[1] != -1) { 949415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[1]); 950415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 951415ccb58SAlexey Bayduraev } 952415ccb58SAlexey Bayduraev if (thread_data->pipes.ack[0] != -1) { 953415ccb58SAlexey Bayduraev close(thread_data->pipes.ack[0]); 954415ccb58SAlexey Bayduraev thread_data->pipes.ack[0] = -1; 955415ccb58SAlexey Bayduraev } 956415ccb58SAlexey Bayduraev if (thread_data->pipes.ack[1] != -1) { 957415ccb58SAlexey Bayduraev close(thread_data->pipes.ack[1]); 958415ccb58SAlexey Bayduraev thread_data->pipes.ack[1] = -1; 959415ccb58SAlexey Bayduraev } 960415ccb58SAlexey Bayduraev } 961415ccb58SAlexey Bayduraev 962415ccb58SAlexey Bayduraev static int record__thread_data_init_maps(struct record_thread *thread_data, struct evlist *evlist) 963415ccb58SAlexey Bayduraev { 964415ccb58SAlexey Bayduraev int m, tm, nr_mmaps = evlist->core.nr_mmaps; 965415ccb58SAlexey Bayduraev struct mmap *mmap = evlist->mmap; 966415ccb58SAlexey Bayduraev struct mmap *overwrite_mmap = evlist->overwrite_mmap; 967415ccb58SAlexey Bayduraev struct perf_cpu_map *cpus = evlist->core.cpus; 968415ccb58SAlexey Bayduraev 969415ccb58SAlexey Bayduraev thread_data->nr_mmaps = bitmap_weight(thread_data->mask->maps.bits, 970415ccb58SAlexey Bayduraev thread_data->mask->maps.nbits); 971415ccb58SAlexey Bayduraev if (mmap) { 972415ccb58SAlexey Bayduraev thread_data->maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *)); 973415ccb58SAlexey Bayduraev if (!thread_data->maps) 974415ccb58SAlexey Bayduraev return -ENOMEM; 975415ccb58SAlexey Bayduraev } 976415ccb58SAlexey Bayduraev if (overwrite_mmap) { 977415ccb58SAlexey Bayduraev thread_data->overwrite_maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *)); 978415ccb58SAlexey Bayduraev if (!thread_data->overwrite_maps) { 979415ccb58SAlexey Bayduraev zfree(&thread_data->maps); 980415ccb58SAlexey Bayduraev return -ENOMEM; 981415ccb58SAlexey Bayduraev } 982415ccb58SAlexey Bayduraev } 983415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: nr_mmaps=%d, maps=%p, ow_maps=%p\n", thread_data, 984415ccb58SAlexey Bayduraev thread_data->nr_mmaps, thread_data->maps, thread_data->overwrite_maps); 985415ccb58SAlexey Bayduraev 986415ccb58SAlexey Bayduraev for (m = 0, tm = 0; m < nr_mmaps && tm < thread_data->nr_mmaps; m++) { 987415ccb58SAlexey Bayduraev if (test_bit(cpus->map[m].cpu, thread_data->mask->maps.bits)) { 988415ccb58SAlexey Bayduraev if (thread_data->maps) { 989415ccb58SAlexey Bayduraev thread_data->maps[tm] = &mmap[m]; 990415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: cpu%d: maps[%d] -> mmap[%d]\n", 991415ccb58SAlexey Bayduraev thread_data, cpus->map[m].cpu, tm, m); 992415ccb58SAlexey Bayduraev } 993415ccb58SAlexey Bayduraev if (thread_data->overwrite_maps) { 994415ccb58SAlexey Bayduraev thread_data->overwrite_maps[tm] = &overwrite_mmap[m]; 995415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: cpu%d: ow_maps[%d] -> ow_mmap[%d]\n", 996415ccb58SAlexey Bayduraev thread_data, cpus->map[m].cpu, tm, m); 997415ccb58SAlexey Bayduraev } 998415ccb58SAlexey Bayduraev tm++; 999415ccb58SAlexey Bayduraev } 1000415ccb58SAlexey Bayduraev } 1001415ccb58SAlexey Bayduraev 1002415ccb58SAlexey Bayduraev return 0; 1003415ccb58SAlexey Bayduraev } 1004415ccb58SAlexey Bayduraev 1005415ccb58SAlexey Bayduraev static int record__thread_data_init_pollfd(struct record_thread *thread_data, struct evlist *evlist) 1006415ccb58SAlexey Bayduraev { 1007415ccb58SAlexey Bayduraev int f, tm, pos; 1008415ccb58SAlexey Bayduraev struct mmap *map, *overwrite_map; 1009415ccb58SAlexey Bayduraev 1010415ccb58SAlexey Bayduraev fdarray__init(&thread_data->pollfd, 64); 1011415ccb58SAlexey Bayduraev 1012415ccb58SAlexey Bayduraev for (tm = 0; tm < thread_data->nr_mmaps; tm++) { 1013415ccb58SAlexey Bayduraev map = thread_data->maps ? thread_data->maps[tm] : NULL; 1014415ccb58SAlexey Bayduraev overwrite_map = thread_data->overwrite_maps ? 1015415ccb58SAlexey Bayduraev thread_data->overwrite_maps[tm] : NULL; 1016415ccb58SAlexey Bayduraev 1017415ccb58SAlexey Bayduraev for (f = 0; f < evlist->core.pollfd.nr; f++) { 1018415ccb58SAlexey Bayduraev void *ptr = evlist->core.pollfd.priv[f].ptr; 1019415ccb58SAlexey Bayduraev 1020415ccb58SAlexey Bayduraev if ((map && ptr == map) || (overwrite_map && ptr == overwrite_map)) { 1021415ccb58SAlexey Bayduraev pos = fdarray__dup_entry_from(&thread_data->pollfd, f, 1022415ccb58SAlexey Bayduraev &evlist->core.pollfd); 1023415ccb58SAlexey Bayduraev if (pos < 0) 1024415ccb58SAlexey Bayduraev return pos; 1025415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: pollfd[%d] <- event_fd=%d\n", 1026415ccb58SAlexey Bayduraev thread_data, pos, evlist->core.pollfd.entries[f].fd); 1027415ccb58SAlexey Bayduraev } 1028415ccb58SAlexey Bayduraev } 1029415ccb58SAlexey Bayduraev } 1030415ccb58SAlexey Bayduraev 1031415ccb58SAlexey Bayduraev return 0; 1032415ccb58SAlexey Bayduraev } 1033415ccb58SAlexey Bayduraev 1034415ccb58SAlexey Bayduraev static void record__free_thread_data(struct record *rec) 1035415ccb58SAlexey Bayduraev { 1036415ccb58SAlexey Bayduraev int t; 1037415ccb58SAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 1038415ccb58SAlexey Bayduraev 1039415ccb58SAlexey Bayduraev if (thread_data == NULL) 1040415ccb58SAlexey Bayduraev return; 1041415ccb58SAlexey Bayduraev 1042415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 1043415ccb58SAlexey Bayduraev record__thread_data_close_pipes(&thread_data[t]); 1044415ccb58SAlexey Bayduraev zfree(&thread_data[t].maps); 1045415ccb58SAlexey Bayduraev zfree(&thread_data[t].overwrite_maps); 1046415ccb58SAlexey Bayduraev fdarray__exit(&thread_data[t].pollfd); 1047415ccb58SAlexey Bayduraev } 1048415ccb58SAlexey Bayduraev 1049415ccb58SAlexey Bayduraev zfree(&rec->thread_data); 1050415ccb58SAlexey Bayduraev } 1051415ccb58SAlexey Bayduraev 1052415ccb58SAlexey Bayduraev static int record__alloc_thread_data(struct record *rec, struct evlist *evlist) 1053415ccb58SAlexey Bayduraev { 1054415ccb58SAlexey Bayduraev int t, ret; 1055415ccb58SAlexey Bayduraev struct record_thread *thread_data; 1056415ccb58SAlexey Bayduraev 1057415ccb58SAlexey Bayduraev rec->thread_data = zalloc(rec->nr_threads * sizeof(*(rec->thread_data))); 1058415ccb58SAlexey Bayduraev if (!rec->thread_data) { 1059415ccb58SAlexey Bayduraev pr_err("Failed to allocate thread data\n"); 1060415ccb58SAlexey Bayduraev return -ENOMEM; 1061415ccb58SAlexey Bayduraev } 1062415ccb58SAlexey Bayduraev thread_data = rec->thread_data; 1063415ccb58SAlexey Bayduraev 1064415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 1065415ccb58SAlexey Bayduraev record__thread_data_init_pipes(&thread_data[t]); 1066415ccb58SAlexey Bayduraev 1067415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 1068415ccb58SAlexey Bayduraev thread_data[t].rec = rec; 1069415ccb58SAlexey Bayduraev thread_data[t].mask = &rec->thread_masks[t]; 1070415ccb58SAlexey Bayduraev ret = record__thread_data_init_maps(&thread_data[t], evlist); 1071415ccb58SAlexey Bayduraev if (ret) { 1072415ccb58SAlexey Bayduraev pr_err("Failed to initialize thread[%d] maps\n", t); 1073415ccb58SAlexey Bayduraev goto out_free; 1074415ccb58SAlexey Bayduraev } 1075415ccb58SAlexey Bayduraev ret = record__thread_data_init_pollfd(&thread_data[t], evlist); 1076415ccb58SAlexey Bayduraev if (ret) { 1077415ccb58SAlexey Bayduraev pr_err("Failed to initialize thread[%d] pollfd\n", t); 1078415ccb58SAlexey Bayduraev goto out_free; 1079415ccb58SAlexey Bayduraev } 1080415ccb58SAlexey Bayduraev if (t) { 1081415ccb58SAlexey Bayduraev thread_data[t].tid = -1; 1082415ccb58SAlexey Bayduraev ret = record__thread_data_open_pipes(&thread_data[t]); 1083415ccb58SAlexey Bayduraev if (ret) { 1084415ccb58SAlexey Bayduraev pr_err("Failed to open thread[%d] communication pipes\n", t); 1085415ccb58SAlexey Bayduraev goto out_free; 1086415ccb58SAlexey Bayduraev } 1087415ccb58SAlexey Bayduraev ret = fdarray__add(&thread_data[t].pollfd, thread_data[t].pipes.msg[0], 1088415ccb58SAlexey Bayduraev POLLIN | POLLERR | POLLHUP, fdarray_flag__nonfilterable); 1089415ccb58SAlexey Bayduraev if (ret < 0) { 1090415ccb58SAlexey Bayduraev pr_err("Failed to add descriptor to thread[%d] pollfd\n", t); 1091415ccb58SAlexey Bayduraev goto out_free; 1092415ccb58SAlexey Bayduraev } 1093415ccb58SAlexey Bayduraev thread_data[t].ctlfd_pos = ret; 1094415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: pollfd[%d] <- ctl_fd=%d\n", 1095415ccb58SAlexey Bayduraev thread_data, thread_data[t].ctlfd_pos, 1096415ccb58SAlexey Bayduraev thread_data[t].pipes.msg[0]); 1097415ccb58SAlexey Bayduraev } else { 1098415ccb58SAlexey Bayduraev thread_data[t].tid = gettid(); 1099415ccb58SAlexey Bayduraev if (evlist->ctl_fd.pos == -1) 1100415ccb58SAlexey Bayduraev continue; 1101415ccb58SAlexey Bayduraev ret = fdarray__dup_entry_from(&thread_data[t].pollfd, evlist->ctl_fd.pos, 1102415ccb58SAlexey Bayduraev &evlist->core.pollfd); 1103415ccb58SAlexey Bayduraev if (ret < 0) { 1104415ccb58SAlexey Bayduraev pr_err("Failed to duplicate descriptor in main thread pollfd\n"); 1105415ccb58SAlexey Bayduraev goto out_free; 1106415ccb58SAlexey Bayduraev } 1107415ccb58SAlexey Bayduraev thread_data[t].ctlfd_pos = ret; 1108415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: pollfd[%d] <- ctl_fd=%d\n", 1109415ccb58SAlexey Bayduraev thread_data, thread_data[t].ctlfd_pos, 1110415ccb58SAlexey Bayduraev evlist->core.pollfd.entries[evlist->ctl_fd.pos].fd); 1111415ccb58SAlexey Bayduraev } 1112415ccb58SAlexey Bayduraev } 1113415ccb58SAlexey Bayduraev 1114415ccb58SAlexey Bayduraev return 0; 1115415ccb58SAlexey Bayduraev 1116415ccb58SAlexey Bayduraev out_free: 1117415ccb58SAlexey Bayduraev record__free_thread_data(rec); 1118415ccb58SAlexey Bayduraev 1119415ccb58SAlexey Bayduraev return ret; 1120415ccb58SAlexey Bayduraev } 1121415ccb58SAlexey Bayduraev 1122cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 112363503dbaSJiri Olsa struct evlist *evlist) 1124cda57a8cSWang Nan { 112556f735ffSAlexey Bayduraev int i, ret; 1126cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 1127c0a6de06SAdrian Hunter bool auxtrace_overwrite = opts->auxtrace_snapshot_mode || 1128c0a6de06SAdrian Hunter opts->auxtrace_sample_mode; 1129cda57a8cSWang Nan char msg[512]; 1130cda57a8cSWang Nan 1131f13de660SAlexey Budankov if (opts->affinity != PERF_AFFINITY_SYS) 1132f13de660SAlexey Budankov cpu__setup_cpunode_map(); 1133f13de660SAlexey Budankov 11349521b5f2SJiri Olsa if (evlist__mmap_ex(evlist, opts->mmap_pages, 1135cda57a8cSWang Nan opts->auxtrace_mmap_pages, 1136c0a6de06SAdrian Hunter auxtrace_overwrite, 1137470530bbSAlexey Budankov opts->nr_cblocks, opts->affinity, 113851255a8aSAlexey Budankov opts->mmap_flush, opts->comp_level) < 0) { 1139cda57a8cSWang Nan if (errno == EPERM) { 1140cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 1141cda57a8cSWang Nan "Consider increasing " 1142cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 1143cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 1144cda57a8cSWang Nan "(current value: %u,%u)\n", 1145cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 1146cda57a8cSWang Nan return -errno; 1147cda57a8cSWang Nan } else { 1148cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 1149c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 1150cda57a8cSWang Nan if (errno) 1151cda57a8cSWang Nan return -errno; 1152cda57a8cSWang Nan else 1153cda57a8cSWang Nan return -EINVAL; 1154cda57a8cSWang Nan } 1155cda57a8cSWang Nan } 1156415ccb58SAlexey Bayduraev 1157415ccb58SAlexey Bayduraev if (evlist__initialize_ctlfd(evlist, opts->ctl_fd, opts->ctl_fd_ack)) 1158415ccb58SAlexey Bayduraev return -1; 1159415ccb58SAlexey Bayduraev 1160415ccb58SAlexey Bayduraev ret = record__alloc_thread_data(rec, evlist); 1161415ccb58SAlexey Bayduraev if (ret) 1162415ccb58SAlexey Bayduraev return ret; 1163415ccb58SAlexey Bayduraev 116456f735ffSAlexey Bayduraev if (record__threads_enabled(rec)) { 116556f735ffSAlexey Bayduraev ret = perf_data__create_dir(&rec->data, evlist->core.nr_mmaps); 116656f735ffSAlexey Bayduraev if (ret) 116756f735ffSAlexey Bayduraev return ret; 116856f735ffSAlexey Bayduraev for (i = 0; i < evlist->core.nr_mmaps; i++) { 116956f735ffSAlexey Bayduraev if (evlist->mmap) 117056f735ffSAlexey Bayduraev evlist->mmap[i].file = &rec->data.dir.files[i]; 117156f735ffSAlexey Bayduraev if (evlist->overwrite_mmap) 117256f735ffSAlexey Bayduraev evlist->overwrite_mmap[i].file = &rec->data.dir.files[i]; 117356f735ffSAlexey Bayduraev } 117456f735ffSAlexey Bayduraev } 117556f735ffSAlexey Bayduraev 1176cda57a8cSWang Nan return 0; 1177cda57a8cSWang Nan } 1178cda57a8cSWang Nan 1179cda57a8cSWang Nan static int record__mmap(struct record *rec) 1180cda57a8cSWang Nan { 1181cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 1182cda57a8cSWang Nan } 1183cda57a8cSWang Nan 11848c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 1185dd7927f4SArnaldo Carvalho de Melo { 1186d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 118732dcd021SJiri Olsa struct evsel *pos; 118863503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 1189d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 1190b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 11918d3eca20SDavid Ahern int rc = 0; 1192dd7927f4SArnaldo Carvalho de Melo 1193d3dbf43cSArnaldo Carvalho de Melo /* 1194b91e5492SKan Liang * For initial_delay, system wide or a hybrid system, we need to add a 1195b91e5492SKan Liang * dummy event so that we can track PERF_RECORD_MMAP to cover the delay 1196b91e5492SKan Liang * of waiting or event synthesis. 1197d3dbf43cSArnaldo Carvalho de Melo */ 1198b91e5492SKan Liang if (opts->initial_delay || target__has_cpu(&opts->target) || 1199b91e5492SKan Liang perf_pmu__has_hybrid()) { 1200e80db255SArnaldo Carvalho de Melo pos = evlist__get_tracking_event(evlist); 1201442ad225SAdrian Hunter if (!evsel__is_dummy_event(pos)) { 1202442ad225SAdrian Hunter /* Set up dummy event. */ 1203e251abeeSArnaldo Carvalho de Melo if (evlist__add_dummy(evlist)) 1204d3dbf43cSArnaldo Carvalho de Melo return -ENOMEM; 1205515dbe48SJiri Olsa pos = evlist__last(evlist); 1206e80db255SArnaldo Carvalho de Melo evlist__set_tracking_event(evlist, pos); 1207442ad225SAdrian Hunter } 1208442ad225SAdrian Hunter 12090a892c1cSIan Rogers /* 12100a892c1cSIan Rogers * Enable the dummy event when the process is forked for 12110a892c1cSIan Rogers * initial_delay, immediately for system wide. 12120a892c1cSIan Rogers */ 1213bb07d62eSNamhyung Kim if (opts->initial_delay && !pos->immediate && 1214bb07d62eSNamhyung Kim !target__has_cpu(&opts->target)) 12151fc632ceSJiri Olsa pos->core.attr.enable_on_exec = 1; 12160a892c1cSIan Rogers else 12170a892c1cSIan Rogers pos->immediate = 1; 1218d3dbf43cSArnaldo Carvalho de Melo } 1219d3dbf43cSArnaldo Carvalho de Melo 122078e1bc25SArnaldo Carvalho de Melo evlist__config(evlist, opts, &callchain_param); 1221cac21425SJiri Olsa 1222e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 12233da297a6SIngo Molnar try_again: 1224af663bd0SJiri Olsa if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) { 1225ae430892SArnaldo Carvalho de Melo if (evsel__fallback(pos, errno, msg, sizeof(msg))) { 1226bb963e16SNamhyung Kim if (verbose > 0) 1227c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 12283da297a6SIngo Molnar goto try_again; 12293da297a6SIngo Molnar } 1230cf99ad14SAndi Kleen if ((errno == EINVAL || errno == EBADF) && 1231fba7c866SJiri Olsa pos->core.leader != &pos->core && 1232cf99ad14SAndi Kleen pos->weak_group) { 123364b4778bSArnaldo Carvalho de Melo pos = evlist__reset_weak_group(evlist, pos, true); 1234cf99ad14SAndi Kleen goto try_again; 1235cf99ad14SAndi Kleen } 123656e52e85SArnaldo Carvalho de Melo rc = -errno; 12372bb72dbbSArnaldo Carvalho de Melo evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg)); 123856e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 12398d3eca20SDavid Ahern goto out; 12407c6a1c65SPeter Zijlstra } 1241bfd8f72cSAndi Kleen 1242bfd8f72cSAndi Kleen pos->supported = true; 12437c6a1c65SPeter Zijlstra } 12447c6a1c65SPeter Zijlstra 124578e1bc25SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) { 1246c8b567c8SArnaldo Carvalho de Melo pr_warning( 1247c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 1248c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n" 1249c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 1250c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 1251c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 1252c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 1253c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 1254c8b567c8SArnaldo Carvalho de Melo } 1255c8b567c8SArnaldo Carvalho de Melo 125624bf91a7SArnaldo Carvalho de Melo if (evlist__apply_filters(evlist, &pos)) { 125762d94b00SArnaldo Carvalho de Melo pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 12588ab2e96dSArnaldo Carvalho de Melo pos->filter, evsel__name(pos), errno, 1259c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 12608d3eca20SDavid Ahern rc = -1; 12618d3eca20SDavid Ahern goto out; 12620a102479SFrederic Weisbecker } 12630a102479SFrederic Weisbecker 1264cda57a8cSWang Nan rc = record__mmap(rec); 1265cda57a8cSWang Nan if (rc) 12668d3eca20SDavid Ahern goto out; 12670a27d7f9SArnaldo Carvalho de Melo 1268a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 12697b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 12708d3eca20SDavid Ahern out: 12718d3eca20SDavid Ahern return rc; 1272a91e5431SArnaldo Carvalho de Melo } 1273a91e5431SArnaldo Carvalho de Melo 127466286ed3SAdrian Hunter static void set_timestamp_boundary(struct record *rec, u64 sample_time) 127566286ed3SAdrian Hunter { 127666286ed3SAdrian Hunter if (rec->evlist->first_sample_time == 0) 127766286ed3SAdrian Hunter rec->evlist->first_sample_time = sample_time; 127866286ed3SAdrian Hunter 127966286ed3SAdrian Hunter if (sample_time) 128066286ed3SAdrian Hunter rec->evlist->last_sample_time = sample_time; 128166286ed3SAdrian Hunter } 128266286ed3SAdrian Hunter 1283e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 1284e3d59112SNamhyung Kim union perf_event *event, 1285e3d59112SNamhyung Kim struct perf_sample *sample, 128632dcd021SJiri Olsa struct evsel *evsel, 1287e3d59112SNamhyung Kim struct machine *machine) 1288e3d59112SNamhyung Kim { 1289e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 1290e3d59112SNamhyung Kim 129166286ed3SAdrian Hunter set_timestamp_boundary(rec, sample->time); 129268588bafSJin Yao 129368588bafSJin Yao if (rec->buildid_all) 129468588bafSJin Yao return 0; 129568588bafSJin Yao 129668588bafSJin Yao rec->samples++; 1297e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 1298e3d59112SNamhyung Kim } 1299e3d59112SNamhyung Kim 13008c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 13016122e4e4SArnaldo Carvalho de Melo { 1302f5fc1412SJiri Olsa struct perf_session *session = rec->session; 13036122e4e4SArnaldo Carvalho de Melo 130445112e89SJiri Olsa if (perf_data__size(&rec->data) == 0) 13059f591fd7SArnaldo Carvalho de Melo return 0; 13069f591fd7SArnaldo Carvalho de Melo 130700dc8657SNamhyung Kim /* 130800dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 130900dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 131000dc8657SNamhyung Kim * we prefer the vmlinux path like 131100dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 131200dc8657SNamhyung Kim * 131300dc8657SNamhyung Kim * rather than build-id path (in debug directory). 131400dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 131500dc8657SNamhyung Kim */ 131600dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 131700dc8657SNamhyung Kim 13186156681bSNamhyung Kim /* 13196156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 132068588bafSJin Yao * so no need to process samples. But if timestamp_boundary is enabled, 132168588bafSJin Yao * it still needs to walk on all samples to get the timestamps of 132268588bafSJin Yao * first/last samples. 13236156681bSNamhyung Kim */ 132468588bafSJin Yao if (rec->buildid_all && !rec->timestamp_boundary) 13256156681bSNamhyung Kim rec->tool.sample = NULL; 13266156681bSNamhyung Kim 1327b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 13286122e4e4SArnaldo Carvalho de Melo } 13296122e4e4SArnaldo Carvalho de Melo 13308115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 1331a1645ce1SZhang, Yanmin { 1332a1645ce1SZhang, Yanmin int err; 133345694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 1334a1645ce1SZhang, Yanmin /* 1335a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 1336a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 1337a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 1338a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 1339a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 1340a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 1341a1645ce1SZhang, Yanmin */ 134245694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 1343743eb868SArnaldo Carvalho de Melo machine); 1344a1645ce1SZhang, Yanmin if (err < 0) 1345a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 134623346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 1347a1645ce1SZhang, Yanmin 1348a1645ce1SZhang, Yanmin /* 1349a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 1350a1645ce1SZhang, Yanmin * have no _text sometimes. 1351a1645ce1SZhang, Yanmin */ 135245694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 13530ae617beSAdrian Hunter machine); 1354a1645ce1SZhang, Yanmin if (err < 0) 1355a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 135623346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 1357a1645ce1SZhang, Yanmin } 1358a1645ce1SZhang, Yanmin 135998402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 136098402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 136198402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 136298402807SFrederic Weisbecker }; 136398402807SFrederic Weisbecker 1364a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map) 1365f13de660SAlexey Budankov { 1366f13de660SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS && 1367396b626bSAlexey Bayduraev !bitmap_equal(thread->mask->affinity.bits, map->affinity_mask.bits, 1368396b626bSAlexey Bayduraev thread->mask->affinity.nbits)) { 1369396b626bSAlexey Bayduraev bitmap_zero(thread->mask->affinity.bits, thread->mask->affinity.nbits); 1370396b626bSAlexey Bayduraev bitmap_or(thread->mask->affinity.bits, thread->mask->affinity.bits, 1371396b626bSAlexey Bayduraev map->affinity_mask.bits, thread->mask->affinity.nbits); 1372396b626bSAlexey Bayduraev sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity), 1373396b626bSAlexey Bayduraev (cpu_set_t *)thread->mask->affinity.bits); 1374396b626bSAlexey Bayduraev if (verbose == 2) { 1375396b626bSAlexey Bayduraev pr_debug("threads[%d]: running on cpu%d: ", thread->tid, sched_getcpu()); 1376396b626bSAlexey Bayduraev mmap_cpu_mask__scnprintf(&thread->mask->affinity, "affinity"); 1377396b626bSAlexey Bayduraev } 1378f13de660SAlexey Budankov } 1379f13de660SAlexey Budankov } 1380f13de660SAlexey Budankov 13815d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment) 13825d7f4116SAlexey Budankov { 138372932371SJiri Olsa struct perf_record_compressed *event = record; 13845d7f4116SAlexey Budankov size_t size = sizeof(*event); 13855d7f4116SAlexey Budankov 13865d7f4116SAlexey Budankov if (increment) { 13875d7f4116SAlexey Budankov event->header.size += increment; 13885d7f4116SAlexey Budankov return increment; 13895d7f4116SAlexey Budankov } 13905d7f4116SAlexey Budankov 13915d7f4116SAlexey Budankov event->header.type = PERF_RECORD_COMPRESSED; 13925d7f4116SAlexey Budankov event->header.size = size; 13935d7f4116SAlexey Budankov 13945d7f4116SAlexey Budankov return size; 13955d7f4116SAlexey Budankov } 13965d7f4116SAlexey Budankov 13975d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size, 13985d7f4116SAlexey Budankov void *src, size_t src_size) 13995d7f4116SAlexey Budankov { 14005d7f4116SAlexey Budankov size_t compressed; 140172932371SJiri Olsa size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1; 14025d7f4116SAlexey Budankov 14035d7f4116SAlexey Budankov compressed = zstd_compress_stream_to_records(&session->zstd_data, dst, dst_size, src, src_size, 14045d7f4116SAlexey Budankov max_record_size, process_comp_header); 14055d7f4116SAlexey Budankov 14065d7f4116SAlexey Budankov session->bytes_transferred += src_size; 14075d7f4116SAlexey Budankov session->bytes_compressed += compressed; 14085d7f4116SAlexey Budankov 14095d7f4116SAlexey Budankov return compressed; 14105d7f4116SAlexey Budankov } 14115d7f4116SAlexey Budankov 141263503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist, 1413470530bbSAlexey Budankov bool overwrite, bool synch) 141498402807SFrederic Weisbecker { 1415dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 14160e2e63ddSPeter Zijlstra int i; 14178d3eca20SDavid Ahern int rc = 0; 1418396b626bSAlexey Bayduraev int nr_mmaps; 1419396b626bSAlexey Bayduraev struct mmap **maps; 1420d3d1af6fSAlexey Budankov int trace_fd = rec->data.file.fd; 1421ef781128SAlexey Budankov off_t off = 0; 142298402807SFrederic Weisbecker 1423cb21686bSWang Nan if (!evlist) 1424cb21686bSWang Nan return 0; 1425ef149c25SAdrian Hunter 1426396b626bSAlexey Bayduraev nr_mmaps = thread->nr_mmaps; 1427396b626bSAlexey Bayduraev maps = overwrite ? thread->overwrite_maps : thread->maps; 1428396b626bSAlexey Bayduraev 1429a4ea0ec4SWang Nan if (!maps) 1430a4ea0ec4SWang Nan return 0; 1431cb21686bSWang Nan 14320b72d69aSWang Nan if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 143354cc54deSWang Nan return 0; 143454cc54deSWang Nan 1435d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1436d3d1af6fSAlexey Budankov off = record__aio_get_pos(trace_fd); 1437d3d1af6fSAlexey Budankov 1438396b626bSAlexey Bayduraev for (i = 0; i < nr_mmaps; i++) { 1439470530bbSAlexey Budankov u64 flush = 0; 1440396b626bSAlexey Bayduraev struct mmap *map = maps[i]; 1441a4ea0ec4SWang Nan 1442547740f7SJiri Olsa if (map->core.base) { 1443f13de660SAlexey Budankov record__adjust_affinity(rec, map); 1444470530bbSAlexey Budankov if (synch) { 144565aa2e6bSJiri Olsa flush = map->core.flush; 144665aa2e6bSJiri Olsa map->core.flush = 1; 1447470530bbSAlexey Budankov } 1448d3d1af6fSAlexey Budankov if (!record__aio_enabled(rec)) { 1449ef781128SAlexey Budankov if (perf_mmap__push(map, rec, record__pushfn) < 0) { 1450470530bbSAlexey Budankov if (synch) 145165aa2e6bSJiri Olsa map->core.flush = flush; 14528d3eca20SDavid Ahern rc = -1; 14538d3eca20SDavid Ahern goto out; 14548d3eca20SDavid Ahern } 1455d3d1af6fSAlexey Budankov } else { 1456ef781128SAlexey Budankov if (record__aio_push(rec, map, &off) < 0) { 1457d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1458470530bbSAlexey Budankov if (synch) 145965aa2e6bSJiri Olsa map->core.flush = flush; 1460d3d1af6fSAlexey Budankov rc = -1; 1461d3d1af6fSAlexey Budankov goto out; 1462d3d1af6fSAlexey Budankov } 1463d3d1af6fSAlexey Budankov } 1464470530bbSAlexey Budankov if (synch) 146565aa2e6bSJiri Olsa map->core.flush = flush; 14668d3eca20SDavid Ahern } 1467ef149c25SAdrian Hunter 1468e035f4caSJiri Olsa if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode && 1469c0a6de06SAdrian Hunter !rec->opts.auxtrace_sample_mode && 1470e035f4caSJiri Olsa record__auxtrace_mmap_read(rec, map) != 0) { 1471ef149c25SAdrian Hunter rc = -1; 1472ef149c25SAdrian Hunter goto out; 1473ef149c25SAdrian Hunter } 147498402807SFrederic Weisbecker } 147598402807SFrederic Weisbecker 1476d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1477d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1478d3d1af6fSAlexey Budankov 1479dcabb507SJiri Olsa /* 1480dcabb507SJiri Olsa * Mark the round finished in case we wrote 1481dcabb507SJiri Olsa * at least one event. 148256f735ffSAlexey Bayduraev * 148356f735ffSAlexey Bayduraev * No need for round events in directory mode, 148456f735ffSAlexey Bayduraev * because per-cpu maps and files have data 148556f735ffSAlexey Bayduraev * sorted by kernel. 1486dcabb507SJiri Olsa */ 148756f735ffSAlexey Bayduraev if (!record__threads_enabled(rec) && bytes_written != rec->bytes_written) 1488ded2b8feSJiri Olsa rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event)); 14898d3eca20SDavid Ahern 14900b72d69aSWang Nan if (overwrite) 1491ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 14928d3eca20SDavid Ahern out: 14938d3eca20SDavid Ahern return rc; 149498402807SFrederic Weisbecker } 149598402807SFrederic Weisbecker 1496470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch) 1497cb21686bSWang Nan { 1498cb21686bSWang Nan int err; 1499cb21686bSWang Nan 1500470530bbSAlexey Budankov err = record__mmap_read_evlist(rec, rec->evlist, false, synch); 1501cb21686bSWang Nan if (err) 1502cb21686bSWang Nan return err; 1503cb21686bSWang Nan 1504470530bbSAlexey Budankov return record__mmap_read_evlist(rec, rec->evlist, true, synch); 1505cb21686bSWang Nan } 1506cb21686bSWang Nan 1507396b626bSAlexey Bayduraev static void record__thread_munmap_filtered(struct fdarray *fda, int fd, 1508396b626bSAlexey Bayduraev void *arg __maybe_unused) 1509396b626bSAlexey Bayduraev { 1510396b626bSAlexey Bayduraev struct perf_mmap *map = fda->priv[fd].ptr; 1511396b626bSAlexey Bayduraev 1512396b626bSAlexey Bayduraev if (map) 1513396b626bSAlexey Bayduraev perf_mmap__put(map); 1514396b626bSAlexey Bayduraev } 1515396b626bSAlexey Bayduraev 15163217e9feSAlexey Bayduraev static void *record__thread(void *arg) 15173217e9feSAlexey Bayduraev { 15183217e9feSAlexey Bayduraev enum thread_msg msg = THREAD_MSG__READY; 15193217e9feSAlexey Bayduraev bool terminate = false; 15203217e9feSAlexey Bayduraev struct fdarray *pollfd; 15213217e9feSAlexey Bayduraev int err, ctlfd_pos; 15223217e9feSAlexey Bayduraev 15233217e9feSAlexey Bayduraev thread = arg; 15243217e9feSAlexey Bayduraev thread->tid = gettid(); 15253217e9feSAlexey Bayduraev 15263217e9feSAlexey Bayduraev err = write(thread->pipes.ack[1], &msg, sizeof(msg)); 15273217e9feSAlexey Bayduraev if (err == -1) 15283217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to notify on start: %s\n", 15293217e9feSAlexey Bayduraev thread->tid, strerror(errno)); 15303217e9feSAlexey Bayduraev 15313217e9feSAlexey Bayduraev pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu()); 15323217e9feSAlexey Bayduraev 15333217e9feSAlexey Bayduraev pollfd = &thread->pollfd; 15343217e9feSAlexey Bayduraev ctlfd_pos = thread->ctlfd_pos; 15353217e9feSAlexey Bayduraev 15363217e9feSAlexey Bayduraev for (;;) { 15373217e9feSAlexey Bayduraev unsigned long long hits = thread->samples; 15383217e9feSAlexey Bayduraev 15393217e9feSAlexey Bayduraev if (record__mmap_read_all(thread->rec, false) < 0 || terminate) 15403217e9feSAlexey Bayduraev break; 15413217e9feSAlexey Bayduraev 15423217e9feSAlexey Bayduraev if (hits == thread->samples) { 15433217e9feSAlexey Bayduraev 15443217e9feSAlexey Bayduraev err = fdarray__poll(pollfd, -1); 15453217e9feSAlexey Bayduraev /* 15463217e9feSAlexey Bayduraev * Propagate error, only if there's any. Ignore positive 15473217e9feSAlexey Bayduraev * number of returned events and interrupt error. 15483217e9feSAlexey Bayduraev */ 15493217e9feSAlexey Bayduraev if (err > 0 || (err < 0 && errno == EINTR)) 15503217e9feSAlexey Bayduraev err = 0; 15513217e9feSAlexey Bayduraev thread->waking++; 15523217e9feSAlexey Bayduraev 15533217e9feSAlexey Bayduraev if (fdarray__filter(pollfd, POLLERR | POLLHUP, 15543217e9feSAlexey Bayduraev record__thread_munmap_filtered, NULL) == 0) 15553217e9feSAlexey Bayduraev break; 15563217e9feSAlexey Bayduraev } 15573217e9feSAlexey Bayduraev 15583217e9feSAlexey Bayduraev if (pollfd->entries[ctlfd_pos].revents & POLLHUP) { 15593217e9feSAlexey Bayduraev terminate = true; 15603217e9feSAlexey Bayduraev close(thread->pipes.msg[0]); 15613217e9feSAlexey Bayduraev thread->pipes.msg[0] = -1; 15623217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].fd = -1; 15633217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].events = 0; 15643217e9feSAlexey Bayduraev } 15653217e9feSAlexey Bayduraev 15663217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].revents = 0; 15673217e9feSAlexey Bayduraev } 15683217e9feSAlexey Bayduraev record__mmap_read_all(thread->rec, true); 15693217e9feSAlexey Bayduraev 15703217e9feSAlexey Bayduraev err = write(thread->pipes.ack[1], &msg, sizeof(msg)); 15713217e9feSAlexey Bayduraev if (err == -1) 15723217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to notify on termination: %s\n", 15733217e9feSAlexey Bayduraev thread->tid, strerror(errno)); 15743217e9feSAlexey Bayduraev 15753217e9feSAlexey Bayduraev return NULL; 15763217e9feSAlexey Bayduraev } 15773217e9feSAlexey Bayduraev 15788c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 157957706abcSDavid Ahern { 158057706abcSDavid Ahern struct perf_session *session = rec->session; 158157706abcSDavid Ahern int feat; 158257706abcSDavid Ahern 158357706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 158457706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 158557706abcSDavid Ahern 158657706abcSDavid Ahern if (rec->no_buildid) 158757706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 158857706abcSDavid Ahern 1589ce9036a6SJiri Olsa if (!have_tracepoints(&rec->evlist->core.entries)) 159057706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 159157706abcSDavid Ahern 159257706abcSDavid Ahern if (!rec->opts.branch_stack) 159357706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 1594ef149c25SAdrian Hunter 1595ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 1596ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 1597ffa517adSJiri Olsa 1598cf790516SAlexey Budankov if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns)) 1599cf790516SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_CLOCKID); 1600cf790516SAlexey Budankov 1601d1e325cfSJiri Olsa if (!rec->opts.use_clockid) 1602d1e325cfSJiri Olsa perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA); 1603d1e325cfSJiri Olsa 160456f735ffSAlexey Bayduraev if (!record__threads_enabled(rec)) 1605258031c0SJiri Olsa perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT); 160656f735ffSAlexey Bayduraev 160742e1fd80SAlexey Budankov if (!record__comp_enabled(rec)) 160842e1fd80SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_COMPRESSED); 1609258031c0SJiri Olsa 1610ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 161157706abcSDavid Ahern } 161257706abcSDavid Ahern 1613e1ab48baSWang Nan static void 1614e1ab48baSWang Nan record__finish_output(struct record *rec) 1615e1ab48baSWang Nan { 161656f735ffSAlexey Bayduraev int i; 16178ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 16188ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1619e1ab48baSWang Nan 16208ceb41d7SJiri Olsa if (data->is_pipe) 1621e1ab48baSWang Nan return; 1622e1ab48baSWang Nan 1623e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 162445112e89SJiri Olsa data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR); 162556f735ffSAlexey Bayduraev if (record__threads_enabled(rec)) { 162656f735ffSAlexey Bayduraev for (i = 0; i < data->dir.nr; i++) 162756f735ffSAlexey Bayduraev data->dir.files[i].size = lseek(data->dir.files[i].fd, 0, SEEK_CUR); 162856f735ffSAlexey Bayduraev } 1629e1ab48baSWang Nan 1630e1ab48baSWang Nan if (!rec->no_buildid) { 1631e1ab48baSWang Nan process_buildids(rec); 1632e1ab48baSWang Nan 1633e1ab48baSWang Nan if (rec->buildid_all) 1634e1ab48baSWang Nan dsos__hit_all(rec->session); 1635e1ab48baSWang Nan } 1636e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 1637e1ab48baSWang Nan 1638e1ab48baSWang Nan return; 1639e1ab48baSWang Nan } 1640e1ab48baSWang Nan 16414ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 1642be7b0c9eSWang Nan { 16439d6aae72SArnaldo Carvalho de Melo int err; 16449749b90eSJiri Olsa struct perf_thread_map *thread_map; 164541b740b6SNamhyung Kim bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP; 1646be7b0c9eSWang Nan 16474ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 16484ea648aeSWang Nan return 0; 16494ea648aeSWang Nan 16509d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 16519d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 16529d6aae72SArnaldo Carvalho de Melo return -1; 16539d6aae72SArnaldo Carvalho de Melo 16549d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 1655be7b0c9eSWang Nan process_synthesized_event, 1656be7b0c9eSWang Nan &rec->session->machines.host, 165741b740b6SNamhyung Kim needs_mmap, 16583fcb10e4SMark Drayton rec->opts.sample_address); 16597836e52eSJiri Olsa perf_thread_map__put(thread_map); 16609d6aae72SArnaldo Carvalho de Melo return err; 1661be7b0c9eSWang Nan } 1662be7b0c9eSWang Nan 16634ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 16643c1cb7e3SWang Nan 1665ecfd7a9cSWang Nan static int 1666ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 1667ecfd7a9cSWang Nan { 16688ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1669ecfd7a9cSWang Nan int fd, err; 167003724b2eSAndi Kleen char *new_filename; 1671ecfd7a9cSWang Nan 1672ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 1673ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 1674ecfd7a9cSWang Nan 1675d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1676d3d1af6fSAlexey Budankov 16774ea648aeSWang Nan record__synthesize(rec, true); 16784ea648aeSWang Nan if (target__none(&rec->opts.target)) 16794ea648aeSWang Nan record__synthesize_workload(rec, true); 16804ea648aeSWang Nan 1681ecfd7a9cSWang Nan rec->samples = 0; 1682ecfd7a9cSWang Nan record__finish_output(rec); 1683ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 1684ecfd7a9cSWang Nan if (err) { 1685ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 1686ecfd7a9cSWang Nan return -EINVAL; 1687ecfd7a9cSWang Nan } 1688ecfd7a9cSWang Nan 16898ceb41d7SJiri Olsa fd = perf_data__switch(data, timestamp, 1690ecfd7a9cSWang Nan rec->session->header.data_offset, 169103724b2eSAndi Kleen at_exit, &new_filename); 1692ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 1693ecfd7a9cSWang Nan rec->bytes_written = 0; 1694ecfd7a9cSWang Nan rec->session->header.data_size = 0; 1695ecfd7a9cSWang Nan } 1696ecfd7a9cSWang Nan 1697ecfd7a9cSWang Nan if (!quiet) 1698ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 16992d4f2799SJiri Olsa data->path, timestamp); 17003c1cb7e3SWang Nan 170103724b2eSAndi Kleen if (rec->switch_output.num_files) { 170203724b2eSAndi Kleen int n = rec->switch_output.cur_file + 1; 170303724b2eSAndi Kleen 170403724b2eSAndi Kleen if (n >= rec->switch_output.num_files) 170503724b2eSAndi Kleen n = 0; 170603724b2eSAndi Kleen rec->switch_output.cur_file = n; 170703724b2eSAndi Kleen if (rec->switch_output.filenames[n]) { 170803724b2eSAndi Kleen remove(rec->switch_output.filenames[n]); 1709d8f9da24SArnaldo Carvalho de Melo zfree(&rec->switch_output.filenames[n]); 171003724b2eSAndi Kleen } 171103724b2eSAndi Kleen rec->switch_output.filenames[n] = new_filename; 171203724b2eSAndi Kleen } else { 171303724b2eSAndi Kleen free(new_filename); 171403724b2eSAndi Kleen } 171503724b2eSAndi Kleen 17163c1cb7e3SWang Nan /* Output tracking events */ 1717be7b0c9eSWang Nan if (!at_exit) { 17184ea648aeSWang Nan record__synthesize(rec, false); 17193c1cb7e3SWang Nan 1720be7b0c9eSWang Nan /* 1721be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 1722be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 1723be7b0c9eSWang Nan * generate tracking events because there's no thread_map 1724be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 1725be7b0c9eSWang Nan * contain map and comm information. 1726be7b0c9eSWang Nan * Create a fake thread_map and directly call 1727be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 1728be7b0c9eSWang Nan */ 1729be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 17304ea648aeSWang Nan record__synthesize_workload(rec, false); 1731be7b0c9eSWang Nan } 1732ecfd7a9cSWang Nan return fd; 1733ecfd7a9cSWang Nan } 1734ecfd7a9cSWang Nan 1735f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno; 1736f33cbe72SArnaldo Carvalho de Melo 1737f33cbe72SArnaldo Carvalho de Melo /* 17387b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload will send a SIGUSR1 1739f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 1740f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 1741f33cbe72SArnaldo Carvalho de Melo */ 174245604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 174345604710SNamhyung Kim siginfo_t *info, 1744f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 1745f33cbe72SArnaldo Carvalho de Melo { 1746f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 1747f33cbe72SArnaldo Carvalho de Melo done = 1; 1748f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 1749f33cbe72SArnaldo Carvalho de Melo } 1750f33cbe72SArnaldo Carvalho de Melo 17512dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 1752bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 17532dd6d8a1SAdrian Hunter 1754db0ea13cSArnaldo Carvalho de Melo static const struct perf_event_mmap_page *evlist__pick_pc(struct evlist *evlist) 1755ee667f94SWang Nan { 1756b2cb615dSWang Nan if (evlist) { 1757547740f7SJiri Olsa if (evlist->mmap && evlist->mmap[0].core.base) 1758547740f7SJiri Olsa return evlist->mmap[0].core.base; 1759547740f7SJiri Olsa if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base) 1760547740f7SJiri Olsa return evlist->overwrite_mmap[0].core.base; 1761b2cb615dSWang Nan } 1762ee667f94SWang Nan return NULL; 1763ee667f94SWang Nan } 1764ee667f94SWang Nan 1765c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 1766c45628b0SWang Nan { 1767db0ea13cSArnaldo Carvalho de Melo const struct perf_event_mmap_page *pc = evlist__pick_pc(rec->evlist); 1768ee667f94SWang Nan if (pc) 1769ee667f94SWang Nan return pc; 1770c45628b0SWang Nan return NULL; 1771c45628b0SWang Nan } 1772c45628b0SWang Nan 17734ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 1774c45c86ebSWang Nan { 1775c45c86ebSWang Nan struct perf_session *session = rec->session; 1776c45c86ebSWang Nan struct machine *machine = &session->machines.host; 17778ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1778c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 1779c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 1780c45c86ebSWang Nan int err = 0; 1781d99c22eaSStephane Eranian event_op f = process_synthesized_event; 1782c45c86ebSWang Nan 17834ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 17844ea648aeSWang Nan return 0; 17854ea648aeSWang Nan 17868ceb41d7SJiri Olsa if (data->is_pipe) { 1787c3a057dcSNamhyung Kim err = perf_event__synthesize_for_pipe(tool, session, data, 1788c45c86ebSWang Nan process_synthesized_event); 1789c3a057dcSNamhyung Kim if (err < 0) 1790c45c86ebSWang Nan goto out; 1791c45c86ebSWang Nan 1792c45c86ebSWang Nan rec->bytes_written += err; 1793c45c86ebSWang Nan } 1794c45c86ebSWang Nan 1795c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 179646bc29b9SAdrian Hunter process_synthesized_event, machine); 179746bc29b9SAdrian Hunter if (err) 179846bc29b9SAdrian Hunter goto out; 179946bc29b9SAdrian Hunter 1800c0a6de06SAdrian Hunter /* Synthesize id_index before auxtrace_info */ 180161750473SAdrian Hunter if (rec->opts.auxtrace_sample_mode || rec->opts.full_auxtrace) { 1802c0a6de06SAdrian Hunter err = perf_event__synthesize_id_index(tool, 1803c0a6de06SAdrian Hunter process_synthesized_event, 1804c0a6de06SAdrian Hunter session->evlist, machine); 1805c0a6de06SAdrian Hunter if (err) 1806c0a6de06SAdrian Hunter goto out; 1807c0a6de06SAdrian Hunter } 1808c0a6de06SAdrian Hunter 1809c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 1810c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 1811c45c86ebSWang Nan session, process_synthesized_event); 1812c45c86ebSWang Nan if (err) 1813c45c86ebSWang Nan goto out; 1814c45c86ebSWang Nan } 1815c45c86ebSWang Nan 181678e1bc25SArnaldo Carvalho de Melo if (!evlist__exclude_kernel(rec->evlist)) { 1817c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 1818c45c86ebSWang Nan machine); 1819c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 1820c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1821c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 1822c45c86ebSWang Nan 1823c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 1824c45c86ebSWang Nan machine); 1825c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 1826c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1827c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 18286c443954SArnaldo Carvalho de Melo } 1829c45c86ebSWang Nan 1830c45c86ebSWang Nan if (perf_guest) { 1831c45c86ebSWang Nan machines__process_guests(&session->machines, 1832c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 1833c45c86ebSWang Nan } 1834c45c86ebSWang Nan 1835bfd8f72cSAndi Kleen err = perf_event__synthesize_extra_attr(&rec->tool, 1836bfd8f72cSAndi Kleen rec->evlist, 1837bfd8f72cSAndi Kleen process_synthesized_event, 1838bfd8f72cSAndi Kleen data->is_pipe); 1839bfd8f72cSAndi Kleen if (err) 1840bfd8f72cSAndi Kleen goto out; 1841bfd8f72cSAndi Kleen 184203617c22SJiri Olsa err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads, 1843373565d2SAndi Kleen process_synthesized_event, 1844373565d2SAndi Kleen NULL); 1845373565d2SAndi Kleen if (err < 0) { 1846373565d2SAndi Kleen pr_err("Couldn't synthesize thread map.\n"); 1847373565d2SAndi Kleen return err; 1848373565d2SAndi Kleen } 1849373565d2SAndi Kleen 1850f72f901dSJiri Olsa err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.cpus, 1851373565d2SAndi Kleen process_synthesized_event, NULL); 1852373565d2SAndi Kleen if (err < 0) { 1853373565d2SAndi Kleen pr_err("Couldn't synthesize cpu map.\n"); 1854373565d2SAndi Kleen return err; 1855373565d2SAndi Kleen } 1856373565d2SAndi Kleen 1857e5416950SSong Liu err = perf_event__synthesize_bpf_events(session, process_synthesized_event, 18587b612e29SSong Liu machine, opts); 18597b612e29SSong Liu if (err < 0) 18607b612e29SSong Liu pr_warning("Couldn't synthesize bpf events.\n"); 18617b612e29SSong Liu 186241b740b6SNamhyung Kim if (rec->opts.synth & PERF_SYNTH_CGROUP) { 1863ab64069fSNamhyung Kim err = perf_event__synthesize_cgroups(tool, process_synthesized_event, 1864ab64069fSNamhyung Kim machine); 1865ab64069fSNamhyung Kim if (err < 0) 1866ab64069fSNamhyung Kim pr_warning("Couldn't synthesize cgroup events.\n"); 186741b740b6SNamhyung Kim } 1868ab64069fSNamhyung Kim 1869d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) { 1870d99c22eaSStephane Eranian perf_set_multithreaded(); 1871d99c22eaSStephane Eranian f = process_locked_synthesized_event; 1872d99c22eaSStephane Eranian } 1873d99c22eaSStephane Eranian 187441b740b6SNamhyung Kim if (rec->opts.synth & PERF_SYNTH_TASK) { 187541b740b6SNamhyung Kim bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP; 187641b740b6SNamhyung Kim 187784111b9cSNamhyung Kim err = __machine__synthesize_threads(machine, tool, &opts->target, 187884111b9cSNamhyung Kim rec->evlist->core.threads, 187941b740b6SNamhyung Kim f, needs_mmap, opts->sample_address, 1880d99c22eaSStephane Eranian rec->opts.nr_threads_synthesize); 188141b740b6SNamhyung Kim } 1882d99c22eaSStephane Eranian 1883d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) 1884d99c22eaSStephane Eranian perf_set_singlethreaded(); 1885d99c22eaSStephane Eranian 1886c45c86ebSWang Nan out: 1887c45c86ebSWang Nan return err; 1888c45c86ebSWang Nan } 1889c45c86ebSWang Nan 1890899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data) 1891899e5ffbSArnaldo Carvalho de Melo { 1892899e5ffbSArnaldo Carvalho de Melo struct record *rec = data; 1893899e5ffbSArnaldo Carvalho de Melo pthread_kill(rec->thread_id, SIGUSR2); 1894899e5ffbSArnaldo Carvalho de Melo return 0; 1895899e5ffbSArnaldo Carvalho de Melo } 1896899e5ffbSArnaldo Carvalho de Melo 189723cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec) 189823cbb41cSArnaldo Carvalho de Melo { 189923cbb41cSArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 190023cbb41cSArnaldo Carvalho de Melo 190123cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist != NULL) { 190223cbb41cSArnaldo Carvalho de Melo /* 190323cbb41cSArnaldo Carvalho de Melo * We get here if --switch-output-event populated the 190423cbb41cSArnaldo Carvalho de Melo * sb_evlist, so associate a callback that will send a SIGUSR2 190523cbb41cSArnaldo Carvalho de Melo * to the main thread. 190623cbb41cSArnaldo Carvalho de Melo */ 190723cbb41cSArnaldo Carvalho de Melo evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec); 190823cbb41cSArnaldo Carvalho de Melo rec->thread_id = pthread_self(); 190923cbb41cSArnaldo Carvalho de Melo } 19101101c872SJin Yao #ifdef HAVE_LIBBPF_SUPPORT 191123cbb41cSArnaldo Carvalho de Melo if (!opts->no_bpf_event) { 191223cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 191323cbb41cSArnaldo Carvalho de Melo rec->sb_evlist = evlist__new(); 191423cbb41cSArnaldo Carvalho de Melo 191523cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 191623cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't create side band evlist.\n."); 191723cbb41cSArnaldo Carvalho de Melo return -1; 191823cbb41cSArnaldo Carvalho de Melo } 191923cbb41cSArnaldo Carvalho de Melo } 192023cbb41cSArnaldo Carvalho de Melo 192123cbb41cSArnaldo Carvalho de Melo if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) { 192223cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n."); 192323cbb41cSArnaldo Carvalho de Melo return -1; 192423cbb41cSArnaldo Carvalho de Melo } 192523cbb41cSArnaldo Carvalho de Melo } 19261101c872SJin Yao #endif 192708c83997SArnaldo Carvalho de Melo if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) { 192823cbb41cSArnaldo Carvalho de Melo pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n"); 192923cbb41cSArnaldo Carvalho de Melo opts->no_bpf_event = true; 193023cbb41cSArnaldo Carvalho de Melo } 193123cbb41cSArnaldo Carvalho de Melo 193223cbb41cSArnaldo Carvalho de Melo return 0; 193323cbb41cSArnaldo Carvalho de Melo } 193423cbb41cSArnaldo Carvalho de Melo 1935d1e325cfSJiri Olsa static int record__init_clock(struct record *rec) 1936d1e325cfSJiri Olsa { 1937d1e325cfSJiri Olsa struct perf_session *session = rec->session; 1938d1e325cfSJiri Olsa struct timespec ref_clockid; 1939d1e325cfSJiri Olsa struct timeval ref_tod; 1940d1e325cfSJiri Olsa u64 ref; 1941d1e325cfSJiri Olsa 1942d1e325cfSJiri Olsa if (!rec->opts.use_clockid) 1943d1e325cfSJiri Olsa return 0; 1944d1e325cfSJiri Olsa 19459d88a1a1SJiri Olsa if (rec->opts.use_clockid && rec->opts.clockid_res_ns) 19469d88a1a1SJiri Olsa session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns; 19479d88a1a1SJiri Olsa 1948d1e325cfSJiri Olsa session->header.env.clock.clockid = rec->opts.clockid; 1949d1e325cfSJiri Olsa 1950d1e325cfSJiri Olsa if (gettimeofday(&ref_tod, NULL) != 0) { 1951d1e325cfSJiri Olsa pr_err("gettimeofday failed, cannot set reference time.\n"); 1952d1e325cfSJiri Olsa return -1; 1953d1e325cfSJiri Olsa } 1954d1e325cfSJiri Olsa 1955d1e325cfSJiri Olsa if (clock_gettime(rec->opts.clockid, &ref_clockid)) { 1956d1e325cfSJiri Olsa pr_err("clock_gettime failed, cannot set reference time.\n"); 1957d1e325cfSJiri Olsa return -1; 1958d1e325cfSJiri Olsa } 1959d1e325cfSJiri Olsa 1960d1e325cfSJiri Olsa ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC + 1961d1e325cfSJiri Olsa (u64) ref_tod.tv_usec * NSEC_PER_USEC; 1962d1e325cfSJiri Olsa 1963d1e325cfSJiri Olsa session->header.env.clock.tod_ns = ref; 1964d1e325cfSJiri Olsa 1965d1e325cfSJiri Olsa ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC + 1966d1e325cfSJiri Olsa (u64) ref_clockid.tv_nsec; 1967d1e325cfSJiri Olsa 1968d1e325cfSJiri Olsa session->header.env.clock.clockid_ns = ref; 1969d1e325cfSJiri Olsa return 0; 1970d1e325cfSJiri Olsa } 1971d1e325cfSJiri Olsa 1972d20aff15SAdrian Hunter static void hit_auxtrace_snapshot_trigger(struct record *rec) 1973d20aff15SAdrian Hunter { 1974d20aff15SAdrian Hunter if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 1975d20aff15SAdrian Hunter trigger_hit(&auxtrace_snapshot_trigger); 1976d20aff15SAdrian Hunter auxtrace_record__snapshot_started = 1; 1977d20aff15SAdrian Hunter if (auxtrace_record__snapshot_start(rec->itr)) 1978d20aff15SAdrian Hunter trigger_error(&auxtrace_snapshot_trigger); 1979d20aff15SAdrian Hunter } 1980d20aff15SAdrian Hunter } 1981d20aff15SAdrian Hunter 198291c0f5ecSJin Yao static void record__uniquify_name(struct record *rec) 198391c0f5ecSJin Yao { 198491c0f5ecSJin Yao struct evsel *pos; 198591c0f5ecSJin Yao struct evlist *evlist = rec->evlist; 198691c0f5ecSJin Yao char *new_name; 198791c0f5ecSJin Yao int ret; 198891c0f5ecSJin Yao 198991c0f5ecSJin Yao if (!perf_pmu__has_hybrid()) 199091c0f5ecSJin Yao return; 199191c0f5ecSJin Yao 199291c0f5ecSJin Yao evlist__for_each_entry(evlist, pos) { 199391c0f5ecSJin Yao if (!evsel__is_hybrid(pos)) 199491c0f5ecSJin Yao continue; 199591c0f5ecSJin Yao 199691c0f5ecSJin Yao if (strchr(pos->name, '/')) 199791c0f5ecSJin Yao continue; 199891c0f5ecSJin Yao 199991c0f5ecSJin Yao ret = asprintf(&new_name, "%s/%s/", 200091c0f5ecSJin Yao pos->pmu_name, pos->name); 200191c0f5ecSJin Yao if (ret) { 200291c0f5ecSJin Yao free(pos->name); 200391c0f5ecSJin Yao pos->name = new_name; 200491c0f5ecSJin Yao } 200591c0f5ecSJin Yao } 200691c0f5ecSJin Yao } 200791c0f5ecSJin Yao 20081e5de7d9SAlexey Bayduraev static int record__terminate_thread(struct record_thread *thread_data) 20091e5de7d9SAlexey Bayduraev { 20101e5de7d9SAlexey Bayduraev int err; 20111e5de7d9SAlexey Bayduraev enum thread_msg ack = THREAD_MSG__UNDEFINED; 20121e5de7d9SAlexey Bayduraev pid_t tid = thread_data->tid; 20131e5de7d9SAlexey Bayduraev 20141e5de7d9SAlexey Bayduraev close(thread_data->pipes.msg[1]); 20151e5de7d9SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 20161e5de7d9SAlexey Bayduraev err = read(thread_data->pipes.ack[0], &ack, sizeof(ack)); 20171e5de7d9SAlexey Bayduraev if (err > 0) 20181e5de7d9SAlexey Bayduraev pr_debug2("threads[%d]: sent %s\n", tid, thread_msg_tags[ack]); 20191e5de7d9SAlexey Bayduraev else 20201e5de7d9SAlexey Bayduraev pr_warning("threads[%d]: failed to receive termination notification from %d\n", 20211e5de7d9SAlexey Bayduraev thread->tid, tid); 20221e5de7d9SAlexey Bayduraev 20231e5de7d9SAlexey Bayduraev return 0; 20241e5de7d9SAlexey Bayduraev } 20251e5de7d9SAlexey Bayduraev 2026396b626bSAlexey Bayduraev static int record__start_threads(struct record *rec) 2027396b626bSAlexey Bayduraev { 20283217e9feSAlexey Bayduraev int t, tt, err, ret = 0, nr_threads = rec->nr_threads; 2029396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 20303217e9feSAlexey Bayduraev sigset_t full, mask; 20313217e9feSAlexey Bayduraev pthread_t handle; 20323217e9feSAlexey Bayduraev pthread_attr_t attrs; 2033396b626bSAlexey Bayduraev 2034396b626bSAlexey Bayduraev thread = &thread_data[0]; 2035396b626bSAlexey Bayduraev 20363217e9feSAlexey Bayduraev if (!record__threads_enabled(rec)) 20373217e9feSAlexey Bayduraev return 0; 20383217e9feSAlexey Bayduraev 20393217e9feSAlexey Bayduraev sigfillset(&full); 20403217e9feSAlexey Bayduraev if (sigprocmask(SIG_SETMASK, &full, &mask)) { 20413217e9feSAlexey Bayduraev pr_err("Failed to block signals on threads start: %s\n", strerror(errno)); 20423217e9feSAlexey Bayduraev return -1; 20433217e9feSAlexey Bayduraev } 20443217e9feSAlexey Bayduraev 20453217e9feSAlexey Bayduraev pthread_attr_init(&attrs); 20463217e9feSAlexey Bayduraev pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED); 20473217e9feSAlexey Bayduraev 20483217e9feSAlexey Bayduraev for (t = 1; t < nr_threads; t++) { 20493217e9feSAlexey Bayduraev enum thread_msg msg = THREAD_MSG__UNDEFINED; 20503217e9feSAlexey Bayduraev 20513217e9feSAlexey Bayduraev #ifdef HAVE_PTHREAD_ATTR_SETAFFINITY_NP 20523217e9feSAlexey Bayduraev pthread_attr_setaffinity_np(&attrs, 20533217e9feSAlexey Bayduraev MMAP_CPU_MASK_BYTES(&(thread_data[t].mask->affinity)), 20543217e9feSAlexey Bayduraev (cpu_set_t *)(thread_data[t].mask->affinity.bits)); 20553217e9feSAlexey Bayduraev #endif 20563217e9feSAlexey Bayduraev if (pthread_create(&handle, &attrs, record__thread, &thread_data[t])) { 20573217e9feSAlexey Bayduraev for (tt = 1; tt < t; tt++) 20583217e9feSAlexey Bayduraev record__terminate_thread(&thread_data[t]); 20593217e9feSAlexey Bayduraev pr_err("Failed to start threads: %s\n", strerror(errno)); 20603217e9feSAlexey Bayduraev ret = -1; 20613217e9feSAlexey Bayduraev goto out_err; 20623217e9feSAlexey Bayduraev } 20633217e9feSAlexey Bayduraev 20643217e9feSAlexey Bayduraev err = read(thread_data[t].pipes.ack[0], &msg, sizeof(msg)); 20653217e9feSAlexey Bayduraev if (err > 0) 20663217e9feSAlexey Bayduraev pr_debug2("threads[%d]: sent %s\n", rec->thread_data[t].tid, 20673217e9feSAlexey Bayduraev thread_msg_tags[msg]); 20683217e9feSAlexey Bayduraev else 20693217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to receive start notification from %d\n", 20703217e9feSAlexey Bayduraev thread->tid, rec->thread_data[t].tid); 20713217e9feSAlexey Bayduraev } 20723217e9feSAlexey Bayduraev 20733217e9feSAlexey Bayduraev sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity), 20743217e9feSAlexey Bayduraev (cpu_set_t *)thread->mask->affinity.bits); 20753217e9feSAlexey Bayduraev 2076396b626bSAlexey Bayduraev pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu()); 2077396b626bSAlexey Bayduraev 20783217e9feSAlexey Bayduraev out_err: 20793217e9feSAlexey Bayduraev pthread_attr_destroy(&attrs); 20803217e9feSAlexey Bayduraev 20813217e9feSAlexey Bayduraev if (sigprocmask(SIG_SETMASK, &mask, NULL)) { 20823217e9feSAlexey Bayduraev pr_err("Failed to unblock signals on threads start: %s\n", strerror(errno)); 20833217e9feSAlexey Bayduraev ret = -1; 20843217e9feSAlexey Bayduraev } 20853217e9feSAlexey Bayduraev 20863217e9feSAlexey Bayduraev return ret; 2087396b626bSAlexey Bayduraev } 2088396b626bSAlexey Bayduraev 2089396b626bSAlexey Bayduraev static int record__stop_threads(struct record *rec) 2090396b626bSAlexey Bayduraev { 2091396b626bSAlexey Bayduraev int t; 2092396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 2093396b626bSAlexey Bayduraev 20941e5de7d9SAlexey Bayduraev for (t = 1; t < rec->nr_threads; t++) 20951e5de7d9SAlexey Bayduraev record__terminate_thread(&thread_data[t]); 20961e5de7d9SAlexey Bayduraev 2097396b626bSAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 2098396b626bSAlexey Bayduraev rec->samples += thread_data[t].samples; 2099396b626bSAlexey Bayduraev 2100396b626bSAlexey Bayduraev return 0; 2101396b626bSAlexey Bayduraev } 2102396b626bSAlexey Bayduraev 2103396b626bSAlexey Bayduraev static unsigned long record__waking(struct record *rec) 2104396b626bSAlexey Bayduraev { 2105396b626bSAlexey Bayduraev int t; 2106396b626bSAlexey Bayduraev unsigned long waking = 0; 2107396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 2108396b626bSAlexey Bayduraev 2109396b626bSAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 2110396b626bSAlexey Bayduraev waking += thread_data[t].waking; 2111396b626bSAlexey Bayduraev 2112396b626bSAlexey Bayduraev return waking; 2113396b626bSAlexey Bayduraev } 2114396b626bSAlexey Bayduraev 21158c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 211686470930SIngo Molnar { 211757706abcSDavid Ahern int err; 211845604710SNamhyung Kim int status = 0; 211946be604bSZhang, Yanmin const bool forks = argc > 0; 212045694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 2121b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 21228ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 2123d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 21246dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 212542aa276fSNamhyung Kim int fd; 2126d3c8c08eSAlexey Budankov float ratio = 0; 2127acce0223SAlexey Budankov enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED; 212886470930SIngo Molnar 212945604710SNamhyung Kim atexit(record__sig_exit); 2130f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 2131f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 2132804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 2133a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 2134c0bdc1c4SWang Nan 2135f3b3614aSHari Bathini if (rec->opts.record_namespaces) 2136f3b3614aSHari Bathini tool->namespace_events = true; 2137f3b3614aSHari Bathini 21388fb4b679SNamhyung Kim if (rec->opts.record_cgroup) { 21398fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE 21408fb4b679SNamhyung Kim tool->cgroup_events = true; 21418fb4b679SNamhyung Kim #else 21428fb4b679SNamhyung Kim pr_err("cgroup tracking is not supported\n"); 21438fb4b679SNamhyung Kim return -1; 21448fb4b679SNamhyung Kim #endif 21458fb4b679SNamhyung Kim } 21468fb4b679SNamhyung Kim 2147dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 21482dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 21493c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 21505f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 2151dc0c6127SJiri Olsa if (rec->switch_output.enabled) 21523c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 2153c0bdc1c4SWang Nan } else { 21542dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 2155c0bdc1c4SWang Nan } 2156f5970550SPeter Zijlstra 21572681bd85SNamhyung Kim session = perf_session__new(data, tool); 21586ef81c55SMamatha Inamdar if (IS_ERR(session)) { 2159ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 21606ef81c55SMamatha Inamdar return PTR_ERR(session); 2161a9a70bbcSArnaldo Carvalho de Melo } 2162a9a70bbcSArnaldo Carvalho de Melo 21638ceb41d7SJiri Olsa fd = perf_data__fd(data); 2164d20deb64SArnaldo Carvalho de Melo rec->session = session; 2165d20deb64SArnaldo Carvalho de Melo 21665d7f4116SAlexey Budankov if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) { 21675d7f4116SAlexey Budankov pr_err("Compression initialization failed.\n"); 21685d7f4116SAlexey Budankov return -1; 21695d7f4116SAlexey Budankov } 2170da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 2171da231338SAnand K Mistry done_fd = eventfd(0, EFD_NONBLOCK); 2172da231338SAnand K Mistry if (done_fd < 0) { 2173da231338SAnand K Mistry pr_err("Failed to create wakeup eventfd, error: %m\n"); 2174da231338SAnand K Mistry status = -1; 2175da231338SAnand K Mistry goto out_delete_session; 2176da231338SAnand K Mistry } 2177e16c2ce7SYang Jihong err = evlist__add_wakeup_eventfd(rec->evlist, done_fd); 2178da231338SAnand K Mistry if (err < 0) { 2179da231338SAnand K Mistry pr_err("Failed to add wakeup eventfd to poll list\n"); 2180da231338SAnand K Mistry status = err; 2181da231338SAnand K Mistry goto out_delete_session; 2182da231338SAnand K Mistry } 2183da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT 21845d7f4116SAlexey Budankov 21855d7f4116SAlexey Budankov session->header.env.comp_type = PERF_COMP_ZSTD; 21865d7f4116SAlexey Budankov session->header.env.comp_level = rec->opts.comp_level; 21875d7f4116SAlexey Budankov 2188eeb399b5SAdrian Hunter if (rec->opts.kcore && 2189eeb399b5SAdrian Hunter !record__kcore_readable(&session->machines.host)) { 2190eeb399b5SAdrian Hunter pr_err("ERROR: kcore is not readable.\n"); 2191eeb399b5SAdrian Hunter return -1; 2192eeb399b5SAdrian Hunter } 2193eeb399b5SAdrian Hunter 2194d1e325cfSJiri Olsa if (record__init_clock(rec)) 2195d1e325cfSJiri Olsa return -1; 2196d1e325cfSJiri Olsa 21978c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 2198330aa675SStephane Eranian 2199d4db3f16SArnaldo Carvalho de Melo if (forks) { 22007b392ef0SArnaldo Carvalho de Melo err = evlist__prepare_workload(rec->evlist, &opts->target, argv, data->is_pipe, 2201735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 220235b9d88eSArnaldo Carvalho de Melo if (err < 0) { 220335b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 220445604710SNamhyung Kim status = err; 220535b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 2206856e9660SPeter Zijlstra } 2207856e9660SPeter Zijlstra } 2208856e9660SPeter Zijlstra 2209ad46e48cSJiri Olsa /* 2210ad46e48cSJiri Olsa * If we have just single event and are sending data 2211ad46e48cSJiri Olsa * through pipe, we need to force the ids allocation, 2212ad46e48cSJiri Olsa * because we synthesize event name through the pipe 2213ad46e48cSJiri Olsa * and need the id for that. 2214ad46e48cSJiri Olsa */ 22156484d2f9SJiri Olsa if (data->is_pipe && rec->evlist->core.nr_entries == 1) 2216ad46e48cSJiri Olsa rec->opts.sample_id = true; 2217ad46e48cSJiri Olsa 221891c0f5ecSJin Yao record__uniquify_name(rec); 221991c0f5ecSJin Yao 22208c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 22218d3eca20SDavid Ahern err = -1; 2222396b626bSAlexey Bayduraev goto out_free_threads; 22238d3eca20SDavid Ahern } 2224f6fa4375SJiri Olsa session->header.env.comp_mmap_len = session->evlist->core.mmap_len; 222586470930SIngo Molnar 2226eeb399b5SAdrian Hunter if (rec->opts.kcore) { 2227eeb399b5SAdrian Hunter err = record__kcore_copy(&session->machines.host, data); 2228eeb399b5SAdrian Hunter if (err) { 2229eeb399b5SAdrian Hunter pr_err("ERROR: Failed to copy kcore\n"); 2230396b626bSAlexey Bayduraev goto out_free_threads; 2231eeb399b5SAdrian Hunter } 2232eeb399b5SAdrian Hunter } 2233eeb399b5SAdrian Hunter 22348690a2a7SWang Nan err = bpf__apply_obj_config(); 22358690a2a7SWang Nan if (err) { 22368690a2a7SWang Nan char errbuf[BUFSIZ]; 22378690a2a7SWang Nan 22388690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 22398690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 22408690a2a7SWang Nan errbuf); 2241396b626bSAlexey Bayduraev goto out_free_threads; 22428690a2a7SWang Nan } 22438690a2a7SWang Nan 2244cca8482cSAdrian Hunter /* 2245cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 2246cca8482cSAdrian Hunter * evlist. 2247cca8482cSAdrian Hunter */ 22488cedf3a5SArnaldo Carvalho de Melo if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) { 2249cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 2250cca8482cSAdrian Hunter rec->tool.ordered_events = false; 2251cca8482cSAdrian Hunter } 2252cca8482cSAdrian Hunter 22533a683120SJiri Olsa if (!rec->evlist->core.nr_groups) 2254a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 2255a8bb559bSNamhyung Kim 22568ceb41d7SJiri Olsa if (data->is_pipe) { 225742aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 2258529870e3STom Zanussi if (err < 0) 2259396b626bSAlexey Bayduraev goto out_free_threads; 2260563aecb2SJiri Olsa } else { 226142aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 2262d5eed904SArnaldo Carvalho de Melo if (err < 0) 2263396b626bSAlexey Bayduraev goto out_free_threads; 2264d5eed904SArnaldo Carvalho de Melo } 22657c6a1c65SPeter Zijlstra 2266b38d85efSArnaldo Carvalho de Melo err = -1; 2267d3665498SDavid Ahern if (!rec->no_buildid 2268e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 2269d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 2270e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 2271396b626bSAlexey Bayduraev goto out_free_threads; 2272e20960c0SRobert Richter } 2273e20960c0SRobert Richter 227423cbb41cSArnaldo Carvalho de Melo err = record__setup_sb_evlist(rec); 227523cbb41cSArnaldo Carvalho de Melo if (err) 2276396b626bSAlexey Bayduraev goto out_free_threads; 2277657ee553SSong Liu 22784ea648aeSWang Nan err = record__synthesize(rec, false); 2279c45c86ebSWang Nan if (err < 0) 2280396b626bSAlexey Bayduraev goto out_free_threads; 22818d3eca20SDavid Ahern 2282d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 228386470930SIngo Molnar struct sched_param param; 228486470930SIngo Molnar 2285d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 228686470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 22876beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 22888d3eca20SDavid Ahern err = -1; 2289396b626bSAlexey Bayduraev goto out_free_threads; 229086470930SIngo Molnar } 229186470930SIngo Molnar } 229286470930SIngo Molnar 2293396b626bSAlexey Bayduraev if (record__start_threads(rec)) 2294396b626bSAlexey Bayduraev goto out_free_threads; 2295396b626bSAlexey Bayduraev 2296774cb499SJiri Olsa /* 2297774cb499SJiri Olsa * When perf is starting the traced process, all the events 2298774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 2299774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 2300774cb499SJiri Olsa */ 23016619a53eSAndi Kleen if (!target__none(&opts->target) && !opts->initial_delay) 23021c87f165SJiri Olsa evlist__enable(rec->evlist); 2303764e16a3SDavid Ahern 2304856e9660SPeter Zijlstra /* 2305856e9660SPeter Zijlstra * Let the child rip 2306856e9660SPeter Zijlstra */ 2307e803cf97SNamhyung Kim if (forks) { 230820a8a3cfSJiri Olsa struct machine *machine = &session->machines.host; 2309e5bed564SNamhyung Kim union perf_event *event; 2310e907caf3SHari Bathini pid_t tgid; 2311e5bed564SNamhyung Kim 2312e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 2313e5bed564SNamhyung Kim if (event == NULL) { 2314e5bed564SNamhyung Kim err = -ENOMEM; 2315e5bed564SNamhyung Kim goto out_child; 2316e5bed564SNamhyung Kim } 2317e5bed564SNamhyung Kim 2318e803cf97SNamhyung Kim /* 2319e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 2320e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 2321e803cf97SNamhyung Kim * cannot see a correct process name for those events. 2322e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 2323e803cf97SNamhyung Kim */ 2324e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 2325e803cf97SNamhyung Kim rec->evlist->workload.pid, 2326e803cf97SNamhyung Kim process_synthesized_event, 2327e803cf97SNamhyung Kim machine); 2328e5bed564SNamhyung Kim free(event); 2329e803cf97SNamhyung Kim 2330e907caf3SHari Bathini if (tgid == -1) 2331e907caf3SHari Bathini goto out_child; 2332e907caf3SHari Bathini 2333e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 2334e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 2335e907caf3SHari Bathini machine->id_hdr_size); 2336e907caf3SHari Bathini if (event == NULL) { 2337e907caf3SHari Bathini err = -ENOMEM; 2338e907caf3SHari Bathini goto out_child; 2339e907caf3SHari Bathini } 2340e907caf3SHari Bathini 2341e907caf3SHari Bathini /* 2342e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 2343e907caf3SHari Bathini */ 2344e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 2345e907caf3SHari Bathini rec->evlist->workload.pid, 2346e907caf3SHari Bathini tgid, process_synthesized_event, 2347e907caf3SHari Bathini machine); 2348e907caf3SHari Bathini free(event); 2349e907caf3SHari Bathini 23507b392ef0SArnaldo Carvalho de Melo evlist__start_workload(rec->evlist); 2351e803cf97SNamhyung Kim } 2352856e9660SPeter Zijlstra 23536619a53eSAndi Kleen if (opts->initial_delay) { 235468cd3b45SAlexey Budankov pr_info(EVLIST_DISABLED_MSG); 235568cd3b45SAlexey Budankov if (opts->initial_delay > 0) { 23560693e680SArnaldo Carvalho de Melo usleep(opts->initial_delay * USEC_PER_MSEC); 23571c87f165SJiri Olsa evlist__enable(rec->evlist); 235868cd3b45SAlexey Budankov pr_info(EVLIST_ENABLED_MSG); 235968cd3b45SAlexey Budankov } 23606619a53eSAndi Kleen } 23616619a53eSAndi Kleen 23625f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 23633c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 2364a074865eSWang Nan perf_hooks__invoke_record_start(); 2365649c48a9SPeter Zijlstra for (;;) { 2366396b626bSAlexey Bayduraev unsigned long long hits = thread->samples; 236786470930SIngo Molnar 236805737464SWang Nan /* 236905737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 237005737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 237105737464SWang Nan * hits != rec->samples in previous round. 237205737464SWang Nan * 2373ade9d208SArnaldo Carvalho de Melo * evlist__toggle_bkw_mmap ensure we never 237405737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 237505737464SWang Nan */ 237605737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 2377ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 237805737464SWang Nan 2379470530bbSAlexey Budankov if (record__mmap_read_all(rec, false) < 0) { 23805f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 23813c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 23828d3eca20SDavid Ahern err = -1; 238345604710SNamhyung Kim goto out_child; 23848d3eca20SDavid Ahern } 238586470930SIngo Molnar 23862dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 23872dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 23885f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 2389ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, false); 23905f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 23912dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 23922dd6d8a1SAdrian Hunter err = -1; 23932dd6d8a1SAdrian Hunter goto out_child; 23942dd6d8a1SAdrian Hunter } 23952dd6d8a1SAdrian Hunter } 23962dd6d8a1SAdrian Hunter 23973c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 239805737464SWang Nan /* 239905737464SWang Nan * If switch_output_trigger is hit, the data in 240005737464SWang Nan * overwritable ring buffer should have been collected, 240105737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 240205737464SWang Nan * 240305737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 240405737464SWang Nan * record__mmap_read_all() didn't collect data from 240505737464SWang Nan * overwritable ring buffer. Read again. 240605737464SWang Nan */ 240705737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 240805737464SWang Nan continue; 24093c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 24103c1cb7e3SWang Nan 241105737464SWang Nan /* 241205737464SWang Nan * Reenable events in overwrite ring buffer after 241305737464SWang Nan * record__mmap_read_all(): we should have collected 241405737464SWang Nan * data from it. 241505737464SWang Nan */ 2416ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 241705737464SWang Nan 24183c1cb7e3SWang Nan if (!quiet) 24193c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 2420396b626bSAlexey Bayduraev record__waking(rec)); 2421396b626bSAlexey Bayduraev thread->waking = 0; 24223c1cb7e3SWang Nan fd = record__switch_output(rec, false); 24233c1cb7e3SWang Nan if (fd < 0) { 24243c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 24253c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 24263c1cb7e3SWang Nan err = fd; 24273c1cb7e3SWang Nan goto out_child; 24283c1cb7e3SWang Nan } 2429bfacbe3bSJiri Olsa 2430bfacbe3bSJiri Olsa /* re-arm the alarm */ 2431bfacbe3bSJiri Olsa if (rec->switch_output.time) 2432bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 24333c1cb7e3SWang Nan } 24343c1cb7e3SWang Nan 2435396b626bSAlexey Bayduraev if (hits == thread->samples) { 24366dcf45efSArnaldo Carvalho de Melo if (done || draining) 2437649c48a9SPeter Zijlstra break; 2438396b626bSAlexey Bayduraev err = fdarray__poll(&thread->pollfd, -1); 2439a515114fSJiri Olsa /* 2440a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 2441a515114fSJiri Olsa * number of returned events and interrupt error. 2442a515114fSJiri Olsa */ 2443a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 244445604710SNamhyung Kim err = 0; 2445396b626bSAlexey Bayduraev thread->waking++; 24466dcf45efSArnaldo Carvalho de Melo 2447396b626bSAlexey Bayduraev if (fdarray__filter(&thread->pollfd, POLLERR | POLLHUP, 2448396b626bSAlexey Bayduraev record__thread_munmap_filtered, NULL) == 0) 24496dcf45efSArnaldo Carvalho de Melo draining = true; 2450396b626bSAlexey Bayduraev 2451396b626bSAlexey Bayduraev evlist__ctlfd_update(rec->evlist, 2452396b626bSAlexey Bayduraev &thread->pollfd.entries[thread->ctlfd_pos]); 24538b412664SPeter Zijlstra } 24548b412664SPeter Zijlstra 2455acce0223SAlexey Budankov if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) { 2456acce0223SAlexey Budankov switch (cmd) { 2457d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 2458d20aff15SAdrian Hunter hit_auxtrace_snapshot_trigger(rec); 2459d20aff15SAdrian Hunter evlist__ctlfd_ack(rec->evlist); 2460d20aff15SAdrian Hunter break; 2461f186cd61SJiri Olsa case EVLIST_CTL_CMD_STOP: 2462f186cd61SJiri Olsa done = 1; 2463f186cd61SJiri Olsa break; 2464acce0223SAlexey Budankov case EVLIST_CTL_CMD_ACK: 2465acce0223SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 2466991ae4ebSJiri Olsa case EVLIST_CTL_CMD_ENABLE: 2467991ae4ebSJiri Olsa case EVLIST_CTL_CMD_DISABLE: 2468142544a9SJiri Olsa case EVLIST_CTL_CMD_EVLIST: 246947fddcb4SJiri Olsa case EVLIST_CTL_CMD_PING: 2470acce0223SAlexey Budankov default: 2471acce0223SAlexey Budankov break; 2472acce0223SAlexey Budankov } 2473acce0223SAlexey Budankov } 2474acce0223SAlexey Budankov 2475774cb499SJiri Olsa /* 2476774cb499SJiri Olsa * When perf is starting the traced process, at the end events 2477774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 2478774cb499SJiri Olsa * disable events in this case. 2479774cb499SJiri Olsa */ 2480602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 24815f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 2482e74676deSJiri Olsa evlist__disable(rec->evlist); 24832711926aSJiri Olsa disabled = true; 24842711926aSJiri Olsa } 24858b412664SPeter Zijlstra } 2486ce7b0e42SAlexander Shishkin 24875f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 24883c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 24898b412664SPeter Zijlstra 2490ce7b0e42SAlexander Shishkin if (opts->auxtrace_snapshot_on_exit) 2491ce7b0e42SAlexander Shishkin record__auxtrace_snapshot_exit(rec); 2492ce7b0e42SAlexander Shishkin 2493f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 24943535a696SArnaldo Carvalho de Melo char msg[STRERR_BUFSIZE], strevsels[2048]; 2495c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 24963535a696SArnaldo Carvalho de Melo 24973535a696SArnaldo Carvalho de Melo evlist__scnprintf_evsels(rec->evlist, sizeof(strevsels), strevsels); 24983535a696SArnaldo Carvalho de Melo 24993535a696SArnaldo Carvalho de Melo pr_err("Failed to collect '%s' for the '%s' workload: %s\n", 25003535a696SArnaldo Carvalho de Melo strevsels, argv[0], emsg); 2501f33cbe72SArnaldo Carvalho de Melo err = -1; 250245604710SNamhyung Kim goto out_child; 2503f33cbe72SArnaldo Carvalho de Melo } 2504f33cbe72SArnaldo Carvalho de Melo 2505e3d59112SNamhyung Kim if (!quiet) 2506396b626bSAlexey Bayduraev fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", 2507396b626bSAlexey Bayduraev record__waking(rec)); 250886470930SIngo Molnar 25094ea648aeSWang Nan if (target__none(&rec->opts.target)) 25104ea648aeSWang Nan record__synthesize_workload(rec, true); 25114ea648aeSWang Nan 251245604710SNamhyung Kim out_child: 2513396b626bSAlexey Bayduraev record__stop_threads(rec); 2514470530bbSAlexey Budankov record__mmap_read_all(rec, true); 2515396b626bSAlexey Bayduraev out_free_threads: 2516415ccb58SAlexey Bayduraev record__free_thread_data(rec); 2517396b626bSAlexey Bayduraev evlist__finalize_ctlfd(rec->evlist); 2518d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 2519d3d1af6fSAlexey Budankov 2520d3c8c08eSAlexey Budankov if (rec->session->bytes_transferred && rec->session->bytes_compressed) { 2521d3c8c08eSAlexey Budankov ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed; 2522d3c8c08eSAlexey Budankov session->header.env.comp_ratio = ratio + 0.5; 2523d3c8c08eSAlexey Budankov } 2524d3c8c08eSAlexey Budankov 252545604710SNamhyung Kim if (forks) { 252645604710SNamhyung Kim int exit_status; 252745604710SNamhyung Kim 252845604710SNamhyung Kim if (!child_finished) 252945604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 253045604710SNamhyung Kim 253145604710SNamhyung Kim wait(&exit_status); 253245604710SNamhyung Kim 253345604710SNamhyung Kim if (err < 0) 253445604710SNamhyung Kim status = err; 253545604710SNamhyung Kim else if (WIFEXITED(exit_status)) 253645604710SNamhyung Kim status = WEXITSTATUS(exit_status); 253745604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 253845604710SNamhyung Kim signr = WTERMSIG(exit_status); 253945604710SNamhyung Kim } else 254045604710SNamhyung Kim status = err; 254145604710SNamhyung Kim 25424ea648aeSWang Nan record__synthesize(rec, true); 2543e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 2544e3d59112SNamhyung Kim rec->samples = 0; 2545e3d59112SNamhyung Kim 2546ecfd7a9cSWang Nan if (!err) { 2547ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 2548e1ab48baSWang Nan record__finish_output(rec); 2549ecfd7a9cSWang Nan } else { 2550ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 2551ecfd7a9cSWang Nan if (fd < 0) { 2552ecfd7a9cSWang Nan status = fd; 2553ecfd7a9cSWang Nan goto out_delete_session; 2554ecfd7a9cSWang Nan } 2555ecfd7a9cSWang Nan } 2556ecfd7a9cSWang Nan } 255739d17dacSArnaldo Carvalho de Melo 2558a074865eSWang Nan perf_hooks__invoke_record_end(); 2559a074865eSWang Nan 2560e3d59112SNamhyung Kim if (!err && !quiet) { 2561e3d59112SNamhyung Kim char samples[128]; 2562ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 2563ecfd7a9cSWang Nan ".<timestamp>" : ""; 2564e3d59112SNamhyung Kim 2565ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 2566e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 2567e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 2568e3d59112SNamhyung Kim else 2569e3d59112SNamhyung Kim samples[0] = '\0'; 2570e3d59112SNamhyung Kim 2571d3c8c08eSAlexey Budankov fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s", 25728ceb41d7SJiri Olsa perf_data__size(data) / 1024.0 / 1024.0, 25732d4f2799SJiri Olsa data->path, postfix, samples); 2574d3c8c08eSAlexey Budankov if (ratio) { 2575d3c8c08eSAlexey Budankov fprintf(stderr, ", compressed (original %.3f MB, ratio is %.3f)", 2576d3c8c08eSAlexey Budankov rec->session->bytes_transferred / 1024.0 / 1024.0, 2577d3c8c08eSAlexey Budankov ratio); 2578d3c8c08eSAlexey Budankov } 2579d3c8c08eSAlexey Budankov fprintf(stderr, " ]\n"); 2580e3d59112SNamhyung Kim } 2581e3d59112SNamhyung Kim 258239d17dacSArnaldo Carvalho de Melo out_delete_session: 2583da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 2584da231338SAnand K Mistry if (done_fd >= 0) 2585da231338SAnand K Mistry close(done_fd); 2586da231338SAnand K Mistry #endif 25875d7f4116SAlexey Budankov zstd_fini(&session->zstd_data); 258839d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 2589657ee553SSong Liu 2590657ee553SSong Liu if (!opts->no_bpf_event) 259108c83997SArnaldo Carvalho de Melo evlist__stop_sb_thread(rec->sb_evlist); 259245604710SNamhyung Kim return status; 259386470930SIngo Molnar } 259486470930SIngo Molnar 25950883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 259609b0fd45SJiri Olsa { 2597aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 2598a601fdffSJiri Olsa 25990883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 260026d33022SJiri Olsa 26010883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 260209b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 26030883e820SArnaldo Carvalho de Melo callchain->dump_size); 26040883e820SArnaldo Carvalho de Melo } 26050883e820SArnaldo Carvalho de Melo 26060883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 26070883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 26080883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 26090883e820SArnaldo Carvalho de Melo { 26100883e820SArnaldo Carvalho de Melo int ret; 26110883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 26120883e820SArnaldo Carvalho de Melo 26130883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 26140883e820SArnaldo Carvalho de Melo if (unset) { 26150883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 26160883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 26170883e820SArnaldo Carvalho de Melo return 0; 26180883e820SArnaldo Carvalho de Melo } 26190883e820SArnaldo Carvalho de Melo 26200883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 26210883e820SArnaldo Carvalho de Melo if (!ret) { 26220883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 26230883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 26240883e820SArnaldo Carvalho de Melo record->sample_address = true; 26250883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 26260883e820SArnaldo Carvalho de Melo } 26270883e820SArnaldo Carvalho de Melo 26280883e820SArnaldo Carvalho de Melo return ret; 262909b0fd45SJiri Olsa } 263009b0fd45SJiri Olsa 2631c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 263209b0fd45SJiri Olsa const char *arg, 263309b0fd45SJiri Olsa int unset) 263409b0fd45SJiri Olsa { 26350883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 263626d33022SJiri Olsa } 263726d33022SJiri Olsa 2638c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 263909b0fd45SJiri Olsa const char *arg __maybe_unused, 264009b0fd45SJiri Olsa int unset __maybe_unused) 264109b0fd45SJiri Olsa { 26422ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 2643c421e80bSKan Liang 26442ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 264509b0fd45SJiri Olsa 26462ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 26472ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 2648eb853e80SJiri Olsa 26492ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 265009b0fd45SJiri Olsa return 0; 265109b0fd45SJiri Olsa } 265209b0fd45SJiri Olsa 2653eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 2654eb853e80SJiri Olsa { 26557a29c087SNamhyung Kim struct record *rec = cb; 26567a29c087SNamhyung Kim 26577a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 26587a29c087SNamhyung Kim if (!strcmp(value, "cache")) 26597a29c087SNamhyung Kim rec->no_buildid_cache = false; 26607a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 26617a29c087SNamhyung Kim rec->no_buildid_cache = true; 26627a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 26637a29c087SNamhyung Kim rec->no_buildid = true; 2664e29386c8SJiri Olsa else if (!strcmp(value, "mmap")) 2665e29386c8SJiri Olsa rec->buildid_mmap = true; 26667a29c087SNamhyung Kim else 26677a29c087SNamhyung Kim return -1; 26687a29c087SNamhyung Kim return 0; 26697a29c087SNamhyung Kim } 2670cff17205SYisheng Xie if (!strcmp(var, "record.call-graph")) { 2671cff17205SYisheng Xie var = "call-graph.record-mode"; 2672eb853e80SJiri Olsa return perf_default_config(var, value, cb); 2673eb853e80SJiri Olsa } 267493f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 267593f20c0fSAlexey Budankov if (!strcmp(var, "record.aio")) { 267693f20c0fSAlexey Budankov rec->opts.nr_cblocks = strtol(value, NULL, 0); 267793f20c0fSAlexey Budankov if (!rec->opts.nr_cblocks) 267893f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_default; 267993f20c0fSAlexey Budankov } 268093f20c0fSAlexey Budankov #endif 26819bce13eaSJiri Olsa if (!strcmp(var, "record.debuginfod")) { 26829bce13eaSJiri Olsa rec->debuginfod.urls = strdup(value); 26839bce13eaSJiri Olsa if (!rec->debuginfod.urls) 26849bce13eaSJiri Olsa return -ENOMEM; 26859bce13eaSJiri Olsa rec->debuginfod.set = true; 26869bce13eaSJiri Olsa } 2687eb853e80SJiri Olsa 2688cff17205SYisheng Xie return 0; 2689cff17205SYisheng Xie } 2690cff17205SYisheng Xie 2691814c8c38SPeter Zijlstra 2692f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset) 2693f4fe11b7SAlexey Budankov { 2694f4fe11b7SAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 2695f4fe11b7SAlexey Budankov 2696f4fe11b7SAlexey Budankov if (unset || !str) 2697f4fe11b7SAlexey Budankov return 0; 2698f4fe11b7SAlexey Budankov 2699f4fe11b7SAlexey Budankov if (!strcasecmp(str, "node")) 2700f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_NODE; 2701f4fe11b7SAlexey Budankov else if (!strcasecmp(str, "cpu")) 2702f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_CPU; 2703f4fe11b7SAlexey Budankov 2704f4fe11b7SAlexey Budankov return 0; 2705f4fe11b7SAlexey Budankov } 2706f4fe11b7SAlexey Budankov 27077954f716SAlexey Bayduraev static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits) 27087954f716SAlexey Bayduraev { 27097954f716SAlexey Bayduraev mask->nbits = nr_bits; 27107954f716SAlexey Bayduraev mask->bits = bitmap_zalloc(mask->nbits); 27117954f716SAlexey Bayduraev if (!mask->bits) 27127954f716SAlexey Bayduraev return -ENOMEM; 27137954f716SAlexey Bayduraev 27147954f716SAlexey Bayduraev return 0; 27157954f716SAlexey Bayduraev } 27167954f716SAlexey Bayduraev 27177954f716SAlexey Bayduraev static void record__mmap_cpu_mask_free(struct mmap_cpu_mask *mask) 27187954f716SAlexey Bayduraev { 27197954f716SAlexey Bayduraev bitmap_free(mask->bits); 27207954f716SAlexey Bayduraev mask->nbits = 0; 27217954f716SAlexey Bayduraev } 27227954f716SAlexey Bayduraev 27237954f716SAlexey Bayduraev static int record__thread_mask_alloc(struct thread_mask *mask, int nr_bits) 27247954f716SAlexey Bayduraev { 27257954f716SAlexey Bayduraev int ret; 27267954f716SAlexey Bayduraev 27277954f716SAlexey Bayduraev ret = record__mmap_cpu_mask_alloc(&mask->maps, nr_bits); 27287954f716SAlexey Bayduraev if (ret) { 27297954f716SAlexey Bayduraev mask->affinity.bits = NULL; 27307954f716SAlexey Bayduraev return ret; 27317954f716SAlexey Bayduraev } 27327954f716SAlexey Bayduraev 27337954f716SAlexey Bayduraev ret = record__mmap_cpu_mask_alloc(&mask->affinity, nr_bits); 27347954f716SAlexey Bayduraev if (ret) { 27357954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->maps); 27367954f716SAlexey Bayduraev mask->maps.bits = NULL; 27377954f716SAlexey Bayduraev } 27387954f716SAlexey Bayduraev 27397954f716SAlexey Bayduraev return ret; 27407954f716SAlexey Bayduraev } 27417954f716SAlexey Bayduraev 27427954f716SAlexey Bayduraev static void record__thread_mask_free(struct thread_mask *mask) 27437954f716SAlexey Bayduraev { 27447954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->maps); 27457954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->affinity); 27467954f716SAlexey Bayduraev } 27477954f716SAlexey Bayduraev 27486d575816SJiwei Sun static int parse_output_max_size(const struct option *opt, 27496d575816SJiwei Sun const char *str, int unset) 27506d575816SJiwei Sun { 27516d575816SJiwei Sun unsigned long *s = (unsigned long *)opt->value; 27526d575816SJiwei Sun static struct parse_tag tags_size[] = { 27536d575816SJiwei Sun { .tag = 'B', .mult = 1 }, 27546d575816SJiwei Sun { .tag = 'K', .mult = 1 << 10 }, 27556d575816SJiwei Sun { .tag = 'M', .mult = 1 << 20 }, 27566d575816SJiwei Sun { .tag = 'G', .mult = 1 << 30 }, 27576d575816SJiwei Sun { .tag = 0 }, 27586d575816SJiwei Sun }; 27596d575816SJiwei Sun unsigned long val; 27606d575816SJiwei Sun 27616d575816SJiwei Sun if (unset) { 27626d575816SJiwei Sun *s = 0; 27636d575816SJiwei Sun return 0; 27646d575816SJiwei Sun } 27656d575816SJiwei Sun 27666d575816SJiwei Sun val = parse_tag_value(str, tags_size); 27676d575816SJiwei Sun if (val != (unsigned long) -1) { 27686d575816SJiwei Sun *s = val; 27696d575816SJiwei Sun return 0; 27706d575816SJiwei Sun } 27716d575816SJiwei Sun 27726d575816SJiwei Sun return -1; 27736d575816SJiwei Sun } 27746d575816SJiwei Sun 2775e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 2776e9db1310SAdrian Hunter const char *str, 2777e9db1310SAdrian Hunter int unset __maybe_unused) 2778e9db1310SAdrian Hunter { 2779e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 2780e9db1310SAdrian Hunter char *s, *p; 2781e9db1310SAdrian Hunter unsigned int mmap_pages; 2782e9db1310SAdrian Hunter int ret; 2783e9db1310SAdrian Hunter 2784e9db1310SAdrian Hunter if (!str) 2785e9db1310SAdrian Hunter return -EINVAL; 2786e9db1310SAdrian Hunter 2787e9db1310SAdrian Hunter s = strdup(str); 2788e9db1310SAdrian Hunter if (!s) 2789e9db1310SAdrian Hunter return -ENOMEM; 2790e9db1310SAdrian Hunter 2791e9db1310SAdrian Hunter p = strchr(s, ','); 2792e9db1310SAdrian Hunter if (p) 2793e9db1310SAdrian Hunter *p = '\0'; 2794e9db1310SAdrian Hunter 2795e9db1310SAdrian Hunter if (*s) { 279625f84702SArnaldo Carvalho de Melo ret = __evlist__parse_mmap_pages(&mmap_pages, s); 2797e9db1310SAdrian Hunter if (ret) 2798e9db1310SAdrian Hunter goto out_free; 2799e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 2800e9db1310SAdrian Hunter } 2801e9db1310SAdrian Hunter 2802e9db1310SAdrian Hunter if (!p) { 2803e9db1310SAdrian Hunter ret = 0; 2804e9db1310SAdrian Hunter goto out_free; 2805e9db1310SAdrian Hunter } 2806e9db1310SAdrian Hunter 280725f84702SArnaldo Carvalho de Melo ret = __evlist__parse_mmap_pages(&mmap_pages, p + 1); 2808e9db1310SAdrian Hunter if (ret) 2809e9db1310SAdrian Hunter goto out_free; 2810e9db1310SAdrian Hunter 2811e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 2812e9db1310SAdrian Hunter 2813e9db1310SAdrian Hunter out_free: 2814e9db1310SAdrian Hunter free(s); 2815e9db1310SAdrian Hunter return ret; 2816e9db1310SAdrian Hunter } 2817e9db1310SAdrian Hunter 28187248e308SAlexandre Truong void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __maybe_unused) 28197248e308SAlexandre Truong { 28207248e308SAlexandre Truong } 28217248e308SAlexandre Truong 28221d078ccbSAlexey Budankov static int parse_control_option(const struct option *opt, 28231d078ccbSAlexey Budankov const char *str, 28241d078ccbSAlexey Budankov int unset __maybe_unused) 28251d078ccbSAlexey Budankov { 28269864a66dSAdrian Hunter struct record_opts *opts = opt->value; 28271d078ccbSAlexey Budankov 2828a8fcbd26SAdrian Hunter return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close); 2829a8fcbd26SAdrian Hunter } 2830a8fcbd26SAdrian Hunter 28310c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 28320c582449SJiri Olsa { 28339521b5f2SJiri Olsa u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages); 28340c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 28350c582449SJiri Olsa 28360c582449SJiri Olsa wakeup_size /= 2; 28370c582449SJiri Olsa 28380c582449SJiri Olsa if (s->size < wakeup_size) { 28390c582449SJiri Olsa char buf[100]; 28400c582449SJiri Olsa 28410c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 28420c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 28430c582449SJiri Olsa "wakeup kernel buffer size (%s) " 28440c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 28450c582449SJiri Olsa } 28460c582449SJiri Olsa } 28470c582449SJiri Olsa 2848cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 2849cb4e1ebbSJiri Olsa { 2850cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 2851dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 2852dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 2853dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 2854dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 2855dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 2856dc0c6127SJiri Olsa { .tag = 0 }, 2857dc0c6127SJiri Olsa }; 2858bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 2859bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 2860bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 2861bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 2862bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 2863bfacbe3bSJiri Olsa { .tag = 0 }, 2864bfacbe3bSJiri Olsa }; 2865dc0c6127SJiri Olsa unsigned long val; 2866cb4e1ebbSJiri Olsa 2867899e5ffbSArnaldo Carvalho de Melo /* 2868899e5ffbSArnaldo Carvalho de Melo * If we're using --switch-output-events, then we imply its 2869899e5ffbSArnaldo Carvalho de Melo * --switch-output=signal, as we'll send a SIGUSR2 from the side band 2870899e5ffbSArnaldo Carvalho de Melo * thread to its parent. 2871899e5ffbSArnaldo Carvalho de Melo */ 2872899e5ffbSArnaldo Carvalho de Melo if (rec->switch_output_event_set) 2873899e5ffbSArnaldo Carvalho de Melo goto do_signal; 2874899e5ffbSArnaldo Carvalho de Melo 2875cb4e1ebbSJiri Olsa if (!s->set) 2876cb4e1ebbSJiri Olsa return 0; 2877cb4e1ebbSJiri Olsa 2878cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 2879899e5ffbSArnaldo Carvalho de Melo do_signal: 2880cb4e1ebbSJiri Olsa s->signal = true; 2881cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 2882dc0c6127SJiri Olsa goto enabled; 2883dc0c6127SJiri Olsa } 2884dc0c6127SJiri Olsa 2885dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 2886dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 2887dc0c6127SJiri Olsa s->size = val; 2888dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 2889dc0c6127SJiri Olsa goto enabled; 2890cb4e1ebbSJiri Olsa } 2891cb4e1ebbSJiri Olsa 2892bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 2893bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 2894bfacbe3bSJiri Olsa s->time = val; 2895bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 2896bfacbe3bSJiri Olsa s->str, s->time); 2897bfacbe3bSJiri Olsa goto enabled; 2898bfacbe3bSJiri Olsa } 2899bfacbe3bSJiri Olsa 2900cb4e1ebbSJiri Olsa return -1; 2901dc0c6127SJiri Olsa 2902dc0c6127SJiri Olsa enabled: 2903dc0c6127SJiri Olsa rec->timestamp_filename = true; 2904dc0c6127SJiri Olsa s->enabled = true; 29050c582449SJiri Olsa 29060c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 29070c582449SJiri Olsa switch_output_size_warn(rec); 29080c582449SJiri Olsa 2909dc0c6127SJiri Olsa return 0; 2910cb4e1ebbSJiri Olsa } 2911cb4e1ebbSJiri Olsa 2912e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 291386470930SIngo Molnar "perf record [<options>] [<command>]", 291486470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 291586470930SIngo Molnar NULL 291686470930SIngo Molnar }; 2917e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 291886470930SIngo Molnar 29196e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event, 29206e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 29216e0a9b3dSArnaldo Carvalho de Melo { 29226e0a9b3dSArnaldo Carvalho de Melo /* 29236e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 29246e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 29256e0a9b3dSArnaldo Carvalho de Melo */ 29266e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 29276e0a9b3dSArnaldo Carvalho de Melo return 0; 29286e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap(tool, event, sample, machine); 29296e0a9b3dSArnaldo Carvalho de Melo } 29306e0a9b3dSArnaldo Carvalho de Melo 29316e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event, 29326e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 29336e0a9b3dSArnaldo Carvalho de Melo { 29346e0a9b3dSArnaldo Carvalho de Melo /* 29356e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 29366e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 29376e0a9b3dSArnaldo Carvalho de Melo */ 29386e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 29396e0a9b3dSArnaldo Carvalho de Melo return 0; 29406e0a9b3dSArnaldo Carvalho de Melo 29416e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap2(tool, event, sample, machine); 29426e0a9b3dSArnaldo Carvalho de Melo } 29436e0a9b3dSArnaldo Carvalho de Melo 294466286ed3SAdrian Hunter static int process_timestamp_boundary(struct perf_tool *tool, 294566286ed3SAdrian Hunter union perf_event *event __maybe_unused, 294666286ed3SAdrian Hunter struct perf_sample *sample, 294766286ed3SAdrian Hunter struct machine *machine __maybe_unused) 294866286ed3SAdrian Hunter { 294966286ed3SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 295066286ed3SAdrian Hunter 295166286ed3SAdrian Hunter set_timestamp_boundary(rec, sample->time); 295266286ed3SAdrian Hunter return 0; 295366286ed3SAdrian Hunter } 295466286ed3SAdrian Hunter 295541b740b6SNamhyung Kim static int parse_record_synth_option(const struct option *opt, 295641b740b6SNamhyung Kim const char *str, 295741b740b6SNamhyung Kim int unset __maybe_unused) 295841b740b6SNamhyung Kim { 295941b740b6SNamhyung Kim struct record_opts *opts = opt->value; 296041b740b6SNamhyung Kim char *p = strdup(str); 296141b740b6SNamhyung Kim 296241b740b6SNamhyung Kim if (p == NULL) 296341b740b6SNamhyung Kim return -1; 296441b740b6SNamhyung Kim 296541b740b6SNamhyung Kim opts->synth = parse_synth_opt(p); 296641b740b6SNamhyung Kim free(p); 296741b740b6SNamhyung Kim 296841b740b6SNamhyung Kim if (opts->synth < 0) { 296941b740b6SNamhyung Kim pr_err("Invalid synth option: %s\n", str); 297041b740b6SNamhyung Kim return -1; 297141b740b6SNamhyung Kim } 297241b740b6SNamhyung Kim return 0; 297341b740b6SNamhyung Kim } 297441b740b6SNamhyung Kim 2975d20deb64SArnaldo Carvalho de Melo /* 29768c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 29778c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 2978d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 2979d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 2980d20deb64SArnaldo Carvalho de Melo * 2981d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 2982d20deb64SArnaldo Carvalho de Melo * 2983d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 2984d20deb64SArnaldo Carvalho de Melo */ 29858c6f45a7SArnaldo Carvalho de Melo static struct record record = { 2986d20deb64SArnaldo Carvalho de Melo .opts = { 29878affc2b8SAndi Kleen .sample_time = true, 2988d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 2989d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 2990d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 2991447a6013SArnaldo Carvalho de Melo .freq = 4000, 2992d1cb9fceSNamhyung Kim .target = { 2993d1cb9fceSNamhyung Kim .uses_mmap = true, 29943aa5939dSAdrian Hunter .default_per_cpu = true, 2995d1cb9fceSNamhyung Kim }, 2996470530bbSAlexey Budankov .mmap_flush = MMAP_FLUSH_DEFAULT, 2997d99c22eaSStephane Eranian .nr_threads_synthesize = 1, 29981d078ccbSAlexey Budankov .ctl_fd = -1, 29991d078ccbSAlexey Budankov .ctl_fd_ack = -1, 300041b740b6SNamhyung Kim .synth = PERF_SYNTH_ALL, 3001d20deb64SArnaldo Carvalho de Melo }, 3002e3d59112SNamhyung Kim .tool = { 3003e3d59112SNamhyung Kim .sample = process_sample_event, 3004e3d59112SNamhyung Kim .fork = perf_event__process_fork, 3005cca8482cSAdrian Hunter .exit = perf_event__process_exit, 3006e3d59112SNamhyung Kim .comm = perf_event__process_comm, 3007f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 30086e0a9b3dSArnaldo Carvalho de Melo .mmap = build_id__process_mmap, 30096e0a9b3dSArnaldo Carvalho de Melo .mmap2 = build_id__process_mmap2, 301066286ed3SAdrian Hunter .itrace_start = process_timestamp_boundary, 301166286ed3SAdrian Hunter .aux = process_timestamp_boundary, 3012cca8482cSAdrian Hunter .ordered_events = true, 3013e3d59112SNamhyung Kim }, 3014d20deb64SArnaldo Carvalho de Melo }; 30157865e817SFrederic Weisbecker 301676a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 301776a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 301861eaa3beSArnaldo Carvalho de Melo 30190aab2136SWang Nan static bool dry_run; 30200aab2136SWang Nan 3021d20deb64SArnaldo Carvalho de Melo /* 3022d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 3023d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 3024b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 30257b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 3026d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 3027d20deb64SArnaldo Carvalho de Melo */ 3028efd21307SJiri Olsa static struct option __record_options[] = { 3029d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 303086470930SIngo Molnar "event selector. use 'perf list' to list available events", 3031f120f9d5SJiri Olsa parse_events_option), 3032d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 3033c171b552SLi Zefan "event filter", parse_filter), 30344ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 30354ba1faa1SWang Nan NULL, "don't record events from perf itself", 30364ba1faa1SWang Nan exclude_perf), 3037bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 3038d6d901c2SZhang, Yanmin "record events on existing process id"), 3039bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 3040d6d901c2SZhang, Yanmin "record events on existing thread id"), 3041d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 304286470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 3043509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 3044acac03faSKirill Smelkov "collect data without buffering"), 3045d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 3046daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 3047bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 304886470930SIngo Molnar "system-wide collection from all CPUs"), 3049bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 3050c45c6ea2SStephane Eranian "list of cpus to monitor"), 3051d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 30522d4f2799SJiri Olsa OPT_STRING('o', "output", &record.data.path, "file", 305386470930SIngo Molnar "output file name"), 305469e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 305569e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 30562e6cdf99SStephane Eranian "child tasks do not inherit counters"), 30574ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 30584ea648aeSWang Nan "synthesize non-sample events at the end of output"), 3059626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 3060a060c1f1SWei Li OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"), 3061b09c2364SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq, 3062b09c2364SArnaldo Carvalho de Melo "Fail if the specified frequency can't be used"), 306367230479SArnaldo Carvalho de Melo OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'", 306467230479SArnaldo Carvalho de Melo "profile at this frequency", 306567230479SArnaldo Carvalho de Melo record__parse_freq), 3066e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 3067e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 3068e9db1310SAdrian Hunter record__parse_mmap_pages), 3069470530bbSAlexey Budankov OPT_CALLBACK(0, "mmap-flush", &record.opts, "number", 3070470530bbSAlexey Budankov "Minimal number of bytes that is extracted from mmap data pages (default: 1)", 3071470530bbSAlexey Budankov record__mmap_flush_parse), 3072d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "group", &record.opts.group, 307343bece79SLin Ming "put the counters into a counter group"), 30742ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 307509b0fd45SJiri Olsa NULL, "enables call-graph recording" , 307609b0fd45SJiri Olsa &record_callchain_opt), 307709b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 307876a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 307909b0fd45SJiri Olsa &record_parse_callchain_opt), 3080c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 30813da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 3082b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 3083d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 3084649c48a9SPeter Zijlstra "per thread counts"), 308556100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 30863b0a5daaSKan Liang OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, 30873b0a5daaSKan Liang "Record the sample physical addresses"), 3088542b88fdSKan Liang OPT_BOOLEAN(0, "data-page-size", &record.opts.sample_data_page_size, 3089542b88fdSKan Liang "Record the sampled data address data page size"), 3090c1de7f3dSKan Liang OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size, 3091c1de7f3dSKan Liang "Record the sampled code address (ip) page size"), 3092b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 30933abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 30943abebc55SAdrian Hunter &record.opts.sample_time_set, 30953abebc55SAdrian Hunter "Record the sample timestamps"), 3096f290aa1fSJiri Olsa OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set, 3097f290aa1fSJiri Olsa "Record the sample period"), 3098d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 3099649c48a9SPeter Zijlstra "don't sample"), 3100d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 3101d2db9a98SWang Nan &record.no_buildid_cache_set, 3102a1ac1d3cSStephane Eranian "do not update the buildid cache"), 3103d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 3104d2db9a98SWang Nan &record.no_buildid_set, 3105baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 3106d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 3107023695d9SStephane Eranian "monitor event in cgroup name only", 3108023695d9SStephane Eranian parse_cgroups), 310968cd3b45SAlexey Budankov OPT_INTEGER('D', "delay", &record.opts.initial_delay, 311068cd3b45SAlexey Budankov "ms to wait before starting measurement after program start (-1: start with events disabled)"), 3111eeb399b5SAdrian Hunter OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"), 3112bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 3113bea03405SNamhyung Kim "user to profile"), 3114a5aabdacSStephane Eranian 3115a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 3116a5aabdacSStephane Eranian "branch any", "sample any taken branches", 3117a5aabdacSStephane Eranian parse_branch_stack), 3118a5aabdacSStephane Eranian 3119a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 3120a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 3121bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 312205484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 312305484298SAndi Kleen "sample by weight (on special events only)"), 3124475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 3125475eeab9SAndi Kleen "sample transaction flags (special events only)"), 31263aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 31273aa5939dSAdrian Hunter "use per-thread mmaps"), 3128bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 3129bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 3130aeea9062SKan Liang " use '-I?' to list register names", parse_intr_regs), 313184c41742SAndi Kleen OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register", 313284c41742SAndi Kleen "sample selected machine registers on interrupt," 3133aeea9062SKan Liang " use '--user-regs=?' to list register names", parse_user_regs), 313485c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 313585c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 3136814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 3137814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 3138814c8c38SPeter Zijlstra parse_clockid), 31392dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 31402dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 3141c0a6de06SAdrian Hunter OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts, 3142c0a6de06SAdrian Hunter "opts", "sample AUX area", ""), 31433fcb10e4SMark Drayton OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 31449d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 3145f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 3146f3b3614aSHari Bathini "Record namespaces events"), 31478fb4b679SNamhyung Kim OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup, 31488fb4b679SNamhyung Kim "Record cgroup events"), 314916b4b4e1SAdrian Hunter OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events, 315016b4b4e1SAdrian Hunter &record.opts.record_switch_events_set, 3151b757bb09SAdrian Hunter "Record context switch events"), 315285723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 315385723885SJiri Olsa "Configure all used events to run in kernel space.", 315485723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 315585723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 315685723885SJiri Olsa "Configure all used events to run in user space.", 315785723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 315853651b28Syuzhoujian OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains, 315953651b28Syuzhoujian "collect kernel callchains"), 316053651b28Syuzhoujian OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains, 316153651b28Syuzhoujian "collect user callchains"), 316271dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 316371dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 316471dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 316571dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 31667efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 31677efe0e03SHe Kuang "file", "vmlinux pathname"), 31686156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 31696156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 3170e29386c8SJiri Olsa OPT_BOOLEAN(0, "buildid-mmap", &record.buildid_mmap, 3171e29386c8SJiri Olsa "Record build-id in map events"), 3172ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 3173ecfd7a9cSWang Nan "append timestamp to output filename"), 317468588bafSJin Yao OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary, 317568588bafSJin Yao "Record timestamp boundary (time of first/last samples)"), 3176cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 3177c38dab7dSAndi Kleen &record.switch_output.set, "signal or size[BKMG] or time[smhd]", 3178c38dab7dSAndi Kleen "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold", 3179dc0c6127SJiri Olsa "signal"), 3180899e5ffbSArnaldo Carvalho de Melo OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event", 3181899e5ffbSArnaldo Carvalho de Melo "switch output event selector. use 'perf list' to list available events", 3182899e5ffbSArnaldo Carvalho de Melo parse_events_option_new_evlist), 318303724b2eSAndi Kleen OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files, 318403724b2eSAndi Kleen "Limit number of switch output generated files"), 31850aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 31860aab2136SWang Nan "Parse options then exit"), 3187d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 318893f20c0fSAlexey Budankov OPT_CALLBACK_OPTARG(0, "aio", &record.opts, 318993f20c0fSAlexey Budankov &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)", 3190d3d1af6fSAlexey Budankov record__aio_parse), 3191d3d1af6fSAlexey Budankov #endif 3192f4fe11b7SAlexey Budankov OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu", 3193f4fe11b7SAlexey Budankov "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer", 3194f4fe11b7SAlexey Budankov record__parse_affinity), 3195504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 3196504c1ad1SAlexey Budankov OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, 3197504c1ad1SAlexey Budankov "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)", 3198504c1ad1SAlexey Budankov record__parse_comp_level), 3199504c1ad1SAlexey Budankov #endif 32006d575816SJiwei Sun OPT_CALLBACK(0, "max-size", &record.output_max_size, 32016d575816SJiwei Sun "size", "Limit the maximum size of the output file", parse_output_max_size), 3202d99c22eaSStephane Eranian OPT_UINTEGER(0, "num-thread-synthesize", 3203d99c22eaSStephane Eranian &record.opts.nr_threads_synthesize, 3204d99c22eaSStephane Eranian "number of threads to run for event synthesis"), 320570943490SStephane Eranian #ifdef HAVE_LIBPFM 320670943490SStephane Eranian OPT_CALLBACK(0, "pfm-events", &record.evlist, "event", 320770943490SStephane Eranian "libpfm4 event selector. use 'perf list' to list available events", 320870943490SStephane Eranian parse_libpfm_events_option), 320970943490SStephane Eranian #endif 3210a8fcbd26SAdrian Hunter OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]", 3211d20aff15SAdrian Hunter "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n" 3212d20aff15SAdrian Hunter "\t\t\t 'snapshot': AUX area tracing snapshot).\n" 3213a8fcbd26SAdrian Hunter "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n" 3214a8fcbd26SAdrian Hunter "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.", 32151d078ccbSAlexey Budankov parse_control_option), 321641b740b6SNamhyung Kim OPT_CALLBACK(0, "synth", &record.opts, "no|all|task|mmap|cgroup", 321741b740b6SNamhyung Kim "Fine-tune event synthesis: default=all", parse_record_synth_option), 32189bce13eaSJiri Olsa OPT_STRING_OPTARG_SET(0, "debuginfod", &record.debuginfod.urls, 32199bce13eaSJiri Olsa &record.debuginfod.set, "debuginfod urls", 32209bce13eaSJiri Olsa "Enable debuginfod data retrieval from DEBUGINFOD_URLS or specified urls", 32219bce13eaSJiri Olsa "system"), 322286470930SIngo Molnar OPT_END() 322386470930SIngo Molnar }; 322486470930SIngo Molnar 3225e5b2c207SNamhyung Kim struct option *record_options = __record_options; 3226e5b2c207SNamhyung Kim 32277954f716SAlexey Bayduraev static void record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpu_map *cpus) 32287954f716SAlexey Bayduraev { 32297954f716SAlexey Bayduraev int c; 32307954f716SAlexey Bayduraev 32317954f716SAlexey Bayduraev for (c = 0; c < cpus->nr; c++) 32327954f716SAlexey Bayduraev set_bit(cpus->map[c].cpu, mask->bits); 32337954f716SAlexey Bayduraev } 32347954f716SAlexey Bayduraev 32357954f716SAlexey Bayduraev static void record__free_thread_masks(struct record *rec, int nr_threads) 32367954f716SAlexey Bayduraev { 32377954f716SAlexey Bayduraev int t; 32387954f716SAlexey Bayduraev 32397954f716SAlexey Bayduraev if (rec->thread_masks) 32407954f716SAlexey Bayduraev for (t = 0; t < nr_threads; t++) 32417954f716SAlexey Bayduraev record__thread_mask_free(&rec->thread_masks[t]); 32427954f716SAlexey Bayduraev 32437954f716SAlexey Bayduraev zfree(&rec->thread_masks); 32447954f716SAlexey Bayduraev } 32457954f716SAlexey Bayduraev 32467954f716SAlexey Bayduraev static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr_bits) 32477954f716SAlexey Bayduraev { 32487954f716SAlexey Bayduraev int t, ret; 32497954f716SAlexey Bayduraev 32507954f716SAlexey Bayduraev rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks))); 32517954f716SAlexey Bayduraev if (!rec->thread_masks) { 32527954f716SAlexey Bayduraev pr_err("Failed to allocate thread masks\n"); 32537954f716SAlexey Bayduraev return -ENOMEM; 32547954f716SAlexey Bayduraev } 32557954f716SAlexey Bayduraev 32567954f716SAlexey Bayduraev for (t = 0; t < nr_threads; t++) { 32577954f716SAlexey Bayduraev ret = record__thread_mask_alloc(&rec->thread_masks[t], nr_bits); 32587954f716SAlexey Bayduraev if (ret) { 32597954f716SAlexey Bayduraev pr_err("Failed to allocate thread masks[%d]\n", t); 32607954f716SAlexey Bayduraev goto out_free; 32617954f716SAlexey Bayduraev } 32627954f716SAlexey Bayduraev } 32637954f716SAlexey Bayduraev 32647954f716SAlexey Bayduraev return 0; 32657954f716SAlexey Bayduraev 32667954f716SAlexey Bayduraev out_free: 32677954f716SAlexey Bayduraev record__free_thread_masks(rec, nr_threads); 32687954f716SAlexey Bayduraev 32697954f716SAlexey Bayduraev return ret; 32707954f716SAlexey Bayduraev } 32717954f716SAlexey Bayduraev 32727954f716SAlexey Bayduraev static int record__init_thread_default_masks(struct record *rec, struct perf_cpu_map *cpus) 32737954f716SAlexey Bayduraev { 32747954f716SAlexey Bayduraev int ret; 32757954f716SAlexey Bayduraev 32767954f716SAlexey Bayduraev ret = record__alloc_thread_masks(rec, 1, cpu__max_cpu().cpu); 32777954f716SAlexey Bayduraev if (ret) 32787954f716SAlexey Bayduraev return ret; 32797954f716SAlexey Bayduraev 32807954f716SAlexey Bayduraev record__mmap_cpu_mask_init(&rec->thread_masks->maps, cpus); 32817954f716SAlexey Bayduraev 32827954f716SAlexey Bayduraev rec->nr_threads = 1; 32837954f716SAlexey Bayduraev 32847954f716SAlexey Bayduraev return 0; 32857954f716SAlexey Bayduraev } 32867954f716SAlexey Bayduraev 32877954f716SAlexey Bayduraev static int record__init_thread_masks(struct record *rec) 32887954f716SAlexey Bayduraev { 32897954f716SAlexey Bayduraev struct perf_cpu_map *cpus = rec->evlist->core.cpus; 32907954f716SAlexey Bayduraev 32917954f716SAlexey Bayduraev return record__init_thread_default_masks(rec, cpus); 32927954f716SAlexey Bayduraev } 32937954f716SAlexey Bayduraev 3294b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 329586470930SIngo Molnar { 3296ef149c25SAdrian Hunter int err; 32978c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 329816ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 329986470930SIngo Molnar 330067230479SArnaldo Carvalho de Melo setlocale(LC_ALL, ""); 330167230479SArnaldo Carvalho de Melo 330248e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 330348e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 330448e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 330548e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 330648e1cab1SWang Nan # undef set_nobuild 330748e1cab1SWang Nan #endif 330848e1cab1SWang Nan 33097efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 33107efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 33117efe0e03SHe Kuang # define REASON "NO_DWARF=1" 33127efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 33137efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 33147efe0e03SHe Kuang # else 33157efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 33167efe0e03SHe Kuang # endif 33177efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 33187efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 33197efe0e03SHe Kuang # undef set_nobuild 33207efe0e03SHe Kuang # undef REASON 33217efe0e03SHe Kuang #endif 33227efe0e03SHe Kuang 33239d2ed645SAlexey Budankov rec->opts.affinity = PERF_AFFINITY_SYS; 33249d2ed645SAlexey Budankov 33250f98b11cSJiri Olsa rec->evlist = evlist__new(); 33263e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 3327361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 3328361c99a6SArnaldo Carvalho de Melo 3329ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 3330ecc4c561SArnaldo Carvalho de Melo if (err) 3331ecc4c561SArnaldo Carvalho de Melo return err; 3332eb853e80SJiri Olsa 3333bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 3334a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 333568ba3235SNamhyung Kim if (quiet) 333668ba3235SNamhyung Kim perf_quiet_option(); 3337483635a9SJiri Olsa 33387cc72553SJames Clark err = symbol__validate_sym_arguments(); 33397cc72553SJames Clark if (err) 33407cc72553SJames Clark return err; 33417cc72553SJames Clark 33429bce13eaSJiri Olsa perf_debuginfod_setup(&record.debuginfod); 33439bce13eaSJiri Olsa 3344483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 3345602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 3346483635a9SJiri Olsa rec->opts.target.system_wide = true; 334786470930SIngo Molnar 3348bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 3349c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 3350c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 3351c7118369SNamhyung Kim 3352023695d9SStephane Eranian } 3353504c1ad1SAlexey Budankov 3354e29386c8SJiri Olsa if (rec->buildid_mmap) { 3355e29386c8SJiri Olsa if (!perf_can_record_build_id()) { 3356e29386c8SJiri Olsa pr_err("Failed: no support to record build id in mmap events, update your kernel.\n"); 3357e29386c8SJiri Olsa err = -EINVAL; 3358e29386c8SJiri Olsa goto out_opts; 3359e29386c8SJiri Olsa } 3360e29386c8SJiri Olsa pr_debug("Enabling build id in mmap2 events.\n"); 3361e29386c8SJiri Olsa /* Enable mmap build id synthesizing. */ 3362e29386c8SJiri Olsa symbol_conf.buildid_mmap2 = true; 3363e29386c8SJiri Olsa /* Enable perf_event_attr::build_id bit. */ 3364e29386c8SJiri Olsa rec->opts.build_id = true; 3365e29386c8SJiri Olsa /* Disable build id cache. */ 3366e29386c8SJiri Olsa rec->no_buildid = true; 3367e29386c8SJiri Olsa } 3368e29386c8SJiri Olsa 33694f2abe91SNamhyung Kim if (rec->opts.record_cgroup && !perf_can_record_cgroup()) { 33704f2abe91SNamhyung Kim pr_err("Kernel has no cgroup sampling support.\n"); 33714f2abe91SNamhyung Kim err = -EINVAL; 33724f2abe91SNamhyung Kim goto out_opts; 33734f2abe91SNamhyung Kim } 33744f2abe91SNamhyung Kim 337556f735ffSAlexey Bayduraev if (rec->opts.kcore || record__threads_enabled(rec)) 3376eeb399b5SAdrian Hunter rec->data.is_dir = true; 3377eeb399b5SAdrian Hunter 3378504c1ad1SAlexey Budankov if (rec->opts.comp_level != 0) { 3379504c1ad1SAlexey Budankov pr_debug("Compression enabled, disabling build id collection at the end of the session.\n"); 3380504c1ad1SAlexey Budankov rec->no_buildid = true; 3381504c1ad1SAlexey Budankov } 3382504c1ad1SAlexey Budankov 3383b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 3384b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 3385c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 3386c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 3387a8fcbd26SAdrian Hunter err = -EINVAL; 3388a8fcbd26SAdrian Hunter goto out_opts; 3389b757bb09SAdrian Hunter } 3390023695d9SStephane Eranian 3391cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 3392cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 3393a8fcbd26SAdrian Hunter err = -EINVAL; 3394a8fcbd26SAdrian Hunter goto out_opts; 3395cb4e1ebbSJiri Olsa } 3396cb4e1ebbSJiri Olsa 3397bfacbe3bSJiri Olsa if (rec->switch_output.time) { 3398bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 3399bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 3400bfacbe3bSJiri Olsa } 3401bfacbe3bSJiri Olsa 340203724b2eSAndi Kleen if (rec->switch_output.num_files) { 340303724b2eSAndi Kleen rec->switch_output.filenames = calloc(sizeof(char *), 340403724b2eSAndi Kleen rec->switch_output.num_files); 3405a8fcbd26SAdrian Hunter if (!rec->switch_output.filenames) { 3406a8fcbd26SAdrian Hunter err = -EINVAL; 3407a8fcbd26SAdrian Hunter goto out_opts; 3408a8fcbd26SAdrian Hunter } 340903724b2eSAndi Kleen } 341003724b2eSAndi Kleen 34111b36c03eSAdrian Hunter /* 34121b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 34131b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 34141b36c03eSAdrian Hunter */ 34151b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 34161b36c03eSAdrian Hunter 34171b36c03eSAdrian Hunter symbol__init(NULL); 34181b36c03eSAdrian Hunter 34194b5ea3bdSAdrian Hunter err = record__auxtrace_init(rec); 34201b36c03eSAdrian Hunter if (err) 34211b36c03eSAdrian Hunter goto out; 34221b36c03eSAdrian Hunter 34230aab2136SWang Nan if (dry_run) 34245c01ad60SAdrian Hunter goto out; 34250aab2136SWang Nan 3426d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 3427d7888573SWang Nan if (err) { 3428d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 3429d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 3430d7888573SWang Nan errbuf); 34315c01ad60SAdrian Hunter goto out; 3432d7888573SWang Nan } 3433d7888573SWang Nan 3434ef149c25SAdrian Hunter err = -ENOMEM; 3435ef149c25SAdrian Hunter 34360c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 3437a1ac1d3cSStephane Eranian disable_buildid_cache(); 3438dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 34390c1d46a8SWang Nan /* 34400c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 34410c1d46a8SWang Nan * generation by default to reduce data file switching 34420c1d46a8SWang Nan * overhead. Still generate buildid if they are required 34430c1d46a8SWang Nan * explicitly using 34440c1d46a8SWang Nan * 344560437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 34460c1d46a8SWang Nan * --no-no-buildid-cache 34470c1d46a8SWang Nan * 34480c1d46a8SWang Nan * Following code equals to: 34490c1d46a8SWang Nan * 34500c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 34510c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 34520c1d46a8SWang Nan * disable_buildid_cache(); 34530c1d46a8SWang Nan */ 34540c1d46a8SWang Nan bool disable = true; 34550c1d46a8SWang Nan 34560c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 34570c1d46a8SWang Nan disable = false; 34580c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 34590c1d46a8SWang Nan disable = false; 34600c1d46a8SWang Nan if (disable) { 34610c1d46a8SWang Nan rec->no_buildid = true; 34620c1d46a8SWang Nan rec->no_buildid_cache = true; 34630c1d46a8SWang Nan disable_buildid_cache(); 34640c1d46a8SWang Nan } 34650c1d46a8SWang Nan } 3466655000e7SArnaldo Carvalho de Melo 34674ea648aeSWang Nan if (record.opts.overwrite) 34684ea648aeSWang Nan record.opts.tail_synthesize = true; 34694ea648aeSWang Nan 3470b53a0755SJin Yao if (rec->evlist->core.nr_entries == 0) { 3471b53a0755SJin Yao if (perf_pmu__has_hybrid()) { 3472b53a0755SJin Yao err = evlist__add_default_hybrid(rec->evlist, 3473b53a0755SJin Yao !record.opts.no_samples); 3474b53a0755SJin Yao } else { 3475b53a0755SJin Yao err = __evlist__add_default(rec->evlist, 3476b53a0755SJin Yao !record.opts.no_samples); 3477b53a0755SJin Yao } 3478b53a0755SJin Yao 3479b53a0755SJin Yao if (err < 0) { 348069aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 3481394c01edSAdrian Hunter goto out; 3482bbd36e5eSPeter Zijlstra } 3483b53a0755SJin Yao } 348486470930SIngo Molnar 348569e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 348669e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 348769e7e5b0SAdrian Hunter 3488602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 348916ad2ffbSNamhyung Kim if (err) { 3490602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 3491c3dec27bSJiri Olsa ui__warning("%s\n", errbuf); 349216ad2ffbSNamhyung Kim } 34934bd0f2d2SNamhyung Kim 3494602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 349516ad2ffbSNamhyung Kim if (err) { 349616ad2ffbSNamhyung Kim int saved_errno = errno; 349716ad2ffbSNamhyung Kim 3498602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 34993780f488SNamhyung Kim ui__error("%s", errbuf); 350016ad2ffbSNamhyung Kim 350116ad2ffbSNamhyung Kim err = -saved_errno; 3502394c01edSAdrian Hunter goto out; 350316ad2ffbSNamhyung Kim } 35040d37aa34SArnaldo Carvalho de Melo 3505ca800068SMengting Zhang /* Enable ignoring missing threads when -u/-p option is defined. */ 3506ca800068SMengting Zhang rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; 350723dc4f15SJiri Olsa 35081d3351e6SJin Yao if (evlist__fix_hybrid_cpus(rec->evlist, rec->opts.target.cpu_list)) { 35091d3351e6SJin Yao pr_err("failed to use cpu list %s\n", 35101d3351e6SJin Yao rec->opts.target.cpu_list); 35111d3351e6SJin Yao goto out; 35121d3351e6SJin Yao } 35131d3351e6SJin Yao 35141d3351e6SJin Yao rec->opts.target.hybrid = perf_pmu__has_hybrid(); 35157248e308SAlexandre Truong 35167248e308SAlexandre Truong if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP) 35177248e308SAlexandre Truong arch__add_leaf_frame_record_opts(&rec->opts); 35187248e308SAlexandre Truong 351916ad2ffbSNamhyung Kim err = -ENOMEM; 35207748bb71SArnaldo Carvalho de Melo if (evlist__create_maps(rec->evlist, &rec->opts.target) < 0) 3521dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 352269aad6f1SArnaldo Carvalho de Melo 3523ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 3524ef149c25SAdrian Hunter if (err) 3525394c01edSAdrian Hunter goto out; 3526ef149c25SAdrian Hunter 35276156681bSNamhyung Kim /* 35286156681bSNamhyung Kim * We take all buildids when the file contains 35296156681bSNamhyung Kim * AUX area tracing data because we do not decode the 35306156681bSNamhyung Kim * trace because it would take too long. 35316156681bSNamhyung Kim */ 35326156681bSNamhyung Kim if (rec->opts.full_auxtrace) 35336156681bSNamhyung Kim rec->buildid_all = true; 35346156681bSNamhyung Kim 3535246eba8eSAdrian Hunter if (rec->opts.text_poke) { 3536246eba8eSAdrian Hunter err = record__config_text_poke(rec->evlist); 3537246eba8eSAdrian Hunter if (err) { 3538246eba8eSAdrian Hunter pr_err("record__config_text_poke failed, error %d\n", err); 3539246eba8eSAdrian Hunter goto out; 3540246eba8eSAdrian Hunter } 3541246eba8eSAdrian Hunter } 3542246eba8eSAdrian Hunter 3543b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 354439d17dacSArnaldo Carvalho de Melo err = -EINVAL; 3545394c01edSAdrian Hunter goto out; 35467e4ff9e3SMike Galbraith } 35477e4ff9e3SMike Galbraith 35487954f716SAlexey Bayduraev err = record__init_thread_masks(rec); 35497954f716SAlexey Bayduraev if (err) { 35507954f716SAlexey Bayduraev pr_err("Failed to initialize parallel data streaming masks\n"); 35517954f716SAlexey Bayduraev goto out; 35527954f716SAlexey Bayduraev } 35537954f716SAlexey Bayduraev 355493f20c0fSAlexey Budankov if (rec->opts.nr_cblocks > nr_cblocks_max) 355593f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_max; 35565d7f4116SAlexey Budankov pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks); 3557d3d1af6fSAlexey Budankov 35589d2ed645SAlexey Budankov pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]); 3559470530bbSAlexey Budankov pr_debug("mmap flush: %d\n", rec->opts.mmap_flush); 35609d2ed645SAlexey Budankov 356151255a8aSAlexey Budankov if (rec->opts.comp_level > comp_level_max) 356251255a8aSAlexey Budankov rec->opts.comp_level = comp_level_max; 356351255a8aSAlexey Budankov pr_debug("comp level: %d\n", rec->opts.comp_level); 356451255a8aSAlexey Budankov 3565d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 3566394c01edSAdrian Hunter out: 3567c12995a5SJiri Olsa evlist__delete(rec->evlist); 3568d65a458bSArnaldo Carvalho de Melo symbol__exit(); 3569ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 3570a8fcbd26SAdrian Hunter out_opts: 35717954f716SAlexey Bayduraev record__free_thread_masks(rec, rec->nr_threads); 35727954f716SAlexey Bayduraev rec->nr_threads = 0; 3573ee7fe31eSAdrian Hunter evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close); 357439d17dacSArnaldo Carvalho de Melo return err; 357586470930SIngo Molnar } 35762dd6d8a1SAdrian Hunter 35772dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 35782dd6d8a1SAdrian Hunter { 3579dc0c6127SJiri Olsa struct record *rec = &record; 3580dc0c6127SJiri Olsa 3581d20aff15SAdrian Hunter hit_auxtrace_snapshot_trigger(rec); 35823c1cb7e3SWang Nan 3583dc0c6127SJiri Olsa if (switch_output_signal(rec)) 35843c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 35852dd6d8a1SAdrian Hunter } 3586bfacbe3bSJiri Olsa 3587bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 3588bfacbe3bSJiri Olsa { 3589bfacbe3bSJiri Olsa struct record *rec = &record; 3590bfacbe3bSJiri Olsa 3591bfacbe3bSJiri Olsa if (switch_output_time(rec)) 3592bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 3593bfacbe3bSJiri Olsa } 3594