23 #include <sys/syscall.h>
24 #include <sys/ioctl.h>
26 #include <sys/prctl.h>
31 #include <linux/unistd.h>
32 #include <linux/types.h>
34 #include "../../include/linux/perf_counter.h"
38 static int nr_counters
= 0;
39 static __u64 event_id
[MAX_COUNTERS
] = { };
40 static int default_interval
= 100000;
41 static int event_count
[MAX_COUNTERS
];
42 static int fd
[MAX_NR_CPUS
][MAX_COUNTERS
];
43 static int nr_cpus
= 0;
44 static unsigned int page_size
;
45 static unsigned int mmap_pages
= 16;
47 static char *output_name
= "output.perf";
49 static unsigned int realtime_prio
= 0;
51 const unsigned int default_count
[] = {
65 static struct event_symbol event_symbols
[] = {
66 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CPU_CYCLES
), "cpu-cycles", },
67 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CPU_CYCLES
), "cycles", },
68 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_INSTRUCTIONS
), "instructions", },
69 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CACHE_REFERENCES
), "cache-references", },
70 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CACHE_MISSES
), "cache-misses", },
71 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_BRANCH_INSTRUCTIONS
), "branch-instructions", },
72 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_BRANCH_INSTRUCTIONS
), "branches", },
73 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_BRANCH_MISSES
), "branch-misses", },
74 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_BUS_CYCLES
), "bus-cycles", },
76 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CPU_CLOCK
), "cpu-clock", },
77 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_TASK_CLOCK
), "task-clock", },
78 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS
), "page-faults", },
79 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS
), "faults", },
80 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS_MIN
), "minor-faults", },
81 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS_MAJ
), "major-faults", },
82 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CONTEXT_SWITCHES
), "context-switches", },
83 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CONTEXT_SWITCHES
), "cs", },
84 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CPU_MIGRATIONS
), "cpu-migrations", },
85 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CPU_MIGRATIONS
), "migrations", },
89 * Each event can have multiple symbolic names.
90 * Symbolic names are (almost) exactly matched.
92 static __u64
match_event_symbols(char *str
)
98 if (sscanf(str
, "r%llx", &config
) == 1)
99 return config
| PERF_COUNTER_RAW_MASK
;
101 if (sscanf(str
, "%d:%llu", &type
, &id
) == 2)
102 return EID(type
, id
);
104 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
105 if (!strncmp(str
, event_symbols
[i
].symbol
,
106 strlen(event_symbols
[i
].symbol
)))
107 return event_symbols
[i
].event
;
113 static int parse_events(char *str
)
118 if (nr_counters
== MAX_COUNTERS
)
121 config
= match_event_symbols(str
);
125 event_id
[nr_counters
] = config
;
128 str
= strstr(str
, ",");
137 #define __PERF_COUNTER_FIELD(config, name) \
138 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
140 #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
141 #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
142 #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
143 #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
145 static void display_events_help(void)
151 " -e EVENT --event=EVENT # symbolic-name abbreviations");
153 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
156 e
= event_symbols
[i
].event
;
157 type
= PERF_COUNTER_TYPE(e
);
158 id
= PERF_COUNTER_ID(e
);
160 printf("\n %d:%d: %-20s",
161 type
, id
, event_symbols
[i
].symbol
);
165 " rNNN: raw PMU events (eventsel+umask)\n\n");
168 static void display_help(void)
171 "Usage: perf-record [<options>]\n"
172 "perf-record Options (up to %d event types can be specified at once):\n\n",
175 display_events_help();
178 " -c CNT --count=CNT # event period to sample\n"
179 " -m pages --mmap_pages=<pages> # number of mmap data pages\n"
180 " -o file --output=<file> # output file\n"
181 " -r prio --realtime=<prio> # use RT prio\n"
187 static void process_options(int argc
, char *argv
[])
189 int error
= 0, counter
;
192 int option_index
= 0;
193 /** Options for getopt */
194 static struct option long_options
[] = {
195 {"count", required_argument
, NULL
, 'c'},
196 {"event", required_argument
, NULL
, 'e'},
197 {"mmap_pages", required_argument
, NULL
, 'm'},
198 {"output", required_argument
, NULL
, 'o'},
199 {"realtime", required_argument
, NULL
, 'r'},
202 int c
= getopt_long(argc
, argv
, "+:c:e:m:o:r:",
203 long_options
, &option_index
);
208 case 'c': default_interval
= atoi(optarg
); break;
209 case 'e': error
= parse_events(optarg
); break;
210 case 'm': mmap_pages
= atoi(optarg
); break;
211 case 'o': output_name
= strdup(optarg
); break;
212 case 'r': realtime_prio
= atoi(optarg
); break;
213 default: error
= 1; break;
224 for (counter
= 0; counter
< nr_counters
; counter
++) {
225 if (event_count
[counter
])
228 event_count
[counter
] = default_interval
;
239 static unsigned int mmap_read_head(struct mmap_data
*md
)
241 struct perf_counter_mmap_page
*pc
= md
->base
;
244 head
= pc
->data_head
;
251 static struct timeval last_read
, this_read
;
253 static void mmap_read(struct mmap_data
*md
)
255 unsigned int head
= mmap_read_head(md
);
256 unsigned int old
= md
->prev
;
257 unsigned char *data
= md
->base
+ page_size
;
262 gettimeofday(&this_read
, NULL
);
265 * If we're further behind than half the buffer, there's a chance
266 * the writer will bite our tail and screw up the events under us.
268 * If we somehow ended up ahead of the head, we got messed up.
270 * In either case, truncate and restart at head.
273 if (diff
> md
->mask
/ 2 || diff
< 0) {
277 timersub(&this_read
, &last_read
, &iv
);
278 msecs
= iv
.tv_sec
*1000 + iv
.tv_usec
/1000;
280 fprintf(stderr
, "WARNING: failed to keep up with mmap data."
281 " Last read %lu msecs ago.\n", msecs
);
284 * head points to a known good entry, start there.
289 last_read
= this_read
;
296 if ((old
& md
->mask
) + size
!= (head
& md
->mask
)) {
297 buf
= &data
[old
& md
->mask
];
298 size
= md
->mask
+ 1 - (old
& md
->mask
);
301 int ret
= write(output
, buf
, size
);
303 perror("failed to write");
311 buf
= &data
[old
& md
->mask
];
315 int ret
= write(output
, buf
, size
);
317 perror("failed to write");
327 static volatile int done
= 0;
329 static void sigchld_handler(int sig
)
335 int cmd_record(int argc
, const char **argv
)
337 struct pollfd event_array
[MAX_NR_CPUS
* MAX_COUNTERS
];
338 struct mmap_data mmap_array
[MAX_NR_CPUS
][MAX_COUNTERS
];
339 struct perf_counter_hw_event hw_event
;
340 int i
, counter
, group_fd
, nr_poll
= 0;
344 page_size
= sysconf(_SC_PAGE_SIZE
);
346 process_options(argc
, argv
);
348 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
349 assert(nr_cpus
<= MAX_NR_CPUS
);
350 assert(nr_cpus
>= 0);
352 output
= open(output_name
, O_CREAT
|O_RDWR
, S_IRWXU
);
354 perror("failed to create output file");
361 for (i
= 0; i
< nr_cpus
; i
++) {
363 for (counter
= 0; counter
< nr_counters
; counter
++) {
365 memset(&hw_event
, 0, sizeof(hw_event
));
366 hw_event
.config
= event_id
[counter
];
367 hw_event
.irq_period
= event_count
[counter
];
368 hw_event
.record_type
= PERF_RECORD_IP
| PERF_RECORD_TID
;
373 fd
[i
][counter
] = sys_perf_counter_open(&hw_event
, -1, i
, group_fd
, 0);
374 if (fd
[i
][counter
] < 0) {
376 printf("kerneltop error: syscall returned with %d (%s)\n",
377 fd
[i
][counter
], strerror(err
));
379 printf("Are you root?\n");
382 assert(fd
[i
][counter
] >= 0);
383 fcntl(fd
[i
][counter
], F_SETFL
, O_NONBLOCK
);
386 * First counter acts as the group leader:
388 if (group
&& group_fd
== -1)
389 group_fd
= fd
[i
][counter
];
391 event_array
[nr_poll
].fd
= fd
[i
][counter
];
392 event_array
[nr_poll
].events
= POLLIN
;
395 mmap_array
[i
][counter
].counter
= counter
;
396 mmap_array
[i
][counter
].prev
= 0;
397 mmap_array
[i
][counter
].mask
= mmap_pages
*page_size
- 1;
398 mmap_array
[i
][counter
].base
= mmap(NULL
, (mmap_pages
+1)*page_size
,
399 PROT_READ
, MAP_SHARED
, fd
[i
][counter
], 0);
400 if (mmap_array
[i
][counter
].base
== MAP_FAILED
) {
401 printf("kerneltop error: failed to mmap with %d (%s)\n",
402 errno
, strerror(errno
));
408 signal(SIGCHLD
, sigchld_handler
);
412 perror("failed to fork");
415 if (execvp(argv
[0], argv
)) {
422 struct sched_param param
;
424 param
.sched_priority
= realtime_prio
;
425 if (sched_setscheduler(0, SCHED_FIFO
, ¶m
)) {
426 printf("Could not set realtime priority.\n");
432 * TODO: store the current /proc/$/maps information somewhere
438 for (i
= 0; i
< nr_cpus
; i
++) {
439 for (counter
= 0; counter
< nr_counters
; counter
++)
440 mmap_read(&mmap_array
[i
][counter
]);
444 ret
= poll(event_array
, nr_poll
, 100);