1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 286470930SIngo Molnar /* 386470930SIngo Molnar * builtin-record.c 486470930SIngo Molnar * 586470930SIngo Molnar * Builtin record command: Record the profile of a workload 686470930SIngo Molnar * (or a CPU, or a PID) into the perf.data output file - for 786470930SIngo Molnar * later analysis via perf report. 886470930SIngo Molnar */ 986470930SIngo Molnar #include "builtin.h" 1086470930SIngo Molnar 116122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h" 124b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1386470930SIngo Molnar #include "util/parse-events.h" 1441840d21STaeung Song #include "util/config.h" 1586470930SIngo Molnar 168f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h" 17f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h" 187c6a1c65SPeter Zijlstra #include "util/header.h" 1966e274f3SFrederic Weisbecker #include "util/event.h" 20361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h" 2169aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 228f28827aSFrederic Weisbecker #include "util/debug.h" 23e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 24aeb00b1aSArnaldo Carvalho de Melo #include "util/target.h" 2594c744b6SArnaldo Carvalho de Melo #include "util/session.h" 2645694aa7SArnaldo Carvalho de Melo #include "util/tool.h" 278d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 28aeb00b1aSArnaldo Carvalho de Melo #include "util/record.h" 29a12b51c4SPaul Mackerras #include "util/cpumap.h" 30fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h" 31f5fc1412SJiri Olsa #include "util/data.h" 32bcc84ec6SStephane Eranian #include "util/perf_regs.h" 33ef149c25SAdrian Hunter #include "util/auxtrace.h" 3446bc29b9SAdrian Hunter #include "util/tsc.h" 35f00898f4SAndi Kleen #include "util/parse-branch-options.h" 36bcc84ec6SStephane Eranian #include "util/parse-regs-options.h" 3740c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h" 3871dc2326SWang Nan #include "util/llvm-utils.h" 398690a2a7SWang Nan #include "util/bpf-loader.h" 405f9cf599SWang Nan #include "util/trigger.h" 41a074865eSWang Nan #include "util/perf-hooks.h" 42f13de660SAlexey Budankov #include "util/cpu-set-sched.h" 43ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h" 44c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h" 4558db1d6eSArnaldo Carvalho de Melo #include "util/units.h" 467b612e29SSong Liu #include "util/bpf-event.h" 47d99c22eaSStephane Eranian #include "util/util.h" 4870943490SStephane Eranian #include "util/pfm.h" 496953beb4SJiri Olsa #include "util/clockid.h" 50b53a0755SJin Yao #include "util/pmu-hybrid.h" 51b53a0755SJin Yao #include "util/evlist-hybrid.h" 52d8871ea7SWang Nan #include "asm/bug.h" 53c1a604dfSArnaldo Carvalho de Melo #include "perf.h" 547c6a1c65SPeter Zijlstra 55a43783aeSArnaldo Carvalho de Melo #include <errno.h> 56fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 5767230479SArnaldo Carvalho de Melo #include <locale.h> 584208735dSArnaldo Carvalho de Melo #include <poll.h> 59d99c22eaSStephane Eranian #include <pthread.h> 6086470930SIngo Molnar #include <unistd.h> 6186470930SIngo Molnar #include <sched.h> 629607ad3aSArnaldo Carvalho de Melo #include <signal.h> 63da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 64da231338SAnand K Mistry #include <sys/eventfd.h> 65da231338SAnand K Mistry #endif 66a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 674208735dSArnaldo Carvalho de Melo #include <sys/wait.h> 68eeb399b5SAdrian Hunter #include <sys/types.h> 69eeb399b5SAdrian Hunter #include <sys/stat.h> 70eeb399b5SAdrian Hunter #include <fcntl.h> 716ef81c55SMamatha Inamdar #include <linux/err.h> 728520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 730693e680SArnaldo Carvalho de Melo #include <linux/time64.h> 74d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h> 758384a260SAlexey Budankov #include <linux/bitmap.h> 76d1e325cfSJiri Olsa #include <sys/time.h> 7778da39faSBernhard Rosenkraenzer 781b43b704SJiri Olsa struct switch_output { 79dc0c6127SJiri Olsa bool enabled; 801b43b704SJiri Olsa bool signal; 81dc0c6127SJiri Olsa unsigned long size; 82bfacbe3bSJiri Olsa unsigned long time; 83cb4e1ebbSJiri Olsa const char *str; 84cb4e1ebbSJiri Olsa bool set; 8503724b2eSAndi Kleen char **filenames; 8603724b2eSAndi Kleen int num_files; 8703724b2eSAndi Kleen int cur_file; 881b43b704SJiri Olsa }; 891b43b704SJiri Olsa 908c6f45a7SArnaldo Carvalho de Melo struct record { 9145694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 92b4006796SArnaldo Carvalho de Melo struct record_opts opts; 93d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 948ceb41d7SJiri Olsa struct perf_data data; 95ef149c25SAdrian Hunter struct auxtrace_record *itr; 9663503dbaSJiri Olsa struct evlist *evlist; 97d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 98bc477d79SArnaldo Carvalho de Melo struct evlist *sb_evlist; 99899e5ffbSArnaldo Carvalho de Melo pthread_t thread_id; 100d20deb64SArnaldo Carvalho de Melo int realtime_prio; 101899e5ffbSArnaldo Carvalho de Melo bool switch_output_event_set; 102d20deb64SArnaldo Carvalho de Melo bool no_buildid; 103d2db9a98SWang Nan bool no_buildid_set; 104d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 105d2db9a98SWang Nan bool no_buildid_cache_set; 1066156681bSNamhyung Kim bool buildid_all; 107e29386c8SJiri Olsa bool buildid_mmap; 108ecfd7a9cSWang Nan bool timestamp_filename; 10968588bafSJin Yao bool timestamp_boundary; 1101b43b704SJiri Olsa struct switch_output switch_output; 1119f065194SYang Shi unsigned long long samples; 1128384a260SAlexey Budankov struct mmap_cpu_mask affinity_mask; 1136d575816SJiwei Sun unsigned long output_max_size; /* = 0: unlimited */ 1140f82ebc4SArnaldo Carvalho de Melo }; 11586470930SIngo Molnar 1166d575816SJiwei Sun static volatile int done; 1176d575816SJiwei Sun 118dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 119dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 120dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 121dc0c6127SJiri Olsa 1229d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = { 1239d2ed645SAlexey Budankov "SYS", "NODE", "CPU" 1249d2ed645SAlexey Budankov }; 1259d2ed645SAlexey Budankov 126dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 127dc0c6127SJiri Olsa { 128dc0c6127SJiri Olsa return rec->switch_output.signal && 129dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 130dc0c6127SJiri Olsa } 131dc0c6127SJiri Olsa 132dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 133dc0c6127SJiri Olsa { 134dc0c6127SJiri Olsa return rec->switch_output.size && 135dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 136dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 137dc0c6127SJiri Olsa } 138dc0c6127SJiri Olsa 139bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 140bfacbe3bSJiri Olsa { 141bfacbe3bSJiri Olsa return rec->switch_output.time && 142bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 143bfacbe3bSJiri Olsa } 144bfacbe3bSJiri Olsa 1456d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec) 1466d575816SJiwei Sun { 1476d575816SJiwei Sun return rec->output_max_size && 1486d575816SJiwei Sun (rec->bytes_written >= rec->output_max_size); 1496d575816SJiwei Sun } 1506d575816SJiwei Sun 151a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused, 152ded2b8feSJiri Olsa void *bf, size_t size) 153f5970550SPeter Zijlstra { 154ded2b8feSJiri Olsa struct perf_data_file *file = &rec->session->data->file; 155ded2b8feSJiri Olsa 156ded2b8feSJiri Olsa if (perf_data_file__write(file, bf, size) < 0) { 1574f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 1588d3eca20SDavid Ahern return -1; 1598d3eca20SDavid Ahern } 160f5970550SPeter Zijlstra 161cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 162dc0c6127SJiri Olsa 1636d575816SJiwei Sun if (record__output_max_size_exceeded(rec) && !done) { 1646d575816SJiwei Sun fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB)," 1656d575816SJiwei Sun " stopping session ]\n", 1666d575816SJiwei Sun rec->bytes_written >> 10); 1676d575816SJiwei Sun done = 1; 1686d575816SJiwei Sun } 1696d575816SJiwei Sun 170dc0c6127SJiri Olsa if (switch_output_size(rec)) 171dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 172dc0c6127SJiri Olsa 1738d3eca20SDavid Ahern return 0; 174f5970550SPeter Zijlstra } 175f5970550SPeter Zijlstra 176ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec); 177ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec); 1785d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size, 1795d7f4116SAlexey Budankov void *src, size_t src_size); 1805d7f4116SAlexey Budankov 181d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 182d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd, 183d3d1af6fSAlexey Budankov void *buf, size_t size, off_t off) 184d3d1af6fSAlexey Budankov { 185d3d1af6fSAlexey Budankov int rc; 186d3d1af6fSAlexey Budankov 187d3d1af6fSAlexey Budankov cblock->aio_fildes = trace_fd; 188d3d1af6fSAlexey Budankov cblock->aio_buf = buf; 189d3d1af6fSAlexey Budankov cblock->aio_nbytes = size; 190d3d1af6fSAlexey Budankov cblock->aio_offset = off; 191d3d1af6fSAlexey Budankov cblock->aio_sigevent.sigev_notify = SIGEV_NONE; 192d3d1af6fSAlexey Budankov 193d3d1af6fSAlexey Budankov do { 194d3d1af6fSAlexey Budankov rc = aio_write(cblock); 195d3d1af6fSAlexey Budankov if (rc == 0) { 196d3d1af6fSAlexey Budankov break; 197d3d1af6fSAlexey Budankov } else if (errno != EAGAIN) { 198d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 199d3d1af6fSAlexey Budankov pr_err("failed to queue perf data, error: %m\n"); 200d3d1af6fSAlexey Budankov break; 201d3d1af6fSAlexey Budankov } 202d3d1af6fSAlexey Budankov } while (1); 203d3d1af6fSAlexey Budankov 204d3d1af6fSAlexey Budankov return rc; 205d3d1af6fSAlexey Budankov } 206d3d1af6fSAlexey Budankov 207a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock) 208d3d1af6fSAlexey Budankov { 209d3d1af6fSAlexey Budankov void *rem_buf; 210d3d1af6fSAlexey Budankov off_t rem_off; 211d3d1af6fSAlexey Budankov size_t rem_size; 212d3d1af6fSAlexey Budankov int rc, aio_errno; 213d3d1af6fSAlexey Budankov ssize_t aio_ret, written; 214d3d1af6fSAlexey Budankov 215d3d1af6fSAlexey Budankov aio_errno = aio_error(cblock); 216d3d1af6fSAlexey Budankov if (aio_errno == EINPROGRESS) 217d3d1af6fSAlexey Budankov return 0; 218d3d1af6fSAlexey Budankov 219d3d1af6fSAlexey Budankov written = aio_ret = aio_return(cblock); 220d3d1af6fSAlexey Budankov if (aio_ret < 0) { 221d3d1af6fSAlexey Budankov if (aio_errno != EINTR) 222d3d1af6fSAlexey Budankov pr_err("failed to write perf data, error: %m\n"); 223d3d1af6fSAlexey Budankov written = 0; 224d3d1af6fSAlexey Budankov } 225d3d1af6fSAlexey Budankov 226d3d1af6fSAlexey Budankov rem_size = cblock->aio_nbytes - written; 227d3d1af6fSAlexey Budankov 228d3d1af6fSAlexey Budankov if (rem_size == 0) { 229d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 230d3d1af6fSAlexey Budankov /* 231ef781128SAlexey Budankov * md->refcount is incremented in record__aio_pushfn() for 232ef781128SAlexey Budankov * every aio write request started in record__aio_push() so 233ef781128SAlexey Budankov * decrement it because the request is now complete. 234d3d1af6fSAlexey Budankov */ 23580e53d11SJiri Olsa perf_mmap__put(&md->core); 236d3d1af6fSAlexey Budankov rc = 1; 237d3d1af6fSAlexey Budankov } else { 238d3d1af6fSAlexey Budankov /* 239d3d1af6fSAlexey Budankov * aio write request may require restart with the 240d3d1af6fSAlexey Budankov * reminder if the kernel didn't write whole 241d3d1af6fSAlexey Budankov * chunk at once. 242d3d1af6fSAlexey Budankov */ 243d3d1af6fSAlexey Budankov rem_off = cblock->aio_offset + written; 244d3d1af6fSAlexey Budankov rem_buf = (void *)(cblock->aio_buf + written); 245d3d1af6fSAlexey Budankov record__aio_write(cblock, cblock->aio_fildes, 246d3d1af6fSAlexey Budankov rem_buf, rem_size, rem_off); 247d3d1af6fSAlexey Budankov rc = 0; 248d3d1af6fSAlexey Budankov } 249d3d1af6fSAlexey Budankov 250d3d1af6fSAlexey Budankov return rc; 251d3d1af6fSAlexey Budankov } 252d3d1af6fSAlexey Budankov 253a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all) 254d3d1af6fSAlexey Budankov { 25593f20c0fSAlexey Budankov struct aiocb **aiocb = md->aio.aiocb; 25693f20c0fSAlexey Budankov struct aiocb *cblocks = md->aio.cblocks; 257d3d1af6fSAlexey Budankov struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */ 25893f20c0fSAlexey Budankov int i, do_suspend; 259d3d1af6fSAlexey Budankov 260d3d1af6fSAlexey Budankov do { 26193f20c0fSAlexey Budankov do_suspend = 0; 26293f20c0fSAlexey Budankov for (i = 0; i < md->aio.nr_cblocks; ++i) { 26393f20c0fSAlexey Budankov if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) { 26493f20c0fSAlexey Budankov if (sync_all) 26593f20c0fSAlexey Budankov aiocb[i] = NULL; 26693f20c0fSAlexey Budankov else 26793f20c0fSAlexey Budankov return i; 26893f20c0fSAlexey Budankov } else { 26993f20c0fSAlexey Budankov /* 27093f20c0fSAlexey Budankov * Started aio write is not complete yet 27193f20c0fSAlexey Budankov * so it has to be waited before the 27293f20c0fSAlexey Budankov * next allocation. 27393f20c0fSAlexey Budankov */ 27493f20c0fSAlexey Budankov aiocb[i] = &cblocks[i]; 27593f20c0fSAlexey Budankov do_suspend = 1; 27693f20c0fSAlexey Budankov } 27793f20c0fSAlexey Budankov } 27893f20c0fSAlexey Budankov if (!do_suspend) 27993f20c0fSAlexey Budankov return -1; 280d3d1af6fSAlexey Budankov 28193f20c0fSAlexey Budankov while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) { 282d3d1af6fSAlexey Budankov if (!(errno == EAGAIN || errno == EINTR)) 283d3d1af6fSAlexey Budankov pr_err("failed to sync perf data, error: %m\n"); 284d3d1af6fSAlexey Budankov } 285d3d1af6fSAlexey Budankov } while (1); 286d3d1af6fSAlexey Budankov } 287d3d1af6fSAlexey Budankov 288ef781128SAlexey Budankov struct record_aio { 289ef781128SAlexey Budankov struct record *rec; 290ef781128SAlexey Budankov void *data; 291ef781128SAlexey Budankov size_t size; 292ef781128SAlexey Budankov }; 293ef781128SAlexey Budankov 294a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size) 295d3d1af6fSAlexey Budankov { 296ef781128SAlexey Budankov struct record_aio *aio = to; 297ef781128SAlexey Budankov 298ef781128SAlexey Budankov /* 299547740f7SJiri Olsa * map->core.base data pointed by buf is copied into free map->aio.data[] buffer 300ef781128SAlexey Budankov * to release space in the kernel buffer as fast as possible, calling 301ef781128SAlexey Budankov * perf_mmap__consume() from perf_mmap__push() function. 302ef781128SAlexey Budankov * 303ef781128SAlexey Budankov * That lets the kernel to proceed with storing more profiling data into 304ef781128SAlexey Budankov * the kernel buffer earlier than other per-cpu kernel buffers are handled. 305ef781128SAlexey Budankov * 306ef781128SAlexey Budankov * Coping can be done in two steps in case the chunk of profiling data 307ef781128SAlexey Budankov * crosses the upper bound of the kernel buffer. In this case we first move 308ef781128SAlexey Budankov * part of data from map->start till the upper bound and then the reminder 309ef781128SAlexey Budankov * from the beginning of the kernel buffer till the end of the data chunk. 310ef781128SAlexey Budankov */ 311ef781128SAlexey Budankov 312ef781128SAlexey Budankov if (record__comp_enabled(aio->rec)) { 313ef781128SAlexey Budankov size = zstd_compress(aio->rec->session, aio->data + aio->size, 314bf59b305SJiri Olsa mmap__mmap_len(map) - aio->size, 315ef781128SAlexey Budankov buf, size); 316ef781128SAlexey Budankov } else { 317ef781128SAlexey Budankov memcpy(aio->data + aio->size, buf, size); 318ef781128SAlexey Budankov } 319ef781128SAlexey Budankov 320ef781128SAlexey Budankov if (!aio->size) { 321ef781128SAlexey Budankov /* 322ef781128SAlexey Budankov * Increment map->refcount to guard map->aio.data[] buffer 323ef781128SAlexey Budankov * from premature deallocation because map object can be 324ef781128SAlexey Budankov * released earlier than aio write request started on 325ef781128SAlexey Budankov * map->aio.data[] buffer is complete. 326ef781128SAlexey Budankov * 327ef781128SAlexey Budankov * perf_mmap__put() is done at record__aio_complete() 328ef781128SAlexey Budankov * after started aio request completion or at record__aio_push() 329ef781128SAlexey Budankov * if the request failed to start. 330ef781128SAlexey Budankov */ 331e75710f0SJiri Olsa perf_mmap__get(&map->core); 332ef781128SAlexey Budankov } 333ef781128SAlexey Budankov 334ef781128SAlexey Budankov aio->size += size; 335ef781128SAlexey Budankov 336ef781128SAlexey Budankov return size; 337ef781128SAlexey Budankov } 338ef781128SAlexey Budankov 339a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off) 340ef781128SAlexey Budankov { 341ef781128SAlexey Budankov int ret, idx; 342ef781128SAlexey Budankov int trace_fd = rec->session->data->file.fd; 343ef781128SAlexey Budankov struct record_aio aio = { .rec = rec, .size = 0 }; 344ef781128SAlexey Budankov 345ef781128SAlexey Budankov /* 346ef781128SAlexey Budankov * Call record__aio_sync() to wait till map->aio.data[] buffer 347ef781128SAlexey Budankov * becomes available after previous aio write operation. 348ef781128SAlexey Budankov */ 349ef781128SAlexey Budankov 350ef781128SAlexey Budankov idx = record__aio_sync(map, false); 351ef781128SAlexey Budankov aio.data = map->aio.data[idx]; 352ef781128SAlexey Budankov ret = perf_mmap__push(map, &aio, record__aio_pushfn); 353ef781128SAlexey Budankov if (ret != 0) /* ret > 0 - no data, ret < 0 - error */ 354ef781128SAlexey Budankov return ret; 355d3d1af6fSAlexey Budankov 356d3d1af6fSAlexey Budankov rec->samples++; 357ef781128SAlexey Budankov ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off); 358d3d1af6fSAlexey Budankov if (!ret) { 359ef781128SAlexey Budankov *off += aio.size; 360ef781128SAlexey Budankov rec->bytes_written += aio.size; 361d3d1af6fSAlexey Budankov if (switch_output_size(rec)) 362d3d1af6fSAlexey Budankov trigger_hit(&switch_output_trigger); 363ef781128SAlexey Budankov } else { 364ef781128SAlexey Budankov /* 365ef781128SAlexey Budankov * Decrement map->refcount incremented in record__aio_pushfn() 366ef781128SAlexey Budankov * back if record__aio_write() operation failed to start, otherwise 367ef781128SAlexey Budankov * map->refcount is decremented in record__aio_complete() after 368ef781128SAlexey Budankov * aio write operation finishes successfully. 369ef781128SAlexey Budankov */ 37080e53d11SJiri Olsa perf_mmap__put(&map->core); 371d3d1af6fSAlexey Budankov } 372d3d1af6fSAlexey Budankov 373d3d1af6fSAlexey Budankov return ret; 374d3d1af6fSAlexey Budankov } 375d3d1af6fSAlexey Budankov 376d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd) 377d3d1af6fSAlexey Budankov { 378d3d1af6fSAlexey Budankov return lseek(trace_fd, 0, SEEK_CUR); 379d3d1af6fSAlexey Budankov } 380d3d1af6fSAlexey Budankov 381d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos) 382d3d1af6fSAlexey Budankov { 383d3d1af6fSAlexey Budankov lseek(trace_fd, pos, SEEK_SET); 384d3d1af6fSAlexey Budankov } 385d3d1af6fSAlexey Budankov 386d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec) 387d3d1af6fSAlexey Budankov { 388d3d1af6fSAlexey Budankov int i; 38963503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 390a5830532SJiri Olsa struct mmap *maps = evlist->mmap; 391d3d1af6fSAlexey Budankov 392ef781128SAlexey Budankov if (!record__aio_enabled(rec)) 393d3d1af6fSAlexey Budankov return; 394d3d1af6fSAlexey Budankov 395c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 396a5830532SJiri Olsa struct mmap *map = &maps[i]; 397d3d1af6fSAlexey Budankov 398547740f7SJiri Olsa if (map->core.base) 39993f20c0fSAlexey Budankov record__aio_sync(map, true); 400d3d1af6fSAlexey Budankov } 401d3d1af6fSAlexey Budankov } 402d3d1af6fSAlexey Budankov 403d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1; 40493f20c0fSAlexey Budankov static int nr_cblocks_max = 4; 405d3d1af6fSAlexey Budankov 406d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt, 40793f20c0fSAlexey Budankov const char *str, 408d3d1af6fSAlexey Budankov int unset) 409d3d1af6fSAlexey Budankov { 410d3d1af6fSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 411d3d1af6fSAlexey Budankov 41293f20c0fSAlexey Budankov if (unset) { 413d3d1af6fSAlexey Budankov opts->nr_cblocks = 0; 41493f20c0fSAlexey Budankov } else { 41593f20c0fSAlexey Budankov if (str) 41693f20c0fSAlexey Budankov opts->nr_cblocks = strtol(str, NULL, 0); 41793f20c0fSAlexey Budankov if (!opts->nr_cblocks) 418d3d1af6fSAlexey Budankov opts->nr_cblocks = nr_cblocks_default; 41993f20c0fSAlexey Budankov } 420d3d1af6fSAlexey Budankov 421d3d1af6fSAlexey Budankov return 0; 422d3d1af6fSAlexey Budankov } 423d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */ 42493f20c0fSAlexey Budankov static int nr_cblocks_max = 0; 42593f20c0fSAlexey Budankov 426a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused, 427ef781128SAlexey Budankov off_t *off __maybe_unused) 428d3d1af6fSAlexey Budankov { 429d3d1af6fSAlexey Budankov return -1; 430d3d1af6fSAlexey Budankov } 431d3d1af6fSAlexey Budankov 432d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused) 433d3d1af6fSAlexey Budankov { 434d3d1af6fSAlexey Budankov return -1; 435d3d1af6fSAlexey Budankov } 436d3d1af6fSAlexey Budankov 437d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused) 438d3d1af6fSAlexey Budankov { 439d3d1af6fSAlexey Budankov } 440d3d1af6fSAlexey Budankov 441d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused) 442d3d1af6fSAlexey Budankov { 443d3d1af6fSAlexey Budankov } 444d3d1af6fSAlexey Budankov #endif 445d3d1af6fSAlexey Budankov 446d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec) 447d3d1af6fSAlexey Budankov { 448d3d1af6fSAlexey Budankov return rec->opts.nr_cblocks > 0; 449d3d1af6fSAlexey Budankov } 450d3d1af6fSAlexey Budankov 451470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1 452470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt, 453470530bbSAlexey Budankov const char *str, 454470530bbSAlexey Budankov int unset) 455470530bbSAlexey Budankov { 456470530bbSAlexey Budankov int flush_max; 457470530bbSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 458470530bbSAlexey Budankov static struct parse_tag tags[] = { 459470530bbSAlexey Budankov { .tag = 'B', .mult = 1 }, 460470530bbSAlexey Budankov { .tag = 'K', .mult = 1 << 10 }, 461470530bbSAlexey Budankov { .tag = 'M', .mult = 1 << 20 }, 462470530bbSAlexey Budankov { .tag = 'G', .mult = 1 << 30 }, 463470530bbSAlexey Budankov { .tag = 0 }, 464470530bbSAlexey Budankov }; 465470530bbSAlexey Budankov 466470530bbSAlexey Budankov if (unset) 467470530bbSAlexey Budankov return 0; 468470530bbSAlexey Budankov 469470530bbSAlexey Budankov if (str) { 470470530bbSAlexey Budankov opts->mmap_flush = parse_tag_value(str, tags); 471470530bbSAlexey Budankov if (opts->mmap_flush == (int)-1) 472470530bbSAlexey Budankov opts->mmap_flush = strtol(str, NULL, 0); 473470530bbSAlexey Budankov } 474470530bbSAlexey Budankov 475470530bbSAlexey Budankov if (!opts->mmap_flush) 476470530bbSAlexey Budankov opts->mmap_flush = MMAP_FLUSH_DEFAULT; 477470530bbSAlexey Budankov 4789521b5f2SJiri Olsa flush_max = evlist__mmap_size(opts->mmap_pages); 479470530bbSAlexey Budankov flush_max /= 4; 480470530bbSAlexey Budankov if (opts->mmap_flush > flush_max) 481470530bbSAlexey Budankov opts->mmap_flush = flush_max; 482470530bbSAlexey Budankov 483470530bbSAlexey Budankov return 0; 484470530bbSAlexey Budankov } 485470530bbSAlexey Budankov 486504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 487504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1; 488504c1ad1SAlexey Budankov 489504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset) 490504c1ad1SAlexey Budankov { 491504c1ad1SAlexey Budankov struct record_opts *opts = opt->value; 492504c1ad1SAlexey Budankov 493504c1ad1SAlexey Budankov if (unset) { 494504c1ad1SAlexey Budankov opts->comp_level = 0; 495504c1ad1SAlexey Budankov } else { 496504c1ad1SAlexey Budankov if (str) 497504c1ad1SAlexey Budankov opts->comp_level = strtol(str, NULL, 0); 498504c1ad1SAlexey Budankov if (!opts->comp_level) 499504c1ad1SAlexey Budankov opts->comp_level = comp_level_default; 500504c1ad1SAlexey Budankov } 501504c1ad1SAlexey Budankov 502504c1ad1SAlexey Budankov return 0; 503504c1ad1SAlexey Budankov } 504504c1ad1SAlexey Budankov #endif 50551255a8aSAlexey Budankov static unsigned int comp_level_max = 22; 50651255a8aSAlexey Budankov 50742e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec) 50842e1fd80SAlexey Budankov { 50942e1fd80SAlexey Budankov return rec->opts.comp_level > 0; 51042e1fd80SAlexey Budankov } 51142e1fd80SAlexey Budankov 51245694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 513d20deb64SArnaldo Carvalho de Melo union perf_event *event, 5141d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 5151d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 516234fbbf5SArnaldo Carvalho de Melo { 5178c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 518ded2b8feSJiri Olsa return record__write(rec, NULL, event, event->header.size); 519234fbbf5SArnaldo Carvalho de Melo } 520234fbbf5SArnaldo Carvalho de Melo 521d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool, 522d99c22eaSStephane Eranian union perf_event *event, 523d99c22eaSStephane Eranian struct perf_sample *sample __maybe_unused, 524d99c22eaSStephane Eranian struct machine *machine __maybe_unused) 525d99c22eaSStephane Eranian { 526d99c22eaSStephane Eranian static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER; 527d99c22eaSStephane Eranian int ret; 528d99c22eaSStephane Eranian 529d99c22eaSStephane Eranian pthread_mutex_lock(&synth_lock); 530d99c22eaSStephane Eranian ret = process_synthesized_event(tool, event, sample, machine); 531d99c22eaSStephane Eranian pthread_mutex_unlock(&synth_lock); 532d99c22eaSStephane Eranian return ret; 533d99c22eaSStephane Eranian } 534d99c22eaSStephane Eranian 535a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size) 536d37f1586SArnaldo Carvalho de Melo { 537d37f1586SArnaldo Carvalho de Melo struct record *rec = to; 538d37f1586SArnaldo Carvalho de Melo 5395d7f4116SAlexey Budankov if (record__comp_enabled(rec)) { 540bf59b305SJiri Olsa size = zstd_compress(rec->session, map->data, mmap__mmap_len(map), bf, size); 5415d7f4116SAlexey Budankov bf = map->data; 5425d7f4116SAlexey Budankov } 5435d7f4116SAlexey Budankov 544d37f1586SArnaldo Carvalho de Melo rec->samples++; 545ded2b8feSJiri Olsa return record__write(rec, map, bf, size); 546d37f1586SArnaldo Carvalho de Melo } 547d37f1586SArnaldo Carvalho de Melo 5482dd6d8a1SAdrian Hunter static volatile int signr = -1; 5492dd6d8a1SAdrian Hunter static volatile int child_finished; 550da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 551da231338SAnand K Mistry static int done_fd = -1; 552da231338SAnand K Mistry #endif 553c0bdc1c4SWang Nan 5542dd6d8a1SAdrian Hunter static void sig_handler(int sig) 5552dd6d8a1SAdrian Hunter { 5562dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 5572dd6d8a1SAdrian Hunter child_finished = 1; 5582dd6d8a1SAdrian Hunter else 5592dd6d8a1SAdrian Hunter signr = sig; 5602dd6d8a1SAdrian Hunter 5612dd6d8a1SAdrian Hunter done = 1; 562da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 563da231338SAnand K Mistry { 564da231338SAnand K Mistry u64 tmp = 1; 565da231338SAnand K Mistry /* 566da231338SAnand K Mistry * It is possible for this signal handler to run after done is checked 567da231338SAnand K Mistry * in the main loop, but before the perf counter fds are polled. If this 568da231338SAnand K Mistry * happens, the poll() will continue to wait even though done is set, 569da231338SAnand K Mistry * and will only break out if either another signal is received, or the 570da231338SAnand K Mistry * counters are ready for read. To ensure the poll() doesn't sleep when 571da231338SAnand K Mistry * done is set, use an eventfd (done_fd) to wake up the poll(). 572da231338SAnand K Mistry */ 573da231338SAnand K Mistry if (write(done_fd, &tmp, sizeof(tmp)) < 0) 574da231338SAnand K Mistry pr_err("failed to signal wakeup fd, error: %m\n"); 575da231338SAnand K Mistry } 576da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT 5772dd6d8a1SAdrian Hunter } 5782dd6d8a1SAdrian Hunter 579a074865eSWang Nan static void sigsegv_handler(int sig) 580a074865eSWang Nan { 581a074865eSWang Nan perf_hooks__recover(); 582a074865eSWang Nan sighandler_dump_stack(sig); 583a074865eSWang Nan } 584a074865eSWang Nan 5852dd6d8a1SAdrian Hunter static void record__sig_exit(void) 5862dd6d8a1SAdrian Hunter { 5872dd6d8a1SAdrian Hunter if (signr == -1) 5882dd6d8a1SAdrian Hunter return; 5892dd6d8a1SAdrian Hunter 5902dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 5912dd6d8a1SAdrian Hunter raise(signr); 5922dd6d8a1SAdrian Hunter } 5932dd6d8a1SAdrian Hunter 594e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 595e31f0d01SAdrian Hunter 596ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 597a5830532SJiri Olsa struct mmap *map, 598ef149c25SAdrian Hunter union perf_event *event, void *data1, 599ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 600ef149c25SAdrian Hunter { 601ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 6028ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 603ef149c25SAdrian Hunter size_t padding; 604ef149c25SAdrian Hunter u8 pad[8] = {0}; 605ef149c25SAdrian Hunter 60646e201efSAdrian Hunter if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) { 60799fa2984SAdrian Hunter off_t file_offset; 6088ceb41d7SJiri Olsa int fd = perf_data__fd(data); 60999fa2984SAdrian Hunter int err; 61099fa2984SAdrian Hunter 61199fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 61299fa2984SAdrian Hunter if (file_offset == -1) 61399fa2984SAdrian Hunter return -1; 61499fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 61599fa2984SAdrian Hunter event, file_offset); 61699fa2984SAdrian Hunter if (err) 61799fa2984SAdrian Hunter return err; 61899fa2984SAdrian Hunter } 61999fa2984SAdrian Hunter 620ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 621ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 622ef149c25SAdrian Hunter if (padding) 623ef149c25SAdrian Hunter padding = 8 - padding; 624ef149c25SAdrian Hunter 625ded2b8feSJiri Olsa record__write(rec, map, event, event->header.size); 626ded2b8feSJiri Olsa record__write(rec, map, data1, len1); 627ef149c25SAdrian Hunter if (len2) 628ded2b8feSJiri Olsa record__write(rec, map, data2, len2); 629ded2b8feSJiri Olsa record__write(rec, map, &pad, padding); 630ef149c25SAdrian Hunter 631ef149c25SAdrian Hunter return 0; 632ef149c25SAdrian Hunter } 633ef149c25SAdrian Hunter 634ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 635a5830532SJiri Olsa struct mmap *map) 636ef149c25SAdrian Hunter { 637ef149c25SAdrian Hunter int ret; 638ef149c25SAdrian Hunter 639e035f4caSJiri Olsa ret = auxtrace_mmap__read(map, rec->itr, &rec->tool, 640ef149c25SAdrian Hunter record__process_auxtrace); 641ef149c25SAdrian Hunter if (ret < 0) 642ef149c25SAdrian Hunter return ret; 643ef149c25SAdrian Hunter 644ef149c25SAdrian Hunter if (ret) 645ef149c25SAdrian Hunter rec->samples++; 646ef149c25SAdrian Hunter 647ef149c25SAdrian Hunter return 0; 648ef149c25SAdrian Hunter } 649ef149c25SAdrian Hunter 6502dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 651a5830532SJiri Olsa struct mmap *map) 6522dd6d8a1SAdrian Hunter { 6532dd6d8a1SAdrian Hunter int ret; 6542dd6d8a1SAdrian Hunter 655e035f4caSJiri Olsa ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool, 6562dd6d8a1SAdrian Hunter record__process_auxtrace, 6572dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 6582dd6d8a1SAdrian Hunter if (ret < 0) 6592dd6d8a1SAdrian Hunter return ret; 6602dd6d8a1SAdrian Hunter 6612dd6d8a1SAdrian Hunter if (ret) 6622dd6d8a1SAdrian Hunter rec->samples++; 6632dd6d8a1SAdrian Hunter 6642dd6d8a1SAdrian Hunter return 0; 6652dd6d8a1SAdrian Hunter } 6662dd6d8a1SAdrian Hunter 6672dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 6682dd6d8a1SAdrian Hunter { 6692dd6d8a1SAdrian Hunter int i; 6702dd6d8a1SAdrian Hunter int rc = 0; 6712dd6d8a1SAdrian Hunter 672c976ee11SJiri Olsa for (i = 0; i < rec->evlist->core.nr_mmaps; i++) { 673a5830532SJiri Olsa struct mmap *map = &rec->evlist->mmap[i]; 6742dd6d8a1SAdrian Hunter 675e035f4caSJiri Olsa if (!map->auxtrace_mmap.base) 6762dd6d8a1SAdrian Hunter continue; 6772dd6d8a1SAdrian Hunter 678e035f4caSJiri Olsa if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) { 6792dd6d8a1SAdrian Hunter rc = -1; 6802dd6d8a1SAdrian Hunter goto out; 6812dd6d8a1SAdrian Hunter } 6822dd6d8a1SAdrian Hunter } 6832dd6d8a1SAdrian Hunter out: 6842dd6d8a1SAdrian Hunter return rc; 6852dd6d8a1SAdrian Hunter } 6862dd6d8a1SAdrian Hunter 687ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit) 6882dd6d8a1SAdrian Hunter { 6892dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 6902dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 6915f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 6922dd6d8a1SAdrian Hunter } else { 693ce7b0e42SAlexander Shishkin if (auxtrace_record__snapshot_finish(rec->itr, on_exit)) 6945f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 6955f9cf599SWang Nan else 6965f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 6972dd6d8a1SAdrian Hunter } 6982dd6d8a1SAdrian Hunter } 6992dd6d8a1SAdrian Hunter 700ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec) 701ce7b0e42SAlexander Shishkin { 702ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 703ce7b0e42SAlexander Shishkin return 0; 704ce7b0e42SAlexander Shishkin 705ce7b0e42SAlexander Shishkin if (!auxtrace_record__snapshot_started && 706ce7b0e42SAlexander Shishkin auxtrace_record__snapshot_start(rec->itr)) 707ce7b0e42SAlexander Shishkin return -1; 708ce7b0e42SAlexander Shishkin 709ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, true); 710ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 711ce7b0e42SAlexander Shishkin return -1; 712ce7b0e42SAlexander Shishkin 713ce7b0e42SAlexander Shishkin return 0; 714ce7b0e42SAlexander Shishkin } 715ce7b0e42SAlexander Shishkin 7164b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec) 7174b5ea3bdSAdrian Hunter { 7184b5ea3bdSAdrian Hunter int err; 7194b5ea3bdSAdrian Hunter 7204b5ea3bdSAdrian Hunter if (!rec->itr) { 7214b5ea3bdSAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 7224b5ea3bdSAdrian Hunter if (err) 7234b5ea3bdSAdrian Hunter return err; 7244b5ea3bdSAdrian Hunter } 7254b5ea3bdSAdrian Hunter 7264b5ea3bdSAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 7274b5ea3bdSAdrian Hunter rec->opts.auxtrace_snapshot_opts); 7284b5ea3bdSAdrian Hunter if (err) 7294b5ea3bdSAdrian Hunter return err; 7304b5ea3bdSAdrian Hunter 731c0a6de06SAdrian Hunter err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts, 732c0a6de06SAdrian Hunter rec->opts.auxtrace_sample_opts); 733c0a6de06SAdrian Hunter if (err) 734c0a6de06SAdrian Hunter return err; 735c0a6de06SAdrian Hunter 736d58b3f7eSAdrian Hunter auxtrace_regroup_aux_output(rec->evlist); 737d58b3f7eSAdrian Hunter 7384b5ea3bdSAdrian Hunter return auxtrace_parse_filters(rec->evlist); 7394b5ea3bdSAdrian Hunter } 7404b5ea3bdSAdrian Hunter 741e31f0d01SAdrian Hunter #else 742e31f0d01SAdrian Hunter 743e31f0d01SAdrian Hunter static inline 744e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 745a5830532SJiri Olsa struct mmap *map __maybe_unused) 746e31f0d01SAdrian Hunter { 747e31f0d01SAdrian Hunter return 0; 748e31f0d01SAdrian Hunter } 749e31f0d01SAdrian Hunter 7502dd6d8a1SAdrian Hunter static inline 751ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused, 752ce7b0e42SAlexander Shishkin bool on_exit __maybe_unused) 7532dd6d8a1SAdrian Hunter { 7542dd6d8a1SAdrian Hunter } 7552dd6d8a1SAdrian Hunter 7562dd6d8a1SAdrian Hunter static inline 7572dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 7582dd6d8a1SAdrian Hunter { 7592dd6d8a1SAdrian Hunter return 0; 7602dd6d8a1SAdrian Hunter } 7612dd6d8a1SAdrian Hunter 762ce7b0e42SAlexander Shishkin static inline 763ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused) 764ce7b0e42SAlexander Shishkin { 765ce7b0e42SAlexander Shishkin return 0; 766ce7b0e42SAlexander Shishkin } 767ce7b0e42SAlexander Shishkin 7684b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused) 7694b5ea3bdSAdrian Hunter { 7704b5ea3bdSAdrian Hunter return 0; 7714b5ea3bdSAdrian Hunter } 7724b5ea3bdSAdrian Hunter 773e31f0d01SAdrian Hunter #endif 774e31f0d01SAdrian Hunter 775246eba8eSAdrian Hunter static int record__config_text_poke(struct evlist *evlist) 776246eba8eSAdrian Hunter { 777246eba8eSAdrian Hunter struct evsel *evsel; 778246eba8eSAdrian Hunter int err; 779246eba8eSAdrian Hunter 780246eba8eSAdrian Hunter /* Nothing to do if text poke is already configured */ 781246eba8eSAdrian Hunter evlist__for_each_entry(evlist, evsel) { 782246eba8eSAdrian Hunter if (evsel->core.attr.text_poke) 783246eba8eSAdrian Hunter return 0; 784246eba8eSAdrian Hunter } 785246eba8eSAdrian Hunter 786246eba8eSAdrian Hunter err = parse_events(evlist, "dummy:u", NULL); 787246eba8eSAdrian Hunter if (err) 788246eba8eSAdrian Hunter return err; 789246eba8eSAdrian Hunter 790246eba8eSAdrian Hunter evsel = evlist__last(evlist); 791246eba8eSAdrian Hunter 792246eba8eSAdrian Hunter evsel->core.attr.freq = 0; 793246eba8eSAdrian Hunter evsel->core.attr.sample_period = 1; 794246eba8eSAdrian Hunter evsel->core.attr.text_poke = 1; 795246eba8eSAdrian Hunter evsel->core.attr.ksymbol = 1; 796246eba8eSAdrian Hunter 797246eba8eSAdrian Hunter evsel->core.system_wide = true; 798246eba8eSAdrian Hunter evsel->no_aux_samples = true; 799246eba8eSAdrian Hunter evsel->immediate = true; 800246eba8eSAdrian Hunter 801246eba8eSAdrian Hunter /* Text poke must be collected on all CPUs */ 802246eba8eSAdrian Hunter perf_cpu_map__put(evsel->core.own_cpus); 803246eba8eSAdrian Hunter evsel->core.own_cpus = perf_cpu_map__new(NULL); 804246eba8eSAdrian Hunter perf_cpu_map__put(evsel->core.cpus); 805246eba8eSAdrian Hunter evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus); 806246eba8eSAdrian Hunter 807246eba8eSAdrian Hunter evsel__set_sample_bit(evsel, TIME); 808246eba8eSAdrian Hunter 809246eba8eSAdrian Hunter return 0; 810246eba8eSAdrian Hunter } 811246eba8eSAdrian Hunter 812eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine) 813eeb399b5SAdrian Hunter { 814eeb399b5SAdrian Hunter char kcore[PATH_MAX]; 815eeb399b5SAdrian Hunter int fd; 816eeb399b5SAdrian Hunter 817eeb399b5SAdrian Hunter scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir); 818eeb399b5SAdrian Hunter 819eeb399b5SAdrian Hunter fd = open(kcore, O_RDONLY); 820eeb399b5SAdrian Hunter if (fd < 0) 821eeb399b5SAdrian Hunter return false; 822eeb399b5SAdrian Hunter 823eeb399b5SAdrian Hunter close(fd); 824eeb399b5SAdrian Hunter 825eeb399b5SAdrian Hunter return true; 826eeb399b5SAdrian Hunter } 827eeb399b5SAdrian Hunter 828eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data) 829eeb399b5SAdrian Hunter { 830eeb399b5SAdrian Hunter char from_dir[PATH_MAX]; 831eeb399b5SAdrian Hunter char kcore_dir[PATH_MAX]; 832eeb399b5SAdrian Hunter int ret; 833eeb399b5SAdrian Hunter 834eeb399b5SAdrian Hunter snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir); 835eeb399b5SAdrian Hunter 836eeb399b5SAdrian Hunter ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir)); 837eeb399b5SAdrian Hunter if (ret) 838eeb399b5SAdrian Hunter return ret; 839eeb399b5SAdrian Hunter 840eeb399b5SAdrian Hunter return kcore_copy(from_dir, kcore_dir); 841eeb399b5SAdrian Hunter } 842eeb399b5SAdrian Hunter 843cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 84463503dbaSJiri Olsa struct evlist *evlist) 845cda57a8cSWang Nan { 846cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 847c0a6de06SAdrian Hunter bool auxtrace_overwrite = opts->auxtrace_snapshot_mode || 848c0a6de06SAdrian Hunter opts->auxtrace_sample_mode; 849cda57a8cSWang Nan char msg[512]; 850cda57a8cSWang Nan 851f13de660SAlexey Budankov if (opts->affinity != PERF_AFFINITY_SYS) 852f13de660SAlexey Budankov cpu__setup_cpunode_map(); 853f13de660SAlexey Budankov 8549521b5f2SJiri Olsa if (evlist__mmap_ex(evlist, opts->mmap_pages, 855cda57a8cSWang Nan opts->auxtrace_mmap_pages, 856c0a6de06SAdrian Hunter auxtrace_overwrite, 857470530bbSAlexey Budankov opts->nr_cblocks, opts->affinity, 85851255a8aSAlexey Budankov opts->mmap_flush, opts->comp_level) < 0) { 859cda57a8cSWang Nan if (errno == EPERM) { 860cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 861cda57a8cSWang Nan "Consider increasing " 862cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 863cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 864cda57a8cSWang Nan "(current value: %u,%u)\n", 865cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 866cda57a8cSWang Nan return -errno; 867cda57a8cSWang Nan } else { 868cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 869c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 870cda57a8cSWang Nan if (errno) 871cda57a8cSWang Nan return -errno; 872cda57a8cSWang Nan else 873cda57a8cSWang Nan return -EINVAL; 874cda57a8cSWang Nan } 875cda57a8cSWang Nan } 876cda57a8cSWang Nan return 0; 877cda57a8cSWang Nan } 878cda57a8cSWang Nan 879cda57a8cSWang Nan static int record__mmap(struct record *rec) 880cda57a8cSWang Nan { 881cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 882cda57a8cSWang Nan } 883cda57a8cSWang Nan 8848c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 885dd7927f4SArnaldo Carvalho de Melo { 886d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 88732dcd021SJiri Olsa struct evsel *pos; 88863503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 889d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 890b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 8918d3eca20SDavid Ahern int rc = 0; 892dd7927f4SArnaldo Carvalho de Melo 893d3dbf43cSArnaldo Carvalho de Melo /* 894b91e5492SKan Liang * For initial_delay, system wide or a hybrid system, we need to add a 895b91e5492SKan Liang * dummy event so that we can track PERF_RECORD_MMAP to cover the delay 896b91e5492SKan Liang * of waiting or event synthesis. 897d3dbf43cSArnaldo Carvalho de Melo */ 898b91e5492SKan Liang if (opts->initial_delay || target__has_cpu(&opts->target) || 899b91e5492SKan Liang perf_pmu__has_hybrid()) { 900e80db255SArnaldo Carvalho de Melo pos = evlist__get_tracking_event(evlist); 901442ad225SAdrian Hunter if (!evsel__is_dummy_event(pos)) { 902442ad225SAdrian Hunter /* Set up dummy event. */ 903e251abeeSArnaldo Carvalho de Melo if (evlist__add_dummy(evlist)) 904d3dbf43cSArnaldo Carvalho de Melo return -ENOMEM; 905515dbe48SJiri Olsa pos = evlist__last(evlist); 906e80db255SArnaldo Carvalho de Melo evlist__set_tracking_event(evlist, pos); 907442ad225SAdrian Hunter } 908442ad225SAdrian Hunter 9090a892c1cSIan Rogers /* 9100a892c1cSIan Rogers * Enable the dummy event when the process is forked for 9110a892c1cSIan Rogers * initial_delay, immediately for system wide. 9120a892c1cSIan Rogers */ 913bb07d62eSNamhyung Kim if (opts->initial_delay && !pos->immediate && 914bb07d62eSNamhyung Kim !target__has_cpu(&opts->target)) 9151fc632ceSJiri Olsa pos->core.attr.enable_on_exec = 1; 9160a892c1cSIan Rogers else 9170a892c1cSIan Rogers pos->immediate = 1; 918d3dbf43cSArnaldo Carvalho de Melo } 919d3dbf43cSArnaldo Carvalho de Melo 92078e1bc25SArnaldo Carvalho de Melo evlist__config(evlist, opts, &callchain_param); 921cac21425SJiri Olsa 922e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 9233da297a6SIngo Molnar try_again: 924af663bd0SJiri Olsa if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) { 925ae430892SArnaldo Carvalho de Melo if (evsel__fallback(pos, errno, msg, sizeof(msg))) { 926bb963e16SNamhyung Kim if (verbose > 0) 927c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 9283da297a6SIngo Molnar goto try_again; 9293da297a6SIngo Molnar } 930cf99ad14SAndi Kleen if ((errno == EINVAL || errno == EBADF) && 931fba7c866SJiri Olsa pos->core.leader != &pos->core && 932cf99ad14SAndi Kleen pos->weak_group) { 93364b4778bSArnaldo Carvalho de Melo pos = evlist__reset_weak_group(evlist, pos, true); 934cf99ad14SAndi Kleen goto try_again; 935cf99ad14SAndi Kleen } 93656e52e85SArnaldo Carvalho de Melo rc = -errno; 9372bb72dbbSArnaldo Carvalho de Melo evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg)); 93856e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 9398d3eca20SDavid Ahern goto out; 9407c6a1c65SPeter Zijlstra } 941bfd8f72cSAndi Kleen 942bfd8f72cSAndi Kleen pos->supported = true; 9437c6a1c65SPeter Zijlstra } 9447c6a1c65SPeter Zijlstra 94578e1bc25SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) { 946c8b567c8SArnaldo Carvalho de Melo pr_warning( 947c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 948c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n" 949c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 950c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 951c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 952c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 953c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 954c8b567c8SArnaldo Carvalho de Melo } 955c8b567c8SArnaldo Carvalho de Melo 95624bf91a7SArnaldo Carvalho de Melo if (evlist__apply_filters(evlist, &pos)) { 95762d94b00SArnaldo Carvalho de Melo pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 9588ab2e96dSArnaldo Carvalho de Melo pos->filter, evsel__name(pos), errno, 959c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 9608d3eca20SDavid Ahern rc = -1; 9618d3eca20SDavid Ahern goto out; 9620a102479SFrederic Weisbecker } 9630a102479SFrederic Weisbecker 964cda57a8cSWang Nan rc = record__mmap(rec); 965cda57a8cSWang Nan if (rc) 9668d3eca20SDavid Ahern goto out; 9670a27d7f9SArnaldo Carvalho de Melo 968a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 9697b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 9708d3eca20SDavid Ahern out: 9718d3eca20SDavid Ahern return rc; 972a91e5431SArnaldo Carvalho de Melo } 973a91e5431SArnaldo Carvalho de Melo 97466286ed3SAdrian Hunter static void set_timestamp_boundary(struct record *rec, u64 sample_time) 97566286ed3SAdrian Hunter { 97666286ed3SAdrian Hunter if (rec->evlist->first_sample_time == 0) 97766286ed3SAdrian Hunter rec->evlist->first_sample_time = sample_time; 97866286ed3SAdrian Hunter 97966286ed3SAdrian Hunter if (sample_time) 98066286ed3SAdrian Hunter rec->evlist->last_sample_time = sample_time; 98166286ed3SAdrian Hunter } 98266286ed3SAdrian Hunter 983e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 984e3d59112SNamhyung Kim union perf_event *event, 985e3d59112SNamhyung Kim struct perf_sample *sample, 98632dcd021SJiri Olsa struct evsel *evsel, 987e3d59112SNamhyung Kim struct machine *machine) 988e3d59112SNamhyung Kim { 989e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 990e3d59112SNamhyung Kim 99166286ed3SAdrian Hunter set_timestamp_boundary(rec, sample->time); 99268588bafSJin Yao 99368588bafSJin Yao if (rec->buildid_all) 99468588bafSJin Yao return 0; 99568588bafSJin Yao 99668588bafSJin Yao rec->samples++; 997e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 998e3d59112SNamhyung Kim } 999e3d59112SNamhyung Kim 10008c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 10016122e4e4SArnaldo Carvalho de Melo { 1002f5fc1412SJiri Olsa struct perf_session *session = rec->session; 10036122e4e4SArnaldo Carvalho de Melo 100445112e89SJiri Olsa if (perf_data__size(&rec->data) == 0) 10059f591fd7SArnaldo Carvalho de Melo return 0; 10069f591fd7SArnaldo Carvalho de Melo 100700dc8657SNamhyung Kim /* 100800dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 100900dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 101000dc8657SNamhyung Kim * we prefer the vmlinux path like 101100dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 101200dc8657SNamhyung Kim * 101300dc8657SNamhyung Kim * rather than build-id path (in debug directory). 101400dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 101500dc8657SNamhyung Kim */ 101600dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 101700dc8657SNamhyung Kim 10186156681bSNamhyung Kim /* 10196156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 102068588bafSJin Yao * so no need to process samples. But if timestamp_boundary is enabled, 102168588bafSJin Yao * it still needs to walk on all samples to get the timestamps of 102268588bafSJin Yao * first/last samples. 10236156681bSNamhyung Kim */ 102468588bafSJin Yao if (rec->buildid_all && !rec->timestamp_boundary) 10256156681bSNamhyung Kim rec->tool.sample = NULL; 10266156681bSNamhyung Kim 1027b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 10286122e4e4SArnaldo Carvalho de Melo } 10296122e4e4SArnaldo Carvalho de Melo 10308115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 1031a1645ce1SZhang, Yanmin { 1032a1645ce1SZhang, Yanmin int err; 103345694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 1034a1645ce1SZhang, Yanmin /* 1035a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 1036a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 1037a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 1038a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 1039a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 1040a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 1041a1645ce1SZhang, Yanmin */ 104245694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 1043743eb868SArnaldo Carvalho de Melo machine); 1044a1645ce1SZhang, Yanmin if (err < 0) 1045a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 104623346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 1047a1645ce1SZhang, Yanmin 1048a1645ce1SZhang, Yanmin /* 1049a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 1050a1645ce1SZhang, Yanmin * have no _text sometimes. 1051a1645ce1SZhang, Yanmin */ 105245694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 10530ae617beSAdrian Hunter machine); 1054a1645ce1SZhang, Yanmin if (err < 0) 1055a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 105623346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 1057a1645ce1SZhang, Yanmin } 1058a1645ce1SZhang, Yanmin 105998402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 106098402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 106198402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 106298402807SFrederic Weisbecker }; 106398402807SFrederic Weisbecker 1064a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map) 1065f13de660SAlexey Budankov { 1066f13de660SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS && 10678384a260SAlexey Budankov !bitmap_equal(rec->affinity_mask.bits, map->affinity_mask.bits, 10688384a260SAlexey Budankov rec->affinity_mask.nbits)) { 10698384a260SAlexey Budankov bitmap_zero(rec->affinity_mask.bits, rec->affinity_mask.nbits); 10708384a260SAlexey Budankov bitmap_or(rec->affinity_mask.bits, rec->affinity_mask.bits, 10718384a260SAlexey Budankov map->affinity_mask.bits, rec->affinity_mask.nbits); 10728384a260SAlexey Budankov sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&rec->affinity_mask), 10738384a260SAlexey Budankov (cpu_set_t *)rec->affinity_mask.bits); 10748384a260SAlexey Budankov if (verbose == 2) 10758384a260SAlexey Budankov mmap_cpu_mask__scnprintf(&rec->affinity_mask, "thread"); 1076f13de660SAlexey Budankov } 1077f13de660SAlexey Budankov } 1078f13de660SAlexey Budankov 10795d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment) 10805d7f4116SAlexey Budankov { 108172932371SJiri Olsa struct perf_record_compressed *event = record; 10825d7f4116SAlexey Budankov size_t size = sizeof(*event); 10835d7f4116SAlexey Budankov 10845d7f4116SAlexey Budankov if (increment) { 10855d7f4116SAlexey Budankov event->header.size += increment; 10865d7f4116SAlexey Budankov return increment; 10875d7f4116SAlexey Budankov } 10885d7f4116SAlexey Budankov 10895d7f4116SAlexey Budankov event->header.type = PERF_RECORD_COMPRESSED; 10905d7f4116SAlexey Budankov event->header.size = size; 10915d7f4116SAlexey Budankov 10925d7f4116SAlexey Budankov return size; 10935d7f4116SAlexey Budankov } 10945d7f4116SAlexey Budankov 10955d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size, 10965d7f4116SAlexey Budankov void *src, size_t src_size) 10975d7f4116SAlexey Budankov { 10985d7f4116SAlexey Budankov size_t compressed; 109972932371SJiri Olsa size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1; 11005d7f4116SAlexey Budankov 11015d7f4116SAlexey Budankov compressed = zstd_compress_stream_to_records(&session->zstd_data, dst, dst_size, src, src_size, 11025d7f4116SAlexey Budankov max_record_size, process_comp_header); 11035d7f4116SAlexey Budankov 11045d7f4116SAlexey Budankov session->bytes_transferred += src_size; 11055d7f4116SAlexey Budankov session->bytes_compressed += compressed; 11065d7f4116SAlexey Budankov 11075d7f4116SAlexey Budankov return compressed; 11085d7f4116SAlexey Budankov } 11095d7f4116SAlexey Budankov 111063503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist, 1111470530bbSAlexey Budankov bool overwrite, bool synch) 111298402807SFrederic Weisbecker { 1113dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 11140e2e63ddSPeter Zijlstra int i; 11158d3eca20SDavid Ahern int rc = 0; 1116a5830532SJiri Olsa struct mmap *maps; 1117d3d1af6fSAlexey Budankov int trace_fd = rec->data.file.fd; 1118ef781128SAlexey Budankov off_t off = 0; 111998402807SFrederic Weisbecker 1120cb21686bSWang Nan if (!evlist) 1121cb21686bSWang Nan return 0; 1122ef149c25SAdrian Hunter 11230b72d69aSWang Nan maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 1124a4ea0ec4SWang Nan if (!maps) 1125a4ea0ec4SWang Nan return 0; 1126cb21686bSWang Nan 11270b72d69aSWang Nan if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 112854cc54deSWang Nan return 0; 112954cc54deSWang Nan 1130d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1131d3d1af6fSAlexey Budankov off = record__aio_get_pos(trace_fd); 1132d3d1af6fSAlexey Budankov 1133c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 1134470530bbSAlexey Budankov u64 flush = 0; 1135a5830532SJiri Olsa struct mmap *map = &maps[i]; 1136a4ea0ec4SWang Nan 1137547740f7SJiri Olsa if (map->core.base) { 1138f13de660SAlexey Budankov record__adjust_affinity(rec, map); 1139470530bbSAlexey Budankov if (synch) { 114065aa2e6bSJiri Olsa flush = map->core.flush; 114165aa2e6bSJiri Olsa map->core.flush = 1; 1142470530bbSAlexey Budankov } 1143d3d1af6fSAlexey Budankov if (!record__aio_enabled(rec)) { 1144ef781128SAlexey Budankov if (perf_mmap__push(map, rec, record__pushfn) < 0) { 1145470530bbSAlexey Budankov if (synch) 114665aa2e6bSJiri Olsa map->core.flush = flush; 11478d3eca20SDavid Ahern rc = -1; 11488d3eca20SDavid Ahern goto out; 11498d3eca20SDavid Ahern } 1150d3d1af6fSAlexey Budankov } else { 1151ef781128SAlexey Budankov if (record__aio_push(rec, map, &off) < 0) { 1152d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1153470530bbSAlexey Budankov if (synch) 115465aa2e6bSJiri Olsa map->core.flush = flush; 1155d3d1af6fSAlexey Budankov rc = -1; 1156d3d1af6fSAlexey Budankov goto out; 1157d3d1af6fSAlexey Budankov } 1158d3d1af6fSAlexey Budankov } 1159470530bbSAlexey Budankov if (synch) 116065aa2e6bSJiri Olsa map->core.flush = flush; 11618d3eca20SDavid Ahern } 1162ef149c25SAdrian Hunter 1163e035f4caSJiri Olsa if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode && 1164c0a6de06SAdrian Hunter !rec->opts.auxtrace_sample_mode && 1165e035f4caSJiri Olsa record__auxtrace_mmap_read(rec, map) != 0) { 1166ef149c25SAdrian Hunter rc = -1; 1167ef149c25SAdrian Hunter goto out; 1168ef149c25SAdrian Hunter } 116998402807SFrederic Weisbecker } 117098402807SFrederic Weisbecker 1171d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1172d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1173d3d1af6fSAlexey Budankov 1174dcabb507SJiri Olsa /* 1175dcabb507SJiri Olsa * Mark the round finished in case we wrote 1176dcabb507SJiri Olsa * at least one event. 1177dcabb507SJiri Olsa */ 1178dcabb507SJiri Olsa if (bytes_written != rec->bytes_written) 1179ded2b8feSJiri Olsa rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event)); 11808d3eca20SDavid Ahern 11810b72d69aSWang Nan if (overwrite) 1182ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 11838d3eca20SDavid Ahern out: 11848d3eca20SDavid Ahern return rc; 118598402807SFrederic Weisbecker } 118698402807SFrederic Weisbecker 1187470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch) 1188cb21686bSWang Nan { 1189cb21686bSWang Nan int err; 1190cb21686bSWang Nan 1191470530bbSAlexey Budankov err = record__mmap_read_evlist(rec, rec->evlist, false, synch); 1192cb21686bSWang Nan if (err) 1193cb21686bSWang Nan return err; 1194cb21686bSWang Nan 1195470530bbSAlexey Budankov return record__mmap_read_evlist(rec, rec->evlist, true, synch); 1196cb21686bSWang Nan } 1197cb21686bSWang Nan 11988c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 119957706abcSDavid Ahern { 120057706abcSDavid Ahern struct perf_session *session = rec->session; 120157706abcSDavid Ahern int feat; 120257706abcSDavid Ahern 120357706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 120457706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 120557706abcSDavid Ahern 120657706abcSDavid Ahern if (rec->no_buildid) 120757706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 120857706abcSDavid Ahern 1209ce9036a6SJiri Olsa if (!have_tracepoints(&rec->evlist->core.entries)) 121057706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 121157706abcSDavid Ahern 121257706abcSDavid Ahern if (!rec->opts.branch_stack) 121357706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 1214ef149c25SAdrian Hunter 1215ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 1216ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 1217ffa517adSJiri Olsa 1218cf790516SAlexey Budankov if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns)) 1219cf790516SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_CLOCKID); 1220cf790516SAlexey Budankov 1221d1e325cfSJiri Olsa if (!rec->opts.use_clockid) 1222d1e325cfSJiri Olsa perf_header__clear_feat(&session->header, HEADER_CLOCK_DATA); 1223d1e325cfSJiri Olsa 1224258031c0SJiri Olsa perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT); 122542e1fd80SAlexey Budankov if (!record__comp_enabled(rec)) 122642e1fd80SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_COMPRESSED); 1227258031c0SJiri Olsa 1228ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 122957706abcSDavid Ahern } 123057706abcSDavid Ahern 1231e1ab48baSWang Nan static void 1232e1ab48baSWang Nan record__finish_output(struct record *rec) 1233e1ab48baSWang Nan { 12348ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 12358ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1236e1ab48baSWang Nan 12378ceb41d7SJiri Olsa if (data->is_pipe) 1238e1ab48baSWang Nan return; 1239e1ab48baSWang Nan 1240e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 124145112e89SJiri Olsa data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR); 1242e1ab48baSWang Nan 1243e1ab48baSWang Nan if (!rec->no_buildid) { 1244e1ab48baSWang Nan process_buildids(rec); 1245e1ab48baSWang Nan 1246e1ab48baSWang Nan if (rec->buildid_all) 1247e1ab48baSWang Nan dsos__hit_all(rec->session); 1248e1ab48baSWang Nan } 1249e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 1250e1ab48baSWang Nan 1251e1ab48baSWang Nan return; 1252e1ab48baSWang Nan } 1253e1ab48baSWang Nan 12544ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 1255be7b0c9eSWang Nan { 12569d6aae72SArnaldo Carvalho de Melo int err; 12579749b90eSJiri Olsa struct perf_thread_map *thread_map; 125841b740b6SNamhyung Kim bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP; 1259be7b0c9eSWang Nan 12604ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 12614ea648aeSWang Nan return 0; 12624ea648aeSWang Nan 12639d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 12649d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 12659d6aae72SArnaldo Carvalho de Melo return -1; 12669d6aae72SArnaldo Carvalho de Melo 12679d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 1268be7b0c9eSWang Nan process_synthesized_event, 1269be7b0c9eSWang Nan &rec->session->machines.host, 127041b740b6SNamhyung Kim needs_mmap, 12713fcb10e4SMark Drayton rec->opts.sample_address); 12727836e52eSJiri Olsa perf_thread_map__put(thread_map); 12739d6aae72SArnaldo Carvalho de Melo return err; 1274be7b0c9eSWang Nan } 1275be7b0c9eSWang Nan 12764ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 12773c1cb7e3SWang Nan 1278ecfd7a9cSWang Nan static int 1279ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 1280ecfd7a9cSWang Nan { 12818ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1282ecfd7a9cSWang Nan int fd, err; 128303724b2eSAndi Kleen char *new_filename; 1284ecfd7a9cSWang Nan 1285ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 1286ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 1287ecfd7a9cSWang Nan 1288d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1289d3d1af6fSAlexey Budankov 12904ea648aeSWang Nan record__synthesize(rec, true); 12914ea648aeSWang Nan if (target__none(&rec->opts.target)) 12924ea648aeSWang Nan record__synthesize_workload(rec, true); 12934ea648aeSWang Nan 1294ecfd7a9cSWang Nan rec->samples = 0; 1295ecfd7a9cSWang Nan record__finish_output(rec); 1296ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 1297ecfd7a9cSWang Nan if (err) { 1298ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 1299ecfd7a9cSWang Nan return -EINVAL; 1300ecfd7a9cSWang Nan } 1301ecfd7a9cSWang Nan 13028ceb41d7SJiri Olsa fd = perf_data__switch(data, timestamp, 1303ecfd7a9cSWang Nan rec->session->header.data_offset, 130403724b2eSAndi Kleen at_exit, &new_filename); 1305ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 1306ecfd7a9cSWang Nan rec->bytes_written = 0; 1307ecfd7a9cSWang Nan rec->session->header.data_size = 0; 1308ecfd7a9cSWang Nan } 1309ecfd7a9cSWang Nan 1310ecfd7a9cSWang Nan if (!quiet) 1311ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 13122d4f2799SJiri Olsa data->path, timestamp); 13133c1cb7e3SWang Nan 131403724b2eSAndi Kleen if (rec->switch_output.num_files) { 131503724b2eSAndi Kleen int n = rec->switch_output.cur_file + 1; 131603724b2eSAndi Kleen 131703724b2eSAndi Kleen if (n >= rec->switch_output.num_files) 131803724b2eSAndi Kleen n = 0; 131903724b2eSAndi Kleen rec->switch_output.cur_file = n; 132003724b2eSAndi Kleen if (rec->switch_output.filenames[n]) { 132103724b2eSAndi Kleen remove(rec->switch_output.filenames[n]); 1322d8f9da24SArnaldo Carvalho de Melo zfree(&rec->switch_output.filenames[n]); 132303724b2eSAndi Kleen } 132403724b2eSAndi Kleen rec->switch_output.filenames[n] = new_filename; 132503724b2eSAndi Kleen } else { 132603724b2eSAndi Kleen free(new_filename); 132703724b2eSAndi Kleen } 132803724b2eSAndi Kleen 13293c1cb7e3SWang Nan /* Output tracking events */ 1330be7b0c9eSWang Nan if (!at_exit) { 13314ea648aeSWang Nan record__synthesize(rec, false); 13323c1cb7e3SWang Nan 1333be7b0c9eSWang Nan /* 1334be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 1335be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 1336be7b0c9eSWang Nan * generate tracking events because there's no thread_map 1337be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 1338be7b0c9eSWang Nan * contain map and comm information. 1339be7b0c9eSWang Nan * Create a fake thread_map and directly call 1340be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 1341be7b0c9eSWang Nan */ 1342be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 13434ea648aeSWang Nan record__synthesize_workload(rec, false); 1344be7b0c9eSWang Nan } 1345ecfd7a9cSWang Nan return fd; 1346ecfd7a9cSWang Nan } 1347ecfd7a9cSWang Nan 1348f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno; 1349f33cbe72SArnaldo Carvalho de Melo 1350f33cbe72SArnaldo Carvalho de Melo /* 13517b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload will send a SIGUSR1 1352f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 1353f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 1354f33cbe72SArnaldo Carvalho de Melo */ 135545604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 135645604710SNamhyung Kim siginfo_t *info, 1357f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 1358f33cbe72SArnaldo Carvalho de Melo { 1359f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 1360f33cbe72SArnaldo Carvalho de Melo done = 1; 1361f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 1362f33cbe72SArnaldo Carvalho de Melo } 1363f33cbe72SArnaldo Carvalho de Melo 13642dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 1365bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 13662dd6d8a1SAdrian Hunter 1367db0ea13cSArnaldo Carvalho de Melo static const struct perf_event_mmap_page *evlist__pick_pc(struct evlist *evlist) 1368ee667f94SWang Nan { 1369b2cb615dSWang Nan if (evlist) { 1370547740f7SJiri Olsa if (evlist->mmap && evlist->mmap[0].core.base) 1371547740f7SJiri Olsa return evlist->mmap[0].core.base; 1372547740f7SJiri Olsa if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base) 1373547740f7SJiri Olsa return evlist->overwrite_mmap[0].core.base; 1374b2cb615dSWang Nan } 1375ee667f94SWang Nan return NULL; 1376ee667f94SWang Nan } 1377ee667f94SWang Nan 1378c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 1379c45628b0SWang Nan { 1380db0ea13cSArnaldo Carvalho de Melo const struct perf_event_mmap_page *pc = evlist__pick_pc(rec->evlist); 1381ee667f94SWang Nan if (pc) 1382ee667f94SWang Nan return pc; 1383c45628b0SWang Nan return NULL; 1384c45628b0SWang Nan } 1385c45628b0SWang Nan 13864ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 1387c45c86ebSWang Nan { 1388c45c86ebSWang Nan struct perf_session *session = rec->session; 1389c45c86ebSWang Nan struct machine *machine = &session->machines.host; 13908ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1391c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 1392c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 1393c45c86ebSWang Nan int err = 0; 1394d99c22eaSStephane Eranian event_op f = process_synthesized_event; 1395c45c86ebSWang Nan 13964ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 13974ea648aeSWang Nan return 0; 13984ea648aeSWang Nan 13998ceb41d7SJiri Olsa if (data->is_pipe) { 1400c3a057dcSNamhyung Kim err = perf_event__synthesize_for_pipe(tool, session, data, 1401c45c86ebSWang Nan process_synthesized_event); 1402c3a057dcSNamhyung Kim if (err < 0) 1403c45c86ebSWang Nan goto out; 1404c45c86ebSWang Nan 1405c45c86ebSWang Nan rec->bytes_written += err; 1406c45c86ebSWang Nan } 1407c45c86ebSWang Nan 1408c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 140946bc29b9SAdrian Hunter process_synthesized_event, machine); 141046bc29b9SAdrian Hunter if (err) 141146bc29b9SAdrian Hunter goto out; 141246bc29b9SAdrian Hunter 1413c0a6de06SAdrian Hunter /* Synthesize id_index before auxtrace_info */ 141461750473SAdrian Hunter if (rec->opts.auxtrace_sample_mode || rec->opts.full_auxtrace) { 1415c0a6de06SAdrian Hunter err = perf_event__synthesize_id_index(tool, 1416c0a6de06SAdrian Hunter process_synthesized_event, 1417c0a6de06SAdrian Hunter session->evlist, machine); 1418c0a6de06SAdrian Hunter if (err) 1419c0a6de06SAdrian Hunter goto out; 1420c0a6de06SAdrian Hunter } 1421c0a6de06SAdrian Hunter 1422c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 1423c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 1424c45c86ebSWang Nan session, process_synthesized_event); 1425c45c86ebSWang Nan if (err) 1426c45c86ebSWang Nan goto out; 1427c45c86ebSWang Nan } 1428c45c86ebSWang Nan 142978e1bc25SArnaldo Carvalho de Melo if (!evlist__exclude_kernel(rec->evlist)) { 1430c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 1431c45c86ebSWang Nan machine); 1432c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 1433c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1434c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 1435c45c86ebSWang Nan 1436c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 1437c45c86ebSWang Nan machine); 1438c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 1439c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1440c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 14416c443954SArnaldo Carvalho de Melo } 1442c45c86ebSWang Nan 1443c45c86ebSWang Nan if (perf_guest) { 1444c45c86ebSWang Nan machines__process_guests(&session->machines, 1445c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 1446c45c86ebSWang Nan } 1447c45c86ebSWang Nan 1448bfd8f72cSAndi Kleen err = perf_event__synthesize_extra_attr(&rec->tool, 1449bfd8f72cSAndi Kleen rec->evlist, 1450bfd8f72cSAndi Kleen process_synthesized_event, 1451bfd8f72cSAndi Kleen data->is_pipe); 1452bfd8f72cSAndi Kleen if (err) 1453bfd8f72cSAndi Kleen goto out; 1454bfd8f72cSAndi Kleen 145503617c22SJiri Olsa err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads, 1456373565d2SAndi Kleen process_synthesized_event, 1457373565d2SAndi Kleen NULL); 1458373565d2SAndi Kleen if (err < 0) { 1459373565d2SAndi Kleen pr_err("Couldn't synthesize thread map.\n"); 1460373565d2SAndi Kleen return err; 1461373565d2SAndi Kleen } 1462373565d2SAndi Kleen 1463f72f901dSJiri Olsa err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.cpus, 1464373565d2SAndi Kleen process_synthesized_event, NULL); 1465373565d2SAndi Kleen if (err < 0) { 1466373565d2SAndi Kleen pr_err("Couldn't synthesize cpu map.\n"); 1467373565d2SAndi Kleen return err; 1468373565d2SAndi Kleen } 1469373565d2SAndi Kleen 1470e5416950SSong Liu err = perf_event__synthesize_bpf_events(session, process_synthesized_event, 14717b612e29SSong Liu machine, opts); 14727b612e29SSong Liu if (err < 0) 14737b612e29SSong Liu pr_warning("Couldn't synthesize bpf events.\n"); 14747b612e29SSong Liu 147541b740b6SNamhyung Kim if (rec->opts.synth & PERF_SYNTH_CGROUP) { 1476ab64069fSNamhyung Kim err = perf_event__synthesize_cgroups(tool, process_synthesized_event, 1477ab64069fSNamhyung Kim machine); 1478ab64069fSNamhyung Kim if (err < 0) 1479ab64069fSNamhyung Kim pr_warning("Couldn't synthesize cgroup events.\n"); 148041b740b6SNamhyung Kim } 1481ab64069fSNamhyung Kim 1482d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) { 1483d99c22eaSStephane Eranian perf_set_multithreaded(); 1484d99c22eaSStephane Eranian f = process_locked_synthesized_event; 1485d99c22eaSStephane Eranian } 1486d99c22eaSStephane Eranian 148741b740b6SNamhyung Kim if (rec->opts.synth & PERF_SYNTH_TASK) { 148841b740b6SNamhyung Kim bool needs_mmap = rec->opts.synth & PERF_SYNTH_MMAP; 148941b740b6SNamhyung Kim 149084111b9cSNamhyung Kim err = __machine__synthesize_threads(machine, tool, &opts->target, 149184111b9cSNamhyung Kim rec->evlist->core.threads, 149241b740b6SNamhyung Kim f, needs_mmap, opts->sample_address, 1493d99c22eaSStephane Eranian rec->opts.nr_threads_synthesize); 149441b740b6SNamhyung Kim } 1495d99c22eaSStephane Eranian 1496d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) 1497d99c22eaSStephane Eranian perf_set_singlethreaded(); 1498d99c22eaSStephane Eranian 1499c45c86ebSWang Nan out: 1500c45c86ebSWang Nan return err; 1501c45c86ebSWang Nan } 1502c45c86ebSWang Nan 1503899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data) 1504899e5ffbSArnaldo Carvalho de Melo { 1505899e5ffbSArnaldo Carvalho de Melo struct record *rec = data; 1506899e5ffbSArnaldo Carvalho de Melo pthread_kill(rec->thread_id, SIGUSR2); 1507899e5ffbSArnaldo Carvalho de Melo return 0; 1508899e5ffbSArnaldo Carvalho de Melo } 1509899e5ffbSArnaldo Carvalho de Melo 151023cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec) 151123cbb41cSArnaldo Carvalho de Melo { 151223cbb41cSArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 151323cbb41cSArnaldo Carvalho de Melo 151423cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist != NULL) { 151523cbb41cSArnaldo Carvalho de Melo /* 151623cbb41cSArnaldo Carvalho de Melo * We get here if --switch-output-event populated the 151723cbb41cSArnaldo Carvalho de Melo * sb_evlist, so associate a callback that will send a SIGUSR2 151823cbb41cSArnaldo Carvalho de Melo * to the main thread. 151923cbb41cSArnaldo Carvalho de Melo */ 152023cbb41cSArnaldo Carvalho de Melo evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec); 152123cbb41cSArnaldo Carvalho de Melo rec->thread_id = pthread_self(); 152223cbb41cSArnaldo Carvalho de Melo } 15231101c872SJin Yao #ifdef HAVE_LIBBPF_SUPPORT 152423cbb41cSArnaldo Carvalho de Melo if (!opts->no_bpf_event) { 152523cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 152623cbb41cSArnaldo Carvalho de Melo rec->sb_evlist = evlist__new(); 152723cbb41cSArnaldo Carvalho de Melo 152823cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 152923cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't create side band evlist.\n."); 153023cbb41cSArnaldo Carvalho de Melo return -1; 153123cbb41cSArnaldo Carvalho de Melo } 153223cbb41cSArnaldo Carvalho de Melo } 153323cbb41cSArnaldo Carvalho de Melo 153423cbb41cSArnaldo Carvalho de Melo if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) { 153523cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n."); 153623cbb41cSArnaldo Carvalho de Melo return -1; 153723cbb41cSArnaldo Carvalho de Melo } 153823cbb41cSArnaldo Carvalho de Melo } 15391101c872SJin Yao #endif 154008c83997SArnaldo Carvalho de Melo if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) { 154123cbb41cSArnaldo Carvalho de Melo pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n"); 154223cbb41cSArnaldo Carvalho de Melo opts->no_bpf_event = true; 154323cbb41cSArnaldo Carvalho de Melo } 154423cbb41cSArnaldo Carvalho de Melo 154523cbb41cSArnaldo Carvalho de Melo return 0; 154623cbb41cSArnaldo Carvalho de Melo } 154723cbb41cSArnaldo Carvalho de Melo 1548d1e325cfSJiri Olsa static int record__init_clock(struct record *rec) 1549d1e325cfSJiri Olsa { 1550d1e325cfSJiri Olsa struct perf_session *session = rec->session; 1551d1e325cfSJiri Olsa struct timespec ref_clockid; 1552d1e325cfSJiri Olsa struct timeval ref_tod; 1553d1e325cfSJiri Olsa u64 ref; 1554d1e325cfSJiri Olsa 1555d1e325cfSJiri Olsa if (!rec->opts.use_clockid) 1556d1e325cfSJiri Olsa return 0; 1557d1e325cfSJiri Olsa 15589d88a1a1SJiri Olsa if (rec->opts.use_clockid && rec->opts.clockid_res_ns) 15599d88a1a1SJiri Olsa session->header.env.clock.clockid_res_ns = rec->opts.clockid_res_ns; 15609d88a1a1SJiri Olsa 1561d1e325cfSJiri Olsa session->header.env.clock.clockid = rec->opts.clockid; 1562d1e325cfSJiri Olsa 1563d1e325cfSJiri Olsa if (gettimeofday(&ref_tod, NULL) != 0) { 1564d1e325cfSJiri Olsa pr_err("gettimeofday failed, cannot set reference time.\n"); 1565d1e325cfSJiri Olsa return -1; 1566d1e325cfSJiri Olsa } 1567d1e325cfSJiri Olsa 1568d1e325cfSJiri Olsa if (clock_gettime(rec->opts.clockid, &ref_clockid)) { 1569d1e325cfSJiri Olsa pr_err("clock_gettime failed, cannot set reference time.\n"); 1570d1e325cfSJiri Olsa return -1; 1571d1e325cfSJiri Olsa } 1572d1e325cfSJiri Olsa 1573d1e325cfSJiri Olsa ref = (u64) ref_tod.tv_sec * NSEC_PER_SEC + 1574d1e325cfSJiri Olsa (u64) ref_tod.tv_usec * NSEC_PER_USEC; 1575d1e325cfSJiri Olsa 1576d1e325cfSJiri Olsa session->header.env.clock.tod_ns = ref; 1577d1e325cfSJiri Olsa 1578d1e325cfSJiri Olsa ref = (u64) ref_clockid.tv_sec * NSEC_PER_SEC + 1579d1e325cfSJiri Olsa (u64) ref_clockid.tv_nsec; 1580d1e325cfSJiri Olsa 1581d1e325cfSJiri Olsa session->header.env.clock.clockid_ns = ref; 1582d1e325cfSJiri Olsa return 0; 1583d1e325cfSJiri Olsa } 1584d1e325cfSJiri Olsa 1585d20aff15SAdrian Hunter static void hit_auxtrace_snapshot_trigger(struct record *rec) 1586d20aff15SAdrian Hunter { 1587d20aff15SAdrian Hunter if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 1588d20aff15SAdrian Hunter trigger_hit(&auxtrace_snapshot_trigger); 1589d20aff15SAdrian Hunter auxtrace_record__snapshot_started = 1; 1590d20aff15SAdrian Hunter if (auxtrace_record__snapshot_start(rec->itr)) 1591d20aff15SAdrian Hunter trigger_error(&auxtrace_snapshot_trigger); 1592d20aff15SAdrian Hunter } 1593d20aff15SAdrian Hunter } 1594d20aff15SAdrian Hunter 159591c0f5ecSJin Yao static void record__uniquify_name(struct record *rec) 159691c0f5ecSJin Yao { 159791c0f5ecSJin Yao struct evsel *pos; 159891c0f5ecSJin Yao struct evlist *evlist = rec->evlist; 159991c0f5ecSJin Yao char *new_name; 160091c0f5ecSJin Yao int ret; 160191c0f5ecSJin Yao 160291c0f5ecSJin Yao if (!perf_pmu__has_hybrid()) 160391c0f5ecSJin Yao return; 160491c0f5ecSJin Yao 160591c0f5ecSJin Yao evlist__for_each_entry(evlist, pos) { 160691c0f5ecSJin Yao if (!evsel__is_hybrid(pos)) 160791c0f5ecSJin Yao continue; 160891c0f5ecSJin Yao 160991c0f5ecSJin Yao if (strchr(pos->name, '/')) 161091c0f5ecSJin Yao continue; 161191c0f5ecSJin Yao 161291c0f5ecSJin Yao ret = asprintf(&new_name, "%s/%s/", 161391c0f5ecSJin Yao pos->pmu_name, pos->name); 161491c0f5ecSJin Yao if (ret) { 161591c0f5ecSJin Yao free(pos->name); 161691c0f5ecSJin Yao pos->name = new_name; 161791c0f5ecSJin Yao } 161891c0f5ecSJin Yao } 161991c0f5ecSJin Yao } 162091c0f5ecSJin Yao 16218c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 162286470930SIngo Molnar { 162357706abcSDavid Ahern int err; 162445604710SNamhyung Kim int status = 0; 16258b412664SPeter Zijlstra unsigned long waking = 0; 162646be604bSZhang, Yanmin const bool forks = argc > 0; 162745694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 1628b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 16298ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1630d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 16316dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 163242aa276fSNamhyung Kim int fd; 1633d3c8c08eSAlexey Budankov float ratio = 0; 1634acce0223SAlexey Budankov enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED; 163586470930SIngo Molnar 163645604710SNamhyung Kim atexit(record__sig_exit); 1637f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 1638f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 1639804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 1640a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 1641c0bdc1c4SWang Nan 1642f3b3614aSHari Bathini if (rec->opts.record_namespaces) 1643f3b3614aSHari Bathini tool->namespace_events = true; 1644f3b3614aSHari Bathini 16458fb4b679SNamhyung Kim if (rec->opts.record_cgroup) { 16468fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE 16478fb4b679SNamhyung Kim tool->cgroup_events = true; 16488fb4b679SNamhyung Kim #else 16498fb4b679SNamhyung Kim pr_err("cgroup tracking is not supported\n"); 16508fb4b679SNamhyung Kim return -1; 16518fb4b679SNamhyung Kim #endif 16528fb4b679SNamhyung Kim } 16538fb4b679SNamhyung Kim 1654dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 16552dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 16563c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 16575f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 1658dc0c6127SJiri Olsa if (rec->switch_output.enabled) 16593c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 1660c0bdc1c4SWang Nan } else { 16612dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 1662c0bdc1c4SWang Nan } 1663f5970550SPeter Zijlstra 16642681bd85SNamhyung Kim session = perf_session__new(data, tool); 16656ef81c55SMamatha Inamdar if (IS_ERR(session)) { 1666ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 16676ef81c55SMamatha Inamdar return PTR_ERR(session); 1668a9a70bbcSArnaldo Carvalho de Melo } 1669a9a70bbcSArnaldo Carvalho de Melo 16708ceb41d7SJiri Olsa fd = perf_data__fd(data); 1671d20deb64SArnaldo Carvalho de Melo rec->session = session; 1672d20deb64SArnaldo Carvalho de Melo 16735d7f4116SAlexey Budankov if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) { 16745d7f4116SAlexey Budankov pr_err("Compression initialization failed.\n"); 16755d7f4116SAlexey Budankov return -1; 16765d7f4116SAlexey Budankov } 1677da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 1678da231338SAnand K Mistry done_fd = eventfd(0, EFD_NONBLOCK); 1679da231338SAnand K Mistry if (done_fd < 0) { 1680da231338SAnand K Mistry pr_err("Failed to create wakeup eventfd, error: %m\n"); 1681da231338SAnand K Mistry status = -1; 1682da231338SAnand K Mistry goto out_delete_session; 1683da231338SAnand K Mistry } 1684e16c2ce7SYang Jihong err = evlist__add_wakeup_eventfd(rec->evlist, done_fd); 1685da231338SAnand K Mistry if (err < 0) { 1686da231338SAnand K Mistry pr_err("Failed to add wakeup eventfd to poll list\n"); 1687da231338SAnand K Mistry status = err; 1688da231338SAnand K Mistry goto out_delete_session; 1689da231338SAnand K Mistry } 1690da231338SAnand K Mistry #endif // HAVE_EVENTFD_SUPPORT 16915d7f4116SAlexey Budankov 16925d7f4116SAlexey Budankov session->header.env.comp_type = PERF_COMP_ZSTD; 16935d7f4116SAlexey Budankov session->header.env.comp_level = rec->opts.comp_level; 16945d7f4116SAlexey Budankov 1695eeb399b5SAdrian Hunter if (rec->opts.kcore && 1696eeb399b5SAdrian Hunter !record__kcore_readable(&session->machines.host)) { 1697eeb399b5SAdrian Hunter pr_err("ERROR: kcore is not readable.\n"); 1698eeb399b5SAdrian Hunter return -1; 1699eeb399b5SAdrian Hunter } 1700eeb399b5SAdrian Hunter 1701d1e325cfSJiri Olsa if (record__init_clock(rec)) 1702d1e325cfSJiri Olsa return -1; 1703d1e325cfSJiri Olsa 17048c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 1705330aa675SStephane Eranian 1706d4db3f16SArnaldo Carvalho de Melo if (forks) { 17077b392ef0SArnaldo Carvalho de Melo err = evlist__prepare_workload(rec->evlist, &opts->target, argv, data->is_pipe, 1708735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 170935b9d88eSArnaldo Carvalho de Melo if (err < 0) { 171035b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 171145604710SNamhyung Kim status = err; 171235b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 1713856e9660SPeter Zijlstra } 1714856e9660SPeter Zijlstra } 1715856e9660SPeter Zijlstra 1716ad46e48cSJiri Olsa /* 1717ad46e48cSJiri Olsa * If we have just single event and are sending data 1718ad46e48cSJiri Olsa * through pipe, we need to force the ids allocation, 1719ad46e48cSJiri Olsa * because we synthesize event name through the pipe 1720ad46e48cSJiri Olsa * and need the id for that. 1721ad46e48cSJiri Olsa */ 17226484d2f9SJiri Olsa if (data->is_pipe && rec->evlist->core.nr_entries == 1) 1723ad46e48cSJiri Olsa rec->opts.sample_id = true; 1724ad46e48cSJiri Olsa 172591c0f5ecSJin Yao record__uniquify_name(rec); 172691c0f5ecSJin Yao 17278c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 17288d3eca20SDavid Ahern err = -1; 172945604710SNamhyung Kim goto out_child; 17308d3eca20SDavid Ahern } 1731f6fa4375SJiri Olsa session->header.env.comp_mmap_len = session->evlist->core.mmap_len; 173286470930SIngo Molnar 1733eeb399b5SAdrian Hunter if (rec->opts.kcore) { 1734eeb399b5SAdrian Hunter err = record__kcore_copy(&session->machines.host, data); 1735eeb399b5SAdrian Hunter if (err) { 1736eeb399b5SAdrian Hunter pr_err("ERROR: Failed to copy kcore\n"); 1737eeb399b5SAdrian Hunter goto out_child; 1738eeb399b5SAdrian Hunter } 1739eeb399b5SAdrian Hunter } 1740eeb399b5SAdrian Hunter 17418690a2a7SWang Nan err = bpf__apply_obj_config(); 17428690a2a7SWang Nan if (err) { 17438690a2a7SWang Nan char errbuf[BUFSIZ]; 17448690a2a7SWang Nan 17458690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 17468690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 17478690a2a7SWang Nan errbuf); 17488690a2a7SWang Nan goto out_child; 17498690a2a7SWang Nan } 17508690a2a7SWang Nan 1751cca8482cSAdrian Hunter /* 1752cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 1753cca8482cSAdrian Hunter * evlist. 1754cca8482cSAdrian Hunter */ 17558cedf3a5SArnaldo Carvalho de Melo if (rec->tool.ordered_events && !evlist__sample_id_all(rec->evlist)) { 1756cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 1757cca8482cSAdrian Hunter rec->tool.ordered_events = false; 1758cca8482cSAdrian Hunter } 1759cca8482cSAdrian Hunter 17603a683120SJiri Olsa if (!rec->evlist->core.nr_groups) 1761a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 1762a8bb559bSNamhyung Kim 17638ceb41d7SJiri Olsa if (data->is_pipe) { 176442aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 1765529870e3STom Zanussi if (err < 0) 176645604710SNamhyung Kim goto out_child; 1767563aecb2SJiri Olsa } else { 176842aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 1769d5eed904SArnaldo Carvalho de Melo if (err < 0) 177045604710SNamhyung Kim goto out_child; 1771d5eed904SArnaldo Carvalho de Melo } 17727c6a1c65SPeter Zijlstra 1773b38d85efSArnaldo Carvalho de Melo err = -1; 1774d3665498SDavid Ahern if (!rec->no_buildid 1775e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 1776d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 1777e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 177845604710SNamhyung Kim goto out_child; 1779e20960c0SRobert Richter } 1780e20960c0SRobert Richter 178123cbb41cSArnaldo Carvalho de Melo err = record__setup_sb_evlist(rec); 178223cbb41cSArnaldo Carvalho de Melo if (err) 1783b38d85efSArnaldo Carvalho de Melo goto out_child; 1784657ee553SSong Liu 17854ea648aeSWang Nan err = record__synthesize(rec, false); 1786c45c86ebSWang Nan if (err < 0) 178745604710SNamhyung Kim goto out_child; 17888d3eca20SDavid Ahern 1789d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 179086470930SIngo Molnar struct sched_param param; 179186470930SIngo Molnar 1792d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 179386470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 17946beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 17958d3eca20SDavid Ahern err = -1; 179645604710SNamhyung Kim goto out_child; 179786470930SIngo Molnar } 179886470930SIngo Molnar } 179986470930SIngo Molnar 1800774cb499SJiri Olsa /* 1801774cb499SJiri Olsa * When perf is starting the traced process, all the events 1802774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 1803774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 1804774cb499SJiri Olsa */ 18056619a53eSAndi Kleen if (!target__none(&opts->target) && !opts->initial_delay) 18061c87f165SJiri Olsa evlist__enable(rec->evlist); 1807764e16a3SDavid Ahern 1808856e9660SPeter Zijlstra /* 1809856e9660SPeter Zijlstra * Let the child rip 1810856e9660SPeter Zijlstra */ 1811e803cf97SNamhyung Kim if (forks) { 181220a8a3cfSJiri Olsa struct machine *machine = &session->machines.host; 1813e5bed564SNamhyung Kim union perf_event *event; 1814e907caf3SHari Bathini pid_t tgid; 1815e5bed564SNamhyung Kim 1816e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 1817e5bed564SNamhyung Kim if (event == NULL) { 1818e5bed564SNamhyung Kim err = -ENOMEM; 1819e5bed564SNamhyung Kim goto out_child; 1820e5bed564SNamhyung Kim } 1821e5bed564SNamhyung Kim 1822e803cf97SNamhyung Kim /* 1823e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 1824e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 1825e803cf97SNamhyung Kim * cannot see a correct process name for those events. 1826e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 1827e803cf97SNamhyung Kim */ 1828e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 1829e803cf97SNamhyung Kim rec->evlist->workload.pid, 1830e803cf97SNamhyung Kim process_synthesized_event, 1831e803cf97SNamhyung Kim machine); 1832e5bed564SNamhyung Kim free(event); 1833e803cf97SNamhyung Kim 1834e907caf3SHari Bathini if (tgid == -1) 1835e907caf3SHari Bathini goto out_child; 1836e907caf3SHari Bathini 1837e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 1838e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 1839e907caf3SHari Bathini machine->id_hdr_size); 1840e907caf3SHari Bathini if (event == NULL) { 1841e907caf3SHari Bathini err = -ENOMEM; 1842e907caf3SHari Bathini goto out_child; 1843e907caf3SHari Bathini } 1844e907caf3SHari Bathini 1845e907caf3SHari Bathini /* 1846e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 1847e907caf3SHari Bathini */ 1848e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 1849e907caf3SHari Bathini rec->evlist->workload.pid, 1850e907caf3SHari Bathini tgid, process_synthesized_event, 1851e907caf3SHari Bathini machine); 1852e907caf3SHari Bathini free(event); 1853e907caf3SHari Bathini 18547b392ef0SArnaldo Carvalho de Melo evlist__start_workload(rec->evlist); 1855e803cf97SNamhyung Kim } 1856856e9660SPeter Zijlstra 18571d078ccbSAlexey Budankov if (evlist__initialize_ctlfd(rec->evlist, opts->ctl_fd, opts->ctl_fd_ack)) 18581d078ccbSAlexey Budankov goto out_child; 18591d078ccbSAlexey Budankov 18606619a53eSAndi Kleen if (opts->initial_delay) { 186168cd3b45SAlexey Budankov pr_info(EVLIST_DISABLED_MSG); 186268cd3b45SAlexey Budankov if (opts->initial_delay > 0) { 18630693e680SArnaldo Carvalho de Melo usleep(opts->initial_delay * USEC_PER_MSEC); 18641c87f165SJiri Olsa evlist__enable(rec->evlist); 186568cd3b45SAlexey Budankov pr_info(EVLIST_ENABLED_MSG); 186668cd3b45SAlexey Budankov } 18676619a53eSAndi Kleen } 18686619a53eSAndi Kleen 18695f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 18703c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 1871a074865eSWang Nan perf_hooks__invoke_record_start(); 1872649c48a9SPeter Zijlstra for (;;) { 18739f065194SYang Shi unsigned long long hits = rec->samples; 187486470930SIngo Molnar 187505737464SWang Nan /* 187605737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 187705737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 187805737464SWang Nan * hits != rec->samples in previous round. 187905737464SWang Nan * 1880ade9d208SArnaldo Carvalho de Melo * evlist__toggle_bkw_mmap ensure we never 188105737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 188205737464SWang Nan */ 188305737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 1884ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 188505737464SWang Nan 1886470530bbSAlexey Budankov if (record__mmap_read_all(rec, false) < 0) { 18875f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 18883c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 18898d3eca20SDavid Ahern err = -1; 189045604710SNamhyung Kim goto out_child; 18918d3eca20SDavid Ahern } 189286470930SIngo Molnar 18932dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 18942dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 18955f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 1896ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, false); 18975f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 18982dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 18992dd6d8a1SAdrian Hunter err = -1; 19002dd6d8a1SAdrian Hunter goto out_child; 19012dd6d8a1SAdrian Hunter } 19022dd6d8a1SAdrian Hunter } 19032dd6d8a1SAdrian Hunter 19043c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 190505737464SWang Nan /* 190605737464SWang Nan * If switch_output_trigger is hit, the data in 190705737464SWang Nan * overwritable ring buffer should have been collected, 190805737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 190905737464SWang Nan * 191005737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 191105737464SWang Nan * record__mmap_read_all() didn't collect data from 191205737464SWang Nan * overwritable ring buffer. Read again. 191305737464SWang Nan */ 191405737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 191505737464SWang Nan continue; 19163c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 19173c1cb7e3SWang Nan 191805737464SWang Nan /* 191905737464SWang Nan * Reenable events in overwrite ring buffer after 192005737464SWang Nan * record__mmap_read_all(): we should have collected 192105737464SWang Nan * data from it. 192205737464SWang Nan */ 1923ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 192405737464SWang Nan 19253c1cb7e3SWang Nan if (!quiet) 19263c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 19273c1cb7e3SWang Nan waking); 19283c1cb7e3SWang Nan waking = 0; 19293c1cb7e3SWang Nan fd = record__switch_output(rec, false); 19303c1cb7e3SWang Nan if (fd < 0) { 19313c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 19323c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 19333c1cb7e3SWang Nan err = fd; 19343c1cb7e3SWang Nan goto out_child; 19353c1cb7e3SWang Nan } 1936bfacbe3bSJiri Olsa 1937bfacbe3bSJiri Olsa /* re-arm the alarm */ 1938bfacbe3bSJiri Olsa if (rec->switch_output.time) 1939bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 19403c1cb7e3SWang Nan } 19413c1cb7e3SWang Nan 1942d20deb64SArnaldo Carvalho de Melo if (hits == rec->samples) { 19436dcf45efSArnaldo Carvalho de Melo if (done || draining) 1944649c48a9SPeter Zijlstra break; 194580ab2987SJiri Olsa err = evlist__poll(rec->evlist, -1); 1946a515114fSJiri Olsa /* 1947a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 1948a515114fSJiri Olsa * number of returned events and interrupt error. 1949a515114fSJiri Olsa */ 1950a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 195145604710SNamhyung Kim err = 0; 19528b412664SPeter Zijlstra waking++; 19536dcf45efSArnaldo Carvalho de Melo 1954f4009e7bSJiri Olsa if (evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) 19556dcf45efSArnaldo Carvalho de Melo draining = true; 19568b412664SPeter Zijlstra } 19578b412664SPeter Zijlstra 1958acce0223SAlexey Budankov if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) { 1959acce0223SAlexey Budankov switch (cmd) { 1960d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 1961d20aff15SAdrian Hunter hit_auxtrace_snapshot_trigger(rec); 1962d20aff15SAdrian Hunter evlist__ctlfd_ack(rec->evlist); 1963d20aff15SAdrian Hunter break; 1964f186cd61SJiri Olsa case EVLIST_CTL_CMD_STOP: 1965f186cd61SJiri Olsa done = 1; 1966f186cd61SJiri Olsa break; 1967acce0223SAlexey Budankov case EVLIST_CTL_CMD_ACK: 1968acce0223SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 1969991ae4ebSJiri Olsa case EVLIST_CTL_CMD_ENABLE: 1970991ae4ebSJiri Olsa case EVLIST_CTL_CMD_DISABLE: 1971142544a9SJiri Olsa case EVLIST_CTL_CMD_EVLIST: 197247fddcb4SJiri Olsa case EVLIST_CTL_CMD_PING: 1973acce0223SAlexey Budankov default: 1974acce0223SAlexey Budankov break; 1975acce0223SAlexey Budankov } 1976acce0223SAlexey Budankov } 1977acce0223SAlexey Budankov 1978774cb499SJiri Olsa /* 1979774cb499SJiri Olsa * When perf is starting the traced process, at the end events 1980774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 1981774cb499SJiri Olsa * disable events in this case. 1982774cb499SJiri Olsa */ 1983602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 19845f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 1985e74676deSJiri Olsa evlist__disable(rec->evlist); 19862711926aSJiri Olsa disabled = true; 19872711926aSJiri Olsa } 19888b412664SPeter Zijlstra } 1989ce7b0e42SAlexander Shishkin 19905f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 19913c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 19928b412664SPeter Zijlstra 1993ce7b0e42SAlexander Shishkin if (opts->auxtrace_snapshot_on_exit) 1994ce7b0e42SAlexander Shishkin record__auxtrace_snapshot_exit(rec); 1995ce7b0e42SAlexander Shishkin 1996f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 19973535a696SArnaldo Carvalho de Melo char msg[STRERR_BUFSIZE], strevsels[2048]; 1998c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 19993535a696SArnaldo Carvalho de Melo 20003535a696SArnaldo Carvalho de Melo evlist__scnprintf_evsels(rec->evlist, sizeof(strevsels), strevsels); 20013535a696SArnaldo Carvalho de Melo 20023535a696SArnaldo Carvalho de Melo pr_err("Failed to collect '%s' for the '%s' workload: %s\n", 20033535a696SArnaldo Carvalho de Melo strevsels, argv[0], emsg); 2004f33cbe72SArnaldo Carvalho de Melo err = -1; 200545604710SNamhyung Kim goto out_child; 2006f33cbe72SArnaldo Carvalho de Melo } 2007f33cbe72SArnaldo Carvalho de Melo 2008e3d59112SNamhyung Kim if (!quiet) 20098b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 201086470930SIngo Molnar 20114ea648aeSWang Nan if (target__none(&rec->opts.target)) 20124ea648aeSWang Nan record__synthesize_workload(rec, true); 20134ea648aeSWang Nan 201445604710SNamhyung Kim out_child: 20151d078ccbSAlexey Budankov evlist__finalize_ctlfd(rec->evlist); 2016470530bbSAlexey Budankov record__mmap_read_all(rec, true); 2017d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 2018d3d1af6fSAlexey Budankov 2019d3c8c08eSAlexey Budankov if (rec->session->bytes_transferred && rec->session->bytes_compressed) { 2020d3c8c08eSAlexey Budankov ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed; 2021d3c8c08eSAlexey Budankov session->header.env.comp_ratio = ratio + 0.5; 2022d3c8c08eSAlexey Budankov } 2023d3c8c08eSAlexey Budankov 202445604710SNamhyung Kim if (forks) { 202545604710SNamhyung Kim int exit_status; 202645604710SNamhyung Kim 202745604710SNamhyung Kim if (!child_finished) 202845604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 202945604710SNamhyung Kim 203045604710SNamhyung Kim wait(&exit_status); 203145604710SNamhyung Kim 203245604710SNamhyung Kim if (err < 0) 203345604710SNamhyung Kim status = err; 203445604710SNamhyung Kim else if (WIFEXITED(exit_status)) 203545604710SNamhyung Kim status = WEXITSTATUS(exit_status); 203645604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 203745604710SNamhyung Kim signr = WTERMSIG(exit_status); 203845604710SNamhyung Kim } else 203945604710SNamhyung Kim status = err; 204045604710SNamhyung Kim 20414ea648aeSWang Nan record__synthesize(rec, true); 2042e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 2043e3d59112SNamhyung Kim rec->samples = 0; 2044e3d59112SNamhyung Kim 2045ecfd7a9cSWang Nan if (!err) { 2046ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 2047e1ab48baSWang Nan record__finish_output(rec); 2048ecfd7a9cSWang Nan } else { 2049ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 2050ecfd7a9cSWang Nan if (fd < 0) { 2051ecfd7a9cSWang Nan status = fd; 2052ecfd7a9cSWang Nan goto out_delete_session; 2053ecfd7a9cSWang Nan } 2054ecfd7a9cSWang Nan } 2055ecfd7a9cSWang Nan } 205639d17dacSArnaldo Carvalho de Melo 2057a074865eSWang Nan perf_hooks__invoke_record_end(); 2058a074865eSWang Nan 2059e3d59112SNamhyung Kim if (!err && !quiet) { 2060e3d59112SNamhyung Kim char samples[128]; 2061ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 2062ecfd7a9cSWang Nan ".<timestamp>" : ""; 2063e3d59112SNamhyung Kim 2064ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 2065e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 2066e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 2067e3d59112SNamhyung Kim else 2068e3d59112SNamhyung Kim samples[0] = '\0'; 2069e3d59112SNamhyung Kim 2070d3c8c08eSAlexey Budankov fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s", 20718ceb41d7SJiri Olsa perf_data__size(data) / 1024.0 / 1024.0, 20722d4f2799SJiri Olsa data->path, postfix, samples); 2073d3c8c08eSAlexey Budankov if (ratio) { 2074d3c8c08eSAlexey Budankov fprintf(stderr, ", compressed (original %.3f MB, ratio is %.3f)", 2075d3c8c08eSAlexey Budankov rec->session->bytes_transferred / 1024.0 / 1024.0, 2076d3c8c08eSAlexey Budankov ratio); 2077d3c8c08eSAlexey Budankov } 2078d3c8c08eSAlexey Budankov fprintf(stderr, " ]\n"); 2079e3d59112SNamhyung Kim } 2080e3d59112SNamhyung Kim 208139d17dacSArnaldo Carvalho de Melo out_delete_session: 2082da231338SAnand K Mistry #ifdef HAVE_EVENTFD_SUPPORT 2083da231338SAnand K Mistry if (done_fd >= 0) 2084da231338SAnand K Mistry close(done_fd); 2085da231338SAnand K Mistry #endif 20865d7f4116SAlexey Budankov zstd_fini(&session->zstd_data); 208739d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 2088657ee553SSong Liu 2089657ee553SSong Liu if (!opts->no_bpf_event) 209008c83997SArnaldo Carvalho de Melo evlist__stop_sb_thread(rec->sb_evlist); 209145604710SNamhyung Kim return status; 209286470930SIngo Molnar } 209386470930SIngo Molnar 20940883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 209509b0fd45SJiri Olsa { 2096aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 2097a601fdffSJiri Olsa 20980883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 209926d33022SJiri Olsa 21000883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 210109b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 21020883e820SArnaldo Carvalho de Melo callchain->dump_size); 21030883e820SArnaldo Carvalho de Melo } 21040883e820SArnaldo Carvalho de Melo 21050883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 21060883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 21070883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 21080883e820SArnaldo Carvalho de Melo { 21090883e820SArnaldo Carvalho de Melo int ret; 21100883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 21110883e820SArnaldo Carvalho de Melo 21120883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 21130883e820SArnaldo Carvalho de Melo if (unset) { 21140883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 21150883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 21160883e820SArnaldo Carvalho de Melo return 0; 21170883e820SArnaldo Carvalho de Melo } 21180883e820SArnaldo Carvalho de Melo 21190883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 21200883e820SArnaldo Carvalho de Melo if (!ret) { 21210883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 21220883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 21230883e820SArnaldo Carvalho de Melo record->sample_address = true; 21240883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 21250883e820SArnaldo Carvalho de Melo } 21260883e820SArnaldo Carvalho de Melo 21270883e820SArnaldo Carvalho de Melo return ret; 212809b0fd45SJiri Olsa } 212909b0fd45SJiri Olsa 2130c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 213109b0fd45SJiri Olsa const char *arg, 213209b0fd45SJiri Olsa int unset) 213309b0fd45SJiri Olsa { 21340883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 213526d33022SJiri Olsa } 213626d33022SJiri Olsa 2137c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 213809b0fd45SJiri Olsa const char *arg __maybe_unused, 213909b0fd45SJiri Olsa int unset __maybe_unused) 214009b0fd45SJiri Olsa { 21412ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 2142c421e80bSKan Liang 21432ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 214409b0fd45SJiri Olsa 21452ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 21462ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 2147eb853e80SJiri Olsa 21482ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 214909b0fd45SJiri Olsa return 0; 215009b0fd45SJiri Olsa } 215109b0fd45SJiri Olsa 2152eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 2153eb853e80SJiri Olsa { 21547a29c087SNamhyung Kim struct record *rec = cb; 21557a29c087SNamhyung Kim 21567a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 21577a29c087SNamhyung Kim if (!strcmp(value, "cache")) 21587a29c087SNamhyung Kim rec->no_buildid_cache = false; 21597a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 21607a29c087SNamhyung Kim rec->no_buildid_cache = true; 21617a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 21627a29c087SNamhyung Kim rec->no_buildid = true; 2163e29386c8SJiri Olsa else if (!strcmp(value, "mmap")) 2164e29386c8SJiri Olsa rec->buildid_mmap = true; 21657a29c087SNamhyung Kim else 21667a29c087SNamhyung Kim return -1; 21677a29c087SNamhyung Kim return 0; 21687a29c087SNamhyung Kim } 2169cff17205SYisheng Xie if (!strcmp(var, "record.call-graph")) { 2170cff17205SYisheng Xie var = "call-graph.record-mode"; 2171eb853e80SJiri Olsa return perf_default_config(var, value, cb); 2172eb853e80SJiri Olsa } 217393f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 217493f20c0fSAlexey Budankov if (!strcmp(var, "record.aio")) { 217593f20c0fSAlexey Budankov rec->opts.nr_cblocks = strtol(value, NULL, 0); 217693f20c0fSAlexey Budankov if (!rec->opts.nr_cblocks) 217793f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_default; 217893f20c0fSAlexey Budankov } 217993f20c0fSAlexey Budankov #endif 2180eb853e80SJiri Olsa 2181cff17205SYisheng Xie return 0; 2182cff17205SYisheng Xie } 2183cff17205SYisheng Xie 2184814c8c38SPeter Zijlstra 2185f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset) 2186f4fe11b7SAlexey Budankov { 2187f4fe11b7SAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 2188f4fe11b7SAlexey Budankov 2189f4fe11b7SAlexey Budankov if (unset || !str) 2190f4fe11b7SAlexey Budankov return 0; 2191f4fe11b7SAlexey Budankov 2192f4fe11b7SAlexey Budankov if (!strcasecmp(str, "node")) 2193f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_NODE; 2194f4fe11b7SAlexey Budankov else if (!strcasecmp(str, "cpu")) 2195f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_CPU; 2196f4fe11b7SAlexey Budankov 2197f4fe11b7SAlexey Budankov return 0; 2198f4fe11b7SAlexey Budankov } 2199f4fe11b7SAlexey Budankov 22006d575816SJiwei Sun static int parse_output_max_size(const struct option *opt, 22016d575816SJiwei Sun const char *str, int unset) 22026d575816SJiwei Sun { 22036d575816SJiwei Sun unsigned long *s = (unsigned long *)opt->value; 22046d575816SJiwei Sun static struct parse_tag tags_size[] = { 22056d575816SJiwei Sun { .tag = 'B', .mult = 1 }, 22066d575816SJiwei Sun { .tag = 'K', .mult = 1 << 10 }, 22076d575816SJiwei Sun { .tag = 'M', .mult = 1 << 20 }, 22086d575816SJiwei Sun { .tag = 'G', .mult = 1 << 30 }, 22096d575816SJiwei Sun { .tag = 0 }, 22106d575816SJiwei Sun }; 22116d575816SJiwei Sun unsigned long val; 22126d575816SJiwei Sun 22136d575816SJiwei Sun if (unset) { 22146d575816SJiwei Sun *s = 0; 22156d575816SJiwei Sun return 0; 22166d575816SJiwei Sun } 22176d575816SJiwei Sun 22186d575816SJiwei Sun val = parse_tag_value(str, tags_size); 22196d575816SJiwei Sun if (val != (unsigned long) -1) { 22206d575816SJiwei Sun *s = val; 22216d575816SJiwei Sun return 0; 22226d575816SJiwei Sun } 22236d575816SJiwei Sun 22246d575816SJiwei Sun return -1; 22256d575816SJiwei Sun } 22266d575816SJiwei Sun 2227e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 2228e9db1310SAdrian Hunter const char *str, 2229e9db1310SAdrian Hunter int unset __maybe_unused) 2230e9db1310SAdrian Hunter { 2231e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 2232e9db1310SAdrian Hunter char *s, *p; 2233e9db1310SAdrian Hunter unsigned int mmap_pages; 2234e9db1310SAdrian Hunter int ret; 2235e9db1310SAdrian Hunter 2236e9db1310SAdrian Hunter if (!str) 2237e9db1310SAdrian Hunter return -EINVAL; 2238e9db1310SAdrian Hunter 2239e9db1310SAdrian Hunter s = strdup(str); 2240e9db1310SAdrian Hunter if (!s) 2241e9db1310SAdrian Hunter return -ENOMEM; 2242e9db1310SAdrian Hunter 2243e9db1310SAdrian Hunter p = strchr(s, ','); 2244e9db1310SAdrian Hunter if (p) 2245e9db1310SAdrian Hunter *p = '\0'; 2246e9db1310SAdrian Hunter 2247e9db1310SAdrian Hunter if (*s) { 224825f84702SArnaldo Carvalho de Melo ret = __evlist__parse_mmap_pages(&mmap_pages, s); 2249e9db1310SAdrian Hunter if (ret) 2250e9db1310SAdrian Hunter goto out_free; 2251e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 2252e9db1310SAdrian Hunter } 2253e9db1310SAdrian Hunter 2254e9db1310SAdrian Hunter if (!p) { 2255e9db1310SAdrian Hunter ret = 0; 2256e9db1310SAdrian Hunter goto out_free; 2257e9db1310SAdrian Hunter } 2258e9db1310SAdrian Hunter 225925f84702SArnaldo Carvalho de Melo ret = __evlist__parse_mmap_pages(&mmap_pages, p + 1); 2260e9db1310SAdrian Hunter if (ret) 2261e9db1310SAdrian Hunter goto out_free; 2262e9db1310SAdrian Hunter 2263e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 2264e9db1310SAdrian Hunter 2265e9db1310SAdrian Hunter out_free: 2266e9db1310SAdrian Hunter free(s); 2267e9db1310SAdrian Hunter return ret; 2268e9db1310SAdrian Hunter } 2269e9db1310SAdrian Hunter 22701d078ccbSAlexey Budankov static int parse_control_option(const struct option *opt, 22711d078ccbSAlexey Budankov const char *str, 22721d078ccbSAlexey Budankov int unset __maybe_unused) 22731d078ccbSAlexey Budankov { 22749864a66dSAdrian Hunter struct record_opts *opts = opt->value; 22751d078ccbSAlexey Budankov 2276a8fcbd26SAdrian Hunter return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close); 2277a8fcbd26SAdrian Hunter } 2278a8fcbd26SAdrian Hunter 22790c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 22800c582449SJiri Olsa { 22819521b5f2SJiri Olsa u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages); 22820c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 22830c582449SJiri Olsa 22840c582449SJiri Olsa wakeup_size /= 2; 22850c582449SJiri Olsa 22860c582449SJiri Olsa if (s->size < wakeup_size) { 22870c582449SJiri Olsa char buf[100]; 22880c582449SJiri Olsa 22890c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 22900c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 22910c582449SJiri Olsa "wakeup kernel buffer size (%s) " 22920c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 22930c582449SJiri Olsa } 22940c582449SJiri Olsa } 22950c582449SJiri Olsa 2296cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 2297cb4e1ebbSJiri Olsa { 2298cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 2299dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 2300dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 2301dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 2302dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 2303dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 2304dc0c6127SJiri Olsa { .tag = 0 }, 2305dc0c6127SJiri Olsa }; 2306bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 2307bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 2308bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 2309bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 2310bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 2311bfacbe3bSJiri Olsa { .tag = 0 }, 2312bfacbe3bSJiri Olsa }; 2313dc0c6127SJiri Olsa unsigned long val; 2314cb4e1ebbSJiri Olsa 2315899e5ffbSArnaldo Carvalho de Melo /* 2316899e5ffbSArnaldo Carvalho de Melo * If we're using --switch-output-events, then we imply its 2317899e5ffbSArnaldo Carvalho de Melo * --switch-output=signal, as we'll send a SIGUSR2 from the side band 2318899e5ffbSArnaldo Carvalho de Melo * thread to its parent. 2319899e5ffbSArnaldo Carvalho de Melo */ 2320899e5ffbSArnaldo Carvalho de Melo if (rec->switch_output_event_set) 2321899e5ffbSArnaldo Carvalho de Melo goto do_signal; 2322899e5ffbSArnaldo Carvalho de Melo 2323cb4e1ebbSJiri Olsa if (!s->set) 2324cb4e1ebbSJiri Olsa return 0; 2325cb4e1ebbSJiri Olsa 2326cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 2327899e5ffbSArnaldo Carvalho de Melo do_signal: 2328cb4e1ebbSJiri Olsa s->signal = true; 2329cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 2330dc0c6127SJiri Olsa goto enabled; 2331dc0c6127SJiri Olsa } 2332dc0c6127SJiri Olsa 2333dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 2334dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 2335dc0c6127SJiri Olsa s->size = val; 2336dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 2337dc0c6127SJiri Olsa goto enabled; 2338cb4e1ebbSJiri Olsa } 2339cb4e1ebbSJiri Olsa 2340bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 2341bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 2342bfacbe3bSJiri Olsa s->time = val; 2343bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 2344bfacbe3bSJiri Olsa s->str, s->time); 2345bfacbe3bSJiri Olsa goto enabled; 2346bfacbe3bSJiri Olsa } 2347bfacbe3bSJiri Olsa 2348cb4e1ebbSJiri Olsa return -1; 2349dc0c6127SJiri Olsa 2350dc0c6127SJiri Olsa enabled: 2351dc0c6127SJiri Olsa rec->timestamp_filename = true; 2352dc0c6127SJiri Olsa s->enabled = true; 23530c582449SJiri Olsa 23540c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 23550c582449SJiri Olsa switch_output_size_warn(rec); 23560c582449SJiri Olsa 2357dc0c6127SJiri Olsa return 0; 2358cb4e1ebbSJiri Olsa } 2359cb4e1ebbSJiri Olsa 2360e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 236186470930SIngo Molnar "perf record [<options>] [<command>]", 236286470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 236386470930SIngo Molnar NULL 236486470930SIngo Molnar }; 2365e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 236686470930SIngo Molnar 23676e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event, 23686e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 23696e0a9b3dSArnaldo Carvalho de Melo { 23706e0a9b3dSArnaldo Carvalho de Melo /* 23716e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 23726e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 23736e0a9b3dSArnaldo Carvalho de Melo */ 23746e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 23756e0a9b3dSArnaldo Carvalho de Melo return 0; 23766e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap(tool, event, sample, machine); 23776e0a9b3dSArnaldo Carvalho de Melo } 23786e0a9b3dSArnaldo Carvalho de Melo 23796e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event, 23806e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 23816e0a9b3dSArnaldo Carvalho de Melo { 23826e0a9b3dSArnaldo Carvalho de Melo /* 23836e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 23846e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 23856e0a9b3dSArnaldo Carvalho de Melo */ 23866e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 23876e0a9b3dSArnaldo Carvalho de Melo return 0; 23886e0a9b3dSArnaldo Carvalho de Melo 23896e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap2(tool, event, sample, machine); 23906e0a9b3dSArnaldo Carvalho de Melo } 23916e0a9b3dSArnaldo Carvalho de Melo 239266286ed3SAdrian Hunter static int process_timestamp_boundary(struct perf_tool *tool, 239366286ed3SAdrian Hunter union perf_event *event __maybe_unused, 239466286ed3SAdrian Hunter struct perf_sample *sample, 239566286ed3SAdrian Hunter struct machine *machine __maybe_unused) 239666286ed3SAdrian Hunter { 239766286ed3SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 239866286ed3SAdrian Hunter 239966286ed3SAdrian Hunter set_timestamp_boundary(rec, sample->time); 240066286ed3SAdrian Hunter return 0; 240166286ed3SAdrian Hunter } 240266286ed3SAdrian Hunter 240341b740b6SNamhyung Kim static int parse_record_synth_option(const struct option *opt, 240441b740b6SNamhyung Kim const char *str, 240541b740b6SNamhyung Kim int unset __maybe_unused) 240641b740b6SNamhyung Kim { 240741b740b6SNamhyung Kim struct record_opts *opts = opt->value; 240841b740b6SNamhyung Kim char *p = strdup(str); 240941b740b6SNamhyung Kim 241041b740b6SNamhyung Kim if (p == NULL) 241141b740b6SNamhyung Kim return -1; 241241b740b6SNamhyung Kim 241341b740b6SNamhyung Kim opts->synth = parse_synth_opt(p); 241441b740b6SNamhyung Kim free(p); 241541b740b6SNamhyung Kim 241641b740b6SNamhyung Kim if (opts->synth < 0) { 241741b740b6SNamhyung Kim pr_err("Invalid synth option: %s\n", str); 241841b740b6SNamhyung Kim return -1; 241941b740b6SNamhyung Kim } 242041b740b6SNamhyung Kim return 0; 242141b740b6SNamhyung Kim } 242241b740b6SNamhyung Kim 2423d20deb64SArnaldo Carvalho de Melo /* 24248c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 24258c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 2426d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 2427d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 2428d20deb64SArnaldo Carvalho de Melo * 2429d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 2430d20deb64SArnaldo Carvalho de Melo * 2431d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 2432d20deb64SArnaldo Carvalho de Melo */ 24338c6f45a7SArnaldo Carvalho de Melo static struct record record = { 2434d20deb64SArnaldo Carvalho de Melo .opts = { 24358affc2b8SAndi Kleen .sample_time = true, 2436d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 2437d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 2438d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 2439447a6013SArnaldo Carvalho de Melo .freq = 4000, 2440d1cb9fceSNamhyung Kim .target = { 2441d1cb9fceSNamhyung Kim .uses_mmap = true, 24423aa5939dSAdrian Hunter .default_per_cpu = true, 2443d1cb9fceSNamhyung Kim }, 2444470530bbSAlexey Budankov .mmap_flush = MMAP_FLUSH_DEFAULT, 2445d99c22eaSStephane Eranian .nr_threads_synthesize = 1, 24461d078ccbSAlexey Budankov .ctl_fd = -1, 24471d078ccbSAlexey Budankov .ctl_fd_ack = -1, 244841b740b6SNamhyung Kim .synth = PERF_SYNTH_ALL, 2449d20deb64SArnaldo Carvalho de Melo }, 2450e3d59112SNamhyung Kim .tool = { 2451e3d59112SNamhyung Kim .sample = process_sample_event, 2452e3d59112SNamhyung Kim .fork = perf_event__process_fork, 2453cca8482cSAdrian Hunter .exit = perf_event__process_exit, 2454e3d59112SNamhyung Kim .comm = perf_event__process_comm, 2455f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 24566e0a9b3dSArnaldo Carvalho de Melo .mmap = build_id__process_mmap, 24576e0a9b3dSArnaldo Carvalho de Melo .mmap2 = build_id__process_mmap2, 245866286ed3SAdrian Hunter .itrace_start = process_timestamp_boundary, 245966286ed3SAdrian Hunter .aux = process_timestamp_boundary, 2460cca8482cSAdrian Hunter .ordered_events = true, 2461e3d59112SNamhyung Kim }, 2462d20deb64SArnaldo Carvalho de Melo }; 24637865e817SFrederic Weisbecker 246476a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 246576a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 246661eaa3beSArnaldo Carvalho de Melo 24670aab2136SWang Nan static bool dry_run; 24680aab2136SWang Nan 2469d20deb64SArnaldo Carvalho de Melo /* 2470d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 2471d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 2472b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 24737b392ef0SArnaldo Carvalho de Melo * evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 2474d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 2475d20deb64SArnaldo Carvalho de Melo */ 2476efd21307SJiri Olsa static struct option __record_options[] = { 2477d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 247886470930SIngo Molnar "event selector. use 'perf list' to list available events", 2479f120f9d5SJiri Olsa parse_events_option), 2480d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 2481c171b552SLi Zefan "event filter", parse_filter), 24824ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 24834ba1faa1SWang Nan NULL, "don't record events from perf itself", 24844ba1faa1SWang Nan exclude_perf), 2485bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 2486d6d901c2SZhang, Yanmin "record events on existing process id"), 2487bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 2488d6d901c2SZhang, Yanmin "record events on existing thread id"), 2489d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 249086470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 2491509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 2492acac03faSKirill Smelkov "collect data without buffering"), 2493d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 2494daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 2495bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 249686470930SIngo Molnar "system-wide collection from all CPUs"), 2497bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 2498c45c6ea2SStephane Eranian "list of cpus to monitor"), 2499d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 25002d4f2799SJiri Olsa OPT_STRING('o', "output", &record.data.path, "file", 250186470930SIngo Molnar "output file name"), 250269e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 250369e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 25042e6cdf99SStephane Eranian "child tasks do not inherit counters"), 25054ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 25064ea648aeSWang Nan "synthesize non-sample events at the end of output"), 2507626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 2508a060c1f1SWei Li OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "do not record bpf events"), 2509b09c2364SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq, 2510b09c2364SArnaldo Carvalho de Melo "Fail if the specified frequency can't be used"), 251167230479SArnaldo Carvalho de Melo OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'", 251267230479SArnaldo Carvalho de Melo "profile at this frequency", 251367230479SArnaldo Carvalho de Melo record__parse_freq), 2514e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 2515e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 2516e9db1310SAdrian Hunter record__parse_mmap_pages), 2517470530bbSAlexey Budankov OPT_CALLBACK(0, "mmap-flush", &record.opts, "number", 2518470530bbSAlexey Budankov "Minimal number of bytes that is extracted from mmap data pages (default: 1)", 2519470530bbSAlexey Budankov record__mmap_flush_parse), 2520d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "group", &record.opts.group, 252143bece79SLin Ming "put the counters into a counter group"), 25222ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 252309b0fd45SJiri Olsa NULL, "enables call-graph recording" , 252409b0fd45SJiri Olsa &record_callchain_opt), 252509b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 252676a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 252709b0fd45SJiri Olsa &record_parse_callchain_opt), 2528c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 25293da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 2530b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 2531d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 2532649c48a9SPeter Zijlstra "per thread counts"), 253356100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 25343b0a5daaSKan Liang OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, 25353b0a5daaSKan Liang "Record the sample physical addresses"), 2536542b88fdSKan Liang OPT_BOOLEAN(0, "data-page-size", &record.opts.sample_data_page_size, 2537542b88fdSKan Liang "Record the sampled data address data page size"), 2538c1de7f3dSKan Liang OPT_BOOLEAN(0, "code-page-size", &record.opts.sample_code_page_size, 2539c1de7f3dSKan Liang "Record the sampled code address (ip) page size"), 2540b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 25413abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 25423abebc55SAdrian Hunter &record.opts.sample_time_set, 25433abebc55SAdrian Hunter "Record the sample timestamps"), 2544f290aa1fSJiri Olsa OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set, 2545f290aa1fSJiri Olsa "Record the sample period"), 2546d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 2547649c48a9SPeter Zijlstra "don't sample"), 2548d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 2549d2db9a98SWang Nan &record.no_buildid_cache_set, 2550a1ac1d3cSStephane Eranian "do not update the buildid cache"), 2551d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 2552d2db9a98SWang Nan &record.no_buildid_set, 2553baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 2554d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 2555023695d9SStephane Eranian "monitor event in cgroup name only", 2556023695d9SStephane Eranian parse_cgroups), 255768cd3b45SAlexey Budankov OPT_INTEGER('D', "delay", &record.opts.initial_delay, 255868cd3b45SAlexey Budankov "ms to wait before starting measurement after program start (-1: start with events disabled)"), 2559eeb399b5SAdrian Hunter OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"), 2560bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 2561bea03405SNamhyung Kim "user to profile"), 2562a5aabdacSStephane Eranian 2563a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 2564a5aabdacSStephane Eranian "branch any", "sample any taken branches", 2565a5aabdacSStephane Eranian parse_branch_stack), 2566a5aabdacSStephane Eranian 2567a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 2568a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 2569bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 257005484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 257105484298SAndi Kleen "sample by weight (on special events only)"), 2572475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 2573475eeab9SAndi Kleen "sample transaction flags (special events only)"), 25743aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 25753aa5939dSAdrian Hunter "use per-thread mmaps"), 2576bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 2577bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 2578aeea9062SKan Liang " use '-I?' to list register names", parse_intr_regs), 257984c41742SAndi Kleen OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register", 258084c41742SAndi Kleen "sample selected machine registers on interrupt," 2581aeea9062SKan Liang " use '--user-regs=?' to list register names", parse_user_regs), 258285c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 258385c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 2584814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 2585814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 2586814c8c38SPeter Zijlstra parse_clockid), 25872dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 25882dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 2589c0a6de06SAdrian Hunter OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts, 2590c0a6de06SAdrian Hunter "opts", "sample AUX area", ""), 25913fcb10e4SMark Drayton OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 25929d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 2593f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 2594f3b3614aSHari Bathini "Record namespaces events"), 25958fb4b679SNamhyung Kim OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup, 25968fb4b679SNamhyung Kim "Record cgroup events"), 259716b4b4e1SAdrian Hunter OPT_BOOLEAN_SET(0, "switch-events", &record.opts.record_switch_events, 259816b4b4e1SAdrian Hunter &record.opts.record_switch_events_set, 2599b757bb09SAdrian Hunter "Record context switch events"), 260085723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 260185723885SJiri Olsa "Configure all used events to run in kernel space.", 260285723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 260385723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 260485723885SJiri Olsa "Configure all used events to run in user space.", 260585723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 260653651b28Syuzhoujian OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains, 260753651b28Syuzhoujian "collect kernel callchains"), 260853651b28Syuzhoujian OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains, 260953651b28Syuzhoujian "collect user callchains"), 261071dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 261171dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 261271dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 261371dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 26147efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 26157efe0e03SHe Kuang "file", "vmlinux pathname"), 26166156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 26176156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 2618e29386c8SJiri Olsa OPT_BOOLEAN(0, "buildid-mmap", &record.buildid_mmap, 2619e29386c8SJiri Olsa "Record build-id in map events"), 2620ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 2621ecfd7a9cSWang Nan "append timestamp to output filename"), 262268588bafSJin Yao OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary, 262368588bafSJin Yao "Record timestamp boundary (time of first/last samples)"), 2624cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 2625c38dab7dSAndi Kleen &record.switch_output.set, "signal or size[BKMG] or time[smhd]", 2626c38dab7dSAndi Kleen "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold", 2627dc0c6127SJiri Olsa "signal"), 2628899e5ffbSArnaldo Carvalho de Melo OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event", 2629899e5ffbSArnaldo Carvalho de Melo "switch output event selector. use 'perf list' to list available events", 2630899e5ffbSArnaldo Carvalho de Melo parse_events_option_new_evlist), 263103724b2eSAndi Kleen OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files, 263203724b2eSAndi Kleen "Limit number of switch output generated files"), 26330aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 26340aab2136SWang Nan "Parse options then exit"), 2635d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 263693f20c0fSAlexey Budankov OPT_CALLBACK_OPTARG(0, "aio", &record.opts, 263793f20c0fSAlexey Budankov &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)", 2638d3d1af6fSAlexey Budankov record__aio_parse), 2639d3d1af6fSAlexey Budankov #endif 2640f4fe11b7SAlexey Budankov OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu", 2641f4fe11b7SAlexey Budankov "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer", 2642f4fe11b7SAlexey Budankov record__parse_affinity), 2643504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 2644504c1ad1SAlexey Budankov OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, 2645504c1ad1SAlexey Budankov "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)", 2646504c1ad1SAlexey Budankov record__parse_comp_level), 2647504c1ad1SAlexey Budankov #endif 26486d575816SJiwei Sun OPT_CALLBACK(0, "max-size", &record.output_max_size, 26496d575816SJiwei Sun "size", "Limit the maximum size of the output file", parse_output_max_size), 2650d99c22eaSStephane Eranian OPT_UINTEGER(0, "num-thread-synthesize", 2651d99c22eaSStephane Eranian &record.opts.nr_threads_synthesize, 2652d99c22eaSStephane Eranian "number of threads to run for event synthesis"), 265370943490SStephane Eranian #ifdef HAVE_LIBPFM 265470943490SStephane Eranian OPT_CALLBACK(0, "pfm-events", &record.evlist, "event", 265570943490SStephane Eranian "libpfm4 event selector. use 'perf list' to list available events", 265670943490SStephane Eranian parse_libpfm_events_option), 265770943490SStephane Eranian #endif 2658a8fcbd26SAdrian Hunter OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]", 2659d20aff15SAdrian Hunter "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events,\n" 2660d20aff15SAdrian Hunter "\t\t\t 'snapshot': AUX area tracing snapshot).\n" 2661a8fcbd26SAdrian Hunter "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n" 2662a8fcbd26SAdrian Hunter "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.", 26631d078ccbSAlexey Budankov parse_control_option), 266441b740b6SNamhyung Kim OPT_CALLBACK(0, "synth", &record.opts, "no|all|task|mmap|cgroup", 266541b740b6SNamhyung Kim "Fine-tune event synthesis: default=all", parse_record_synth_option), 266686470930SIngo Molnar OPT_END() 266786470930SIngo Molnar }; 266886470930SIngo Molnar 2669e5b2c207SNamhyung Kim struct option *record_options = __record_options; 2670e5b2c207SNamhyung Kim 2671b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 267286470930SIngo Molnar { 2673ef149c25SAdrian Hunter int err; 26748c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 267516ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 267686470930SIngo Molnar 267767230479SArnaldo Carvalho de Melo setlocale(LC_ALL, ""); 267867230479SArnaldo Carvalho de Melo 267948e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 268048e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 268148e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 268248e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 268348e1cab1SWang Nan # undef set_nobuild 268448e1cab1SWang Nan #endif 268548e1cab1SWang Nan 26867efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 26877efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 26887efe0e03SHe Kuang # define REASON "NO_DWARF=1" 26897efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 26907efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 26917efe0e03SHe Kuang # else 26927efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 26937efe0e03SHe Kuang # endif 26947efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 26957efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 26967efe0e03SHe Kuang # undef set_nobuild 26977efe0e03SHe Kuang # undef REASON 26987efe0e03SHe Kuang #endif 26997efe0e03SHe Kuang 27009d2ed645SAlexey Budankov rec->opts.affinity = PERF_AFFINITY_SYS; 27019d2ed645SAlexey Budankov 27020f98b11cSJiri Olsa rec->evlist = evlist__new(); 27033e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 2704361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 2705361c99a6SArnaldo Carvalho de Melo 2706ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 2707ecc4c561SArnaldo Carvalho de Melo if (err) 2708ecc4c561SArnaldo Carvalho de Melo return err; 2709eb853e80SJiri Olsa 2710bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 2711a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 271268ba3235SNamhyung Kim if (quiet) 271368ba3235SNamhyung Kim perf_quiet_option(); 2714483635a9SJiri Olsa 2715*7cc72553SJames Clark err = symbol__validate_sym_arguments(); 2716*7cc72553SJames Clark if (err) 2717*7cc72553SJames Clark return err; 2718*7cc72553SJames Clark 2719483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 2720602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 2721483635a9SJiri Olsa rec->opts.target.system_wide = true; 272286470930SIngo Molnar 2723bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 2724c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 2725c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 2726c7118369SNamhyung Kim 2727023695d9SStephane Eranian } 2728504c1ad1SAlexey Budankov 2729e29386c8SJiri Olsa if (rec->buildid_mmap) { 2730e29386c8SJiri Olsa if (!perf_can_record_build_id()) { 2731e29386c8SJiri Olsa pr_err("Failed: no support to record build id in mmap events, update your kernel.\n"); 2732e29386c8SJiri Olsa err = -EINVAL; 2733e29386c8SJiri Olsa goto out_opts; 2734e29386c8SJiri Olsa } 2735e29386c8SJiri Olsa pr_debug("Enabling build id in mmap2 events.\n"); 2736e29386c8SJiri Olsa /* Enable mmap build id synthesizing. */ 2737e29386c8SJiri Olsa symbol_conf.buildid_mmap2 = true; 2738e29386c8SJiri Olsa /* Enable perf_event_attr::build_id bit. */ 2739e29386c8SJiri Olsa rec->opts.build_id = true; 2740e29386c8SJiri Olsa /* Disable build id cache. */ 2741e29386c8SJiri Olsa rec->no_buildid = true; 2742e29386c8SJiri Olsa } 2743e29386c8SJiri Olsa 27444f2abe91SNamhyung Kim if (rec->opts.record_cgroup && !perf_can_record_cgroup()) { 27454f2abe91SNamhyung Kim pr_err("Kernel has no cgroup sampling support.\n"); 27464f2abe91SNamhyung Kim err = -EINVAL; 27474f2abe91SNamhyung Kim goto out_opts; 27484f2abe91SNamhyung Kim } 27494f2abe91SNamhyung Kim 2750eeb399b5SAdrian Hunter if (rec->opts.kcore) 2751eeb399b5SAdrian Hunter rec->data.is_dir = true; 2752eeb399b5SAdrian Hunter 2753504c1ad1SAlexey Budankov if (rec->opts.comp_level != 0) { 2754504c1ad1SAlexey Budankov pr_debug("Compression enabled, disabling build id collection at the end of the session.\n"); 2755504c1ad1SAlexey Budankov rec->no_buildid = true; 2756504c1ad1SAlexey Budankov } 2757504c1ad1SAlexey Budankov 2758b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 2759b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 2760c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 2761c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 2762a8fcbd26SAdrian Hunter err = -EINVAL; 2763a8fcbd26SAdrian Hunter goto out_opts; 2764b757bb09SAdrian Hunter } 2765023695d9SStephane Eranian 2766cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 2767cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 2768a8fcbd26SAdrian Hunter err = -EINVAL; 2769a8fcbd26SAdrian Hunter goto out_opts; 2770cb4e1ebbSJiri Olsa } 2771cb4e1ebbSJiri Olsa 2772bfacbe3bSJiri Olsa if (rec->switch_output.time) { 2773bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 2774bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 2775bfacbe3bSJiri Olsa } 2776bfacbe3bSJiri Olsa 277703724b2eSAndi Kleen if (rec->switch_output.num_files) { 277803724b2eSAndi Kleen rec->switch_output.filenames = calloc(sizeof(char *), 277903724b2eSAndi Kleen rec->switch_output.num_files); 2780a8fcbd26SAdrian Hunter if (!rec->switch_output.filenames) { 2781a8fcbd26SAdrian Hunter err = -EINVAL; 2782a8fcbd26SAdrian Hunter goto out_opts; 2783a8fcbd26SAdrian Hunter } 278403724b2eSAndi Kleen } 278503724b2eSAndi Kleen 27861b36c03eSAdrian Hunter /* 27871b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 27881b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 27891b36c03eSAdrian Hunter */ 27901b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 27911b36c03eSAdrian Hunter 27921b36c03eSAdrian Hunter symbol__init(NULL); 27931b36c03eSAdrian Hunter 27948384a260SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS) { 27958384a260SAlexey Budankov rec->affinity_mask.nbits = cpu__max_cpu(); 27967fc5b571SAndy Shevchenko rec->affinity_mask.bits = bitmap_zalloc(rec->affinity_mask.nbits); 27978384a260SAlexey Budankov if (!rec->affinity_mask.bits) { 27988384a260SAlexey Budankov pr_err("Failed to allocate thread mask for %zd cpus\n", rec->affinity_mask.nbits); 2799a8fcbd26SAdrian Hunter err = -ENOMEM; 2800a8fcbd26SAdrian Hunter goto out_opts; 28018384a260SAlexey Budankov } 28028384a260SAlexey Budankov pr_debug2("thread mask[%zd]: empty\n", rec->affinity_mask.nbits); 28038384a260SAlexey Budankov } 28048384a260SAlexey Budankov 28054b5ea3bdSAdrian Hunter err = record__auxtrace_init(rec); 28061b36c03eSAdrian Hunter if (err) 28071b36c03eSAdrian Hunter goto out; 28081b36c03eSAdrian Hunter 28090aab2136SWang Nan if (dry_run) 28105c01ad60SAdrian Hunter goto out; 28110aab2136SWang Nan 2812d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 2813d7888573SWang Nan if (err) { 2814d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 2815d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 2816d7888573SWang Nan errbuf); 28175c01ad60SAdrian Hunter goto out; 2818d7888573SWang Nan } 2819d7888573SWang Nan 2820ef149c25SAdrian Hunter err = -ENOMEM; 2821ef149c25SAdrian Hunter 28220c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 2823a1ac1d3cSStephane Eranian disable_buildid_cache(); 2824dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 28250c1d46a8SWang Nan /* 28260c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 28270c1d46a8SWang Nan * generation by default to reduce data file switching 28280c1d46a8SWang Nan * overhead. Still generate buildid if they are required 28290c1d46a8SWang Nan * explicitly using 28300c1d46a8SWang Nan * 283160437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 28320c1d46a8SWang Nan * --no-no-buildid-cache 28330c1d46a8SWang Nan * 28340c1d46a8SWang Nan * Following code equals to: 28350c1d46a8SWang Nan * 28360c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 28370c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 28380c1d46a8SWang Nan * disable_buildid_cache(); 28390c1d46a8SWang Nan */ 28400c1d46a8SWang Nan bool disable = true; 28410c1d46a8SWang Nan 28420c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 28430c1d46a8SWang Nan disable = false; 28440c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 28450c1d46a8SWang Nan disable = false; 28460c1d46a8SWang Nan if (disable) { 28470c1d46a8SWang Nan rec->no_buildid = true; 28480c1d46a8SWang Nan rec->no_buildid_cache = true; 28490c1d46a8SWang Nan disable_buildid_cache(); 28500c1d46a8SWang Nan } 28510c1d46a8SWang Nan } 2852655000e7SArnaldo Carvalho de Melo 28534ea648aeSWang Nan if (record.opts.overwrite) 28544ea648aeSWang Nan record.opts.tail_synthesize = true; 28554ea648aeSWang Nan 2856b53a0755SJin Yao if (rec->evlist->core.nr_entries == 0) { 2857b53a0755SJin Yao if (perf_pmu__has_hybrid()) { 2858b53a0755SJin Yao err = evlist__add_default_hybrid(rec->evlist, 2859b53a0755SJin Yao !record.opts.no_samples); 2860b53a0755SJin Yao } else { 2861b53a0755SJin Yao err = __evlist__add_default(rec->evlist, 2862b53a0755SJin Yao !record.opts.no_samples); 2863b53a0755SJin Yao } 2864b53a0755SJin Yao 2865b53a0755SJin Yao if (err < 0) { 286669aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 2867394c01edSAdrian Hunter goto out; 2868bbd36e5eSPeter Zijlstra } 2869b53a0755SJin Yao } 287086470930SIngo Molnar 287169e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 287269e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 287369e7e5b0SAdrian Hunter 2874602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 287516ad2ffbSNamhyung Kim if (err) { 2876602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 2877c3dec27bSJiri Olsa ui__warning("%s\n", errbuf); 287816ad2ffbSNamhyung Kim } 28794bd0f2d2SNamhyung Kim 2880602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 288116ad2ffbSNamhyung Kim if (err) { 288216ad2ffbSNamhyung Kim int saved_errno = errno; 288316ad2ffbSNamhyung Kim 2884602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 28853780f488SNamhyung Kim ui__error("%s", errbuf); 288616ad2ffbSNamhyung Kim 288716ad2ffbSNamhyung Kim err = -saved_errno; 2888394c01edSAdrian Hunter goto out; 288916ad2ffbSNamhyung Kim } 28900d37aa34SArnaldo Carvalho de Melo 2891ca800068SMengting Zhang /* Enable ignoring missing threads when -u/-p option is defined. */ 2892ca800068SMengting Zhang rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; 289323dc4f15SJiri Olsa 28941d3351e6SJin Yao if (evlist__fix_hybrid_cpus(rec->evlist, rec->opts.target.cpu_list)) { 28951d3351e6SJin Yao pr_err("failed to use cpu list %s\n", 28961d3351e6SJin Yao rec->opts.target.cpu_list); 28971d3351e6SJin Yao goto out; 28981d3351e6SJin Yao } 28991d3351e6SJin Yao 29001d3351e6SJin Yao rec->opts.target.hybrid = perf_pmu__has_hybrid(); 290116ad2ffbSNamhyung Kim err = -ENOMEM; 29027748bb71SArnaldo Carvalho de Melo if (evlist__create_maps(rec->evlist, &rec->opts.target) < 0) 2903dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 290469aad6f1SArnaldo Carvalho de Melo 2905ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 2906ef149c25SAdrian Hunter if (err) 2907394c01edSAdrian Hunter goto out; 2908ef149c25SAdrian Hunter 29096156681bSNamhyung Kim /* 29106156681bSNamhyung Kim * We take all buildids when the file contains 29116156681bSNamhyung Kim * AUX area tracing data because we do not decode the 29126156681bSNamhyung Kim * trace because it would take too long. 29136156681bSNamhyung Kim */ 29146156681bSNamhyung Kim if (rec->opts.full_auxtrace) 29156156681bSNamhyung Kim rec->buildid_all = true; 29166156681bSNamhyung Kim 2917246eba8eSAdrian Hunter if (rec->opts.text_poke) { 2918246eba8eSAdrian Hunter err = record__config_text_poke(rec->evlist); 2919246eba8eSAdrian Hunter if (err) { 2920246eba8eSAdrian Hunter pr_err("record__config_text_poke failed, error %d\n", err); 2921246eba8eSAdrian Hunter goto out; 2922246eba8eSAdrian Hunter } 2923246eba8eSAdrian Hunter } 2924246eba8eSAdrian Hunter 2925b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 292639d17dacSArnaldo Carvalho de Melo err = -EINVAL; 2927394c01edSAdrian Hunter goto out; 29287e4ff9e3SMike Galbraith } 29297e4ff9e3SMike Galbraith 293093f20c0fSAlexey Budankov if (rec->opts.nr_cblocks > nr_cblocks_max) 293193f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_max; 29325d7f4116SAlexey Budankov pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks); 2933d3d1af6fSAlexey Budankov 29349d2ed645SAlexey Budankov pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]); 2935470530bbSAlexey Budankov pr_debug("mmap flush: %d\n", rec->opts.mmap_flush); 29369d2ed645SAlexey Budankov 293751255a8aSAlexey Budankov if (rec->opts.comp_level > comp_level_max) 293851255a8aSAlexey Budankov rec->opts.comp_level = comp_level_max; 293951255a8aSAlexey Budankov pr_debug("comp level: %d\n", rec->opts.comp_level); 294051255a8aSAlexey Budankov 2941d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 2942394c01edSAdrian Hunter out: 29438384a260SAlexey Budankov bitmap_free(rec->affinity_mask.bits); 2944c12995a5SJiri Olsa evlist__delete(rec->evlist); 2945d65a458bSArnaldo Carvalho de Melo symbol__exit(); 2946ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 2947a8fcbd26SAdrian Hunter out_opts: 2948ee7fe31eSAdrian Hunter evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close); 294939d17dacSArnaldo Carvalho de Melo return err; 295086470930SIngo Molnar } 29512dd6d8a1SAdrian Hunter 29522dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 29532dd6d8a1SAdrian Hunter { 2954dc0c6127SJiri Olsa struct record *rec = &record; 2955dc0c6127SJiri Olsa 2956d20aff15SAdrian Hunter hit_auxtrace_snapshot_trigger(rec); 29573c1cb7e3SWang Nan 2958dc0c6127SJiri Olsa if (switch_output_signal(rec)) 29593c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 29602dd6d8a1SAdrian Hunter } 2961bfacbe3bSJiri Olsa 2962bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 2963bfacbe3bSJiri Olsa { 2964bfacbe3bSJiri Olsa struct record *rec = &record; 2965bfacbe3bSJiri Olsa 2966bfacbe3bSJiri Olsa if (switch_output_time(rec)) 2967bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 2968bfacbe3bSJiri Olsa } 2969