2 * sampleip: sample instruction pointer and frequency count in a BPF map.
4 * Copyright 2016 Netflix, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
18 #include <linux/perf_event.h>
19 #include <linux/ptrace.h>
20 #include <linux/bpf.h>
21 #include <sys/ioctl.h>
25 #include "trace_helpers.h"
27 #define DEFAULT_FREQ 99
28 #define DEFAULT_SECS 5
30 #define PAGE_OFFSET 0xffff880000000000
34 static void usage(void)
36 printf("USAGE: sampleip [-F freq] [duration]\n");
37 printf(" -F freq # sample frequency (Hertz), default 99\n");
38 printf(" duration # sampling duration (seconds), default 5\n");
41 static int sampling_start(int *pmu_fd, int freq)
45 struct perf_event_attr pe_sample_attr = {
46 .type = PERF_TYPE_SOFTWARE,
48 .sample_period = freq,
49 .config = PERF_COUNT_SW_CPU_CLOCK,
53 for (i = 0; i < nr_cpus; i++) {
54 pmu_fd[i] = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
55 -1 /* group_fd */, 0 /* flags */);
57 fprintf(stderr, "ERROR: Initializing perf sampling\n");
60 assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF,
62 assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
68 static void sampling_end(int *pmu_fd)
72 for (i = 0; i < nr_cpus; i++)
81 /* used for sorting */
82 struct ipcount counts[MAX_IPS];
84 static int count_cmp(const void *p1, const void *p2)
86 return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count;
89 static void print_ip_map(int fd)
96 printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
98 /* fetch IPs and counts */
100 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
101 bpf_map_lookup_elem(fd, &next_key, &value);
102 counts[i].ip = next_key;
103 counts[i++].count = value;
109 qsort(counts, max, sizeof(struct ipcount), count_cmp);
110 for (i = 0; i < max; i++) {
111 if (counts[i].ip > PAGE_OFFSET) {
112 sym = ksym_search(counts[i].ip);
113 printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
116 printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)",
121 if (max == MAX_IPS) {
122 printf("WARNING: IP hash was full (max %d entries); ", max);
123 printf("may have dropped samples\n");
127 static void int_exit(int sig)
130 print_ip_map(map_fd[0]);
134 int main(int argc, char **argv)
137 int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
139 /* process arguments */
140 while ((opt = getopt(argc, argv, "F:h")) != -1) {
151 if (argc - optind == 1)
152 secs = atoi(argv[optind]);
153 if (freq == 0 || secs == 0) {
158 /* initialize kernel symbol translation */
159 if (load_kallsyms()) {
160 fprintf(stderr, "ERROR: loading /proc/kallsyms\n");
164 /* create perf FDs for each CPU */
165 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
166 pmu_fd = malloc(nr_cpus * sizeof(int));
167 if (pmu_fd == NULL) {
168 fprintf(stderr, "ERROR: malloc of pmu_fd\n");
172 /* load BPF program */
173 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
174 if (load_bpf_file(filename)) {
175 fprintf(stderr, "ERROR: loading BPF program (errno %d):\n",
177 if (strcmp(bpf_log_buf, "") == 0)
178 fprintf(stderr, "Try: ulimit -l unlimited\n");
180 fprintf(stderr, "%s", bpf_log_buf);
183 signal(SIGINT, int_exit);
184 signal(SIGTERM, int_exit);
187 printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
189 if (sampling_start(pmu_fd, freq) != 0)
192 sampling_end(pmu_fd);
195 /* output sample counts */
196 print_ip_map(map_fd[0]);