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; 6503724b2eSAndi Kleen char **filenames; 6603724b2eSAndi Kleen int num_files; 6703724b2eSAndi Kleen int cur_file; 681b43b704SJiri Olsa }; 691b43b704SJiri Olsa 708c6f45a7SArnaldo Carvalho de Melo struct record { 7145694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 72b4006796SArnaldo Carvalho de Melo struct record_opts opts; 73d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 748ceb41d7SJiri Olsa struct perf_data data; 75ef149c25SAdrian Hunter struct auxtrace_record *itr; 76d20deb64SArnaldo Carvalho de Melo struct perf_evlist *evlist; 77d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 78d20deb64SArnaldo Carvalho de Melo int realtime_prio; 79d20deb64SArnaldo Carvalho de Melo bool no_buildid; 80d2db9a98SWang Nan bool no_buildid_set; 81d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 82d2db9a98SWang Nan bool no_buildid_cache_set; 836156681bSNamhyung Kim bool buildid_all; 84ecfd7a9cSWang Nan bool timestamp_filename; 8568588bafSJin Yao bool timestamp_boundary; 861b43b704SJiri Olsa struct switch_output switch_output; 879f065194SYang Shi unsigned long long samples; 889d2ed645SAlexey Budankov cpu_set_t affinity_mask; 890f82ebc4SArnaldo Carvalho de Melo }; 9086470930SIngo Molnar 91dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 92dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 93dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 94dc0c6127SJiri Olsa 959d2ed645SAlexey Budankov static const char *affinity_tags[PERF_AFFINITY_MAX] = { 969d2ed645SAlexey Budankov "SYS", "NODE", "CPU" 979d2ed645SAlexey Budankov }; 989d2ed645SAlexey Budankov 99dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 100dc0c6127SJiri Olsa { 101dc0c6127SJiri Olsa return rec->switch_output.signal && 102dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 103dc0c6127SJiri Olsa } 104dc0c6127SJiri Olsa 105dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 106dc0c6127SJiri Olsa { 107dc0c6127SJiri Olsa return rec->switch_output.size && 108dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 109dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 110dc0c6127SJiri Olsa } 111dc0c6127SJiri Olsa 112bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 113bfacbe3bSJiri Olsa { 114bfacbe3bSJiri Olsa return rec->switch_output.time && 115bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 116bfacbe3bSJiri Olsa } 117bfacbe3bSJiri Olsa 118ded2b8feSJiri Olsa static int record__write(struct record *rec, struct perf_mmap *map __maybe_unused, 119ded2b8feSJiri Olsa void *bf, size_t size) 120f5970550SPeter Zijlstra { 121ded2b8feSJiri Olsa struct perf_data_file *file = &rec->session->data->file; 122ded2b8feSJiri Olsa 123ded2b8feSJiri Olsa if (perf_data_file__write(file, bf, size) < 0) { 1244f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 1258d3eca20SDavid Ahern return -1; 1268d3eca20SDavid Ahern } 127f5970550SPeter Zijlstra 128cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 129dc0c6127SJiri Olsa 130dc0c6127SJiri Olsa if (switch_output_size(rec)) 131dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 132dc0c6127SJiri Olsa 1338d3eca20SDavid Ahern return 0; 134f5970550SPeter Zijlstra } 135f5970550SPeter Zijlstra 136ef781128SAlexey Budankov static int record__aio_enabled(struct record *rec); 137ef781128SAlexey Budankov static int record__comp_enabled(struct record *rec); 1385d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size, 1395d7f4116SAlexey Budankov void *src, size_t src_size); 1405d7f4116SAlexey Budankov 141d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 142d3d1af6fSAlexey Budankov static int record__aio_write(struct aiocb *cblock, int trace_fd, 143d3d1af6fSAlexey Budankov void *buf, size_t size, off_t off) 144d3d1af6fSAlexey Budankov { 145d3d1af6fSAlexey Budankov int rc; 146d3d1af6fSAlexey Budankov 147d3d1af6fSAlexey Budankov cblock->aio_fildes = trace_fd; 148d3d1af6fSAlexey Budankov cblock->aio_buf = buf; 149d3d1af6fSAlexey Budankov cblock->aio_nbytes = size; 150d3d1af6fSAlexey Budankov cblock->aio_offset = off; 151d3d1af6fSAlexey Budankov cblock->aio_sigevent.sigev_notify = SIGEV_NONE; 152d3d1af6fSAlexey Budankov 153d3d1af6fSAlexey Budankov do { 154d3d1af6fSAlexey Budankov rc = aio_write(cblock); 155d3d1af6fSAlexey Budankov if (rc == 0) { 156d3d1af6fSAlexey Budankov break; 157d3d1af6fSAlexey Budankov } else if (errno != EAGAIN) { 158d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 159d3d1af6fSAlexey Budankov pr_err("failed to queue perf data, error: %m\n"); 160d3d1af6fSAlexey Budankov break; 161d3d1af6fSAlexey Budankov } 162d3d1af6fSAlexey Budankov } while (1); 163d3d1af6fSAlexey Budankov 164d3d1af6fSAlexey Budankov return rc; 165d3d1af6fSAlexey Budankov } 166d3d1af6fSAlexey Budankov 167d3d1af6fSAlexey Budankov static int record__aio_complete(struct perf_mmap *md, struct aiocb *cblock) 168d3d1af6fSAlexey Budankov { 169d3d1af6fSAlexey Budankov void *rem_buf; 170d3d1af6fSAlexey Budankov off_t rem_off; 171d3d1af6fSAlexey Budankov size_t rem_size; 172d3d1af6fSAlexey Budankov int rc, aio_errno; 173d3d1af6fSAlexey Budankov ssize_t aio_ret, written; 174d3d1af6fSAlexey Budankov 175d3d1af6fSAlexey Budankov aio_errno = aio_error(cblock); 176d3d1af6fSAlexey Budankov if (aio_errno == EINPROGRESS) 177d3d1af6fSAlexey Budankov return 0; 178d3d1af6fSAlexey Budankov 179d3d1af6fSAlexey Budankov written = aio_ret = aio_return(cblock); 180d3d1af6fSAlexey Budankov if (aio_ret < 0) { 181d3d1af6fSAlexey Budankov if (aio_errno != EINTR) 182d3d1af6fSAlexey Budankov pr_err("failed to write perf data, error: %m\n"); 183d3d1af6fSAlexey Budankov written = 0; 184d3d1af6fSAlexey Budankov } 185d3d1af6fSAlexey Budankov 186d3d1af6fSAlexey Budankov rem_size = cblock->aio_nbytes - written; 187d3d1af6fSAlexey Budankov 188d3d1af6fSAlexey Budankov if (rem_size == 0) { 189d3d1af6fSAlexey Budankov cblock->aio_fildes = -1; 190d3d1af6fSAlexey Budankov /* 191ef781128SAlexey Budankov * md->refcount is incremented in record__aio_pushfn() for 192ef781128SAlexey Budankov * every aio write request started in record__aio_push() so 193ef781128SAlexey Budankov * decrement it because the request is now complete. 194d3d1af6fSAlexey Budankov */ 195d3d1af6fSAlexey Budankov perf_mmap__put(md); 196d3d1af6fSAlexey Budankov rc = 1; 197d3d1af6fSAlexey Budankov } else { 198d3d1af6fSAlexey Budankov /* 199d3d1af6fSAlexey Budankov * aio write request may require restart with the 200d3d1af6fSAlexey Budankov * reminder if the kernel didn't write whole 201d3d1af6fSAlexey Budankov * chunk at once. 202d3d1af6fSAlexey Budankov */ 203d3d1af6fSAlexey Budankov rem_off = cblock->aio_offset + written; 204d3d1af6fSAlexey Budankov rem_buf = (void *)(cblock->aio_buf + written); 205d3d1af6fSAlexey Budankov record__aio_write(cblock, cblock->aio_fildes, 206d3d1af6fSAlexey Budankov rem_buf, rem_size, rem_off); 207d3d1af6fSAlexey Budankov rc = 0; 208d3d1af6fSAlexey Budankov } 209d3d1af6fSAlexey Budankov 210d3d1af6fSAlexey Budankov return rc; 211d3d1af6fSAlexey Budankov } 212d3d1af6fSAlexey Budankov 21393f20c0fSAlexey Budankov static int record__aio_sync(struct perf_mmap *md, bool sync_all) 214d3d1af6fSAlexey Budankov { 21593f20c0fSAlexey Budankov struct aiocb **aiocb = md->aio.aiocb; 21693f20c0fSAlexey Budankov struct aiocb *cblocks = md->aio.cblocks; 217d3d1af6fSAlexey Budankov struct timespec timeout = { 0, 1000 * 1000 * 1 }; /* 1ms */ 21893f20c0fSAlexey Budankov int i, do_suspend; 219d3d1af6fSAlexey Budankov 220d3d1af6fSAlexey Budankov do { 22193f20c0fSAlexey Budankov do_suspend = 0; 22293f20c0fSAlexey Budankov for (i = 0; i < md->aio.nr_cblocks; ++i) { 22393f20c0fSAlexey Budankov if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) { 22493f20c0fSAlexey Budankov if (sync_all) 22593f20c0fSAlexey Budankov aiocb[i] = NULL; 22693f20c0fSAlexey Budankov else 22793f20c0fSAlexey Budankov return i; 22893f20c0fSAlexey Budankov } else { 22993f20c0fSAlexey Budankov /* 23093f20c0fSAlexey Budankov * Started aio write is not complete yet 23193f20c0fSAlexey Budankov * so it has to be waited before the 23293f20c0fSAlexey Budankov * next allocation. 23393f20c0fSAlexey Budankov */ 23493f20c0fSAlexey Budankov aiocb[i] = &cblocks[i]; 23593f20c0fSAlexey Budankov do_suspend = 1; 23693f20c0fSAlexey Budankov } 23793f20c0fSAlexey Budankov } 23893f20c0fSAlexey Budankov if (!do_suspend) 23993f20c0fSAlexey Budankov return -1; 240d3d1af6fSAlexey Budankov 24193f20c0fSAlexey Budankov while (aio_suspend((const struct aiocb **)aiocb, md->aio.nr_cblocks, &timeout)) { 242d3d1af6fSAlexey Budankov if (!(errno == EAGAIN || errno == EINTR)) 243d3d1af6fSAlexey Budankov pr_err("failed to sync perf data, error: %m\n"); 244d3d1af6fSAlexey Budankov } 245d3d1af6fSAlexey Budankov } while (1); 246d3d1af6fSAlexey Budankov } 247d3d1af6fSAlexey Budankov 248ef781128SAlexey Budankov struct record_aio { 249ef781128SAlexey Budankov struct record *rec; 250ef781128SAlexey Budankov void *data; 251ef781128SAlexey Budankov size_t size; 252ef781128SAlexey Budankov }; 253ef781128SAlexey Budankov 254ef781128SAlexey Budankov static int record__aio_pushfn(struct perf_mmap *map, void *to, void *buf, size_t size) 255d3d1af6fSAlexey Budankov { 256ef781128SAlexey Budankov struct record_aio *aio = to; 257ef781128SAlexey Budankov 258ef781128SAlexey Budankov /* 259ef781128SAlexey Budankov * map->base data pointed by buf is copied into free map->aio.data[] buffer 260ef781128SAlexey Budankov * to release space in the kernel buffer as fast as possible, calling 261ef781128SAlexey Budankov * perf_mmap__consume() from perf_mmap__push() function. 262ef781128SAlexey Budankov * 263ef781128SAlexey Budankov * That lets the kernel to proceed with storing more profiling data into 264ef781128SAlexey Budankov * the kernel buffer earlier than other per-cpu kernel buffers are handled. 265ef781128SAlexey Budankov * 266ef781128SAlexey Budankov * Coping can be done in two steps in case the chunk of profiling data 267ef781128SAlexey Budankov * crosses the upper bound of the kernel buffer. In this case we first move 268ef781128SAlexey Budankov * part of data from map->start till the upper bound and then the reminder 269ef781128SAlexey Budankov * from the beginning of the kernel buffer till the end of the data chunk. 270ef781128SAlexey Budankov */ 271ef781128SAlexey Budankov 272ef781128SAlexey Budankov if (record__comp_enabled(aio->rec)) { 273ef781128SAlexey Budankov size = zstd_compress(aio->rec->session, aio->data + aio->size, 274ef781128SAlexey Budankov perf_mmap__mmap_len(map) - aio->size, 275ef781128SAlexey Budankov buf, size); 276ef781128SAlexey Budankov } else { 277ef781128SAlexey Budankov memcpy(aio->data + aio->size, buf, size); 278ef781128SAlexey Budankov } 279ef781128SAlexey Budankov 280ef781128SAlexey Budankov if (!aio->size) { 281ef781128SAlexey Budankov /* 282ef781128SAlexey Budankov * Increment map->refcount to guard map->aio.data[] buffer 283ef781128SAlexey Budankov * from premature deallocation because map object can be 284ef781128SAlexey Budankov * released earlier than aio write request started on 285ef781128SAlexey Budankov * map->aio.data[] buffer is complete. 286ef781128SAlexey Budankov * 287ef781128SAlexey Budankov * perf_mmap__put() is done at record__aio_complete() 288ef781128SAlexey Budankov * after started aio request completion or at record__aio_push() 289ef781128SAlexey Budankov * if the request failed to start. 290ef781128SAlexey Budankov */ 291ef781128SAlexey Budankov perf_mmap__get(map); 292ef781128SAlexey Budankov } 293ef781128SAlexey Budankov 294ef781128SAlexey Budankov aio->size += size; 295ef781128SAlexey Budankov 296ef781128SAlexey Budankov return size; 297ef781128SAlexey Budankov } 298ef781128SAlexey Budankov 299ef781128SAlexey Budankov static int record__aio_push(struct record *rec, struct perf_mmap *map, off_t *off) 300ef781128SAlexey Budankov { 301ef781128SAlexey Budankov int ret, idx; 302ef781128SAlexey Budankov int trace_fd = rec->session->data->file.fd; 303ef781128SAlexey Budankov struct record_aio aio = { .rec = rec, .size = 0 }; 304ef781128SAlexey Budankov 305ef781128SAlexey Budankov /* 306ef781128SAlexey Budankov * Call record__aio_sync() to wait till map->aio.data[] buffer 307ef781128SAlexey Budankov * becomes available after previous aio write operation. 308ef781128SAlexey Budankov */ 309ef781128SAlexey Budankov 310ef781128SAlexey Budankov idx = record__aio_sync(map, false); 311ef781128SAlexey Budankov aio.data = map->aio.data[idx]; 312ef781128SAlexey Budankov ret = perf_mmap__push(map, &aio, record__aio_pushfn); 313ef781128SAlexey Budankov if (ret != 0) /* ret > 0 - no data, ret < 0 - error */ 314ef781128SAlexey Budankov return ret; 315d3d1af6fSAlexey Budankov 316d3d1af6fSAlexey Budankov rec->samples++; 317ef781128SAlexey Budankov ret = record__aio_write(&(map->aio.cblocks[idx]), trace_fd, aio.data, aio.size, *off); 318d3d1af6fSAlexey Budankov if (!ret) { 319ef781128SAlexey Budankov *off += aio.size; 320ef781128SAlexey Budankov rec->bytes_written += aio.size; 321d3d1af6fSAlexey Budankov if (switch_output_size(rec)) 322d3d1af6fSAlexey Budankov trigger_hit(&switch_output_trigger); 323ef781128SAlexey Budankov } else { 324ef781128SAlexey Budankov /* 325ef781128SAlexey Budankov * Decrement map->refcount incremented in record__aio_pushfn() 326ef781128SAlexey Budankov * back if record__aio_write() operation failed to start, otherwise 327ef781128SAlexey Budankov * map->refcount is decremented in record__aio_complete() after 328ef781128SAlexey Budankov * aio write operation finishes successfully. 329ef781128SAlexey Budankov */ 330ef781128SAlexey Budankov perf_mmap__put(map); 331d3d1af6fSAlexey Budankov } 332d3d1af6fSAlexey Budankov 333d3d1af6fSAlexey Budankov return ret; 334d3d1af6fSAlexey Budankov } 335d3d1af6fSAlexey Budankov 336d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd) 337d3d1af6fSAlexey Budankov { 338d3d1af6fSAlexey Budankov return lseek(trace_fd, 0, SEEK_CUR); 339d3d1af6fSAlexey Budankov } 340d3d1af6fSAlexey Budankov 341d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd, off_t pos) 342d3d1af6fSAlexey Budankov { 343d3d1af6fSAlexey Budankov lseek(trace_fd, pos, SEEK_SET); 344d3d1af6fSAlexey Budankov } 345d3d1af6fSAlexey Budankov 346d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec) 347d3d1af6fSAlexey Budankov { 348d3d1af6fSAlexey Budankov int i; 349d3d1af6fSAlexey Budankov struct perf_evlist *evlist = rec->evlist; 350d3d1af6fSAlexey Budankov struct perf_mmap *maps = evlist->mmap; 351d3d1af6fSAlexey Budankov 352ef781128SAlexey Budankov if (!record__aio_enabled(rec)) 353d3d1af6fSAlexey Budankov return; 354d3d1af6fSAlexey Budankov 355d3d1af6fSAlexey Budankov for (i = 0; i < evlist->nr_mmaps; i++) { 356d3d1af6fSAlexey Budankov struct perf_mmap *map = &maps[i]; 357d3d1af6fSAlexey Budankov 358d3d1af6fSAlexey Budankov if (map->base) 35993f20c0fSAlexey Budankov record__aio_sync(map, true); 360d3d1af6fSAlexey Budankov } 361d3d1af6fSAlexey Budankov } 362d3d1af6fSAlexey Budankov 363d3d1af6fSAlexey Budankov static int nr_cblocks_default = 1; 36493f20c0fSAlexey Budankov static int nr_cblocks_max = 4; 365d3d1af6fSAlexey Budankov 366d3d1af6fSAlexey Budankov static int record__aio_parse(const struct option *opt, 36793f20c0fSAlexey Budankov const char *str, 368d3d1af6fSAlexey Budankov int unset) 369d3d1af6fSAlexey Budankov { 370d3d1af6fSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 371d3d1af6fSAlexey Budankov 37293f20c0fSAlexey Budankov if (unset) { 373d3d1af6fSAlexey Budankov opts->nr_cblocks = 0; 37493f20c0fSAlexey Budankov } else { 37593f20c0fSAlexey Budankov if (str) 37693f20c0fSAlexey Budankov opts->nr_cblocks = strtol(str, NULL, 0); 37793f20c0fSAlexey Budankov if (!opts->nr_cblocks) 378d3d1af6fSAlexey Budankov opts->nr_cblocks = nr_cblocks_default; 37993f20c0fSAlexey Budankov } 380d3d1af6fSAlexey Budankov 381d3d1af6fSAlexey Budankov return 0; 382d3d1af6fSAlexey Budankov } 383d3d1af6fSAlexey Budankov #else /* HAVE_AIO_SUPPORT */ 38493f20c0fSAlexey Budankov static int nr_cblocks_max = 0; 38593f20c0fSAlexey Budankov 386ef781128SAlexey Budankov static int record__aio_push(struct record *rec __maybe_unused, struct perf_mmap *map __maybe_unused, 387ef781128SAlexey Budankov off_t *off __maybe_unused) 388d3d1af6fSAlexey Budankov { 389d3d1af6fSAlexey Budankov return -1; 390d3d1af6fSAlexey Budankov } 391d3d1af6fSAlexey Budankov 392d3d1af6fSAlexey Budankov static off_t record__aio_get_pos(int trace_fd __maybe_unused) 393d3d1af6fSAlexey Budankov { 394d3d1af6fSAlexey Budankov return -1; 395d3d1af6fSAlexey Budankov } 396d3d1af6fSAlexey Budankov 397d3d1af6fSAlexey Budankov static void record__aio_set_pos(int trace_fd __maybe_unused, off_t pos __maybe_unused) 398d3d1af6fSAlexey Budankov { 399d3d1af6fSAlexey Budankov } 400d3d1af6fSAlexey Budankov 401d3d1af6fSAlexey Budankov static void record__aio_mmap_read_sync(struct record *rec __maybe_unused) 402d3d1af6fSAlexey Budankov { 403d3d1af6fSAlexey Budankov } 404d3d1af6fSAlexey Budankov #endif 405d3d1af6fSAlexey Budankov 406d3d1af6fSAlexey Budankov static int record__aio_enabled(struct record *rec) 407d3d1af6fSAlexey Budankov { 408d3d1af6fSAlexey Budankov return rec->opts.nr_cblocks > 0; 409d3d1af6fSAlexey Budankov } 410d3d1af6fSAlexey Budankov 411470530bbSAlexey Budankov #define MMAP_FLUSH_DEFAULT 1 412470530bbSAlexey Budankov static int record__mmap_flush_parse(const struct option *opt, 413470530bbSAlexey Budankov const char *str, 414470530bbSAlexey Budankov int unset) 415470530bbSAlexey Budankov { 416470530bbSAlexey Budankov int flush_max; 417470530bbSAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 418470530bbSAlexey Budankov static struct parse_tag tags[] = { 419470530bbSAlexey Budankov { .tag = 'B', .mult = 1 }, 420470530bbSAlexey Budankov { .tag = 'K', .mult = 1 << 10 }, 421470530bbSAlexey Budankov { .tag = 'M', .mult = 1 << 20 }, 422470530bbSAlexey Budankov { .tag = 'G', .mult = 1 << 30 }, 423470530bbSAlexey Budankov { .tag = 0 }, 424470530bbSAlexey Budankov }; 425470530bbSAlexey Budankov 426470530bbSAlexey Budankov if (unset) 427470530bbSAlexey Budankov return 0; 428470530bbSAlexey Budankov 429470530bbSAlexey Budankov if (str) { 430470530bbSAlexey Budankov opts->mmap_flush = parse_tag_value(str, tags); 431470530bbSAlexey Budankov if (opts->mmap_flush == (int)-1) 432470530bbSAlexey Budankov opts->mmap_flush = strtol(str, NULL, 0); 433470530bbSAlexey Budankov } 434470530bbSAlexey Budankov 435470530bbSAlexey Budankov if (!opts->mmap_flush) 436470530bbSAlexey Budankov opts->mmap_flush = MMAP_FLUSH_DEFAULT; 437470530bbSAlexey Budankov 438470530bbSAlexey Budankov flush_max = perf_evlist__mmap_size(opts->mmap_pages); 439470530bbSAlexey Budankov flush_max /= 4; 440470530bbSAlexey Budankov if (opts->mmap_flush > flush_max) 441470530bbSAlexey Budankov opts->mmap_flush = flush_max; 442470530bbSAlexey Budankov 443470530bbSAlexey Budankov return 0; 444470530bbSAlexey Budankov } 445470530bbSAlexey Budankov 446*504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 447*504c1ad1SAlexey Budankov static unsigned int comp_level_default = 1; 448*504c1ad1SAlexey Budankov 449*504c1ad1SAlexey Budankov static int record__parse_comp_level(const struct option *opt, const char *str, int unset) 450*504c1ad1SAlexey Budankov { 451*504c1ad1SAlexey Budankov struct record_opts *opts = opt->value; 452*504c1ad1SAlexey Budankov 453*504c1ad1SAlexey Budankov if (unset) { 454*504c1ad1SAlexey Budankov opts->comp_level = 0; 455*504c1ad1SAlexey Budankov } else { 456*504c1ad1SAlexey Budankov if (str) 457*504c1ad1SAlexey Budankov opts->comp_level = strtol(str, NULL, 0); 458*504c1ad1SAlexey Budankov if (!opts->comp_level) 459*504c1ad1SAlexey Budankov opts->comp_level = comp_level_default; 460*504c1ad1SAlexey Budankov } 461*504c1ad1SAlexey Budankov 462*504c1ad1SAlexey Budankov return 0; 463*504c1ad1SAlexey Budankov } 464*504c1ad1SAlexey Budankov #endif 46551255a8aSAlexey Budankov static unsigned int comp_level_max = 22; 46651255a8aSAlexey Budankov 46742e1fd80SAlexey Budankov static int record__comp_enabled(struct record *rec) 46842e1fd80SAlexey Budankov { 46942e1fd80SAlexey Budankov return rec->opts.comp_level > 0; 47042e1fd80SAlexey Budankov } 47142e1fd80SAlexey Budankov 47245694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 473d20deb64SArnaldo Carvalho de Melo union perf_event *event, 4741d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 4751d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 476234fbbf5SArnaldo Carvalho de Melo { 4778c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 478ded2b8feSJiri Olsa return record__write(rec, NULL, event, event->header.size); 479234fbbf5SArnaldo Carvalho de Melo } 480234fbbf5SArnaldo Carvalho de Melo 481ded2b8feSJiri Olsa static int record__pushfn(struct perf_mmap *map, void *to, void *bf, size_t size) 482d37f1586SArnaldo Carvalho de Melo { 483d37f1586SArnaldo Carvalho de Melo struct record *rec = to; 484d37f1586SArnaldo Carvalho de Melo 4855d7f4116SAlexey Budankov if (record__comp_enabled(rec)) { 4865d7f4116SAlexey Budankov size = zstd_compress(rec->session, map->data, perf_mmap__mmap_len(map), bf, size); 4875d7f4116SAlexey Budankov bf = map->data; 4885d7f4116SAlexey Budankov } 4895d7f4116SAlexey Budankov 490d37f1586SArnaldo Carvalho de Melo rec->samples++; 491ded2b8feSJiri Olsa return record__write(rec, map, bf, size); 492d37f1586SArnaldo Carvalho de Melo } 493d37f1586SArnaldo Carvalho de Melo 4942dd6d8a1SAdrian Hunter static volatile int done; 4952dd6d8a1SAdrian Hunter static volatile int signr = -1; 4962dd6d8a1SAdrian Hunter static volatile int child_finished; 497c0bdc1c4SWang Nan 4982dd6d8a1SAdrian Hunter static void sig_handler(int sig) 4992dd6d8a1SAdrian Hunter { 5002dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 5012dd6d8a1SAdrian Hunter child_finished = 1; 5022dd6d8a1SAdrian Hunter else 5032dd6d8a1SAdrian Hunter signr = sig; 5042dd6d8a1SAdrian Hunter 5052dd6d8a1SAdrian Hunter done = 1; 5062dd6d8a1SAdrian Hunter } 5072dd6d8a1SAdrian Hunter 508a074865eSWang Nan static void sigsegv_handler(int sig) 509a074865eSWang Nan { 510a074865eSWang Nan perf_hooks__recover(); 511a074865eSWang Nan sighandler_dump_stack(sig); 512a074865eSWang Nan } 513a074865eSWang Nan 5142dd6d8a1SAdrian Hunter static void record__sig_exit(void) 5152dd6d8a1SAdrian Hunter { 5162dd6d8a1SAdrian Hunter if (signr == -1) 5172dd6d8a1SAdrian Hunter return; 5182dd6d8a1SAdrian Hunter 5192dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 5202dd6d8a1SAdrian Hunter raise(signr); 5212dd6d8a1SAdrian Hunter } 5222dd6d8a1SAdrian Hunter 523e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 524e31f0d01SAdrian Hunter 525ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 526ded2b8feSJiri Olsa struct perf_mmap *map, 527ef149c25SAdrian Hunter union perf_event *event, void *data1, 528ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 529ef149c25SAdrian Hunter { 530ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 5318ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 532ef149c25SAdrian Hunter size_t padding; 533ef149c25SAdrian Hunter u8 pad[8] = {0}; 534ef149c25SAdrian Hunter 535cd3dd8ddSJiri Olsa if (!perf_data__is_pipe(data) && !perf_data__is_dir(data)) { 53699fa2984SAdrian Hunter off_t file_offset; 5378ceb41d7SJiri Olsa int fd = perf_data__fd(data); 53899fa2984SAdrian Hunter int err; 53999fa2984SAdrian Hunter 54099fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 54199fa2984SAdrian Hunter if (file_offset == -1) 54299fa2984SAdrian Hunter return -1; 54399fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 54499fa2984SAdrian Hunter event, file_offset); 54599fa2984SAdrian Hunter if (err) 54699fa2984SAdrian Hunter return err; 54799fa2984SAdrian Hunter } 54899fa2984SAdrian Hunter 549ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 550ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 551ef149c25SAdrian Hunter if (padding) 552ef149c25SAdrian Hunter padding = 8 - padding; 553ef149c25SAdrian Hunter 554ded2b8feSJiri Olsa record__write(rec, map, event, event->header.size); 555ded2b8feSJiri Olsa record__write(rec, map, data1, len1); 556ef149c25SAdrian Hunter if (len2) 557ded2b8feSJiri Olsa record__write(rec, map, data2, len2); 558ded2b8feSJiri Olsa record__write(rec, map, &pad, padding); 559ef149c25SAdrian Hunter 560ef149c25SAdrian Hunter return 0; 561ef149c25SAdrian Hunter } 562ef149c25SAdrian Hunter 563ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 564e035f4caSJiri Olsa struct perf_mmap *map) 565ef149c25SAdrian Hunter { 566ef149c25SAdrian Hunter int ret; 567ef149c25SAdrian Hunter 568e035f4caSJiri Olsa ret = auxtrace_mmap__read(map, rec->itr, &rec->tool, 569ef149c25SAdrian Hunter record__process_auxtrace); 570ef149c25SAdrian Hunter if (ret < 0) 571ef149c25SAdrian Hunter return ret; 572ef149c25SAdrian Hunter 573ef149c25SAdrian Hunter if (ret) 574ef149c25SAdrian Hunter rec->samples++; 575ef149c25SAdrian Hunter 576ef149c25SAdrian Hunter return 0; 577ef149c25SAdrian Hunter } 578ef149c25SAdrian Hunter 5792dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 580e035f4caSJiri Olsa struct perf_mmap *map) 5812dd6d8a1SAdrian Hunter { 5822dd6d8a1SAdrian Hunter int ret; 5832dd6d8a1SAdrian Hunter 584e035f4caSJiri Olsa ret = auxtrace_mmap__read_snapshot(map, rec->itr, &rec->tool, 5852dd6d8a1SAdrian Hunter record__process_auxtrace, 5862dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 5872dd6d8a1SAdrian Hunter if (ret < 0) 5882dd6d8a1SAdrian Hunter return ret; 5892dd6d8a1SAdrian Hunter 5902dd6d8a1SAdrian Hunter if (ret) 5912dd6d8a1SAdrian Hunter rec->samples++; 5922dd6d8a1SAdrian Hunter 5932dd6d8a1SAdrian Hunter return 0; 5942dd6d8a1SAdrian Hunter } 5952dd6d8a1SAdrian Hunter 5962dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 5972dd6d8a1SAdrian Hunter { 5982dd6d8a1SAdrian Hunter int i; 5992dd6d8a1SAdrian Hunter int rc = 0; 6002dd6d8a1SAdrian Hunter 6012dd6d8a1SAdrian Hunter for (i = 0; i < rec->evlist->nr_mmaps; i++) { 602e035f4caSJiri Olsa struct perf_mmap *map = &rec->evlist->mmap[i]; 6032dd6d8a1SAdrian Hunter 604e035f4caSJiri Olsa if (!map->auxtrace_mmap.base) 6052dd6d8a1SAdrian Hunter continue; 6062dd6d8a1SAdrian Hunter 607e035f4caSJiri Olsa if (record__auxtrace_mmap_read_snapshot(rec, map) != 0) { 6082dd6d8a1SAdrian Hunter rc = -1; 6092dd6d8a1SAdrian Hunter goto out; 6102dd6d8a1SAdrian Hunter } 6112dd6d8a1SAdrian Hunter } 6122dd6d8a1SAdrian Hunter out: 6132dd6d8a1SAdrian Hunter return rc; 6142dd6d8a1SAdrian Hunter } 6152dd6d8a1SAdrian Hunter 6162dd6d8a1SAdrian Hunter static void record__read_auxtrace_snapshot(struct record *rec) 6172dd6d8a1SAdrian Hunter { 6182dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 6192dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 6205f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 6212dd6d8a1SAdrian Hunter } else { 6225f9cf599SWang Nan if (auxtrace_record__snapshot_finish(rec->itr)) 6235f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 6245f9cf599SWang Nan else 6255f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 6262dd6d8a1SAdrian Hunter } 6272dd6d8a1SAdrian Hunter } 6282dd6d8a1SAdrian Hunter 6294b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec) 6304b5ea3bdSAdrian Hunter { 6314b5ea3bdSAdrian Hunter int err; 6324b5ea3bdSAdrian Hunter 6334b5ea3bdSAdrian Hunter if (!rec->itr) { 6344b5ea3bdSAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 6354b5ea3bdSAdrian Hunter if (err) 6364b5ea3bdSAdrian Hunter return err; 6374b5ea3bdSAdrian Hunter } 6384b5ea3bdSAdrian Hunter 6394b5ea3bdSAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 6404b5ea3bdSAdrian Hunter rec->opts.auxtrace_snapshot_opts); 6414b5ea3bdSAdrian Hunter if (err) 6424b5ea3bdSAdrian Hunter return err; 6434b5ea3bdSAdrian Hunter 6444b5ea3bdSAdrian Hunter return auxtrace_parse_filters(rec->evlist); 6454b5ea3bdSAdrian Hunter } 6464b5ea3bdSAdrian Hunter 647e31f0d01SAdrian Hunter #else 648e31f0d01SAdrian Hunter 649e31f0d01SAdrian Hunter static inline 650e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 651e035f4caSJiri Olsa struct perf_mmap *map __maybe_unused) 652e31f0d01SAdrian Hunter { 653e31f0d01SAdrian Hunter return 0; 654e31f0d01SAdrian Hunter } 655e31f0d01SAdrian Hunter 6562dd6d8a1SAdrian Hunter static inline 6572dd6d8a1SAdrian Hunter void record__read_auxtrace_snapshot(struct record *rec __maybe_unused) 6582dd6d8a1SAdrian Hunter { 6592dd6d8a1SAdrian Hunter } 6602dd6d8a1SAdrian Hunter 6612dd6d8a1SAdrian Hunter static inline 6622dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 6632dd6d8a1SAdrian Hunter { 6642dd6d8a1SAdrian Hunter return 0; 6652dd6d8a1SAdrian Hunter } 6662dd6d8a1SAdrian Hunter 6674b5ea3bdSAdrian Hunter static int record__auxtrace_init(struct record *rec __maybe_unused) 6684b5ea3bdSAdrian Hunter { 6694b5ea3bdSAdrian Hunter return 0; 6704b5ea3bdSAdrian Hunter } 6714b5ea3bdSAdrian Hunter 672e31f0d01SAdrian Hunter #endif 673e31f0d01SAdrian Hunter 674cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 675cda57a8cSWang Nan struct perf_evlist *evlist) 676cda57a8cSWang Nan { 677cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 678cda57a8cSWang Nan char msg[512]; 679cda57a8cSWang Nan 680f13de660SAlexey Budankov if (opts->affinity != PERF_AFFINITY_SYS) 681f13de660SAlexey Budankov cpu__setup_cpunode_map(); 682f13de660SAlexey Budankov 6837a276ff6SWang Nan if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, 684cda57a8cSWang Nan opts->auxtrace_mmap_pages, 6859d2ed645SAlexey Budankov opts->auxtrace_snapshot_mode, 686470530bbSAlexey Budankov opts->nr_cblocks, opts->affinity, 68751255a8aSAlexey Budankov opts->mmap_flush, opts->comp_level) < 0) { 688cda57a8cSWang Nan if (errno == EPERM) { 689cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 690cda57a8cSWang Nan "Consider increasing " 691cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 692cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 693cda57a8cSWang Nan "(current value: %u,%u)\n", 694cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 695cda57a8cSWang Nan return -errno; 696cda57a8cSWang Nan } else { 697cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 698c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 699cda57a8cSWang Nan if (errno) 700cda57a8cSWang Nan return -errno; 701cda57a8cSWang Nan else 702cda57a8cSWang Nan return -EINVAL; 703cda57a8cSWang Nan } 704cda57a8cSWang Nan } 705cda57a8cSWang Nan return 0; 706cda57a8cSWang Nan } 707cda57a8cSWang Nan 708cda57a8cSWang Nan static int record__mmap(struct record *rec) 709cda57a8cSWang Nan { 710cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 711cda57a8cSWang Nan } 712cda57a8cSWang Nan 7138c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 714dd7927f4SArnaldo Carvalho de Melo { 715d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 7166a4bb04cSJiri Olsa struct perf_evsel *pos; 717d20deb64SArnaldo Carvalho de Melo struct perf_evlist *evlist = rec->evlist; 718d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 719b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 7208d3eca20SDavid Ahern int rc = 0; 721dd7927f4SArnaldo Carvalho de Melo 722d3dbf43cSArnaldo Carvalho de Melo /* 723d3dbf43cSArnaldo Carvalho de Melo * For initial_delay we need to add a dummy event so that we can track 724d3dbf43cSArnaldo Carvalho de Melo * PERF_RECORD_MMAP while we wait for the initial delay to enable the 725d3dbf43cSArnaldo Carvalho de Melo * real events, the ones asked by the user. 726d3dbf43cSArnaldo Carvalho de Melo */ 727d3dbf43cSArnaldo Carvalho de Melo if (opts->initial_delay) { 728d3dbf43cSArnaldo Carvalho de Melo if (perf_evlist__add_dummy(evlist)) 729d3dbf43cSArnaldo Carvalho de Melo return -ENOMEM; 730d3dbf43cSArnaldo Carvalho de Melo 731d3dbf43cSArnaldo Carvalho de Melo pos = perf_evlist__first(evlist); 732d3dbf43cSArnaldo Carvalho de Melo pos->tracking = 0; 733d3dbf43cSArnaldo Carvalho de Melo pos = perf_evlist__last(evlist); 734d3dbf43cSArnaldo Carvalho de Melo pos->tracking = 1; 735d3dbf43cSArnaldo Carvalho de Melo pos->attr.enable_on_exec = 1; 736d3dbf43cSArnaldo Carvalho de Melo } 737d3dbf43cSArnaldo Carvalho de Melo 738e68ae9cfSArnaldo Carvalho de Melo perf_evlist__config(evlist, opts, &callchain_param); 739cac21425SJiri Olsa 740e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 7413da297a6SIngo Molnar try_again: 742d988d5eeSKan Liang if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) { 74356e52e85SArnaldo Carvalho de Melo if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) { 744bb963e16SNamhyung Kim if (verbose > 0) 745c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 7463da297a6SIngo Molnar goto try_again; 7473da297a6SIngo Molnar } 748cf99ad14SAndi Kleen if ((errno == EINVAL || errno == EBADF) && 749cf99ad14SAndi Kleen pos->leader != pos && 750cf99ad14SAndi Kleen pos->weak_group) { 751cf99ad14SAndi Kleen pos = perf_evlist__reset_weak_group(evlist, pos); 752cf99ad14SAndi Kleen goto try_again; 753cf99ad14SAndi Kleen } 75456e52e85SArnaldo Carvalho de Melo rc = -errno; 75556e52e85SArnaldo Carvalho de Melo perf_evsel__open_strerror(pos, &opts->target, 75656e52e85SArnaldo Carvalho de Melo errno, msg, sizeof(msg)); 75756e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 7588d3eca20SDavid Ahern goto out; 7597c6a1c65SPeter Zijlstra } 760bfd8f72cSAndi Kleen 761bfd8f72cSAndi Kleen pos->supported = true; 7627c6a1c65SPeter Zijlstra } 7637c6a1c65SPeter Zijlstra 76423d4aad4SArnaldo Carvalho de Melo if (perf_evlist__apply_filters(evlist, &pos)) { 76562d94b00SArnaldo Carvalho de Melo pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 76623d4aad4SArnaldo Carvalho de Melo pos->filter, perf_evsel__name(pos), errno, 767c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 7688d3eca20SDavid Ahern rc = -1; 7698d3eca20SDavid Ahern goto out; 7700a102479SFrederic Weisbecker } 7710a102479SFrederic Weisbecker 772cda57a8cSWang Nan rc = record__mmap(rec); 773cda57a8cSWang Nan if (rc) 7748d3eca20SDavid Ahern goto out; 7750a27d7f9SArnaldo Carvalho de Melo 776a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 7777b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 7788d3eca20SDavid Ahern out: 7798d3eca20SDavid Ahern return rc; 780a91e5431SArnaldo Carvalho de Melo } 781a91e5431SArnaldo Carvalho de Melo 782e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 783e3d59112SNamhyung Kim union perf_event *event, 784e3d59112SNamhyung Kim struct perf_sample *sample, 785e3d59112SNamhyung Kim struct perf_evsel *evsel, 786e3d59112SNamhyung Kim struct machine *machine) 787e3d59112SNamhyung Kim { 788e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 789e3d59112SNamhyung Kim 79068588bafSJin Yao if (rec->evlist->first_sample_time == 0) 79168588bafSJin Yao rec->evlist->first_sample_time = sample->time; 792e3d59112SNamhyung Kim 79368588bafSJin Yao rec->evlist->last_sample_time = sample->time; 79468588bafSJin Yao 79568588bafSJin Yao if (rec->buildid_all) 79668588bafSJin Yao return 0; 79768588bafSJin Yao 79868588bafSJin Yao rec->samples++; 799e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 800e3d59112SNamhyung Kim } 801e3d59112SNamhyung Kim 8028c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 8036122e4e4SArnaldo Carvalho de Melo { 804f5fc1412SJiri Olsa struct perf_session *session = rec->session; 8056122e4e4SArnaldo Carvalho de Melo 80645112e89SJiri Olsa if (perf_data__size(&rec->data) == 0) 8079f591fd7SArnaldo Carvalho de Melo return 0; 8089f591fd7SArnaldo Carvalho de Melo 80900dc8657SNamhyung Kim /* 81000dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 81100dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 81200dc8657SNamhyung Kim * we prefer the vmlinux path like 81300dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 81400dc8657SNamhyung Kim * 81500dc8657SNamhyung Kim * rather than build-id path (in debug directory). 81600dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 81700dc8657SNamhyung Kim */ 81800dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 81900dc8657SNamhyung Kim 8206156681bSNamhyung Kim /* 8216156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 82268588bafSJin Yao * so no need to process samples. But if timestamp_boundary is enabled, 82368588bafSJin Yao * it still needs to walk on all samples to get the timestamps of 82468588bafSJin Yao * first/last samples. 8256156681bSNamhyung Kim */ 82668588bafSJin Yao if (rec->buildid_all && !rec->timestamp_boundary) 8276156681bSNamhyung Kim rec->tool.sample = NULL; 8286156681bSNamhyung Kim 829b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 8306122e4e4SArnaldo Carvalho de Melo } 8316122e4e4SArnaldo Carvalho de Melo 8328115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 833a1645ce1SZhang, Yanmin { 834a1645ce1SZhang, Yanmin int err; 83545694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 836a1645ce1SZhang, Yanmin /* 837a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 838a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 839a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 840a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 841a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 842a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 843a1645ce1SZhang, Yanmin */ 84445694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 845743eb868SArnaldo Carvalho de Melo machine); 846a1645ce1SZhang, Yanmin if (err < 0) 847a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 84823346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 849a1645ce1SZhang, Yanmin 850a1645ce1SZhang, Yanmin /* 851a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 852a1645ce1SZhang, Yanmin * have no _text sometimes. 853a1645ce1SZhang, Yanmin */ 85445694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 8550ae617beSAdrian Hunter machine); 856a1645ce1SZhang, Yanmin if (err < 0) 857a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 85823346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 859a1645ce1SZhang, Yanmin } 860a1645ce1SZhang, Yanmin 86198402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 86298402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 86398402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 86498402807SFrederic Weisbecker }; 86598402807SFrederic Weisbecker 866f13de660SAlexey Budankov static void record__adjust_affinity(struct record *rec, struct perf_mmap *map) 867f13de660SAlexey Budankov { 868f13de660SAlexey Budankov if (rec->opts.affinity != PERF_AFFINITY_SYS && 869f13de660SAlexey Budankov !CPU_EQUAL(&rec->affinity_mask, &map->affinity_mask)) { 870f13de660SAlexey Budankov CPU_ZERO(&rec->affinity_mask); 871f13de660SAlexey Budankov CPU_OR(&rec->affinity_mask, &rec->affinity_mask, &map->affinity_mask); 872f13de660SAlexey Budankov sched_setaffinity(0, sizeof(rec->affinity_mask), &rec->affinity_mask); 873f13de660SAlexey Budankov } 874f13de660SAlexey Budankov } 875f13de660SAlexey Budankov 8765d7f4116SAlexey Budankov static size_t process_comp_header(void *record, size_t increment) 8775d7f4116SAlexey Budankov { 8785d7f4116SAlexey Budankov struct compressed_event *event = record; 8795d7f4116SAlexey Budankov size_t size = sizeof(*event); 8805d7f4116SAlexey Budankov 8815d7f4116SAlexey Budankov if (increment) { 8825d7f4116SAlexey Budankov event->header.size += increment; 8835d7f4116SAlexey Budankov return increment; 8845d7f4116SAlexey Budankov } 8855d7f4116SAlexey Budankov 8865d7f4116SAlexey Budankov event->header.type = PERF_RECORD_COMPRESSED; 8875d7f4116SAlexey Budankov event->header.size = size; 8885d7f4116SAlexey Budankov 8895d7f4116SAlexey Budankov return size; 8905d7f4116SAlexey Budankov } 8915d7f4116SAlexey Budankov 8925d7f4116SAlexey Budankov static size_t zstd_compress(struct perf_session *session, void *dst, size_t dst_size, 8935d7f4116SAlexey Budankov void *src, size_t src_size) 8945d7f4116SAlexey Budankov { 8955d7f4116SAlexey Budankov size_t compressed; 8965d7f4116SAlexey Budankov size_t max_record_size = PERF_SAMPLE_MAX_SIZE - sizeof(struct compressed_event) - 1; 8975d7f4116SAlexey Budankov 8985d7f4116SAlexey Budankov compressed = zstd_compress_stream_to_records(&session->zstd_data, dst, dst_size, src, src_size, 8995d7f4116SAlexey Budankov max_record_size, process_comp_header); 9005d7f4116SAlexey Budankov 9015d7f4116SAlexey Budankov session->bytes_transferred += src_size; 9025d7f4116SAlexey Budankov session->bytes_compressed += compressed; 9035d7f4116SAlexey Budankov 9045d7f4116SAlexey Budankov return compressed; 9055d7f4116SAlexey Budankov } 9065d7f4116SAlexey Budankov 907a4ea0ec4SWang Nan static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist, 908470530bbSAlexey Budankov bool overwrite, bool synch) 90998402807SFrederic Weisbecker { 910dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 9110e2e63ddSPeter Zijlstra int i; 9128d3eca20SDavid Ahern int rc = 0; 913a4ea0ec4SWang Nan struct perf_mmap *maps; 914d3d1af6fSAlexey Budankov int trace_fd = rec->data.file.fd; 915ef781128SAlexey Budankov off_t off = 0; 91698402807SFrederic Weisbecker 917cb21686bSWang Nan if (!evlist) 918cb21686bSWang Nan return 0; 919ef149c25SAdrian Hunter 9200b72d69aSWang Nan maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 921a4ea0ec4SWang Nan if (!maps) 922a4ea0ec4SWang Nan return 0; 923cb21686bSWang Nan 9240b72d69aSWang Nan if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 92554cc54deSWang Nan return 0; 92654cc54deSWang Nan 927d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 928d3d1af6fSAlexey Budankov off = record__aio_get_pos(trace_fd); 929d3d1af6fSAlexey Budankov 930a4ea0ec4SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 931470530bbSAlexey Budankov u64 flush = 0; 932e035f4caSJiri Olsa struct perf_mmap *map = &maps[i]; 933a4ea0ec4SWang Nan 934e035f4caSJiri Olsa if (map->base) { 935f13de660SAlexey Budankov record__adjust_affinity(rec, map); 936470530bbSAlexey Budankov if (synch) { 937470530bbSAlexey Budankov flush = map->flush; 938470530bbSAlexey Budankov map->flush = 1; 939470530bbSAlexey Budankov } 940d3d1af6fSAlexey Budankov if (!record__aio_enabled(rec)) { 941ef781128SAlexey Budankov if (perf_mmap__push(map, rec, record__pushfn) < 0) { 942470530bbSAlexey Budankov if (synch) 943470530bbSAlexey Budankov map->flush = flush; 9448d3eca20SDavid Ahern rc = -1; 9458d3eca20SDavid Ahern goto out; 9468d3eca20SDavid Ahern } 947d3d1af6fSAlexey Budankov } else { 948ef781128SAlexey Budankov if (record__aio_push(rec, map, &off) < 0) { 949d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 950470530bbSAlexey Budankov if (synch) 951470530bbSAlexey Budankov map->flush = flush; 952d3d1af6fSAlexey Budankov rc = -1; 953d3d1af6fSAlexey Budankov goto out; 954d3d1af6fSAlexey Budankov } 955d3d1af6fSAlexey Budankov } 956470530bbSAlexey Budankov if (synch) 957470530bbSAlexey Budankov map->flush = flush; 9588d3eca20SDavid Ahern } 959ef149c25SAdrian Hunter 960e035f4caSJiri Olsa if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode && 961e035f4caSJiri Olsa record__auxtrace_mmap_read(rec, map) != 0) { 962ef149c25SAdrian Hunter rc = -1; 963ef149c25SAdrian Hunter goto out; 964ef149c25SAdrian Hunter } 96598402807SFrederic Weisbecker } 96698402807SFrederic Weisbecker 967d3d1af6fSAlexey Budankov if (record__aio_enabled(rec)) 968d3d1af6fSAlexey Budankov record__aio_set_pos(trace_fd, off); 969d3d1af6fSAlexey Budankov 970dcabb507SJiri Olsa /* 971dcabb507SJiri Olsa * Mark the round finished in case we wrote 972dcabb507SJiri Olsa * at least one event. 973dcabb507SJiri Olsa */ 974dcabb507SJiri Olsa if (bytes_written != rec->bytes_written) 975ded2b8feSJiri Olsa rc = record__write(rec, NULL, &finished_round_event, sizeof(finished_round_event)); 9768d3eca20SDavid Ahern 9770b72d69aSWang Nan if (overwrite) 97854cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 9798d3eca20SDavid Ahern out: 9808d3eca20SDavid Ahern return rc; 98198402807SFrederic Weisbecker } 98298402807SFrederic Weisbecker 983470530bbSAlexey Budankov static int record__mmap_read_all(struct record *rec, bool synch) 984cb21686bSWang Nan { 985cb21686bSWang Nan int err; 986cb21686bSWang Nan 987470530bbSAlexey Budankov err = record__mmap_read_evlist(rec, rec->evlist, false, synch); 988cb21686bSWang Nan if (err) 989cb21686bSWang Nan return err; 990cb21686bSWang Nan 991470530bbSAlexey Budankov return record__mmap_read_evlist(rec, rec->evlist, true, synch); 992cb21686bSWang Nan } 993cb21686bSWang Nan 9948c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 99557706abcSDavid Ahern { 99657706abcSDavid Ahern struct perf_session *session = rec->session; 99757706abcSDavid Ahern int feat; 99857706abcSDavid Ahern 99957706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 100057706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 100157706abcSDavid Ahern 100257706abcSDavid Ahern if (rec->no_buildid) 100357706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 100457706abcSDavid Ahern 10053e2be2daSArnaldo Carvalho de Melo if (!have_tracepoints(&rec->evlist->entries)) 100657706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 100757706abcSDavid Ahern 100857706abcSDavid Ahern if (!rec->opts.branch_stack) 100957706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 1010ef149c25SAdrian Hunter 1011ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 1012ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 1013ffa517adSJiri Olsa 1014cf790516SAlexey Budankov if (!(rec->opts.use_clockid && rec->opts.clockid_res_ns)) 1015cf790516SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_CLOCKID); 1016cf790516SAlexey Budankov 1017258031c0SJiri Olsa perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT); 101842e1fd80SAlexey Budankov if (!record__comp_enabled(rec)) 101942e1fd80SAlexey Budankov perf_header__clear_feat(&session->header, HEADER_COMPRESSED); 1020258031c0SJiri Olsa 1021ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 102257706abcSDavid Ahern } 102357706abcSDavid Ahern 1024e1ab48baSWang Nan static void 1025e1ab48baSWang Nan record__finish_output(struct record *rec) 1026e1ab48baSWang Nan { 10278ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 10288ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1029e1ab48baSWang Nan 10308ceb41d7SJiri Olsa if (data->is_pipe) 1031e1ab48baSWang Nan return; 1032e1ab48baSWang Nan 1033e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 103445112e89SJiri Olsa data->file.size = lseek(perf_data__fd(data), 0, SEEK_CUR); 1035e1ab48baSWang Nan 1036e1ab48baSWang Nan if (!rec->no_buildid) { 1037e1ab48baSWang Nan process_buildids(rec); 1038e1ab48baSWang Nan 1039e1ab48baSWang Nan if (rec->buildid_all) 1040e1ab48baSWang Nan dsos__hit_all(rec->session); 1041e1ab48baSWang Nan } 1042e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 1043e1ab48baSWang Nan 1044e1ab48baSWang Nan return; 1045e1ab48baSWang Nan } 1046e1ab48baSWang Nan 10474ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 1048be7b0c9eSWang Nan { 10499d6aae72SArnaldo Carvalho de Melo int err; 10509d6aae72SArnaldo Carvalho de Melo struct thread_map *thread_map; 1051be7b0c9eSWang Nan 10524ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 10534ea648aeSWang Nan return 0; 10544ea648aeSWang Nan 10559d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 10569d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 10579d6aae72SArnaldo Carvalho de Melo return -1; 10589d6aae72SArnaldo Carvalho de Melo 10599d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 1060be7b0c9eSWang Nan process_synthesized_event, 1061be7b0c9eSWang Nan &rec->session->machines.host, 10623fcb10e4SMark Drayton rec->opts.sample_address); 10639d6aae72SArnaldo Carvalho de Melo thread_map__put(thread_map); 10649d6aae72SArnaldo Carvalho de Melo return err; 1065be7b0c9eSWang Nan } 1066be7b0c9eSWang Nan 10674ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 10683c1cb7e3SWang Nan 1069ecfd7a9cSWang Nan static int 1070ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 1071ecfd7a9cSWang Nan { 10728ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1073ecfd7a9cSWang Nan int fd, err; 107403724b2eSAndi Kleen char *new_filename; 1075ecfd7a9cSWang Nan 1076ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 1077ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 1078ecfd7a9cSWang Nan 1079d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1080d3d1af6fSAlexey Budankov 10814ea648aeSWang Nan record__synthesize(rec, true); 10824ea648aeSWang Nan if (target__none(&rec->opts.target)) 10834ea648aeSWang Nan record__synthesize_workload(rec, true); 10844ea648aeSWang Nan 1085ecfd7a9cSWang Nan rec->samples = 0; 1086ecfd7a9cSWang Nan record__finish_output(rec); 1087ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 1088ecfd7a9cSWang Nan if (err) { 1089ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 1090ecfd7a9cSWang Nan return -EINVAL; 1091ecfd7a9cSWang Nan } 1092ecfd7a9cSWang Nan 10938ceb41d7SJiri Olsa fd = perf_data__switch(data, timestamp, 1094ecfd7a9cSWang Nan rec->session->header.data_offset, 109503724b2eSAndi Kleen at_exit, &new_filename); 1096ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 1097ecfd7a9cSWang Nan rec->bytes_written = 0; 1098ecfd7a9cSWang Nan rec->session->header.data_size = 0; 1099ecfd7a9cSWang Nan } 1100ecfd7a9cSWang Nan 1101ecfd7a9cSWang Nan if (!quiet) 1102ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 11032d4f2799SJiri Olsa data->path, timestamp); 11043c1cb7e3SWang Nan 110503724b2eSAndi Kleen if (rec->switch_output.num_files) { 110603724b2eSAndi Kleen int n = rec->switch_output.cur_file + 1; 110703724b2eSAndi Kleen 110803724b2eSAndi Kleen if (n >= rec->switch_output.num_files) 110903724b2eSAndi Kleen n = 0; 111003724b2eSAndi Kleen rec->switch_output.cur_file = n; 111103724b2eSAndi Kleen if (rec->switch_output.filenames[n]) { 111203724b2eSAndi Kleen remove(rec->switch_output.filenames[n]); 111303724b2eSAndi Kleen free(rec->switch_output.filenames[n]); 111403724b2eSAndi Kleen } 111503724b2eSAndi Kleen rec->switch_output.filenames[n] = new_filename; 111603724b2eSAndi Kleen } else { 111703724b2eSAndi Kleen free(new_filename); 111803724b2eSAndi Kleen } 111903724b2eSAndi Kleen 11203c1cb7e3SWang Nan /* Output tracking events */ 1121be7b0c9eSWang Nan if (!at_exit) { 11224ea648aeSWang Nan record__synthesize(rec, false); 11233c1cb7e3SWang Nan 1124be7b0c9eSWang Nan /* 1125be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 1126be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 1127be7b0c9eSWang Nan * generate tracking events because there's no thread_map 1128be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 1129be7b0c9eSWang Nan * contain map and comm information. 1130be7b0c9eSWang Nan * Create a fake thread_map and directly call 1131be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 1132be7b0c9eSWang Nan */ 1133be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 11344ea648aeSWang Nan record__synthesize_workload(rec, false); 1135be7b0c9eSWang Nan } 1136ecfd7a9cSWang Nan return fd; 1137ecfd7a9cSWang Nan } 1138ecfd7a9cSWang Nan 1139f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno; 1140f33cbe72SArnaldo Carvalho de Melo 1141f33cbe72SArnaldo Carvalho de Melo /* 1142f33cbe72SArnaldo Carvalho de Melo * perf_evlist__prepare_workload will send a SIGUSR1 1143f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 1144f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 1145f33cbe72SArnaldo Carvalho de Melo */ 114645604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 114745604710SNamhyung Kim siginfo_t *info, 1148f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 1149f33cbe72SArnaldo Carvalho de Melo { 1150f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 1151f33cbe72SArnaldo Carvalho de Melo done = 1; 1152f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 1153f33cbe72SArnaldo Carvalho de Melo } 1154f33cbe72SArnaldo Carvalho de Melo 11552dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 1156bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 11572dd6d8a1SAdrian Hunter 115846bc29b9SAdrian Hunter int __weak 115946bc29b9SAdrian Hunter perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused, 116046bc29b9SAdrian Hunter struct perf_tool *tool __maybe_unused, 116146bc29b9SAdrian Hunter perf_event__handler_t process __maybe_unused, 116246bc29b9SAdrian Hunter struct machine *machine __maybe_unused) 116346bc29b9SAdrian Hunter { 116446bc29b9SAdrian Hunter return 0; 116546bc29b9SAdrian Hunter } 116646bc29b9SAdrian Hunter 1167ee667f94SWang Nan static const struct perf_event_mmap_page * 1168ee667f94SWang Nan perf_evlist__pick_pc(struct perf_evlist *evlist) 1169ee667f94SWang Nan { 1170b2cb615dSWang Nan if (evlist) { 1171b2cb615dSWang Nan if (evlist->mmap && evlist->mmap[0].base) 1172ee667f94SWang Nan return evlist->mmap[0].base; 11730b72d69aSWang Nan if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].base) 11740b72d69aSWang Nan return evlist->overwrite_mmap[0].base; 1175b2cb615dSWang Nan } 1176ee667f94SWang Nan return NULL; 1177ee667f94SWang Nan } 1178ee667f94SWang Nan 1179c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 1180c45628b0SWang Nan { 1181ee667f94SWang Nan const struct perf_event_mmap_page *pc; 1182ee667f94SWang Nan 1183ee667f94SWang Nan pc = perf_evlist__pick_pc(rec->evlist); 1184ee667f94SWang Nan if (pc) 1185ee667f94SWang Nan return pc; 1186c45628b0SWang Nan return NULL; 1187c45628b0SWang Nan } 1188c45628b0SWang Nan 11894ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 1190c45c86ebSWang Nan { 1191c45c86ebSWang Nan struct perf_session *session = rec->session; 1192c45c86ebSWang Nan struct machine *machine = &session->machines.host; 11938ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1194c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 1195c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 11968ceb41d7SJiri Olsa int fd = perf_data__fd(data); 1197c45c86ebSWang Nan int err = 0; 1198c45c86ebSWang Nan 11994ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 12004ea648aeSWang Nan return 0; 12014ea648aeSWang Nan 12028ceb41d7SJiri Olsa if (data->is_pipe) { 1203a2015516SJiri Olsa /* 1204a2015516SJiri Olsa * We need to synthesize events first, because some 1205a2015516SJiri Olsa * features works on top of them (on report side). 1206a2015516SJiri Olsa */ 1207318ec184SJiri Olsa err = perf_event__synthesize_attrs(tool, rec->evlist, 1208c45c86ebSWang Nan process_synthesized_event); 1209c45c86ebSWang Nan if (err < 0) { 1210c45c86ebSWang Nan pr_err("Couldn't synthesize attrs.\n"); 1211c45c86ebSWang Nan goto out; 1212c45c86ebSWang Nan } 1213c45c86ebSWang Nan 1214a2015516SJiri Olsa err = perf_event__synthesize_features(tool, session, rec->evlist, 1215a2015516SJiri Olsa process_synthesized_event); 1216a2015516SJiri Olsa if (err < 0) { 1217a2015516SJiri Olsa pr_err("Couldn't synthesize features.\n"); 1218a2015516SJiri Olsa return err; 1219a2015516SJiri Olsa } 1220a2015516SJiri Olsa 1221c45c86ebSWang Nan if (have_tracepoints(&rec->evlist->entries)) { 1222c45c86ebSWang Nan /* 1223c45c86ebSWang Nan * FIXME err <= 0 here actually means that 1224c45c86ebSWang Nan * there were no tracepoints so its not really 1225c45c86ebSWang Nan * an error, just that we don't need to 1226c45c86ebSWang Nan * synthesize anything. We really have to 1227c45c86ebSWang Nan * return this more properly and also 1228c45c86ebSWang Nan * propagate errors that now are calling die() 1229c45c86ebSWang Nan */ 1230c45c86ebSWang Nan err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, 1231c45c86ebSWang Nan process_synthesized_event); 1232c45c86ebSWang Nan if (err <= 0) { 1233c45c86ebSWang Nan pr_err("Couldn't record tracing data.\n"); 1234c45c86ebSWang Nan goto out; 1235c45c86ebSWang Nan } 1236c45c86ebSWang Nan rec->bytes_written += err; 1237c45c86ebSWang Nan } 1238c45c86ebSWang Nan } 1239c45c86ebSWang Nan 1240c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 124146bc29b9SAdrian Hunter process_synthesized_event, machine); 124246bc29b9SAdrian Hunter if (err) 124346bc29b9SAdrian Hunter goto out; 124446bc29b9SAdrian Hunter 1245c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 1246c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 1247c45c86ebSWang Nan session, process_synthesized_event); 1248c45c86ebSWang Nan if (err) 1249c45c86ebSWang Nan goto out; 1250c45c86ebSWang Nan } 1251c45c86ebSWang Nan 12526c443954SArnaldo Carvalho de Melo if (!perf_evlist__exclude_kernel(rec->evlist)) { 1253c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 1254c45c86ebSWang Nan machine); 1255c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 1256c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1257c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 1258c45c86ebSWang Nan 1259c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 1260c45c86ebSWang Nan machine); 1261c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 1262c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 1263c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 12646c443954SArnaldo Carvalho de Melo } 1265c45c86ebSWang Nan 1266c45c86ebSWang Nan if (perf_guest) { 1267c45c86ebSWang Nan machines__process_guests(&session->machines, 1268c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 1269c45c86ebSWang Nan } 1270c45c86ebSWang Nan 1271bfd8f72cSAndi Kleen err = perf_event__synthesize_extra_attr(&rec->tool, 1272bfd8f72cSAndi Kleen rec->evlist, 1273bfd8f72cSAndi Kleen process_synthesized_event, 1274bfd8f72cSAndi Kleen data->is_pipe); 1275bfd8f72cSAndi Kleen if (err) 1276bfd8f72cSAndi Kleen goto out; 1277bfd8f72cSAndi Kleen 1278373565d2SAndi Kleen err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->threads, 1279373565d2SAndi Kleen process_synthesized_event, 1280373565d2SAndi Kleen NULL); 1281373565d2SAndi Kleen if (err < 0) { 1282373565d2SAndi Kleen pr_err("Couldn't synthesize thread map.\n"); 1283373565d2SAndi Kleen return err; 1284373565d2SAndi Kleen } 1285373565d2SAndi Kleen 1286373565d2SAndi Kleen err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->cpus, 1287373565d2SAndi Kleen process_synthesized_event, NULL); 1288373565d2SAndi Kleen if (err < 0) { 1289373565d2SAndi Kleen pr_err("Couldn't synthesize cpu map.\n"); 1290373565d2SAndi Kleen return err; 1291373565d2SAndi Kleen } 1292373565d2SAndi Kleen 1293e5416950SSong Liu err = perf_event__synthesize_bpf_events(session, process_synthesized_event, 12947b612e29SSong Liu machine, opts); 12957b612e29SSong Liu if (err < 0) 12967b612e29SSong Liu pr_warning("Couldn't synthesize bpf events.\n"); 12977b612e29SSong Liu 1298c45c86ebSWang Nan err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, 1299c45c86ebSWang Nan process_synthesized_event, opts->sample_address, 13003fcb10e4SMark Drayton 1); 1301c45c86ebSWang Nan out: 1302c45c86ebSWang Nan return err; 1303c45c86ebSWang Nan } 1304c45c86ebSWang Nan 13058c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 130686470930SIngo Molnar { 130757706abcSDavid Ahern int err; 130845604710SNamhyung Kim int status = 0; 13098b412664SPeter Zijlstra unsigned long waking = 0; 131046be604bSZhang, Yanmin const bool forks = argc > 0; 131145694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 1312b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 13138ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 1314d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 13156dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 1316657ee553SSong Liu struct perf_evlist *sb_evlist = NULL; 131742aa276fSNamhyung Kim int fd; 1318d3c8c08eSAlexey Budankov float ratio = 0; 131986470930SIngo Molnar 132045604710SNamhyung Kim atexit(record__sig_exit); 1321f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 1322f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 1323804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 1324a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 1325c0bdc1c4SWang Nan 1326f3b3614aSHari Bathini if (rec->opts.record_namespaces) 1327f3b3614aSHari Bathini tool->namespace_events = true; 1328f3b3614aSHari Bathini 1329dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 13302dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 13313c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 13325f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 1333dc0c6127SJiri Olsa if (rec->switch_output.enabled) 13343c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 1335c0bdc1c4SWang Nan } else { 13362dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 1337c0bdc1c4SWang Nan } 1338f5970550SPeter Zijlstra 13398ceb41d7SJiri Olsa session = perf_session__new(data, false, tool); 134094c744b6SArnaldo Carvalho de Melo if (session == NULL) { 1341ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 1342a9a70bbcSArnaldo Carvalho de Melo return -1; 1343a9a70bbcSArnaldo Carvalho de Melo } 1344a9a70bbcSArnaldo Carvalho de Melo 13458ceb41d7SJiri Olsa fd = perf_data__fd(data); 1346d20deb64SArnaldo Carvalho de Melo rec->session = session; 1347d20deb64SArnaldo Carvalho de Melo 13485d7f4116SAlexey Budankov if (zstd_init(&session->zstd_data, rec->opts.comp_level) < 0) { 13495d7f4116SAlexey Budankov pr_err("Compression initialization failed.\n"); 13505d7f4116SAlexey Budankov return -1; 13515d7f4116SAlexey Budankov } 13525d7f4116SAlexey Budankov 13535d7f4116SAlexey Budankov session->header.env.comp_type = PERF_COMP_ZSTD; 13545d7f4116SAlexey Budankov session->header.env.comp_level = rec->opts.comp_level; 13555d7f4116SAlexey Budankov 13568c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 1357330aa675SStephane Eranian 1358cf790516SAlexey Budankov if (rec->opts.use_clockid && rec->opts.clockid_res_ns) 1359cf790516SAlexey Budankov session->header.env.clockid_res_ns = rec->opts.clockid_res_ns; 1360cf790516SAlexey Budankov 1361d4db3f16SArnaldo Carvalho de Melo if (forks) { 13623e2be2daSArnaldo Carvalho de Melo err = perf_evlist__prepare_workload(rec->evlist, &opts->target, 13638ceb41d7SJiri Olsa argv, data->is_pipe, 1364735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 136535b9d88eSArnaldo Carvalho de Melo if (err < 0) { 136635b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 136745604710SNamhyung Kim status = err; 136835b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 1369856e9660SPeter Zijlstra } 1370856e9660SPeter Zijlstra } 1371856e9660SPeter Zijlstra 1372ad46e48cSJiri Olsa /* 1373ad46e48cSJiri Olsa * If we have just single event and are sending data 1374ad46e48cSJiri Olsa * through pipe, we need to force the ids allocation, 1375ad46e48cSJiri Olsa * because we synthesize event name through the pipe 1376ad46e48cSJiri Olsa * and need the id for that. 1377ad46e48cSJiri Olsa */ 1378ad46e48cSJiri Olsa if (data->is_pipe && rec->evlist->nr_entries == 1) 1379ad46e48cSJiri Olsa rec->opts.sample_id = true; 1380ad46e48cSJiri Olsa 13818c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 13828d3eca20SDavid Ahern err = -1; 138345604710SNamhyung Kim goto out_child; 13848d3eca20SDavid Ahern } 138542e1fd80SAlexey Budankov session->header.env.comp_mmap_len = session->evlist->mmap_len; 138686470930SIngo Molnar 13878690a2a7SWang Nan err = bpf__apply_obj_config(); 13888690a2a7SWang Nan if (err) { 13898690a2a7SWang Nan char errbuf[BUFSIZ]; 13908690a2a7SWang Nan 13918690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 13928690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 13938690a2a7SWang Nan errbuf); 13948690a2a7SWang Nan goto out_child; 13958690a2a7SWang Nan } 13968690a2a7SWang Nan 1397cca8482cSAdrian Hunter /* 1398cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 1399cca8482cSAdrian Hunter * evlist. 1400cca8482cSAdrian Hunter */ 1401cca8482cSAdrian Hunter if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) { 1402cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 1403cca8482cSAdrian Hunter rec->tool.ordered_events = false; 1404cca8482cSAdrian Hunter } 1405cca8482cSAdrian Hunter 14063e2be2daSArnaldo Carvalho de Melo if (!rec->evlist->nr_groups) 1407a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 1408a8bb559bSNamhyung Kim 14098ceb41d7SJiri Olsa if (data->is_pipe) { 141042aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 1411529870e3STom Zanussi if (err < 0) 141245604710SNamhyung Kim goto out_child; 1413563aecb2SJiri Olsa } else { 141442aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 1415d5eed904SArnaldo Carvalho de Melo if (err < 0) 141645604710SNamhyung Kim goto out_child; 1417d5eed904SArnaldo Carvalho de Melo } 14187c6a1c65SPeter Zijlstra 1419d3665498SDavid Ahern if (!rec->no_buildid 1420e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 1421d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 1422e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 14238d3eca20SDavid Ahern err = -1; 142445604710SNamhyung Kim goto out_child; 1425e20960c0SRobert Richter } 1426e20960c0SRobert Richter 1427d56354dcSSong Liu if (!opts->no_bpf_event) 1428d56354dcSSong Liu bpf_event__add_sb_event(&sb_evlist, &session->header.env); 1429d56354dcSSong Liu 1430657ee553SSong Liu if (perf_evlist__start_sb_thread(sb_evlist, &rec->opts.target)) { 1431657ee553SSong Liu pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n"); 1432657ee553SSong Liu opts->no_bpf_event = true; 1433657ee553SSong Liu } 1434657ee553SSong Liu 14354ea648aeSWang Nan err = record__synthesize(rec, false); 1436c45c86ebSWang Nan if (err < 0) 143745604710SNamhyung Kim goto out_child; 14388d3eca20SDavid Ahern 1439d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 144086470930SIngo Molnar struct sched_param param; 144186470930SIngo Molnar 1442d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 144386470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 14446beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 14458d3eca20SDavid Ahern err = -1; 144645604710SNamhyung Kim goto out_child; 144786470930SIngo Molnar } 144886470930SIngo Molnar } 144986470930SIngo Molnar 1450774cb499SJiri Olsa /* 1451774cb499SJiri Olsa * When perf is starting the traced process, all the events 1452774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 1453774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 1454774cb499SJiri Olsa */ 14556619a53eSAndi Kleen if (!target__none(&opts->target) && !opts->initial_delay) 14563e2be2daSArnaldo Carvalho de Melo perf_evlist__enable(rec->evlist); 1457764e16a3SDavid Ahern 1458856e9660SPeter Zijlstra /* 1459856e9660SPeter Zijlstra * Let the child rip 1460856e9660SPeter Zijlstra */ 1461e803cf97SNamhyung Kim if (forks) { 146220a8a3cfSJiri Olsa struct machine *machine = &session->machines.host; 1463e5bed564SNamhyung Kim union perf_event *event; 1464e907caf3SHari Bathini pid_t tgid; 1465e5bed564SNamhyung Kim 1466e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 1467e5bed564SNamhyung Kim if (event == NULL) { 1468e5bed564SNamhyung Kim err = -ENOMEM; 1469e5bed564SNamhyung Kim goto out_child; 1470e5bed564SNamhyung Kim } 1471e5bed564SNamhyung Kim 1472e803cf97SNamhyung Kim /* 1473e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 1474e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 1475e803cf97SNamhyung Kim * cannot see a correct process name for those events. 1476e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 1477e803cf97SNamhyung Kim */ 1478e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 1479e803cf97SNamhyung Kim rec->evlist->workload.pid, 1480e803cf97SNamhyung Kim process_synthesized_event, 1481e803cf97SNamhyung Kim machine); 1482e5bed564SNamhyung Kim free(event); 1483e803cf97SNamhyung Kim 1484e907caf3SHari Bathini if (tgid == -1) 1485e907caf3SHari Bathini goto out_child; 1486e907caf3SHari Bathini 1487e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 1488e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 1489e907caf3SHari Bathini machine->id_hdr_size); 1490e907caf3SHari Bathini if (event == NULL) { 1491e907caf3SHari Bathini err = -ENOMEM; 1492e907caf3SHari Bathini goto out_child; 1493e907caf3SHari Bathini } 1494e907caf3SHari Bathini 1495e907caf3SHari Bathini /* 1496e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 1497e907caf3SHari Bathini */ 1498e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 1499e907caf3SHari Bathini rec->evlist->workload.pid, 1500e907caf3SHari Bathini tgid, process_synthesized_event, 1501e907caf3SHari Bathini machine); 1502e907caf3SHari Bathini free(event); 1503e907caf3SHari Bathini 15043e2be2daSArnaldo Carvalho de Melo perf_evlist__start_workload(rec->evlist); 1505e803cf97SNamhyung Kim } 1506856e9660SPeter Zijlstra 15076619a53eSAndi Kleen if (opts->initial_delay) { 15080693e680SArnaldo Carvalho de Melo usleep(opts->initial_delay * USEC_PER_MSEC); 15096619a53eSAndi Kleen perf_evlist__enable(rec->evlist); 15106619a53eSAndi Kleen } 15116619a53eSAndi Kleen 15125f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 15133c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 1514a074865eSWang Nan perf_hooks__invoke_record_start(); 1515649c48a9SPeter Zijlstra for (;;) { 15169f065194SYang Shi unsigned long long hits = rec->samples; 151786470930SIngo Molnar 151805737464SWang Nan /* 151905737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 152005737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 152105737464SWang Nan * hits != rec->samples in previous round. 152205737464SWang Nan * 152305737464SWang Nan * perf_evlist__toggle_bkw_mmap ensure we never 152405737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 152505737464SWang Nan */ 152605737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 152705737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 152805737464SWang Nan 1529470530bbSAlexey Budankov if (record__mmap_read_all(rec, false) < 0) { 15305f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 15313c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 15328d3eca20SDavid Ahern err = -1; 153345604710SNamhyung Kim goto out_child; 15348d3eca20SDavid Ahern } 153586470930SIngo Molnar 15362dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 15372dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 15385f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 15392dd6d8a1SAdrian Hunter record__read_auxtrace_snapshot(rec); 15405f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 15412dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 15422dd6d8a1SAdrian Hunter err = -1; 15432dd6d8a1SAdrian Hunter goto out_child; 15442dd6d8a1SAdrian Hunter } 15452dd6d8a1SAdrian Hunter } 15462dd6d8a1SAdrian Hunter 15473c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 154805737464SWang Nan /* 154905737464SWang Nan * If switch_output_trigger is hit, the data in 155005737464SWang Nan * overwritable ring buffer should have been collected, 155105737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 155205737464SWang Nan * 155305737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 155405737464SWang Nan * record__mmap_read_all() didn't collect data from 155505737464SWang Nan * overwritable ring buffer. Read again. 155605737464SWang Nan */ 155705737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 155805737464SWang Nan continue; 15593c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 15603c1cb7e3SWang Nan 156105737464SWang Nan /* 156205737464SWang Nan * Reenable events in overwrite ring buffer after 156305737464SWang Nan * record__mmap_read_all(): we should have collected 156405737464SWang Nan * data from it. 156505737464SWang Nan */ 156605737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 156705737464SWang Nan 15683c1cb7e3SWang Nan if (!quiet) 15693c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 15703c1cb7e3SWang Nan waking); 15713c1cb7e3SWang Nan waking = 0; 15723c1cb7e3SWang Nan fd = record__switch_output(rec, false); 15733c1cb7e3SWang Nan if (fd < 0) { 15743c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 15753c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 15763c1cb7e3SWang Nan err = fd; 15773c1cb7e3SWang Nan goto out_child; 15783c1cb7e3SWang Nan } 1579bfacbe3bSJiri Olsa 1580bfacbe3bSJiri Olsa /* re-arm the alarm */ 1581bfacbe3bSJiri Olsa if (rec->switch_output.time) 1582bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 15833c1cb7e3SWang Nan } 15843c1cb7e3SWang Nan 1585d20deb64SArnaldo Carvalho de Melo if (hits == rec->samples) { 15866dcf45efSArnaldo Carvalho de Melo if (done || draining) 1587649c48a9SPeter Zijlstra break; 1588f66a889dSArnaldo Carvalho de Melo err = perf_evlist__poll(rec->evlist, -1); 1589a515114fSJiri Olsa /* 1590a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 1591a515114fSJiri Olsa * number of returned events and interrupt error. 1592a515114fSJiri Olsa */ 1593a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 159445604710SNamhyung Kim err = 0; 15958b412664SPeter Zijlstra waking++; 15966dcf45efSArnaldo Carvalho de Melo 15976dcf45efSArnaldo Carvalho de Melo if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) 15986dcf45efSArnaldo Carvalho de Melo draining = true; 15998b412664SPeter Zijlstra } 16008b412664SPeter Zijlstra 1601774cb499SJiri Olsa /* 1602774cb499SJiri Olsa * When perf is starting the traced process, at the end events 1603774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 1604774cb499SJiri Olsa * disable events in this case. 1605774cb499SJiri Olsa */ 1606602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 16075f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 16083e2be2daSArnaldo Carvalho de Melo perf_evlist__disable(rec->evlist); 16092711926aSJiri Olsa disabled = true; 16102711926aSJiri Olsa } 16118b412664SPeter Zijlstra } 16125f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 16133c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 16148b412664SPeter Zijlstra 1615f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 161635550da3SMasami Hiramatsu char msg[STRERR_BUFSIZE]; 1617c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 1618f33cbe72SArnaldo Carvalho de Melo pr_err("Workload failed: %s\n", emsg); 1619f33cbe72SArnaldo Carvalho de Melo err = -1; 162045604710SNamhyung Kim goto out_child; 1621f33cbe72SArnaldo Carvalho de Melo } 1622f33cbe72SArnaldo Carvalho de Melo 1623e3d59112SNamhyung Kim if (!quiet) 16248b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 162586470930SIngo Molnar 16264ea648aeSWang Nan if (target__none(&rec->opts.target)) 16274ea648aeSWang Nan record__synthesize_workload(rec, true); 16284ea648aeSWang Nan 162945604710SNamhyung Kim out_child: 1630470530bbSAlexey Budankov record__mmap_read_all(rec, true); 1631d3d1af6fSAlexey Budankov record__aio_mmap_read_sync(rec); 1632d3d1af6fSAlexey Budankov 1633d3c8c08eSAlexey Budankov if (rec->session->bytes_transferred && rec->session->bytes_compressed) { 1634d3c8c08eSAlexey Budankov ratio = (float)rec->session->bytes_transferred/(float)rec->session->bytes_compressed; 1635d3c8c08eSAlexey Budankov session->header.env.comp_ratio = ratio + 0.5; 1636d3c8c08eSAlexey Budankov } 1637d3c8c08eSAlexey Budankov 163845604710SNamhyung Kim if (forks) { 163945604710SNamhyung Kim int exit_status; 164045604710SNamhyung Kim 164145604710SNamhyung Kim if (!child_finished) 164245604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 164345604710SNamhyung Kim 164445604710SNamhyung Kim wait(&exit_status); 164545604710SNamhyung Kim 164645604710SNamhyung Kim if (err < 0) 164745604710SNamhyung Kim status = err; 164845604710SNamhyung Kim else if (WIFEXITED(exit_status)) 164945604710SNamhyung Kim status = WEXITSTATUS(exit_status); 165045604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 165145604710SNamhyung Kim signr = WTERMSIG(exit_status); 165245604710SNamhyung Kim } else 165345604710SNamhyung Kim status = err; 165445604710SNamhyung Kim 16554ea648aeSWang Nan record__synthesize(rec, true); 1656e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 1657e3d59112SNamhyung Kim rec->samples = 0; 1658e3d59112SNamhyung Kim 1659ecfd7a9cSWang Nan if (!err) { 1660ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 1661e1ab48baSWang Nan record__finish_output(rec); 1662ecfd7a9cSWang Nan } else { 1663ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 1664ecfd7a9cSWang Nan if (fd < 0) { 1665ecfd7a9cSWang Nan status = fd; 1666ecfd7a9cSWang Nan goto out_delete_session; 1667ecfd7a9cSWang Nan } 1668ecfd7a9cSWang Nan } 1669ecfd7a9cSWang Nan } 167039d17dacSArnaldo Carvalho de Melo 1671a074865eSWang Nan perf_hooks__invoke_record_end(); 1672a074865eSWang Nan 1673e3d59112SNamhyung Kim if (!err && !quiet) { 1674e3d59112SNamhyung Kim char samples[128]; 1675ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 1676ecfd7a9cSWang Nan ".<timestamp>" : ""; 1677e3d59112SNamhyung Kim 1678ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 1679e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 1680e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 1681e3d59112SNamhyung Kim else 1682e3d59112SNamhyung Kim samples[0] = '\0'; 1683e3d59112SNamhyung Kim 1684d3c8c08eSAlexey Budankov fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s", 16858ceb41d7SJiri Olsa perf_data__size(data) / 1024.0 / 1024.0, 16862d4f2799SJiri Olsa data->path, postfix, samples); 1687d3c8c08eSAlexey Budankov if (ratio) { 1688d3c8c08eSAlexey Budankov fprintf(stderr, ", compressed (original %.3f MB, ratio is %.3f)", 1689d3c8c08eSAlexey Budankov rec->session->bytes_transferred / 1024.0 / 1024.0, 1690d3c8c08eSAlexey Budankov ratio); 1691d3c8c08eSAlexey Budankov } 1692d3c8c08eSAlexey Budankov fprintf(stderr, " ]\n"); 1693e3d59112SNamhyung Kim } 1694e3d59112SNamhyung Kim 169539d17dacSArnaldo Carvalho de Melo out_delete_session: 16965d7f4116SAlexey Budankov zstd_fini(&session->zstd_data); 169739d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 1698657ee553SSong Liu 1699657ee553SSong Liu if (!opts->no_bpf_event) 1700657ee553SSong Liu perf_evlist__stop_sb_thread(sb_evlist); 170145604710SNamhyung Kim return status; 170286470930SIngo Molnar } 170386470930SIngo Molnar 17040883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 170509b0fd45SJiri Olsa { 1706aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 1707a601fdffSJiri Olsa 17080883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 170926d33022SJiri Olsa 17100883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 171109b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 17120883e820SArnaldo Carvalho de Melo callchain->dump_size); 17130883e820SArnaldo Carvalho de Melo } 17140883e820SArnaldo Carvalho de Melo 17150883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 17160883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 17170883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 17180883e820SArnaldo Carvalho de Melo { 17190883e820SArnaldo Carvalho de Melo int ret; 17200883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 17210883e820SArnaldo Carvalho de Melo 17220883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 17230883e820SArnaldo Carvalho de Melo if (unset) { 17240883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 17250883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 17260883e820SArnaldo Carvalho de Melo return 0; 17270883e820SArnaldo Carvalho de Melo } 17280883e820SArnaldo Carvalho de Melo 17290883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 17300883e820SArnaldo Carvalho de Melo if (!ret) { 17310883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 17320883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 17330883e820SArnaldo Carvalho de Melo record->sample_address = true; 17340883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 17350883e820SArnaldo Carvalho de Melo } 17360883e820SArnaldo Carvalho de Melo 17370883e820SArnaldo Carvalho de Melo return ret; 173809b0fd45SJiri Olsa } 173909b0fd45SJiri Olsa 1740c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 174109b0fd45SJiri Olsa const char *arg, 174209b0fd45SJiri Olsa int unset) 174309b0fd45SJiri Olsa { 17440883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 174526d33022SJiri Olsa } 174626d33022SJiri Olsa 1747c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 174809b0fd45SJiri Olsa const char *arg __maybe_unused, 174909b0fd45SJiri Olsa int unset __maybe_unused) 175009b0fd45SJiri Olsa { 17512ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 1752c421e80bSKan Liang 17532ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 175409b0fd45SJiri Olsa 17552ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 17562ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 1757eb853e80SJiri Olsa 17582ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 175909b0fd45SJiri Olsa return 0; 176009b0fd45SJiri Olsa } 176109b0fd45SJiri Olsa 1762eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 1763eb853e80SJiri Olsa { 17647a29c087SNamhyung Kim struct record *rec = cb; 17657a29c087SNamhyung Kim 17667a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 17677a29c087SNamhyung Kim if (!strcmp(value, "cache")) 17687a29c087SNamhyung Kim rec->no_buildid_cache = false; 17697a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 17707a29c087SNamhyung Kim rec->no_buildid_cache = true; 17717a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 17727a29c087SNamhyung Kim rec->no_buildid = true; 17737a29c087SNamhyung Kim else 17747a29c087SNamhyung Kim return -1; 17757a29c087SNamhyung Kim return 0; 17767a29c087SNamhyung Kim } 1777cff17205SYisheng Xie if (!strcmp(var, "record.call-graph")) { 1778cff17205SYisheng Xie var = "call-graph.record-mode"; 1779eb853e80SJiri Olsa return perf_default_config(var, value, cb); 1780eb853e80SJiri Olsa } 178193f20c0fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 178293f20c0fSAlexey Budankov if (!strcmp(var, "record.aio")) { 178393f20c0fSAlexey Budankov rec->opts.nr_cblocks = strtol(value, NULL, 0); 178493f20c0fSAlexey Budankov if (!rec->opts.nr_cblocks) 178593f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_default; 178693f20c0fSAlexey Budankov } 178793f20c0fSAlexey Budankov #endif 1788eb853e80SJiri Olsa 1789cff17205SYisheng Xie return 0; 1790cff17205SYisheng Xie } 1791cff17205SYisheng Xie 1792814c8c38SPeter Zijlstra struct clockid_map { 1793814c8c38SPeter Zijlstra const char *name; 1794814c8c38SPeter Zijlstra int clockid; 1795814c8c38SPeter Zijlstra }; 1796814c8c38SPeter Zijlstra 1797814c8c38SPeter Zijlstra #define CLOCKID_MAP(n, c) \ 1798814c8c38SPeter Zijlstra { .name = n, .clockid = (c), } 1799814c8c38SPeter Zijlstra 1800814c8c38SPeter Zijlstra #define CLOCKID_END { .name = NULL, } 1801814c8c38SPeter Zijlstra 1802814c8c38SPeter Zijlstra 1803814c8c38SPeter Zijlstra /* 1804814c8c38SPeter Zijlstra * Add the missing ones, we need to build on many distros... 1805814c8c38SPeter Zijlstra */ 1806814c8c38SPeter Zijlstra #ifndef CLOCK_MONOTONIC_RAW 1807814c8c38SPeter Zijlstra #define CLOCK_MONOTONIC_RAW 4 1808814c8c38SPeter Zijlstra #endif 1809814c8c38SPeter Zijlstra #ifndef CLOCK_BOOTTIME 1810814c8c38SPeter Zijlstra #define CLOCK_BOOTTIME 7 1811814c8c38SPeter Zijlstra #endif 1812814c8c38SPeter Zijlstra #ifndef CLOCK_TAI 1813814c8c38SPeter Zijlstra #define CLOCK_TAI 11 1814814c8c38SPeter Zijlstra #endif 1815814c8c38SPeter Zijlstra 1816814c8c38SPeter Zijlstra static const struct clockid_map clockids[] = { 1817814c8c38SPeter Zijlstra /* available for all events, NMI safe */ 1818814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), 1819814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), 1820814c8c38SPeter Zijlstra 1821814c8c38SPeter Zijlstra /* available for some events */ 1822814c8c38SPeter Zijlstra CLOCKID_MAP("realtime", CLOCK_REALTIME), 1823814c8c38SPeter Zijlstra CLOCKID_MAP("boottime", CLOCK_BOOTTIME), 1824814c8c38SPeter Zijlstra CLOCKID_MAP("tai", CLOCK_TAI), 1825814c8c38SPeter Zijlstra 1826814c8c38SPeter Zijlstra /* available for the lazy */ 1827814c8c38SPeter Zijlstra CLOCKID_MAP("mono", CLOCK_MONOTONIC), 1828814c8c38SPeter Zijlstra CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), 1829814c8c38SPeter Zijlstra CLOCKID_MAP("real", CLOCK_REALTIME), 1830814c8c38SPeter Zijlstra CLOCKID_MAP("boot", CLOCK_BOOTTIME), 1831814c8c38SPeter Zijlstra 1832814c8c38SPeter Zijlstra CLOCKID_END, 1833814c8c38SPeter Zijlstra }; 1834814c8c38SPeter Zijlstra 1835cf790516SAlexey Budankov static int get_clockid_res(clockid_t clk_id, u64 *res_ns) 1836cf790516SAlexey Budankov { 1837cf790516SAlexey Budankov struct timespec res; 1838cf790516SAlexey Budankov 1839cf790516SAlexey Budankov *res_ns = 0; 1840cf790516SAlexey Budankov if (!clock_getres(clk_id, &res)) 1841cf790516SAlexey Budankov *res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC; 1842cf790516SAlexey Budankov else 1843cf790516SAlexey Budankov pr_warning("WARNING: Failed to determine specified clock resolution.\n"); 1844cf790516SAlexey Budankov 1845cf790516SAlexey Budankov return 0; 1846cf790516SAlexey Budankov } 1847cf790516SAlexey Budankov 1848814c8c38SPeter Zijlstra static int parse_clockid(const struct option *opt, const char *str, int unset) 1849814c8c38SPeter Zijlstra { 1850814c8c38SPeter Zijlstra struct record_opts *opts = (struct record_opts *)opt->value; 1851814c8c38SPeter Zijlstra const struct clockid_map *cm; 1852814c8c38SPeter Zijlstra const char *ostr = str; 1853814c8c38SPeter Zijlstra 1854814c8c38SPeter Zijlstra if (unset) { 1855814c8c38SPeter Zijlstra opts->use_clockid = 0; 1856814c8c38SPeter Zijlstra return 0; 1857814c8c38SPeter Zijlstra } 1858814c8c38SPeter Zijlstra 1859814c8c38SPeter Zijlstra /* no arg passed */ 1860814c8c38SPeter Zijlstra if (!str) 1861814c8c38SPeter Zijlstra return 0; 1862814c8c38SPeter Zijlstra 1863814c8c38SPeter Zijlstra /* no setting it twice */ 1864814c8c38SPeter Zijlstra if (opts->use_clockid) 1865814c8c38SPeter Zijlstra return -1; 1866814c8c38SPeter Zijlstra 1867814c8c38SPeter Zijlstra opts->use_clockid = true; 1868814c8c38SPeter Zijlstra 1869814c8c38SPeter Zijlstra /* if its a number, we're done */ 1870814c8c38SPeter Zijlstra if (sscanf(str, "%d", &opts->clockid) == 1) 1871cf790516SAlexey Budankov return get_clockid_res(opts->clockid, &opts->clockid_res_ns); 1872814c8c38SPeter Zijlstra 1873814c8c38SPeter Zijlstra /* allow a "CLOCK_" prefix to the name */ 1874814c8c38SPeter Zijlstra if (!strncasecmp(str, "CLOCK_", 6)) 1875814c8c38SPeter Zijlstra str += 6; 1876814c8c38SPeter Zijlstra 1877814c8c38SPeter Zijlstra for (cm = clockids; cm->name; cm++) { 1878814c8c38SPeter Zijlstra if (!strcasecmp(str, cm->name)) { 1879814c8c38SPeter Zijlstra opts->clockid = cm->clockid; 1880cf790516SAlexey Budankov return get_clockid_res(opts->clockid, 1881cf790516SAlexey Budankov &opts->clockid_res_ns); 1882814c8c38SPeter Zijlstra } 1883814c8c38SPeter Zijlstra } 1884814c8c38SPeter Zijlstra 1885814c8c38SPeter Zijlstra opts->use_clockid = false; 1886814c8c38SPeter Zijlstra ui__warning("unknown clockid %s, check man page\n", ostr); 1887814c8c38SPeter Zijlstra return -1; 1888814c8c38SPeter Zijlstra } 1889814c8c38SPeter Zijlstra 1890f4fe11b7SAlexey Budankov static int record__parse_affinity(const struct option *opt, const char *str, int unset) 1891f4fe11b7SAlexey Budankov { 1892f4fe11b7SAlexey Budankov struct record_opts *opts = (struct record_opts *)opt->value; 1893f4fe11b7SAlexey Budankov 1894f4fe11b7SAlexey Budankov if (unset || !str) 1895f4fe11b7SAlexey Budankov return 0; 1896f4fe11b7SAlexey Budankov 1897f4fe11b7SAlexey Budankov if (!strcasecmp(str, "node")) 1898f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_NODE; 1899f4fe11b7SAlexey Budankov else if (!strcasecmp(str, "cpu")) 1900f4fe11b7SAlexey Budankov opts->affinity = PERF_AFFINITY_CPU; 1901f4fe11b7SAlexey Budankov 1902f4fe11b7SAlexey Budankov return 0; 1903f4fe11b7SAlexey Budankov } 1904f4fe11b7SAlexey Budankov 1905e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 1906e9db1310SAdrian Hunter const char *str, 1907e9db1310SAdrian Hunter int unset __maybe_unused) 1908e9db1310SAdrian Hunter { 1909e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 1910e9db1310SAdrian Hunter char *s, *p; 1911e9db1310SAdrian Hunter unsigned int mmap_pages; 1912e9db1310SAdrian Hunter int ret; 1913e9db1310SAdrian Hunter 1914e9db1310SAdrian Hunter if (!str) 1915e9db1310SAdrian Hunter return -EINVAL; 1916e9db1310SAdrian Hunter 1917e9db1310SAdrian Hunter s = strdup(str); 1918e9db1310SAdrian Hunter if (!s) 1919e9db1310SAdrian Hunter return -ENOMEM; 1920e9db1310SAdrian Hunter 1921e9db1310SAdrian Hunter p = strchr(s, ','); 1922e9db1310SAdrian Hunter if (p) 1923e9db1310SAdrian Hunter *p = '\0'; 1924e9db1310SAdrian Hunter 1925e9db1310SAdrian Hunter if (*s) { 1926e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s); 1927e9db1310SAdrian Hunter if (ret) 1928e9db1310SAdrian Hunter goto out_free; 1929e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 1930e9db1310SAdrian Hunter } 1931e9db1310SAdrian Hunter 1932e9db1310SAdrian Hunter if (!p) { 1933e9db1310SAdrian Hunter ret = 0; 1934e9db1310SAdrian Hunter goto out_free; 1935e9db1310SAdrian Hunter } 1936e9db1310SAdrian Hunter 1937e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1); 1938e9db1310SAdrian Hunter if (ret) 1939e9db1310SAdrian Hunter goto out_free; 1940e9db1310SAdrian Hunter 1941e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 1942e9db1310SAdrian Hunter 1943e9db1310SAdrian Hunter out_free: 1944e9db1310SAdrian Hunter free(s); 1945e9db1310SAdrian Hunter return ret; 1946e9db1310SAdrian Hunter } 1947e9db1310SAdrian Hunter 19480c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 19490c582449SJiri Olsa { 19500c582449SJiri Olsa u64 wakeup_size = perf_evlist__mmap_size(rec->opts.mmap_pages); 19510c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 19520c582449SJiri Olsa 19530c582449SJiri Olsa wakeup_size /= 2; 19540c582449SJiri Olsa 19550c582449SJiri Olsa if (s->size < wakeup_size) { 19560c582449SJiri Olsa char buf[100]; 19570c582449SJiri Olsa 19580c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 19590c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 19600c582449SJiri Olsa "wakeup kernel buffer size (%s) " 19610c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 19620c582449SJiri Olsa } 19630c582449SJiri Olsa } 19640c582449SJiri Olsa 1965cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 1966cb4e1ebbSJiri Olsa { 1967cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 1968dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 1969dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 1970dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 1971dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 1972dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 1973dc0c6127SJiri Olsa { .tag = 0 }, 1974dc0c6127SJiri Olsa }; 1975bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 1976bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 1977bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 1978bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 1979bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 1980bfacbe3bSJiri Olsa { .tag = 0 }, 1981bfacbe3bSJiri Olsa }; 1982dc0c6127SJiri Olsa unsigned long val; 1983cb4e1ebbSJiri Olsa 1984cb4e1ebbSJiri Olsa if (!s->set) 1985cb4e1ebbSJiri Olsa return 0; 1986cb4e1ebbSJiri Olsa 1987cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 1988cb4e1ebbSJiri Olsa s->signal = true; 1989cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 1990dc0c6127SJiri Olsa goto enabled; 1991dc0c6127SJiri Olsa } 1992dc0c6127SJiri Olsa 1993dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 1994dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 1995dc0c6127SJiri Olsa s->size = val; 1996dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 1997dc0c6127SJiri Olsa goto enabled; 1998cb4e1ebbSJiri Olsa } 1999cb4e1ebbSJiri Olsa 2000bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 2001bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 2002bfacbe3bSJiri Olsa s->time = val; 2003bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 2004bfacbe3bSJiri Olsa s->str, s->time); 2005bfacbe3bSJiri Olsa goto enabled; 2006bfacbe3bSJiri Olsa } 2007bfacbe3bSJiri Olsa 2008cb4e1ebbSJiri Olsa return -1; 2009dc0c6127SJiri Olsa 2010dc0c6127SJiri Olsa enabled: 2011dc0c6127SJiri Olsa rec->timestamp_filename = true; 2012dc0c6127SJiri Olsa s->enabled = true; 20130c582449SJiri Olsa 20140c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 20150c582449SJiri Olsa switch_output_size_warn(rec); 20160c582449SJiri Olsa 2017dc0c6127SJiri Olsa return 0; 2018cb4e1ebbSJiri Olsa } 2019cb4e1ebbSJiri Olsa 2020e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 202186470930SIngo Molnar "perf record [<options>] [<command>]", 202286470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 202386470930SIngo Molnar NULL 202486470930SIngo Molnar }; 2025e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 202686470930SIngo Molnar 2027d20deb64SArnaldo Carvalho de Melo /* 20288c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 20298c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 2030d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 2031d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 2032d20deb64SArnaldo Carvalho de Melo * 2033d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 2034d20deb64SArnaldo Carvalho de Melo * 2035d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 2036d20deb64SArnaldo Carvalho de Melo */ 20378c6f45a7SArnaldo Carvalho de Melo static struct record record = { 2038d20deb64SArnaldo Carvalho de Melo .opts = { 20398affc2b8SAndi Kleen .sample_time = true, 2040d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 2041d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 2042d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 2043447a6013SArnaldo Carvalho de Melo .freq = 4000, 2044d1cb9fceSNamhyung Kim .target = { 2045d1cb9fceSNamhyung Kim .uses_mmap = true, 20463aa5939dSAdrian Hunter .default_per_cpu = true, 2047d1cb9fceSNamhyung Kim }, 2048470530bbSAlexey Budankov .mmap_flush = MMAP_FLUSH_DEFAULT, 2049d20deb64SArnaldo Carvalho de Melo }, 2050e3d59112SNamhyung Kim .tool = { 2051e3d59112SNamhyung Kim .sample = process_sample_event, 2052e3d59112SNamhyung Kim .fork = perf_event__process_fork, 2053cca8482cSAdrian Hunter .exit = perf_event__process_exit, 2054e3d59112SNamhyung Kim .comm = perf_event__process_comm, 2055f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 2056e3d59112SNamhyung Kim .mmap = perf_event__process_mmap, 2057e3d59112SNamhyung Kim .mmap2 = perf_event__process_mmap2, 2058cca8482cSAdrian Hunter .ordered_events = true, 2059e3d59112SNamhyung Kim }, 2060d20deb64SArnaldo Carvalho de Melo }; 20617865e817SFrederic Weisbecker 206276a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 206376a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 206461eaa3beSArnaldo Carvalho de Melo 20650aab2136SWang Nan static bool dry_run; 20660aab2136SWang Nan 2067d20deb64SArnaldo Carvalho de Melo /* 2068d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 2069d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 2070b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 2071d20deb64SArnaldo Carvalho de Melo * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 2072d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 2073d20deb64SArnaldo Carvalho de Melo */ 2074efd21307SJiri Olsa static struct option __record_options[] = { 2075d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 207686470930SIngo Molnar "event selector. use 'perf list' to list available events", 2077f120f9d5SJiri Olsa parse_events_option), 2078d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 2079c171b552SLi Zefan "event filter", parse_filter), 20804ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 20814ba1faa1SWang Nan NULL, "don't record events from perf itself", 20824ba1faa1SWang Nan exclude_perf), 2083bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 2084d6d901c2SZhang, Yanmin "record events on existing process id"), 2085bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 2086d6d901c2SZhang, Yanmin "record events on existing thread id"), 2087d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 208886470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 2089509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 2090acac03faSKirill Smelkov "collect data without buffering"), 2091d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 2092daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 2093bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 209486470930SIngo Molnar "system-wide collection from all CPUs"), 2095bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 2096c45c6ea2SStephane Eranian "list of cpus to monitor"), 2097d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 20982d4f2799SJiri Olsa OPT_STRING('o', "output", &record.data.path, "file", 209986470930SIngo Molnar "output file name"), 210069e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 210169e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 21022e6cdf99SStephane Eranian "child tasks do not inherit counters"), 21034ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 21044ea648aeSWang Nan "synthesize non-sample events at the end of output"), 2105626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 210671184c6aSSong Liu OPT_BOOLEAN(0, "no-bpf-event", &record.opts.no_bpf_event, "record bpf events"), 2107b09c2364SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "strict-freq", &record.opts.strict_freq, 2108b09c2364SArnaldo Carvalho de Melo "Fail if the specified frequency can't be used"), 210967230479SArnaldo Carvalho de Melo OPT_CALLBACK('F', "freq", &record.opts, "freq or 'max'", 211067230479SArnaldo Carvalho de Melo "profile at this frequency", 211167230479SArnaldo Carvalho de Melo record__parse_freq), 2112e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 2113e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 2114e9db1310SAdrian Hunter record__parse_mmap_pages), 2115470530bbSAlexey Budankov OPT_CALLBACK(0, "mmap-flush", &record.opts, "number", 2116470530bbSAlexey Budankov "Minimal number of bytes that is extracted from mmap data pages (default: 1)", 2117470530bbSAlexey Budankov record__mmap_flush_parse), 2118d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "group", &record.opts.group, 211943bece79SLin Ming "put the counters into a counter group"), 21202ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 212109b0fd45SJiri Olsa NULL, "enables call-graph recording" , 212209b0fd45SJiri Olsa &record_callchain_opt), 212309b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 212476a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 212509b0fd45SJiri Olsa &record_parse_callchain_opt), 2126c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 21273da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 2128b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 2129d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 2130649c48a9SPeter Zijlstra "per thread counts"), 213156100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 21323b0a5daaSKan Liang OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, 21333b0a5daaSKan Liang "Record the sample physical addresses"), 2134b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 21353abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 21363abebc55SAdrian Hunter &record.opts.sample_time_set, 21373abebc55SAdrian Hunter "Record the sample timestamps"), 2138f290aa1fSJiri Olsa OPT_BOOLEAN_SET('P', "period", &record.opts.period, &record.opts.period_set, 2139f290aa1fSJiri Olsa "Record the sample period"), 2140d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 2141649c48a9SPeter Zijlstra "don't sample"), 2142d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 2143d2db9a98SWang Nan &record.no_buildid_cache_set, 2144a1ac1d3cSStephane Eranian "do not update the buildid cache"), 2145d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 2146d2db9a98SWang Nan &record.no_buildid_set, 2147baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 2148d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 2149023695d9SStephane Eranian "monitor event in cgroup name only", 2150023695d9SStephane Eranian parse_cgroups), 2151a6205a35SArnaldo Carvalho de Melo OPT_UINTEGER('D', "delay", &record.opts.initial_delay, 21526619a53eSAndi Kleen "ms to wait before starting measurement after program start"), 2153bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 2154bea03405SNamhyung Kim "user to profile"), 2155a5aabdacSStephane Eranian 2156a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 2157a5aabdacSStephane Eranian "branch any", "sample any taken branches", 2158a5aabdacSStephane Eranian parse_branch_stack), 2159a5aabdacSStephane Eranian 2160a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 2161a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 2162bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 216305484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 216405484298SAndi Kleen "sample by weight (on special events only)"), 2165475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 2166475eeab9SAndi Kleen "sample transaction flags (special events only)"), 21673aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 21683aa5939dSAdrian Hunter "use per-thread mmaps"), 2169bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 2170bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 21718e5bc76fSArnaldo Carvalho de Melo " use '-I?' to list register names", parse_regs), 217284c41742SAndi Kleen OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register", 217384c41742SAndi Kleen "sample selected machine registers on interrupt," 21748e5bc76fSArnaldo Carvalho de Melo " use '-I?' to list register names", parse_regs), 217585c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 217685c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 2177814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 2178814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 2179814c8c38SPeter Zijlstra parse_clockid), 21802dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 21812dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 21823fcb10e4SMark Drayton OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 21839d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 2184f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 2185f3b3614aSHari Bathini "Record namespaces events"), 2186b757bb09SAdrian Hunter OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events, 2187b757bb09SAdrian Hunter "Record context switch events"), 218885723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 218985723885SJiri Olsa "Configure all used events to run in kernel space.", 219085723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 219185723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 219285723885SJiri Olsa "Configure all used events to run in user space.", 219385723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 219471dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 219571dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 219671dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 219771dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 21987efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 21997efe0e03SHe Kuang "file", "vmlinux pathname"), 22006156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 22016156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 2202ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 2203ecfd7a9cSWang Nan "append timestamp to output filename"), 220468588bafSJin Yao OPT_BOOLEAN(0, "timestamp-boundary", &record.timestamp_boundary, 220568588bafSJin Yao "Record timestamp boundary (time of first/last samples)"), 2206cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 2207c38dab7dSAndi Kleen &record.switch_output.set, "signal or size[BKMG] or time[smhd]", 2208c38dab7dSAndi Kleen "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold", 2209dc0c6127SJiri Olsa "signal"), 221003724b2eSAndi Kleen OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files, 221103724b2eSAndi Kleen "Limit number of switch output generated files"), 22120aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 22130aab2136SWang Nan "Parse options then exit"), 2214d3d1af6fSAlexey Budankov #ifdef HAVE_AIO_SUPPORT 221593f20c0fSAlexey Budankov OPT_CALLBACK_OPTARG(0, "aio", &record.opts, 221693f20c0fSAlexey Budankov &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)", 2217d3d1af6fSAlexey Budankov record__aio_parse), 2218d3d1af6fSAlexey Budankov #endif 2219f4fe11b7SAlexey Budankov OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu", 2220f4fe11b7SAlexey Budankov "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer", 2221f4fe11b7SAlexey Budankov record__parse_affinity), 2222*504c1ad1SAlexey Budankov #ifdef HAVE_ZSTD_SUPPORT 2223*504c1ad1SAlexey Budankov OPT_CALLBACK_OPTARG('z', "compression-level", &record.opts, &comp_level_default, 2224*504c1ad1SAlexey Budankov "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)", 2225*504c1ad1SAlexey Budankov record__parse_comp_level), 2226*504c1ad1SAlexey Budankov #endif 222786470930SIngo Molnar OPT_END() 222886470930SIngo Molnar }; 222986470930SIngo Molnar 2230e5b2c207SNamhyung Kim struct option *record_options = __record_options; 2231e5b2c207SNamhyung Kim 2232b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 223386470930SIngo Molnar { 2234ef149c25SAdrian Hunter int err; 22358c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 223616ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 223786470930SIngo Molnar 223867230479SArnaldo Carvalho de Melo setlocale(LC_ALL, ""); 223967230479SArnaldo Carvalho de Melo 224048e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 224148e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 224248e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 224348e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 224448e1cab1SWang Nan # undef set_nobuild 224548e1cab1SWang Nan #endif 224648e1cab1SWang Nan 22477efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 22487efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 22497efe0e03SHe Kuang # define REASON "NO_DWARF=1" 22507efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 22517efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 22527efe0e03SHe Kuang # else 22537efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 22547efe0e03SHe Kuang # endif 22557efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 22567efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 22577efe0e03SHe Kuang # undef set_nobuild 22587efe0e03SHe Kuang # undef REASON 22597efe0e03SHe Kuang #endif 22607efe0e03SHe Kuang 22619d2ed645SAlexey Budankov CPU_ZERO(&rec->affinity_mask); 22629d2ed645SAlexey Budankov rec->opts.affinity = PERF_AFFINITY_SYS; 22639d2ed645SAlexey Budankov 22643e2be2daSArnaldo Carvalho de Melo rec->evlist = perf_evlist__new(); 22653e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 2266361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 2267361c99a6SArnaldo Carvalho de Melo 2268ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 2269ecc4c561SArnaldo Carvalho de Melo if (err) 2270ecc4c561SArnaldo Carvalho de Melo return err; 2271eb853e80SJiri Olsa 2272bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 2273a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 227468ba3235SNamhyung Kim if (quiet) 227568ba3235SNamhyung Kim perf_quiet_option(); 2276483635a9SJiri Olsa 2277483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 2278602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 2279483635a9SJiri Olsa rec->opts.target.system_wide = true; 228086470930SIngo Molnar 2281bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 2282c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 2283c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 2284c7118369SNamhyung Kim 2285023695d9SStephane Eranian } 2286*504c1ad1SAlexey Budankov 2287*504c1ad1SAlexey Budankov if (rec->opts.comp_level != 0) { 2288*504c1ad1SAlexey Budankov pr_debug("Compression enabled, disabling build id collection at the end of the session.\n"); 2289*504c1ad1SAlexey Budankov rec->no_buildid = true; 2290*504c1ad1SAlexey Budankov } 2291*504c1ad1SAlexey Budankov 2292b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 2293b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 2294c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 2295c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 2296c7118369SNamhyung Kim return -EINVAL; 2297b757bb09SAdrian Hunter } 2298023695d9SStephane Eranian 2299cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 2300cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 2301cb4e1ebbSJiri Olsa return -EINVAL; 2302cb4e1ebbSJiri Olsa } 2303cb4e1ebbSJiri Olsa 2304bfacbe3bSJiri Olsa if (rec->switch_output.time) { 2305bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 2306bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 2307bfacbe3bSJiri Olsa } 2308bfacbe3bSJiri Olsa 230903724b2eSAndi Kleen if (rec->switch_output.num_files) { 231003724b2eSAndi Kleen rec->switch_output.filenames = calloc(sizeof(char *), 231103724b2eSAndi Kleen rec->switch_output.num_files); 231203724b2eSAndi Kleen if (!rec->switch_output.filenames) 231303724b2eSAndi Kleen return -EINVAL; 231403724b2eSAndi Kleen } 231503724b2eSAndi Kleen 23161b36c03eSAdrian Hunter /* 23171b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 23181b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 23191b36c03eSAdrian Hunter */ 23201b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 23211b36c03eSAdrian Hunter 23221b36c03eSAdrian Hunter symbol__init(NULL); 23231b36c03eSAdrian Hunter 23244b5ea3bdSAdrian Hunter err = record__auxtrace_init(rec); 23251b36c03eSAdrian Hunter if (err) 23261b36c03eSAdrian Hunter goto out; 23271b36c03eSAdrian Hunter 23280aab2136SWang Nan if (dry_run) 23295c01ad60SAdrian Hunter goto out; 23300aab2136SWang Nan 2331d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 2332d7888573SWang Nan if (err) { 2333d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 2334d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 2335d7888573SWang Nan errbuf); 23365c01ad60SAdrian Hunter goto out; 2337d7888573SWang Nan } 2338d7888573SWang Nan 2339ef149c25SAdrian Hunter err = -ENOMEM; 2340ef149c25SAdrian Hunter 23416c443954SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict && !perf_evlist__exclude_kernel(rec->evlist)) 2342646aaea6SArnaldo Carvalho de Melo pr_warning( 2343646aaea6SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 2344ec80fde7SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict.\n\n" 2345646aaea6SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 2346646aaea6SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 2347646aaea6SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 2348646aaea6SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 2349646aaea6SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 2350ec80fde7SArnaldo Carvalho de Melo 23510c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 2352a1ac1d3cSStephane Eranian disable_buildid_cache(); 2353dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 23540c1d46a8SWang Nan /* 23550c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 23560c1d46a8SWang Nan * generation by default to reduce data file switching 23570c1d46a8SWang Nan * overhead. Still generate buildid if they are required 23580c1d46a8SWang Nan * explicitly using 23590c1d46a8SWang Nan * 236060437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 23610c1d46a8SWang Nan * --no-no-buildid-cache 23620c1d46a8SWang Nan * 23630c1d46a8SWang Nan * Following code equals to: 23640c1d46a8SWang Nan * 23650c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 23660c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 23670c1d46a8SWang Nan * disable_buildid_cache(); 23680c1d46a8SWang Nan */ 23690c1d46a8SWang Nan bool disable = true; 23700c1d46a8SWang Nan 23710c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 23720c1d46a8SWang Nan disable = false; 23730c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 23740c1d46a8SWang Nan disable = false; 23750c1d46a8SWang Nan if (disable) { 23760c1d46a8SWang Nan rec->no_buildid = true; 23770c1d46a8SWang Nan rec->no_buildid_cache = true; 23780c1d46a8SWang Nan disable_buildid_cache(); 23790c1d46a8SWang Nan } 23800c1d46a8SWang Nan } 2381655000e7SArnaldo Carvalho de Melo 23824ea648aeSWang Nan if (record.opts.overwrite) 23834ea648aeSWang Nan record.opts.tail_synthesize = true; 23844ea648aeSWang Nan 23853e2be2daSArnaldo Carvalho de Melo if (rec->evlist->nr_entries == 0 && 23864b4cd503SArnaldo Carvalho de Melo __perf_evlist__add_default(rec->evlist, !record.opts.no_samples) < 0) { 238769aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 2388394c01edSAdrian Hunter goto out; 2389bbd36e5eSPeter Zijlstra } 239086470930SIngo Molnar 239169e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 239269e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 239369e7e5b0SAdrian Hunter 2394602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 239516ad2ffbSNamhyung Kim if (err) { 2396602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 2397c3dec27bSJiri Olsa ui__warning("%s\n", errbuf); 239816ad2ffbSNamhyung Kim } 23994bd0f2d2SNamhyung Kim 2400602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 240116ad2ffbSNamhyung Kim if (err) { 240216ad2ffbSNamhyung Kim int saved_errno = errno; 240316ad2ffbSNamhyung Kim 2404602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 24053780f488SNamhyung Kim ui__error("%s", errbuf); 240616ad2ffbSNamhyung Kim 240716ad2ffbSNamhyung Kim err = -saved_errno; 2408394c01edSAdrian Hunter goto out; 240916ad2ffbSNamhyung Kim } 24100d37aa34SArnaldo Carvalho de Melo 2411ca800068SMengting Zhang /* Enable ignoring missing threads when -u/-p option is defined. */ 2412ca800068SMengting Zhang rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; 241323dc4f15SJiri Olsa 241416ad2ffbSNamhyung Kim err = -ENOMEM; 24153e2be2daSArnaldo Carvalho de Melo if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) 2416dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 241769aad6f1SArnaldo Carvalho de Melo 2418ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 2419ef149c25SAdrian Hunter if (err) 2420394c01edSAdrian Hunter goto out; 2421ef149c25SAdrian Hunter 24226156681bSNamhyung Kim /* 24236156681bSNamhyung Kim * We take all buildids when the file contains 24246156681bSNamhyung Kim * AUX area tracing data because we do not decode the 24256156681bSNamhyung Kim * trace because it would take too long. 24266156681bSNamhyung Kim */ 24276156681bSNamhyung Kim if (rec->opts.full_auxtrace) 24286156681bSNamhyung Kim rec->buildid_all = true; 24296156681bSNamhyung Kim 2430b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 243139d17dacSArnaldo Carvalho de Melo err = -EINVAL; 2432394c01edSAdrian Hunter goto out; 24337e4ff9e3SMike Galbraith } 24347e4ff9e3SMike Galbraith 243593f20c0fSAlexey Budankov if (rec->opts.nr_cblocks > nr_cblocks_max) 243693f20c0fSAlexey Budankov rec->opts.nr_cblocks = nr_cblocks_max; 24375d7f4116SAlexey Budankov pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks); 2438d3d1af6fSAlexey Budankov 24399d2ed645SAlexey Budankov pr_debug("affinity: %s\n", affinity_tags[rec->opts.affinity]); 2440470530bbSAlexey Budankov pr_debug("mmap flush: %d\n", rec->opts.mmap_flush); 24419d2ed645SAlexey Budankov 244251255a8aSAlexey Budankov if (rec->opts.comp_level > comp_level_max) 244351255a8aSAlexey Budankov rec->opts.comp_level = comp_level_max; 244451255a8aSAlexey Budankov pr_debug("comp level: %d\n", rec->opts.comp_level); 244551255a8aSAlexey Budankov 2446d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 2447394c01edSAdrian Hunter out: 244845604710SNamhyung Kim perf_evlist__delete(rec->evlist); 2449d65a458bSArnaldo Carvalho de Melo symbol__exit(); 2450ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 245139d17dacSArnaldo Carvalho de Melo return err; 245286470930SIngo Molnar } 24532dd6d8a1SAdrian Hunter 24542dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 24552dd6d8a1SAdrian Hunter { 2456dc0c6127SJiri Olsa struct record *rec = &record; 2457dc0c6127SJiri Olsa 24585f9cf599SWang Nan if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 24595f9cf599SWang Nan trigger_hit(&auxtrace_snapshot_trigger); 24602dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 1; 24615f9cf599SWang Nan if (auxtrace_record__snapshot_start(record.itr)) 24625f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 24635f9cf599SWang Nan } 24643c1cb7e3SWang Nan 2465dc0c6127SJiri Olsa if (switch_output_signal(rec)) 24663c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 24672dd6d8a1SAdrian Hunter } 2468bfacbe3bSJiri Olsa 2469bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 2470bfacbe3bSJiri Olsa { 2471bfacbe3bSJiri Olsa struct record *rec = &record; 2472bfacbe3bSJiri Olsa 2473bfacbe3bSJiri Olsa if (switch_output_time(rec)) 2474bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 2475bfacbe3bSJiri Olsa } 2476