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 */ 886470930SIngo Molnar #include "builtin.h" 986470930SIngo Molnar 1086470930SIngo Molnar #include "perf.h" 1186470930SIngo Molnar 1286470930SIngo Molnar #include "util/util.h" 1386470930SIngo Molnar #include "util/parse-options.h" 1486470930SIngo Molnar #include "util/parse-events.h" 1586470930SIngo Molnar #include "util/string.h" 1686470930SIngo Molnar 177c6a1c65SPeter Zijlstra #include "util/header.h" 1866e274f3SFrederic Weisbecker #include "util/event.h" 198f28827aSFrederic Weisbecker #include "util/debug.h" 205f9c39dcSFrederic Weisbecker #include "util/trace-event.h" 217c6a1c65SPeter Zijlstra 2286470930SIngo Molnar #include <unistd.h> 2386470930SIngo Molnar #include <sched.h> 2486470930SIngo Molnar 2586470930SIngo Molnar #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) 2686470930SIngo Molnar #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) 2786470930SIngo Molnar 2886470930SIngo Molnar static int fd[MAX_NR_CPUS][MAX_COUNTERS]; 2986470930SIngo Molnar 3086470930SIngo Molnar static long default_interval = 100000; 3186470930SIngo Molnar 3286470930SIngo Molnar static int nr_cpus = 0; 3386470930SIngo Molnar static unsigned int page_size; 3486470930SIngo Molnar static unsigned int mmap_pages = 128; 3586470930SIngo Molnar static int freq = 0; 3686470930SIngo Molnar static int output; 3786470930SIngo Molnar static const char *output_name = "perf.data"; 3886470930SIngo Molnar static int group = 0; 3986470930SIngo Molnar static unsigned int realtime_prio = 0; 40daac07b2SFrederic Weisbecker static int raw_samples = 0; 4186470930SIngo Molnar static int system_wide = 0; 420a5ac846SJens Axboe static int profile_cpu = -1; 4386470930SIngo Molnar static pid_t target_pid = -1; 4486470930SIngo Molnar static int inherit = 1; 4586470930SIngo Molnar static int force = 0; 4686470930SIngo Molnar static int append_file = 0; 473efa1cc9SIngo Molnar static int call_graph = 0; 48649c48a9SPeter Zijlstra static int inherit_stat = 0; 49649c48a9SPeter Zijlstra static int no_samples = 0; 504bba828dSAnton Blanchard static int sample_address = 0; 51*d1302522SFrederic Weisbecker static int multiplex = 0; 5286470930SIngo Molnar 5386470930SIngo Molnar static long samples; 5486470930SIngo Molnar static struct timeval last_read; 5586470930SIngo Molnar static struct timeval this_read; 5686470930SIngo Molnar 579cffa8d5SPaul Mackerras static u64 bytes_written; 5886470930SIngo Molnar 5986470930SIngo Molnar static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; 6086470930SIngo Molnar 6186470930SIngo Molnar static int nr_poll; 6286470930SIngo Molnar static int nr_cpu; 6386470930SIngo Molnar 64f5970550SPeter Zijlstra static int file_new = 1; 657c6a1c65SPeter Zijlstra 667c6a1c65SPeter Zijlstra struct perf_header *header; 67f5970550SPeter Zijlstra 6886470930SIngo Molnar struct mmap_data { 6986470930SIngo Molnar int counter; 7086470930SIngo Molnar void *base; 7186470930SIngo Molnar unsigned int mask; 7286470930SIngo Molnar unsigned int prev; 7386470930SIngo Molnar }; 7486470930SIngo Molnar 7586470930SIngo Molnar static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; 7686470930SIngo Molnar 779d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md) 7886470930SIngo Molnar { 7986470930SIngo Molnar struct perf_counter_mmap_page *pc = md->base; 809d91a6f7SPeter Zijlstra long head; 8186470930SIngo Molnar 8286470930SIngo Molnar head = pc->data_head; 8386470930SIngo Molnar rmb(); 8486470930SIngo Molnar 8586470930SIngo Molnar return head; 8686470930SIngo Molnar } 8786470930SIngo Molnar 889d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail) 899d91a6f7SPeter Zijlstra { 909d91a6f7SPeter Zijlstra struct perf_counter_mmap_page *pc = md->base; 919d91a6f7SPeter Zijlstra 929d91a6f7SPeter Zijlstra /* 939d91a6f7SPeter Zijlstra * ensure all reads are done before we write the tail out. 949d91a6f7SPeter Zijlstra */ 959d91a6f7SPeter Zijlstra /* mb(); */ 969d91a6f7SPeter Zijlstra pc->data_tail = tail; 979d91a6f7SPeter Zijlstra } 989d91a6f7SPeter Zijlstra 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 11486470930SIngo Molnar static void mmap_read(struct mmap_data *md) 11586470930SIngo Molnar { 11686470930SIngo Molnar unsigned int head = mmap_read_head(md); 11786470930SIngo Molnar unsigned int old = md->prev; 11886470930SIngo Molnar unsigned char *data = md->base + page_size; 11986470930SIngo Molnar unsigned long size; 12086470930SIngo Molnar void *buf; 12186470930SIngo Molnar int diff; 12286470930SIngo Molnar 12386470930SIngo Molnar gettimeofday(&this_read, NULL); 12486470930SIngo Molnar 12586470930SIngo Molnar /* 12686470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 12786470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 12886470930SIngo Molnar * 12986470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 13086470930SIngo Molnar * 13186470930SIngo Molnar * In either case, truncate and restart at head. 13286470930SIngo Molnar */ 13386470930SIngo Molnar diff = head - old; 1349d91a6f7SPeter Zijlstra if (diff < 0) { 13586470930SIngo Molnar struct timeval iv; 13686470930SIngo Molnar unsigned long msecs; 13786470930SIngo Molnar 13886470930SIngo Molnar timersub(&this_read, &last_read, &iv); 13986470930SIngo Molnar msecs = iv.tv_sec*1000 + iv.tv_usec/1000; 14086470930SIngo Molnar 14186470930SIngo Molnar fprintf(stderr, "WARNING: failed to keep up with mmap data." 14286470930SIngo Molnar " Last read %lu msecs ago.\n", msecs); 14386470930SIngo Molnar 14486470930SIngo Molnar /* 14586470930SIngo Molnar * head points to a known good entry, start there. 14686470930SIngo Molnar */ 14786470930SIngo Molnar old = head; 14886470930SIngo Molnar } 14986470930SIngo Molnar 15086470930SIngo Molnar last_read = this_read; 15186470930SIngo Molnar 15286470930SIngo Molnar if (old != head) 15386470930SIngo Molnar samples++; 15486470930SIngo Molnar 15586470930SIngo Molnar size = head - old; 15686470930SIngo Molnar 15786470930SIngo Molnar if ((old & md->mask) + size != (head & md->mask)) { 15886470930SIngo Molnar buf = &data[old & md->mask]; 15986470930SIngo Molnar size = md->mask + 1 - (old & md->mask); 16086470930SIngo Molnar old += size; 16186470930SIngo Molnar 162f5970550SPeter Zijlstra write_output(buf, size); 16386470930SIngo Molnar } 16486470930SIngo Molnar 16586470930SIngo Molnar buf = &data[old & md->mask]; 16686470930SIngo Molnar size = head - old; 16786470930SIngo Molnar old += size; 16886470930SIngo Molnar 169f5970550SPeter Zijlstra write_output(buf, size); 17086470930SIngo Molnar 17186470930SIngo Molnar md->prev = old; 1729d91a6f7SPeter Zijlstra mmap_write_tail(md, old); 17386470930SIngo Molnar } 17486470930SIngo Molnar 17586470930SIngo Molnar static volatile int done = 0; 176f7b7c26eSPeter Zijlstra static volatile int signr = -1; 17786470930SIngo Molnar 17886470930SIngo Molnar static void sig_handler(int sig) 17986470930SIngo Molnar { 18086470930SIngo Molnar done = 1; 181f7b7c26eSPeter Zijlstra signr = sig; 182f7b7c26eSPeter Zijlstra } 183f7b7c26eSPeter Zijlstra 184f7b7c26eSPeter Zijlstra static void sig_atexit(void) 185f7b7c26eSPeter Zijlstra { 186f7b7c26eSPeter Zijlstra if (signr == -1) 187f7b7c26eSPeter Zijlstra return; 188f7b7c26eSPeter Zijlstra 189f7b7c26eSPeter Zijlstra signal(signr, SIG_DFL); 190f7b7c26eSPeter Zijlstra kill(getpid(), signr); 19186470930SIngo Molnar } 19286470930SIngo Molnar 1932a8083f0SArnaldo Carvalho de Melo static pid_t pid_synthesize_comm_event(pid_t pid, int full) 19486470930SIngo Molnar { 19586470930SIngo Molnar struct comm_event comm_ev; 19686470930SIngo Molnar char filename[PATH_MAX]; 19786470930SIngo Molnar char bf[BUFSIZ]; 1982a8083f0SArnaldo Carvalho de Melo FILE *fp; 1992a8083f0SArnaldo Carvalho de Melo size_t size = 0; 20086470930SIngo Molnar DIR *tasks; 20186470930SIngo Molnar struct dirent dirent, *next; 2022a8083f0SArnaldo Carvalho de Melo pid_t tgid = 0; 20386470930SIngo Molnar 2042a8083f0SArnaldo Carvalho de Melo snprintf(filename, sizeof(filename), "/proc/%d/status", pid); 20586470930SIngo Molnar 2062a8083f0SArnaldo Carvalho de Melo fp = fopen(filename, "r"); 20739e6dd73SArnaldo Carvalho de Melo if (fp == NULL) { 208613d8602SIngo Molnar /* 209613d8602SIngo Molnar * We raced with a task exiting - just return: 210613d8602SIngo Molnar */ 211613d8602SIngo Molnar if (verbose) 21286470930SIngo Molnar fprintf(stderr, "couldn't open %s\n", filename); 2132a8083f0SArnaldo Carvalho de Melo return 0; 21486470930SIngo Molnar } 21586470930SIngo Molnar 21686470930SIngo Molnar memset(&comm_ev, 0, sizeof(comm_ev)); 2172a8083f0SArnaldo Carvalho de Melo while (!comm_ev.comm[0] || !comm_ev.pid) { 2182a8083f0SArnaldo Carvalho de Melo if (fgets(bf, sizeof(bf), fp) == NULL) 21986470930SIngo Molnar goto out_failure; 22086470930SIngo Molnar 2212a8083f0SArnaldo Carvalho de Melo if (memcmp(bf, "Name:", 5) == 0) { 2222a8083f0SArnaldo Carvalho de Melo char *name = bf + 5; 2232a8083f0SArnaldo Carvalho de Melo while (*name && isspace(*name)) 2242a8083f0SArnaldo Carvalho de Melo ++name; 2252a8083f0SArnaldo Carvalho de Melo size = strlen(name) - 1; 2262a8083f0SArnaldo Carvalho de Melo memcpy(comm_ev.comm, name, size++); 2272a8083f0SArnaldo Carvalho de Melo } else if (memcmp(bf, "Tgid:", 5) == 0) { 2282a8083f0SArnaldo Carvalho de Melo char *tgids = bf + 5; 2292a8083f0SArnaldo Carvalho de Melo while (*tgids && isspace(*tgids)) 2302a8083f0SArnaldo Carvalho de Melo ++tgids; 2312a8083f0SArnaldo Carvalho de Melo tgid = comm_ev.pid = atoi(tgids); 2322a8083f0SArnaldo Carvalho de Melo } 2332a8083f0SArnaldo Carvalho de Melo } 2342a8083f0SArnaldo Carvalho de Melo 23586470930SIngo Molnar comm_ev.header.type = PERF_EVENT_COMM; 2369cffa8d5SPaul Mackerras size = ALIGN(size, sizeof(u64)); 23786470930SIngo Molnar comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size); 23886470930SIngo Molnar 23986470930SIngo Molnar if (!full) { 24086470930SIngo Molnar comm_ev.tid = pid; 24186470930SIngo Molnar 242f5970550SPeter Zijlstra write_output(&comm_ev, comm_ev.header.size); 2432a8083f0SArnaldo Carvalho de Melo goto out_fclose; 24486470930SIngo Molnar } 24586470930SIngo Molnar 24686470930SIngo Molnar snprintf(filename, sizeof(filename), "/proc/%d/task", pid); 24786470930SIngo Molnar 24886470930SIngo Molnar tasks = opendir(filename); 24986470930SIngo Molnar while (!readdir_r(tasks, &dirent, &next) && next) { 25086470930SIngo Molnar char *end; 25186470930SIngo Molnar pid = strtol(dirent.d_name, &end, 10); 25286470930SIngo Molnar if (*end) 25386470930SIngo Molnar continue; 25486470930SIngo Molnar 25586470930SIngo Molnar comm_ev.tid = pid; 25686470930SIngo Molnar 257f5970550SPeter Zijlstra write_output(&comm_ev, comm_ev.header.size); 25886470930SIngo Molnar } 25986470930SIngo Molnar closedir(tasks); 2602a8083f0SArnaldo Carvalho de Melo 2612a8083f0SArnaldo Carvalho de Melo out_fclose: 2622a8083f0SArnaldo Carvalho de Melo fclose(fp); 2632a8083f0SArnaldo Carvalho de Melo return tgid; 26486470930SIngo Molnar 26586470930SIngo Molnar out_failure: 26686470930SIngo Molnar fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n", 26786470930SIngo Molnar filename); 26886470930SIngo Molnar exit(EXIT_FAILURE); 26986470930SIngo Molnar } 27086470930SIngo Molnar 2712a8083f0SArnaldo Carvalho de Melo static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid) 27286470930SIngo Molnar { 27386470930SIngo Molnar char filename[PATH_MAX]; 27486470930SIngo Molnar FILE *fp; 27586470930SIngo Molnar 27686470930SIngo Molnar snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); 27786470930SIngo Molnar 27886470930SIngo Molnar fp = fopen(filename, "r"); 27986470930SIngo Molnar if (fp == NULL) { 280613d8602SIngo Molnar /* 281613d8602SIngo Molnar * We raced with a task exiting - just return: 282613d8602SIngo Molnar */ 283613d8602SIngo Molnar if (verbose) 28486470930SIngo Molnar fprintf(stderr, "couldn't open %s\n", filename); 285613d8602SIngo Molnar return; 28686470930SIngo Molnar } 28786470930SIngo Molnar while (1) { 28886470930SIngo Molnar char bf[BUFSIZ], *pbf = bf; 28986470930SIngo Molnar struct mmap_event mmap_ev = { 290f37a291cSIngo Molnar .header = { .type = PERF_EVENT_MMAP }, 29186470930SIngo Molnar }; 29286470930SIngo Molnar int n; 29386470930SIngo Molnar size_t size; 29486470930SIngo Molnar if (fgets(bf, sizeof(bf), fp) == NULL) 29586470930SIngo Molnar break; 29686470930SIngo Molnar 29786470930SIngo Molnar /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ 29886470930SIngo Molnar n = hex2u64(pbf, &mmap_ev.start); 29986470930SIngo Molnar if (n < 0) 30086470930SIngo Molnar continue; 30186470930SIngo Molnar pbf += n + 1; 30286470930SIngo Molnar n = hex2u64(pbf, &mmap_ev.len); 30386470930SIngo Molnar if (n < 0) 30486470930SIngo Molnar continue; 30586470930SIngo Molnar pbf += n + 3; 30686470930SIngo Molnar if (*pbf == 'x') { /* vm_exec */ 30776c64c5eSJohannes Weiner char *execname = strchr(bf, '/'); 30886470930SIngo Molnar 30911b5f81eSAnton Blanchard /* Catch VDSO */ 31011b5f81eSAnton Blanchard if (execname == NULL) 31111b5f81eSAnton Blanchard execname = strstr(bf, "[vdso]"); 31211b5f81eSAnton Blanchard 31376c64c5eSJohannes Weiner if (execname == NULL) 31486470930SIngo Molnar continue; 31586470930SIngo Molnar 31686470930SIngo Molnar size = strlen(execname); 31786470930SIngo Molnar execname[size - 1] = '\0'; /* Remove \n */ 31886470930SIngo Molnar memcpy(mmap_ev.filename, execname, size); 3199cffa8d5SPaul Mackerras size = ALIGN(size, sizeof(u64)); 32086470930SIngo Molnar mmap_ev.len -= mmap_ev.start; 32186470930SIngo Molnar mmap_ev.header.size = (sizeof(mmap_ev) - 32286470930SIngo Molnar (sizeof(mmap_ev.filename) - size)); 3232a8083f0SArnaldo Carvalho de Melo mmap_ev.pid = tgid; 32486470930SIngo Molnar mmap_ev.tid = pid; 32586470930SIngo Molnar 326f5970550SPeter Zijlstra write_output(&mmap_ev, mmap_ev.header.size); 32786470930SIngo Molnar } 32886470930SIngo Molnar } 32986470930SIngo Molnar 33086470930SIngo Molnar fclose(fp); 33186470930SIngo Molnar } 33286470930SIngo Molnar 3337c6a1c65SPeter Zijlstra static void synthesize_all(void) 33486470930SIngo Molnar { 33586470930SIngo Molnar DIR *proc; 33686470930SIngo Molnar struct dirent dirent, *next; 33786470930SIngo Molnar 33886470930SIngo Molnar proc = opendir("/proc"); 33986470930SIngo Molnar 34086470930SIngo Molnar while (!readdir_r(proc, &dirent, &next) && next) { 34186470930SIngo Molnar char *end; 3422a8083f0SArnaldo Carvalho de Melo pid_t pid, tgid; 34386470930SIngo Molnar 34486470930SIngo Molnar pid = strtol(dirent.d_name, &end, 10); 34586470930SIngo Molnar if (*end) /* only interested in proper numerical dirents */ 34686470930SIngo Molnar continue; 34786470930SIngo Molnar 3482a8083f0SArnaldo Carvalho de Melo tgid = pid_synthesize_comm_event(pid, 1); 3492a8083f0SArnaldo Carvalho de Melo pid_synthesize_mmap_samples(pid, tgid); 35086470930SIngo Molnar } 35186470930SIngo Molnar 35286470930SIngo Molnar closedir(proc); 35386470930SIngo Molnar } 35486470930SIngo Molnar 35586470930SIngo Molnar static int group_fd; 35686470930SIngo Molnar 3577c6a1c65SPeter Zijlstra static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr) 3587c6a1c65SPeter Zijlstra { 3597c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 3607c6a1c65SPeter Zijlstra 3617c6a1c65SPeter Zijlstra if (nr < header->attrs) { 3627c6a1c65SPeter Zijlstra h_attr = header->attr[nr]; 3637c6a1c65SPeter Zijlstra } else { 3647c6a1c65SPeter Zijlstra h_attr = perf_header_attr__new(a); 3657c6a1c65SPeter Zijlstra perf_header__add_attr(header, h_attr); 3667c6a1c65SPeter Zijlstra } 3677c6a1c65SPeter Zijlstra 3687c6a1c65SPeter Zijlstra return h_attr; 3697c6a1c65SPeter Zijlstra } 3707c6a1c65SPeter Zijlstra 37186470930SIngo Molnar static void create_counter(int counter, int cpu, pid_t pid) 37286470930SIngo Molnar { 37386470930SIngo Molnar struct perf_counter_attr *attr = attrs + counter; 3747c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 3757c6a1c65SPeter Zijlstra int track = !counter; /* only the first counter needs these */ 3767c6a1c65SPeter Zijlstra struct { 3777c6a1c65SPeter Zijlstra u64 count; 3787c6a1c65SPeter Zijlstra u64 time_enabled; 3797c6a1c65SPeter Zijlstra u64 time_running; 3807c6a1c65SPeter Zijlstra u64 id; 3817c6a1c65SPeter Zijlstra } read_data; 3827c6a1c65SPeter Zijlstra 3837c6a1c65SPeter Zijlstra attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | 3847c6a1c65SPeter Zijlstra PERF_FORMAT_TOTAL_TIME_RUNNING | 3857c6a1c65SPeter Zijlstra PERF_FORMAT_ID; 38686470930SIngo Molnar 3873a9f131fSFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; 3883efa1cc9SIngo Molnar 38986470930SIngo Molnar if (freq) { 390ea1900e5SPeter Zijlstra attr->sample_type |= PERF_SAMPLE_PERIOD; 39186470930SIngo Molnar attr->freq = 1; 39286470930SIngo Molnar attr->sample_freq = freq; 39386470930SIngo Molnar } 3943efa1cc9SIngo Molnar 395649c48a9SPeter Zijlstra if (no_samples) 396649c48a9SPeter Zijlstra attr->sample_freq = 0; 397649c48a9SPeter Zijlstra 398649c48a9SPeter Zijlstra if (inherit_stat) 399649c48a9SPeter Zijlstra attr->inherit_stat = 1; 400649c48a9SPeter Zijlstra 4014bba828dSAnton Blanchard if (sample_address) 4024bba828dSAnton Blanchard attr->sample_type |= PERF_SAMPLE_ADDR; 4034bba828dSAnton Blanchard 4043efa1cc9SIngo Molnar if (call_graph) 4053efa1cc9SIngo Molnar attr->sample_type |= PERF_SAMPLE_CALLCHAIN; 4063efa1cc9SIngo Molnar 407cd6feeeaSIngo Molnar if (raw_samples) { 4086ddf259dSIngo Molnar attr->sample_type |= PERF_SAMPLE_TIME; 409daac07b2SFrederic Weisbecker attr->sample_type |= PERF_SAMPLE_RAW; 410cd6feeeaSIngo Molnar attr->sample_type |= PERF_SAMPLE_CPU; 411cd6feeeaSIngo Molnar } 412f413cdb8SFrederic Weisbecker 41386470930SIngo Molnar attr->mmap = track; 41486470930SIngo Molnar attr->comm = track; 41586470930SIngo Molnar attr->inherit = (cpu < 0) && inherit; 4164502d77cSPeter Zijlstra attr->disabled = 1; 41786470930SIngo Molnar 4183da297a6SIngo Molnar try_again: 41986470930SIngo Molnar fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0); 42086470930SIngo Molnar 42186470930SIngo Molnar if (fd[nr_cpu][counter] < 0) { 42286470930SIngo Molnar int err = errno; 42386470930SIngo Molnar 42486470930SIngo Molnar if (err == EPERM) 4253da297a6SIngo Molnar die("Permission error - are you root?\n"); 4260a5ac846SJens Axboe else if (err == ENODEV && profile_cpu != -1) 4270a5ac846SJens Axboe die("No such device - did you specify an out-of-range profile CPU?\n"); 4283da297a6SIngo Molnar 4293da297a6SIngo Molnar /* 4303da297a6SIngo Molnar * If it's cycles then fall back to hrtimer 4313da297a6SIngo Molnar * based cpu-clock-tick sw counter, which 4323da297a6SIngo Molnar * is always available even if no PMU support: 4333da297a6SIngo Molnar */ 4343da297a6SIngo Molnar if (attr->type == PERF_TYPE_HARDWARE 435f4dbfa8fSPeter Zijlstra && attr->config == PERF_COUNT_HW_CPU_CYCLES) { 4363da297a6SIngo Molnar 4373da297a6SIngo Molnar if (verbose) 4383da297a6SIngo Molnar warning(" ... trying to fall back to cpu-clock-ticks\n"); 4393da297a6SIngo Molnar attr->type = PERF_TYPE_SOFTWARE; 440f4dbfa8fSPeter Zijlstra attr->config = PERF_COUNT_SW_CPU_CLOCK; 4413da297a6SIngo Molnar goto try_again; 4423da297a6SIngo Molnar } 44330c806a0SIngo Molnar printf("\n"); 44430c806a0SIngo Molnar error("perfcounter syscall returned with %d (%s)\n", 44530c806a0SIngo Molnar fd[nr_cpu][counter], strerror(err)); 44630c806a0SIngo Molnar die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n"); 44786470930SIngo Molnar exit(-1); 44886470930SIngo Molnar } 4493da297a6SIngo Molnar 4507c6a1c65SPeter Zijlstra h_attr = get_header_attr(attr, counter); 4517c6a1c65SPeter Zijlstra 4527c6a1c65SPeter Zijlstra if (!file_new) { 4537c6a1c65SPeter Zijlstra if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { 4547c6a1c65SPeter Zijlstra fprintf(stderr, "incompatible append\n"); 4557c6a1c65SPeter Zijlstra exit(-1); 4567c6a1c65SPeter Zijlstra } 4577c6a1c65SPeter Zijlstra } 4587c6a1c65SPeter Zijlstra 4593928ddbeSFrederic Weisbecker if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) { 4603928ddbeSFrederic Weisbecker perror("Unable to read perf file descriptor\n"); 4613928ddbeSFrederic Weisbecker exit(-1); 4623928ddbeSFrederic Weisbecker } 4637c6a1c65SPeter Zijlstra 4647c6a1c65SPeter Zijlstra perf_header_attr__add_id(h_attr, read_data.id); 4657c6a1c65SPeter Zijlstra 46686470930SIngo Molnar assert(fd[nr_cpu][counter] >= 0); 46786470930SIngo Molnar fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); 46886470930SIngo Molnar 46986470930SIngo Molnar /* 47086470930SIngo Molnar * First counter acts as the group leader: 47186470930SIngo Molnar */ 47286470930SIngo Molnar if (group && group_fd == -1) 47386470930SIngo Molnar group_fd = fd[nr_cpu][counter]; 47486470930SIngo Molnar 47586470930SIngo Molnar event_array[nr_poll].fd = fd[nr_cpu][counter]; 47686470930SIngo Molnar event_array[nr_poll].events = POLLIN; 47786470930SIngo Molnar nr_poll++; 47886470930SIngo Molnar 47986470930SIngo Molnar mmap_array[nr_cpu][counter].counter = counter; 48086470930SIngo Molnar mmap_array[nr_cpu][counter].prev = 0; 48186470930SIngo Molnar mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; 48286470930SIngo Molnar mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, 4839d91a6f7SPeter Zijlstra PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0); 48486470930SIngo Molnar if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { 48586470930SIngo Molnar error("failed to mmap with %d (%s)\n", errno, strerror(errno)); 48686470930SIngo Molnar exit(-1); 48786470930SIngo Molnar } 4884502d77cSPeter Zijlstra 489*d1302522SFrederic Weisbecker if (multiplex && fd[nr_cpu][counter] != group_fd) 490*d1302522SFrederic Weisbecker ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_SET_OUTPUT, group_fd); 491*d1302522SFrederic Weisbecker 4924502d77cSPeter Zijlstra ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE); 49386470930SIngo Molnar } 49486470930SIngo Molnar 49586470930SIngo Molnar static void open_counters(int cpu, pid_t pid) 49686470930SIngo Molnar { 49786470930SIngo Molnar int counter; 49886470930SIngo Molnar 49986470930SIngo Molnar group_fd = -1; 50086470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) 50186470930SIngo Molnar create_counter(counter, cpu, pid); 50286470930SIngo Molnar 50386470930SIngo Molnar nr_cpu++; 50486470930SIngo Molnar } 50586470930SIngo Molnar 506f5970550SPeter Zijlstra static void atexit_header(void) 507f5970550SPeter Zijlstra { 5087c6a1c65SPeter Zijlstra header->data_size += bytes_written; 509f5970550SPeter Zijlstra 5107c6a1c65SPeter Zijlstra perf_header__write(header, output); 511f5970550SPeter Zijlstra } 512f5970550SPeter Zijlstra 51386470930SIngo Molnar static int __cmd_record(int argc, const char **argv) 51486470930SIngo Molnar { 51586470930SIngo Molnar int i, counter; 51686470930SIngo Molnar struct stat st; 5177c6a1c65SPeter Zijlstra pid_t pid = 0; 51886470930SIngo Molnar int flags; 51986470930SIngo Molnar int ret; 52086470930SIngo Molnar 52186470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 52286470930SIngo Molnar nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); 52386470930SIngo Molnar assert(nr_cpus <= MAX_NR_CPUS); 52486470930SIngo Molnar assert(nr_cpus >= 0); 52586470930SIngo Molnar 526f5970550SPeter Zijlstra atexit(sig_atexit); 527f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 528f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 529f5970550SPeter Zijlstra 530266e0e21SPierre Habouzit if (!stat(output_name, &st) && st.st_size) { 531266e0e21SPierre Habouzit if (!force && !append_file) { 53286470930SIngo Molnar fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n", 53386470930SIngo Molnar output_name); 53486470930SIngo Molnar exit(-1); 53586470930SIngo Molnar } 536266e0e21SPierre Habouzit } else { 537266e0e21SPierre Habouzit append_file = 0; 538266e0e21SPierre Habouzit } 53986470930SIngo Molnar 54086470930SIngo Molnar flags = O_CREAT|O_RDWR; 54186470930SIngo Molnar if (append_file) 542f5970550SPeter Zijlstra file_new = 0; 54386470930SIngo Molnar else 54486470930SIngo Molnar flags |= O_TRUNC; 54586470930SIngo Molnar 54686470930SIngo Molnar output = open(output_name, flags, S_IRUSR|S_IWUSR); 54786470930SIngo Molnar if (output < 0) { 54886470930SIngo Molnar perror("failed to create output file"); 54986470930SIngo Molnar exit(-1); 55086470930SIngo Molnar } 55186470930SIngo Molnar 5527c6a1c65SPeter Zijlstra if (!file_new) 5537c6a1c65SPeter Zijlstra header = perf_header__read(output); 5547c6a1c65SPeter Zijlstra else 5557c6a1c65SPeter Zijlstra header = perf_header__new(); 556f5970550SPeter Zijlstra 5579df37dddSFrederic Weisbecker 5589df37dddSFrederic Weisbecker if (raw_samples) { 5591ef2ed10SFrederic Weisbecker read_tracing_data(attrs, nr_counters); 5609df37dddSFrederic Weisbecker } else { 5619df37dddSFrederic Weisbecker for (i = 0; i < nr_counters; i++) { 5629df37dddSFrederic Weisbecker if (attrs[i].sample_type & PERF_SAMPLE_RAW) { 5631ef2ed10SFrederic Weisbecker read_tracing_data(attrs, nr_counters); 5649df37dddSFrederic Weisbecker break; 5659df37dddSFrederic Weisbecker } 5669df37dddSFrederic Weisbecker } 5679df37dddSFrederic Weisbecker } 568f5970550SPeter Zijlstra atexit(atexit_header); 569f5970550SPeter Zijlstra 57086470930SIngo Molnar if (!system_wide) { 5717c6a1c65SPeter Zijlstra pid = target_pid; 5727c6a1c65SPeter Zijlstra if (pid == -1) 5737c6a1c65SPeter Zijlstra pid = getpid(); 5747c6a1c65SPeter Zijlstra 5750a5ac846SJens Axboe open_counters(profile_cpu, pid); 5760a5ac846SJens Axboe } else { 5770a5ac846SJens Axboe if (profile_cpu != -1) { 5780a5ac846SJens Axboe open_counters(profile_cpu, target_pid); 5790a5ac846SJens Axboe } else { 5800a5ac846SJens Axboe for (i = 0; i < nr_cpus; i++) 58186470930SIngo Molnar open_counters(i, target_pid); 5820a5ac846SJens Axboe } 5830a5ac846SJens Axboe } 58486470930SIngo Molnar 5857c6a1c65SPeter Zijlstra if (file_new) 5867c6a1c65SPeter Zijlstra perf_header__write(header, output); 5877c6a1c65SPeter Zijlstra 5887c6a1c65SPeter Zijlstra if (!system_wide) { 5892a8083f0SArnaldo Carvalho de Melo pid_t tgid = pid_synthesize_comm_event(pid, 0); 5902a8083f0SArnaldo Carvalho de Melo pid_synthesize_mmap_samples(pid, tgid); 5917c6a1c65SPeter Zijlstra } else 5927c6a1c65SPeter Zijlstra synthesize_all(); 5937c6a1c65SPeter Zijlstra 59486470930SIngo Molnar if (target_pid == -1 && argc) { 59586470930SIngo Molnar pid = fork(); 59686470930SIngo Molnar if (pid < 0) 59786470930SIngo Molnar perror("failed to fork"); 59886470930SIngo Molnar 59986470930SIngo Molnar if (!pid) { 60086470930SIngo Molnar if (execvp(argv[0], (char **)argv)) { 60186470930SIngo Molnar perror(argv[0]); 60286470930SIngo Molnar exit(-1); 60386470930SIngo Molnar } 60486470930SIngo Molnar } 60586470930SIngo Molnar } 60686470930SIngo Molnar 60786470930SIngo Molnar if (realtime_prio) { 60886470930SIngo Molnar struct sched_param param; 60986470930SIngo Molnar 61086470930SIngo Molnar param.sched_priority = realtime_prio; 61186470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 61286470930SIngo Molnar printf("Could not set realtime priority.\n"); 61386470930SIngo Molnar exit(-1); 61486470930SIngo Molnar } 61586470930SIngo Molnar } 61686470930SIngo Molnar 617649c48a9SPeter Zijlstra for (;;) { 61886470930SIngo Molnar int hits = samples; 61986470930SIngo Molnar 62086470930SIngo Molnar for (i = 0; i < nr_cpu; i++) { 62186470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) 62286470930SIngo Molnar mmap_read(&mmap_array[i][counter]); 62386470930SIngo Molnar } 62486470930SIngo Molnar 625649c48a9SPeter Zijlstra if (hits == samples) { 626649c48a9SPeter Zijlstra if (done) 627649c48a9SPeter Zijlstra break; 62886470930SIngo Molnar ret = poll(event_array, nr_poll, 100); 62986470930SIngo Molnar } 630649c48a9SPeter Zijlstra } 63186470930SIngo Molnar 63286470930SIngo Molnar /* 63386470930SIngo Molnar * Approximate RIP event size: 24 bytes. 63486470930SIngo Molnar */ 63586470930SIngo Molnar fprintf(stderr, 63686470930SIngo Molnar "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", 63786470930SIngo Molnar (double)bytes_written / 1024.0 / 1024.0, 63886470930SIngo Molnar output_name, 63986470930SIngo Molnar bytes_written / 24); 64086470930SIngo Molnar 64186470930SIngo Molnar return 0; 64286470930SIngo Molnar } 64386470930SIngo Molnar 64486470930SIngo Molnar static const char * const record_usage[] = { 64586470930SIngo Molnar "perf record [<options>] [<command>]", 64686470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 64786470930SIngo Molnar NULL 64886470930SIngo Molnar }; 64986470930SIngo Molnar 65086470930SIngo Molnar static const struct option options[] = { 65186470930SIngo Molnar OPT_CALLBACK('e', "event", NULL, "event", 65286470930SIngo Molnar "event selector. use 'perf list' to list available events", 65386470930SIngo Molnar parse_events), 65486470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 65586470930SIngo Molnar "record events on existing pid"), 65686470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 65786470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 658daac07b2SFrederic Weisbecker OPT_BOOLEAN('R', "raw-samples", &raw_samples, 659daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 66086470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 66186470930SIngo Molnar "system-wide collection from all CPUs"), 66286470930SIngo Molnar OPT_BOOLEAN('A', "append", &append_file, 66386470930SIngo Molnar "append to the output file to do incremental profiling"), 6640a5ac846SJens Axboe OPT_INTEGER('C', "profile_cpu", &profile_cpu, 6650a5ac846SJens Axboe "CPU to profile on"), 66686470930SIngo Molnar OPT_BOOLEAN('f', "force", &force, 66786470930SIngo Molnar "overwrite existing data file"), 66886470930SIngo Molnar OPT_LONG('c', "count", &default_interval, 66986470930SIngo Molnar "event period to sample"), 67086470930SIngo Molnar OPT_STRING('o', "output", &output_name, "file", 67186470930SIngo Molnar "output file name"), 67286470930SIngo Molnar OPT_BOOLEAN('i', "inherit", &inherit, 67386470930SIngo Molnar "child tasks inherit counters"), 67486470930SIngo Molnar OPT_INTEGER('F', "freq", &freq, 67586470930SIngo Molnar "profile at this frequency"), 67686470930SIngo Molnar OPT_INTEGER('m', "mmap-pages", &mmap_pages, 67786470930SIngo Molnar "number of mmap data pages"), 6783efa1cc9SIngo Molnar OPT_BOOLEAN('g', "call-graph", &call_graph, 6793efa1cc9SIngo Molnar "do call-graph (stack chain/backtrace) recording"), 6803da297a6SIngo Molnar OPT_BOOLEAN('v', "verbose", &verbose, 6813da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 682649c48a9SPeter Zijlstra OPT_BOOLEAN('s', "stat", &inherit_stat, 683649c48a9SPeter Zijlstra "per thread counts"), 6844bba828dSAnton Blanchard OPT_BOOLEAN('d', "data", &sample_address, 6854bba828dSAnton Blanchard "Sample addresses"), 686649c48a9SPeter Zijlstra OPT_BOOLEAN('n', "no-samples", &no_samples, 687649c48a9SPeter Zijlstra "don't sample"), 688*d1302522SFrederic Weisbecker OPT_BOOLEAN('M', "multiplex", &multiplex, 689*d1302522SFrederic Weisbecker "multiplex counter output in a single channel"), 69086470930SIngo Molnar OPT_END() 69186470930SIngo Molnar }; 69286470930SIngo Molnar 693f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used) 69486470930SIngo Molnar { 69586470930SIngo Molnar int counter; 69686470930SIngo Molnar 697a0541234SAnton Blanchard argc = parse_options(argc, argv, options, record_usage, 698a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 69986470930SIngo Molnar if (!argc && target_pid == -1 && !system_wide) 70086470930SIngo Molnar usage_with_options(record_usage, options); 70186470930SIngo Molnar 702bbd36e5eSPeter Zijlstra if (!nr_counters) { 70386470930SIngo Molnar nr_counters = 1; 704bbd36e5eSPeter Zijlstra attrs[0].type = PERF_TYPE_HARDWARE; 705bbd36e5eSPeter Zijlstra attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; 706bbd36e5eSPeter Zijlstra } 70786470930SIngo Molnar 70886470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) { 70986470930SIngo Molnar if (attrs[counter].sample_period) 71086470930SIngo Molnar continue; 71186470930SIngo Molnar 71286470930SIngo Molnar attrs[counter].sample_period = default_interval; 71386470930SIngo Molnar } 71486470930SIngo Molnar 71586470930SIngo Molnar return __cmd_record(argc, argv); 71686470930SIngo Molnar } 717