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*361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h" 2269aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 238f28827aSFrederic Weisbecker #include "util/debug.h" 2494c744b6SArnaldo Carvalho de Melo #include "util/session.h" 258d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 26a12b51c4SPaul Mackerras #include "util/cpumap.h" 277c6a1c65SPeter Zijlstra 2886470930SIngo Molnar #include <unistd.h> 2986470930SIngo Molnar #include <sched.h> 30a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 3186470930SIngo Molnar 3269aad6f1SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 3369aad6f1SArnaldo Carvalho de Melo 347865e817SFrederic Weisbecker enum write_mode_t { 357865e817SFrederic Weisbecker WRITE_FORCE, 367865e817SFrederic Weisbecker WRITE_APPEND 377865e817SFrederic Weisbecker }; 387865e817SFrederic Weisbecker 393de29cabSStephane Eranian static u64 user_interval = ULLONG_MAX; 403de29cabSStephane Eranian static u64 default_interval = 0; 41640c03ceSArnaldo Carvalho de Melo static u64 sample_type; 4286470930SIngo Molnar 4360d567e2SArnaldo Carvalho de Melo static struct cpu_map *cpus; 4486470930SIngo Molnar static unsigned int page_size; 4586470930SIngo Molnar static unsigned int mmap_pages = 128; 46f9212819SFrederic Weisbecker static unsigned int user_freq = UINT_MAX; 4742e59d7dSIngo Molnar static int freq = 1000; 4886470930SIngo Molnar static int output; 49529870e3STom Zanussi static int pipe_output = 0; 5086470930SIngo Molnar static const char *output_name = "perf.data"; 5186470930SIngo Molnar static int group = 0; 521967936dSArnaldo Carvalho de Melo static int realtime_prio = 0; 53acac03faSKirill Smelkov static bool nodelay = false; 54c0555642SIan Munsie static bool raw_samples = false; 559c90a61cSArnaldo Carvalho de Melo static bool sample_id_all_avail = true; 56c0555642SIan Munsie static bool system_wide = false; 5786470930SIngo Molnar static pid_t target_pid = -1; 58d6d901c2SZhang, Yanmin static pid_t target_tid = -1; 595c98d466SArnaldo Carvalho de Melo static struct thread_map *threads; 60933da83aSChris Wilson static pid_t child_pid = -1; 612e6cdf99SStephane Eranian static bool no_inherit = false; 627865e817SFrederic Weisbecker static enum write_mode_t write_mode = WRITE_FORCE; 63c0555642SIan Munsie static bool call_graph = false; 64c0555642SIan Munsie static bool inherit_stat = false; 65c0555642SIan Munsie static bool no_samples = false; 66c0555642SIan Munsie static bool sample_address = false; 679c90a61cSArnaldo Carvalho de Melo static bool sample_time = false; 68a1ac1d3cSStephane Eranian static bool no_buildid = false; 69baa2f6ceSArnaldo Carvalho de Melo static bool no_buildid_cache = false; 70*361c99a6SArnaldo Carvalho de Melo static struct perf_evlist *evsel_list; 7186470930SIngo Molnar 7242e59d7dSIngo Molnar static long samples = 0; 7342e59d7dSIngo Molnar static u64 bytes_written = 0; 7486470930SIngo Molnar 75d6d901c2SZhang, Yanmin static struct pollfd *event_array; 7686470930SIngo Molnar 7742e59d7dSIngo Molnar static int nr_poll = 0; 7842e59d7dSIngo Molnar static int nr_cpu = 0; 7986470930SIngo Molnar 80f5970550SPeter Zijlstra static int file_new = 1; 816122e4e4SArnaldo Carvalho de Melo static off_t post_processing_offset; 827c6a1c65SPeter Zijlstra 8394c744b6SArnaldo Carvalho de Melo static struct perf_session *session; 84c45c6ea2SStephane Eranian static const char *cpu_list; 85f5970550SPeter Zijlstra 8686470930SIngo Molnar struct mmap_data { 8786470930SIngo Molnar void *base; 8886470930SIngo Molnar unsigned int mask; 8986470930SIngo Molnar unsigned int prev; 9086470930SIngo Molnar }; 9186470930SIngo Molnar 920e2e63ddSPeter Zijlstra static struct mmap_data mmap_array[MAX_NR_CPUS]; 9386470930SIngo Molnar 949d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md) 9586470930SIngo Molnar { 96cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 979d91a6f7SPeter Zijlstra long head; 9886470930SIngo Molnar 9986470930SIngo Molnar head = pc->data_head; 10086470930SIngo Molnar rmb(); 10186470930SIngo Molnar 10286470930SIngo Molnar return head; 10386470930SIngo Molnar } 10486470930SIngo Molnar 1059d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail) 1069d91a6f7SPeter Zijlstra { 107cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 1089d91a6f7SPeter Zijlstra 1099d91a6f7SPeter Zijlstra /* 1109d91a6f7SPeter Zijlstra * ensure all reads are done before we write the tail out. 1119d91a6f7SPeter Zijlstra */ 1129d91a6f7SPeter Zijlstra /* mb(); */ 1139d91a6f7SPeter Zijlstra pc->data_tail = tail; 1149d91a6f7SPeter Zijlstra } 1159d91a6f7SPeter Zijlstra 1169215545eSTom Zanussi static void advance_output(size_t size) 1179215545eSTom Zanussi { 1189215545eSTom Zanussi bytes_written += size; 1199215545eSTom Zanussi } 1209215545eSTom Zanussi 121f5970550SPeter Zijlstra static void write_output(void *buf, size_t size) 122f5970550SPeter Zijlstra { 123f5970550SPeter Zijlstra while (size) { 124f5970550SPeter Zijlstra int ret = write(output, buf, size); 125f5970550SPeter Zijlstra 126f5970550SPeter Zijlstra if (ret < 0) 127f5970550SPeter Zijlstra die("failed to write"); 128f5970550SPeter Zijlstra 129f5970550SPeter Zijlstra size -= ret; 130f5970550SPeter Zijlstra buf += ret; 131f5970550SPeter Zijlstra 132f5970550SPeter Zijlstra bytes_written += ret; 133f5970550SPeter Zijlstra } 134f5970550SPeter Zijlstra } 135f5970550SPeter Zijlstra 136d8f66248SArnaldo Carvalho de Melo static int process_synthesized_event(event_t *event, 137640c03ceSArnaldo Carvalho de Melo struct sample_data *sample __used, 138d8f66248SArnaldo Carvalho de Melo struct perf_session *self __used) 139234fbbf5SArnaldo Carvalho de Melo { 1406122e4e4SArnaldo Carvalho de Melo write_output(event, event->header.size); 141234fbbf5SArnaldo Carvalho de Melo return 0; 142234fbbf5SArnaldo Carvalho de Melo } 143234fbbf5SArnaldo Carvalho de Melo 14486470930SIngo Molnar static void mmap_read(struct mmap_data *md) 14586470930SIngo Molnar { 14686470930SIngo Molnar unsigned int head = mmap_read_head(md); 14786470930SIngo Molnar unsigned int old = md->prev; 14886470930SIngo Molnar unsigned char *data = md->base + page_size; 14986470930SIngo Molnar unsigned long size; 15086470930SIngo Molnar void *buf; 15186470930SIngo Molnar int diff; 15286470930SIngo Molnar 15386470930SIngo Molnar /* 15486470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 15586470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 15686470930SIngo Molnar * 15786470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 15886470930SIngo Molnar * 15986470930SIngo Molnar * In either case, truncate and restart at head. 16086470930SIngo Molnar */ 16186470930SIngo Molnar diff = head - old; 1629d91a6f7SPeter Zijlstra if (diff < 0) { 163ef365cefSRuss Anderson fprintf(stderr, "WARNING: failed to keep up with mmap data\n"); 16486470930SIngo Molnar /* 16586470930SIngo Molnar * head points to a known good entry, start there. 16686470930SIngo Molnar */ 16786470930SIngo Molnar old = head; 16886470930SIngo Molnar } 16986470930SIngo Molnar 17086470930SIngo Molnar if (old != head) 17186470930SIngo Molnar samples++; 17286470930SIngo Molnar 17386470930SIngo Molnar size = head - old; 17486470930SIngo Molnar 17586470930SIngo Molnar if ((old & md->mask) + size != (head & md->mask)) { 17686470930SIngo Molnar buf = &data[old & md->mask]; 17786470930SIngo Molnar size = md->mask + 1 - (old & md->mask); 17886470930SIngo Molnar old += size; 17986470930SIngo Molnar 1806122e4e4SArnaldo Carvalho de Melo write_output(buf, size); 18186470930SIngo Molnar } 18286470930SIngo Molnar 18386470930SIngo Molnar buf = &data[old & md->mask]; 18486470930SIngo Molnar size = head - old; 18586470930SIngo Molnar old += size; 18686470930SIngo Molnar 1876122e4e4SArnaldo Carvalho de Melo write_output(buf, size); 18886470930SIngo Molnar 18986470930SIngo Molnar md->prev = old; 1909d91a6f7SPeter Zijlstra mmap_write_tail(md, old); 19186470930SIngo Molnar } 19286470930SIngo Molnar 19386470930SIngo Molnar static volatile int done = 0; 194f7b7c26eSPeter Zijlstra static volatile int signr = -1; 19586470930SIngo Molnar 19686470930SIngo Molnar static void sig_handler(int sig) 19786470930SIngo Molnar { 19886470930SIngo Molnar done = 1; 199f7b7c26eSPeter Zijlstra signr = sig; 200f7b7c26eSPeter Zijlstra } 201f7b7c26eSPeter Zijlstra 202f7b7c26eSPeter Zijlstra static void sig_atexit(void) 203f7b7c26eSPeter Zijlstra { 2045ffc8881SIan Munsie if (child_pid > 0) 205933da83aSChris Wilson kill(child_pid, SIGTERM); 206933da83aSChris Wilson 20718483b81SArnaldo Carvalho de Melo if (signr == -1 || signr == SIGUSR1) 208f7b7c26eSPeter Zijlstra return; 209f7b7c26eSPeter Zijlstra 210f7b7c26eSPeter Zijlstra signal(signr, SIG_DFL); 211f7b7c26eSPeter Zijlstra kill(getpid(), signr); 21286470930SIngo Molnar } 21386470930SIngo Molnar 21486470930SIngo Molnar static int group_fd; 21586470930SIngo Molnar 216cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) 2177c6a1c65SPeter Zijlstra { 2187c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 2197c6a1c65SPeter Zijlstra 22094c744b6SArnaldo Carvalho de Melo if (nr < session->header.attrs) { 22194c744b6SArnaldo Carvalho de Melo h_attr = session->header.attr[nr]; 2227c6a1c65SPeter Zijlstra } else { 2237c6a1c65SPeter Zijlstra h_attr = perf_header_attr__new(a); 224dc79c0fcSArnaldo Carvalho de Melo if (h_attr != NULL) 22594c744b6SArnaldo Carvalho de Melo if (perf_header__add_attr(&session->header, h_attr) < 0) { 22611deb1f9SArnaldo Carvalho de Melo perf_header_attr__delete(h_attr); 22711deb1f9SArnaldo Carvalho de Melo h_attr = NULL; 22811deb1f9SArnaldo Carvalho de Melo } 2297c6a1c65SPeter Zijlstra } 2307c6a1c65SPeter Zijlstra 2317c6a1c65SPeter Zijlstra return h_attr; 2327c6a1c65SPeter Zijlstra } 2337c6a1c65SPeter Zijlstra 234*361c99a6SArnaldo Carvalho de Melo static void create_counter(struct perf_evlist *evlist, 235*361c99a6SArnaldo Carvalho de Melo struct perf_evsel *evsel, int cpu) 23686470930SIngo Molnar { 23769aad6f1SArnaldo Carvalho de Melo char *filter = evsel->filter; 23869aad6f1SArnaldo Carvalho de Melo struct perf_event_attr *attr = &evsel->attr; 2397c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 24069aad6f1SArnaldo Carvalho de Melo int track = !evsel->idx; /* only the first counter needs these */ 241d6d901c2SZhang, Yanmin int thread_index; 242c171b552SLi Zefan int ret; 2437c6a1c65SPeter Zijlstra struct { 2447c6a1c65SPeter Zijlstra u64 count; 2457c6a1c65SPeter Zijlstra u64 time_enabled; 2467c6a1c65SPeter Zijlstra u64 time_running; 2477c6a1c65SPeter Zijlstra u64 id; 2487c6a1c65SPeter Zijlstra } read_data; 249a43d3f08SArnaldo Carvalho de Melo /* 250a43d3f08SArnaldo Carvalho de Melo * Check if parse_single_tracepoint_event has already asked for 251a43d3f08SArnaldo Carvalho de Melo * PERF_SAMPLE_TIME. 252a43d3f08SArnaldo Carvalho de Melo * 253a43d3f08SArnaldo Carvalho de Melo * XXX this is kludgy but short term fix for problems introduced by 254a43d3f08SArnaldo Carvalho de Melo * eac23d1c that broke 'perf script' by having different sample_types 255a43d3f08SArnaldo Carvalho de Melo * when using multiple tracepoint events when we use a perf binary 256a43d3f08SArnaldo Carvalho de Melo * that tries to use sample_id_all on an older kernel. 257a43d3f08SArnaldo Carvalho de Melo * 258a43d3f08SArnaldo Carvalho de Melo * We need to move counter creation to perf_session, support 259a43d3f08SArnaldo Carvalho de Melo * different sample_types, etc. 260a43d3f08SArnaldo Carvalho de Melo */ 261a43d3f08SArnaldo Carvalho de Melo bool time_needed = attr->sample_type & PERF_SAMPLE_TIME; 2627c6a1c65SPeter Zijlstra 2637c6a1c65SPeter Zijlstra attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | 2647c6a1c65SPeter Zijlstra PERF_FORMAT_TOTAL_TIME_RUNNING | 2657c6a1c65SPeter Zijlstra PERF_FORMAT_ID; 26686470930SIngo Molnar 2673a9f131fSFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; 2683efa1cc9SIngo Molnar 269*361c99a6SArnaldo Carvalho de Melo if (evlist->nr_entries > 1) 2708907fd60SEric B Munson attr->sample_type |= PERF_SAMPLE_ID; 2718907fd60SEric B Munson 272f9212819SFrederic Weisbecker /* 273f9212819SFrederic Weisbecker * We default some events to a 1 default interval. But keep 274f9212819SFrederic Weisbecker * it a weak assumption overridable by the user. 275f9212819SFrederic Weisbecker */ 276f9212819SFrederic Weisbecker if (!attr->sample_period || (user_freq != UINT_MAX && 2773de29cabSStephane Eranian user_interval != ULLONG_MAX)) { 27886470930SIngo Molnar if (freq) { 279ea1900e5SPeter Zijlstra attr->sample_type |= PERF_SAMPLE_PERIOD; 28086470930SIngo Molnar attr->freq = 1; 28186470930SIngo Molnar attr->sample_freq = freq; 282f9212819SFrederic Weisbecker } else { 283f9212819SFrederic Weisbecker attr->sample_period = default_interval; 284f9212819SFrederic Weisbecker } 28586470930SIngo Molnar } 2863efa1cc9SIngo Molnar 287649c48a9SPeter Zijlstra if (no_samples) 288649c48a9SPeter Zijlstra attr->sample_freq = 0; 289649c48a9SPeter Zijlstra 290649c48a9SPeter Zijlstra if (inherit_stat) 291649c48a9SPeter Zijlstra attr->inherit_stat = 1; 292649c48a9SPeter Zijlstra 2933af9e859SEric B Munson if (sample_address) { 2944bba828dSAnton Blanchard attr->sample_type |= PERF_SAMPLE_ADDR; 2953af9e859SEric B Munson attr->mmap_data = track; 2963af9e859SEric B Munson } 2974bba828dSAnton Blanchard 2983efa1cc9SIngo Molnar if (call_graph) 2993efa1cc9SIngo Molnar attr->sample_type |= PERF_SAMPLE_CALLCHAIN; 3003efa1cc9SIngo Molnar 301f60f3593SArun Sharma if (system_wide) 302f60f3593SArun Sharma attr->sample_type |= PERF_SAMPLE_CPU; 303f60f3593SArun Sharma 304a43d3f08SArnaldo Carvalho de Melo if (sample_id_all_avail && 305a43d3f08SArnaldo Carvalho de Melo (sample_time || system_wide || !no_inherit || cpu_list)) 3069c90a61cSArnaldo Carvalho de Melo attr->sample_type |= PERF_SAMPLE_TIME; 3079c90a61cSArnaldo Carvalho de Melo 308cd6feeeaSIngo Molnar if (raw_samples) { 3096ddf259dSIngo Molnar attr->sample_type |= PERF_SAMPLE_TIME; 310daac07b2SFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_RAW; 311cd6feeeaSIngo Molnar attr->sample_type |= PERF_SAMPLE_CPU; 312cd6feeeaSIngo Molnar } 313f413cdb8SFrederic Weisbecker 314acac03faSKirill Smelkov if (nodelay) { 315acac03faSKirill Smelkov attr->watermark = 0; 316acac03faSKirill Smelkov attr->wakeup_events = 1; 317acac03faSKirill Smelkov } 318acac03faSKirill Smelkov 31986470930SIngo Molnar attr->mmap = track; 32086470930SIngo Molnar attr->comm = track; 3212e6cdf99SStephane Eranian attr->inherit = !no_inherit; 3222e6cdf99SStephane Eranian if (target_pid == -1 && target_tid == -1 && !system_wide) { 3234502d77cSPeter Zijlstra attr->disabled = 1; 324bedbfdeaSEric B Munson attr->enable_on_exec = 1; 32546be604bSZhang, Yanmin } 3269c90a61cSArnaldo Carvalho de Melo retry_sample_id: 3279c90a61cSArnaldo Carvalho de Melo attr->sample_id_all = sample_id_all_avail ? 1 : 0; 328bedbfdeaSEric B Munson 3295c98d466SArnaldo Carvalho de Melo for (thread_index = 0; thread_index < threads->nr; thread_index++) { 3303da297a6SIngo Molnar try_again: 3315c98d466SArnaldo Carvalho de Melo FD(evsel, nr_cpu, thread_index) = sys_perf_event_open(attr, threads->map[thread_index], cpu, group_fd, 0); 33286470930SIngo Molnar 33369aad6f1SArnaldo Carvalho de Melo if (FD(evsel, nr_cpu, thread_index) < 0) { 33486470930SIngo Molnar int err = errno; 33586470930SIngo Molnar 336c10edee2SPekka Enberg if (err == EPERM || err == EACCES) 3376230f2c7SArnaldo Carvalho de Melo die("Permission error - are you root?\n" 338d6d901c2SZhang, Yanmin "\t Consider tweaking" 339d6d901c2SZhang, Yanmin " /proc/sys/kernel/perf_event_paranoid.\n"); 340c45c6ea2SStephane Eranian else if (err == ENODEV && cpu_list) { 341d6d901c2SZhang, Yanmin die("No such device - did you specify" 342d6d901c2SZhang, Yanmin " an out-of-range profile CPU?\n"); 3439c90a61cSArnaldo Carvalho de Melo } else if (err == EINVAL && sample_id_all_avail) { 3449c90a61cSArnaldo Carvalho de Melo /* 3459c90a61cSArnaldo Carvalho de Melo * Old kernel, no attr->sample_id_type_all field 3469c90a61cSArnaldo Carvalho de Melo */ 3479c90a61cSArnaldo Carvalho de Melo sample_id_all_avail = false; 348a43d3f08SArnaldo Carvalho de Melo if (!sample_time && !raw_samples && !time_needed) 349eac23d1cSIan Munsie attr->sample_type &= ~PERF_SAMPLE_TIME; 350eac23d1cSIan Munsie 3519c90a61cSArnaldo Carvalho de Melo goto retry_sample_id; 352d6d901c2SZhang, Yanmin } 3533da297a6SIngo Molnar 3543da297a6SIngo Molnar /* 3553da297a6SIngo Molnar * If it's cycles then fall back to hrtimer 3563da297a6SIngo Molnar * based cpu-clock-tick sw counter, which 3573da297a6SIngo Molnar * is always available even if no PMU support: 3583da297a6SIngo Molnar */ 3593da297a6SIngo Molnar if (attr->type == PERF_TYPE_HARDWARE 360f4dbfa8fSPeter Zijlstra && attr->config == PERF_COUNT_HW_CPU_CYCLES) { 3613da297a6SIngo Molnar 3623da297a6SIngo Molnar if (verbose) 3633da297a6SIngo Molnar warning(" ... trying to fall back to cpu-clock-ticks\n"); 3643da297a6SIngo Molnar attr->type = PERF_TYPE_SOFTWARE; 365f4dbfa8fSPeter Zijlstra attr->config = PERF_COUNT_SW_CPU_CLOCK; 3663da297a6SIngo Molnar goto try_again; 3673da297a6SIngo Molnar } 36830c806a0SIngo Molnar printf("\n"); 369d9cf837eSCorey Ashford error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n", 37069aad6f1SArnaldo Carvalho de Melo FD(evsel, nr_cpu, thread_index), strerror(err)); 371bfd45118SSimon Kaempflein 372bfd45118SSimon Kaempflein #if defined(__i386__) || defined(__x86_64__) 373bfd45118SSimon Kaempflein if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) 374d6d901c2SZhang, Yanmin die("No hardware sampling interrupt available." 375d6d901c2SZhang, Yanmin " No APIC? If so then you can boot the kernel" 376d6d901c2SZhang, Yanmin " with the \"lapic\" boot parameter to" 377d6d901c2SZhang, Yanmin " force-enable it.\n"); 378bfd45118SSimon Kaempflein #endif 379bfd45118SSimon Kaempflein 380cdd6c482SIngo Molnar die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); 38186470930SIngo Molnar exit(-1); 38286470930SIngo Molnar } 3833da297a6SIngo Molnar 38469aad6f1SArnaldo Carvalho de Melo h_attr = get_header_attr(attr, evsel->idx); 385dc79c0fcSArnaldo Carvalho de Melo if (h_attr == NULL) 386dc79c0fcSArnaldo Carvalho de Melo die("nomem\n"); 3877c6a1c65SPeter Zijlstra 3887c6a1c65SPeter Zijlstra if (!file_new) { 3897c6a1c65SPeter Zijlstra if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { 3907c6a1c65SPeter Zijlstra fprintf(stderr, "incompatible append\n"); 3917c6a1c65SPeter Zijlstra exit(-1); 3927c6a1c65SPeter Zijlstra } 3937c6a1c65SPeter Zijlstra } 3947c6a1c65SPeter Zijlstra 39569aad6f1SArnaldo Carvalho de Melo if (read(FD(evsel, nr_cpu, thread_index), &read_data, sizeof(read_data)) == -1) { 3960ab7368fSMatt Fleming perror("Unable to read perf file descriptor"); 3973928ddbeSFrederic Weisbecker exit(-1); 3983928ddbeSFrederic Weisbecker } 3997c6a1c65SPeter Zijlstra 40058754121SArnaldo Carvalho de Melo if (perf_header_attr__add_id(h_attr, read_data.id) < 0) { 40158754121SArnaldo Carvalho de Melo pr_warning("Not enough memory to add id\n"); 40258754121SArnaldo Carvalho de Melo exit(-1); 40358754121SArnaldo Carvalho de Melo } 4047c6a1c65SPeter Zijlstra 40569aad6f1SArnaldo Carvalho de Melo assert(FD(evsel, nr_cpu, thread_index) >= 0); 40669aad6f1SArnaldo Carvalho de Melo fcntl(FD(evsel, nr_cpu, thread_index), F_SETFL, O_NONBLOCK); 40786470930SIngo Molnar 40886470930SIngo Molnar /* 40986470930SIngo Molnar * First counter acts as the group leader: 41086470930SIngo Molnar */ 41186470930SIngo Molnar if (group && group_fd == -1) 41269aad6f1SArnaldo Carvalho de Melo group_fd = FD(evsel, nr_cpu, thread_index); 41386470930SIngo Molnar 41469aad6f1SArnaldo Carvalho de Melo if (evsel->idx || thread_index) { 41569aad6f1SArnaldo Carvalho de Melo struct perf_evsel *first; 416*361c99a6SArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 41769aad6f1SArnaldo Carvalho de Melo ret = ioctl(FD(evsel, nr_cpu, thread_index), 4180e2e63ddSPeter Zijlstra PERF_EVENT_IOC_SET_OUTPUT, 41969aad6f1SArnaldo Carvalho de Melo FD(first, nr_cpu, 0)); 4200e2e63ddSPeter Zijlstra if (ret) { 4210e2e63ddSPeter Zijlstra error("failed to set output: %d (%s)\n", errno, 4220e2e63ddSPeter Zijlstra strerror(errno)); 4230e2e63ddSPeter Zijlstra exit(-1); 4240e2e63ddSPeter Zijlstra } 4250e2e63ddSPeter Zijlstra } else { 4260e2e63ddSPeter Zijlstra mmap_array[nr_cpu].prev = 0; 4270e2e63ddSPeter Zijlstra mmap_array[nr_cpu].mask = mmap_pages*page_size - 1; 4280e2e63ddSPeter Zijlstra mmap_array[nr_cpu].base = mmap(NULL, (mmap_pages+1)*page_size, 42969aad6f1SArnaldo Carvalho de Melo PROT_READ | PROT_WRITE, MAP_SHARED, FD(evsel, nr_cpu, thread_index), 0); 4300e2e63ddSPeter Zijlstra if (mmap_array[nr_cpu].base == MAP_FAILED) { 4310e2e63ddSPeter Zijlstra error("failed to mmap with %d (%s)\n", errno, strerror(errno)); 4320e2e63ddSPeter Zijlstra exit(-1); 4330e2e63ddSPeter Zijlstra } 4340e2e63ddSPeter Zijlstra 43569aad6f1SArnaldo Carvalho de Melo event_array[nr_poll].fd = FD(evsel, nr_cpu, thread_index); 43686470930SIngo Molnar event_array[nr_poll].events = POLLIN; 43786470930SIngo Molnar nr_poll++; 43886470930SIngo Molnar } 439d1302522SFrederic Weisbecker 440c171b552SLi Zefan if (filter != NULL) { 44169aad6f1SArnaldo Carvalho de Melo ret = ioctl(FD(evsel, nr_cpu, thread_index), 442c171b552SLi Zefan PERF_EVENT_IOC_SET_FILTER, filter); 443c171b552SLi Zefan if (ret) { 444c171b552SLi Zefan error("failed to set filter with %d (%s)\n", errno, 445c171b552SLi Zefan strerror(errno)); 446c171b552SLi Zefan exit(-1); 447c171b552SLi Zefan } 448c171b552SLi Zefan } 44986470930SIngo Molnar } 450a43d3f08SArnaldo Carvalho de Melo 451a43d3f08SArnaldo Carvalho de Melo if (!sample_type) 452a43d3f08SArnaldo Carvalho de Melo sample_type = attr->sample_type; 453d6d901c2SZhang, Yanmin } 45486470930SIngo Molnar 455*361c99a6SArnaldo Carvalho de Melo static void open_counters(struct perf_evlist *evlist, int cpu) 45686470930SIngo Molnar { 45769aad6f1SArnaldo Carvalho de Melo struct perf_evsel *pos; 45886470930SIngo Molnar 45986470930SIngo Molnar group_fd = -1; 46069aad6f1SArnaldo Carvalho de Melo 461*361c99a6SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) 462*361c99a6SArnaldo Carvalho de Melo create_counter(evlist, pos, cpu); 46386470930SIngo Molnar 46486470930SIngo Molnar nr_cpu++; 46586470930SIngo Molnar } 46686470930SIngo Molnar 4676122e4e4SArnaldo Carvalho de Melo static int process_buildids(void) 4686122e4e4SArnaldo Carvalho de Melo { 4696122e4e4SArnaldo Carvalho de Melo u64 size = lseek(output, 0, SEEK_CUR); 4706122e4e4SArnaldo Carvalho de Melo 4719f591fd7SArnaldo Carvalho de Melo if (size == 0) 4729f591fd7SArnaldo Carvalho de Melo return 0; 4739f591fd7SArnaldo Carvalho de Melo 4746122e4e4SArnaldo Carvalho de Melo session->fd = output; 4756122e4e4SArnaldo Carvalho de Melo return __perf_session__process_events(session, post_processing_offset, 4766122e4e4SArnaldo Carvalho de Melo size - post_processing_offset, 4776122e4e4SArnaldo Carvalho de Melo size, &build_id__mark_dso_hit_ops); 4786122e4e4SArnaldo Carvalho de Melo } 4796122e4e4SArnaldo Carvalho de Melo 480f5970550SPeter Zijlstra static void atexit_header(void) 481f5970550SPeter Zijlstra { 482c7929e47STom Zanussi if (!pipe_output) { 48394c744b6SArnaldo Carvalho de Melo session->header.data_size += bytes_written; 484f5970550SPeter Zijlstra 485baa2f6ceSArnaldo Carvalho de Melo if (!no_buildid) 4866122e4e4SArnaldo Carvalho de Melo process_buildids(); 487*361c99a6SArnaldo Carvalho de Melo perf_header__write(&session->header, evsel_list, output, true); 48839d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 489*361c99a6SArnaldo Carvalho de Melo perf_evlist__delete(evsel_list); 490d65a458bSArnaldo Carvalho de Melo symbol__exit(); 491c7929e47STom Zanussi } 492f5970550SPeter Zijlstra } 493f5970550SPeter Zijlstra 49423346f21SArnaldo Carvalho de Melo static void event__synthesize_guest_os(struct machine *machine, void *data) 495a1645ce1SZhang, Yanmin { 496a1645ce1SZhang, Yanmin int err; 49723346f21SArnaldo Carvalho de Melo struct perf_session *psession = data; 498a1645ce1SZhang, Yanmin 49923346f21SArnaldo Carvalho de Melo if (machine__is_host(machine)) 500a1645ce1SZhang, Yanmin return; 501a1645ce1SZhang, Yanmin 502a1645ce1SZhang, Yanmin /* 503a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 504a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 505a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 506a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 507a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 508a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 509a1645ce1SZhang, Yanmin */ 510a1645ce1SZhang, Yanmin err = event__synthesize_modules(process_synthesized_event, 51123346f21SArnaldo Carvalho de Melo psession, machine); 512a1645ce1SZhang, Yanmin if (err < 0) 513a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 51423346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 515a1645ce1SZhang, Yanmin 516a1645ce1SZhang, Yanmin /* 517a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 518a1645ce1SZhang, Yanmin * have no _text sometimes. 519a1645ce1SZhang, Yanmin */ 520a1645ce1SZhang, Yanmin err = event__synthesize_kernel_mmap(process_synthesized_event, 52123346f21SArnaldo Carvalho de Melo psession, machine, "_text"); 522a1645ce1SZhang, Yanmin if (err < 0) 523a1645ce1SZhang, Yanmin err = event__synthesize_kernel_mmap(process_synthesized_event, 52423346f21SArnaldo Carvalho de Melo psession, machine, "_stext"); 525a1645ce1SZhang, Yanmin if (err < 0) 526a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 52723346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 528a1645ce1SZhang, Yanmin } 529a1645ce1SZhang, Yanmin 53098402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 53198402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 53298402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 53398402807SFrederic Weisbecker }; 53498402807SFrederic Weisbecker 53598402807SFrederic Weisbecker static void mmap_read_all(void) 53698402807SFrederic Weisbecker { 5370e2e63ddSPeter Zijlstra int i; 53898402807SFrederic Weisbecker 53998402807SFrederic Weisbecker for (i = 0; i < nr_cpu; i++) { 5400e2e63ddSPeter Zijlstra if (mmap_array[i].base) 5410e2e63ddSPeter Zijlstra mmap_read(&mmap_array[i]); 54298402807SFrederic Weisbecker } 54398402807SFrederic Weisbecker 54498402807SFrederic Weisbecker if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO)) 54598402807SFrederic Weisbecker write_output(&finished_round_event, sizeof(finished_round_event)); 54698402807SFrederic Weisbecker } 54798402807SFrederic Weisbecker 548d4db3f16SArnaldo Carvalho de Melo static int __cmd_record(int argc, const char **argv) 54986470930SIngo Molnar { 55069aad6f1SArnaldo Carvalho de Melo int i; 55186470930SIngo Molnar struct stat st; 55286470930SIngo Molnar int flags; 5534dc0a04bSArnaldo Carvalho de Melo int err; 5548b412664SPeter Zijlstra unsigned long waking = 0; 555856e9660SPeter Zijlstra int child_ready_pipe[2], go_pipe[2]; 55646be604bSZhang, Yanmin const bool forks = argc > 0; 557856e9660SPeter Zijlstra char buf; 55823346f21SArnaldo Carvalho de Melo struct machine *machine; 55986470930SIngo Molnar 56086470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 56186470930SIngo Molnar 562f5970550SPeter Zijlstra atexit(sig_atexit); 563f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 564f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 56518483b81SArnaldo Carvalho de Melo signal(SIGUSR1, sig_handler); 566f5970550SPeter Zijlstra 567d4db3f16SArnaldo Carvalho de Melo if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { 568856e9660SPeter Zijlstra perror("failed to create pipes"); 569856e9660SPeter Zijlstra exit(-1); 570856e9660SPeter Zijlstra } 571856e9660SPeter Zijlstra 572529870e3STom Zanussi if (!strcmp(output_name, "-")) 573529870e3STom Zanussi pipe_output = 1; 574529870e3STom Zanussi else if (!stat(output_name, &st) && st.st_size) { 5757865e817SFrederic Weisbecker if (write_mode == WRITE_FORCE) { 576b38d3464SArnaldo Carvalho de Melo char oldname[PATH_MAX]; 577b38d3464SArnaldo Carvalho de Melo snprintf(oldname, sizeof(oldname), "%s.old", 578b38d3464SArnaldo Carvalho de Melo output_name); 579b38d3464SArnaldo Carvalho de Melo unlink(oldname); 580b38d3464SArnaldo Carvalho de Melo rename(output_name, oldname); 581b38d3464SArnaldo Carvalho de Melo } 5827865e817SFrederic Weisbecker } else if (write_mode == WRITE_APPEND) { 5837865e817SFrederic Weisbecker write_mode = WRITE_FORCE; 584266e0e21SPierre Habouzit } 58586470930SIngo Molnar 586f887f301SXiao Guangrong flags = O_CREAT|O_RDWR; 5877865e817SFrederic Weisbecker if (write_mode == WRITE_APPEND) 588f5970550SPeter Zijlstra file_new = 0; 58986470930SIngo Molnar else 59086470930SIngo Molnar flags |= O_TRUNC; 59186470930SIngo Molnar 592529870e3STom Zanussi if (pipe_output) 593529870e3STom Zanussi output = STDOUT_FILENO; 594529870e3STom Zanussi else 59586470930SIngo Molnar output = open(output_name, flags, S_IRUSR | S_IWUSR); 59686470930SIngo Molnar if (output < 0) { 59786470930SIngo Molnar perror("failed to create output file"); 59886470930SIngo Molnar exit(-1); 59986470930SIngo Molnar } 60086470930SIngo Molnar 6017865e817SFrederic Weisbecker session = perf_session__new(output_name, O_WRONLY, 60221ef97f0SIan Munsie write_mode == WRITE_FORCE, false, NULL); 60394c744b6SArnaldo Carvalho de Melo if (session == NULL) { 604a9a70bbcSArnaldo Carvalho de Melo pr_err("Not enough memory for reading perf file header\n"); 605a9a70bbcSArnaldo Carvalho de Melo return -1; 606a9a70bbcSArnaldo Carvalho de Melo } 607a9a70bbcSArnaldo Carvalho de Melo 608baa2f6ceSArnaldo Carvalho de Melo if (!no_buildid) 609baa2f6ceSArnaldo Carvalho de Melo perf_header__set_feat(&session->header, HEADER_BUILD_ID); 610baa2f6ceSArnaldo Carvalho de Melo 6114dc0a04bSArnaldo Carvalho de Melo if (!file_new) { 6128dc58101STom Zanussi err = perf_header__read(session, output); 6134dc0a04bSArnaldo Carvalho de Melo if (err < 0) 61439d17dacSArnaldo Carvalho de Melo goto out_delete_session; 6154dc0a04bSArnaldo Carvalho de Melo } 6164dc0a04bSArnaldo Carvalho de Melo 617*361c99a6SArnaldo Carvalho de Melo if (have_tracepoints(&evsel_list->entries)) 61894c744b6SArnaldo Carvalho de Melo perf_header__set_feat(&session->header, HEADER_TRACE_INFO); 61903456a15SFrederic Weisbecker 62039d17dacSArnaldo Carvalho de Melo /* 62139d17dacSArnaldo Carvalho de Melo * perf_session__delete(session) will be called at atexit_header() 62239d17dacSArnaldo Carvalho de Melo */ 623f5970550SPeter Zijlstra atexit(atexit_header); 624f5970550SPeter Zijlstra 625d4db3f16SArnaldo Carvalho de Melo if (forks) { 62646be604bSZhang, Yanmin child_pid = fork(); 6272fb750e8SBorislav Petkov if (child_pid < 0) { 628856e9660SPeter Zijlstra perror("failed to fork"); 629856e9660SPeter Zijlstra exit(-1); 630856e9660SPeter Zijlstra } 6317c6a1c65SPeter Zijlstra 63246be604bSZhang, Yanmin if (!child_pid) { 633529870e3STom Zanussi if (pipe_output) 634529870e3STom Zanussi dup2(2, 1); 635856e9660SPeter Zijlstra close(child_ready_pipe[0]); 636856e9660SPeter Zijlstra close(go_pipe[1]); 637856e9660SPeter Zijlstra fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 638856e9660SPeter Zijlstra 639856e9660SPeter Zijlstra /* 640856e9660SPeter Zijlstra * Do a dummy execvp to get the PLT entry resolved, 641856e9660SPeter Zijlstra * so we avoid the resolver overhead on the real 642856e9660SPeter Zijlstra * execvp call. 643856e9660SPeter Zijlstra */ 644856e9660SPeter Zijlstra execvp("", (char **)argv); 645856e9660SPeter Zijlstra 646856e9660SPeter Zijlstra /* 647856e9660SPeter Zijlstra * Tell the parent we're ready to go 648856e9660SPeter Zijlstra */ 649856e9660SPeter Zijlstra close(child_ready_pipe[1]); 650856e9660SPeter Zijlstra 651856e9660SPeter Zijlstra /* 652856e9660SPeter Zijlstra * Wait until the parent tells us to go. 653856e9660SPeter Zijlstra */ 654856e9660SPeter Zijlstra if (read(go_pipe[0], &buf, 1) == -1) 655856e9660SPeter Zijlstra perror("unable to read pipe"); 656856e9660SPeter Zijlstra 657856e9660SPeter Zijlstra execvp(argv[0], (char **)argv); 658856e9660SPeter Zijlstra 659856e9660SPeter Zijlstra perror(argv[0]); 66018483b81SArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 661856e9660SPeter Zijlstra exit(-1); 662856e9660SPeter Zijlstra } 663856e9660SPeter Zijlstra 664d6d901c2SZhang, Yanmin if (!system_wide && target_tid == -1 && target_pid == -1) 6655c98d466SArnaldo Carvalho de Melo threads->map[0] = child_pid; 666d6d901c2SZhang, Yanmin 667856e9660SPeter Zijlstra close(child_ready_pipe[1]); 668856e9660SPeter Zijlstra close(go_pipe[0]); 669856e9660SPeter Zijlstra /* 670856e9660SPeter Zijlstra * wait for child to settle 671856e9660SPeter Zijlstra */ 672856e9660SPeter Zijlstra if (read(child_ready_pipe[0], &buf, 1) == -1) { 673856e9660SPeter Zijlstra perror("unable to read pipe"); 674856e9660SPeter Zijlstra exit(-1); 675856e9660SPeter Zijlstra } 676856e9660SPeter Zijlstra close(child_ready_pipe[0]); 677856e9660SPeter Zijlstra } 678856e9660SPeter Zijlstra 679c45c6ea2SStephane Eranian if (!system_wide && no_inherit && !cpu_list) { 680*361c99a6SArnaldo Carvalho de Melo open_counters(evsel_list, -1); 6810a5ac846SJens Axboe } else { 68260d567e2SArnaldo Carvalho de Melo for (i = 0; i < cpus->nr; i++) 683*361c99a6SArnaldo Carvalho de Melo open_counters(evsel_list, cpus->map[i]); 6840a5ac846SJens Axboe } 68586470930SIngo Molnar 686640c03ceSArnaldo Carvalho de Melo perf_session__set_sample_type(session, sample_type); 687640c03ceSArnaldo Carvalho de Melo 688529870e3STom Zanussi if (pipe_output) { 689529870e3STom Zanussi err = perf_header__write_pipe(output); 690529870e3STom Zanussi if (err < 0) 691529870e3STom Zanussi return err; 692529870e3STom Zanussi } else if (file_new) { 693*361c99a6SArnaldo Carvalho de Melo err = perf_header__write(&session->header, evsel_list, 694*361c99a6SArnaldo Carvalho de Melo output, false); 695d5eed904SArnaldo Carvalho de Melo if (err < 0) 696d5eed904SArnaldo Carvalho de Melo return err; 697d5eed904SArnaldo Carvalho de Melo } 6987c6a1c65SPeter Zijlstra 6996122e4e4SArnaldo Carvalho de Melo post_processing_offset = lseek(output, 0, SEEK_CUR); 7006122e4e4SArnaldo Carvalho de Melo 7019c90a61cSArnaldo Carvalho de Melo perf_session__set_sample_id_all(session, sample_id_all_avail); 7029c90a61cSArnaldo Carvalho de Melo 7032c46dbb5STom Zanussi if (pipe_output) { 7042c46dbb5STom Zanussi err = event__synthesize_attrs(&session->header, 7052c46dbb5STom Zanussi process_synthesized_event, 7062c46dbb5STom Zanussi session); 7072c46dbb5STom Zanussi if (err < 0) { 7082c46dbb5STom Zanussi pr_err("Couldn't synthesize attrs.\n"); 7092c46dbb5STom Zanussi return err; 7102c46dbb5STom Zanussi } 711cd19a035STom Zanussi 712cd19a035STom Zanussi err = event__synthesize_event_types(process_synthesized_event, 713cd19a035STom Zanussi session); 714cd19a035STom Zanussi if (err < 0) { 715cd19a035STom Zanussi pr_err("Couldn't synthesize event_types.\n"); 716cd19a035STom Zanussi return err; 717cd19a035STom Zanussi } 7189215545eSTom Zanussi 719*361c99a6SArnaldo Carvalho de Melo if (have_tracepoints(&evsel_list->entries)) { 72063e0c771STom Zanussi /* 72163e0c771STom Zanussi * FIXME err <= 0 here actually means that 72263e0c771STom Zanussi * there were no tracepoints so its not really 72363e0c771STom Zanussi * an error, just that we don't need to 72463e0c771STom Zanussi * synthesize anything. We really have to 72563e0c771STom Zanussi * return this more properly and also 72663e0c771STom Zanussi * propagate errors that now are calling die() 72763e0c771STom Zanussi */ 728*361c99a6SArnaldo Carvalho de Melo err = event__synthesize_tracing_data(output, evsel_list, 7299215545eSTom Zanussi process_synthesized_event, 7309215545eSTom Zanussi session); 73163e0c771STom Zanussi if (err <= 0) { 73263e0c771STom Zanussi pr_err("Couldn't record tracing data.\n"); 73363e0c771STom Zanussi return err; 73463e0c771STom Zanussi } 7359215545eSTom Zanussi advance_output(err); 7362c46dbb5STom Zanussi } 73763e0c771STom Zanussi } 7382c46dbb5STom Zanussi 73923346f21SArnaldo Carvalho de Melo machine = perf_session__find_host_machine(session); 74023346f21SArnaldo Carvalho de Melo if (!machine) { 741a1645ce1SZhang, Yanmin pr_err("Couldn't find native kernel information.\n"); 742a1645ce1SZhang, Yanmin return -1; 743a1645ce1SZhang, Yanmin } 744a1645ce1SZhang, Yanmin 74556b03f3cSArnaldo Carvalho de Melo err = event__synthesize_kernel_mmap(process_synthesized_event, 74623346f21SArnaldo Carvalho de Melo session, machine, "_text"); 74770162138SArnaldo Carvalho de Melo if (err < 0) 74870162138SArnaldo Carvalho de Melo err = event__synthesize_kernel_mmap(process_synthesized_event, 74923346f21SArnaldo Carvalho de Melo session, machine, "_stext"); 750c1a3a4b9SArnaldo Carvalho de Melo if (err < 0) 751c1a3a4b9SArnaldo Carvalho de Melo pr_err("Couldn't record kernel reference relocation symbol\n" 752c1a3a4b9SArnaldo Carvalho de Melo "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 753c1a3a4b9SArnaldo Carvalho de Melo "Check /proc/kallsyms permission or run as root.\n"); 75456b03f3cSArnaldo Carvalho de Melo 755a1645ce1SZhang, Yanmin err = event__synthesize_modules(process_synthesized_event, 75623346f21SArnaldo Carvalho de Melo session, machine); 757c1a3a4b9SArnaldo Carvalho de Melo if (err < 0) 758c1a3a4b9SArnaldo Carvalho de Melo pr_err("Couldn't record kernel module information.\n" 759c1a3a4b9SArnaldo Carvalho de Melo "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 760c1a3a4b9SArnaldo Carvalho de Melo "Check /proc/modules permission or run as root.\n"); 761c1a3a4b9SArnaldo Carvalho de Melo 762a1645ce1SZhang, Yanmin if (perf_guest) 76323346f21SArnaldo Carvalho de Melo perf_session__process_machines(session, event__synthesize_guest_os); 764b7cece76SArnaldo Carvalho de Melo 765cf103a14SStephane Eranian if (!system_wide) 766d6d901c2SZhang, Yanmin event__synthesize_thread(target_tid, process_synthesized_event, 767d8f66248SArnaldo Carvalho de Melo session); 768234fbbf5SArnaldo Carvalho de Melo else 769d8f66248SArnaldo Carvalho de Melo event__synthesize_threads(process_synthesized_event, session); 7707c6a1c65SPeter Zijlstra 77186470930SIngo Molnar if (realtime_prio) { 77286470930SIngo Molnar struct sched_param param; 77386470930SIngo Molnar 77486470930SIngo Molnar param.sched_priority = realtime_prio; 77586470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 7766beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 77786470930SIngo Molnar exit(-1); 77886470930SIngo Molnar } 77986470930SIngo Molnar } 78086470930SIngo Molnar 781856e9660SPeter Zijlstra /* 782856e9660SPeter Zijlstra * Let the child rip 783856e9660SPeter Zijlstra */ 784d4db3f16SArnaldo Carvalho de Melo if (forks) 785856e9660SPeter Zijlstra close(go_pipe[1]); 786856e9660SPeter Zijlstra 787649c48a9SPeter Zijlstra for (;;) { 78886470930SIngo Molnar int hits = samples; 789d6d901c2SZhang, Yanmin int thread; 79086470930SIngo Molnar 79198402807SFrederic Weisbecker mmap_read_all(); 79286470930SIngo Molnar 793649c48a9SPeter Zijlstra if (hits == samples) { 794649c48a9SPeter Zijlstra if (done) 795649c48a9SPeter Zijlstra break; 7964dc0a04bSArnaldo Carvalho de Melo err = poll(event_array, nr_poll, -1); 7978b412664SPeter Zijlstra waking++; 7988b412664SPeter Zijlstra } 7998b412664SPeter Zijlstra 8008b412664SPeter Zijlstra if (done) { 8018b412664SPeter Zijlstra for (i = 0; i < nr_cpu; i++) { 80269aad6f1SArnaldo Carvalho de Melo struct perf_evsel *pos; 80369aad6f1SArnaldo Carvalho de Melo 804*361c99a6SArnaldo Carvalho de Melo list_for_each_entry(pos, &evsel_list->entries, node) { 805d6d901c2SZhang, Yanmin for (thread = 0; 8065c98d466SArnaldo Carvalho de Melo thread < threads->nr; 807d6d901c2SZhang, Yanmin thread++) 80869aad6f1SArnaldo Carvalho de Melo ioctl(FD(pos, i, thread), 809d6d901c2SZhang, Yanmin PERF_EVENT_IOC_DISABLE); 810d6d901c2SZhang, Yanmin } 81186470930SIngo Molnar } 812649c48a9SPeter Zijlstra } 8138b412664SPeter Zijlstra } 8148b412664SPeter Zijlstra 81518483b81SArnaldo Carvalho de Melo if (quiet || signr == SIGUSR1) 816b44308f5SArnaldo Carvalho de Melo return 0; 817b44308f5SArnaldo Carvalho de Melo 8188b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 81986470930SIngo Molnar 82086470930SIngo Molnar /* 82186470930SIngo Molnar * Approximate RIP event size: 24 bytes. 82286470930SIngo Molnar */ 82386470930SIngo Molnar fprintf(stderr, 8249486aa38SArnaldo Carvalho de Melo "[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n", 82586470930SIngo Molnar (double)bytes_written / 1024.0 / 1024.0, 82686470930SIngo Molnar output_name, 82786470930SIngo Molnar bytes_written / 24); 82886470930SIngo Molnar 82986470930SIngo Molnar return 0; 83039d17dacSArnaldo Carvalho de Melo 83139d17dacSArnaldo Carvalho de Melo out_delete_session: 83239d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 83339d17dacSArnaldo Carvalho de Melo return err; 83486470930SIngo Molnar } 83586470930SIngo Molnar 83686470930SIngo Molnar static const char * const record_usage[] = { 83786470930SIngo Molnar "perf record [<options>] [<command>]", 83886470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 83986470930SIngo Molnar NULL 84086470930SIngo Molnar }; 84186470930SIngo Molnar 8427865e817SFrederic Weisbecker static bool force, append_file; 8437865e817SFrederic Weisbecker 844bca647aaSTom Zanussi const struct option record_options[] = { 845*361c99a6SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &evsel_list, "event", 84686470930SIngo Molnar "event selector. use 'perf list' to list available events", 84786470930SIngo Molnar parse_events), 848*361c99a6SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &evsel_list, "filter", 849c171b552SLi Zefan "event filter", parse_filter), 85086470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 851d6d901c2SZhang, Yanmin "record events on existing process id"), 852d6d901c2SZhang, Yanmin OPT_INTEGER('t', "tid", &target_tid, 853d6d901c2SZhang, Yanmin "record events on existing thread id"), 85486470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 85586470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 856acac03faSKirill Smelkov OPT_BOOLEAN('D', "no-delay", &nodelay, 857acac03faSKirill Smelkov "collect data without buffering"), 858daac07b2SFrederic Weisbecker OPT_BOOLEAN('R', "raw-samples", &raw_samples, 859daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 86086470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 86186470930SIngo Molnar "system-wide collection from all CPUs"), 86286470930SIngo Molnar OPT_BOOLEAN('A', "append", &append_file, 86386470930SIngo Molnar "append to the output file to do incremental profiling"), 864c45c6ea2SStephane Eranian OPT_STRING('C', "cpu", &cpu_list, "cpu", 865c45c6ea2SStephane Eranian "list of cpus to monitor"), 86686470930SIngo Molnar OPT_BOOLEAN('f', "force", &force, 8677865e817SFrederic Weisbecker "overwrite existing data file (deprecated)"), 8683de29cabSStephane Eranian OPT_U64('c', "count", &user_interval, "event period to sample"), 86986470930SIngo Molnar OPT_STRING('o', "output", &output_name, "file", 87086470930SIngo Molnar "output file name"), 8712e6cdf99SStephane Eranian OPT_BOOLEAN('i', "no-inherit", &no_inherit, 8722e6cdf99SStephane Eranian "child tasks do not inherit counters"), 8731967936dSArnaldo Carvalho de Melo OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"), 8741967936dSArnaldo Carvalho de Melo OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"), 8753efa1cc9SIngo Molnar OPT_BOOLEAN('g', "call-graph", &call_graph, 8763efa1cc9SIngo Molnar "do call-graph (stack chain/backtrace) recording"), 877c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 8783da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 879b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 880649c48a9SPeter Zijlstra OPT_BOOLEAN('s', "stat", &inherit_stat, 881649c48a9SPeter Zijlstra "per thread counts"), 8824bba828dSAnton Blanchard OPT_BOOLEAN('d', "data", &sample_address, 8834bba828dSAnton Blanchard "Sample addresses"), 8849c90a61cSArnaldo Carvalho de Melo OPT_BOOLEAN('T', "timestamp", &sample_time, "Sample timestamps"), 885649c48a9SPeter Zijlstra OPT_BOOLEAN('n', "no-samples", &no_samples, 886649c48a9SPeter Zijlstra "don't sample"), 887baa2f6ceSArnaldo Carvalho de Melo OPT_BOOLEAN('N', "no-buildid-cache", &no_buildid_cache, 888a1ac1d3cSStephane Eranian "do not update the buildid cache"), 889baa2f6ceSArnaldo Carvalho de Melo OPT_BOOLEAN('B', "no-buildid", &no_buildid, 890baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 89186470930SIngo Molnar OPT_END() 89286470930SIngo Molnar }; 89386470930SIngo Molnar 894f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used) 89586470930SIngo Molnar { 89669aad6f1SArnaldo Carvalho de Melo int err = -ENOMEM; 89769aad6f1SArnaldo Carvalho de Melo struct perf_evsel *pos; 89886470930SIngo Molnar 899*361c99a6SArnaldo Carvalho de Melo evsel_list = perf_evlist__new(); 900*361c99a6SArnaldo Carvalho de Melo if (evsel_list == NULL) 901*361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 902*361c99a6SArnaldo Carvalho de Melo 903bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 904a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 905d6d901c2SZhang, Yanmin if (!argc && target_pid == -1 && target_tid == -1 && 906c45c6ea2SStephane Eranian !system_wide && !cpu_list) 907bca647aaSTom Zanussi usage_with_options(record_usage, record_options); 90886470930SIngo Molnar 9097865e817SFrederic Weisbecker if (force && append_file) { 9107865e817SFrederic Weisbecker fprintf(stderr, "Can't overwrite and append at the same time." 9117865e817SFrederic Weisbecker " You need to choose between -f and -A"); 912bca647aaSTom Zanussi usage_with_options(record_usage, record_options); 9137865e817SFrederic Weisbecker } else if (append_file) { 9147865e817SFrederic Weisbecker write_mode = WRITE_APPEND; 9157865e817SFrederic Weisbecker } else { 9167865e817SFrederic Weisbecker write_mode = WRITE_FORCE; 9177865e817SFrederic Weisbecker } 9187865e817SFrederic Weisbecker 919655000e7SArnaldo Carvalho de Melo symbol__init(); 920baa2f6ceSArnaldo Carvalho de Melo 921baa2f6ceSArnaldo Carvalho de Melo if (no_buildid_cache || no_buildid) 922a1ac1d3cSStephane Eranian disable_buildid_cache(); 923655000e7SArnaldo Carvalho de Melo 924*361c99a6SArnaldo Carvalho de Melo if (evsel_list->nr_entries == 0 && 925*361c99a6SArnaldo Carvalho de Melo perf_evlist__add_default(evsel_list) < 0) { 92669aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 92769aad6f1SArnaldo Carvalho de Melo goto out_symbol_exit; 928bbd36e5eSPeter Zijlstra } 92986470930SIngo Molnar 9305c98d466SArnaldo Carvalho de Melo if (target_pid != -1) 931d6d901c2SZhang, Yanmin target_tid = target_pid; 932d6d901c2SZhang, Yanmin 9335c98d466SArnaldo Carvalho de Melo threads = thread_map__new(target_pid, target_tid); 9345c98d466SArnaldo Carvalho de Melo if (threads == NULL) { 9355c98d466SArnaldo Carvalho de Melo pr_err("Problems finding threads of monitor\n"); 9365c98d466SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 937d6d901c2SZhang, Yanmin } 938d6d901c2SZhang, Yanmin 93960d567e2SArnaldo Carvalho de Melo cpus = cpu_map__new(cpu_list); 94060d567e2SArnaldo Carvalho de Melo if (cpus == NULL) { 94160d567e2SArnaldo Carvalho de Melo perror("failed to parse CPUs map"); 94269aad6f1SArnaldo Carvalho de Melo return -1; 943d6d901c2SZhang, Yanmin } 94469aad6f1SArnaldo Carvalho de Melo 945*361c99a6SArnaldo Carvalho de Melo list_for_each_entry(pos, &evsel_list->entries, node) { 9465c98d466SArnaldo Carvalho de Melo if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0) 94769aad6f1SArnaldo Carvalho de Melo goto out_free_fd; 948ad7f4e3fSArnaldo Carvalho de Melo if (perf_header__push_event(pos->attr.config, event_name(pos))) 949ad7f4e3fSArnaldo Carvalho de Melo goto out_free_fd; 950d6d901c2SZhang, Yanmin } 9515c98d466SArnaldo Carvalho de Melo event_array = malloc((sizeof(struct pollfd) * MAX_NR_CPUS * 9525c98d466SArnaldo Carvalho de Melo MAX_COUNTERS * threads->nr)); 953d6d901c2SZhang, Yanmin if (!event_array) 95439d17dacSArnaldo Carvalho de Melo goto out_free_fd; 955d6d901c2SZhang, Yanmin 9563de29cabSStephane Eranian if (user_interval != ULLONG_MAX) 957f9212819SFrederic Weisbecker default_interval = user_interval; 958f9212819SFrederic Weisbecker if (user_freq != UINT_MAX) 959f9212819SFrederic Weisbecker freq = user_freq; 960f9212819SFrederic Weisbecker 9617e4ff9e3SMike Galbraith /* 9627e4ff9e3SMike Galbraith * User specified count overrides default frequency. 9637e4ff9e3SMike Galbraith */ 9647e4ff9e3SMike Galbraith if (default_interval) 9657e4ff9e3SMike Galbraith freq = 0; 9667e4ff9e3SMike Galbraith else if (freq) { 9677e4ff9e3SMike Galbraith default_interval = freq; 9687e4ff9e3SMike Galbraith } else { 9697e4ff9e3SMike Galbraith fprintf(stderr, "frequency and count are zero, aborting\n"); 97039d17dacSArnaldo Carvalho de Melo err = -EINVAL; 97139d17dacSArnaldo Carvalho de Melo goto out_free_event_array; 9727e4ff9e3SMike Galbraith } 9737e4ff9e3SMike Galbraith 97439d17dacSArnaldo Carvalho de Melo err = __cmd_record(argc, argv); 97539d17dacSArnaldo Carvalho de Melo 97639d17dacSArnaldo Carvalho de Melo out_free_event_array: 97739d17dacSArnaldo Carvalho de Melo free(event_array); 97839d17dacSArnaldo Carvalho de Melo out_free_fd: 9795c98d466SArnaldo Carvalho de Melo thread_map__delete(threads); 9805c98d466SArnaldo Carvalho de Melo threads = NULL; 981d65a458bSArnaldo Carvalho de Melo out_symbol_exit: 982d65a458bSArnaldo Carvalho de Melo symbol__exit(); 98339d17dacSArnaldo Carvalho de Melo return err; 98486470930SIngo Molnar } 985