6 #include "util/parse-options.h"
7 #include "util/parse-events.h"
8 #include "util/string.h"
13 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
14 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
16 static int default_interval = 100000;
17 static int event_count[MAX_COUNTERS];
19 static int fd[MAX_NR_CPUS][MAX_COUNTERS];
20 static int nr_cpus = 0;
21 static unsigned int page_size;
22 static unsigned int mmap_pages = 16;
24 static const char *output_name = "perf.data";
26 static unsigned int realtime_prio = 0;
27 static int system_wide = 0;
28 static pid_t target_pid = -1;
29 static int inherit = 1;
32 const unsigned int default_count[] = {
48 static unsigned int mmap_read_head(struct mmap_data *md)
50 struct perf_counter_mmap_page *pc = md->base;
60 static struct timeval last_read, this_read;
62 static void mmap_read(struct mmap_data *md)
64 unsigned int head = mmap_read_head(md);
65 unsigned int old = md->prev;
66 unsigned char *data = md->base + page_size;
71 gettimeofday(&this_read, NULL);
74 * If we're further behind than half the buffer, there's a chance
75 * the writer will bite our tail and screw up the events under us.
77 * If we somehow ended up ahead of the head, we got messed up.
79 * In either case, truncate and restart at head.
82 if (diff > md->mask / 2 || diff < 0) {
86 timersub(&this_read, &last_read, &iv);
87 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
89 fprintf(stderr, "WARNING: failed to keep up with mmap data."
90 " Last read %lu msecs ago.\n", msecs);
93 * head points to a known good entry, start there.
98 last_read = this_read;
105 if ((old & md->mask) + size != (head & md->mask)) {
106 buf = &data[old & md->mask];
107 size = md->mask + 1 - (old & md->mask);
110 int ret = write(output, buf, size);
112 perror("failed to write");
120 buf = &data[old & md->mask];
124 int ret = write(output, buf, size);
126 perror("failed to write");
136 static volatile int done = 0;
138 static void sig_handler(int sig)
143 static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
144 static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
150 struct perf_event_header header;
156 char filename[PATH_MAX];
160 struct perf_event_header header;
166 static void pid_synthesize_comm_event(pid_t pid, int full)
168 struct comm_event comm_ev;
169 char filename[PATH_MAX];
175 struct dirent dirent, *next;
177 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
179 fd = open(filename, O_RDONLY);
181 fprintf(stderr, "couldn't open %s\n", filename);
184 if (read(fd, bf, sizeof(bf)) < 0) {
185 fprintf(stderr, "couldn't read %s\n", filename);
190 /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
191 memset(&comm_ev, 0, sizeof(comm_ev));
192 field = strchr(bf, '(');
195 sep = strchr(++field, ')');
199 memcpy(comm_ev.comm, field, size++);
202 comm_ev.header.type = PERF_EVENT_COMM;
203 size = ALIGN(size, sizeof(uint64_t));
204 comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
209 ret = write(output, &comm_ev, comm_ev.header.size);
211 perror("failed to write");
217 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
219 tasks = opendir(filename);
220 while (!readdir_r(tasks, &dirent, &next) && next) {
222 pid = strtol(dirent.d_name, &end, 10);
228 ret = write(output, &comm_ev, comm_ev.header.size);
230 perror("failed to write");
238 fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
243 static void pid_synthesize_mmap_events(pid_t pid)
245 char filename[PATH_MAX];
248 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
250 fp = fopen(filename, "r");
252 fprintf(stderr, "couldn't open %s\n", filename);
256 char bf[BUFSIZ], *pbf = bf;
257 struct mmap_event mmap_ev = {
258 .header.type = PERF_EVENT_MMAP,
262 if (fgets(bf, sizeof(bf), fp) == NULL)
265 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
266 n = hex2u64(pbf, &mmap_ev.start);
270 n = hex2u64(pbf, &mmap_ev.len);
274 if (*pbf == 'x') { /* vm_exec */
275 char *execname = strrchr(bf, ' ');
277 if (execname == NULL || execname[1] != '/')
281 size = strlen(execname);
282 execname[size - 1] = '\0'; /* Remove \n */
283 memcpy(mmap_ev.filename, execname, size);
284 size = ALIGN(size, sizeof(uint64_t));
285 mmap_ev.len -= mmap_ev.start;
286 mmap_ev.header.size = (sizeof(mmap_ev) -
287 (sizeof(mmap_ev.filename) - size));
291 if (write(output, &mmap_ev, mmap_ev.header.size) < 0) {
292 perror("failed to write");
301 static void synthesize_events(void)
304 struct dirent dirent, *next;
306 proc = opendir("/proc");
308 while (!readdir_r(proc, &dirent, &next) && next) {
312 pid = strtol(dirent.d_name, &end, 10);
313 if (*end) /* only interested in proper numerical dirents */
316 pid_synthesize_comm_event(pid, 1);
317 pid_synthesize_mmap_events(pid);
323 static void open_counters(int cpu, pid_t pid)
325 struct perf_counter_hw_event hw_event;
326 int counter, group_fd;
330 pid_synthesize_comm_event(pid, 0);
331 pid_synthesize_mmap_events(pid);
335 for (counter = 0; counter < nr_counters; counter++) {
337 memset(&hw_event, 0, sizeof(hw_event));
338 hw_event.config = event_id[counter];
339 hw_event.irq_period = event_count[counter];
340 hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID;
341 hw_event.mmap = track;
342 hw_event.comm = track;
343 hw_event.inherit = (cpu < 0) && inherit;
345 track = 0; // only the first counter needs these
347 fd[nr_cpu][counter] =
348 sys_perf_counter_open(&hw_event, pid, cpu, group_fd, 0);
350 if (fd[nr_cpu][counter] < 0) {
352 printf("kerneltop error: syscall returned with %d (%s)\n",
353 fd[nr_cpu][counter], strerror(err));
355 printf("Are you root?\n");
358 assert(fd[nr_cpu][counter] >= 0);
359 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
362 * First counter acts as the group leader:
364 if (group && group_fd == -1)
365 group_fd = fd[nr_cpu][counter];
367 event_array[nr_poll].fd = fd[nr_cpu][counter];
368 event_array[nr_poll].events = POLLIN;
371 mmap_array[nr_cpu][counter].counter = counter;
372 mmap_array[nr_cpu][counter].prev = 0;
373 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
374 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
375 PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
376 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
377 printf("kerneltop error: failed to mmap with %d (%s)\n",
378 errno, strerror(errno));
385 static int __cmd_record(int argc, const char **argv)
392 page_size = sysconf(_SC_PAGE_SIZE);
393 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
394 assert(nr_cpus <= MAX_NR_CPUS);
395 assert(nr_cpus >= 0);
397 if (!stat(output_name, &st) && !force) {
398 fprintf(stderr, "Error, output file: %s exists, use -f to overwrite.\n",
403 output = open(output_name, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR);
405 perror("failed to create output file");
410 open_counters(-1, target_pid != -1 ? target_pid : 0);
411 } else for (i = 0; i < nr_cpus; i++)
412 open_counters(i, target_pid);
414 signal(SIGCHLD, sig_handler);
415 signal(SIGINT, sig_handler);
417 if (target_pid == -1 && argc) {
420 perror("failed to fork");
423 if (execvp(argv[0], (char **)argv)) {
431 struct sched_param param;
433 param.sched_priority = realtime_prio;
434 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
435 printf("Could not set realtime priority.\n");
446 for (i = 0; i < nr_cpu; i++) {
447 for (counter = 0; counter < nr_counters; counter++)
448 mmap_read(&mmap_array[i][counter]);
452 ret = poll(event_array, nr_poll, 100);
458 static const char * const record_usage[] = {
459 "perf record [<options>] [<command>]",
460 "perf record [<options>] -- <command> [<options>]",
464 static char events_help_msg[EVENTS_HELP_MAX];
466 static const struct option options[] = {
467 OPT_CALLBACK('e', "event", NULL, "event",
468 events_help_msg, parse_events),
469 OPT_INTEGER('c', "count", &default_interval,
470 "event period to sample"),
471 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
472 "number of mmap data pages"),
473 OPT_STRING('o', "output", &output_name, "file",
475 OPT_BOOLEAN('i', "inherit", &inherit,
476 "child tasks inherit counters"),
477 OPT_INTEGER('p', "pid", &target_pid,
478 "record events on existing pid"),
479 OPT_INTEGER('r', "realtime", &realtime_prio,
480 "collect data with this RT SCHED_FIFO priority"),
481 OPT_BOOLEAN('a', "all-cpus", &system_wide,
482 "system-wide collection from all CPUs"),
483 OPT_BOOLEAN('f', "force", &force,
484 "overwrite existing data file"),
488 int cmd_record(int argc, const char **argv, const char *prefix)
492 create_events_help(events_help_msg);
494 argc = parse_options(argc, argv, options, record_usage, 0);
495 if (!argc && target_pid == -1 && !system_wide)
496 usage_with_options(record_usage, options);
503 for (counter = 0; counter < nr_counters; counter++) {
504 if (event_count[counter])
507 event_count[counter] = default_interval;
510 return __cmd_record(argc, argv);