186470930SIngo Molnar /* 286470930SIngo Molnar * builtin-record.c 386470930SIngo Molnar * 486470930SIngo Molnar * Builtin record command: Record the profile of a workload 586470930SIngo Molnar * (or a CPU, or a PID) into the perf.data output file - for 686470930SIngo Molnar * later analysis via perf report. 786470930SIngo Molnar */ 8b8f46c5aSXiao Guangrong #define _FILE_OFFSET_BITS 64 9b8f46c5aSXiao Guangrong 1086470930SIngo Molnar #include "builtin.h" 1186470930SIngo Molnar 1286470930SIngo Molnar #include "perf.h" 1386470930SIngo Molnar 146122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h" 1586470930SIngo Molnar #include "util/util.h" 1686470930SIngo Molnar #include "util/parse-options.h" 1786470930SIngo Molnar #include "util/parse-events.h" 1886470930SIngo Molnar 197c6a1c65SPeter Zijlstra #include "util/header.h" 2066e274f3SFrederic Weisbecker #include "util/event.h" 21*69aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 228f28827aSFrederic Weisbecker #include "util/debug.h" 2394c744b6SArnaldo Carvalho de Melo #include "util/session.h" 248d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 25a12b51c4SPaul Mackerras #include "util/cpumap.h" 267c6a1c65SPeter Zijlstra 2786470930SIngo Molnar #include <unistd.h> 2886470930SIngo Molnar #include <sched.h> 29a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 3086470930SIngo Molnar 31*69aad6f1SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 32*69aad6f1SArnaldo Carvalho de Melo 337865e817SFrederic Weisbecker enum write_mode_t { 347865e817SFrederic Weisbecker WRITE_FORCE, 357865e817SFrederic Weisbecker WRITE_APPEND 367865e817SFrederic Weisbecker }; 377865e817SFrederic Weisbecker 383de29cabSStephane Eranian static u64 user_interval = ULLONG_MAX; 393de29cabSStephane Eranian static u64 default_interval = 0; 40640c03ceSArnaldo Carvalho de Melo static u64 sample_type; 4186470930SIngo Molnar 4286470930SIngo Molnar static int nr_cpus = 0; 4386470930SIngo Molnar static unsigned int page_size; 4486470930SIngo Molnar static unsigned int mmap_pages = 128; 45f9212819SFrederic Weisbecker static unsigned int user_freq = UINT_MAX; 4642e59d7dSIngo Molnar static int freq = 1000; 4786470930SIngo Molnar static int output; 48529870e3STom Zanussi static int pipe_output = 0; 4986470930SIngo Molnar static const char *output_name = "perf.data"; 5086470930SIngo Molnar static int group = 0; 511967936dSArnaldo Carvalho de Melo static int realtime_prio = 0; 52c0555642SIan Munsie static bool raw_samples = false; 539c90a61cSArnaldo Carvalho de Melo static bool sample_id_all_avail = true; 54c0555642SIan Munsie static bool system_wide = false; 5586470930SIngo Molnar static pid_t target_pid = -1; 56d6d901c2SZhang, Yanmin static pid_t target_tid = -1; 57d6d901c2SZhang, Yanmin static pid_t *all_tids = NULL; 58d6d901c2SZhang, Yanmin static int thread_num = 0; 59933da83aSChris Wilson static pid_t child_pid = -1; 602e6cdf99SStephane Eranian static bool no_inherit = false; 617865e817SFrederic Weisbecker static enum write_mode_t write_mode = WRITE_FORCE; 62c0555642SIan Munsie static bool call_graph = false; 63c0555642SIan Munsie static bool inherit_stat = false; 64c0555642SIan Munsie static bool no_samples = false; 65c0555642SIan Munsie static bool sample_address = false; 669c90a61cSArnaldo Carvalho de Melo static bool sample_time = false; 67a1ac1d3cSStephane Eranian static bool no_buildid = false; 68baa2f6ceSArnaldo Carvalho de Melo static bool no_buildid_cache = false; 6986470930SIngo Molnar 7042e59d7dSIngo Molnar static long samples = 0; 7142e59d7dSIngo Molnar static u64 bytes_written = 0; 7286470930SIngo Molnar 73d6d901c2SZhang, Yanmin static struct pollfd *event_array; 7486470930SIngo Molnar 7542e59d7dSIngo Molnar static int nr_poll = 0; 7642e59d7dSIngo Molnar static int nr_cpu = 0; 7786470930SIngo Molnar 78f5970550SPeter Zijlstra static int file_new = 1; 796122e4e4SArnaldo Carvalho de Melo static off_t post_processing_offset; 807c6a1c65SPeter Zijlstra 8194c744b6SArnaldo Carvalho de Melo static struct perf_session *session; 82c45c6ea2SStephane Eranian static const char *cpu_list; 83f5970550SPeter Zijlstra 8486470930SIngo Molnar struct mmap_data { 8586470930SIngo Molnar void *base; 8686470930SIngo Molnar unsigned int mask; 8786470930SIngo Molnar unsigned int prev; 8886470930SIngo Molnar }; 8986470930SIngo Molnar 900e2e63ddSPeter Zijlstra static struct mmap_data mmap_array[MAX_NR_CPUS]; 9186470930SIngo Molnar 929d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md) 9386470930SIngo Molnar { 94cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 959d91a6f7SPeter Zijlstra long head; 9686470930SIngo Molnar 9786470930SIngo Molnar head = pc->data_head; 9886470930SIngo Molnar rmb(); 9986470930SIngo Molnar 10086470930SIngo Molnar return head; 10186470930SIngo Molnar } 10286470930SIngo Molnar 1039d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail) 1049d91a6f7SPeter Zijlstra { 105cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 1069d91a6f7SPeter Zijlstra 1079d91a6f7SPeter Zijlstra /* 1089d91a6f7SPeter Zijlstra * ensure all reads are done before we write the tail out. 1099d91a6f7SPeter Zijlstra */ 1109d91a6f7SPeter Zijlstra /* mb(); */ 1119d91a6f7SPeter Zijlstra pc->data_tail = tail; 1129d91a6f7SPeter Zijlstra } 1139d91a6f7SPeter Zijlstra 1149215545eSTom Zanussi static void advance_output(size_t size) 1159215545eSTom Zanussi { 1169215545eSTom Zanussi bytes_written += size; 1179215545eSTom Zanussi } 1189215545eSTom Zanussi 119f5970550SPeter Zijlstra static void write_output(void *buf, size_t size) 120f5970550SPeter Zijlstra { 121f5970550SPeter Zijlstra while (size) { 122f5970550SPeter Zijlstra int ret = write(output, buf, size); 123f5970550SPeter Zijlstra 124f5970550SPeter Zijlstra if (ret < 0) 125f5970550SPeter Zijlstra die("failed to write"); 126f5970550SPeter Zijlstra 127f5970550SPeter Zijlstra size -= ret; 128f5970550SPeter Zijlstra buf += ret; 129f5970550SPeter Zijlstra 130f5970550SPeter Zijlstra bytes_written += ret; 131f5970550SPeter Zijlstra } 132f5970550SPeter Zijlstra } 133f5970550SPeter Zijlstra 134d8f66248SArnaldo Carvalho de Melo static int process_synthesized_event(event_t *event, 135640c03ceSArnaldo Carvalho de Melo struct sample_data *sample __used, 136d8f66248SArnaldo Carvalho de Melo struct perf_session *self __used) 137234fbbf5SArnaldo Carvalho de Melo { 1386122e4e4SArnaldo Carvalho de Melo write_output(event, event->header.size); 139234fbbf5SArnaldo Carvalho de Melo return 0; 140234fbbf5SArnaldo Carvalho de Melo } 141234fbbf5SArnaldo Carvalho de Melo 14286470930SIngo Molnar static void mmap_read(struct mmap_data *md) 14386470930SIngo Molnar { 14486470930SIngo Molnar unsigned int head = mmap_read_head(md); 14586470930SIngo Molnar unsigned int old = md->prev; 14686470930SIngo Molnar unsigned char *data = md->base + page_size; 14786470930SIngo Molnar unsigned long size; 14886470930SIngo Molnar void *buf; 14986470930SIngo Molnar int diff; 15086470930SIngo Molnar 15186470930SIngo Molnar /* 15286470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 15386470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 15486470930SIngo Molnar * 15586470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 15686470930SIngo Molnar * 15786470930SIngo Molnar * In either case, truncate and restart at head. 15886470930SIngo Molnar */ 15986470930SIngo Molnar diff = head - old; 1609d91a6f7SPeter Zijlstra if (diff < 0) { 161ef365cefSRuss Anderson fprintf(stderr, "WARNING: failed to keep up with mmap data\n"); 16286470930SIngo Molnar /* 16386470930SIngo Molnar * head points to a known good entry, start there. 16486470930SIngo Molnar */ 16586470930SIngo Molnar old = head; 16686470930SIngo Molnar } 16786470930SIngo Molnar 16886470930SIngo Molnar if (old != head) 16986470930SIngo Molnar samples++; 17086470930SIngo Molnar 17186470930SIngo Molnar size = head - old; 17286470930SIngo Molnar 17386470930SIngo Molnar if ((old & md->mask) + size != (head & md->mask)) { 17486470930SIngo Molnar buf = &data[old & md->mask]; 17586470930SIngo Molnar size = md->mask + 1 - (old & md->mask); 17686470930SIngo Molnar old += size; 17786470930SIngo Molnar 1786122e4e4SArnaldo Carvalho de Melo write_output(buf, size); 17986470930SIngo Molnar } 18086470930SIngo Molnar 18186470930SIngo Molnar buf = &data[old & md->mask]; 18286470930SIngo Molnar size = head - old; 18386470930SIngo Molnar old += size; 18486470930SIngo Molnar 1856122e4e4SArnaldo Carvalho de Melo write_output(buf, size); 18686470930SIngo Molnar 18786470930SIngo Molnar md->prev = old; 1889d91a6f7SPeter Zijlstra mmap_write_tail(md, old); 18986470930SIngo Molnar } 19086470930SIngo Molnar 19186470930SIngo Molnar static volatile int done = 0; 192f7b7c26eSPeter Zijlstra static volatile int signr = -1; 19386470930SIngo Molnar 19486470930SIngo Molnar static void sig_handler(int sig) 19586470930SIngo Molnar { 19686470930SIngo Molnar done = 1; 197f7b7c26eSPeter Zijlstra signr = sig; 198f7b7c26eSPeter Zijlstra } 199f7b7c26eSPeter Zijlstra 200f7b7c26eSPeter Zijlstra static void sig_atexit(void) 201f7b7c26eSPeter Zijlstra { 2025ffc8881SIan Munsie if (child_pid > 0) 203933da83aSChris Wilson kill(child_pid, SIGTERM); 204933da83aSChris Wilson 20518483b81SArnaldo Carvalho de Melo if (signr == -1 || signr == SIGUSR1) 206f7b7c26eSPeter Zijlstra return; 207f7b7c26eSPeter Zijlstra 208f7b7c26eSPeter Zijlstra signal(signr, SIG_DFL); 209f7b7c26eSPeter Zijlstra kill(getpid(), signr); 21086470930SIngo Molnar } 21186470930SIngo Molnar 21286470930SIngo Molnar static int group_fd; 21386470930SIngo Molnar 214cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) 2157c6a1c65SPeter Zijlstra { 2167c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 2177c6a1c65SPeter Zijlstra 21894c744b6SArnaldo Carvalho de Melo if (nr < session->header.attrs) { 21994c744b6SArnaldo Carvalho de Melo h_attr = session->header.attr[nr]; 2207c6a1c65SPeter Zijlstra } else { 2217c6a1c65SPeter Zijlstra h_attr = perf_header_attr__new(a); 222dc79c0fcSArnaldo Carvalho de Melo if (h_attr != NULL) 22394c744b6SArnaldo Carvalho de Melo if (perf_header__add_attr(&session->header, h_attr) < 0) { 22411deb1f9SArnaldo Carvalho de Melo perf_header_attr__delete(h_attr); 22511deb1f9SArnaldo Carvalho de Melo h_attr = NULL; 22611deb1f9SArnaldo Carvalho de Melo } 2277c6a1c65SPeter Zijlstra } 2287c6a1c65SPeter Zijlstra 2297c6a1c65SPeter Zijlstra return h_attr; 2307c6a1c65SPeter Zijlstra } 2317c6a1c65SPeter Zijlstra 232*69aad6f1SArnaldo Carvalho de Melo static void create_counter(struct perf_evsel *evsel, int cpu) 23386470930SIngo Molnar { 234*69aad6f1SArnaldo Carvalho de Melo char *filter = evsel->filter; 235*69aad6f1SArnaldo Carvalho de Melo struct perf_event_attr *attr = &evsel->attr; 2367c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 237*69aad6f1SArnaldo Carvalho de Melo int track = !evsel->idx; /* only the first counter needs these */ 238d6d901c2SZhang, Yanmin int thread_index; 239c171b552SLi Zefan int ret; 2407c6a1c65SPeter Zijlstra struct { 2417c6a1c65SPeter Zijlstra u64 count; 2427c6a1c65SPeter Zijlstra u64 time_enabled; 2437c6a1c65SPeter Zijlstra u64 time_running; 2447c6a1c65SPeter Zijlstra u64 id; 2457c6a1c65SPeter Zijlstra } read_data; 246a43d3f08SArnaldo Carvalho de Melo /* 247a43d3f08SArnaldo Carvalho de Melo * Check if parse_single_tracepoint_event has already asked for 248a43d3f08SArnaldo Carvalho de Melo * PERF_SAMPLE_TIME. 249a43d3f08SArnaldo Carvalho de Melo * 250a43d3f08SArnaldo Carvalho de Melo * XXX this is kludgy but short term fix for problems introduced by 251a43d3f08SArnaldo Carvalho de Melo * eac23d1c that broke 'perf script' by having different sample_types 252a43d3f08SArnaldo Carvalho de Melo * when using multiple tracepoint events when we use a perf binary 253a43d3f08SArnaldo Carvalho de Melo * that tries to use sample_id_all on an older kernel. 254a43d3f08SArnaldo Carvalho de Melo * 255a43d3f08SArnaldo Carvalho de Melo * We need to move counter creation to perf_session, support 256a43d3f08SArnaldo Carvalho de Melo * different sample_types, etc. 257a43d3f08SArnaldo Carvalho de Melo */ 258a43d3f08SArnaldo Carvalho de Melo bool time_needed = attr->sample_type & PERF_SAMPLE_TIME; 2597c6a1c65SPeter Zijlstra 2607c6a1c65SPeter Zijlstra attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | 2617c6a1c65SPeter Zijlstra PERF_FORMAT_TOTAL_TIME_RUNNING | 2627c6a1c65SPeter Zijlstra PERF_FORMAT_ID; 26386470930SIngo Molnar 2643a9f131fSFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; 2653efa1cc9SIngo Molnar 2668907fd60SEric B Munson if (nr_counters > 1) 2678907fd60SEric B Munson attr->sample_type |= PERF_SAMPLE_ID; 2688907fd60SEric B Munson 269f9212819SFrederic Weisbecker /* 270f9212819SFrederic Weisbecker * We default some events to a 1 default interval. But keep 271f9212819SFrederic Weisbecker * it a weak assumption overridable by the user. 272f9212819SFrederic Weisbecker */ 273f9212819SFrederic Weisbecker if (!attr->sample_period || (user_freq != UINT_MAX && 2743de29cabSStephane Eranian user_interval != ULLONG_MAX)) { 27586470930SIngo Molnar if (freq) { 276ea1900e5SPeter Zijlstra attr->sample_type |= PERF_SAMPLE_PERIOD; 27786470930SIngo Molnar attr->freq = 1; 27886470930SIngo Molnar attr->sample_freq = freq; 279f9212819SFrederic Weisbecker } else { 280f9212819SFrederic Weisbecker attr->sample_period = default_interval; 281f9212819SFrederic Weisbecker } 28286470930SIngo Molnar } 2833efa1cc9SIngo Molnar 284649c48a9SPeter Zijlstra if (no_samples) 285649c48a9SPeter Zijlstra attr->sample_freq = 0; 286649c48a9SPeter Zijlstra 287649c48a9SPeter Zijlstra if (inherit_stat) 288649c48a9SPeter Zijlstra attr->inherit_stat = 1; 289649c48a9SPeter Zijlstra 2903af9e859SEric B Munson if (sample_address) { 2914bba828dSAnton Blanchard attr->sample_type |= PERF_SAMPLE_ADDR; 2923af9e859SEric B Munson attr->mmap_data = track; 2933af9e859SEric B Munson } 2944bba828dSAnton Blanchard 2953efa1cc9SIngo Molnar if (call_graph) 2963efa1cc9SIngo Molnar attr->sample_type |= PERF_SAMPLE_CALLCHAIN; 2973efa1cc9SIngo Molnar 298f60f3593SArun Sharma if (system_wide) 299f60f3593SArun Sharma attr->sample_type |= PERF_SAMPLE_CPU; 300f60f3593SArun Sharma 301a43d3f08SArnaldo Carvalho de Melo if (sample_id_all_avail && 302a43d3f08SArnaldo Carvalho de Melo (sample_time || system_wide || !no_inherit || cpu_list)) 3039c90a61cSArnaldo Carvalho de Melo attr->sample_type |= PERF_SAMPLE_TIME; 3049c90a61cSArnaldo Carvalho de Melo 305cd6feeeaSIngo Molnar if (raw_samples) { 3066ddf259dSIngo Molnar attr->sample_type |= PERF_SAMPLE_TIME; 307daac07b2SFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_RAW; 308cd6feeeaSIngo Molnar attr->sample_type |= PERF_SAMPLE_CPU; 309cd6feeeaSIngo Molnar } 310f413cdb8SFrederic Weisbecker 31186470930SIngo Molnar attr->mmap = track; 31286470930SIngo Molnar attr->comm = track; 3132e6cdf99SStephane Eranian attr->inherit = !no_inherit; 3142e6cdf99SStephane Eranian if (target_pid == -1 && target_tid == -1 && !system_wide) { 3154502d77cSPeter Zijlstra attr->disabled = 1; 316bedbfdeaSEric B Munson attr->enable_on_exec = 1; 31746be604bSZhang, Yanmin } 3189c90a61cSArnaldo Carvalho de Melo retry_sample_id: 3199c90a61cSArnaldo Carvalho de Melo attr->sample_id_all = sample_id_all_avail ? 1 : 0; 320bedbfdeaSEric B Munson 321d6d901c2SZhang, Yanmin for (thread_index = 0; thread_index < thread_num; thread_index++) { 3223da297a6SIngo Molnar try_again: 323*69aad6f1SArnaldo Carvalho de Melo FD(evsel, nr_cpu, thread_index) = sys_perf_event_open(attr, all_tids[thread_index], cpu, group_fd, 0); 32486470930SIngo Molnar 325*69aad6f1SArnaldo Carvalho de Melo if (FD(evsel, nr_cpu, thread_index) < 0) { 32686470930SIngo Molnar int err = errno; 32786470930SIngo Molnar 328c10edee2SPekka Enberg if (err == EPERM || err == EACCES) 3296230f2c7SArnaldo Carvalho de Melo die("Permission error - are you root?\n" 330d6d901c2SZhang, Yanmin "\t Consider tweaking" 331d6d901c2SZhang, Yanmin " /proc/sys/kernel/perf_event_paranoid.\n"); 332c45c6ea2SStephane Eranian else if (err == ENODEV && cpu_list) { 333d6d901c2SZhang, Yanmin die("No such device - did you specify" 334d6d901c2SZhang, Yanmin " an out-of-range profile CPU?\n"); 3359c90a61cSArnaldo Carvalho de Melo } else if (err == EINVAL && sample_id_all_avail) { 3369c90a61cSArnaldo Carvalho de Melo /* 3379c90a61cSArnaldo Carvalho de Melo * Old kernel, no attr->sample_id_type_all field 3389c90a61cSArnaldo Carvalho de Melo */ 3399c90a61cSArnaldo Carvalho de Melo sample_id_all_avail = false; 340a43d3f08SArnaldo Carvalho de Melo if (!sample_time && !raw_samples && !time_needed) 341eac23d1cSIan Munsie attr->sample_type &= ~PERF_SAMPLE_TIME; 342eac23d1cSIan Munsie 3439c90a61cSArnaldo Carvalho de Melo goto retry_sample_id; 344d6d901c2SZhang, Yanmin } 3453da297a6SIngo Molnar 3463da297a6SIngo Molnar /* 3473da297a6SIngo Molnar * If it's cycles then fall back to hrtimer 3483da297a6SIngo Molnar * based cpu-clock-tick sw counter, which 3493da297a6SIngo Molnar * is always available even if no PMU support: 3503da297a6SIngo Molnar */ 3513da297a6SIngo Molnar if (attr->type == PERF_TYPE_HARDWARE 352f4dbfa8fSPeter Zijlstra && attr->config == PERF_COUNT_HW_CPU_CYCLES) { 3533da297a6SIngo Molnar 3543da297a6SIngo Molnar if (verbose) 3553da297a6SIngo Molnar warning(" ... trying to fall back to cpu-clock-ticks\n"); 3563da297a6SIngo Molnar attr->type = PERF_TYPE_SOFTWARE; 357f4dbfa8fSPeter Zijlstra attr->config = PERF_COUNT_SW_CPU_CLOCK; 3583da297a6SIngo Molnar goto try_again; 3593da297a6SIngo Molnar } 36030c806a0SIngo Molnar printf("\n"); 361d9cf837eSCorey Ashford error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n", 362*69aad6f1SArnaldo Carvalho de Melo FD(evsel, nr_cpu, thread_index), strerror(err)); 363bfd45118SSimon Kaempflein 364bfd45118SSimon Kaempflein #if defined(__i386__) || defined(__x86_64__) 365bfd45118SSimon Kaempflein if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) 366d6d901c2SZhang, Yanmin die("No hardware sampling interrupt available." 367d6d901c2SZhang, Yanmin " No APIC? If so then you can boot the kernel" 368d6d901c2SZhang, Yanmin " with the \"lapic\" boot parameter to" 369d6d901c2SZhang, Yanmin " force-enable it.\n"); 370bfd45118SSimon Kaempflein #endif 371bfd45118SSimon Kaempflein 372cdd6c482SIngo Molnar die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); 37386470930SIngo Molnar exit(-1); 37486470930SIngo Molnar } 3753da297a6SIngo Molnar 376*69aad6f1SArnaldo Carvalho de Melo h_attr = get_header_attr(attr, evsel->idx); 377dc79c0fcSArnaldo Carvalho de Melo if (h_attr == NULL) 378dc79c0fcSArnaldo Carvalho de Melo die("nomem\n"); 3797c6a1c65SPeter Zijlstra 3807c6a1c65SPeter Zijlstra if (!file_new) { 3817c6a1c65SPeter Zijlstra if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { 3827c6a1c65SPeter Zijlstra fprintf(stderr, "incompatible append\n"); 3837c6a1c65SPeter Zijlstra exit(-1); 3847c6a1c65SPeter Zijlstra } 3857c6a1c65SPeter Zijlstra } 3867c6a1c65SPeter Zijlstra 387*69aad6f1SArnaldo Carvalho de Melo if (read(FD(evsel, nr_cpu, thread_index), &read_data, sizeof(read_data)) == -1) { 3880ab7368fSMatt Fleming perror("Unable to read perf file descriptor"); 3893928ddbeSFrederic Weisbecker exit(-1); 3903928ddbeSFrederic Weisbecker } 3917c6a1c65SPeter Zijlstra 39258754121SArnaldo Carvalho de Melo if (perf_header_attr__add_id(h_attr, read_data.id) < 0) { 39358754121SArnaldo Carvalho de Melo pr_warning("Not enough memory to add id\n"); 39458754121SArnaldo Carvalho de Melo exit(-1); 39558754121SArnaldo Carvalho de Melo } 3967c6a1c65SPeter Zijlstra 397*69aad6f1SArnaldo Carvalho de Melo assert(FD(evsel, nr_cpu, thread_index) >= 0); 398*69aad6f1SArnaldo Carvalho de Melo fcntl(FD(evsel, nr_cpu, thread_index), F_SETFL, O_NONBLOCK); 39986470930SIngo Molnar 40086470930SIngo Molnar /* 40186470930SIngo Molnar * First counter acts as the group leader: 40286470930SIngo Molnar */ 40386470930SIngo Molnar if (group && group_fd == -1) 404*69aad6f1SArnaldo Carvalho de Melo group_fd = FD(evsel, nr_cpu, thread_index); 40586470930SIngo Molnar 406*69aad6f1SArnaldo Carvalho de Melo if (evsel->idx || thread_index) { 407*69aad6f1SArnaldo Carvalho de Melo struct perf_evsel *first; 408*69aad6f1SArnaldo Carvalho de Melo first = list_entry(evsel_list.next, struct perf_evsel, node); 409*69aad6f1SArnaldo Carvalho de Melo ret = ioctl(FD(evsel, nr_cpu, thread_index), 4100e2e63ddSPeter Zijlstra PERF_EVENT_IOC_SET_OUTPUT, 411*69aad6f1SArnaldo Carvalho de Melo FD(first, nr_cpu, 0)); 4120e2e63ddSPeter Zijlstra if (ret) { 4130e2e63ddSPeter Zijlstra error("failed to set output: %d (%s)\n", errno, 4140e2e63ddSPeter Zijlstra strerror(errno)); 4150e2e63ddSPeter Zijlstra exit(-1); 4160e2e63ddSPeter Zijlstra } 4170e2e63ddSPeter Zijlstra } else { 4180e2e63ddSPeter Zijlstra mmap_array[nr_cpu].prev = 0; 4190e2e63ddSPeter Zijlstra mmap_array[nr_cpu].mask = mmap_pages*page_size - 1; 4200e2e63ddSPeter Zijlstra mmap_array[nr_cpu].base = mmap(NULL, (mmap_pages+1)*page_size, 421*69aad6f1SArnaldo Carvalho de Melo PROT_READ | PROT_WRITE, MAP_SHARED, FD(evsel, nr_cpu, thread_index), 0); 4220e2e63ddSPeter Zijlstra if (mmap_array[nr_cpu].base == MAP_FAILED) { 4230e2e63ddSPeter Zijlstra error("failed to mmap with %d (%s)\n", errno, strerror(errno)); 4240e2e63ddSPeter Zijlstra exit(-1); 4250e2e63ddSPeter Zijlstra } 4260e2e63ddSPeter Zijlstra 427*69aad6f1SArnaldo Carvalho de Melo event_array[nr_poll].fd = FD(evsel, nr_cpu, thread_index); 42886470930SIngo Molnar event_array[nr_poll].events = POLLIN; 42986470930SIngo Molnar nr_poll++; 43086470930SIngo Molnar } 431d1302522SFrederic Weisbecker 432c171b552SLi Zefan if (filter != NULL) { 433*69aad6f1SArnaldo Carvalho de Melo ret = ioctl(FD(evsel, nr_cpu, thread_index), 434c171b552SLi Zefan PERF_EVENT_IOC_SET_FILTER, filter); 435c171b552SLi Zefan if (ret) { 436c171b552SLi Zefan error("failed to set filter with %d (%s)\n", errno, 437c171b552SLi Zefan strerror(errno)); 438c171b552SLi Zefan exit(-1); 439c171b552SLi Zefan } 440c171b552SLi Zefan } 44186470930SIngo Molnar } 442a43d3f08SArnaldo Carvalho de Melo 443a43d3f08SArnaldo Carvalho de Melo if (!sample_type) 444a43d3f08SArnaldo Carvalho de Melo sample_type = attr->sample_type; 445d6d901c2SZhang, Yanmin } 44686470930SIngo Molnar 447d6d901c2SZhang, Yanmin static void open_counters(int cpu) 44886470930SIngo Molnar { 449*69aad6f1SArnaldo Carvalho de Melo struct perf_evsel *pos; 45086470930SIngo Molnar 45186470930SIngo Molnar group_fd = -1; 452*69aad6f1SArnaldo Carvalho de Melo 453*69aad6f1SArnaldo Carvalho de Melo list_for_each_entry(pos, &evsel_list, node) 454*69aad6f1SArnaldo Carvalho de Melo create_counter(pos, cpu); 45586470930SIngo Molnar 45686470930SIngo Molnar nr_cpu++; 45786470930SIngo Molnar } 45886470930SIngo Molnar 4596122e4e4SArnaldo Carvalho de Melo static int process_buildids(void) 4606122e4e4SArnaldo Carvalho de Melo { 4616122e4e4SArnaldo Carvalho de Melo u64 size = lseek(output, 0, SEEK_CUR); 4626122e4e4SArnaldo Carvalho de Melo 4639f591fd7SArnaldo Carvalho de Melo if (size == 0) 4649f591fd7SArnaldo Carvalho de Melo return 0; 4659f591fd7SArnaldo Carvalho de Melo 4666122e4e4SArnaldo Carvalho de Melo session->fd = output; 4676122e4e4SArnaldo Carvalho de Melo return __perf_session__process_events(session, post_processing_offset, 4686122e4e4SArnaldo Carvalho de Melo size - post_processing_offset, 4696122e4e4SArnaldo Carvalho de Melo size, &build_id__mark_dso_hit_ops); 4706122e4e4SArnaldo Carvalho de Melo } 4716122e4e4SArnaldo Carvalho de Melo 472f5970550SPeter Zijlstra static void atexit_header(void) 473f5970550SPeter Zijlstra { 474c7929e47STom Zanussi if (!pipe_output) { 47594c744b6SArnaldo Carvalho de Melo session->header.data_size += bytes_written; 476f5970550SPeter Zijlstra 477baa2f6ceSArnaldo Carvalho de Melo if (!no_buildid) 4786122e4e4SArnaldo Carvalho de Melo process_buildids(); 47994c744b6SArnaldo Carvalho de Melo perf_header__write(&session->header, output, true); 48039d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 481d65a458bSArnaldo Carvalho de Melo symbol__exit(); 482c7929e47STom Zanussi } 483f5970550SPeter Zijlstra } 484f5970550SPeter Zijlstra 48523346f21SArnaldo Carvalho de Melo static void event__synthesize_guest_os(struct machine *machine, void *data) 486a1645ce1SZhang, Yanmin { 487a1645ce1SZhang, Yanmin int err; 48823346f21SArnaldo Carvalho de Melo struct perf_session *psession = data; 489a1645ce1SZhang, Yanmin 49023346f21SArnaldo Carvalho de Melo if (machine__is_host(machine)) 491a1645ce1SZhang, Yanmin return; 492a1645ce1SZhang, Yanmin 493a1645ce1SZhang, Yanmin /* 494a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 495a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 496a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 497a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 498a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 499a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 500a1645ce1SZhang, Yanmin */ 501a1645ce1SZhang, Yanmin err = event__synthesize_modules(process_synthesized_event, 50223346f21SArnaldo Carvalho de Melo psession, machine); 503a1645ce1SZhang, Yanmin if (err < 0) 504a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 50523346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 506a1645ce1SZhang, Yanmin 507a1645ce1SZhang, Yanmin /* 508a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 509a1645ce1SZhang, Yanmin * have no _text sometimes. 510a1645ce1SZhang, Yanmin */ 511a1645ce1SZhang, Yanmin err = event__synthesize_kernel_mmap(process_synthesized_event, 51223346f21SArnaldo Carvalho de Melo psession, machine, "_text"); 513a1645ce1SZhang, Yanmin if (err < 0) 514a1645ce1SZhang, Yanmin err = event__synthesize_kernel_mmap(process_synthesized_event, 51523346f21SArnaldo Carvalho de Melo psession, machine, "_stext"); 516a1645ce1SZhang, Yanmin if (err < 0) 517a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 51823346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 519a1645ce1SZhang, Yanmin } 520a1645ce1SZhang, Yanmin 52198402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 52298402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 52398402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 52498402807SFrederic Weisbecker }; 52598402807SFrederic Weisbecker 52698402807SFrederic Weisbecker static void mmap_read_all(void) 52798402807SFrederic Weisbecker { 5280e2e63ddSPeter Zijlstra int i; 52998402807SFrederic Weisbecker 53098402807SFrederic Weisbecker for (i = 0; i < nr_cpu; i++) { 5310e2e63ddSPeter Zijlstra if (mmap_array[i].base) 5320e2e63ddSPeter Zijlstra mmap_read(&mmap_array[i]); 53398402807SFrederic Weisbecker } 53498402807SFrederic Weisbecker 53598402807SFrederic Weisbecker if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO)) 53698402807SFrederic Weisbecker write_output(&finished_round_event, sizeof(finished_round_event)); 53798402807SFrederic Weisbecker } 53898402807SFrederic Weisbecker 539d4db3f16SArnaldo Carvalho de Melo static int __cmd_record(int argc, const char **argv) 54086470930SIngo Molnar { 541*69aad6f1SArnaldo Carvalho de Melo int i; 54286470930SIngo Molnar struct stat st; 54386470930SIngo Molnar int flags; 5444dc0a04bSArnaldo Carvalho de Melo int err; 5458b412664SPeter Zijlstra unsigned long waking = 0; 546856e9660SPeter Zijlstra int child_ready_pipe[2], go_pipe[2]; 54746be604bSZhang, Yanmin const bool forks = argc > 0; 548856e9660SPeter Zijlstra char buf; 54923346f21SArnaldo Carvalho de Melo struct machine *machine; 55086470930SIngo Molnar 55186470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 55286470930SIngo Molnar 553f5970550SPeter Zijlstra atexit(sig_atexit); 554f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 555f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 55618483b81SArnaldo Carvalho de Melo signal(SIGUSR1, sig_handler); 557f5970550SPeter Zijlstra 558d4db3f16SArnaldo Carvalho de Melo if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { 559856e9660SPeter Zijlstra perror("failed to create pipes"); 560856e9660SPeter Zijlstra exit(-1); 561856e9660SPeter Zijlstra } 562856e9660SPeter Zijlstra 563529870e3STom Zanussi if (!strcmp(output_name, "-")) 564529870e3STom Zanussi pipe_output = 1; 565529870e3STom Zanussi else if (!stat(output_name, &st) && st.st_size) { 5667865e817SFrederic Weisbecker if (write_mode == WRITE_FORCE) { 567b38d3464SArnaldo Carvalho de Melo char oldname[PATH_MAX]; 568b38d3464SArnaldo Carvalho de Melo snprintf(oldname, sizeof(oldname), "%s.old", 569b38d3464SArnaldo Carvalho de Melo output_name); 570b38d3464SArnaldo Carvalho de Melo unlink(oldname); 571b38d3464SArnaldo Carvalho de Melo rename(output_name, oldname); 572b38d3464SArnaldo Carvalho de Melo } 5737865e817SFrederic Weisbecker } else if (write_mode == WRITE_APPEND) { 5747865e817SFrederic Weisbecker write_mode = WRITE_FORCE; 575266e0e21SPierre Habouzit } 57686470930SIngo Molnar 577f887f301SXiao Guangrong flags = O_CREAT|O_RDWR; 5787865e817SFrederic Weisbecker if (write_mode == WRITE_APPEND) 579f5970550SPeter Zijlstra file_new = 0; 58086470930SIngo Molnar else 58186470930SIngo Molnar flags |= O_TRUNC; 58286470930SIngo Molnar 583529870e3STom Zanussi if (pipe_output) 584529870e3STom Zanussi output = STDOUT_FILENO; 585529870e3STom Zanussi else 58686470930SIngo Molnar output = open(output_name, flags, S_IRUSR | S_IWUSR); 58786470930SIngo Molnar if (output < 0) { 58886470930SIngo Molnar perror("failed to create output file"); 58986470930SIngo Molnar exit(-1); 59086470930SIngo Molnar } 59186470930SIngo Molnar 5927865e817SFrederic Weisbecker session = perf_session__new(output_name, O_WRONLY, 59321ef97f0SIan Munsie write_mode == WRITE_FORCE, false, NULL); 59494c744b6SArnaldo Carvalho de Melo if (session == NULL) { 595a9a70bbcSArnaldo Carvalho de Melo pr_err("Not enough memory for reading perf file header\n"); 596a9a70bbcSArnaldo Carvalho de Melo return -1; 597a9a70bbcSArnaldo Carvalho de Melo } 598a9a70bbcSArnaldo Carvalho de Melo 599baa2f6ceSArnaldo Carvalho de Melo if (!no_buildid) 600baa2f6ceSArnaldo Carvalho de Melo perf_header__set_feat(&session->header, HEADER_BUILD_ID); 601baa2f6ceSArnaldo Carvalho de Melo 6024dc0a04bSArnaldo Carvalho de Melo if (!file_new) { 6038dc58101STom Zanussi err = perf_header__read(session, output); 6044dc0a04bSArnaldo Carvalho de Melo if (err < 0) 60539d17dacSArnaldo Carvalho de Melo goto out_delete_session; 6064dc0a04bSArnaldo Carvalho de Melo } 6074dc0a04bSArnaldo Carvalho de Melo 608*69aad6f1SArnaldo Carvalho de Melo if (have_tracepoints(&evsel_list)) 60994c744b6SArnaldo Carvalho de Melo perf_header__set_feat(&session->header, HEADER_TRACE_INFO); 61003456a15SFrederic Weisbecker 61139d17dacSArnaldo Carvalho de Melo /* 61239d17dacSArnaldo Carvalho de Melo * perf_session__delete(session) will be called at atexit_header() 61339d17dacSArnaldo Carvalho de Melo */ 614f5970550SPeter Zijlstra atexit(atexit_header); 615f5970550SPeter Zijlstra 616d4db3f16SArnaldo Carvalho de Melo if (forks) { 61746be604bSZhang, Yanmin child_pid = fork(); 6182fb750e8SBorislav Petkov if (child_pid < 0) { 619856e9660SPeter Zijlstra perror("failed to fork"); 620856e9660SPeter Zijlstra exit(-1); 621856e9660SPeter Zijlstra } 6227c6a1c65SPeter Zijlstra 62346be604bSZhang, Yanmin if (!child_pid) { 624529870e3STom Zanussi if (pipe_output) 625529870e3STom Zanussi dup2(2, 1); 626856e9660SPeter Zijlstra close(child_ready_pipe[0]); 627856e9660SPeter Zijlstra close(go_pipe[1]); 628856e9660SPeter Zijlstra fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 629856e9660SPeter Zijlstra 630856e9660SPeter Zijlstra /* 631856e9660SPeter Zijlstra * Do a dummy execvp to get the PLT entry resolved, 632856e9660SPeter Zijlstra * so we avoid the resolver overhead on the real 633856e9660SPeter Zijlstra * execvp call. 634856e9660SPeter Zijlstra */ 635856e9660SPeter Zijlstra execvp("", (char **)argv); 636856e9660SPeter Zijlstra 637856e9660SPeter Zijlstra /* 638856e9660SPeter Zijlstra * Tell the parent we're ready to go 639856e9660SPeter Zijlstra */ 640856e9660SPeter Zijlstra close(child_ready_pipe[1]); 641856e9660SPeter Zijlstra 642856e9660SPeter Zijlstra /* 643856e9660SPeter Zijlstra * Wait until the parent tells us to go. 644856e9660SPeter Zijlstra */ 645856e9660SPeter Zijlstra if (read(go_pipe[0], &buf, 1) == -1) 646856e9660SPeter Zijlstra perror("unable to read pipe"); 647856e9660SPeter Zijlstra 648856e9660SPeter Zijlstra execvp(argv[0], (char **)argv); 649856e9660SPeter Zijlstra 650856e9660SPeter Zijlstra perror(argv[0]); 65118483b81SArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 652856e9660SPeter Zijlstra exit(-1); 653856e9660SPeter Zijlstra } 654856e9660SPeter Zijlstra 655d6d901c2SZhang, Yanmin if (!system_wide && target_tid == -1 && target_pid == -1) 656d6d901c2SZhang, Yanmin all_tids[0] = child_pid; 657d6d901c2SZhang, Yanmin 658856e9660SPeter Zijlstra close(child_ready_pipe[1]); 659856e9660SPeter Zijlstra close(go_pipe[0]); 660856e9660SPeter Zijlstra /* 661856e9660SPeter Zijlstra * wait for child to settle 662856e9660SPeter Zijlstra */ 663856e9660SPeter Zijlstra if (read(child_ready_pipe[0], &buf, 1) == -1) { 664856e9660SPeter Zijlstra perror("unable to read pipe"); 665856e9660SPeter Zijlstra exit(-1); 666856e9660SPeter Zijlstra } 667856e9660SPeter Zijlstra close(child_ready_pipe[0]); 668856e9660SPeter Zijlstra } 669856e9660SPeter Zijlstra 670c45c6ea2SStephane Eranian if (!system_wide && no_inherit && !cpu_list) { 671c45c6ea2SStephane Eranian open_counters(-1); 6720a5ac846SJens Axboe } else { 6730a5ac846SJens Axboe for (i = 0; i < nr_cpus; i++) 674d6d901c2SZhang, Yanmin open_counters(cpumap[i]); 6750a5ac846SJens Axboe } 67686470930SIngo Molnar 677640c03ceSArnaldo Carvalho de Melo perf_session__set_sample_type(session, sample_type); 678640c03ceSArnaldo Carvalho de Melo 679529870e3STom Zanussi if (pipe_output) { 680529870e3STom Zanussi err = perf_header__write_pipe(output); 681529870e3STom Zanussi if (err < 0) 682529870e3STom Zanussi return err; 683529870e3STom Zanussi } else if (file_new) { 68494c744b6SArnaldo Carvalho de Melo err = perf_header__write(&session->header, output, false); 685d5eed904SArnaldo Carvalho de Melo if (err < 0) 686d5eed904SArnaldo Carvalho de Melo return err; 687d5eed904SArnaldo Carvalho de Melo } 6887c6a1c65SPeter Zijlstra 6896122e4e4SArnaldo Carvalho de Melo post_processing_offset = lseek(output, 0, SEEK_CUR); 6906122e4e4SArnaldo Carvalho de Melo 6919c90a61cSArnaldo Carvalho de Melo perf_session__set_sample_id_all(session, sample_id_all_avail); 6929c90a61cSArnaldo Carvalho de Melo 6932c46dbb5STom Zanussi if (pipe_output) { 6942c46dbb5STom Zanussi err = event__synthesize_attrs(&session->header, 6952c46dbb5STom Zanussi process_synthesized_event, 6962c46dbb5STom Zanussi session); 6972c46dbb5STom Zanussi if (err < 0) { 6982c46dbb5STom Zanussi pr_err("Couldn't synthesize attrs.\n"); 6992c46dbb5STom Zanussi return err; 7002c46dbb5STom Zanussi } 701cd19a035STom Zanussi 702cd19a035STom Zanussi err = event__synthesize_event_types(process_synthesized_event, 703cd19a035STom Zanussi session); 704cd19a035STom Zanussi if (err < 0) { 705cd19a035STom Zanussi pr_err("Couldn't synthesize event_types.\n"); 706cd19a035STom Zanussi return err; 707cd19a035STom Zanussi } 7089215545eSTom Zanussi 709*69aad6f1SArnaldo Carvalho de Melo if (have_tracepoints(&evsel_list)) { 71063e0c771STom Zanussi /* 71163e0c771STom Zanussi * FIXME err <= 0 here actually means that 71263e0c771STom Zanussi * there were no tracepoints so its not really 71363e0c771STom Zanussi * an error, just that we don't need to 71463e0c771STom Zanussi * synthesize anything. We really have to 71563e0c771STom Zanussi * return this more properly and also 71663e0c771STom Zanussi * propagate errors that now are calling die() 71763e0c771STom Zanussi */ 718*69aad6f1SArnaldo Carvalho de Melo err = event__synthesize_tracing_data(output, &evsel_list, 7199215545eSTom Zanussi process_synthesized_event, 7209215545eSTom Zanussi session); 72163e0c771STom Zanussi if (err <= 0) { 72263e0c771STom Zanussi pr_err("Couldn't record tracing data.\n"); 72363e0c771STom Zanussi return err; 72463e0c771STom Zanussi } 7259215545eSTom Zanussi advance_output(err); 7262c46dbb5STom Zanussi } 72763e0c771STom Zanussi } 7282c46dbb5STom Zanussi 72923346f21SArnaldo Carvalho de Melo machine = perf_session__find_host_machine(session); 73023346f21SArnaldo Carvalho de Melo if (!machine) { 731a1645ce1SZhang, Yanmin pr_err("Couldn't find native kernel information.\n"); 732a1645ce1SZhang, Yanmin return -1; 733a1645ce1SZhang, Yanmin } 734a1645ce1SZhang, Yanmin 73556b03f3cSArnaldo Carvalho de Melo err = event__synthesize_kernel_mmap(process_synthesized_event, 73623346f21SArnaldo Carvalho de Melo session, machine, "_text"); 73770162138SArnaldo Carvalho de Melo if (err < 0) 73870162138SArnaldo Carvalho de Melo err = event__synthesize_kernel_mmap(process_synthesized_event, 73923346f21SArnaldo Carvalho de Melo session, machine, "_stext"); 740c1a3a4b9SArnaldo Carvalho de Melo if (err < 0) 741c1a3a4b9SArnaldo Carvalho de Melo pr_err("Couldn't record kernel reference relocation symbol\n" 742c1a3a4b9SArnaldo Carvalho de Melo "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 743c1a3a4b9SArnaldo Carvalho de Melo "Check /proc/kallsyms permission or run as root.\n"); 74456b03f3cSArnaldo Carvalho de Melo 745a1645ce1SZhang, Yanmin err = event__synthesize_modules(process_synthesized_event, 74623346f21SArnaldo Carvalho de Melo session, machine); 747c1a3a4b9SArnaldo Carvalho de Melo if (err < 0) 748c1a3a4b9SArnaldo Carvalho de Melo pr_err("Couldn't record kernel module information.\n" 749c1a3a4b9SArnaldo Carvalho de Melo "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 750c1a3a4b9SArnaldo Carvalho de Melo "Check /proc/modules permission or run as root.\n"); 751c1a3a4b9SArnaldo Carvalho de Melo 752a1645ce1SZhang, Yanmin if (perf_guest) 75323346f21SArnaldo Carvalho de Melo perf_session__process_machines(session, event__synthesize_guest_os); 754b7cece76SArnaldo Carvalho de Melo 755cf103a14SStephane Eranian if (!system_wide) 756d6d901c2SZhang, Yanmin event__synthesize_thread(target_tid, process_synthesized_event, 757d8f66248SArnaldo Carvalho de Melo session); 758234fbbf5SArnaldo Carvalho de Melo else 759d8f66248SArnaldo Carvalho de Melo event__synthesize_threads(process_synthesized_event, session); 7607c6a1c65SPeter Zijlstra 76186470930SIngo Molnar if (realtime_prio) { 76286470930SIngo Molnar struct sched_param param; 76386470930SIngo Molnar 76486470930SIngo Molnar param.sched_priority = realtime_prio; 76586470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 7666beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 76786470930SIngo Molnar exit(-1); 76886470930SIngo Molnar } 76986470930SIngo Molnar } 77086470930SIngo Molnar 771856e9660SPeter Zijlstra /* 772856e9660SPeter Zijlstra * Let the child rip 773856e9660SPeter Zijlstra */ 774d4db3f16SArnaldo Carvalho de Melo if (forks) 775856e9660SPeter Zijlstra close(go_pipe[1]); 776856e9660SPeter Zijlstra 777649c48a9SPeter Zijlstra for (;;) { 77886470930SIngo Molnar int hits = samples; 779d6d901c2SZhang, Yanmin int thread; 78086470930SIngo Molnar 78198402807SFrederic Weisbecker mmap_read_all(); 78286470930SIngo Molnar 783649c48a9SPeter Zijlstra if (hits == samples) { 784649c48a9SPeter Zijlstra if (done) 785649c48a9SPeter Zijlstra break; 7864dc0a04bSArnaldo Carvalho de Melo err = poll(event_array, nr_poll, -1); 7878b412664SPeter Zijlstra waking++; 7888b412664SPeter Zijlstra } 7898b412664SPeter Zijlstra 7908b412664SPeter Zijlstra if (done) { 7918b412664SPeter Zijlstra for (i = 0; i < nr_cpu; i++) { 792*69aad6f1SArnaldo Carvalho de Melo struct perf_evsel *pos; 793*69aad6f1SArnaldo Carvalho de Melo 794*69aad6f1SArnaldo Carvalho de Melo list_for_each_entry(pos, &evsel_list, node) { 795d6d901c2SZhang, Yanmin for (thread = 0; 796d6d901c2SZhang, Yanmin thread < thread_num; 797d6d901c2SZhang, Yanmin thread++) 798*69aad6f1SArnaldo Carvalho de Melo ioctl(FD(pos, i, thread), 799d6d901c2SZhang, Yanmin PERF_EVENT_IOC_DISABLE); 800d6d901c2SZhang, Yanmin } 80186470930SIngo Molnar } 802649c48a9SPeter Zijlstra } 8038b412664SPeter Zijlstra } 8048b412664SPeter Zijlstra 80518483b81SArnaldo Carvalho de Melo if (quiet || signr == SIGUSR1) 806b44308f5SArnaldo Carvalho de Melo return 0; 807b44308f5SArnaldo Carvalho de Melo 8088b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 80986470930SIngo Molnar 81086470930SIngo Molnar /* 81186470930SIngo Molnar * Approximate RIP event size: 24 bytes. 81286470930SIngo Molnar */ 81386470930SIngo Molnar fprintf(stderr, 81486470930SIngo Molnar "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", 81586470930SIngo Molnar (double)bytes_written / 1024.0 / 1024.0, 81686470930SIngo Molnar output_name, 81786470930SIngo Molnar bytes_written / 24); 81886470930SIngo Molnar 81986470930SIngo Molnar return 0; 82039d17dacSArnaldo Carvalho de Melo 82139d17dacSArnaldo Carvalho de Melo out_delete_session: 82239d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 82339d17dacSArnaldo Carvalho de Melo return err; 82486470930SIngo Molnar } 82586470930SIngo Molnar 82686470930SIngo Molnar static const char * const record_usage[] = { 82786470930SIngo Molnar "perf record [<options>] [<command>]", 82886470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 82986470930SIngo Molnar NULL 83086470930SIngo Molnar }; 83186470930SIngo Molnar 8327865e817SFrederic Weisbecker static bool force, append_file; 8337865e817SFrederic Weisbecker 834bca647aaSTom Zanussi const struct option record_options[] = { 83586470930SIngo Molnar OPT_CALLBACK('e', "event", NULL, "event", 83686470930SIngo Molnar "event selector. use 'perf list' to list available events", 83786470930SIngo Molnar parse_events), 838c171b552SLi Zefan OPT_CALLBACK(0, "filter", NULL, "filter", 839c171b552SLi Zefan "event filter", parse_filter), 84086470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 841d6d901c2SZhang, Yanmin "record events on existing process id"), 842d6d901c2SZhang, Yanmin OPT_INTEGER('t', "tid", &target_tid, 843d6d901c2SZhang, Yanmin "record events on existing thread id"), 84486470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 84586470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 846daac07b2SFrederic Weisbecker OPT_BOOLEAN('R', "raw-samples", &raw_samples, 847daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 84886470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 84986470930SIngo Molnar "system-wide collection from all CPUs"), 85086470930SIngo Molnar OPT_BOOLEAN('A', "append", &append_file, 85186470930SIngo Molnar "append to the output file to do incremental profiling"), 852c45c6ea2SStephane Eranian OPT_STRING('C', "cpu", &cpu_list, "cpu", 853c45c6ea2SStephane Eranian "list of cpus to monitor"), 85486470930SIngo Molnar OPT_BOOLEAN('f', "force", &force, 8557865e817SFrederic Weisbecker "overwrite existing data file (deprecated)"), 8563de29cabSStephane Eranian OPT_U64('c', "count", &user_interval, "event period to sample"), 85786470930SIngo Molnar OPT_STRING('o', "output", &output_name, "file", 85886470930SIngo Molnar "output file name"), 8592e6cdf99SStephane Eranian OPT_BOOLEAN('i', "no-inherit", &no_inherit, 8602e6cdf99SStephane Eranian "child tasks do not inherit counters"), 8611967936dSArnaldo Carvalho de Melo OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"), 8621967936dSArnaldo Carvalho de Melo OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"), 8633efa1cc9SIngo Molnar OPT_BOOLEAN('g', "call-graph", &call_graph, 8643efa1cc9SIngo Molnar "do call-graph (stack chain/backtrace) recording"), 865c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 8663da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 867b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 868649c48a9SPeter Zijlstra OPT_BOOLEAN('s', "stat", &inherit_stat, 869649c48a9SPeter Zijlstra "per thread counts"), 8704bba828dSAnton Blanchard OPT_BOOLEAN('d', "data", &sample_address, 8714bba828dSAnton Blanchard "Sample addresses"), 8729c90a61cSArnaldo Carvalho de Melo OPT_BOOLEAN('T', "timestamp", &sample_time, "Sample timestamps"), 873649c48a9SPeter Zijlstra OPT_BOOLEAN('n', "no-samples", &no_samples, 874649c48a9SPeter Zijlstra "don't sample"), 875baa2f6ceSArnaldo Carvalho de Melo OPT_BOOLEAN('N', "no-buildid-cache", &no_buildid_cache, 876a1ac1d3cSStephane Eranian "do not update the buildid cache"), 877baa2f6ceSArnaldo Carvalho de Melo OPT_BOOLEAN('B', "no-buildid", &no_buildid, 878baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 87986470930SIngo Molnar OPT_END() 88086470930SIngo Molnar }; 88186470930SIngo Molnar 882f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used) 88386470930SIngo Molnar { 884*69aad6f1SArnaldo Carvalho de Melo int err = -ENOMEM; 885*69aad6f1SArnaldo Carvalho de Melo struct perf_evsel *pos; 88686470930SIngo Molnar 887bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 888a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 889d6d901c2SZhang, Yanmin if (!argc && target_pid == -1 && target_tid == -1 && 890c45c6ea2SStephane Eranian !system_wide && !cpu_list) 891bca647aaSTom Zanussi usage_with_options(record_usage, record_options); 89286470930SIngo Molnar 8937865e817SFrederic Weisbecker if (force && append_file) { 8947865e817SFrederic Weisbecker fprintf(stderr, "Can't overwrite and append at the same time." 8957865e817SFrederic Weisbecker " You need to choose between -f and -A"); 896bca647aaSTom Zanussi usage_with_options(record_usage, record_options); 8977865e817SFrederic Weisbecker } else if (append_file) { 8987865e817SFrederic Weisbecker write_mode = WRITE_APPEND; 8997865e817SFrederic Weisbecker } else { 9007865e817SFrederic Weisbecker write_mode = WRITE_FORCE; 9017865e817SFrederic Weisbecker } 9027865e817SFrederic Weisbecker 903655000e7SArnaldo Carvalho de Melo symbol__init(); 904baa2f6ceSArnaldo Carvalho de Melo 905baa2f6ceSArnaldo Carvalho de Melo if (no_buildid_cache || no_buildid) 906a1ac1d3cSStephane Eranian disable_buildid_cache(); 907655000e7SArnaldo Carvalho de Melo 908*69aad6f1SArnaldo Carvalho de Melo if (list_empty(&evsel_list) && perf_evsel_list__create_default() < 0) { 909*69aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 910*69aad6f1SArnaldo Carvalho de Melo goto out_symbol_exit; 911bbd36e5eSPeter Zijlstra } 91286470930SIngo Molnar 913d6d901c2SZhang, Yanmin if (target_pid != -1) { 914d6d901c2SZhang, Yanmin target_tid = target_pid; 915d6d901c2SZhang, Yanmin thread_num = find_all_tid(target_pid, &all_tids); 916d6d901c2SZhang, Yanmin if (thread_num <= 0) { 917d6d901c2SZhang, Yanmin fprintf(stderr, "Can't find all threads of pid %d\n", 918d6d901c2SZhang, Yanmin target_pid); 919bca647aaSTom Zanussi usage_with_options(record_usage, record_options); 920d6d901c2SZhang, Yanmin } 921d6d901c2SZhang, Yanmin } else { 922d6d901c2SZhang, Yanmin all_tids=malloc(sizeof(pid_t)); 923d6d901c2SZhang, Yanmin if (!all_tids) 924d65a458bSArnaldo Carvalho de Melo goto out_symbol_exit; 925d6d901c2SZhang, Yanmin 926d6d901c2SZhang, Yanmin all_tids[0] = target_tid; 927d6d901c2SZhang, Yanmin thread_num = 1; 928d6d901c2SZhang, Yanmin } 929d6d901c2SZhang, Yanmin 930*69aad6f1SArnaldo Carvalho de Melo nr_cpus = read_cpu_map(cpu_list); 931*69aad6f1SArnaldo Carvalho de Melo if (nr_cpus < 1) { 932*69aad6f1SArnaldo Carvalho de Melo perror("failed to collect number of CPUs"); 933*69aad6f1SArnaldo Carvalho de Melo return -1; 934d6d901c2SZhang, Yanmin } 935*69aad6f1SArnaldo Carvalho de Melo 936*69aad6f1SArnaldo Carvalho de Melo list_for_each_entry(pos, &evsel_list, node) { 937*69aad6f1SArnaldo Carvalho de Melo if (perf_evsel__alloc_fd(pos, nr_cpus, thread_num) < 0) 938*69aad6f1SArnaldo Carvalho de Melo goto out_free_fd; 939d6d901c2SZhang, Yanmin } 940d6d901c2SZhang, Yanmin event_array = malloc( 941d6d901c2SZhang, Yanmin sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num); 942d6d901c2SZhang, Yanmin if (!event_array) 94339d17dacSArnaldo Carvalho de Melo goto out_free_fd; 944d6d901c2SZhang, Yanmin 9453de29cabSStephane Eranian if (user_interval != ULLONG_MAX) 946f9212819SFrederic Weisbecker default_interval = user_interval; 947f9212819SFrederic Weisbecker if (user_freq != UINT_MAX) 948f9212819SFrederic Weisbecker freq = user_freq; 949f9212819SFrederic Weisbecker 9507e4ff9e3SMike Galbraith /* 9517e4ff9e3SMike Galbraith * User specified count overrides default frequency. 9527e4ff9e3SMike Galbraith */ 9537e4ff9e3SMike Galbraith if (default_interval) 9547e4ff9e3SMike Galbraith freq = 0; 9557e4ff9e3SMike Galbraith else if (freq) { 9567e4ff9e3SMike Galbraith default_interval = freq; 9577e4ff9e3SMike Galbraith } else { 9587e4ff9e3SMike Galbraith fprintf(stderr, "frequency and count are zero, aborting\n"); 95939d17dacSArnaldo Carvalho de Melo err = -EINVAL; 96039d17dacSArnaldo Carvalho de Melo goto out_free_event_array; 9617e4ff9e3SMike Galbraith } 9627e4ff9e3SMike Galbraith 96339d17dacSArnaldo Carvalho de Melo err = __cmd_record(argc, argv); 96439d17dacSArnaldo Carvalho de Melo 96539d17dacSArnaldo Carvalho de Melo out_free_event_array: 96639d17dacSArnaldo Carvalho de Melo free(event_array); 96739d17dacSArnaldo Carvalho de Melo out_free_fd: 968*69aad6f1SArnaldo Carvalho de Melo list_for_each_entry(pos, &evsel_list, node) 969*69aad6f1SArnaldo Carvalho de Melo perf_evsel__free_fd(pos); 97039d17dacSArnaldo Carvalho de Melo free(all_tids); 97139d17dacSArnaldo Carvalho de Melo all_tids = NULL; 972d65a458bSArnaldo Carvalho de Melo out_symbol_exit: 973d65a458bSArnaldo Carvalho de Melo symbol__exit(); 97439d17dacSArnaldo Carvalho de Melo return err; 97586470930SIngo Molnar } 976