1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 286470930SIngo Molnar /* 386470930SIngo Molnar * builtin-record.c 486470930SIngo Molnar * 586470930SIngo Molnar * Builtin record command: Record the profile of a workload 686470930SIngo Molnar * (or a CPU, or a PID) into the perf.data output file - for 786470930SIngo Molnar * later analysis via perf report. 886470930SIngo Molnar */ 986470930SIngo Molnar #include "builtin.h" 1086470930SIngo Molnar 116122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h" 124b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1386470930SIngo Molnar #include "util/parse-events.h" 1441840d21STaeung Song #include "util/config.h" 1586470930SIngo Molnar 168f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h" 17f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h" 187c6a1c65SPeter Zijlstra #include "util/header.h" 1966e274f3SFrederic Weisbecker #include "util/event.h" 20361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h" 2169aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 228f28827aSFrederic Weisbecker #include "util/debug.h" 23e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 24aeb00b1aSArnaldo Carvalho de Melo #include "util/target.h" 2594c744b6SArnaldo Carvalho de Melo #include "util/session.h" 2645694aa7SArnaldo Carvalho de Melo #include "util/tool.h" 278d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 28aeb00b1aSArnaldo Carvalho de Melo #include "util/record.h" 29a12b51c4SPaul Mackerras #include "util/cpumap.h" 30fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h" 31f5fc1412SJiri Olsa #include "util/data.h" 32bcc84ec6SStephane Eranian #include "util/perf_regs.h" 33ef149c25SAdrian Hunter #include "util/auxtrace.h" 3446bc29b9SAdrian Hunter #include "util/tsc.h" 35f00898f4SAndi Kleen #include "util/parse-branch-options.h" 36bcc84ec6SStephane Eranian #include "util/parse-regs-options.h" 3740c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h" 3871dc2326SWang Nan #include "util/llvm-utils.h" 398690a2a7SWang Nan #include "util/bpf-loader.h" 405f9cf599SWang Nan #include "util/trigger.h" 41a074865eSWang Nan #include "util/perf-hooks.h" 42f13de660SAlexey Budankov #include "util/cpu-set-sched.h" 43ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h" 44c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h" 4558db1d6eSArnaldo Carvalho de Melo #include "util/units.h" 467b612e29SSong Liu #include "util/bpf-event.h" 47d99c22eaSStephane Eranian #include "util/util.h" 4870943490SStephane Eranian #include "util/pfm.h" 496953beb4SJiri Olsa #include "util/clockid.h" 50b53a0755SJin Yao #include "util/pmu-hybrid.h" 51b53a0755SJin Yao #include "util/evlist-hybrid.h" 52d8871ea7SWang Nan #include "asm/bug.h" 53c1a604dfSArnaldo Carvalho de Melo #include "perf.h" 54f466e5edSAlexey Bayduraev #include "cputopo.h" 557c6a1c65SPeter Zijlstra 56a43783aeSArnaldo Carvalho de Melo #include <errno.h> 57fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 5867230479SArnaldo Carvalho de Melo #include <locale.h> 594208735dSArnaldo Carvalho de Melo #include <poll.h> 60d99c22eaSStephane Eranian #include <pthread.h> 6186470930SIngo Molnar #include <unistd.h> 62415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID 63415ccb58SAlexey Bayduraev #include <syscall.h> 64415ccb58SAlexey Bayduraev #endif 6586470930SIngo Molnar #include <sched.h> 669607ad3aSArnaldo Carvalho de Melo #include <signal.h> 67da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 68da231338SAnand K Mistry #include <sys/eventfd.h> 69da231338SAnand K Mistry #endif 70a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 714208735dSArnaldo Carvalho de Melo #include <sys/wait.h> 72eeb399b5SAdrian Hunter #include <sys/types.h> 73eeb399b5SAdrian Hunter #include <sys/stat.h> 74eeb399b5SAdrian Hunter #include <fcntl.h> 756ef81c55SMamatha Inamdar #include <linux/err.h> 768520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 770693e680SArnaldo Carvalho de Melo #include <linux/time64.h> 78d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h> 798384a260SAlexey Budankov #include <linux/bitmap.h> 80d1e325cfSJiri Olsa #include <sys/time.h> 8178da39faSBernhard Rosenkraenzer 821b43b704SJiri Olsa struct switch_output { 83dc0c6127SJiri Olsa bool enabled; 841b43b704SJiri Olsa bool signal; 85dc0c6127SJiri Olsa unsigned long size; 86bfacbe3bSJiri Olsa unsigned long time; 87cb4e1ebbSJiri Olsa const char *str; 88cb4e1ebbSJiri Olsa bool set; 8903724b2eSAndi Kleen char **filenames; 9003724b2eSAndi Kleen int num_files; 9103724b2eSAndi Kleen int cur_file; 921b43b704SJiri Olsa }; 931b43b704SJiri Olsa 947954f716SAlexey Bayduraev struct thread_mask { 957954f716SAlexey Bayduraev struct mmap_cpu_mask maps; 967954f716SAlexey Bayduraev struct mmap_cpu_mask affinity; 977954f716SAlexey Bayduraev }; 987954f716SAlexey Bayduraev 99415ccb58SAlexey Bayduraev struct record_thread { 100415ccb58SAlexey Bayduraev pid_t tid; 101415ccb58SAlexey Bayduraev struct thread_mask *mask; 102415ccb58SAlexey Bayduraev struct { 103415ccb58SAlexey Bayduraev int msg[2]; 104415ccb58SAlexey Bayduraev int ack[2]; 105415ccb58SAlexey Bayduraev } pipes; 106415ccb58SAlexey Bayduraev struct fdarray pollfd; 107415ccb58SAlexey Bayduraev int ctlfd_pos; 108415ccb58SAlexey Bayduraev int nr_mmaps; 109415ccb58SAlexey Bayduraev struct mmap **maps; 110415ccb58SAlexey Bayduraev struct mmap **overwrite_maps; 111415ccb58SAlexey Bayduraev struct record *rec; 112396b626bSAlexey Bayduraev unsigned long long samples; 113396b626bSAlexey Bayduraev unsigned long waking; 114ae9c7242SAlexey Bayduraev u64 bytes_written; 115610fbc01SAlexey Bayduraev u64 bytes_transferred; 116610fbc01SAlexey Bayduraev u64 bytes_compressed; 117415ccb58SAlexey Bayduraev }; 118415ccb58SAlexey Bayduraev 119396b626bSAlexey Bayduraev static __thread struct record_thread *thread; 120396b626bSAlexey Bayduraev 1211e5de7d9SAlexey Bayduraev enum thread_msg { 1221e5de7d9SAlexey Bayduraev THREAD_MSG__UNDEFINED = 0, 1231e5de7d9SAlexey Bayduraev THREAD_MSG__READY, 1241e5de7d9SAlexey Bayduraev THREAD_MSG__MAX, 1251e5de7d9SAlexey Bayduraev }; 1261e5de7d9SAlexey Bayduraev 1271e5de7d9SAlexey Bayduraev static const char *thread_msg_tags[THREAD_MSG__MAX] = { 1281e5de7d9SAlexey Bayduraev "UNDEFINED", "READY" 1291e5de7d9SAlexey Bayduraev }; 1301e5de7d9SAlexey Bayduraev 13106380a84SAlexey Bayduraev enum thread_spec { 13206380a84SAlexey Bayduraev THREAD_SPEC__UNDEFINED = 0, 13306380a84SAlexey Bayduraev THREAD_SPEC__CPU, 134f466e5edSAlexey Bayduraev THREAD_SPEC__CORE, 135f466e5edSAlexey Bayduraev THREAD_SPEC__PACKAGE, 136f466e5edSAlexey Bayduraev THREAD_SPEC__NUMA, 137f466e5edSAlexey Bayduraev THREAD_SPEC__USER, 138f466e5edSAlexey Bayduraev THREAD_SPEC__MAX, 139f466e5edSAlexey Bayduraev }; 140f466e5edSAlexey Bayduraev 141f466e5edSAlexey Bayduraev static const char *thread_spec_tags[THREAD_SPEC__MAX] = { 142f466e5edSAlexey Bayduraev "undefined", "cpu", "core", "package", "numa", "user" 14306380a84SAlexey Bayduraev }; 14406380a84SAlexey Bayduraev 1458c6f45a7SArnaldo Carvalho de Melo struct record { 14645694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 147b4006796SArnaldo Carvalho de Melo struct record_opts opts; 148d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 1498ceb41d7SJiri Olsa struct perf_data data; 150ef149c25SAdrian Hunter struct auxtrace_record *itr; 15163503dbaSJiri Olsa struct evlist *evlist; 152d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 153bc477d79SArnaldo Carvalho de Melo struct evlist *sb_evlist; 154899e5ffbSArnaldo Carvalho de Melo pthread_t thread_id; 155d20deb64SArnaldo Carvalho de Melo int realtime_prio; 156899e5ffbSArnaldo Carvalho de Melo bool switch_output_event_set; 157d20deb64SArnaldo Carvalho de Melo bool no_buildid; 158d2db9a98SWang Nan bool no_buildid_set; 159d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 160d2db9a98SWang Nan bool no_buildid_cache_set; 1616156681bSNamhyung Kim bool buildid_all; 162e29386c8SJiri Olsa bool buildid_mmap; 163ecfd7a9cSWang Nan bool timestamp_filename; 16468588bafSJin Yao bool timestamp_boundary; 1651b43b704SJiri Olsa struct switch_output switch_output; 1669f065194SYang Shi unsigned long long samples; 1676d575816SJiwei Sun unsigned long output_max_size; /* = 0: unlimited */ 1689bce13eaSJiri Olsa struct perf_debuginfod debuginfod; 1697954f716SAlexey Bayduraev int nr_threads; 1707954f716SAlexey Bayduraev struct thread_mask *thread_masks; 171415ccb58SAlexey Bayduraev struct record_thread *thread_data; 1720f82ebc4SArnaldo Carvalho de Melo }; 17386470930SIngo Molnar 1746d575816SJiwei Sun static volatile int done; 1756d575816SJiwei Sun 176dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 177dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 178dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 179dc0c6127SJiri Olsa 1809d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = { 1819d2ed645SAlexey Budankov "SYS", "NODE", "CPU" 1829d2ed645SAlexey Budankov }; 1839d2ed645SAlexey Budankov 184415ccb58SAlexey Bayduraev #ifndef HAVE_GETTID 185415ccb58SAlexey Bayduraev static inline pid_t gettid(void) 186415ccb58SAlexey Bayduraev { 187415ccb58SAlexey Bayduraev return (pid_t)syscall(__NR_gettid); 188415ccb58SAlexey Bayduraev } 189415ccb58SAlexey Bayduraev #endif 190415ccb58SAlexey Bayduraev 1913217e9feSAlexey Bayduraev static int record__threads_enabled(struct record *rec) 1923217e9feSAlexey Bayduraev { 1933217e9feSAlexey Bayduraev return rec->opts.threads_spec; 1943217e9feSAlexey Bayduraev } 1953217e9feSAlexey Bayduraev 196dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 197dc0c6127SJiri Olsa { 198dc0c6127SJiri Olsa return rec->switch_output.signal && 199dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 200dc0c6127SJiri Olsa } 201dc0c6127SJiri Olsa 202dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 203dc0c6127SJiri Olsa { 204dc0c6127SJiri Olsa return rec->switch_output.size && 205dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 206dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 207dc0c6127SJiri Olsa } 208dc0c6127SJiri Olsa 209bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 210bfacbe3bSJiri Olsa { 211bfacbe3bSJiri Olsa return rec->switch_output.time && 212bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 213bfacbe3bSJiri Olsa } 214bfacbe3bSJiri Olsa 215ae9c7242SAlexey Bayduraev static u64 record__bytes_written(struct record *rec) 216ae9c7242SAlexey Bayduraev { 217ae9c7242SAlexey Bayduraev int t; 218ae9c7242SAlexey Bayduraev u64 bytes_written = rec->bytes_written; 219ae9c7242SAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 220ae9c7242SAlexey Bayduraev 221ae9c7242SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 222ae9c7242SAlexey Bayduraev bytes_written += thread_data[t].bytes_written; 223ae9c7242SAlexey Bayduraev 224ae9c7242SAlexey Bayduraev return bytes_written; 225ae9c7242SAlexey Bayduraev } 226ae9c7242SAlexey Bayduraev 2276d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec) 2286d575816SJiwei Sun { 2296d575816SJiwei Sun return rec->output_max_size && 230ae9c7242SAlexey Bayduraev (record__bytes_written(rec) >= rec->output_max_size); 2316d575816SJiwei Sun } 2326d575816SJiwei Sun 233a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused, 234ded2b8feSJiri Olsa void *bf, size_t size) 235f5970550SPeter Zijlstra { 236ded2b8feSJiri Olsa struct perf_data_file *file = &rec->session->data->file; 237ded2b8feSJiri Olsa 23856f735ffSAlexey Bayduraev if (map && map->file) 23956f735ffSAlexey Bayduraev file = map->file; 24056f735ffSAlexey Bayduraev 241ded2b8feSJiri Olsa if (perf_data_file__write(file, bf, size) < 0) { 2424f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 2438d3eca20SDavid Ahern return -1; 2448d3eca20SDavid Ahern } 245f5970550SPeter Zijlstra 246ae9c7242SAlexey Bayduraev if (map && map->file) 247ae9c7242SAlexey Bayduraev thread->bytes_written += size; 248ae9c7242SAlexey Bayduraev else 249cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 250dc0c6127SJiri Olsa 2516d575816SJiwei Sun if (record__output_max_size_exceeded(rec) && !done) { 2526d575816SJiwei Sun fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB)," 2536d575816SJiwei Sun " stopping session ]\n", 254ae9c7242SAlexey Bayduraev record__bytes_written(rec) >> 10); 2556d575816SJiwei Sun done = 1; 2566d575816SJiwei Sun } 2576d575816SJiwei Sun 258dc0c6127SJiri Olsa if (switch_output_size(rec)) 259dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 260dc0c6127SJiri Olsa 2618d3eca20SDavid Ahern return 0; 262f5970550SPeter Zijlstra } 263f5970550SPeter Zijlstra 264ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec); 265ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec); 26675f5f1fcSAlexey Bayduraev static size_t zstd_compress(struct perf_session *session, struct mmap *map, 26775f5f1fcSAlexey Bayduraev void *dst, size_t dst_size, void *src, size_t src_size); 2685d7f4116SAlexey Budankov 269d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 270d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd, 271d3d1af6fSAlexey Budankov void *buf, size_t size, off_t off) 272d3d1af6fSAlexey Budankov { 273d3d1af6fSAlexey Budankov int rc; 274d3d1af6fSAlexey Budankov 275d3d1af6fSAlexey Budankov cblock->aio_fildes = trace_fd; 276d3d1af6fSAlexey Budankov cblock->aio_buf = buf; 277d3d1af6fSAlexey Budankov cblock->aio_nbytes = size; 278d3d1af6fSAlexey Budankov cblock->aio_offset = off; 279d3d1af6fSAlexey Budankov cblock->aio_sigevent.sigev_notify = SIGEV_NONE; 280d3d1af6fSAlexey Budankov 281d3d1af6fSAlexey Budankov do { 282d3d1af6fSAlexey Budankov rc = aio_write(cblock); 283d3d1af6fSAlexey Budankov if (rc == 0) { 284d3d1af6fSAlexey Budankov break; 285d3d1af6fSAlexey Budankov } else if (errno != EAGAIN) { 286d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 287d3d1af6fSAlexey Budankov pr_err("failed to queue perf data, error: %m\n"); 288d3d1af6fSAlexey Budankov break; 289d3d1af6fSAlexey Budankov } 290d3d1af6fSAlexey Budankov } while (1); 291d3d1af6fSAlexey Budankov 292d3d1af6fSAlexey Budankov return rc; 293d3d1af6fSAlexey Budankov } 294d3d1af6fSAlexey Budankov 295a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock) 296d3d1af6fSAlexey Budankov { 297d3d1af6fSAlexey Budankov void *rem_buf; 298d3d1af6fSAlexey Budankov off_t rem_off; 299d3d1af6fSAlexey Budankov size_t rem_size; 300d3d1af6fSAlexey Budankov int rc, aio_errno; 301d3d1af6fSAlexey Budankov ssize_t aio_ret, written; 302d3d1af6fSAlexey Budankov 303d3d1af6fSAlexey Budankov aio_errno = aio_error(cblock); 304d3d1af6fSAlexey Budankov if (aio_errno == EINPROGRESS) 305d3d1af6fSAlexey Budankov return 0; 306d3d1af6fSAlexey Budankov 307d3d1af6fSAlexey Budankov written = aio_ret = aio_return(cblock); 308d3d1af6fSAlexey Budankov if (aio_ret < 0) { 309d3d1af6fSAlexey Budankov if (aio_errno != EINTR) 310d3d1af6fSAlexey Budankov pr_err("failed to write perf data, error: %m\n"); 311d3d1af6fSAlexey Budankov written = 0; 312d3d1af6fSAlexey Budankov } 313d3d1af6fSAlexey Budankov 314d3d1af6fSAlexey Budankov rem_size = cblock->aio_nbytes - written; 315d3d1af6fSAlexey Budankov 316d3d1af6fSAlexey Budankov if (rem_size == 0) { 317d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 318d3d1af6fSAlexey Budankov /* 319ef781128SAlexey Budankov * md->refcount is incremented in record__aio_pushfn() for 320ef781128SAlexey Budankov * every aio write request started in record__aio_push() so 321ef781128SAlexey Budankov * decrement it because the request is now complete. 322d3d1af6fSAlexey Budankov */ 32380e53d11SJiri Olsa perf_mmap__put(&md->core); 324d3d1af6fSAlexey Budankov rc = 1; 325d3d1af6fSAlexey Budankov } else { 326d3d1af6fSAlexey Budankov /* 327d3d1af6fSAlexey Budankov * aio write request may require restart with the 328d3d1af6fSAlexey Budankov * reminder if the kernel didn't write whole 329d3d1af6fSAlexey Budankov * chunk at once. 330d3d1af6fSAlexey Budankov */ 331d3d1af6fSAlexey Budankov rem_off = cblock->aio_offset + written; 332d3d1af6fSAlexey Budankov rem_buf = (void *)(cblock->aio_buf + written); 333d3d1af6fSAlexey Budankov record__aio_write(cblock, cblock->aio_fildes, 334d3d1af6fSAlexey Budankov rem_buf, rem_size, rem_off); 335d3d1af6fSAlexey Budankov rc = 0; 336d3d1af6fSAlexey Budankov } 337d3d1af6fSAlexey Budankov 338d3d1af6fSAlexey Budankov return rc; 339d3d1af6fSAlexey Budankov } 340d3d1af6fSAlexey Budankov 341a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all) 342d3d1af6fSAlexey Budankov { 34393f20c0fSAlexey Budankov struct aiocb **aiocb = md->aio.aiocb; 34493f20c0fSAlexey Budankov struct aiocb *cblocks = md->aio.cblocks; 345d3d1af6fSAlexey Budankov struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */ 34693f20c0fSAlexey Budankov int i, do_suspend; 347d3d1af6fSAlexey Budankov 348d3d1af6fSAlexey Budankov do { 34993f20c0fSAlexey Budankov do_suspend = 0; 35093f20c0fSAlexey Budankov for (i = 0; i < md->aio.nr_cblocks; ++i) { 35193f20c0fSAlexey Budankov if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) { 35293f20c0fSAlexey Budankov if (sync_all) 35393f20c0fSAlexey Budankov aiocb[i] = NULL; 35493f20c0fSAlexey Budankov else 35593f20c0fSAlexey Budankov return i; 35693f20c0fSAlexey Budankov } else { 35793f20c0fSAlexey Budankov /* 35893f20c0fSAlexey Budankov * Started aio write is not complete yet 35993f20c0fSAlexey Budankov * so it has to be waited before the 36093f20c0fSAlexey Budankov * next allocation. 36193f20c0fSAlexey Budankov */ 36293f20c0fSAlexey Budankov aiocb[i] = &cblocks[i]; 36393f20c0fSAlexey Budankov do_suspend = 1; 36493f20c0fSAlexey Budankov } 36593f20c0fSAlexey Budankov } 36693f20c0fSAlexey Budankov if (!do_suspend) 36793f20c0fSAlexey Budankov return -1; 368d3d1af6fSAlexey Budankov 36993f20c0fSAlexey Budankov while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) { 370d3d1af6fSAlexey Budankov if (!(errno == EAGAIN || errno == EINTR)) 371d3d1af6fSAlexey Budankov pr_err("failed to sync perf data, error: %m\n"); 372d3d1af6fSAlexey Budankov } 373d3d1af6fSAlexey Budankov } while (1); 374d3d1af6fSAlexey Budankov } 375d3d1af6fSAlexey Budankov 376ef781128SAlexey Budankov struct record_aio { 377ef781128SAlexey Budankov struct record *rec; 378ef781128SAlexey Budankov void *data; 379ef781128SAlexey Budankov size_t size; 380ef781128SAlexey Budankov }; 381ef781128SAlexey Budankov 382a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size) 383d3d1af6fSAlexey Budankov { 384ef781128SAlexey Budankov struct record_aio *aio = to; 385ef781128SAlexey Budankov 386ef781128SAlexey Budankov /* 387547740f7SJiri Olsa * map->core.base data pointed by buf is copied into free map->aio.data[] buffer 388ef781128SAlexey Budankov * to release space in the kernel buffer as fast as possible, calling 389ef781128SAlexey Budankov * perf_mmap__consume() from perf_mmap__push() function. 390ef781128SAlexey Budankov * 391ef781128SAlexey Budankov * That lets the kernel to proceed with storing more profiling data into 392ef781128SAlexey Budankov * the kernel buffer earlier than other per-cpu kernel buffers are handled. 393ef781128SAlexey Budankov * 394ef781128SAlexey Budankov * Coping can be done in two steps in case the chunk of profiling data 395ef781128SAlexey Budankov * crosses the upper bound of the kernel buffer. In this case we first move 396ef781128SAlexey Budankov * part of data from map->start till the upper bound and then the reminder 397ef781128SAlexey Budankov * from the beginning of the kernel buffer till the end of the data chunk. 398ef781128SAlexey Budankov */ 399ef781128SAlexey Budankov 400ef781128SAlexey Budankov if (record__comp_enabled(aio->rec)) { 40175f5f1fcSAlexey Bayduraev size = zstd_compress(aio->rec->session, NULL, aio->data + aio->size, 402bf59b305SJiri Olsa mmap__mmap_len(map) - aio->size, 403ef781128SAlexey Budankov buf, size); 404ef781128SAlexey Budankov } else { 405ef781128SAlexey Budankov memcpy(aio->data + aio->size, buf, size); 406ef781128SAlexey Budankov } 407ef781128SAlexey Budankov 408ef781128SAlexey Budankov if (!aio->size) { 409ef781128SAlexey Budankov /* 410ef781128SAlexey Budankov * Increment map->refcount to guard map->aio.data[] buffer 411ef781128SAlexey Budankov * from premature deallocation because map object can be 412ef781128SAlexey Budankov * released earlier than aio write request started on 413ef781128SAlexey Budankov * map->aio.data[] buffer is complete. 414ef781128SAlexey Budankov * 415ef781128SAlexey Budankov * perf_mmap__put() is done at record__aio_complete() 416ef781128SAlexey Budankov * after started aio request completion or at record__aio_push() 417ef781128SAlexey Budankov * if the request failed to start. 418ef781128SAlexey Budankov */ 419e75710f0SJiri Olsa perf_mmap__get(&map->core); 420ef781128SAlexey Budankov } 421ef781128SAlexey Budankov 422ef781128SAlexey Budankov aio->size += size; 423ef781128SAlexey Budankov 424ef781128SAlexey Budankov return size; 425ef781128SAlexey Budankov } 426ef781128SAlexey Budankov 427a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off) 428ef781128SAlexey Budankov { 429ef781128SAlexey Budankov int ret, idx; 430ef781128SAlexey Budankov int trace_fd = rec->session->data->file.fd; 431ef781128SAlexey Budankov struct record_aio aio = { .rec = rec, .size = 0 }; 432ef781128SAlexey Budankov 433ef781128SAlexey Budankov /* 434ef781128SAlexey Budankov * Call record__aio_sync() to wait till map->aio.data[] buffer 435ef781128SAlexey Budankov * becomes available after previous aio write operation. 436ef781128SAlexey Budankov */ 437ef781128SAlexey Budankov 438ef781128SAlexey Budankov idx = record__aio_sync(map, false); 439ef781128SAlexey Budankov aio.data = map->aio.data[idx]; 440ef781128SAlexey Budankov ret = perf_mmap__push(map, &aio, record__aio_pushfn); 441ef781128SAlexey Budankov if (ret != 0) /* ret > 0 - no data, ret < 0 - error */ 442ef781128SAlexey Budankov return ret; 443d3d1af6fSAlexey Budankov 444d3d1af6fSAlexey Budankov rec->samples++; 445ef781128SAlexey Budankov ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off); 446d3d1af6fSAlexey Budankov if (!ret) { 447ef781128SAlexey Budankov *off += aio.size; 448ef781128SAlexey Budankov rec->bytes_written += aio.size; 449d3d1af6fSAlexey Budankov if (switch_output_size(rec)) 450d3d1af6fSAlexey Budankov trigger_hit(&switch_output_trigger); 451ef781128SAlexey Budankov } else { 452ef781128SAlexey Budankov /* 453ef781128SAlexey Budankov * Decrement map->refcount incremented in record__aio_pushfn() 454ef781128SAlexey Budankov * back if record__aio_write() operation failed to start, otherwise 455ef781128SAlexey Budankov * map->refcount is decremented in record__aio_complete() after 456ef781128SAlexey Budankov * aio write operation finishes successfully. 457ef781128SAlexey Budankov */ 45880e53d11SJiri Olsa perf_mmap__put(&map->core); 459d3d1af6fSAlexey Budankov } 460d3d1af6fSAlexey Budankov 461d3d1af6fSAlexey Budankov return ret; 462d3d1af6fSAlexey Budankov } 463d3d1af6fSAlexey Budankov 464d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd) 465d3d1af6fSAlexey Budankov { 466d3d1af6fSAlexey Budankov return lseek(trace_fd, 0, SEEK_CUR); 467d3d1af6fSAlexey Budankov } 468d3d1af6fSAlexey Budankov 469d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos) 470d3d1af6fSAlexey Budankov { 471d3d1af6fSAlexey Budankov lseek(trace_fd, pos, SEEK_SET); 472d3d1af6fSAlexey Budankov } 473d3d1af6fSAlexey Budankov 474d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec) 475d3d1af6fSAlexey Budankov { 476d3d1af6fSAlexey Budankov int i; 47763503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 478a5830532SJiri Olsa struct mmap *maps = evlist->mmap; 479d3d1af6fSAlexey Budankov 480ef781128SAlexey Budankov if (!record__aio_enabled(rec)) 481d3d1af6fSAlexey Budankov return; 482d3d1af6fSAlexey Budankov 483c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 484a5830532SJiri Olsa struct mmap *map = &maps[i]; 485d3d1af6fSAlexey Budankov 486547740f7SJiri Olsa if (map->core.base) 48793f20c0fSAlexey Budankov record__aio_sync(map, true); 488d3d1af6fSAlexey Budankov } 489d3d1af6fSAlexey Budankov } 490d3d1af6fSAlexey Budankov 491d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1; 49293f20c0fSAlexey Budankov static int nr_cblocks_max = 4; 493d3d1af6fSAlexey Budankov 494d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt, 49593f20c0fSAlexey Budankov const char *str, 496d3d1af6fSAlexey Budankov int unset) 497d3d1af6fSAlexey Budankov { 498d3d1af6fSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 499d3d1af6fSAlexey Budankov 50093f20c0fSAlexey Budankov if (unset) { 501d3d1af6fSAlexey Budankov opts->nr_cblocks = 0; 50293f20c0fSAlexey Budankov } else { 50393f20c0fSAlexey Budankov if (str) 50493f20c0fSAlexey Budankov opts->nr_cblocks = strtol(str, NULL, 0); 50593f20c0fSAlexey Budankov if (!opts->nr_cblocks) 506d3d1af6fSAlexey Budankov opts->nr_cblocks = nr_cblocks_default; 50793f20c0fSAlexey Budankov } 508d3d1af6fSAlexey Budankov 509d3d1af6fSAlexey Budankov return 0; 510d3d1af6fSAlexey Budankov } 511d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */ 51293f20c0fSAlexey Budankov static int nr_cblocks_max = 0; 51393f20c0fSAlexey Budankov 514a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused, 515ef781128SAlexey Budankov off_t *off __maybe_unused) 516d3d1af6fSAlexey Budankov { 517d3d1af6fSAlexey Budankov return -1; 518d3d1af6fSAlexey Budankov } 519d3d1af6fSAlexey Budankov 520d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused) 521d3d1af6fSAlexey Budankov { 522d3d1af6fSAlexey Budankov return -1; 523d3d1af6fSAlexey Budankov } 524d3d1af6fSAlexey Budankov 525d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused) 526d3d1af6fSAlexey Budankov { 527d3d1af6fSAlexey Budankov } 528d3d1af6fSAlexey Budankov 529d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused) 530d3d1af6fSAlexey Budankov { 531d3d1af6fSAlexey Budankov } 532d3d1af6fSAlexey Budankov #endif 533d3d1af6fSAlexey Budankov 534d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec) 535d3d1af6fSAlexey Budankov { 536d3d1af6fSAlexey Budankov return rec->opts.nr_cblocks > 0; 537d3d1af6fSAlexey Budankov } 538d3d1af6fSAlexey Budankov 539470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1 540470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt, 541470530bbSAlexey Budankov const char *str, 542470530bbSAlexey Budankov int unset) 543470530bbSAlexey Budankov { 544470530bbSAlexey Budankov int flush_max; 545470530bbSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 546470530bbSAlexey Budankov static struct parse_tag tags[] = { 547470530bbSAlexey Budankov { .tag = 'B', .mult = 1 }, 548470530bbSAlexey Budankov { .tag = 'K', .mult = 1 << 10 }, 549470530bbSAlexey Budankov { .tag = 'M', .mult = 1 << 20 }, 550470530bbSAlexey Budankov { .tag = 'G', .mult = 1 << 30 }, 551470530bbSAlexey Budankov { .tag = 0 }, 552470530bbSAlexey Budankov }; 553470530bbSAlexey Budankov 554470530bbSAlexey Budankov if (unset) 555470530bbSAlexey Budankov return 0; 556470530bbSAlexey Budankov 557470530bbSAlexey Budankov if (str) { 558470530bbSAlexey Budankov opts->mmap_flush = parse_tag_value(str, tags); 559470530bbSAlexey Budankov if (opts->mmap_flush == (int)-1) 560470530bbSAlexey Budankov opts->mmap_flush = strtol(str, NULL, 0); 561470530bbSAlexey Budankov } 562470530bbSAlexey Budankov 563470530bbSAlexey Budankov if (!opts->mmap_flush) 564470530bbSAlexey Budankov opts->mmap_flush = MMAP_FLUSH_DEFAULT; 565470530bbSAlexey Budankov 5669521b5f2SJiri Olsa flush_max = evlist__mmap_size(opts->mmap_pages); 567470530bbSAlexey Budankov flush_max /= 4; 568470530bbSAlexey Budankov if (opts->mmap_flush > flush_max) 569470530bbSAlexey Budankov opts->mmap_flush = flush_max; 570470530bbSAlexey Budankov 571470530bbSAlexey Budankov return 0; 572470530bbSAlexey Budankov } 573470530bbSAlexey Budankov 574504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 575504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1; 576504c1ad1SAlexey Budankov 577504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset) 578504c1ad1SAlexey Budankov { 579504c1ad1SAlexey Budankov struct record_opts *opts = opt->value; 580504c1ad1SAlexey Budankov 581504c1ad1SAlexey Budankov if (unset) { 582504c1ad1SAlexey Budankov opts->comp_level = 0; 583504c1ad1SAlexey Budankov } else { 584504c1ad1SAlexey Budankov if (str) 585504c1ad1SAlexey Budankov opts->comp_level = strtol(str, NULL, 0); 586504c1ad1SAlexey Budankov if (!opts->comp_level) 587504c1ad1SAlexey Budankov opts->comp_level = comp_level_default; 588504c1ad1SAlexey Budankov } 589504c1ad1SAlexey Budankov 590504c1ad1SAlexey Budankov return 0; 591504c1ad1SAlexey Budankov } 592504c1ad1SAlexey Budankov #endif 59351255a8aSAlexey Budankov static unsigned int comp_level_max = 22; 59451255a8aSAlexey Budankov 59542e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec) 59642e1fd80SAlexey Budankov { 59742e1fd80SAlexey Budankov return rec->opts.comp_level > 0; 59842e1fd80SAlexey Budankov } 59942e1fd80SAlexey Budankov 60045694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 601d20deb64SArnaldo Carvalho de Melo union perf_event *event, 6021d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 6031d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 604234fbbf5SArnaldo Carvalho de Melo { 6058c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 606ded2b8feSJiri Olsa return record__write(rec, NULL, event, event->header.size); 607234fbbf5SArnaldo Carvalho de Melo } 608234fbbf5SArnaldo Carvalho de Melo 609d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool, 610d99c22eaSStephane Eranian union perf_event *event, 611d99c22eaSStephane Eranian struct perf_sample *sample __maybe_unused, 612d99c22eaSStephane Eranian struct machine *machine __maybe_unused) 613d99c22eaSStephane Eranian { 614d99c22eaSStephane Eranian static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER; 615d99c22eaSStephane Eranian int ret; 616d99c22eaSStephane Eranian 617d99c22eaSStephane Eranian pthread_mutex_lock(&synth_lock); 618d99c22eaSStephane Eranian ret = process_synthesized_event(tool, event, sample, machine); 619d99c22eaSStephane Eranian pthread_mutex_unlock(&synth_lock); 620d99c22eaSStephane Eranian return ret; 621d99c22eaSStephane Eranian } 622d99c22eaSStephane Eranian 623a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size) 624d37f1586SArnaldo Carvalho de Melo { 625d37f1586SArnaldo Carvalho de Melo struct record *rec = to; 626d37f1586SArnaldo Carvalho de Melo 6275d7f4116SAlexey Budankov if (record__comp_enabled(rec)) { 62875f5f1fcSAlexey Bayduraev size = zstd_compress(rec->session, map, map->data, mmap__mmap_len(map), bf, size); 6295d7f4116SAlexey Budankov bf = map->data; 6305d7f4116SAlexey Budankov } 6315d7f4116SAlexey Budankov 632396b626bSAlexey Bayduraev thread->samples++; 633ded2b8feSJiri Olsa return record__write(rec, map, bf, size); 634d37f1586SArnaldo Carvalho de Melo } 635d37f1586SArnaldo Carvalho de Melo 6362dd6d8a1SAdrian Hunter static volatile int signr = -1; 6372dd6d8a1SAdrian Hunter static volatile int child_finished; 638da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 639da231338SAnand K Mistry static int done_fd = -1; 640da231338SAnand K Mistry #endif 641c0bdc1c4SWang Nan 6422dd6d8a1SAdrian Hunter static void sig_handler(int sig) 6432dd6d8a1SAdrian Hunter { 6442dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 6452dd6d8a1SAdrian Hunter child_finished = 1; 6462dd6d8a1SAdrian Hunter else 6472dd6d8a1SAdrian Hunter signr = sig; 6482dd6d8a1SAdrian Hunter 6492dd6d8a1SAdrian Hunter done = 1; 650da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 651da231338SAnand K Mistry { 652da231338SAnand K Mistry u64 tmp = 1; 653da231338SAnand K Mistry /* 654da231338SAnand K Mistry * It is possible for this signal handler to run after done is checked 655da231338SAnand K Mistry * in the main loop, but before the perf counter fds are polled. If this 656da231338SAnand K Mistry * happens, the poll() will continue to wait even though done is set, 657da231338SAnand K Mistry * and will only break out if either another signal is received, or the 658da231338SAnand K Mistry * counters are ready for read. To ensure the poll() doesn't sleep when 659da231338SAnand K Mistry * done is set, use an eventfd (done_fd) to wake up the poll(). 660da231338SAnand K Mistry */ 661da231338SAnand K Mistry if (write(done_fd, &tmp, sizeof(tmp)) < 0) 662da231338SAnand K Mistry pr_err("failed to signal wakeup fd, error: %m\n"); 663da231338SAnand K Mistry } 664da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT 6652dd6d8a1SAdrian Hunter } 6662dd6d8a1SAdrian Hunter 667a074865eSWang Nan static void sigsegv_handler(int sig) 668a074865eSWang Nan { 669a074865eSWang Nan perf_hooks__recover(); 670a074865eSWang Nan sighandler_dump_stack(sig); 671a074865eSWang Nan } 672a074865eSWang Nan 6732dd6d8a1SAdrian Hunter static void record__sig_exit(void) 6742dd6d8a1SAdrian Hunter { 6752dd6d8a1SAdrian Hunter if (signr == -1) 6762dd6d8a1SAdrian Hunter return; 6772dd6d8a1SAdrian Hunter 6782dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 6792dd6d8a1SAdrian Hunter raise(signr); 6802dd6d8a1SAdrian Hunter } 6812dd6d8a1SAdrian Hunter 682e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 683e31f0d01SAdrian Hunter 684ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 685a5830532SJiri Olsa struct mmap *map, 686ef149c25SAdrian Hunter union perf_event *event, void *data1, 687ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 688ef149c25SAdrian Hunter { 689ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 6908ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 691ef149c25SAdrian Hunter size_t padding; 692ef149c25SAdrian Hunter u8 pad[8] = {0}; 693ef149c25SAdrian Hunter 69446e201efSAdrian Hunter if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) { 69599fa2984SAdrian Hunter off_t file_offset; 6968ceb41d7SJiri Olsa int fd = perf_data__fd(data); 69799fa2984SAdrian Hunter int err; 69899fa2984SAdrian Hunter 69999fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 70099fa2984SAdrian Hunter if (file_offset == -1) 70199fa2984SAdrian Hunter return -1; 70299fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 70399fa2984SAdrian Hunter event, file_offset); 70499fa2984SAdrian Hunter if (err) 70599fa2984SAdrian Hunter return err; 70699fa2984SAdrian Hunter } 70799fa2984SAdrian Hunter 708ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 709ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 710ef149c25SAdrian Hunter if (padding) 711ef149c25SAdrian Hunter padding = 8 - padding; 712ef149c25SAdrian Hunter 713ded2b8feSJiri Olsa record__write(rec, map, event, event->header.size); 714ded2b8feSJiri Olsa record__write(rec, map, data1, len1); 715ef149c25SAdrian Hunter if (len2) 716ded2b8feSJiri Olsa record__write(rec, map, data2, len2); 717ded2b8feSJiri Olsa record__write(rec, map, &pad, padding); 718ef149c25SAdrian Hunter 719ef149c25SAdrian Hunter return 0; 720ef149c25SAdrian Hunter } 721ef149c25SAdrian Hunter 722ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 723a5830532SJiri Olsa struct mmap *map) 724ef149c25SAdrian Hunter { 725ef149c25SAdrian Hunter int ret; 726ef149c25SAdrian Hunter 727e035f4caSJiri Olsa ret = auxtrace_mmap__read(map, rec->itr, &rec->tool, 728ef149c25SAdrian Hunter record__process_auxtrace); 729ef149c25SAdrian Hunter if (ret < 0) 730ef149c25SAdrian Hunter return ret; 731ef149c25SAdrian Hunter 732ef149c25SAdrian Hunter if (ret) 733ef149c25SAdrian Hunter rec->samples++; 734ef149c25SAdrian Hunter 735ef149c25SAdrian Hunter return 0; 736ef149c25SAdrian Hunter } 737ef149c25SAdrian Hunter 7382dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 739a5830532SJiri Olsa struct mmap *map) 7402dd6d8a1SAdrian Hunter { 7412dd6d8a1SAdrian Hunter int ret; 7422dd6d8a1SAdrian Hunter 743e035f4caSJiri Olsa ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool, 7442dd6d8a1SAdrian Hunter record__process_auxtrace, 7452dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 7462dd6d8a1SAdrian Hunter if (ret < 0) 7472dd6d8a1SAdrian Hunter return ret; 7482dd6d8a1SAdrian Hunter 7492dd6d8a1SAdrian Hunter if (ret) 7502dd6d8a1SAdrian Hunter rec->samples++; 7512dd6d8a1SAdrian Hunter 7522dd6d8a1SAdrian Hunter return 0; 7532dd6d8a1SAdrian Hunter } 7542dd6d8a1SAdrian Hunter 7552dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 7562dd6d8a1SAdrian Hunter { 7572dd6d8a1SAdrian Hunter int i; 7582dd6d8a1SAdrian Hunter int rc = 0; 7592dd6d8a1SAdrian Hunter 760c976ee11SJiri Olsa for (i = 0; i < rec->evlist->core.nr_mmaps; i++) { 761a5830532SJiri Olsa struct mmap *map = &rec->evlist->mmap[i]; 7622dd6d8a1SAdrian Hunter 763e035f4caSJiri Olsa if (!map->auxtrace_mmap.base) 7642dd6d8a1SAdrian Hunter continue; 7652dd6d8a1SAdrian Hunter 766e035f4caSJiri Olsa if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) { 7672dd6d8a1SAdrian Hunter rc = -1; 7682dd6d8a1SAdrian Hunter goto out; 7692dd6d8a1SAdrian Hunter } 7702dd6d8a1SAdrian Hunter } 7712dd6d8a1SAdrian Hunter out: 7722dd6d8a1SAdrian Hunter return rc; 7732dd6d8a1SAdrian Hunter } 7742dd6d8a1SAdrian Hunter 775ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit) 7762dd6d8a1SAdrian Hunter { 7772dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 7782dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 7795f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 7802dd6d8a1SAdrian Hunter } else { 781ce7b0e42SAlexander Shishkin if (auxtrace_record__snapshot_finish(rec->itr, on_exit)) 7825f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 7835f9cf599SWang Nan else 7845f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 7852dd6d8a1SAdrian Hunter } 7862dd6d8a1SAdrian Hunter } 7872dd6d8a1SAdrian Hunter 788ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec) 789ce7b0e42SAlexander Shishkin { 790ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 791ce7b0e42SAlexander Shishkin return 0; 792ce7b0e42SAlexander Shishkin 793ce7b0e42SAlexander Shishkin if (!auxtrace_record__snapshot_started && 794ce7b0e42SAlexander Shishkin auxtrace_record__snapshot_start(rec->itr)) 795ce7b0e42SAlexander Shishkin return -1; 796ce7b0e42SAlexander Shishkin 797ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, true); 798ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 799ce7b0e42SAlexander Shishkin return -1; 800ce7b0e42SAlexander Shishkin 801ce7b0e42SAlexander Shishkin return 0; 802ce7b0e42SAlexander Shishkin } 803ce7b0e42SAlexander Shishkin 8044b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec) 8054b5ea3bdSAdrian Hunter { 8064b5ea3bdSAdrian Hunter int err; 8074b5ea3bdSAdrian Hunter 808b5f2511dSAlexey Bayduraev if ((rec->opts.auxtrace_snapshot_opts || rec->opts.auxtrace_sample_opts) 809b5f2511dSAlexey Bayduraev && record__threads_enabled(rec)) { 810b5f2511dSAlexey Bayduraev pr_err("AUX area tracing options are not available in parallel streaming mode.\n"); 811b5f2511dSAlexey Bayduraev return -EINVAL; 812b5f2511dSAlexey Bayduraev } 813b5f2511dSAlexey Bayduraev 8144b5ea3bdSAdrian Hunter if (!rec->itr) { 8154b5ea3bdSAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 8164b5ea3bdSAdrian Hunter if (err) 8174b5ea3bdSAdrian Hunter return err; 8184b5ea3bdSAdrian Hunter } 8194b5ea3bdSAdrian Hunter 8204b5ea3bdSAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 8214b5ea3bdSAdrian Hunter rec->opts.auxtrace_snapshot_opts); 8224b5ea3bdSAdrian Hunter if (err) 8234b5ea3bdSAdrian Hunter return err; 8244b5ea3bdSAdrian Hunter 825c0a6de06SAdrian Hunter err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts, 826c0a6de06SAdrian Hunter rec->opts.auxtrace_sample_opts); 827c0a6de06SAdrian Hunter if (err) 828c0a6de06SAdrian Hunter return err; 829c0a6de06SAdrian Hunter 830d58b3f7eSAdrian Hunter auxtrace_regroup_aux_output(rec->evlist); 831d58b3f7eSAdrian Hunter 8324b5ea3bdSAdrian Hunter return auxtrace_parse_filters(rec->evlist); 8334b5ea3bdSAdrian Hunter } 8344b5ea3bdSAdrian Hunter 835e31f0d01SAdrian Hunter #else 836e31f0d01SAdrian Hunter 837e31f0d01SAdrian Hunter static inline 838e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 839a5830532SJiri Olsa struct mmap *map __maybe_unused) 840e31f0d01SAdrian Hunter { 841e31f0d01SAdrian Hunter return 0; 842e31f0d01SAdrian Hunter } 843e31f0d01SAdrian Hunter 8442dd6d8a1SAdrian Hunter static inline 845ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused, 846ce7b0e42SAlexander Shishkin bool on_exit __maybe_unused) 8472dd6d8a1SAdrian Hunter { 8482dd6d8a1SAdrian Hunter } 8492dd6d8a1SAdrian Hunter 8502dd6d8a1SAdrian Hunter static inline 8512dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 8522dd6d8a1SAdrian Hunter { 8532dd6d8a1SAdrian Hunter return 0; 8542dd6d8a1SAdrian Hunter } 8552dd6d8a1SAdrian Hunter 856ce7b0e42SAlexander Shishkin static inline 857ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused) 858ce7b0e42SAlexander Shishkin { 859ce7b0e42SAlexander Shishkin return 0; 860ce7b0e42SAlexander Shishkin } 861ce7b0e42SAlexander Shishkin 8624b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused) 8634b5ea3bdSAdrian Hunter { 8644b5ea3bdSAdrian Hunter return 0; 8654b5ea3bdSAdrian Hunter } 8664b5ea3bdSAdrian Hunter 867e31f0d01SAdrian Hunter #endif 868e31f0d01SAdrian Hunter 869246eba8eSAdrian Hunter static int record__config_text_poke(struct evlist *evlist) 870246eba8eSAdrian Hunter { 871246eba8eSAdrian Hunter struct evsel *evsel; 872246eba8eSAdrian Hunter int err; 873246eba8eSAdrian Hunter 874246eba8eSAdrian Hunter /* Nothing to do if text poke is already configured */ 875246eba8eSAdrian Hunter evlist__for_each_entry(evlist, evsel) { 876246eba8eSAdrian Hunter if (evsel->core.attr.text_poke) 877246eba8eSAdrian Hunter return 0; 878246eba8eSAdrian Hunter } 879246eba8eSAdrian Hunter 880246eba8eSAdrian Hunter err = parse_events(evlist, "dummy:u", NULL); 881246eba8eSAdrian Hunter if (err) 882246eba8eSAdrian Hunter return err; 883246eba8eSAdrian Hunter 884246eba8eSAdrian Hunter evsel = evlist__last(evlist); 885246eba8eSAdrian Hunter 886246eba8eSAdrian Hunter evsel->core.attr.freq = 0; 887246eba8eSAdrian Hunter evsel->core.attr.sample_period = 1; 888246eba8eSAdrian Hunter evsel->core.attr.text_poke = 1; 889246eba8eSAdrian Hunter evsel->core.attr.ksymbol = 1; 890246eba8eSAdrian Hunter 891246eba8eSAdrian Hunter evsel->core.system_wide = true; 892246eba8eSAdrian Hunter evsel->no_aux_samples = true; 893246eba8eSAdrian Hunter evsel->immediate = true; 894246eba8eSAdrian Hunter 895246eba8eSAdrian Hunter /* Text poke must be collected on all CPUs */ 896246eba8eSAdrian Hunter perf_cpu_map__put(evsel->core.own_cpus); 897246eba8eSAdrian Hunter evsel->core.own_cpus = perf_cpu_map__new(NULL); 898246eba8eSAdrian Hunter perf_cpu_map__put(evsel->core.cpus); 899246eba8eSAdrian Hunter evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus); 900246eba8eSAdrian Hunter 901246eba8eSAdrian Hunter evsel__set_sample_bit(evsel, TIME); 902246eba8eSAdrian Hunter 903246eba8eSAdrian Hunter return 0; 904246eba8eSAdrian Hunter } 905246eba8eSAdrian Hunter 906eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine) 907eeb399b5SAdrian Hunter { 908eeb399b5SAdrian Hunter char kcore[PATH_MAX]; 909eeb399b5SAdrian Hunter int fd; 910eeb399b5SAdrian Hunter 911eeb399b5SAdrian Hunter scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir); 912eeb399b5SAdrian Hunter 913eeb399b5SAdrian Hunter fd = open(kcore, O_RDONLY); 914eeb399b5SAdrian Hunter if (fd < 0) 915eeb399b5SAdrian Hunter return false; 916eeb399b5SAdrian Hunter 917eeb399b5SAdrian Hunter close(fd); 918eeb399b5SAdrian Hunter 919eeb399b5SAdrian Hunter return true; 920eeb399b5SAdrian Hunter } 921eeb399b5SAdrian Hunter 922eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data) 923eeb399b5SAdrian Hunter { 924eeb399b5SAdrian Hunter char from_dir[PATH_MAX]; 925eeb399b5SAdrian Hunter char kcore_dir[PATH_MAX]; 926eeb399b5SAdrian Hunter int ret; 927eeb399b5SAdrian Hunter 928eeb399b5SAdrian Hunter snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir); 929eeb399b5SAdrian Hunter 930eeb399b5SAdrian Hunter ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir)); 931eeb399b5SAdrian Hunter if (ret) 932eeb399b5SAdrian Hunter return ret; 933eeb399b5SAdrian Hunter 934eeb399b5SAdrian Hunter return kcore_copy(from_dir, kcore_dir); 935eeb399b5SAdrian Hunter } 936eeb399b5SAdrian Hunter 937415ccb58SAlexey Bayduraev static void record__thread_data_init_pipes(struct record_thread *thread_data) 938415ccb58SAlexey Bayduraev { 939415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 940415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 941415ccb58SAlexey Bayduraev thread_data->pipes.ack[0] = -1; 942415ccb58SAlexey Bayduraev thread_data->pipes.ack[1] = -1; 943415ccb58SAlexey Bayduraev } 944415ccb58SAlexey Bayduraev 945415ccb58SAlexey Bayduraev static int record__thread_data_open_pipes(struct record_thread *thread_data) 946415ccb58SAlexey Bayduraev { 947415ccb58SAlexey Bayduraev if (pipe(thread_data->pipes.msg)) 948415ccb58SAlexey Bayduraev return -EINVAL; 949415ccb58SAlexey Bayduraev 950415ccb58SAlexey Bayduraev if (pipe(thread_data->pipes.ack)) { 951415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[0]); 952415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 953415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[1]); 954415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 955415ccb58SAlexey Bayduraev return -EINVAL; 956415ccb58SAlexey Bayduraev } 957415ccb58SAlexey Bayduraev 958415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: msg=[%d,%d], ack=[%d,%d]\n", thread_data, 959415ccb58SAlexey Bayduraev thread_data->pipes.msg[0], thread_data->pipes.msg[1], 960415ccb58SAlexey Bayduraev thread_data->pipes.ack[0], thread_data->pipes.ack[1]); 961415ccb58SAlexey Bayduraev 962415ccb58SAlexey Bayduraev return 0; 963415ccb58SAlexey Bayduraev } 964415ccb58SAlexey Bayduraev 965415ccb58SAlexey Bayduraev static void record__thread_data_close_pipes(struct record_thread *thread_data) 966415ccb58SAlexey Bayduraev { 967415ccb58SAlexey Bayduraev if (thread_data->pipes.msg[0] != -1) { 968415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[0]); 969415ccb58SAlexey Bayduraev thread_data->pipes.msg[0] = -1; 970415ccb58SAlexey Bayduraev } 971415ccb58SAlexey Bayduraev if (thread_data->pipes.msg[1] != -1) { 972415ccb58SAlexey Bayduraev close(thread_data->pipes.msg[1]); 973415ccb58SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 974415ccb58SAlexey Bayduraev } 975415ccb58SAlexey Bayduraev if (thread_data->pipes.ack[0] != -1) { 976415ccb58SAlexey Bayduraev close(thread_data->pipes.ack[0]); 977415ccb58SAlexey Bayduraev thread_data->pipes.ack[0] = -1; 978415ccb58SAlexey Bayduraev } 979415ccb58SAlexey Bayduraev if (thread_data->pipes.ack[1] != -1) { 980415ccb58SAlexey Bayduraev close(thread_data->pipes.ack[1]); 981415ccb58SAlexey Bayduraev thread_data->pipes.ack[1] = -1; 982415ccb58SAlexey Bayduraev } 983415ccb58SAlexey Bayduraev } 984415ccb58SAlexey Bayduraev 985415ccb58SAlexey Bayduraev static int record__thread_data_init_maps(struct record_thread *thread_data, struct evlist *evlist) 986415ccb58SAlexey Bayduraev { 987415ccb58SAlexey Bayduraev int m, tm, nr_mmaps = evlist->core.nr_mmaps; 988415ccb58SAlexey Bayduraev struct mmap *mmap = evlist->mmap; 989415ccb58SAlexey Bayduraev struct mmap *overwrite_mmap = evlist->overwrite_mmap; 9900df6ade7SIan Rogers struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; 991415ccb58SAlexey Bayduraev 99223380e4dSAlexey Bayduraev if (cpu_map__is_dummy(cpus)) 99323380e4dSAlexey Bayduraev thread_data->nr_mmaps = nr_mmaps; 99423380e4dSAlexey Bayduraev else 995415ccb58SAlexey Bayduraev thread_data->nr_mmaps = bitmap_weight(thread_data->mask->maps.bits, 996415ccb58SAlexey Bayduraev thread_data->mask->maps.nbits); 997415ccb58SAlexey Bayduraev if (mmap) { 998415ccb58SAlexey Bayduraev thread_data->maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *)); 999415ccb58SAlexey Bayduraev if (!thread_data->maps) 1000415ccb58SAlexey Bayduraev return -ENOMEM; 1001415ccb58SAlexey Bayduraev } 1002415ccb58SAlexey Bayduraev if (overwrite_mmap) { 1003415ccb58SAlexey Bayduraev thread_data->overwrite_maps = zalloc(thread_data->nr_mmaps * sizeof(struct mmap *)); 1004415ccb58SAlexey Bayduraev if (!thread_data->overwrite_maps) { 1005415ccb58SAlexey Bayduraev zfree(&thread_data->maps); 1006415ccb58SAlexey Bayduraev return -ENOMEM; 1007415ccb58SAlexey Bayduraev } 1008415ccb58SAlexey Bayduraev } 1009415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: nr_mmaps=%d, maps=%p, ow_maps=%p\n", thread_data, 1010415ccb58SAlexey Bayduraev thread_data->nr_mmaps, thread_data->maps, thread_data->overwrite_maps); 1011415ccb58SAlexey Bayduraev 1012415ccb58SAlexey Bayduraev for (m = 0, tm = 0; m < nr_mmaps && tm < thread_data->nr_mmaps; m++) { 101323380e4dSAlexey Bayduraev if (cpu_map__is_dummy(cpus) || 1014*0255571aSIan Rogers test_bit(perf_cpu_map__cpu(cpus, m).cpu, thread_data->mask->maps.bits)) { 1015415ccb58SAlexey Bayduraev if (thread_data->maps) { 1016415ccb58SAlexey Bayduraev thread_data->maps[tm] = &mmap[m]; 1017415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: cpu%d: maps[%d] -> mmap[%d]\n", 101823380e4dSAlexey Bayduraev thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m); 1019415ccb58SAlexey Bayduraev } 1020415ccb58SAlexey Bayduraev if (thread_data->overwrite_maps) { 1021415ccb58SAlexey Bayduraev thread_data->overwrite_maps[tm] = &overwrite_mmap[m]; 1022415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: cpu%d: ow_maps[%d] -> ow_mmap[%d]\n", 102323380e4dSAlexey Bayduraev thread_data, perf_cpu_map__cpu(cpus, m).cpu, tm, m); 1024415ccb58SAlexey Bayduraev } 1025415ccb58SAlexey Bayduraev tm++; 1026415ccb58SAlexey Bayduraev } 1027415ccb58SAlexey Bayduraev } 1028415ccb58SAlexey Bayduraev 1029415ccb58SAlexey Bayduraev return 0; 1030415ccb58SAlexey Bayduraev } 1031415ccb58SAlexey Bayduraev 1032415ccb58SAlexey Bayduraev static int record__thread_data_init_pollfd(struct record_thread *thread_data, struct evlist *evlist) 1033415ccb58SAlexey Bayduraev { 1034415ccb58SAlexey Bayduraev int f, tm, pos; 1035415ccb58SAlexey Bayduraev struct mmap *map, *overwrite_map; 1036415ccb58SAlexey Bayduraev 1037415ccb58SAlexey Bayduraev fdarray__init(&thread_data->pollfd, 64); 1038415ccb58SAlexey Bayduraev 1039415ccb58SAlexey Bayduraev for (tm = 0; tm < thread_data->nr_mmaps; tm++) { 1040415ccb58SAlexey Bayduraev map = thread_data->maps ? thread_data->maps[tm] : NULL; 1041415ccb58SAlexey Bayduraev overwrite_map = thread_data->overwrite_maps ? 1042415ccb58SAlexey Bayduraev thread_data->overwrite_maps[tm] : NULL; 1043415ccb58SAlexey Bayduraev 1044415ccb58SAlexey Bayduraev for (f = 0; f < evlist->core.pollfd.nr; f++) { 1045415ccb58SAlexey Bayduraev void *ptr = evlist->core.pollfd.priv[f].ptr; 1046415ccb58SAlexey Bayduraev 1047415ccb58SAlexey Bayduraev if ((map && ptr == map) || (overwrite_map && ptr == overwrite_map)) { 1048415ccb58SAlexey Bayduraev pos = fdarray__dup_entry_from(&thread_data->pollfd, f, 1049415ccb58SAlexey Bayduraev &evlist->core.pollfd); 1050415ccb58SAlexey Bayduraev if (pos < 0) 1051415ccb58SAlexey Bayduraev return pos; 1052415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: pollfd[%d] <- event_fd=%d\n", 1053415ccb58SAlexey Bayduraev thread_data, pos, evlist->core.pollfd.entries[f].fd); 1054415ccb58SAlexey Bayduraev } 1055415ccb58SAlexey Bayduraev } 1056415ccb58SAlexey Bayduraev } 1057415ccb58SAlexey Bayduraev 1058415ccb58SAlexey Bayduraev return 0; 1059415ccb58SAlexey Bayduraev } 1060415ccb58SAlexey Bayduraev 1061415ccb58SAlexey Bayduraev static void record__free_thread_data(struct record *rec) 1062415ccb58SAlexey Bayduraev { 1063415ccb58SAlexey Bayduraev int t; 1064415ccb58SAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 1065415ccb58SAlexey Bayduraev 1066415ccb58SAlexey Bayduraev if (thread_data == NULL) 1067415ccb58SAlexey Bayduraev return; 1068415ccb58SAlexey Bayduraev 1069415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 1070415ccb58SAlexey Bayduraev record__thread_data_close_pipes(&thread_data[t]); 1071415ccb58SAlexey Bayduraev zfree(&thread_data[t].maps); 1072415ccb58SAlexey Bayduraev zfree(&thread_data[t].overwrite_maps); 1073415ccb58SAlexey Bayduraev fdarray__exit(&thread_data[t].pollfd); 1074415ccb58SAlexey Bayduraev } 1075415ccb58SAlexey Bayduraev 1076415ccb58SAlexey Bayduraev zfree(&rec->thread_data); 1077415ccb58SAlexey Bayduraev } 1078415ccb58SAlexey Bayduraev 1079415ccb58SAlexey Bayduraev static int record__alloc_thread_data(struct record *rec, struct evlist *evlist) 1080415ccb58SAlexey Bayduraev { 1081415ccb58SAlexey Bayduraev int t, ret; 1082415ccb58SAlexey Bayduraev struct record_thread *thread_data; 1083415ccb58SAlexey Bayduraev 1084415ccb58SAlexey Bayduraev rec->thread_data = zalloc(rec->nr_threads * sizeof(*(rec->thread_data))); 1085415ccb58SAlexey Bayduraev if (!rec->thread_data) { 1086415ccb58SAlexey Bayduraev pr_err("Failed to allocate thread data\n"); 1087415ccb58SAlexey Bayduraev return -ENOMEM; 1088415ccb58SAlexey Bayduraev } 1089415ccb58SAlexey Bayduraev thread_data = rec->thread_data; 1090415ccb58SAlexey Bayduraev 1091415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 1092415ccb58SAlexey Bayduraev record__thread_data_init_pipes(&thread_data[t]); 1093415ccb58SAlexey Bayduraev 1094415ccb58SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 1095415ccb58SAlexey Bayduraev thread_data[t].rec = rec; 1096415ccb58SAlexey Bayduraev thread_data[t].mask = &rec->thread_masks[t]; 1097415ccb58SAlexey Bayduraev ret = record__thread_data_init_maps(&thread_data[t], evlist); 1098415ccb58SAlexey Bayduraev if (ret) { 1099415ccb58SAlexey Bayduraev pr_err("Failed to initialize thread[%d] maps\n", t); 1100415ccb58SAlexey Bayduraev goto out_free; 1101415ccb58SAlexey Bayduraev } 1102415ccb58SAlexey Bayduraev ret = record__thread_data_init_pollfd(&thread_data[t], evlist); 1103415ccb58SAlexey Bayduraev if (ret) { 1104415ccb58SAlexey Bayduraev pr_err("Failed to initialize thread[%d] pollfd\n", t); 1105415ccb58SAlexey Bayduraev goto out_free; 1106415ccb58SAlexey Bayduraev } 1107415ccb58SAlexey Bayduraev if (t) { 1108415ccb58SAlexey Bayduraev thread_data[t].tid = -1; 1109415ccb58SAlexey Bayduraev ret = record__thread_data_open_pipes(&thread_data[t]); 1110415ccb58SAlexey Bayduraev if (ret) { 1111415ccb58SAlexey Bayduraev pr_err("Failed to open thread[%d] communication pipes\n", t); 1112415ccb58SAlexey Bayduraev goto out_free; 1113415ccb58SAlexey Bayduraev } 1114415ccb58SAlexey Bayduraev ret = fdarray__add(&thread_data[t].pollfd, thread_data[t].pipes.msg[0], 1115415ccb58SAlexey Bayduraev POLLIN | POLLERR | POLLHUP, fdarray_flag__nonfilterable); 1116415ccb58SAlexey Bayduraev if (ret < 0) { 1117415ccb58SAlexey Bayduraev pr_err("Failed to add descriptor to thread[%d] pollfd\n", t); 1118415ccb58SAlexey Bayduraev goto out_free; 1119415ccb58SAlexey Bayduraev } 1120415ccb58SAlexey Bayduraev thread_data[t].ctlfd_pos = ret; 1121415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: pollfd[%d] <- ctl_fd=%d\n", 1122415ccb58SAlexey Bayduraev thread_data, thread_data[t].ctlfd_pos, 1123415ccb58SAlexey Bayduraev thread_data[t].pipes.msg[0]); 1124415ccb58SAlexey Bayduraev } else { 1125415ccb58SAlexey Bayduraev thread_data[t].tid = gettid(); 1126415ccb58SAlexey Bayduraev if (evlist->ctl_fd.pos == -1) 1127415ccb58SAlexey Bayduraev continue; 1128415ccb58SAlexey Bayduraev ret = fdarray__dup_entry_from(&thread_data[t].pollfd, evlist->ctl_fd.pos, 1129415ccb58SAlexey Bayduraev &evlist->core.pollfd); 1130415ccb58SAlexey Bayduraev if (ret < 0) { 1131415ccb58SAlexey Bayduraev pr_err("Failed to duplicate descriptor in main thread pollfd\n"); 1132415ccb58SAlexey Bayduraev goto out_free; 1133415ccb58SAlexey Bayduraev } 1134415ccb58SAlexey Bayduraev thread_data[t].ctlfd_pos = ret; 1135415ccb58SAlexey Bayduraev pr_debug2("thread_data[%p]: pollfd[%d] <- ctl_fd=%d\n", 1136415ccb58SAlexey Bayduraev thread_data, thread_data[t].ctlfd_pos, 1137415ccb58SAlexey Bayduraev evlist->core.pollfd.entries[evlist->ctl_fd.pos].fd); 1138415ccb58SAlexey Bayduraev } 1139415ccb58SAlexey Bayduraev } 1140415ccb58SAlexey Bayduraev 1141415ccb58SAlexey Bayduraev return 0; 1142415ccb58SAlexey Bayduraev 1143415ccb58SAlexey Bayduraev out_free: 1144415ccb58SAlexey Bayduraev record__free_thread_data(rec); 1145415ccb58SAlexey Bayduraev 1146415ccb58SAlexey Bayduraev return ret; 1147415ccb58SAlexey Bayduraev } 1148415ccb58SAlexey Bayduraev 1149cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 115063503dbaSJiri Olsa struct evlist *evlist) 1151cda57a8cSWang Nan { 115256f735ffSAlexey Bayduraev int i, ret; 1153cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 1154c0a6de06SAdrian Hunter bool auxtrace_overwrite = opts->auxtrace_snapshot_mode || 1155c0a6de06SAdrian Hunter opts->auxtrace_sample_mode; 1156cda57a8cSWang Nan char msg[512]; 1157cda57a8cSWang Nan 1158f13de660SAlexey Budankov if (opts->affinity != PERF_AFFINITY_SYS) 1159f13de660SAlexey Budankov cpu__setup_cpunode_map(); 1160f13de660SAlexey Budankov 11619521b5f2SJiri Olsa if (evlist__mmap_ex(evlist, opts->mmap_pages, 1162cda57a8cSWang Nan opts->auxtrace_mmap_pages, 1163c0a6de06SAdrian Hunter auxtrace_overwrite, 1164470530bbSAlexey Budankov opts->nr_cblocks, opts->affinity, 116551255a8aSAlexey Budankov opts->mmap_flush, opts->comp_level) < 0) { 1166cda57a8cSWang Nan if (errno == EPERM) { 1167cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 1168cda57a8cSWang Nan "Consider increasing " 1169cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 1170cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 1171cda57a8cSWang Nan "(current value: %u,%u)\n", 1172cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 1173cda57a8cSWang Nan return -errno; 1174cda57a8cSWang Nan } else { 1175cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 1176c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 1177cda57a8cSWang Nan if (errno) 1178cda57a8cSWang Nan return -errno; 1179cda57a8cSWang Nan else 1180cda57a8cSWang Nan return -EINVAL; 1181cda57a8cSWang Nan } 1182cda57a8cSWang Nan } 1183415ccb58SAlexey Bayduraev 1184415ccb58SAlexey Bayduraev if (evlist__initialize_ctlfd(evlist, opts->ctl_fd, opts->ctl_fd_ack)) 1185415ccb58SAlexey Bayduraev return -1; 1186415ccb58SAlexey Bayduraev 1187415ccb58SAlexey Bayduraev ret = record__alloc_thread_data(rec, evlist); 1188415ccb58SAlexey Bayduraev if (ret) 1189415ccb58SAlexey Bayduraev return ret; 1190415ccb58SAlexey Bayduraev 119156f735ffSAlexey Bayduraev if (record__threads_enabled(rec)) { 119256f735ffSAlexey Bayduraev ret = perf_data__create_dir(&rec->data, evlist->core.nr_mmaps); 119365e7c963SAlexey Bayduraev if (ret) { 119465e7c963SAlexey Bayduraev pr_err("Failed to create data directory: %s\n", strerror(-ret)); 119556f735ffSAlexey Bayduraev return ret; 119665e7c963SAlexey Bayduraev } 119756f735ffSAlexey Bayduraev for (i = 0; i < evlist->core.nr_mmaps; i++) { 119856f735ffSAlexey Bayduraev if (evlist->mmap) 119956f735ffSAlexey Bayduraev evlist->mmap[i].file = &rec->data.dir.files[i]; 120056f735ffSAlexey Bayduraev if (evlist->overwrite_mmap) 120156f735ffSAlexey Bayduraev evlist->overwrite_mmap[i].file = &rec->data.dir.files[i]; 120256f735ffSAlexey Bayduraev } 120356f735ffSAlexey Bayduraev } 120456f735ffSAlexey Bayduraev 1205cda57a8cSWang Nan return 0; 1206cda57a8cSWang Nan } 1207cda57a8cSWang Nan 1208cda57a8cSWang Nan static int record__mmap(struct record *rec) 1209cda57a8cSWang Nan { 1210cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 1211cda57a8cSWang Nan } 1212cda57a8cSWang Nan 12138c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 1214dd7927f4SArnaldo Carvalho de Melo { 1215d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 121632dcd021SJiri Olsa struct evsel *pos; 121763503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 1218d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 1219b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 12208d3eca20SDavid Ahern int rc = 0; 1221dd7927f4SArnaldo Carvalho de Melo 1222d3dbf43cSArnaldo Carvalho de Melo /* 1223b91e5492SKan Liang * For initial_delay, system wide or a hybrid system, we need to add a 1224b91e5492SKan Liang * dummy event so that we can track PERF_RECORD_MMAP to cover the delay 1225b91e5492SKan Liang * of waiting or event synthesis. 1226d3dbf43cSArnaldo Carvalho de Melo */ 1227b91e5492SKan Liang if (opts->initial_delay || target__has_cpu(&opts->target) || 1228b91e5492SKan Liang perf_pmu__has_hybrid()) { 1229e80db255SArnaldo Carvalho de Melo pos = evlist__get_tracking_event(evlist); 1230442ad225SAdrian Hunter if (!evsel__is_dummy_event(pos)) { 1231442ad225SAdrian Hunter /* Set up dummy event. */ 1232e251abeeSArnaldo Carvalho de Melo if (evlist__add_dummy(evlist)) 1233d3dbf43cSArnaldo Carvalho de Melo return -ENOMEM; 1234515dbe48SJiri Olsa pos = evlist__last(evlist); 1235e80db255SArnaldo Carvalho de Melo evlist__set_tracking_event(evlist, pos); 1236442ad225SAdrian Hunter } 1237442ad225SAdrian Hunter 12380a892c1cSIan Rogers /* 12390a892c1cSIan Rogers * Enable the dummy event when the process is forked for 12400a892c1cSIan Rogers * initial_delay, immediately for system wide. 12410a892c1cSIan Rogers */ 1242bb07d62eSNamhyung Kim if (opts->initial_delay && !pos->immediate && 1243bb07d62eSNamhyung Kim !target__has_cpu(&opts->target)) 12441fc632ceSJiri Olsa pos->core.attr.enable_on_exec = 1; 12450a892c1cSIan Rogers else 12460a892c1cSIan Rogers pos->immediate = 1; 1247d3dbf43cSArnaldo Carvalho de Melo } 1248d3dbf43cSArnaldo Carvalho de Melo 124978e1bc25SArnaldo Carvalho de Melo evlist__config(evlist, opts, &callchain_param); 1250cac21425SJiri Olsa 1251e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 12523da297a6SIngo Molnar try_again: 1253af663bd0SJiri Olsa if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) { 1254ae430892SArnaldo Carvalho de Melo if (evsel__fallback(pos, errno, msg, sizeof(msg))) { 1255bb963e16SNamhyung Kim if (verbose > 0) 1256c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 12573da297a6SIngo Molnar goto try_again; 12583da297a6SIngo Molnar } 1259cf99ad14SAndi Kleen if ((errno == EINVAL || errno == EBADF) && 1260fba7c866SJiri Olsa pos->core.leader != &pos->core && 1261cf99ad14SAndi Kleen pos->weak_group) { 126264b4778bSArnaldo Carvalho de Melo pos = evlist__reset_weak_group(evlist, pos, true); 1263cf99ad14SAndi Kleen goto try_again; 1264cf99ad14SAndi Kleen } 126556e52e85SArnaldo Carvalho de Melo rc = -errno; 12662bb72dbbSArnaldo Carvalho de Melo evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg)); 126756e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 12688d3eca20SDavid Ahern goto out; 12697c6a1c65SPeter Zijlstra } 1270bfd8f72cSAndi Kleen 1271bfd8f72cSAndi Kleen pos->supported = true; 12727c6a1c65SPeter Zijlstra } 12737c6a1c65SPeter Zijlstra 127478e1bc25SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) { 1275c8b567c8SArnaldo Carvalho de Melo pr_warning( 1276c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 1277c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n" 1278c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 1279c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 1280c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 1281c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 1282c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 1283c8b567c8SArnaldo Carvalho de Melo } 1284c8b567c8SArnaldo Carvalho de Melo 128524bf91a7SArnaldo Carvalho de Melo if (evlist__apply_filters(evlist, &pos)) { 128662d94b00SArnaldo Carvalho de Melo pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 12878ab2e96dSArnaldo Carvalho de Melo pos->filter, evsel__name(pos), errno, 1288c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 12898d3eca20SDavid Ahern rc = -1; 12908d3eca20SDavid Ahern goto out; 12910a102479SFrederic Weisbecker } 12920a102479SFrederic Weisbecker 1293cda57a8cSWang Nan rc = record__mmap(rec); 1294cda57a8cSWang Nan if (rc) 12958d3eca20SDavid Ahern goto out; 12960a27d7f9SArnaldo Carvalho de Melo 1297a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 12987b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 12998d3eca20SDavid Ahern out: 13008d3eca20SDavid Ahern return rc; 1301a91e5431SArnaldo Carvalho de Melo } 1302a91e5431SArnaldo Carvalho de Melo 130366286ed3SAdrian Hunter static void set_timestamp_boundary(struct record *rec, u64 sample_time) 130466286ed3SAdrian Hunter { 130566286ed3SAdrian Hunter if (rec->evlist->first_sample_time == 0) 130666286ed3SAdrian Hunter rec->evlist->first_sample_time = sample_time; 130766286ed3SAdrian Hunter 130866286ed3SAdrian Hunter if (sample_time) 130966286ed3SAdrian Hunter rec->evlist->last_sample_time = sample_time; 131066286ed3SAdrian Hunter } 131166286ed3SAdrian Hunter 1312e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 1313e3d59112SNamhyung Kim union perf_event *event, 1314e3d59112SNamhyung Kim struct perf_sample *sample, 131532dcd021SJiri Olsa struct evsel *evsel, 1316e3d59112SNamhyung Kim struct machine *machine) 1317e3d59112SNamhyung Kim { 1318e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 1319e3d59112SNamhyung Kim 132066286ed3SAdrian Hunter set_timestamp_boundary(rec, sample->time); 132168588bafSJin Yao 132268588bafSJin Yao if (rec->buildid_all) 132368588bafSJin Yao return 0; 132468588bafSJin Yao 132568588bafSJin Yao rec->samples++; 1326e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 1327e3d59112SNamhyung Kim } 1328e3d59112SNamhyung Kim 13298c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 13306122e4e4SArnaldo Carvalho de Melo { 1331f5fc1412SJiri Olsa struct perf_session *session = rec->session; 13326122e4e4SArnaldo Carvalho de Melo 133345112e89SJiri Olsa if (perf_data__size(&rec->data) == 0) 13349f591fd7SArnaldo Carvalho de Melo return 0; 13359f591fd7SArnaldo Carvalho de Melo 133600dc8657SNamhyung Kim /* 133700dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 133800dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 133900dc8657SNamhyung Kim * we prefer the vmlinux path like 134000dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 134100dc8657SNamhyung Kim * 134200dc8657SNamhyung Kim * rather than build-id path (in debug directory). 134300dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 134400dc8657SNamhyung Kim */ 134500dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 134600dc8657SNamhyung Kim 13476156681bSNamhyung Kim /* 13486156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 134968588bafSJin Yao * so no need to process samples. But if timestamp_boundary is enabled, 135068588bafSJin Yao * it still needs to walk on all samples to get the timestamps of 135168588bafSJin Yao * first/last samples. 13526156681bSNamhyung Kim */ 135368588bafSJin Yao if (rec->buildid_all && !rec->timestamp_boundary) 13546156681bSNamhyung Kim rec->tool.sample = NULL; 13556156681bSNamhyung Kim 1356b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 13576122e4e4SArnaldo Carvalho de Melo } 13586122e4e4SArnaldo Carvalho de Melo 13598115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 1360a1645ce1SZhang, Yanmin { 1361a1645ce1SZhang, Yanmin int err; 136245694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 1363a1645ce1SZhang, Yanmin /* 1364a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 1365a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 1366a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 1367a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 1368a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 1369a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 1370a1645ce1SZhang, Yanmin */ 137145694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 1372743eb868SArnaldo Carvalho de Melo machine); 1373a1645ce1SZhang, Yanmin if (err < 0) 1374a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 137523346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 1376a1645ce1SZhang, Yanmin 1377a1645ce1SZhang, Yanmin /* 1378a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 1379a1645ce1SZhang, Yanmin * have no _text sometimes. 1380a1645ce1SZhang, Yanmin */ 138145694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 13820ae617beSAdrian Hunter machine); 1383a1645ce1SZhang, Yanmin if (err < 0) 1384a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 138523346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 1386a1645ce1SZhang, Yanmin } 1387a1645ce1SZhang, Yanmin 138898402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 138998402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 139098402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 139198402807SFrederic Weisbecker }; 139298402807SFrederic Weisbecker 1393a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map) 1394f13de660SAlexey Budankov { 1395f13de660SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS && 1396396b626bSAlexey Bayduraev !bitmap_equal(thread->mask->affinity.bits, map->affinity_mask.bits, 1397396b626bSAlexey Bayduraev thread->mask->affinity.nbits)) { 1398396b626bSAlexey Bayduraev bitmap_zero(thread->mask->affinity.bits, thread->mask->affinity.nbits); 1399396b626bSAlexey Bayduraev bitmap_or(thread->mask->affinity.bits, thread->mask->affinity.bits, 1400396b626bSAlexey Bayduraev map->affinity_mask.bits, thread->mask->affinity.nbits); 1401396b626bSAlexey Bayduraev sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity), 1402396b626bSAlexey Bayduraev (cpu_set_t *)thread->mask->affinity.bits); 1403396b626bSAlexey Bayduraev if (verbose == 2) { 1404396b626bSAlexey Bayduraev pr_debug("threads[%d]: running on cpu%d: ", thread->tid, sched_getcpu()); 1405396b626bSAlexey Bayduraev mmap_cpu_mask__scnprintf(&thread->mask->affinity, "affinity"); 1406396b626bSAlexey Bayduraev } 1407f13de660SAlexey Budankov } 1408f13de660SAlexey Budankov } 1409f13de660SAlexey Budankov 14105d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment) 14115d7f4116SAlexey Budankov { 141272932371SJiri Olsa struct perf_record_compressed *event = record; 14135d7f4116SAlexey Budankov size_t size = sizeof(*event); 14145d7f4116SAlexey Budankov 14155d7f4116SAlexey Budankov if (increment) { 14165d7f4116SAlexey Budankov event->header.size += increment; 14175d7f4116SAlexey Budankov return increment; 14185d7f4116SAlexey Budankov } 14195d7f4116SAlexey Budankov 14205d7f4116SAlexey Budankov event->header.type = PERF_RECORD_COMPRESSED; 14215d7f4116SAlexey Budankov event->header.size = size; 14225d7f4116SAlexey Budankov 14235d7f4116SAlexey Budankov return size; 14245d7f4116SAlexey Budankov } 14255d7f4116SAlexey Budankov 142675f5f1fcSAlexey Bayduraev static size_t zstd_compress(struct perf_session *session, struct mmap *map, 142775f5f1fcSAlexey Bayduraev void *dst, size_t dst_size, void *src, size_t src_size) 14285d7f4116SAlexey Budankov { 14295d7f4116SAlexey Budankov size_t compressed; 143072932371SJiri Olsa size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1; 143175f5f1fcSAlexey Bayduraev struct zstd_data *zstd_data = &session->zstd_data; 14325d7f4116SAlexey Budankov 143375f5f1fcSAlexey Bayduraev if (map && map->file) 143475f5f1fcSAlexey Bayduraev zstd_data = &map->zstd_data; 143575f5f1fcSAlexey Bayduraev 143675f5f1fcSAlexey Bayduraev compressed = zstd_compress_stream_to_records(zstd_data, dst, dst_size, src, src_size, 14375d7f4116SAlexey Budankov max_record_size, process_comp_header); 14385d7f4116SAlexey Budankov 1439610fbc01SAlexey Bayduraev if (map && map->file) { 1440610fbc01SAlexey Bayduraev thread->bytes_transferred += src_size; 1441610fbc01SAlexey Bayduraev thread->bytes_compressed += compressed; 1442610fbc01SAlexey Bayduraev } else { 14435d7f4116SAlexey Budankov session->bytes_transferred += src_size; 14445d7f4116SAlexey Budankov session->bytes_compressed += compressed; 1445610fbc01SAlexey Bayduraev } 14465d7f4116SAlexey Budankov 14475d7f4116SAlexey Budankov return compressed; 14485d7f4116SAlexey Budankov } 14495d7f4116SAlexey Budankov 145063503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist, 1451470530bbSAlexey Budankov bool overwrite, bool synch) 145298402807SFrederic Weisbecker { 1453dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 14540e2e63ddSPeter Zijlstra int i; 14558d3eca20SDavid Ahern int rc = 0; 1456396b626bSAlexey Bayduraev int nr_mmaps; 1457396b626bSAlexey Bayduraev struct mmap **maps; 1458d3d1af6fSAlexey Budankov int trace_fd = rec->data.file.fd; 1459ef781128SAlexey Budankov off_t off = 0; 146098402807SFrederic Weisbecker 1461cb21686bSWang Nan if (!evlist) 1462cb21686bSWang Nan return 0; 1463ef149c25SAdrian Hunter 1464396b626bSAlexey Bayduraev nr_mmaps = thread->nr_mmaps; 1465396b626bSAlexey Bayduraev maps = overwrite ? thread->overwrite_maps : thread->maps; 1466396b626bSAlexey Bayduraev 1467a4ea0ec4SWang Nan if (!maps) 1468a4ea0ec4SWang Nan return 0; 1469cb21686bSWang Nan 14700b72d69aSWang Nan if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 147154cc54deSWang Nan return 0; 147254cc54deSWang Nan 1473d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1474d3d1af6fSAlexey Budankov off = record__aio_get_pos(trace_fd); 1475d3d1af6fSAlexey Budankov 1476396b626bSAlexey Bayduraev for (i = 0; i < nr_mmaps; i++) { 1477470530bbSAlexey Budankov u64 flush = 0; 1478396b626bSAlexey Bayduraev struct mmap *map = maps[i]; 1479a4ea0ec4SWang Nan 1480547740f7SJiri Olsa if (map->core.base) { 1481f13de660SAlexey Budankov record__adjust_affinity(rec, map); 1482470530bbSAlexey Budankov if (synch) { 148365aa2e6bSJiri Olsa flush = map->core.flush; 148465aa2e6bSJiri Olsa map->core.flush = 1; 1485470530bbSAlexey Budankov } 1486d3d1af6fSAlexey Budankov if (!record__aio_enabled(rec)) { 1487ef781128SAlexey Budankov if (perf_mmap__push(map, rec, record__pushfn) < 0) { 1488470530bbSAlexey Budankov if (synch) 148965aa2e6bSJiri Olsa map->core.flush = flush; 14908d3eca20SDavid Ahern rc = -1; 14918d3eca20SDavid Ahern goto out; 14928d3eca20SDavid Ahern } 1493d3d1af6fSAlexey Budankov } else { 1494ef781128SAlexey Budankov if (record__aio_push(rec, map, &off) < 0) { 1495d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1496470530bbSAlexey Budankov if (synch) 149765aa2e6bSJiri Olsa map->core.flush = flush; 1498d3d1af6fSAlexey Budankov rc = -1; 1499d3d1af6fSAlexey Budankov goto out; 1500d3d1af6fSAlexey Budankov } 1501d3d1af6fSAlexey Budankov } 1502470530bbSAlexey Budankov if (synch) 150365aa2e6bSJiri Olsa map->core.flush = flush; 15048d3eca20SDavid Ahern } 1505ef149c25SAdrian Hunter 1506e035f4caSJiri Olsa if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode && 1507c0a6de06SAdrian Hunter !rec->opts.auxtrace_sample_mode && 1508e035f4caSJiri Olsa record__auxtrace_mmap_read(rec, map) != 0) { 1509ef149c25SAdrian Hunter rc = -1; 1510ef149c25SAdrian Hunter goto out; 1511ef149c25SAdrian Hunter } 151298402807SFrederic Weisbecker } 151398402807SFrederic Weisbecker 1514d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1515d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1516d3d1af6fSAlexey Budankov 1517dcabb507SJiri Olsa /* 1518dcabb507SJiri Olsa * Mark the round finished in case we wrote 1519dcabb507SJiri Olsa * at least one event. 152056f735ffSAlexey Bayduraev * 152156f735ffSAlexey Bayduraev * No need for round events in directory mode, 152256f735ffSAlexey Bayduraev * because per-cpu maps and files have data 152356f735ffSAlexey Bayduraev * sorted by kernel. 1524dcabb507SJiri Olsa */ 152556f735ffSAlexey Bayduraev if (!record__threads_enabled(rec) && bytes_written != rec->bytes_written) 1526ded2b8feSJiri Olsa rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event)); 15278d3eca20SDavid Ahern 15280b72d69aSWang Nan if (overwrite) 1529ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 15308d3eca20SDavid Ahern out: 15318d3eca20SDavid Ahern return rc; 153298402807SFrederic Weisbecker } 153398402807SFrederic Weisbecker 1534470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch) 1535cb21686bSWang Nan { 1536cb21686bSWang Nan int err; 1537cb21686bSWang Nan 1538470530bbSAlexey Budankov err = record__mmap_read_evlist(rec, rec->evlist, false, synch); 1539cb21686bSWang Nan if (err) 1540cb21686bSWang Nan return err; 1541cb21686bSWang Nan 1542470530bbSAlexey Budankov return record__mmap_read_evlist(rec, rec->evlist, true, synch); 1543cb21686bSWang Nan } 1544cb21686bSWang Nan 1545396b626bSAlexey Bayduraev static void record__thread_munmap_filtered(struct fdarray *fda, int fd, 1546396b626bSAlexey Bayduraev void *arg __maybe_unused) 1547396b626bSAlexey Bayduraev { 1548396b626bSAlexey Bayduraev struct perf_mmap *map = fda->priv[fd].ptr; 1549396b626bSAlexey Bayduraev 1550396b626bSAlexey Bayduraev if (map) 1551396b626bSAlexey Bayduraev perf_mmap__put(map); 1552396b626bSAlexey Bayduraev } 1553396b626bSAlexey Bayduraev 15543217e9feSAlexey Bayduraev static void *record__thread(void *arg) 15553217e9feSAlexey Bayduraev { 15563217e9feSAlexey Bayduraev enum thread_msg msg = THREAD_MSG__READY; 15573217e9feSAlexey Bayduraev bool terminate = false; 15583217e9feSAlexey Bayduraev struct fdarray *pollfd; 15593217e9feSAlexey Bayduraev int err, ctlfd_pos; 15603217e9feSAlexey Bayduraev 15613217e9feSAlexey Bayduraev thread = arg; 15623217e9feSAlexey Bayduraev thread->tid = gettid(); 15633217e9feSAlexey Bayduraev 15643217e9feSAlexey Bayduraev err = write(thread->pipes.ack[1], &msg, sizeof(msg)); 15653217e9feSAlexey Bayduraev if (err == -1) 15663217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to notify on start: %s\n", 15673217e9feSAlexey Bayduraev thread->tid, strerror(errno)); 15683217e9feSAlexey Bayduraev 15693217e9feSAlexey Bayduraev pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu()); 15703217e9feSAlexey Bayduraev 15713217e9feSAlexey Bayduraev pollfd = &thread->pollfd; 15723217e9feSAlexey Bayduraev ctlfd_pos = thread->ctlfd_pos; 15733217e9feSAlexey Bayduraev 15743217e9feSAlexey Bayduraev for (;;) { 15753217e9feSAlexey Bayduraev unsigned long long hits = thread->samples; 15763217e9feSAlexey Bayduraev 15773217e9feSAlexey Bayduraev if (record__mmap_read_all(thread->rec, false) < 0 || terminate) 15783217e9feSAlexey Bayduraev break; 15793217e9feSAlexey Bayduraev 15803217e9feSAlexey Bayduraev if (hits == thread->samples) { 15813217e9feSAlexey Bayduraev 15823217e9feSAlexey Bayduraev err = fdarray__poll(pollfd, -1); 15833217e9feSAlexey Bayduraev /* 15843217e9feSAlexey Bayduraev * Propagate error, only if there's any. Ignore positive 15853217e9feSAlexey Bayduraev * number of returned events and interrupt error. 15863217e9feSAlexey Bayduraev */ 15873217e9feSAlexey Bayduraev if (err > 0 || (err < 0 && errno == EINTR)) 15883217e9feSAlexey Bayduraev err = 0; 15893217e9feSAlexey Bayduraev thread->waking++; 15903217e9feSAlexey Bayduraev 15913217e9feSAlexey Bayduraev if (fdarray__filter(pollfd, POLLERR | POLLHUP, 15923217e9feSAlexey Bayduraev record__thread_munmap_filtered, NULL) == 0) 15933217e9feSAlexey Bayduraev break; 15943217e9feSAlexey Bayduraev } 15953217e9feSAlexey Bayduraev 15963217e9feSAlexey Bayduraev if (pollfd->entries[ctlfd_pos].revents & POLLHUP) { 15973217e9feSAlexey Bayduraev terminate = true; 15983217e9feSAlexey Bayduraev close(thread->pipes.msg[0]); 15993217e9feSAlexey Bayduraev thread->pipes.msg[0] = -1; 16003217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].fd = -1; 16013217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].events = 0; 16023217e9feSAlexey Bayduraev } 16033217e9feSAlexey Bayduraev 16043217e9feSAlexey Bayduraev pollfd->entries[ctlfd_pos].revents = 0; 16053217e9feSAlexey Bayduraev } 16063217e9feSAlexey Bayduraev record__mmap_read_all(thread->rec, true); 16073217e9feSAlexey Bayduraev 16083217e9feSAlexey Bayduraev err = write(thread->pipes.ack[1], &msg, sizeof(msg)); 16093217e9feSAlexey Bayduraev if (err == -1) 16103217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to notify on termination: %s\n", 16113217e9feSAlexey Bayduraev thread->tid, strerror(errno)); 16123217e9feSAlexey Bayduraev 16133217e9feSAlexey Bayduraev return NULL; 16143217e9feSAlexey Bayduraev } 16153217e9feSAlexey Bayduraev 16168c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 161757706abcSDavid Ahern { 161857706abcSDavid Ahern struct perf_session *session = rec->session; 161957706abcSDavid Ahern int feat; 162057706abcSDavid Ahern 162157706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 162257706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 162357706abcSDavid Ahern 162457706abcSDavid Ahern if (rec->no_buildid) 162557706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 162657706abcSDavid Ahern 1627ce9036a6SJiri Olsa if (!have_tracepoints(&rec->evlist->core.entries)) 162857706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 162957706abcSDavid Ahern 163057706abcSDavid Ahern if (!rec->opts.branch_stack) 163157706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 1632ef149c25SAdrian Hunter 1633ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 1634ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 1635ffa517adSJiri Olsa 1636cf790516SAlexey Budankov if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns)) 1637cf790516SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_CLOCKID); 1638cf790516SAlexey Budankov 1639d1e325cfSJiri Olsa if (!rec->opts.use_clockid) 1640d1e325cfSJiri Olsa perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA); 1641d1e325cfSJiri Olsa 164256f735ffSAlexey Bayduraev if (!record__threads_enabled(rec)) 1643258031c0SJiri Olsa perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT); 164456f735ffSAlexey Bayduraev 164542e1fd80SAlexey Budankov if (!record__comp_enabled(rec)) 164642e1fd80SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_COMPRESSED); 1647258031c0SJiri Olsa 1648ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 164957706abcSDavid Ahern } 165057706abcSDavid Ahern 1651e1ab48baSWang Nan static void 1652e1ab48baSWang Nan record__finish_output(struct record *rec) 1653e1ab48baSWang Nan { 165456f735ffSAlexey Bayduraev int i; 16558ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 16568ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1657e1ab48baSWang Nan 16588ceb41d7SJiri Olsa if (data->is_pipe) 1659e1ab48baSWang Nan return; 1660e1ab48baSWang Nan 1661e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 166245112e89SJiri Olsa data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR); 166356f735ffSAlexey Bayduraev if (record__threads_enabled(rec)) { 166456f735ffSAlexey Bayduraev for (i = 0; i < data->dir.nr; i++) 166556f735ffSAlexey Bayduraev data->dir.files[i].size = lseek(data->dir.files[i].fd, 0, SEEK_CUR); 166656f735ffSAlexey Bayduraev } 1667e1ab48baSWang Nan 1668e1ab48baSWang Nan if (!rec->no_buildid) { 1669e1ab48baSWang Nan process_buildids(rec); 1670e1ab48baSWang Nan 1671e1ab48baSWang Nan if (rec->buildid_all) 1672e1ab48baSWang Nan dsos__hit_all(rec->session); 1673e1ab48baSWang Nan } 1674e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 1675e1ab48baSWang Nan 1676e1ab48baSWang Nan return; 1677e1ab48baSWang Nan } 1678e1ab48baSWang Nan 16794ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 1680be7b0c9eSWang Nan { 16819d6aae72SArnaldo Carvalho de Melo int err; 16829749b90eSJiri Olsa struct perf_thread_map *thread_map; 168341b740b6SNamhyung Kim bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP; 1684be7b0c9eSWang Nan 16854ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 16864ea648aeSWang Nan return 0; 16874ea648aeSWang Nan 16889d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 16899d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 16909d6aae72SArnaldo Carvalho de Melo return -1; 16919d6aae72SArnaldo Carvalho de Melo 16929d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 1693be7b0c9eSWang Nan process_synthesized_event, 1694be7b0c9eSWang Nan &rec->session->machines.host, 169541b740b6SNamhyung Kim needs_mmap, 16963fcb10e4SMark Drayton rec->opts.sample_address); 16977836e52eSJiri Olsa perf_thread_map__put(thread_map); 16989d6aae72SArnaldo Carvalho de Melo return err; 1699be7b0c9eSWang Nan } 1700be7b0c9eSWang Nan 17014ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 17023c1cb7e3SWang Nan 1703ecfd7a9cSWang Nan static int 1704ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 1705ecfd7a9cSWang Nan { 17068ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1707ecfd7a9cSWang Nan int fd, err; 170803724b2eSAndi Kleen char *new_filename; 1709ecfd7a9cSWang Nan 1710ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 1711ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 1712ecfd7a9cSWang Nan 1713d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1714d3d1af6fSAlexey Budankov 17154ea648aeSWang Nan record__synthesize(rec, true); 17164ea648aeSWang Nan if (target__none(&rec->opts.target)) 17174ea648aeSWang Nan record__synthesize_workload(rec, true); 17184ea648aeSWang Nan 1719ecfd7a9cSWang Nan rec->samples = 0; 1720ecfd7a9cSWang Nan record__finish_output(rec); 1721ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 1722ecfd7a9cSWang Nan if (err) { 1723ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 1724ecfd7a9cSWang Nan return -EINVAL; 1725ecfd7a9cSWang Nan } 1726ecfd7a9cSWang Nan 17278ceb41d7SJiri Olsa fd = perf_data__switch(data, timestamp, 1728ecfd7a9cSWang Nan rec->session->header.data_offset, 172903724b2eSAndi Kleen at_exit, &new_filename); 1730ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 1731ecfd7a9cSWang Nan rec->bytes_written = 0; 1732ecfd7a9cSWang Nan rec->session->header.data_size = 0; 1733ecfd7a9cSWang Nan } 1734ecfd7a9cSWang Nan 1735ecfd7a9cSWang Nan if (!quiet) 1736ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 17372d4f2799SJiri Olsa data->path, timestamp); 17383c1cb7e3SWang Nan 173903724b2eSAndi Kleen if (rec->switch_output.num_files) { 174003724b2eSAndi Kleen int n = rec->switch_output.cur_file + 1; 174103724b2eSAndi Kleen 174203724b2eSAndi Kleen if (n >= rec->switch_output.num_files) 174303724b2eSAndi Kleen n = 0; 174403724b2eSAndi Kleen rec->switch_output.cur_file = n; 174503724b2eSAndi Kleen if (rec->switch_output.filenames[n]) { 174603724b2eSAndi Kleen remove(rec->switch_output.filenames[n]); 1747d8f9da24SArnaldo Carvalho de Melo zfree(&rec->switch_output.filenames[n]); 174803724b2eSAndi Kleen } 174903724b2eSAndi Kleen rec->switch_output.filenames[n] = new_filename; 175003724b2eSAndi Kleen } else { 175103724b2eSAndi Kleen free(new_filename); 175203724b2eSAndi Kleen } 175303724b2eSAndi Kleen 17543c1cb7e3SWang Nan /* Output tracking events */ 1755be7b0c9eSWang Nan if (!at_exit) { 17564ea648aeSWang Nan record__synthesize(rec, false); 17573c1cb7e3SWang Nan 1758be7b0c9eSWang Nan /* 1759be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 1760be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 1761be7b0c9eSWang Nan * generate tracking events because there's no thread_map 1762be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 1763be7b0c9eSWang Nan * contain map and comm information. 1764be7b0c9eSWang Nan * Create a fake thread_map and directly call 1765be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 1766be7b0c9eSWang Nan */ 1767be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 17684ea648aeSWang Nan record__synthesize_workload(rec, false); 1769be7b0c9eSWang Nan } 1770ecfd7a9cSWang Nan return fd; 1771ecfd7a9cSWang Nan } 1772ecfd7a9cSWang Nan 1773f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno; 1774f33cbe72SArnaldo Carvalho de Melo 1775f33cbe72SArnaldo Carvalho de Melo /* 17767b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload will send a SIGUSR1 1777f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 1778f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 1779f33cbe72SArnaldo Carvalho de Melo */ 178045604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 178145604710SNamhyung Kim siginfo_t *info, 1782f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 1783f33cbe72SArnaldo Carvalho de Melo { 1784f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 1785f33cbe72SArnaldo Carvalho de Melo done = 1; 1786f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 1787f33cbe72SArnaldo Carvalho de Melo } 1788f33cbe72SArnaldo Carvalho de Melo 17892dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 1790bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 17912dd6d8a1SAdrian Hunter 1792db0ea13cSArnaldo Carvalho de Melo static const struct perf_event_mmap_page *evlist__pick_pc(struct evlist *evlist) 1793ee667f94SWang Nan { 1794b2cb615dSWang Nan if (evlist) { 1795547740f7SJiri Olsa if (evlist->mmap && evlist->mmap[0].core.base) 1796547740f7SJiri Olsa return evlist->mmap[0].core.base; 1797547740f7SJiri Olsa if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base) 1798547740f7SJiri Olsa return evlist->overwrite_mmap[0].core.base; 1799b2cb615dSWang Nan } 1800ee667f94SWang Nan return NULL; 1801ee667f94SWang Nan } 1802ee667f94SWang Nan 1803c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 1804c45628b0SWang Nan { 1805db0ea13cSArnaldo Carvalho de Melo const struct perf_event_mmap_page *pc = evlist__pick_pc(rec->evlist); 1806ee667f94SWang Nan if (pc) 1807ee667f94SWang Nan return pc; 1808c45628b0SWang Nan return NULL; 1809c45628b0SWang Nan } 1810c45628b0SWang Nan 18114ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 1812c45c86ebSWang Nan { 1813c45c86ebSWang Nan struct perf_session *session = rec->session; 1814c45c86ebSWang Nan struct machine *machine = &session->machines.host; 18158ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1816c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 1817c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 1818c45c86ebSWang Nan int err = 0; 1819d99c22eaSStephane Eranian event_op f = process_synthesized_event; 1820c45c86ebSWang Nan 18214ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 18224ea648aeSWang Nan return 0; 18234ea648aeSWang Nan 18248ceb41d7SJiri Olsa if (data->is_pipe) { 1825c3a057dcSNamhyung Kim err = perf_event__synthesize_for_pipe(tool, session, data, 1826c45c86ebSWang Nan process_synthesized_event); 1827c3a057dcSNamhyung Kim if (err < 0) 1828c45c86ebSWang Nan goto out; 1829c45c86ebSWang Nan 1830c45c86ebSWang Nan rec->bytes_written += err; 1831c45c86ebSWang Nan } 1832c45c86ebSWang Nan 1833c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 183446bc29b9SAdrian Hunter process_synthesized_event, machine); 183546bc29b9SAdrian Hunter if (err) 183646bc29b9SAdrian Hunter goto out; 183746bc29b9SAdrian Hunter 1838c0a6de06SAdrian Hunter /* Synthesize id_index before auxtrace_info */ 183961750473SAdrian Hunter if (rec->opts.auxtrace_sample_mode || rec->opts.full_auxtrace) { 1840c0a6de06SAdrian Hunter err = perf_event__synthesize_id_index(tool, 1841c0a6de06SAdrian Hunter process_synthesized_event, 1842c0a6de06SAdrian Hunter session->evlist, machine); 1843c0a6de06SAdrian Hunter if (err) 1844c0a6de06SAdrian Hunter goto out; 1845c0a6de06SAdrian Hunter } 1846c0a6de06SAdrian Hunter 1847c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 1848c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 1849c45c86ebSWang Nan session, process_synthesized_event); 1850c45c86ebSWang Nan if (err) 1851c45c86ebSWang Nan goto out; 1852c45c86ebSWang Nan } 1853c45c86ebSWang Nan 185478e1bc25SArnaldo Carvalho de Melo if (!evlist__exclude_kernel(rec->evlist)) { 1855c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 1856c45c86ebSWang Nan machine); 1857c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 1858c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1859c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 1860c45c86ebSWang Nan 1861c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 1862c45c86ebSWang Nan machine); 1863c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 1864c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1865c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 18666c443954SArnaldo Carvalho de Melo } 1867c45c86ebSWang Nan 1868c45c86ebSWang Nan if (perf_guest) { 1869c45c86ebSWang Nan machines__process_guests(&session->machines, 1870c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 1871c45c86ebSWang Nan } 1872c45c86ebSWang Nan 1873bfd8f72cSAndi Kleen err = perf_event__synthesize_extra_attr(&rec->tool, 1874bfd8f72cSAndi Kleen rec->evlist, 1875bfd8f72cSAndi Kleen process_synthesized_event, 1876bfd8f72cSAndi Kleen data->is_pipe); 1877bfd8f72cSAndi Kleen if (err) 1878bfd8f72cSAndi Kleen goto out; 1879bfd8f72cSAndi Kleen 188003617c22SJiri Olsa err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads, 1881373565d2SAndi Kleen process_synthesized_event, 1882373565d2SAndi Kleen NULL); 1883373565d2SAndi Kleen if (err < 0) { 1884373565d2SAndi Kleen pr_err("Couldn't synthesize thread map.\n"); 1885373565d2SAndi Kleen return err; 1886373565d2SAndi Kleen } 1887373565d2SAndi Kleen 18880df6ade7SIan Rogers err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.user_requested_cpus, 1889373565d2SAndi Kleen process_synthesized_event, NULL); 1890373565d2SAndi Kleen if (err < 0) { 1891373565d2SAndi Kleen pr_err("Couldn't synthesize cpu map.\n"); 1892373565d2SAndi Kleen return err; 1893373565d2SAndi Kleen } 1894373565d2SAndi Kleen 1895e5416950SSong Liu err = perf_event__synthesize_bpf_events(session, process_synthesized_event, 18967b612e29SSong Liu machine, opts); 18977b612e29SSong Liu if (err < 0) 18987b612e29SSong Liu pr_warning("Couldn't synthesize bpf events.\n"); 18997b612e29SSong Liu 190041b740b6SNamhyung Kim if (rec->opts.synth & PERF_SYNTH_CGROUP) { 1901ab64069fSNamhyung Kim err = perf_event__synthesize_cgroups(tool, process_synthesized_event, 1902ab64069fSNamhyung Kim machine); 1903ab64069fSNamhyung Kim if (err < 0) 1904ab64069fSNamhyung Kim pr_warning("Couldn't synthesize cgroup events.\n"); 190541b740b6SNamhyung Kim } 1906ab64069fSNamhyung Kim 1907d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) { 1908d99c22eaSStephane Eranian perf_set_multithreaded(); 1909d99c22eaSStephane Eranian f = process_locked_synthesized_event; 1910d99c22eaSStephane Eranian } 1911d99c22eaSStephane Eranian 191241b740b6SNamhyung Kim if (rec->opts.synth & PERF_SYNTH_TASK) { 191341b740b6SNamhyung Kim bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP; 191441b740b6SNamhyung Kim 191584111b9cSNamhyung Kim err = __machine__synthesize_threads(machine, tool, &opts->target, 191684111b9cSNamhyung Kim rec->evlist->core.threads, 191741b740b6SNamhyung Kim f, needs_mmap, opts->sample_address, 1918d99c22eaSStephane Eranian rec->opts.nr_threads_synthesize); 191941b740b6SNamhyung Kim } 1920d99c22eaSStephane Eranian 1921d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) 1922d99c22eaSStephane Eranian perf_set_singlethreaded(); 1923d99c22eaSStephane Eranian 1924c45c86ebSWang Nan out: 1925c45c86ebSWang Nan return err; 1926c45c86ebSWang Nan } 1927c45c86ebSWang Nan 1928899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data) 1929899e5ffbSArnaldo Carvalho de Melo { 1930899e5ffbSArnaldo Carvalho de Melo struct record *rec = data; 1931899e5ffbSArnaldo Carvalho de Melo pthread_kill(rec->thread_id, SIGUSR2); 1932899e5ffbSArnaldo Carvalho de Melo return 0; 1933899e5ffbSArnaldo Carvalho de Melo } 1934899e5ffbSArnaldo Carvalho de Melo 193523cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec) 193623cbb41cSArnaldo Carvalho de Melo { 193723cbb41cSArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 193823cbb41cSArnaldo Carvalho de Melo 193923cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist != NULL) { 194023cbb41cSArnaldo Carvalho de Melo /* 194123cbb41cSArnaldo Carvalho de Melo * We get here if --switch-output-event populated the 194223cbb41cSArnaldo Carvalho de Melo * sb_evlist, so associate a callback that will send a SIGUSR2 194323cbb41cSArnaldo Carvalho de Melo * to the main thread. 194423cbb41cSArnaldo Carvalho de Melo */ 194523cbb41cSArnaldo Carvalho de Melo evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec); 194623cbb41cSArnaldo Carvalho de Melo rec->thread_id = pthread_self(); 194723cbb41cSArnaldo Carvalho de Melo } 19481101c872SJin Yao #ifdef HAVE_LIBBPF_SUPPORT 194923cbb41cSArnaldo Carvalho de Melo if (!opts->no_bpf_event) { 195023cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 195123cbb41cSArnaldo Carvalho de Melo rec->sb_evlist = evlist__new(); 195223cbb41cSArnaldo Carvalho de Melo 195323cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 195423cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't create side band evlist.\n."); 195523cbb41cSArnaldo Carvalho de Melo return -1; 195623cbb41cSArnaldo Carvalho de Melo } 195723cbb41cSArnaldo Carvalho de Melo } 195823cbb41cSArnaldo Carvalho de Melo 195923cbb41cSArnaldo Carvalho de Melo if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) { 196023cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n."); 196123cbb41cSArnaldo Carvalho de Melo return -1; 196223cbb41cSArnaldo Carvalho de Melo } 196323cbb41cSArnaldo Carvalho de Melo } 19641101c872SJin Yao #endif 196508c83997SArnaldo Carvalho de Melo if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) { 196623cbb41cSArnaldo Carvalho de Melo pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n"); 196723cbb41cSArnaldo Carvalho de Melo opts->no_bpf_event = true; 196823cbb41cSArnaldo Carvalho de Melo } 196923cbb41cSArnaldo Carvalho de Melo 197023cbb41cSArnaldo Carvalho de Melo return 0; 197123cbb41cSArnaldo Carvalho de Melo } 197223cbb41cSArnaldo Carvalho de Melo 1973d1e325cfSJiri Olsa static int record__init_clock(struct record *rec) 1974d1e325cfSJiri Olsa { 1975d1e325cfSJiri Olsa struct perf_session *session = rec->session; 1976d1e325cfSJiri Olsa struct timespec ref_clockid; 1977d1e325cfSJiri Olsa struct timeval ref_tod; 1978d1e325cfSJiri Olsa u64 ref; 1979d1e325cfSJiri Olsa 1980d1e325cfSJiri Olsa if (!rec->opts.use_clockid) 1981d1e325cfSJiri Olsa return 0; 1982d1e325cfSJiri Olsa 19839d88a1a1SJiri Olsa if (rec->opts.use_clockid && rec->opts.clockid_res_ns) 19849d88a1a1SJiri Olsa session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns; 19859d88a1a1SJiri Olsa 1986d1e325cfSJiri Olsa session->header.env.clock.clockid = rec->opts.clockid; 1987d1e325cfSJiri Olsa 1988d1e325cfSJiri Olsa if (gettimeofday(&ref_tod, NULL) != 0) { 1989d1e325cfSJiri Olsa pr_err("gettimeofday failed, cannot set reference time.\n"); 1990d1e325cfSJiri Olsa return -1; 1991d1e325cfSJiri Olsa } 1992d1e325cfSJiri Olsa 1993d1e325cfSJiri Olsa if (clock_gettime(rec->opts.clockid, &ref_clockid)) { 1994d1e325cfSJiri Olsa pr_err("clock_gettime failed, cannot set reference time.\n"); 1995d1e325cfSJiri Olsa return -1; 1996d1e325cfSJiri Olsa } 1997d1e325cfSJiri Olsa 1998d1e325cfSJiri Olsa ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC + 1999d1e325cfSJiri Olsa (u64) ref_tod.tv_usec * NSEC_PER_USEC; 2000d1e325cfSJiri Olsa 2001d1e325cfSJiri Olsa session->header.env.clock.tod_ns = ref; 2002d1e325cfSJiri Olsa 2003d1e325cfSJiri Olsa ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC + 2004d1e325cfSJiri Olsa (u64) ref_clockid.tv_nsec; 2005d1e325cfSJiri Olsa 2006d1e325cfSJiri Olsa session->header.env.clock.clockid_ns = ref; 2007d1e325cfSJiri Olsa return 0; 2008d1e325cfSJiri Olsa } 2009d1e325cfSJiri Olsa 2010d20aff15SAdrian Hunter static void hit_auxtrace_snapshot_trigger(struct record *rec) 2011d20aff15SAdrian Hunter { 2012d20aff15SAdrian Hunter if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 2013d20aff15SAdrian Hunter trigger_hit(&auxtrace_snapshot_trigger); 2014d20aff15SAdrian Hunter auxtrace_record__snapshot_started = 1; 2015d20aff15SAdrian Hunter if (auxtrace_record__snapshot_start(rec->itr)) 2016d20aff15SAdrian Hunter trigger_error(&auxtrace_snapshot_trigger); 2017d20aff15SAdrian Hunter } 2018d20aff15SAdrian Hunter } 2019d20aff15SAdrian Hunter 202091c0f5ecSJin Yao static void record__uniquify_name(struct record *rec) 202191c0f5ecSJin Yao { 202291c0f5ecSJin Yao struct evsel *pos; 202391c0f5ecSJin Yao struct evlist *evlist = rec->evlist; 202491c0f5ecSJin Yao char *new_name; 202591c0f5ecSJin Yao int ret; 202691c0f5ecSJin Yao 202791c0f5ecSJin Yao if (!perf_pmu__has_hybrid()) 202891c0f5ecSJin Yao return; 202991c0f5ecSJin Yao 203091c0f5ecSJin Yao evlist__for_each_entry(evlist, pos) { 203191c0f5ecSJin Yao if (!evsel__is_hybrid(pos)) 203291c0f5ecSJin Yao continue; 203391c0f5ecSJin Yao 203491c0f5ecSJin Yao if (strchr(pos->name, '/')) 203591c0f5ecSJin Yao continue; 203691c0f5ecSJin Yao 203791c0f5ecSJin Yao ret = asprintf(&new_name, "%s/%s/", 203891c0f5ecSJin Yao pos->pmu_name, pos->name); 203991c0f5ecSJin Yao if (ret) { 204091c0f5ecSJin Yao free(pos->name); 204191c0f5ecSJin Yao pos->name = new_name; 204291c0f5ecSJin Yao } 204391c0f5ecSJin Yao } 204491c0f5ecSJin Yao } 204591c0f5ecSJin Yao 20461e5de7d9SAlexey Bayduraev static int record__terminate_thread(struct record_thread *thread_data) 20471e5de7d9SAlexey Bayduraev { 20481e5de7d9SAlexey Bayduraev int err; 20491e5de7d9SAlexey Bayduraev enum thread_msg ack = THREAD_MSG__UNDEFINED; 20501e5de7d9SAlexey Bayduraev pid_t tid = thread_data->tid; 20511e5de7d9SAlexey Bayduraev 20521e5de7d9SAlexey Bayduraev close(thread_data->pipes.msg[1]); 20531e5de7d9SAlexey Bayduraev thread_data->pipes.msg[1] = -1; 20541e5de7d9SAlexey Bayduraev err = read(thread_data->pipes.ack[0], &ack, sizeof(ack)); 20551e5de7d9SAlexey Bayduraev if (err > 0) 20561e5de7d9SAlexey Bayduraev pr_debug2("threads[%d]: sent %s\n", tid, thread_msg_tags[ack]); 20571e5de7d9SAlexey Bayduraev else 20581e5de7d9SAlexey Bayduraev pr_warning("threads[%d]: failed to receive termination notification from %d\n", 20591e5de7d9SAlexey Bayduraev thread->tid, tid); 20601e5de7d9SAlexey Bayduraev 20611e5de7d9SAlexey Bayduraev return 0; 20621e5de7d9SAlexey Bayduraev } 20631e5de7d9SAlexey Bayduraev 2064396b626bSAlexey Bayduraev static int record__start_threads(struct record *rec) 2065396b626bSAlexey Bayduraev { 20663217e9feSAlexey Bayduraev int t, tt, err, ret = 0, nr_threads = rec->nr_threads; 2067396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 20683217e9feSAlexey Bayduraev sigset_t full, mask; 20693217e9feSAlexey Bayduraev pthread_t handle; 20703217e9feSAlexey Bayduraev pthread_attr_t attrs; 2071396b626bSAlexey Bayduraev 2072396b626bSAlexey Bayduraev thread = &thread_data[0]; 2073396b626bSAlexey Bayduraev 20743217e9feSAlexey Bayduraev if (!record__threads_enabled(rec)) 20753217e9feSAlexey Bayduraev return 0; 20763217e9feSAlexey Bayduraev 20773217e9feSAlexey Bayduraev sigfillset(&full); 20783217e9feSAlexey Bayduraev if (sigprocmask(SIG_SETMASK, &full, &mask)) { 20793217e9feSAlexey Bayduraev pr_err("Failed to block signals on threads start: %s\n", strerror(errno)); 20803217e9feSAlexey Bayduraev return -1; 20813217e9feSAlexey Bayduraev } 20823217e9feSAlexey Bayduraev 20833217e9feSAlexey Bayduraev pthread_attr_init(&attrs); 20843217e9feSAlexey Bayduraev pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED); 20853217e9feSAlexey Bayduraev 20863217e9feSAlexey Bayduraev for (t = 1; t < nr_threads; t++) { 20873217e9feSAlexey Bayduraev enum thread_msg msg = THREAD_MSG__UNDEFINED; 20883217e9feSAlexey Bayduraev 20893217e9feSAlexey Bayduraev #ifdef HAVE_PTHREAD_ATTR_SETAFFINITY_NP 20903217e9feSAlexey Bayduraev pthread_attr_setaffinity_np(&attrs, 20913217e9feSAlexey Bayduraev MMAP_CPU_MASK_BYTES(&(thread_data[t].mask->affinity)), 20923217e9feSAlexey Bayduraev (cpu_set_t *)(thread_data[t].mask->affinity.bits)); 20933217e9feSAlexey Bayduraev #endif 20943217e9feSAlexey Bayduraev if (pthread_create(&handle, &attrs, record__thread, &thread_data[t])) { 20953217e9feSAlexey Bayduraev for (tt = 1; tt < t; tt++) 20963217e9feSAlexey Bayduraev record__terminate_thread(&thread_data[t]); 20973217e9feSAlexey Bayduraev pr_err("Failed to start threads: %s\n", strerror(errno)); 20983217e9feSAlexey Bayduraev ret = -1; 20993217e9feSAlexey Bayduraev goto out_err; 21003217e9feSAlexey Bayduraev } 21013217e9feSAlexey Bayduraev 21023217e9feSAlexey Bayduraev err = read(thread_data[t].pipes.ack[0], &msg, sizeof(msg)); 21033217e9feSAlexey Bayduraev if (err > 0) 21043217e9feSAlexey Bayduraev pr_debug2("threads[%d]: sent %s\n", rec->thread_data[t].tid, 21053217e9feSAlexey Bayduraev thread_msg_tags[msg]); 21063217e9feSAlexey Bayduraev else 21073217e9feSAlexey Bayduraev pr_warning("threads[%d]: failed to receive start notification from %d\n", 21083217e9feSAlexey Bayduraev thread->tid, rec->thread_data[t].tid); 21093217e9feSAlexey Bayduraev } 21103217e9feSAlexey Bayduraev 21113217e9feSAlexey Bayduraev sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity), 21123217e9feSAlexey Bayduraev (cpu_set_t *)thread->mask->affinity.bits); 21133217e9feSAlexey Bayduraev 2114396b626bSAlexey Bayduraev pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu()); 2115396b626bSAlexey Bayduraev 21163217e9feSAlexey Bayduraev out_err: 21173217e9feSAlexey Bayduraev pthread_attr_destroy(&attrs); 21183217e9feSAlexey Bayduraev 21193217e9feSAlexey Bayduraev if (sigprocmask(SIG_SETMASK, &mask, NULL)) { 21203217e9feSAlexey Bayduraev pr_err("Failed to unblock signals on threads start: %s\n", strerror(errno)); 21213217e9feSAlexey Bayduraev ret = -1; 21223217e9feSAlexey Bayduraev } 21233217e9feSAlexey Bayduraev 21243217e9feSAlexey Bayduraev return ret; 2125396b626bSAlexey Bayduraev } 2126396b626bSAlexey Bayduraev 2127396b626bSAlexey Bayduraev static int record__stop_threads(struct record *rec) 2128396b626bSAlexey Bayduraev { 2129396b626bSAlexey Bayduraev int t; 2130396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 2131396b626bSAlexey Bayduraev 21321e5de7d9SAlexey Bayduraev for (t = 1; t < rec->nr_threads; t++) 21331e5de7d9SAlexey Bayduraev record__terminate_thread(&thread_data[t]); 21341e5de7d9SAlexey Bayduraev 2135610fbc01SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 2136396b626bSAlexey Bayduraev rec->samples += thread_data[t].samples; 2137610fbc01SAlexey Bayduraev if (!record__threads_enabled(rec)) 2138610fbc01SAlexey Bayduraev continue; 2139610fbc01SAlexey Bayduraev rec->session->bytes_transferred += thread_data[t].bytes_transferred; 2140610fbc01SAlexey Bayduraev rec->session->bytes_compressed += thread_data[t].bytes_compressed; 2141610fbc01SAlexey Bayduraev pr_debug("threads[%d]: samples=%lld, wakes=%ld, ", thread_data[t].tid, 2142610fbc01SAlexey Bayduraev thread_data[t].samples, thread_data[t].waking); 2143610fbc01SAlexey Bayduraev if (thread_data[t].bytes_transferred && thread_data[t].bytes_compressed) 2144610fbc01SAlexey Bayduraev pr_debug("transferred=%" PRIu64 ", compressed=%" PRIu64 "\n", 2145610fbc01SAlexey Bayduraev thread_data[t].bytes_transferred, thread_data[t].bytes_compressed); 2146610fbc01SAlexey Bayduraev else 2147610fbc01SAlexey Bayduraev pr_debug("written=%" PRIu64 "\n", thread_data[t].bytes_written); 2148610fbc01SAlexey Bayduraev } 2149396b626bSAlexey Bayduraev 2150396b626bSAlexey Bayduraev return 0; 2151396b626bSAlexey Bayduraev } 2152396b626bSAlexey Bayduraev 2153396b626bSAlexey Bayduraev static unsigned long record__waking(struct record *rec) 2154396b626bSAlexey Bayduraev { 2155396b626bSAlexey Bayduraev int t; 2156396b626bSAlexey Bayduraev unsigned long waking = 0; 2157396b626bSAlexey Bayduraev struct record_thread *thread_data = rec->thread_data; 2158396b626bSAlexey Bayduraev 2159396b626bSAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) 2160396b626bSAlexey Bayduraev waking += thread_data[t].waking; 2161396b626bSAlexey Bayduraev 2162396b626bSAlexey Bayduraev return waking; 2163396b626bSAlexey Bayduraev } 2164396b626bSAlexey Bayduraev 21658c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 216686470930SIngo Molnar { 216757706abcSDavid Ahern int err; 216845604710SNamhyung Kim int status = 0; 216946be604bSZhang, Yanmin const bool forks = argc > 0; 217045694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 2171b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 21728ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 2173d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 21746dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 217542aa276fSNamhyung Kim int fd; 2176d3c8c08eSAlexey Budankov float ratio = 0; 2177acce0223SAlexey Budankov enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED; 217886470930SIngo Molnar 217945604710SNamhyung Kim atexit(record__sig_exit); 2180f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 2181f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 2182804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 2183a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 2184c0bdc1c4SWang Nan 2185f3b3614aSHari Bathini if (rec->opts.record_namespaces) 2186f3b3614aSHari Bathini tool->namespace_events = true; 2187f3b3614aSHari Bathini 21888fb4b679SNamhyung Kim if (rec->opts.record_cgroup) { 21898fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE 21908fb4b679SNamhyung Kim tool->cgroup_events = true; 21918fb4b679SNamhyung Kim #else 21928fb4b679SNamhyung Kim pr_err("cgroup tracking is not supported\n"); 21938fb4b679SNamhyung Kim return -1; 21948fb4b679SNamhyung Kim #endif 21958fb4b679SNamhyung Kim } 21968fb4b679SNamhyung Kim 2197dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 21982dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 21993c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 22005f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 2201dc0c6127SJiri Olsa if (rec->switch_output.enabled) 22023c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 2203c0bdc1c4SWang Nan } else { 22042dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 2205c0bdc1c4SWang Nan } 2206f5970550SPeter Zijlstra 22072681bd85SNamhyung Kim session = perf_session__new(data, tool); 22086ef81c55SMamatha Inamdar if (IS_ERR(session)) { 2209ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 22106ef81c55SMamatha Inamdar return PTR_ERR(session); 2211a9a70bbcSArnaldo Carvalho de Melo } 2212a9a70bbcSArnaldo Carvalho de Melo 2213b5f2511dSAlexey Bayduraev if (record__threads_enabled(rec)) { 2214b5f2511dSAlexey Bayduraev if (perf_data__is_pipe(&rec->data)) { 2215b5f2511dSAlexey Bayduraev pr_err("Parallel trace streaming is not available in pipe mode.\n"); 2216b5f2511dSAlexey Bayduraev return -1; 2217b5f2511dSAlexey Bayduraev } 2218b5f2511dSAlexey Bayduraev if (rec->opts.full_auxtrace) { 2219b5f2511dSAlexey Bayduraev pr_err("Parallel trace streaming is not available in AUX area tracing mode.\n"); 2220b5f2511dSAlexey Bayduraev return -1; 2221b5f2511dSAlexey Bayduraev } 2222b5f2511dSAlexey Bayduraev } 2223b5f2511dSAlexey Bayduraev 22248ceb41d7SJiri Olsa fd = perf_data__fd(data); 2225d20deb64SArnaldo Carvalho de Melo rec->session = session; 2226d20deb64SArnaldo Carvalho de Melo 22275d7f4116SAlexey Budankov if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) { 22285d7f4116SAlexey Budankov pr_err("Compression initialization failed.\n"); 22295d7f4116SAlexey Budankov return -1; 22305d7f4116SAlexey Budankov } 2231da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 2232da231338SAnand K Mistry done_fd = eventfd(0, EFD_NONBLOCK); 2233da231338SAnand K Mistry if (done_fd < 0) { 2234da231338SAnand K Mistry pr_err("Failed to create wakeup eventfd, error: %m\n"); 2235da231338SAnand K Mistry status = -1; 2236da231338SAnand K Mistry goto out_delete_session; 2237da231338SAnand K Mistry } 2238e16c2ce7SYang Jihong err = evlist__add_wakeup_eventfd(rec->evlist, done_fd); 2239da231338SAnand K Mistry if (err < 0) { 2240da231338SAnand K Mistry pr_err("Failed to add wakeup eventfd to poll list\n"); 2241da231338SAnand K Mistry status = err; 2242da231338SAnand K Mistry goto out_delete_session; 2243da231338SAnand K Mistry } 2244da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT 22455d7f4116SAlexey Budankov 22465d7f4116SAlexey Budankov session->header.env.comp_type = PERF_COMP_ZSTD; 22475d7f4116SAlexey Budankov session->header.env.comp_level = rec->opts.comp_level; 22485d7f4116SAlexey Budankov 2249eeb399b5SAdrian Hunter if (rec->opts.kcore && 2250eeb399b5SAdrian Hunter !record__kcore_readable(&session->machines.host)) { 2251eeb399b5SAdrian Hunter pr_err("ERROR: kcore is not readable.\n"); 2252eeb399b5SAdrian Hunter return -1; 2253eeb399b5SAdrian Hunter } 2254eeb399b5SAdrian Hunter 2255d1e325cfSJiri Olsa if (record__init_clock(rec)) 2256d1e325cfSJiri Olsa return -1; 2257d1e325cfSJiri Olsa 22588c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 2259330aa675SStephane Eranian 2260d4db3f16SArnaldo Carvalho de Melo if (forks) { 22617b392ef0SArnaldo Carvalho de Melo err = evlist__prepare_workload(rec->evlist, &opts->target, argv, data->is_pipe, 2262735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 226335b9d88eSArnaldo Carvalho de Melo if (err < 0) { 226435b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 226545604710SNamhyung Kim status = err; 226635b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 2267856e9660SPeter Zijlstra } 2268856e9660SPeter Zijlstra } 2269856e9660SPeter Zijlstra 2270ad46e48cSJiri Olsa /* 2271ad46e48cSJiri Olsa * If we have just single event and are sending data 2272ad46e48cSJiri Olsa * through pipe, we need to force the ids allocation, 2273ad46e48cSJiri Olsa * because we synthesize event name through the pipe 2274ad46e48cSJiri Olsa * and need the id for that. 2275ad46e48cSJiri Olsa */ 22766484d2f9SJiri Olsa if (data->is_pipe && rec->evlist->core.nr_entries == 1) 2277ad46e48cSJiri Olsa rec->opts.sample_id = true; 2278ad46e48cSJiri Olsa 227991c0f5ecSJin Yao record__uniquify_name(rec); 228091c0f5ecSJin Yao 22818c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 22828d3eca20SDavid Ahern err = -1; 2283396b626bSAlexey Bayduraev goto out_free_threads; 22848d3eca20SDavid Ahern } 2285f6fa4375SJiri Olsa session->header.env.comp_mmap_len = session->evlist->core.mmap_len; 228686470930SIngo Molnar 2287eeb399b5SAdrian Hunter if (rec->opts.kcore) { 2288eeb399b5SAdrian Hunter err = record__kcore_copy(&session->machines.host, data); 2289eeb399b5SAdrian Hunter if (err) { 2290eeb399b5SAdrian Hunter pr_err("ERROR: Failed to copy kcore\n"); 2291396b626bSAlexey Bayduraev goto out_free_threads; 2292eeb399b5SAdrian Hunter } 2293eeb399b5SAdrian Hunter } 2294eeb399b5SAdrian Hunter 22958690a2a7SWang Nan err = bpf__apply_obj_config(); 22968690a2a7SWang Nan if (err) { 22978690a2a7SWang Nan char errbuf[BUFSIZ]; 22988690a2a7SWang Nan 22998690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 23008690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 23018690a2a7SWang Nan errbuf); 2302396b626bSAlexey Bayduraev goto out_free_threads; 23038690a2a7SWang Nan } 23048690a2a7SWang Nan 2305cca8482cSAdrian Hunter /* 2306cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 2307cca8482cSAdrian Hunter * evlist. 2308cca8482cSAdrian Hunter */ 23098cedf3a5SArnaldo Carvalho de Melo if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) { 2310cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 2311cca8482cSAdrian Hunter rec->tool.ordered_events = false; 2312cca8482cSAdrian Hunter } 2313cca8482cSAdrian Hunter 23143a683120SJiri Olsa if (!rec->evlist->core.nr_groups) 2315a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 2316a8bb559bSNamhyung Kim 23178ceb41d7SJiri Olsa if (data->is_pipe) { 231842aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 2319529870e3STom Zanussi if (err < 0) 2320396b626bSAlexey Bayduraev goto out_free_threads; 2321563aecb2SJiri Olsa } else { 232242aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 2323d5eed904SArnaldo Carvalho de Melo if (err < 0) 2324396b626bSAlexey Bayduraev goto out_free_threads; 2325d5eed904SArnaldo Carvalho de Melo } 23267c6a1c65SPeter Zijlstra 2327b38d85efSArnaldo Carvalho de Melo err = -1; 2328d3665498SDavid Ahern if (!rec->no_buildid 2329e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 2330d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 2331e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 2332396b626bSAlexey Bayduraev goto out_free_threads; 2333e20960c0SRobert Richter } 2334e20960c0SRobert Richter 233523cbb41cSArnaldo Carvalho de Melo err = record__setup_sb_evlist(rec); 233623cbb41cSArnaldo Carvalho de Melo if (err) 2337396b626bSAlexey Bayduraev goto out_free_threads; 2338657ee553SSong Liu 23394ea648aeSWang Nan err = record__synthesize(rec, false); 2340c45c86ebSWang Nan if (err < 0) 2341396b626bSAlexey Bayduraev goto out_free_threads; 23428d3eca20SDavid Ahern 2343d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 234486470930SIngo Molnar struct sched_param param; 234586470930SIngo Molnar 2346d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 234786470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 23486beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 23498d3eca20SDavid Ahern err = -1; 2350396b626bSAlexey Bayduraev goto out_free_threads; 235186470930SIngo Molnar } 235286470930SIngo Molnar } 235386470930SIngo Molnar 2354396b626bSAlexey Bayduraev if (record__start_threads(rec)) 2355396b626bSAlexey Bayduraev goto out_free_threads; 2356396b626bSAlexey Bayduraev 2357774cb499SJiri Olsa /* 2358774cb499SJiri Olsa * When perf is starting the traced process, all the events 2359774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 2360774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 2361774cb499SJiri Olsa */ 23626619a53eSAndi Kleen if (!target__none(&opts->target) && !opts->initial_delay) 23631c87f165SJiri Olsa evlist__enable(rec->evlist); 2364764e16a3SDavid Ahern 2365856e9660SPeter Zijlstra /* 2366856e9660SPeter Zijlstra * Let the child rip 2367856e9660SPeter Zijlstra */ 2368e803cf97SNamhyung Kim if (forks) { 236920a8a3cfSJiri Olsa struct machine *machine = &session->machines.host; 2370e5bed564SNamhyung Kim union perf_event *event; 2371e907caf3SHari Bathini pid_t tgid; 2372e5bed564SNamhyung Kim 2373e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 2374e5bed564SNamhyung Kim if (event == NULL) { 2375e5bed564SNamhyung Kim err = -ENOMEM; 2376e5bed564SNamhyung Kim goto out_child; 2377e5bed564SNamhyung Kim } 2378e5bed564SNamhyung Kim 2379e803cf97SNamhyung Kim /* 2380e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 2381e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 2382e803cf97SNamhyung Kim * cannot see a correct process name for those events. 2383e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 2384e803cf97SNamhyung Kim */ 2385e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 2386e803cf97SNamhyung Kim rec->evlist->workload.pid, 2387e803cf97SNamhyung Kim process_synthesized_event, 2388e803cf97SNamhyung Kim machine); 2389e5bed564SNamhyung Kim free(event); 2390e803cf97SNamhyung Kim 2391e907caf3SHari Bathini if (tgid == -1) 2392e907caf3SHari Bathini goto out_child; 2393e907caf3SHari Bathini 2394e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 2395e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 2396e907caf3SHari Bathini machine->id_hdr_size); 2397e907caf3SHari Bathini if (event == NULL) { 2398e907caf3SHari Bathini err = -ENOMEM; 2399e907caf3SHari Bathini goto out_child; 2400e907caf3SHari Bathini } 2401e907caf3SHari Bathini 2402e907caf3SHari Bathini /* 2403e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 2404e907caf3SHari Bathini */ 2405e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 2406e907caf3SHari Bathini rec->evlist->workload.pid, 2407e907caf3SHari Bathini tgid, process_synthesized_event, 2408e907caf3SHari Bathini machine); 2409e907caf3SHari Bathini free(event); 2410e907caf3SHari Bathini 24117b392ef0SArnaldo Carvalho de Melo evlist__start_workload(rec->evlist); 2412e803cf97SNamhyung Kim } 2413856e9660SPeter Zijlstra 24146619a53eSAndi Kleen if (opts->initial_delay) { 241568cd3b45SAlexey Budankov pr_info(EVLIST_DISABLED_MSG); 241668cd3b45SAlexey Budankov if (opts->initial_delay > 0) { 24170693e680SArnaldo Carvalho de Melo usleep(opts->initial_delay * USEC_PER_MSEC); 24181c87f165SJiri Olsa evlist__enable(rec->evlist); 241968cd3b45SAlexey Budankov pr_info(EVLIST_ENABLED_MSG); 242068cd3b45SAlexey Budankov } 24216619a53eSAndi Kleen } 24226619a53eSAndi Kleen 24235f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 24243c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 2425a074865eSWang Nan perf_hooks__invoke_record_start(); 2426649c48a9SPeter Zijlstra for (;;) { 2427396b626bSAlexey Bayduraev unsigned long long hits = thread->samples; 242886470930SIngo Molnar 242905737464SWang Nan /* 243005737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 243105737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 243205737464SWang Nan * hits != rec->samples in previous round. 243305737464SWang Nan * 2434ade9d208SArnaldo Carvalho de Melo * evlist__toggle_bkw_mmap ensure we never 243505737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 243605737464SWang Nan */ 243705737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 2438ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 243905737464SWang Nan 2440470530bbSAlexey Budankov if (record__mmap_read_all(rec, false) < 0) { 24415f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 24423c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 24438d3eca20SDavid Ahern err = -1; 244445604710SNamhyung Kim goto out_child; 24458d3eca20SDavid Ahern } 244686470930SIngo Molnar 24472dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 24482dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 24495f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 2450ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, false); 24515f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 24522dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 24532dd6d8a1SAdrian Hunter err = -1; 24542dd6d8a1SAdrian Hunter goto out_child; 24552dd6d8a1SAdrian Hunter } 24562dd6d8a1SAdrian Hunter } 24572dd6d8a1SAdrian Hunter 24583c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 245905737464SWang Nan /* 246005737464SWang Nan * If switch_output_trigger is hit, the data in 246105737464SWang Nan * overwritable ring buffer should have been collected, 246205737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 246305737464SWang Nan * 246405737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 246505737464SWang Nan * record__mmap_read_all() didn't collect data from 246605737464SWang Nan * overwritable ring buffer. Read again. 246705737464SWang Nan */ 246805737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 246905737464SWang Nan continue; 24703c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 24713c1cb7e3SWang Nan 247205737464SWang Nan /* 247305737464SWang Nan * Reenable events in overwrite ring buffer after 247405737464SWang Nan * record__mmap_read_all(): we should have collected 247505737464SWang Nan * data from it. 247605737464SWang Nan */ 2477ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 247805737464SWang Nan 24793c1cb7e3SWang Nan if (!quiet) 24803c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 2481396b626bSAlexey Bayduraev record__waking(rec)); 2482396b626bSAlexey Bayduraev thread->waking = 0; 24833c1cb7e3SWang Nan fd = record__switch_output(rec, false); 24843c1cb7e3SWang Nan if (fd < 0) { 24853c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 24863c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 24873c1cb7e3SWang Nan err = fd; 24883c1cb7e3SWang Nan goto out_child; 24893c1cb7e3SWang Nan } 2490bfacbe3bSJiri Olsa 2491bfacbe3bSJiri Olsa /* re-arm the alarm */ 2492bfacbe3bSJiri Olsa if (rec->switch_output.time) 2493bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 24943c1cb7e3SWang Nan } 24953c1cb7e3SWang Nan 2496396b626bSAlexey Bayduraev if (hits == thread->samples) { 24976dcf45efSArnaldo Carvalho de Melo if (done || draining) 2498649c48a9SPeter Zijlstra break; 2499396b626bSAlexey Bayduraev err = fdarray__poll(&thread->pollfd, -1); 2500a515114fSJiri Olsa /* 2501a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 2502a515114fSJiri Olsa * number of returned events and interrupt error. 2503a515114fSJiri Olsa */ 2504a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 250545604710SNamhyung Kim err = 0; 2506396b626bSAlexey Bayduraev thread->waking++; 25076dcf45efSArnaldo Carvalho de Melo 2508396b626bSAlexey Bayduraev if (fdarray__filter(&thread->pollfd, POLLERR | POLLHUP, 2509396b626bSAlexey Bayduraev record__thread_munmap_filtered, NULL) == 0) 25106dcf45efSArnaldo Carvalho de Melo draining = true; 2511396b626bSAlexey Bayduraev 2512396b626bSAlexey Bayduraev evlist__ctlfd_update(rec->evlist, 2513396b626bSAlexey Bayduraev &thread->pollfd.entries[thread->ctlfd_pos]); 25148b412664SPeter Zijlstra } 25158b412664SPeter Zijlstra 2516acce0223SAlexey Budankov if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) { 2517acce0223SAlexey Budankov switch (cmd) { 2518d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 2519d20aff15SAdrian Hunter hit_auxtrace_snapshot_trigger(rec); 2520d20aff15SAdrian Hunter evlist__ctlfd_ack(rec->evlist); 2521d20aff15SAdrian Hunter break; 2522f186cd61SJiri Olsa case EVLIST_CTL_CMD_STOP: 2523f186cd61SJiri Olsa done = 1; 2524f186cd61SJiri Olsa break; 2525acce0223SAlexey Budankov case EVLIST_CTL_CMD_ACK: 2526acce0223SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 2527991ae4ebSJiri Olsa case EVLIST_CTL_CMD_ENABLE: 2528991ae4ebSJiri Olsa case EVLIST_CTL_CMD_DISABLE: 2529142544a9SJiri Olsa case EVLIST_CTL_CMD_EVLIST: 253047fddcb4SJiri Olsa case EVLIST_CTL_CMD_PING: 2531acce0223SAlexey Budankov default: 2532acce0223SAlexey Budankov break; 2533acce0223SAlexey Budankov } 2534acce0223SAlexey Budankov } 2535acce0223SAlexey Budankov 2536774cb499SJiri Olsa /* 2537774cb499SJiri Olsa * When perf is starting the traced process, at the end events 2538774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 2539774cb499SJiri Olsa * disable events in this case. 2540774cb499SJiri Olsa */ 2541602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 25425f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 2543e74676deSJiri Olsa evlist__disable(rec->evlist); 25442711926aSJiri Olsa disabled = true; 25452711926aSJiri Olsa } 25468b412664SPeter Zijlstra } 2547ce7b0e42SAlexander Shishkin 25485f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 25493c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 25508b412664SPeter Zijlstra 2551ce7b0e42SAlexander Shishkin if (opts->auxtrace_snapshot_on_exit) 2552ce7b0e42SAlexander Shishkin record__auxtrace_snapshot_exit(rec); 2553ce7b0e42SAlexander Shishkin 2554f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 25553535a696SArnaldo Carvalho de Melo char msg[STRERR_BUFSIZE], strevsels[2048]; 2556c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 25573535a696SArnaldo Carvalho de Melo 25583535a696SArnaldo Carvalho de Melo evlist__scnprintf_evsels(rec->evlist, sizeof(strevsels), strevsels); 25593535a696SArnaldo Carvalho de Melo 25603535a696SArnaldo Carvalho de Melo pr_err("Failed to collect '%s' for the '%s' workload: %s\n", 25613535a696SArnaldo Carvalho de Melo strevsels, argv[0], emsg); 2562f33cbe72SArnaldo Carvalho de Melo err = -1; 256345604710SNamhyung Kim goto out_child; 2564f33cbe72SArnaldo Carvalho de Melo } 2565f33cbe72SArnaldo Carvalho de Melo 2566e3d59112SNamhyung Kim if (!quiet) 2567396b626bSAlexey Bayduraev fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", 2568396b626bSAlexey Bayduraev record__waking(rec)); 256986470930SIngo Molnar 25704ea648aeSWang Nan if (target__none(&rec->opts.target)) 25714ea648aeSWang Nan record__synthesize_workload(rec, true); 25724ea648aeSWang Nan 257345604710SNamhyung Kim out_child: 2574396b626bSAlexey Bayduraev record__stop_threads(rec); 2575470530bbSAlexey Budankov record__mmap_read_all(rec, true); 2576396b626bSAlexey Bayduraev out_free_threads: 2577415ccb58SAlexey Bayduraev record__free_thread_data(rec); 2578396b626bSAlexey Bayduraev evlist__finalize_ctlfd(rec->evlist); 2579d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 2580d3d1af6fSAlexey Budankov 2581d3c8c08eSAlexey Budankov if (rec->session->bytes_transferred && rec->session->bytes_compressed) { 2582d3c8c08eSAlexey Budankov ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed; 2583d3c8c08eSAlexey Budankov session->header.env.comp_ratio = ratio + 0.5; 2584d3c8c08eSAlexey Budankov } 2585d3c8c08eSAlexey Budankov 258645604710SNamhyung Kim if (forks) { 258745604710SNamhyung Kim int exit_status; 258845604710SNamhyung Kim 258945604710SNamhyung Kim if (!child_finished) 259045604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 259145604710SNamhyung Kim 259245604710SNamhyung Kim wait(&exit_status); 259345604710SNamhyung Kim 259445604710SNamhyung Kim if (err < 0) 259545604710SNamhyung Kim status = err; 259645604710SNamhyung Kim else if (WIFEXITED(exit_status)) 259745604710SNamhyung Kim status = WEXITSTATUS(exit_status); 259845604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 259945604710SNamhyung Kim signr = WTERMSIG(exit_status); 260045604710SNamhyung Kim } else 260145604710SNamhyung Kim status = err; 260245604710SNamhyung Kim 26034ea648aeSWang Nan record__synthesize(rec, true); 2604e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 2605e3d59112SNamhyung Kim rec->samples = 0; 2606e3d59112SNamhyung Kim 2607ecfd7a9cSWang Nan if (!err) { 2608ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 2609e1ab48baSWang Nan record__finish_output(rec); 2610ecfd7a9cSWang Nan } else { 2611ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 2612ecfd7a9cSWang Nan if (fd < 0) { 2613ecfd7a9cSWang Nan status = fd; 2614ecfd7a9cSWang Nan goto out_delete_session; 2615ecfd7a9cSWang Nan } 2616ecfd7a9cSWang Nan } 2617ecfd7a9cSWang Nan } 261839d17dacSArnaldo Carvalho de Melo 2619a074865eSWang Nan perf_hooks__invoke_record_end(); 2620a074865eSWang Nan 2621e3d59112SNamhyung Kim if (!err && !quiet) { 2622e3d59112SNamhyung Kim char samples[128]; 2623ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 2624ecfd7a9cSWang Nan ".<timestamp>" : ""; 2625e3d59112SNamhyung Kim 2626ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 2627e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 2628e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 2629e3d59112SNamhyung Kim else 2630e3d59112SNamhyung Kim samples[0] = '\0'; 2631e3d59112SNamhyung Kim 2632d3c8c08eSAlexey Budankov fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s", 26338ceb41d7SJiri Olsa perf_data__size(data) / 1024.0 / 1024.0, 26342d4f2799SJiri Olsa data->path, postfix, samples); 2635d3c8c08eSAlexey Budankov if (ratio) { 2636d3c8c08eSAlexey Budankov fprintf(stderr, ", compressed (original %.3f MB, ratio is %.3f)", 2637d3c8c08eSAlexey Budankov rec->session->bytes_transferred / 1024.0 / 1024.0, 2638d3c8c08eSAlexey Budankov ratio); 2639d3c8c08eSAlexey Budankov } 2640d3c8c08eSAlexey Budankov fprintf(stderr, " ]\n"); 2641e3d59112SNamhyung Kim } 2642e3d59112SNamhyung Kim 264339d17dacSArnaldo Carvalho de Melo out_delete_session: 2644da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 2645da231338SAnand K Mistry if (done_fd >= 0) 2646da231338SAnand K Mistry close(done_fd); 2647da231338SAnand K Mistry #endif 26485d7f4116SAlexey Budankov zstd_fini(&session->zstd_data); 264939d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 2650657ee553SSong Liu 2651657ee553SSong Liu if (!opts->no_bpf_event) 265208c83997SArnaldo Carvalho de Melo evlist__stop_sb_thread(rec->sb_evlist); 265345604710SNamhyung Kim return status; 265486470930SIngo Molnar } 265586470930SIngo Molnar 26560883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 265709b0fd45SJiri Olsa { 2658aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 2659a601fdffSJiri Olsa 26600883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 266126d33022SJiri Olsa 26620883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 266309b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 26640883e820SArnaldo Carvalho de Melo callchain->dump_size); 26650883e820SArnaldo Carvalho de Melo } 26660883e820SArnaldo Carvalho de Melo 26670883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 26680883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 26690883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 26700883e820SArnaldo Carvalho de Melo { 26710883e820SArnaldo Carvalho de Melo int ret; 26720883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 26730883e820SArnaldo Carvalho de Melo 26740883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 26750883e820SArnaldo Carvalho de Melo if (unset) { 26760883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 26770883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 26780883e820SArnaldo Carvalho de Melo return 0; 26790883e820SArnaldo Carvalho de Melo } 26800883e820SArnaldo Carvalho de Melo 26810883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 26820883e820SArnaldo Carvalho de Melo if (!ret) { 26830883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 26840883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 26850883e820SArnaldo Carvalho de Melo record->sample_address = true; 26860883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 26870883e820SArnaldo Carvalho de Melo } 26880883e820SArnaldo Carvalho de Melo 26890883e820SArnaldo Carvalho de Melo return ret; 269009b0fd45SJiri Olsa } 269109b0fd45SJiri Olsa 2692c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 269309b0fd45SJiri Olsa const char *arg, 269409b0fd45SJiri Olsa int unset) 269509b0fd45SJiri Olsa { 26960883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 269726d33022SJiri Olsa } 269826d33022SJiri Olsa 2699c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 270009b0fd45SJiri Olsa const char *arg __maybe_unused, 270109b0fd45SJiri Olsa int unset __maybe_unused) 270209b0fd45SJiri Olsa { 27032ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 2704c421e80bSKan Liang 27052ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 270609b0fd45SJiri Olsa 27072ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 27082ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 2709eb853e80SJiri Olsa 27102ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 271109b0fd45SJiri Olsa return 0; 271209b0fd45SJiri Olsa } 271309b0fd45SJiri Olsa 2714eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 2715eb853e80SJiri Olsa { 27167a29c087SNamhyung Kim struct record *rec = cb; 27177a29c087SNamhyung Kim 27187a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 27197a29c087SNamhyung Kim if (!strcmp(value, "cache")) 27207a29c087SNamhyung Kim rec->no_buildid_cache = false; 27217a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 27227a29c087SNamhyung Kim rec->no_buildid_cache = true; 27237a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 27247a29c087SNamhyung Kim rec->no_buildid = true; 2725e29386c8SJiri Olsa else if (!strcmp(value, "mmap")) 2726e29386c8SJiri Olsa rec->buildid_mmap = true; 27277a29c087SNamhyung Kim else 27287a29c087SNamhyung Kim return -1; 27297a29c087SNamhyung Kim return 0; 27307a29c087SNamhyung Kim } 2731cff17205SYisheng Xie if (!strcmp(var, "record.call-graph")) { 2732cff17205SYisheng Xie var = "call-graph.record-mode"; 2733eb853e80SJiri Olsa return perf_default_config(var, value, cb); 2734eb853e80SJiri Olsa } 273593f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 273693f20c0fSAlexey Budankov if (!strcmp(var, "record.aio")) { 273793f20c0fSAlexey Budankov rec->opts.nr_cblocks = strtol(value, NULL, 0); 273893f20c0fSAlexey Budankov if (!rec->opts.nr_cblocks) 273993f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_default; 274093f20c0fSAlexey Budankov } 274193f20c0fSAlexey Budankov #endif 27429bce13eaSJiri Olsa if (!strcmp(var, "record.debuginfod")) { 27439bce13eaSJiri Olsa rec->debuginfod.urls = strdup(value); 27449bce13eaSJiri Olsa if (!rec->debuginfod.urls) 27459bce13eaSJiri Olsa return -ENOMEM; 27469bce13eaSJiri Olsa rec->debuginfod.set = true; 27479bce13eaSJiri Olsa } 2748eb853e80SJiri Olsa 2749cff17205SYisheng Xie return 0; 2750cff17205SYisheng Xie } 2751cff17205SYisheng Xie 2752814c8c38SPeter Zijlstra 2753f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset) 2754f4fe11b7SAlexey Budankov { 2755f4fe11b7SAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 2756f4fe11b7SAlexey Budankov 2757f4fe11b7SAlexey Budankov if (unset || !str) 2758f4fe11b7SAlexey Budankov return 0; 2759f4fe11b7SAlexey Budankov 2760f4fe11b7SAlexey Budankov if (!strcasecmp(str, "node")) 2761f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_NODE; 2762f4fe11b7SAlexey Budankov else if (!strcasecmp(str, "cpu")) 2763f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_CPU; 2764f4fe11b7SAlexey Budankov 2765f4fe11b7SAlexey Budankov return 0; 2766f4fe11b7SAlexey Budankov } 2767f4fe11b7SAlexey Budankov 27687954f716SAlexey Bayduraev static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits) 27697954f716SAlexey Bayduraev { 27707954f716SAlexey Bayduraev mask->nbits = nr_bits; 27717954f716SAlexey Bayduraev mask->bits = bitmap_zalloc(mask->nbits); 27727954f716SAlexey Bayduraev if (!mask->bits) 27737954f716SAlexey Bayduraev return -ENOMEM; 27747954f716SAlexey Bayduraev 27757954f716SAlexey Bayduraev return 0; 27767954f716SAlexey Bayduraev } 27777954f716SAlexey Bayduraev 27787954f716SAlexey Bayduraev static void record__mmap_cpu_mask_free(struct mmap_cpu_mask *mask) 27797954f716SAlexey Bayduraev { 27807954f716SAlexey Bayduraev bitmap_free(mask->bits); 27817954f716SAlexey Bayduraev mask->nbits = 0; 27827954f716SAlexey Bayduraev } 27837954f716SAlexey Bayduraev 27847954f716SAlexey Bayduraev static int record__thread_mask_alloc(struct thread_mask *mask, int nr_bits) 27857954f716SAlexey Bayduraev { 27867954f716SAlexey Bayduraev int ret; 27877954f716SAlexey Bayduraev 27887954f716SAlexey Bayduraev ret = record__mmap_cpu_mask_alloc(&mask->maps, nr_bits); 27897954f716SAlexey Bayduraev if (ret) { 27907954f716SAlexey Bayduraev mask->affinity.bits = NULL; 27917954f716SAlexey Bayduraev return ret; 27927954f716SAlexey Bayduraev } 27937954f716SAlexey Bayduraev 27947954f716SAlexey Bayduraev ret = record__mmap_cpu_mask_alloc(&mask->affinity, nr_bits); 27957954f716SAlexey Bayduraev if (ret) { 27967954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->maps); 27977954f716SAlexey Bayduraev mask->maps.bits = NULL; 27987954f716SAlexey Bayduraev } 27997954f716SAlexey Bayduraev 28007954f716SAlexey Bayduraev return ret; 28017954f716SAlexey Bayduraev } 28027954f716SAlexey Bayduraev 28037954f716SAlexey Bayduraev static void record__thread_mask_free(struct thread_mask *mask) 28047954f716SAlexey Bayduraev { 28057954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->maps); 28067954f716SAlexey Bayduraev record__mmap_cpu_mask_free(&mask->affinity); 28077954f716SAlexey Bayduraev } 28087954f716SAlexey Bayduraev 280906380a84SAlexey Bayduraev static int record__parse_threads(const struct option *opt, const char *str, int unset) 281006380a84SAlexey Bayduraev { 2811f466e5edSAlexey Bayduraev int s; 281206380a84SAlexey Bayduraev struct record_opts *opts = opt->value; 281306380a84SAlexey Bayduraev 2814f466e5edSAlexey Bayduraev if (unset || !str || !strlen(str)) { 281506380a84SAlexey Bayduraev opts->threads_spec = THREAD_SPEC__CPU; 2816f466e5edSAlexey Bayduraev } else { 2817f466e5edSAlexey Bayduraev for (s = 1; s < THREAD_SPEC__MAX; s++) { 2818f466e5edSAlexey Bayduraev if (s == THREAD_SPEC__USER) { 2819f466e5edSAlexey Bayduraev opts->threads_user_spec = strdup(str); 2820f466e5edSAlexey Bayduraev if (!opts->threads_user_spec) 2821f466e5edSAlexey Bayduraev return -ENOMEM; 2822f466e5edSAlexey Bayduraev opts->threads_spec = THREAD_SPEC__USER; 2823f466e5edSAlexey Bayduraev break; 2824f466e5edSAlexey Bayduraev } 2825f466e5edSAlexey Bayduraev if (!strncasecmp(str, thread_spec_tags[s], strlen(thread_spec_tags[s]))) { 2826f466e5edSAlexey Bayduraev opts->threads_spec = s; 2827f466e5edSAlexey Bayduraev break; 2828f466e5edSAlexey Bayduraev } 2829f466e5edSAlexey Bayduraev } 2830f466e5edSAlexey Bayduraev } 2831f466e5edSAlexey Bayduraev 2832f466e5edSAlexey Bayduraev if (opts->threads_spec == THREAD_SPEC__USER) 2833f466e5edSAlexey Bayduraev pr_debug("threads_spec: %s\n", opts->threads_user_spec); 2834f466e5edSAlexey Bayduraev else 2835f466e5edSAlexey Bayduraev pr_debug("threads_spec: %s\n", thread_spec_tags[opts->threads_spec]); 283606380a84SAlexey Bayduraev 283706380a84SAlexey Bayduraev return 0; 283806380a84SAlexey Bayduraev } 283906380a84SAlexey Bayduraev 28406d575816SJiwei Sun static int parse_output_max_size(const struct option *opt, 28416d575816SJiwei Sun const char *str, int unset) 28426d575816SJiwei Sun { 28436d575816SJiwei Sun unsigned long *s = (unsigned long *)opt->value; 28446d575816SJiwei Sun static struct parse_tag tags_size[] = { 28456d575816SJiwei Sun { .tag = 'B', .mult = 1 }, 28466d575816SJiwei Sun { .tag = 'K', .mult = 1 << 10 }, 28476d575816SJiwei Sun { .tag = 'M', .mult = 1 << 20 }, 28486d575816SJiwei Sun { .tag = 'G', .mult = 1 << 30 }, 28496d575816SJiwei Sun { .tag = 0 }, 28506d575816SJiwei Sun }; 28516d575816SJiwei Sun unsigned long val; 28526d575816SJiwei Sun 28536d575816SJiwei Sun if (unset) { 28546d575816SJiwei Sun *s = 0; 28556d575816SJiwei Sun return 0; 28566d575816SJiwei Sun } 28576d575816SJiwei Sun 28586d575816SJiwei Sun val = parse_tag_value(str, tags_size); 28596d575816SJiwei Sun if (val != (unsigned long) -1) { 28606d575816SJiwei Sun *s = val; 28616d575816SJiwei Sun return 0; 28626d575816SJiwei Sun } 28636d575816SJiwei Sun 28646d575816SJiwei Sun return -1; 28656d575816SJiwei Sun } 28666d575816SJiwei Sun 2867e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 2868e9db1310SAdrian Hunter const char *str, 2869e9db1310SAdrian Hunter int unset __maybe_unused) 2870e9db1310SAdrian Hunter { 2871e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 2872e9db1310SAdrian Hunter char *s, *p; 2873e9db1310SAdrian Hunter unsigned int mmap_pages; 2874e9db1310SAdrian Hunter int ret; 2875e9db1310SAdrian Hunter 2876e9db1310SAdrian Hunter if (!str) 2877e9db1310SAdrian Hunter return -EINVAL; 2878e9db1310SAdrian Hunter 2879e9db1310SAdrian Hunter s = strdup(str); 2880e9db1310SAdrian Hunter if (!s) 2881e9db1310SAdrian Hunter return -ENOMEM; 2882e9db1310SAdrian Hunter 2883e9db1310SAdrian Hunter p = strchr(s, ','); 2884e9db1310SAdrian Hunter if (p) 2885e9db1310SAdrian Hunter *p = '\0'; 2886e9db1310SAdrian Hunter 2887e9db1310SAdrian Hunter if (*s) { 288825f84702SArnaldo Carvalho de Melo ret = __evlist__parse_mmap_pages(&mmap_pages, s); 2889e9db1310SAdrian Hunter if (ret) 2890e9db1310SAdrian Hunter goto out_free; 2891e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 2892e9db1310SAdrian Hunter } 2893e9db1310SAdrian Hunter 2894e9db1310SAdrian Hunter if (!p) { 2895e9db1310SAdrian Hunter ret = 0; 2896e9db1310SAdrian Hunter goto out_free; 2897e9db1310SAdrian Hunter } 2898e9db1310SAdrian Hunter 289925f84702SArnaldo Carvalho de Melo ret = __evlist__parse_mmap_pages(&mmap_pages, p + 1); 2900e9db1310SAdrian Hunter if (ret) 2901e9db1310SAdrian Hunter goto out_free; 2902e9db1310SAdrian Hunter 2903e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 2904e9db1310SAdrian Hunter 2905e9db1310SAdrian Hunter out_free: 2906e9db1310SAdrian Hunter free(s); 2907e9db1310SAdrian Hunter return ret; 2908e9db1310SAdrian Hunter } 2909e9db1310SAdrian Hunter 29107248e308SAlexandre Truong void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __maybe_unused) 29117248e308SAlexandre Truong { 29127248e308SAlexandre Truong } 29137248e308SAlexandre Truong 29141d078ccbSAlexey Budankov static int parse_control_option(const struct option *opt, 29151d078ccbSAlexey Budankov const char *str, 29161d078ccbSAlexey Budankov int unset __maybe_unused) 29171d078ccbSAlexey Budankov { 29189864a66dSAdrian Hunter struct record_opts *opts = opt->value; 29191d078ccbSAlexey Budankov 2920a8fcbd26SAdrian Hunter return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close); 2921a8fcbd26SAdrian Hunter } 2922a8fcbd26SAdrian Hunter 29230c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 29240c582449SJiri Olsa { 29259521b5f2SJiri Olsa u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages); 29260c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 29270c582449SJiri Olsa 29280c582449SJiri Olsa wakeup_size /= 2; 29290c582449SJiri Olsa 29300c582449SJiri Olsa if (s->size < wakeup_size) { 29310c582449SJiri Olsa char buf[100]; 29320c582449SJiri Olsa 29330c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 29340c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 29350c582449SJiri Olsa "wakeup kernel buffer size (%s) " 29360c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 29370c582449SJiri Olsa } 29380c582449SJiri Olsa } 29390c582449SJiri Olsa 2940cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 2941cb4e1ebbSJiri Olsa { 2942cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 2943dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 2944dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 2945dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 2946dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 2947dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 2948dc0c6127SJiri Olsa { .tag = 0 }, 2949dc0c6127SJiri Olsa }; 2950bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 2951bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 2952bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 2953bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 2954bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 2955bfacbe3bSJiri Olsa { .tag = 0 }, 2956bfacbe3bSJiri Olsa }; 2957dc0c6127SJiri Olsa unsigned long val; 2958cb4e1ebbSJiri Olsa 2959899e5ffbSArnaldo Carvalho de Melo /* 2960899e5ffbSArnaldo Carvalho de Melo * If we're using --switch-output-events, then we imply its 2961899e5ffbSArnaldo Carvalho de Melo * --switch-output=signal, as we'll send a SIGUSR2 from the side band 2962899e5ffbSArnaldo Carvalho de Melo * thread to its parent. 2963899e5ffbSArnaldo Carvalho de Melo */ 2964b5f2511dSAlexey Bayduraev if (rec->switch_output_event_set) { 2965b5f2511dSAlexey Bayduraev if (record__threads_enabled(rec)) { 2966b5f2511dSAlexey Bayduraev pr_warning("WARNING: --switch-output-event option is not available in parallel streaming mode.\n"); 2967b5f2511dSAlexey Bayduraev return 0; 2968b5f2511dSAlexey Bayduraev } 2969899e5ffbSArnaldo Carvalho de Melo goto do_signal; 2970b5f2511dSAlexey Bayduraev } 2971899e5ffbSArnaldo Carvalho de Melo 2972cb4e1ebbSJiri Olsa if (!s->set) 2973cb4e1ebbSJiri Olsa return 0; 2974cb4e1ebbSJiri Olsa 2975b5f2511dSAlexey Bayduraev if (record__threads_enabled(rec)) { 2976b5f2511dSAlexey Bayduraev pr_warning("WARNING: --switch-output option is not available in parallel streaming mode.\n"); 2977b5f2511dSAlexey Bayduraev return 0; 2978b5f2511dSAlexey Bayduraev } 2979b5f2511dSAlexey Bayduraev 2980cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 2981899e5ffbSArnaldo Carvalho de Melo do_signal: 2982cb4e1ebbSJiri Olsa s->signal = true; 2983cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 2984dc0c6127SJiri Olsa goto enabled; 2985dc0c6127SJiri Olsa } 2986dc0c6127SJiri Olsa 2987dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 2988dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 2989dc0c6127SJiri Olsa s->size = val; 2990dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 2991dc0c6127SJiri Olsa goto enabled; 2992cb4e1ebbSJiri Olsa } 2993cb4e1ebbSJiri Olsa 2994bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 2995bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 2996bfacbe3bSJiri Olsa s->time = val; 2997bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 2998bfacbe3bSJiri Olsa s->str, s->time); 2999bfacbe3bSJiri Olsa goto enabled; 3000bfacbe3bSJiri Olsa } 3001bfacbe3bSJiri Olsa 3002cb4e1ebbSJiri Olsa return -1; 3003dc0c6127SJiri Olsa 3004dc0c6127SJiri Olsa enabled: 3005dc0c6127SJiri Olsa rec->timestamp_filename = true; 3006dc0c6127SJiri Olsa s->enabled = true; 30070c582449SJiri Olsa 30080c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 30090c582449SJiri Olsa switch_output_size_warn(rec); 30100c582449SJiri Olsa 3011dc0c6127SJiri Olsa return 0; 3012cb4e1ebbSJiri Olsa } 3013cb4e1ebbSJiri Olsa 3014e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 301586470930SIngo Molnar "perf record [<options>] [<command>]", 301686470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 301786470930SIngo Molnar NULL 301886470930SIngo Molnar }; 3019e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 302086470930SIngo Molnar 30216e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event, 30226e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 30236e0a9b3dSArnaldo Carvalho de Melo { 30246e0a9b3dSArnaldo Carvalho de Melo /* 30256e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 30266e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 30276e0a9b3dSArnaldo Carvalho de Melo */ 30286e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 30296e0a9b3dSArnaldo Carvalho de Melo return 0; 30306e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap(tool, event, sample, machine); 30316e0a9b3dSArnaldo Carvalho de Melo } 30326e0a9b3dSArnaldo Carvalho de Melo 30336e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event, 30346e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 30356e0a9b3dSArnaldo Carvalho de Melo { 30366e0a9b3dSArnaldo Carvalho de Melo /* 30376e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 30386e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 30396e0a9b3dSArnaldo Carvalho de Melo */ 30406e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 30416e0a9b3dSArnaldo Carvalho de Melo return 0; 30426e0a9b3dSArnaldo Carvalho de Melo 30436e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap2(tool, event, sample, machine); 30446e0a9b3dSArnaldo Carvalho de Melo } 30456e0a9b3dSArnaldo Carvalho de Melo 304666286ed3SAdrian Hunter static int process_timestamp_boundary(struct perf_tool *tool, 304766286ed3SAdrian Hunter union perf_event *event __maybe_unused, 304866286ed3SAdrian Hunter struct perf_sample *sample, 304966286ed3SAdrian Hunter struct machine *machine __maybe_unused) 305066286ed3SAdrian Hunter { 305166286ed3SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 305266286ed3SAdrian Hunter 305366286ed3SAdrian Hunter set_timestamp_boundary(rec, sample->time); 305466286ed3SAdrian Hunter return 0; 305566286ed3SAdrian Hunter } 305666286ed3SAdrian Hunter 305741b740b6SNamhyung Kim static int parse_record_synth_option(const struct option *opt, 305841b740b6SNamhyung Kim const char *str, 305941b740b6SNamhyung Kim int unset __maybe_unused) 306041b740b6SNamhyung Kim { 306141b740b6SNamhyung Kim struct record_opts *opts = opt->value; 306241b740b6SNamhyung Kim char *p = strdup(str); 306341b740b6SNamhyung Kim 306441b740b6SNamhyung Kim if (p == NULL) 306541b740b6SNamhyung Kim return -1; 306641b740b6SNamhyung Kim 306741b740b6SNamhyung Kim opts->synth = parse_synth_opt(p); 306841b740b6SNamhyung Kim free(p); 306941b740b6SNamhyung Kim 307041b740b6SNamhyung Kim if (opts->synth < 0) { 307141b740b6SNamhyung Kim pr_err("Invalid synth option: %s\n", str); 307241b740b6SNamhyung Kim return -1; 307341b740b6SNamhyung Kim } 307441b740b6SNamhyung Kim return 0; 307541b740b6SNamhyung Kim } 307641b740b6SNamhyung Kim 3077d20deb64SArnaldo Carvalho de Melo /* 30788c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 30798c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 3080d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 3081d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 3082d20deb64SArnaldo Carvalho de Melo * 3083d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 3084d20deb64SArnaldo Carvalho de Melo * 3085d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 3086d20deb64SArnaldo Carvalho de Melo */ 30878c6f45a7SArnaldo Carvalho de Melo static struct record record = { 3088d20deb64SArnaldo Carvalho de Melo .opts = { 30898affc2b8SAndi Kleen .sample_time = true, 3090d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 3091d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 3092d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 3093447a6013SArnaldo Carvalho de Melo .freq = 4000, 3094d1cb9fceSNamhyung Kim .target = { 3095d1cb9fceSNamhyung Kim .uses_mmap = true, 30963aa5939dSAdrian Hunter .default_per_cpu = true, 3097d1cb9fceSNamhyung Kim }, 3098470530bbSAlexey Budankov .mmap_flush = MMAP_FLUSH_DEFAULT, 3099d99c22eaSStephane Eranian .nr_threads_synthesize = 1, 31001d078ccbSAlexey Budankov .ctl_fd = -1, 31011d078ccbSAlexey Budankov .ctl_fd_ack = -1, 310241b740b6SNamhyung Kim .synth = PERF_SYNTH_ALL, 3103d20deb64SArnaldo Carvalho de Melo }, 3104e3d59112SNamhyung Kim .tool = { 3105e3d59112SNamhyung Kim .sample = process_sample_event, 3106e3d59112SNamhyung Kim .fork = perf_event__process_fork, 3107cca8482cSAdrian Hunter .exit = perf_event__process_exit, 3108e3d59112SNamhyung Kim .comm = perf_event__process_comm, 3109f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 31106e0a9b3dSArnaldo Carvalho de Melo .mmap = build_id__process_mmap, 31116e0a9b3dSArnaldo Carvalho de Melo .mmap2 = build_id__process_mmap2, 311266286ed3SAdrian Hunter .itrace_start = process_timestamp_boundary, 311366286ed3SAdrian Hunter .aux = process_timestamp_boundary, 3114cca8482cSAdrian Hunter .ordered_events = true, 3115e3d59112SNamhyung Kim }, 3116d20deb64SArnaldo Carvalho de Melo }; 31177865e817SFrederic Weisbecker 311876a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 311976a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 312061eaa3beSArnaldo Carvalho de Melo 31210aab2136SWang Nan static bool dry_run; 31220aab2136SWang Nan 3123d20deb64SArnaldo Carvalho de Melo /* 3124d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 3125d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 3126b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 31277b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 3128d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 3129d20deb64SArnaldo Carvalho de Melo */ 3130efd21307SJiri Olsa static struct option __record_options[] = { 3131d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 313286470930SIngo Molnar "event selector. use 'perf list' to list available events", 3133f120f9d5SJiri Olsa parse_events_option), 3134d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 3135c171b552SLi Zefan "event filter", parse_filter), 31364ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 31374ba1faa1SWang Nan NULL, "don't record events from perf itself", 31384ba1faa1SWang Nan exclude_perf), 3139bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 3140d6d901c2SZhang, Yanmin "record events on existing process id"), 3141bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 3142d6d901c2SZhang, Yanmin "record events on existing thread id"), 3143d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 314486470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 3145509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 3146acac03faSKirill Smelkov "collect data without buffering"), 3147d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 3148daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 3149bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 315086470930SIngo Molnar "system-wide collection from all CPUs"), 3151bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 3152c45c6ea2SStephane Eranian "list of cpus to monitor"), 3153d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 31542d4f2799SJiri Olsa OPT_STRING('o', "output", &record.data.path, "file", 315586470930SIngo Molnar "output file name"), 315669e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 315769e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 31582e6cdf99SStephane Eranian "child tasks do not inherit counters"), 31594ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 31604ea648aeSWang Nan "synthesize non-sample events at the end of output"), 3161626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 3162a060c1f1SWei Li OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"), 3163b09c2364SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq, 3164b09c2364SArnaldo Carvalho de Melo "Fail if the specified frequency can't be used"), 316567230479SArnaldo Carvalho de Melo OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'", 316667230479SArnaldo Carvalho de Melo "profile at this frequency", 316767230479SArnaldo Carvalho de Melo record__parse_freq), 3168e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 3169e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 3170e9db1310SAdrian Hunter record__parse_mmap_pages), 3171470530bbSAlexey Budankov OPT_CALLBACK(0, "mmap-flush", &record.opts, "number", 3172470530bbSAlexey Budankov "Minimal number of bytes that is extracted from mmap data pages (default: 1)", 3173470530bbSAlexey Budankov record__mmap_flush_parse), 3174d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "group", &record.opts.group, 317543bece79SLin Ming "put the counters into a counter group"), 31762ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 317709b0fd45SJiri Olsa NULL, "enables call-graph recording" , 317809b0fd45SJiri Olsa &record_callchain_opt), 317909b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 318076a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 318109b0fd45SJiri Olsa &record_parse_callchain_opt), 3182c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 31833da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 3184b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 3185d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 3186649c48a9SPeter Zijlstra "per thread counts"), 318756100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 31883b0a5daaSKan Liang OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, 31893b0a5daaSKan Liang "Record the sample physical addresses"), 3190542b88fdSKan Liang OPT_BOOLEAN(0, "data-page-size", &record.opts.sample_data_page_size, 3191542b88fdSKan Liang "Record the sampled data address data page size"), 3192c1de7f3dSKan Liang OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size, 3193c1de7f3dSKan Liang "Record the sampled code address (ip) page size"), 3194b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 31953abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 31963abebc55SAdrian Hunter &record.opts.sample_time_set, 31973abebc55SAdrian Hunter "Record the sample timestamps"), 3198f290aa1fSJiri Olsa OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set, 3199f290aa1fSJiri Olsa "Record the sample period"), 3200d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 3201649c48a9SPeter Zijlstra "don't sample"), 3202d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 3203d2db9a98SWang Nan &record.no_buildid_cache_set, 3204a1ac1d3cSStephane Eranian "do not update the buildid cache"), 3205d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 3206d2db9a98SWang Nan &record.no_buildid_set, 3207baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 3208d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 3209023695d9SStephane Eranian "monitor event in cgroup name only", 3210023695d9SStephane Eranian parse_cgroups), 321168cd3b45SAlexey Budankov OPT_INTEGER('D', "delay", &record.opts.initial_delay, 321268cd3b45SAlexey Budankov "ms to wait before starting measurement after program start (-1: start with events disabled)"), 3213eeb399b5SAdrian Hunter OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"), 3214bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 3215bea03405SNamhyung Kim "user to profile"), 3216a5aabdacSStephane Eranian 3217a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 3218a5aabdacSStephane Eranian "branch any", "sample any taken branches", 3219a5aabdacSStephane Eranian parse_branch_stack), 3220a5aabdacSStephane Eranian 3221a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 3222a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 3223bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 322405484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 322505484298SAndi Kleen "sample by weight (on special events only)"), 3226475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 3227475eeab9SAndi Kleen "sample transaction flags (special events only)"), 32283aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 32293aa5939dSAdrian Hunter "use per-thread mmaps"), 3230bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 3231bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 3232aeea9062SKan Liang " use '-I?' to list register names", parse_intr_regs), 323384c41742SAndi Kleen OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register", 323484c41742SAndi Kleen "sample selected machine registers on interrupt," 3235aeea9062SKan Liang " use '--user-regs=?' to list register names", parse_user_regs), 323685c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 323785c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 3238814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 3239814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 3240814c8c38SPeter Zijlstra parse_clockid), 32412dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 32422dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 3243c0a6de06SAdrian Hunter OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts, 3244c0a6de06SAdrian Hunter "opts", "sample AUX area", ""), 32453fcb10e4SMark Drayton OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 32469d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 3247f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 3248f3b3614aSHari Bathini "Record namespaces events"), 32498fb4b679SNamhyung Kim OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup, 32508fb4b679SNamhyung Kim "Record cgroup events"), 325116b4b4e1SAdrian Hunter OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events, 325216b4b4e1SAdrian Hunter &record.opts.record_switch_events_set, 3253b757bb09SAdrian Hunter "Record context switch events"), 325485723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 325585723885SJiri Olsa "Configure all used events to run in kernel space.", 325685723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 325785723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 325885723885SJiri Olsa "Configure all used events to run in user space.", 325985723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 326053651b28Syuzhoujian OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains, 326153651b28Syuzhoujian "collect kernel callchains"), 326253651b28Syuzhoujian OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains, 326353651b28Syuzhoujian "collect user callchains"), 326471dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 326571dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 326671dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 326771dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 32687efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 32697efe0e03SHe Kuang "file", "vmlinux pathname"), 32706156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 32716156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 3272e29386c8SJiri Olsa OPT_BOOLEAN(0, "buildid-mmap", &record.buildid_mmap, 3273e29386c8SJiri Olsa "Record build-id in map events"), 3274ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 3275ecfd7a9cSWang Nan "append timestamp to output filename"), 327668588bafSJin Yao OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary, 327768588bafSJin Yao "Record timestamp boundary (time of first/last samples)"), 3278cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 3279c38dab7dSAndi Kleen &record.switch_output.set, "signal or size[BKMG] or time[smhd]", 3280c38dab7dSAndi Kleen "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold", 3281dc0c6127SJiri Olsa "signal"), 3282899e5ffbSArnaldo Carvalho de Melo OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event", 3283899e5ffbSArnaldo Carvalho de Melo "switch output event selector. use 'perf list' to list available events", 3284899e5ffbSArnaldo Carvalho de Melo parse_events_option_new_evlist), 328503724b2eSAndi Kleen OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files, 328603724b2eSAndi Kleen "Limit number of switch output generated files"), 32870aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 32880aab2136SWang Nan "Parse options then exit"), 3289d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 329093f20c0fSAlexey Budankov OPT_CALLBACK_OPTARG(0, "aio", &record.opts, 329193f20c0fSAlexey Budankov &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)", 3292d3d1af6fSAlexey Budankov record__aio_parse), 3293d3d1af6fSAlexey Budankov #endif 3294f4fe11b7SAlexey Budankov OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu", 3295f4fe11b7SAlexey Budankov "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer", 3296f4fe11b7SAlexey Budankov record__parse_affinity), 3297504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 3298b5f2511dSAlexey Bayduraev OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, "n", 3299b5f2511dSAlexey Bayduraev "Compress records using specified level (default: 1 - fastest compression, 22 - greatest compression)", 3300504c1ad1SAlexey Budankov record__parse_comp_level), 3301504c1ad1SAlexey Budankov #endif 33026d575816SJiwei Sun OPT_CALLBACK(0, "max-size", &record.output_max_size, 33036d575816SJiwei Sun "size", "Limit the maximum size of the output file", parse_output_max_size), 3304d99c22eaSStephane Eranian OPT_UINTEGER(0, "num-thread-synthesize", 3305d99c22eaSStephane Eranian &record.opts.nr_threads_synthesize, 3306d99c22eaSStephane Eranian "number of threads to run for event synthesis"), 330770943490SStephane Eranian #ifdef HAVE_LIBPFM 330870943490SStephane Eranian OPT_CALLBACK(0, "pfm-events", &record.evlist, "event", 330970943490SStephane Eranian "libpfm4 event selector. use 'perf list' to list available events", 331070943490SStephane Eranian parse_libpfm_events_option), 331170943490SStephane Eranian #endif 3312a8fcbd26SAdrian Hunter OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]", 3313d20aff15SAdrian Hunter "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n" 3314d20aff15SAdrian Hunter "\t\t\t 'snapshot': AUX area tracing snapshot).\n" 3315a8fcbd26SAdrian Hunter "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n" 3316a8fcbd26SAdrian Hunter "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.", 33171d078ccbSAlexey Budankov parse_control_option), 331841b740b6SNamhyung Kim OPT_CALLBACK(0, "synth", &record.opts, "no|all|task|mmap|cgroup", 331941b740b6SNamhyung Kim "Fine-tune event synthesis: default=all", parse_record_synth_option), 33209bce13eaSJiri Olsa OPT_STRING_OPTARG_SET(0, "debuginfod", &record.debuginfod.urls, 33219bce13eaSJiri Olsa &record.debuginfod.set, "debuginfod urls", 33229bce13eaSJiri Olsa "Enable debuginfod data retrieval from DEBUGINFOD_URLS or specified urls", 33239bce13eaSJiri Olsa "system"), 332406380a84SAlexey Bayduraev OPT_CALLBACK_OPTARG(0, "threads", &record.opts, NULL, "spec", 332506380a84SAlexey Bayduraev "write collected trace data into several data files using parallel threads", 332606380a84SAlexey Bayduraev record__parse_threads), 332786470930SIngo Molnar OPT_END() 332886470930SIngo Molnar }; 332986470930SIngo Molnar 3330e5b2c207SNamhyung Kim struct option *record_options = __record_options; 3331e5b2c207SNamhyung Kim 33327954f716SAlexey Bayduraev static void record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpu_map *cpus) 33337954f716SAlexey Bayduraev { 3334*0255571aSIan Rogers struct perf_cpu cpu; 3335*0255571aSIan Rogers int idx; 33367954f716SAlexey Bayduraev 333723380e4dSAlexey Bayduraev if (cpu_map__is_dummy(cpus)) 333823380e4dSAlexey Bayduraev return; 333923380e4dSAlexey Bayduraev 3340*0255571aSIan Rogers perf_cpu_map__for_each_cpu(cpu, idx, cpus) 3341*0255571aSIan Rogers set_bit(cpu.cpu, mask->bits); 33427954f716SAlexey Bayduraev } 33437954f716SAlexey Bayduraev 3344f466e5edSAlexey Bayduraev static int record__mmap_cpu_mask_init_spec(struct mmap_cpu_mask *mask, const char *mask_spec) 3345f466e5edSAlexey Bayduraev { 3346f466e5edSAlexey Bayduraev struct perf_cpu_map *cpus; 3347f466e5edSAlexey Bayduraev 3348f466e5edSAlexey Bayduraev cpus = perf_cpu_map__new(mask_spec); 3349f466e5edSAlexey Bayduraev if (!cpus) 3350f466e5edSAlexey Bayduraev return -ENOMEM; 3351f466e5edSAlexey Bayduraev 3352f466e5edSAlexey Bayduraev bitmap_zero(mask->bits, mask->nbits); 3353f466e5edSAlexey Bayduraev record__mmap_cpu_mask_init(mask, cpus); 3354f466e5edSAlexey Bayduraev perf_cpu_map__put(cpus); 3355f466e5edSAlexey Bayduraev 3356f466e5edSAlexey Bayduraev return 0; 3357f466e5edSAlexey Bayduraev } 3358f466e5edSAlexey Bayduraev 33597954f716SAlexey Bayduraev static void record__free_thread_masks(struct record *rec, int nr_threads) 33607954f716SAlexey Bayduraev { 33617954f716SAlexey Bayduraev int t; 33627954f716SAlexey Bayduraev 33637954f716SAlexey Bayduraev if (rec->thread_masks) 33647954f716SAlexey Bayduraev for (t = 0; t < nr_threads; t++) 33657954f716SAlexey Bayduraev record__thread_mask_free(&rec->thread_masks[t]); 33667954f716SAlexey Bayduraev 33677954f716SAlexey Bayduraev zfree(&rec->thread_masks); 33687954f716SAlexey Bayduraev } 33697954f716SAlexey Bayduraev 33707954f716SAlexey Bayduraev static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr_bits) 33717954f716SAlexey Bayduraev { 33727954f716SAlexey Bayduraev int t, ret; 33737954f716SAlexey Bayduraev 33747954f716SAlexey Bayduraev rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks))); 33757954f716SAlexey Bayduraev if (!rec->thread_masks) { 33767954f716SAlexey Bayduraev pr_err("Failed to allocate thread masks\n"); 33777954f716SAlexey Bayduraev return -ENOMEM; 33787954f716SAlexey Bayduraev } 33797954f716SAlexey Bayduraev 33807954f716SAlexey Bayduraev for (t = 0; t < nr_threads; t++) { 33817954f716SAlexey Bayduraev ret = record__thread_mask_alloc(&rec->thread_masks[t], nr_bits); 33827954f716SAlexey Bayduraev if (ret) { 33837954f716SAlexey Bayduraev pr_err("Failed to allocate thread masks[%d]\n", t); 33847954f716SAlexey Bayduraev goto out_free; 33857954f716SAlexey Bayduraev } 33867954f716SAlexey Bayduraev } 33877954f716SAlexey Bayduraev 33887954f716SAlexey Bayduraev return 0; 33897954f716SAlexey Bayduraev 33907954f716SAlexey Bayduraev out_free: 33917954f716SAlexey Bayduraev record__free_thread_masks(rec, nr_threads); 33927954f716SAlexey Bayduraev 33937954f716SAlexey Bayduraev return ret; 33947954f716SAlexey Bayduraev } 33957954f716SAlexey Bayduraev 339606380a84SAlexey Bayduraev static int record__init_thread_cpu_masks(struct record *rec, struct perf_cpu_map *cpus) 339706380a84SAlexey Bayduraev { 339806380a84SAlexey Bayduraev int t, ret, nr_cpus = perf_cpu_map__nr(cpus); 339906380a84SAlexey Bayduraev 340006380a84SAlexey Bayduraev ret = record__alloc_thread_masks(rec, nr_cpus, cpu__max_cpu().cpu); 340106380a84SAlexey Bayduraev if (ret) 340206380a84SAlexey Bayduraev return ret; 340306380a84SAlexey Bayduraev 340406380a84SAlexey Bayduraev rec->nr_threads = nr_cpus; 340506380a84SAlexey Bayduraev pr_debug("nr_threads: %d\n", rec->nr_threads); 340606380a84SAlexey Bayduraev 340706380a84SAlexey Bayduraev for (t = 0; t < rec->nr_threads; t++) { 3408*0255571aSIan Rogers set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].maps.bits); 3409*0255571aSIan Rogers set_bit(perf_cpu_map__cpu(cpus, t).cpu, rec->thread_masks[t].affinity.bits); 341006380a84SAlexey Bayduraev if (verbose) { 341106380a84SAlexey Bayduraev pr_debug("thread_masks[%d]: ", t); 341206380a84SAlexey Bayduraev mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps"); 341306380a84SAlexey Bayduraev pr_debug("thread_masks[%d]: ", t); 341406380a84SAlexey Bayduraev mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity"); 341506380a84SAlexey Bayduraev } 341606380a84SAlexey Bayduraev } 341706380a84SAlexey Bayduraev 341806380a84SAlexey Bayduraev return 0; 341906380a84SAlexey Bayduraev } 342006380a84SAlexey Bayduraev 3421f466e5edSAlexey Bayduraev static int record__init_thread_masks_spec(struct record *rec, struct perf_cpu_map *cpus, 3422f466e5edSAlexey Bayduraev const char **maps_spec, const char **affinity_spec, 3423f466e5edSAlexey Bayduraev u32 nr_spec) 3424f466e5edSAlexey Bayduraev { 3425f466e5edSAlexey Bayduraev u32 s; 3426f466e5edSAlexey Bayduraev int ret = 0, t = 0; 3427f466e5edSAlexey Bayduraev struct mmap_cpu_mask cpus_mask; 3428f466e5edSAlexey Bayduraev struct thread_mask thread_mask, full_mask, *thread_masks; 3429f466e5edSAlexey Bayduraev 3430f466e5edSAlexey Bayduraev ret = record__mmap_cpu_mask_alloc(&cpus_mask, cpu__max_cpu().cpu); 3431f466e5edSAlexey Bayduraev if (ret) { 3432f466e5edSAlexey Bayduraev pr_err("Failed to allocate CPUs mask\n"); 3433f466e5edSAlexey Bayduraev return ret; 3434f466e5edSAlexey Bayduraev } 3435f466e5edSAlexey Bayduraev record__mmap_cpu_mask_init(&cpus_mask, cpus); 3436f466e5edSAlexey Bayduraev 3437f466e5edSAlexey Bayduraev ret = record__thread_mask_alloc(&full_mask, cpu__max_cpu().cpu); 3438f466e5edSAlexey Bayduraev if (ret) { 3439f466e5edSAlexey Bayduraev pr_err("Failed to allocate full mask\n"); 3440f466e5edSAlexey Bayduraev goto out_free_cpu_mask; 3441f466e5edSAlexey Bayduraev } 3442f466e5edSAlexey Bayduraev 3443f466e5edSAlexey Bayduraev ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu); 3444f466e5edSAlexey Bayduraev if (ret) { 3445f466e5edSAlexey Bayduraev pr_err("Failed to allocate thread mask\n"); 3446f466e5edSAlexey Bayduraev goto out_free_full_and_cpu_masks; 3447f466e5edSAlexey Bayduraev } 3448f466e5edSAlexey Bayduraev 3449f466e5edSAlexey Bayduraev for (s = 0; s < nr_spec; s++) { 3450f466e5edSAlexey Bayduraev ret = record__mmap_cpu_mask_init_spec(&thread_mask.maps, maps_spec[s]); 3451f466e5edSAlexey Bayduraev if (ret) { 3452f466e5edSAlexey Bayduraev pr_err("Failed to initialize maps thread mask\n"); 3453f466e5edSAlexey Bayduraev goto out_free; 3454f466e5edSAlexey Bayduraev } 3455f466e5edSAlexey Bayduraev ret = record__mmap_cpu_mask_init_spec(&thread_mask.affinity, affinity_spec[s]); 3456f466e5edSAlexey Bayduraev if (ret) { 3457f466e5edSAlexey Bayduraev pr_err("Failed to initialize affinity thread mask\n"); 3458f466e5edSAlexey Bayduraev goto out_free; 3459f466e5edSAlexey Bayduraev } 3460f466e5edSAlexey Bayduraev 3461f466e5edSAlexey Bayduraev /* ignore invalid CPUs but do not allow empty masks */ 3462f466e5edSAlexey Bayduraev if (!bitmap_and(thread_mask.maps.bits, thread_mask.maps.bits, 3463f466e5edSAlexey Bayduraev cpus_mask.bits, thread_mask.maps.nbits)) { 3464f466e5edSAlexey Bayduraev pr_err("Empty maps mask: %s\n", maps_spec[s]); 3465f466e5edSAlexey Bayduraev ret = -EINVAL; 3466f466e5edSAlexey Bayduraev goto out_free; 3467f466e5edSAlexey Bayduraev } 3468f466e5edSAlexey Bayduraev if (!bitmap_and(thread_mask.affinity.bits, thread_mask.affinity.bits, 3469f466e5edSAlexey Bayduraev cpus_mask.bits, thread_mask.affinity.nbits)) { 3470f466e5edSAlexey Bayduraev pr_err("Empty affinity mask: %s\n", affinity_spec[s]); 3471f466e5edSAlexey Bayduraev ret = -EINVAL; 3472f466e5edSAlexey Bayduraev goto out_free; 3473f466e5edSAlexey Bayduraev } 3474f466e5edSAlexey Bayduraev 3475f466e5edSAlexey Bayduraev /* do not allow intersection with other masks (full_mask) */ 3476f466e5edSAlexey Bayduraev if (bitmap_intersects(thread_mask.maps.bits, full_mask.maps.bits, 3477f466e5edSAlexey Bayduraev thread_mask.maps.nbits)) { 3478f466e5edSAlexey Bayduraev pr_err("Intersecting maps mask: %s\n", maps_spec[s]); 3479f466e5edSAlexey Bayduraev ret = -EINVAL; 3480f466e5edSAlexey Bayduraev goto out_free; 3481f466e5edSAlexey Bayduraev } 3482f466e5edSAlexey Bayduraev if (bitmap_intersects(thread_mask.affinity.bits, full_mask.affinity.bits, 3483f466e5edSAlexey Bayduraev thread_mask.affinity.nbits)) { 3484f466e5edSAlexey Bayduraev pr_err("Intersecting affinity mask: %s\n", affinity_spec[s]); 3485f466e5edSAlexey Bayduraev ret = -EINVAL; 3486f466e5edSAlexey Bayduraev goto out_free; 3487f466e5edSAlexey Bayduraev } 3488f466e5edSAlexey Bayduraev 3489f466e5edSAlexey Bayduraev bitmap_or(full_mask.maps.bits, full_mask.maps.bits, 3490f466e5edSAlexey Bayduraev thread_mask.maps.bits, full_mask.maps.nbits); 3491f466e5edSAlexey Bayduraev bitmap_or(full_mask.affinity.bits, full_mask.affinity.bits, 3492f466e5edSAlexey Bayduraev thread_mask.affinity.bits, full_mask.maps.nbits); 3493f466e5edSAlexey Bayduraev 3494f466e5edSAlexey Bayduraev thread_masks = realloc(rec->thread_masks, (t + 1) * sizeof(struct thread_mask)); 3495f466e5edSAlexey Bayduraev if (!thread_masks) { 3496f466e5edSAlexey Bayduraev pr_err("Failed to reallocate thread masks\n"); 3497f466e5edSAlexey Bayduraev ret = -ENOMEM; 3498f466e5edSAlexey Bayduraev goto out_free; 3499f466e5edSAlexey Bayduraev } 3500f466e5edSAlexey Bayduraev rec->thread_masks = thread_masks; 3501f466e5edSAlexey Bayduraev rec->thread_masks[t] = thread_mask; 3502f466e5edSAlexey Bayduraev if (verbose) { 3503f466e5edSAlexey Bayduraev pr_debug("thread_masks[%d]: ", t); 3504f466e5edSAlexey Bayduraev mmap_cpu_mask__scnprintf(&rec->thread_masks[t].maps, "maps"); 3505f466e5edSAlexey Bayduraev pr_debug("thread_masks[%d]: ", t); 3506f466e5edSAlexey Bayduraev mmap_cpu_mask__scnprintf(&rec->thread_masks[t].affinity, "affinity"); 3507f466e5edSAlexey Bayduraev } 3508f466e5edSAlexey Bayduraev t++; 3509f466e5edSAlexey Bayduraev ret = record__thread_mask_alloc(&thread_mask, cpu__max_cpu().cpu); 3510f466e5edSAlexey Bayduraev if (ret) { 3511f466e5edSAlexey Bayduraev pr_err("Failed to allocate thread mask\n"); 3512f466e5edSAlexey Bayduraev goto out_free_full_and_cpu_masks; 3513f466e5edSAlexey Bayduraev } 3514f466e5edSAlexey Bayduraev } 3515f466e5edSAlexey Bayduraev rec->nr_threads = t; 3516f466e5edSAlexey Bayduraev pr_debug("nr_threads: %d\n", rec->nr_threads); 3517f466e5edSAlexey Bayduraev if (!rec->nr_threads) 3518f466e5edSAlexey Bayduraev ret = -EINVAL; 3519f466e5edSAlexey Bayduraev 3520f466e5edSAlexey Bayduraev out_free: 3521f466e5edSAlexey Bayduraev record__thread_mask_free(&thread_mask); 3522f466e5edSAlexey Bayduraev out_free_full_and_cpu_masks: 3523f466e5edSAlexey Bayduraev record__thread_mask_free(&full_mask); 3524f466e5edSAlexey Bayduraev out_free_cpu_mask: 3525f466e5edSAlexey Bayduraev record__mmap_cpu_mask_free(&cpus_mask); 3526f466e5edSAlexey Bayduraev 3527f466e5edSAlexey Bayduraev return ret; 3528f466e5edSAlexey Bayduraev } 3529f466e5edSAlexey Bayduraev 3530f466e5edSAlexey Bayduraev static int record__init_thread_core_masks(struct record *rec, struct perf_cpu_map *cpus) 3531f466e5edSAlexey Bayduraev { 3532f466e5edSAlexey Bayduraev int ret; 3533f466e5edSAlexey Bayduraev struct cpu_topology *topo; 3534f466e5edSAlexey Bayduraev 3535f466e5edSAlexey Bayduraev topo = cpu_topology__new(); 3536f466e5edSAlexey Bayduraev if (!topo) { 3537f466e5edSAlexey Bayduraev pr_err("Failed to allocate CPU topology\n"); 3538f466e5edSAlexey Bayduraev return -ENOMEM; 3539f466e5edSAlexey Bayduraev } 3540f466e5edSAlexey Bayduraev 3541f466e5edSAlexey Bayduraev ret = record__init_thread_masks_spec(rec, cpus, topo->core_cpus_list, 3542f466e5edSAlexey Bayduraev topo->core_cpus_list, topo->core_cpus_lists); 3543f466e5edSAlexey Bayduraev cpu_topology__delete(topo); 3544f466e5edSAlexey Bayduraev 3545f466e5edSAlexey Bayduraev return ret; 3546f466e5edSAlexey Bayduraev } 3547f466e5edSAlexey Bayduraev 3548f466e5edSAlexey Bayduraev static int record__init_thread_package_masks(struct record *rec, struct perf_cpu_map *cpus) 3549f466e5edSAlexey Bayduraev { 3550f466e5edSAlexey Bayduraev int ret; 3551f466e5edSAlexey Bayduraev struct cpu_topology *topo; 3552f466e5edSAlexey Bayduraev 3553f466e5edSAlexey Bayduraev topo = cpu_topology__new(); 3554f466e5edSAlexey Bayduraev if (!topo) { 3555f466e5edSAlexey Bayduraev pr_err("Failed to allocate CPU topology\n"); 3556f466e5edSAlexey Bayduraev return -ENOMEM; 3557f466e5edSAlexey Bayduraev } 3558f466e5edSAlexey Bayduraev 3559f466e5edSAlexey Bayduraev ret = record__init_thread_masks_spec(rec, cpus, topo->package_cpus_list, 3560f466e5edSAlexey Bayduraev topo->package_cpus_list, topo->package_cpus_lists); 3561f466e5edSAlexey Bayduraev cpu_topology__delete(topo); 3562f466e5edSAlexey Bayduraev 3563f466e5edSAlexey Bayduraev return ret; 3564f466e5edSAlexey Bayduraev } 3565f466e5edSAlexey Bayduraev 3566f466e5edSAlexey Bayduraev static int record__init_thread_numa_masks(struct record *rec, struct perf_cpu_map *cpus) 3567f466e5edSAlexey Bayduraev { 3568f466e5edSAlexey Bayduraev u32 s; 3569f466e5edSAlexey Bayduraev int ret; 3570f466e5edSAlexey Bayduraev const char **spec; 3571f466e5edSAlexey Bayduraev struct numa_topology *topo; 3572f466e5edSAlexey Bayduraev 3573f466e5edSAlexey Bayduraev topo = numa_topology__new(); 3574f466e5edSAlexey Bayduraev if (!topo) { 3575f466e5edSAlexey Bayduraev pr_err("Failed to allocate NUMA topology\n"); 3576f466e5edSAlexey Bayduraev return -ENOMEM; 3577f466e5edSAlexey Bayduraev } 3578f466e5edSAlexey Bayduraev 3579f466e5edSAlexey Bayduraev spec = zalloc(topo->nr * sizeof(char *)); 3580f466e5edSAlexey Bayduraev if (!spec) { 3581f466e5edSAlexey Bayduraev pr_err("Failed to allocate NUMA spec\n"); 3582f466e5edSAlexey Bayduraev ret = -ENOMEM; 3583f466e5edSAlexey Bayduraev goto out_delete_topo; 3584f466e5edSAlexey Bayduraev } 3585f466e5edSAlexey Bayduraev for (s = 0; s < topo->nr; s++) 3586f466e5edSAlexey Bayduraev spec[s] = topo->nodes[s].cpus; 3587f466e5edSAlexey Bayduraev 3588f466e5edSAlexey Bayduraev ret = record__init_thread_masks_spec(rec, cpus, spec, spec, topo->nr); 3589f466e5edSAlexey Bayduraev 3590f466e5edSAlexey Bayduraev zfree(&spec); 3591f466e5edSAlexey Bayduraev 3592f466e5edSAlexey Bayduraev out_delete_topo: 3593f466e5edSAlexey Bayduraev numa_topology__delete(topo); 3594f466e5edSAlexey Bayduraev 3595f466e5edSAlexey Bayduraev return ret; 3596f466e5edSAlexey Bayduraev } 3597f466e5edSAlexey Bayduraev 3598f466e5edSAlexey Bayduraev static int record__init_thread_user_masks(struct record *rec, struct perf_cpu_map *cpus) 3599f466e5edSAlexey Bayduraev { 3600f466e5edSAlexey Bayduraev int t, ret; 3601f466e5edSAlexey Bayduraev u32 s, nr_spec = 0; 3602f466e5edSAlexey Bayduraev char **maps_spec = NULL, **affinity_spec = NULL, **tmp_spec; 3603f466e5edSAlexey Bayduraev char *user_spec, *spec, *spec_ptr, *mask, *mask_ptr, *dup_mask = NULL; 3604f466e5edSAlexey Bayduraev 3605f466e5edSAlexey Bayduraev for (t = 0, user_spec = (char *)rec->opts.threads_user_spec; ; t++, user_spec = NULL) { 3606f466e5edSAlexey Bayduraev spec = strtok_r(user_spec, ":", &spec_ptr); 3607f466e5edSAlexey Bayduraev if (spec == NULL) 3608f466e5edSAlexey Bayduraev break; 3609f466e5edSAlexey Bayduraev pr_debug2("threads_spec[%d]: %s\n", t, spec); 3610f466e5edSAlexey Bayduraev mask = strtok_r(spec, "/", &mask_ptr); 3611f466e5edSAlexey Bayduraev if (mask == NULL) 3612f466e5edSAlexey Bayduraev break; 3613f466e5edSAlexey Bayduraev pr_debug2(" maps mask: %s\n", mask); 3614f466e5edSAlexey Bayduraev tmp_spec = realloc(maps_spec, (nr_spec + 1) * sizeof(char *)); 3615f466e5edSAlexey Bayduraev if (!tmp_spec) { 3616f466e5edSAlexey Bayduraev pr_err("Failed to reallocate maps spec\n"); 3617f466e5edSAlexey Bayduraev ret = -ENOMEM; 3618f466e5edSAlexey Bayduraev goto out_free; 3619f466e5edSAlexey Bayduraev } 3620f466e5edSAlexey Bayduraev maps_spec = tmp_spec; 3621f466e5edSAlexey Bayduraev maps_spec[nr_spec] = dup_mask = strdup(mask); 3622f466e5edSAlexey Bayduraev if (!maps_spec[nr_spec]) { 3623f466e5edSAlexey Bayduraev pr_err("Failed to allocate maps spec[%d]\n", nr_spec); 3624f466e5edSAlexey Bayduraev ret = -ENOMEM; 3625f466e5edSAlexey Bayduraev goto out_free; 3626f466e5edSAlexey Bayduraev } 3627f466e5edSAlexey Bayduraev mask = strtok_r(NULL, "/", &mask_ptr); 3628f466e5edSAlexey Bayduraev if (mask == NULL) { 3629f466e5edSAlexey Bayduraev pr_err("Invalid thread maps or affinity specs\n"); 3630f466e5edSAlexey Bayduraev ret = -EINVAL; 3631f466e5edSAlexey Bayduraev goto out_free; 3632f466e5edSAlexey Bayduraev } 3633f466e5edSAlexey Bayduraev pr_debug2(" affinity mask: %s\n", mask); 3634f466e5edSAlexey Bayduraev tmp_spec = realloc(affinity_spec, (nr_spec + 1) * sizeof(char *)); 3635f466e5edSAlexey Bayduraev if (!tmp_spec) { 3636f466e5edSAlexey Bayduraev pr_err("Failed to reallocate affinity spec\n"); 3637f466e5edSAlexey Bayduraev ret = -ENOMEM; 3638f466e5edSAlexey Bayduraev goto out_free; 3639f466e5edSAlexey Bayduraev } 3640f466e5edSAlexey Bayduraev affinity_spec = tmp_spec; 3641f466e5edSAlexey Bayduraev affinity_spec[nr_spec] = strdup(mask); 3642f466e5edSAlexey Bayduraev if (!affinity_spec[nr_spec]) { 3643f466e5edSAlexey Bayduraev pr_err("Failed to allocate affinity spec[%d]\n", nr_spec); 3644f466e5edSAlexey Bayduraev ret = -ENOMEM; 3645f466e5edSAlexey Bayduraev goto out_free; 3646f466e5edSAlexey Bayduraev } 3647f466e5edSAlexey Bayduraev dup_mask = NULL; 3648f466e5edSAlexey Bayduraev nr_spec++; 3649f466e5edSAlexey Bayduraev } 3650f466e5edSAlexey Bayduraev 3651f466e5edSAlexey Bayduraev ret = record__init_thread_masks_spec(rec, cpus, (const char **)maps_spec, 3652f466e5edSAlexey Bayduraev (const char **)affinity_spec, nr_spec); 3653f466e5edSAlexey Bayduraev 3654f466e5edSAlexey Bayduraev out_free: 3655f466e5edSAlexey Bayduraev free(dup_mask); 3656f466e5edSAlexey Bayduraev for (s = 0; s < nr_spec; s++) { 3657f466e5edSAlexey Bayduraev if (maps_spec) 3658f466e5edSAlexey Bayduraev free(maps_spec[s]); 3659f466e5edSAlexey Bayduraev if (affinity_spec) 3660f466e5edSAlexey Bayduraev free(affinity_spec[s]); 3661f466e5edSAlexey Bayduraev } 3662f466e5edSAlexey Bayduraev free(affinity_spec); 3663f466e5edSAlexey Bayduraev free(maps_spec); 3664f466e5edSAlexey Bayduraev 3665f466e5edSAlexey Bayduraev return ret; 3666f466e5edSAlexey Bayduraev } 3667f466e5edSAlexey Bayduraev 36687954f716SAlexey Bayduraev static int record__init_thread_default_masks(struct record *rec, struct perf_cpu_map *cpus) 36697954f716SAlexey Bayduraev { 36707954f716SAlexey Bayduraev int ret; 36717954f716SAlexey Bayduraev 36727954f716SAlexey Bayduraev ret = record__alloc_thread_masks(rec, 1, cpu__max_cpu().cpu); 36737954f716SAlexey Bayduraev if (ret) 36747954f716SAlexey Bayduraev return ret; 36757954f716SAlexey Bayduraev 36767954f716SAlexey Bayduraev record__mmap_cpu_mask_init(&rec->thread_masks->maps, cpus); 36777954f716SAlexey Bayduraev 36787954f716SAlexey Bayduraev rec->nr_threads = 1; 36797954f716SAlexey Bayduraev 36807954f716SAlexey Bayduraev return 0; 36817954f716SAlexey Bayduraev } 36827954f716SAlexey Bayduraev 36837954f716SAlexey Bayduraev static int record__init_thread_masks(struct record *rec) 36847954f716SAlexey Bayduraev { 3685f466e5edSAlexey Bayduraev int ret = 0; 36860df6ade7SIan Rogers struct perf_cpu_map *cpus = rec->evlist->core.user_requested_cpus; 36877954f716SAlexey Bayduraev 368806380a84SAlexey Bayduraev if (!record__threads_enabled(rec)) 36897954f716SAlexey Bayduraev return record__init_thread_default_masks(rec, cpus); 369006380a84SAlexey Bayduraev 369123380e4dSAlexey Bayduraev if (cpu_map__is_dummy(cpus)) { 369223380e4dSAlexey Bayduraev pr_err("--per-thread option is mutually exclusive to parallel streaming mode.\n"); 369323380e4dSAlexey Bayduraev return -EINVAL; 369423380e4dSAlexey Bayduraev } 369523380e4dSAlexey Bayduraev 3696f466e5edSAlexey Bayduraev switch (rec->opts.threads_spec) { 3697f466e5edSAlexey Bayduraev case THREAD_SPEC__CPU: 3698f466e5edSAlexey Bayduraev ret = record__init_thread_cpu_masks(rec, cpus); 3699f466e5edSAlexey Bayduraev break; 3700f466e5edSAlexey Bayduraev case THREAD_SPEC__CORE: 3701f466e5edSAlexey Bayduraev ret = record__init_thread_core_masks(rec, cpus); 3702f466e5edSAlexey Bayduraev break; 3703f466e5edSAlexey Bayduraev case THREAD_SPEC__PACKAGE: 3704f466e5edSAlexey Bayduraev ret = record__init_thread_package_masks(rec, cpus); 3705f466e5edSAlexey Bayduraev break; 3706f466e5edSAlexey Bayduraev case THREAD_SPEC__NUMA: 3707f466e5edSAlexey Bayduraev ret = record__init_thread_numa_masks(rec, cpus); 3708f466e5edSAlexey Bayduraev break; 3709f466e5edSAlexey Bayduraev case THREAD_SPEC__USER: 3710f466e5edSAlexey Bayduraev ret = record__init_thread_user_masks(rec, cpus); 3711f466e5edSAlexey Bayduraev break; 3712f466e5edSAlexey Bayduraev default: 3713f466e5edSAlexey Bayduraev break; 3714f466e5edSAlexey Bayduraev } 3715f466e5edSAlexey Bayduraev 3716f466e5edSAlexey Bayduraev return ret; 37177954f716SAlexey Bayduraev } 37187954f716SAlexey Bayduraev 3719b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 372086470930SIngo Molnar { 3721ef149c25SAdrian Hunter int err; 37228c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 372316ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 372486470930SIngo Molnar 372567230479SArnaldo Carvalho de Melo setlocale(LC_ALL, ""); 372667230479SArnaldo Carvalho de Melo 372748e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 372848e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 372948e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 373048e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 373148e1cab1SWang Nan # undef set_nobuild 373248e1cab1SWang Nan #endif 373348e1cab1SWang Nan 37347efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 37357efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 37367efe0e03SHe Kuang # define REASON "NO_DWARF=1" 37377efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 37387efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 37397efe0e03SHe Kuang # else 37407efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 37417efe0e03SHe Kuang # endif 37427efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 37437efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 37447efe0e03SHe Kuang # undef set_nobuild 37457efe0e03SHe Kuang # undef REASON 37467efe0e03SHe Kuang #endif 37477efe0e03SHe Kuang 37489d2ed645SAlexey Budankov rec->opts.affinity = PERF_AFFINITY_SYS; 37499d2ed645SAlexey Budankov 37500f98b11cSJiri Olsa rec->evlist = evlist__new(); 37513e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 3752361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 3753361c99a6SArnaldo Carvalho de Melo 3754ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 3755ecc4c561SArnaldo Carvalho de Melo if (err) 3756ecc4c561SArnaldo Carvalho de Melo return err; 3757eb853e80SJiri Olsa 3758bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 3759a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 376068ba3235SNamhyung Kim if (quiet) 376168ba3235SNamhyung Kim perf_quiet_option(); 3762483635a9SJiri Olsa 37637cc72553SJames Clark err = symbol__validate_sym_arguments(); 37647cc72553SJames Clark if (err) 37657cc72553SJames Clark return err; 37667cc72553SJames Clark 37679bce13eaSJiri Olsa perf_debuginfod_setup(&record.debuginfod); 37689bce13eaSJiri Olsa 3769483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 3770602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 3771483635a9SJiri Olsa rec->opts.target.system_wide = true; 377286470930SIngo Molnar 3773bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 3774c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 3775c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 3776c7118369SNamhyung Kim 3777023695d9SStephane Eranian } 3778504c1ad1SAlexey Budankov 3779e29386c8SJiri Olsa if (rec->buildid_mmap) { 3780e29386c8SJiri Olsa if (!perf_can_record_build_id()) { 3781e29386c8SJiri Olsa pr_err("Failed: no support to record build id in mmap events, update your kernel.\n"); 3782e29386c8SJiri Olsa err = -EINVAL; 3783e29386c8SJiri Olsa goto out_opts; 3784e29386c8SJiri Olsa } 3785e29386c8SJiri Olsa pr_debug("Enabling build id in mmap2 events.\n"); 3786e29386c8SJiri Olsa /* Enable mmap build id synthesizing. */ 3787e29386c8SJiri Olsa symbol_conf.buildid_mmap2 = true; 3788e29386c8SJiri Olsa /* Enable perf_event_attr::build_id bit. */ 3789e29386c8SJiri Olsa rec->opts.build_id = true; 3790e29386c8SJiri Olsa /* Disable build id cache. */ 3791e29386c8SJiri Olsa rec->no_buildid = true; 3792e29386c8SJiri Olsa } 3793e29386c8SJiri Olsa 37944f2abe91SNamhyung Kim if (rec->opts.record_cgroup && !perf_can_record_cgroup()) { 37954f2abe91SNamhyung Kim pr_err("Kernel has no cgroup sampling support.\n"); 37964f2abe91SNamhyung Kim err = -EINVAL; 37974f2abe91SNamhyung Kim goto out_opts; 37984f2abe91SNamhyung Kim } 37994f2abe91SNamhyung Kim 380056f735ffSAlexey Bayduraev if (rec->opts.kcore || record__threads_enabled(rec)) 3801eeb399b5SAdrian Hunter rec->data.is_dir = true; 3802eeb399b5SAdrian Hunter 3803b5f2511dSAlexey Bayduraev if (record__threads_enabled(rec)) { 3804b5f2511dSAlexey Bayduraev if (rec->opts.affinity != PERF_AFFINITY_SYS) { 3805b5f2511dSAlexey Bayduraev pr_err("--affinity option is mutually exclusive to parallel streaming mode.\n"); 3806b5f2511dSAlexey Bayduraev goto out_opts; 3807b5f2511dSAlexey Bayduraev } 3808b5f2511dSAlexey Bayduraev if (record__aio_enabled(rec)) { 3809b5f2511dSAlexey Bayduraev pr_err("Asynchronous streaming mode (--aio) is mutually exclusive to parallel streaming mode.\n"); 3810b5f2511dSAlexey Bayduraev goto out_opts; 3811b5f2511dSAlexey Bayduraev } 3812b5f2511dSAlexey Bayduraev } 3813b5f2511dSAlexey Bayduraev 3814504c1ad1SAlexey Budankov if (rec->opts.comp_level != 0) { 3815504c1ad1SAlexey Budankov pr_debug("Compression enabled, disabling build id collection at the end of the session.\n"); 3816504c1ad1SAlexey Budankov rec->no_buildid = true; 3817504c1ad1SAlexey Budankov } 3818504c1ad1SAlexey Budankov 3819b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 3820b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 3821c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 3822c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 3823a8fcbd26SAdrian Hunter err = -EINVAL; 3824a8fcbd26SAdrian Hunter goto out_opts; 3825b757bb09SAdrian Hunter } 3826023695d9SStephane Eranian 3827cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 3828cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 3829a8fcbd26SAdrian Hunter err = -EINVAL; 3830a8fcbd26SAdrian Hunter goto out_opts; 3831cb4e1ebbSJiri Olsa } 3832cb4e1ebbSJiri Olsa 3833bfacbe3bSJiri Olsa if (rec->switch_output.time) { 3834bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 3835bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 3836bfacbe3bSJiri Olsa } 3837bfacbe3bSJiri Olsa 383803724b2eSAndi Kleen if (rec->switch_output.num_files) { 383903724b2eSAndi Kleen rec->switch_output.filenames = calloc(sizeof(char *), 384003724b2eSAndi Kleen rec->switch_output.num_files); 3841a8fcbd26SAdrian Hunter if (!rec->switch_output.filenames) { 3842a8fcbd26SAdrian Hunter err = -EINVAL; 3843a8fcbd26SAdrian Hunter goto out_opts; 3844a8fcbd26SAdrian Hunter } 384503724b2eSAndi Kleen } 384603724b2eSAndi Kleen 3847b5f2511dSAlexey Bayduraev if (rec->timestamp_filename && record__threads_enabled(rec)) { 3848b5f2511dSAlexey Bayduraev rec->timestamp_filename = false; 3849b5f2511dSAlexey Bayduraev pr_warning("WARNING: --timestamp-filename option is not available in parallel streaming mode.\n"); 3850b5f2511dSAlexey Bayduraev } 3851b5f2511dSAlexey Bayduraev 38521b36c03eSAdrian Hunter /* 38531b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 38541b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 38551b36c03eSAdrian Hunter */ 38561b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 38571b36c03eSAdrian Hunter 38581b36c03eSAdrian Hunter symbol__init(NULL); 38591b36c03eSAdrian Hunter 38604b5ea3bdSAdrian Hunter err = record__auxtrace_init(rec); 38611b36c03eSAdrian Hunter if (err) 38621b36c03eSAdrian Hunter goto out; 38631b36c03eSAdrian Hunter 38640aab2136SWang Nan if (dry_run) 38655c01ad60SAdrian Hunter goto out; 38660aab2136SWang Nan 3867d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 3868d7888573SWang Nan if (err) { 3869d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 3870d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 3871d7888573SWang Nan errbuf); 38725c01ad60SAdrian Hunter goto out; 3873d7888573SWang Nan } 3874d7888573SWang Nan 3875ef149c25SAdrian Hunter err = -ENOMEM; 3876ef149c25SAdrian Hunter 38770c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 3878a1ac1d3cSStephane Eranian disable_buildid_cache(); 3879dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 38800c1d46a8SWang Nan /* 38810c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 38820c1d46a8SWang Nan * generation by default to reduce data file switching 38830c1d46a8SWang Nan * overhead. Still generate buildid if they are required 38840c1d46a8SWang Nan * explicitly using 38850c1d46a8SWang Nan * 388660437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 38870c1d46a8SWang Nan * --no-no-buildid-cache 38880c1d46a8SWang Nan * 38890c1d46a8SWang Nan * Following code equals to: 38900c1d46a8SWang Nan * 38910c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 38920c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 38930c1d46a8SWang Nan * disable_buildid_cache(); 38940c1d46a8SWang Nan */ 38950c1d46a8SWang Nan bool disable = true; 38960c1d46a8SWang Nan 38970c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 38980c1d46a8SWang Nan disable = false; 38990c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 39000c1d46a8SWang Nan disable = false; 39010c1d46a8SWang Nan if (disable) { 39020c1d46a8SWang Nan rec->no_buildid = true; 39030c1d46a8SWang Nan rec->no_buildid_cache = true; 39040c1d46a8SWang Nan disable_buildid_cache(); 39050c1d46a8SWang Nan } 39060c1d46a8SWang Nan } 3907655000e7SArnaldo Carvalho de Melo 39084ea648aeSWang Nan if (record.opts.overwrite) 39094ea648aeSWang Nan record.opts.tail_synthesize = true; 39104ea648aeSWang Nan 3911b53a0755SJin Yao if (rec->evlist->core.nr_entries == 0) { 3912b53a0755SJin Yao if (perf_pmu__has_hybrid()) { 3913b53a0755SJin Yao err = evlist__add_default_hybrid(rec->evlist, 3914b53a0755SJin Yao !record.opts.no_samples); 3915b53a0755SJin Yao } else { 3916b53a0755SJin Yao err = __evlist__add_default(rec->evlist, 3917b53a0755SJin Yao !record.opts.no_samples); 3918b53a0755SJin Yao } 3919b53a0755SJin Yao 3920b53a0755SJin Yao if (err < 0) { 392169aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 3922394c01edSAdrian Hunter goto out; 3923bbd36e5eSPeter Zijlstra } 3924b53a0755SJin Yao } 392586470930SIngo Molnar 392669e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 392769e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 392869e7e5b0SAdrian Hunter 3929602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 393016ad2ffbSNamhyung Kim if (err) { 3931602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 3932c3dec27bSJiri Olsa ui__warning("%s\n", errbuf); 393316ad2ffbSNamhyung Kim } 39344bd0f2d2SNamhyung Kim 3935602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 393616ad2ffbSNamhyung Kim if (err) { 393716ad2ffbSNamhyung Kim int saved_errno = errno; 393816ad2ffbSNamhyung Kim 3939602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 39403780f488SNamhyung Kim ui__error("%s", errbuf); 394116ad2ffbSNamhyung Kim 394216ad2ffbSNamhyung Kim err = -saved_errno; 3943394c01edSAdrian Hunter goto out; 394416ad2ffbSNamhyung Kim } 39450d37aa34SArnaldo Carvalho de Melo 3946ca800068SMengting Zhang /* Enable ignoring missing threads when -u/-p option is defined. */ 3947ca800068SMengting Zhang rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; 394823dc4f15SJiri Olsa 39491d3351e6SJin Yao if (evlist__fix_hybrid_cpus(rec->evlist, rec->opts.target.cpu_list)) { 39501d3351e6SJin Yao pr_err("failed to use cpu list %s\n", 39511d3351e6SJin Yao rec->opts.target.cpu_list); 39521d3351e6SJin Yao goto out; 39531d3351e6SJin Yao } 39541d3351e6SJin Yao 39551d3351e6SJin Yao rec->opts.target.hybrid = perf_pmu__has_hybrid(); 39567248e308SAlexandre Truong 39577248e308SAlexandre Truong if (callchain_param.enabled && callchain_param.record_mode == CALLCHAIN_FP) 39587248e308SAlexandre Truong arch__add_leaf_frame_record_opts(&rec->opts); 39597248e308SAlexandre Truong 396016ad2ffbSNamhyung Kim err = -ENOMEM; 39617748bb71SArnaldo Carvalho de Melo if (evlist__create_maps(rec->evlist, &rec->opts.target) < 0) 3962dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 396369aad6f1SArnaldo Carvalho de Melo 3964ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 3965ef149c25SAdrian Hunter if (err) 3966394c01edSAdrian Hunter goto out; 3967ef149c25SAdrian Hunter 39686156681bSNamhyung Kim /* 39696156681bSNamhyung Kim * We take all buildids when the file contains 39706156681bSNamhyung Kim * AUX area tracing data because we do not decode the 39716156681bSNamhyung Kim * trace because it would take too long. 39726156681bSNamhyung Kim */ 39736156681bSNamhyung Kim if (rec->opts.full_auxtrace) 39746156681bSNamhyung Kim rec->buildid_all = true; 39756156681bSNamhyung Kim 3976246eba8eSAdrian Hunter if (rec->opts.text_poke) { 3977246eba8eSAdrian Hunter err = record__config_text_poke(rec->evlist); 3978246eba8eSAdrian Hunter if (err) { 3979246eba8eSAdrian Hunter pr_err("record__config_text_poke failed, error %d\n", err); 3980246eba8eSAdrian Hunter goto out; 3981246eba8eSAdrian Hunter } 3982246eba8eSAdrian Hunter } 3983246eba8eSAdrian Hunter 3984b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 398539d17dacSArnaldo Carvalho de Melo err = -EINVAL; 3986394c01edSAdrian Hunter goto out; 39877e4ff9e3SMike Galbraith } 39887e4ff9e3SMike Galbraith 39897954f716SAlexey Bayduraev err = record__init_thread_masks(rec); 39907954f716SAlexey Bayduraev if (err) { 39917954f716SAlexey Bayduraev pr_err("Failed to initialize parallel data streaming masks\n"); 39927954f716SAlexey Bayduraev goto out; 39937954f716SAlexey Bayduraev } 39947954f716SAlexey Bayduraev 399593f20c0fSAlexey Budankov if (rec->opts.nr_cblocks > nr_cblocks_max) 399693f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_max; 39975d7f4116SAlexey Budankov pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks); 3998d3d1af6fSAlexey Budankov 39999d2ed645SAlexey Budankov pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]); 4000470530bbSAlexey Budankov pr_debug("mmap flush: %d\n", rec->opts.mmap_flush); 40019d2ed645SAlexey Budankov 400251255a8aSAlexey Budankov if (rec->opts.comp_level > comp_level_max) 400351255a8aSAlexey Budankov rec->opts.comp_level = comp_level_max; 400451255a8aSAlexey Budankov pr_debug("comp level: %d\n", rec->opts.comp_level); 400551255a8aSAlexey Budankov 4006d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 4007394c01edSAdrian Hunter out: 4008c12995a5SJiri Olsa evlist__delete(rec->evlist); 4009d65a458bSArnaldo Carvalho de Melo symbol__exit(); 4010ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 4011a8fcbd26SAdrian Hunter out_opts: 40127954f716SAlexey Bayduraev record__free_thread_masks(rec, rec->nr_threads); 40137954f716SAlexey Bayduraev rec->nr_threads = 0; 4014ee7fe31eSAdrian Hunter evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close); 401539d17dacSArnaldo Carvalho de Melo return err; 401686470930SIngo Molnar } 40172dd6d8a1SAdrian Hunter 40182dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 40192dd6d8a1SAdrian Hunter { 4020dc0c6127SJiri Olsa struct record *rec = &record; 4021dc0c6127SJiri Olsa 4022d20aff15SAdrian Hunter hit_auxtrace_snapshot_trigger(rec); 40233c1cb7e3SWang Nan 4024dc0c6127SJiri Olsa if (switch_output_signal(rec)) 40253c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 40262dd6d8a1SAdrian Hunter } 4027bfacbe3bSJiri Olsa 4028bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 4029bfacbe3bSJiri Olsa { 4030bfacbe3bSJiri Olsa struct record *rec = &record; 4031bfacbe3bSJiri Olsa 4032bfacbe3bSJiri Olsa if (switch_output_time(rec)) 4033bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 4034bfacbe3bSJiri Olsa } 4035