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.
13 #include <linux/perf_event.h>
14 #include <linux/ptrace.h>
15 #include <linux/bpf.h>
17 #include <bpf/libbpf.h>
19 #include "trace_helpers.h"
22 #include <linux/err.h>
24 #define DEFAULT_FREQ 99
25 #define DEFAULT_SECS 5
27 #define PAGE_OFFSET 0xffff880000000000
32 static void usage(void)
34 printf("USAGE: sampleip [-F freq] [duration]\n");
35 printf(" -F freq # sample frequency (Hertz), default 99\n");
36 printf(" duration # sampling duration (seconds), default 5\n");
39 static int sampling_start(int freq, struct bpf_program *prog,
40 struct bpf_link *links[])
44 struct perf_event_attr pe_sample_attr = {
45 .type = PERF_TYPE_SOFTWARE,
47 .sample_period = freq,
48 .config = PERF_COUNT_SW_CPU_CLOCK,
52 for (i = 0; i < nr_cpus; i++) {
53 pmu_fd = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
54 -1 /* group_fd */, 0 /* flags */);
56 fprintf(stderr, "ERROR: Initializing perf sampling\n");
59 links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
60 if (IS_ERR(links[i])) {
61 fprintf(stderr, "ERROR: Attach perf event\n");
71 static void sampling_end(struct bpf_link *links[])
75 for (i = 0; i < nr_cpus; i++)
76 bpf_link__destroy(links[i]);
84 /* used for sorting */
85 struct ipcount counts[MAX_IPS];
87 static int count_cmp(const void *p1, const void *p2)
89 return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count;
92 static void print_ip_map(int fd)
99 printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
101 /* fetch IPs and counts */
103 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
104 bpf_map_lookup_elem(fd, &next_key, &value);
105 counts[i].ip = next_key;
106 counts[i++].count = value;
112 qsort(counts, max, sizeof(struct ipcount), count_cmp);
113 for (i = 0; i < max; i++) {
114 if (counts[i].ip > PAGE_OFFSET) {
115 sym = ksym_search(counts[i].ip);
117 printf("ksym not found. Is kallsyms loaded?\n");
121 printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
124 printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)",
129 if (max == MAX_IPS) {
130 printf("WARNING: IP hash was full (max %d entries); ", max);
131 printf("may have dropped samples\n");
135 static void int_exit(int sig)
138 print_ip_map(map_fd);
142 int main(int argc, char **argv)
144 int opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS, error = 1;
145 struct bpf_object *obj = NULL;
146 struct bpf_program *prog;
147 struct bpf_link **links;
150 /* process arguments */
151 while ((opt = getopt(argc, argv, "F:h")) != -1) {
162 if (argc - optind == 1)
163 secs = atoi(argv[optind]);
164 if (freq == 0 || secs == 0) {
169 /* initialize kernel symbol translation */
170 if (load_kallsyms()) {
171 fprintf(stderr, "ERROR: loading /proc/kallsyms\n");
175 /* create perf FDs for each CPU */
176 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
177 links = calloc(nr_cpus, sizeof(struct bpf_link *));
179 fprintf(stderr, "ERROR: malloc of links\n");
183 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
184 obj = bpf_object__open_file(filename, NULL);
186 fprintf(stderr, "ERROR: opening BPF object file failed\n");
191 prog = bpf_object__find_program_by_name(obj, "do_sample");
193 fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
197 /* load BPF program */
198 if (bpf_object__load(obj)) {
199 fprintf(stderr, "ERROR: loading BPF object file failed\n");
203 map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map");
205 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
209 signal(SIGINT, int_exit);
210 signal(SIGTERM, int_exit);
213 printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
215 if (sampling_start(freq, prog, links) != 0)
223 /* output sample counts */
225 print_ip_map(map_fd);
228 bpf_object__close(obj);