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 1186470930SIngo Molnar #include "perf.h" 1286470930SIngo Molnar 136122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h" 1486470930SIngo Molnar #include "util/util.h" 154b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1686470930SIngo Molnar #include "util/parse-events.h" 1741840d21STaeung Song #include "util/config.h" 1886470930SIngo Molnar 198f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h" 20f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h" 217c6a1c65SPeter Zijlstra #include "util/header.h" 2266e274f3SFrederic Weisbecker #include "util/event.h" 23361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h" 2469aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 258f28827aSFrederic Weisbecker #include "util/debug.h" 2694c744b6SArnaldo Carvalho de Melo #include "util/session.h" 2745694aa7SArnaldo Carvalho de Melo #include "util/tool.h" 288d06367fSArnaldo Carvalho de Melo #include "util/symbol.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" 3771dc2326SWang Nan #include "util/llvm-utils.h" 388690a2a7SWang Nan #include "util/bpf-loader.h" 395f9cf599SWang Nan #include "util/trigger.h" 40a074865eSWang Nan #include "util/perf-hooks.h" 41f13de660SAlexey Budankov #include "util/cpu-set-sched.h" 42c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h" 4358db1d6eSArnaldo Carvalho de Melo #include "util/units.h" 447b612e29SSong Liu #include "util/bpf-event.h" 45d8871ea7SWang Nan #include "asm/bug.h" 467c6a1c65SPeter Zijlstra 47a43783aeSArnaldo Carvalho de Melo #include <errno.h> 48fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 4967230479SArnaldo Carvalho de Melo #include <locale.h> 504208735dSArnaldo Carvalho de Melo #include <poll.h> 5186470930SIngo Molnar #include <unistd.h> 5286470930SIngo Molnar #include <sched.h> 539607ad3aSArnaldo Carvalho de Melo #include <signal.h> 54a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 554208735dSArnaldo Carvalho de Melo #include <sys/wait.h> 560693e680SArnaldo Carvalho de Melo #include <linux/time64.h> 5778da39faSBernhard Rosenkraenzer 581b43b704SJiri Olsa struct switch_output { 59dc0c6127SJiri Olsa bool enabled; 601b43b704SJiri Olsa bool signal; 61dc0c6127SJiri Olsa unsigned long size; 62bfacbe3bSJiri Olsa unsigned long time; 63cb4e1ebbSJiri Olsa const char *str; 64cb4e1ebbSJiri Olsa bool set; 651b43b704SJiri Olsa }; 661b43b704SJiri Olsa 678c6f45a7SArnaldo Carvalho de Melo struct record { 6845694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 69b4006796SArnaldo Carvalho de Melo struct record_opts opts; 70d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 718ceb41d7SJiri Olsa struct perf_data data; 72ef149c25SAdrian Hunter struct auxtrace_record *itr; 73d20deb64SArnaldo Carvalho de Melo struct perf_evlist *evlist; 74d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 75d20deb64SArnaldo Carvalho de Melo int realtime_prio; 76d20deb64SArnaldo Carvalho de Melo bool no_buildid; 77d2db9a98SWang Nan bool no_buildid_set; 78d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 79d2db9a98SWang Nan bool no_buildid_cache_set; 806156681bSNamhyung Kim bool buildid_all; 81ecfd7a9cSWang Nan bool timestamp_filename; 8268588bafSJin Yao bool timestamp_boundary; 831b43b704SJiri Olsa struct switch_output switch_output; 849f065194SYang Shi unsigned long long samples; 859d2ed645SAlexey Budankov cpu_set_t affinity_mask; 860f82ebc4SArnaldo Carvalho de Melo }; 8786470930SIngo Molnar 88dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 89dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 90dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 91dc0c6127SJiri Olsa 929d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = { 939d2ed645SAlexey Budankov "SYS", "NODE", "CPU" 949d2ed645SAlexey Budankov }; 959d2ed645SAlexey Budankov 96dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 97dc0c6127SJiri Olsa { 98dc0c6127SJiri Olsa return rec->switch_output.signal && 99dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 100dc0c6127SJiri Olsa } 101dc0c6127SJiri Olsa 102dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 103dc0c6127SJiri Olsa { 104dc0c6127SJiri Olsa return rec->switch_output.size && 105dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 106dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 107dc0c6127SJiri Olsa } 108dc0c6127SJiri Olsa 109bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 110bfacbe3bSJiri Olsa { 111bfacbe3bSJiri Olsa return rec->switch_output.time && 112bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 113bfacbe3bSJiri Olsa } 114bfacbe3bSJiri Olsa 115ded2b8feSJiri Olsa static int record__write(struct record *rec, struct perf_mmap *map __maybe_unused, 116ded2b8feSJiri Olsa void *bf, size_t size) 117f5970550SPeter Zijlstra { 118ded2b8feSJiri Olsa struct perf_data_file *file = &rec->session->data->file; 119ded2b8feSJiri Olsa 120ded2b8feSJiri Olsa if (perf_data_file__write(file, bf, size) < 0) { 1214f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 1228d3eca20SDavid Ahern return -1; 1238d3eca20SDavid Ahern } 124f5970550SPeter Zijlstra 125cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 126dc0c6127SJiri Olsa 127dc0c6127SJiri Olsa if (switch_output_size(rec)) 128dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 129dc0c6127SJiri Olsa 1308d3eca20SDavid Ahern return 0; 131f5970550SPeter Zijlstra } 132f5970550SPeter Zijlstra 133d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 134d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd, 135d3d1af6fSAlexey Budankov void *buf, size_t size, off_t off) 136d3d1af6fSAlexey Budankov { 137d3d1af6fSAlexey Budankov int rc; 138d3d1af6fSAlexey Budankov 139d3d1af6fSAlexey Budankov cblock->aio_fildes = trace_fd; 140d3d1af6fSAlexey Budankov cblock->aio_buf = buf; 141d3d1af6fSAlexey Budankov cblock->aio_nbytes = size; 142d3d1af6fSAlexey Budankov cblock->aio_offset = off; 143d3d1af6fSAlexey Budankov cblock->aio_sigevent.sigev_notify = SIGEV_NONE; 144d3d1af6fSAlexey Budankov 145d3d1af6fSAlexey Budankov do { 146d3d1af6fSAlexey Budankov rc = aio_write(cblock); 147d3d1af6fSAlexey Budankov if (rc == 0) { 148d3d1af6fSAlexey Budankov break; 149d3d1af6fSAlexey Budankov } else if (errno != EAGAIN) { 150d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 151d3d1af6fSAlexey Budankov pr_err("failed to queue perf data, error: %m\n"); 152d3d1af6fSAlexey Budankov break; 153d3d1af6fSAlexey Budankov } 154d3d1af6fSAlexey Budankov } while (1); 155d3d1af6fSAlexey Budankov 156d3d1af6fSAlexey Budankov return rc; 157d3d1af6fSAlexey Budankov } 158d3d1af6fSAlexey Budankov 159d3d1af6fSAlexey Budankov static int record__aio_complete(struct perf_mmap *md, struct aiocb *cblock) 160d3d1af6fSAlexey Budankov { 161d3d1af6fSAlexey Budankov void *rem_buf; 162d3d1af6fSAlexey Budankov off_t rem_off; 163d3d1af6fSAlexey Budankov size_t rem_size; 164d3d1af6fSAlexey Budankov int rc, aio_errno; 165d3d1af6fSAlexey Budankov ssize_t aio_ret, written; 166d3d1af6fSAlexey Budankov 167d3d1af6fSAlexey Budankov aio_errno = aio_error(cblock); 168d3d1af6fSAlexey Budankov if (aio_errno == EINPROGRESS) 169d3d1af6fSAlexey Budankov return 0; 170d3d1af6fSAlexey Budankov 171d3d1af6fSAlexey Budankov written = aio_ret = aio_return(cblock); 172d3d1af6fSAlexey Budankov if (aio_ret < 0) { 173d3d1af6fSAlexey Budankov if (aio_errno != EINTR) 174d3d1af6fSAlexey Budankov pr_err("failed to write perf data, error: %m\n"); 175d3d1af6fSAlexey Budankov written = 0; 176d3d1af6fSAlexey Budankov } 177d3d1af6fSAlexey Budankov 178d3d1af6fSAlexey Budankov rem_size = cblock->aio_nbytes - written; 179d3d1af6fSAlexey Budankov 180d3d1af6fSAlexey Budankov if (rem_size == 0) { 181d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 182d3d1af6fSAlexey Budankov /* 183d3d1af6fSAlexey Budankov * md->refcount is incremented in perf_mmap__push() for 184d3d1af6fSAlexey Budankov * every enqueued aio write request so decrement it because 185d3d1af6fSAlexey Budankov * the request is now complete. 186d3d1af6fSAlexey Budankov */ 187d3d1af6fSAlexey Budankov perf_mmap__put(md); 188d3d1af6fSAlexey Budankov rc = 1; 189d3d1af6fSAlexey Budankov } else { 190d3d1af6fSAlexey Budankov /* 191d3d1af6fSAlexey Budankov * aio write request may require restart with the 192d3d1af6fSAlexey Budankov * reminder if the kernel didn't write whole 193d3d1af6fSAlexey Budankov * chunk at once. 194d3d1af6fSAlexey Budankov */ 195d3d1af6fSAlexey Budankov rem_off = cblock->aio_offset + written; 196d3d1af6fSAlexey Budankov rem_buf = (void *)(cblock->aio_buf + written); 197d3d1af6fSAlexey Budankov record__aio_write(cblock, cblock->aio_fildes, 198d3d1af6fSAlexey Budankov rem_buf, rem_size, rem_off); 199d3d1af6fSAlexey Budankov rc = 0; 200d3d1af6fSAlexey Budankov } 201d3d1af6fSAlexey Budankov 202d3d1af6fSAlexey Budankov return rc; 203d3d1af6fSAlexey Budankov } 204d3d1af6fSAlexey Budankov 20593f20c0fSAlexey Budankov static int record__aio_sync(struct perf_mmap *md, bool sync_all) 206d3d1af6fSAlexey Budankov { 20793f20c0fSAlexey Budankov struct aiocb **aiocb = md->aio.aiocb; 20893f20c0fSAlexey Budankov struct aiocb *cblocks = md->aio.cblocks; 209d3d1af6fSAlexey Budankov struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */ 21093f20c0fSAlexey Budankov int i, do_suspend; 211d3d1af6fSAlexey Budankov 212d3d1af6fSAlexey Budankov do { 21393f20c0fSAlexey Budankov do_suspend = 0; 21493f20c0fSAlexey Budankov for (i = 0; i < md->aio.nr_cblocks; ++i) { 21593f20c0fSAlexey Budankov if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) { 21693f20c0fSAlexey Budankov if (sync_all) 21793f20c0fSAlexey Budankov aiocb[i] = NULL; 21893f20c0fSAlexey Budankov else 21993f20c0fSAlexey Budankov return i; 22093f20c0fSAlexey Budankov } else { 22193f20c0fSAlexey Budankov /* 22293f20c0fSAlexey Budankov * Started aio write is not complete yet 22393f20c0fSAlexey Budankov * so it has to be waited before the 22493f20c0fSAlexey Budankov * next allocation. 22593f20c0fSAlexey Budankov */ 22693f20c0fSAlexey Budankov aiocb[i] = &cblocks[i]; 22793f20c0fSAlexey Budankov do_suspend = 1; 22893f20c0fSAlexey Budankov } 22993f20c0fSAlexey Budankov } 23093f20c0fSAlexey Budankov if (!do_suspend) 23193f20c0fSAlexey Budankov return -1; 232d3d1af6fSAlexey Budankov 23393f20c0fSAlexey Budankov while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) { 234d3d1af6fSAlexey Budankov if (!(errno == EAGAIN || errno == EINTR)) 235d3d1af6fSAlexey Budankov pr_err("failed to sync perf data, error: %m\n"); 236d3d1af6fSAlexey Budankov } 237d3d1af6fSAlexey Budankov } while (1); 238d3d1af6fSAlexey Budankov } 239d3d1af6fSAlexey Budankov 240d3d1af6fSAlexey Budankov static int record__aio_pushfn(void *to, struct aiocb *cblock, void *bf, size_t size, off_t off) 241d3d1af6fSAlexey Budankov { 242d3d1af6fSAlexey Budankov struct record *rec = to; 243d3d1af6fSAlexey Budankov int ret, trace_fd = rec->session->data->file.fd; 244d3d1af6fSAlexey Budankov 245d3d1af6fSAlexey Budankov rec->samples++; 246d3d1af6fSAlexey Budankov 247d3d1af6fSAlexey Budankov ret = record__aio_write(cblock, trace_fd, bf, size, off); 248d3d1af6fSAlexey Budankov if (!ret) { 249d3d1af6fSAlexey Budankov rec->bytes_written += size; 250d3d1af6fSAlexey Budankov if (switch_output_size(rec)) 251d3d1af6fSAlexey Budankov trigger_hit(&switch_output_trigger); 252d3d1af6fSAlexey Budankov } 253d3d1af6fSAlexey Budankov 254d3d1af6fSAlexey Budankov return ret; 255d3d1af6fSAlexey Budankov } 256d3d1af6fSAlexey Budankov 257d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd) 258d3d1af6fSAlexey Budankov { 259d3d1af6fSAlexey Budankov return lseek(trace_fd, 0, SEEK_CUR); 260d3d1af6fSAlexey Budankov } 261d3d1af6fSAlexey Budankov 262d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos) 263d3d1af6fSAlexey Budankov { 264d3d1af6fSAlexey Budankov lseek(trace_fd, pos, SEEK_SET); 265d3d1af6fSAlexey Budankov } 266d3d1af6fSAlexey Budankov 267d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec) 268d3d1af6fSAlexey Budankov { 269d3d1af6fSAlexey Budankov int i; 270d3d1af6fSAlexey Budankov struct perf_evlist *evlist = rec->evlist; 271d3d1af6fSAlexey Budankov struct perf_mmap *maps = evlist->mmap; 272d3d1af6fSAlexey Budankov 273d3d1af6fSAlexey Budankov if (!rec->opts.nr_cblocks) 274d3d1af6fSAlexey Budankov return; 275d3d1af6fSAlexey Budankov 276d3d1af6fSAlexey Budankov for (i = 0; i < evlist->nr_mmaps; i++) { 277d3d1af6fSAlexey Budankov struct perf_mmap *map = &maps[i]; 278d3d1af6fSAlexey Budankov 279d3d1af6fSAlexey Budankov if (map->base) 28093f20c0fSAlexey Budankov record__aio_sync(map, true); 281d3d1af6fSAlexey Budankov } 282d3d1af6fSAlexey Budankov } 283d3d1af6fSAlexey Budankov 284d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1; 28593f20c0fSAlexey Budankov static int nr_cblocks_max = 4; 286d3d1af6fSAlexey Budankov 287d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt, 28893f20c0fSAlexey Budankov const char *str, 289d3d1af6fSAlexey Budankov int unset) 290d3d1af6fSAlexey Budankov { 291d3d1af6fSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 292d3d1af6fSAlexey Budankov 29393f20c0fSAlexey Budankov if (unset) { 294d3d1af6fSAlexey Budankov opts->nr_cblocks = 0; 29593f20c0fSAlexey Budankov } else { 29693f20c0fSAlexey Budankov if (str) 29793f20c0fSAlexey Budankov opts->nr_cblocks = strtol(str, NULL, 0); 29893f20c0fSAlexey Budankov if (!opts->nr_cblocks) 299d3d1af6fSAlexey Budankov opts->nr_cblocks = nr_cblocks_default; 30093f20c0fSAlexey Budankov } 301d3d1af6fSAlexey Budankov 302d3d1af6fSAlexey Budankov return 0; 303d3d1af6fSAlexey Budankov } 304d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */ 30593f20c0fSAlexey Budankov static int nr_cblocks_max = 0; 30693f20c0fSAlexey Budankov 30793f20c0fSAlexey Budankov static int record__aio_sync(struct perf_mmap *md __maybe_unused, bool sync_all __maybe_unused) 308d3d1af6fSAlexey Budankov { 30993f20c0fSAlexey Budankov return -1; 310d3d1af6fSAlexey Budankov } 311d3d1af6fSAlexey Budankov 312d3d1af6fSAlexey Budankov static int record__aio_pushfn(void *to __maybe_unused, struct aiocb *cblock __maybe_unused, 313d3d1af6fSAlexey Budankov void *bf __maybe_unused, size_t size __maybe_unused, off_t off __maybe_unused) 314d3d1af6fSAlexey Budankov { 315d3d1af6fSAlexey Budankov return -1; 316d3d1af6fSAlexey Budankov } 317d3d1af6fSAlexey Budankov 318d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused) 319d3d1af6fSAlexey Budankov { 320d3d1af6fSAlexey Budankov return -1; 321d3d1af6fSAlexey Budankov } 322d3d1af6fSAlexey Budankov 323d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused) 324d3d1af6fSAlexey Budankov { 325d3d1af6fSAlexey Budankov } 326d3d1af6fSAlexey Budankov 327d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused) 328d3d1af6fSAlexey Budankov { 329d3d1af6fSAlexey Budankov } 330d3d1af6fSAlexey Budankov #endif 331d3d1af6fSAlexey Budankov 332d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec) 333d3d1af6fSAlexey Budankov { 334d3d1af6fSAlexey Budankov return rec->opts.nr_cblocks > 0; 335d3d1af6fSAlexey Budankov } 336d3d1af6fSAlexey Budankov 33745694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 338d20deb64SArnaldo Carvalho de Melo union perf_event *event, 3391d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 3401d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 341234fbbf5SArnaldo Carvalho de Melo { 3428c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 343ded2b8feSJiri Olsa return record__write(rec, NULL, event, event->header.size); 344234fbbf5SArnaldo Carvalho de Melo } 345234fbbf5SArnaldo Carvalho de Melo 346ded2b8feSJiri Olsa static int record__pushfn(struct perf_mmap *map, void *to, void *bf, size_t size) 347d37f1586SArnaldo Carvalho de Melo { 348d37f1586SArnaldo Carvalho de Melo struct record *rec = to; 349d37f1586SArnaldo Carvalho de Melo 350d37f1586SArnaldo Carvalho de Melo rec->samples++; 351ded2b8feSJiri Olsa return record__write(rec, map, bf, size); 352d37f1586SArnaldo Carvalho de Melo } 353d37f1586SArnaldo Carvalho de Melo 3542dd6d8a1SAdrian Hunter static volatile int done; 3552dd6d8a1SAdrian Hunter static volatile int signr = -1; 3562dd6d8a1SAdrian Hunter static volatile int child_finished; 357c0bdc1c4SWang Nan 3582dd6d8a1SAdrian Hunter static void sig_handler(int sig) 3592dd6d8a1SAdrian Hunter { 3602dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 3612dd6d8a1SAdrian Hunter child_finished = 1; 3622dd6d8a1SAdrian Hunter else 3632dd6d8a1SAdrian Hunter signr = sig; 3642dd6d8a1SAdrian Hunter 3652dd6d8a1SAdrian Hunter done = 1; 3662dd6d8a1SAdrian Hunter } 3672dd6d8a1SAdrian Hunter 368a074865eSWang Nan static void sigsegv_handler(int sig) 369a074865eSWang Nan { 370a074865eSWang Nan perf_hooks__recover(); 371a074865eSWang Nan sighandler_dump_stack(sig); 372a074865eSWang Nan } 373a074865eSWang Nan 3742dd6d8a1SAdrian Hunter static void record__sig_exit(void) 3752dd6d8a1SAdrian Hunter { 3762dd6d8a1SAdrian Hunter if (signr == -1) 3772dd6d8a1SAdrian Hunter return; 3782dd6d8a1SAdrian Hunter 3792dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 3802dd6d8a1SAdrian Hunter raise(signr); 3812dd6d8a1SAdrian Hunter } 3822dd6d8a1SAdrian Hunter 383e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 384e31f0d01SAdrian Hunter 385ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 386ded2b8feSJiri Olsa struct perf_mmap *map, 387ef149c25SAdrian Hunter union perf_event *event, void *data1, 388ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 389ef149c25SAdrian Hunter { 390ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 3918ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 392ef149c25SAdrian Hunter size_t padding; 393ef149c25SAdrian Hunter u8 pad[8] = {0}; 394ef149c25SAdrian Hunter 3958ceb41d7SJiri Olsa if (!perf_data__is_pipe(data)) { 39699fa2984SAdrian Hunter off_t file_offset; 3978ceb41d7SJiri Olsa int fd = perf_data__fd(data); 39899fa2984SAdrian Hunter int err; 39999fa2984SAdrian Hunter 40099fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 40199fa2984SAdrian Hunter if (file_offset == -1) 40299fa2984SAdrian Hunter return -1; 40399fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 40499fa2984SAdrian Hunter event, file_offset); 40599fa2984SAdrian Hunter if (err) 40699fa2984SAdrian Hunter return err; 40799fa2984SAdrian Hunter } 40899fa2984SAdrian Hunter 409ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 410ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 411ef149c25SAdrian Hunter if (padding) 412ef149c25SAdrian Hunter padding = 8 - padding; 413ef149c25SAdrian Hunter 414ded2b8feSJiri Olsa record__write(rec, map, event, event->header.size); 415ded2b8feSJiri Olsa record__write(rec, map, data1, len1); 416ef149c25SAdrian Hunter if (len2) 417ded2b8feSJiri Olsa record__write(rec, map, data2, len2); 418ded2b8feSJiri Olsa record__write(rec, map, &pad, padding); 419ef149c25SAdrian Hunter 420ef149c25SAdrian Hunter return 0; 421ef149c25SAdrian Hunter } 422ef149c25SAdrian Hunter 423ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 424e035f4caSJiri Olsa struct perf_mmap *map) 425ef149c25SAdrian Hunter { 426ef149c25SAdrian Hunter int ret; 427ef149c25SAdrian Hunter 428e035f4caSJiri Olsa ret = auxtrace_mmap__read(map, rec->itr, &rec->tool, 429ef149c25SAdrian Hunter record__process_auxtrace); 430ef149c25SAdrian Hunter if (ret < 0) 431ef149c25SAdrian Hunter return ret; 432ef149c25SAdrian Hunter 433ef149c25SAdrian Hunter if (ret) 434ef149c25SAdrian Hunter rec->samples++; 435ef149c25SAdrian Hunter 436ef149c25SAdrian Hunter return 0; 437ef149c25SAdrian Hunter } 438ef149c25SAdrian Hunter 4392dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 440e035f4caSJiri Olsa struct perf_mmap *map) 4412dd6d8a1SAdrian Hunter { 4422dd6d8a1SAdrian Hunter int ret; 4432dd6d8a1SAdrian Hunter 444e035f4caSJiri Olsa ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool, 4452dd6d8a1SAdrian Hunter record__process_auxtrace, 4462dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 4472dd6d8a1SAdrian Hunter if (ret < 0) 4482dd6d8a1SAdrian Hunter return ret; 4492dd6d8a1SAdrian Hunter 4502dd6d8a1SAdrian Hunter if (ret) 4512dd6d8a1SAdrian Hunter rec->samples++; 4522dd6d8a1SAdrian Hunter 4532dd6d8a1SAdrian Hunter return 0; 4542dd6d8a1SAdrian Hunter } 4552dd6d8a1SAdrian Hunter 4562dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 4572dd6d8a1SAdrian Hunter { 4582dd6d8a1SAdrian Hunter int i; 4592dd6d8a1SAdrian Hunter int rc = 0; 4602dd6d8a1SAdrian Hunter 4612dd6d8a1SAdrian Hunter for (i = 0; i < rec->evlist->nr_mmaps; i++) { 462e035f4caSJiri Olsa struct perf_mmap *map = &rec->evlist->mmap[i]; 4632dd6d8a1SAdrian Hunter 464e035f4caSJiri Olsa if (!map->auxtrace_mmap.base) 4652dd6d8a1SAdrian Hunter continue; 4662dd6d8a1SAdrian Hunter 467e035f4caSJiri Olsa if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) { 4682dd6d8a1SAdrian Hunter rc = -1; 4692dd6d8a1SAdrian Hunter goto out; 4702dd6d8a1SAdrian Hunter } 4712dd6d8a1SAdrian Hunter } 4722dd6d8a1SAdrian Hunter out: 4732dd6d8a1SAdrian Hunter return rc; 4742dd6d8a1SAdrian Hunter } 4752dd6d8a1SAdrian Hunter 4762dd6d8a1SAdrian Hunter static void record__read_auxtrace_snapshot(struct record *rec) 4772dd6d8a1SAdrian Hunter { 4782dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 4792dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 4805f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 4812dd6d8a1SAdrian Hunter } else { 4825f9cf599SWang Nan if (auxtrace_record__snapshot_finish(rec->itr)) 4835f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 4845f9cf599SWang Nan else 4855f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 4862dd6d8a1SAdrian Hunter } 4872dd6d8a1SAdrian Hunter } 4882dd6d8a1SAdrian Hunter 4894b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec) 4904b5ea3bdSAdrian Hunter { 4914b5ea3bdSAdrian Hunter int err; 4924b5ea3bdSAdrian Hunter 4934b5ea3bdSAdrian Hunter if (!rec->itr) { 4944b5ea3bdSAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 4954b5ea3bdSAdrian Hunter if (err) 4964b5ea3bdSAdrian Hunter return err; 4974b5ea3bdSAdrian Hunter } 4984b5ea3bdSAdrian Hunter 4994b5ea3bdSAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 5004b5ea3bdSAdrian Hunter rec->opts.auxtrace_snapshot_opts); 5014b5ea3bdSAdrian Hunter if (err) 5024b5ea3bdSAdrian Hunter return err; 5034b5ea3bdSAdrian Hunter 5044b5ea3bdSAdrian Hunter return auxtrace_parse_filters(rec->evlist); 5054b5ea3bdSAdrian Hunter } 5064b5ea3bdSAdrian Hunter 507e31f0d01SAdrian Hunter #else 508e31f0d01SAdrian Hunter 509e31f0d01SAdrian Hunter static inline 510e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 511e035f4caSJiri Olsa struct perf_mmap *map __maybe_unused) 512e31f0d01SAdrian Hunter { 513e31f0d01SAdrian Hunter return 0; 514e31f0d01SAdrian Hunter } 515e31f0d01SAdrian Hunter 5162dd6d8a1SAdrian Hunter static inline 5172dd6d8a1SAdrian Hunter void record__read_auxtrace_snapshot(struct record *rec __maybe_unused) 5182dd6d8a1SAdrian Hunter { 5192dd6d8a1SAdrian Hunter } 5202dd6d8a1SAdrian Hunter 5212dd6d8a1SAdrian Hunter static inline 5222dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 5232dd6d8a1SAdrian Hunter { 5242dd6d8a1SAdrian Hunter return 0; 5252dd6d8a1SAdrian Hunter } 5262dd6d8a1SAdrian Hunter 5274b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused) 5284b5ea3bdSAdrian Hunter { 5294b5ea3bdSAdrian Hunter return 0; 5304b5ea3bdSAdrian Hunter } 5314b5ea3bdSAdrian Hunter 532e31f0d01SAdrian Hunter #endif 533e31f0d01SAdrian Hunter 534cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 535cda57a8cSWang Nan struct perf_evlist *evlist) 536cda57a8cSWang Nan { 537cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 538cda57a8cSWang Nan char msg[512]; 539cda57a8cSWang Nan 540f13de660SAlexey Budankov if (opts->affinity != PERF_AFFINITY_SYS) 541f13de660SAlexey Budankov cpu__setup_cpunode_map(); 542f13de660SAlexey Budankov 5437a276ff6SWang Nan if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, 544cda57a8cSWang Nan opts->auxtrace_mmap_pages, 5459d2ed645SAlexey Budankov opts->auxtrace_snapshot_mode, 5469d2ed645SAlexey Budankov opts->nr_cblocks, opts->affinity) < 0) { 547cda57a8cSWang Nan if (errno == EPERM) { 548cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 549cda57a8cSWang Nan "Consider increasing " 550cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 551cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 552cda57a8cSWang Nan "(current value: %u,%u)\n", 553cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 554cda57a8cSWang Nan return -errno; 555cda57a8cSWang Nan } else { 556cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 557c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 558cda57a8cSWang Nan if (errno) 559cda57a8cSWang Nan return -errno; 560cda57a8cSWang Nan else 561cda57a8cSWang Nan return -EINVAL; 562cda57a8cSWang Nan } 563cda57a8cSWang Nan } 564cda57a8cSWang Nan return 0; 565cda57a8cSWang Nan } 566cda57a8cSWang Nan 567cda57a8cSWang Nan static int record__mmap(struct record *rec) 568cda57a8cSWang Nan { 569cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 570cda57a8cSWang Nan } 571cda57a8cSWang Nan 5728c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 573dd7927f4SArnaldo Carvalho de Melo { 574d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 5756a4bb04cSJiri Olsa struct perf_evsel *pos; 576d20deb64SArnaldo Carvalho de Melo struct perf_evlist *evlist = rec->evlist; 577d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 578b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 5798d3eca20SDavid Ahern int rc = 0; 580dd7927f4SArnaldo Carvalho de Melo 581d3dbf43cSArnaldo Carvalho de Melo /* 582d3dbf43cSArnaldo Carvalho de Melo * For initial_delay we need to add a dummy event so that we can track 583d3dbf43cSArnaldo Carvalho de Melo * PERF_RECORD_MMAP while we wait for the initial delay to enable the 584d3dbf43cSArnaldo Carvalho de Melo * real events, the ones asked by the user. 585d3dbf43cSArnaldo Carvalho de Melo */ 586d3dbf43cSArnaldo Carvalho de Melo if (opts->initial_delay) { 587d3dbf43cSArnaldo Carvalho de Melo if (perf_evlist__add_dummy(evlist)) 588d3dbf43cSArnaldo Carvalho de Melo return -ENOMEM; 589d3dbf43cSArnaldo Carvalho de Melo 590d3dbf43cSArnaldo Carvalho de Melo pos = perf_evlist__first(evlist); 591d3dbf43cSArnaldo Carvalho de Melo pos->tracking = 0; 592d3dbf43cSArnaldo Carvalho de Melo pos = perf_evlist__last(evlist); 593d3dbf43cSArnaldo Carvalho de Melo pos->tracking = 1; 594d3dbf43cSArnaldo Carvalho de Melo pos->attr.enable_on_exec = 1; 595d3dbf43cSArnaldo Carvalho de Melo } 596d3dbf43cSArnaldo Carvalho de Melo 597e68ae9cfSArnaldo Carvalho de Melo perf_evlist__config(evlist, opts, &callchain_param); 598cac21425SJiri Olsa 599e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 6003da297a6SIngo Molnar try_again: 601d988d5eeSKan Liang if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) { 60256e52e85SArnaldo Carvalho de Melo if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) { 603bb963e16SNamhyung Kim if (verbose > 0) 604c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 6053da297a6SIngo Molnar goto try_again; 6063da297a6SIngo Molnar } 607cf99ad14SAndi Kleen if ((errno == EINVAL || errno == EBADF) && 608cf99ad14SAndi Kleen pos->leader != pos && 609cf99ad14SAndi Kleen pos->weak_group) { 610cf99ad14SAndi Kleen pos = perf_evlist__reset_weak_group(evlist, pos); 611cf99ad14SAndi Kleen goto try_again; 612cf99ad14SAndi Kleen } 61356e52e85SArnaldo Carvalho de Melo rc = -errno; 61456e52e85SArnaldo Carvalho de Melo perf_evsel__open_strerror(pos, &opts->target, 61556e52e85SArnaldo Carvalho de Melo errno, msg, sizeof(msg)); 61656e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 6178d3eca20SDavid Ahern goto out; 6187c6a1c65SPeter Zijlstra } 619bfd8f72cSAndi Kleen 620bfd8f72cSAndi Kleen pos->supported = true; 6217c6a1c65SPeter Zijlstra } 6227c6a1c65SPeter Zijlstra 62323d4aad4SArnaldo Carvalho de Melo if (perf_evlist__apply_filters(evlist, &pos)) { 62462d94b00SArnaldo Carvalho de Melo pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 62523d4aad4SArnaldo Carvalho de Melo pos->filter, perf_evsel__name(pos), errno, 626c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 6278d3eca20SDavid Ahern rc = -1; 6288d3eca20SDavid Ahern goto out; 6290a102479SFrederic Weisbecker } 6300a102479SFrederic Weisbecker 631cda57a8cSWang Nan rc = record__mmap(rec); 632cda57a8cSWang Nan if (rc) 6338d3eca20SDavid Ahern goto out; 6340a27d7f9SArnaldo Carvalho de Melo 635a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 6367b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 6378d3eca20SDavid Ahern out: 6388d3eca20SDavid Ahern return rc; 639a91e5431SArnaldo Carvalho de Melo } 640a91e5431SArnaldo Carvalho de Melo 641e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 642e3d59112SNamhyung Kim union perf_event *event, 643e3d59112SNamhyung Kim struct perf_sample *sample, 644e3d59112SNamhyung Kim struct perf_evsel *evsel, 645e3d59112SNamhyung Kim struct machine *machine) 646e3d59112SNamhyung Kim { 647e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 648e3d59112SNamhyung Kim 64968588bafSJin Yao if (rec->evlist->first_sample_time == 0) 65068588bafSJin Yao rec->evlist->first_sample_time = sample->time; 651e3d59112SNamhyung Kim 65268588bafSJin Yao rec->evlist->last_sample_time = sample->time; 65368588bafSJin Yao 65468588bafSJin Yao if (rec->buildid_all) 65568588bafSJin Yao return 0; 65668588bafSJin Yao 65768588bafSJin Yao rec->samples++; 658e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 659e3d59112SNamhyung Kim } 660e3d59112SNamhyung Kim 6618c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 6626122e4e4SArnaldo Carvalho de Melo { 663f5fc1412SJiri Olsa struct perf_session *session = rec->session; 6646122e4e4SArnaldo Carvalho de Melo 665*45112e89SJiri Olsa if (perf_data__size(&rec->data) == 0) 6669f591fd7SArnaldo Carvalho de Melo return 0; 6679f591fd7SArnaldo Carvalho de Melo 66800dc8657SNamhyung Kim /* 66900dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 67000dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 67100dc8657SNamhyung Kim * we prefer the vmlinux path like 67200dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 67300dc8657SNamhyung Kim * 67400dc8657SNamhyung Kim * rather than build-id path (in debug directory). 67500dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 67600dc8657SNamhyung Kim */ 67700dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 67800dc8657SNamhyung Kim 6796156681bSNamhyung Kim /* 6806156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 68168588bafSJin Yao * so no need to process samples. But if timestamp_boundary is enabled, 68268588bafSJin Yao * it still needs to walk on all samples to get the timestamps of 68368588bafSJin Yao * first/last samples. 6846156681bSNamhyung Kim */ 68568588bafSJin Yao if (rec->buildid_all && !rec->timestamp_boundary) 6866156681bSNamhyung Kim rec->tool.sample = NULL; 6876156681bSNamhyung Kim 688b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 6896122e4e4SArnaldo Carvalho de Melo } 6906122e4e4SArnaldo Carvalho de Melo 6918115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 692a1645ce1SZhang, Yanmin { 693a1645ce1SZhang, Yanmin int err; 69445694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 695a1645ce1SZhang, Yanmin /* 696a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 697a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 698a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 699a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 700a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 701a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 702a1645ce1SZhang, Yanmin */ 70345694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 704743eb868SArnaldo Carvalho de Melo machine); 705a1645ce1SZhang, Yanmin if (err < 0) 706a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 70723346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 708a1645ce1SZhang, Yanmin 709a1645ce1SZhang, Yanmin /* 710a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 711a1645ce1SZhang, Yanmin * have no _text sometimes. 712a1645ce1SZhang, Yanmin */ 71345694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 7140ae617beSAdrian Hunter machine); 715a1645ce1SZhang, Yanmin if (err < 0) 716a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 71723346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 718a1645ce1SZhang, Yanmin } 719a1645ce1SZhang, Yanmin 72098402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 72198402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 72298402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 72398402807SFrederic Weisbecker }; 72498402807SFrederic Weisbecker 725f13de660SAlexey Budankov static void record__adjust_affinity(struct record *rec, struct perf_mmap *map) 726f13de660SAlexey Budankov { 727f13de660SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS && 728f13de660SAlexey Budankov !CPU_EQUAL(&rec->affinity_mask, &map->affinity_mask)) { 729f13de660SAlexey Budankov CPU_ZERO(&rec->affinity_mask); 730f13de660SAlexey Budankov CPU_OR(&rec->affinity_mask, &rec->affinity_mask, &map->affinity_mask); 731f13de660SAlexey Budankov sched_setaffinity(0, sizeof(rec->affinity_mask), &rec->affinity_mask); 732f13de660SAlexey Budankov } 733f13de660SAlexey Budankov } 734f13de660SAlexey Budankov 735a4ea0ec4SWang Nan static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist, 7360b72d69aSWang Nan bool overwrite) 73798402807SFrederic Weisbecker { 738dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 7390e2e63ddSPeter Zijlstra int i; 7408d3eca20SDavid Ahern int rc = 0; 741a4ea0ec4SWang Nan struct perf_mmap *maps; 742d3d1af6fSAlexey Budankov int trace_fd = rec->data.file.fd; 743d3d1af6fSAlexey Budankov off_t off; 74498402807SFrederic Weisbecker 745cb21686bSWang Nan if (!evlist) 746cb21686bSWang Nan return 0; 747ef149c25SAdrian Hunter 7480b72d69aSWang Nan maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 749a4ea0ec4SWang Nan if (!maps) 750a4ea0ec4SWang Nan return 0; 751cb21686bSWang Nan 7520b72d69aSWang Nan if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 75354cc54deSWang Nan return 0; 75454cc54deSWang Nan 755d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 756d3d1af6fSAlexey Budankov off = record__aio_get_pos(trace_fd); 757d3d1af6fSAlexey Budankov 758a4ea0ec4SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 759e035f4caSJiri Olsa struct perf_mmap *map = &maps[i]; 760a4ea0ec4SWang Nan 761e035f4caSJiri Olsa if (map->base) { 762f13de660SAlexey Budankov record__adjust_affinity(rec, map); 763d3d1af6fSAlexey Budankov if (!record__aio_enabled(rec)) { 764e035f4caSJiri Olsa if (perf_mmap__push(map, rec, record__pushfn) != 0) { 7658d3eca20SDavid Ahern rc = -1; 7668d3eca20SDavid Ahern goto out; 7678d3eca20SDavid Ahern } 768d3d1af6fSAlexey Budankov } else { 76993f20c0fSAlexey Budankov int idx; 770d3d1af6fSAlexey Budankov /* 771d3d1af6fSAlexey Budankov * Call record__aio_sync() to wait till map->data buffer 772d3d1af6fSAlexey Budankov * becomes available after previous aio write request. 773d3d1af6fSAlexey Budankov */ 77493f20c0fSAlexey Budankov idx = record__aio_sync(map, false); 77593f20c0fSAlexey Budankov if (perf_mmap__aio_push(map, rec, idx, record__aio_pushfn, &off) != 0) { 776d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 777d3d1af6fSAlexey Budankov rc = -1; 778d3d1af6fSAlexey Budankov goto out; 779d3d1af6fSAlexey Budankov } 780d3d1af6fSAlexey Budankov } 7818d3eca20SDavid Ahern } 782ef149c25SAdrian Hunter 783e035f4caSJiri Olsa if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode && 784e035f4caSJiri Olsa record__auxtrace_mmap_read(rec, map) != 0) { 785ef149c25SAdrian Hunter rc = -1; 786ef149c25SAdrian Hunter goto out; 787ef149c25SAdrian Hunter } 78898402807SFrederic Weisbecker } 78998402807SFrederic Weisbecker 790d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 791d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 792d3d1af6fSAlexey Budankov 793dcabb507SJiri Olsa /* 794dcabb507SJiri Olsa * Mark the round finished in case we wrote 795dcabb507SJiri Olsa * at least one event. 796dcabb507SJiri Olsa */ 797dcabb507SJiri Olsa if (bytes_written != rec->bytes_written) 798ded2b8feSJiri Olsa rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event)); 7998d3eca20SDavid Ahern 8000b72d69aSWang Nan if (overwrite) 80154cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 8028d3eca20SDavid Ahern out: 8038d3eca20SDavid Ahern return rc; 80498402807SFrederic Weisbecker } 80598402807SFrederic Weisbecker 806cb21686bSWang Nan static int record__mmap_read_all(struct record *rec) 807cb21686bSWang Nan { 808cb21686bSWang Nan int err; 809cb21686bSWang Nan 810a4ea0ec4SWang Nan err = record__mmap_read_evlist(rec, rec->evlist, false); 811cb21686bSWang Nan if (err) 812cb21686bSWang Nan return err; 813cb21686bSWang Nan 81405737464SWang Nan return record__mmap_read_evlist(rec, rec->evlist, true); 815cb21686bSWang Nan } 816cb21686bSWang Nan 8178c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 81857706abcSDavid Ahern { 81957706abcSDavid Ahern struct perf_session *session = rec->session; 82057706abcSDavid Ahern int feat; 82157706abcSDavid Ahern 82257706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 82357706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 82457706abcSDavid Ahern 82557706abcSDavid Ahern if (rec->no_buildid) 82657706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 82757706abcSDavid Ahern 8283e2be2daSArnaldo Carvalho de Melo if (!have_tracepoints(&rec->evlist->entries)) 82957706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 83057706abcSDavid Ahern 83157706abcSDavid Ahern if (!rec->opts.branch_stack) 83257706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 833ef149c25SAdrian Hunter 834ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 835ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 836ffa517adSJiri Olsa 837cf790516SAlexey Budankov if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns)) 838cf790516SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_CLOCKID); 839cf790516SAlexey Budankov 840ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 84157706abcSDavid Ahern } 84257706abcSDavid Ahern 843e1ab48baSWang Nan static void 844e1ab48baSWang Nan record__finish_output(struct record *rec) 845e1ab48baSWang Nan { 8468ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 8478ceb41d7SJiri Olsa int fd = perf_data__fd(data); 848e1ab48baSWang Nan 8498ceb41d7SJiri Olsa if (data->is_pipe) 850e1ab48baSWang Nan return; 851e1ab48baSWang Nan 852e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 853*45112e89SJiri Olsa data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR); 854e1ab48baSWang Nan 855e1ab48baSWang Nan if (!rec->no_buildid) { 856e1ab48baSWang Nan process_buildids(rec); 857e1ab48baSWang Nan 858e1ab48baSWang Nan if (rec->buildid_all) 859e1ab48baSWang Nan dsos__hit_all(rec->session); 860e1ab48baSWang Nan } 861e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 862e1ab48baSWang Nan 863e1ab48baSWang Nan return; 864e1ab48baSWang Nan } 865e1ab48baSWang Nan 8664ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 867be7b0c9eSWang Nan { 8689d6aae72SArnaldo Carvalho de Melo int err; 8699d6aae72SArnaldo Carvalho de Melo struct thread_map *thread_map; 870be7b0c9eSWang Nan 8714ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 8724ea648aeSWang Nan return 0; 8734ea648aeSWang Nan 8749d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 8759d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 8769d6aae72SArnaldo Carvalho de Melo return -1; 8779d6aae72SArnaldo Carvalho de Melo 8789d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 879be7b0c9eSWang Nan process_synthesized_event, 880be7b0c9eSWang Nan &rec->session->machines.host, 8813fcb10e4SMark Drayton rec->opts.sample_address); 8829d6aae72SArnaldo Carvalho de Melo thread_map__put(thread_map); 8839d6aae72SArnaldo Carvalho de Melo return err; 884be7b0c9eSWang Nan } 885be7b0c9eSWang Nan 8864ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 8873c1cb7e3SWang Nan 888ecfd7a9cSWang Nan static int 889ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 890ecfd7a9cSWang Nan { 8918ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 892ecfd7a9cSWang Nan int fd, err; 893ecfd7a9cSWang Nan 894ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 895ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 896ecfd7a9cSWang Nan 897d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 898d3d1af6fSAlexey Budankov 8994ea648aeSWang Nan record__synthesize(rec, true); 9004ea648aeSWang Nan if (target__none(&rec->opts.target)) 9014ea648aeSWang Nan record__synthesize_workload(rec, true); 9024ea648aeSWang Nan 903ecfd7a9cSWang Nan rec->samples = 0; 904ecfd7a9cSWang Nan record__finish_output(rec); 905ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 906ecfd7a9cSWang Nan if (err) { 907ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 908ecfd7a9cSWang Nan return -EINVAL; 909ecfd7a9cSWang Nan } 910ecfd7a9cSWang Nan 9118ceb41d7SJiri Olsa fd = perf_data__switch(data, timestamp, 912ecfd7a9cSWang Nan rec->session->header.data_offset, 913ecfd7a9cSWang Nan at_exit); 914ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 915ecfd7a9cSWang Nan rec->bytes_written = 0; 916ecfd7a9cSWang Nan rec->session->header.data_size = 0; 917ecfd7a9cSWang Nan } 918ecfd7a9cSWang Nan 919ecfd7a9cSWang Nan if (!quiet) 920ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 921eae8ad80SJiri Olsa data->file.path, timestamp); 9223c1cb7e3SWang Nan 9233c1cb7e3SWang Nan /* Output tracking events */ 924be7b0c9eSWang Nan if (!at_exit) { 9254ea648aeSWang Nan record__synthesize(rec, false); 9263c1cb7e3SWang Nan 927be7b0c9eSWang Nan /* 928be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 929be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 930be7b0c9eSWang Nan * generate tracking events because there's no thread_map 931be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 932be7b0c9eSWang Nan * contain map and comm information. 933be7b0c9eSWang Nan * Create a fake thread_map and directly call 934be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 935be7b0c9eSWang Nan */ 936be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 9374ea648aeSWang Nan record__synthesize_workload(rec, false); 938be7b0c9eSWang Nan } 939ecfd7a9cSWang Nan return fd; 940ecfd7a9cSWang Nan } 941ecfd7a9cSWang Nan 942f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno; 943f33cbe72SArnaldo Carvalho de Melo 944f33cbe72SArnaldo Carvalho de Melo /* 945f33cbe72SArnaldo Carvalho de Melo * perf_evlist__prepare_workload will send a SIGUSR1 946f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 947f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 948f33cbe72SArnaldo Carvalho de Melo */ 94945604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 95045604710SNamhyung Kim siginfo_t *info, 951f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 952f33cbe72SArnaldo Carvalho de Melo { 953f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 954f33cbe72SArnaldo Carvalho de Melo done = 1; 955f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 956f33cbe72SArnaldo Carvalho de Melo } 957f33cbe72SArnaldo Carvalho de Melo 9582dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 959bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 9602dd6d8a1SAdrian Hunter 96146bc29b9SAdrian Hunter int __weak 96246bc29b9SAdrian Hunter perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused, 96346bc29b9SAdrian Hunter struct perf_tool *tool __maybe_unused, 96446bc29b9SAdrian Hunter perf_event__handler_t process __maybe_unused, 96546bc29b9SAdrian Hunter struct machine *machine __maybe_unused) 96646bc29b9SAdrian Hunter { 96746bc29b9SAdrian Hunter return 0; 96846bc29b9SAdrian Hunter } 96946bc29b9SAdrian Hunter 970ee667f94SWang Nan static const struct perf_event_mmap_page * 971ee667f94SWang Nan perf_evlist__pick_pc(struct perf_evlist *evlist) 972ee667f94SWang Nan { 973b2cb615dSWang Nan if (evlist) { 974b2cb615dSWang Nan if (evlist->mmap && evlist->mmap[0].base) 975ee667f94SWang Nan return evlist->mmap[0].base; 9760b72d69aSWang Nan if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].base) 9770b72d69aSWang Nan return evlist->overwrite_mmap[0].base; 978b2cb615dSWang Nan } 979ee667f94SWang Nan return NULL; 980ee667f94SWang Nan } 981ee667f94SWang Nan 982c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 983c45628b0SWang Nan { 984ee667f94SWang Nan const struct perf_event_mmap_page *pc; 985ee667f94SWang Nan 986ee667f94SWang Nan pc = perf_evlist__pick_pc(rec->evlist); 987ee667f94SWang Nan if (pc) 988ee667f94SWang Nan return pc; 989c45628b0SWang Nan return NULL; 990c45628b0SWang Nan } 991c45628b0SWang Nan 9924ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 993c45c86ebSWang Nan { 994c45c86ebSWang Nan struct perf_session *session = rec->session; 995c45c86ebSWang Nan struct machine *machine = &session->machines.host; 9968ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 997c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 998c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 9998ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1000c45c86ebSWang Nan int err = 0; 1001c45c86ebSWang Nan 10024ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 10034ea648aeSWang Nan return 0; 10044ea648aeSWang Nan 10058ceb41d7SJiri Olsa if (data->is_pipe) { 1006a2015516SJiri Olsa /* 1007a2015516SJiri Olsa * We need to synthesize events first, because some 1008a2015516SJiri Olsa * features works on top of them (on report side). 1009a2015516SJiri Olsa */ 1010318ec184SJiri Olsa err = perf_event__synthesize_attrs(tool, rec->evlist, 1011c45c86ebSWang Nan process_synthesized_event); 1012c45c86ebSWang Nan if (err < 0) { 1013c45c86ebSWang Nan pr_err("Couldn't synthesize attrs.\n"); 1014c45c86ebSWang Nan goto out; 1015c45c86ebSWang Nan } 1016c45c86ebSWang Nan 1017a2015516SJiri Olsa err = perf_event__synthesize_features(tool, session, rec->evlist, 1018a2015516SJiri Olsa process_synthesized_event); 1019a2015516SJiri Olsa if (err < 0) { 1020a2015516SJiri Olsa pr_err("Couldn't synthesize features.\n"); 1021a2015516SJiri Olsa return err; 1022a2015516SJiri Olsa } 1023a2015516SJiri Olsa 1024c45c86ebSWang Nan if (have_tracepoints(&rec->evlist->entries)) { 1025c45c86ebSWang Nan /* 1026c45c86ebSWang Nan * FIXME err <= 0 here actually means that 1027c45c86ebSWang Nan * there were no tracepoints so its not really 1028c45c86ebSWang Nan * an error, just that we don't need to 1029c45c86ebSWang Nan * synthesize anything. We really have to 1030c45c86ebSWang Nan * return this more properly and also 1031c45c86ebSWang Nan * propagate errors that now are calling die() 1032c45c86ebSWang Nan */ 1033c45c86ebSWang Nan err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, 1034c45c86ebSWang Nan process_synthesized_event); 1035c45c86ebSWang Nan if (err <= 0) { 1036c45c86ebSWang Nan pr_err("Couldn't record tracing data.\n"); 1037c45c86ebSWang Nan goto out; 1038c45c86ebSWang Nan } 1039c45c86ebSWang Nan rec->bytes_written += err; 1040c45c86ebSWang Nan } 1041c45c86ebSWang Nan } 1042c45c86ebSWang Nan 1043c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 104446bc29b9SAdrian Hunter process_synthesized_event, machine); 104546bc29b9SAdrian Hunter if (err) 104646bc29b9SAdrian Hunter goto out; 104746bc29b9SAdrian Hunter 1048c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 1049c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 1050c45c86ebSWang Nan session, process_synthesized_event); 1051c45c86ebSWang Nan if (err) 1052c45c86ebSWang Nan goto out; 1053c45c86ebSWang Nan } 1054c45c86ebSWang Nan 10556c443954SArnaldo Carvalho de Melo if (!perf_evlist__exclude_kernel(rec->evlist)) { 1056c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 1057c45c86ebSWang Nan machine); 1058c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 1059c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1060c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 1061c45c86ebSWang Nan 1062c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 1063c45c86ebSWang Nan machine); 1064c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 1065c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1066c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 10676c443954SArnaldo Carvalho de Melo } 1068c45c86ebSWang Nan 1069c45c86ebSWang Nan if (perf_guest) { 1070c45c86ebSWang Nan machines__process_guests(&session->machines, 1071c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 1072c45c86ebSWang Nan } 1073c45c86ebSWang Nan 1074bfd8f72cSAndi Kleen err = perf_event__synthesize_extra_attr(&rec->tool, 1075bfd8f72cSAndi Kleen rec->evlist, 1076bfd8f72cSAndi Kleen process_synthesized_event, 1077bfd8f72cSAndi Kleen data->is_pipe); 1078bfd8f72cSAndi Kleen if (err) 1079bfd8f72cSAndi Kleen goto out; 1080bfd8f72cSAndi Kleen 1081373565d2SAndi Kleen err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->threads, 1082373565d2SAndi Kleen process_synthesized_event, 1083373565d2SAndi Kleen NULL); 1084373565d2SAndi Kleen if (err < 0) { 1085373565d2SAndi Kleen pr_err("Couldn't synthesize thread map.\n"); 1086373565d2SAndi Kleen return err; 1087373565d2SAndi Kleen } 1088373565d2SAndi Kleen 1089373565d2SAndi Kleen err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->cpus, 1090373565d2SAndi Kleen process_synthesized_event, NULL); 1091373565d2SAndi Kleen if (err < 0) { 1092373565d2SAndi Kleen pr_err("Couldn't synthesize cpu map.\n"); 1093373565d2SAndi Kleen return err; 1094373565d2SAndi Kleen } 1095373565d2SAndi Kleen 10967b612e29SSong Liu err = perf_event__synthesize_bpf_events(tool, process_synthesized_event, 10977b612e29SSong Liu machine, opts); 10987b612e29SSong Liu if (err < 0) 10997b612e29SSong Liu pr_warning("Couldn't synthesize bpf events.\n"); 11007b612e29SSong Liu 1101c45c86ebSWang Nan err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, 1102c45c86ebSWang Nan process_synthesized_event, opts->sample_address, 11033fcb10e4SMark Drayton 1); 1104c45c86ebSWang Nan out: 1105c45c86ebSWang Nan return err; 1106c45c86ebSWang Nan } 1107c45c86ebSWang Nan 11088c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 110986470930SIngo Molnar { 111057706abcSDavid Ahern int err; 111145604710SNamhyung Kim int status = 0; 11128b412664SPeter Zijlstra unsigned long waking = 0; 111346be604bSZhang, Yanmin const bool forks = argc > 0; 111445694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 1115b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 11168ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1117d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 11186dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 111942aa276fSNamhyung Kim int fd; 112086470930SIngo Molnar 112145604710SNamhyung Kim atexit(record__sig_exit); 1122f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 1123f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 1124804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 1125a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 1126c0bdc1c4SWang Nan 1127f3b3614aSHari Bathini if (rec->opts.record_namespaces) 1128f3b3614aSHari Bathini tool->namespace_events = true; 1129f3b3614aSHari Bathini 1130dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 11312dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 11323c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 11335f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 1134dc0c6127SJiri Olsa if (rec->switch_output.enabled) 11353c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 1136c0bdc1c4SWang Nan } else { 11372dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 1138c0bdc1c4SWang Nan } 1139f5970550SPeter Zijlstra 11408ceb41d7SJiri Olsa session = perf_session__new(data, false, tool); 114194c744b6SArnaldo Carvalho de Melo if (session == NULL) { 1142ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 1143a9a70bbcSArnaldo Carvalho de Melo return -1; 1144a9a70bbcSArnaldo Carvalho de Melo } 1145a9a70bbcSArnaldo Carvalho de Melo 11468ceb41d7SJiri Olsa fd = perf_data__fd(data); 1147d20deb64SArnaldo Carvalho de Melo rec->session = session; 1148d20deb64SArnaldo Carvalho de Melo 11498c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 1150330aa675SStephane Eranian 1151cf790516SAlexey Budankov if (rec->opts.use_clockid && rec->opts.clockid_res_ns) 1152cf790516SAlexey Budankov session->header.env.clockid_res_ns = rec->opts.clockid_res_ns; 1153cf790516SAlexey Budankov 1154d4db3f16SArnaldo Carvalho de Melo if (forks) { 11553e2be2daSArnaldo Carvalho de Melo err = perf_evlist__prepare_workload(rec->evlist, &opts->target, 11568ceb41d7SJiri Olsa argv, data->is_pipe, 1157735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 115835b9d88eSArnaldo Carvalho de Melo if (err < 0) { 115935b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 116045604710SNamhyung Kim status = err; 116135b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 1162856e9660SPeter Zijlstra } 1163856e9660SPeter Zijlstra } 1164856e9660SPeter Zijlstra 1165ad46e48cSJiri Olsa /* 1166ad46e48cSJiri Olsa * If we have just single event and are sending data 1167ad46e48cSJiri Olsa * through pipe, we need to force the ids allocation, 1168ad46e48cSJiri Olsa * because we synthesize event name through the pipe 1169ad46e48cSJiri Olsa * and need the id for that. 1170ad46e48cSJiri Olsa */ 1171ad46e48cSJiri Olsa if (data->is_pipe && rec->evlist->nr_entries == 1) 1172ad46e48cSJiri Olsa rec->opts.sample_id = true; 1173ad46e48cSJiri Olsa 11748c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 11758d3eca20SDavid Ahern err = -1; 117645604710SNamhyung Kim goto out_child; 11778d3eca20SDavid Ahern } 117886470930SIngo Molnar 11798690a2a7SWang Nan err = bpf__apply_obj_config(); 11808690a2a7SWang Nan if (err) { 11818690a2a7SWang Nan char errbuf[BUFSIZ]; 11828690a2a7SWang Nan 11838690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 11848690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 11858690a2a7SWang Nan errbuf); 11868690a2a7SWang Nan goto out_child; 11878690a2a7SWang Nan } 11888690a2a7SWang Nan 1189cca8482cSAdrian Hunter /* 1190cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 1191cca8482cSAdrian Hunter * evlist. 1192cca8482cSAdrian Hunter */ 1193cca8482cSAdrian Hunter if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) { 1194cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 1195cca8482cSAdrian Hunter rec->tool.ordered_events = false; 1196cca8482cSAdrian Hunter } 1197cca8482cSAdrian Hunter 11983e2be2daSArnaldo Carvalho de Melo if (!rec->evlist->nr_groups) 1199a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 1200a8bb559bSNamhyung Kim 12018ceb41d7SJiri Olsa if (data->is_pipe) { 120242aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 1203529870e3STom Zanussi if (err < 0) 120445604710SNamhyung Kim goto out_child; 1205563aecb2SJiri Olsa } else { 120642aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 1207d5eed904SArnaldo Carvalho de Melo if (err < 0) 120845604710SNamhyung Kim goto out_child; 1209d5eed904SArnaldo Carvalho de Melo } 12107c6a1c65SPeter Zijlstra 1211d3665498SDavid Ahern if (!rec->no_buildid 1212e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 1213d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 1214e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 12158d3eca20SDavid Ahern err = -1; 121645604710SNamhyung Kim goto out_child; 1217e20960c0SRobert Richter } 1218e20960c0SRobert Richter 12194ea648aeSWang Nan err = record__synthesize(rec, false); 1220c45c86ebSWang Nan if (err < 0) 122145604710SNamhyung Kim goto out_child; 12228d3eca20SDavid Ahern 1223d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 122486470930SIngo Molnar struct sched_param param; 122586470930SIngo Molnar 1226d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 122786470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 12286beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 12298d3eca20SDavid Ahern err = -1; 123045604710SNamhyung Kim goto out_child; 123186470930SIngo Molnar } 123286470930SIngo Molnar } 123386470930SIngo Molnar 1234774cb499SJiri Olsa /* 1235774cb499SJiri Olsa * When perf is starting the traced process, all the events 1236774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 1237774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 1238774cb499SJiri Olsa */ 12396619a53eSAndi Kleen if (!target__none(&opts->target) && !opts->initial_delay) 12403e2be2daSArnaldo Carvalho de Melo perf_evlist__enable(rec->evlist); 1241764e16a3SDavid Ahern 1242856e9660SPeter Zijlstra /* 1243856e9660SPeter Zijlstra * Let the child rip 1244856e9660SPeter Zijlstra */ 1245e803cf97SNamhyung Kim if (forks) { 124620a8a3cfSJiri Olsa struct machine *machine = &session->machines.host; 1247e5bed564SNamhyung Kim union perf_event *event; 1248e907caf3SHari Bathini pid_t tgid; 1249e5bed564SNamhyung Kim 1250e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 1251e5bed564SNamhyung Kim if (event == NULL) { 1252e5bed564SNamhyung Kim err = -ENOMEM; 1253e5bed564SNamhyung Kim goto out_child; 1254e5bed564SNamhyung Kim } 1255e5bed564SNamhyung Kim 1256e803cf97SNamhyung Kim /* 1257e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 1258e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 1259e803cf97SNamhyung Kim * cannot see a correct process name for those events. 1260e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 1261e803cf97SNamhyung Kim */ 1262e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 1263e803cf97SNamhyung Kim rec->evlist->workload.pid, 1264e803cf97SNamhyung Kim process_synthesized_event, 1265e803cf97SNamhyung Kim machine); 1266e5bed564SNamhyung Kim free(event); 1267e803cf97SNamhyung Kim 1268e907caf3SHari Bathini if (tgid == -1) 1269e907caf3SHari Bathini goto out_child; 1270e907caf3SHari Bathini 1271e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 1272e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 1273e907caf3SHari Bathini machine->id_hdr_size); 1274e907caf3SHari Bathini if (event == NULL) { 1275e907caf3SHari Bathini err = -ENOMEM; 1276e907caf3SHari Bathini goto out_child; 1277e907caf3SHari Bathini } 1278e907caf3SHari Bathini 1279e907caf3SHari Bathini /* 1280e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 1281e907caf3SHari Bathini */ 1282e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 1283e907caf3SHari Bathini rec->evlist->workload.pid, 1284e907caf3SHari Bathini tgid, process_synthesized_event, 1285e907caf3SHari Bathini machine); 1286e907caf3SHari Bathini free(event); 1287e907caf3SHari Bathini 12883e2be2daSArnaldo Carvalho de Melo perf_evlist__start_workload(rec->evlist); 1289e803cf97SNamhyung Kim } 1290856e9660SPeter Zijlstra 12916619a53eSAndi Kleen if (opts->initial_delay) { 12920693e680SArnaldo Carvalho de Melo usleep(opts->initial_delay * USEC_PER_MSEC); 12936619a53eSAndi Kleen perf_evlist__enable(rec->evlist); 12946619a53eSAndi Kleen } 12956619a53eSAndi Kleen 12965f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 12973c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 1298a074865eSWang Nan perf_hooks__invoke_record_start(); 1299649c48a9SPeter Zijlstra for (;;) { 13009f065194SYang Shi unsigned long long hits = rec->samples; 130186470930SIngo Molnar 130205737464SWang Nan /* 130305737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 130405737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 130505737464SWang Nan * hits != rec->samples in previous round. 130605737464SWang Nan * 130705737464SWang Nan * perf_evlist__toggle_bkw_mmap ensure we never 130805737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 130905737464SWang Nan */ 131005737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 131105737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 131205737464SWang Nan 13138c6f45a7SArnaldo Carvalho de Melo if (record__mmap_read_all(rec) < 0) { 13145f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 13153c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 13168d3eca20SDavid Ahern err = -1; 131745604710SNamhyung Kim goto out_child; 13188d3eca20SDavid Ahern } 131986470930SIngo Molnar 13202dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 13212dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 13225f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 13232dd6d8a1SAdrian Hunter record__read_auxtrace_snapshot(rec); 13245f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 13252dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 13262dd6d8a1SAdrian Hunter err = -1; 13272dd6d8a1SAdrian Hunter goto out_child; 13282dd6d8a1SAdrian Hunter } 13292dd6d8a1SAdrian Hunter } 13302dd6d8a1SAdrian Hunter 13313c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 133205737464SWang Nan /* 133305737464SWang Nan * If switch_output_trigger is hit, the data in 133405737464SWang Nan * overwritable ring buffer should have been collected, 133505737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 133605737464SWang Nan * 133705737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 133805737464SWang Nan * record__mmap_read_all() didn't collect data from 133905737464SWang Nan * overwritable ring buffer. Read again. 134005737464SWang Nan */ 134105737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 134205737464SWang Nan continue; 13433c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 13443c1cb7e3SWang Nan 134505737464SWang Nan /* 134605737464SWang Nan * Reenable events in overwrite ring buffer after 134705737464SWang Nan * record__mmap_read_all(): we should have collected 134805737464SWang Nan * data from it. 134905737464SWang Nan */ 135005737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 135105737464SWang Nan 13523c1cb7e3SWang Nan if (!quiet) 13533c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 13543c1cb7e3SWang Nan waking); 13553c1cb7e3SWang Nan waking = 0; 13563c1cb7e3SWang Nan fd = record__switch_output(rec, false); 13573c1cb7e3SWang Nan if (fd < 0) { 13583c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 13593c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 13603c1cb7e3SWang Nan err = fd; 13613c1cb7e3SWang Nan goto out_child; 13623c1cb7e3SWang Nan } 1363bfacbe3bSJiri Olsa 1364bfacbe3bSJiri Olsa /* re-arm the alarm */ 1365bfacbe3bSJiri Olsa if (rec->switch_output.time) 1366bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 13673c1cb7e3SWang Nan } 13683c1cb7e3SWang Nan 1369d20deb64SArnaldo Carvalho de Melo if (hits == rec->samples) { 13706dcf45efSArnaldo Carvalho de Melo if (done || draining) 1371649c48a9SPeter Zijlstra break; 1372f66a889dSArnaldo Carvalho de Melo err = perf_evlist__poll(rec->evlist, -1); 1373a515114fSJiri Olsa /* 1374a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 1375a515114fSJiri Olsa * number of returned events and interrupt error. 1376a515114fSJiri Olsa */ 1377a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 137845604710SNamhyung Kim err = 0; 13798b412664SPeter Zijlstra waking++; 13806dcf45efSArnaldo Carvalho de Melo 13816dcf45efSArnaldo Carvalho de Melo if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) 13826dcf45efSArnaldo Carvalho de Melo draining = true; 13838b412664SPeter Zijlstra } 13848b412664SPeter Zijlstra 1385774cb499SJiri Olsa /* 1386774cb499SJiri Olsa * When perf is starting the traced process, at the end events 1387774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 1388774cb499SJiri Olsa * disable events in this case. 1389774cb499SJiri Olsa */ 1390602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 13915f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 13923e2be2daSArnaldo Carvalho de Melo perf_evlist__disable(rec->evlist); 13932711926aSJiri Olsa disabled = true; 13942711926aSJiri Olsa } 13958b412664SPeter Zijlstra } 13965f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 13973c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 13988b412664SPeter Zijlstra 1399f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 140035550da3SMasami Hiramatsu char msg[STRERR_BUFSIZE]; 1401c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 1402f33cbe72SArnaldo Carvalho de Melo pr_err("Workload failed: %s\n", emsg); 1403f33cbe72SArnaldo Carvalho de Melo err = -1; 140445604710SNamhyung Kim goto out_child; 1405f33cbe72SArnaldo Carvalho de Melo } 1406f33cbe72SArnaldo Carvalho de Melo 1407e3d59112SNamhyung Kim if (!quiet) 14088b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 140986470930SIngo Molnar 14104ea648aeSWang Nan if (target__none(&rec->opts.target)) 14114ea648aeSWang Nan record__synthesize_workload(rec, true); 14124ea648aeSWang Nan 141345604710SNamhyung Kim out_child: 1414d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1415d3d1af6fSAlexey Budankov 141645604710SNamhyung Kim if (forks) { 141745604710SNamhyung Kim int exit_status; 141845604710SNamhyung Kim 141945604710SNamhyung Kim if (!child_finished) 142045604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 142145604710SNamhyung Kim 142245604710SNamhyung Kim wait(&exit_status); 142345604710SNamhyung Kim 142445604710SNamhyung Kim if (err < 0) 142545604710SNamhyung Kim status = err; 142645604710SNamhyung Kim else if (WIFEXITED(exit_status)) 142745604710SNamhyung Kim status = WEXITSTATUS(exit_status); 142845604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 142945604710SNamhyung Kim signr = WTERMSIG(exit_status); 143045604710SNamhyung Kim } else 143145604710SNamhyung Kim status = err; 143245604710SNamhyung Kim 14334ea648aeSWang Nan record__synthesize(rec, true); 1434e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 1435e3d59112SNamhyung Kim rec->samples = 0; 1436e3d59112SNamhyung Kim 1437ecfd7a9cSWang Nan if (!err) { 1438ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 1439e1ab48baSWang Nan record__finish_output(rec); 1440ecfd7a9cSWang Nan } else { 1441ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 1442ecfd7a9cSWang Nan if (fd < 0) { 1443ecfd7a9cSWang Nan status = fd; 1444ecfd7a9cSWang Nan goto out_delete_session; 1445ecfd7a9cSWang Nan } 1446ecfd7a9cSWang Nan } 1447ecfd7a9cSWang Nan } 144839d17dacSArnaldo Carvalho de Melo 1449a074865eSWang Nan perf_hooks__invoke_record_end(); 1450a074865eSWang Nan 1451e3d59112SNamhyung Kim if (!err && !quiet) { 1452e3d59112SNamhyung Kim char samples[128]; 1453ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 1454ecfd7a9cSWang Nan ".<timestamp>" : ""; 1455e3d59112SNamhyung Kim 1456ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 1457e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 1458e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 1459e3d59112SNamhyung Kim else 1460e3d59112SNamhyung Kim samples[0] = '\0'; 1461e3d59112SNamhyung Kim 1462ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s ]\n", 14638ceb41d7SJiri Olsa perf_data__size(data) / 1024.0 / 1024.0, 1464eae8ad80SJiri Olsa data->file.path, postfix, samples); 1465e3d59112SNamhyung Kim } 1466e3d59112SNamhyung Kim 146739d17dacSArnaldo Carvalho de Melo out_delete_session: 146839d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 146945604710SNamhyung Kim return status; 147086470930SIngo Molnar } 147186470930SIngo Molnar 14720883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 147309b0fd45SJiri Olsa { 1474aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 1475a601fdffSJiri Olsa 14760883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 147726d33022SJiri Olsa 14780883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 147909b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 14800883e820SArnaldo Carvalho de Melo callchain->dump_size); 14810883e820SArnaldo Carvalho de Melo } 14820883e820SArnaldo Carvalho de Melo 14830883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 14840883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 14850883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 14860883e820SArnaldo Carvalho de Melo { 14870883e820SArnaldo Carvalho de Melo int ret; 14880883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 14890883e820SArnaldo Carvalho de Melo 14900883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 14910883e820SArnaldo Carvalho de Melo if (unset) { 14920883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 14930883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 14940883e820SArnaldo Carvalho de Melo return 0; 14950883e820SArnaldo Carvalho de Melo } 14960883e820SArnaldo Carvalho de Melo 14970883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 14980883e820SArnaldo Carvalho de Melo if (!ret) { 14990883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 15000883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 15010883e820SArnaldo Carvalho de Melo record->sample_address = true; 15020883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 15030883e820SArnaldo Carvalho de Melo } 15040883e820SArnaldo Carvalho de Melo 15050883e820SArnaldo Carvalho de Melo return ret; 150609b0fd45SJiri Olsa } 150709b0fd45SJiri Olsa 1508c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 150909b0fd45SJiri Olsa const char *arg, 151009b0fd45SJiri Olsa int unset) 151109b0fd45SJiri Olsa { 15120883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 151326d33022SJiri Olsa } 151426d33022SJiri Olsa 1515c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 151609b0fd45SJiri Olsa const char *arg __maybe_unused, 151709b0fd45SJiri Olsa int unset __maybe_unused) 151809b0fd45SJiri Olsa { 15192ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 1520c421e80bSKan Liang 15212ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 152209b0fd45SJiri Olsa 15232ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 15242ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 1525eb853e80SJiri Olsa 15262ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 152709b0fd45SJiri Olsa return 0; 152809b0fd45SJiri Olsa } 152909b0fd45SJiri Olsa 1530eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 1531eb853e80SJiri Olsa { 15327a29c087SNamhyung Kim struct record *rec = cb; 15337a29c087SNamhyung Kim 15347a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 15357a29c087SNamhyung Kim if (!strcmp(value, "cache")) 15367a29c087SNamhyung Kim rec->no_buildid_cache = false; 15377a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 15387a29c087SNamhyung Kim rec->no_buildid_cache = true; 15397a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 15407a29c087SNamhyung Kim rec->no_buildid = true; 15417a29c087SNamhyung Kim else 15427a29c087SNamhyung Kim return -1; 15437a29c087SNamhyung Kim return 0; 15447a29c087SNamhyung Kim } 1545cff17205SYisheng Xie if (!strcmp(var, "record.call-graph")) { 1546cff17205SYisheng Xie var = "call-graph.record-mode"; 1547eb853e80SJiri Olsa return perf_default_config(var, value, cb); 1548eb853e80SJiri Olsa } 154993f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 155093f20c0fSAlexey Budankov if (!strcmp(var, "record.aio")) { 155193f20c0fSAlexey Budankov rec->opts.nr_cblocks = strtol(value, NULL, 0); 155293f20c0fSAlexey Budankov if (!rec->opts.nr_cblocks) 155393f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_default; 155493f20c0fSAlexey Budankov } 155593f20c0fSAlexey Budankov #endif 1556eb853e80SJiri Olsa 1557cff17205SYisheng Xie return 0; 1558cff17205SYisheng Xie } 1559cff17205SYisheng Xie 1560814c8c38SPeter Zijlstra struct clockid_map { 1561814c8c38SPeter Zijlstra const char *name; 1562814c8c38SPeter Zijlstra int clockid; 1563814c8c38SPeter Zijlstra }; 1564814c8c38SPeter Zijlstra 1565814c8c38SPeter Zijlstra #define CLOCKID_MAP(n, c) \ 1566814c8c38SPeter Zijlstra { .name = n, .clockid = (c), } 1567814c8c38SPeter Zijlstra 1568814c8c38SPeter Zijlstra #define CLOCKID_END { .name = NULL, } 1569814c8c38SPeter Zijlstra 1570814c8c38SPeter Zijlstra 1571814c8c38SPeter Zijlstra /* 1572814c8c38SPeter Zijlstra * Add the missing ones, we need to build on many distros... 1573814c8c38SPeter Zijlstra */ 1574814c8c38SPeter Zijlstra #ifndef CLOCK_MONOTONIC_RAW 1575814c8c38SPeter Zijlstra #define CLOCK_MONOTONIC_RAW 4 1576814c8c38SPeter Zijlstra #endif 1577814c8c38SPeter Zijlstra #ifndef CLOCK_BOOTTIME 1578814c8c38SPeter Zijlstra #define CLOCK_BOOTTIME 7 1579814c8c38SPeter Zijlstra #endif 1580814c8c38SPeter Zijlstra #ifndef CLOCK_TAI 1581814c8c38SPeter Zijlstra #define CLOCK_TAI 11 1582814c8c38SPeter Zijlstra #endif 1583814c8c38SPeter Zijlstra 1584814c8c38SPeter Zijlstra static const struct clockid_map clockids[] = { 1585814c8c38SPeter Zijlstra /* available for all events, NMI safe */ 1586814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), 1587814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), 1588814c8c38SPeter Zijlstra 1589814c8c38SPeter Zijlstra /* available for some events */ 1590814c8c38SPeter Zijlstra CLOCKID_MAP("realtime", CLOCK_REALTIME), 1591814c8c38SPeter Zijlstra CLOCKID_MAP("boottime", CLOCK_BOOTTIME), 1592814c8c38SPeter Zijlstra CLOCKID_MAP("tai", CLOCK_TAI), 1593814c8c38SPeter Zijlstra 1594814c8c38SPeter Zijlstra /* available for the lazy */ 1595814c8c38SPeter Zijlstra CLOCKID_MAP("mono", CLOCK_MONOTONIC), 1596814c8c38SPeter Zijlstra CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), 1597814c8c38SPeter Zijlstra CLOCKID_MAP("real", CLOCK_REALTIME), 1598814c8c38SPeter Zijlstra CLOCKID_MAP("boot", CLOCK_BOOTTIME), 1599814c8c38SPeter Zijlstra 1600814c8c38SPeter Zijlstra CLOCKID_END, 1601814c8c38SPeter Zijlstra }; 1602814c8c38SPeter Zijlstra 1603cf790516SAlexey Budankov static int get_clockid_res(clockid_t clk_id, u64 *res_ns) 1604cf790516SAlexey Budankov { 1605cf790516SAlexey Budankov struct timespec res; 1606cf790516SAlexey Budankov 1607cf790516SAlexey Budankov *res_ns = 0; 1608cf790516SAlexey Budankov if (!clock_getres(clk_id, &res)) 1609cf790516SAlexey Budankov *res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC; 1610cf790516SAlexey Budankov else 1611cf790516SAlexey Budankov pr_warning("WARNING: Failed to determine specified clock resolution.\n"); 1612cf790516SAlexey Budankov 1613cf790516SAlexey Budankov return 0; 1614cf790516SAlexey Budankov } 1615cf790516SAlexey Budankov 1616814c8c38SPeter Zijlstra static int parse_clockid(const struct option *opt, const char *str, int unset) 1617814c8c38SPeter Zijlstra { 1618814c8c38SPeter Zijlstra struct record_opts *opts = (struct record_opts *)opt->value; 1619814c8c38SPeter Zijlstra const struct clockid_map *cm; 1620814c8c38SPeter Zijlstra const char *ostr = str; 1621814c8c38SPeter Zijlstra 1622814c8c38SPeter Zijlstra if (unset) { 1623814c8c38SPeter Zijlstra opts->use_clockid = 0; 1624814c8c38SPeter Zijlstra return 0; 1625814c8c38SPeter Zijlstra } 1626814c8c38SPeter Zijlstra 1627814c8c38SPeter Zijlstra /* no arg passed */ 1628814c8c38SPeter Zijlstra if (!str) 1629814c8c38SPeter Zijlstra return 0; 1630814c8c38SPeter Zijlstra 1631814c8c38SPeter Zijlstra /* no setting it twice */ 1632814c8c38SPeter Zijlstra if (opts->use_clockid) 1633814c8c38SPeter Zijlstra return -1; 1634814c8c38SPeter Zijlstra 1635814c8c38SPeter Zijlstra opts->use_clockid = true; 1636814c8c38SPeter Zijlstra 1637814c8c38SPeter Zijlstra /* if its a number, we're done */ 1638814c8c38SPeter Zijlstra if (sscanf(str, "%d", &opts->clockid) == 1) 1639cf790516SAlexey Budankov return get_clockid_res(opts->clockid, &opts->clockid_res_ns); 1640814c8c38SPeter Zijlstra 1641814c8c38SPeter Zijlstra /* allow a "CLOCK_" prefix to the name */ 1642814c8c38SPeter Zijlstra if (!strncasecmp(str, "CLOCK_", 6)) 1643814c8c38SPeter Zijlstra str += 6; 1644814c8c38SPeter Zijlstra 1645814c8c38SPeter Zijlstra for (cm = clockids; cm->name; cm++) { 1646814c8c38SPeter Zijlstra if (!strcasecmp(str, cm->name)) { 1647814c8c38SPeter Zijlstra opts->clockid = cm->clockid; 1648cf790516SAlexey Budankov return get_clockid_res(opts->clockid, 1649cf790516SAlexey Budankov &opts->clockid_res_ns); 1650814c8c38SPeter Zijlstra } 1651814c8c38SPeter Zijlstra } 1652814c8c38SPeter Zijlstra 1653814c8c38SPeter Zijlstra opts->use_clockid = false; 1654814c8c38SPeter Zijlstra ui__warning("unknown clockid %s, check man page\n", ostr); 1655814c8c38SPeter Zijlstra return -1; 1656814c8c38SPeter Zijlstra } 1657814c8c38SPeter Zijlstra 1658f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset) 1659f4fe11b7SAlexey Budankov { 1660f4fe11b7SAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 1661f4fe11b7SAlexey Budankov 1662f4fe11b7SAlexey Budankov if (unset || !str) 1663f4fe11b7SAlexey Budankov return 0; 1664f4fe11b7SAlexey Budankov 1665f4fe11b7SAlexey Budankov if (!strcasecmp(str, "node")) 1666f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_NODE; 1667f4fe11b7SAlexey Budankov else if (!strcasecmp(str, "cpu")) 1668f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_CPU; 1669f4fe11b7SAlexey Budankov 1670f4fe11b7SAlexey Budankov return 0; 1671f4fe11b7SAlexey Budankov } 1672f4fe11b7SAlexey Budankov 1673e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 1674e9db1310SAdrian Hunter const char *str, 1675e9db1310SAdrian Hunter int unset __maybe_unused) 1676e9db1310SAdrian Hunter { 1677e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 1678e9db1310SAdrian Hunter char *s, *p; 1679e9db1310SAdrian Hunter unsigned int mmap_pages; 1680e9db1310SAdrian Hunter int ret; 1681e9db1310SAdrian Hunter 1682e9db1310SAdrian Hunter if (!str) 1683e9db1310SAdrian Hunter return -EINVAL; 1684e9db1310SAdrian Hunter 1685e9db1310SAdrian Hunter s = strdup(str); 1686e9db1310SAdrian Hunter if (!s) 1687e9db1310SAdrian Hunter return -ENOMEM; 1688e9db1310SAdrian Hunter 1689e9db1310SAdrian Hunter p = strchr(s, ','); 1690e9db1310SAdrian Hunter if (p) 1691e9db1310SAdrian Hunter *p = '\0'; 1692e9db1310SAdrian Hunter 1693e9db1310SAdrian Hunter if (*s) { 1694e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s); 1695e9db1310SAdrian Hunter if (ret) 1696e9db1310SAdrian Hunter goto out_free; 1697e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 1698e9db1310SAdrian Hunter } 1699e9db1310SAdrian Hunter 1700e9db1310SAdrian Hunter if (!p) { 1701e9db1310SAdrian Hunter ret = 0; 1702e9db1310SAdrian Hunter goto out_free; 1703e9db1310SAdrian Hunter } 1704e9db1310SAdrian Hunter 1705e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1); 1706e9db1310SAdrian Hunter if (ret) 1707e9db1310SAdrian Hunter goto out_free; 1708e9db1310SAdrian Hunter 1709e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 1710e9db1310SAdrian Hunter 1711e9db1310SAdrian Hunter out_free: 1712e9db1310SAdrian Hunter free(s); 1713e9db1310SAdrian Hunter return ret; 1714e9db1310SAdrian Hunter } 1715e9db1310SAdrian Hunter 17160c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 17170c582449SJiri Olsa { 17180c582449SJiri Olsa u64 wakeup_size = perf_evlist__mmap_size(rec->opts.mmap_pages); 17190c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 17200c582449SJiri Olsa 17210c582449SJiri Olsa wakeup_size /= 2; 17220c582449SJiri Olsa 17230c582449SJiri Olsa if (s->size < wakeup_size) { 17240c582449SJiri Olsa char buf[100]; 17250c582449SJiri Olsa 17260c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 17270c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 17280c582449SJiri Olsa "wakeup kernel buffer size (%s) " 17290c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 17300c582449SJiri Olsa } 17310c582449SJiri Olsa } 17320c582449SJiri Olsa 1733cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 1734cb4e1ebbSJiri Olsa { 1735cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 1736dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 1737dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 1738dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 1739dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 1740dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 1741dc0c6127SJiri Olsa { .tag = 0 }, 1742dc0c6127SJiri Olsa }; 1743bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 1744bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 1745bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 1746bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 1747bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 1748bfacbe3bSJiri Olsa { .tag = 0 }, 1749bfacbe3bSJiri Olsa }; 1750dc0c6127SJiri Olsa unsigned long val; 1751cb4e1ebbSJiri Olsa 1752cb4e1ebbSJiri Olsa if (!s->set) 1753cb4e1ebbSJiri Olsa return 0; 1754cb4e1ebbSJiri Olsa 1755cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 1756cb4e1ebbSJiri Olsa s->signal = true; 1757cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 1758dc0c6127SJiri Olsa goto enabled; 1759dc0c6127SJiri Olsa } 1760dc0c6127SJiri Olsa 1761dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 1762dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 1763dc0c6127SJiri Olsa s->size = val; 1764dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 1765dc0c6127SJiri Olsa goto enabled; 1766cb4e1ebbSJiri Olsa } 1767cb4e1ebbSJiri Olsa 1768bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 1769bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 1770bfacbe3bSJiri Olsa s->time = val; 1771bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 1772bfacbe3bSJiri Olsa s->str, s->time); 1773bfacbe3bSJiri Olsa goto enabled; 1774bfacbe3bSJiri Olsa } 1775bfacbe3bSJiri Olsa 1776cb4e1ebbSJiri Olsa return -1; 1777dc0c6127SJiri Olsa 1778dc0c6127SJiri Olsa enabled: 1779dc0c6127SJiri Olsa rec->timestamp_filename = true; 1780dc0c6127SJiri Olsa s->enabled = true; 17810c582449SJiri Olsa 17820c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 17830c582449SJiri Olsa switch_output_size_warn(rec); 17840c582449SJiri Olsa 1785dc0c6127SJiri Olsa return 0; 1786cb4e1ebbSJiri Olsa } 1787cb4e1ebbSJiri Olsa 1788e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 178986470930SIngo Molnar "perf record [<options>] [<command>]", 179086470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 179186470930SIngo Molnar NULL 179286470930SIngo Molnar }; 1793e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 179486470930SIngo Molnar 1795d20deb64SArnaldo Carvalho de Melo /* 17968c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 17978c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 1798d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 1799d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 1800d20deb64SArnaldo Carvalho de Melo * 1801d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 1802d20deb64SArnaldo Carvalho de Melo * 1803d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 1804d20deb64SArnaldo Carvalho de Melo */ 18058c6f45a7SArnaldo Carvalho de Melo static struct record record = { 1806d20deb64SArnaldo Carvalho de Melo .opts = { 18078affc2b8SAndi Kleen .sample_time = true, 1808d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 1809d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 1810d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 1811447a6013SArnaldo Carvalho de Melo .freq = 4000, 1812d1cb9fceSNamhyung Kim .target = { 1813d1cb9fceSNamhyung Kim .uses_mmap = true, 18143aa5939dSAdrian Hunter .default_per_cpu = true, 1815d1cb9fceSNamhyung Kim }, 1816d20deb64SArnaldo Carvalho de Melo }, 1817e3d59112SNamhyung Kim .tool = { 1818e3d59112SNamhyung Kim .sample = process_sample_event, 1819e3d59112SNamhyung Kim .fork = perf_event__process_fork, 1820cca8482cSAdrian Hunter .exit = perf_event__process_exit, 1821e3d59112SNamhyung Kim .comm = perf_event__process_comm, 1822f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 1823e3d59112SNamhyung Kim .mmap = perf_event__process_mmap, 1824e3d59112SNamhyung Kim .mmap2 = perf_event__process_mmap2, 1825cca8482cSAdrian Hunter .ordered_events = true, 1826e3d59112SNamhyung Kim }, 1827d20deb64SArnaldo Carvalho de Melo }; 18287865e817SFrederic Weisbecker 182976a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 183076a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 183161eaa3beSArnaldo Carvalho de Melo 18320aab2136SWang Nan static bool dry_run; 18330aab2136SWang Nan 1834d20deb64SArnaldo Carvalho de Melo /* 1835d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 1836d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 1837b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 1838d20deb64SArnaldo Carvalho de Melo * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 1839d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 1840d20deb64SArnaldo Carvalho de Melo */ 1841efd21307SJiri Olsa static struct option __record_options[] = { 1842d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 184386470930SIngo Molnar "event selector. use 'perf list' to list available events", 1844f120f9d5SJiri Olsa parse_events_option), 1845d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 1846c171b552SLi Zefan "event filter", parse_filter), 18474ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 18484ba1faa1SWang Nan NULL, "don't record events from perf itself", 18494ba1faa1SWang Nan exclude_perf), 1850bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 1851d6d901c2SZhang, Yanmin "record events on existing process id"), 1852bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 1853d6d901c2SZhang, Yanmin "record events on existing thread id"), 1854d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 185586470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 1856509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 1857acac03faSKirill Smelkov "collect data without buffering"), 1858d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 1859daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 1860bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 186186470930SIngo Molnar "system-wide collection from all CPUs"), 1862bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 1863c45c6ea2SStephane Eranian "list of cpus to monitor"), 1864d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 1865eae8ad80SJiri Olsa OPT_STRING('o', "output", &record.data.file.path, "file", 186686470930SIngo Molnar "output file name"), 186769e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 186869e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 18692e6cdf99SStephane Eranian "child tasks do not inherit counters"), 18704ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 18714ea648aeSWang Nan "synthesize non-sample events at the end of output"), 1872626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 187345178a92SSong Liu OPT_BOOLEAN(0, "bpf-event", &record.opts.bpf_event, "record bpf events"), 1874b09c2364SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq, 1875b09c2364SArnaldo Carvalho de Melo "Fail if the specified frequency can't be used"), 187667230479SArnaldo Carvalho de Melo OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'", 187767230479SArnaldo Carvalho de Melo "profile at this frequency", 187867230479SArnaldo Carvalho de Melo record__parse_freq), 1879e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 1880e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 1881e9db1310SAdrian Hunter record__parse_mmap_pages), 1882d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "group", &record.opts.group, 188343bece79SLin Ming "put the counters into a counter group"), 18842ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 188509b0fd45SJiri Olsa NULL, "enables call-graph recording" , 188609b0fd45SJiri Olsa &record_callchain_opt), 188709b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 188876a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 188909b0fd45SJiri Olsa &record_parse_callchain_opt), 1890c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 18913da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 1892b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 1893d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 1894649c48a9SPeter Zijlstra "per thread counts"), 189556100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 18963b0a5daaSKan Liang OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, 18973b0a5daaSKan Liang "Record the sample physical addresses"), 1898b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 18993abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 19003abebc55SAdrian Hunter &record.opts.sample_time_set, 19013abebc55SAdrian Hunter "Record the sample timestamps"), 1902f290aa1fSJiri Olsa OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set, 1903f290aa1fSJiri Olsa "Record the sample period"), 1904d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 1905649c48a9SPeter Zijlstra "don't sample"), 1906d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 1907d2db9a98SWang Nan &record.no_buildid_cache_set, 1908a1ac1d3cSStephane Eranian "do not update the buildid cache"), 1909d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 1910d2db9a98SWang Nan &record.no_buildid_set, 1911baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 1912d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 1913023695d9SStephane Eranian "monitor event in cgroup name only", 1914023695d9SStephane Eranian parse_cgroups), 1915a6205a35SArnaldo Carvalho de Melo OPT_UINTEGER('D', "delay", &record.opts.initial_delay, 19166619a53eSAndi Kleen "ms to wait before starting measurement after program start"), 1917bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 1918bea03405SNamhyung Kim "user to profile"), 1919a5aabdacSStephane Eranian 1920a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 1921a5aabdacSStephane Eranian "branch any", "sample any taken branches", 1922a5aabdacSStephane Eranian parse_branch_stack), 1923a5aabdacSStephane Eranian 1924a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 1925a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 1926bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 192705484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 192805484298SAndi Kleen "sample by weight (on special events only)"), 1929475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 1930475eeab9SAndi Kleen "sample transaction flags (special events only)"), 19313aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 19323aa5939dSAdrian Hunter "use per-thread mmaps"), 1933bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 1934bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 1935bcc84ec6SStephane Eranian " use -I ? to list register names", parse_regs), 193684c41742SAndi Kleen OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register", 193784c41742SAndi Kleen "sample selected machine registers on interrupt," 193884c41742SAndi Kleen " use -I ? to list register names", parse_regs), 193985c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 194085c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 1941814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 1942814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 1943814c8c38SPeter Zijlstra parse_clockid), 19442dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 19452dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 19463fcb10e4SMark Drayton OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 19479d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 1948f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 1949f3b3614aSHari Bathini "Record namespaces events"), 1950b757bb09SAdrian Hunter OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events, 1951b757bb09SAdrian Hunter "Record context switch events"), 195285723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 195385723885SJiri Olsa "Configure all used events to run in kernel space.", 195485723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 195585723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 195685723885SJiri Olsa "Configure all used events to run in user space.", 195785723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 195871dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 195971dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 196071dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 196171dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 19627efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 19637efe0e03SHe Kuang "file", "vmlinux pathname"), 19646156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 19656156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 1966ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 1967ecfd7a9cSWang Nan "append timestamp to output filename"), 196868588bafSJin Yao OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary, 196968588bafSJin Yao "Record timestamp boundary (time of first/last samples)"), 1970cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 1971bfacbe3bSJiri Olsa &record.switch_output.set, "signal,size,time", 1972bfacbe3bSJiri Olsa "Switch output when receive SIGUSR2 or cross size,time threshold", 1973dc0c6127SJiri Olsa "signal"), 19740aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 19750aab2136SWang Nan "Parse options then exit"), 1976d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 197793f20c0fSAlexey Budankov OPT_CALLBACK_OPTARG(0, "aio", &record.opts, 197893f20c0fSAlexey Budankov &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)", 1979d3d1af6fSAlexey Budankov record__aio_parse), 1980d3d1af6fSAlexey Budankov #endif 1981f4fe11b7SAlexey Budankov OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu", 1982f4fe11b7SAlexey Budankov "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer", 1983f4fe11b7SAlexey Budankov record__parse_affinity), 198486470930SIngo Molnar OPT_END() 198586470930SIngo Molnar }; 198686470930SIngo Molnar 1987e5b2c207SNamhyung Kim struct option *record_options = __record_options; 1988e5b2c207SNamhyung Kim 1989b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 199086470930SIngo Molnar { 1991ef149c25SAdrian Hunter int err; 19928c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 199316ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 199486470930SIngo Molnar 199567230479SArnaldo Carvalho de Melo setlocale(LC_ALL, ""); 199667230479SArnaldo Carvalho de Melo 199748e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 199848e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 199948e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 200048e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 200148e1cab1SWang Nan # undef set_nobuild 200248e1cab1SWang Nan #endif 200348e1cab1SWang Nan 20047efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 20057efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 20067efe0e03SHe Kuang # define REASON "NO_DWARF=1" 20077efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 20087efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 20097efe0e03SHe Kuang # else 20107efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 20117efe0e03SHe Kuang # endif 20127efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 20137efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 20147efe0e03SHe Kuang # undef set_nobuild 20157efe0e03SHe Kuang # undef REASON 20167efe0e03SHe Kuang #endif 20177efe0e03SHe Kuang 20189d2ed645SAlexey Budankov CPU_ZERO(&rec->affinity_mask); 20199d2ed645SAlexey Budankov rec->opts.affinity = PERF_AFFINITY_SYS; 20209d2ed645SAlexey Budankov 20213e2be2daSArnaldo Carvalho de Melo rec->evlist = perf_evlist__new(); 20223e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 2023361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 2024361c99a6SArnaldo Carvalho de Melo 2025ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 2026ecc4c561SArnaldo Carvalho de Melo if (err) 2027ecc4c561SArnaldo Carvalho de Melo return err; 2028eb853e80SJiri Olsa 2029bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 2030a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 203168ba3235SNamhyung Kim if (quiet) 203268ba3235SNamhyung Kim perf_quiet_option(); 2033483635a9SJiri Olsa 2034483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 2035602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 2036483635a9SJiri Olsa rec->opts.target.system_wide = true; 203786470930SIngo Molnar 2038bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 2039c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 2040c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 2041c7118369SNamhyung Kim 2042023695d9SStephane Eranian } 2043b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 2044b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 2045c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 2046c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 2047c7118369SNamhyung Kim return -EINVAL; 2048b757bb09SAdrian Hunter } 2049023695d9SStephane Eranian 2050cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 2051cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 2052cb4e1ebbSJiri Olsa return -EINVAL; 2053cb4e1ebbSJiri Olsa } 2054cb4e1ebbSJiri Olsa 2055bfacbe3bSJiri Olsa if (rec->switch_output.time) { 2056bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 2057bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 2058bfacbe3bSJiri Olsa } 2059bfacbe3bSJiri Olsa 20601b36c03eSAdrian Hunter /* 20611b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 20621b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 20631b36c03eSAdrian Hunter */ 20641b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 20651b36c03eSAdrian Hunter 20661b36c03eSAdrian Hunter symbol__init(NULL); 20671b36c03eSAdrian Hunter 20684b5ea3bdSAdrian Hunter err = record__auxtrace_init(rec); 20691b36c03eSAdrian Hunter if (err) 20701b36c03eSAdrian Hunter goto out; 20711b36c03eSAdrian Hunter 20720aab2136SWang Nan if (dry_run) 20735c01ad60SAdrian Hunter goto out; 20740aab2136SWang Nan 2075d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 2076d7888573SWang Nan if (err) { 2077d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 2078d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 2079d7888573SWang Nan errbuf); 20805c01ad60SAdrian Hunter goto out; 2081d7888573SWang Nan } 2082d7888573SWang Nan 2083ef149c25SAdrian Hunter err = -ENOMEM; 2084ef149c25SAdrian Hunter 20856c443954SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict && !perf_evlist__exclude_kernel(rec->evlist)) 2086646aaea6SArnaldo Carvalho de Melo pr_warning( 2087646aaea6SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 2088ec80fde7SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict.\n\n" 2089646aaea6SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 2090646aaea6SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 2091646aaea6SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 2092646aaea6SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 2093646aaea6SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 2094ec80fde7SArnaldo Carvalho de Melo 20950c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 2096a1ac1d3cSStephane Eranian disable_buildid_cache(); 2097dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 20980c1d46a8SWang Nan /* 20990c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 21000c1d46a8SWang Nan * generation by default to reduce data file switching 21010c1d46a8SWang Nan * overhead. Still generate buildid if they are required 21020c1d46a8SWang Nan * explicitly using 21030c1d46a8SWang Nan * 210460437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 21050c1d46a8SWang Nan * --no-no-buildid-cache 21060c1d46a8SWang Nan * 21070c1d46a8SWang Nan * Following code equals to: 21080c1d46a8SWang Nan * 21090c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 21100c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 21110c1d46a8SWang Nan * disable_buildid_cache(); 21120c1d46a8SWang Nan */ 21130c1d46a8SWang Nan bool disable = true; 21140c1d46a8SWang Nan 21150c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 21160c1d46a8SWang Nan disable = false; 21170c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 21180c1d46a8SWang Nan disable = false; 21190c1d46a8SWang Nan if (disable) { 21200c1d46a8SWang Nan rec->no_buildid = true; 21210c1d46a8SWang Nan rec->no_buildid_cache = true; 21220c1d46a8SWang Nan disable_buildid_cache(); 21230c1d46a8SWang Nan } 21240c1d46a8SWang Nan } 2125655000e7SArnaldo Carvalho de Melo 21264ea648aeSWang Nan if (record.opts.overwrite) 21274ea648aeSWang Nan record.opts.tail_synthesize = true; 21284ea648aeSWang Nan 21293e2be2daSArnaldo Carvalho de Melo if (rec->evlist->nr_entries == 0 && 21304b4cd503SArnaldo Carvalho de Melo __perf_evlist__add_default(rec->evlist, !record.opts.no_samples) < 0) { 213169aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 2132394c01edSAdrian Hunter goto out; 2133bbd36e5eSPeter Zijlstra } 213486470930SIngo Molnar 213569e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 213669e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 213769e7e5b0SAdrian Hunter 2138602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 213916ad2ffbSNamhyung Kim if (err) { 2140602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 2141c3dec27bSJiri Olsa ui__warning("%s\n", errbuf); 214216ad2ffbSNamhyung Kim } 21434bd0f2d2SNamhyung Kim 2144602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 214516ad2ffbSNamhyung Kim if (err) { 214616ad2ffbSNamhyung Kim int saved_errno = errno; 214716ad2ffbSNamhyung Kim 2148602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 21493780f488SNamhyung Kim ui__error("%s", errbuf); 215016ad2ffbSNamhyung Kim 215116ad2ffbSNamhyung Kim err = -saved_errno; 2152394c01edSAdrian Hunter goto out; 215316ad2ffbSNamhyung Kim } 21540d37aa34SArnaldo Carvalho de Melo 2155ca800068SMengting Zhang /* Enable ignoring missing threads when -u/-p option is defined. */ 2156ca800068SMengting Zhang rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; 215723dc4f15SJiri Olsa 215816ad2ffbSNamhyung Kim err = -ENOMEM; 21593e2be2daSArnaldo Carvalho de Melo if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) 2160dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 216169aad6f1SArnaldo Carvalho de Melo 2162ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 2163ef149c25SAdrian Hunter if (err) 2164394c01edSAdrian Hunter goto out; 2165ef149c25SAdrian Hunter 21666156681bSNamhyung Kim /* 21676156681bSNamhyung Kim * We take all buildids when the file contains 21686156681bSNamhyung Kim * AUX area tracing data because we do not decode the 21696156681bSNamhyung Kim * trace because it would take too long. 21706156681bSNamhyung Kim */ 21716156681bSNamhyung Kim if (rec->opts.full_auxtrace) 21726156681bSNamhyung Kim rec->buildid_all = true; 21736156681bSNamhyung Kim 2174b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 217539d17dacSArnaldo Carvalho de Melo err = -EINVAL; 2176394c01edSAdrian Hunter goto out; 21777e4ff9e3SMike Galbraith } 21787e4ff9e3SMike Galbraith 217993f20c0fSAlexey Budankov if (rec->opts.nr_cblocks > nr_cblocks_max) 218093f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_max; 2181d3d1af6fSAlexey Budankov if (verbose > 0) 2182d3d1af6fSAlexey Budankov pr_info("nr_cblocks: %d\n", rec->opts.nr_cblocks); 2183d3d1af6fSAlexey Budankov 21849d2ed645SAlexey Budankov pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]); 21859d2ed645SAlexey Budankov 2186d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 2187394c01edSAdrian Hunter out: 218845604710SNamhyung Kim perf_evlist__delete(rec->evlist); 2189d65a458bSArnaldo Carvalho de Melo symbol__exit(); 2190ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 219139d17dacSArnaldo Carvalho de Melo return err; 219286470930SIngo Molnar } 21932dd6d8a1SAdrian Hunter 21942dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 21952dd6d8a1SAdrian Hunter { 2196dc0c6127SJiri Olsa struct record *rec = &record; 2197dc0c6127SJiri Olsa 21985f9cf599SWang Nan if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 21995f9cf599SWang Nan trigger_hit(&auxtrace_snapshot_trigger); 22002dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 1; 22015f9cf599SWang Nan if (auxtrace_record__snapshot_start(record.itr)) 22025f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 22035f9cf599SWang Nan } 22043c1cb7e3SWang Nan 2205dc0c6127SJiri Olsa if (switch_output_signal(rec)) 22063c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 22072dd6d8a1SAdrian Hunter } 2208bfacbe3bSJiri Olsa 2209bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 2210bfacbe3bSJiri Olsa { 2211bfacbe3bSJiri Olsa struct record *rec = &record; 2212bfacbe3bSJiri Olsa 2213bfacbe3bSJiri Olsa if (switch_output_time(rec)) 2214bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 2215bfacbe3bSJiri Olsa } 2216