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" 21361c99a6SArnaldo 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; 70361c99a6SArnaldo 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 75f5970550SPeter Zijlstra static int file_new = 1; 766122e4e4SArnaldo Carvalho de Melo static off_t post_processing_offset; 777c6a1c65SPeter Zijlstra 7894c744b6SArnaldo Carvalho de Melo static struct perf_session *session; 79c45c6ea2SStephane Eranian static const char *cpu_list; 80f5970550SPeter Zijlstra 81*744bd8aaSArnaldo Carvalho de Melo static struct perf_mmap mmap_array[MAX_NR_CPUS]; 8286470930SIngo Molnar 83*744bd8aaSArnaldo Carvalho de Melo static void mmap_write_tail(struct perf_mmap *md, unsigned long tail) 849d91a6f7SPeter Zijlstra { 85cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 869d91a6f7SPeter Zijlstra 879d91a6f7SPeter Zijlstra /* 889d91a6f7SPeter Zijlstra * ensure all reads are done before we write the tail out. 899d91a6f7SPeter Zijlstra */ 909d91a6f7SPeter Zijlstra /* mb(); */ 919d91a6f7SPeter Zijlstra pc->data_tail = tail; 929d91a6f7SPeter Zijlstra } 939d91a6f7SPeter Zijlstra 949215545eSTom Zanussi static void advance_output(size_t size) 959215545eSTom Zanussi { 969215545eSTom Zanussi bytes_written += size; 979215545eSTom Zanussi } 989215545eSTom Zanussi 99f5970550SPeter Zijlstra static void write_output(void *buf, size_t size) 100f5970550SPeter Zijlstra { 101f5970550SPeter Zijlstra while (size) { 102f5970550SPeter Zijlstra int ret = write(output, buf, size); 103f5970550SPeter Zijlstra 104f5970550SPeter Zijlstra if (ret < 0) 105f5970550SPeter Zijlstra die("failed to write"); 106f5970550SPeter Zijlstra 107f5970550SPeter Zijlstra size -= ret; 108f5970550SPeter Zijlstra buf += ret; 109f5970550SPeter Zijlstra 110f5970550SPeter Zijlstra bytes_written += ret; 111f5970550SPeter Zijlstra } 112f5970550SPeter Zijlstra } 113f5970550SPeter Zijlstra 114d8f66248SArnaldo Carvalho de Melo static int process_synthesized_event(event_t *event, 115640c03ceSArnaldo Carvalho de Melo struct sample_data *sample __used, 116d8f66248SArnaldo Carvalho de Melo struct perf_session *self __used) 117234fbbf5SArnaldo Carvalho de Melo { 1186122e4e4SArnaldo Carvalho de Melo write_output(event, event->header.size); 119234fbbf5SArnaldo Carvalho de Melo return 0; 120234fbbf5SArnaldo Carvalho de Melo } 121234fbbf5SArnaldo Carvalho de Melo 122*744bd8aaSArnaldo Carvalho de Melo static void mmap_read(struct perf_mmap *md) 12386470930SIngo Molnar { 124*744bd8aaSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 12586470930SIngo Molnar unsigned int old = md->prev; 12686470930SIngo Molnar unsigned char *data = md->base + page_size; 12786470930SIngo Molnar unsigned long size; 12886470930SIngo Molnar void *buf; 12986470930SIngo Molnar int diff; 13086470930SIngo Molnar 13186470930SIngo Molnar /* 13286470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 13386470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 13486470930SIngo Molnar * 13586470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 13686470930SIngo Molnar * 13786470930SIngo Molnar * In either case, truncate and restart at head. 13886470930SIngo Molnar */ 13986470930SIngo Molnar diff = head - old; 1409d91a6f7SPeter Zijlstra if (diff < 0) { 141ef365cefSRuss Anderson fprintf(stderr, "WARNING: failed to keep up with mmap data\n"); 14286470930SIngo Molnar /* 14386470930SIngo Molnar * head points to a known good entry, start there. 14486470930SIngo Molnar */ 14586470930SIngo Molnar old = head; 14686470930SIngo Molnar } 14786470930SIngo Molnar 14886470930SIngo Molnar if (old != head) 14986470930SIngo Molnar samples++; 15086470930SIngo Molnar 15186470930SIngo Molnar size = head - old; 15286470930SIngo Molnar 15386470930SIngo Molnar if ((old & md->mask) + size != (head & md->mask)) { 15486470930SIngo Molnar buf = &data[old & md->mask]; 15586470930SIngo Molnar size = md->mask + 1 - (old & md->mask); 15686470930SIngo Molnar old += size; 15786470930SIngo Molnar 1586122e4e4SArnaldo Carvalho de Melo write_output(buf, size); 15986470930SIngo Molnar } 16086470930SIngo Molnar 16186470930SIngo Molnar buf = &data[old & md->mask]; 16286470930SIngo Molnar size = head - old; 16386470930SIngo Molnar old += size; 16486470930SIngo Molnar 1656122e4e4SArnaldo Carvalho de Melo write_output(buf, size); 16686470930SIngo Molnar 16786470930SIngo Molnar md->prev = old; 1689d91a6f7SPeter Zijlstra mmap_write_tail(md, old); 16986470930SIngo Molnar } 17086470930SIngo Molnar 17186470930SIngo Molnar static volatile int done = 0; 172f7b7c26eSPeter Zijlstra static volatile int signr = -1; 17386470930SIngo Molnar 17486470930SIngo Molnar static void sig_handler(int sig) 17586470930SIngo Molnar { 17686470930SIngo Molnar done = 1; 177f7b7c26eSPeter Zijlstra signr = sig; 178f7b7c26eSPeter Zijlstra } 179f7b7c26eSPeter Zijlstra 180f7b7c26eSPeter Zijlstra static void sig_atexit(void) 181f7b7c26eSPeter Zijlstra { 1825ffc8881SIan Munsie if (child_pid > 0) 183933da83aSChris Wilson kill(child_pid, SIGTERM); 184933da83aSChris Wilson 18518483b81SArnaldo Carvalho de Melo if (signr == -1 || signr == SIGUSR1) 186f7b7c26eSPeter Zijlstra return; 187f7b7c26eSPeter Zijlstra 188f7b7c26eSPeter Zijlstra signal(signr, SIG_DFL); 189f7b7c26eSPeter Zijlstra kill(getpid(), signr); 19086470930SIngo Molnar } 19186470930SIngo Molnar 192cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) 1937c6a1c65SPeter Zijlstra { 1947c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 1957c6a1c65SPeter Zijlstra 19694c744b6SArnaldo Carvalho de Melo if (nr < session->header.attrs) { 19794c744b6SArnaldo Carvalho de Melo h_attr = session->header.attr[nr]; 1987c6a1c65SPeter Zijlstra } else { 1997c6a1c65SPeter Zijlstra h_attr = perf_header_attr__new(a); 200dc79c0fcSArnaldo Carvalho de Melo if (h_attr != NULL) 20194c744b6SArnaldo Carvalho de Melo if (perf_header__add_attr(&session->header, h_attr) < 0) { 20211deb1f9SArnaldo Carvalho de Melo perf_header_attr__delete(h_attr); 20311deb1f9SArnaldo Carvalho de Melo h_attr = NULL; 20411deb1f9SArnaldo Carvalho de Melo } 2057c6a1c65SPeter Zijlstra } 2067c6a1c65SPeter Zijlstra 2077c6a1c65SPeter Zijlstra return h_attr; 2087c6a1c65SPeter Zijlstra } 2097c6a1c65SPeter Zijlstra 210361c99a6SArnaldo Carvalho de Melo static void create_counter(struct perf_evlist *evlist, 211361c99a6SArnaldo Carvalho de Melo struct perf_evsel *evsel, int cpu) 21286470930SIngo Molnar { 21369aad6f1SArnaldo Carvalho de Melo char *filter = evsel->filter; 21469aad6f1SArnaldo Carvalho de Melo struct perf_event_attr *attr = &evsel->attr; 2157c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 216d6d901c2SZhang, Yanmin int thread_index; 217c171b552SLi Zefan int ret; 2187c6a1c65SPeter Zijlstra struct { 2197c6a1c65SPeter Zijlstra u64 count; 2207c6a1c65SPeter Zijlstra u64 time_enabled; 2217c6a1c65SPeter Zijlstra u64 time_running; 2227c6a1c65SPeter Zijlstra u64 id; 2237c6a1c65SPeter Zijlstra } read_data; 224dd7927f4SArnaldo Carvalho de Melo 225dd7927f4SArnaldo Carvalho de Melo for (thread_index = 0; thread_index < threads->nr; thread_index++) { 226dd7927f4SArnaldo Carvalho de Melo h_attr = get_header_attr(attr, evsel->idx); 227dd7927f4SArnaldo Carvalho de Melo if (h_attr == NULL) 228dd7927f4SArnaldo Carvalho de Melo die("nomem\n"); 229dd7927f4SArnaldo Carvalho de Melo 230dd7927f4SArnaldo Carvalho de Melo if (!file_new) { 231dd7927f4SArnaldo Carvalho de Melo if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { 232dd7927f4SArnaldo Carvalho de Melo fprintf(stderr, "incompatible append\n"); 233dd7927f4SArnaldo Carvalho de Melo exit(-1); 234dd7927f4SArnaldo Carvalho de Melo } 235dd7927f4SArnaldo Carvalho de Melo } 236dd7927f4SArnaldo Carvalho de Melo 237dd7927f4SArnaldo Carvalho de Melo if (read(FD(evsel, cpu, thread_index), &read_data, sizeof(read_data)) == -1) { 238dd7927f4SArnaldo Carvalho de Melo perror("Unable to read perf file descriptor"); 239dd7927f4SArnaldo Carvalho de Melo exit(-1); 240dd7927f4SArnaldo Carvalho de Melo } 241dd7927f4SArnaldo Carvalho de Melo 242dd7927f4SArnaldo Carvalho de Melo if (perf_header_attr__add_id(h_attr, read_data.id) < 0) { 243dd7927f4SArnaldo Carvalho de Melo pr_warning("Not enough memory to add id\n"); 244dd7927f4SArnaldo Carvalho de Melo exit(-1); 245dd7927f4SArnaldo Carvalho de Melo } 246dd7927f4SArnaldo Carvalho de Melo 247dd7927f4SArnaldo Carvalho de Melo assert(FD(evsel, cpu, thread_index) >= 0); 248dd7927f4SArnaldo Carvalho de Melo fcntl(FD(evsel, cpu, thread_index), F_SETFL, O_NONBLOCK); 249dd7927f4SArnaldo Carvalho de Melo 250dd7927f4SArnaldo Carvalho de Melo if (evsel->idx || thread_index) { 251dd7927f4SArnaldo Carvalho de Melo struct perf_evsel *first; 252dd7927f4SArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 253dd7927f4SArnaldo Carvalho de Melo ret = ioctl(FD(evsel, cpu, thread_index), 254dd7927f4SArnaldo Carvalho de Melo PERF_EVENT_IOC_SET_OUTPUT, 255dd7927f4SArnaldo Carvalho de Melo FD(first, cpu, 0)); 256dd7927f4SArnaldo Carvalho de Melo if (ret) { 257dd7927f4SArnaldo Carvalho de Melo error("failed to set output: %d (%s)\n", errno, 258dd7927f4SArnaldo Carvalho de Melo strerror(errno)); 259dd7927f4SArnaldo Carvalho de Melo exit(-1); 260dd7927f4SArnaldo Carvalho de Melo } 261dd7927f4SArnaldo Carvalho de Melo } else { 262dd7927f4SArnaldo Carvalho de Melo mmap_array[cpu].prev = 0; 263dd7927f4SArnaldo Carvalho de Melo mmap_array[cpu].mask = mmap_pages*page_size - 1; 264dd7927f4SArnaldo Carvalho de Melo mmap_array[cpu].base = mmap(NULL, (mmap_pages+1)*page_size, 265dd7927f4SArnaldo Carvalho de Melo PROT_READ | PROT_WRITE, MAP_SHARED, FD(evsel, cpu, thread_index), 0); 266dd7927f4SArnaldo Carvalho de Melo if (mmap_array[cpu].base == MAP_FAILED) { 267dd7927f4SArnaldo Carvalho de Melo error("failed to mmap with %d (%s)\n", errno, strerror(errno)); 268dd7927f4SArnaldo Carvalho de Melo exit(-1); 269dd7927f4SArnaldo Carvalho de Melo } 270dd7927f4SArnaldo Carvalho de Melo 271dd7927f4SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = FD(evsel, cpu, thread_index); 272dd7927f4SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 273dd7927f4SArnaldo Carvalho de Melo evlist->nr_fds++; 274dd7927f4SArnaldo Carvalho de Melo } 275dd7927f4SArnaldo Carvalho de Melo 276dd7927f4SArnaldo Carvalho de Melo if (filter != NULL) { 277dd7927f4SArnaldo Carvalho de Melo ret = ioctl(FD(evsel, cpu, thread_index), 278dd7927f4SArnaldo Carvalho de Melo PERF_EVENT_IOC_SET_FILTER, filter); 279dd7927f4SArnaldo Carvalho de Melo if (ret) { 280dd7927f4SArnaldo Carvalho de Melo error("failed to set filter with %d (%s)\n", errno, 281dd7927f4SArnaldo Carvalho de Melo strerror(errno)); 282dd7927f4SArnaldo Carvalho de Melo exit(-1); 283dd7927f4SArnaldo Carvalho de Melo } 284dd7927f4SArnaldo Carvalho de Melo } 285dd7927f4SArnaldo Carvalho de Melo } 286dd7927f4SArnaldo Carvalho de Melo 287dd7927f4SArnaldo Carvalho de Melo if (!sample_type) 288dd7927f4SArnaldo Carvalho de Melo sample_type = attr->sample_type; 289dd7927f4SArnaldo Carvalho de Melo } 290dd7927f4SArnaldo Carvalho de Melo 291dd7927f4SArnaldo Carvalho de Melo static void config_attr(struct perf_evsel *evsel, struct perf_evlist *evlist) 292dd7927f4SArnaldo Carvalho de Melo { 293dd7927f4SArnaldo Carvalho de Melo struct perf_event_attr *attr = &evsel->attr; 294dd7927f4SArnaldo Carvalho de Melo int track = !evsel->idx; /* only the first counter needs these */ 2957c6a1c65SPeter Zijlstra 2967c6a1c65SPeter Zijlstra attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | 2977c6a1c65SPeter Zijlstra PERF_FORMAT_TOTAL_TIME_RUNNING | 2987c6a1c65SPeter Zijlstra PERF_FORMAT_ID; 29986470930SIngo Molnar 3003a9f131fSFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; 3013efa1cc9SIngo Molnar 302361c99a6SArnaldo Carvalho de Melo if (evlist->nr_entries > 1) 3038907fd60SEric B Munson attr->sample_type |= PERF_SAMPLE_ID; 3048907fd60SEric B Munson 305f9212819SFrederic Weisbecker /* 306f9212819SFrederic Weisbecker * We default some events to a 1 default interval. But keep 307f9212819SFrederic Weisbecker * it a weak assumption overridable by the user. 308f9212819SFrederic Weisbecker */ 309f9212819SFrederic Weisbecker if (!attr->sample_period || (user_freq != UINT_MAX && 3103de29cabSStephane Eranian user_interval != ULLONG_MAX)) { 31186470930SIngo Molnar if (freq) { 312ea1900e5SPeter Zijlstra attr->sample_type |= PERF_SAMPLE_PERIOD; 31386470930SIngo Molnar attr->freq = 1; 31486470930SIngo Molnar attr->sample_freq = freq; 315f9212819SFrederic Weisbecker } else { 316f9212819SFrederic Weisbecker attr->sample_period = default_interval; 317f9212819SFrederic Weisbecker } 31886470930SIngo Molnar } 3193efa1cc9SIngo Molnar 320649c48a9SPeter Zijlstra if (no_samples) 321649c48a9SPeter Zijlstra attr->sample_freq = 0; 322649c48a9SPeter Zijlstra 323649c48a9SPeter Zijlstra if (inherit_stat) 324649c48a9SPeter Zijlstra attr->inherit_stat = 1; 325649c48a9SPeter Zijlstra 3263af9e859SEric B Munson if (sample_address) { 3274bba828dSAnton Blanchard attr->sample_type |= PERF_SAMPLE_ADDR; 3283af9e859SEric B Munson attr->mmap_data = track; 3293af9e859SEric B Munson } 3304bba828dSAnton Blanchard 3313efa1cc9SIngo Molnar if (call_graph) 3323efa1cc9SIngo Molnar attr->sample_type |= PERF_SAMPLE_CALLCHAIN; 3333efa1cc9SIngo Molnar 334f60f3593SArun Sharma if (system_wide) 335f60f3593SArun Sharma attr->sample_type |= PERF_SAMPLE_CPU; 336f60f3593SArun Sharma 337a43d3f08SArnaldo Carvalho de Melo if (sample_id_all_avail && 338a43d3f08SArnaldo Carvalho de Melo (sample_time || system_wide || !no_inherit || cpu_list)) 3399c90a61cSArnaldo Carvalho de Melo attr->sample_type |= PERF_SAMPLE_TIME; 3409c90a61cSArnaldo Carvalho de Melo 341cd6feeeaSIngo Molnar if (raw_samples) { 3426ddf259dSIngo Molnar attr->sample_type |= PERF_SAMPLE_TIME; 343daac07b2SFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_RAW; 344cd6feeeaSIngo Molnar attr->sample_type |= PERF_SAMPLE_CPU; 345cd6feeeaSIngo Molnar } 346f413cdb8SFrederic Weisbecker 347acac03faSKirill Smelkov if (nodelay) { 348acac03faSKirill Smelkov attr->watermark = 0; 349acac03faSKirill Smelkov attr->wakeup_events = 1; 350acac03faSKirill Smelkov } 351acac03faSKirill Smelkov 35286470930SIngo Molnar attr->mmap = track; 35386470930SIngo Molnar attr->comm = track; 354dd7927f4SArnaldo Carvalho de Melo 3552e6cdf99SStephane Eranian if (target_pid == -1 && target_tid == -1 && !system_wide) { 3564502d77cSPeter Zijlstra attr->disabled = 1; 357bedbfdeaSEric B Munson attr->enable_on_exec = 1; 35846be604bSZhang, Yanmin } 359dd7927f4SArnaldo Carvalho de Melo } 360dd7927f4SArnaldo Carvalho de Melo 361dd7927f4SArnaldo Carvalho de Melo static void open_counters(struct perf_evlist *evlist) 362dd7927f4SArnaldo Carvalho de Melo { 363dd7927f4SArnaldo Carvalho de Melo struct perf_evsel *pos; 364dd7927f4SArnaldo Carvalho de Melo int cpu; 365dd7927f4SArnaldo Carvalho de Melo 366dd7927f4SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 367dd7927f4SArnaldo Carvalho de Melo struct perf_event_attr *attr = &pos->attr; 368dd7927f4SArnaldo Carvalho de Melo /* 369dd7927f4SArnaldo Carvalho de Melo * Check if parse_single_tracepoint_event has already asked for 370dd7927f4SArnaldo Carvalho de Melo * PERF_SAMPLE_TIME. 371dd7927f4SArnaldo Carvalho de Melo * 372dd7927f4SArnaldo Carvalho de Melo * XXX this is kludgy but short term fix for problems introduced by 373dd7927f4SArnaldo Carvalho de Melo * eac23d1c that broke 'perf script' by having different sample_types 374dd7927f4SArnaldo Carvalho de Melo * when using multiple tracepoint events when we use a perf binary 375dd7927f4SArnaldo Carvalho de Melo * that tries to use sample_id_all on an older kernel. 376dd7927f4SArnaldo Carvalho de Melo * 377dd7927f4SArnaldo Carvalho de Melo * We need to move counter creation to perf_session, support 378dd7927f4SArnaldo Carvalho de Melo * different sample_types, etc. 379dd7927f4SArnaldo Carvalho de Melo */ 380dd7927f4SArnaldo Carvalho de Melo bool time_needed = attr->sample_type & PERF_SAMPLE_TIME; 381dd7927f4SArnaldo Carvalho de Melo 382dd7927f4SArnaldo Carvalho de Melo config_attr(pos, evlist); 3839c90a61cSArnaldo Carvalho de Melo retry_sample_id: 3849c90a61cSArnaldo Carvalho de Melo attr->sample_id_all = sample_id_all_avail ? 1 : 0; 3853da297a6SIngo Molnar try_again: 386dd7927f4SArnaldo Carvalho de Melo if (perf_evsel__open(pos, cpus, threads, group, !no_inherit) < 0) { 38786470930SIngo Molnar int err = errno; 38886470930SIngo Molnar 389c10edee2SPekka Enberg if (err == EPERM || err == EACCES) 3906230f2c7SArnaldo Carvalho de Melo die("Permission error - are you root?\n" 391d6d901c2SZhang, Yanmin "\t Consider tweaking" 392d6d901c2SZhang, Yanmin " /proc/sys/kernel/perf_event_paranoid.\n"); 393c45c6ea2SStephane Eranian else if (err == ENODEV && cpu_list) { 394d6d901c2SZhang, Yanmin die("No such device - did you specify" 395d6d901c2SZhang, Yanmin " an out-of-range profile CPU?\n"); 3969c90a61cSArnaldo Carvalho de Melo } else if (err == EINVAL && sample_id_all_avail) { 3979c90a61cSArnaldo Carvalho de Melo /* 3989c90a61cSArnaldo Carvalho de Melo * Old kernel, no attr->sample_id_type_all field 3999c90a61cSArnaldo Carvalho de Melo */ 4009c90a61cSArnaldo Carvalho de Melo sample_id_all_avail = false; 401a43d3f08SArnaldo Carvalho de Melo if (!sample_time && !raw_samples && !time_needed) 402eac23d1cSIan Munsie attr->sample_type &= ~PERF_SAMPLE_TIME; 403eac23d1cSIan Munsie 4049c90a61cSArnaldo Carvalho de Melo goto retry_sample_id; 405d6d901c2SZhang, Yanmin } 4063da297a6SIngo Molnar 4073da297a6SIngo Molnar /* 4083da297a6SIngo Molnar * If it's cycles then fall back to hrtimer 4093da297a6SIngo Molnar * based cpu-clock-tick sw counter, which 4103da297a6SIngo Molnar * is always available even if no PMU support: 4113da297a6SIngo Molnar */ 4123da297a6SIngo Molnar if (attr->type == PERF_TYPE_HARDWARE 413f4dbfa8fSPeter Zijlstra && attr->config == PERF_COUNT_HW_CPU_CYCLES) { 4143da297a6SIngo Molnar 4153da297a6SIngo Molnar if (verbose) 4163da297a6SIngo Molnar warning(" ... trying to fall back to cpu-clock-ticks\n"); 4173da297a6SIngo Molnar attr->type = PERF_TYPE_SOFTWARE; 418f4dbfa8fSPeter Zijlstra attr->config = PERF_COUNT_SW_CPU_CLOCK; 4193da297a6SIngo Molnar goto try_again; 4203da297a6SIngo Molnar } 42130c806a0SIngo Molnar printf("\n"); 422d9cf837eSCorey Ashford error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n", 423dd7927f4SArnaldo Carvalho de Melo err, strerror(err)); 424bfd45118SSimon Kaempflein 425bfd45118SSimon Kaempflein #if defined(__i386__) || defined(__x86_64__) 426bfd45118SSimon Kaempflein if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) 427d6d901c2SZhang, Yanmin die("No hardware sampling interrupt available." 428d6d901c2SZhang, Yanmin " No APIC? If so then you can boot the kernel" 429d6d901c2SZhang, Yanmin " with the \"lapic\" boot parameter to" 430d6d901c2SZhang, Yanmin " force-enable it.\n"); 431bfd45118SSimon Kaempflein #endif 432bfd45118SSimon Kaempflein 433cdd6c482SIngo Molnar die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); 4347c6a1c65SPeter Zijlstra } 4357c6a1c65SPeter Zijlstra } 4367c6a1c65SPeter Zijlstra 437dd7927f4SArnaldo Carvalho de Melo for (cpu = 0; cpu < cpus->nr; ++cpu) { 438361c99a6SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) 439361c99a6SArnaldo Carvalho de Melo create_counter(evlist, pos, cpu); 440dd7927f4SArnaldo Carvalho de Melo } 44186470930SIngo Molnar } 44286470930SIngo Molnar 4436122e4e4SArnaldo Carvalho de Melo static int process_buildids(void) 4446122e4e4SArnaldo Carvalho de Melo { 4456122e4e4SArnaldo Carvalho de Melo u64 size = lseek(output, 0, SEEK_CUR); 4466122e4e4SArnaldo Carvalho de Melo 4479f591fd7SArnaldo Carvalho de Melo if (size == 0) 4489f591fd7SArnaldo Carvalho de Melo return 0; 4499f591fd7SArnaldo Carvalho de Melo 4506122e4e4SArnaldo Carvalho de Melo session->fd = output; 4516122e4e4SArnaldo Carvalho de Melo return __perf_session__process_events(session, post_processing_offset, 4526122e4e4SArnaldo Carvalho de Melo size - post_processing_offset, 4536122e4e4SArnaldo Carvalho de Melo size, &build_id__mark_dso_hit_ops); 4546122e4e4SArnaldo Carvalho de Melo } 4556122e4e4SArnaldo Carvalho de Melo 456f5970550SPeter Zijlstra static void atexit_header(void) 457f5970550SPeter Zijlstra { 458c7929e47STom Zanussi if (!pipe_output) { 45994c744b6SArnaldo Carvalho de Melo session->header.data_size += bytes_written; 460f5970550SPeter Zijlstra 461baa2f6ceSArnaldo Carvalho de Melo if (!no_buildid) 4626122e4e4SArnaldo Carvalho de Melo process_buildids(); 463361c99a6SArnaldo Carvalho de Melo perf_header__write(&session->header, evsel_list, output, true); 46439d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 465361c99a6SArnaldo Carvalho de Melo perf_evlist__delete(evsel_list); 466d65a458bSArnaldo Carvalho de Melo symbol__exit(); 467c7929e47STom Zanussi } 468f5970550SPeter Zijlstra } 469f5970550SPeter Zijlstra 47023346f21SArnaldo Carvalho de Melo static void event__synthesize_guest_os(struct machine *machine, void *data) 471a1645ce1SZhang, Yanmin { 472a1645ce1SZhang, Yanmin int err; 47323346f21SArnaldo Carvalho de Melo struct perf_session *psession = data; 474a1645ce1SZhang, Yanmin 47523346f21SArnaldo Carvalho de Melo if (machine__is_host(machine)) 476a1645ce1SZhang, Yanmin return; 477a1645ce1SZhang, Yanmin 478a1645ce1SZhang, Yanmin /* 479a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 480a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 481a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 482a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 483a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 484a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 485a1645ce1SZhang, Yanmin */ 486a1645ce1SZhang, Yanmin err = event__synthesize_modules(process_synthesized_event, 48723346f21SArnaldo Carvalho de Melo psession, machine); 488a1645ce1SZhang, Yanmin if (err < 0) 489a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 49023346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 491a1645ce1SZhang, Yanmin 492a1645ce1SZhang, Yanmin /* 493a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 494a1645ce1SZhang, Yanmin * have no _text sometimes. 495a1645ce1SZhang, Yanmin */ 496a1645ce1SZhang, Yanmin err = event__synthesize_kernel_mmap(process_synthesized_event, 49723346f21SArnaldo Carvalho de Melo psession, machine, "_text"); 498a1645ce1SZhang, Yanmin if (err < 0) 499a1645ce1SZhang, Yanmin err = event__synthesize_kernel_mmap(process_synthesized_event, 50023346f21SArnaldo Carvalho de Melo psession, machine, "_stext"); 501a1645ce1SZhang, Yanmin if (err < 0) 502a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 50323346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 504a1645ce1SZhang, Yanmin } 505a1645ce1SZhang, Yanmin 50698402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 50798402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 50898402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 50998402807SFrederic Weisbecker }; 51098402807SFrederic Weisbecker 51198402807SFrederic Weisbecker static void mmap_read_all(void) 51298402807SFrederic Weisbecker { 5130e2e63ddSPeter Zijlstra int i; 51498402807SFrederic Weisbecker 515dd7927f4SArnaldo Carvalho de Melo for (i = 0; i < cpus->nr; i++) { 5160e2e63ddSPeter Zijlstra if (mmap_array[i].base) 5170e2e63ddSPeter Zijlstra mmap_read(&mmap_array[i]); 51898402807SFrederic Weisbecker } 51998402807SFrederic Weisbecker 52098402807SFrederic Weisbecker if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO)) 52198402807SFrederic Weisbecker write_output(&finished_round_event, sizeof(finished_round_event)); 52298402807SFrederic Weisbecker } 52398402807SFrederic Weisbecker 524d4db3f16SArnaldo Carvalho de Melo static int __cmd_record(int argc, const char **argv) 52586470930SIngo Molnar { 52669aad6f1SArnaldo Carvalho de Melo int i; 52786470930SIngo Molnar struct stat st; 52886470930SIngo Molnar int flags; 5294dc0a04bSArnaldo Carvalho de Melo int err; 5308b412664SPeter Zijlstra unsigned long waking = 0; 531856e9660SPeter Zijlstra int child_ready_pipe[2], go_pipe[2]; 53246be604bSZhang, Yanmin const bool forks = argc > 0; 533856e9660SPeter Zijlstra char buf; 53423346f21SArnaldo Carvalho de Melo struct machine *machine; 53586470930SIngo Molnar 53686470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 53786470930SIngo Molnar 538f5970550SPeter Zijlstra atexit(sig_atexit); 539f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 540f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 54118483b81SArnaldo Carvalho de Melo signal(SIGUSR1, sig_handler); 542f5970550SPeter Zijlstra 543d4db3f16SArnaldo Carvalho de Melo if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { 544856e9660SPeter Zijlstra perror("failed to create pipes"); 545856e9660SPeter Zijlstra exit(-1); 546856e9660SPeter Zijlstra } 547856e9660SPeter Zijlstra 548529870e3STom Zanussi if (!strcmp(output_name, "-")) 549529870e3STom Zanussi pipe_output = 1; 550529870e3STom Zanussi else if (!stat(output_name, &st) && st.st_size) { 5517865e817SFrederic Weisbecker if (write_mode == WRITE_FORCE) { 552b38d3464SArnaldo Carvalho de Melo char oldname[PATH_MAX]; 553b38d3464SArnaldo Carvalho de Melo snprintf(oldname, sizeof(oldname), "%s.old", 554b38d3464SArnaldo Carvalho de Melo output_name); 555b38d3464SArnaldo Carvalho de Melo unlink(oldname); 556b38d3464SArnaldo Carvalho de Melo rename(output_name, oldname); 557b38d3464SArnaldo Carvalho de Melo } 5587865e817SFrederic Weisbecker } else if (write_mode == WRITE_APPEND) { 5597865e817SFrederic Weisbecker write_mode = WRITE_FORCE; 560266e0e21SPierre Habouzit } 56186470930SIngo Molnar 562f887f301SXiao Guangrong flags = O_CREAT|O_RDWR; 5637865e817SFrederic Weisbecker if (write_mode == WRITE_APPEND) 564f5970550SPeter Zijlstra file_new = 0; 56586470930SIngo Molnar else 56686470930SIngo Molnar flags |= O_TRUNC; 56786470930SIngo Molnar 568529870e3STom Zanussi if (pipe_output) 569529870e3STom Zanussi output = STDOUT_FILENO; 570529870e3STom Zanussi else 57186470930SIngo Molnar output = open(output_name, flags, S_IRUSR | S_IWUSR); 57286470930SIngo Molnar if (output < 0) { 57386470930SIngo Molnar perror("failed to create output file"); 57486470930SIngo Molnar exit(-1); 57586470930SIngo Molnar } 57686470930SIngo Molnar 5777865e817SFrederic Weisbecker session = perf_session__new(output_name, O_WRONLY, 57821ef97f0SIan Munsie write_mode == WRITE_FORCE, false, NULL); 57994c744b6SArnaldo Carvalho de Melo if (session == NULL) { 580a9a70bbcSArnaldo Carvalho de Melo pr_err("Not enough memory for reading perf file header\n"); 581a9a70bbcSArnaldo Carvalho de Melo return -1; 582a9a70bbcSArnaldo Carvalho de Melo } 583a9a70bbcSArnaldo Carvalho de Melo 584baa2f6ceSArnaldo Carvalho de Melo if (!no_buildid) 585baa2f6ceSArnaldo Carvalho de Melo perf_header__set_feat(&session->header, HEADER_BUILD_ID); 586baa2f6ceSArnaldo Carvalho de Melo 5874dc0a04bSArnaldo Carvalho de Melo if (!file_new) { 5888dc58101STom Zanussi err = perf_header__read(session, output); 5894dc0a04bSArnaldo Carvalho de Melo if (err < 0) 59039d17dacSArnaldo Carvalho de Melo goto out_delete_session; 5914dc0a04bSArnaldo Carvalho de Melo } 5924dc0a04bSArnaldo Carvalho de Melo 593361c99a6SArnaldo Carvalho de Melo if (have_tracepoints(&evsel_list->entries)) 59494c744b6SArnaldo Carvalho de Melo perf_header__set_feat(&session->header, HEADER_TRACE_INFO); 59503456a15SFrederic Weisbecker 59639d17dacSArnaldo Carvalho de Melo /* 59739d17dacSArnaldo Carvalho de Melo * perf_session__delete(session) will be called at atexit_header() 59839d17dacSArnaldo Carvalho de Melo */ 599f5970550SPeter Zijlstra atexit(atexit_header); 600f5970550SPeter Zijlstra 601d4db3f16SArnaldo Carvalho de Melo if (forks) { 60246be604bSZhang, Yanmin child_pid = fork(); 6032fb750e8SBorislav Petkov if (child_pid < 0) { 604856e9660SPeter Zijlstra perror("failed to fork"); 605856e9660SPeter Zijlstra exit(-1); 606856e9660SPeter Zijlstra } 6077c6a1c65SPeter Zijlstra 60846be604bSZhang, Yanmin if (!child_pid) { 609529870e3STom Zanussi if (pipe_output) 610529870e3STom Zanussi dup2(2, 1); 611856e9660SPeter Zijlstra close(child_ready_pipe[0]); 612856e9660SPeter Zijlstra close(go_pipe[1]); 613856e9660SPeter Zijlstra fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 614856e9660SPeter Zijlstra 615856e9660SPeter Zijlstra /* 616856e9660SPeter Zijlstra * Do a dummy execvp to get the PLT entry resolved, 617856e9660SPeter Zijlstra * so we avoid the resolver overhead on the real 618856e9660SPeter Zijlstra * execvp call. 619856e9660SPeter Zijlstra */ 620856e9660SPeter Zijlstra execvp("", (char **)argv); 621856e9660SPeter Zijlstra 622856e9660SPeter Zijlstra /* 623856e9660SPeter Zijlstra * Tell the parent we're ready to go 624856e9660SPeter Zijlstra */ 625856e9660SPeter Zijlstra close(child_ready_pipe[1]); 626856e9660SPeter Zijlstra 627856e9660SPeter Zijlstra /* 628856e9660SPeter Zijlstra * Wait until the parent tells us to go. 629856e9660SPeter Zijlstra */ 630856e9660SPeter Zijlstra if (read(go_pipe[0], &buf, 1) == -1) 631856e9660SPeter Zijlstra perror("unable to read pipe"); 632856e9660SPeter Zijlstra 633856e9660SPeter Zijlstra execvp(argv[0], (char **)argv); 634856e9660SPeter Zijlstra 635856e9660SPeter Zijlstra perror(argv[0]); 63618483b81SArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 637856e9660SPeter Zijlstra exit(-1); 638856e9660SPeter Zijlstra } 639856e9660SPeter Zijlstra 640d6d901c2SZhang, Yanmin if (!system_wide && target_tid == -1 && target_pid == -1) 6415c98d466SArnaldo Carvalho de Melo threads->map[0] = child_pid; 642d6d901c2SZhang, Yanmin 643856e9660SPeter Zijlstra close(child_ready_pipe[1]); 644856e9660SPeter Zijlstra close(go_pipe[0]); 645856e9660SPeter Zijlstra /* 646856e9660SPeter Zijlstra * wait for child to settle 647856e9660SPeter Zijlstra */ 648856e9660SPeter Zijlstra if (read(child_ready_pipe[0], &buf, 1) == -1) { 649856e9660SPeter Zijlstra perror("unable to read pipe"); 650856e9660SPeter Zijlstra exit(-1); 651856e9660SPeter Zijlstra } 652856e9660SPeter Zijlstra close(child_ready_pipe[0]); 653856e9660SPeter Zijlstra } 654856e9660SPeter Zijlstra 655dd7927f4SArnaldo Carvalho de Melo open_counters(evsel_list); 65686470930SIngo Molnar 657640c03ceSArnaldo Carvalho de Melo perf_session__set_sample_type(session, sample_type); 658640c03ceSArnaldo Carvalho de Melo 659529870e3STom Zanussi if (pipe_output) { 660529870e3STom Zanussi err = perf_header__write_pipe(output); 661529870e3STom Zanussi if (err < 0) 662529870e3STom Zanussi return err; 663529870e3STom Zanussi } else if (file_new) { 664361c99a6SArnaldo Carvalho de Melo err = perf_header__write(&session->header, evsel_list, 665361c99a6SArnaldo Carvalho de Melo output, false); 666d5eed904SArnaldo Carvalho de Melo if (err < 0) 667d5eed904SArnaldo Carvalho de Melo return err; 668d5eed904SArnaldo Carvalho de Melo } 6697c6a1c65SPeter Zijlstra 6706122e4e4SArnaldo Carvalho de Melo post_processing_offset = lseek(output, 0, SEEK_CUR); 6716122e4e4SArnaldo Carvalho de Melo 6729c90a61cSArnaldo Carvalho de Melo perf_session__set_sample_id_all(session, sample_id_all_avail); 6739c90a61cSArnaldo Carvalho de Melo 6742c46dbb5STom Zanussi if (pipe_output) { 6752c46dbb5STom Zanussi err = event__synthesize_attrs(&session->header, 6762c46dbb5STom Zanussi process_synthesized_event, 6772c46dbb5STom Zanussi session); 6782c46dbb5STom Zanussi if (err < 0) { 6792c46dbb5STom Zanussi pr_err("Couldn't synthesize attrs.\n"); 6802c46dbb5STom Zanussi return err; 6812c46dbb5STom Zanussi } 682cd19a035STom Zanussi 683cd19a035STom Zanussi err = event__synthesize_event_types(process_synthesized_event, 684cd19a035STom Zanussi session); 685cd19a035STom Zanussi if (err < 0) { 686cd19a035STom Zanussi pr_err("Couldn't synthesize event_types.\n"); 687cd19a035STom Zanussi return err; 688cd19a035STom Zanussi } 6899215545eSTom Zanussi 690361c99a6SArnaldo Carvalho de Melo if (have_tracepoints(&evsel_list->entries)) { 69163e0c771STom Zanussi /* 69263e0c771STom Zanussi * FIXME err <= 0 here actually means that 69363e0c771STom Zanussi * there were no tracepoints so its not really 69463e0c771STom Zanussi * an error, just that we don't need to 69563e0c771STom Zanussi * synthesize anything. We really have to 69663e0c771STom Zanussi * return this more properly and also 69763e0c771STom Zanussi * propagate errors that now are calling die() 69863e0c771STom Zanussi */ 699361c99a6SArnaldo Carvalho de Melo err = event__synthesize_tracing_data(output, evsel_list, 7009215545eSTom Zanussi process_synthesized_event, 7019215545eSTom Zanussi session); 70263e0c771STom Zanussi if (err <= 0) { 70363e0c771STom Zanussi pr_err("Couldn't record tracing data.\n"); 70463e0c771STom Zanussi return err; 70563e0c771STom Zanussi } 7069215545eSTom Zanussi advance_output(err); 7072c46dbb5STom Zanussi } 70863e0c771STom Zanussi } 7092c46dbb5STom Zanussi 71023346f21SArnaldo Carvalho de Melo machine = perf_session__find_host_machine(session); 71123346f21SArnaldo Carvalho de Melo if (!machine) { 712a1645ce1SZhang, Yanmin pr_err("Couldn't find native kernel information.\n"); 713a1645ce1SZhang, Yanmin return -1; 714a1645ce1SZhang, Yanmin } 715a1645ce1SZhang, Yanmin 71656b03f3cSArnaldo Carvalho de Melo err = event__synthesize_kernel_mmap(process_synthesized_event, 71723346f21SArnaldo Carvalho de Melo session, machine, "_text"); 71870162138SArnaldo Carvalho de Melo if (err < 0) 71970162138SArnaldo Carvalho de Melo err = event__synthesize_kernel_mmap(process_synthesized_event, 72023346f21SArnaldo Carvalho de Melo session, machine, "_stext"); 721c1a3a4b9SArnaldo Carvalho de Melo if (err < 0) 722c1a3a4b9SArnaldo Carvalho de Melo pr_err("Couldn't record kernel reference relocation symbol\n" 723c1a3a4b9SArnaldo Carvalho de Melo "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 724c1a3a4b9SArnaldo Carvalho de Melo "Check /proc/kallsyms permission or run as root.\n"); 72556b03f3cSArnaldo Carvalho de Melo 726a1645ce1SZhang, Yanmin err = event__synthesize_modules(process_synthesized_event, 72723346f21SArnaldo Carvalho de Melo session, machine); 728c1a3a4b9SArnaldo Carvalho de Melo if (err < 0) 729c1a3a4b9SArnaldo Carvalho de Melo pr_err("Couldn't record kernel module information.\n" 730c1a3a4b9SArnaldo Carvalho de Melo "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 731c1a3a4b9SArnaldo Carvalho de Melo "Check /proc/modules permission or run as root.\n"); 732c1a3a4b9SArnaldo Carvalho de Melo 733a1645ce1SZhang, Yanmin if (perf_guest) 73423346f21SArnaldo Carvalho de Melo perf_session__process_machines(session, event__synthesize_guest_os); 735b7cece76SArnaldo Carvalho de Melo 736cf103a14SStephane Eranian if (!system_wide) 737d6d901c2SZhang, Yanmin event__synthesize_thread(target_tid, process_synthesized_event, 738d8f66248SArnaldo Carvalho de Melo session); 739234fbbf5SArnaldo Carvalho de Melo else 740d8f66248SArnaldo Carvalho de Melo event__synthesize_threads(process_synthesized_event, session); 7417c6a1c65SPeter Zijlstra 74286470930SIngo Molnar if (realtime_prio) { 74386470930SIngo Molnar struct sched_param param; 74486470930SIngo Molnar 74586470930SIngo Molnar param.sched_priority = realtime_prio; 74686470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 7476beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 74886470930SIngo Molnar exit(-1); 74986470930SIngo Molnar } 75086470930SIngo Molnar } 75186470930SIngo Molnar 752856e9660SPeter Zijlstra /* 753856e9660SPeter Zijlstra * Let the child rip 754856e9660SPeter Zijlstra */ 755d4db3f16SArnaldo Carvalho de Melo if (forks) 756856e9660SPeter Zijlstra close(go_pipe[1]); 757856e9660SPeter Zijlstra 758649c48a9SPeter Zijlstra for (;;) { 75986470930SIngo Molnar int hits = samples; 760d6d901c2SZhang, Yanmin int thread; 76186470930SIngo Molnar 76298402807SFrederic Weisbecker mmap_read_all(); 76386470930SIngo Molnar 764649c48a9SPeter Zijlstra if (hits == samples) { 765649c48a9SPeter Zijlstra if (done) 766649c48a9SPeter Zijlstra break; 7675c581041SArnaldo Carvalho de Melo err = poll(evsel_list->pollfd, evsel_list->nr_fds, -1); 7688b412664SPeter Zijlstra waking++; 7698b412664SPeter Zijlstra } 7708b412664SPeter Zijlstra 7718b412664SPeter Zijlstra if (done) { 772dd7927f4SArnaldo Carvalho de Melo for (i = 0; i < cpus->nr; i++) { 77369aad6f1SArnaldo Carvalho de Melo struct perf_evsel *pos; 77469aad6f1SArnaldo Carvalho de Melo 775361c99a6SArnaldo Carvalho de Melo list_for_each_entry(pos, &evsel_list->entries, node) { 776d6d901c2SZhang, Yanmin for (thread = 0; 7775c98d466SArnaldo Carvalho de Melo thread < threads->nr; 778d6d901c2SZhang, Yanmin thread++) 77969aad6f1SArnaldo Carvalho de Melo ioctl(FD(pos, i, thread), 780d6d901c2SZhang, Yanmin PERF_EVENT_IOC_DISABLE); 781d6d901c2SZhang, Yanmin } 78286470930SIngo Molnar } 783649c48a9SPeter Zijlstra } 7848b412664SPeter Zijlstra } 7858b412664SPeter Zijlstra 78618483b81SArnaldo Carvalho de Melo if (quiet || signr == SIGUSR1) 787b44308f5SArnaldo Carvalho de Melo return 0; 788b44308f5SArnaldo Carvalho de Melo 7898b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 79086470930SIngo Molnar 79186470930SIngo Molnar /* 79286470930SIngo Molnar * Approximate RIP event size: 24 bytes. 79386470930SIngo Molnar */ 79486470930SIngo Molnar fprintf(stderr, 7959486aa38SArnaldo Carvalho de Melo "[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n", 79686470930SIngo Molnar (double)bytes_written / 1024.0 / 1024.0, 79786470930SIngo Molnar output_name, 79886470930SIngo Molnar bytes_written / 24); 79986470930SIngo Molnar 80086470930SIngo Molnar return 0; 80139d17dacSArnaldo Carvalho de Melo 80239d17dacSArnaldo Carvalho de Melo out_delete_session: 80339d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 80439d17dacSArnaldo Carvalho de Melo return err; 80586470930SIngo Molnar } 80686470930SIngo Molnar 80786470930SIngo Molnar static const char * const record_usage[] = { 80886470930SIngo Molnar "perf record [<options>] [<command>]", 80986470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 81086470930SIngo Molnar NULL 81186470930SIngo Molnar }; 81286470930SIngo Molnar 8137865e817SFrederic Weisbecker static bool force, append_file; 8147865e817SFrederic Weisbecker 815bca647aaSTom Zanussi const struct option record_options[] = { 816361c99a6SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &evsel_list, "event", 81786470930SIngo Molnar "event selector. use 'perf list' to list available events", 81886470930SIngo Molnar parse_events), 819361c99a6SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &evsel_list, "filter", 820c171b552SLi Zefan "event filter", parse_filter), 82186470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 822d6d901c2SZhang, Yanmin "record events on existing process id"), 823d6d901c2SZhang, Yanmin OPT_INTEGER('t', "tid", &target_tid, 824d6d901c2SZhang, Yanmin "record events on existing thread id"), 82586470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 82686470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 827acac03faSKirill Smelkov OPT_BOOLEAN('D', "no-delay", &nodelay, 828acac03faSKirill Smelkov "collect data without buffering"), 829daac07b2SFrederic Weisbecker OPT_BOOLEAN('R', "raw-samples", &raw_samples, 830daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 83186470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 83286470930SIngo Molnar "system-wide collection from all CPUs"), 83386470930SIngo Molnar OPT_BOOLEAN('A', "append", &append_file, 83486470930SIngo Molnar "append to the output file to do incremental profiling"), 835c45c6ea2SStephane Eranian OPT_STRING('C', "cpu", &cpu_list, "cpu", 836c45c6ea2SStephane Eranian "list of cpus to monitor"), 83786470930SIngo Molnar OPT_BOOLEAN('f', "force", &force, 8387865e817SFrederic Weisbecker "overwrite existing data file (deprecated)"), 8393de29cabSStephane Eranian OPT_U64('c', "count", &user_interval, "event period to sample"), 84086470930SIngo Molnar OPT_STRING('o', "output", &output_name, "file", 84186470930SIngo Molnar "output file name"), 8422e6cdf99SStephane Eranian OPT_BOOLEAN('i', "no-inherit", &no_inherit, 8432e6cdf99SStephane Eranian "child tasks do not inherit counters"), 8441967936dSArnaldo Carvalho de Melo OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"), 8451967936dSArnaldo Carvalho de Melo OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"), 8463efa1cc9SIngo Molnar OPT_BOOLEAN('g', "call-graph", &call_graph, 8473efa1cc9SIngo Molnar "do call-graph (stack chain/backtrace) recording"), 848c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 8493da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 850b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 851649c48a9SPeter Zijlstra OPT_BOOLEAN('s', "stat", &inherit_stat, 852649c48a9SPeter Zijlstra "per thread counts"), 8534bba828dSAnton Blanchard OPT_BOOLEAN('d', "data", &sample_address, 8544bba828dSAnton Blanchard "Sample addresses"), 8559c90a61cSArnaldo Carvalho de Melo OPT_BOOLEAN('T', "timestamp", &sample_time, "Sample timestamps"), 856649c48a9SPeter Zijlstra OPT_BOOLEAN('n', "no-samples", &no_samples, 857649c48a9SPeter Zijlstra "don't sample"), 858baa2f6ceSArnaldo Carvalho de Melo OPT_BOOLEAN('N', "no-buildid-cache", &no_buildid_cache, 859a1ac1d3cSStephane Eranian "do not update the buildid cache"), 860baa2f6ceSArnaldo Carvalho de Melo OPT_BOOLEAN('B', "no-buildid", &no_buildid, 861baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 86286470930SIngo Molnar OPT_END() 86386470930SIngo Molnar }; 86486470930SIngo Molnar 865f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used) 86686470930SIngo Molnar { 86769aad6f1SArnaldo Carvalho de Melo int err = -ENOMEM; 86869aad6f1SArnaldo Carvalho de Melo struct perf_evsel *pos; 86986470930SIngo Molnar 870361c99a6SArnaldo Carvalho de Melo evsel_list = perf_evlist__new(); 871361c99a6SArnaldo Carvalho de Melo if (evsel_list == NULL) 872361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 873361c99a6SArnaldo Carvalho de Melo 874bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 875a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 876d6d901c2SZhang, Yanmin if (!argc && target_pid == -1 && target_tid == -1 && 877c45c6ea2SStephane Eranian !system_wide && !cpu_list) 878bca647aaSTom Zanussi usage_with_options(record_usage, record_options); 87986470930SIngo Molnar 8807865e817SFrederic Weisbecker if (force && append_file) { 8817865e817SFrederic Weisbecker fprintf(stderr, "Can't overwrite and append at the same time." 8827865e817SFrederic Weisbecker " You need to choose between -f and -A"); 883bca647aaSTom Zanussi usage_with_options(record_usage, record_options); 8847865e817SFrederic Weisbecker } else if (append_file) { 8857865e817SFrederic Weisbecker write_mode = WRITE_APPEND; 8867865e817SFrederic Weisbecker } else { 8877865e817SFrederic Weisbecker write_mode = WRITE_FORCE; 8887865e817SFrederic Weisbecker } 8897865e817SFrederic Weisbecker 890655000e7SArnaldo Carvalho de Melo symbol__init(); 891baa2f6ceSArnaldo Carvalho de Melo 892baa2f6ceSArnaldo Carvalho de Melo if (no_buildid_cache || no_buildid) 893a1ac1d3cSStephane Eranian disable_buildid_cache(); 894655000e7SArnaldo Carvalho de Melo 895361c99a6SArnaldo Carvalho de Melo if (evsel_list->nr_entries == 0 && 896361c99a6SArnaldo Carvalho de Melo perf_evlist__add_default(evsel_list) < 0) { 89769aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 89869aad6f1SArnaldo Carvalho de Melo goto out_symbol_exit; 899bbd36e5eSPeter Zijlstra } 90086470930SIngo Molnar 9015c98d466SArnaldo Carvalho de Melo if (target_pid != -1) 902d6d901c2SZhang, Yanmin target_tid = target_pid; 903d6d901c2SZhang, Yanmin 9045c98d466SArnaldo Carvalho de Melo threads = thread_map__new(target_pid, target_tid); 9055c98d466SArnaldo Carvalho de Melo if (threads == NULL) { 9065c98d466SArnaldo Carvalho de Melo pr_err("Problems finding threads of monitor\n"); 9075c98d466SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 908d6d901c2SZhang, Yanmin } 909d6d901c2SZhang, Yanmin 910dd7927f4SArnaldo Carvalho de Melo if (target_tid != -1) 911dd7927f4SArnaldo Carvalho de Melo cpus = cpu_map__dummy_new(); 912dd7927f4SArnaldo Carvalho de Melo else 91360d567e2SArnaldo Carvalho de Melo cpus = cpu_map__new(cpu_list); 914dd7927f4SArnaldo Carvalho de Melo 915dd7927f4SArnaldo Carvalho de Melo if (cpus == NULL) 916dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 91769aad6f1SArnaldo Carvalho de Melo 918361c99a6SArnaldo Carvalho de Melo list_for_each_entry(pos, &evsel_list->entries, node) { 9195c98d466SArnaldo Carvalho de Melo if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0) 92069aad6f1SArnaldo Carvalho de Melo goto out_free_fd; 921ad7f4e3fSArnaldo Carvalho de Melo if (perf_header__push_event(pos->attr.config, event_name(pos))) 922ad7f4e3fSArnaldo Carvalho de Melo goto out_free_fd; 923d6d901c2SZhang, Yanmin } 9245c581041SArnaldo Carvalho de Melo 9255c581041SArnaldo Carvalho de Melo if (perf_evlist__alloc_pollfd(evsel_list, cpus->nr, threads->nr) < 0) 92639d17dacSArnaldo Carvalho de Melo goto out_free_fd; 927d6d901c2SZhang, Yanmin 9283de29cabSStephane Eranian if (user_interval != ULLONG_MAX) 929f9212819SFrederic Weisbecker default_interval = user_interval; 930f9212819SFrederic Weisbecker if (user_freq != UINT_MAX) 931f9212819SFrederic Weisbecker freq = user_freq; 932f9212819SFrederic Weisbecker 9337e4ff9e3SMike Galbraith /* 9347e4ff9e3SMike Galbraith * User specified count overrides default frequency. 9357e4ff9e3SMike Galbraith */ 9367e4ff9e3SMike Galbraith if (default_interval) 9377e4ff9e3SMike Galbraith freq = 0; 9387e4ff9e3SMike Galbraith else if (freq) { 9397e4ff9e3SMike Galbraith default_interval = freq; 9407e4ff9e3SMike Galbraith } else { 9417e4ff9e3SMike Galbraith fprintf(stderr, "frequency and count are zero, aborting\n"); 94239d17dacSArnaldo Carvalho de Melo err = -EINVAL; 9435c581041SArnaldo Carvalho de Melo goto out_free_fd; 9447e4ff9e3SMike Galbraith } 9457e4ff9e3SMike Galbraith 94639d17dacSArnaldo Carvalho de Melo err = __cmd_record(argc, argv); 94739d17dacSArnaldo Carvalho de Melo 94839d17dacSArnaldo Carvalho de Melo out_free_fd: 9495c98d466SArnaldo Carvalho de Melo thread_map__delete(threads); 9505c98d466SArnaldo Carvalho de Melo threads = NULL; 951d65a458bSArnaldo Carvalho de Melo out_symbol_exit: 952d65a458bSArnaldo Carvalho de Melo symbol__exit(); 95339d17dacSArnaldo Carvalho de Melo return err; 95486470930SIngo Molnar } 955