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" 218f28827aSFrederic Weisbecker #include "util/debug.h" 2294c744b6SArnaldo Carvalho de Melo #include "util/session.h" 238d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 24a12b51c4SPaul Mackerras #include "util/cpumap.h" 257c6a1c65SPeter Zijlstra 2686470930SIngo Molnar #include <unistd.h> 2786470930SIngo Molnar #include <sched.h> 2886470930SIngo Molnar 297865e817SFrederic Weisbecker enum write_mode_t { 307865e817SFrederic Weisbecker WRITE_FORCE, 317865e817SFrederic Weisbecker WRITE_APPEND 327865e817SFrederic Weisbecker }; 337865e817SFrederic Weisbecker 34d6d901c2SZhang, Yanmin static int *fd[MAX_NR_CPUS][MAX_COUNTERS]; 3586470930SIngo Molnar 36*3de29cabSStephane Eranian static u64 user_interval = ULLONG_MAX; 37*3de29cabSStephane Eranian static u64 default_interval = 0; 3886470930SIngo Molnar 3986470930SIngo Molnar static int nr_cpus = 0; 4086470930SIngo Molnar static unsigned int page_size; 4186470930SIngo Molnar static unsigned int mmap_pages = 128; 42f9212819SFrederic Weisbecker static unsigned int user_freq = UINT_MAX; 4342e59d7dSIngo Molnar static int freq = 1000; 4486470930SIngo Molnar static int output; 45529870e3STom Zanussi static int pipe_output = 0; 4686470930SIngo Molnar static const char *output_name = "perf.data"; 4786470930SIngo Molnar static int group = 0; 4886470930SIngo Molnar static unsigned int realtime_prio = 0; 49c0555642SIan Munsie static bool raw_samples = false; 50c0555642SIan Munsie static bool system_wide = false; 510a5ac846SJens Axboe static int profile_cpu = -1; 5286470930SIngo Molnar static pid_t target_pid = -1; 53d6d901c2SZhang, Yanmin static pid_t target_tid = -1; 54d6d901c2SZhang, Yanmin static pid_t *all_tids = NULL; 55d6d901c2SZhang, Yanmin static int thread_num = 0; 56933da83aSChris Wilson static pid_t child_pid = -1; 572e6cdf99SStephane Eranian static bool no_inherit = false; 587865e817SFrederic Weisbecker static enum write_mode_t write_mode = WRITE_FORCE; 59c0555642SIan Munsie static bool call_graph = false; 60c0555642SIan Munsie static bool inherit_stat = false; 61c0555642SIan Munsie static bool no_samples = false; 62c0555642SIan Munsie static bool sample_address = false; 63c0555642SIan Munsie static bool multiplex = false; 64ea57c4f5SIngo Molnar static int multiplex_fd = -1; 6586470930SIngo Molnar 6642e59d7dSIngo Molnar static long samples = 0; 6786470930SIngo Molnar static struct timeval last_read; 6886470930SIngo Molnar static struct timeval this_read; 6986470930SIngo Molnar 7042e59d7dSIngo Molnar static u64 bytes_written = 0; 7186470930SIngo Molnar 72d6d901c2SZhang, Yanmin static struct pollfd *event_array; 7386470930SIngo Molnar 7442e59d7dSIngo Molnar static int nr_poll = 0; 7542e59d7dSIngo Molnar static int nr_cpu = 0; 7686470930SIngo Molnar 77f5970550SPeter Zijlstra static int file_new = 1; 786122e4e4SArnaldo Carvalho de Melo static off_t post_processing_offset; 797c6a1c65SPeter Zijlstra 8094c744b6SArnaldo Carvalho de Melo static struct perf_session *session; 81f5970550SPeter Zijlstra 8286470930SIngo Molnar struct mmap_data { 8386470930SIngo Molnar int counter; 8486470930SIngo Molnar void *base; 8586470930SIngo Molnar unsigned int mask; 8686470930SIngo Molnar unsigned int prev; 8786470930SIngo Molnar }; 8886470930SIngo Molnar 89d6d901c2SZhang, Yanmin static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; 9086470930SIngo Molnar 919d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md) 9286470930SIngo Molnar { 93cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 949d91a6f7SPeter Zijlstra long head; 9586470930SIngo Molnar 9686470930SIngo Molnar head = pc->data_head; 9786470930SIngo Molnar rmb(); 9886470930SIngo Molnar 9986470930SIngo Molnar return head; 10086470930SIngo Molnar } 10186470930SIngo Molnar 1029d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail) 1039d91a6f7SPeter Zijlstra { 104cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 1059d91a6f7SPeter Zijlstra 1069d91a6f7SPeter Zijlstra /* 1079d91a6f7SPeter Zijlstra * ensure all reads are done before we write the tail out. 1089d91a6f7SPeter Zijlstra */ 1099d91a6f7SPeter Zijlstra /* mb(); */ 1109d91a6f7SPeter Zijlstra pc->data_tail = tail; 1119d91a6f7SPeter Zijlstra } 1129d91a6f7SPeter Zijlstra 1139215545eSTom Zanussi static void advance_output(size_t size) 1149215545eSTom Zanussi { 1159215545eSTom Zanussi bytes_written += size; 1169215545eSTom Zanussi } 1179215545eSTom Zanussi 118f5970550SPeter Zijlstra static void write_output(void *buf, size_t size) 119f5970550SPeter Zijlstra { 120f5970550SPeter Zijlstra while (size) { 121f5970550SPeter Zijlstra int ret = write(output, buf, size); 122f5970550SPeter Zijlstra 123f5970550SPeter Zijlstra if (ret < 0) 124f5970550SPeter Zijlstra die("failed to write"); 125f5970550SPeter Zijlstra 126f5970550SPeter Zijlstra size -= ret; 127f5970550SPeter Zijlstra buf += ret; 128f5970550SPeter Zijlstra 129f5970550SPeter Zijlstra bytes_written += ret; 130f5970550SPeter Zijlstra } 131f5970550SPeter Zijlstra } 132f5970550SPeter Zijlstra 133d8f66248SArnaldo Carvalho de Melo static int process_synthesized_event(event_t *event, 134d8f66248SArnaldo Carvalho de Melo struct perf_session *self __used) 135234fbbf5SArnaldo Carvalho de Melo { 1366122e4e4SArnaldo Carvalho de Melo write_output(event, event->header.size); 137234fbbf5SArnaldo Carvalho de Melo return 0; 138234fbbf5SArnaldo Carvalho de Melo } 139234fbbf5SArnaldo Carvalho de Melo 14086470930SIngo Molnar static void mmap_read(struct mmap_data *md) 14186470930SIngo Molnar { 14286470930SIngo Molnar unsigned int head = mmap_read_head(md); 14386470930SIngo Molnar unsigned int old = md->prev; 14486470930SIngo Molnar unsigned char *data = md->base + page_size; 14586470930SIngo Molnar unsigned long size; 14686470930SIngo Molnar void *buf; 14786470930SIngo Molnar int diff; 14886470930SIngo Molnar 14986470930SIngo Molnar gettimeofday(&this_read, NULL); 15086470930SIngo Molnar 15186470930SIngo Molnar /* 15286470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 15386470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 15486470930SIngo Molnar * 15586470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 15686470930SIngo Molnar * 15786470930SIngo Molnar * In either case, truncate and restart at head. 15886470930SIngo Molnar */ 15986470930SIngo Molnar diff = head - old; 1609d91a6f7SPeter Zijlstra if (diff < 0) { 16186470930SIngo Molnar struct timeval iv; 16286470930SIngo Molnar unsigned long msecs; 16386470930SIngo Molnar 16486470930SIngo Molnar timersub(&this_read, &last_read, &iv); 16586470930SIngo Molnar msecs = iv.tv_sec*1000 + iv.tv_usec/1000; 16686470930SIngo Molnar 16786470930SIngo Molnar fprintf(stderr, "WARNING: failed to keep up with mmap data." 16886470930SIngo Molnar " Last read %lu msecs ago.\n", msecs); 16986470930SIngo Molnar 17086470930SIngo Molnar /* 17186470930SIngo Molnar * head points to a known good entry, start there. 17286470930SIngo Molnar */ 17386470930SIngo Molnar old = head; 17486470930SIngo Molnar } 17586470930SIngo Molnar 17686470930SIngo Molnar last_read = this_read; 17786470930SIngo Molnar 17886470930SIngo Molnar if (old != head) 17986470930SIngo Molnar samples++; 18086470930SIngo Molnar 18186470930SIngo Molnar size = head - old; 18286470930SIngo Molnar 18386470930SIngo Molnar if ((old & md->mask) + size != (head & md->mask)) { 18486470930SIngo Molnar buf = &data[old & md->mask]; 18586470930SIngo Molnar size = md->mask + 1 - (old & md->mask); 18686470930SIngo Molnar old += size; 18786470930SIngo Molnar 1886122e4e4SArnaldo Carvalho de Melo write_output(buf, size); 18986470930SIngo Molnar } 19086470930SIngo Molnar 19186470930SIngo Molnar buf = &data[old & md->mask]; 19286470930SIngo Molnar size = head - old; 19386470930SIngo Molnar old += size; 19486470930SIngo Molnar 1956122e4e4SArnaldo Carvalho de Melo write_output(buf, size); 19686470930SIngo Molnar 19786470930SIngo Molnar md->prev = old; 1989d91a6f7SPeter Zijlstra mmap_write_tail(md, old); 19986470930SIngo Molnar } 20086470930SIngo Molnar 20186470930SIngo Molnar static volatile int done = 0; 202f7b7c26eSPeter Zijlstra static volatile int signr = -1; 20386470930SIngo Molnar 20486470930SIngo Molnar static void sig_handler(int sig) 20586470930SIngo Molnar { 20686470930SIngo Molnar done = 1; 207f7b7c26eSPeter Zijlstra signr = sig; 208f7b7c26eSPeter Zijlstra } 209f7b7c26eSPeter Zijlstra 210f7b7c26eSPeter Zijlstra static void sig_atexit(void) 211f7b7c26eSPeter Zijlstra { 212933da83aSChris Wilson if (child_pid != -1) 213933da83aSChris Wilson kill(child_pid, SIGTERM); 214933da83aSChris Wilson 215f7b7c26eSPeter Zijlstra if (signr == -1) 216f7b7c26eSPeter Zijlstra return; 217f7b7c26eSPeter Zijlstra 218f7b7c26eSPeter Zijlstra signal(signr, SIG_DFL); 219f7b7c26eSPeter Zijlstra kill(getpid(), signr); 22086470930SIngo Molnar } 22186470930SIngo Molnar 22286470930SIngo Molnar static int group_fd; 22386470930SIngo Molnar 224cdd6c482SIngo Molnar static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) 2257c6a1c65SPeter Zijlstra { 2267c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 2277c6a1c65SPeter Zijlstra 22894c744b6SArnaldo Carvalho de Melo if (nr < session->header.attrs) { 22994c744b6SArnaldo Carvalho de Melo h_attr = session->header.attr[nr]; 2307c6a1c65SPeter Zijlstra } else { 2317c6a1c65SPeter Zijlstra h_attr = perf_header_attr__new(a); 232dc79c0fcSArnaldo Carvalho de Melo if (h_attr != NULL) 23394c744b6SArnaldo Carvalho de Melo if (perf_header__add_attr(&session->header, h_attr) < 0) { 23411deb1f9SArnaldo Carvalho de Melo perf_header_attr__delete(h_attr); 23511deb1f9SArnaldo Carvalho de Melo h_attr = NULL; 23611deb1f9SArnaldo Carvalho de Melo } 2377c6a1c65SPeter Zijlstra } 2387c6a1c65SPeter Zijlstra 2397c6a1c65SPeter Zijlstra return h_attr; 2407c6a1c65SPeter Zijlstra } 2417c6a1c65SPeter Zijlstra 242d6d901c2SZhang, Yanmin static void create_counter(int counter, int cpu) 24386470930SIngo Molnar { 244c171b552SLi Zefan char *filter = filters[counter]; 245cdd6c482SIngo Molnar struct perf_event_attr *attr = attrs + counter; 2467c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 2477c6a1c65SPeter Zijlstra int track = !counter; /* only the first counter needs these */ 248d6d901c2SZhang, Yanmin int thread_index; 249c171b552SLi Zefan int ret; 2507c6a1c65SPeter Zijlstra struct { 2517c6a1c65SPeter Zijlstra u64 count; 2527c6a1c65SPeter Zijlstra u64 time_enabled; 2537c6a1c65SPeter Zijlstra u64 time_running; 2547c6a1c65SPeter Zijlstra u64 id; 2557c6a1c65SPeter Zijlstra } read_data; 2567c6a1c65SPeter Zijlstra 2577c6a1c65SPeter Zijlstra attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | 2587c6a1c65SPeter Zijlstra PERF_FORMAT_TOTAL_TIME_RUNNING | 2597c6a1c65SPeter Zijlstra PERF_FORMAT_ID; 26086470930SIngo Molnar 2613a9f131fSFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; 2623efa1cc9SIngo Molnar 2638907fd60SEric B Munson if (nr_counters > 1) 2648907fd60SEric B Munson attr->sample_type |= PERF_SAMPLE_ID; 2658907fd60SEric B Munson 266f9212819SFrederic Weisbecker /* 267f9212819SFrederic Weisbecker * We default some events to a 1 default interval. But keep 268f9212819SFrederic Weisbecker * it a weak assumption overridable by the user. 269f9212819SFrederic Weisbecker */ 270f9212819SFrederic Weisbecker if (!attr->sample_period || (user_freq != UINT_MAX && 271*3de29cabSStephane Eranian user_interval != ULLONG_MAX)) { 27286470930SIngo Molnar if (freq) { 273ea1900e5SPeter Zijlstra attr->sample_type |= PERF_SAMPLE_PERIOD; 27486470930SIngo Molnar attr->freq = 1; 27586470930SIngo Molnar attr->sample_freq = freq; 276f9212819SFrederic Weisbecker } else { 277f9212819SFrederic Weisbecker attr->sample_period = default_interval; 278f9212819SFrederic Weisbecker } 27986470930SIngo Molnar } 2803efa1cc9SIngo Molnar 281649c48a9SPeter Zijlstra if (no_samples) 282649c48a9SPeter Zijlstra attr->sample_freq = 0; 283649c48a9SPeter Zijlstra 284649c48a9SPeter Zijlstra if (inherit_stat) 285649c48a9SPeter Zijlstra attr->inherit_stat = 1; 286649c48a9SPeter Zijlstra 2874bba828dSAnton Blanchard if (sample_address) 2884bba828dSAnton Blanchard attr->sample_type |= PERF_SAMPLE_ADDR; 2894bba828dSAnton Blanchard 2903efa1cc9SIngo Molnar if (call_graph) 2913efa1cc9SIngo Molnar attr->sample_type |= PERF_SAMPLE_CALLCHAIN; 2923efa1cc9SIngo Molnar 293cd6feeeaSIngo Molnar if (raw_samples) { 2946ddf259dSIngo Molnar attr->sample_type |= PERF_SAMPLE_TIME; 295daac07b2SFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_RAW; 296cd6feeeaSIngo Molnar attr->sample_type |= PERF_SAMPLE_CPU; 297cd6feeeaSIngo Molnar } 298f413cdb8SFrederic Weisbecker 29986470930SIngo Molnar attr->mmap = track; 30086470930SIngo Molnar attr->comm = track; 3012e6cdf99SStephane Eranian attr->inherit = !no_inherit; 3022e6cdf99SStephane Eranian if (target_pid == -1 && target_tid == -1 && !system_wide) { 3034502d77cSPeter Zijlstra attr->disabled = 1; 304bedbfdeaSEric B Munson attr->enable_on_exec = 1; 30546be604bSZhang, Yanmin } 306bedbfdeaSEric B Munson 307d6d901c2SZhang, Yanmin for (thread_index = 0; thread_index < thread_num; thread_index++) { 3083da297a6SIngo Molnar try_again: 309d6d901c2SZhang, Yanmin fd[nr_cpu][counter][thread_index] = sys_perf_event_open(attr, 310d6d901c2SZhang, Yanmin all_tids[thread_index], cpu, group_fd, 0); 31186470930SIngo Molnar 312d6d901c2SZhang, Yanmin if (fd[nr_cpu][counter][thread_index] < 0) { 31386470930SIngo Molnar int err = errno; 31486470930SIngo Molnar 315c10edee2SPekka Enberg if (err == EPERM || err == EACCES) 3166230f2c7SArnaldo Carvalho de Melo die("Permission error - are you root?\n" 317d6d901c2SZhang, Yanmin "\t Consider tweaking" 318d6d901c2SZhang, Yanmin " /proc/sys/kernel/perf_event_paranoid.\n"); 319d6d901c2SZhang, Yanmin else if (err == ENODEV && profile_cpu != -1) { 320d6d901c2SZhang, Yanmin die("No such device - did you specify" 321d6d901c2SZhang, Yanmin " an out-of-range profile CPU?\n"); 322d6d901c2SZhang, Yanmin } 3233da297a6SIngo Molnar 3243da297a6SIngo Molnar /* 3253da297a6SIngo Molnar * If it's cycles then fall back to hrtimer 3263da297a6SIngo Molnar * based cpu-clock-tick sw counter, which 3273da297a6SIngo Molnar * is always available even if no PMU support: 3283da297a6SIngo Molnar */ 3293da297a6SIngo Molnar if (attr->type == PERF_TYPE_HARDWARE 330f4dbfa8fSPeter Zijlstra && attr->config == PERF_COUNT_HW_CPU_CYCLES) { 3313da297a6SIngo Molnar 3323da297a6SIngo Molnar if (verbose) 3333da297a6SIngo Molnar warning(" ... trying to fall back to cpu-clock-ticks\n"); 3343da297a6SIngo Molnar attr->type = PERF_TYPE_SOFTWARE; 335f4dbfa8fSPeter Zijlstra attr->config = PERF_COUNT_SW_CPU_CLOCK; 3363da297a6SIngo Molnar goto try_again; 3373da297a6SIngo Molnar } 33830c806a0SIngo Molnar printf("\n"); 33930c806a0SIngo Molnar error("perfcounter syscall returned with %d (%s)\n", 340d6d901c2SZhang, Yanmin fd[nr_cpu][counter][thread_index], strerror(err)); 341bfd45118SSimon Kaempflein 342bfd45118SSimon Kaempflein #if defined(__i386__) || defined(__x86_64__) 343bfd45118SSimon Kaempflein if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) 344d6d901c2SZhang, Yanmin die("No hardware sampling interrupt available." 345d6d901c2SZhang, Yanmin " No APIC? If so then you can boot the kernel" 346d6d901c2SZhang, Yanmin " with the \"lapic\" boot parameter to" 347d6d901c2SZhang, Yanmin " force-enable it.\n"); 348bfd45118SSimon Kaempflein #endif 349bfd45118SSimon Kaempflein 350cdd6c482SIngo Molnar die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); 35186470930SIngo Molnar exit(-1); 35286470930SIngo Molnar } 3533da297a6SIngo Molnar 3547c6a1c65SPeter Zijlstra h_attr = get_header_attr(attr, counter); 355dc79c0fcSArnaldo Carvalho de Melo if (h_attr == NULL) 356dc79c0fcSArnaldo Carvalho de Melo die("nomem\n"); 3577c6a1c65SPeter Zijlstra 3587c6a1c65SPeter Zijlstra if (!file_new) { 3597c6a1c65SPeter Zijlstra if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { 3607c6a1c65SPeter Zijlstra fprintf(stderr, "incompatible append\n"); 3617c6a1c65SPeter Zijlstra exit(-1); 3627c6a1c65SPeter Zijlstra } 3637c6a1c65SPeter Zijlstra } 3647c6a1c65SPeter Zijlstra 365d6d901c2SZhang, Yanmin if (read(fd[nr_cpu][counter][thread_index], &read_data, sizeof(read_data)) == -1) { 3663928ddbeSFrederic Weisbecker perror("Unable to read perf file descriptor\n"); 3673928ddbeSFrederic Weisbecker exit(-1); 3683928ddbeSFrederic Weisbecker } 3697c6a1c65SPeter Zijlstra 37058754121SArnaldo Carvalho de Melo if (perf_header_attr__add_id(h_attr, read_data.id) < 0) { 37158754121SArnaldo Carvalho de Melo pr_warning("Not enough memory to add id\n"); 37258754121SArnaldo Carvalho de Melo exit(-1); 37358754121SArnaldo Carvalho de Melo } 3747c6a1c65SPeter Zijlstra 375d6d901c2SZhang, Yanmin assert(fd[nr_cpu][counter][thread_index] >= 0); 376d6d901c2SZhang, Yanmin fcntl(fd[nr_cpu][counter][thread_index], F_SETFL, O_NONBLOCK); 37786470930SIngo Molnar 37886470930SIngo Molnar /* 37986470930SIngo Molnar * First counter acts as the group leader: 38086470930SIngo Molnar */ 38186470930SIngo Molnar if (group && group_fd == -1) 382d6d901c2SZhang, Yanmin group_fd = fd[nr_cpu][counter][thread_index]; 383ea57c4f5SIngo Molnar if (multiplex && multiplex_fd == -1) 384d6d901c2SZhang, Yanmin multiplex_fd = fd[nr_cpu][counter][thread_index]; 38586470930SIngo Molnar 386d6d901c2SZhang, Yanmin if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) { 387ea57c4f5SIngo Molnar 388d6d901c2SZhang, Yanmin ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd); 389ea57c4f5SIngo Molnar assert(ret != -1); 390ea57c4f5SIngo Molnar } else { 391d6d901c2SZhang, Yanmin event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index]; 39286470930SIngo Molnar event_array[nr_poll].events = POLLIN; 39386470930SIngo Molnar nr_poll++; 39486470930SIngo Molnar 395d6d901c2SZhang, Yanmin mmap_array[nr_cpu][counter][thread_index].counter = counter; 396d6d901c2SZhang, Yanmin mmap_array[nr_cpu][counter][thread_index].prev = 0; 397d6d901c2SZhang, Yanmin mmap_array[nr_cpu][counter][thread_index].mask = mmap_pages*page_size - 1; 398d6d901c2SZhang, Yanmin mmap_array[nr_cpu][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size, 399d6d901c2SZhang, Yanmin PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0); 400d6d901c2SZhang, Yanmin if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) { 40186470930SIngo Molnar error("failed to mmap with %d (%s)\n", errno, strerror(errno)); 40286470930SIngo Molnar exit(-1); 40386470930SIngo Molnar } 404ea57c4f5SIngo Molnar } 405d1302522SFrederic Weisbecker 406c171b552SLi Zefan if (filter != NULL) { 407d6d901c2SZhang, Yanmin ret = ioctl(fd[nr_cpu][counter][thread_index], 408c171b552SLi Zefan PERF_EVENT_IOC_SET_FILTER, filter); 409c171b552SLi Zefan if (ret) { 410c171b552SLi Zefan error("failed to set filter with %d (%s)\n", errno, 411c171b552SLi Zefan strerror(errno)); 412c171b552SLi Zefan exit(-1); 413c171b552SLi Zefan } 414c171b552SLi Zefan } 41586470930SIngo Molnar } 416d6d901c2SZhang, Yanmin } 41786470930SIngo Molnar 418d6d901c2SZhang, Yanmin static void open_counters(int cpu) 41986470930SIngo Molnar { 42086470930SIngo Molnar int counter; 42186470930SIngo Molnar 42286470930SIngo Molnar group_fd = -1; 42386470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) 424d6d901c2SZhang, Yanmin create_counter(counter, cpu); 42586470930SIngo Molnar 42686470930SIngo Molnar nr_cpu++; 42786470930SIngo Molnar } 42886470930SIngo Molnar 4296122e4e4SArnaldo Carvalho de Melo static int process_buildids(void) 4306122e4e4SArnaldo Carvalho de Melo { 4316122e4e4SArnaldo Carvalho de Melo u64 size = lseek(output, 0, SEEK_CUR); 4326122e4e4SArnaldo Carvalho de Melo 4339f591fd7SArnaldo Carvalho de Melo if (size == 0) 4349f591fd7SArnaldo Carvalho de Melo return 0; 4359f591fd7SArnaldo Carvalho de Melo 4366122e4e4SArnaldo Carvalho de Melo session->fd = output; 4376122e4e4SArnaldo Carvalho de Melo return __perf_session__process_events(session, post_processing_offset, 4386122e4e4SArnaldo Carvalho de Melo size - post_processing_offset, 4396122e4e4SArnaldo Carvalho de Melo size, &build_id__mark_dso_hit_ops); 4406122e4e4SArnaldo Carvalho de Melo } 4416122e4e4SArnaldo Carvalho de Melo 442f5970550SPeter Zijlstra static void atexit_header(void) 443f5970550SPeter Zijlstra { 444c7929e47STom Zanussi if (!pipe_output) { 44594c744b6SArnaldo Carvalho de Melo session->header.data_size += bytes_written; 446f5970550SPeter Zijlstra 4476122e4e4SArnaldo Carvalho de Melo process_buildids(); 44894c744b6SArnaldo Carvalho de Melo perf_header__write(&session->header, output, true); 449c7929e47STom Zanussi } 450f5970550SPeter Zijlstra } 451f5970550SPeter Zijlstra 45223346f21SArnaldo Carvalho de Melo static void event__synthesize_guest_os(struct machine *machine, void *data) 453a1645ce1SZhang, Yanmin { 454a1645ce1SZhang, Yanmin int err; 455a1645ce1SZhang, Yanmin char *guest_kallsyms; 456a1645ce1SZhang, Yanmin char path[PATH_MAX]; 45723346f21SArnaldo Carvalho de Melo struct perf_session *psession = data; 458a1645ce1SZhang, Yanmin 45923346f21SArnaldo Carvalho de Melo if (machine__is_host(machine)) 460a1645ce1SZhang, Yanmin return; 461a1645ce1SZhang, Yanmin 462a1645ce1SZhang, Yanmin /* 463a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 464a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 465a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 466a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 467a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 468a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 469a1645ce1SZhang, Yanmin */ 470a1645ce1SZhang, Yanmin err = event__synthesize_modules(process_synthesized_event, 47123346f21SArnaldo Carvalho de Melo psession, machine); 472a1645ce1SZhang, Yanmin if (err < 0) 473a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 47423346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 475a1645ce1SZhang, Yanmin 47623346f21SArnaldo Carvalho de Melo if (machine__is_default_guest(machine)) 477a1645ce1SZhang, Yanmin guest_kallsyms = (char *) symbol_conf.default_guest_kallsyms; 478a1645ce1SZhang, Yanmin else { 47923346f21SArnaldo Carvalho de Melo sprintf(path, "%s/proc/kallsyms", machine->root_dir); 480a1645ce1SZhang, Yanmin guest_kallsyms = path; 481a1645ce1SZhang, Yanmin } 482a1645ce1SZhang, Yanmin 483a1645ce1SZhang, Yanmin /* 484a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 485a1645ce1SZhang, Yanmin * have no _text sometimes. 486a1645ce1SZhang, Yanmin */ 487a1645ce1SZhang, Yanmin err = event__synthesize_kernel_mmap(process_synthesized_event, 48823346f21SArnaldo Carvalho de Melo psession, machine, "_text"); 489a1645ce1SZhang, Yanmin if (err < 0) 490a1645ce1SZhang, Yanmin err = event__synthesize_kernel_mmap(process_synthesized_event, 49123346f21SArnaldo Carvalho de Melo psession, machine, "_stext"); 492a1645ce1SZhang, Yanmin if (err < 0) 493a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 49423346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 495a1645ce1SZhang, Yanmin } 496a1645ce1SZhang, Yanmin 49798402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 49898402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 49998402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 50098402807SFrederic Weisbecker }; 50198402807SFrederic Weisbecker 50298402807SFrederic Weisbecker static void mmap_read_all(void) 50398402807SFrederic Weisbecker { 50498402807SFrederic Weisbecker int i, counter, thread; 50598402807SFrederic Weisbecker 50698402807SFrederic Weisbecker for (i = 0; i < nr_cpu; i++) { 50798402807SFrederic Weisbecker for (counter = 0; counter < nr_counters; counter++) { 50898402807SFrederic Weisbecker for (thread = 0; thread < thread_num; thread++) { 50998402807SFrederic Weisbecker if (mmap_array[i][counter][thread].base) 51098402807SFrederic Weisbecker mmap_read(&mmap_array[i][counter][thread]); 51198402807SFrederic Weisbecker } 51298402807SFrederic Weisbecker 51398402807SFrederic Weisbecker } 51498402807SFrederic Weisbecker } 51598402807SFrederic Weisbecker 51698402807SFrederic Weisbecker if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO)) 51798402807SFrederic Weisbecker write_output(&finished_round_event, sizeof(finished_round_event)); 51898402807SFrederic Weisbecker } 51998402807SFrederic Weisbecker 520d4db3f16SArnaldo Carvalho de Melo static int __cmd_record(int argc, const char **argv) 52186470930SIngo Molnar { 52286470930SIngo Molnar int i, counter; 52386470930SIngo Molnar struct stat st; 5247c6a1c65SPeter Zijlstra pid_t pid = 0; 52586470930SIngo Molnar int flags; 5264dc0a04bSArnaldo Carvalho de Melo int err; 5278b412664SPeter Zijlstra unsigned long waking = 0; 528856e9660SPeter Zijlstra int child_ready_pipe[2], go_pipe[2]; 52946be604bSZhang, Yanmin const bool forks = argc > 0; 530856e9660SPeter Zijlstra char buf; 53123346f21SArnaldo Carvalho de Melo struct machine *machine; 53286470930SIngo Molnar 53386470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 53486470930SIngo Molnar 535f5970550SPeter Zijlstra atexit(sig_atexit); 536f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 537f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 538f5970550SPeter Zijlstra 539d4db3f16SArnaldo Carvalho de Melo if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { 540856e9660SPeter Zijlstra perror("failed to create pipes"); 541856e9660SPeter Zijlstra exit(-1); 542856e9660SPeter Zijlstra } 543856e9660SPeter Zijlstra 544529870e3STom Zanussi if (!strcmp(output_name, "-")) 545529870e3STom Zanussi pipe_output = 1; 546529870e3STom Zanussi else if (!stat(output_name, &st) && st.st_size) { 5477865e817SFrederic Weisbecker if (write_mode == WRITE_FORCE) { 548b38d3464SArnaldo Carvalho de Melo char oldname[PATH_MAX]; 549b38d3464SArnaldo Carvalho de Melo snprintf(oldname, sizeof(oldname), "%s.old", 550b38d3464SArnaldo Carvalho de Melo output_name); 551b38d3464SArnaldo Carvalho de Melo unlink(oldname); 552b38d3464SArnaldo Carvalho de Melo rename(output_name, oldname); 553b38d3464SArnaldo Carvalho de Melo } 5547865e817SFrederic Weisbecker } else if (write_mode == WRITE_APPEND) { 5557865e817SFrederic Weisbecker write_mode = WRITE_FORCE; 556266e0e21SPierre Habouzit } 55786470930SIngo Molnar 558f887f301SXiao Guangrong flags = O_CREAT|O_RDWR; 5597865e817SFrederic Weisbecker if (write_mode == WRITE_APPEND) 560f5970550SPeter Zijlstra file_new = 0; 56186470930SIngo Molnar else 56286470930SIngo Molnar flags |= O_TRUNC; 56386470930SIngo Molnar 564529870e3STom Zanussi if (pipe_output) 565529870e3STom Zanussi output = STDOUT_FILENO; 566529870e3STom Zanussi else 56786470930SIngo Molnar output = open(output_name, flags, S_IRUSR | S_IWUSR); 56886470930SIngo Molnar if (output < 0) { 56986470930SIngo Molnar perror("failed to create output file"); 57086470930SIngo Molnar exit(-1); 57186470930SIngo Molnar } 57286470930SIngo Molnar 5737865e817SFrederic Weisbecker session = perf_session__new(output_name, O_WRONLY, 574454c407eSTom Zanussi write_mode == WRITE_FORCE, false); 57594c744b6SArnaldo Carvalho de Melo if (session == NULL) { 576a9a70bbcSArnaldo Carvalho de Melo pr_err("Not enough memory for reading perf file header\n"); 577a9a70bbcSArnaldo Carvalho de Melo return -1; 578a9a70bbcSArnaldo Carvalho de Melo } 579a9a70bbcSArnaldo Carvalho de Melo 5804dc0a04bSArnaldo Carvalho de Melo if (!file_new) { 5818dc58101STom Zanussi err = perf_header__read(session, output); 5824dc0a04bSArnaldo Carvalho de Melo if (err < 0) 5834dc0a04bSArnaldo Carvalho de Melo return err; 5844dc0a04bSArnaldo Carvalho de Melo } 5854dc0a04bSArnaldo Carvalho de Melo 586db620b1cSTom Zanussi if (have_tracepoints(attrs, nr_counters)) 58794c744b6SArnaldo Carvalho de Melo perf_header__set_feat(&session->header, HEADER_TRACE_INFO); 58803456a15SFrederic Weisbecker 589f5970550SPeter Zijlstra atexit(atexit_header); 590f5970550SPeter Zijlstra 591d4db3f16SArnaldo Carvalho de Melo if (forks) { 59246be604bSZhang, Yanmin child_pid = fork(); 593856e9660SPeter Zijlstra if (pid < 0) { 594856e9660SPeter Zijlstra perror("failed to fork"); 595856e9660SPeter Zijlstra exit(-1); 596856e9660SPeter Zijlstra } 5977c6a1c65SPeter Zijlstra 59846be604bSZhang, Yanmin if (!child_pid) { 599529870e3STom Zanussi if (pipe_output) 600529870e3STom Zanussi dup2(2, 1); 601856e9660SPeter Zijlstra close(child_ready_pipe[0]); 602856e9660SPeter Zijlstra close(go_pipe[1]); 603856e9660SPeter Zijlstra fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 604856e9660SPeter Zijlstra 605856e9660SPeter Zijlstra /* 606856e9660SPeter Zijlstra * Do a dummy execvp to get the PLT entry resolved, 607856e9660SPeter Zijlstra * so we avoid the resolver overhead on the real 608856e9660SPeter Zijlstra * execvp call. 609856e9660SPeter Zijlstra */ 610856e9660SPeter Zijlstra execvp("", (char **)argv); 611856e9660SPeter Zijlstra 612856e9660SPeter Zijlstra /* 613856e9660SPeter Zijlstra * Tell the parent we're ready to go 614856e9660SPeter Zijlstra */ 615856e9660SPeter Zijlstra close(child_ready_pipe[1]); 616856e9660SPeter Zijlstra 617856e9660SPeter Zijlstra /* 618856e9660SPeter Zijlstra * Wait until the parent tells us to go. 619856e9660SPeter Zijlstra */ 620856e9660SPeter Zijlstra if (read(go_pipe[0], &buf, 1) == -1) 621856e9660SPeter Zijlstra perror("unable to read pipe"); 622856e9660SPeter Zijlstra 623856e9660SPeter Zijlstra execvp(argv[0], (char **)argv); 624856e9660SPeter Zijlstra 625856e9660SPeter Zijlstra perror(argv[0]); 626856e9660SPeter Zijlstra exit(-1); 627856e9660SPeter Zijlstra } 628856e9660SPeter Zijlstra 629d6d901c2SZhang, Yanmin if (!system_wide && target_tid == -1 && target_pid == -1) 630d6d901c2SZhang, Yanmin all_tids[0] = child_pid; 631d6d901c2SZhang, Yanmin 632856e9660SPeter Zijlstra close(child_ready_pipe[1]); 633856e9660SPeter Zijlstra close(go_pipe[0]); 634856e9660SPeter Zijlstra /* 635856e9660SPeter Zijlstra * wait for child to settle 636856e9660SPeter Zijlstra */ 637856e9660SPeter Zijlstra if (read(child_ready_pipe[0], &buf, 1) == -1) { 638856e9660SPeter Zijlstra perror("unable to read pipe"); 639856e9660SPeter Zijlstra exit(-1); 640856e9660SPeter Zijlstra } 641856e9660SPeter Zijlstra close(child_ready_pipe[0]); 642856e9660SPeter Zijlstra } 643856e9660SPeter Zijlstra 6442e6cdf99SStephane Eranian if ((!system_wide && no_inherit) || profile_cpu != -1) { 645d6d901c2SZhang, Yanmin open_counters(profile_cpu); 6460a5ac846SJens Axboe } else { 647a12b51c4SPaul Mackerras nr_cpus = read_cpu_map(); 6480a5ac846SJens Axboe for (i = 0; i < nr_cpus; i++) 649d6d901c2SZhang, Yanmin open_counters(cpumap[i]); 6500a5ac846SJens Axboe } 65186470930SIngo Molnar 652529870e3STom Zanussi if (pipe_output) { 653529870e3STom Zanussi err = perf_header__write_pipe(output); 654529870e3STom Zanussi if (err < 0) 655529870e3STom Zanussi return err; 656529870e3STom Zanussi } else if (file_new) { 65794c744b6SArnaldo Carvalho de Melo err = perf_header__write(&session->header, output, false); 658d5eed904SArnaldo Carvalho de Melo if (err < 0) 659d5eed904SArnaldo Carvalho de Melo return err; 660d5eed904SArnaldo Carvalho de Melo } 6617c6a1c65SPeter Zijlstra 6626122e4e4SArnaldo Carvalho de Melo post_processing_offset = lseek(output, 0, SEEK_CUR); 6636122e4e4SArnaldo Carvalho de Melo 6642c46dbb5STom Zanussi if (pipe_output) { 6652c46dbb5STom Zanussi err = event__synthesize_attrs(&session->header, 6662c46dbb5STom Zanussi process_synthesized_event, 6672c46dbb5STom Zanussi session); 6682c46dbb5STom Zanussi if (err < 0) { 6692c46dbb5STom Zanussi pr_err("Couldn't synthesize attrs.\n"); 6702c46dbb5STom Zanussi return err; 6712c46dbb5STom Zanussi } 672cd19a035STom Zanussi 673cd19a035STom Zanussi err = event__synthesize_event_types(process_synthesized_event, 674cd19a035STom Zanussi session); 675cd19a035STom Zanussi if (err < 0) { 676cd19a035STom Zanussi pr_err("Couldn't synthesize event_types.\n"); 677cd19a035STom Zanussi return err; 678cd19a035STom Zanussi } 6799215545eSTom Zanussi 68063e0c771STom Zanussi if (have_tracepoints(attrs, nr_counters)) { 68163e0c771STom Zanussi /* 68263e0c771STom Zanussi * FIXME err <= 0 here actually means that 68363e0c771STom Zanussi * there were no tracepoints so its not really 68463e0c771STom Zanussi * an error, just that we don't need to 68563e0c771STom Zanussi * synthesize anything. We really have to 68663e0c771STom Zanussi * return this more properly and also 68763e0c771STom Zanussi * propagate errors that now are calling die() 68863e0c771STom Zanussi */ 6899215545eSTom Zanussi err = event__synthesize_tracing_data(output, attrs, 6909215545eSTom Zanussi nr_counters, 6919215545eSTom Zanussi process_synthesized_event, 6929215545eSTom Zanussi session); 69363e0c771STom Zanussi if (err <= 0) { 69463e0c771STom Zanussi pr_err("Couldn't record tracing data.\n"); 69563e0c771STom Zanussi return err; 69663e0c771STom Zanussi } 6979215545eSTom Zanussi advance_output(err); 6982c46dbb5STom Zanussi } 69963e0c771STom Zanussi } 7002c46dbb5STom Zanussi 70123346f21SArnaldo Carvalho de Melo machine = perf_session__find_host_machine(session); 70223346f21SArnaldo Carvalho de Melo if (!machine) { 703a1645ce1SZhang, Yanmin pr_err("Couldn't find native kernel information.\n"); 704a1645ce1SZhang, Yanmin return -1; 705a1645ce1SZhang, Yanmin } 706a1645ce1SZhang, Yanmin 70756b03f3cSArnaldo Carvalho de Melo err = event__synthesize_kernel_mmap(process_synthesized_event, 70823346f21SArnaldo Carvalho de Melo session, machine, "_text"); 70970162138SArnaldo Carvalho de Melo if (err < 0) 71070162138SArnaldo Carvalho de Melo err = event__synthesize_kernel_mmap(process_synthesized_event, 71123346f21SArnaldo Carvalho de Melo session, machine, "_stext"); 71256b03f3cSArnaldo Carvalho de Melo if (err < 0) { 71356b03f3cSArnaldo Carvalho de Melo pr_err("Couldn't record kernel reference relocation symbol.\n"); 71456b03f3cSArnaldo Carvalho de Melo return err; 71556b03f3cSArnaldo Carvalho de Melo } 71656b03f3cSArnaldo Carvalho de Melo 717a1645ce1SZhang, Yanmin err = event__synthesize_modules(process_synthesized_event, 71823346f21SArnaldo Carvalho de Melo session, machine); 719b7cece76SArnaldo Carvalho de Melo if (err < 0) { 720b7cece76SArnaldo Carvalho de Melo pr_err("Couldn't record kernel reference relocation symbol.\n"); 721b7cece76SArnaldo Carvalho de Melo return err; 722b7cece76SArnaldo Carvalho de Melo } 723a1645ce1SZhang, Yanmin if (perf_guest) 72423346f21SArnaldo Carvalho de Melo perf_session__process_machines(session, event__synthesize_guest_os); 725b7cece76SArnaldo Carvalho de Melo 726d4db3f16SArnaldo Carvalho de Melo if (!system_wide && profile_cpu == -1) 727d6d901c2SZhang, Yanmin event__synthesize_thread(target_tid, process_synthesized_event, 728d8f66248SArnaldo Carvalho de Melo session); 729234fbbf5SArnaldo Carvalho de Melo else 730d8f66248SArnaldo Carvalho de Melo event__synthesize_threads(process_synthesized_event, session); 7317c6a1c65SPeter Zijlstra 73286470930SIngo Molnar if (realtime_prio) { 73386470930SIngo Molnar struct sched_param param; 73486470930SIngo Molnar 73586470930SIngo Molnar param.sched_priority = realtime_prio; 73686470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 7376beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 73886470930SIngo Molnar exit(-1); 73986470930SIngo Molnar } 74086470930SIngo Molnar } 74186470930SIngo Molnar 742856e9660SPeter Zijlstra /* 743856e9660SPeter Zijlstra * Let the child rip 744856e9660SPeter Zijlstra */ 745d4db3f16SArnaldo Carvalho de Melo if (forks) 746856e9660SPeter Zijlstra close(go_pipe[1]); 747856e9660SPeter Zijlstra 748649c48a9SPeter Zijlstra for (;;) { 74986470930SIngo Molnar int hits = samples; 750d6d901c2SZhang, Yanmin int thread; 75186470930SIngo Molnar 75298402807SFrederic Weisbecker mmap_read_all(); 75386470930SIngo Molnar 754649c48a9SPeter Zijlstra if (hits == samples) { 755649c48a9SPeter Zijlstra if (done) 756649c48a9SPeter Zijlstra break; 7574dc0a04bSArnaldo Carvalho de Melo err = poll(event_array, nr_poll, -1); 7588b412664SPeter Zijlstra waking++; 7598b412664SPeter Zijlstra } 7608b412664SPeter Zijlstra 7618b412664SPeter Zijlstra if (done) { 7628b412664SPeter Zijlstra for (i = 0; i < nr_cpu; i++) { 763d6d901c2SZhang, Yanmin for (counter = 0; 764d6d901c2SZhang, Yanmin counter < nr_counters; 765d6d901c2SZhang, Yanmin counter++) { 766d6d901c2SZhang, Yanmin for (thread = 0; 767d6d901c2SZhang, Yanmin thread < thread_num; 768d6d901c2SZhang, Yanmin thread++) 769d6d901c2SZhang, Yanmin ioctl(fd[i][counter][thread], 770d6d901c2SZhang, Yanmin PERF_EVENT_IOC_DISABLE); 771d6d901c2SZhang, Yanmin } 77286470930SIngo Molnar } 773649c48a9SPeter Zijlstra } 7748b412664SPeter Zijlstra } 7758b412664SPeter Zijlstra 7768b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 77786470930SIngo Molnar 77886470930SIngo Molnar /* 77986470930SIngo Molnar * Approximate RIP event size: 24 bytes. 78086470930SIngo Molnar */ 78186470930SIngo Molnar fprintf(stderr, 78286470930SIngo Molnar "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", 78386470930SIngo Molnar (double)bytes_written / 1024.0 / 1024.0, 78486470930SIngo Molnar output_name, 78586470930SIngo Molnar bytes_written / 24); 78686470930SIngo Molnar 78786470930SIngo Molnar return 0; 78886470930SIngo Molnar } 78986470930SIngo Molnar 79086470930SIngo Molnar static const char * const record_usage[] = { 79186470930SIngo Molnar "perf record [<options>] [<command>]", 79286470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 79386470930SIngo Molnar NULL 79486470930SIngo Molnar }; 79586470930SIngo Molnar 7967865e817SFrederic Weisbecker static bool force, append_file; 7977865e817SFrederic Weisbecker 79886470930SIngo Molnar static const struct option options[] = { 79986470930SIngo Molnar OPT_CALLBACK('e', "event", NULL, "event", 80086470930SIngo Molnar "event selector. use 'perf list' to list available events", 80186470930SIngo Molnar parse_events), 802c171b552SLi Zefan OPT_CALLBACK(0, "filter", NULL, "filter", 803c171b552SLi Zefan "event filter", parse_filter), 80486470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 805d6d901c2SZhang, Yanmin "record events on existing process id"), 806d6d901c2SZhang, Yanmin OPT_INTEGER('t', "tid", &target_tid, 807d6d901c2SZhang, Yanmin "record events on existing thread id"), 80886470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 80986470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 810daac07b2SFrederic Weisbecker OPT_BOOLEAN('R', "raw-samples", &raw_samples, 811daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 81286470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 81386470930SIngo Molnar "system-wide collection from all CPUs"), 81486470930SIngo Molnar OPT_BOOLEAN('A', "append", &append_file, 81586470930SIngo Molnar "append to the output file to do incremental profiling"), 8160a5ac846SJens Axboe OPT_INTEGER('C', "profile_cpu", &profile_cpu, 8170a5ac846SJens Axboe "CPU to profile on"), 81886470930SIngo Molnar OPT_BOOLEAN('f', "force", &force, 8197865e817SFrederic Weisbecker "overwrite existing data file (deprecated)"), 820*3de29cabSStephane Eranian OPT_U64('c', "count", &user_interval, "event period to sample"), 82186470930SIngo Molnar OPT_STRING('o', "output", &output_name, "file", 82286470930SIngo Molnar "output file name"), 8232e6cdf99SStephane Eranian OPT_BOOLEAN('i', "no-inherit", &no_inherit, 8242e6cdf99SStephane Eranian "child tasks do not inherit counters"), 825f9212819SFrederic Weisbecker OPT_INTEGER('F', "freq", &user_freq, 82686470930SIngo Molnar "profile at this frequency"), 82786470930SIngo Molnar OPT_INTEGER('m', "mmap-pages", &mmap_pages, 82886470930SIngo Molnar "number of mmap data pages"), 8293efa1cc9SIngo Molnar OPT_BOOLEAN('g', "call-graph", &call_graph, 8303efa1cc9SIngo Molnar "do call-graph (stack chain/backtrace) recording"), 831c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 8323da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 833649c48a9SPeter Zijlstra OPT_BOOLEAN('s', "stat", &inherit_stat, 834649c48a9SPeter Zijlstra "per thread counts"), 8354bba828dSAnton Blanchard OPT_BOOLEAN('d', "data", &sample_address, 8364bba828dSAnton Blanchard "Sample addresses"), 837649c48a9SPeter Zijlstra OPT_BOOLEAN('n', "no-samples", &no_samples, 838649c48a9SPeter Zijlstra "don't sample"), 839d1302522SFrederic Weisbecker OPT_BOOLEAN('M', "multiplex", &multiplex, 840d1302522SFrederic Weisbecker "multiplex counter output in a single channel"), 84186470930SIngo Molnar OPT_END() 84286470930SIngo Molnar }; 84386470930SIngo Molnar 844f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used) 84586470930SIngo Molnar { 846d6d901c2SZhang, Yanmin int i,j; 84786470930SIngo Molnar 848a0541234SAnton Blanchard argc = parse_options(argc, argv, options, record_usage, 849a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 850d6d901c2SZhang, Yanmin if (!argc && target_pid == -1 && target_tid == -1 && 851d6d901c2SZhang, Yanmin !system_wide && profile_cpu == -1) 85286470930SIngo Molnar usage_with_options(record_usage, options); 85386470930SIngo Molnar 8547865e817SFrederic Weisbecker if (force && append_file) { 8557865e817SFrederic Weisbecker fprintf(stderr, "Can't overwrite and append at the same time." 8567865e817SFrederic Weisbecker " You need to choose between -f and -A"); 8577865e817SFrederic Weisbecker usage_with_options(record_usage, options); 8587865e817SFrederic Weisbecker } else if (append_file) { 8597865e817SFrederic Weisbecker write_mode = WRITE_APPEND; 8607865e817SFrederic Weisbecker } else { 8617865e817SFrederic Weisbecker write_mode = WRITE_FORCE; 8627865e817SFrederic Weisbecker } 8637865e817SFrederic Weisbecker 864655000e7SArnaldo Carvalho de Melo symbol__init(); 865655000e7SArnaldo Carvalho de Melo 866bbd36e5eSPeter Zijlstra if (!nr_counters) { 86786470930SIngo Molnar nr_counters = 1; 868bbd36e5eSPeter Zijlstra attrs[0].type = PERF_TYPE_HARDWARE; 869bbd36e5eSPeter Zijlstra attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; 870bbd36e5eSPeter Zijlstra } 87186470930SIngo Molnar 872d6d901c2SZhang, Yanmin if (target_pid != -1) { 873d6d901c2SZhang, Yanmin target_tid = target_pid; 874d6d901c2SZhang, Yanmin thread_num = find_all_tid(target_pid, &all_tids); 875d6d901c2SZhang, Yanmin if (thread_num <= 0) { 876d6d901c2SZhang, Yanmin fprintf(stderr, "Can't find all threads of pid %d\n", 877d6d901c2SZhang, Yanmin target_pid); 878d6d901c2SZhang, Yanmin usage_with_options(record_usage, options); 879d6d901c2SZhang, Yanmin } 880d6d901c2SZhang, Yanmin } else { 881d6d901c2SZhang, Yanmin all_tids=malloc(sizeof(pid_t)); 882d6d901c2SZhang, Yanmin if (!all_tids) 883d6d901c2SZhang, Yanmin return -ENOMEM; 884d6d901c2SZhang, Yanmin 885d6d901c2SZhang, Yanmin all_tids[0] = target_tid; 886d6d901c2SZhang, Yanmin thread_num = 1; 887d6d901c2SZhang, Yanmin } 888d6d901c2SZhang, Yanmin 889d6d901c2SZhang, Yanmin for (i = 0; i < MAX_NR_CPUS; i++) { 890d6d901c2SZhang, Yanmin for (j = 0; j < MAX_COUNTERS; j++) { 891d6d901c2SZhang, Yanmin fd[i][j] = malloc(sizeof(int)*thread_num); 8925a103174SZhang, Yanmin mmap_array[i][j] = zalloc( 893d6d901c2SZhang, Yanmin sizeof(struct mmap_data)*thread_num); 894d6d901c2SZhang, Yanmin if (!fd[i][j] || !mmap_array[i][j]) 895d6d901c2SZhang, Yanmin return -ENOMEM; 896d6d901c2SZhang, Yanmin } 897d6d901c2SZhang, Yanmin } 898d6d901c2SZhang, Yanmin event_array = malloc( 899d6d901c2SZhang, Yanmin sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num); 900d6d901c2SZhang, Yanmin if (!event_array) 901d6d901c2SZhang, Yanmin return -ENOMEM; 902d6d901c2SZhang, Yanmin 903*3de29cabSStephane Eranian if (user_interval != ULLONG_MAX) 904f9212819SFrederic Weisbecker default_interval = user_interval; 905f9212819SFrederic Weisbecker if (user_freq != UINT_MAX) 906f9212819SFrederic Weisbecker freq = user_freq; 907f9212819SFrederic Weisbecker 9087e4ff9e3SMike Galbraith /* 9097e4ff9e3SMike Galbraith * User specified count overrides default frequency. 9107e4ff9e3SMike Galbraith */ 9117e4ff9e3SMike Galbraith if (default_interval) 9127e4ff9e3SMike Galbraith freq = 0; 9137e4ff9e3SMike Galbraith else if (freq) { 9147e4ff9e3SMike Galbraith default_interval = freq; 9157e4ff9e3SMike Galbraith } else { 9167e4ff9e3SMike Galbraith fprintf(stderr, "frequency and count are zero, aborting\n"); 9177e4ff9e3SMike Galbraith exit(EXIT_FAILURE); 9187e4ff9e3SMike Galbraith } 9197e4ff9e3SMike Galbraith 92086470930SIngo Molnar return __cmd_record(argc, argv); 92186470930SIngo Molnar } 922