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> 13e3a23261SNamhyung Kim #include <internal/xyarray.h> 1486470930SIngo Molnar #include "util/parse-events.h" 1541840d21STaeung Song #include "util/config.h" 1686470930SIngo Molnar 178f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h" 18f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h" 197c6a1c65SPeter Zijlstra #include "util/header.h" 2066e274f3SFrederic Weisbecker #include "util/event.h" 21361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h" 2269aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 238f28827aSFrederic Weisbecker #include "util/debug.h" 24e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 2549c670b1SIan Rogers #include "util/mutex.h" 26aeb00b1aSArnaldo Carvalho de Melo #include "util/target.h" 2794c744b6SArnaldo Carvalho de Melo #include "util/session.h" 2845694aa7SArnaldo Carvalho de Melo #include "util/tool.h" 298d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 30aeb00b1aSArnaldo Carvalho de Melo #include "util/record.h" 31a12b51c4SPaul Mackerras #include "util/cpumap.h" 32fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h" 33f5fc1412SJiri Olsa #include "util/data.h" 34bcc84ec6SStephane Eranian #include "util/perf_regs.h" 35ef149c25SAdrian Hunter #include "util/auxtrace.h" 3646bc29b9SAdrian Hunter #include "util/tsc.h" 37f00898f4SAndi Kleen #include "util/parse-branch-options.h" 38bcc84ec6SStephane Eranian #include "util/parse-regs-options.h" 3940c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h" 4071dc2326SWang Nan #include "util/llvm-utils.h" 418690a2a7SWang Nan #include "util/bpf-loader.h" 425f9cf599SWang Nan #include "util/trigger.h" 43a074865eSWang Nan #include "util/perf-hooks.h" 44f13de660SAlexey Budankov #include "util/cpu-set-sched.h" 45ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h" 46c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h" 4758db1d6eSArnaldo Carvalho de Melo #include "util/units.h" 487b612e29SSong Liu #include "util/bpf-event.h" 49d99c22eaSStephane Eranian #include "util/util.h" 5070943490SStephane Eranian #include "util/pfm.h" 516953beb4SJiri Olsa #include "util/clockid.h" 52b53a0755SJin Yao #include "util/pmu-hybrid.h" 53b53a0755SJin Yao #include "util/evlist-hybrid.h" 54edc41a10SNamhyung Kim #include "util/off_cpu.h" 55*27c6f245SNamhyung Kim #include "util/bpf-filter.h" 56d8871ea7SWang Nan #include "asm/bug.h" 57c1a604dfSArnaldo Carvalho de Melo #include "perf.h" 58f466e5edSAlexey Bayduraev #include "cputopo.h" 597c6a1c65SPeter Zijlstra 60a43783aeSArnaldo Carvalho de Melo #include <errno.h> 61fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 6267230479SArnaldo Carvalho de Melo #include <locale.h> 634208735dSArnaldo Carvalho de Melo #include <poll.h> 64d99c22eaSStephane Eranian #include <pthread.h> 6586470930SIngo Molnar #include <unistd.h> 66415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID 67415ccb58SAlexey Bayduraev #include <syscall.h> 68415ccb58SAlexey Bayduraev #endif 6986470930SIngo Molnar #include <sched.h> 709607ad3aSArnaldo Carvalho de Melo #include <signal.h> 71da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 72da231338SAnand K Mistry #include <sys/eventfd.h> 73da231338SAnand K Mistry #endif 74a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 754208735dSArnaldo Carvalho de Melo #include <sys/wait.h> 76eeb399b5SAdrian Hunter #include <sys/types.h> 77eeb399b5SAdrian Hunter #include <sys/stat.h> 78eeb399b5SAdrian Hunter #include <fcntl.h> 796ef81c55SMamatha Inamdar #include <linux/err.h> 808520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 810693e680SArnaldo Carvalho de Melo #include <linux/time64.h> 82d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h> 838384a260SAlexey Budankov #include <linux/bitmap.h> 84d1e325cfSJiri Olsa #include <sys/time.h> 8578da39faSBernhard Rosenkraenzer 861b43b704SJiri Olsa struct switch_output { 87dc0c6127SJiri Olsa bool enabled; 881b43b704SJiri Olsa bool signal; 89dc0c6127SJiri Olsa unsigned long size; 90bfacbe3bSJiri Olsa unsigned long time; 91cb4e1ebbSJiri Olsa const char *str; 92cb4e1ebbSJiri Olsa bool set; 9303724b2eSAndi Kleen char **filenames; 9403724b2eSAndi Kleen int num_files; 9503724b2eSAndi Kleen int cur_file; 961b43b704SJiri Olsa }; 971b43b704SJiri Olsa 987954f716SAlexey Bayduraev struct thread_mask { 997954f716SAlexey Bayduraev struct mmap_cpu_mask maps; 1007954f716SAlexey Bayduraev struct mmap_cpu_mask affinity; 1017954f716SAlexey Bayduraev }; 1027954f716SAlexey Bayduraev 103415ccb58SAlexey Bayduraev struct record_thread { 104415ccb58SAlexey Bayduraev pid_t tid; 105415ccb58SAlexey Bayduraev struct thread_mask *mask; 106415ccb58SAlexey Bayduraev struct { 107415ccb58SAlexey Bayduraev int msg[2]; 108415ccb58SAlexey Bayduraev int ack[2]; 109415ccb58SAlexey Bayduraev } pipes; 110415ccb58SAlexey Bayduraev struct fdarray pollfd; 111415ccb58SAlexey Bayduraev int ctlfd_pos; 112415ccb58SAlexey Bayduraev int nr_mmaps; 113415ccb58SAlexey Bayduraev struct mmap **maps; 114415ccb58SAlexey Bayduraev struct mmap **overwrite_maps; 115415ccb58SAlexey Bayduraev struct record *rec; 116396b626bSAlexey Bayduraev unsigned long long samples; 117396b626bSAlexey Bayduraev unsigned long waking; 118ae9c7242SAlexey Bayduraev u64 bytes_written; 119610fbc01SAlexey Bayduraev u64 bytes_transferred; 120610fbc01SAlexey Bayduraev u64 bytes_compressed; 121415ccb58SAlexey Bayduraev }; 122415ccb58SAlexey Bayduraev 123396b626bSAlexey Bayduraev static __thread struct record_thread *thread; 124396b626bSAlexey Bayduraev 1251e5de7d9SAlexey Bayduraev enum thread_msg { 1261e5de7d9SAlexey Bayduraev THREAD_MSG__UNDEFINED = 0, 1271e5de7d9SAlexey Bayduraev THREAD_MSG__READY, 1281e5de7d9SAlexey Bayduraev THREAD_MSG__MAX, 1291e5de7d9SAlexey Bayduraev }; 1301e5de7d9SAlexey Bayduraev 1311e5de7d9SAlexey Bayduraev static const char *thread_msg_tags[THREAD_MSG__MAX] = { 1321e5de7d9SAlexey Bayduraev "UNDEFINED", "READY" 1331e5de7d9SAlexey Bayduraev }; 1341e5de7d9SAlexey Bayduraev 13506380a84SAlexey Bayduraev enum thread_spec { 13606380a84SAlexey Bayduraev THREAD_SPEC__UNDEFINED = 0, 13706380a84SAlexey Bayduraev THREAD_SPEC__CPU, 138f466e5edSAlexey Bayduraev THREAD_SPEC__CORE, 139f466e5edSAlexey Bayduraev THREAD_SPEC__PACKAGE, 140f466e5edSAlexey Bayduraev THREAD_SPEC__NUMA, 141f466e5edSAlexey Bayduraev THREAD_SPEC__USER, 142f466e5edSAlexey Bayduraev THREAD_SPEC__MAX, 143f466e5edSAlexey Bayduraev }; 144f466e5edSAlexey Bayduraev 145f466e5edSAlexey Bayduraev static const char *thread_spec_tags[THREAD_SPEC__MAX] = { 146f466e5edSAlexey Bayduraev "undefined", "cpu", "core", "package", "numa", "user" 14706380a84SAlexey Bayduraev }; 14806380a84SAlexey Bayduraev 1496562c9acSAdrian Hunter struct pollfd_index_map { 1506562c9acSAdrian Hunter int evlist_pollfd_index; 1516562c9acSAdrian Hunter int thread_pollfd_index; 1526562c9acSAdrian Hunter }; 1536562c9acSAdrian Hunter 1548c6f45a7SArnaldo Carvalho de Melo struct record { 15545694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 156b4006796SArnaldo Carvalho de Melo struct record_opts opts; 157d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 15891621be6SYang Jihong u64 thread_bytes_written; 1598ceb41d7SJiri Olsa struct perf_data data; 160ef149c25SAdrian Hunter struct auxtrace_record *itr; 16163503dbaSJiri Olsa struct evlist *evlist; 162d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 163bc477d79SArnaldo Carvalho de Melo struct evlist *sb_evlist; 164899e5ffbSArnaldo Carvalho de Melo pthread_t thread_id; 165d20deb64SArnaldo Carvalho de Melo int realtime_prio; 166899e5ffbSArnaldo Carvalho de Melo bool switch_output_event_set; 167d20deb64SArnaldo Carvalho de Melo bool no_buildid; 168d2db9a98SWang Nan bool no_buildid_set; 169d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 170d2db9a98SWang Nan bool no_buildid_cache_set; 1716156681bSNamhyung Kim bool buildid_all; 172e29386c8SJiri Olsa bool buildid_mmap; 173ecfd7a9cSWang Nan bool timestamp_filename; 17468588bafSJin Yao bool timestamp_boundary; 175edc41a10SNamhyung Kim bool off_cpu; 1761b43b704SJiri Olsa struct switch_output switch_output; 1779f065194SYang Shi unsigned long long samples; 1786d575816SJiwei Sun unsigned long output_max_size; /* = 0: unlimited */ 1799bce13eaSJiri Olsa struct perf_debuginfod debuginfod; 1807954f716SAlexey Bayduraev int nr_threads; 1817954f716SAlexey Bayduraev struct thread_mask *thread_masks; 182415ccb58SAlexey Bayduraev struct record_thread *thread_data; 1836562c9acSAdrian Hunter struct pollfd_index_map *index_map; 1846562c9acSAdrian Hunter size_t index_map_sz; 1856562c9acSAdrian Hunter size_t index_map_cnt; 1860f82ebc4SArnaldo Carvalho de Melo }; 18786470930SIngo Molnar 1886d575816SJiwei Sun static volatile int done; 1896d575816SJiwei Sun 190dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 191dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 192dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 193dc0c6127SJiri Olsa 1949d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = { 1959d2ed645SAlexey Budankov "SYS", "NODE", "CPU" 1969d2ed645SAlexey Budankov }; 1979d2ed645SAlexey Budankov 198415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID 199415ccb58SAlexey Bayduraev static inline pid_t gettid(void) 200415ccb58SAlexey Bayduraev { 201415ccb58SAlexey Bayduraev return (pid_t)syscall(__NR_gettid); 202415ccb58SAlexey Bayduraev } 203415ccb58SAlexey Bayduraev #endif 204415ccb58SAlexey Bayduraev 2053217e9feSAlexey Bayduraev static int record__threads_enabled(struct record *rec) 2063217e9feSAlexey Bayduraev { 2073217e9feSAlexey Bayduraev return rec->opts.threads_spec; 2083217e9feSAlexey Bayduraev } 2093217e9feSAlexey Bayduraev 210dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 211dc0c6127SJiri Olsa { 212dc0c6127SJiri Olsa return rec->switch_output.signal && 213dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 214dc0c6127SJiri Olsa } 215dc0c6127SJiri Olsa 216dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 217dc0c6127SJiri Olsa { 218dc0c6127SJiri Olsa return rec->switch_output.size && 219dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 220dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 221dc0c6127SJiri Olsa } 222dc0c6127SJiri Olsa 223bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 224bfacbe3bSJiri Olsa { 225bfacbe3bSJiri Olsa return rec->switch_output.time && 226bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 227bfacbe3bSJiri Olsa } 228bfacbe3bSJiri Olsa 229ae9c7242SAlexey Bayduraev static u64 record__bytes_written(struct record *rec) 230ae9c7242SAlexey Bayduraev { 23191621be6SYang Jihong return rec->bytes_written + rec->thread_bytes_written; 232ae9c7242SAlexey Bayduraev } 233ae9c7242SAlexey Bayduraev 2346d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec) 2356d575816SJiwei Sun { 2366d575816SJiwei Sun return rec->output_max_size && 237ae9c7242SAlexey Bayduraev (record__bytes_written(rec) >= rec->output_max_size); 2386d575816SJiwei Sun } 2396d575816SJiwei Sun 240a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused, 241ded2b8feSJiri Olsa void *bf, size_t size) 242f5970550SPeter Zijlstra { 243ded2b8feSJiri Olsa struct perf_data_file *file = &rec->session->data->file; 244ded2b8feSJiri Olsa 24556f735ffSAlexey Bayduraev if (map && map->file) 24656f735ffSAlexey Bayduraev file = map->file; 24756f735ffSAlexey Bayduraev 248ded2b8feSJiri Olsa if (perf_data_file__write(file, bf, size) < 0) { 2494f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 2508d3eca20SDavid Ahern return -1; 2518d3eca20SDavid Ahern } 252f5970550SPeter Zijlstra 25391621be6SYang Jihong if (map && map->file) { 254ae9c7242SAlexey Bayduraev thread->bytes_written += size; 25591621be6SYang Jihong rec->thread_bytes_written += size; 25691621be6SYang Jihong } else { 257cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 25891621be6SYang Jihong } 259dc0c6127SJiri Olsa 2606d575816SJiwei Sun if (record__output_max_size_exceeded(rec) && !done) { 2616d575816SJiwei Sun fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB)," 2626d575816SJiwei Sun " stopping session ]\n", 263ae9c7242SAlexey Bayduraev record__bytes_written(rec) >> 10); 2646d575816SJiwei Sun done = 1; 2656d575816SJiwei Sun } 2666d575816SJiwei Sun 267dc0c6127SJiri Olsa if (switch_output_size(rec)) 268dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 269dc0c6127SJiri Olsa 2708d3eca20SDavid Ahern return 0; 271f5970550SPeter Zijlstra } 272f5970550SPeter Zijlstra 273ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec); 274ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec); 27575f5f1fcSAlexey Bayduraev static size_t zstd_compress(struct perf_session *session, struct mmap *map, 27675f5f1fcSAlexey Bayduraev void *dst, size_t dst_size, void *src, size_t src_size); 2775d7f4116SAlexey Budankov 278d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 279d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd, 280d3d1af6fSAlexey Budankov void *buf, size_t size, off_t off) 281d3d1af6fSAlexey Budankov { 282d3d1af6fSAlexey Budankov int rc; 283d3d1af6fSAlexey Budankov 284d3d1af6fSAlexey Budankov cblock->aio_fildes = trace_fd; 285d3d1af6fSAlexey Budankov cblock->aio_buf = buf; 286d3d1af6fSAlexey Budankov cblock->aio_nbytes = size; 287d3d1af6fSAlexey Budankov cblock->aio_offset = off; 288d3d1af6fSAlexey Budankov cblock->aio_sigevent.sigev_notify = SIGEV_NONE; 289d3d1af6fSAlexey Budankov 290d3d1af6fSAlexey Budankov do { 291d3d1af6fSAlexey Budankov rc = aio_write(cblock); 292d3d1af6fSAlexey Budankov if (rc == 0) { 293d3d1af6fSAlexey Budankov break; 294d3d1af6fSAlexey Budankov } else if (errno != EAGAIN) { 295d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 296d3d1af6fSAlexey Budankov pr_err("failed to queue perf data, error: %m\n"); 297d3d1af6fSAlexey Budankov break; 298d3d1af6fSAlexey Budankov } 299d3d1af6fSAlexey Budankov } while (1); 300d3d1af6fSAlexey Budankov 301d3d1af6fSAlexey Budankov return rc; 302d3d1af6fSAlexey Budankov } 303d3d1af6fSAlexey Budankov 304a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock) 305d3d1af6fSAlexey Budankov { 306d3d1af6fSAlexey Budankov void *rem_buf; 307d3d1af6fSAlexey Budankov off_t rem_off; 308d3d1af6fSAlexey Budankov size_t rem_size; 309d3d1af6fSAlexey Budankov int rc, aio_errno; 310d3d1af6fSAlexey Budankov ssize_t aio_ret, written; 311d3d1af6fSAlexey Budankov 312d3d1af6fSAlexey Budankov aio_errno = aio_error(cblock); 313d3d1af6fSAlexey Budankov if (aio_errno == EINPROGRESS) 314d3d1af6fSAlexey Budankov return 0; 315d3d1af6fSAlexey Budankov 316d3d1af6fSAlexey Budankov written = aio_ret = aio_return(cblock); 317d3d1af6fSAlexey Budankov if (aio_ret < 0) { 318d3d1af6fSAlexey Budankov if (aio_errno != EINTR) 319d3d1af6fSAlexey Budankov pr_err("failed to write perf data, error: %m\n"); 320d3d1af6fSAlexey Budankov written = 0; 321d3d1af6fSAlexey Budankov } 322d3d1af6fSAlexey Budankov 323d3d1af6fSAlexey Budankov rem_size = cblock->aio_nbytes - written; 324d3d1af6fSAlexey Budankov 325d3d1af6fSAlexey Budankov if (rem_size == 0) { 326d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 327d3d1af6fSAlexey Budankov /* 328ef781128SAlexey Budankov * md->refcount is incremented in record__aio_pushfn() for 329ef781128SAlexey Budankov * every aio write request started in record__aio_push() so 330ef781128SAlexey Budankov * decrement it because the request is now complete. 331d3d1af6fSAlexey Budankov */ 33280e53d11SJiri Olsa perf_mmap__put(&md->core); 333d3d1af6fSAlexey Budankov rc = 1; 334d3d1af6fSAlexey Budankov } else { 335d3d1af6fSAlexey Budankov /* 336d3d1af6fSAlexey Budankov * aio write request may require restart with the 337d3d1af6fSAlexey Budankov * reminder if the kernel didn't write whole 338d3d1af6fSAlexey Budankov * chunk at once. 339d3d1af6fSAlexey Budankov */ 340d3d1af6fSAlexey Budankov rem_off = cblock->aio_offset + written; 341d3d1af6fSAlexey Budankov rem_buf = (void *)(cblock->aio_buf + written); 342d3d1af6fSAlexey Budankov record__aio_write(cblock, cblock->aio_fildes, 343d3d1af6fSAlexey Budankov rem_buf, rem_size, rem_off); 344d3d1af6fSAlexey Budankov rc = 0; 345d3d1af6fSAlexey Budankov } 346d3d1af6fSAlexey Budankov 347d3d1af6fSAlexey Budankov return rc; 348d3d1af6fSAlexey Budankov } 349d3d1af6fSAlexey Budankov 350a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all) 351d3d1af6fSAlexey Budankov { 35293f20c0fSAlexey Budankov struct aiocb **aiocb = md->aio.aiocb; 35393f20c0fSAlexey Budankov struct aiocb *cblocks = md->aio.cblocks; 354d3d1af6fSAlexey Budankov struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */ 35593f20c0fSAlexey Budankov int i, do_suspend; 356d3d1af6fSAlexey Budankov 357d3d1af6fSAlexey Budankov do { 35893f20c0fSAlexey Budankov do_suspend = 0; 35993f20c0fSAlexey Budankov for (i = 0; i < md->aio.nr_cblocks; ++i) { 36093f20c0fSAlexey Budankov if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) { 36193f20c0fSAlexey Budankov if (sync_all) 36293f20c0fSAlexey Budankov aiocb[i] = NULL; 36393f20c0fSAlexey Budankov else 36493f20c0fSAlexey Budankov return i; 36593f20c0fSAlexey Budankov } else { 36693f20c0fSAlexey Budankov /* 36793f20c0fSAlexey Budankov * Started aio write is not complete yet 36893f20c0fSAlexey Budankov * so it has to be waited before the 36993f20c0fSAlexey Budankov * next allocation. 37093f20c0fSAlexey Budankov */ 37193f20c0fSAlexey Budankov aiocb[i] = &cblocks[i]; 37293f20c0fSAlexey Budankov do_suspend = 1; 37393f20c0fSAlexey Budankov } 37493f20c0fSAlexey Budankov } 37593f20c0fSAlexey Budankov if (!do_suspend) 37693f20c0fSAlexey Budankov return -1; 377d3d1af6fSAlexey Budankov 37893f20c0fSAlexey Budankov while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) { 379d3d1af6fSAlexey Budankov if (!(errno == EAGAIN || errno == EINTR)) 380d3d1af6fSAlexey Budankov pr_err("failed to sync perf data, error: %m\n"); 381d3d1af6fSAlexey Budankov } 382d3d1af6fSAlexey Budankov } while (1); 383d3d1af6fSAlexey Budankov } 384d3d1af6fSAlexey Budankov 385ef781128SAlexey Budankov struct record_aio { 386ef781128SAlexey Budankov struct record *rec; 387ef781128SAlexey Budankov void *data; 388ef781128SAlexey Budankov size_t size; 389ef781128SAlexey Budankov }; 390ef781128SAlexey Budankov 391a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size) 392d3d1af6fSAlexey Budankov { 393ef781128SAlexey Budankov struct record_aio *aio = to; 394ef781128SAlexey Budankov 395ef781128SAlexey Budankov /* 396547740f7SJiri Olsa * map->core.base data pointed by buf is copied into free map->aio.data[] buffer 397ef781128SAlexey Budankov * to release space in the kernel buffer as fast as possible, calling 398ef781128SAlexey Budankov * perf_mmap__consume() from perf_mmap__push() function. 399ef781128SAlexey Budankov * 400ef781128SAlexey Budankov * That lets the kernel to proceed with storing more profiling data into 401ef781128SAlexey Budankov * the kernel buffer earlier than other per-cpu kernel buffers are handled. 402ef781128SAlexey Budankov * 403ef781128SAlexey Budankov * Coping can be done in two steps in case the chunk of profiling data 404ef781128SAlexey Budankov * crosses the upper bound of the kernel buffer. In this case we first move 405ef781128SAlexey Budankov * part of data from map->start till the upper bound and then the reminder 406ef781128SAlexey Budankov * from the beginning of the kernel buffer till the end of the data chunk. 407ef781128SAlexey Budankov */ 408ef781128SAlexey Budankov 409ef781128SAlexey Budankov if (record__comp_enabled(aio->rec)) { 41075f5f1fcSAlexey Bayduraev size = zstd_compress(aio->rec->session, NULL, aio->data + aio->size, 411bf59b305SJiri Olsa mmap__mmap_len(map) - aio->size, 412ef781128SAlexey Budankov buf, size); 413ef781128SAlexey Budankov } else { 414ef781128SAlexey Budankov memcpy(aio->data + aio->size, buf, size); 415ef781128SAlexey Budankov } 416ef781128SAlexey Budankov 417ef781128SAlexey Budankov if (!aio->size) { 418ef781128SAlexey Budankov /* 419ef781128SAlexey Budankov * Increment map->refcount to guard map->aio.data[] buffer 420ef781128SAlexey Budankov * from premature deallocation because map object can be 421ef781128SAlexey Budankov * released earlier than aio write request started on 422ef781128SAlexey Budankov * map->aio.data[] buffer is complete. 423ef781128SAlexey Budankov * 424ef781128SAlexey Budankov * perf_mmap__put() is done at record__aio_complete() 425ef781128SAlexey Budankov * after started aio request completion or at record__aio_push() 426ef781128SAlexey Budankov * if the request failed to start. 427ef781128SAlexey Budankov */ 428e75710f0SJiri Olsa perf_mmap__get(&map->core); 429ef781128SAlexey Budankov } 430ef781128SAlexey Budankov 431ef781128SAlexey Budankov aio->size += size; 432ef781128SAlexey Budankov 433ef781128SAlexey Budankov return size; 434ef781128SAlexey Budankov } 435ef781128SAlexey Budankov 436a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off) 437ef781128SAlexey Budankov { 438ef781128SAlexey Budankov int ret, idx; 439ef781128SAlexey Budankov int trace_fd = rec->session->data->file.fd; 440ef781128SAlexey Budankov struct record_aio aio = { .rec = rec, .size = 0 }; 441ef781128SAlexey Budankov 442ef781128SAlexey Budankov /* 443ef781128SAlexey Budankov * Call record__aio_sync() to wait till map->aio.data[] buffer 444ef781128SAlexey Budankov * becomes available after previous aio write operation. 445ef781128SAlexey Budankov */ 446ef781128SAlexey Budankov 447ef781128SAlexey Budankov idx = record__aio_sync(map, false); 448ef781128SAlexey Budankov aio.data = map->aio.data[idx]; 449ef781128SAlexey Budankov ret = perf_mmap__push(map, &aio, record__aio_pushfn); 450ef781128SAlexey Budankov if (ret != 0) /* ret > 0 - no data, ret < 0 - error */ 451ef781128SAlexey Budankov return ret; 452d3d1af6fSAlexey Budankov 453d3d1af6fSAlexey Budankov rec->samples++; 454ef781128SAlexey Budankov ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off); 455d3d1af6fSAlexey Budankov if (!ret) { 456ef781128SAlexey Budankov *off += aio.size; 457ef781128SAlexey Budankov rec->bytes_written += aio.size; 458d3d1af6fSAlexey Budankov if (switch_output_size(rec)) 459d3d1af6fSAlexey Budankov trigger_hit(&switch_output_trigger); 460ef781128SAlexey Budankov } else { 461ef781128SAlexey Budankov /* 462ef781128SAlexey Budankov * Decrement map->refcount incremented in record__aio_pushfn() 463ef781128SAlexey Budankov * back if record__aio_write() operation failed to start, otherwise 464ef781128SAlexey Budankov * map->refcount is decremented in record__aio_complete() after 465ef781128SAlexey Budankov * aio write operation finishes successfully. 466ef781128SAlexey Budankov */ 46780e53d11SJiri Olsa perf_mmap__put(&map->core); 468d3d1af6fSAlexey Budankov } 469d3d1af6fSAlexey Budankov 470d3d1af6fSAlexey Budankov return ret; 471d3d1af6fSAlexey Budankov } 472d3d1af6fSAlexey Budankov 473d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd) 474d3d1af6fSAlexey Budankov { 475d3d1af6fSAlexey Budankov return lseek(trace_fd, 0, SEEK_CUR); 476d3d1af6fSAlexey Budankov } 477d3d1af6fSAlexey Budankov 478d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos) 479d3d1af6fSAlexey Budankov { 480d3d1af6fSAlexey Budankov lseek(trace_fd, pos, SEEK_SET); 481d3d1af6fSAlexey Budankov } 482d3d1af6fSAlexey Budankov 483d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec) 484d3d1af6fSAlexey Budankov { 485d3d1af6fSAlexey Budankov int i; 48663503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 487a5830532SJiri Olsa struct mmap *maps = evlist->mmap; 488d3d1af6fSAlexey Budankov 489ef781128SAlexey Budankov if (!record__aio_enabled(rec)) 490d3d1af6fSAlexey Budankov return; 491d3d1af6fSAlexey Budankov 492c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 493a5830532SJiri Olsa struct mmap *map = &maps[i]; 494d3d1af6fSAlexey Budankov 495547740f7SJiri Olsa if (map->core.base) 49693f20c0fSAlexey Budankov record__aio_sync(map, true); 497d3d1af6fSAlexey Budankov } 498d3d1af6fSAlexey Budankov } 499d3d1af6fSAlexey Budankov 500d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1; 50193f20c0fSAlexey Budankov static int nr_cblocks_max = 4; 502d3d1af6fSAlexey Budankov 503d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt, 50493f20c0fSAlexey Budankov const char *str, 505d3d1af6fSAlexey Budankov int unset) 506d3d1af6fSAlexey Budankov { 507d3d1af6fSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 508d3d1af6fSAlexey Budankov 50993f20c0fSAlexey Budankov if (unset) { 510d3d1af6fSAlexey Budankov opts->nr_cblocks = 0; 51193f20c0fSAlexey Budankov } else { 51293f20c0fSAlexey Budankov if (str) 51393f20c0fSAlexey Budankov opts->nr_cblocks = strtol(str, NULL, 0); 51493f20c0fSAlexey Budankov if (!opts->nr_cblocks) 515d3d1af6fSAlexey Budankov opts->nr_cblocks = nr_cblocks_default; 51693f20c0fSAlexey Budankov } 517d3d1af6fSAlexey Budankov 518d3d1af6fSAlexey Budankov return 0; 519d3d1af6fSAlexey Budankov } 520d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */ 52193f20c0fSAlexey Budankov static int nr_cblocks_max = 0; 52293f20c0fSAlexey Budankov 523a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused, 524ef781128SAlexey Budankov off_t *off __maybe_unused) 525d3d1af6fSAlexey Budankov { 526d3d1af6fSAlexey Budankov return -1; 527d3d1af6fSAlexey Budankov } 528d3d1af6fSAlexey Budankov 529d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused) 530d3d1af6fSAlexey Budankov { 531d3d1af6fSAlexey Budankov return -1; 532d3d1af6fSAlexey Budankov } 533d3d1af6fSAlexey Budankov 534d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused) 535d3d1af6fSAlexey Budankov { 536d3d1af6fSAlexey Budankov } 537d3d1af6fSAlexey Budankov 538d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused) 539d3d1af6fSAlexey Budankov { 540d3d1af6fSAlexey Budankov } 541d3d1af6fSAlexey Budankov #endif 542d3d1af6fSAlexey Budankov 543d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec) 544d3d1af6fSAlexey Budankov { 545d3d1af6fSAlexey Budankov return rec->opts.nr_cblocks > 0; 546d3d1af6fSAlexey Budankov } 547d3d1af6fSAlexey Budankov 548470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1 549470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt, 550470530bbSAlexey Budankov const char *str, 551470530bbSAlexey Budankov int unset) 552470530bbSAlexey Budankov { 553470530bbSAlexey Budankov int flush_max; 554470530bbSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 555470530bbSAlexey Budankov static struct parse_tag tags[] = { 556470530bbSAlexey Budankov { .tag = 'B', .mult = 1 }, 557470530bbSAlexey Budankov { .tag = 'K', .mult = 1 << 10 }, 558470530bbSAlexey Budankov { .tag = 'M', .mult = 1 << 20 }, 559470530bbSAlexey Budankov { .tag = 'G', .mult = 1 << 30 }, 560470530bbSAlexey Budankov { .tag = 0 }, 561470530bbSAlexey Budankov }; 562470530bbSAlexey Budankov 563470530bbSAlexey Budankov if (unset) 564470530bbSAlexey Budankov return 0; 565470530bbSAlexey Budankov 566470530bbSAlexey Budankov if (str) { 567470530bbSAlexey Budankov opts->mmap_flush = parse_tag_value(str, tags); 568470530bbSAlexey Budankov if (opts->mmap_flush == (int)-1) 569470530bbSAlexey Budankov opts->mmap_flush = strtol(str, NULL, 0); 570470530bbSAlexey Budankov } 571470530bbSAlexey Budankov 572470530bbSAlexey Budankov if (!opts->mmap_flush) 573470530bbSAlexey Budankov opts->mmap_flush = MMAP_FLUSH_DEFAULT; 574470530bbSAlexey Budankov 5759521b5f2SJiri Olsa flush_max = evlist__mmap_size(opts->mmap_pages); 576470530bbSAlexey Budankov flush_max /= 4; 577470530bbSAlexey Budankov if (opts->mmap_flush > flush_max) 578470530bbSAlexey Budankov opts->mmap_flush = flush_max; 579470530bbSAlexey Budankov 580470530bbSAlexey Budankov return 0; 581470530bbSAlexey Budankov } 582470530bbSAlexey Budankov 583504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 584504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1; 585504c1ad1SAlexey Budankov 586504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset) 587504c1ad1SAlexey Budankov { 588504c1ad1SAlexey Budankov struct record_opts *opts = opt->value; 589504c1ad1SAlexey Budankov 590504c1ad1SAlexey Budankov if (unset) { 591504c1ad1SAlexey Budankov opts->comp_level = 0; 592504c1ad1SAlexey Budankov } else { 593504c1ad1SAlexey Budankov if (str) 594504c1ad1SAlexey Budankov opts->comp_level = strtol(str, NULL, 0); 595504c1ad1SAlexey Budankov if (!opts->comp_level) 596504c1ad1SAlexey Budankov opts->comp_level = comp_level_default; 597504c1ad1SAlexey Budankov } 598504c1ad1SAlexey Budankov 599504c1ad1SAlexey Budankov return 0; 600504c1ad1SAlexey Budankov } 601504c1ad1SAlexey Budankov #endif 60251255a8aSAlexey Budankov static unsigned int comp_level_max = 22; 60351255a8aSAlexey Budankov 60442e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec) 60542e1fd80SAlexey Budankov { 60642e1fd80SAlexey Budankov return rec->opts.comp_level > 0; 60742e1fd80SAlexey Budankov } 60842e1fd80SAlexey Budankov 60945694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 610d20deb64SArnaldo Carvalho de Melo union perf_event *event, 6111d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 6121d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 613234fbbf5SArnaldo Carvalho de Melo { 6148c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 615ded2b8feSJiri Olsa return record__write(rec, NULL, event, event->header.size); 616234fbbf5SArnaldo Carvalho de Melo } 617234fbbf5SArnaldo Carvalho de Melo 61849c670b1SIan Rogers static struct mutex synth_lock; 61949c670b1SIan Rogers 620d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool, 621d99c22eaSStephane Eranian union perf_event *event, 622d99c22eaSStephane Eranian struct perf_sample *sample __maybe_unused, 623d99c22eaSStephane Eranian struct machine *machine __maybe_unused) 624d99c22eaSStephane Eranian { 625d99c22eaSStephane Eranian int ret; 626d99c22eaSStephane Eranian 62749c670b1SIan Rogers mutex_lock(&synth_lock); 628d99c22eaSStephane Eranian ret = process_synthesized_event(tool, event, sample, machine); 62949c670b1SIan Rogers mutex_unlock(&synth_lock); 630d99c22eaSStephane Eranian return ret; 631d99c22eaSStephane Eranian } 632d99c22eaSStephane Eranian 633a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size) 634d37f1586SArnaldo Carvalho de Melo { 635d37f1586SArnaldo Carvalho de Melo struct record *rec = to; 636d37f1586SArnaldo Carvalho de Melo 6375d7f4116SAlexey Budankov if (record__comp_enabled(rec)) { 63875f5f1fcSAlexey Bayduraev size = zstd_compress(rec->session, map, map->data, mmap__mmap_len(map), bf, size); 6395d7f4116SAlexey Budankov bf = map->data; 6405d7f4116SAlexey Budankov } 6415d7f4116SAlexey Budankov 642396b626bSAlexey Bayduraev thread->samples++; 643ded2b8feSJiri Olsa return record__write(rec, map, bf, size); 644d37f1586SArnaldo Carvalho de Melo } 645d37f1586SArnaldo Carvalho de Melo 6468ed28c2bSIan Rogers static volatile sig_atomic_t signr = -1; 6478ed28c2bSIan Rogers static volatile sig_atomic_t child_finished; 648da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 6498ed28c2bSIan Rogers static volatile sig_atomic_t done_fd = -1; 650da231338SAnand K Mistry #endif 651c0bdc1c4SWang Nan 6522dd6d8a1SAdrian Hunter static void sig_handler(int sig) 6532dd6d8a1SAdrian Hunter { 6542dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 6552dd6d8a1SAdrian Hunter child_finished = 1; 6562dd6d8a1SAdrian Hunter else 6572dd6d8a1SAdrian Hunter signr = sig; 6582dd6d8a1SAdrian Hunter 6592dd6d8a1SAdrian Hunter done = 1; 660da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 661304f0a2fSIan Rogers if (done_fd >= 0) { 662da231338SAnand K Mistry u64 tmp = 1; 663304f0a2fSIan Rogers int orig_errno = errno; 664304f0a2fSIan Rogers 665da231338SAnand K Mistry /* 666304f0a2fSIan Rogers * It is possible for this signal handler to run after done is 667304f0a2fSIan Rogers * checked in the main loop, but before the perf counter fds are 668304f0a2fSIan Rogers * polled. If this happens, the poll() will continue to wait 669304f0a2fSIan Rogers * even though done is set, and will only break out if either 670304f0a2fSIan Rogers * another signal is received, or the counters are ready for 671304f0a2fSIan Rogers * read. To ensure the poll() doesn't sleep when done is set, 672304f0a2fSIan Rogers * use an eventfd (done_fd) to wake up the poll(). 673da231338SAnand K Mistry */ 674da231338SAnand K Mistry if (write(done_fd, &tmp, sizeof(tmp)) < 0) 675da231338SAnand K Mistry pr_err("failed to signal wakeup fd, error: %m\n"); 676304f0a2fSIan Rogers 677304f0a2fSIan Rogers errno = orig_errno; 678da231338SAnand K Mistry } 679da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT 6802dd6d8a1SAdrian Hunter } 6812dd6d8a1SAdrian Hunter 682a074865eSWang Nan static void sigsegv_handler(int sig) 683a074865eSWang Nan { 684a074865eSWang Nan perf_hooks__recover(); 685a074865eSWang Nan sighandler_dump_stack(sig); 686a074865eSWang Nan } 687a074865eSWang Nan 6882dd6d8a1SAdrian Hunter static void record__sig_exit(void) 6892dd6d8a1SAdrian Hunter { 6902dd6d8a1SAdrian Hunter if (signr == -1) 6912dd6d8a1SAdrian Hunter return; 6922dd6d8a1SAdrian Hunter 6932dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 6942dd6d8a1SAdrian Hunter raise(signr); 6952dd6d8a1SAdrian Hunter } 6962dd6d8a1SAdrian Hunter 697e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 698e31f0d01SAdrian Hunter 699ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 700a5830532SJiri Olsa struct mmap *map, 701ef149c25SAdrian Hunter union perf_event *event, void *data1, 702ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 703ef149c25SAdrian Hunter { 704ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 7058ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 706ef149c25SAdrian Hunter size_t padding; 707ef149c25SAdrian Hunter u8 pad[8] = {0}; 708ef149c25SAdrian Hunter 70946e201efSAdrian Hunter if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) { 71099fa2984SAdrian Hunter off_t file_offset; 7118ceb41d7SJiri Olsa int fd = perf_data__fd(data); 71299fa2984SAdrian Hunter int err; 71399fa2984SAdrian Hunter 71499fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 71599fa2984SAdrian Hunter if (file_offset == -1) 71699fa2984SAdrian Hunter return -1; 71799fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 71899fa2984SAdrian Hunter event, file_offset); 71999fa2984SAdrian Hunter if (err) 72099fa2984SAdrian Hunter return err; 72199fa2984SAdrian Hunter } 72299fa2984SAdrian Hunter 723ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 724ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 725ef149c25SAdrian Hunter if (padding) 726ef149c25SAdrian Hunter padding = 8 - padding; 727ef149c25SAdrian Hunter 728ded2b8feSJiri Olsa record__write(rec, map, event, event->header.size); 729ded2b8feSJiri Olsa record__write(rec, map, data1, len1); 730ef149c25SAdrian Hunter if (len2) 731ded2b8feSJiri Olsa record__write(rec, map, data2, len2); 732ded2b8feSJiri Olsa record__write(rec, map, &pad, padding); 733ef149c25SAdrian Hunter 734ef149c25SAdrian Hunter return 0; 735ef149c25SAdrian Hunter } 736ef149c25SAdrian Hunter 737ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 738a5830532SJiri Olsa struct mmap *map) 739ef149c25SAdrian Hunter { 740ef149c25SAdrian Hunter int ret; 741ef149c25SAdrian Hunter 742e035f4caSJiri Olsa ret = auxtrace_mmap__read(map, rec->itr, &rec->tool, 743ef149c25SAdrian Hunter record__process_auxtrace); 744ef149c25SAdrian Hunter if (ret < 0) 745ef149c25SAdrian Hunter return ret; 746ef149c25SAdrian Hunter 747ef149c25SAdrian Hunter if (ret) 748ef149c25SAdrian Hunter rec->samples++; 749ef149c25SAdrian Hunter 750ef149c25SAdrian Hunter return 0; 751ef149c25SAdrian Hunter } 752ef149c25SAdrian Hunter 7532dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 754a5830532SJiri Olsa struct mmap *map) 7552dd6d8a1SAdrian Hunter { 7562dd6d8a1SAdrian Hunter int ret; 7572dd6d8a1SAdrian Hunter 758e035f4caSJiri Olsa ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool, 7592dd6d8a1SAdrian Hunter record__process_auxtrace, 7602dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 7612dd6d8a1SAdrian Hunter if (ret < 0) 7622dd6d8a1SAdrian Hunter return ret; 7632dd6d8a1SAdrian Hunter 7642dd6d8a1SAdrian Hunter if (ret) 7652dd6d8a1SAdrian Hunter rec->samples++; 7662dd6d8a1SAdrian Hunter 7672dd6d8a1SAdrian Hunter return 0; 7682dd6d8a1SAdrian Hunter } 7692dd6d8a1SAdrian Hunter 7702dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 7712dd6d8a1SAdrian Hunter { 7722dd6d8a1SAdrian Hunter int i; 7732dd6d8a1SAdrian Hunter int rc = 0; 7742dd6d8a1SAdrian Hunter 775c976ee11SJiri Olsa for (i = 0; i < rec->evlist->core.nr_mmaps; i++) { 776a5830532SJiri Olsa struct mmap *map = &rec->evlist->mmap[i]; 7772dd6d8a1SAdrian Hunter 778e035f4caSJiri Olsa if (!map->auxtrace_mmap.base) 7792dd6d8a1SAdrian Hunter continue; 7802dd6d8a1SAdrian Hunter 781e035f4caSJiri Olsa if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) { 7822dd6d8a1SAdrian Hunter rc = -1; 7832dd6d8a1SAdrian Hunter goto out; 7842dd6d8a1SAdrian Hunter } 7852dd6d8a1SAdrian Hunter } 7862dd6d8a1SAdrian Hunter out: 7872dd6d8a1SAdrian Hunter return rc; 7882dd6d8a1SAdrian Hunter } 7892dd6d8a1SAdrian Hunter 790ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit) 7912dd6d8a1SAdrian Hunter { 7922dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 7932dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 7945f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 7952dd6d8a1SAdrian Hunter } else { 796ce7b0e42SAlexander Shishkin if (auxtrace_record__snapshot_finish(rec->itr, on_exit)) 7975f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 7985f9cf599SWang Nan else 7995f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 8002dd6d8a1SAdrian Hunter } 8012dd6d8a1SAdrian Hunter } 8022dd6d8a1SAdrian Hunter 803ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec) 804ce7b0e42SAlexander Shishkin { 805ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 806ce7b0e42SAlexander Shishkin return 0; 807ce7b0e42SAlexander Shishkin 808ce7b0e42SAlexander Shishkin if (!auxtrace_record__snapshot_started && 809ce7b0e42SAlexander Shishkin auxtrace_record__snapshot_start(rec->itr)) 810ce7b0e42SAlexander Shishkin return -1; 811ce7b0e42SAlexander Shishkin 812ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, true); 813ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 814ce7b0e42SAlexander Shishkin return -1; 815ce7b0e42SAlexander Shishkin 816ce7b0e42SAlexander Shishkin return 0; 817ce7b0e42SAlexander Shishkin } 818ce7b0e42SAlexander Shishkin 8194b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec) 8204b5ea3bdSAdrian Hunter { 8214b5ea3bdSAdrian Hunter int err; 8224b5ea3bdSAdrian Hunter 823b5f2511dSAlexey Bayduraev if ((rec->opts.auxtrace_snapshot_opts || rec->opts.auxtrace_sample_opts) 824b5f2511dSAlexey Bayduraev && record__threads_enabled(rec)) { 825b5f2511dSAlexey Bayduraev pr_err("AUX area tracing options are not available in parallel streaming mode.\n"); 826b5f2511dSAlexey Bayduraev return -EINVAL; 827b5f2511dSAlexey Bayduraev } 828b5f2511dSAlexey Bayduraev 8294b5ea3bdSAdrian Hunter if (!rec->itr) { 8304b5ea3bdSAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 8314b5ea3bdSAdrian Hunter if (err) 8324b5ea3bdSAdrian Hunter return err; 8334b5ea3bdSAdrian Hunter } 8344b5ea3bdSAdrian Hunter 8354b5ea3bdSAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 8364b5ea3bdSAdrian Hunter rec->opts.auxtrace_snapshot_opts); 8374b5ea3bdSAdrian Hunter if (err) 8384b5ea3bdSAdrian Hunter return err; 8394b5ea3bdSAdrian Hunter 840c0a6de06SAdrian Hunter err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts, 841c0a6de06SAdrian Hunter rec->opts.auxtrace_sample_opts); 842c0a6de06SAdrian Hunter if (err) 843c0a6de06SAdrian Hunter return err; 844c0a6de06SAdrian Hunter 845d58b3f7eSAdrian Hunter auxtrace_regroup_aux_output(rec->evlist); 846d58b3f7eSAdrian Hunter 8474b5ea3bdSAdrian Hunter return auxtrace_parse_filters(rec->evlist); 8484b5ea3bdSAdrian Hunter } 8494b5ea3bdSAdrian Hunter 850e31f0d01SAdrian Hunter #else 851e31f0d01SAdrian Hunter 852e31f0d01SAdrian Hunter static inline 853e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 854a5830532SJiri Olsa struct mmap *map __maybe_unused) 855e31f0d01SAdrian Hunter { 856e31f0d01SAdrian Hunter return 0; 857e31f0d01SAdrian Hunter } 858e31f0d01SAdrian Hunter 8592dd6d8a1SAdrian Hunter static inline 860ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused, 861ce7b0e42SAlexander Shishkin bool on_exit __maybe_unused) 8622dd6d8a1SAdrian Hunter { 8632dd6d8a1SAdrian Hunter } 8642dd6d8a1SAdrian Hunter 8652dd6d8a1SAdrian Hunter static inline 8662dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 8672dd6d8a1SAdrian Hunter { 8682dd6d8a1SAdrian Hunter return 0; 8692dd6d8a1SAdrian Hunter } 8702dd6d8a1SAdrian Hunter 871ce7b0e42SAlexander Shishkin static inline 872ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused) 873ce7b0e42SAlexander Shishkin { 874ce7b0e42SAlexander Shishkin return 0; 875ce7b0e42SAlexander Shishkin } 876ce7b0e42SAlexander Shishkin 8774b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused) 8784b5ea3bdSAdrian Hunter { 8794b5ea3bdSAdrian Hunter return 0; 8804b5ea3bdSAdrian Hunter } 8814b5ea3bdSAdrian Hunter 882e31f0d01SAdrian Hunter #endif 883e31f0d01SAdrian Hunter 884246eba8eSAdrian Hunter static int record__config_text_poke(struct evlist *evlist) 885246eba8eSAdrian Hunter { 886246eba8eSAdrian Hunter struct evsel *evsel; 887246eba8eSAdrian Hunter 888246eba8eSAdrian Hunter /* Nothing to do if text poke is already configured */ 889246eba8eSAdrian Hunter evlist__for_each_entry(evlist, evsel) { 890246eba8eSAdrian Hunter if (evsel->core.attr.text_poke) 891246eba8eSAdrian Hunter return 0; 892246eba8eSAdrian Hunter } 893246eba8eSAdrian Hunter 894921e3be5SAdrian Hunter evsel = evlist__add_dummy_on_all_cpus(evlist); 895921e3be5SAdrian Hunter if (!evsel) 896921e3be5SAdrian Hunter return -ENOMEM; 897246eba8eSAdrian Hunter 898246eba8eSAdrian Hunter evsel->core.attr.text_poke = 1; 899246eba8eSAdrian Hunter evsel->core.attr.ksymbol = 1; 900246eba8eSAdrian Hunter evsel->immediate = true; 901246eba8eSAdrian Hunter evsel__set_sample_bit(evsel, TIME); 902246eba8eSAdrian Hunter 903246eba8eSAdrian Hunter return 0; 904246eba8eSAdrian Hunter } 905246eba8eSAdrian Hunter 906edc41a10SNamhyung Kim static int record__config_off_cpu(struct record *rec) 907edc41a10SNamhyung Kim { 908685439a7SNamhyung Kim return off_cpu_prepare(rec->evlist, &rec->opts.target, &rec->opts); 909edc41a10SNamhyung Kim } 910edc41a10SNamhyung Kim 911eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine) 912eeb399b5SAdrian Hunter { 913eeb399b5SAdrian Hunter char kcore[PATH_MAX]; 914eeb399b5SAdrian Hunter int fd; 915eeb399b5SAdrian Hunter 916eeb399b5SAdrian Hunter scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir); 917eeb399b5SAdrian Hunter 918eeb399b5SAdrian Hunter fd = open(kcore, O_RDONLY); 919eeb399b5SAdrian Hunter if (fd < 0) 920eeb399b5SAdrian Hunter return false; 921eeb399b5SAdrian Hunter 922eeb399b5SAdrian Hunter close(fd); 923eeb399b5SAdrian Hunter 924eeb399b5SAdrian Hunter return true; 925eeb399b5SAdrian Hunter } 926eeb399b5SAdrian Hunter 927eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data) 928eeb399b5SAdrian Hunter { 929eeb399b5SAdrian Hunter char from_dir[PATH_MAX]; 930eeb399b5SAdrian Hunter char kcore_dir[PATH_MAX]; 931eeb399b5SAdrian Hunter int ret; 932eeb399b5SAdrian Hunter 933eeb399b5SAdrian Hunter snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir); 934eeb399b5SAdrian Hunter 935eeb399b5SAdrian Hunter ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir)); 936eeb399b5SAdrian Hunter if (ret) 937eeb399b5SAdrian Hunter return ret; 938eeb399b5SAdrian Hunter 939eeb399b5SAdrian Hunter return kcore_copy(from_dir, kcore_dir); 940eeb399b5SAdrian Hunter } 941eeb399b5SAdrian Hunter 942415ccb58SAlexey Bayduraev static void record__thread_data_init_pipes(struct record_thread *thread_data) 943415ccb58SAlexey Bayduraev { 944415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 945415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 946415ccb58SAlexey Bayduraev thread_data->pipes.ack[0] = -1; 947415ccb58SAlexey Bayduraev thread_data->pipes.ack[1] = -1; 948415ccb58SAlexey Bayduraev } 949415ccb58SAlexey Bayduraev 950415ccb58SAlexey Bayduraev static int record__thread_data_open_pipes(struct record_thread *thread_data) 951415ccb58SAlexey Bayduraev { 952415ccb58SAlexey Bayduraev if (pipe(thread_data->pipes.msg)) 953415ccb58SAlexey Bayduraev return -EINVAL; 954415ccb58SAlexey Bayduraev 955415ccb58SAlexey Bayduraev if (pipe(thread_data->pipes.ack)) { 956415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[0]); 957415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 958415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[1]); 959415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 960415ccb58SAlexey Bayduraev return -EINVAL; 961415ccb58SAlexey Bayduraev } 962415ccb58SAlexey Bayduraev 963415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: msg=[%d,%d], ack=[%d,%d]\n", thread_data, 964415ccb58SAlexey Bayduraev thread_data->pipes.msg[0], thread_data->pipes.msg[1], 965415ccb58SAlexey Bayduraev thread_data->pipes.ack[0], thread_data->pipes.ack[1]); 966415ccb58SAlexey Bayduraev 967415ccb58SAlexey Bayduraev return 0; 968415ccb58SAlexey Bayduraev } 969415ccb58SAlexey Bayduraev 970415ccb58SAlexey Bayduraev static void record__thread_data_close_pipes(struct record_thread *thread_data) 971415ccb58SAlexey Bayduraev { 972415ccb58SAlexey Bayduraev if (thread_data->pipes.msg[0] != -1) { 973415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[0]); 974415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 975415ccb58SAlexey Bayduraev } 976415ccb58SAlexey Bayduraev if (thread_data->pipes.msg[1] != -1) { 977415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[1]); 978415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 979415ccb58SAlexey Bayduraev } 980415ccb58SAlexey Bayduraev if (thread_data->pipes.ack[0] != -1) { 981415ccb58SAlexey Bayduraev close(thread_data->pipes.ack[0]); 982415ccb58SAlexey Bayduraev thread_data->pipes.ack[0] = -1; 983415ccb58SAlexey Bayduraev } 984415ccb58SAlexey Bayduraev if (thread_data->pipes.ack[1] != -1) { 985415ccb58SAlexey Bayduraev close(thread_data->pipes.ack[1]); 986415ccb58SAlexey Bayduraev thread_data->pipes.ack[1] = -1; 987415ccb58SAlexey Bayduraev } 988415ccb58SAlexey Bayduraev } 989415ccb58SAlexey Bayduraev 9907be1feddSAdrian Hunter static bool evlist__per_thread(struct evlist *evlist) 9917be1feddSAdrian Hunter { 9927be1feddSAdrian Hunter return cpu_map__is_dummy(evlist->core.user_requested_cpus); 9937be1feddSAdrian Hunter } 9947be1feddSAdrian Hunter 995415ccb58SAlexey Bayduraev static int record__thread_data_init_maps(struct record_thread *thread_data, struct evlist *evlist) 996415ccb58SAlexey Bayduraev { 997415ccb58SAlexey Bayduraev int m, tm, nr_mmaps = evlist->core.nr_mmaps; 998415ccb58SAlexey Bayduraev struct mmap *mmap = evlist->mmap; 999415ccb58SAlexey Bayduraev struct mmap *overwrite_mmap = evlist->overwrite_mmap; 10007be1feddSAdrian Hunter struct perf_cpu_map *cpus = evlist->core.all_cpus; 10017be1feddSAdrian Hunter bool per_thread = evlist__per_thread(evlist); 1002415ccb58SAlexey Bayduraev 10037be1feddSAdrian Hunter if (per_thread) 100423380e4dSAlexey Bayduraev thread_data->nr_mmaps = nr_mmaps; 100523380e4dSAlexey Bayduraev else 1006415ccb58SAlexey Bayduraev thread_data->nr_mmaps = bitmap_weight(thread_data->mask->maps.bits, 1007415ccb58SAlexey Bayduraev thread_data->mask->maps.nbits); 1008415ccb58SAlexey Bayduraev if (mmap) { 1009415ccb58SAlexey Bayduraev thread_data->maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *)); 1010415ccb58SAlexey Bayduraev if (!thread_data->maps) 1011415ccb58SAlexey Bayduraev return -ENOMEM; 1012415ccb58SAlexey Bayduraev } 1013415ccb58SAlexey Bayduraev if (overwrite_mmap) { 1014415ccb58SAlexey Bayduraev thread_data->overwrite_maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *)); 1015415ccb58SAlexey Bayduraev if (!thread_data->overwrite_maps) { 1016415ccb58SAlexey Bayduraev zfree(&thread_data->maps); 1017415ccb58SAlexey Bayduraev return -ENOMEM; 1018415ccb58SAlexey Bayduraev } 1019415ccb58SAlexey Bayduraev } 1020415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: nr_mmaps=%d, maps=%p, ow_maps=%p\n", thread_data, 1021415ccb58SAlexey Bayduraev thread_data->nr_mmaps, thread_data->maps, thread_data->overwrite_maps); 1022415ccb58SAlexey Bayduraev 1023415ccb58SAlexey Bayduraev for (m = 0, tm = 0; m < nr_mmaps && tm < thread_data->nr_mmaps; m++) { 10247be1feddSAdrian Hunter if (per_thread || 10250255571aSIan Rogers test_bit(perf_cpu_map__cpu(cpus, m).cpu, thread_data->mask->maps.bits)) { 1026415ccb58SAlexey Bayduraev if (thread_data->maps) { 1027415ccb58SAlexey Bayduraev thread_data->maps[tm] = &mmap[m]; 1028415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: cpu%d: maps[%d] -> mmap[%d]\n", 102923380e4dSAlexey Bayduraev thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m); 1030415ccb58SAlexey Bayduraev } 1031415ccb58SAlexey Bayduraev if (thread_data->overwrite_maps) { 1032415ccb58SAlexey Bayduraev thread_data->overwrite_maps[tm] = &overwrite_mmap[m]; 1033415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: cpu%d: ow_maps[%d] -> ow_mmap[%d]\n", 103423380e4dSAlexey Bayduraev thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m); 1035415ccb58SAlexey Bayduraev } 1036415ccb58SAlexey Bayduraev tm++; 1037415ccb58SAlexey Bayduraev } 1038415ccb58SAlexey Bayduraev } 1039415ccb58SAlexey Bayduraev 1040415ccb58SAlexey Bayduraev return 0; 1041415ccb58SAlexey Bayduraev } 1042415ccb58SAlexey Bayduraev 1043415ccb58SAlexey Bayduraev static int record__thread_data_init_pollfd(struct record_thread *thread_data, struct evlist *evlist) 1044415ccb58SAlexey Bayduraev { 1045415ccb58SAlexey Bayduraev int f, tm, pos; 1046415ccb58SAlexey Bayduraev struct mmap *map, *overwrite_map; 1047415ccb58SAlexey Bayduraev 1048415ccb58SAlexey Bayduraev fdarray__init(&thread_data->pollfd, 64); 1049415ccb58SAlexey Bayduraev 1050415ccb58SAlexey Bayduraev for (tm = 0; tm < thread_data->nr_mmaps; tm++) { 1051415ccb58SAlexey Bayduraev map = thread_data->maps ? thread_data->maps[tm] : NULL; 1052415ccb58SAlexey Bayduraev overwrite_map = thread_data->overwrite_maps ? 1053415ccb58SAlexey Bayduraev thread_data->overwrite_maps[tm] : NULL; 1054415ccb58SAlexey Bayduraev 1055415ccb58SAlexey Bayduraev for (f = 0; f < evlist->core.pollfd.nr; f++) { 1056415ccb58SAlexey Bayduraev void *ptr = evlist->core.pollfd.priv[f].ptr; 1057415ccb58SAlexey Bayduraev 1058415ccb58SAlexey Bayduraev if ((map && ptr == map) || (overwrite_map && ptr == overwrite_map)) { 1059415ccb58SAlexey Bayduraev pos = fdarray__dup_entry_from(&thread_data->pollfd, f, 1060415ccb58SAlexey Bayduraev &evlist->core.pollfd); 1061415ccb58SAlexey Bayduraev if (pos < 0) 1062415ccb58SAlexey Bayduraev return pos; 1063415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: pollfd[%d] <- event_fd=%d\n", 1064415ccb58SAlexey Bayduraev thread_data, pos, evlist->core.pollfd.entries[f].fd); 1065415ccb58SAlexey Bayduraev } 1066415ccb58SAlexey Bayduraev } 1067415ccb58SAlexey Bayduraev } 1068415ccb58SAlexey Bayduraev 1069415ccb58SAlexey Bayduraev return 0; 1070415ccb58SAlexey Bayduraev } 1071415ccb58SAlexey Bayduraev 1072415ccb58SAlexey Bayduraev static void record__free_thread_data(struct record *rec) 1073415ccb58SAlexey Bayduraev { 1074415ccb58SAlexey Bayduraev int t; 1075415ccb58SAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 1076415ccb58SAlexey Bayduraev 1077415ccb58SAlexey Bayduraev if (thread_data == NULL) 1078415ccb58SAlexey Bayduraev return; 1079415ccb58SAlexey Bayduraev 1080415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 1081415ccb58SAlexey Bayduraev record__thread_data_close_pipes(&thread_data[t]); 1082415ccb58SAlexey Bayduraev zfree(&thread_data[t].maps); 1083415ccb58SAlexey Bayduraev zfree(&thread_data[t].overwrite_maps); 1084415ccb58SAlexey Bayduraev fdarray__exit(&thread_data[t].pollfd); 1085415ccb58SAlexey Bayduraev } 1086415ccb58SAlexey Bayduraev 1087415ccb58SAlexey Bayduraev zfree(&rec->thread_data); 1088415ccb58SAlexey Bayduraev } 1089415ccb58SAlexey Bayduraev 10906562c9acSAdrian Hunter static int record__map_thread_evlist_pollfd_indexes(struct record *rec, 10916562c9acSAdrian Hunter int evlist_pollfd_index, 10926562c9acSAdrian Hunter int thread_pollfd_index) 10936562c9acSAdrian Hunter { 10946562c9acSAdrian Hunter size_t x = rec->index_map_cnt; 10956562c9acSAdrian Hunter 10966562c9acSAdrian Hunter if (realloc_array_as_needed(rec->index_map, rec->index_map_sz, x, NULL)) 10976562c9acSAdrian Hunter return -ENOMEM; 10986562c9acSAdrian Hunter rec->index_map[x].evlist_pollfd_index = evlist_pollfd_index; 10996562c9acSAdrian Hunter rec->index_map[x].thread_pollfd_index = thread_pollfd_index; 11006562c9acSAdrian Hunter rec->index_map_cnt += 1; 11016562c9acSAdrian Hunter return 0; 11026562c9acSAdrian Hunter } 11036562c9acSAdrian Hunter 11046562c9acSAdrian Hunter static int record__update_evlist_pollfd_from_thread(struct record *rec, 11056562c9acSAdrian Hunter struct evlist *evlist, 11066562c9acSAdrian Hunter struct record_thread *thread_data) 11076562c9acSAdrian Hunter { 11086562c9acSAdrian Hunter struct pollfd *e_entries = evlist->core.pollfd.entries; 11096562c9acSAdrian Hunter struct pollfd *t_entries = thread_data->pollfd.entries; 11106562c9acSAdrian Hunter int err = 0; 11116562c9acSAdrian Hunter size_t i; 11126562c9acSAdrian Hunter 11136562c9acSAdrian Hunter for (i = 0; i < rec->index_map_cnt; i++) { 11146562c9acSAdrian Hunter int e_pos = rec->index_map[i].evlist_pollfd_index; 11156562c9acSAdrian Hunter int t_pos = rec->index_map[i].thread_pollfd_index; 11166562c9acSAdrian Hunter 11176562c9acSAdrian Hunter if (e_entries[e_pos].fd != t_entries[t_pos].fd || 11186562c9acSAdrian Hunter e_entries[e_pos].events != t_entries[t_pos].events) { 11196562c9acSAdrian Hunter pr_err("Thread and evlist pollfd index mismatch\n"); 11206562c9acSAdrian Hunter err = -EINVAL; 11216562c9acSAdrian Hunter continue; 11226562c9acSAdrian Hunter } 11236562c9acSAdrian Hunter e_entries[e_pos].revents = t_entries[t_pos].revents; 11246562c9acSAdrian Hunter } 11256562c9acSAdrian Hunter return err; 11266562c9acSAdrian Hunter } 11276562c9acSAdrian Hunter 11286562c9acSAdrian Hunter static int record__dup_non_perf_events(struct record *rec, 11296562c9acSAdrian Hunter struct evlist *evlist, 11306562c9acSAdrian Hunter struct record_thread *thread_data) 11316562c9acSAdrian Hunter { 11326562c9acSAdrian Hunter struct fdarray *fda = &evlist->core.pollfd; 11336562c9acSAdrian Hunter int i, ret; 11346562c9acSAdrian Hunter 11356562c9acSAdrian Hunter for (i = 0; i < fda->nr; i++) { 11366562c9acSAdrian Hunter if (!(fda->priv[i].flags & fdarray_flag__non_perf_event)) 11376562c9acSAdrian Hunter continue; 11386562c9acSAdrian Hunter ret = fdarray__dup_entry_from(&thread_data->pollfd, i, fda); 11396562c9acSAdrian Hunter if (ret < 0) { 11406562c9acSAdrian Hunter pr_err("Failed to duplicate descriptor in main thread pollfd\n"); 11416562c9acSAdrian Hunter return ret; 11426562c9acSAdrian Hunter } 11436562c9acSAdrian Hunter pr_debug2("thread_data[%p]: pollfd[%d] <- non_perf_event fd=%d\n", 11446562c9acSAdrian Hunter thread_data, ret, fda->entries[i].fd); 11456562c9acSAdrian Hunter ret = record__map_thread_evlist_pollfd_indexes(rec, i, ret); 11466562c9acSAdrian Hunter if (ret < 0) { 11476562c9acSAdrian Hunter pr_err("Failed to map thread and evlist pollfd indexes\n"); 11486562c9acSAdrian Hunter return ret; 11496562c9acSAdrian Hunter } 11506562c9acSAdrian Hunter } 11516562c9acSAdrian Hunter return 0; 11526562c9acSAdrian Hunter } 11536562c9acSAdrian Hunter 1154415ccb58SAlexey Bayduraev static int record__alloc_thread_data(struct record *rec, struct evlist *evlist) 1155415ccb58SAlexey Bayduraev { 1156415ccb58SAlexey Bayduraev int t, ret; 1157415ccb58SAlexey Bayduraev struct record_thread *thread_data; 1158415ccb58SAlexey Bayduraev 1159415ccb58SAlexey Bayduraev rec->thread_data = zalloc(rec->nr_threads * sizeof(*(rec->thread_data))); 1160415ccb58SAlexey Bayduraev if (!rec->thread_data) { 1161415ccb58SAlexey Bayduraev pr_err("Failed to allocate thread data\n"); 1162415ccb58SAlexey Bayduraev return -ENOMEM; 1163415ccb58SAlexey Bayduraev } 1164415ccb58SAlexey Bayduraev thread_data = rec->thread_data; 1165415ccb58SAlexey Bayduraev 1166415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 1167415ccb58SAlexey Bayduraev record__thread_data_init_pipes(&thread_data[t]); 1168415ccb58SAlexey Bayduraev 1169415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 1170415ccb58SAlexey Bayduraev thread_data[t].rec = rec; 1171415ccb58SAlexey Bayduraev thread_data[t].mask = &rec->thread_masks[t]; 1172415ccb58SAlexey Bayduraev ret = record__thread_data_init_maps(&thread_data[t], evlist); 1173415ccb58SAlexey Bayduraev if (ret) { 1174415ccb58SAlexey Bayduraev pr_err("Failed to initialize thread[%d] maps\n", t); 1175415ccb58SAlexey Bayduraev goto out_free; 1176415ccb58SAlexey Bayduraev } 1177415ccb58SAlexey Bayduraev ret = record__thread_data_init_pollfd(&thread_data[t], evlist); 1178415ccb58SAlexey Bayduraev if (ret) { 1179415ccb58SAlexey Bayduraev pr_err("Failed to initialize thread[%d] pollfd\n", t); 1180415ccb58SAlexey Bayduraev goto out_free; 1181415ccb58SAlexey Bayduraev } 1182415ccb58SAlexey Bayduraev if (t) { 1183415ccb58SAlexey Bayduraev thread_data[t].tid = -1; 1184415ccb58SAlexey Bayduraev ret = record__thread_data_open_pipes(&thread_data[t]); 1185415ccb58SAlexey Bayduraev if (ret) { 1186415ccb58SAlexey Bayduraev pr_err("Failed to open thread[%d] communication pipes\n", t); 1187415ccb58SAlexey Bayduraev goto out_free; 1188415ccb58SAlexey Bayduraev } 1189415ccb58SAlexey Bayduraev ret = fdarray__add(&thread_data[t].pollfd, thread_data[t].pipes.msg[0], 1190415ccb58SAlexey Bayduraev POLLIN | POLLERR | POLLHUP, fdarray_flag__nonfilterable); 1191415ccb58SAlexey Bayduraev if (ret < 0) { 1192415ccb58SAlexey Bayduraev pr_err("Failed to add descriptor to thread[%d] pollfd\n", t); 1193415ccb58SAlexey Bayduraev goto out_free; 1194415ccb58SAlexey Bayduraev } 1195415ccb58SAlexey Bayduraev thread_data[t].ctlfd_pos = ret; 1196415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: pollfd[%d] <- ctl_fd=%d\n", 1197415ccb58SAlexey Bayduraev thread_data, thread_data[t].ctlfd_pos, 1198415ccb58SAlexey Bayduraev thread_data[t].pipes.msg[0]); 1199415ccb58SAlexey Bayduraev } else { 1200415ccb58SAlexey Bayduraev thread_data[t].tid = gettid(); 12016562c9acSAdrian Hunter 12026562c9acSAdrian Hunter ret = record__dup_non_perf_events(rec, evlist, &thread_data[t]); 12036562c9acSAdrian Hunter if (ret < 0) 12046562c9acSAdrian Hunter goto out_free; 12056562c9acSAdrian Hunter 1206feff0b61SAdrian Hunter thread_data[t].ctlfd_pos = -1; /* Not used */ 1207415ccb58SAlexey Bayduraev } 1208415ccb58SAlexey Bayduraev } 1209415ccb58SAlexey Bayduraev 1210415ccb58SAlexey Bayduraev return 0; 1211415ccb58SAlexey Bayduraev 1212415ccb58SAlexey Bayduraev out_free: 1213415ccb58SAlexey Bayduraev record__free_thread_data(rec); 1214415ccb58SAlexey Bayduraev 1215415ccb58SAlexey Bayduraev return ret; 1216415ccb58SAlexey Bayduraev } 1217415ccb58SAlexey Bayduraev 1218cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 121963503dbaSJiri Olsa struct evlist *evlist) 1220cda57a8cSWang Nan { 122156f735ffSAlexey Bayduraev int i, ret; 1222cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 1223c0a6de06SAdrian Hunter bool auxtrace_overwrite = opts->auxtrace_snapshot_mode || 1224c0a6de06SAdrian Hunter opts->auxtrace_sample_mode; 1225cda57a8cSWang Nan char msg[512]; 1226cda57a8cSWang Nan 1227f13de660SAlexey Budankov if (opts->affinity != PERF_AFFINITY_SYS) 1228f13de660SAlexey Budankov cpu__setup_cpunode_map(); 1229f13de660SAlexey Budankov 12309521b5f2SJiri Olsa if (evlist__mmap_ex(evlist, opts->mmap_pages, 1231cda57a8cSWang Nan opts->auxtrace_mmap_pages, 1232c0a6de06SAdrian Hunter auxtrace_overwrite, 1233470530bbSAlexey Budankov opts->nr_cblocks, opts->affinity, 123451255a8aSAlexey Budankov opts->mmap_flush, opts->comp_level) < 0) { 1235cda57a8cSWang Nan if (errno == EPERM) { 1236cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 1237cda57a8cSWang Nan "Consider increasing " 1238cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 1239cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 1240cda57a8cSWang Nan "(current value: %u,%u)\n", 1241cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 1242cda57a8cSWang Nan return -errno; 1243cda57a8cSWang Nan } else { 1244cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 1245c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 1246cda57a8cSWang Nan if (errno) 1247cda57a8cSWang Nan return -errno; 1248cda57a8cSWang Nan else 1249cda57a8cSWang Nan return -EINVAL; 1250cda57a8cSWang Nan } 1251cda57a8cSWang Nan } 1252415ccb58SAlexey Bayduraev 1253415ccb58SAlexey Bayduraev if (evlist__initialize_ctlfd(evlist, opts->ctl_fd, opts->ctl_fd_ack)) 1254415ccb58SAlexey Bayduraev return -1; 1255415ccb58SAlexey Bayduraev 1256415ccb58SAlexey Bayduraev ret = record__alloc_thread_data(rec, evlist); 1257415ccb58SAlexey Bayduraev if (ret) 1258415ccb58SAlexey Bayduraev return ret; 1259415ccb58SAlexey Bayduraev 126056f735ffSAlexey Bayduraev if (record__threads_enabled(rec)) { 126156f735ffSAlexey Bayduraev ret = perf_data__create_dir(&rec->data, evlist->core.nr_mmaps); 126265e7c963SAlexey Bayduraev if (ret) { 126365e7c963SAlexey Bayduraev pr_err("Failed to create data directory: %s\n", strerror(-ret)); 126456f735ffSAlexey Bayduraev return ret; 126565e7c963SAlexey Bayduraev } 126656f735ffSAlexey Bayduraev for (i = 0; i < evlist->core.nr_mmaps; i++) { 126756f735ffSAlexey Bayduraev if (evlist->mmap) 126856f735ffSAlexey Bayduraev evlist->mmap[i].file = &rec->data.dir.files[i]; 126956f735ffSAlexey Bayduraev if (evlist->overwrite_mmap) 127056f735ffSAlexey Bayduraev evlist->overwrite_mmap[i].file = &rec->data.dir.files[i]; 127156f735ffSAlexey Bayduraev } 127256f735ffSAlexey Bayduraev } 127356f735ffSAlexey Bayduraev 1274cda57a8cSWang Nan return 0; 1275cda57a8cSWang Nan } 1276cda57a8cSWang Nan 1277cda57a8cSWang Nan static int record__mmap(struct record *rec) 1278cda57a8cSWang Nan { 1279cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 1280cda57a8cSWang Nan } 1281cda57a8cSWang Nan 12828c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 1283dd7927f4SArnaldo Carvalho de Melo { 1284d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 128532dcd021SJiri Olsa struct evsel *pos; 128663503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 1287d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 1288b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 12898d3eca20SDavid Ahern int rc = 0; 1290dd7927f4SArnaldo Carvalho de Melo 1291d3dbf43cSArnaldo Carvalho de Melo /* 1292b91e5492SKan Liang * For initial_delay, system wide or a hybrid system, we need to add a 1293b91e5492SKan Liang * dummy event so that we can track PERF_RECORD_MMAP to cover the delay 1294b91e5492SKan Liang * of waiting or event synthesis. 1295d3dbf43cSArnaldo Carvalho de Melo */ 1296cb4b9e68SChangbin Du if (opts->target.initial_delay || target__has_cpu(&opts->target) || 1297b91e5492SKan Liang perf_pmu__has_hybrid()) { 1298e80db255SArnaldo Carvalho de Melo pos = evlist__get_tracking_event(evlist); 1299442ad225SAdrian Hunter if (!evsel__is_dummy_event(pos)) { 1300442ad225SAdrian Hunter /* Set up dummy event. */ 1301e251abeeSArnaldo Carvalho de Melo if (evlist__add_dummy(evlist)) 1302d3dbf43cSArnaldo Carvalho de Melo return -ENOMEM; 1303515dbe48SJiri Olsa pos = evlist__last(evlist); 1304e80db255SArnaldo Carvalho de Melo evlist__set_tracking_event(evlist, pos); 1305442ad225SAdrian Hunter } 1306442ad225SAdrian Hunter 13070a892c1cSIan Rogers /* 13080a892c1cSIan Rogers * Enable the dummy event when the process is forked for 13090a892c1cSIan Rogers * initial_delay, immediately for system wide. 13100a892c1cSIan Rogers */ 1311cb4b9e68SChangbin Du if (opts->target.initial_delay && !pos->immediate && 1312bb07d62eSNamhyung Kim !target__has_cpu(&opts->target)) 13131fc632ceSJiri Olsa pos->core.attr.enable_on_exec = 1; 13140a892c1cSIan Rogers else 13150a892c1cSIan Rogers pos->immediate = 1; 1316d3dbf43cSArnaldo Carvalho de Melo } 1317d3dbf43cSArnaldo Carvalho de Melo 131878e1bc25SArnaldo Carvalho de Melo evlist__config(evlist, opts, &callchain_param); 1319cac21425SJiri Olsa 1320e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 13213da297a6SIngo Molnar try_again: 1322af663bd0SJiri Olsa if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) { 1323ae430892SArnaldo Carvalho de Melo if (evsel__fallback(pos, errno, msg, sizeof(msg))) { 1324bb963e16SNamhyung Kim if (verbose > 0) 1325c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 13263da297a6SIngo Molnar goto try_again; 13273da297a6SIngo Molnar } 1328cf99ad14SAndi Kleen if ((errno == EINVAL || errno == EBADF) && 1329fba7c866SJiri Olsa pos->core.leader != &pos->core && 1330cf99ad14SAndi Kleen pos->weak_group) { 133164b4778bSArnaldo Carvalho de Melo pos = evlist__reset_weak_group(evlist, pos, true); 1332cf99ad14SAndi Kleen goto try_again; 1333cf99ad14SAndi Kleen } 133456e52e85SArnaldo Carvalho de Melo rc = -errno; 13352bb72dbbSArnaldo Carvalho de Melo evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg)); 133656e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 13378d3eca20SDavid Ahern goto out; 13387c6a1c65SPeter Zijlstra } 1339bfd8f72cSAndi Kleen 1340bfd8f72cSAndi Kleen pos->supported = true; 13417c6a1c65SPeter Zijlstra } 13427c6a1c65SPeter Zijlstra 134378e1bc25SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) { 1344c8b567c8SArnaldo Carvalho de Melo pr_warning( 1345c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 1346c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n" 1347c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 1348c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 1349c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 1350c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 1351c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 1352c8b567c8SArnaldo Carvalho de Melo } 1353c8b567c8SArnaldo Carvalho de Melo 135424bf91a7SArnaldo Carvalho de Melo if (evlist__apply_filters(evlist, &pos)) { 135562d94b00SArnaldo Carvalho de Melo pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 13568ab2e96dSArnaldo Carvalho de Melo pos->filter, evsel__name(pos), errno, 1357c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 13588d3eca20SDavid Ahern rc = -1; 13598d3eca20SDavid Ahern goto out; 13600a102479SFrederic Weisbecker } 13610a102479SFrederic Weisbecker 1362cda57a8cSWang Nan rc = record__mmap(rec); 1363cda57a8cSWang Nan if (rc) 13648d3eca20SDavid Ahern goto out; 13650a27d7f9SArnaldo Carvalho de Melo 1366a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 13677b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 13688d3eca20SDavid Ahern out: 13698d3eca20SDavid Ahern return rc; 1370a91e5431SArnaldo Carvalho de Melo } 1371a91e5431SArnaldo Carvalho de Melo 137266286ed3SAdrian Hunter static void set_timestamp_boundary(struct record *rec, u64 sample_time) 137366286ed3SAdrian Hunter { 137466286ed3SAdrian Hunter if (rec->evlist->first_sample_time == 0) 137566286ed3SAdrian Hunter rec->evlist->first_sample_time = sample_time; 137666286ed3SAdrian Hunter 137766286ed3SAdrian Hunter if (sample_time) 137866286ed3SAdrian Hunter rec->evlist->last_sample_time = sample_time; 137966286ed3SAdrian Hunter } 138066286ed3SAdrian Hunter 1381e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 1382e3d59112SNamhyung Kim union perf_event *event, 1383e3d59112SNamhyung Kim struct perf_sample *sample, 138432dcd021SJiri Olsa struct evsel *evsel, 1385e3d59112SNamhyung Kim struct machine *machine) 1386e3d59112SNamhyung Kim { 1387e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 1388e3d59112SNamhyung Kim 138966286ed3SAdrian Hunter set_timestamp_boundary(rec, sample->time); 139068588bafSJin Yao 139168588bafSJin Yao if (rec->buildid_all) 139268588bafSJin Yao return 0; 139368588bafSJin Yao 139468588bafSJin Yao rec->samples++; 1395e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 1396e3d59112SNamhyung Kim } 1397e3d59112SNamhyung Kim 13988c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 13996122e4e4SArnaldo Carvalho de Melo { 1400f5fc1412SJiri Olsa struct perf_session *session = rec->session; 14016122e4e4SArnaldo Carvalho de Melo 140245112e89SJiri Olsa if (perf_data__size(&rec->data) == 0) 14039f591fd7SArnaldo Carvalho de Melo return 0; 14049f591fd7SArnaldo Carvalho de Melo 140500dc8657SNamhyung Kim /* 140600dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 140700dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 140800dc8657SNamhyung Kim * we prefer the vmlinux path like 140900dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 141000dc8657SNamhyung Kim * 141100dc8657SNamhyung Kim * rather than build-id path (in debug directory). 141200dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 141300dc8657SNamhyung Kim */ 141400dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 141500dc8657SNamhyung Kim 14166156681bSNamhyung Kim /* 14176156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 141868588bafSJin Yao * so no need to process samples. But if timestamp_boundary is enabled, 141968588bafSJin Yao * it still needs to walk on all samples to get the timestamps of 142068588bafSJin Yao * first/last samples. 14216156681bSNamhyung Kim */ 142268588bafSJin Yao if (rec->buildid_all && !rec->timestamp_boundary) 14236156681bSNamhyung Kim rec->tool.sample = NULL; 14246156681bSNamhyung Kim 1425b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 14266122e4e4SArnaldo Carvalho de Melo } 14276122e4e4SArnaldo Carvalho de Melo 14288115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 1429a1645ce1SZhang, Yanmin { 1430a1645ce1SZhang, Yanmin int err; 143145694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 1432a1645ce1SZhang, Yanmin /* 1433a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 1434a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 1435a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 1436a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 1437a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 1438a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 1439a1645ce1SZhang, Yanmin */ 144045694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 1441743eb868SArnaldo Carvalho de Melo machine); 1442a1645ce1SZhang, Yanmin if (err < 0) 1443a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 144423346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 1445a1645ce1SZhang, Yanmin 1446a1645ce1SZhang, Yanmin /* 1447a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 1448a1645ce1SZhang, Yanmin * have no _text sometimes. 1449a1645ce1SZhang, Yanmin */ 145045694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 14510ae617beSAdrian Hunter machine); 1452a1645ce1SZhang, Yanmin if (err < 0) 1453a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 145423346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 1455a1645ce1SZhang, Yanmin } 1456a1645ce1SZhang, Yanmin 145798402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 145898402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 145998402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 146098402807SFrederic Weisbecker }; 146198402807SFrederic Weisbecker 14623812d298SAdrian Hunter static struct perf_event_header finished_init_event = { 14633812d298SAdrian Hunter .size = sizeof(struct perf_event_header), 14643812d298SAdrian Hunter .type = PERF_RECORD_FINISHED_INIT, 14653812d298SAdrian Hunter }; 14663812d298SAdrian Hunter 1467a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map) 1468f13de660SAlexey Budankov { 1469f13de660SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS && 1470396b626bSAlexey Bayduraev !bitmap_equal(thread->mask->affinity.bits, map->affinity_mask.bits, 1471396b626bSAlexey Bayduraev thread->mask->affinity.nbits)) { 1472396b626bSAlexey Bayduraev bitmap_zero(thread->mask->affinity.bits, thread->mask->affinity.nbits); 1473396b626bSAlexey Bayduraev bitmap_or(thread->mask->affinity.bits, thread->mask->affinity.bits, 1474396b626bSAlexey Bayduraev map->affinity_mask.bits, thread->mask->affinity.nbits); 1475396b626bSAlexey Bayduraev sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity), 1476396b626bSAlexey Bayduraev (cpu_set_t *)thread->mask->affinity.bits); 1477396b626bSAlexey Bayduraev if (verbose == 2) { 1478396b626bSAlexey Bayduraev pr_debug("threads[%d]: running on cpu%d: ", thread->tid, sched_getcpu()); 1479396b626bSAlexey Bayduraev mmap_cpu_mask__scnprintf(&thread->mask->affinity, "affinity"); 1480396b626bSAlexey Bayduraev } 1481f13de660SAlexey Budankov } 1482f13de660SAlexey Budankov } 1483f13de660SAlexey Budankov 14845d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment) 14855d7f4116SAlexey Budankov { 148672932371SJiri Olsa struct perf_record_compressed *event = record; 14875d7f4116SAlexey Budankov size_t size = sizeof(*event); 14885d7f4116SAlexey Budankov 14895d7f4116SAlexey Budankov if (increment) { 14905d7f4116SAlexey Budankov event->header.size += increment; 14915d7f4116SAlexey Budankov return increment; 14925d7f4116SAlexey Budankov } 14935d7f4116SAlexey Budankov 14945d7f4116SAlexey Budankov event->header.type = PERF_RECORD_COMPRESSED; 14955d7f4116SAlexey Budankov event->header.size = size; 14965d7f4116SAlexey Budankov 14975d7f4116SAlexey Budankov return size; 14985d7f4116SAlexey Budankov } 14995d7f4116SAlexey Budankov 150075f5f1fcSAlexey Bayduraev static size_t zstd_compress(struct perf_session *session, struct mmap *map, 150175f5f1fcSAlexey Bayduraev void *dst, size_t dst_size, void *src, size_t src_size) 15025d7f4116SAlexey Budankov { 15035d7f4116SAlexey Budankov size_t compressed; 150472932371SJiri Olsa size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1; 150575f5f1fcSAlexey Bayduraev struct zstd_data *zstd_data = &session->zstd_data; 15065d7f4116SAlexey Budankov 150775f5f1fcSAlexey Bayduraev if (map && map->file) 150875f5f1fcSAlexey Bayduraev zstd_data = &map->zstd_data; 150975f5f1fcSAlexey Bayduraev 151075f5f1fcSAlexey Bayduraev compressed = zstd_compress_stream_to_records(zstd_data, dst, dst_size, src, src_size, 15115d7f4116SAlexey Budankov max_record_size, process_comp_header); 15125d7f4116SAlexey Budankov 1513610fbc01SAlexey Bayduraev if (map && map->file) { 1514610fbc01SAlexey Bayduraev thread->bytes_transferred += src_size; 1515610fbc01SAlexey Bayduraev thread->bytes_compressed += compressed; 1516610fbc01SAlexey Bayduraev } else { 15175d7f4116SAlexey Budankov session->bytes_transferred += src_size; 15185d7f4116SAlexey Budankov session->bytes_compressed += compressed; 1519610fbc01SAlexey Bayduraev } 15205d7f4116SAlexey Budankov 15215d7f4116SAlexey Budankov return compressed; 15225d7f4116SAlexey Budankov } 15235d7f4116SAlexey Budankov 152463503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist, 1525470530bbSAlexey Budankov bool overwrite, bool synch) 152698402807SFrederic Weisbecker { 1527dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 15280e2e63ddSPeter Zijlstra int i; 15298d3eca20SDavid Ahern int rc = 0; 1530396b626bSAlexey Bayduraev int nr_mmaps; 1531396b626bSAlexey Bayduraev struct mmap **maps; 1532d3d1af6fSAlexey Budankov int trace_fd = rec->data.file.fd; 1533ef781128SAlexey Budankov off_t off = 0; 153498402807SFrederic Weisbecker 1535cb21686bSWang Nan if (!evlist) 1536cb21686bSWang Nan return 0; 1537ef149c25SAdrian Hunter 1538396b626bSAlexey Bayduraev nr_mmaps = thread->nr_mmaps; 1539396b626bSAlexey Bayduraev maps = overwrite ? thread->overwrite_maps : thread->maps; 1540396b626bSAlexey Bayduraev 1541a4ea0ec4SWang Nan if (!maps) 1542a4ea0ec4SWang Nan return 0; 1543cb21686bSWang Nan 15440b72d69aSWang Nan if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 154554cc54deSWang Nan return 0; 154654cc54deSWang Nan 1547d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1548d3d1af6fSAlexey Budankov off = record__aio_get_pos(trace_fd); 1549d3d1af6fSAlexey Budankov 1550396b626bSAlexey Bayduraev for (i = 0; i < nr_mmaps; i++) { 1551470530bbSAlexey Budankov u64 flush = 0; 1552396b626bSAlexey Bayduraev struct mmap *map = maps[i]; 1553a4ea0ec4SWang Nan 1554547740f7SJiri Olsa if (map->core.base) { 1555f13de660SAlexey Budankov record__adjust_affinity(rec, map); 1556470530bbSAlexey Budankov if (synch) { 155765aa2e6bSJiri Olsa flush = map->core.flush; 155865aa2e6bSJiri Olsa map->core.flush = 1; 1559470530bbSAlexey Budankov } 1560d3d1af6fSAlexey Budankov if (!record__aio_enabled(rec)) { 1561ef781128SAlexey Budankov if (perf_mmap__push(map, rec, record__pushfn) < 0) { 1562470530bbSAlexey Budankov if (synch) 156365aa2e6bSJiri Olsa map->core.flush = flush; 15648d3eca20SDavid Ahern rc = -1; 15658d3eca20SDavid Ahern goto out; 15668d3eca20SDavid Ahern } 1567d3d1af6fSAlexey Budankov } else { 1568ef781128SAlexey Budankov if (record__aio_push(rec, map, &off) < 0) { 1569d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1570470530bbSAlexey Budankov if (synch) 157165aa2e6bSJiri Olsa map->core.flush = flush; 1572d3d1af6fSAlexey Budankov rc = -1; 1573d3d1af6fSAlexey Budankov goto out; 1574d3d1af6fSAlexey Budankov } 1575d3d1af6fSAlexey Budankov } 1576470530bbSAlexey Budankov if (synch) 157765aa2e6bSJiri Olsa map->core.flush = flush; 15788d3eca20SDavid Ahern } 1579ef149c25SAdrian Hunter 1580e035f4caSJiri Olsa if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode && 1581c0a6de06SAdrian Hunter !rec->opts.auxtrace_sample_mode && 1582e035f4caSJiri Olsa record__auxtrace_mmap_read(rec, map) != 0) { 1583ef149c25SAdrian Hunter rc = -1; 1584ef149c25SAdrian Hunter goto out; 1585ef149c25SAdrian Hunter } 158698402807SFrederic Weisbecker } 158798402807SFrederic Weisbecker 1588d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1589d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1590d3d1af6fSAlexey Budankov 1591dcabb507SJiri Olsa /* 1592dcabb507SJiri Olsa * Mark the round finished in case we wrote 1593dcabb507SJiri Olsa * at least one event. 159456f735ffSAlexey Bayduraev * 159556f735ffSAlexey Bayduraev * No need for round events in directory mode, 159656f735ffSAlexey Bayduraev * because per-cpu maps and files have data 159756f735ffSAlexey Bayduraev * sorted by kernel. 1598dcabb507SJiri Olsa */ 159956f735ffSAlexey Bayduraev if (!record__threads_enabled(rec) && bytes_written != rec->bytes_written) 1600ded2b8feSJiri Olsa rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event)); 16018d3eca20SDavid Ahern 16020b72d69aSWang Nan if (overwrite) 1603ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 16048d3eca20SDavid Ahern out: 16058d3eca20SDavid Ahern return rc; 160698402807SFrederic Weisbecker } 160798402807SFrederic Weisbecker 1608470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch) 1609cb21686bSWang Nan { 1610cb21686bSWang Nan int err; 1611cb21686bSWang Nan 1612470530bbSAlexey Budankov err = record__mmap_read_evlist(rec, rec->evlist, false, synch); 1613cb21686bSWang Nan if (err) 1614cb21686bSWang Nan return err; 1615cb21686bSWang Nan 1616470530bbSAlexey Budankov return record__mmap_read_evlist(rec, rec->evlist, true, synch); 1617cb21686bSWang Nan } 1618cb21686bSWang Nan 1619396b626bSAlexey Bayduraev static void record__thread_munmap_filtered(struct fdarray *fda, int fd, 1620396b626bSAlexey Bayduraev void *arg __maybe_unused) 1621396b626bSAlexey Bayduraev { 1622396b626bSAlexey Bayduraev struct perf_mmap *map = fda->priv[fd].ptr; 1623396b626bSAlexey Bayduraev 1624396b626bSAlexey Bayduraev if (map) 1625396b626bSAlexey Bayduraev perf_mmap__put(map); 1626396b626bSAlexey Bayduraev } 1627396b626bSAlexey Bayduraev 16283217e9feSAlexey Bayduraev static void *record__thread(void *arg) 16293217e9feSAlexey Bayduraev { 16303217e9feSAlexey Bayduraev enum thread_msg msg = THREAD_MSG__READY; 16313217e9feSAlexey Bayduraev bool terminate = false; 16323217e9feSAlexey Bayduraev struct fdarray *pollfd; 16333217e9feSAlexey Bayduraev int err, ctlfd_pos; 16343217e9feSAlexey Bayduraev 16353217e9feSAlexey Bayduraev thread = arg; 16363217e9feSAlexey Bayduraev thread->tid = gettid(); 16373217e9feSAlexey Bayduraev 16383217e9feSAlexey Bayduraev err = write(thread->pipes.ack[1], &msg, sizeof(msg)); 16393217e9feSAlexey Bayduraev if (err == -1) 16403217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to notify on start: %s\n", 16413217e9feSAlexey Bayduraev thread->tid, strerror(errno)); 16423217e9feSAlexey Bayduraev 16433217e9feSAlexey Bayduraev pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu()); 16443217e9feSAlexey Bayduraev 16453217e9feSAlexey Bayduraev pollfd = &thread->pollfd; 16463217e9feSAlexey Bayduraev ctlfd_pos = thread->ctlfd_pos; 16473217e9feSAlexey Bayduraev 16483217e9feSAlexey Bayduraev for (;;) { 16493217e9feSAlexey Bayduraev unsigned long long hits = thread->samples; 16503217e9feSAlexey Bayduraev 16513217e9feSAlexey Bayduraev if (record__mmap_read_all(thread->rec, false) < 0 || terminate) 16523217e9feSAlexey Bayduraev break; 16533217e9feSAlexey Bayduraev 16543217e9feSAlexey Bayduraev if (hits == thread->samples) { 16553217e9feSAlexey Bayduraev 16563217e9feSAlexey Bayduraev err = fdarray__poll(pollfd, -1); 16573217e9feSAlexey Bayduraev /* 16583217e9feSAlexey Bayduraev * Propagate error, only if there's any. Ignore positive 16593217e9feSAlexey Bayduraev * number of returned events and interrupt error. 16603217e9feSAlexey Bayduraev */ 16613217e9feSAlexey Bayduraev if (err > 0 || (err < 0 && errno == EINTR)) 16623217e9feSAlexey Bayduraev err = 0; 16633217e9feSAlexey Bayduraev thread->waking++; 16643217e9feSAlexey Bayduraev 16653217e9feSAlexey Bayduraev if (fdarray__filter(pollfd, POLLERR | POLLHUP, 16663217e9feSAlexey Bayduraev record__thread_munmap_filtered, NULL) == 0) 16673217e9feSAlexey Bayduraev break; 16683217e9feSAlexey Bayduraev } 16693217e9feSAlexey Bayduraev 16703217e9feSAlexey Bayduraev if (pollfd->entries[ctlfd_pos].revents & POLLHUP) { 16713217e9feSAlexey Bayduraev terminate = true; 16723217e9feSAlexey Bayduraev close(thread->pipes.msg[0]); 16733217e9feSAlexey Bayduraev thread->pipes.msg[0] = -1; 16743217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].fd = -1; 16753217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].events = 0; 16763217e9feSAlexey Bayduraev } 16773217e9feSAlexey Bayduraev 16783217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].revents = 0; 16793217e9feSAlexey Bayduraev } 16803217e9feSAlexey Bayduraev record__mmap_read_all(thread->rec, true); 16813217e9feSAlexey Bayduraev 16823217e9feSAlexey Bayduraev err = write(thread->pipes.ack[1], &msg, sizeof(msg)); 16833217e9feSAlexey Bayduraev if (err == -1) 16843217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to notify on termination: %s\n", 16853217e9feSAlexey Bayduraev thread->tid, strerror(errno)); 16863217e9feSAlexey Bayduraev 16873217e9feSAlexey Bayduraev return NULL; 16883217e9feSAlexey Bayduraev } 16893217e9feSAlexey Bayduraev 16908c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 169157706abcSDavid Ahern { 169257706abcSDavid Ahern struct perf_session *session = rec->session; 169357706abcSDavid Ahern int feat; 169457706abcSDavid Ahern 169557706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 169657706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 169757706abcSDavid Ahern 169857706abcSDavid Ahern if (rec->no_buildid) 169957706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 170057706abcSDavid Ahern 1701378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 1702ce9036a6SJiri Olsa if (!have_tracepoints(&rec->evlist->core.entries)) 170357706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 1704378ef0f5SIan Rogers #endif 170557706abcSDavid Ahern 170657706abcSDavid Ahern if (!rec->opts.branch_stack) 170757706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 1708ef149c25SAdrian Hunter 1709ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 1710ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 1711ffa517adSJiri Olsa 1712cf790516SAlexey Budankov if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns)) 1713cf790516SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_CLOCKID); 1714cf790516SAlexey Budankov 1715d1e325cfSJiri Olsa if (!rec->opts.use_clockid) 1716d1e325cfSJiri Olsa perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA); 1717d1e325cfSJiri Olsa 171856f735ffSAlexey Bayduraev if (!record__threads_enabled(rec)) 1719258031c0SJiri Olsa perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT); 172056f735ffSAlexey Bayduraev 172142e1fd80SAlexey Budankov if (!record__comp_enabled(rec)) 172242e1fd80SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_COMPRESSED); 1723258031c0SJiri Olsa 1724ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 172557706abcSDavid Ahern } 172657706abcSDavid Ahern 1727e1ab48baSWang Nan static void 1728e1ab48baSWang Nan record__finish_output(struct record *rec) 1729e1ab48baSWang Nan { 173056f735ffSAlexey Bayduraev int i; 17318ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 17328ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1733e1ab48baSWang Nan 17348ceb41d7SJiri Olsa if (data->is_pipe) 1735e1ab48baSWang Nan return; 1736e1ab48baSWang Nan 1737e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 173845112e89SJiri Olsa data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR); 173956f735ffSAlexey Bayduraev if (record__threads_enabled(rec)) { 174056f735ffSAlexey Bayduraev for (i = 0; i < data->dir.nr; i++) 174156f735ffSAlexey Bayduraev data->dir.files[i].size = lseek(data->dir.files[i].fd, 0, SEEK_CUR); 174256f735ffSAlexey Bayduraev } 1743e1ab48baSWang Nan 1744e1ab48baSWang Nan if (!rec->no_buildid) { 1745e1ab48baSWang Nan process_buildids(rec); 1746e1ab48baSWang Nan 1747e1ab48baSWang Nan if (rec->buildid_all) 1748e1ab48baSWang Nan dsos__hit_all(rec->session); 1749e1ab48baSWang Nan } 1750e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 1751e1ab48baSWang Nan 1752e1ab48baSWang Nan return; 1753e1ab48baSWang Nan } 1754e1ab48baSWang Nan 17554ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 1756be7b0c9eSWang Nan { 17579d6aae72SArnaldo Carvalho de Melo int err; 17589749b90eSJiri Olsa struct perf_thread_map *thread_map; 175941b740b6SNamhyung Kim bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP; 1760be7b0c9eSWang Nan 17614ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 17624ea648aeSWang Nan return 0; 17634ea648aeSWang Nan 17649d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 17659d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 17669d6aae72SArnaldo Carvalho de Melo return -1; 17679d6aae72SArnaldo Carvalho de Melo 17689d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 1769be7b0c9eSWang Nan process_synthesized_event, 1770be7b0c9eSWang Nan &rec->session->machines.host, 177141b740b6SNamhyung Kim needs_mmap, 17723fcb10e4SMark Drayton rec->opts.sample_address); 17737836e52eSJiri Olsa perf_thread_map__put(thread_map); 17749d6aae72SArnaldo Carvalho de Melo return err; 1775be7b0c9eSWang Nan } 1776be7b0c9eSWang Nan 17773812d298SAdrian Hunter static int write_finished_init(struct record *rec, bool tail) 17783812d298SAdrian Hunter { 17793812d298SAdrian Hunter if (rec->opts.tail_synthesize != tail) 17803812d298SAdrian Hunter return 0; 17813812d298SAdrian Hunter 17823812d298SAdrian Hunter return record__write(rec, NULL, &finished_init_event, sizeof(finished_init_event)); 17833812d298SAdrian Hunter } 17843812d298SAdrian Hunter 17854ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 17863c1cb7e3SWang Nan 1787ecfd7a9cSWang Nan static int 1788ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 1789ecfd7a9cSWang Nan { 17908ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1791ecfd7a9cSWang Nan int fd, err; 179203724b2eSAndi Kleen char *new_filename; 1793ecfd7a9cSWang Nan 1794ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 1795ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 1796ecfd7a9cSWang Nan 1797d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1798d3d1af6fSAlexey Budankov 17993812d298SAdrian Hunter write_finished_init(rec, true); 18003812d298SAdrian Hunter 18014ea648aeSWang Nan record__synthesize(rec, true); 18024ea648aeSWang Nan if (target__none(&rec->opts.target)) 18034ea648aeSWang Nan record__synthesize_workload(rec, true); 18044ea648aeSWang Nan 1805ecfd7a9cSWang Nan rec->samples = 0; 1806ecfd7a9cSWang Nan record__finish_output(rec); 1807ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 1808ecfd7a9cSWang Nan if (err) { 1809ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 1810ecfd7a9cSWang Nan return -EINVAL; 1811ecfd7a9cSWang Nan } 1812ecfd7a9cSWang Nan 18138ceb41d7SJiri Olsa fd = perf_data__switch(data, timestamp, 1814ecfd7a9cSWang Nan rec->session->header.data_offset, 181503724b2eSAndi Kleen at_exit, &new_filename); 1816ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 1817ecfd7a9cSWang Nan rec->bytes_written = 0; 1818ecfd7a9cSWang Nan rec->session->header.data_size = 0; 1819ecfd7a9cSWang Nan } 1820ecfd7a9cSWang Nan 1821ecfd7a9cSWang Nan if (!quiet) 1822ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 18232d4f2799SJiri Olsa data->path, timestamp); 18243c1cb7e3SWang Nan 182503724b2eSAndi Kleen if (rec->switch_output.num_files) { 182603724b2eSAndi Kleen int n = rec->switch_output.cur_file + 1; 182703724b2eSAndi Kleen 182803724b2eSAndi Kleen if (n >= rec->switch_output.num_files) 182903724b2eSAndi Kleen n = 0; 183003724b2eSAndi Kleen rec->switch_output.cur_file = n; 183103724b2eSAndi Kleen if (rec->switch_output.filenames[n]) { 183203724b2eSAndi Kleen remove(rec->switch_output.filenames[n]); 1833d8f9da24SArnaldo Carvalho de Melo zfree(&rec->switch_output.filenames[n]); 183403724b2eSAndi Kleen } 183503724b2eSAndi Kleen rec->switch_output.filenames[n] = new_filename; 183603724b2eSAndi Kleen } else { 183703724b2eSAndi Kleen free(new_filename); 183803724b2eSAndi Kleen } 183903724b2eSAndi Kleen 18403c1cb7e3SWang Nan /* Output tracking events */ 1841be7b0c9eSWang Nan if (!at_exit) { 18424ea648aeSWang Nan record__synthesize(rec, false); 18433c1cb7e3SWang Nan 1844be7b0c9eSWang Nan /* 1845be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 1846be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 1847be7b0c9eSWang Nan * generate tracking events because there's no thread_map 1848be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 1849be7b0c9eSWang Nan * contain map and comm information. 1850be7b0c9eSWang Nan * Create a fake thread_map and directly call 1851be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 1852be7b0c9eSWang Nan */ 1853be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 18544ea648aeSWang Nan record__synthesize_workload(rec, false); 18553812d298SAdrian Hunter write_finished_init(rec, false); 1856be7b0c9eSWang Nan } 1857ecfd7a9cSWang Nan return fd; 1858ecfd7a9cSWang Nan } 1859ecfd7a9cSWang Nan 1860*27c6f245SNamhyung Kim static void __record__save_lost_samples(struct record *rec, struct evsel *evsel, 1861e3a23261SNamhyung Kim struct perf_record_lost_samples *lost, 1862*27c6f245SNamhyung Kim int cpu_idx, int thread_idx, u64 lost_count, 1863*27c6f245SNamhyung Kim u16 misc_flag) 1864e3a23261SNamhyung Kim { 1865e3a23261SNamhyung Kim struct perf_sample_id *sid; 1866e3a23261SNamhyung Kim struct perf_sample sample = {}; 1867e3a23261SNamhyung Kim int id_hdr_size; 1868e3a23261SNamhyung Kim 1869*27c6f245SNamhyung Kim lost->lost = lost_count; 1870e3a23261SNamhyung Kim if (evsel->core.ids) { 1871e3a23261SNamhyung Kim sid = xyarray__entry(evsel->core.sample_id, cpu_idx, thread_idx); 1872e3a23261SNamhyung Kim sample.id = sid->id; 1873e3a23261SNamhyung Kim } 1874e3a23261SNamhyung Kim 1875e3a23261SNamhyung Kim id_hdr_size = perf_event__synthesize_id_sample((void *)(lost + 1), 1876e3a23261SNamhyung Kim evsel->core.attr.sample_type, &sample); 1877e3a23261SNamhyung Kim lost->header.size = sizeof(*lost) + id_hdr_size; 1878*27c6f245SNamhyung Kim lost->header.misc = misc_flag; 1879e3a23261SNamhyung Kim record__write(rec, NULL, lost, lost->header.size); 1880e3a23261SNamhyung Kim } 1881e3a23261SNamhyung Kim 1882e3a23261SNamhyung Kim static void record__read_lost_samples(struct record *rec) 1883e3a23261SNamhyung Kim { 1884e3a23261SNamhyung Kim struct perf_session *session = rec->session; 1885e3a23261SNamhyung Kim struct perf_record_lost_samples *lost; 1886e3a23261SNamhyung Kim struct evsel *evsel; 1887e3a23261SNamhyung Kim 1888d031a00aSNamhyung Kim /* there was an error during record__open */ 1889d031a00aSNamhyung Kim if (session->evlist == NULL) 1890d031a00aSNamhyung Kim return; 1891d031a00aSNamhyung Kim 1892e3a23261SNamhyung Kim lost = zalloc(PERF_SAMPLE_MAX_SIZE); 1893e3a23261SNamhyung Kim if (lost == NULL) { 1894e3a23261SNamhyung Kim pr_debug("Memory allocation failed\n"); 1895e3a23261SNamhyung Kim return; 1896e3a23261SNamhyung Kim } 1897e3a23261SNamhyung Kim 1898e3a23261SNamhyung Kim lost->header.type = PERF_RECORD_LOST_SAMPLES; 1899e3a23261SNamhyung Kim 1900e3a23261SNamhyung Kim evlist__for_each_entry(session->evlist, evsel) { 1901e3a23261SNamhyung Kim struct xyarray *xy = evsel->core.sample_id; 1902*27c6f245SNamhyung Kim u64 lost_count; 1903e3a23261SNamhyung Kim 1904d031a00aSNamhyung Kim if (xy == NULL || evsel->core.fd == NULL) 1905d031a00aSNamhyung Kim continue; 1906e3a23261SNamhyung Kim if (xyarray__max_x(evsel->core.fd) != xyarray__max_x(xy) || 1907e3a23261SNamhyung Kim xyarray__max_y(evsel->core.fd) != xyarray__max_y(xy)) { 1908e3a23261SNamhyung Kim pr_debug("Unmatched FD vs. sample ID: skip reading LOST count\n"); 1909e3a23261SNamhyung Kim continue; 1910e3a23261SNamhyung Kim } 1911e3a23261SNamhyung Kim 1912e3a23261SNamhyung Kim for (int x = 0; x < xyarray__max_x(xy); x++) { 1913e3a23261SNamhyung Kim for (int y = 0; y < xyarray__max_y(xy); y++) { 1914*27c6f245SNamhyung Kim struct perf_counts_values count; 1915e3a23261SNamhyung Kim 1916*27c6f245SNamhyung Kim if (perf_evsel__read(&evsel->core, x, y, &count) < 0) { 1917*27c6f245SNamhyung Kim pr_debug("read LOST count failed\n"); 1918*27c6f245SNamhyung Kim goto out; 1919*27c6f245SNamhyung Kim } 1920*27c6f245SNamhyung Kim 1921*27c6f245SNamhyung Kim if (count.lost) { 1922*27c6f245SNamhyung Kim __record__save_lost_samples(rec, evsel, lost, 1923*27c6f245SNamhyung Kim x, y, count.lost, 0); 1924*27c6f245SNamhyung Kim } 1925*27c6f245SNamhyung Kim } 1926*27c6f245SNamhyung Kim } 1927*27c6f245SNamhyung Kim 1928*27c6f245SNamhyung Kim lost_count = perf_bpf_filter__lost_count(evsel); 1929*27c6f245SNamhyung Kim if (lost_count) 1930*27c6f245SNamhyung Kim __record__save_lost_samples(rec, evsel, lost, 0, 0, lost_count, 1931*27c6f245SNamhyung Kim PERF_RECORD_MISC_LOST_SAMPLES_BPF); 1932*27c6f245SNamhyung Kim } 1933*27c6f245SNamhyung Kim out: 1934*27c6f245SNamhyung Kim free(lost); 1935e3a23261SNamhyung Kim } 1936e3a23261SNamhyung Kim 19378ed28c2bSIan Rogers static volatile sig_atomic_t workload_exec_errno; 1938f33cbe72SArnaldo Carvalho de Melo 1939f33cbe72SArnaldo Carvalho de Melo /* 19407b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload will send a SIGUSR1 1941f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 1942f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 1943f33cbe72SArnaldo Carvalho de Melo */ 194445604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 194545604710SNamhyung Kim siginfo_t *info, 1946f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 1947f33cbe72SArnaldo Carvalho de Melo { 1948f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 1949f33cbe72SArnaldo Carvalho de Melo done = 1; 1950f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 1951f33cbe72SArnaldo Carvalho de Melo } 1952f33cbe72SArnaldo Carvalho de Melo 19532dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 1954bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 19552dd6d8a1SAdrian Hunter 1956db0ea13cSArnaldo Carvalho de Melo static const struct perf_event_mmap_page *evlist__pick_pc(struct evlist *evlist) 1957ee667f94SWang Nan { 1958b2cb615dSWang Nan if (evlist) { 1959547740f7SJiri Olsa if (evlist->mmap && evlist->mmap[0].core.base) 1960547740f7SJiri Olsa return evlist->mmap[0].core.base; 1961547740f7SJiri Olsa if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base) 1962547740f7SJiri Olsa return evlist->overwrite_mmap[0].core.base; 1963b2cb615dSWang Nan } 1964ee667f94SWang Nan return NULL; 1965ee667f94SWang Nan } 1966ee667f94SWang Nan 1967c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 1968c45628b0SWang Nan { 1969db0ea13cSArnaldo Carvalho de Melo const struct perf_event_mmap_page *pc = evlist__pick_pc(rec->evlist); 1970ee667f94SWang Nan if (pc) 1971ee667f94SWang Nan return pc; 1972c45628b0SWang Nan return NULL; 1973c45628b0SWang Nan } 1974c45628b0SWang Nan 19754ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 1976c45c86ebSWang Nan { 1977c45c86ebSWang Nan struct perf_session *session = rec->session; 1978c45c86ebSWang Nan struct machine *machine = &session->machines.host; 19798ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1980c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 1981c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 1982c45c86ebSWang Nan int err = 0; 1983d99c22eaSStephane Eranian event_op f = process_synthesized_event; 1984c45c86ebSWang Nan 19854ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 19864ea648aeSWang Nan return 0; 19874ea648aeSWang Nan 19888ceb41d7SJiri Olsa if (data->is_pipe) { 1989c3a057dcSNamhyung Kim err = perf_event__synthesize_for_pipe(tool, session, data, 1990c45c86ebSWang Nan process_synthesized_event); 1991c3a057dcSNamhyung Kim if (err < 0) 1992c45c86ebSWang Nan goto out; 1993c45c86ebSWang Nan 1994c45c86ebSWang Nan rec->bytes_written += err; 1995c45c86ebSWang Nan } 1996c45c86ebSWang Nan 1997c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 199846bc29b9SAdrian Hunter process_synthesized_event, machine); 199946bc29b9SAdrian Hunter if (err) 200046bc29b9SAdrian Hunter goto out; 200146bc29b9SAdrian Hunter 2002c0a6de06SAdrian Hunter /* Synthesize id_index before auxtrace_info */ 2003c0a6de06SAdrian Hunter err = perf_event__synthesize_id_index(tool, 2004c0a6de06SAdrian Hunter process_synthesized_event, 2005c0a6de06SAdrian Hunter session->evlist, machine); 2006c0a6de06SAdrian Hunter if (err) 2007c0a6de06SAdrian Hunter goto out; 2008c0a6de06SAdrian Hunter 2009c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 2010c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 2011c45c86ebSWang Nan session, process_synthesized_event); 2012c45c86ebSWang Nan if (err) 2013c45c86ebSWang Nan goto out; 2014c45c86ebSWang Nan } 2015c45c86ebSWang Nan 201678e1bc25SArnaldo Carvalho de Melo if (!evlist__exclude_kernel(rec->evlist)) { 2017c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 2018c45c86ebSWang Nan machine); 2019c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 2020c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 2021c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 2022c45c86ebSWang Nan 2023c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 2024c45c86ebSWang Nan machine); 2025c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 2026c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 2027c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 20286c443954SArnaldo Carvalho de Melo } 2029c45c86ebSWang Nan 2030c45c86ebSWang Nan if (perf_guest) { 2031c45c86ebSWang Nan machines__process_guests(&session->machines, 2032c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 2033c45c86ebSWang Nan } 2034c45c86ebSWang Nan 2035bfd8f72cSAndi Kleen err = perf_event__synthesize_extra_attr(&rec->tool, 2036bfd8f72cSAndi Kleen rec->evlist, 2037bfd8f72cSAndi Kleen process_synthesized_event, 2038bfd8f72cSAndi Kleen data->is_pipe); 2039bfd8f72cSAndi Kleen if (err) 2040bfd8f72cSAndi Kleen goto out; 2041bfd8f72cSAndi Kleen 204203617c22SJiri Olsa err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads, 2043373565d2SAndi Kleen process_synthesized_event, 2044373565d2SAndi Kleen NULL); 2045373565d2SAndi Kleen if (err < 0) { 2046373565d2SAndi Kleen pr_err("Couldn't synthesize thread map.\n"); 2047373565d2SAndi Kleen return err; 2048373565d2SAndi Kleen } 2049373565d2SAndi Kleen 20507be1feddSAdrian Hunter err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.all_cpus, 2051373565d2SAndi Kleen process_synthesized_event, NULL); 2052373565d2SAndi Kleen if (err < 0) { 2053373565d2SAndi Kleen pr_err("Couldn't synthesize cpu map.\n"); 2054373565d2SAndi Kleen return err; 2055373565d2SAndi Kleen } 2056373565d2SAndi Kleen 2057e5416950SSong Liu err = perf_event__synthesize_bpf_events(session, process_synthesized_event, 20587b612e29SSong Liu machine, opts); 2059faf59ec8SAdrian Hunter if (err < 0) { 20607b612e29SSong Liu pr_warning("Couldn't synthesize bpf events.\n"); 2061faf59ec8SAdrian Hunter err = 0; 2062faf59ec8SAdrian Hunter } 20637b612e29SSong Liu 206441b740b6SNamhyung Kim if (rec->opts.synth & PERF_SYNTH_CGROUP) { 2065ab64069fSNamhyung Kim err = perf_event__synthesize_cgroups(tool, process_synthesized_event, 2066ab64069fSNamhyung Kim machine); 2067faf59ec8SAdrian Hunter if (err < 0) { 2068ab64069fSNamhyung Kim pr_warning("Couldn't synthesize cgroup events.\n"); 2069faf59ec8SAdrian Hunter err = 0; 2070faf59ec8SAdrian Hunter } 207141b740b6SNamhyung Kim } 2072ab64069fSNamhyung Kim 2073d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) { 207449c670b1SIan Rogers mutex_init(&synth_lock); 2075d99c22eaSStephane Eranian perf_set_multithreaded(); 2076d99c22eaSStephane Eranian f = process_locked_synthesized_event; 2077d99c22eaSStephane Eranian } 2078d99c22eaSStephane Eranian 207941b740b6SNamhyung Kim if (rec->opts.synth & PERF_SYNTH_TASK) { 208041b740b6SNamhyung Kim bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP; 208141b740b6SNamhyung Kim 208284111b9cSNamhyung Kim err = __machine__synthesize_threads(machine, tool, &opts->target, 208384111b9cSNamhyung Kim rec->evlist->core.threads, 208441b740b6SNamhyung Kim f, needs_mmap, opts->sample_address, 2085d99c22eaSStephane Eranian rec->opts.nr_threads_synthesize); 208641b740b6SNamhyung Kim } 2087d99c22eaSStephane Eranian 208849c670b1SIan Rogers if (rec->opts.nr_threads_synthesize > 1) { 2089d99c22eaSStephane Eranian perf_set_singlethreaded(); 209049c670b1SIan Rogers mutex_destroy(&synth_lock); 209149c670b1SIan Rogers } 2092d99c22eaSStephane Eranian 2093c45c86ebSWang Nan out: 2094c45c86ebSWang Nan return err; 2095c45c86ebSWang Nan } 2096c45c86ebSWang Nan 2097899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data) 2098899e5ffbSArnaldo Carvalho de Melo { 2099899e5ffbSArnaldo Carvalho de Melo struct record *rec = data; 2100899e5ffbSArnaldo Carvalho de Melo pthread_kill(rec->thread_id, SIGUSR2); 2101899e5ffbSArnaldo Carvalho de Melo return 0; 2102899e5ffbSArnaldo Carvalho de Melo } 2103899e5ffbSArnaldo Carvalho de Melo 210423cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec) 210523cbb41cSArnaldo Carvalho de Melo { 210623cbb41cSArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 210723cbb41cSArnaldo Carvalho de Melo 210823cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist != NULL) { 210923cbb41cSArnaldo Carvalho de Melo /* 211023cbb41cSArnaldo Carvalho de Melo * We get here if --switch-output-event populated the 211123cbb41cSArnaldo Carvalho de Melo * sb_evlist, so associate a callback that will send a SIGUSR2 211223cbb41cSArnaldo Carvalho de Melo * to the main thread. 211323cbb41cSArnaldo Carvalho de Melo */ 211423cbb41cSArnaldo Carvalho de Melo evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec); 211523cbb41cSArnaldo Carvalho de Melo rec->thread_id = pthread_self(); 211623cbb41cSArnaldo Carvalho de Melo } 21171101c872SJin Yao #ifdef HAVE_LIBBPF_SUPPORT 211823cbb41cSArnaldo Carvalho de Melo if (!opts->no_bpf_event) { 211923cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 212023cbb41cSArnaldo Carvalho de Melo rec->sb_evlist = evlist__new(); 212123cbb41cSArnaldo Carvalho de Melo 212223cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 212323cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't create side band evlist.\n."); 212423cbb41cSArnaldo Carvalho de Melo return -1; 212523cbb41cSArnaldo Carvalho de Melo } 212623cbb41cSArnaldo Carvalho de Melo } 212723cbb41cSArnaldo Carvalho de Melo 212823cbb41cSArnaldo Carvalho de Melo if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) { 212923cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n."); 213023cbb41cSArnaldo Carvalho de Melo return -1; 213123cbb41cSArnaldo Carvalho de Melo } 213223cbb41cSArnaldo Carvalho de Melo } 21331101c872SJin Yao #endif 213408c83997SArnaldo Carvalho de Melo if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) { 213523cbb41cSArnaldo Carvalho de Melo pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n"); 213623cbb41cSArnaldo Carvalho de Melo opts->no_bpf_event = true; 213723cbb41cSArnaldo Carvalho de Melo } 213823cbb41cSArnaldo Carvalho de Melo 213923cbb41cSArnaldo Carvalho de Melo return 0; 214023cbb41cSArnaldo Carvalho de Melo } 214123cbb41cSArnaldo Carvalho de Melo 2142d1e325cfSJiri Olsa static int record__init_clock(struct record *rec) 2143d1e325cfSJiri Olsa { 2144d1e325cfSJiri Olsa struct perf_session *session = rec->session; 2145d1e325cfSJiri Olsa struct timespec ref_clockid; 2146d1e325cfSJiri Olsa struct timeval ref_tod; 2147d1e325cfSJiri Olsa u64 ref; 2148d1e325cfSJiri Olsa 2149d1e325cfSJiri Olsa if (!rec->opts.use_clockid) 2150d1e325cfSJiri Olsa return 0; 2151d1e325cfSJiri Olsa 21529d88a1a1SJiri Olsa if (rec->opts.use_clockid && rec->opts.clockid_res_ns) 21539d88a1a1SJiri Olsa session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns; 21549d88a1a1SJiri Olsa 2155d1e325cfSJiri Olsa session->header.env.clock.clockid = rec->opts.clockid; 2156d1e325cfSJiri Olsa 2157d1e325cfSJiri Olsa if (gettimeofday(&ref_tod, NULL) != 0) { 2158d1e325cfSJiri Olsa pr_err("gettimeofday failed, cannot set reference time.\n"); 2159d1e325cfSJiri Olsa return -1; 2160d1e325cfSJiri Olsa } 2161d1e325cfSJiri Olsa 2162d1e325cfSJiri Olsa if (clock_gettime(rec->opts.clockid, &ref_clockid)) { 2163d1e325cfSJiri Olsa pr_err("clock_gettime failed, cannot set reference time.\n"); 2164d1e325cfSJiri Olsa return -1; 2165d1e325cfSJiri Olsa } 2166d1e325cfSJiri Olsa 2167d1e325cfSJiri Olsa ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC + 2168d1e325cfSJiri Olsa (u64) ref_tod.tv_usec * NSEC_PER_USEC; 2169d1e325cfSJiri Olsa 2170d1e325cfSJiri Olsa session->header.env.clock.tod_ns = ref; 2171d1e325cfSJiri Olsa 2172d1e325cfSJiri Olsa ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC + 2173d1e325cfSJiri Olsa (u64) ref_clockid.tv_nsec; 2174d1e325cfSJiri Olsa 2175d1e325cfSJiri Olsa session->header.env.clock.clockid_ns = ref; 2176d1e325cfSJiri Olsa return 0; 2177d1e325cfSJiri Olsa } 2178d1e325cfSJiri Olsa 2179d20aff15SAdrian Hunter static void hit_auxtrace_snapshot_trigger(struct record *rec) 2180d20aff15SAdrian Hunter { 2181d20aff15SAdrian Hunter if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 2182d20aff15SAdrian Hunter trigger_hit(&auxtrace_snapshot_trigger); 2183d20aff15SAdrian Hunter auxtrace_record__snapshot_started = 1; 2184d20aff15SAdrian Hunter if (auxtrace_record__snapshot_start(rec->itr)) 2185d20aff15SAdrian Hunter trigger_error(&auxtrace_snapshot_trigger); 2186d20aff15SAdrian Hunter } 2187d20aff15SAdrian Hunter } 2188d20aff15SAdrian Hunter 218991c0f5ecSJin Yao static void record__uniquify_name(struct record *rec) 219091c0f5ecSJin Yao { 219191c0f5ecSJin Yao struct evsel *pos; 219291c0f5ecSJin Yao struct evlist *evlist = rec->evlist; 219391c0f5ecSJin Yao char *new_name; 219491c0f5ecSJin Yao int ret; 219591c0f5ecSJin Yao 219691c0f5ecSJin Yao if (!perf_pmu__has_hybrid()) 219791c0f5ecSJin Yao return; 219891c0f5ecSJin Yao 219991c0f5ecSJin Yao evlist__for_each_entry(evlist, pos) { 220091c0f5ecSJin Yao if (!evsel__is_hybrid(pos)) 220191c0f5ecSJin Yao continue; 220291c0f5ecSJin Yao 220391c0f5ecSJin Yao if (strchr(pos->name, '/')) 220491c0f5ecSJin Yao continue; 220591c0f5ecSJin Yao 220691c0f5ecSJin Yao ret = asprintf(&new_name, "%s/%s/", 220791c0f5ecSJin Yao pos->pmu_name, pos->name); 220891c0f5ecSJin Yao if (ret) { 220991c0f5ecSJin Yao free(pos->name); 221091c0f5ecSJin Yao pos->name = new_name; 221191c0f5ecSJin Yao } 221291c0f5ecSJin Yao } 221391c0f5ecSJin Yao } 221491c0f5ecSJin Yao 22151e5de7d9SAlexey Bayduraev static int record__terminate_thread(struct record_thread *thread_data) 22161e5de7d9SAlexey Bayduraev { 22171e5de7d9SAlexey Bayduraev int err; 22181e5de7d9SAlexey Bayduraev enum thread_msg ack = THREAD_MSG__UNDEFINED; 22191e5de7d9SAlexey Bayduraev pid_t tid = thread_data->tid; 22201e5de7d9SAlexey Bayduraev 22211e5de7d9SAlexey Bayduraev close(thread_data->pipes.msg[1]); 22221e5de7d9SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 22231e5de7d9SAlexey Bayduraev err = read(thread_data->pipes.ack[0], &ack, sizeof(ack)); 22241e5de7d9SAlexey Bayduraev if (err > 0) 22251e5de7d9SAlexey Bayduraev pr_debug2("threads[%d]: sent %s\n", tid, thread_msg_tags[ack]); 22261e5de7d9SAlexey Bayduraev else 22271e5de7d9SAlexey Bayduraev pr_warning("threads[%d]: failed to receive termination notification from %d\n", 22281e5de7d9SAlexey Bayduraev thread->tid, tid); 22291e5de7d9SAlexey Bayduraev 22301e5de7d9SAlexey Bayduraev return 0; 22311e5de7d9SAlexey Bayduraev } 22321e5de7d9SAlexey Bayduraev 2233396b626bSAlexey Bayduraev static int record__start_threads(struct record *rec) 2234396b626bSAlexey Bayduraev { 22353217e9feSAlexey Bayduraev int t, tt, err, ret = 0, nr_threads = rec->nr_threads; 2236396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 22373217e9feSAlexey Bayduraev sigset_t full, mask; 22383217e9feSAlexey Bayduraev pthread_t handle; 22393217e9feSAlexey Bayduraev pthread_attr_t attrs; 2240396b626bSAlexey Bayduraev 2241396b626bSAlexey Bayduraev thread = &thread_data[0]; 2242396b626bSAlexey Bayduraev 22433217e9feSAlexey Bayduraev if (!record__threads_enabled(rec)) 22443217e9feSAlexey Bayduraev return 0; 22453217e9feSAlexey Bayduraev 22463217e9feSAlexey Bayduraev sigfillset(&full); 22473217e9feSAlexey Bayduraev if (sigprocmask(SIG_SETMASK, &full, &mask)) { 22483217e9feSAlexey Bayduraev pr_err("Failed to block signals on threads start: %s\n", strerror(errno)); 22493217e9feSAlexey Bayduraev return -1; 22503217e9feSAlexey Bayduraev } 22513217e9feSAlexey Bayduraev 22523217e9feSAlexey Bayduraev pthread_attr_init(&attrs); 22533217e9feSAlexey Bayduraev pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED); 22543217e9feSAlexey Bayduraev 22553217e9feSAlexey Bayduraev for (t = 1; t < nr_threads; t++) { 22563217e9feSAlexey Bayduraev enum thread_msg msg = THREAD_MSG__UNDEFINED; 22573217e9feSAlexey Bayduraev 22583217e9feSAlexey Bayduraev #ifdef HAVE_PTHREAD_ATTR_SETAFFINITY_NP 22593217e9feSAlexey Bayduraev pthread_attr_setaffinity_np(&attrs, 22603217e9feSAlexey Bayduraev MMAP_CPU_MASK_BYTES(&(thread_data[t].mask->affinity)), 22613217e9feSAlexey Bayduraev (cpu_set_t *)(thread_data[t].mask->affinity.bits)); 22623217e9feSAlexey Bayduraev #endif 22633217e9feSAlexey Bayduraev if (pthread_create(&handle, &attrs, record__thread, &thread_data[t])) { 22643217e9feSAlexey Bayduraev for (tt = 1; tt < t; tt++) 22653217e9feSAlexey Bayduraev record__terminate_thread(&thread_data[t]); 22663217e9feSAlexey Bayduraev pr_err("Failed to start threads: %s\n", strerror(errno)); 22673217e9feSAlexey Bayduraev ret = -1; 22683217e9feSAlexey Bayduraev goto out_err; 22693217e9feSAlexey Bayduraev } 22703217e9feSAlexey Bayduraev 22713217e9feSAlexey Bayduraev err = read(thread_data[t].pipes.ack[0], &msg, sizeof(msg)); 22723217e9feSAlexey Bayduraev if (err > 0) 22733217e9feSAlexey Bayduraev pr_debug2("threads[%d]: sent %s\n", rec->thread_data[t].tid, 22743217e9feSAlexey Bayduraev thread_msg_tags[msg]); 22753217e9feSAlexey Bayduraev else 22763217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to receive start notification from %d\n", 22773217e9feSAlexey Bayduraev thread->tid, rec->thread_data[t].tid); 22783217e9feSAlexey Bayduraev } 22793217e9feSAlexey Bayduraev 22803217e9feSAlexey Bayduraev sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity), 22813217e9feSAlexey Bayduraev (cpu_set_t *)thread->mask->affinity.bits); 22823217e9feSAlexey Bayduraev 2283396b626bSAlexey Bayduraev pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu()); 2284396b626bSAlexey Bayduraev 22853217e9feSAlexey Bayduraev out_err: 22863217e9feSAlexey Bayduraev pthread_attr_destroy(&attrs); 22873217e9feSAlexey Bayduraev 22883217e9feSAlexey Bayduraev if (sigprocmask(SIG_SETMASK, &mask, NULL)) { 22893217e9feSAlexey Bayduraev pr_err("Failed to unblock signals on threads start: %s\n", strerror(errno)); 22903217e9feSAlexey Bayduraev ret = -1; 22913217e9feSAlexey Bayduraev } 22923217e9feSAlexey Bayduraev 22933217e9feSAlexey Bayduraev return ret; 2294396b626bSAlexey Bayduraev } 2295396b626bSAlexey Bayduraev 2296396b626bSAlexey Bayduraev static int record__stop_threads(struct record *rec) 2297396b626bSAlexey Bayduraev { 2298396b626bSAlexey Bayduraev int t; 2299396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 2300396b626bSAlexey Bayduraev 23011e5de7d9SAlexey Bayduraev for (t = 1; t < rec->nr_threads; t++) 23021e5de7d9SAlexey Bayduraev record__terminate_thread(&thread_data[t]); 23031e5de7d9SAlexey Bayduraev 2304610fbc01SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 2305396b626bSAlexey Bayduraev rec->samples += thread_data[t].samples; 2306610fbc01SAlexey Bayduraev if (!record__threads_enabled(rec)) 2307610fbc01SAlexey Bayduraev continue; 2308610fbc01SAlexey Bayduraev rec->session->bytes_transferred += thread_data[t].bytes_transferred; 2309610fbc01SAlexey Bayduraev rec->session->bytes_compressed += thread_data[t].bytes_compressed; 2310610fbc01SAlexey Bayduraev pr_debug("threads[%d]: samples=%lld, wakes=%ld, ", thread_data[t].tid, 2311610fbc01SAlexey Bayduraev thread_data[t].samples, thread_data[t].waking); 2312610fbc01SAlexey Bayduraev if (thread_data[t].bytes_transferred && thread_data[t].bytes_compressed) 2313610fbc01SAlexey Bayduraev pr_debug("transferred=%" PRIu64 ", compressed=%" PRIu64 "\n", 2314610fbc01SAlexey Bayduraev thread_data[t].bytes_transferred, thread_data[t].bytes_compressed); 2315610fbc01SAlexey Bayduraev else 2316610fbc01SAlexey Bayduraev pr_debug("written=%" PRIu64 "\n", thread_data[t].bytes_written); 2317610fbc01SAlexey Bayduraev } 2318396b626bSAlexey Bayduraev 2319396b626bSAlexey Bayduraev return 0; 2320396b626bSAlexey Bayduraev } 2321396b626bSAlexey Bayduraev 2322396b626bSAlexey Bayduraev static unsigned long record__waking(struct record *rec) 2323396b626bSAlexey Bayduraev { 2324396b626bSAlexey Bayduraev int t; 2325396b626bSAlexey Bayduraev unsigned long waking = 0; 2326396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 2327396b626bSAlexey Bayduraev 2328396b626bSAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 2329396b626bSAlexey Bayduraev waking += thread_data[t].waking; 2330396b626bSAlexey Bayduraev 2331396b626bSAlexey Bayduraev return waking; 2332396b626bSAlexey Bayduraev } 2333396b626bSAlexey Bayduraev 23348c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 233586470930SIngo Molnar { 233657706abcSDavid Ahern int err; 233745604710SNamhyung Kim int status = 0; 233846be604bSZhang, Yanmin const bool forks = argc > 0; 233945694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 2340b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 23418ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 2342d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 23436dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 234442aa276fSNamhyung Kim int fd; 2345d3c8c08eSAlexey Budankov float ratio = 0; 2346acce0223SAlexey Budankov enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED; 234786470930SIngo Molnar 234845604710SNamhyung Kim atexit(record__sig_exit); 2349f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 2350f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 2351804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 2352a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 2353c0bdc1c4SWang Nan 2354f3b3614aSHari Bathini if (rec->opts.record_namespaces) 2355f3b3614aSHari Bathini tool->namespace_events = true; 2356f3b3614aSHari Bathini 23578fb4b679SNamhyung Kim if (rec->opts.record_cgroup) { 23588fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE 23598fb4b679SNamhyung Kim tool->cgroup_events = true; 23608fb4b679SNamhyung Kim #else 23618fb4b679SNamhyung Kim pr_err("cgroup tracking is not supported\n"); 23628fb4b679SNamhyung Kim return -1; 23638fb4b679SNamhyung Kim #endif 23648fb4b679SNamhyung Kim } 23658fb4b679SNamhyung Kim 2366dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 23672dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 23683c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 23695f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 2370dc0c6127SJiri Olsa if (rec->switch_output.enabled) 23713c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 2372c0bdc1c4SWang Nan } else { 23732dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 2374c0bdc1c4SWang Nan } 2375f5970550SPeter Zijlstra 23762681bd85SNamhyung Kim session = perf_session__new(data, tool); 23776ef81c55SMamatha Inamdar if (IS_ERR(session)) { 2378ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 23796ef81c55SMamatha Inamdar return PTR_ERR(session); 2380a9a70bbcSArnaldo Carvalho de Melo } 2381a9a70bbcSArnaldo Carvalho de Melo 2382b5f2511dSAlexey Bayduraev if (record__threads_enabled(rec)) { 2383b5f2511dSAlexey Bayduraev if (perf_data__is_pipe(&rec->data)) { 2384b5f2511dSAlexey Bayduraev pr_err("Parallel trace streaming is not available in pipe mode.\n"); 2385b5f2511dSAlexey Bayduraev return -1; 2386b5f2511dSAlexey Bayduraev } 2387b5f2511dSAlexey Bayduraev if (rec->opts.full_auxtrace) { 2388b5f2511dSAlexey Bayduraev pr_err("Parallel trace streaming is not available in AUX area tracing mode.\n"); 2389b5f2511dSAlexey Bayduraev return -1; 2390b5f2511dSAlexey Bayduraev } 2391b5f2511dSAlexey Bayduraev } 2392b5f2511dSAlexey Bayduraev 23938ceb41d7SJiri Olsa fd = perf_data__fd(data); 2394d20deb64SArnaldo Carvalho de Melo rec->session = session; 2395d20deb64SArnaldo Carvalho de Melo 23965d7f4116SAlexey Budankov if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) { 23975d7f4116SAlexey Budankov pr_err("Compression initialization failed.\n"); 23985d7f4116SAlexey Budankov return -1; 23995d7f4116SAlexey Budankov } 2400da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 2401da231338SAnand K Mistry done_fd = eventfd(0, EFD_NONBLOCK); 2402da231338SAnand K Mistry if (done_fd < 0) { 2403da231338SAnand K Mistry pr_err("Failed to create wakeup eventfd, error: %m\n"); 2404da231338SAnand K Mistry status = -1; 2405da231338SAnand K Mistry goto out_delete_session; 2406da231338SAnand K Mistry } 2407e16c2ce7SYang Jihong err = evlist__add_wakeup_eventfd(rec->evlist, done_fd); 2408da231338SAnand K Mistry if (err < 0) { 2409da231338SAnand K Mistry pr_err("Failed to add wakeup eventfd to poll list\n"); 2410da231338SAnand K Mistry status = err; 2411da231338SAnand K Mistry goto out_delete_session; 2412da231338SAnand K Mistry } 2413da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT 24145d7f4116SAlexey Budankov 24155d7f4116SAlexey Budankov session->header.env.comp_type = PERF_COMP_ZSTD; 24165d7f4116SAlexey Budankov session->header.env.comp_level = rec->opts.comp_level; 24175d7f4116SAlexey Budankov 2418eeb399b5SAdrian Hunter if (rec->opts.kcore && 2419eeb399b5SAdrian Hunter !record__kcore_readable(&session->machines.host)) { 2420eeb399b5SAdrian Hunter pr_err("ERROR: kcore is not readable.\n"); 2421eeb399b5SAdrian Hunter return -1; 2422eeb399b5SAdrian Hunter } 2423eeb399b5SAdrian Hunter 2424d1e325cfSJiri Olsa if (record__init_clock(rec)) 2425d1e325cfSJiri Olsa return -1; 2426d1e325cfSJiri Olsa 24278c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 2428330aa675SStephane Eranian 2429d4db3f16SArnaldo Carvalho de Melo if (forks) { 24307b392ef0SArnaldo Carvalho de Melo err = evlist__prepare_workload(rec->evlist, &opts->target, argv, data->is_pipe, 2431735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 243235b9d88eSArnaldo Carvalho de Melo if (err < 0) { 243335b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 243445604710SNamhyung Kim status = err; 243535b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 2436856e9660SPeter Zijlstra } 2437856e9660SPeter Zijlstra } 2438856e9660SPeter Zijlstra 2439ad46e48cSJiri Olsa /* 2440ad46e48cSJiri Olsa * If we have just single event and are sending data 2441ad46e48cSJiri Olsa * through pipe, we need to force the ids allocation, 2442ad46e48cSJiri Olsa * because we synthesize event name through the pipe 2443ad46e48cSJiri Olsa * and need the id for that. 2444ad46e48cSJiri Olsa */ 24456484d2f9SJiri Olsa if (data->is_pipe && rec->evlist->core.nr_entries == 1) 2446ad46e48cSJiri Olsa rec->opts.sample_id = true; 2447ad46e48cSJiri Olsa 244891c0f5ecSJin Yao record__uniquify_name(rec); 244991c0f5ecSJin Yao 2450da406202SAdrian Hunter /* Debug message used by test scripts */ 2451da406202SAdrian Hunter pr_debug3("perf record opening and mmapping events\n"); 24528c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 24538d3eca20SDavid Ahern err = -1; 2454396b626bSAlexey Bayduraev goto out_free_threads; 24558d3eca20SDavid Ahern } 2456da406202SAdrian Hunter /* Debug message used by test scripts */ 2457da406202SAdrian Hunter pr_debug3("perf record done opening and mmapping events\n"); 2458f6fa4375SJiri Olsa session->header.env.comp_mmap_len = session->evlist->core.mmap_len; 245986470930SIngo Molnar 2460eeb399b5SAdrian Hunter if (rec->opts.kcore) { 2461eeb399b5SAdrian Hunter err = record__kcore_copy(&session->machines.host, data); 2462eeb399b5SAdrian Hunter if (err) { 2463eeb399b5SAdrian Hunter pr_err("ERROR: Failed to copy kcore\n"); 2464396b626bSAlexey Bayduraev goto out_free_threads; 2465eeb399b5SAdrian Hunter } 2466eeb399b5SAdrian Hunter } 2467eeb399b5SAdrian Hunter 24688690a2a7SWang Nan err = bpf__apply_obj_config(); 24698690a2a7SWang Nan if (err) { 24708690a2a7SWang Nan char errbuf[BUFSIZ]; 24718690a2a7SWang Nan 24728690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 24738690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 24748690a2a7SWang Nan errbuf); 2475396b626bSAlexey Bayduraev goto out_free_threads; 24768690a2a7SWang Nan } 24778690a2a7SWang Nan 2478cca8482cSAdrian Hunter /* 2479cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 2480cca8482cSAdrian Hunter * evlist. 2481cca8482cSAdrian Hunter */ 24828cedf3a5SArnaldo Carvalho de Melo if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) { 2483cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 2484cca8482cSAdrian Hunter rec->tool.ordered_events = false; 2485cca8482cSAdrian Hunter } 2486cca8482cSAdrian Hunter 24879d2dc632SIan Rogers if (evlist__nr_groups(rec->evlist) == 0) 2488a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 2489a8bb559bSNamhyung Kim 24908ceb41d7SJiri Olsa if (data->is_pipe) { 249142aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 2492529870e3STom Zanussi if (err < 0) 2493396b626bSAlexey Bayduraev goto out_free_threads; 2494563aecb2SJiri Olsa } else { 249542aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 2496d5eed904SArnaldo Carvalho de Melo if (err < 0) 2497396b626bSAlexey Bayduraev goto out_free_threads; 2498d5eed904SArnaldo Carvalho de Melo } 24997c6a1c65SPeter Zijlstra 2500b38d85efSArnaldo Carvalho de Melo err = -1; 2501d3665498SDavid Ahern if (!rec->no_buildid 2502e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 2503d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 2504e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 2505396b626bSAlexey Bayduraev goto out_free_threads; 2506e20960c0SRobert Richter } 2507e20960c0SRobert Richter 250823cbb41cSArnaldo Carvalho de Melo err = record__setup_sb_evlist(rec); 250923cbb41cSArnaldo Carvalho de Melo if (err) 2510396b626bSAlexey Bayduraev goto out_free_threads; 2511657ee553SSong Liu 25124ea648aeSWang Nan err = record__synthesize(rec, false); 2513c45c86ebSWang Nan if (err < 0) 2514396b626bSAlexey Bayduraev goto out_free_threads; 25158d3eca20SDavid Ahern 2516d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 251786470930SIngo Molnar struct sched_param param; 251886470930SIngo Molnar 2519d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 252086470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 25216beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 25228d3eca20SDavid Ahern err = -1; 2523396b626bSAlexey Bayduraev goto out_free_threads; 252486470930SIngo Molnar } 252586470930SIngo Molnar } 252686470930SIngo Molnar 2527396b626bSAlexey Bayduraev if (record__start_threads(rec)) 2528396b626bSAlexey Bayduraev goto out_free_threads; 2529396b626bSAlexey Bayduraev 2530774cb499SJiri Olsa /* 2531774cb499SJiri Olsa * When perf is starting the traced process, all the events 2532774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 2533774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 2534774cb499SJiri Olsa */ 2535cb4b9e68SChangbin Du if (!target__none(&opts->target) && !opts->target.initial_delay) 25361c87f165SJiri Olsa evlist__enable(rec->evlist); 2537764e16a3SDavid Ahern 2538856e9660SPeter Zijlstra /* 2539856e9660SPeter Zijlstra * Let the child rip 2540856e9660SPeter Zijlstra */ 2541e803cf97SNamhyung Kim if (forks) { 254220a8a3cfSJiri Olsa struct machine *machine = &session->machines.host; 2543e5bed564SNamhyung Kim union perf_event *event; 2544e907caf3SHari Bathini pid_t tgid; 2545e5bed564SNamhyung Kim 2546e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 2547e5bed564SNamhyung Kim if (event == NULL) { 2548e5bed564SNamhyung Kim err = -ENOMEM; 2549e5bed564SNamhyung Kim goto out_child; 2550e5bed564SNamhyung Kim } 2551e5bed564SNamhyung Kim 2552e803cf97SNamhyung Kim /* 2553e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 2554e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 2555e803cf97SNamhyung Kim * cannot see a correct process name for those events. 2556e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 2557e803cf97SNamhyung Kim */ 2558e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 2559e803cf97SNamhyung Kim rec->evlist->workload.pid, 2560e803cf97SNamhyung Kim process_synthesized_event, 2561e803cf97SNamhyung Kim machine); 2562e5bed564SNamhyung Kim free(event); 2563e803cf97SNamhyung Kim 2564e907caf3SHari Bathini if (tgid == -1) 2565e907caf3SHari Bathini goto out_child; 2566e907caf3SHari Bathini 2567e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 2568e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 2569e907caf3SHari Bathini machine->id_hdr_size); 2570e907caf3SHari Bathini if (event == NULL) { 2571e907caf3SHari Bathini err = -ENOMEM; 2572e907caf3SHari Bathini goto out_child; 2573e907caf3SHari Bathini } 2574e907caf3SHari Bathini 2575e907caf3SHari Bathini /* 2576e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 2577e907caf3SHari Bathini */ 2578e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 2579e907caf3SHari Bathini rec->evlist->workload.pid, 2580e907caf3SHari Bathini tgid, process_synthesized_event, 2581e907caf3SHari Bathini machine); 2582e907caf3SHari Bathini free(event); 2583e907caf3SHari Bathini 25847b392ef0SArnaldo Carvalho de Melo evlist__start_workload(rec->evlist); 2585e803cf97SNamhyung Kim } 2586856e9660SPeter Zijlstra 2587cb4b9e68SChangbin Du if (opts->target.initial_delay) { 258868cd3b45SAlexey Budankov pr_info(EVLIST_DISABLED_MSG); 2589cb4b9e68SChangbin Du if (opts->target.initial_delay > 0) { 2590cb4b9e68SChangbin Du usleep(opts->target.initial_delay * USEC_PER_MSEC); 25911c87f165SJiri Olsa evlist__enable(rec->evlist); 259268cd3b45SAlexey Budankov pr_info(EVLIST_ENABLED_MSG); 259368cd3b45SAlexey Budankov } 25946619a53eSAndi Kleen } 25956619a53eSAndi Kleen 25966657a099SAdrian Hunter err = event_enable_timer__start(rec->evlist->eet); 25976657a099SAdrian Hunter if (err) 25986657a099SAdrian Hunter goto out_child; 25996657a099SAdrian Hunter 2600da406202SAdrian Hunter /* Debug message used by test scripts */ 2601da406202SAdrian Hunter pr_debug3("perf record has started\n"); 2602da406202SAdrian Hunter fflush(stderr); 2603da406202SAdrian Hunter 26045f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 26053c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 2606a074865eSWang Nan perf_hooks__invoke_record_start(); 26073812d298SAdrian Hunter 26083812d298SAdrian Hunter /* 26093812d298SAdrian Hunter * Must write FINISHED_INIT so it will be seen after all other 26103812d298SAdrian Hunter * synthesized user events, but before any regular events. 26113812d298SAdrian Hunter */ 26123812d298SAdrian Hunter err = write_finished_init(rec, false); 26133812d298SAdrian Hunter if (err < 0) 26143812d298SAdrian Hunter goto out_child; 26153812d298SAdrian Hunter 2616649c48a9SPeter Zijlstra for (;;) { 2617396b626bSAlexey Bayduraev unsigned long long hits = thread->samples; 261886470930SIngo Molnar 261905737464SWang Nan /* 262005737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 262105737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 262205737464SWang Nan * hits != rec->samples in previous round. 262305737464SWang Nan * 2624ade9d208SArnaldo Carvalho de Melo * evlist__toggle_bkw_mmap ensure we never 262505737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 262605737464SWang Nan */ 262705737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 2628ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 262905737464SWang Nan 2630470530bbSAlexey Budankov if (record__mmap_read_all(rec, false) < 0) { 26315f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 26323c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 26338d3eca20SDavid Ahern err = -1; 263445604710SNamhyung Kim goto out_child; 26358d3eca20SDavid Ahern } 263686470930SIngo Molnar 26372dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 26382dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 26395f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 2640ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, false); 26415f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 26422dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 26432dd6d8a1SAdrian Hunter err = -1; 26442dd6d8a1SAdrian Hunter goto out_child; 26452dd6d8a1SAdrian Hunter } 26462dd6d8a1SAdrian Hunter } 26472dd6d8a1SAdrian Hunter 26483c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 264905737464SWang Nan /* 265005737464SWang Nan * If switch_output_trigger is hit, the data in 265105737464SWang Nan * overwritable ring buffer should have been collected, 265205737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 265305737464SWang Nan * 265405737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 265505737464SWang Nan * record__mmap_read_all() didn't collect data from 265605737464SWang Nan * overwritable ring buffer. Read again. 265705737464SWang Nan */ 265805737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 265905737464SWang Nan continue; 26603c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 26613c1cb7e3SWang Nan 266205737464SWang Nan /* 266305737464SWang Nan * Reenable events in overwrite ring buffer after 266405737464SWang Nan * record__mmap_read_all(): we should have collected 266505737464SWang Nan * data from it. 266605737464SWang Nan */ 2667ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 266805737464SWang Nan 26693c1cb7e3SWang Nan if (!quiet) 26703c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 2671396b626bSAlexey Bayduraev record__waking(rec)); 2672396b626bSAlexey Bayduraev thread->waking = 0; 26733c1cb7e3SWang Nan fd = record__switch_output(rec, false); 26743c1cb7e3SWang Nan if (fd < 0) { 26753c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 26763c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 26773c1cb7e3SWang Nan err = fd; 26783c1cb7e3SWang Nan goto out_child; 26793c1cb7e3SWang Nan } 2680bfacbe3bSJiri Olsa 2681bfacbe3bSJiri Olsa /* re-arm the alarm */ 2682bfacbe3bSJiri Olsa if (rec->switch_output.time) 2683bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 26843c1cb7e3SWang Nan } 26853c1cb7e3SWang Nan 2686396b626bSAlexey Bayduraev if (hits == thread->samples) { 26876dcf45efSArnaldo Carvalho de Melo if (done || draining) 2688649c48a9SPeter Zijlstra break; 2689396b626bSAlexey Bayduraev err = fdarray__poll(&thread->pollfd, -1); 2690a515114fSJiri Olsa /* 2691a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 2692a515114fSJiri Olsa * number of returned events and interrupt error. 2693a515114fSJiri Olsa */ 2694a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 269545604710SNamhyung Kim err = 0; 2696396b626bSAlexey Bayduraev thread->waking++; 26976dcf45efSArnaldo Carvalho de Melo 2698396b626bSAlexey Bayduraev if (fdarray__filter(&thread->pollfd, POLLERR | POLLHUP, 2699396b626bSAlexey Bayduraev record__thread_munmap_filtered, NULL) == 0) 27006dcf45efSArnaldo Carvalho de Melo draining = true; 2701396b626bSAlexey Bayduraev 27026562c9acSAdrian Hunter err = record__update_evlist_pollfd_from_thread(rec, rec->evlist, thread); 27036562c9acSAdrian Hunter if (err) 27046562c9acSAdrian Hunter goto out_child; 27058b412664SPeter Zijlstra } 27068b412664SPeter Zijlstra 2707acce0223SAlexey Budankov if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) { 2708acce0223SAlexey Budankov switch (cmd) { 2709d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 2710d20aff15SAdrian Hunter hit_auxtrace_snapshot_trigger(rec); 2711d20aff15SAdrian Hunter evlist__ctlfd_ack(rec->evlist); 2712d20aff15SAdrian Hunter break; 2713f186cd61SJiri Olsa case EVLIST_CTL_CMD_STOP: 2714f186cd61SJiri Olsa done = 1; 2715f186cd61SJiri Olsa break; 2716acce0223SAlexey Budankov case EVLIST_CTL_CMD_ACK: 2717acce0223SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 2718991ae4ebSJiri Olsa case EVLIST_CTL_CMD_ENABLE: 2719991ae4ebSJiri Olsa case EVLIST_CTL_CMD_DISABLE: 2720142544a9SJiri Olsa case EVLIST_CTL_CMD_EVLIST: 272147fddcb4SJiri Olsa case EVLIST_CTL_CMD_PING: 2722acce0223SAlexey Budankov default: 2723acce0223SAlexey Budankov break; 2724acce0223SAlexey Budankov } 2725acce0223SAlexey Budankov } 2726acce0223SAlexey Budankov 27276657a099SAdrian Hunter err = event_enable_timer__process(rec->evlist->eet); 27286657a099SAdrian Hunter if (err < 0) 27296657a099SAdrian Hunter goto out_child; 27306657a099SAdrian Hunter if (err) { 27316657a099SAdrian Hunter err = 0; 27326657a099SAdrian Hunter done = 1; 27336657a099SAdrian Hunter } 27346657a099SAdrian Hunter 2735774cb499SJiri Olsa /* 2736774cb499SJiri Olsa * When perf is starting the traced process, at the end events 2737774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 2738774cb499SJiri Olsa * disable events in this case. 2739774cb499SJiri Olsa */ 2740602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 27415f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 2742e74676deSJiri Olsa evlist__disable(rec->evlist); 27432711926aSJiri Olsa disabled = true; 27442711926aSJiri Olsa } 27458b412664SPeter Zijlstra } 2746ce7b0e42SAlexander Shishkin 27475f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 27483c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 27498b412664SPeter Zijlstra 2750ce7b0e42SAlexander Shishkin if (opts->auxtrace_snapshot_on_exit) 2751ce7b0e42SAlexander Shishkin record__auxtrace_snapshot_exit(rec); 2752ce7b0e42SAlexander Shishkin 2753f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 27543535a696SArnaldo Carvalho de Melo char msg[STRERR_BUFSIZE], strevsels[2048]; 2755c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 27563535a696SArnaldo Carvalho de Melo 27573535a696SArnaldo Carvalho de Melo evlist__scnprintf_evsels(rec->evlist, sizeof(strevsels), strevsels); 27583535a696SArnaldo Carvalho de Melo 27593535a696SArnaldo Carvalho de Melo pr_err("Failed to collect '%s' for the '%s' workload: %s\n", 27603535a696SArnaldo Carvalho de Melo strevsels, argv[0], emsg); 2761f33cbe72SArnaldo Carvalho de Melo err = -1; 276245604710SNamhyung Kim goto out_child; 2763f33cbe72SArnaldo Carvalho de Melo } 2764f33cbe72SArnaldo Carvalho de Melo 2765e3d59112SNamhyung Kim if (!quiet) 2766396b626bSAlexey Bayduraev fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", 2767396b626bSAlexey Bayduraev record__waking(rec)); 276886470930SIngo Molnar 27693812d298SAdrian Hunter write_finished_init(rec, true); 27703812d298SAdrian Hunter 27714ea648aeSWang Nan if (target__none(&rec->opts.target)) 27724ea648aeSWang Nan record__synthesize_workload(rec, true); 27734ea648aeSWang Nan 277445604710SNamhyung Kim out_child: 2775396b626bSAlexey Bayduraev record__stop_threads(rec); 2776470530bbSAlexey Budankov record__mmap_read_all(rec, true); 2777396b626bSAlexey Bayduraev out_free_threads: 2778415ccb58SAlexey Bayduraev record__free_thread_data(rec); 2779396b626bSAlexey Bayduraev evlist__finalize_ctlfd(rec->evlist); 2780d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 2781d3d1af6fSAlexey Budankov 2782d3c8c08eSAlexey Budankov if (rec->session->bytes_transferred && rec->session->bytes_compressed) { 2783d3c8c08eSAlexey Budankov ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed; 2784d3c8c08eSAlexey Budankov session->header.env.comp_ratio = ratio + 0.5; 2785d3c8c08eSAlexey Budankov } 2786d3c8c08eSAlexey Budankov 278745604710SNamhyung Kim if (forks) { 278845604710SNamhyung Kim int exit_status; 278945604710SNamhyung Kim 279045604710SNamhyung Kim if (!child_finished) 279145604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 279245604710SNamhyung Kim 279345604710SNamhyung Kim wait(&exit_status); 279445604710SNamhyung Kim 279545604710SNamhyung Kim if (err < 0) 279645604710SNamhyung Kim status = err; 279745604710SNamhyung Kim else if (WIFEXITED(exit_status)) 279845604710SNamhyung Kim status = WEXITSTATUS(exit_status); 279945604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 280045604710SNamhyung Kim signr = WTERMSIG(exit_status); 280145604710SNamhyung Kim } else 280245604710SNamhyung Kim status = err; 280345604710SNamhyung Kim 2804edc41a10SNamhyung Kim if (rec->off_cpu) 2805edc41a10SNamhyung Kim rec->bytes_written += off_cpu_write(rec->session); 2806edc41a10SNamhyung Kim 2807e3a23261SNamhyung Kim record__read_lost_samples(rec); 28084ea648aeSWang Nan record__synthesize(rec, true); 2809e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 2810e3d59112SNamhyung Kim rec->samples = 0; 2811e3d59112SNamhyung Kim 2812ecfd7a9cSWang Nan if (!err) { 2813ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 2814e1ab48baSWang Nan record__finish_output(rec); 2815ecfd7a9cSWang Nan } else { 2816ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 2817ecfd7a9cSWang Nan if (fd < 0) { 2818ecfd7a9cSWang Nan status = fd; 2819ecfd7a9cSWang Nan goto out_delete_session; 2820ecfd7a9cSWang Nan } 2821ecfd7a9cSWang Nan } 2822ecfd7a9cSWang Nan } 282339d17dacSArnaldo Carvalho de Melo 2824a074865eSWang Nan perf_hooks__invoke_record_end(); 2825a074865eSWang Nan 2826e3d59112SNamhyung Kim if (!err && !quiet) { 2827e3d59112SNamhyung Kim char samples[128]; 2828ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 2829ecfd7a9cSWang Nan ".<timestamp>" : ""; 2830e3d59112SNamhyung Kim 2831ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 2832e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 2833e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 2834e3d59112SNamhyung Kim else 2835e3d59112SNamhyung Kim samples[0] = '\0'; 2836e3d59112SNamhyung Kim 2837d3c8c08eSAlexey Budankov fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s", 28388ceb41d7SJiri Olsa perf_data__size(data) / 1024.0 / 1024.0, 28392d4f2799SJiri Olsa data->path, postfix, samples); 2840d3c8c08eSAlexey Budankov if (ratio) { 2841d3c8c08eSAlexey Budankov fprintf(stderr, ", compressed (original %.3f MB, ratio is %.3f)", 2842d3c8c08eSAlexey Budankov rec->session->bytes_transferred / 1024.0 / 1024.0, 2843d3c8c08eSAlexey Budankov ratio); 2844d3c8c08eSAlexey Budankov } 2845d3c8c08eSAlexey Budankov fprintf(stderr, " ]\n"); 2846e3d59112SNamhyung Kim } 2847e3d59112SNamhyung Kim 284839d17dacSArnaldo Carvalho de Melo out_delete_session: 2849da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 2850304f0a2fSIan Rogers if (done_fd >= 0) { 2851304f0a2fSIan Rogers fd = done_fd; 2852304f0a2fSIan Rogers done_fd = -1; 2853304f0a2fSIan Rogers 2854304f0a2fSIan Rogers close(fd); 2855304f0a2fSIan Rogers } 2856da231338SAnand K Mistry #endif 28575d7f4116SAlexey Budankov zstd_fini(&session->zstd_data); 285839d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 2859657ee553SSong Liu 2860657ee553SSong Liu if (!opts->no_bpf_event) 286108c83997SArnaldo Carvalho de Melo evlist__stop_sb_thread(rec->sb_evlist); 286245604710SNamhyung Kim return status; 286386470930SIngo Molnar } 286486470930SIngo Molnar 28650883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 286609b0fd45SJiri Olsa { 2867aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 2868a601fdffSJiri Olsa 28690883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 287026d33022SJiri Olsa 28710883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 287209b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 28730883e820SArnaldo Carvalho de Melo callchain->dump_size); 28740883e820SArnaldo Carvalho de Melo } 28750883e820SArnaldo Carvalho de Melo 28760883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 28770883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 28780883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 28790883e820SArnaldo Carvalho de Melo { 28800883e820SArnaldo Carvalho de Melo int ret; 28810883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 28820883e820SArnaldo Carvalho de Melo 28830883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 28840883e820SArnaldo Carvalho de Melo if (unset) { 28850883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 28860883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 28870883e820SArnaldo Carvalho de Melo return 0; 28880883e820SArnaldo Carvalho de Melo } 28890883e820SArnaldo Carvalho de Melo 28900883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 28910883e820SArnaldo Carvalho de Melo if (!ret) { 28920883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 28930883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 28940883e820SArnaldo Carvalho de Melo record->sample_address = true; 28950883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 28960883e820SArnaldo Carvalho de Melo } 28970883e820SArnaldo Carvalho de Melo 28980883e820SArnaldo Carvalho de Melo return ret; 289909b0fd45SJiri Olsa } 290009b0fd45SJiri Olsa 2901c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 290209b0fd45SJiri Olsa const char *arg, 290309b0fd45SJiri Olsa int unset) 290409b0fd45SJiri Olsa { 29050883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 290626d33022SJiri Olsa } 290726d33022SJiri Olsa 2908c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 290909b0fd45SJiri Olsa const char *arg __maybe_unused, 291009b0fd45SJiri Olsa int unset __maybe_unused) 291109b0fd45SJiri Olsa { 29122ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 2913c421e80bSKan Liang 29142ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 291509b0fd45SJiri Olsa 29162ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 29172ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 2918eb853e80SJiri Olsa 29192ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 292009b0fd45SJiri Olsa return 0; 292109b0fd45SJiri Olsa } 292209b0fd45SJiri Olsa 2923eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 2924eb853e80SJiri Olsa { 29257a29c087SNamhyung Kim struct record *rec = cb; 29267a29c087SNamhyung Kim 29277a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 29287a29c087SNamhyung Kim if (!strcmp(value, "cache")) 29297a29c087SNamhyung Kim rec->no_buildid_cache = false; 29307a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 29317a29c087SNamhyung Kim rec->no_buildid_cache = true; 29327a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 29337a29c087SNamhyung Kim rec->no_buildid = true; 2934e29386c8SJiri Olsa else if (!strcmp(value, "mmap")) 2935e29386c8SJiri Olsa rec->buildid_mmap = true; 29367a29c087SNamhyung Kim else 29377a29c087SNamhyung Kim return -1; 29387a29c087SNamhyung Kim return 0; 29397a29c087SNamhyung Kim } 2940cff17205SYisheng Xie if (!strcmp(var, "record.call-graph")) { 2941cff17205SYisheng Xie var = "call-graph.record-mode"; 2942eb853e80SJiri Olsa return perf_default_config(var, value, cb); 2943eb853e80SJiri Olsa } 294493f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 294593f20c0fSAlexey Budankov if (!strcmp(var, "record.aio")) { 294693f20c0fSAlexey Budankov rec->opts.nr_cblocks = strtol(value, NULL, 0); 294793f20c0fSAlexey Budankov if (!rec->opts.nr_cblocks) 294893f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_default; 294993f20c0fSAlexey Budankov } 295093f20c0fSAlexey Budankov #endif 29519bce13eaSJiri Olsa if (!strcmp(var, "record.debuginfod")) { 29529bce13eaSJiri Olsa rec->debuginfod.urls = strdup(value); 29539bce13eaSJiri Olsa if (!rec->debuginfod.urls) 29549bce13eaSJiri Olsa return -ENOMEM; 29559bce13eaSJiri Olsa rec->debuginfod.set = true; 29569bce13eaSJiri Olsa } 2957eb853e80SJiri Olsa 2958cff17205SYisheng Xie return 0; 2959cff17205SYisheng Xie } 2960cff17205SYisheng Xie 29616657a099SAdrian Hunter static int record__parse_event_enable_time(const struct option *opt, const char *str, int unset) 29626657a099SAdrian Hunter { 29636657a099SAdrian Hunter struct record *rec = (struct record *)opt->value; 29646657a099SAdrian Hunter 29656657a099SAdrian Hunter return evlist__parse_event_enable_time(rec->evlist, &rec->opts, str, unset); 29666657a099SAdrian Hunter } 2967814c8c38SPeter Zijlstra 2968f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset) 2969f4fe11b7SAlexey Budankov { 2970f4fe11b7SAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 2971f4fe11b7SAlexey Budankov 2972f4fe11b7SAlexey Budankov if (unset || !str) 2973f4fe11b7SAlexey Budankov return 0; 2974f4fe11b7SAlexey Budankov 2975f4fe11b7SAlexey Budankov if (!strcasecmp(str, "node")) 2976f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_NODE; 2977f4fe11b7SAlexey Budankov else if (!strcasecmp(str, "cpu")) 2978f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_CPU; 2979f4fe11b7SAlexey Budankov 2980f4fe11b7SAlexey Budankov return 0; 2981f4fe11b7SAlexey Budankov } 2982f4fe11b7SAlexey Budankov 29837954f716SAlexey Bayduraev static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits) 29847954f716SAlexey Bayduraev { 29857954f716SAlexey Bayduraev mask->nbits = nr_bits; 29867954f716SAlexey Bayduraev mask->bits = bitmap_zalloc(mask->nbits); 29877954f716SAlexey Bayduraev if (!mask->bits) 29887954f716SAlexey Bayduraev return -ENOMEM; 29897954f716SAlexey Bayduraev 29907954f716SAlexey Bayduraev return 0; 29917954f716SAlexey Bayduraev } 29927954f716SAlexey Bayduraev 29937954f716SAlexey Bayduraev static void record__mmap_cpu_mask_free(struct mmap_cpu_mask *mask) 29947954f716SAlexey Bayduraev { 29957954f716SAlexey Bayduraev bitmap_free(mask->bits); 29967954f716SAlexey Bayduraev mask->nbits = 0; 29977954f716SAlexey Bayduraev } 29987954f716SAlexey Bayduraev 29997954f716SAlexey Bayduraev static int record__thread_mask_alloc(struct thread_mask *mask, int nr_bits) 30007954f716SAlexey Bayduraev { 30017954f716SAlexey Bayduraev int ret; 30027954f716SAlexey Bayduraev 30037954f716SAlexey Bayduraev ret = record__mmap_cpu_mask_alloc(&mask->maps, nr_bits); 30047954f716SAlexey Bayduraev if (ret) { 30057954f716SAlexey Bayduraev mask->affinity.bits = NULL; 30067954f716SAlexey Bayduraev return ret; 30077954f716SAlexey Bayduraev } 30087954f716SAlexey Bayduraev 30097954f716SAlexey Bayduraev ret = record__mmap_cpu_mask_alloc(&mask->affinity, nr_bits); 30107954f716SAlexey Bayduraev if (ret) { 30117954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->maps); 30127954f716SAlexey Bayduraev mask->maps.bits = NULL; 30137954f716SAlexey Bayduraev } 30147954f716SAlexey Bayduraev 30157954f716SAlexey Bayduraev return ret; 30167954f716SAlexey Bayduraev } 30177954f716SAlexey Bayduraev 30187954f716SAlexey Bayduraev static void record__thread_mask_free(struct thread_mask *mask) 30197954f716SAlexey Bayduraev { 30207954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->maps); 30217954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->affinity); 30227954f716SAlexey Bayduraev } 30237954f716SAlexey Bayduraev 302406380a84SAlexey Bayduraev static int record__parse_threads(const struct option *opt, const char *str, int unset) 302506380a84SAlexey Bayduraev { 3026f466e5edSAlexey Bayduraev int s; 302706380a84SAlexey Bayduraev struct record_opts *opts = opt->value; 302806380a84SAlexey Bayduraev 3029f466e5edSAlexey Bayduraev if (unset || !str || !strlen(str)) { 303006380a84SAlexey Bayduraev opts->threads_spec = THREAD_SPEC__CPU; 3031f466e5edSAlexey Bayduraev } else { 3032f466e5edSAlexey Bayduraev for (s = 1; s < THREAD_SPEC__MAX; s++) { 3033f466e5edSAlexey Bayduraev if (s == THREAD_SPEC__USER) { 3034f466e5edSAlexey Bayduraev opts->threads_user_spec = strdup(str); 3035f466e5edSAlexey Bayduraev if (!opts->threads_user_spec) 3036f466e5edSAlexey Bayduraev return -ENOMEM; 3037f466e5edSAlexey Bayduraev opts->threads_spec = THREAD_SPEC__USER; 3038f466e5edSAlexey Bayduraev break; 3039f466e5edSAlexey Bayduraev } 3040f466e5edSAlexey Bayduraev if (!strncasecmp(str, thread_spec_tags[s], strlen(thread_spec_tags[s]))) { 3041f466e5edSAlexey Bayduraev opts->threads_spec = s; 3042f466e5edSAlexey Bayduraev break; 3043f466e5edSAlexey Bayduraev } 3044f466e5edSAlexey Bayduraev } 3045f466e5edSAlexey Bayduraev } 3046f466e5edSAlexey Bayduraev 3047f466e5edSAlexey Bayduraev if (opts->threads_spec == THREAD_SPEC__USER) 3048f466e5edSAlexey Bayduraev pr_debug("threads_spec: %s\n", opts->threads_user_spec); 3049f466e5edSAlexey Bayduraev else 3050f466e5edSAlexey Bayduraev pr_debug("threads_spec: %s\n", thread_spec_tags[opts->threads_spec]); 305106380a84SAlexey Bayduraev 305206380a84SAlexey Bayduraev return 0; 305306380a84SAlexey Bayduraev } 305406380a84SAlexey Bayduraev 30556d575816SJiwei Sun static int parse_output_max_size(const struct option *opt, 30566d575816SJiwei Sun const char *str, int unset) 30576d575816SJiwei Sun { 30586d575816SJiwei Sun unsigned long *s = (unsigned long *)opt->value; 30596d575816SJiwei Sun static struct parse_tag tags_size[] = { 30606d575816SJiwei Sun { .tag = 'B', .mult = 1 }, 30616d575816SJiwei Sun { .tag = 'K', .mult = 1 << 10 }, 30626d575816SJiwei Sun { .tag = 'M', .mult = 1 << 20 }, 30636d575816SJiwei Sun { .tag = 'G', .mult = 1 << 30 }, 30646d575816SJiwei Sun { .tag = 0 }, 30656d575816SJiwei Sun }; 30666d575816SJiwei Sun unsigned long val; 30676d575816SJiwei Sun 30686d575816SJiwei Sun if (unset) { 30696d575816SJiwei Sun *s = 0; 30706d575816SJiwei Sun return 0; 30716d575816SJiwei Sun } 30726d575816SJiwei Sun 30736d575816SJiwei Sun val = parse_tag_value(str, tags_size); 30746d575816SJiwei Sun if (val != (unsigned long) -1) { 30756d575816SJiwei Sun *s = val; 30766d575816SJiwei Sun return 0; 30776d575816SJiwei Sun } 30786d575816SJiwei Sun 30796d575816SJiwei Sun return -1; 30806d575816SJiwei Sun } 30816d575816SJiwei Sun 3082e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 3083e9db1310SAdrian Hunter const char *str, 3084e9db1310SAdrian Hunter int unset __maybe_unused) 3085e9db1310SAdrian Hunter { 3086e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 3087e9db1310SAdrian Hunter char *s, *p; 3088e9db1310SAdrian Hunter unsigned int mmap_pages; 3089e9db1310SAdrian Hunter int ret; 3090e9db1310SAdrian Hunter 3091e9db1310SAdrian Hunter if (!str) 3092e9db1310SAdrian Hunter return -EINVAL; 3093e9db1310SAdrian Hunter 3094e9db1310SAdrian Hunter s = strdup(str); 3095e9db1310SAdrian Hunter if (!s) 3096e9db1310SAdrian Hunter return -ENOMEM; 3097e9db1310SAdrian Hunter 3098e9db1310SAdrian Hunter p = strchr(s, ','); 3099e9db1310SAdrian Hunter if (p) 3100e9db1310SAdrian Hunter *p = '\0'; 3101e9db1310SAdrian Hunter 3102e9db1310SAdrian Hunter if (*s) { 310325f84702SArnaldo Carvalho de Melo ret = __evlist__parse_mmap_pages(&mmap_pages, s); 3104e9db1310SAdrian Hunter if (ret) 3105e9db1310SAdrian Hunter goto out_free; 3106e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 3107e9db1310SAdrian Hunter } 3108e9db1310SAdrian Hunter 3109e9db1310SAdrian Hunter if (!p) { 3110e9db1310SAdrian Hunter ret = 0; 3111e9db1310SAdrian Hunter goto out_free; 3112e9db1310SAdrian Hunter } 3113e9db1310SAdrian Hunter 311425f84702SArnaldo Carvalho de Melo ret = __evlist__parse_mmap_pages(&mmap_pages, p + 1); 3115e9db1310SAdrian Hunter if (ret) 3116e9db1310SAdrian Hunter goto out_free; 3117e9db1310SAdrian Hunter 3118e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 3119e9db1310SAdrian Hunter 3120e9db1310SAdrian Hunter out_free: 3121e9db1310SAdrian Hunter free(s); 3122e9db1310SAdrian Hunter return ret; 3123e9db1310SAdrian Hunter } 3124e9db1310SAdrian Hunter 31257248e308SAlexandre Truong void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __maybe_unused) 31267248e308SAlexandre Truong { 31277248e308SAlexandre Truong } 31287248e308SAlexandre Truong 31291d078ccbSAlexey Budankov static int parse_control_option(const struct option *opt, 31301d078ccbSAlexey Budankov const char *str, 31311d078ccbSAlexey Budankov int unset __maybe_unused) 31321d078ccbSAlexey Budankov { 31339864a66dSAdrian Hunter struct record_opts *opts = opt->value; 31341d078ccbSAlexey Budankov 3135a8fcbd26SAdrian Hunter return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close); 3136a8fcbd26SAdrian Hunter } 3137a8fcbd26SAdrian Hunter 31380c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 31390c582449SJiri Olsa { 31409521b5f2SJiri Olsa u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages); 31410c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 31420c582449SJiri Olsa 31430c582449SJiri Olsa wakeup_size /= 2; 31440c582449SJiri Olsa 31450c582449SJiri Olsa if (s->size < wakeup_size) { 31460c582449SJiri Olsa char buf[100]; 31470c582449SJiri Olsa 31480c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 31490c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 31500c582449SJiri Olsa "wakeup kernel buffer size (%s) " 31510c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 31520c582449SJiri Olsa } 31530c582449SJiri Olsa } 31540c582449SJiri Olsa 3155cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 3156cb4e1ebbSJiri Olsa { 3157cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 3158dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 3159dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 3160dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 3161dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 3162dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 3163dc0c6127SJiri Olsa { .tag = 0 }, 3164dc0c6127SJiri Olsa }; 3165bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 3166bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 3167bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 3168bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 3169bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 3170bfacbe3bSJiri Olsa { .tag = 0 }, 3171bfacbe3bSJiri Olsa }; 3172dc0c6127SJiri Olsa unsigned long val; 3173cb4e1ebbSJiri Olsa 3174899e5ffbSArnaldo Carvalho de Melo /* 3175899e5ffbSArnaldo Carvalho de Melo * If we're using --switch-output-events, then we imply its 3176899e5ffbSArnaldo Carvalho de Melo * --switch-output=signal, as we'll send a SIGUSR2 from the side band 3177899e5ffbSArnaldo Carvalho de Melo * thread to its parent. 3178899e5ffbSArnaldo Carvalho de Melo */ 3179b5f2511dSAlexey Bayduraev if (rec->switch_output_event_set) { 3180b5f2511dSAlexey Bayduraev if (record__threads_enabled(rec)) { 3181b5f2511dSAlexey Bayduraev pr_warning("WARNING: --switch-output-event option is not available in parallel streaming mode.\n"); 3182b5f2511dSAlexey Bayduraev return 0; 3183b5f2511dSAlexey Bayduraev } 3184899e5ffbSArnaldo Carvalho de Melo goto do_signal; 3185b5f2511dSAlexey Bayduraev } 3186899e5ffbSArnaldo Carvalho de Melo 3187cb4e1ebbSJiri Olsa if (!s->set) 3188cb4e1ebbSJiri Olsa return 0; 3189cb4e1ebbSJiri Olsa 3190b5f2511dSAlexey Bayduraev if (record__threads_enabled(rec)) { 3191b5f2511dSAlexey Bayduraev pr_warning("WARNING: --switch-output option is not available in parallel streaming mode.\n"); 3192b5f2511dSAlexey Bayduraev return 0; 3193b5f2511dSAlexey Bayduraev } 3194b5f2511dSAlexey Bayduraev 3195cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 3196899e5ffbSArnaldo Carvalho de Melo do_signal: 3197cb4e1ebbSJiri Olsa s->signal = true; 3198cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 3199dc0c6127SJiri Olsa goto enabled; 3200dc0c6127SJiri Olsa } 3201dc0c6127SJiri Olsa 3202dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 3203dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 3204dc0c6127SJiri Olsa s->size = val; 3205dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 3206dc0c6127SJiri Olsa goto enabled; 3207cb4e1ebbSJiri Olsa } 3208cb4e1ebbSJiri Olsa 3209bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 3210bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 3211bfacbe3bSJiri Olsa s->time = val; 3212bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 3213bfacbe3bSJiri Olsa s->str, s->time); 3214bfacbe3bSJiri Olsa goto enabled; 3215bfacbe3bSJiri Olsa } 3216bfacbe3bSJiri Olsa 3217cb4e1ebbSJiri Olsa return -1; 3218dc0c6127SJiri Olsa 3219dc0c6127SJiri Olsa enabled: 3220dc0c6127SJiri Olsa rec->timestamp_filename = true; 3221dc0c6127SJiri Olsa s->enabled = true; 32220c582449SJiri Olsa 32230c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 32240c582449SJiri Olsa switch_output_size_warn(rec); 32250c582449SJiri Olsa 3226dc0c6127SJiri Olsa return 0; 3227cb4e1ebbSJiri Olsa } 3228cb4e1ebbSJiri Olsa 3229e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 323086470930SIngo Molnar "perf record [<options>] [<command>]", 323186470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 323286470930SIngo Molnar NULL 323386470930SIngo Molnar }; 3234e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 323586470930SIngo Molnar 32366e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event, 32376e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 32386e0a9b3dSArnaldo Carvalho de Melo { 32396e0a9b3dSArnaldo Carvalho de Melo /* 32406e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 32416e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 32426e0a9b3dSArnaldo Carvalho de Melo */ 32436e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 32446e0a9b3dSArnaldo Carvalho de Melo return 0; 32456e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap(tool, event, sample, machine); 32466e0a9b3dSArnaldo Carvalho de Melo } 32476e0a9b3dSArnaldo Carvalho de Melo 32486e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event, 32496e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 32506e0a9b3dSArnaldo Carvalho de Melo { 32516e0a9b3dSArnaldo Carvalho de Melo /* 32526e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 32536e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 32546e0a9b3dSArnaldo Carvalho de Melo */ 32556e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 32566e0a9b3dSArnaldo Carvalho de Melo return 0; 32576e0a9b3dSArnaldo Carvalho de Melo 32586e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap2(tool, event, sample, machine); 32596e0a9b3dSArnaldo Carvalho de Melo } 32606e0a9b3dSArnaldo Carvalho de Melo 326166286ed3SAdrian Hunter static int process_timestamp_boundary(struct perf_tool *tool, 326266286ed3SAdrian Hunter union perf_event *event __maybe_unused, 326366286ed3SAdrian Hunter struct perf_sample *sample, 326466286ed3SAdrian Hunter struct machine *machine __maybe_unused) 326566286ed3SAdrian Hunter { 326666286ed3SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 326766286ed3SAdrian Hunter 326866286ed3SAdrian Hunter set_timestamp_boundary(rec, sample->time); 326966286ed3SAdrian Hunter return 0; 327066286ed3SAdrian Hunter } 327166286ed3SAdrian Hunter 327241b740b6SNamhyung Kim static int parse_record_synth_option(const struct option *opt, 327341b740b6SNamhyung Kim const char *str, 327441b740b6SNamhyung Kim int unset __maybe_unused) 327541b740b6SNamhyung Kim { 327641b740b6SNamhyung Kim struct record_opts *opts = opt->value; 327741b740b6SNamhyung Kim char *p = strdup(str); 327841b740b6SNamhyung Kim 327941b740b6SNamhyung Kim if (p == NULL) 328041b740b6SNamhyung Kim return -1; 328141b740b6SNamhyung Kim 328241b740b6SNamhyung Kim opts->synth = parse_synth_opt(p); 328341b740b6SNamhyung Kim free(p); 328441b740b6SNamhyung Kim 328541b740b6SNamhyung Kim if (opts->synth < 0) { 328641b740b6SNamhyung Kim pr_err("Invalid synth option: %s\n", str); 328741b740b6SNamhyung Kim return -1; 328841b740b6SNamhyung Kim } 328941b740b6SNamhyung Kim return 0; 329041b740b6SNamhyung Kim } 329141b740b6SNamhyung Kim 3292d20deb64SArnaldo Carvalho de Melo /* 32938c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 32948c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 3295d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 3296d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 3297d20deb64SArnaldo Carvalho de Melo * 3298d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 3299d20deb64SArnaldo Carvalho de Melo * 3300d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 3301d20deb64SArnaldo Carvalho de Melo */ 33028c6f45a7SArnaldo Carvalho de Melo static struct record record = { 3303d20deb64SArnaldo Carvalho de Melo .opts = { 33048affc2b8SAndi Kleen .sample_time = true, 3305d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 3306d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 3307d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 3308447a6013SArnaldo Carvalho de Melo .freq = 4000, 3309d1cb9fceSNamhyung Kim .target = { 3310d1cb9fceSNamhyung Kim .uses_mmap = true, 33113aa5939dSAdrian Hunter .default_per_cpu = true, 3312d1cb9fceSNamhyung Kim }, 3313470530bbSAlexey Budankov .mmap_flush = MMAP_FLUSH_DEFAULT, 3314d99c22eaSStephane Eranian .nr_threads_synthesize = 1, 33151d078ccbSAlexey Budankov .ctl_fd = -1, 33161d078ccbSAlexey Budankov .ctl_fd_ack = -1, 331741b740b6SNamhyung Kim .synth = PERF_SYNTH_ALL, 3318d20deb64SArnaldo Carvalho de Melo }, 3319e3d59112SNamhyung Kim .tool = { 3320e3d59112SNamhyung Kim .sample = process_sample_event, 3321e3d59112SNamhyung Kim .fork = perf_event__process_fork, 3322cca8482cSAdrian Hunter .exit = perf_event__process_exit, 3323e3d59112SNamhyung Kim .comm = perf_event__process_comm, 3324f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 33256e0a9b3dSArnaldo Carvalho de Melo .mmap = build_id__process_mmap, 33266e0a9b3dSArnaldo Carvalho de Melo .mmap2 = build_id__process_mmap2, 332766286ed3SAdrian Hunter .itrace_start = process_timestamp_boundary, 332866286ed3SAdrian Hunter .aux = process_timestamp_boundary, 3329cca8482cSAdrian Hunter .ordered_events = true, 3330e3d59112SNamhyung Kim }, 3331d20deb64SArnaldo Carvalho de Melo }; 33327865e817SFrederic Weisbecker 333376a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 333476a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 333561eaa3beSArnaldo Carvalho de Melo 33360aab2136SWang Nan static bool dry_run; 33370aab2136SWang Nan 3338d20deb64SArnaldo Carvalho de Melo /* 3339d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 3340d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 3341b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 33427b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 3343d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 3344d20deb64SArnaldo Carvalho de Melo */ 3345efd21307SJiri Olsa static struct option __record_options[] = { 3346d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 334786470930SIngo Molnar "event selector. use 'perf list' to list available events", 3348f120f9d5SJiri Olsa parse_events_option), 3349d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 3350c171b552SLi Zefan "event filter", parse_filter), 33514ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 33524ba1faa1SWang Nan NULL, "don't record events from perf itself", 33534ba1faa1SWang Nan exclude_perf), 3354bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 3355d6d901c2SZhang, Yanmin "record events on existing process id"), 3356bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 3357d6d901c2SZhang, Yanmin "record events on existing thread id"), 3358d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 335986470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 3360509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 3361acac03faSKirill Smelkov "collect data without buffering"), 3362d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 3363daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 3364bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 336586470930SIngo Molnar "system-wide collection from all CPUs"), 3366bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 3367c45c6ea2SStephane Eranian "list of cpus to monitor"), 3368d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 33692d4f2799SJiri Olsa OPT_STRING('o', "output", &record.data.path, "file", 337086470930SIngo Molnar "output file name"), 337169e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 337269e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 33732e6cdf99SStephane Eranian "child tasks do not inherit counters"), 33744ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 33754ea648aeSWang Nan "synthesize non-sample events at the end of output"), 3376626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 3377a060c1f1SWei Li OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"), 3378b09c2364SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq, 3379b09c2364SArnaldo Carvalho de Melo "Fail if the specified frequency can't be used"), 338067230479SArnaldo Carvalho de Melo OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'", 338167230479SArnaldo Carvalho de Melo "profile at this frequency", 338267230479SArnaldo Carvalho de Melo record__parse_freq), 3383e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 3384e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 3385e9db1310SAdrian Hunter record__parse_mmap_pages), 3386470530bbSAlexey Budankov OPT_CALLBACK(0, "mmap-flush", &record.opts, "number", 3387470530bbSAlexey Budankov "Minimal number of bytes that is extracted from mmap data pages (default: 1)", 3388470530bbSAlexey Budankov record__mmap_flush_parse), 33892ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 339009b0fd45SJiri Olsa NULL, "enables call-graph recording" , 339109b0fd45SJiri Olsa &record_callchain_opt), 339209b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 339376a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 339409b0fd45SJiri Olsa &record_parse_callchain_opt), 3395c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 33963da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 3397a527c2c1SJames Clark OPT_BOOLEAN('q', "quiet", &quiet, "don't print any warnings or messages"), 3398d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 3399649c48a9SPeter Zijlstra "per thread counts"), 340056100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 34013b0a5daaSKan Liang OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, 34023b0a5daaSKan Liang "Record the sample physical addresses"), 3403542b88fdSKan Liang OPT_BOOLEAN(0, "data-page-size", &record.opts.sample_data_page_size, 3404542b88fdSKan Liang "Record the sampled data address data page size"), 3405c1de7f3dSKan Liang OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size, 3406c1de7f3dSKan Liang "Record the sampled code address (ip) page size"), 3407b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 340861110883SAdrian Hunter OPT_BOOLEAN(0, "sample-identifier", &record.opts.sample_identifier, 340961110883SAdrian Hunter "Record the sample identifier"), 34103abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 34113abebc55SAdrian Hunter &record.opts.sample_time_set, 34123abebc55SAdrian Hunter "Record the sample timestamps"), 3413f290aa1fSJiri Olsa OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set, 3414f290aa1fSJiri Olsa "Record the sample period"), 3415d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 3416649c48a9SPeter Zijlstra "don't sample"), 3417d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 3418d2db9a98SWang Nan &record.no_buildid_cache_set, 3419a1ac1d3cSStephane Eranian "do not update the buildid cache"), 3420d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 3421d2db9a98SWang Nan &record.no_buildid_set, 3422baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 3423d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 3424023695d9SStephane Eranian "monitor event in cgroup name only", 3425023695d9SStephane Eranian parse_cgroups), 34266657a099SAdrian Hunter OPT_CALLBACK('D', "delay", &record, "ms", 34276657a099SAdrian Hunter "ms to wait before starting measurement after program start (-1: start with events disabled), " 34286657a099SAdrian Hunter "or ranges of time to enable events e.g. '-D 10-20,30-40'", 34296657a099SAdrian Hunter record__parse_event_enable_time), 3430eeb399b5SAdrian Hunter OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"), 3431bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 3432bea03405SNamhyung Kim "user to profile"), 3433a5aabdacSStephane Eranian 3434a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 3435a5aabdacSStephane Eranian "branch any", "sample any taken branches", 3436a5aabdacSStephane Eranian parse_branch_stack), 3437a5aabdacSStephane Eranian 3438a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 3439a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 3440bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 344105484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 344205484298SAndi Kleen "sample by weight (on special events only)"), 3443475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 3444475eeab9SAndi Kleen "sample transaction flags (special events only)"), 34453aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 34463aa5939dSAdrian Hunter "use per-thread mmaps"), 3447bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 3448bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 3449aeea9062SKan Liang " use '-I?' to list register names", parse_intr_regs), 345084c41742SAndi Kleen OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register", 345184c41742SAndi Kleen "sample selected machine registers on interrupt," 3452aeea9062SKan Liang " use '--user-regs=?' to list register names", parse_user_regs), 345385c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 345485c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 3455814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 3456814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 3457814c8c38SPeter Zijlstra parse_clockid), 34582dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 34592dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 3460c0a6de06SAdrian Hunter OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts, 3461c0a6de06SAdrian Hunter "opts", "sample AUX area", ""), 34623fcb10e4SMark Drayton OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 34639d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 3464f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 3465f3b3614aSHari Bathini "Record namespaces events"), 34668fb4b679SNamhyung Kim OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup, 34678fb4b679SNamhyung Kim "Record cgroup events"), 346816b4b4e1SAdrian Hunter OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events, 346916b4b4e1SAdrian Hunter &record.opts.record_switch_events_set, 3470b757bb09SAdrian Hunter "Record context switch events"), 347185723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 347285723885SJiri Olsa "Configure all used events to run in kernel space.", 347385723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 347485723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 347585723885SJiri Olsa "Configure all used events to run in user space.", 347685723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 347753651b28Syuzhoujian OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains, 347853651b28Syuzhoujian "collect kernel callchains"), 347953651b28Syuzhoujian OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains, 348053651b28Syuzhoujian "collect user callchains"), 348171dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 348271dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 348371dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 348471dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 34857efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 34867efe0e03SHe Kuang "file", "vmlinux pathname"), 34876156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 34886156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 3489e29386c8SJiri Olsa OPT_BOOLEAN(0, "buildid-mmap", &record.buildid_mmap, 3490e29386c8SJiri Olsa "Record build-id in map events"), 3491ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 3492ecfd7a9cSWang Nan "append timestamp to output filename"), 349368588bafSJin Yao OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary, 349468588bafSJin Yao "Record timestamp boundary (time of first/last samples)"), 3495cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 3496c38dab7dSAndi Kleen &record.switch_output.set, "signal or size[BKMG] or time[smhd]", 3497c38dab7dSAndi Kleen "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold", 3498dc0c6127SJiri Olsa "signal"), 3499899e5ffbSArnaldo Carvalho de Melo OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event", 3500899e5ffbSArnaldo Carvalho de Melo "switch output event selector. use 'perf list' to list available events", 3501899e5ffbSArnaldo Carvalho de Melo parse_events_option_new_evlist), 350203724b2eSAndi Kleen OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files, 350303724b2eSAndi Kleen "Limit number of switch output generated files"), 35040aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 35050aab2136SWang Nan "Parse options then exit"), 3506d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 350793f20c0fSAlexey Budankov OPT_CALLBACK_OPTARG(0, "aio", &record.opts, 350893f20c0fSAlexey Budankov &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)", 3509d3d1af6fSAlexey Budankov record__aio_parse), 3510d3d1af6fSAlexey Budankov #endif 3511f4fe11b7SAlexey Budankov OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu", 3512f4fe11b7SAlexey Budankov "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer", 3513f4fe11b7SAlexey Budankov record__parse_affinity), 3514504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 3515b5f2511dSAlexey Bayduraev OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, "n", 3516b5f2511dSAlexey Bayduraev "Compress records using specified level (default: 1 - fastest compression, 22 - greatest compression)", 3517504c1ad1SAlexey Budankov record__parse_comp_level), 3518504c1ad1SAlexey Budankov #endif 35196d575816SJiwei Sun OPT_CALLBACK(0, "max-size", &record.output_max_size, 35206d575816SJiwei Sun "size", "Limit the maximum size of the output file", parse_output_max_size), 3521d99c22eaSStephane Eranian OPT_UINTEGER(0, "num-thread-synthesize", 3522d99c22eaSStephane Eranian &record.opts.nr_threads_synthesize, 3523d99c22eaSStephane Eranian "number of threads to run for event synthesis"), 352470943490SStephane Eranian #ifdef HAVE_LIBPFM 352570943490SStephane Eranian OPT_CALLBACK(0, "pfm-events", &record.evlist, "event", 352670943490SStephane Eranian "libpfm4 event selector. use 'perf list' to list available events", 352770943490SStephane Eranian parse_libpfm_events_option), 352870943490SStephane Eranian #endif 3529a8fcbd26SAdrian Hunter OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]", 3530d20aff15SAdrian Hunter "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n" 3531d20aff15SAdrian Hunter "\t\t\t 'snapshot': AUX area tracing snapshot).\n" 3532a8fcbd26SAdrian Hunter "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n" 3533a8fcbd26SAdrian Hunter "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.", 35341d078ccbSAlexey Budankov parse_control_option), 353541b740b6SNamhyung Kim OPT_CALLBACK(0, "synth", &record.opts, "no|all|task|mmap|cgroup", 353641b740b6SNamhyung Kim "Fine-tune event synthesis: default=all", parse_record_synth_option), 35379bce13eaSJiri Olsa OPT_STRING_OPTARG_SET(0, "debuginfod", &record.debuginfod.urls, 35389bce13eaSJiri Olsa &record.debuginfod.set, "debuginfod urls", 35399bce13eaSJiri Olsa "Enable debuginfod data retrieval from DEBUGINFOD_URLS or specified urls", 35409bce13eaSJiri Olsa "system"), 354106380a84SAlexey Bayduraev OPT_CALLBACK_OPTARG(0, "threads", &record.opts, NULL, "spec", 354206380a84SAlexey Bayduraev "write collected trace data into several data files using parallel threads", 354306380a84SAlexey Bayduraev record__parse_threads), 3544edc41a10SNamhyung Kim OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"), 354586470930SIngo Molnar OPT_END() 354686470930SIngo Molnar }; 354786470930SIngo Molnar 3548e5b2c207SNamhyung Kim struct option *record_options = __record_options; 3549e5b2c207SNamhyung Kim 3550cbd7bfc7SAthira Rajeev static int record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpu_map *cpus) 35517954f716SAlexey Bayduraev { 35520255571aSIan Rogers struct perf_cpu cpu; 35530255571aSIan Rogers int idx; 35547954f716SAlexey Bayduraev 355523380e4dSAlexey Bayduraev if (cpu_map__is_dummy(cpus)) 3556cbd7bfc7SAthira Rajeev return 0; 355723380e4dSAlexey Bayduraev 3558cbd7bfc7SAthira Rajeev perf_cpu_map__for_each_cpu(cpu, idx, cpus) { 3559ca76d7d2SAdrian Hunter if (cpu.cpu == -1) 3560ca76d7d2SAdrian Hunter continue; 3561cbd7bfc7SAthira Rajeev /* Return ENODEV is input cpu is greater than max cpu */ 3562cbd7bfc7SAthira Rajeev if ((unsigned long)cpu.cpu > mask->nbits) 3563cbd7bfc7SAthira Rajeev return -ENODEV; 356449bd97c2SSean Christopherson __set_bit(cpu.cpu, mask->bits); 35657954f716SAlexey Bayduraev } 35667954f716SAlexey Bayduraev 3567cbd7bfc7SAthira Rajeev return 0; 3568cbd7bfc7SAthira Rajeev } 3569cbd7bfc7SAthira Rajeev 3570f466e5edSAlexey Bayduraev static int record__mmap_cpu_mask_init_spec(struct mmap_cpu_mask *mask, const char *mask_spec) 3571f466e5edSAlexey Bayduraev { 3572f466e5edSAlexey Bayduraev struct perf_cpu_map *cpus; 3573f466e5edSAlexey Bayduraev 3574f466e5edSAlexey Bayduraev cpus = perf_cpu_map__new(mask_spec); 3575f466e5edSAlexey Bayduraev if (!cpus) 3576f466e5edSAlexey Bayduraev return -ENOMEM; 3577f466e5edSAlexey Bayduraev 3578f466e5edSAlexey Bayduraev bitmap_zero(mask->bits, mask->nbits); 3579cbd7bfc7SAthira Rajeev if (record__mmap_cpu_mask_init(mask, cpus)) 3580cbd7bfc7SAthira Rajeev return -ENODEV; 3581cbd7bfc7SAthira Rajeev 3582f466e5edSAlexey Bayduraev perf_cpu_map__put(cpus); 3583f466e5edSAlexey Bayduraev 3584f466e5edSAlexey Bayduraev return 0; 3585f466e5edSAlexey Bayduraev } 3586f466e5edSAlexey Bayduraev 35877954f716SAlexey Bayduraev static void record__free_thread_masks(struct record *rec, int nr_threads) 35887954f716SAlexey Bayduraev { 35897954f716SAlexey Bayduraev int t; 35907954f716SAlexey Bayduraev 35917954f716SAlexey Bayduraev if (rec->thread_masks) 35927954f716SAlexey Bayduraev for (t = 0; t < nr_threads; t++) 35937954f716SAlexey Bayduraev record__thread_mask_free(&rec->thread_masks[t]); 35947954f716SAlexey Bayduraev 35957954f716SAlexey Bayduraev zfree(&rec->thread_masks); 35967954f716SAlexey Bayduraev } 35977954f716SAlexey Bayduraev 35987954f716SAlexey Bayduraev static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr_bits) 35997954f716SAlexey Bayduraev { 36007954f716SAlexey Bayduraev int t, ret; 36017954f716SAlexey Bayduraev 36027954f716SAlexey Bayduraev rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks))); 36037954f716SAlexey Bayduraev if (!rec->thread_masks) { 36047954f716SAlexey Bayduraev pr_err("Failed to allocate thread masks\n"); 36057954f716SAlexey Bayduraev return -ENOMEM; 36067954f716SAlexey Bayduraev } 36077954f716SAlexey Bayduraev 36087954f716SAlexey Bayduraev for (t = 0; t < nr_threads; t++) { 36097954f716SAlexey Bayduraev ret = record__thread_mask_alloc(&rec->thread_masks[t], nr_bits); 36107954f716SAlexey Bayduraev if (ret) { 36117954f716SAlexey Bayduraev pr_err("Failed to allocate thread masks[%d]\n", t); 36127954f716SAlexey Bayduraev goto out_free; 36137954f716SAlexey Bayduraev } 36147954f716SAlexey Bayduraev } 36157954f716SAlexey Bayduraev 36167954f716SAlexey Bayduraev return 0; 36177954f716SAlexey Bayduraev 36187954f716SAlexey Bayduraev out_free: 36197954f716SAlexey Bayduraev record__free_thread_masks(rec, nr_threads); 36207954f716SAlexey Bayduraev 36217954f716SAlexey Bayduraev return ret; 36227954f716SAlexey Bayduraev } 36237954f716SAlexey Bayduraev 362406380a84SAlexey Bayduraev static int record__init_thread_cpu_masks(struct record *rec, struct perf_cpu_map *cpus) 362506380a84SAlexey Bayduraev { 362606380a84SAlexey Bayduraev int t, ret, nr_cpus = perf_cpu_map__nr(cpus); 362706380a84SAlexey Bayduraev 362806380a84SAlexey Bayduraev ret = record__alloc_thread_masks(rec, nr_cpus, cpu__max_cpu().cpu); 362906380a84SAlexey Bayduraev if (ret) 363006380a84SAlexey Bayduraev return ret; 363106380a84SAlexey Bayduraev 363206380a84SAlexey Bayduraev rec->nr_threads = nr_cpus; 363306380a84SAlexey Bayduraev pr_debug("nr_threads: %d\n", rec->nr_threads); 363406380a84SAlexey Bayduraev 363506380a84SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 363649bd97c2SSean Christopherson __set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].maps.bits); 363749bd97c2SSean Christopherson __set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].affinity.bits); 36387c0a6144SYang Jihong if (verbose > 0) { 363906380a84SAlexey Bayduraev pr_debug("thread_masks[%d]: ", t); 364006380a84SAlexey Bayduraev mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps"); 364106380a84SAlexey Bayduraev pr_debug("thread_masks[%d]: ", t); 364206380a84SAlexey Bayduraev mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity"); 364306380a84SAlexey Bayduraev } 364406380a84SAlexey Bayduraev } 364506380a84SAlexey Bayduraev 364606380a84SAlexey Bayduraev return 0; 364706380a84SAlexey Bayduraev } 364806380a84SAlexey Bayduraev 3649f466e5edSAlexey Bayduraev static int record__init_thread_masks_spec(struct record *rec, struct perf_cpu_map *cpus, 3650f466e5edSAlexey Bayduraev const char **maps_spec, const char **affinity_spec, 3651f466e5edSAlexey Bayduraev u32 nr_spec) 3652f466e5edSAlexey Bayduraev { 3653f466e5edSAlexey Bayduraev u32 s; 3654f466e5edSAlexey Bayduraev int ret = 0, t = 0; 3655f466e5edSAlexey Bayduraev struct mmap_cpu_mask cpus_mask; 3656f466e5edSAlexey Bayduraev struct thread_mask thread_mask, full_mask, *thread_masks; 3657f466e5edSAlexey Bayduraev 3658f466e5edSAlexey Bayduraev ret = record__mmap_cpu_mask_alloc(&cpus_mask, cpu__max_cpu().cpu); 3659f466e5edSAlexey Bayduraev if (ret) { 3660f466e5edSAlexey Bayduraev pr_err("Failed to allocate CPUs mask\n"); 3661f466e5edSAlexey Bayduraev return ret; 3662f466e5edSAlexey Bayduraev } 3663cbd7bfc7SAthira Rajeev 3664cbd7bfc7SAthira Rajeev ret = record__mmap_cpu_mask_init(&cpus_mask, cpus); 3665cbd7bfc7SAthira Rajeev if (ret) { 3666cbd7bfc7SAthira Rajeev pr_err("Failed to init cpu mask\n"); 3667cbd7bfc7SAthira Rajeev goto out_free_cpu_mask; 3668cbd7bfc7SAthira Rajeev } 3669f466e5edSAlexey Bayduraev 3670f466e5edSAlexey Bayduraev ret = record__thread_mask_alloc(&full_mask, cpu__max_cpu().cpu); 3671f466e5edSAlexey Bayduraev if (ret) { 3672f466e5edSAlexey Bayduraev pr_err("Failed to allocate full mask\n"); 3673f466e5edSAlexey Bayduraev goto out_free_cpu_mask; 3674f466e5edSAlexey Bayduraev } 3675f466e5edSAlexey Bayduraev 3676f466e5edSAlexey Bayduraev ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu); 3677f466e5edSAlexey Bayduraev if (ret) { 3678f466e5edSAlexey Bayduraev pr_err("Failed to allocate thread mask\n"); 3679f466e5edSAlexey Bayduraev goto out_free_full_and_cpu_masks; 3680f466e5edSAlexey Bayduraev } 3681f466e5edSAlexey Bayduraev 3682f466e5edSAlexey Bayduraev for (s = 0; s < nr_spec; s++) { 3683f466e5edSAlexey Bayduraev ret = record__mmap_cpu_mask_init_spec(&thread_mask.maps, maps_spec[s]); 3684f466e5edSAlexey Bayduraev if (ret) { 3685f466e5edSAlexey Bayduraev pr_err("Failed to initialize maps thread mask\n"); 3686f466e5edSAlexey Bayduraev goto out_free; 3687f466e5edSAlexey Bayduraev } 3688f466e5edSAlexey Bayduraev ret = record__mmap_cpu_mask_init_spec(&thread_mask.affinity, affinity_spec[s]); 3689f466e5edSAlexey Bayduraev if (ret) { 3690f466e5edSAlexey Bayduraev pr_err("Failed to initialize affinity thread mask\n"); 3691f466e5edSAlexey Bayduraev goto out_free; 3692f466e5edSAlexey Bayduraev } 3693f466e5edSAlexey Bayduraev 3694f466e5edSAlexey Bayduraev /* ignore invalid CPUs but do not allow empty masks */ 3695f466e5edSAlexey Bayduraev if (!bitmap_and(thread_mask.maps.bits, thread_mask.maps.bits, 3696f466e5edSAlexey Bayduraev cpus_mask.bits, thread_mask.maps.nbits)) { 3697f466e5edSAlexey Bayduraev pr_err("Empty maps mask: %s\n", maps_spec[s]); 3698f466e5edSAlexey Bayduraev ret = -EINVAL; 3699f466e5edSAlexey Bayduraev goto out_free; 3700f466e5edSAlexey Bayduraev } 3701f466e5edSAlexey Bayduraev if (!bitmap_and(thread_mask.affinity.bits, thread_mask.affinity.bits, 3702f466e5edSAlexey Bayduraev cpus_mask.bits, thread_mask.affinity.nbits)) { 3703f466e5edSAlexey Bayduraev pr_err("Empty affinity mask: %s\n", affinity_spec[s]); 3704f466e5edSAlexey Bayduraev ret = -EINVAL; 3705f466e5edSAlexey Bayduraev goto out_free; 3706f466e5edSAlexey Bayduraev } 3707f466e5edSAlexey Bayduraev 3708f466e5edSAlexey Bayduraev /* do not allow intersection with other masks (full_mask) */ 3709f466e5edSAlexey Bayduraev if (bitmap_intersects(thread_mask.maps.bits, full_mask.maps.bits, 3710f466e5edSAlexey Bayduraev thread_mask.maps.nbits)) { 3711f466e5edSAlexey Bayduraev pr_err("Intersecting maps mask: %s\n", maps_spec[s]); 3712f466e5edSAlexey Bayduraev ret = -EINVAL; 3713f466e5edSAlexey Bayduraev goto out_free; 3714f466e5edSAlexey Bayduraev } 3715f466e5edSAlexey Bayduraev if (bitmap_intersects(thread_mask.affinity.bits, full_mask.affinity.bits, 3716f466e5edSAlexey Bayduraev thread_mask.affinity.nbits)) { 3717f466e5edSAlexey Bayduraev pr_err("Intersecting affinity mask: %s\n", affinity_spec[s]); 3718f466e5edSAlexey Bayduraev ret = -EINVAL; 3719f466e5edSAlexey Bayduraev goto out_free; 3720f466e5edSAlexey Bayduraev } 3721f466e5edSAlexey Bayduraev 3722f466e5edSAlexey Bayduraev bitmap_or(full_mask.maps.bits, full_mask.maps.bits, 3723f466e5edSAlexey Bayduraev thread_mask.maps.bits, full_mask.maps.nbits); 3724f466e5edSAlexey Bayduraev bitmap_or(full_mask.affinity.bits, full_mask.affinity.bits, 3725f466e5edSAlexey Bayduraev thread_mask.affinity.bits, full_mask.maps.nbits); 3726f466e5edSAlexey Bayduraev 3727f466e5edSAlexey Bayduraev thread_masks = realloc(rec->thread_masks, (t + 1) * sizeof(struct thread_mask)); 3728f466e5edSAlexey Bayduraev if (!thread_masks) { 3729f466e5edSAlexey Bayduraev pr_err("Failed to reallocate thread masks\n"); 3730f466e5edSAlexey Bayduraev ret = -ENOMEM; 3731f466e5edSAlexey Bayduraev goto out_free; 3732f466e5edSAlexey Bayduraev } 3733f466e5edSAlexey Bayduraev rec->thread_masks = thread_masks; 3734f466e5edSAlexey Bayduraev rec->thread_masks[t] = thread_mask; 37357c0a6144SYang Jihong if (verbose > 0) { 3736f466e5edSAlexey Bayduraev pr_debug("thread_masks[%d]: ", t); 3737f466e5edSAlexey Bayduraev mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps"); 3738f466e5edSAlexey Bayduraev pr_debug("thread_masks[%d]: ", t); 3739f466e5edSAlexey Bayduraev mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity"); 3740f466e5edSAlexey Bayduraev } 3741f466e5edSAlexey Bayduraev t++; 3742f466e5edSAlexey Bayduraev ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu); 3743f466e5edSAlexey Bayduraev if (ret) { 3744f466e5edSAlexey Bayduraev pr_err("Failed to allocate thread mask\n"); 3745f466e5edSAlexey Bayduraev goto out_free_full_and_cpu_masks; 3746f466e5edSAlexey Bayduraev } 3747f466e5edSAlexey Bayduraev } 3748f466e5edSAlexey Bayduraev rec->nr_threads = t; 3749f466e5edSAlexey Bayduraev pr_debug("nr_threads: %d\n", rec->nr_threads); 3750f466e5edSAlexey Bayduraev if (!rec->nr_threads) 3751f466e5edSAlexey Bayduraev ret = -EINVAL; 3752f466e5edSAlexey Bayduraev 3753f466e5edSAlexey Bayduraev out_free: 3754f466e5edSAlexey Bayduraev record__thread_mask_free(&thread_mask); 3755f466e5edSAlexey Bayduraev out_free_full_and_cpu_masks: 3756f466e5edSAlexey Bayduraev record__thread_mask_free(&full_mask); 3757f466e5edSAlexey Bayduraev out_free_cpu_mask: 3758f466e5edSAlexey Bayduraev record__mmap_cpu_mask_free(&cpus_mask); 3759f466e5edSAlexey Bayduraev 3760f466e5edSAlexey Bayduraev return ret; 3761f466e5edSAlexey Bayduraev } 3762f466e5edSAlexey Bayduraev 3763f466e5edSAlexey Bayduraev static int record__init_thread_core_masks(struct record *rec, struct perf_cpu_map *cpus) 3764f466e5edSAlexey Bayduraev { 3765f466e5edSAlexey Bayduraev int ret; 3766f466e5edSAlexey Bayduraev struct cpu_topology *topo; 3767f466e5edSAlexey Bayduraev 3768f466e5edSAlexey Bayduraev topo = cpu_topology__new(); 3769f466e5edSAlexey Bayduraev if (!topo) { 3770f466e5edSAlexey Bayduraev pr_err("Failed to allocate CPU topology\n"); 3771f466e5edSAlexey Bayduraev return -ENOMEM; 3772f466e5edSAlexey Bayduraev } 3773f466e5edSAlexey Bayduraev 3774f466e5edSAlexey Bayduraev ret = record__init_thread_masks_spec(rec, cpus, topo->core_cpus_list, 3775f466e5edSAlexey Bayduraev topo->core_cpus_list, topo->core_cpus_lists); 3776f466e5edSAlexey Bayduraev cpu_topology__delete(topo); 3777f466e5edSAlexey Bayduraev 3778f466e5edSAlexey Bayduraev return ret; 3779f466e5edSAlexey Bayduraev } 3780f466e5edSAlexey Bayduraev 3781f466e5edSAlexey Bayduraev static int record__init_thread_package_masks(struct record *rec, struct perf_cpu_map *cpus) 3782f466e5edSAlexey Bayduraev { 3783f466e5edSAlexey Bayduraev int ret; 3784f466e5edSAlexey Bayduraev struct cpu_topology *topo; 3785f466e5edSAlexey Bayduraev 3786f466e5edSAlexey Bayduraev topo = cpu_topology__new(); 3787f466e5edSAlexey Bayduraev if (!topo) { 3788f466e5edSAlexey Bayduraev pr_err("Failed to allocate CPU topology\n"); 3789f466e5edSAlexey Bayduraev return -ENOMEM; 3790f466e5edSAlexey Bayduraev } 3791f466e5edSAlexey Bayduraev 3792f466e5edSAlexey Bayduraev ret = record__init_thread_masks_spec(rec, cpus, topo->package_cpus_list, 3793f466e5edSAlexey Bayduraev topo->package_cpus_list, topo->package_cpus_lists); 3794f466e5edSAlexey Bayduraev cpu_topology__delete(topo); 3795f466e5edSAlexey Bayduraev 3796f466e5edSAlexey Bayduraev return ret; 3797f466e5edSAlexey Bayduraev } 3798f466e5edSAlexey Bayduraev 3799f466e5edSAlexey Bayduraev static int record__init_thread_numa_masks(struct record *rec, struct perf_cpu_map *cpus) 3800f466e5edSAlexey Bayduraev { 3801f466e5edSAlexey Bayduraev u32 s; 3802f466e5edSAlexey Bayduraev int ret; 3803f466e5edSAlexey Bayduraev const char **spec; 3804f466e5edSAlexey Bayduraev struct numa_topology *topo; 3805f466e5edSAlexey Bayduraev 3806f466e5edSAlexey Bayduraev topo = numa_topology__new(); 3807f466e5edSAlexey Bayduraev if (!topo) { 3808f466e5edSAlexey Bayduraev pr_err("Failed to allocate NUMA topology\n"); 3809f466e5edSAlexey Bayduraev return -ENOMEM; 3810f466e5edSAlexey Bayduraev } 3811f466e5edSAlexey Bayduraev 3812f466e5edSAlexey Bayduraev spec = zalloc(topo->nr * sizeof(char *)); 3813f466e5edSAlexey Bayduraev if (!spec) { 3814f466e5edSAlexey Bayduraev pr_err("Failed to allocate NUMA spec\n"); 3815f466e5edSAlexey Bayduraev ret = -ENOMEM; 3816f466e5edSAlexey Bayduraev goto out_delete_topo; 3817f466e5edSAlexey Bayduraev } 3818f466e5edSAlexey Bayduraev for (s = 0; s < topo->nr; s++) 3819f466e5edSAlexey Bayduraev spec[s] = topo->nodes[s].cpus; 3820f466e5edSAlexey Bayduraev 3821f466e5edSAlexey Bayduraev ret = record__init_thread_masks_spec(rec, cpus, spec, spec, topo->nr); 3822f466e5edSAlexey Bayduraev 3823f466e5edSAlexey Bayduraev zfree(&spec); 3824f466e5edSAlexey Bayduraev 3825f466e5edSAlexey Bayduraev out_delete_topo: 3826f466e5edSAlexey Bayduraev numa_topology__delete(topo); 3827f466e5edSAlexey Bayduraev 3828f466e5edSAlexey Bayduraev return ret; 3829f466e5edSAlexey Bayduraev } 3830f466e5edSAlexey Bayduraev 3831f466e5edSAlexey Bayduraev static int record__init_thread_user_masks(struct record *rec, struct perf_cpu_map *cpus) 3832f466e5edSAlexey Bayduraev { 3833f466e5edSAlexey Bayduraev int t, ret; 3834f466e5edSAlexey Bayduraev u32 s, nr_spec = 0; 3835f466e5edSAlexey Bayduraev char **maps_spec = NULL, **affinity_spec = NULL, **tmp_spec; 3836f466e5edSAlexey Bayduraev char *user_spec, *spec, *spec_ptr, *mask, *mask_ptr, *dup_mask = NULL; 3837f466e5edSAlexey Bayduraev 3838f466e5edSAlexey Bayduraev for (t = 0, user_spec = (char *)rec->opts.threads_user_spec; ; t++, user_spec = NULL) { 3839f466e5edSAlexey Bayduraev spec = strtok_r(user_spec, ":", &spec_ptr); 3840f466e5edSAlexey Bayduraev if (spec == NULL) 3841f466e5edSAlexey Bayduraev break; 3842f466e5edSAlexey Bayduraev pr_debug2("threads_spec[%d]: %s\n", t, spec); 3843f466e5edSAlexey Bayduraev mask = strtok_r(spec, "/", &mask_ptr); 3844f466e5edSAlexey Bayduraev if (mask == NULL) 3845f466e5edSAlexey Bayduraev break; 3846f466e5edSAlexey Bayduraev pr_debug2(" maps mask: %s\n", mask); 3847f466e5edSAlexey Bayduraev tmp_spec = realloc(maps_spec, (nr_spec + 1) * sizeof(char *)); 3848f466e5edSAlexey Bayduraev if (!tmp_spec) { 3849f466e5edSAlexey Bayduraev pr_err("Failed to reallocate maps spec\n"); 3850f466e5edSAlexey Bayduraev ret = -ENOMEM; 3851f466e5edSAlexey Bayduraev goto out_free; 3852f466e5edSAlexey Bayduraev } 3853f466e5edSAlexey Bayduraev maps_spec = tmp_spec; 3854f466e5edSAlexey Bayduraev maps_spec[nr_spec] = dup_mask = strdup(mask); 3855f466e5edSAlexey Bayduraev if (!maps_spec[nr_spec]) { 3856f466e5edSAlexey Bayduraev pr_err("Failed to allocate maps spec[%d]\n", nr_spec); 3857f466e5edSAlexey Bayduraev ret = -ENOMEM; 3858f466e5edSAlexey Bayduraev goto out_free; 3859f466e5edSAlexey Bayduraev } 3860f466e5edSAlexey Bayduraev mask = strtok_r(NULL, "/", &mask_ptr); 3861f466e5edSAlexey Bayduraev if (mask == NULL) { 3862f466e5edSAlexey Bayduraev pr_err("Invalid thread maps or affinity specs\n"); 3863f466e5edSAlexey Bayduraev ret = -EINVAL; 3864f466e5edSAlexey Bayduraev goto out_free; 3865f466e5edSAlexey Bayduraev } 3866f466e5edSAlexey Bayduraev pr_debug2(" affinity mask: %s\n", mask); 3867f466e5edSAlexey Bayduraev tmp_spec = realloc(affinity_spec, (nr_spec + 1) * sizeof(char *)); 3868f466e5edSAlexey Bayduraev if (!tmp_spec) { 3869f466e5edSAlexey Bayduraev pr_err("Failed to reallocate affinity spec\n"); 3870f466e5edSAlexey Bayduraev ret = -ENOMEM; 3871f466e5edSAlexey Bayduraev goto out_free; 3872f466e5edSAlexey Bayduraev } 3873f466e5edSAlexey Bayduraev affinity_spec = tmp_spec; 3874f466e5edSAlexey Bayduraev affinity_spec[nr_spec] = strdup(mask); 3875f466e5edSAlexey Bayduraev if (!affinity_spec[nr_spec]) { 3876f466e5edSAlexey Bayduraev pr_err("Failed to allocate affinity spec[%d]\n", nr_spec); 3877f466e5edSAlexey Bayduraev ret = -ENOMEM; 3878f466e5edSAlexey Bayduraev goto out_free; 3879f466e5edSAlexey Bayduraev } 3880f466e5edSAlexey Bayduraev dup_mask = NULL; 3881f466e5edSAlexey Bayduraev nr_spec++; 3882f466e5edSAlexey Bayduraev } 3883f466e5edSAlexey Bayduraev 3884f466e5edSAlexey Bayduraev ret = record__init_thread_masks_spec(rec, cpus, (const char **)maps_spec, 3885f466e5edSAlexey Bayduraev (const char **)affinity_spec, nr_spec); 3886f466e5edSAlexey Bayduraev 3887f466e5edSAlexey Bayduraev out_free: 3888f466e5edSAlexey Bayduraev free(dup_mask); 3889f466e5edSAlexey Bayduraev for (s = 0; s < nr_spec; s++) { 3890f466e5edSAlexey Bayduraev if (maps_spec) 3891f466e5edSAlexey Bayduraev free(maps_spec[s]); 3892f466e5edSAlexey Bayduraev if (affinity_spec) 3893f466e5edSAlexey Bayduraev free(affinity_spec[s]); 3894f466e5edSAlexey Bayduraev } 3895f466e5edSAlexey Bayduraev free(affinity_spec); 3896f466e5edSAlexey Bayduraev free(maps_spec); 3897f466e5edSAlexey Bayduraev 3898f466e5edSAlexey Bayduraev return ret; 3899f466e5edSAlexey Bayduraev } 3900f466e5edSAlexey Bayduraev 39017954f716SAlexey Bayduraev static int record__init_thread_default_masks(struct record *rec, struct perf_cpu_map *cpus) 39027954f716SAlexey Bayduraev { 39037954f716SAlexey Bayduraev int ret; 39047954f716SAlexey Bayduraev 39057954f716SAlexey Bayduraev ret = record__alloc_thread_masks(rec, 1, cpu__max_cpu().cpu); 39067954f716SAlexey Bayduraev if (ret) 39077954f716SAlexey Bayduraev return ret; 39087954f716SAlexey Bayduraev 3909cbd7bfc7SAthira Rajeev if (record__mmap_cpu_mask_init(&rec->thread_masks->maps, cpus)) 3910cbd7bfc7SAthira Rajeev return -ENODEV; 39117954f716SAlexey Bayduraev 39127954f716SAlexey Bayduraev rec->nr_threads = 1; 39137954f716SAlexey Bayduraev 39147954f716SAlexey Bayduraev return 0; 39157954f716SAlexey Bayduraev } 39167954f716SAlexey Bayduraev 39177954f716SAlexey Bayduraev static int record__init_thread_masks(struct record *rec) 39187954f716SAlexey Bayduraev { 3919f466e5edSAlexey Bayduraev int ret = 0; 39207be1feddSAdrian Hunter struct perf_cpu_map *cpus = rec->evlist->core.all_cpus; 39217954f716SAlexey Bayduraev 392206380a84SAlexey Bayduraev if (!record__threads_enabled(rec)) 39237954f716SAlexey Bayduraev return record__init_thread_default_masks(rec, cpus); 392406380a84SAlexey Bayduraev 39257be1feddSAdrian Hunter if (evlist__per_thread(rec->evlist)) { 392623380e4dSAlexey Bayduraev pr_err("--per-thread option is mutually exclusive to parallel streaming mode.\n"); 392723380e4dSAlexey Bayduraev return -EINVAL; 392823380e4dSAlexey Bayduraev } 392923380e4dSAlexey Bayduraev 3930f466e5edSAlexey Bayduraev switch (rec->opts.threads_spec) { 3931f466e5edSAlexey Bayduraev case THREAD_SPEC__CPU: 3932f466e5edSAlexey Bayduraev ret = record__init_thread_cpu_masks(rec, cpus); 3933f466e5edSAlexey Bayduraev break; 3934f466e5edSAlexey Bayduraev case THREAD_SPEC__CORE: 3935f466e5edSAlexey Bayduraev ret = record__init_thread_core_masks(rec, cpus); 3936f466e5edSAlexey Bayduraev break; 3937f466e5edSAlexey Bayduraev case THREAD_SPEC__PACKAGE: 3938f466e5edSAlexey Bayduraev ret = record__init_thread_package_masks(rec, cpus); 3939f466e5edSAlexey Bayduraev break; 3940f466e5edSAlexey Bayduraev case THREAD_SPEC__NUMA: 3941f466e5edSAlexey Bayduraev ret = record__init_thread_numa_masks(rec, cpus); 3942f466e5edSAlexey Bayduraev break; 3943f466e5edSAlexey Bayduraev case THREAD_SPEC__USER: 3944f466e5edSAlexey Bayduraev ret = record__init_thread_user_masks(rec, cpus); 3945f466e5edSAlexey Bayduraev break; 3946f466e5edSAlexey Bayduraev default: 3947f466e5edSAlexey Bayduraev break; 3948f466e5edSAlexey Bayduraev } 3949f466e5edSAlexey Bayduraev 3950f466e5edSAlexey Bayduraev return ret; 39517954f716SAlexey Bayduraev } 39527954f716SAlexey Bayduraev 3953b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 395486470930SIngo Molnar { 3955ef149c25SAdrian Hunter int err; 39568c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 395716ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 395886470930SIngo Molnar 395967230479SArnaldo Carvalho de Melo setlocale(LC_ALL, ""); 396067230479SArnaldo Carvalho de Melo 396148e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 396248e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 396348e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 396448e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 396548e1cab1SWang Nan # undef set_nobuild 396648e1cab1SWang Nan #endif 396748e1cab1SWang Nan 39687efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 39697efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 39707efe0e03SHe Kuang # define REASON "NO_DWARF=1" 39717efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 39727efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 39737efe0e03SHe Kuang # else 39747efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 39757efe0e03SHe Kuang # endif 39767efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 39777efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 39787efe0e03SHe Kuang # undef set_nobuild 39797efe0e03SHe Kuang # undef REASON 39807efe0e03SHe Kuang #endif 39817efe0e03SHe Kuang 3982edc41a10SNamhyung Kim #ifndef HAVE_BPF_SKEL 3983edc41a10SNamhyung Kim # define set_nobuild(s, l, m, c) set_option_nobuild(record_options, s, l, m, c) 3984a980755bSIan Rogers set_nobuild('\0', "off-cpu", "NO_BPF_SKEL=1", true); 3985edc41a10SNamhyung Kim # undef set_nobuild 3986edc41a10SNamhyung Kim #endif 3987edc41a10SNamhyung Kim 39889d2ed645SAlexey Budankov rec->opts.affinity = PERF_AFFINITY_SYS; 39899d2ed645SAlexey Budankov 39900f98b11cSJiri Olsa rec->evlist = evlist__new(); 39913e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 3992361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 3993361c99a6SArnaldo Carvalho de Melo 3994ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 3995ecc4c561SArnaldo Carvalho de Melo if (err) 3996ecc4c561SArnaldo Carvalho de Melo return err; 3997eb853e80SJiri Olsa 3998bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 3999a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 400068ba3235SNamhyung Kim if (quiet) 400168ba3235SNamhyung Kim perf_quiet_option(); 4002483635a9SJiri Olsa 40037cc72553SJames Clark err = symbol__validate_sym_arguments(); 40047cc72553SJames Clark if (err) 40057cc72553SJames Clark return err; 40067cc72553SJames Clark 40079bce13eaSJiri Olsa perf_debuginfod_setup(&record.debuginfod); 40089bce13eaSJiri Olsa 4009483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 4010602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 4011483635a9SJiri Olsa rec->opts.target.system_wide = true; 401286470930SIngo Molnar 4013bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 4014c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 4015c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 4016c7118369SNamhyung Kim 4017023695d9SStephane Eranian } 4018504c1ad1SAlexey Budankov 4019e29386c8SJiri Olsa if (rec->buildid_mmap) { 4020e29386c8SJiri Olsa if (!perf_can_record_build_id()) { 4021e29386c8SJiri Olsa pr_err("Failed: no support to record build id in mmap events, update your kernel.\n"); 4022e29386c8SJiri Olsa err = -EINVAL; 4023e29386c8SJiri Olsa goto out_opts; 4024e29386c8SJiri Olsa } 4025e29386c8SJiri Olsa pr_debug("Enabling build id in mmap2 events.\n"); 4026e29386c8SJiri Olsa /* Enable mmap build id synthesizing. */ 4027e29386c8SJiri Olsa symbol_conf.buildid_mmap2 = true; 4028e29386c8SJiri Olsa /* Enable perf_event_attr::build_id bit. */ 4029e29386c8SJiri Olsa rec->opts.build_id = true; 4030e29386c8SJiri Olsa /* Disable build id cache. */ 4031e29386c8SJiri Olsa rec->no_buildid = true; 4032e29386c8SJiri Olsa } 4033e29386c8SJiri Olsa 40344f2abe91SNamhyung Kim if (rec->opts.record_cgroup && !perf_can_record_cgroup()) { 40354f2abe91SNamhyung Kim pr_err("Kernel has no cgroup sampling support.\n"); 40364f2abe91SNamhyung Kim err = -EINVAL; 40374f2abe91SNamhyung Kim goto out_opts; 40384f2abe91SNamhyung Kim } 40394f2abe91SNamhyung Kim 4040f42c0ce5SAdrian Hunter if (rec->opts.kcore) 4041f42c0ce5SAdrian Hunter rec->opts.text_poke = true; 4042f42c0ce5SAdrian Hunter 404356f735ffSAlexey Bayduraev if (rec->opts.kcore || record__threads_enabled(rec)) 4044eeb399b5SAdrian Hunter rec->data.is_dir = true; 4045eeb399b5SAdrian Hunter 4046b5f2511dSAlexey Bayduraev if (record__threads_enabled(rec)) { 4047b5f2511dSAlexey Bayduraev if (rec->opts.affinity != PERF_AFFINITY_SYS) { 4048b5f2511dSAlexey Bayduraev pr_err("--affinity option is mutually exclusive to parallel streaming mode.\n"); 4049b5f2511dSAlexey Bayduraev goto out_opts; 4050b5f2511dSAlexey Bayduraev } 4051b5f2511dSAlexey Bayduraev if (record__aio_enabled(rec)) { 4052b5f2511dSAlexey Bayduraev pr_err("Asynchronous streaming mode (--aio) is mutually exclusive to parallel streaming mode.\n"); 4053b5f2511dSAlexey Bayduraev goto out_opts; 4054b5f2511dSAlexey Bayduraev } 4055b5f2511dSAlexey Bayduraev } 4056b5f2511dSAlexey Bayduraev 4057504c1ad1SAlexey Budankov if (rec->opts.comp_level != 0) { 4058504c1ad1SAlexey Budankov pr_debug("Compression enabled, disabling build id collection at the end of the session.\n"); 4059504c1ad1SAlexey Budankov rec->no_buildid = true; 4060504c1ad1SAlexey Budankov } 4061504c1ad1SAlexey Budankov 4062b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 4063b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 4064c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 4065c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 4066a8fcbd26SAdrian Hunter err = -EINVAL; 4067a8fcbd26SAdrian Hunter goto out_opts; 4068b757bb09SAdrian Hunter } 4069023695d9SStephane Eranian 4070cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 4071cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 4072a8fcbd26SAdrian Hunter err = -EINVAL; 4073a8fcbd26SAdrian Hunter goto out_opts; 4074cb4e1ebbSJiri Olsa } 4075cb4e1ebbSJiri Olsa 4076bfacbe3bSJiri Olsa if (rec->switch_output.time) { 4077bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 4078bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 4079bfacbe3bSJiri Olsa } 4080bfacbe3bSJiri Olsa 408103724b2eSAndi Kleen if (rec->switch_output.num_files) { 408203724b2eSAndi Kleen rec->switch_output.filenames = calloc(sizeof(char *), 408303724b2eSAndi Kleen rec->switch_output.num_files); 4084a8fcbd26SAdrian Hunter if (!rec->switch_output.filenames) { 4085a8fcbd26SAdrian Hunter err = -EINVAL; 4086a8fcbd26SAdrian Hunter goto out_opts; 4087a8fcbd26SAdrian Hunter } 408803724b2eSAndi Kleen } 408903724b2eSAndi Kleen 4090b5f2511dSAlexey Bayduraev if (rec->timestamp_filename && record__threads_enabled(rec)) { 4091b5f2511dSAlexey Bayduraev rec->timestamp_filename = false; 4092b5f2511dSAlexey Bayduraev pr_warning("WARNING: --timestamp-filename option is not available in parallel streaming mode.\n"); 4093b5f2511dSAlexey Bayduraev } 4094b5f2511dSAlexey Bayduraev 40951b36c03eSAdrian Hunter /* 40961b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 40971b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 40981b36c03eSAdrian Hunter */ 40991b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 41001b36c03eSAdrian Hunter 41011b36c03eSAdrian Hunter symbol__init(NULL); 41021b36c03eSAdrian Hunter 41034b5ea3bdSAdrian Hunter err = record__auxtrace_init(rec); 41041b36c03eSAdrian Hunter if (err) 41051b36c03eSAdrian Hunter goto out; 41061b36c03eSAdrian Hunter 41070aab2136SWang Nan if (dry_run) 41085c01ad60SAdrian Hunter goto out; 41090aab2136SWang Nan 4110d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 4111d7888573SWang Nan if (err) { 4112d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 4113d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 4114d7888573SWang Nan errbuf); 41155c01ad60SAdrian Hunter goto out; 4116d7888573SWang Nan } 4117d7888573SWang Nan 4118ef149c25SAdrian Hunter err = -ENOMEM; 4119ef149c25SAdrian Hunter 41200c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 4121a1ac1d3cSStephane Eranian disable_buildid_cache(); 4122dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 41230c1d46a8SWang Nan /* 41240c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 41250c1d46a8SWang Nan * generation by default to reduce data file switching 41260c1d46a8SWang Nan * overhead. Still generate buildid if they are required 41270c1d46a8SWang Nan * explicitly using 41280c1d46a8SWang Nan * 412960437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 41300c1d46a8SWang Nan * --no-no-buildid-cache 41310c1d46a8SWang Nan * 41320c1d46a8SWang Nan * Following code equals to: 41330c1d46a8SWang Nan * 41340c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 41350c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 41360c1d46a8SWang Nan * disable_buildid_cache(); 41370c1d46a8SWang Nan */ 41380c1d46a8SWang Nan bool disable = true; 41390c1d46a8SWang Nan 41400c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 41410c1d46a8SWang Nan disable = false; 41420c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 41430c1d46a8SWang Nan disable = false; 41440c1d46a8SWang Nan if (disable) { 41450c1d46a8SWang Nan rec->no_buildid = true; 41460c1d46a8SWang Nan rec->no_buildid_cache = true; 41470c1d46a8SWang Nan disable_buildid_cache(); 41480c1d46a8SWang Nan } 41490c1d46a8SWang Nan } 4150655000e7SArnaldo Carvalho de Melo 41514ea648aeSWang Nan if (record.opts.overwrite) 41524ea648aeSWang Nan record.opts.tail_synthesize = true; 41534ea648aeSWang Nan 4154b53a0755SJin Yao if (rec->evlist->core.nr_entries == 0) { 4155b53a0755SJin Yao if (perf_pmu__has_hybrid()) { 4156b53a0755SJin Yao err = evlist__add_default_hybrid(rec->evlist, 4157b53a0755SJin Yao !record.opts.no_samples); 4158b53a0755SJin Yao } else { 4159b53a0755SJin Yao err = __evlist__add_default(rec->evlist, 4160b53a0755SJin Yao !record.opts.no_samples); 4161b53a0755SJin Yao } 4162b53a0755SJin Yao 4163b53a0755SJin Yao if (err < 0) { 416469aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 4165394c01edSAdrian Hunter goto out; 4166bbd36e5eSPeter Zijlstra } 4167b53a0755SJin Yao } 416886470930SIngo Molnar 416969e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 417069e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 417169e7e5b0SAdrian Hunter 4172602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 417316ad2ffbSNamhyung Kim if (err) { 4174602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 4175c3dec27bSJiri Olsa ui__warning("%s\n", errbuf); 417616ad2ffbSNamhyung Kim } 41774bd0f2d2SNamhyung Kim 4178602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 417916ad2ffbSNamhyung Kim if (err) { 418016ad2ffbSNamhyung Kim int saved_errno = errno; 418116ad2ffbSNamhyung Kim 4182602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 41833780f488SNamhyung Kim ui__error("%s", errbuf); 418416ad2ffbSNamhyung Kim 418516ad2ffbSNamhyung Kim err = -saved_errno; 4186394c01edSAdrian Hunter goto out; 418716ad2ffbSNamhyung Kim } 41880d37aa34SArnaldo Carvalho de Melo 4189ca800068SMengting Zhang /* Enable ignoring missing threads when -u/-p option is defined. */ 4190ca800068SMengting Zhang rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; 419123dc4f15SJiri Olsa 41921d3351e6SJin Yao if (evlist__fix_hybrid_cpus(rec->evlist, rec->opts.target.cpu_list)) { 41931d3351e6SJin Yao pr_err("failed to use cpu list %s\n", 41941d3351e6SJin Yao rec->opts.target.cpu_list); 41951d3351e6SJin Yao goto out; 41961d3351e6SJin Yao } 41971d3351e6SJin Yao 41981d3351e6SJin Yao rec->opts.target.hybrid = perf_pmu__has_hybrid(); 41997248e308SAlexandre Truong 42007248e308SAlexandre Truong if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP) 42017248e308SAlexandre Truong arch__add_leaf_frame_record_opts(&rec->opts); 42027248e308SAlexandre Truong 420316ad2ffbSNamhyung Kim err = -ENOMEM; 42041bf7d836SMartin Liška if (evlist__create_maps(rec->evlist, &rec->opts.target) < 0) { 42051bf7d836SMartin Liška if (rec->opts.target.pid != NULL) { 42061bf7d836SMartin Liška pr_err("Couldn't create thread/CPU maps: %s\n", 42071bf7d836SMartin Liška errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf))); 42081bf7d836SMartin Liška goto out; 42091bf7d836SMartin Liška } 42101bf7d836SMartin Liška else 4211dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 42121bf7d836SMartin Liška } 421369aad6f1SArnaldo Carvalho de Melo 4214ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 4215ef149c25SAdrian Hunter if (err) 4216394c01edSAdrian Hunter goto out; 4217ef149c25SAdrian Hunter 42186156681bSNamhyung Kim /* 42196156681bSNamhyung Kim * We take all buildids when the file contains 42206156681bSNamhyung Kim * AUX area tracing data because we do not decode the 42216156681bSNamhyung Kim * trace because it would take too long. 42226156681bSNamhyung Kim */ 42236156681bSNamhyung Kim if (rec->opts.full_auxtrace) 42246156681bSNamhyung Kim rec->buildid_all = true; 42256156681bSNamhyung Kim 4226246eba8eSAdrian Hunter if (rec->opts.text_poke) { 4227246eba8eSAdrian Hunter err = record__config_text_poke(rec->evlist); 4228246eba8eSAdrian Hunter if (err) { 4229246eba8eSAdrian Hunter pr_err("record__config_text_poke failed, error %d\n", err); 4230246eba8eSAdrian Hunter goto out; 4231246eba8eSAdrian Hunter } 4232246eba8eSAdrian Hunter } 4233246eba8eSAdrian Hunter 4234edc41a10SNamhyung Kim if (rec->off_cpu) { 4235edc41a10SNamhyung Kim err = record__config_off_cpu(rec); 4236edc41a10SNamhyung Kim if (err) { 4237edc41a10SNamhyung Kim pr_err("record__config_off_cpu failed, error %d\n", err); 4238edc41a10SNamhyung Kim goto out; 4239edc41a10SNamhyung Kim } 4240edc41a10SNamhyung Kim } 4241edc41a10SNamhyung Kim 4242b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 424339d17dacSArnaldo Carvalho de Melo err = -EINVAL; 4244394c01edSAdrian Hunter goto out; 42457e4ff9e3SMike Galbraith } 42467e4ff9e3SMike Galbraith 42477954f716SAlexey Bayduraev err = record__init_thread_masks(rec); 42487954f716SAlexey Bayduraev if (err) { 42497954f716SAlexey Bayduraev pr_err("Failed to initialize parallel data streaming masks\n"); 42507954f716SAlexey Bayduraev goto out; 42517954f716SAlexey Bayduraev } 42527954f716SAlexey Bayduraev 425393f20c0fSAlexey Budankov if (rec->opts.nr_cblocks > nr_cblocks_max) 425493f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_max; 42555d7f4116SAlexey Budankov pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks); 4256d3d1af6fSAlexey Budankov 42579d2ed645SAlexey Budankov pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]); 4258470530bbSAlexey Budankov pr_debug("mmap flush: %d\n", rec->opts.mmap_flush); 42599d2ed645SAlexey Budankov 426051255a8aSAlexey Budankov if (rec->opts.comp_level > comp_level_max) 426151255a8aSAlexey Budankov rec->opts.comp_level = comp_level_max; 426251255a8aSAlexey Budankov pr_debug("comp level: %d\n", rec->opts.comp_level); 426351255a8aSAlexey Budankov 4264d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 4265394c01edSAdrian Hunter out: 4266c12995a5SJiri Olsa evlist__delete(rec->evlist); 4267d65a458bSArnaldo Carvalho de Melo symbol__exit(); 4268ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 4269a8fcbd26SAdrian Hunter out_opts: 42707954f716SAlexey Bayduraev record__free_thread_masks(rec, rec->nr_threads); 42717954f716SAlexey Bayduraev rec->nr_threads = 0; 4272ee7fe31eSAdrian Hunter evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close); 427339d17dacSArnaldo Carvalho de Melo return err; 427486470930SIngo Molnar } 42752dd6d8a1SAdrian Hunter 42762dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 42772dd6d8a1SAdrian Hunter { 4278dc0c6127SJiri Olsa struct record *rec = &record; 4279dc0c6127SJiri Olsa 4280d20aff15SAdrian Hunter hit_auxtrace_snapshot_trigger(rec); 42813c1cb7e3SWang Nan 4282dc0c6127SJiri Olsa if (switch_output_signal(rec)) 42833c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 42842dd6d8a1SAdrian Hunter } 4285bfacbe3bSJiri Olsa 4286bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 4287bfacbe3bSJiri Olsa { 4288bfacbe3bSJiri Olsa struct record *rec = &record; 4289bfacbe3bSJiri Olsa 4290bfacbe3bSJiri Olsa if (switch_output_time(rec)) 4291bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 4292bfacbe3bSJiri Olsa } 4293