22 #include <sys/syscall.h>
23 #include <sys/ioctl.h>
25 #include <sys/prctl.h>
30 #include <linux/unistd.h>
31 #include <linux/types.h>
33 #include "../../include/linux/perf_counter.h"
37 static int nr_counters = 0;
38 static __u64 event_id[MAX_COUNTERS] = { };
39 static int default_interval = 100000;
40 static int event_count[MAX_COUNTERS];
41 static int fd[MAX_NR_CPUS][MAX_COUNTERS];
42 static int nr_cpus = 0;
43 static unsigned int page_size;
44 static unsigned int mmap_pages = 16;
46 static char *output_name = "output.perf";
48 static unsigned int realtime_prio = 0;
50 const unsigned int default_count[] = {
64 static struct event_symbol event_symbols[] = {
65 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cpu-cycles", },
66 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cycles", },
67 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS), "instructions", },
68 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES), "cache-references", },
69 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES), "cache-misses", },
70 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branch-instructions", },
71 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branches", },
72 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_MISSES), "branch-misses", },
73 {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BUS_CYCLES), "bus-cycles", },
75 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK), "cpu-clock", },
76 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK), "task-clock", },
77 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "page-faults", },
78 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "faults", },
79 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MIN), "minor-faults", },
80 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MAJ), "major-faults", },
81 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "context-switches", },
82 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "cs", },
83 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "cpu-migrations", },
84 {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "migrations", },
88 * Each event can have multiple symbolic names.
89 * Symbolic names are (almost) exactly matched.
91 static __u64 match_event_symbols(char *str)
97 if (sscanf(str, "r%llx", &config) == 1)
98 return config | PERF_COUNTER_RAW_MASK;
100 if (sscanf(str, "%d:%llu", &type, &id) == 2)
101 return EID(type, id);
103 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
104 if (!strncmp(str, event_symbols[i].symbol,
105 strlen(event_symbols[i].symbol)))
106 return event_symbols[i].event;
112 static int parse_events(char *str)
117 if (nr_counters == MAX_COUNTERS)
120 config = match_event_symbols(str);
124 event_id[nr_counters] = config;
127 str = strstr(str, ",");
136 #define __PERF_COUNTER_FIELD(config, name) \
137 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
139 #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
140 #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
141 #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
142 #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
144 static void display_events_help(void)
150 " -e EVENT --event=EVENT # symbolic-name abbreviations");
152 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
155 e = event_symbols[i].event;
156 type = PERF_COUNTER_TYPE(e);
157 id = PERF_COUNTER_ID(e);
159 printf("\n %d:%d: %-20s",
160 type, id, event_symbols[i].symbol);
164 " rNNN: raw PMU events (eventsel+umask)\n\n");
167 static void display_help(void)
170 "Usage: perf-record [<options>]\n"
171 "perf-record Options (up to %d event types can be specified at once):\n\n",
174 display_events_help();
177 " -c CNT --count=CNT # event period to sample\n"
178 " -m pages --mmap_pages=<pages> # number of mmap data pages\n"
179 " -o file --output=<file> # output file\n"
180 " -r prio --realtime=<prio> # use RT prio\n"
186 static void process_options(int argc, char *argv[])
188 int error = 0, counter;
191 int option_index = 0;
192 /** Options for getopt */
193 static struct option long_options[] = {
194 {"count", required_argument, NULL, 'c'},
195 {"event", required_argument, NULL, 'e'},
196 {"mmap_pages", required_argument, NULL, 'm'},
197 {"output", required_argument, NULL, 'o'},
198 {"realtime", required_argument, NULL, 'r'},
201 int c = getopt_long(argc, argv, "+:c:e:m:o:r:",
202 long_options, &option_index);
207 case 'c': default_interval = atoi(optarg); break;
208 case 'e': error = parse_events(optarg); break;
209 case 'm': mmap_pages = atoi(optarg); break;
210 case 'o': output_name = strdup(optarg); break;
211 case 'r': realtime_prio = atoi(optarg); break;
212 default: error = 1; break;
223 for (counter = 0; counter < nr_counters; counter++) {
224 if (event_count[counter])
227 event_count[counter] = default_interval;
238 static unsigned int mmap_read_head(struct mmap_data *md)
240 struct perf_counter_mmap_page *pc = md->base;
243 head = pc->data_head;
250 static struct timeval last_read, this_read;
252 static void mmap_read(struct mmap_data *md)
254 unsigned int head = mmap_read_head(md);
255 unsigned int old = md->prev;
256 unsigned char *data = md->base + page_size;
261 gettimeofday(&this_read, NULL);
264 * If we're further behind than half the buffer, there's a chance
265 * the writer will bite our tail and screw up the events under us.
267 * If we somehow ended up ahead of the head, we got messed up.
269 * In either case, truncate and restart at head.
272 if (diff > md->mask / 2 || diff < 0) {
276 timersub(&this_read, &last_read, &iv);
277 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
279 fprintf(stderr, "WARNING: failed to keep up with mmap data."
280 " Last read %lu msecs ago.\n", msecs);
283 * head points to a known good entry, start there.
288 last_read = this_read;
295 if ((old & md->mask) + size != (head & md->mask)) {
296 buf = &data[old & md->mask];
297 size = md->mask + 1 - (old & md->mask);
300 int ret = write(output, buf, size);
302 perror("failed to write");
310 buf = &data[old & md->mask];
314 int ret = write(output, buf, size);
316 perror("failed to write");
326 static volatile int done = 0;
328 static void sigchld_handler(int sig)
334 int cmd_record(int argc, char **argv)
336 struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
337 struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
338 struct perf_counter_hw_event hw_event;
339 int i, counter, group_fd, nr_poll = 0;
343 page_size = sysconf(_SC_PAGE_SIZE);
345 process_options(argc, argv);
347 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
348 assert(nr_cpus <= MAX_NR_CPUS);
349 assert(nr_cpus >= 0);
351 output = open(output_name, O_CREAT|O_RDWR, S_IRWXU);
353 perror("failed to create output file");
360 for (i = 0; i < nr_cpus; i++) {
362 for (counter = 0; counter < nr_counters; counter++) {
364 memset(&hw_event, 0, sizeof(hw_event));
365 hw_event.config = event_id[counter];
366 hw_event.irq_period = event_count[counter];
367 hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID;
372 fd[i][counter] = sys_perf_counter_open(&hw_event, -1, i, group_fd, 0);
373 if (fd[i][counter] < 0) {
375 printf("kerneltop error: syscall returned with %d (%s)\n",
376 fd[i][counter], strerror(err));
378 printf("Are you root?\n");
381 assert(fd[i][counter] >= 0);
382 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
385 * First counter acts as the group leader:
387 if (group && group_fd == -1)
388 group_fd = fd[i][counter];
390 event_array[nr_poll].fd = fd[i][counter];
391 event_array[nr_poll].events = POLLIN;
394 mmap_array[i][counter].counter = counter;
395 mmap_array[i][counter].prev = 0;
396 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
397 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
398 PROT_READ, MAP_SHARED, fd[i][counter], 0);
399 if (mmap_array[i][counter].base == MAP_FAILED) {
400 printf("kerneltop error: failed to mmap with %d (%s)\n",
401 errno, strerror(errno));
407 signal(SIGCHLD, sigchld_handler);
411 perror("failed to fork");
414 if (execvp(argv[0], argv)) {
421 struct sched_param param;
423 param.sched_priority = realtime_prio;
424 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
425 printf("Could not set realtime priority.\n");
431 * TODO: store the current /proc/$/maps information somewhere
437 for (i = 0; i < nr_cpus; i++) {
438 for (counter = 0; counter < nr_counters; counter++)
439 mmap_read(&mmap_array[i][counter]);
443 ret = poll(event_array, nr_poll, 100);