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" 187c6a1c65SPeter Zijlstra 1986470930SIngo Molnar #include <unistd.h> 2086470930SIngo Molnar #include <sched.h> 2186470930SIngo Molnar 2286470930SIngo Molnar #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) 2386470930SIngo Molnar #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) 2486470930SIngo Molnar 2586470930SIngo Molnar static int fd[MAX_NR_CPUS][MAX_COUNTERS]; 2686470930SIngo Molnar 2786470930SIngo Molnar static long default_interval = 100000; 2886470930SIngo Molnar 2986470930SIngo Molnar static int nr_cpus = 0; 3086470930SIngo Molnar static unsigned int page_size; 3186470930SIngo Molnar static unsigned int mmap_pages = 128; 3286470930SIngo Molnar static int freq = 0; 3386470930SIngo Molnar static int output; 3486470930SIngo Molnar static const char *output_name = "perf.data"; 3586470930SIngo Molnar static int group = 0; 3686470930SIngo Molnar static unsigned int realtime_prio = 0; 3786470930SIngo Molnar static int system_wide = 0; 3886470930SIngo Molnar static pid_t target_pid = -1; 3986470930SIngo Molnar static int inherit = 1; 4086470930SIngo Molnar static int force = 0; 4186470930SIngo Molnar static int append_file = 0; 423efa1cc9SIngo Molnar static int call_graph = 0; 433da297a6SIngo Molnar static int verbose = 0; 44649c48a9SPeter Zijlstra static int inherit_stat = 0; 45649c48a9SPeter Zijlstra static int no_samples = 0; 46*4bba828dSAnton Blanchard static int sample_address = 0; 4786470930SIngo Molnar 4886470930SIngo Molnar static long samples; 4986470930SIngo Molnar static struct timeval last_read; 5086470930SIngo Molnar static struct timeval this_read; 5186470930SIngo Molnar 529cffa8d5SPaul Mackerras static u64 bytes_written; 5386470930SIngo Molnar 5486470930SIngo Molnar static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; 5586470930SIngo Molnar 5686470930SIngo Molnar static int nr_poll; 5786470930SIngo Molnar static int nr_cpu; 5886470930SIngo Molnar 59f5970550SPeter Zijlstra static int file_new = 1; 607c6a1c65SPeter Zijlstra 617c6a1c65SPeter Zijlstra struct perf_header *header; 62f5970550SPeter Zijlstra 6386470930SIngo Molnar struct mmap_event { 6486470930SIngo Molnar struct perf_event_header header; 659cffa8d5SPaul Mackerras u32 pid; 669cffa8d5SPaul Mackerras u32 tid; 679cffa8d5SPaul Mackerras u64 start; 689cffa8d5SPaul Mackerras u64 len; 699cffa8d5SPaul Mackerras u64 pgoff; 7086470930SIngo Molnar char filename[PATH_MAX]; 7186470930SIngo Molnar }; 7286470930SIngo Molnar 7386470930SIngo Molnar struct comm_event { 7486470930SIngo Molnar struct perf_event_header header; 759cffa8d5SPaul Mackerras u32 pid; 769cffa8d5SPaul Mackerras u32 tid; 7786470930SIngo Molnar char comm[16]; 7886470930SIngo Molnar }; 7986470930SIngo Molnar 8086470930SIngo Molnar 8186470930SIngo Molnar struct mmap_data { 8286470930SIngo Molnar int counter; 8386470930SIngo Molnar void *base; 8486470930SIngo Molnar unsigned int mask; 8586470930SIngo Molnar unsigned int prev; 8686470930SIngo Molnar }; 8786470930SIngo Molnar 8886470930SIngo Molnar static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; 8986470930SIngo Molnar 909d91a6f7SPeter Zijlstra static unsigned long mmap_read_head(struct mmap_data *md) 9186470930SIngo Molnar { 9286470930SIngo Molnar struct perf_counter_mmap_page *pc = md->base; 939d91a6f7SPeter Zijlstra long head; 9486470930SIngo Molnar 9586470930SIngo Molnar head = pc->data_head; 9686470930SIngo Molnar rmb(); 9786470930SIngo Molnar 9886470930SIngo Molnar return head; 9986470930SIngo Molnar } 10086470930SIngo Molnar 1019d91a6f7SPeter Zijlstra static void mmap_write_tail(struct mmap_data *md, unsigned long tail) 1029d91a6f7SPeter Zijlstra { 1039d91a6f7SPeter Zijlstra struct perf_counter_mmap_page *pc = md->base; 1049d91a6f7SPeter Zijlstra 1059d91a6f7SPeter Zijlstra /* 1069d91a6f7SPeter Zijlstra * ensure all reads are done before we write the tail out. 1079d91a6f7SPeter Zijlstra */ 1089d91a6f7SPeter Zijlstra /* mb(); */ 1099d91a6f7SPeter Zijlstra pc->data_tail = tail; 1109d91a6f7SPeter Zijlstra } 1119d91a6f7SPeter Zijlstra 112f5970550SPeter Zijlstra static void write_output(void *buf, size_t size) 113f5970550SPeter Zijlstra { 114f5970550SPeter Zijlstra while (size) { 115f5970550SPeter Zijlstra int ret = write(output, buf, size); 116f5970550SPeter Zijlstra 117f5970550SPeter Zijlstra if (ret < 0) 118f5970550SPeter Zijlstra die("failed to write"); 119f5970550SPeter Zijlstra 120f5970550SPeter Zijlstra size -= ret; 121f5970550SPeter Zijlstra buf += ret; 122f5970550SPeter Zijlstra 123f5970550SPeter Zijlstra bytes_written += ret; 124f5970550SPeter Zijlstra } 125f5970550SPeter Zijlstra } 126f5970550SPeter Zijlstra 12786470930SIngo Molnar static void mmap_read(struct mmap_data *md) 12886470930SIngo Molnar { 12986470930SIngo Molnar unsigned int head = mmap_read_head(md); 13086470930SIngo Molnar unsigned int old = md->prev; 13186470930SIngo Molnar unsigned char *data = md->base + page_size; 13286470930SIngo Molnar unsigned long size; 13386470930SIngo Molnar void *buf; 13486470930SIngo Molnar int diff; 13586470930SIngo Molnar 13686470930SIngo Molnar gettimeofday(&this_read, NULL); 13786470930SIngo Molnar 13886470930SIngo Molnar /* 13986470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 14086470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 14186470930SIngo Molnar * 14286470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 14386470930SIngo Molnar * 14486470930SIngo Molnar * In either case, truncate and restart at head. 14586470930SIngo Molnar */ 14686470930SIngo Molnar diff = head - old; 1479d91a6f7SPeter Zijlstra if (diff < 0) { 14886470930SIngo Molnar struct timeval iv; 14986470930SIngo Molnar unsigned long msecs; 15086470930SIngo Molnar 15186470930SIngo Molnar timersub(&this_read, &last_read, &iv); 15286470930SIngo Molnar msecs = iv.tv_sec*1000 + iv.tv_usec/1000; 15386470930SIngo Molnar 15486470930SIngo Molnar fprintf(stderr, "WARNING: failed to keep up with mmap data." 15586470930SIngo Molnar " Last read %lu msecs ago.\n", msecs); 15686470930SIngo Molnar 15786470930SIngo Molnar /* 15886470930SIngo Molnar * head points to a known good entry, start there. 15986470930SIngo Molnar */ 16086470930SIngo Molnar old = head; 16186470930SIngo Molnar } 16286470930SIngo Molnar 16386470930SIngo Molnar last_read = this_read; 16486470930SIngo Molnar 16586470930SIngo Molnar if (old != head) 16686470930SIngo Molnar samples++; 16786470930SIngo Molnar 16886470930SIngo Molnar size = head - old; 16986470930SIngo Molnar 17086470930SIngo Molnar if ((old & md->mask) + size != (head & md->mask)) { 17186470930SIngo Molnar buf = &data[old & md->mask]; 17286470930SIngo Molnar size = md->mask + 1 - (old & md->mask); 17386470930SIngo Molnar old += size; 17486470930SIngo Molnar 175f5970550SPeter Zijlstra write_output(buf, size); 17686470930SIngo Molnar } 17786470930SIngo Molnar 17886470930SIngo Molnar buf = &data[old & md->mask]; 17986470930SIngo Molnar size = head - old; 18086470930SIngo Molnar old += size; 18186470930SIngo Molnar 182f5970550SPeter Zijlstra write_output(buf, size); 18386470930SIngo Molnar 18486470930SIngo Molnar md->prev = old; 1859d91a6f7SPeter Zijlstra mmap_write_tail(md, old); 18686470930SIngo Molnar } 18786470930SIngo Molnar 18886470930SIngo Molnar static volatile int done = 0; 189f7b7c26eSPeter Zijlstra static volatile int signr = -1; 19086470930SIngo Molnar 19186470930SIngo Molnar static void sig_handler(int sig) 19286470930SIngo Molnar { 19386470930SIngo Molnar done = 1; 194f7b7c26eSPeter Zijlstra signr = sig; 195f7b7c26eSPeter Zijlstra } 196f7b7c26eSPeter Zijlstra 197f7b7c26eSPeter Zijlstra static void sig_atexit(void) 198f7b7c26eSPeter Zijlstra { 199f7b7c26eSPeter Zijlstra if (signr == -1) 200f7b7c26eSPeter Zijlstra return; 201f7b7c26eSPeter Zijlstra 202f7b7c26eSPeter Zijlstra signal(signr, SIG_DFL); 203f7b7c26eSPeter Zijlstra kill(getpid(), signr); 20486470930SIngo Molnar } 20586470930SIngo Molnar 20686470930SIngo Molnar static void pid_synthesize_comm_event(pid_t pid, int full) 20786470930SIngo Molnar { 20886470930SIngo Molnar struct comm_event comm_ev; 20986470930SIngo Molnar char filename[PATH_MAX]; 21086470930SIngo Molnar char bf[BUFSIZ]; 211f5970550SPeter Zijlstra int fd; 21286470930SIngo Molnar size_t size; 21386470930SIngo Molnar char *field, *sep; 21486470930SIngo Molnar DIR *tasks; 21586470930SIngo Molnar struct dirent dirent, *next; 21686470930SIngo Molnar 21786470930SIngo Molnar snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); 21886470930SIngo Molnar 21986470930SIngo Molnar fd = open(filename, O_RDONLY); 22086470930SIngo Molnar if (fd < 0) { 221613d8602SIngo Molnar /* 222613d8602SIngo Molnar * We raced with a task exiting - just return: 223613d8602SIngo Molnar */ 224613d8602SIngo Molnar if (verbose) 22586470930SIngo Molnar fprintf(stderr, "couldn't open %s\n", filename); 226613d8602SIngo Molnar return; 22786470930SIngo Molnar } 22886470930SIngo Molnar if (read(fd, bf, sizeof(bf)) < 0) { 22986470930SIngo Molnar fprintf(stderr, "couldn't read %s\n", filename); 23086470930SIngo Molnar exit(EXIT_FAILURE); 23186470930SIngo Molnar } 23286470930SIngo Molnar close(fd); 23386470930SIngo Molnar 23486470930SIngo Molnar /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */ 23586470930SIngo Molnar memset(&comm_ev, 0, sizeof(comm_ev)); 23686470930SIngo Molnar field = strchr(bf, '('); 23786470930SIngo Molnar if (field == NULL) 23886470930SIngo Molnar goto out_failure; 23986470930SIngo Molnar sep = strchr(++field, ')'); 24086470930SIngo Molnar if (sep == NULL) 24186470930SIngo Molnar goto out_failure; 24286470930SIngo Molnar size = sep - field; 24386470930SIngo Molnar memcpy(comm_ev.comm, field, size++); 24486470930SIngo Molnar 24586470930SIngo Molnar comm_ev.pid = pid; 24686470930SIngo Molnar comm_ev.header.type = PERF_EVENT_COMM; 2479cffa8d5SPaul Mackerras size = ALIGN(size, sizeof(u64)); 24886470930SIngo Molnar comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size); 24986470930SIngo Molnar 25086470930SIngo Molnar if (!full) { 25186470930SIngo Molnar comm_ev.tid = pid; 25286470930SIngo Molnar 253f5970550SPeter Zijlstra write_output(&comm_ev, comm_ev.header.size); 25486470930SIngo Molnar return; 25586470930SIngo Molnar } 25686470930SIngo Molnar 25786470930SIngo Molnar snprintf(filename, sizeof(filename), "/proc/%d/task", pid); 25886470930SIngo Molnar 25986470930SIngo Molnar tasks = opendir(filename); 26086470930SIngo Molnar while (!readdir_r(tasks, &dirent, &next) && next) { 26186470930SIngo Molnar char *end; 26286470930SIngo Molnar pid = strtol(dirent.d_name, &end, 10); 26386470930SIngo Molnar if (*end) 26486470930SIngo Molnar continue; 26586470930SIngo Molnar 26686470930SIngo Molnar comm_ev.tid = pid; 26786470930SIngo Molnar 268f5970550SPeter Zijlstra write_output(&comm_ev, comm_ev.header.size); 26986470930SIngo Molnar } 27086470930SIngo Molnar closedir(tasks); 27186470930SIngo Molnar return; 27286470930SIngo Molnar 27386470930SIngo Molnar out_failure: 27486470930SIngo Molnar fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n", 27586470930SIngo Molnar filename); 27686470930SIngo Molnar exit(EXIT_FAILURE); 27786470930SIngo Molnar } 27886470930SIngo Molnar 27986470930SIngo Molnar static void pid_synthesize_mmap_samples(pid_t pid) 28086470930SIngo Molnar { 28186470930SIngo Molnar char filename[PATH_MAX]; 28286470930SIngo Molnar FILE *fp; 28386470930SIngo Molnar 28486470930SIngo Molnar snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); 28586470930SIngo Molnar 28686470930SIngo Molnar fp = fopen(filename, "r"); 28786470930SIngo Molnar if (fp == NULL) { 288613d8602SIngo Molnar /* 289613d8602SIngo Molnar * We raced with a task exiting - just return: 290613d8602SIngo Molnar */ 291613d8602SIngo Molnar if (verbose) 29286470930SIngo Molnar fprintf(stderr, "couldn't open %s\n", filename); 293613d8602SIngo Molnar return; 29486470930SIngo Molnar } 29586470930SIngo Molnar while (1) { 29686470930SIngo Molnar char bf[BUFSIZ], *pbf = bf; 29786470930SIngo Molnar struct mmap_event mmap_ev = { 298f37a291cSIngo Molnar .header = { .type = PERF_EVENT_MMAP }, 29986470930SIngo Molnar }; 30086470930SIngo Molnar int n; 30186470930SIngo Molnar size_t size; 30286470930SIngo Molnar if (fgets(bf, sizeof(bf), fp) == NULL) 30386470930SIngo Molnar break; 30486470930SIngo Molnar 30586470930SIngo Molnar /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ 30686470930SIngo Molnar n = hex2u64(pbf, &mmap_ev.start); 30786470930SIngo Molnar if (n < 0) 30886470930SIngo Molnar continue; 30986470930SIngo Molnar pbf += n + 1; 31086470930SIngo Molnar n = hex2u64(pbf, &mmap_ev.len); 31186470930SIngo Molnar if (n < 0) 31286470930SIngo Molnar continue; 31386470930SIngo Molnar pbf += n + 3; 31486470930SIngo Molnar if (*pbf == 'x') { /* vm_exec */ 31576c64c5eSJohannes Weiner char *execname = strchr(bf, '/'); 31686470930SIngo Molnar 31711b5f81eSAnton Blanchard /* Catch VDSO */ 31811b5f81eSAnton Blanchard if (execname == NULL) 31911b5f81eSAnton Blanchard execname = strstr(bf, "[vdso]"); 32011b5f81eSAnton Blanchard 32176c64c5eSJohannes Weiner if (execname == NULL) 32286470930SIngo Molnar continue; 32386470930SIngo Molnar 32486470930SIngo Molnar size = strlen(execname); 32586470930SIngo Molnar execname[size - 1] = '\0'; /* Remove \n */ 32686470930SIngo Molnar memcpy(mmap_ev.filename, execname, size); 3279cffa8d5SPaul Mackerras size = ALIGN(size, sizeof(u64)); 32886470930SIngo Molnar mmap_ev.len -= mmap_ev.start; 32986470930SIngo Molnar mmap_ev.header.size = (sizeof(mmap_ev) - 33086470930SIngo Molnar (sizeof(mmap_ev.filename) - size)); 33186470930SIngo Molnar mmap_ev.pid = pid; 33286470930SIngo Molnar mmap_ev.tid = pid; 33386470930SIngo Molnar 334f5970550SPeter Zijlstra write_output(&mmap_ev, mmap_ev.header.size); 33586470930SIngo Molnar } 33686470930SIngo Molnar } 33786470930SIngo Molnar 33886470930SIngo Molnar fclose(fp); 33986470930SIngo Molnar } 34086470930SIngo Molnar 3417c6a1c65SPeter Zijlstra static void synthesize_all(void) 34286470930SIngo Molnar { 34386470930SIngo Molnar DIR *proc; 34486470930SIngo Molnar struct dirent dirent, *next; 34586470930SIngo Molnar 34686470930SIngo Molnar proc = opendir("/proc"); 34786470930SIngo Molnar 34886470930SIngo Molnar while (!readdir_r(proc, &dirent, &next) && next) { 34986470930SIngo Molnar char *end; 35086470930SIngo Molnar pid_t pid; 35186470930SIngo Molnar 35286470930SIngo Molnar pid = strtol(dirent.d_name, &end, 10); 35386470930SIngo Molnar if (*end) /* only interested in proper numerical dirents */ 35486470930SIngo Molnar continue; 35586470930SIngo Molnar 35686470930SIngo Molnar pid_synthesize_comm_event(pid, 1); 35786470930SIngo Molnar pid_synthesize_mmap_samples(pid); 35886470930SIngo Molnar } 35986470930SIngo Molnar 36086470930SIngo Molnar closedir(proc); 36186470930SIngo Molnar } 36286470930SIngo Molnar 36386470930SIngo Molnar static int group_fd; 36486470930SIngo Molnar 3657c6a1c65SPeter Zijlstra static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr) 3667c6a1c65SPeter Zijlstra { 3677c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 3687c6a1c65SPeter Zijlstra 3697c6a1c65SPeter Zijlstra if (nr < header->attrs) { 3707c6a1c65SPeter Zijlstra h_attr = header->attr[nr]; 3717c6a1c65SPeter Zijlstra } else { 3727c6a1c65SPeter Zijlstra h_attr = perf_header_attr__new(a); 3737c6a1c65SPeter Zijlstra perf_header__add_attr(header, h_attr); 3747c6a1c65SPeter Zijlstra } 3757c6a1c65SPeter Zijlstra 3767c6a1c65SPeter Zijlstra return h_attr; 3777c6a1c65SPeter Zijlstra } 3787c6a1c65SPeter Zijlstra 37986470930SIngo Molnar static void create_counter(int counter, int cpu, pid_t pid) 38086470930SIngo Molnar { 38186470930SIngo Molnar struct perf_counter_attr *attr = attrs + counter; 3827c6a1c65SPeter Zijlstra struct perf_header_attr *h_attr; 3837c6a1c65SPeter Zijlstra int track = !counter; /* only the first counter needs these */ 3847c6a1c65SPeter Zijlstra struct { 3857c6a1c65SPeter Zijlstra u64 count; 3867c6a1c65SPeter Zijlstra u64 time_enabled; 3877c6a1c65SPeter Zijlstra u64 time_running; 3887c6a1c65SPeter Zijlstra u64 id; 3897c6a1c65SPeter Zijlstra } read_data; 3907c6a1c65SPeter Zijlstra 3917c6a1c65SPeter Zijlstra attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | 3927c6a1c65SPeter Zijlstra PERF_FORMAT_TOTAL_TIME_RUNNING | 3937c6a1c65SPeter Zijlstra PERF_FORMAT_ID; 39486470930SIngo Molnar 395ea1900e5SPeter Zijlstra attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; 3963efa1cc9SIngo Molnar 39786470930SIngo Molnar if (freq) { 398ea1900e5SPeter Zijlstra attr->sample_type |= PERF_SAMPLE_PERIOD; 39986470930SIngo Molnar attr->freq = 1; 40086470930SIngo Molnar attr->sample_freq = freq; 40186470930SIngo Molnar } 4023efa1cc9SIngo Molnar 403649c48a9SPeter Zijlstra if (no_samples) 404649c48a9SPeter Zijlstra attr->sample_freq = 0; 405649c48a9SPeter Zijlstra 406649c48a9SPeter Zijlstra if (inherit_stat) 407649c48a9SPeter Zijlstra attr->inherit_stat = 1; 408649c48a9SPeter Zijlstra 409*4bba828dSAnton Blanchard if (sample_address) 410*4bba828dSAnton Blanchard attr->sample_type |= PERF_SAMPLE_ADDR; 411*4bba828dSAnton Blanchard 4123efa1cc9SIngo Molnar if (call_graph) 4133efa1cc9SIngo Molnar attr->sample_type |= PERF_SAMPLE_CALLCHAIN; 4143efa1cc9SIngo Molnar 41586470930SIngo Molnar attr->mmap = track; 41686470930SIngo Molnar attr->comm = track; 41786470930SIngo Molnar attr->inherit = (cpu < 0) && inherit; 4184502d77cSPeter Zijlstra attr->disabled = 1; 41986470930SIngo Molnar 4203da297a6SIngo Molnar try_again: 42186470930SIngo Molnar fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0); 42286470930SIngo Molnar 42386470930SIngo Molnar if (fd[nr_cpu][counter] < 0) { 42486470930SIngo Molnar int err = errno; 42586470930SIngo Molnar 42686470930SIngo Molnar if (err == EPERM) 4273da297a6SIngo Molnar die("Permission error - are you root?\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 4894502d77cSPeter Zijlstra ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE); 49086470930SIngo Molnar } 49186470930SIngo Molnar 49286470930SIngo Molnar static void open_counters(int cpu, pid_t pid) 49386470930SIngo Molnar { 49486470930SIngo Molnar int counter; 49586470930SIngo Molnar 49686470930SIngo Molnar group_fd = -1; 49786470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) 49886470930SIngo Molnar create_counter(counter, cpu, pid); 49986470930SIngo Molnar 50086470930SIngo Molnar nr_cpu++; 50186470930SIngo Molnar } 50286470930SIngo Molnar 503f5970550SPeter Zijlstra static void atexit_header(void) 504f5970550SPeter Zijlstra { 5057c6a1c65SPeter Zijlstra header->data_size += bytes_written; 506f5970550SPeter Zijlstra 5077c6a1c65SPeter Zijlstra perf_header__write(header, output); 508f5970550SPeter Zijlstra } 509f5970550SPeter Zijlstra 51086470930SIngo Molnar static int __cmd_record(int argc, const char **argv) 51186470930SIngo Molnar { 51286470930SIngo Molnar int i, counter; 51386470930SIngo Molnar struct stat st; 5147c6a1c65SPeter Zijlstra pid_t pid = 0; 51586470930SIngo Molnar int flags; 51686470930SIngo Molnar int ret; 51786470930SIngo Molnar 51886470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 51986470930SIngo Molnar nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); 52086470930SIngo Molnar assert(nr_cpus <= MAX_NR_CPUS); 52186470930SIngo Molnar assert(nr_cpus >= 0); 52286470930SIngo Molnar 523f5970550SPeter Zijlstra atexit(sig_atexit); 524f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 525f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 526f5970550SPeter Zijlstra 52786470930SIngo Molnar if (!stat(output_name, &st) && !force && !append_file) { 52886470930SIngo Molnar fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n", 52986470930SIngo Molnar output_name); 53086470930SIngo Molnar exit(-1); 53186470930SIngo Molnar } 53286470930SIngo Molnar 53386470930SIngo Molnar flags = O_CREAT|O_RDWR; 53486470930SIngo Molnar if (append_file) 535f5970550SPeter Zijlstra file_new = 0; 53686470930SIngo Molnar else 53786470930SIngo Molnar flags |= O_TRUNC; 53886470930SIngo Molnar 53986470930SIngo Molnar output = open(output_name, flags, S_IRUSR|S_IWUSR); 54086470930SIngo Molnar if (output < 0) { 54186470930SIngo Molnar perror("failed to create output file"); 54286470930SIngo Molnar exit(-1); 54386470930SIngo Molnar } 54486470930SIngo Molnar 5457c6a1c65SPeter Zijlstra if (!file_new) 5467c6a1c65SPeter Zijlstra header = perf_header__read(output); 5477c6a1c65SPeter Zijlstra else 5487c6a1c65SPeter Zijlstra header = perf_header__new(); 549f5970550SPeter Zijlstra 550f5970550SPeter Zijlstra atexit(atexit_header); 551f5970550SPeter Zijlstra 55286470930SIngo Molnar if (!system_wide) { 5537c6a1c65SPeter Zijlstra pid = target_pid; 5547c6a1c65SPeter Zijlstra if (pid == -1) 5557c6a1c65SPeter Zijlstra pid = getpid(); 5567c6a1c65SPeter Zijlstra 5577c6a1c65SPeter Zijlstra open_counters(-1, pid); 55886470930SIngo Molnar } else for (i = 0; i < nr_cpus; i++) 55986470930SIngo Molnar open_counters(i, target_pid); 56086470930SIngo Molnar 5617c6a1c65SPeter Zijlstra if (file_new) 5627c6a1c65SPeter Zijlstra perf_header__write(header, output); 5637c6a1c65SPeter Zijlstra 5647c6a1c65SPeter Zijlstra if (!system_wide) { 5657c6a1c65SPeter Zijlstra pid_synthesize_comm_event(pid, 0); 5667c6a1c65SPeter Zijlstra pid_synthesize_mmap_samples(pid); 5677c6a1c65SPeter Zijlstra } else 5687c6a1c65SPeter Zijlstra synthesize_all(); 5697c6a1c65SPeter Zijlstra 57086470930SIngo Molnar if (target_pid == -1 && argc) { 57186470930SIngo Molnar pid = fork(); 57286470930SIngo Molnar if (pid < 0) 57386470930SIngo Molnar perror("failed to fork"); 57486470930SIngo Molnar 57586470930SIngo Molnar if (!pid) { 57686470930SIngo Molnar if (execvp(argv[0], (char **)argv)) { 57786470930SIngo Molnar perror(argv[0]); 57886470930SIngo Molnar exit(-1); 57986470930SIngo Molnar } 58086470930SIngo Molnar } 58186470930SIngo Molnar } 58286470930SIngo Molnar 58386470930SIngo Molnar if (realtime_prio) { 58486470930SIngo Molnar struct sched_param param; 58586470930SIngo Molnar 58686470930SIngo Molnar param.sched_priority = realtime_prio; 58786470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 58886470930SIngo Molnar printf("Could not set realtime priority.\n"); 58986470930SIngo Molnar exit(-1); 59086470930SIngo Molnar } 59186470930SIngo Molnar } 59286470930SIngo Molnar 593649c48a9SPeter Zijlstra for (;;) { 59486470930SIngo Molnar int hits = samples; 59586470930SIngo Molnar 59686470930SIngo Molnar for (i = 0; i < nr_cpu; i++) { 59786470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) 59886470930SIngo Molnar mmap_read(&mmap_array[i][counter]); 59986470930SIngo Molnar } 60086470930SIngo Molnar 601649c48a9SPeter Zijlstra if (hits == samples) { 602649c48a9SPeter Zijlstra if (done) 603649c48a9SPeter Zijlstra break; 60486470930SIngo Molnar ret = poll(event_array, nr_poll, 100); 60586470930SIngo Molnar } 606649c48a9SPeter Zijlstra } 60786470930SIngo Molnar 60886470930SIngo Molnar /* 60986470930SIngo Molnar * Approximate RIP event size: 24 bytes. 61086470930SIngo Molnar */ 61186470930SIngo Molnar fprintf(stderr, 61286470930SIngo Molnar "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", 61386470930SIngo Molnar (double)bytes_written / 1024.0 / 1024.0, 61486470930SIngo Molnar output_name, 61586470930SIngo Molnar bytes_written / 24); 61686470930SIngo Molnar 61786470930SIngo Molnar return 0; 61886470930SIngo Molnar } 61986470930SIngo Molnar 62086470930SIngo Molnar static const char * const record_usage[] = { 62186470930SIngo Molnar "perf record [<options>] [<command>]", 62286470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 62386470930SIngo Molnar NULL 62486470930SIngo Molnar }; 62586470930SIngo Molnar 62686470930SIngo Molnar static const struct option options[] = { 62786470930SIngo Molnar OPT_CALLBACK('e', "event", NULL, "event", 62886470930SIngo Molnar "event selector. use 'perf list' to list available events", 62986470930SIngo Molnar parse_events), 63086470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 63186470930SIngo Molnar "record events on existing pid"), 63286470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 63386470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 63486470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 63586470930SIngo Molnar "system-wide collection from all CPUs"), 63686470930SIngo Molnar OPT_BOOLEAN('A', "append", &append_file, 63786470930SIngo Molnar "append to the output file to do incremental profiling"), 63886470930SIngo Molnar OPT_BOOLEAN('f', "force", &force, 63986470930SIngo Molnar "overwrite existing data file"), 64086470930SIngo Molnar OPT_LONG('c', "count", &default_interval, 64186470930SIngo Molnar "event period to sample"), 64286470930SIngo Molnar OPT_STRING('o', "output", &output_name, "file", 64386470930SIngo Molnar "output file name"), 64486470930SIngo Molnar OPT_BOOLEAN('i', "inherit", &inherit, 64586470930SIngo Molnar "child tasks inherit counters"), 64686470930SIngo Molnar OPT_INTEGER('F', "freq", &freq, 64786470930SIngo Molnar "profile at this frequency"), 64886470930SIngo Molnar OPT_INTEGER('m', "mmap-pages", &mmap_pages, 64986470930SIngo Molnar "number of mmap data pages"), 6503efa1cc9SIngo Molnar OPT_BOOLEAN('g', "call-graph", &call_graph, 6513efa1cc9SIngo Molnar "do call-graph (stack chain/backtrace) recording"), 6523da297a6SIngo Molnar OPT_BOOLEAN('v', "verbose", &verbose, 6533da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 654649c48a9SPeter Zijlstra OPT_BOOLEAN('s', "stat", &inherit_stat, 655649c48a9SPeter Zijlstra "per thread counts"), 656*4bba828dSAnton Blanchard OPT_BOOLEAN('d', "data", &sample_address, 657*4bba828dSAnton Blanchard "Sample addresses"), 658649c48a9SPeter Zijlstra OPT_BOOLEAN('n', "no-samples", &no_samples, 659649c48a9SPeter Zijlstra "don't sample"), 66086470930SIngo Molnar OPT_END() 66186470930SIngo Molnar }; 66286470930SIngo Molnar 663f37a291cSIngo Molnar int cmd_record(int argc, const char **argv, const char *prefix __used) 66486470930SIngo Molnar { 66586470930SIngo Molnar int counter; 66686470930SIngo Molnar 66786470930SIngo Molnar argc = parse_options(argc, argv, options, record_usage, 0); 66886470930SIngo Molnar if (!argc && target_pid == -1 && !system_wide) 66986470930SIngo Molnar usage_with_options(record_usage, options); 67086470930SIngo Molnar 671bbd36e5eSPeter Zijlstra if (!nr_counters) { 67286470930SIngo Molnar nr_counters = 1; 673bbd36e5eSPeter Zijlstra attrs[0].type = PERF_TYPE_HARDWARE; 674bbd36e5eSPeter Zijlstra attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; 675bbd36e5eSPeter Zijlstra } 67686470930SIngo Molnar 67786470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) { 67886470930SIngo Molnar if (attrs[counter].sample_period) 67986470930SIngo Molnar continue; 68086470930SIngo Molnar 68186470930SIngo Molnar attrs[counter].sample_period = default_interval; 68286470930SIngo Molnar } 68386470930SIngo Molnar 68486470930SIngo Molnar return __cmd_record(argc, argv); 68586470930SIngo Molnar } 686