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" 48d8871ea7SWang Nan #include "asm/bug.h" 49c1a604dfSArnaldo Carvalho de Melo #include "perf.h" 507c6a1c65SPeter Zijlstra 51a43783aeSArnaldo Carvalho de Melo #include <errno.h> 52fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 5367230479SArnaldo Carvalho de Melo #include <locale.h> 544208735dSArnaldo Carvalho de Melo #include <poll.h> 55d99c22eaSStephane Eranian #include <pthread.h> 5686470930SIngo Molnar #include <unistd.h> 5786470930SIngo Molnar #include <sched.h> 589607ad3aSArnaldo Carvalho de Melo #include <signal.h> 59a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 604208735dSArnaldo Carvalho de Melo #include <sys/wait.h> 61eeb399b5SAdrian Hunter #include <sys/types.h> 62eeb399b5SAdrian Hunter #include <sys/stat.h> 63eeb399b5SAdrian Hunter #include <fcntl.h> 646ef81c55SMamatha Inamdar #include <linux/err.h> 658520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 660693e680SArnaldo Carvalho de Melo #include <linux/time64.h> 67d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h> 688384a260SAlexey Budankov #include <linux/bitmap.h> 6978da39faSBernhard Rosenkraenzer 701b43b704SJiri Olsa struct switch_output { 71dc0c6127SJiri Olsa bool enabled; 721b43b704SJiri Olsa bool signal; 73dc0c6127SJiri Olsa unsigned long size; 74bfacbe3bSJiri Olsa unsigned long time; 75cb4e1ebbSJiri Olsa const char *str; 76cb4e1ebbSJiri Olsa bool set; 7703724b2eSAndi Kleen char **filenames; 7803724b2eSAndi Kleen int num_files; 7903724b2eSAndi Kleen int cur_file; 801b43b704SJiri Olsa }; 811b43b704SJiri Olsa 828c6f45a7SArnaldo Carvalho de Melo struct record { 8345694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 84b4006796SArnaldo Carvalho de Melo struct record_opts opts; 85d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 868ceb41d7SJiri Olsa struct perf_data data; 87ef149c25SAdrian Hunter struct auxtrace_record *itr; 8863503dbaSJiri Olsa struct evlist *evlist; 89d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 90bc477d79SArnaldo Carvalho de Melo struct evlist *sb_evlist; 91899e5ffbSArnaldo Carvalho de Melo pthread_t thread_id; 92d20deb64SArnaldo Carvalho de Melo int realtime_prio; 93899e5ffbSArnaldo Carvalho de Melo bool switch_output_event_set; 94d20deb64SArnaldo Carvalho de Melo bool no_buildid; 95d2db9a98SWang Nan bool no_buildid_set; 96d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 97d2db9a98SWang Nan bool no_buildid_cache_set; 986156681bSNamhyung Kim bool buildid_all; 99ecfd7a9cSWang Nan bool timestamp_filename; 10068588bafSJin Yao bool timestamp_boundary; 1011b43b704SJiri Olsa struct switch_output switch_output; 1029f065194SYang Shi unsigned long long samples; 1038384a260SAlexey Budankov struct mmap_cpu_mask affinity_mask; 1046d575816SJiwei Sun unsigned long output_max_size; /* = 0: unlimited */ 1050f82ebc4SArnaldo Carvalho de Melo }; 10686470930SIngo Molnar 1076d575816SJiwei Sun static volatile int done; 1086d575816SJiwei Sun 109dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 110dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 111dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 112dc0c6127SJiri Olsa 1139d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = { 1149d2ed645SAlexey Budankov "SYS", "NODE", "CPU" 1159d2ed645SAlexey Budankov }; 1169d2ed645SAlexey Budankov 117dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 118dc0c6127SJiri Olsa { 119dc0c6127SJiri Olsa return rec->switch_output.signal && 120dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 121dc0c6127SJiri Olsa } 122dc0c6127SJiri Olsa 123dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 124dc0c6127SJiri Olsa { 125dc0c6127SJiri Olsa return rec->switch_output.size && 126dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 127dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 128dc0c6127SJiri Olsa } 129dc0c6127SJiri Olsa 130bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 131bfacbe3bSJiri Olsa { 132bfacbe3bSJiri Olsa return rec->switch_output.time && 133bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 134bfacbe3bSJiri Olsa } 135bfacbe3bSJiri Olsa 1366d575816SJiwei Sun static bool record__output_max_size_exceeded(struct record *rec) 1376d575816SJiwei Sun { 1386d575816SJiwei Sun return rec->output_max_size && 1396d575816SJiwei Sun (rec->bytes_written >= rec->output_max_size); 1406d575816SJiwei Sun } 1416d575816SJiwei Sun 142a5830532SJiri Olsa static int record__write(struct record *rec, struct mmap *map __maybe_unused, 143ded2b8feSJiri Olsa void *bf, size_t size) 144f5970550SPeter Zijlstra { 145ded2b8feSJiri Olsa struct perf_data_file *file = &rec->session->data->file; 146ded2b8feSJiri Olsa 147ded2b8feSJiri Olsa if (perf_data_file__write(file, bf, size) < 0) { 1484f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 1498d3eca20SDavid Ahern return -1; 1508d3eca20SDavid Ahern } 151f5970550SPeter Zijlstra 152cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 153dc0c6127SJiri Olsa 1546d575816SJiwei Sun if (record__output_max_size_exceeded(rec) && !done) { 1556d575816SJiwei Sun fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB)," 1566d575816SJiwei Sun " stopping session ]\n", 1576d575816SJiwei Sun rec->bytes_written >> 10); 1586d575816SJiwei Sun done = 1; 1596d575816SJiwei Sun } 1606d575816SJiwei Sun 161dc0c6127SJiri Olsa if (switch_output_size(rec)) 162dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 163dc0c6127SJiri Olsa 1648d3eca20SDavid Ahern return 0; 165f5970550SPeter Zijlstra } 166f5970550SPeter Zijlstra 167ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec); 168ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec); 1695d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size, 1705d7f4116SAlexey Budankov void *src, size_t src_size); 1715d7f4116SAlexey Budankov 172d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 173d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd, 174d3d1af6fSAlexey Budankov void *buf, size_t size, off_t off) 175d3d1af6fSAlexey Budankov { 176d3d1af6fSAlexey Budankov int rc; 177d3d1af6fSAlexey Budankov 178d3d1af6fSAlexey Budankov cblock->aio_fildes = trace_fd; 179d3d1af6fSAlexey Budankov cblock->aio_buf = buf; 180d3d1af6fSAlexey Budankov cblock->aio_nbytes = size; 181d3d1af6fSAlexey Budankov cblock->aio_offset = off; 182d3d1af6fSAlexey Budankov cblock->aio_sigevent.sigev_notify = SIGEV_NONE; 183d3d1af6fSAlexey Budankov 184d3d1af6fSAlexey Budankov do { 185d3d1af6fSAlexey Budankov rc = aio_write(cblock); 186d3d1af6fSAlexey Budankov if (rc == 0) { 187d3d1af6fSAlexey Budankov break; 188d3d1af6fSAlexey Budankov } else if (errno != EAGAIN) { 189d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 190d3d1af6fSAlexey Budankov pr_err("failed to queue perf data, error: %m\n"); 191d3d1af6fSAlexey Budankov break; 192d3d1af6fSAlexey Budankov } 193d3d1af6fSAlexey Budankov } while (1); 194d3d1af6fSAlexey Budankov 195d3d1af6fSAlexey Budankov return rc; 196d3d1af6fSAlexey Budankov } 197d3d1af6fSAlexey Budankov 198a5830532SJiri Olsa static int record__aio_complete(struct mmap *md, struct aiocb *cblock) 199d3d1af6fSAlexey Budankov { 200d3d1af6fSAlexey Budankov void *rem_buf; 201d3d1af6fSAlexey Budankov off_t rem_off; 202d3d1af6fSAlexey Budankov size_t rem_size; 203d3d1af6fSAlexey Budankov int rc, aio_errno; 204d3d1af6fSAlexey Budankov ssize_t aio_ret, written; 205d3d1af6fSAlexey Budankov 206d3d1af6fSAlexey Budankov aio_errno = aio_error(cblock); 207d3d1af6fSAlexey Budankov if (aio_errno == EINPROGRESS) 208d3d1af6fSAlexey Budankov return 0; 209d3d1af6fSAlexey Budankov 210d3d1af6fSAlexey Budankov written = aio_ret = aio_return(cblock); 211d3d1af6fSAlexey Budankov if (aio_ret < 0) { 212d3d1af6fSAlexey Budankov if (aio_errno != EINTR) 213d3d1af6fSAlexey Budankov pr_err("failed to write perf data, error: %m\n"); 214d3d1af6fSAlexey Budankov written = 0; 215d3d1af6fSAlexey Budankov } 216d3d1af6fSAlexey Budankov 217d3d1af6fSAlexey Budankov rem_size = cblock->aio_nbytes - written; 218d3d1af6fSAlexey Budankov 219d3d1af6fSAlexey Budankov if (rem_size == 0) { 220d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 221d3d1af6fSAlexey Budankov /* 222ef781128SAlexey Budankov * md->refcount is incremented in record__aio_pushfn() for 223ef781128SAlexey Budankov * every aio write request started in record__aio_push() so 224ef781128SAlexey Budankov * decrement it because the request is now complete. 225d3d1af6fSAlexey Budankov */ 22680e53d11SJiri Olsa perf_mmap__put(&md->core); 227d3d1af6fSAlexey Budankov rc = 1; 228d3d1af6fSAlexey Budankov } else { 229d3d1af6fSAlexey Budankov /* 230d3d1af6fSAlexey Budankov * aio write request may require restart with the 231d3d1af6fSAlexey Budankov * reminder if the kernel didn't write whole 232d3d1af6fSAlexey Budankov * chunk at once. 233d3d1af6fSAlexey Budankov */ 234d3d1af6fSAlexey Budankov rem_off = cblock->aio_offset + written; 235d3d1af6fSAlexey Budankov rem_buf = (void *)(cblock->aio_buf + written); 236d3d1af6fSAlexey Budankov record__aio_write(cblock, cblock->aio_fildes, 237d3d1af6fSAlexey Budankov rem_buf, rem_size, rem_off); 238d3d1af6fSAlexey Budankov rc = 0; 239d3d1af6fSAlexey Budankov } 240d3d1af6fSAlexey Budankov 241d3d1af6fSAlexey Budankov return rc; 242d3d1af6fSAlexey Budankov } 243d3d1af6fSAlexey Budankov 244a5830532SJiri Olsa static int record__aio_sync(struct mmap *md, bool sync_all) 245d3d1af6fSAlexey Budankov { 24693f20c0fSAlexey Budankov struct aiocb **aiocb = md->aio.aiocb; 24793f20c0fSAlexey Budankov struct aiocb *cblocks = md->aio.cblocks; 248d3d1af6fSAlexey Budankov struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */ 24993f20c0fSAlexey Budankov int i, do_suspend; 250d3d1af6fSAlexey Budankov 251d3d1af6fSAlexey Budankov do { 25293f20c0fSAlexey Budankov do_suspend = 0; 25393f20c0fSAlexey Budankov for (i = 0; i < md->aio.nr_cblocks; ++i) { 25493f20c0fSAlexey Budankov if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) { 25593f20c0fSAlexey Budankov if (sync_all) 25693f20c0fSAlexey Budankov aiocb[i] = NULL; 25793f20c0fSAlexey Budankov else 25893f20c0fSAlexey Budankov return i; 25993f20c0fSAlexey Budankov } else { 26093f20c0fSAlexey Budankov /* 26193f20c0fSAlexey Budankov * Started aio write is not complete yet 26293f20c0fSAlexey Budankov * so it has to be waited before the 26393f20c0fSAlexey Budankov * next allocation. 26493f20c0fSAlexey Budankov */ 26593f20c0fSAlexey Budankov aiocb[i] = &cblocks[i]; 26693f20c0fSAlexey Budankov do_suspend = 1; 26793f20c0fSAlexey Budankov } 26893f20c0fSAlexey Budankov } 26993f20c0fSAlexey Budankov if (!do_suspend) 27093f20c0fSAlexey Budankov return -1; 271d3d1af6fSAlexey Budankov 27293f20c0fSAlexey Budankov while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) { 273d3d1af6fSAlexey Budankov if (!(errno == EAGAIN || errno == EINTR)) 274d3d1af6fSAlexey Budankov pr_err("failed to sync perf data, error: %m\n"); 275d3d1af6fSAlexey Budankov } 276d3d1af6fSAlexey Budankov } while (1); 277d3d1af6fSAlexey Budankov } 278d3d1af6fSAlexey Budankov 279ef781128SAlexey Budankov struct record_aio { 280ef781128SAlexey Budankov struct record *rec; 281ef781128SAlexey Budankov void *data; 282ef781128SAlexey Budankov size_t size; 283ef781128SAlexey Budankov }; 284ef781128SAlexey Budankov 285a5830532SJiri Olsa static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size) 286d3d1af6fSAlexey Budankov { 287ef781128SAlexey Budankov struct record_aio *aio = to; 288ef781128SAlexey Budankov 289ef781128SAlexey Budankov /* 290547740f7SJiri Olsa * map->core.base data pointed by buf is copied into free map->aio.data[] buffer 291ef781128SAlexey Budankov * to release space in the kernel buffer as fast as possible, calling 292ef781128SAlexey Budankov * perf_mmap__consume() from perf_mmap__push() function. 293ef781128SAlexey Budankov * 294ef781128SAlexey Budankov * That lets the kernel to proceed with storing more profiling data into 295ef781128SAlexey Budankov * the kernel buffer earlier than other per-cpu kernel buffers are handled. 296ef781128SAlexey Budankov * 297ef781128SAlexey Budankov * Coping can be done in two steps in case the chunk of profiling data 298ef781128SAlexey Budankov * crosses the upper bound of the kernel buffer. In this case we first move 299ef781128SAlexey Budankov * part of data from map->start till the upper bound and then the reminder 300ef781128SAlexey Budankov * from the beginning of the kernel buffer till the end of the data chunk. 301ef781128SAlexey Budankov */ 302ef781128SAlexey Budankov 303ef781128SAlexey Budankov if (record__comp_enabled(aio->rec)) { 304ef781128SAlexey Budankov size = zstd_compress(aio->rec->session, aio->data + aio->size, 305bf59b305SJiri Olsa mmap__mmap_len(map) - aio->size, 306ef781128SAlexey Budankov buf, size); 307ef781128SAlexey Budankov } else { 308ef781128SAlexey Budankov memcpy(aio->data + aio->size, buf, size); 309ef781128SAlexey Budankov } 310ef781128SAlexey Budankov 311ef781128SAlexey Budankov if (!aio->size) { 312ef781128SAlexey Budankov /* 313ef781128SAlexey Budankov * Increment map->refcount to guard map->aio.data[] buffer 314ef781128SAlexey Budankov * from premature deallocation because map object can be 315ef781128SAlexey Budankov * released earlier than aio write request started on 316ef781128SAlexey Budankov * map->aio.data[] buffer is complete. 317ef781128SAlexey Budankov * 318ef781128SAlexey Budankov * perf_mmap__put() is done at record__aio_complete() 319ef781128SAlexey Budankov * after started aio request completion or at record__aio_push() 320ef781128SAlexey Budankov * if the request failed to start. 321ef781128SAlexey Budankov */ 322e75710f0SJiri Olsa perf_mmap__get(&map->core); 323ef781128SAlexey Budankov } 324ef781128SAlexey Budankov 325ef781128SAlexey Budankov aio->size += size; 326ef781128SAlexey Budankov 327ef781128SAlexey Budankov return size; 328ef781128SAlexey Budankov } 329ef781128SAlexey Budankov 330a5830532SJiri Olsa static int record__aio_push(struct record *rec, struct mmap *map, off_t *off) 331ef781128SAlexey Budankov { 332ef781128SAlexey Budankov int ret, idx; 333ef781128SAlexey Budankov int trace_fd = rec->session->data->file.fd; 334ef781128SAlexey Budankov struct record_aio aio = { .rec = rec, .size = 0 }; 335ef781128SAlexey Budankov 336ef781128SAlexey Budankov /* 337ef781128SAlexey Budankov * Call record__aio_sync() to wait till map->aio.data[] buffer 338ef781128SAlexey Budankov * becomes available after previous aio write operation. 339ef781128SAlexey Budankov */ 340ef781128SAlexey Budankov 341ef781128SAlexey Budankov idx = record__aio_sync(map, false); 342ef781128SAlexey Budankov aio.data = map->aio.data[idx]; 343ef781128SAlexey Budankov ret = perf_mmap__push(map, &aio, record__aio_pushfn); 344ef781128SAlexey Budankov if (ret != 0) /* ret > 0 - no data, ret < 0 - error */ 345ef781128SAlexey Budankov return ret; 346d3d1af6fSAlexey Budankov 347d3d1af6fSAlexey Budankov rec->samples++; 348ef781128SAlexey Budankov ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off); 349d3d1af6fSAlexey Budankov if (!ret) { 350ef781128SAlexey Budankov *off += aio.size; 351ef781128SAlexey Budankov rec->bytes_written += aio.size; 352d3d1af6fSAlexey Budankov if (switch_output_size(rec)) 353d3d1af6fSAlexey Budankov trigger_hit(&switch_output_trigger); 354ef781128SAlexey Budankov } else { 355ef781128SAlexey Budankov /* 356ef781128SAlexey Budankov * Decrement map->refcount incremented in record__aio_pushfn() 357ef781128SAlexey Budankov * back if record__aio_write() operation failed to start, otherwise 358ef781128SAlexey Budankov * map->refcount is decremented in record__aio_complete() after 359ef781128SAlexey Budankov * aio write operation finishes successfully. 360ef781128SAlexey Budankov */ 36180e53d11SJiri Olsa perf_mmap__put(&map->core); 362d3d1af6fSAlexey Budankov } 363d3d1af6fSAlexey Budankov 364d3d1af6fSAlexey Budankov return ret; 365d3d1af6fSAlexey Budankov } 366d3d1af6fSAlexey Budankov 367d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd) 368d3d1af6fSAlexey Budankov { 369d3d1af6fSAlexey Budankov return lseek(trace_fd, 0, SEEK_CUR); 370d3d1af6fSAlexey Budankov } 371d3d1af6fSAlexey Budankov 372d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos) 373d3d1af6fSAlexey Budankov { 374d3d1af6fSAlexey Budankov lseek(trace_fd, pos, SEEK_SET); 375d3d1af6fSAlexey Budankov } 376d3d1af6fSAlexey Budankov 377d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec) 378d3d1af6fSAlexey Budankov { 379d3d1af6fSAlexey Budankov int i; 38063503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 381a5830532SJiri Olsa struct mmap *maps = evlist->mmap; 382d3d1af6fSAlexey Budankov 383ef781128SAlexey Budankov if (!record__aio_enabled(rec)) 384d3d1af6fSAlexey Budankov return; 385d3d1af6fSAlexey Budankov 386c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 387a5830532SJiri Olsa struct mmap *map = &maps[i]; 388d3d1af6fSAlexey Budankov 389547740f7SJiri Olsa if (map->core.base) 39093f20c0fSAlexey Budankov record__aio_sync(map, true); 391d3d1af6fSAlexey Budankov } 392d3d1af6fSAlexey Budankov } 393d3d1af6fSAlexey Budankov 394d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1; 39593f20c0fSAlexey Budankov static int nr_cblocks_max = 4; 396d3d1af6fSAlexey Budankov 397d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt, 39893f20c0fSAlexey Budankov const char *str, 399d3d1af6fSAlexey Budankov int unset) 400d3d1af6fSAlexey Budankov { 401d3d1af6fSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 402d3d1af6fSAlexey Budankov 40393f20c0fSAlexey Budankov if (unset) { 404d3d1af6fSAlexey Budankov opts->nr_cblocks = 0; 40593f20c0fSAlexey Budankov } else { 40693f20c0fSAlexey Budankov if (str) 40793f20c0fSAlexey Budankov opts->nr_cblocks = strtol(str, NULL, 0); 40893f20c0fSAlexey Budankov if (!opts->nr_cblocks) 409d3d1af6fSAlexey Budankov opts->nr_cblocks = nr_cblocks_default; 41093f20c0fSAlexey Budankov } 411d3d1af6fSAlexey Budankov 412d3d1af6fSAlexey Budankov return 0; 413d3d1af6fSAlexey Budankov } 414d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */ 41593f20c0fSAlexey Budankov static int nr_cblocks_max = 0; 41693f20c0fSAlexey Budankov 417a5830532SJiri Olsa static int record__aio_push(struct record *rec __maybe_unused, struct mmap *map __maybe_unused, 418ef781128SAlexey Budankov off_t *off __maybe_unused) 419d3d1af6fSAlexey Budankov { 420d3d1af6fSAlexey Budankov return -1; 421d3d1af6fSAlexey Budankov } 422d3d1af6fSAlexey Budankov 423d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused) 424d3d1af6fSAlexey Budankov { 425d3d1af6fSAlexey Budankov return -1; 426d3d1af6fSAlexey Budankov } 427d3d1af6fSAlexey Budankov 428d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused) 429d3d1af6fSAlexey Budankov { 430d3d1af6fSAlexey Budankov } 431d3d1af6fSAlexey Budankov 432d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused) 433d3d1af6fSAlexey Budankov { 434d3d1af6fSAlexey Budankov } 435d3d1af6fSAlexey Budankov #endif 436d3d1af6fSAlexey Budankov 437d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec) 438d3d1af6fSAlexey Budankov { 439d3d1af6fSAlexey Budankov return rec->opts.nr_cblocks > 0; 440d3d1af6fSAlexey Budankov } 441d3d1af6fSAlexey Budankov 442470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1 443470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt, 444470530bbSAlexey Budankov const char *str, 445470530bbSAlexey Budankov int unset) 446470530bbSAlexey Budankov { 447470530bbSAlexey Budankov int flush_max; 448470530bbSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 449470530bbSAlexey Budankov static struct parse_tag tags[] = { 450470530bbSAlexey Budankov { .tag = 'B', .mult = 1 }, 451470530bbSAlexey Budankov { .tag = 'K', .mult = 1 << 10 }, 452470530bbSAlexey Budankov { .tag = 'M', .mult = 1 << 20 }, 453470530bbSAlexey Budankov { .tag = 'G', .mult = 1 << 30 }, 454470530bbSAlexey Budankov { .tag = 0 }, 455470530bbSAlexey Budankov }; 456470530bbSAlexey Budankov 457470530bbSAlexey Budankov if (unset) 458470530bbSAlexey Budankov return 0; 459470530bbSAlexey Budankov 460470530bbSAlexey Budankov if (str) { 461470530bbSAlexey Budankov opts->mmap_flush = parse_tag_value(str, tags); 462470530bbSAlexey Budankov if (opts->mmap_flush == (int)-1) 463470530bbSAlexey Budankov opts->mmap_flush = strtol(str, NULL, 0); 464470530bbSAlexey Budankov } 465470530bbSAlexey Budankov 466470530bbSAlexey Budankov if (!opts->mmap_flush) 467470530bbSAlexey Budankov opts->mmap_flush = MMAP_FLUSH_DEFAULT; 468470530bbSAlexey Budankov 4699521b5f2SJiri Olsa flush_max = evlist__mmap_size(opts->mmap_pages); 470470530bbSAlexey Budankov flush_max /= 4; 471470530bbSAlexey Budankov if (opts->mmap_flush > flush_max) 472470530bbSAlexey Budankov opts->mmap_flush = flush_max; 473470530bbSAlexey Budankov 474470530bbSAlexey Budankov return 0; 475470530bbSAlexey Budankov } 476470530bbSAlexey Budankov 477504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 478504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1; 479504c1ad1SAlexey Budankov 480504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset) 481504c1ad1SAlexey Budankov { 482504c1ad1SAlexey Budankov struct record_opts *opts = opt->value; 483504c1ad1SAlexey Budankov 484504c1ad1SAlexey Budankov if (unset) { 485504c1ad1SAlexey Budankov opts->comp_level = 0; 486504c1ad1SAlexey Budankov } else { 487504c1ad1SAlexey Budankov if (str) 488504c1ad1SAlexey Budankov opts->comp_level = strtol(str, NULL, 0); 489504c1ad1SAlexey Budankov if (!opts->comp_level) 490504c1ad1SAlexey Budankov opts->comp_level = comp_level_default; 491504c1ad1SAlexey Budankov } 492504c1ad1SAlexey Budankov 493504c1ad1SAlexey Budankov return 0; 494504c1ad1SAlexey Budankov } 495504c1ad1SAlexey Budankov #endif 49651255a8aSAlexey Budankov static unsigned int comp_level_max = 22; 49751255a8aSAlexey Budankov 49842e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec) 49942e1fd80SAlexey Budankov { 50042e1fd80SAlexey Budankov return rec->opts.comp_level > 0; 50142e1fd80SAlexey Budankov } 50242e1fd80SAlexey Budankov 50345694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 504d20deb64SArnaldo Carvalho de Melo union perf_event *event, 5051d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 5061d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 507234fbbf5SArnaldo Carvalho de Melo { 5088c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 509ded2b8feSJiri Olsa return record__write(rec, NULL, event, event->header.size); 510234fbbf5SArnaldo Carvalho de Melo } 511234fbbf5SArnaldo Carvalho de Melo 512d99c22eaSStephane Eranian static int process_locked_synthesized_event(struct perf_tool *tool, 513d99c22eaSStephane Eranian union perf_event *event, 514d99c22eaSStephane Eranian struct perf_sample *sample __maybe_unused, 515d99c22eaSStephane Eranian struct machine *machine __maybe_unused) 516d99c22eaSStephane Eranian { 517d99c22eaSStephane Eranian static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER; 518d99c22eaSStephane Eranian int ret; 519d99c22eaSStephane Eranian 520d99c22eaSStephane Eranian pthread_mutex_lock(&synth_lock); 521d99c22eaSStephane Eranian ret = process_synthesized_event(tool, event, sample, machine); 522d99c22eaSStephane Eranian pthread_mutex_unlock(&synth_lock); 523d99c22eaSStephane Eranian return ret; 524d99c22eaSStephane Eranian } 525d99c22eaSStephane Eranian 526a5830532SJiri Olsa static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size) 527d37f1586SArnaldo Carvalho de Melo { 528d37f1586SArnaldo Carvalho de Melo struct record *rec = to; 529d37f1586SArnaldo Carvalho de Melo 5305d7f4116SAlexey Budankov if (record__comp_enabled(rec)) { 531bf59b305SJiri Olsa size = zstd_compress(rec->session, map->data, mmap__mmap_len(map), bf, size); 5325d7f4116SAlexey Budankov bf = map->data; 5335d7f4116SAlexey Budankov } 5345d7f4116SAlexey Budankov 535d37f1586SArnaldo Carvalho de Melo rec->samples++; 536ded2b8feSJiri Olsa return record__write(rec, map, bf, size); 537d37f1586SArnaldo Carvalho de Melo } 538d37f1586SArnaldo Carvalho de Melo 5392dd6d8a1SAdrian Hunter static volatile int signr = -1; 5402dd6d8a1SAdrian Hunter static volatile int child_finished; 541c0bdc1c4SWang Nan 5422dd6d8a1SAdrian Hunter static void sig_handler(int sig) 5432dd6d8a1SAdrian Hunter { 5442dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 5452dd6d8a1SAdrian Hunter child_finished = 1; 5462dd6d8a1SAdrian Hunter else 5472dd6d8a1SAdrian Hunter signr = sig; 5482dd6d8a1SAdrian Hunter 5492dd6d8a1SAdrian Hunter done = 1; 5502dd6d8a1SAdrian Hunter } 5512dd6d8a1SAdrian Hunter 552a074865eSWang Nan static void sigsegv_handler(int sig) 553a074865eSWang Nan { 554a074865eSWang Nan perf_hooks__recover(); 555a074865eSWang Nan sighandler_dump_stack(sig); 556a074865eSWang Nan } 557a074865eSWang Nan 5582dd6d8a1SAdrian Hunter static void record__sig_exit(void) 5592dd6d8a1SAdrian Hunter { 5602dd6d8a1SAdrian Hunter if (signr == -1) 5612dd6d8a1SAdrian Hunter return; 5622dd6d8a1SAdrian Hunter 5632dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 5642dd6d8a1SAdrian Hunter raise(signr); 5652dd6d8a1SAdrian Hunter } 5662dd6d8a1SAdrian Hunter 567e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 568e31f0d01SAdrian Hunter 569ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 570a5830532SJiri Olsa struct mmap *map, 571ef149c25SAdrian Hunter union perf_event *event, void *data1, 572ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 573ef149c25SAdrian Hunter { 574ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 5758ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 576ef149c25SAdrian Hunter size_t padding; 577ef149c25SAdrian Hunter u8 pad[8] = {0}; 578ef149c25SAdrian Hunter 57946e201efSAdrian Hunter if (!perf_data__is_pipe(data) && perf_data__is_single_file(data)) { 58099fa2984SAdrian Hunter off_t file_offset; 5818ceb41d7SJiri Olsa int fd = perf_data__fd(data); 58299fa2984SAdrian Hunter int err; 58399fa2984SAdrian Hunter 58499fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 58599fa2984SAdrian Hunter if (file_offset == -1) 58699fa2984SAdrian Hunter return -1; 58799fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 58899fa2984SAdrian Hunter event, file_offset); 58999fa2984SAdrian Hunter if (err) 59099fa2984SAdrian Hunter return err; 59199fa2984SAdrian Hunter } 59299fa2984SAdrian Hunter 593ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 594ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 595ef149c25SAdrian Hunter if (padding) 596ef149c25SAdrian Hunter padding = 8 - padding; 597ef149c25SAdrian Hunter 598ded2b8feSJiri Olsa record__write(rec, map, event, event->header.size); 599ded2b8feSJiri Olsa record__write(rec, map, data1, len1); 600ef149c25SAdrian Hunter if (len2) 601ded2b8feSJiri Olsa record__write(rec, map, data2, len2); 602ded2b8feSJiri Olsa record__write(rec, map, &pad, padding); 603ef149c25SAdrian Hunter 604ef149c25SAdrian Hunter return 0; 605ef149c25SAdrian Hunter } 606ef149c25SAdrian Hunter 607ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 608a5830532SJiri Olsa struct mmap *map) 609ef149c25SAdrian Hunter { 610ef149c25SAdrian Hunter int ret; 611ef149c25SAdrian Hunter 612e035f4caSJiri Olsa ret = auxtrace_mmap__read(map, rec->itr, &rec->tool, 613ef149c25SAdrian Hunter record__process_auxtrace); 614ef149c25SAdrian Hunter if (ret < 0) 615ef149c25SAdrian Hunter return ret; 616ef149c25SAdrian Hunter 617ef149c25SAdrian Hunter if (ret) 618ef149c25SAdrian Hunter rec->samples++; 619ef149c25SAdrian Hunter 620ef149c25SAdrian Hunter return 0; 621ef149c25SAdrian Hunter } 622ef149c25SAdrian Hunter 6232dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 624a5830532SJiri Olsa struct mmap *map) 6252dd6d8a1SAdrian Hunter { 6262dd6d8a1SAdrian Hunter int ret; 6272dd6d8a1SAdrian Hunter 628e035f4caSJiri Olsa ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool, 6292dd6d8a1SAdrian Hunter record__process_auxtrace, 6302dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 6312dd6d8a1SAdrian Hunter if (ret < 0) 6322dd6d8a1SAdrian Hunter return ret; 6332dd6d8a1SAdrian Hunter 6342dd6d8a1SAdrian Hunter if (ret) 6352dd6d8a1SAdrian Hunter rec->samples++; 6362dd6d8a1SAdrian Hunter 6372dd6d8a1SAdrian Hunter return 0; 6382dd6d8a1SAdrian Hunter } 6392dd6d8a1SAdrian Hunter 6402dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 6412dd6d8a1SAdrian Hunter { 6422dd6d8a1SAdrian Hunter int i; 6432dd6d8a1SAdrian Hunter int rc = 0; 6442dd6d8a1SAdrian Hunter 645c976ee11SJiri Olsa for (i = 0; i < rec->evlist->core.nr_mmaps; i++) { 646a5830532SJiri Olsa struct mmap *map = &rec->evlist->mmap[i]; 6472dd6d8a1SAdrian Hunter 648e035f4caSJiri Olsa if (!map->auxtrace_mmap.base) 6492dd6d8a1SAdrian Hunter continue; 6502dd6d8a1SAdrian Hunter 651e035f4caSJiri Olsa if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) { 6522dd6d8a1SAdrian Hunter rc = -1; 6532dd6d8a1SAdrian Hunter goto out; 6542dd6d8a1SAdrian Hunter } 6552dd6d8a1SAdrian Hunter } 6562dd6d8a1SAdrian Hunter out: 6572dd6d8a1SAdrian Hunter return rc; 6582dd6d8a1SAdrian Hunter } 6592dd6d8a1SAdrian Hunter 660ce7b0e42SAlexander Shishkin static void record__read_auxtrace_snapshot(struct record *rec, bool on_exit) 6612dd6d8a1SAdrian Hunter { 6622dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 6632dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 6645f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 6652dd6d8a1SAdrian Hunter } else { 666ce7b0e42SAlexander Shishkin if (auxtrace_record__snapshot_finish(rec->itr, on_exit)) 6675f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 6685f9cf599SWang Nan else 6695f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 6702dd6d8a1SAdrian Hunter } 6712dd6d8a1SAdrian Hunter } 6722dd6d8a1SAdrian Hunter 673ce7b0e42SAlexander Shishkin static int record__auxtrace_snapshot_exit(struct record *rec) 674ce7b0e42SAlexander Shishkin { 675ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 676ce7b0e42SAlexander Shishkin return 0; 677ce7b0e42SAlexander Shishkin 678ce7b0e42SAlexander Shishkin if (!auxtrace_record__snapshot_started && 679ce7b0e42SAlexander Shishkin auxtrace_record__snapshot_start(rec->itr)) 680ce7b0e42SAlexander Shishkin return -1; 681ce7b0e42SAlexander Shishkin 682ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, true); 683ce7b0e42SAlexander Shishkin if (trigger_is_error(&auxtrace_snapshot_trigger)) 684ce7b0e42SAlexander Shishkin return -1; 685ce7b0e42SAlexander Shishkin 686ce7b0e42SAlexander Shishkin return 0; 687ce7b0e42SAlexander Shishkin } 688ce7b0e42SAlexander Shishkin 6894b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec) 6904b5ea3bdSAdrian Hunter { 6914b5ea3bdSAdrian Hunter int err; 6924b5ea3bdSAdrian Hunter 6934b5ea3bdSAdrian Hunter if (!rec->itr) { 6944b5ea3bdSAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 6954b5ea3bdSAdrian Hunter if (err) 6964b5ea3bdSAdrian Hunter return err; 6974b5ea3bdSAdrian Hunter } 6984b5ea3bdSAdrian Hunter 6994b5ea3bdSAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 7004b5ea3bdSAdrian Hunter rec->opts.auxtrace_snapshot_opts); 7014b5ea3bdSAdrian Hunter if (err) 7024b5ea3bdSAdrian Hunter return err; 7034b5ea3bdSAdrian Hunter 704c0a6de06SAdrian Hunter err = auxtrace_parse_sample_options(rec->itr, rec->evlist, &rec->opts, 705c0a6de06SAdrian Hunter rec->opts.auxtrace_sample_opts); 706c0a6de06SAdrian Hunter if (err) 707c0a6de06SAdrian Hunter return err; 708c0a6de06SAdrian Hunter 7094b5ea3bdSAdrian Hunter return auxtrace_parse_filters(rec->evlist); 7104b5ea3bdSAdrian Hunter } 7114b5ea3bdSAdrian Hunter 712e31f0d01SAdrian Hunter #else 713e31f0d01SAdrian Hunter 714e31f0d01SAdrian Hunter static inline 715e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 716a5830532SJiri Olsa struct mmap *map __maybe_unused) 717e31f0d01SAdrian Hunter { 718e31f0d01SAdrian Hunter return 0; 719e31f0d01SAdrian Hunter } 720e31f0d01SAdrian Hunter 7212dd6d8a1SAdrian Hunter static inline 722ce7b0e42SAlexander Shishkin void record__read_auxtrace_snapshot(struct record *rec __maybe_unused, 723ce7b0e42SAlexander Shishkin bool on_exit __maybe_unused) 7242dd6d8a1SAdrian Hunter { 7252dd6d8a1SAdrian Hunter } 7262dd6d8a1SAdrian Hunter 7272dd6d8a1SAdrian Hunter static inline 7282dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 7292dd6d8a1SAdrian Hunter { 7302dd6d8a1SAdrian Hunter return 0; 7312dd6d8a1SAdrian Hunter } 7322dd6d8a1SAdrian Hunter 733ce7b0e42SAlexander Shishkin static inline 734ce7b0e42SAlexander Shishkin int record__auxtrace_snapshot_exit(struct record *rec __maybe_unused) 735ce7b0e42SAlexander Shishkin { 736ce7b0e42SAlexander Shishkin return 0; 737ce7b0e42SAlexander Shishkin } 738ce7b0e42SAlexander Shishkin 7394b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused) 7404b5ea3bdSAdrian Hunter { 7414b5ea3bdSAdrian Hunter return 0; 7424b5ea3bdSAdrian Hunter } 7434b5ea3bdSAdrian Hunter 744e31f0d01SAdrian Hunter #endif 745e31f0d01SAdrian Hunter 746eeb399b5SAdrian Hunter static bool record__kcore_readable(struct machine *machine) 747eeb399b5SAdrian Hunter { 748eeb399b5SAdrian Hunter char kcore[PATH_MAX]; 749eeb399b5SAdrian Hunter int fd; 750eeb399b5SAdrian Hunter 751eeb399b5SAdrian Hunter scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir); 752eeb399b5SAdrian Hunter 753eeb399b5SAdrian Hunter fd = open(kcore, O_RDONLY); 754eeb399b5SAdrian Hunter if (fd < 0) 755eeb399b5SAdrian Hunter return false; 756eeb399b5SAdrian Hunter 757eeb399b5SAdrian Hunter close(fd); 758eeb399b5SAdrian Hunter 759eeb399b5SAdrian Hunter return true; 760eeb399b5SAdrian Hunter } 761eeb399b5SAdrian Hunter 762eeb399b5SAdrian Hunter static int record__kcore_copy(struct machine *machine, struct perf_data *data) 763eeb399b5SAdrian Hunter { 764eeb399b5SAdrian Hunter char from_dir[PATH_MAX]; 765eeb399b5SAdrian Hunter char kcore_dir[PATH_MAX]; 766eeb399b5SAdrian Hunter int ret; 767eeb399b5SAdrian Hunter 768eeb399b5SAdrian Hunter snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir); 769eeb399b5SAdrian Hunter 770eeb399b5SAdrian Hunter ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir)); 771eeb399b5SAdrian Hunter if (ret) 772eeb399b5SAdrian Hunter return ret; 773eeb399b5SAdrian Hunter 774eeb399b5SAdrian Hunter return kcore_copy(from_dir, kcore_dir); 775eeb399b5SAdrian Hunter } 776eeb399b5SAdrian Hunter 777cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 77863503dbaSJiri Olsa struct evlist *evlist) 779cda57a8cSWang Nan { 780cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 781c0a6de06SAdrian Hunter bool auxtrace_overwrite = opts->auxtrace_snapshot_mode || 782c0a6de06SAdrian Hunter opts->auxtrace_sample_mode; 783cda57a8cSWang Nan char msg[512]; 784cda57a8cSWang Nan 785f13de660SAlexey Budankov if (opts->affinity != PERF_AFFINITY_SYS) 786f13de660SAlexey Budankov cpu__setup_cpunode_map(); 787f13de660SAlexey Budankov 7889521b5f2SJiri Olsa if (evlist__mmap_ex(evlist, opts->mmap_pages, 789cda57a8cSWang Nan opts->auxtrace_mmap_pages, 790c0a6de06SAdrian Hunter auxtrace_overwrite, 791470530bbSAlexey Budankov opts->nr_cblocks, opts->affinity, 79251255a8aSAlexey Budankov opts->mmap_flush, opts->comp_level) < 0) { 793cda57a8cSWang Nan if (errno == EPERM) { 794cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 795cda57a8cSWang Nan "Consider increasing " 796cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 797cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 798cda57a8cSWang Nan "(current value: %u,%u)\n", 799cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 800cda57a8cSWang Nan return -errno; 801cda57a8cSWang Nan } else { 802cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 803c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 804cda57a8cSWang Nan if (errno) 805cda57a8cSWang Nan return -errno; 806cda57a8cSWang Nan else 807cda57a8cSWang Nan return -EINVAL; 808cda57a8cSWang Nan } 809cda57a8cSWang Nan } 810cda57a8cSWang Nan return 0; 811cda57a8cSWang Nan } 812cda57a8cSWang Nan 813cda57a8cSWang Nan static int record__mmap(struct record *rec) 814cda57a8cSWang Nan { 815cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 816cda57a8cSWang Nan } 817cda57a8cSWang Nan 8188c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 819dd7927f4SArnaldo Carvalho de Melo { 820d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 82132dcd021SJiri Olsa struct evsel *pos; 82263503dbaSJiri Olsa struct evlist *evlist = rec->evlist; 823d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 824b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 8258d3eca20SDavid Ahern int rc = 0; 826dd7927f4SArnaldo Carvalho de Melo 827d3dbf43cSArnaldo Carvalho de Melo /* 828d3dbf43cSArnaldo Carvalho de Melo * For initial_delay we need to add a dummy event so that we can track 829d3dbf43cSArnaldo Carvalho de Melo * PERF_RECORD_MMAP while we wait for the initial delay to enable the 830d3dbf43cSArnaldo Carvalho de Melo * real events, the ones asked by the user. 831d3dbf43cSArnaldo Carvalho de Melo */ 832d3dbf43cSArnaldo Carvalho de Melo if (opts->initial_delay) { 833d3dbf43cSArnaldo Carvalho de Melo if (perf_evlist__add_dummy(evlist)) 834d3dbf43cSArnaldo Carvalho de Melo return -ENOMEM; 835d3dbf43cSArnaldo Carvalho de Melo 836515dbe48SJiri Olsa pos = evlist__first(evlist); 837d3dbf43cSArnaldo Carvalho de Melo pos->tracking = 0; 838515dbe48SJiri Olsa pos = evlist__last(evlist); 839d3dbf43cSArnaldo Carvalho de Melo pos->tracking = 1; 8401fc632ceSJiri Olsa pos->core.attr.enable_on_exec = 1; 841d3dbf43cSArnaldo Carvalho de Melo } 842d3dbf43cSArnaldo Carvalho de Melo 843e68ae9cfSArnaldo Carvalho de Melo perf_evlist__config(evlist, opts, &callchain_param); 844cac21425SJiri Olsa 845e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 8463da297a6SIngo Molnar try_again: 847af663bd0SJiri Olsa if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) { 848ae430892SArnaldo Carvalho de Melo if (evsel__fallback(pos, errno, msg, sizeof(msg))) { 849bb963e16SNamhyung Kim if (verbose > 0) 850c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 8513da297a6SIngo Molnar goto try_again; 8523da297a6SIngo Molnar } 853cf99ad14SAndi Kleen if ((errno == EINVAL || errno == EBADF) && 854cf99ad14SAndi Kleen pos->leader != pos && 855cf99ad14SAndi Kleen pos->weak_group) { 8564804e011SAndi Kleen pos = perf_evlist__reset_weak_group(evlist, pos, true); 857cf99ad14SAndi Kleen goto try_again; 858cf99ad14SAndi Kleen } 85956e52e85SArnaldo Carvalho de Melo rc = -errno; 860*2bb72dbbSArnaldo Carvalho de Melo evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg)); 86156e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 8628d3eca20SDavid Ahern goto out; 8637c6a1c65SPeter Zijlstra } 864bfd8f72cSAndi Kleen 865bfd8f72cSAndi Kleen pos->supported = true; 8667c6a1c65SPeter Zijlstra } 8677c6a1c65SPeter Zijlstra 868c8b567c8SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict && !perf_evlist__exclude_kernel(evlist)) { 869c8b567c8SArnaldo Carvalho de Melo pr_warning( 870c8b567c8SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 871c8b567c8SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n" 872c8b567c8SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 873c8b567c8SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 874c8b567c8SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 875c8b567c8SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 876c8b567c8SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 877c8b567c8SArnaldo Carvalho de Melo } 878c8b567c8SArnaldo Carvalho de Melo 87923d4aad4SArnaldo Carvalho de Melo if (perf_evlist__apply_filters(evlist, &pos)) { 88062d94b00SArnaldo Carvalho de Melo pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 8818ab2e96dSArnaldo Carvalho de Melo pos->filter, evsel__name(pos), errno, 882c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 8838d3eca20SDavid Ahern rc = -1; 8848d3eca20SDavid Ahern goto out; 8850a102479SFrederic Weisbecker } 8860a102479SFrederic Weisbecker 887cda57a8cSWang Nan rc = record__mmap(rec); 888cda57a8cSWang Nan if (rc) 8898d3eca20SDavid Ahern goto out; 8900a27d7f9SArnaldo Carvalho de Melo 891a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 8927b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 8938d3eca20SDavid Ahern out: 8948d3eca20SDavid Ahern return rc; 895a91e5431SArnaldo Carvalho de Melo } 896a91e5431SArnaldo Carvalho de Melo 897e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 898e3d59112SNamhyung Kim union perf_event *event, 899e3d59112SNamhyung Kim struct perf_sample *sample, 90032dcd021SJiri Olsa struct evsel *evsel, 901e3d59112SNamhyung Kim struct machine *machine) 902e3d59112SNamhyung Kim { 903e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 904e3d59112SNamhyung Kim 90568588bafSJin Yao if (rec->evlist->first_sample_time == 0) 90668588bafSJin Yao rec->evlist->first_sample_time = sample->time; 907e3d59112SNamhyung Kim 90868588bafSJin Yao rec->evlist->last_sample_time = sample->time; 90968588bafSJin Yao 91068588bafSJin Yao if (rec->buildid_all) 91168588bafSJin Yao return 0; 91268588bafSJin Yao 91368588bafSJin Yao rec->samples++; 914e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 915e3d59112SNamhyung Kim } 916e3d59112SNamhyung Kim 9178c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 9186122e4e4SArnaldo Carvalho de Melo { 919f5fc1412SJiri Olsa struct perf_session *session = rec->session; 9206122e4e4SArnaldo Carvalho de Melo 92145112e89SJiri Olsa if (perf_data__size(&rec->data) == 0) 9229f591fd7SArnaldo Carvalho de Melo return 0; 9239f591fd7SArnaldo Carvalho de Melo 92400dc8657SNamhyung Kim /* 92500dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 92600dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 92700dc8657SNamhyung Kim * we prefer the vmlinux path like 92800dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 92900dc8657SNamhyung Kim * 93000dc8657SNamhyung Kim * rather than build-id path (in debug directory). 93100dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 93200dc8657SNamhyung Kim */ 93300dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 93400dc8657SNamhyung Kim 9356156681bSNamhyung Kim /* 9366156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 93768588bafSJin Yao * so no need to process samples. But if timestamp_boundary is enabled, 93868588bafSJin Yao * it still needs to walk on all samples to get the timestamps of 93968588bafSJin Yao * first/last samples. 9406156681bSNamhyung Kim */ 94168588bafSJin Yao if (rec->buildid_all && !rec->timestamp_boundary) 9426156681bSNamhyung Kim rec->tool.sample = NULL; 9436156681bSNamhyung Kim 944b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 9456122e4e4SArnaldo Carvalho de Melo } 9466122e4e4SArnaldo Carvalho de Melo 9478115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 948a1645ce1SZhang, Yanmin { 949a1645ce1SZhang, Yanmin int err; 95045694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 951a1645ce1SZhang, Yanmin /* 952a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 953a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 954a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 955a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 956a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 957a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 958a1645ce1SZhang, Yanmin */ 95945694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 960743eb868SArnaldo Carvalho de Melo machine); 961a1645ce1SZhang, Yanmin if (err < 0) 962a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 96323346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 964a1645ce1SZhang, Yanmin 965a1645ce1SZhang, Yanmin /* 966a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 967a1645ce1SZhang, Yanmin * have no _text sometimes. 968a1645ce1SZhang, Yanmin */ 96945694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 9700ae617beSAdrian Hunter machine); 971a1645ce1SZhang, Yanmin if (err < 0) 972a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 97323346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 974a1645ce1SZhang, Yanmin } 975a1645ce1SZhang, Yanmin 97698402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 97798402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 97898402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 97998402807SFrederic Weisbecker }; 98098402807SFrederic Weisbecker 981a5830532SJiri Olsa static void record__adjust_affinity(struct record *rec, struct mmap *map) 982f13de660SAlexey Budankov { 983f13de660SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS && 9848384a260SAlexey Budankov !bitmap_equal(rec->affinity_mask.bits, map->affinity_mask.bits, 9858384a260SAlexey Budankov rec->affinity_mask.nbits)) { 9868384a260SAlexey Budankov bitmap_zero(rec->affinity_mask.bits, rec->affinity_mask.nbits); 9878384a260SAlexey Budankov bitmap_or(rec->affinity_mask.bits, rec->affinity_mask.bits, 9888384a260SAlexey Budankov map->affinity_mask.bits, rec->affinity_mask.nbits); 9898384a260SAlexey Budankov sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&rec->affinity_mask), 9908384a260SAlexey Budankov (cpu_set_t *)rec->affinity_mask.bits); 9918384a260SAlexey Budankov if (verbose == 2) 9928384a260SAlexey Budankov mmap_cpu_mask__scnprintf(&rec->affinity_mask, "thread"); 993f13de660SAlexey Budankov } 994f13de660SAlexey Budankov } 995f13de660SAlexey Budankov 9965d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment) 9975d7f4116SAlexey Budankov { 99872932371SJiri Olsa struct perf_record_compressed *event = record; 9995d7f4116SAlexey Budankov size_t size = sizeof(*event); 10005d7f4116SAlexey Budankov 10015d7f4116SAlexey Budankov if (increment) { 10025d7f4116SAlexey Budankov event->header.size += increment; 10035d7f4116SAlexey Budankov return increment; 10045d7f4116SAlexey Budankov } 10055d7f4116SAlexey Budankov 10065d7f4116SAlexey Budankov event->header.type = PERF_RECORD_COMPRESSED; 10075d7f4116SAlexey Budankov event->header.size = size; 10085d7f4116SAlexey Budankov 10095d7f4116SAlexey Budankov return size; 10105d7f4116SAlexey Budankov } 10115d7f4116SAlexey Budankov 10125d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size, 10135d7f4116SAlexey Budankov void *src, size_t src_size) 10145d7f4116SAlexey Budankov { 10155d7f4116SAlexey Budankov size_t compressed; 101672932371SJiri Olsa size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_record_compressed) - 1; 10175d7f4116SAlexey Budankov 10185d7f4116SAlexey Budankov compressed = zstd_compress_stream_to_records(&session->zstd_data, dst, dst_size, src, src_size, 10195d7f4116SAlexey Budankov max_record_size, process_comp_header); 10205d7f4116SAlexey Budankov 10215d7f4116SAlexey Budankov session->bytes_transferred += src_size; 10225d7f4116SAlexey Budankov session->bytes_compressed += compressed; 10235d7f4116SAlexey Budankov 10245d7f4116SAlexey Budankov return compressed; 10255d7f4116SAlexey Budankov } 10265d7f4116SAlexey Budankov 102763503dbaSJiri Olsa static int record__mmap_read_evlist(struct record *rec, struct evlist *evlist, 1028470530bbSAlexey Budankov bool overwrite, bool synch) 102998402807SFrederic Weisbecker { 1030dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 10310e2e63ddSPeter Zijlstra int i; 10328d3eca20SDavid Ahern int rc = 0; 1033a5830532SJiri Olsa struct mmap *maps; 1034d3d1af6fSAlexey Budankov int trace_fd = rec->data.file.fd; 1035ef781128SAlexey Budankov off_t off = 0; 103698402807SFrederic Weisbecker 1037cb21686bSWang Nan if (!evlist) 1038cb21686bSWang Nan return 0; 1039ef149c25SAdrian Hunter 10400b72d69aSWang Nan maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 1041a4ea0ec4SWang Nan if (!maps) 1042a4ea0ec4SWang Nan return 0; 1043cb21686bSWang Nan 10440b72d69aSWang Nan if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 104554cc54deSWang Nan return 0; 104654cc54deSWang Nan 1047d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1048d3d1af6fSAlexey Budankov off = record__aio_get_pos(trace_fd); 1049d3d1af6fSAlexey Budankov 1050c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 1051470530bbSAlexey Budankov u64 flush = 0; 1052a5830532SJiri Olsa struct mmap *map = &maps[i]; 1053a4ea0ec4SWang Nan 1054547740f7SJiri Olsa if (map->core.base) { 1055f13de660SAlexey Budankov record__adjust_affinity(rec, map); 1056470530bbSAlexey Budankov if (synch) { 105765aa2e6bSJiri Olsa flush = map->core.flush; 105865aa2e6bSJiri Olsa map->core.flush = 1; 1059470530bbSAlexey Budankov } 1060d3d1af6fSAlexey Budankov if (!record__aio_enabled(rec)) { 1061ef781128SAlexey Budankov if (perf_mmap__push(map, rec, record__pushfn) < 0) { 1062470530bbSAlexey Budankov if (synch) 106365aa2e6bSJiri Olsa map->core.flush = flush; 10648d3eca20SDavid Ahern rc = -1; 10658d3eca20SDavid Ahern goto out; 10668d3eca20SDavid Ahern } 1067d3d1af6fSAlexey Budankov } else { 1068ef781128SAlexey Budankov if (record__aio_push(rec, map, &off) < 0) { 1069d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1070470530bbSAlexey Budankov if (synch) 107165aa2e6bSJiri Olsa map->core.flush = flush; 1072d3d1af6fSAlexey Budankov rc = -1; 1073d3d1af6fSAlexey Budankov goto out; 1074d3d1af6fSAlexey Budankov } 1075d3d1af6fSAlexey Budankov } 1076470530bbSAlexey Budankov if (synch) 107765aa2e6bSJiri Olsa map->core.flush = flush; 10788d3eca20SDavid Ahern } 1079ef149c25SAdrian Hunter 1080e035f4caSJiri Olsa if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode && 1081c0a6de06SAdrian Hunter !rec->opts.auxtrace_sample_mode && 1082e035f4caSJiri Olsa record__auxtrace_mmap_read(rec, map) != 0) { 1083ef149c25SAdrian Hunter rc = -1; 1084ef149c25SAdrian Hunter goto out; 1085ef149c25SAdrian Hunter } 108698402807SFrederic Weisbecker } 108798402807SFrederic Weisbecker 1088d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 1089d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 1090d3d1af6fSAlexey Budankov 1091dcabb507SJiri Olsa /* 1092dcabb507SJiri Olsa * Mark the round finished in case we wrote 1093dcabb507SJiri Olsa * at least one event. 1094dcabb507SJiri Olsa */ 1095dcabb507SJiri Olsa if (bytes_written != rec->bytes_written) 1096ded2b8feSJiri Olsa rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event)); 10978d3eca20SDavid Ahern 10980b72d69aSWang Nan if (overwrite) 109954cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 11008d3eca20SDavid Ahern out: 11018d3eca20SDavid Ahern return rc; 110298402807SFrederic Weisbecker } 110398402807SFrederic Weisbecker 1104470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch) 1105cb21686bSWang Nan { 1106cb21686bSWang Nan int err; 1107cb21686bSWang Nan 1108470530bbSAlexey Budankov err = record__mmap_read_evlist(rec, rec->evlist, false, synch); 1109cb21686bSWang Nan if (err) 1110cb21686bSWang Nan return err; 1111cb21686bSWang Nan 1112470530bbSAlexey Budankov return record__mmap_read_evlist(rec, rec->evlist, true, synch); 1113cb21686bSWang Nan } 1114cb21686bSWang Nan 11158c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 111657706abcSDavid Ahern { 111757706abcSDavid Ahern struct perf_session *session = rec->session; 111857706abcSDavid Ahern int feat; 111957706abcSDavid Ahern 112057706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 112157706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 112257706abcSDavid Ahern 112357706abcSDavid Ahern if (rec->no_buildid) 112457706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 112557706abcSDavid Ahern 1126ce9036a6SJiri Olsa if (!have_tracepoints(&rec->evlist->core.entries)) 112757706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 112857706abcSDavid Ahern 112957706abcSDavid Ahern if (!rec->opts.branch_stack) 113057706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 1131ef149c25SAdrian Hunter 1132ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 1133ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 1134ffa517adSJiri Olsa 1135cf790516SAlexey Budankov if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns)) 1136cf790516SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_CLOCKID); 1137cf790516SAlexey Budankov 1138258031c0SJiri Olsa perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT); 113942e1fd80SAlexey Budankov if (!record__comp_enabled(rec)) 114042e1fd80SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_COMPRESSED); 1141258031c0SJiri Olsa 1142ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 114357706abcSDavid Ahern } 114457706abcSDavid Ahern 1145e1ab48baSWang Nan static void 1146e1ab48baSWang Nan record__finish_output(struct record *rec) 1147e1ab48baSWang Nan { 11488ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 11498ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1150e1ab48baSWang Nan 11518ceb41d7SJiri Olsa if (data->is_pipe) 1152e1ab48baSWang Nan return; 1153e1ab48baSWang Nan 1154e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 115545112e89SJiri Olsa data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR); 1156e1ab48baSWang Nan 1157e1ab48baSWang Nan if (!rec->no_buildid) { 1158e1ab48baSWang Nan process_buildids(rec); 1159e1ab48baSWang Nan 1160e1ab48baSWang Nan if (rec->buildid_all) 1161e1ab48baSWang Nan dsos__hit_all(rec->session); 1162e1ab48baSWang Nan } 1163e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 1164e1ab48baSWang Nan 1165e1ab48baSWang Nan return; 1166e1ab48baSWang Nan } 1167e1ab48baSWang Nan 11684ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 1169be7b0c9eSWang Nan { 11709d6aae72SArnaldo Carvalho de Melo int err; 11719749b90eSJiri Olsa struct perf_thread_map *thread_map; 1172be7b0c9eSWang Nan 11734ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 11744ea648aeSWang Nan return 0; 11754ea648aeSWang Nan 11769d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 11779d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 11789d6aae72SArnaldo Carvalho de Melo return -1; 11799d6aae72SArnaldo Carvalho de Melo 11809d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 1181be7b0c9eSWang Nan process_synthesized_event, 1182be7b0c9eSWang Nan &rec->session->machines.host, 11833fcb10e4SMark Drayton rec->opts.sample_address); 11847836e52eSJiri Olsa perf_thread_map__put(thread_map); 11859d6aae72SArnaldo Carvalho de Melo return err; 1186be7b0c9eSWang Nan } 1187be7b0c9eSWang Nan 11884ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 11893c1cb7e3SWang Nan 1190ecfd7a9cSWang Nan static int 1191ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 1192ecfd7a9cSWang Nan { 11938ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1194ecfd7a9cSWang Nan int fd, err; 119503724b2eSAndi Kleen char *new_filename; 1196ecfd7a9cSWang Nan 1197ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 1198ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 1199ecfd7a9cSWang Nan 1200d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1201d3d1af6fSAlexey Budankov 12024ea648aeSWang Nan record__synthesize(rec, true); 12034ea648aeSWang Nan if (target__none(&rec->opts.target)) 12044ea648aeSWang Nan record__synthesize_workload(rec, true); 12054ea648aeSWang Nan 1206ecfd7a9cSWang Nan rec->samples = 0; 1207ecfd7a9cSWang Nan record__finish_output(rec); 1208ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 1209ecfd7a9cSWang Nan if (err) { 1210ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 1211ecfd7a9cSWang Nan return -EINVAL; 1212ecfd7a9cSWang Nan } 1213ecfd7a9cSWang Nan 12148ceb41d7SJiri Olsa fd = perf_data__switch(data, timestamp, 1215ecfd7a9cSWang Nan rec->session->header.data_offset, 121603724b2eSAndi Kleen at_exit, &new_filename); 1217ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 1218ecfd7a9cSWang Nan rec->bytes_written = 0; 1219ecfd7a9cSWang Nan rec->session->header.data_size = 0; 1220ecfd7a9cSWang Nan } 1221ecfd7a9cSWang Nan 1222ecfd7a9cSWang Nan if (!quiet) 1223ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 12242d4f2799SJiri Olsa data->path, timestamp); 12253c1cb7e3SWang Nan 122603724b2eSAndi Kleen if (rec->switch_output.num_files) { 122703724b2eSAndi Kleen int n = rec->switch_output.cur_file + 1; 122803724b2eSAndi Kleen 122903724b2eSAndi Kleen if (n >= rec->switch_output.num_files) 123003724b2eSAndi Kleen n = 0; 123103724b2eSAndi Kleen rec->switch_output.cur_file = n; 123203724b2eSAndi Kleen if (rec->switch_output.filenames[n]) { 123303724b2eSAndi Kleen remove(rec->switch_output.filenames[n]); 1234d8f9da24SArnaldo Carvalho de Melo zfree(&rec->switch_output.filenames[n]); 123503724b2eSAndi Kleen } 123603724b2eSAndi Kleen rec->switch_output.filenames[n] = new_filename; 123703724b2eSAndi Kleen } else { 123803724b2eSAndi Kleen free(new_filename); 123903724b2eSAndi Kleen } 124003724b2eSAndi Kleen 12413c1cb7e3SWang Nan /* Output tracking events */ 1242be7b0c9eSWang Nan if (!at_exit) { 12434ea648aeSWang Nan record__synthesize(rec, false); 12443c1cb7e3SWang Nan 1245be7b0c9eSWang Nan /* 1246be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 1247be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 1248be7b0c9eSWang Nan * generate tracking events because there's no thread_map 1249be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 1250be7b0c9eSWang Nan * contain map and comm information. 1251be7b0c9eSWang Nan * Create a fake thread_map and directly call 1252be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 1253be7b0c9eSWang Nan */ 1254be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 12554ea648aeSWang Nan record__synthesize_workload(rec, false); 1256be7b0c9eSWang Nan } 1257ecfd7a9cSWang Nan return fd; 1258ecfd7a9cSWang Nan } 1259ecfd7a9cSWang Nan 1260f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno; 1261f33cbe72SArnaldo Carvalho de Melo 1262f33cbe72SArnaldo Carvalho de Melo /* 1263f33cbe72SArnaldo Carvalho de Melo * perf_evlist__prepare_workload will send a SIGUSR1 1264f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 1265f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 1266f33cbe72SArnaldo Carvalho de Melo */ 126745604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 126845604710SNamhyung Kim siginfo_t *info, 1269f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 1270f33cbe72SArnaldo Carvalho de Melo { 1271f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 1272f33cbe72SArnaldo Carvalho de Melo done = 1; 1273f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 1274f33cbe72SArnaldo Carvalho de Melo } 1275f33cbe72SArnaldo Carvalho de Melo 12762dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 1277bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 12782dd6d8a1SAdrian Hunter 1279ee667f94SWang Nan static const struct perf_event_mmap_page * 128063503dbaSJiri Olsa perf_evlist__pick_pc(struct evlist *evlist) 1281ee667f94SWang Nan { 1282b2cb615dSWang Nan if (evlist) { 1283547740f7SJiri Olsa if (evlist->mmap && evlist->mmap[0].core.base) 1284547740f7SJiri Olsa return evlist->mmap[0].core.base; 1285547740f7SJiri Olsa if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].core.base) 1286547740f7SJiri Olsa return evlist->overwrite_mmap[0].core.base; 1287b2cb615dSWang Nan } 1288ee667f94SWang Nan return NULL; 1289ee667f94SWang Nan } 1290ee667f94SWang Nan 1291c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 1292c45628b0SWang Nan { 1293ee667f94SWang Nan const struct perf_event_mmap_page *pc; 1294ee667f94SWang Nan 1295ee667f94SWang Nan pc = perf_evlist__pick_pc(rec->evlist); 1296ee667f94SWang Nan if (pc) 1297ee667f94SWang Nan return pc; 1298c45628b0SWang Nan return NULL; 1299c45628b0SWang Nan } 1300c45628b0SWang Nan 13014ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 1302c45c86ebSWang Nan { 1303c45c86ebSWang Nan struct perf_session *session = rec->session; 1304c45c86ebSWang Nan struct machine *machine = &session->machines.host; 13058ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1306c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 1307c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 13088ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1309c45c86ebSWang Nan int err = 0; 1310d99c22eaSStephane Eranian event_op f = process_synthesized_event; 1311c45c86ebSWang Nan 13124ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 13134ea648aeSWang Nan return 0; 13144ea648aeSWang Nan 13158ceb41d7SJiri Olsa if (data->is_pipe) { 1316a2015516SJiri Olsa /* 1317a2015516SJiri Olsa * We need to synthesize events first, because some 1318a2015516SJiri Olsa * features works on top of them (on report side). 1319a2015516SJiri Olsa */ 1320318ec184SJiri Olsa err = perf_event__synthesize_attrs(tool, rec->evlist, 1321c45c86ebSWang Nan process_synthesized_event); 1322c45c86ebSWang Nan if (err < 0) { 1323c45c86ebSWang Nan pr_err("Couldn't synthesize attrs.\n"); 1324c45c86ebSWang Nan goto out; 1325c45c86ebSWang Nan } 1326c45c86ebSWang Nan 1327a2015516SJiri Olsa err = perf_event__synthesize_features(tool, session, rec->evlist, 1328a2015516SJiri Olsa process_synthesized_event); 1329a2015516SJiri Olsa if (err < 0) { 1330a2015516SJiri Olsa pr_err("Couldn't synthesize features.\n"); 1331a2015516SJiri Olsa return err; 1332a2015516SJiri Olsa } 1333a2015516SJiri Olsa 1334ce9036a6SJiri Olsa if (have_tracepoints(&rec->evlist->core.entries)) { 1335c45c86ebSWang Nan /* 1336c45c86ebSWang Nan * FIXME err <= 0 here actually means that 1337c45c86ebSWang Nan * there were no tracepoints so its not really 1338c45c86ebSWang Nan * an error, just that we don't need to 1339c45c86ebSWang Nan * synthesize anything. We really have to 1340c45c86ebSWang Nan * return this more properly and also 1341c45c86ebSWang Nan * propagate errors that now are calling die() 1342c45c86ebSWang Nan */ 1343c45c86ebSWang Nan err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, 1344c45c86ebSWang Nan process_synthesized_event); 1345c45c86ebSWang Nan if (err <= 0) { 1346c45c86ebSWang Nan pr_err("Couldn't record tracing data.\n"); 1347c45c86ebSWang Nan goto out; 1348c45c86ebSWang Nan } 1349c45c86ebSWang Nan rec->bytes_written += err; 1350c45c86ebSWang Nan } 1351c45c86ebSWang Nan } 1352c45c86ebSWang Nan 1353c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 135446bc29b9SAdrian Hunter process_synthesized_event, machine); 135546bc29b9SAdrian Hunter if (err) 135646bc29b9SAdrian Hunter goto out; 135746bc29b9SAdrian Hunter 1358c0a6de06SAdrian Hunter /* Synthesize id_index before auxtrace_info */ 1359c0a6de06SAdrian Hunter if (rec->opts.auxtrace_sample_mode) { 1360c0a6de06SAdrian Hunter err = perf_event__synthesize_id_index(tool, 1361c0a6de06SAdrian Hunter process_synthesized_event, 1362c0a6de06SAdrian Hunter session->evlist, machine); 1363c0a6de06SAdrian Hunter if (err) 1364c0a6de06SAdrian Hunter goto out; 1365c0a6de06SAdrian Hunter } 1366c0a6de06SAdrian Hunter 1367c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 1368c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 1369c45c86ebSWang Nan session, process_synthesized_event); 1370c45c86ebSWang Nan if (err) 1371c45c86ebSWang Nan goto out; 1372c45c86ebSWang Nan } 1373c45c86ebSWang Nan 13746c443954SArnaldo Carvalho de Melo if (!perf_evlist__exclude_kernel(rec->evlist)) { 1375c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 1376c45c86ebSWang Nan machine); 1377c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 1378c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1379c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 1380c45c86ebSWang Nan 1381c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 1382c45c86ebSWang Nan machine); 1383c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 1384c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1385c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 13866c443954SArnaldo Carvalho de Melo } 1387c45c86ebSWang Nan 1388c45c86ebSWang Nan if (perf_guest) { 1389c45c86ebSWang Nan machines__process_guests(&session->machines, 1390c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 1391c45c86ebSWang Nan } 1392c45c86ebSWang Nan 1393bfd8f72cSAndi Kleen err = perf_event__synthesize_extra_attr(&rec->tool, 1394bfd8f72cSAndi Kleen rec->evlist, 1395bfd8f72cSAndi Kleen process_synthesized_event, 1396bfd8f72cSAndi Kleen data->is_pipe); 1397bfd8f72cSAndi Kleen if (err) 1398bfd8f72cSAndi Kleen goto out; 1399bfd8f72cSAndi Kleen 140003617c22SJiri Olsa err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->core.threads, 1401373565d2SAndi Kleen process_synthesized_event, 1402373565d2SAndi Kleen NULL); 1403373565d2SAndi Kleen if (err < 0) { 1404373565d2SAndi Kleen pr_err("Couldn't synthesize thread map.\n"); 1405373565d2SAndi Kleen return err; 1406373565d2SAndi Kleen } 1407373565d2SAndi Kleen 1408f72f901dSJiri Olsa err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.cpus, 1409373565d2SAndi Kleen process_synthesized_event, NULL); 1410373565d2SAndi Kleen if (err < 0) { 1411373565d2SAndi Kleen pr_err("Couldn't synthesize cpu map.\n"); 1412373565d2SAndi Kleen return err; 1413373565d2SAndi Kleen } 1414373565d2SAndi Kleen 1415e5416950SSong Liu err = perf_event__synthesize_bpf_events(session, process_synthesized_event, 14167b612e29SSong Liu machine, opts); 14177b612e29SSong Liu if (err < 0) 14187b612e29SSong Liu pr_warning("Couldn't synthesize bpf events.\n"); 14197b612e29SSong Liu 1420ab64069fSNamhyung Kim err = perf_event__synthesize_cgroups(tool, process_synthesized_event, 1421ab64069fSNamhyung Kim machine); 1422ab64069fSNamhyung Kim if (err < 0) 1423ab64069fSNamhyung Kim pr_warning("Couldn't synthesize cgroup events.\n"); 1424ab64069fSNamhyung Kim 1425d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) { 1426d99c22eaSStephane Eranian perf_set_multithreaded(); 1427d99c22eaSStephane Eranian f = process_locked_synthesized_event; 1428d99c22eaSStephane Eranian } 1429d99c22eaSStephane Eranian 143003617c22SJiri Olsa err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads, 1431d99c22eaSStephane Eranian f, opts->sample_address, 1432d99c22eaSStephane Eranian rec->opts.nr_threads_synthesize); 1433d99c22eaSStephane Eranian 1434d99c22eaSStephane Eranian if (rec->opts.nr_threads_synthesize > 1) 1435d99c22eaSStephane Eranian perf_set_singlethreaded(); 1436d99c22eaSStephane Eranian 1437c45c86ebSWang Nan out: 1438c45c86ebSWang Nan return err; 1439c45c86ebSWang Nan } 1440c45c86ebSWang Nan 1441899e5ffbSArnaldo Carvalho de Melo static int record__process_signal_event(union perf_event *event __maybe_unused, void *data) 1442899e5ffbSArnaldo Carvalho de Melo { 1443899e5ffbSArnaldo Carvalho de Melo struct record *rec = data; 1444899e5ffbSArnaldo Carvalho de Melo pthread_kill(rec->thread_id, SIGUSR2); 1445899e5ffbSArnaldo Carvalho de Melo return 0; 1446899e5ffbSArnaldo Carvalho de Melo } 1447899e5ffbSArnaldo Carvalho de Melo 144823cbb41cSArnaldo Carvalho de Melo static int record__setup_sb_evlist(struct record *rec) 144923cbb41cSArnaldo Carvalho de Melo { 145023cbb41cSArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 145123cbb41cSArnaldo Carvalho de Melo 145223cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist != NULL) { 145323cbb41cSArnaldo Carvalho de Melo /* 145423cbb41cSArnaldo Carvalho de Melo * We get here if --switch-output-event populated the 145523cbb41cSArnaldo Carvalho de Melo * sb_evlist, so associate a callback that will send a SIGUSR2 145623cbb41cSArnaldo Carvalho de Melo * to the main thread. 145723cbb41cSArnaldo Carvalho de Melo */ 145823cbb41cSArnaldo Carvalho de Melo evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec); 145923cbb41cSArnaldo Carvalho de Melo rec->thread_id = pthread_self(); 146023cbb41cSArnaldo Carvalho de Melo } 146123cbb41cSArnaldo Carvalho de Melo 146223cbb41cSArnaldo Carvalho de Melo if (!opts->no_bpf_event) { 146323cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 146423cbb41cSArnaldo Carvalho de Melo rec->sb_evlist = evlist__new(); 146523cbb41cSArnaldo Carvalho de Melo 146623cbb41cSArnaldo Carvalho de Melo if (rec->sb_evlist == NULL) { 146723cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't create side band evlist.\n."); 146823cbb41cSArnaldo Carvalho de Melo return -1; 146923cbb41cSArnaldo Carvalho de Melo } 147023cbb41cSArnaldo Carvalho de Melo } 147123cbb41cSArnaldo Carvalho de Melo 147223cbb41cSArnaldo Carvalho de Melo if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) { 147323cbb41cSArnaldo Carvalho de Melo pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n."); 147423cbb41cSArnaldo Carvalho de Melo return -1; 147523cbb41cSArnaldo Carvalho de Melo } 147623cbb41cSArnaldo Carvalho de Melo } 147723cbb41cSArnaldo Carvalho de Melo 147823cbb41cSArnaldo Carvalho de Melo if (perf_evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) { 147923cbb41cSArnaldo Carvalho de Melo pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n"); 148023cbb41cSArnaldo Carvalho de Melo opts->no_bpf_event = true; 148123cbb41cSArnaldo Carvalho de Melo } 148223cbb41cSArnaldo Carvalho de Melo 148323cbb41cSArnaldo Carvalho de Melo return 0; 148423cbb41cSArnaldo Carvalho de Melo } 148523cbb41cSArnaldo Carvalho de Melo 14868c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 148786470930SIngo Molnar { 148857706abcSDavid Ahern int err; 148945604710SNamhyung Kim int status = 0; 14908b412664SPeter Zijlstra unsigned long waking = 0; 149146be604bSZhang, Yanmin const bool forks = argc > 0; 149245694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 1493b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 14948ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1495d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 14966dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 149742aa276fSNamhyung Kim int fd; 1498d3c8c08eSAlexey Budankov float ratio = 0; 149986470930SIngo Molnar 150045604710SNamhyung Kim atexit(record__sig_exit); 1501f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 1502f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 1503804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 1504a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 1505c0bdc1c4SWang Nan 1506f3b3614aSHari Bathini if (rec->opts.record_namespaces) 1507f3b3614aSHari Bathini tool->namespace_events = true; 1508f3b3614aSHari Bathini 15098fb4b679SNamhyung Kim if (rec->opts.record_cgroup) { 15108fb4b679SNamhyung Kim #ifdef HAVE_FILE_HANDLE 15118fb4b679SNamhyung Kim tool->cgroup_events = true; 15128fb4b679SNamhyung Kim #else 15138fb4b679SNamhyung Kim pr_err("cgroup tracking is not supported\n"); 15148fb4b679SNamhyung Kim return -1; 15158fb4b679SNamhyung Kim #endif 15168fb4b679SNamhyung Kim } 15178fb4b679SNamhyung Kim 1518dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 15192dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 15203c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 15215f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 1522dc0c6127SJiri Olsa if (rec->switch_output.enabled) 15233c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 1524c0bdc1c4SWang Nan } else { 15252dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 1526c0bdc1c4SWang Nan } 1527f5970550SPeter Zijlstra 15288ceb41d7SJiri Olsa session = perf_session__new(data, false, tool); 15296ef81c55SMamatha Inamdar if (IS_ERR(session)) { 1530ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 15316ef81c55SMamatha Inamdar return PTR_ERR(session); 1532a9a70bbcSArnaldo Carvalho de Melo } 1533a9a70bbcSArnaldo Carvalho de Melo 15348ceb41d7SJiri Olsa fd = perf_data__fd(data); 1535d20deb64SArnaldo Carvalho de Melo rec->session = session; 1536d20deb64SArnaldo Carvalho de Melo 15375d7f4116SAlexey Budankov if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) { 15385d7f4116SAlexey Budankov pr_err("Compression initialization failed.\n"); 15395d7f4116SAlexey Budankov return -1; 15405d7f4116SAlexey Budankov } 15415d7f4116SAlexey Budankov 15425d7f4116SAlexey Budankov session->header.env.comp_type = PERF_COMP_ZSTD; 15435d7f4116SAlexey Budankov session->header.env.comp_level = rec->opts.comp_level; 15445d7f4116SAlexey Budankov 1545eeb399b5SAdrian Hunter if (rec->opts.kcore && 1546eeb399b5SAdrian Hunter !record__kcore_readable(&session->machines.host)) { 1547eeb399b5SAdrian Hunter pr_err("ERROR: kcore is not readable.\n"); 1548eeb399b5SAdrian Hunter return -1; 1549eeb399b5SAdrian Hunter } 1550eeb399b5SAdrian Hunter 15518c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 1552330aa675SStephane Eranian 1553cf790516SAlexey Budankov if (rec->opts.use_clockid && rec->opts.clockid_res_ns) 1554cf790516SAlexey Budankov session->header.env.clockid_res_ns = rec->opts.clockid_res_ns; 1555cf790516SAlexey Budankov 1556d4db3f16SArnaldo Carvalho de Melo if (forks) { 15573e2be2daSArnaldo Carvalho de Melo err = perf_evlist__prepare_workload(rec->evlist, &opts->target, 15588ceb41d7SJiri Olsa argv, data->is_pipe, 1559735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 156035b9d88eSArnaldo Carvalho de Melo if (err < 0) { 156135b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 156245604710SNamhyung Kim status = err; 156335b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 1564856e9660SPeter Zijlstra } 1565856e9660SPeter Zijlstra } 1566856e9660SPeter Zijlstra 1567ad46e48cSJiri Olsa /* 1568ad46e48cSJiri Olsa * If we have just single event and are sending data 1569ad46e48cSJiri Olsa * through pipe, we need to force the ids allocation, 1570ad46e48cSJiri Olsa * because we synthesize event name through the pipe 1571ad46e48cSJiri Olsa * and need the id for that. 1572ad46e48cSJiri Olsa */ 15736484d2f9SJiri Olsa if (data->is_pipe && rec->evlist->core.nr_entries == 1) 1574ad46e48cSJiri Olsa rec->opts.sample_id = true; 1575ad46e48cSJiri Olsa 15768c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 15778d3eca20SDavid Ahern err = -1; 157845604710SNamhyung Kim goto out_child; 15798d3eca20SDavid Ahern } 1580f6fa4375SJiri Olsa session->header.env.comp_mmap_len = session->evlist->core.mmap_len; 158186470930SIngo Molnar 1582eeb399b5SAdrian Hunter if (rec->opts.kcore) { 1583eeb399b5SAdrian Hunter err = record__kcore_copy(&session->machines.host, data); 1584eeb399b5SAdrian Hunter if (err) { 1585eeb399b5SAdrian Hunter pr_err("ERROR: Failed to copy kcore\n"); 1586eeb399b5SAdrian Hunter goto out_child; 1587eeb399b5SAdrian Hunter } 1588eeb399b5SAdrian Hunter } 1589eeb399b5SAdrian Hunter 15908690a2a7SWang Nan err = bpf__apply_obj_config(); 15918690a2a7SWang Nan if (err) { 15928690a2a7SWang Nan char errbuf[BUFSIZ]; 15938690a2a7SWang Nan 15948690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 15958690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 15968690a2a7SWang Nan errbuf); 15978690a2a7SWang Nan goto out_child; 15988690a2a7SWang Nan } 15998690a2a7SWang Nan 1600cca8482cSAdrian Hunter /* 1601cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 1602cca8482cSAdrian Hunter * evlist. 1603cca8482cSAdrian Hunter */ 1604cca8482cSAdrian Hunter if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) { 1605cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 1606cca8482cSAdrian Hunter rec->tool.ordered_events = false; 1607cca8482cSAdrian Hunter } 1608cca8482cSAdrian Hunter 16093e2be2daSArnaldo Carvalho de Melo if (!rec->evlist->nr_groups) 1610a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 1611a8bb559bSNamhyung Kim 16128ceb41d7SJiri Olsa if (data->is_pipe) { 161342aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 1614529870e3STom Zanussi if (err < 0) 161545604710SNamhyung Kim goto out_child; 1616563aecb2SJiri Olsa } else { 161742aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 1618d5eed904SArnaldo Carvalho de Melo if (err < 0) 161945604710SNamhyung Kim goto out_child; 1620d5eed904SArnaldo Carvalho de Melo } 16217c6a1c65SPeter Zijlstra 1622b38d85efSArnaldo Carvalho de Melo err = -1; 1623d3665498SDavid Ahern if (!rec->no_buildid 1624e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 1625d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 1626e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 162745604710SNamhyung Kim goto out_child; 1628e20960c0SRobert Richter } 1629e20960c0SRobert Richter 163023cbb41cSArnaldo Carvalho de Melo err = record__setup_sb_evlist(rec); 163123cbb41cSArnaldo Carvalho de Melo if (err) 1632b38d85efSArnaldo Carvalho de Melo goto out_child; 1633657ee553SSong Liu 16344ea648aeSWang Nan err = record__synthesize(rec, false); 1635c45c86ebSWang Nan if (err < 0) 163645604710SNamhyung Kim goto out_child; 16378d3eca20SDavid Ahern 1638d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 163986470930SIngo Molnar struct sched_param param; 164086470930SIngo Molnar 1641d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 164286470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 16436beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 16448d3eca20SDavid Ahern err = -1; 164545604710SNamhyung Kim goto out_child; 164686470930SIngo Molnar } 164786470930SIngo Molnar } 164886470930SIngo Molnar 1649774cb499SJiri Olsa /* 1650774cb499SJiri Olsa * When perf is starting the traced process, all the events 1651774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 1652774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 1653774cb499SJiri Olsa */ 16546619a53eSAndi Kleen if (!target__none(&opts->target) && !opts->initial_delay) 16551c87f165SJiri Olsa evlist__enable(rec->evlist); 1656764e16a3SDavid Ahern 1657856e9660SPeter Zijlstra /* 1658856e9660SPeter Zijlstra * Let the child rip 1659856e9660SPeter Zijlstra */ 1660e803cf97SNamhyung Kim if (forks) { 166120a8a3cfSJiri Olsa struct machine *machine = &session->machines.host; 1662e5bed564SNamhyung Kim union perf_event *event; 1663e907caf3SHari Bathini pid_t tgid; 1664e5bed564SNamhyung Kim 1665e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 1666e5bed564SNamhyung Kim if (event == NULL) { 1667e5bed564SNamhyung Kim err = -ENOMEM; 1668e5bed564SNamhyung Kim goto out_child; 1669e5bed564SNamhyung Kim } 1670e5bed564SNamhyung Kim 1671e803cf97SNamhyung Kim /* 1672e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 1673e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 1674e803cf97SNamhyung Kim * cannot see a correct process name for those events. 1675e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 1676e803cf97SNamhyung Kim */ 1677e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 1678e803cf97SNamhyung Kim rec->evlist->workload.pid, 1679e803cf97SNamhyung Kim process_synthesized_event, 1680e803cf97SNamhyung Kim machine); 1681e5bed564SNamhyung Kim free(event); 1682e803cf97SNamhyung Kim 1683e907caf3SHari Bathini if (tgid == -1) 1684e907caf3SHari Bathini goto out_child; 1685e907caf3SHari Bathini 1686e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 1687e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 1688e907caf3SHari Bathini machine->id_hdr_size); 1689e907caf3SHari Bathini if (event == NULL) { 1690e907caf3SHari Bathini err = -ENOMEM; 1691e907caf3SHari Bathini goto out_child; 1692e907caf3SHari Bathini } 1693e907caf3SHari Bathini 1694e907caf3SHari Bathini /* 1695e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 1696e907caf3SHari Bathini */ 1697e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 1698e907caf3SHari Bathini rec->evlist->workload.pid, 1699e907caf3SHari Bathini tgid, process_synthesized_event, 1700e907caf3SHari Bathini machine); 1701e907caf3SHari Bathini free(event); 1702e907caf3SHari Bathini 17033e2be2daSArnaldo Carvalho de Melo perf_evlist__start_workload(rec->evlist); 1704e803cf97SNamhyung Kim } 1705856e9660SPeter Zijlstra 17066619a53eSAndi Kleen if (opts->initial_delay) { 17070693e680SArnaldo Carvalho de Melo usleep(opts->initial_delay * USEC_PER_MSEC); 17081c87f165SJiri Olsa evlist__enable(rec->evlist); 17096619a53eSAndi Kleen } 17106619a53eSAndi Kleen 17115f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 17123c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 1713a074865eSWang Nan perf_hooks__invoke_record_start(); 1714649c48a9SPeter Zijlstra for (;;) { 17159f065194SYang Shi unsigned long long hits = rec->samples; 171686470930SIngo Molnar 171705737464SWang Nan /* 171805737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 171905737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 172005737464SWang Nan * hits != rec->samples in previous round. 172105737464SWang Nan * 172205737464SWang Nan * perf_evlist__toggle_bkw_mmap ensure we never 172305737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 172405737464SWang Nan */ 172505737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 172605737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 172705737464SWang Nan 1728470530bbSAlexey Budankov if (record__mmap_read_all(rec, false) < 0) { 17295f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 17303c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 17318d3eca20SDavid Ahern err = -1; 173245604710SNamhyung Kim goto out_child; 17338d3eca20SDavid Ahern } 173486470930SIngo Molnar 17352dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 17362dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 17375f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 1738ce7b0e42SAlexander Shishkin record__read_auxtrace_snapshot(rec, false); 17395f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 17402dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 17412dd6d8a1SAdrian Hunter err = -1; 17422dd6d8a1SAdrian Hunter goto out_child; 17432dd6d8a1SAdrian Hunter } 17442dd6d8a1SAdrian Hunter } 17452dd6d8a1SAdrian Hunter 17463c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 174705737464SWang Nan /* 174805737464SWang Nan * If switch_output_trigger is hit, the data in 174905737464SWang Nan * overwritable ring buffer should have been collected, 175005737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 175105737464SWang Nan * 175205737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 175305737464SWang Nan * record__mmap_read_all() didn't collect data from 175405737464SWang Nan * overwritable ring buffer. Read again. 175505737464SWang Nan */ 175605737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 175705737464SWang Nan continue; 17583c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 17593c1cb7e3SWang Nan 176005737464SWang Nan /* 176105737464SWang Nan * Reenable events in overwrite ring buffer after 176205737464SWang Nan * record__mmap_read_all(): we should have collected 176305737464SWang Nan * data from it. 176405737464SWang Nan */ 176505737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 176605737464SWang Nan 17673c1cb7e3SWang Nan if (!quiet) 17683c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 17693c1cb7e3SWang Nan waking); 17703c1cb7e3SWang Nan waking = 0; 17713c1cb7e3SWang Nan fd = record__switch_output(rec, false); 17723c1cb7e3SWang Nan if (fd < 0) { 17733c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 17743c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 17753c1cb7e3SWang Nan err = fd; 17763c1cb7e3SWang Nan goto out_child; 17773c1cb7e3SWang Nan } 1778bfacbe3bSJiri Olsa 1779bfacbe3bSJiri Olsa /* re-arm the alarm */ 1780bfacbe3bSJiri Olsa if (rec->switch_output.time) 1781bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 17823c1cb7e3SWang Nan } 17833c1cb7e3SWang Nan 1784d20deb64SArnaldo Carvalho de Melo if (hits == rec->samples) { 17856dcf45efSArnaldo Carvalho de Melo if (done || draining) 1786649c48a9SPeter Zijlstra break; 178780ab2987SJiri Olsa err = evlist__poll(rec->evlist, -1); 1788a515114fSJiri Olsa /* 1789a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 1790a515114fSJiri Olsa * number of returned events and interrupt error. 1791a515114fSJiri Olsa */ 1792a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 179345604710SNamhyung Kim err = 0; 17948b412664SPeter Zijlstra waking++; 17956dcf45efSArnaldo Carvalho de Melo 1796f4009e7bSJiri Olsa if (evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) 17976dcf45efSArnaldo Carvalho de Melo draining = true; 17988b412664SPeter Zijlstra } 17998b412664SPeter Zijlstra 1800774cb499SJiri Olsa /* 1801774cb499SJiri Olsa * When perf is starting the traced process, at the end events 1802774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 1803774cb499SJiri Olsa * disable events in this case. 1804774cb499SJiri Olsa */ 1805602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 18065f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 1807e74676deSJiri Olsa evlist__disable(rec->evlist); 18082711926aSJiri Olsa disabled = true; 18092711926aSJiri Olsa } 18108b412664SPeter Zijlstra } 1811ce7b0e42SAlexander Shishkin 18125f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 18133c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 18148b412664SPeter Zijlstra 1815ce7b0e42SAlexander Shishkin if (opts->auxtrace_snapshot_on_exit) 1816ce7b0e42SAlexander Shishkin record__auxtrace_snapshot_exit(rec); 1817ce7b0e42SAlexander Shishkin 1818f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 181935550da3SMasami Hiramatsu char msg[STRERR_BUFSIZE]; 1820c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 1821f33cbe72SArnaldo Carvalho de Melo pr_err("Workload failed: %s\n", emsg); 1822f33cbe72SArnaldo Carvalho de Melo err = -1; 182345604710SNamhyung Kim goto out_child; 1824f33cbe72SArnaldo Carvalho de Melo } 1825f33cbe72SArnaldo Carvalho de Melo 1826e3d59112SNamhyung Kim if (!quiet) 18278b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 182886470930SIngo Molnar 18294ea648aeSWang Nan if (target__none(&rec->opts.target)) 18304ea648aeSWang Nan record__synthesize_workload(rec, true); 18314ea648aeSWang Nan 183245604710SNamhyung Kim out_child: 1833470530bbSAlexey Budankov record__mmap_read_all(rec, true); 1834d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1835d3d1af6fSAlexey Budankov 1836d3c8c08eSAlexey Budankov if (rec->session->bytes_transferred && rec->session->bytes_compressed) { 1837d3c8c08eSAlexey Budankov ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed; 1838d3c8c08eSAlexey Budankov session->header.env.comp_ratio = ratio + 0.5; 1839d3c8c08eSAlexey Budankov } 1840d3c8c08eSAlexey Budankov 184145604710SNamhyung Kim if (forks) { 184245604710SNamhyung Kim int exit_status; 184345604710SNamhyung Kim 184445604710SNamhyung Kim if (!child_finished) 184545604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 184645604710SNamhyung Kim 184745604710SNamhyung Kim wait(&exit_status); 184845604710SNamhyung Kim 184945604710SNamhyung Kim if (err < 0) 185045604710SNamhyung Kim status = err; 185145604710SNamhyung Kim else if (WIFEXITED(exit_status)) 185245604710SNamhyung Kim status = WEXITSTATUS(exit_status); 185345604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 185445604710SNamhyung Kim signr = WTERMSIG(exit_status); 185545604710SNamhyung Kim } else 185645604710SNamhyung Kim status = err; 185745604710SNamhyung Kim 18584ea648aeSWang Nan record__synthesize(rec, true); 1859e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 1860e3d59112SNamhyung Kim rec->samples = 0; 1861e3d59112SNamhyung Kim 1862ecfd7a9cSWang Nan if (!err) { 1863ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 1864e1ab48baSWang Nan record__finish_output(rec); 1865ecfd7a9cSWang Nan } else { 1866ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 1867ecfd7a9cSWang Nan if (fd < 0) { 1868ecfd7a9cSWang Nan status = fd; 1869ecfd7a9cSWang Nan goto out_delete_session; 1870ecfd7a9cSWang Nan } 1871ecfd7a9cSWang Nan } 1872ecfd7a9cSWang Nan } 187339d17dacSArnaldo Carvalho de Melo 1874a074865eSWang Nan perf_hooks__invoke_record_end(); 1875a074865eSWang Nan 1876e3d59112SNamhyung Kim if (!err && !quiet) { 1877e3d59112SNamhyung Kim char samples[128]; 1878ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 1879ecfd7a9cSWang Nan ".<timestamp>" : ""; 1880e3d59112SNamhyung Kim 1881ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 1882e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 1883e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 1884e3d59112SNamhyung Kim else 1885e3d59112SNamhyung Kim samples[0] = '\0'; 1886e3d59112SNamhyung Kim 1887d3c8c08eSAlexey Budankov fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s", 18888ceb41d7SJiri Olsa perf_data__size(data) / 1024.0 / 1024.0, 18892d4f2799SJiri Olsa data->path, postfix, samples); 1890d3c8c08eSAlexey Budankov if (ratio) { 1891d3c8c08eSAlexey Budankov fprintf(stderr, ", compressed (original %.3f MB, ratio is %.3f)", 1892d3c8c08eSAlexey Budankov rec->session->bytes_transferred / 1024.0 / 1024.0, 1893d3c8c08eSAlexey Budankov ratio); 1894d3c8c08eSAlexey Budankov } 1895d3c8c08eSAlexey Budankov fprintf(stderr, " ]\n"); 1896e3d59112SNamhyung Kim } 1897e3d59112SNamhyung Kim 189839d17dacSArnaldo Carvalho de Melo out_delete_session: 18995d7f4116SAlexey Budankov zstd_fini(&session->zstd_data); 190039d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 1901657ee553SSong Liu 1902657ee553SSong Liu if (!opts->no_bpf_event) 1903bc477d79SArnaldo Carvalho de Melo perf_evlist__stop_sb_thread(rec->sb_evlist); 190445604710SNamhyung Kim return status; 190586470930SIngo Molnar } 190686470930SIngo Molnar 19070883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 190809b0fd45SJiri Olsa { 1909aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 1910a601fdffSJiri Olsa 19110883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 191226d33022SJiri Olsa 19130883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 191409b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 19150883e820SArnaldo Carvalho de Melo callchain->dump_size); 19160883e820SArnaldo Carvalho de Melo } 19170883e820SArnaldo Carvalho de Melo 19180883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 19190883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 19200883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 19210883e820SArnaldo Carvalho de Melo { 19220883e820SArnaldo Carvalho de Melo int ret; 19230883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 19240883e820SArnaldo Carvalho de Melo 19250883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 19260883e820SArnaldo Carvalho de Melo if (unset) { 19270883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 19280883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 19290883e820SArnaldo Carvalho de Melo return 0; 19300883e820SArnaldo Carvalho de Melo } 19310883e820SArnaldo Carvalho de Melo 19320883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 19330883e820SArnaldo Carvalho de Melo if (!ret) { 19340883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 19350883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 19360883e820SArnaldo Carvalho de Melo record->sample_address = true; 19370883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 19380883e820SArnaldo Carvalho de Melo } 19390883e820SArnaldo Carvalho de Melo 19400883e820SArnaldo Carvalho de Melo return ret; 194109b0fd45SJiri Olsa } 194209b0fd45SJiri Olsa 1943c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 194409b0fd45SJiri Olsa const char *arg, 194509b0fd45SJiri Olsa int unset) 194609b0fd45SJiri Olsa { 19470883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 194826d33022SJiri Olsa } 194926d33022SJiri Olsa 1950c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 195109b0fd45SJiri Olsa const char *arg __maybe_unused, 195209b0fd45SJiri Olsa int unset __maybe_unused) 195309b0fd45SJiri Olsa { 19542ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 1955c421e80bSKan Liang 19562ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 195709b0fd45SJiri Olsa 19582ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 19592ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 1960eb853e80SJiri Olsa 19612ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 196209b0fd45SJiri Olsa return 0; 196309b0fd45SJiri Olsa } 196409b0fd45SJiri Olsa 1965eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 1966eb853e80SJiri Olsa { 19677a29c087SNamhyung Kim struct record *rec = cb; 19687a29c087SNamhyung Kim 19697a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 19707a29c087SNamhyung Kim if (!strcmp(value, "cache")) 19717a29c087SNamhyung Kim rec->no_buildid_cache = false; 19727a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 19737a29c087SNamhyung Kim rec->no_buildid_cache = true; 19747a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 19757a29c087SNamhyung Kim rec->no_buildid = true; 19767a29c087SNamhyung Kim else 19777a29c087SNamhyung Kim return -1; 19787a29c087SNamhyung Kim return 0; 19797a29c087SNamhyung Kim } 1980cff17205SYisheng Xie if (!strcmp(var, "record.call-graph")) { 1981cff17205SYisheng Xie var = "call-graph.record-mode"; 1982eb853e80SJiri Olsa return perf_default_config(var, value, cb); 1983eb853e80SJiri Olsa } 198493f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 198593f20c0fSAlexey Budankov if (!strcmp(var, "record.aio")) { 198693f20c0fSAlexey Budankov rec->opts.nr_cblocks = strtol(value, NULL, 0); 198793f20c0fSAlexey Budankov if (!rec->opts.nr_cblocks) 198893f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_default; 198993f20c0fSAlexey Budankov } 199093f20c0fSAlexey Budankov #endif 1991eb853e80SJiri Olsa 1992cff17205SYisheng Xie return 0; 1993cff17205SYisheng Xie } 1994cff17205SYisheng Xie 1995814c8c38SPeter Zijlstra struct clockid_map { 1996814c8c38SPeter Zijlstra const char *name; 1997814c8c38SPeter Zijlstra int clockid; 1998814c8c38SPeter Zijlstra }; 1999814c8c38SPeter Zijlstra 2000814c8c38SPeter Zijlstra #define CLOCKID_MAP(n, c) \ 2001814c8c38SPeter Zijlstra { .name = n, .clockid = (c), } 2002814c8c38SPeter Zijlstra 2003814c8c38SPeter Zijlstra #define CLOCKID_END { .name = NULL, } 2004814c8c38SPeter Zijlstra 2005814c8c38SPeter Zijlstra 2006814c8c38SPeter Zijlstra /* 2007814c8c38SPeter Zijlstra * Add the missing ones, we need to build on many distros... 2008814c8c38SPeter Zijlstra */ 2009814c8c38SPeter Zijlstra #ifndef CLOCK_MONOTONIC_RAW 2010814c8c38SPeter Zijlstra #define CLOCK_MONOTONIC_RAW 4 2011814c8c38SPeter Zijlstra #endif 2012814c8c38SPeter Zijlstra #ifndef CLOCK_BOOTTIME 2013814c8c38SPeter Zijlstra #define CLOCK_BOOTTIME 7 2014814c8c38SPeter Zijlstra #endif 2015814c8c38SPeter Zijlstra #ifndef CLOCK_TAI 2016814c8c38SPeter Zijlstra #define CLOCK_TAI 11 2017814c8c38SPeter Zijlstra #endif 2018814c8c38SPeter Zijlstra 2019814c8c38SPeter Zijlstra static const struct clockid_map clockids[] = { 2020814c8c38SPeter Zijlstra /* available for all events, NMI safe */ 2021814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), 2022814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), 2023814c8c38SPeter Zijlstra 2024814c8c38SPeter Zijlstra /* available for some events */ 2025814c8c38SPeter Zijlstra CLOCKID_MAP("realtime", CLOCK_REALTIME), 2026814c8c38SPeter Zijlstra CLOCKID_MAP("boottime", CLOCK_BOOTTIME), 2027814c8c38SPeter Zijlstra CLOCKID_MAP("tai", CLOCK_TAI), 2028814c8c38SPeter Zijlstra 2029814c8c38SPeter Zijlstra /* available for the lazy */ 2030814c8c38SPeter Zijlstra CLOCKID_MAP("mono", CLOCK_MONOTONIC), 2031814c8c38SPeter Zijlstra CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), 2032814c8c38SPeter Zijlstra CLOCKID_MAP("real", CLOCK_REALTIME), 2033814c8c38SPeter Zijlstra CLOCKID_MAP("boot", CLOCK_BOOTTIME), 2034814c8c38SPeter Zijlstra 2035814c8c38SPeter Zijlstra CLOCKID_END, 2036814c8c38SPeter Zijlstra }; 2037814c8c38SPeter Zijlstra 2038cf790516SAlexey Budankov static int get_clockid_res(clockid_t clk_id, u64 *res_ns) 2039cf790516SAlexey Budankov { 2040cf790516SAlexey Budankov struct timespec res; 2041cf790516SAlexey Budankov 2042cf790516SAlexey Budankov *res_ns = 0; 2043cf790516SAlexey Budankov if (!clock_getres(clk_id, &res)) 2044cf790516SAlexey Budankov *res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC; 2045cf790516SAlexey Budankov else 2046cf790516SAlexey Budankov pr_warning("WARNING: Failed to determine specified clock resolution.\n"); 2047cf790516SAlexey Budankov 2048cf790516SAlexey Budankov return 0; 2049cf790516SAlexey Budankov } 2050cf790516SAlexey Budankov 2051814c8c38SPeter Zijlstra static int parse_clockid(const struct option *opt, const char *str, int unset) 2052814c8c38SPeter Zijlstra { 2053814c8c38SPeter Zijlstra struct record_opts *opts = (struct record_opts *)opt->value; 2054814c8c38SPeter Zijlstra const struct clockid_map *cm; 2055814c8c38SPeter Zijlstra const char *ostr = str; 2056814c8c38SPeter Zijlstra 2057814c8c38SPeter Zijlstra if (unset) { 2058814c8c38SPeter Zijlstra opts->use_clockid = 0; 2059814c8c38SPeter Zijlstra return 0; 2060814c8c38SPeter Zijlstra } 2061814c8c38SPeter Zijlstra 2062814c8c38SPeter Zijlstra /* no arg passed */ 2063814c8c38SPeter Zijlstra if (!str) 2064814c8c38SPeter Zijlstra return 0; 2065814c8c38SPeter Zijlstra 2066814c8c38SPeter Zijlstra /* no setting it twice */ 2067814c8c38SPeter Zijlstra if (opts->use_clockid) 2068814c8c38SPeter Zijlstra return -1; 2069814c8c38SPeter Zijlstra 2070814c8c38SPeter Zijlstra opts->use_clockid = true; 2071814c8c38SPeter Zijlstra 2072814c8c38SPeter Zijlstra /* if its a number, we're done */ 2073814c8c38SPeter Zijlstra if (sscanf(str, "%d", &opts->clockid) == 1) 2074cf790516SAlexey Budankov return get_clockid_res(opts->clockid, &opts->clockid_res_ns); 2075814c8c38SPeter Zijlstra 2076814c8c38SPeter Zijlstra /* allow a "CLOCK_" prefix to the name */ 2077814c8c38SPeter Zijlstra if (!strncasecmp(str, "CLOCK_", 6)) 2078814c8c38SPeter Zijlstra str += 6; 2079814c8c38SPeter Zijlstra 2080814c8c38SPeter Zijlstra for (cm = clockids; cm->name; cm++) { 2081814c8c38SPeter Zijlstra if (!strcasecmp(str, cm->name)) { 2082814c8c38SPeter Zijlstra opts->clockid = cm->clockid; 2083cf790516SAlexey Budankov return get_clockid_res(opts->clockid, 2084cf790516SAlexey Budankov &opts->clockid_res_ns); 2085814c8c38SPeter Zijlstra } 2086814c8c38SPeter Zijlstra } 2087814c8c38SPeter Zijlstra 2088814c8c38SPeter Zijlstra opts->use_clockid = false; 2089814c8c38SPeter Zijlstra ui__warning("unknown clockid %s, check man page\n", ostr); 2090814c8c38SPeter Zijlstra return -1; 2091814c8c38SPeter Zijlstra } 2092814c8c38SPeter Zijlstra 2093f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset) 2094f4fe11b7SAlexey Budankov { 2095f4fe11b7SAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 2096f4fe11b7SAlexey Budankov 2097f4fe11b7SAlexey Budankov if (unset || !str) 2098f4fe11b7SAlexey Budankov return 0; 2099f4fe11b7SAlexey Budankov 2100f4fe11b7SAlexey Budankov if (!strcasecmp(str, "node")) 2101f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_NODE; 2102f4fe11b7SAlexey Budankov else if (!strcasecmp(str, "cpu")) 2103f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_CPU; 2104f4fe11b7SAlexey Budankov 2105f4fe11b7SAlexey Budankov return 0; 2106f4fe11b7SAlexey Budankov } 2107f4fe11b7SAlexey Budankov 21086d575816SJiwei Sun static int parse_output_max_size(const struct option *opt, 21096d575816SJiwei Sun const char *str, int unset) 21106d575816SJiwei Sun { 21116d575816SJiwei Sun unsigned long *s = (unsigned long *)opt->value; 21126d575816SJiwei Sun static struct parse_tag tags_size[] = { 21136d575816SJiwei Sun { .tag = 'B', .mult = 1 }, 21146d575816SJiwei Sun { .tag = 'K', .mult = 1 << 10 }, 21156d575816SJiwei Sun { .tag = 'M', .mult = 1 << 20 }, 21166d575816SJiwei Sun { .tag = 'G', .mult = 1 << 30 }, 21176d575816SJiwei Sun { .tag = 0 }, 21186d575816SJiwei Sun }; 21196d575816SJiwei Sun unsigned long val; 21206d575816SJiwei Sun 21216d575816SJiwei Sun if (unset) { 21226d575816SJiwei Sun *s = 0; 21236d575816SJiwei Sun return 0; 21246d575816SJiwei Sun } 21256d575816SJiwei Sun 21266d575816SJiwei Sun val = parse_tag_value(str, tags_size); 21276d575816SJiwei Sun if (val != (unsigned long) -1) { 21286d575816SJiwei Sun *s = val; 21296d575816SJiwei Sun return 0; 21306d575816SJiwei Sun } 21316d575816SJiwei Sun 21326d575816SJiwei Sun return -1; 21336d575816SJiwei Sun } 21346d575816SJiwei Sun 2135e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 2136e9db1310SAdrian Hunter const char *str, 2137e9db1310SAdrian Hunter int unset __maybe_unused) 2138e9db1310SAdrian Hunter { 2139e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 2140e9db1310SAdrian Hunter char *s, *p; 2141e9db1310SAdrian Hunter unsigned int mmap_pages; 2142e9db1310SAdrian Hunter int ret; 2143e9db1310SAdrian Hunter 2144e9db1310SAdrian Hunter if (!str) 2145e9db1310SAdrian Hunter return -EINVAL; 2146e9db1310SAdrian Hunter 2147e9db1310SAdrian Hunter s = strdup(str); 2148e9db1310SAdrian Hunter if (!s) 2149e9db1310SAdrian Hunter return -ENOMEM; 2150e9db1310SAdrian Hunter 2151e9db1310SAdrian Hunter p = strchr(s, ','); 2152e9db1310SAdrian Hunter if (p) 2153e9db1310SAdrian Hunter *p = '\0'; 2154e9db1310SAdrian Hunter 2155e9db1310SAdrian Hunter if (*s) { 2156e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s); 2157e9db1310SAdrian Hunter if (ret) 2158e9db1310SAdrian Hunter goto out_free; 2159e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 2160e9db1310SAdrian Hunter } 2161e9db1310SAdrian Hunter 2162e9db1310SAdrian Hunter if (!p) { 2163e9db1310SAdrian Hunter ret = 0; 2164e9db1310SAdrian Hunter goto out_free; 2165e9db1310SAdrian Hunter } 2166e9db1310SAdrian Hunter 2167e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1); 2168e9db1310SAdrian Hunter if (ret) 2169e9db1310SAdrian Hunter goto out_free; 2170e9db1310SAdrian Hunter 2171e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 2172e9db1310SAdrian Hunter 2173e9db1310SAdrian Hunter out_free: 2174e9db1310SAdrian Hunter free(s); 2175e9db1310SAdrian Hunter return ret; 2176e9db1310SAdrian Hunter } 2177e9db1310SAdrian Hunter 21780c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 21790c582449SJiri Olsa { 21809521b5f2SJiri Olsa u64 wakeup_size = evlist__mmap_size(rec->opts.mmap_pages); 21810c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 21820c582449SJiri Olsa 21830c582449SJiri Olsa wakeup_size /= 2; 21840c582449SJiri Olsa 21850c582449SJiri Olsa if (s->size < wakeup_size) { 21860c582449SJiri Olsa char buf[100]; 21870c582449SJiri Olsa 21880c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 21890c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 21900c582449SJiri Olsa "wakeup kernel buffer size (%s) " 21910c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 21920c582449SJiri Olsa } 21930c582449SJiri Olsa } 21940c582449SJiri Olsa 2195cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 2196cb4e1ebbSJiri Olsa { 2197cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 2198dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 2199dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 2200dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 2201dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 2202dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 2203dc0c6127SJiri Olsa { .tag = 0 }, 2204dc0c6127SJiri Olsa }; 2205bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 2206bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 2207bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 2208bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 2209bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 2210bfacbe3bSJiri Olsa { .tag = 0 }, 2211bfacbe3bSJiri Olsa }; 2212dc0c6127SJiri Olsa unsigned long val; 2213cb4e1ebbSJiri Olsa 2214899e5ffbSArnaldo Carvalho de Melo /* 2215899e5ffbSArnaldo Carvalho de Melo * If we're using --switch-output-events, then we imply its 2216899e5ffbSArnaldo Carvalho de Melo * --switch-output=signal, as we'll send a SIGUSR2 from the side band 2217899e5ffbSArnaldo Carvalho de Melo * thread to its parent. 2218899e5ffbSArnaldo Carvalho de Melo */ 2219899e5ffbSArnaldo Carvalho de Melo if (rec->switch_output_event_set) 2220899e5ffbSArnaldo Carvalho de Melo goto do_signal; 2221899e5ffbSArnaldo Carvalho de Melo 2222cb4e1ebbSJiri Olsa if (!s->set) 2223cb4e1ebbSJiri Olsa return 0; 2224cb4e1ebbSJiri Olsa 2225cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 2226899e5ffbSArnaldo Carvalho de Melo do_signal: 2227cb4e1ebbSJiri Olsa s->signal = true; 2228cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 2229dc0c6127SJiri Olsa goto enabled; 2230dc0c6127SJiri Olsa } 2231dc0c6127SJiri Olsa 2232dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 2233dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 2234dc0c6127SJiri Olsa s->size = val; 2235dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 2236dc0c6127SJiri Olsa goto enabled; 2237cb4e1ebbSJiri Olsa } 2238cb4e1ebbSJiri Olsa 2239bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 2240bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 2241bfacbe3bSJiri Olsa s->time = val; 2242bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 2243bfacbe3bSJiri Olsa s->str, s->time); 2244bfacbe3bSJiri Olsa goto enabled; 2245bfacbe3bSJiri Olsa } 2246bfacbe3bSJiri Olsa 2247cb4e1ebbSJiri Olsa return -1; 2248dc0c6127SJiri Olsa 2249dc0c6127SJiri Olsa enabled: 2250dc0c6127SJiri Olsa rec->timestamp_filename = true; 2251dc0c6127SJiri Olsa s->enabled = true; 22520c582449SJiri Olsa 22530c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 22540c582449SJiri Olsa switch_output_size_warn(rec); 22550c582449SJiri Olsa 2256dc0c6127SJiri Olsa return 0; 2257cb4e1ebbSJiri Olsa } 2258cb4e1ebbSJiri Olsa 2259e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 226086470930SIngo Molnar "perf record [<options>] [<command>]", 226186470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 226286470930SIngo Molnar NULL 226386470930SIngo Molnar }; 2264e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 226586470930SIngo Molnar 22666e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap(struct perf_tool *tool, union perf_event *event, 22676e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 22686e0a9b3dSArnaldo Carvalho de Melo { 22696e0a9b3dSArnaldo Carvalho de Melo /* 22706e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 22716e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 22726e0a9b3dSArnaldo Carvalho de Melo */ 22736e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 22746e0a9b3dSArnaldo Carvalho de Melo return 0; 22756e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap(tool, event, sample, machine); 22766e0a9b3dSArnaldo Carvalho de Melo } 22776e0a9b3dSArnaldo Carvalho de Melo 22786e0a9b3dSArnaldo Carvalho de Melo static int build_id__process_mmap2(struct perf_tool *tool, union perf_event *event, 22796e0a9b3dSArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine) 22806e0a9b3dSArnaldo Carvalho de Melo { 22816e0a9b3dSArnaldo Carvalho de Melo /* 22826e0a9b3dSArnaldo Carvalho de Melo * We already have the kernel maps, put in place via perf_session__create_kernel_maps() 22836e0a9b3dSArnaldo Carvalho de Melo * no need to add them twice. 22846e0a9b3dSArnaldo Carvalho de Melo */ 22856e0a9b3dSArnaldo Carvalho de Melo if (!(event->header.misc & PERF_RECORD_MISC_USER)) 22866e0a9b3dSArnaldo Carvalho de Melo return 0; 22876e0a9b3dSArnaldo Carvalho de Melo 22886e0a9b3dSArnaldo Carvalho de Melo return perf_event__process_mmap2(tool, event, sample, machine); 22896e0a9b3dSArnaldo Carvalho de Melo } 22906e0a9b3dSArnaldo Carvalho de Melo 2291d20deb64SArnaldo Carvalho de Melo /* 22928c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 22938c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 2294d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 2295d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 2296d20deb64SArnaldo Carvalho de Melo * 2297d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 2298d20deb64SArnaldo Carvalho de Melo * 2299d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 2300d20deb64SArnaldo Carvalho de Melo */ 23018c6f45a7SArnaldo Carvalho de Melo static struct record record = { 2302d20deb64SArnaldo Carvalho de Melo .opts = { 23038affc2b8SAndi Kleen .sample_time = true, 2304d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 2305d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 2306d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 2307447a6013SArnaldo Carvalho de Melo .freq = 4000, 2308d1cb9fceSNamhyung Kim .target = { 2309d1cb9fceSNamhyung Kim .uses_mmap = true, 23103aa5939dSAdrian Hunter .default_per_cpu = true, 2311d1cb9fceSNamhyung Kim }, 2312470530bbSAlexey Budankov .mmap_flush = MMAP_FLUSH_DEFAULT, 2313d99c22eaSStephane Eranian .nr_threads_synthesize = 1, 2314d20deb64SArnaldo Carvalho de Melo }, 2315e3d59112SNamhyung Kim .tool = { 2316e3d59112SNamhyung Kim .sample = process_sample_event, 2317e3d59112SNamhyung Kim .fork = perf_event__process_fork, 2318cca8482cSAdrian Hunter .exit = perf_event__process_exit, 2319e3d59112SNamhyung Kim .comm = perf_event__process_comm, 2320f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 23216e0a9b3dSArnaldo Carvalho de Melo .mmap = build_id__process_mmap, 23226e0a9b3dSArnaldo Carvalho de Melo .mmap2 = build_id__process_mmap2, 2323cca8482cSAdrian Hunter .ordered_events = true, 2324e3d59112SNamhyung Kim }, 2325d20deb64SArnaldo Carvalho de Melo }; 23267865e817SFrederic Weisbecker 232776a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 232876a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 232961eaa3beSArnaldo Carvalho de Melo 23300aab2136SWang Nan static bool dry_run; 23310aab2136SWang Nan 2332d20deb64SArnaldo Carvalho de Melo /* 2333d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 2334d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 2335b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 2336d20deb64SArnaldo Carvalho de Melo * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 2337d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 2338d20deb64SArnaldo Carvalho de Melo */ 2339efd21307SJiri Olsa static struct option __record_options[] = { 2340d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 234186470930SIngo Molnar "event selector. use 'perf list' to list available events", 2342f120f9d5SJiri Olsa parse_events_option), 2343d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 2344c171b552SLi Zefan "event filter", parse_filter), 23454ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 23464ba1faa1SWang Nan NULL, "don't record events from perf itself", 23474ba1faa1SWang Nan exclude_perf), 2348bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 2349d6d901c2SZhang, Yanmin "record events on existing process id"), 2350bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 2351d6d901c2SZhang, Yanmin "record events on existing thread id"), 2352d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 235386470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 2354509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 2355acac03faSKirill Smelkov "collect data without buffering"), 2356d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 2357daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 2358bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 235986470930SIngo Molnar "system-wide collection from all CPUs"), 2360bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 2361c45c6ea2SStephane Eranian "list of cpus to monitor"), 2362d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 23632d4f2799SJiri Olsa OPT_STRING('o', "output", &record.data.path, "file", 236486470930SIngo Molnar "output file name"), 236569e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 236669e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 23672e6cdf99SStephane Eranian "child tasks do not inherit counters"), 23684ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 23694ea648aeSWang Nan "synthesize non-sample events at the end of output"), 2370626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 237171184c6aSSong Liu OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "record bpf events"), 2372b09c2364SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq, 2373b09c2364SArnaldo Carvalho de Melo "Fail if the specified frequency can't be used"), 237467230479SArnaldo Carvalho de Melo OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'", 237567230479SArnaldo Carvalho de Melo "profile at this frequency", 237667230479SArnaldo Carvalho de Melo record__parse_freq), 2377e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 2378e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 2379e9db1310SAdrian Hunter record__parse_mmap_pages), 2380470530bbSAlexey Budankov OPT_CALLBACK(0, "mmap-flush", &record.opts, "number", 2381470530bbSAlexey Budankov "Minimal number of bytes that is extracted from mmap data pages (default: 1)", 2382470530bbSAlexey Budankov record__mmap_flush_parse), 2383d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "group", &record.opts.group, 238443bece79SLin Ming "put the counters into a counter group"), 23852ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 238609b0fd45SJiri Olsa NULL, "enables call-graph recording" , 238709b0fd45SJiri Olsa &record_callchain_opt), 238809b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 238976a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 239009b0fd45SJiri Olsa &record_parse_callchain_opt), 2391c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 23923da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 2393b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 2394d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 2395649c48a9SPeter Zijlstra "per thread counts"), 239656100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 23973b0a5daaSKan Liang OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, 23983b0a5daaSKan Liang "Record the sample physical addresses"), 2399b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 24003abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 24013abebc55SAdrian Hunter &record.opts.sample_time_set, 24023abebc55SAdrian Hunter "Record the sample timestamps"), 2403f290aa1fSJiri Olsa OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set, 2404f290aa1fSJiri Olsa "Record the sample period"), 2405d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 2406649c48a9SPeter Zijlstra "don't sample"), 2407d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 2408d2db9a98SWang Nan &record.no_buildid_cache_set, 2409a1ac1d3cSStephane Eranian "do not update the buildid cache"), 2410d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 2411d2db9a98SWang Nan &record.no_buildid_set, 2412baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 2413d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 2414023695d9SStephane Eranian "monitor event in cgroup name only", 2415023695d9SStephane Eranian parse_cgroups), 2416a6205a35SArnaldo Carvalho de Melo OPT_UINTEGER('D', "delay", &record.opts.initial_delay, 24176619a53eSAndi Kleen "ms to wait before starting measurement after program start"), 2418eeb399b5SAdrian Hunter OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"), 2419bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 2420bea03405SNamhyung Kim "user to profile"), 2421a5aabdacSStephane Eranian 2422a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 2423a5aabdacSStephane Eranian "branch any", "sample any taken branches", 2424a5aabdacSStephane Eranian parse_branch_stack), 2425a5aabdacSStephane Eranian 2426a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 2427a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 2428bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 242905484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 243005484298SAndi Kleen "sample by weight (on special events only)"), 2431475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 2432475eeab9SAndi Kleen "sample transaction flags (special events only)"), 24333aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 24343aa5939dSAdrian Hunter "use per-thread mmaps"), 2435bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 2436bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 2437aeea9062SKan Liang " use '-I?' to list register names", parse_intr_regs), 243884c41742SAndi Kleen OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register", 243984c41742SAndi Kleen "sample selected machine registers on interrupt," 2440aeea9062SKan Liang " use '--user-regs=?' to list register names", parse_user_regs), 244185c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 244285c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 2443814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 2444814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 2445814c8c38SPeter Zijlstra parse_clockid), 24462dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 24472dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 2448c0a6de06SAdrian Hunter OPT_STRING_OPTARG(0, "aux-sample", &record.opts.auxtrace_sample_opts, 2449c0a6de06SAdrian Hunter "opts", "sample AUX area", ""), 24503fcb10e4SMark Drayton OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 24519d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 2452f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 2453f3b3614aSHari Bathini "Record namespaces events"), 24548fb4b679SNamhyung Kim OPT_BOOLEAN(0, "all-cgroups", &record.opts.record_cgroup, 24558fb4b679SNamhyung Kim "Record cgroup events"), 2456b757bb09SAdrian Hunter OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events, 2457b757bb09SAdrian Hunter "Record context switch events"), 245885723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 245985723885SJiri Olsa "Configure all used events to run in kernel space.", 246085723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 246185723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 246285723885SJiri Olsa "Configure all used events to run in user space.", 246385723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 246453651b28Syuzhoujian OPT_BOOLEAN(0, "kernel-callchains", &record.opts.kernel_callchains, 246553651b28Syuzhoujian "collect kernel callchains"), 246653651b28Syuzhoujian OPT_BOOLEAN(0, "user-callchains", &record.opts.user_callchains, 246753651b28Syuzhoujian "collect user callchains"), 246871dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 246971dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 247071dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 247171dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 24727efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 24737efe0e03SHe Kuang "file", "vmlinux pathname"), 24746156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 24756156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 2476ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 2477ecfd7a9cSWang Nan "append timestamp to output filename"), 247868588bafSJin Yao OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary, 247968588bafSJin Yao "Record timestamp boundary (time of first/last samples)"), 2480cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 2481c38dab7dSAndi Kleen &record.switch_output.set, "signal or size[BKMG] or time[smhd]", 2482c38dab7dSAndi Kleen "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold", 2483dc0c6127SJiri Olsa "signal"), 2484899e5ffbSArnaldo Carvalho de Melo OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event", 2485899e5ffbSArnaldo Carvalho de Melo "switch output event selector. use 'perf list' to list available events", 2486899e5ffbSArnaldo Carvalho de Melo parse_events_option_new_evlist), 248703724b2eSAndi Kleen OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files, 248803724b2eSAndi Kleen "Limit number of switch output generated files"), 24890aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 24900aab2136SWang Nan "Parse options then exit"), 2491d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 249293f20c0fSAlexey Budankov OPT_CALLBACK_OPTARG(0, "aio", &record.opts, 249393f20c0fSAlexey Budankov &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)", 2494d3d1af6fSAlexey Budankov record__aio_parse), 2495d3d1af6fSAlexey Budankov #endif 2496f4fe11b7SAlexey Budankov OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu", 2497f4fe11b7SAlexey Budankov "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer", 2498f4fe11b7SAlexey Budankov record__parse_affinity), 2499504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 2500504c1ad1SAlexey Budankov OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, 2501504c1ad1SAlexey Budankov "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)", 2502504c1ad1SAlexey Budankov record__parse_comp_level), 2503504c1ad1SAlexey Budankov #endif 25046d575816SJiwei Sun OPT_CALLBACK(0, "max-size", &record.output_max_size, 25056d575816SJiwei Sun "size", "Limit the maximum size of the output file", parse_output_max_size), 2506d99c22eaSStephane Eranian OPT_UINTEGER(0, "num-thread-synthesize", 2507d99c22eaSStephane Eranian &record.opts.nr_threads_synthesize, 2508d99c22eaSStephane Eranian "number of threads to run for event synthesis"), 250986470930SIngo Molnar OPT_END() 251086470930SIngo Molnar }; 251186470930SIngo Molnar 2512e5b2c207SNamhyung Kim struct option *record_options = __record_options; 2513e5b2c207SNamhyung Kim 2514b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 251586470930SIngo Molnar { 2516ef149c25SAdrian Hunter int err; 25178c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 251816ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 251986470930SIngo Molnar 252067230479SArnaldo Carvalho de Melo setlocale(LC_ALL, ""); 252167230479SArnaldo Carvalho de Melo 252248e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 252348e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 252448e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 252548e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 252648e1cab1SWang Nan # undef set_nobuild 252748e1cab1SWang Nan #endif 252848e1cab1SWang Nan 25297efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 25307efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 25317efe0e03SHe Kuang # define REASON "NO_DWARF=1" 25327efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 25337efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 25347efe0e03SHe Kuang # else 25357efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 25367efe0e03SHe Kuang # endif 25377efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 25387efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 25397efe0e03SHe Kuang # undef set_nobuild 25407efe0e03SHe Kuang # undef REASON 25417efe0e03SHe Kuang #endif 25427efe0e03SHe Kuang 25439d2ed645SAlexey Budankov rec->opts.affinity = PERF_AFFINITY_SYS; 25449d2ed645SAlexey Budankov 25450f98b11cSJiri Olsa rec->evlist = evlist__new(); 25463e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 2547361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 2548361c99a6SArnaldo Carvalho de Melo 2549ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 2550ecc4c561SArnaldo Carvalho de Melo if (err) 2551ecc4c561SArnaldo Carvalho de Melo return err; 2552eb853e80SJiri Olsa 2553bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 2554a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 255568ba3235SNamhyung Kim if (quiet) 255668ba3235SNamhyung Kim perf_quiet_option(); 2557483635a9SJiri Olsa 2558483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 2559602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 2560483635a9SJiri Olsa rec->opts.target.system_wide = true; 256186470930SIngo Molnar 2562bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 2563c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 2564c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 2565c7118369SNamhyung Kim 2566023695d9SStephane Eranian } 2567504c1ad1SAlexey Budankov 2568eeb399b5SAdrian Hunter if (rec->opts.kcore) 2569eeb399b5SAdrian Hunter rec->data.is_dir = true; 2570eeb399b5SAdrian Hunter 2571504c1ad1SAlexey Budankov if (rec->opts.comp_level != 0) { 2572504c1ad1SAlexey Budankov pr_debug("Compression enabled, disabling build id collection at the end of the session.\n"); 2573504c1ad1SAlexey Budankov rec->no_buildid = true; 2574504c1ad1SAlexey Budankov } 2575504c1ad1SAlexey Budankov 2576b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 2577b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 2578c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 2579c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 2580c7118369SNamhyung Kim return -EINVAL; 2581b757bb09SAdrian Hunter } 2582023695d9SStephane Eranian 2583cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 2584cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 2585cb4e1ebbSJiri Olsa return -EINVAL; 2586cb4e1ebbSJiri Olsa } 2587cb4e1ebbSJiri Olsa 2588bfacbe3bSJiri Olsa if (rec->switch_output.time) { 2589bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 2590bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 2591bfacbe3bSJiri Olsa } 2592bfacbe3bSJiri Olsa 259303724b2eSAndi Kleen if (rec->switch_output.num_files) { 259403724b2eSAndi Kleen rec->switch_output.filenames = calloc(sizeof(char *), 259503724b2eSAndi Kleen rec->switch_output.num_files); 259603724b2eSAndi Kleen if (!rec->switch_output.filenames) 259703724b2eSAndi Kleen return -EINVAL; 259803724b2eSAndi Kleen } 259903724b2eSAndi Kleen 26001b36c03eSAdrian Hunter /* 26011b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 26021b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 26031b36c03eSAdrian Hunter */ 26041b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 26051b36c03eSAdrian Hunter 26061b36c03eSAdrian Hunter symbol__init(NULL); 26071b36c03eSAdrian Hunter 26088384a260SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS) { 26098384a260SAlexey Budankov rec->affinity_mask.nbits = cpu__max_cpu(); 26108384a260SAlexey Budankov rec->affinity_mask.bits = bitmap_alloc(rec->affinity_mask.nbits); 26118384a260SAlexey Budankov if (!rec->affinity_mask.bits) { 26128384a260SAlexey Budankov pr_err("Failed to allocate thread mask for %zd cpus\n", rec->affinity_mask.nbits); 26138384a260SAlexey Budankov return -ENOMEM; 26148384a260SAlexey Budankov } 26158384a260SAlexey Budankov pr_debug2("thread mask[%zd]: empty\n", rec->affinity_mask.nbits); 26168384a260SAlexey Budankov } 26178384a260SAlexey Budankov 26184b5ea3bdSAdrian Hunter err = record__auxtrace_init(rec); 26191b36c03eSAdrian Hunter if (err) 26201b36c03eSAdrian Hunter goto out; 26211b36c03eSAdrian Hunter 26220aab2136SWang Nan if (dry_run) 26235c01ad60SAdrian Hunter goto out; 26240aab2136SWang Nan 2625d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 2626d7888573SWang Nan if (err) { 2627d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 2628d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 2629d7888573SWang Nan errbuf); 26305c01ad60SAdrian Hunter goto out; 2631d7888573SWang Nan } 2632d7888573SWang Nan 2633ef149c25SAdrian Hunter err = -ENOMEM; 2634ef149c25SAdrian Hunter 26350c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 2636a1ac1d3cSStephane Eranian disable_buildid_cache(); 2637dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 26380c1d46a8SWang Nan /* 26390c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 26400c1d46a8SWang Nan * generation by default to reduce data file switching 26410c1d46a8SWang Nan * overhead. Still generate buildid if they are required 26420c1d46a8SWang Nan * explicitly using 26430c1d46a8SWang Nan * 264460437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 26450c1d46a8SWang Nan * --no-no-buildid-cache 26460c1d46a8SWang Nan * 26470c1d46a8SWang Nan * Following code equals to: 26480c1d46a8SWang Nan * 26490c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 26500c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 26510c1d46a8SWang Nan * disable_buildid_cache(); 26520c1d46a8SWang Nan */ 26530c1d46a8SWang Nan bool disable = true; 26540c1d46a8SWang Nan 26550c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 26560c1d46a8SWang Nan disable = false; 26570c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 26580c1d46a8SWang Nan disable = false; 26590c1d46a8SWang Nan if (disable) { 26600c1d46a8SWang Nan rec->no_buildid = true; 26610c1d46a8SWang Nan rec->no_buildid_cache = true; 26620c1d46a8SWang Nan disable_buildid_cache(); 26630c1d46a8SWang Nan } 26640c1d46a8SWang Nan } 2665655000e7SArnaldo Carvalho de Melo 26664ea648aeSWang Nan if (record.opts.overwrite) 26674ea648aeSWang Nan record.opts.tail_synthesize = true; 26684ea648aeSWang Nan 26696484d2f9SJiri Olsa if (rec->evlist->core.nr_entries == 0 && 26704b4cd503SArnaldo Carvalho de Melo __perf_evlist__add_default(rec->evlist, !record.opts.no_samples) < 0) { 267169aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 2672394c01edSAdrian Hunter goto out; 2673bbd36e5eSPeter Zijlstra } 267486470930SIngo Molnar 267569e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 267669e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 267769e7e5b0SAdrian Hunter 2678602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 267916ad2ffbSNamhyung Kim if (err) { 2680602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 2681c3dec27bSJiri Olsa ui__warning("%s\n", errbuf); 268216ad2ffbSNamhyung Kim } 26834bd0f2d2SNamhyung Kim 2684602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 268516ad2ffbSNamhyung Kim if (err) { 268616ad2ffbSNamhyung Kim int saved_errno = errno; 268716ad2ffbSNamhyung Kim 2688602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 26893780f488SNamhyung Kim ui__error("%s", errbuf); 269016ad2ffbSNamhyung Kim 269116ad2ffbSNamhyung Kim err = -saved_errno; 2692394c01edSAdrian Hunter goto out; 269316ad2ffbSNamhyung Kim } 26940d37aa34SArnaldo Carvalho de Melo 2695ca800068SMengting Zhang /* Enable ignoring missing threads when -u/-p option is defined. */ 2696ca800068SMengting Zhang rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; 269723dc4f15SJiri Olsa 269816ad2ffbSNamhyung Kim err = -ENOMEM; 26993e2be2daSArnaldo Carvalho de Melo if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) 2700dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 270169aad6f1SArnaldo Carvalho de Melo 2702ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 2703ef149c25SAdrian Hunter if (err) 2704394c01edSAdrian Hunter goto out; 2705ef149c25SAdrian Hunter 27066156681bSNamhyung Kim /* 27076156681bSNamhyung Kim * We take all buildids when the file contains 27086156681bSNamhyung Kim * AUX area tracing data because we do not decode the 27096156681bSNamhyung Kim * trace because it would take too long. 27106156681bSNamhyung Kim */ 27116156681bSNamhyung Kim if (rec->opts.full_auxtrace) 27126156681bSNamhyung Kim rec->buildid_all = true; 27136156681bSNamhyung Kim 2714b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 271539d17dacSArnaldo Carvalho de Melo err = -EINVAL; 2716394c01edSAdrian Hunter goto out; 27177e4ff9e3SMike Galbraith } 27187e4ff9e3SMike Galbraith 271993f20c0fSAlexey Budankov if (rec->opts.nr_cblocks > nr_cblocks_max) 272093f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_max; 27215d7f4116SAlexey Budankov pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks); 2722d3d1af6fSAlexey Budankov 27239d2ed645SAlexey Budankov pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]); 2724470530bbSAlexey Budankov pr_debug("mmap flush: %d\n", rec->opts.mmap_flush); 27259d2ed645SAlexey Budankov 272651255a8aSAlexey Budankov if (rec->opts.comp_level > comp_level_max) 272751255a8aSAlexey Budankov rec->opts.comp_level = comp_level_max; 272851255a8aSAlexey Budankov pr_debug("comp level: %d\n", rec->opts.comp_level); 272951255a8aSAlexey Budankov 2730d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 2731394c01edSAdrian Hunter out: 27328384a260SAlexey Budankov bitmap_free(rec->affinity_mask.bits); 2733c12995a5SJiri Olsa evlist__delete(rec->evlist); 2734d65a458bSArnaldo Carvalho de Melo symbol__exit(); 2735ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 273639d17dacSArnaldo Carvalho de Melo return err; 273786470930SIngo Molnar } 27382dd6d8a1SAdrian Hunter 27392dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 27402dd6d8a1SAdrian Hunter { 2741dc0c6127SJiri Olsa struct record *rec = &record; 2742dc0c6127SJiri Olsa 27435f9cf599SWang Nan if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 27445f9cf599SWang Nan trigger_hit(&auxtrace_snapshot_trigger); 27452dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 1; 27465f9cf599SWang Nan if (auxtrace_record__snapshot_start(record.itr)) 27475f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 27485f9cf599SWang Nan } 27493c1cb7e3SWang Nan 2750dc0c6127SJiri Olsa if (switch_output_signal(rec)) 27513c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 27522dd6d8a1SAdrian Hunter } 2753bfacbe3bSJiri Olsa 2754bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 2755bfacbe3bSJiri Olsa { 2756bfacbe3bSJiri Olsa struct record *rec = &record; 2757bfacbe3bSJiri Olsa 2758bfacbe3bSJiri Olsa if (switch_output_time(rec)) 2759bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 2760bfacbe3bSJiri Olsa } 2761