1 // SPDX-License-Identifier: GPL-2.0-only
3 * sampleip: sample instruction pointer and frequency count in a BPF map.
5 * Copyright 2016 Netflix, Inc.
14 #include <linux/perf_event.h>
15 #include <linux/ptrace.h>
16 #include <linux/bpf.h>
17 #include <sys/ioctl.h>
21 #include "trace_helpers.h"
23 #define DEFAULT_FREQ 99
24 #define DEFAULT_SECS 5
26 #define PAGE_OFFSET 0xffff880000000000
30 static void usage(void)
32 printf("USAGE: sampleip [-F freq] [duration]\n");
33 printf(" -F freq # sample frequency (Hertz), default 99\n");
34 printf(" duration # sampling duration (seconds), default 5\n");
37 static int sampling_start(int *pmu_fd, int freq)
41 struct perf_event_attr pe_sample_attr = {
42 .type = PERF_TYPE_SOFTWARE,
44 .sample_period = freq,
45 .config = PERF_COUNT_SW_CPU_CLOCK,
49 for (i = 0; i < nr_cpus; i++) {
50 pmu_fd[i] = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
51 -1 /* group_fd */, 0 /* flags */);
53 fprintf(stderr, "ERROR: Initializing perf sampling\n");
56 assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF,
58 assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
64 static void sampling_end(int *pmu_fd)
68 for (i = 0; i < nr_cpus; i++)
77 /* used for sorting */
78 struct ipcount counts[MAX_IPS];
80 static int count_cmp(const void *p1, const void *p2)
82 return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count;
85 static void print_ip_map(int fd)
92 printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
94 /* fetch IPs and counts */
96 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
97 bpf_map_lookup_elem(fd, &next_key, &value);
98 counts[i].ip = next_key;
99 counts[i++].count = value;
105 qsort(counts, max, sizeof(struct ipcount), count_cmp);
106 for (i = 0; i < max; i++) {
107 if (counts[i].ip > PAGE_OFFSET) {
108 sym = ksym_search(counts[i].ip);
110 printf("ksym not found. Is kallsyms loaded?\n");
114 printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
117 printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)",
122 if (max == MAX_IPS) {
123 printf("WARNING: IP hash was full (max %d entries); ", max);
124 printf("may have dropped samples\n");
128 static void int_exit(int sig)
131 print_ip_map(map_fd[0]);
135 int main(int argc, char **argv)
138 int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
140 /* process arguments */
141 while ((opt = getopt(argc, argv, "F:h")) != -1) {
152 if (argc - optind == 1)
153 secs = atoi(argv[optind]);
154 if (freq == 0 || secs == 0) {
159 /* initialize kernel symbol translation */
160 if (load_kallsyms()) {
161 fprintf(stderr, "ERROR: loading /proc/kallsyms\n");
165 /* create perf FDs for each CPU */
166 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
167 pmu_fd = malloc(nr_cpus * sizeof(int));
168 if (pmu_fd == NULL) {
169 fprintf(stderr, "ERROR: malloc of pmu_fd\n");
173 /* load BPF program */
174 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
175 if (load_bpf_file(filename)) {
176 fprintf(stderr, "ERROR: loading BPF program (errno %d):\n",
178 if (strcmp(bpf_log_buf, "") == 0)
179 fprintf(stderr, "Try: ulimit -l unlimited\n");
181 fprintf(stderr, "%s", bpf_log_buf);
184 signal(SIGINT, int_exit);
185 signal(SIGTERM, int_exit);
188 printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
190 if (sampling_start(pmu_fd, freq) != 0)
193 sampling_end(pmu_fd);
196 /* output sample counts */
197 print_ip_map(map_fd[0]);