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"
21 #define DEFAULT_FREQ 99
22 #define DEFAULT_SECS 5
24 #define PAGE_OFFSET 0xffff880000000000
29 static void usage(void)
31 printf("USAGE: sampleip [-F freq] [duration]\n");
32 printf(" -F freq # sample frequency (Hertz), default 99\n");
33 printf(" duration # sampling duration (seconds), default 5\n");
36 static int sampling_start(int freq
, struct bpf_program
*prog
,
37 struct bpf_link
*links
[])
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
= 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 links
[i
] = bpf_program__attach_perf_event(prog
, pmu_fd
);
57 if (libbpf_get_error(links
[i
])) {
58 fprintf(stderr
, "ERROR: Attach perf event\n");
68 static void sampling_end(struct bpf_link
*links
[])
72 for (i
= 0; i
< nr_cpus
; i
++)
73 bpf_link__destroy(links
[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
);
114 printf("ksym not found. Is kallsyms loaded?\n");
118 printf("0x%-17llx %-32s %u\n", counts
[i
].ip
, sym
->name
,
121 printf("0x%-17llx %-32s %u\n", counts
[i
].ip
, "(user)",
126 if (max
== MAX_IPS
) {
127 printf("WARNING: IP hash was full (max %d entries); ", max
);
128 printf("may have dropped samples\n");
132 static void int_exit(int sig
)
135 print_ip_map(map_fd
);
139 int main(int argc
, char **argv
)
141 int opt
, freq
= DEFAULT_FREQ
, secs
= DEFAULT_SECS
, error
= 1;
142 struct bpf_object
*obj
= NULL
;
143 struct bpf_program
*prog
;
144 struct bpf_link
**links
;
147 /* process arguments */
148 while ((opt
= getopt(argc
, argv
, "F:h")) != -1) {
159 if (argc
- optind
== 1)
160 secs
= atoi(argv
[optind
]);
161 if (freq
== 0 || secs
== 0) {
166 /* initialize kernel symbol translation */
167 if (load_kallsyms()) {
168 fprintf(stderr
, "ERROR: loading /proc/kallsyms\n");
172 /* create perf FDs for each CPU */
173 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
174 links
= calloc(nr_cpus
, sizeof(struct bpf_link
*));
176 fprintf(stderr
, "ERROR: malloc of links\n");
180 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
181 obj
= bpf_object__open_file(filename
, NULL
);
182 if (libbpf_get_error(obj
)) {
183 fprintf(stderr
, "ERROR: opening BPF object file failed\n");
188 prog
= bpf_object__find_program_by_name(obj
, "do_sample");
190 fprintf(stderr
, "ERROR: finding a prog in obj file failed\n");
194 /* load BPF program */
195 if (bpf_object__load(obj
)) {
196 fprintf(stderr
, "ERROR: loading BPF object file failed\n");
200 map_fd
= bpf_object__find_map_fd_by_name(obj
, "ip_map");
202 fprintf(stderr
, "ERROR: finding a map in obj file failed\n");
206 signal(SIGINT
, int_exit
);
207 signal(SIGTERM
, int_exit
);
210 printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
212 if (sampling_start(freq
, prog
, links
) != 0)
220 /* output sample counts */
222 print_ip_map(map_fd
);
225 bpf_object__close(obj
);