125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
272874418SBrendan Gregg /*
372874418SBrendan Gregg * sampleip: sample instruction pointer and frequency count in a BPF map.
472874418SBrendan Gregg *
572874418SBrendan Gregg * Copyright 2016 Netflix, Inc.
672874418SBrendan Gregg */
772874418SBrendan Gregg #include <stdio.h>
872874418SBrendan Gregg #include <stdlib.h>
972874418SBrendan Gregg #include <unistd.h>
1072874418SBrendan Gregg #include <errno.h>
1172874418SBrendan Gregg #include <signal.h>
1272874418SBrendan Gregg #include <string.h>
1372874418SBrendan Gregg #include <linux/perf_event.h>
1472874418SBrendan Gregg #include <linux/ptrace.h>
1572874418SBrendan Gregg #include <linux/bpf.h>
16aa5e2af6SDaniel T. Lee #include <bpf/bpf.h>
177cf245a3SToke Høiland-Jørgensen #include <bpf/libbpf.h>
18205c8adaSJoe Stringer #include "perf-sys.h"
1928dbf861SYonghong Song #include "trace_helpers.h"
2072874418SBrendan Gregg
2172874418SBrendan Gregg #define DEFAULT_FREQ 99
2272874418SBrendan Gregg #define DEFAULT_SECS 5
2372874418SBrendan Gregg #define MAX_IPS 8192
2472874418SBrendan Gregg
25aa5e2af6SDaniel T. Lee static int map_fd;
2672874418SBrendan Gregg static int nr_cpus;
27*4a1e885cSRong Tao static long _text_addr;
2872874418SBrendan Gregg
usage(void)2972874418SBrendan Gregg static void usage(void)
3072874418SBrendan Gregg {
3172874418SBrendan Gregg printf("USAGE: sampleip [-F freq] [duration]\n");
3272874418SBrendan Gregg printf(" -F freq # sample frequency (Hertz), default 99\n");
3372874418SBrendan Gregg printf(" duration # sampling duration (seconds), default 5\n");
3472874418SBrendan Gregg }
3572874418SBrendan Gregg
sampling_start(int freq,struct bpf_program * prog,struct bpf_link * links[])36aa5e2af6SDaniel T. Lee static int sampling_start(int freq, struct bpf_program *prog,
37aa5e2af6SDaniel T. Lee struct bpf_link *links[])
3872874418SBrendan Gregg {
39aa5e2af6SDaniel T. Lee int i, pmu_fd;
4072874418SBrendan Gregg
4172874418SBrendan Gregg struct perf_event_attr pe_sample_attr = {
4272874418SBrendan Gregg .type = PERF_TYPE_SOFTWARE,
4372874418SBrendan Gregg .freq = 1,
4472874418SBrendan Gregg .sample_period = freq,
4572874418SBrendan Gregg .config = PERF_COUNT_SW_CPU_CLOCK,
4672874418SBrendan Gregg .inherit = 1,
4772874418SBrendan Gregg };
4872874418SBrendan Gregg
4972874418SBrendan Gregg for (i = 0; i < nr_cpus; i++) {
50aa5e2af6SDaniel T. Lee pmu_fd = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
5172874418SBrendan Gregg -1 /* group_fd */, 0 /* flags */);
52aa5e2af6SDaniel T. Lee if (pmu_fd < 0) {
5372874418SBrendan Gregg fprintf(stderr, "ERROR: Initializing perf sampling\n");
5472874418SBrendan Gregg return 1;
5572874418SBrendan Gregg }
56aa5e2af6SDaniel T. Lee links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
570efdcefbSDaniel T. Lee if (libbpf_get_error(links[i])) {
58aa5e2af6SDaniel T. Lee fprintf(stderr, "ERROR: Attach perf event\n");
59aa5e2af6SDaniel T. Lee links[i] = NULL;
60aa5e2af6SDaniel T. Lee close(pmu_fd);
61aa5e2af6SDaniel T. Lee return 1;
62aa5e2af6SDaniel T. Lee }
6372874418SBrendan Gregg }
6472874418SBrendan Gregg
6572874418SBrendan Gregg return 0;
6672874418SBrendan Gregg }
6772874418SBrendan Gregg
sampling_end(struct bpf_link * links[])68aa5e2af6SDaniel T. Lee static void sampling_end(struct bpf_link *links[])
6972874418SBrendan Gregg {
7072874418SBrendan Gregg int i;
7172874418SBrendan Gregg
7272874418SBrendan Gregg for (i = 0; i < nr_cpus; i++)
73aa5e2af6SDaniel T. Lee bpf_link__destroy(links[i]);
7472874418SBrendan Gregg }
7572874418SBrendan Gregg
7672874418SBrendan Gregg struct ipcount {
7772874418SBrendan Gregg __u64 ip;
7872874418SBrendan Gregg __u32 count;
7972874418SBrendan Gregg };
8072874418SBrendan Gregg
8172874418SBrendan Gregg /* used for sorting */
8272874418SBrendan Gregg struct ipcount counts[MAX_IPS];
8372874418SBrendan Gregg
count_cmp(const void * p1,const void * p2)8472874418SBrendan Gregg static int count_cmp(const void *p1, const void *p2)
8572874418SBrendan Gregg {
8672874418SBrendan Gregg return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count;
8772874418SBrendan Gregg }
8872874418SBrendan Gregg
print_ip_map(int fd)8972874418SBrendan Gregg static void print_ip_map(int fd)
9072874418SBrendan Gregg {
9172874418SBrendan Gregg struct ksym *sym;
9272874418SBrendan Gregg __u64 key, next_key;
9372874418SBrendan Gregg __u32 value;
9472874418SBrendan Gregg int i, max;
9572874418SBrendan Gregg
9672874418SBrendan Gregg printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
9772874418SBrendan Gregg
9872874418SBrendan Gregg /* fetch IPs and counts */
9972874418SBrendan Gregg key = 0, i = 0;
100d40fc181SJoe Stringer while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
101d40fc181SJoe Stringer bpf_map_lookup_elem(fd, &next_key, &value);
10272874418SBrendan Gregg counts[i].ip = next_key;
10372874418SBrendan Gregg counts[i++].count = value;
10472874418SBrendan Gregg key = next_key;
10572874418SBrendan Gregg }
10672874418SBrendan Gregg max = i;
10772874418SBrendan Gregg
10872874418SBrendan Gregg /* sort and print */
10972874418SBrendan Gregg qsort(counts, max, sizeof(struct ipcount), count_cmp);
11072874418SBrendan Gregg for (i = 0; i < max; i++) {
111*4a1e885cSRong Tao if (counts[i].ip > _text_addr) {
11272874418SBrendan Gregg sym = ksym_search(counts[i].ip);
113e67b2c71SDaniel T. Lee if (!sym) {
114e67b2c71SDaniel T. Lee printf("ksym not found. Is kallsyms loaded?\n");
115e67b2c71SDaniel T. Lee continue;
116e67b2c71SDaniel T. Lee }
117e67b2c71SDaniel T. Lee
11872874418SBrendan Gregg printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
11972874418SBrendan Gregg counts[i].count);
12072874418SBrendan Gregg } else {
12172874418SBrendan Gregg printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)",
12272874418SBrendan Gregg counts[i].count);
12372874418SBrendan Gregg }
12472874418SBrendan Gregg }
12572874418SBrendan Gregg
12672874418SBrendan Gregg if (max == MAX_IPS) {
12772874418SBrendan Gregg printf("WARNING: IP hash was full (max %d entries); ", max);
12872874418SBrendan Gregg printf("may have dropped samples\n");
12972874418SBrendan Gregg }
13072874418SBrendan Gregg }
13172874418SBrendan Gregg
int_exit(int sig)13272874418SBrendan Gregg static void int_exit(int sig)
13372874418SBrendan Gregg {
13472874418SBrendan Gregg printf("\n");
135aa5e2af6SDaniel T. Lee print_ip_map(map_fd);
13672874418SBrendan Gregg exit(0);
13772874418SBrendan Gregg }
13872874418SBrendan Gregg
main(int argc,char ** argv)13972874418SBrendan Gregg int main(int argc, char **argv)
14072874418SBrendan Gregg {
141aa5e2af6SDaniel T. Lee int opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS, error = 1;
142aa5e2af6SDaniel T. Lee struct bpf_object *obj = NULL;
143aa5e2af6SDaniel T. Lee struct bpf_program *prog;
144aa5e2af6SDaniel T. Lee struct bpf_link **links;
14572874418SBrendan Gregg char filename[256];
14672874418SBrendan Gregg
14772874418SBrendan Gregg /* process arguments */
14872874418SBrendan Gregg while ((opt = getopt(argc, argv, "F:h")) != -1) {
14972874418SBrendan Gregg switch (opt) {
15072874418SBrendan Gregg case 'F':
15172874418SBrendan Gregg freq = atoi(optarg);
15272874418SBrendan Gregg break;
15372874418SBrendan Gregg case 'h':
15472874418SBrendan Gregg default:
15572874418SBrendan Gregg usage();
15672874418SBrendan Gregg return 0;
15772874418SBrendan Gregg }
15872874418SBrendan Gregg }
15972874418SBrendan Gregg if (argc - optind == 1)
16072874418SBrendan Gregg secs = atoi(argv[optind]);
16172874418SBrendan Gregg if (freq == 0 || secs == 0) {
16272874418SBrendan Gregg usage();
16372874418SBrendan Gregg return 1;
16472874418SBrendan Gregg }
16572874418SBrendan Gregg
16672874418SBrendan Gregg /* initialize kernel symbol translation */
16772874418SBrendan Gregg if (load_kallsyms()) {
16872874418SBrendan Gregg fprintf(stderr, "ERROR: loading /proc/kallsyms\n");
16972874418SBrendan Gregg return 2;
17072874418SBrendan Gregg }
17172874418SBrendan Gregg
172*4a1e885cSRong Tao /* used to determine whether the address is kernel space */
173*4a1e885cSRong Tao _text_addr = ksym_get_addr("_text");
174*4a1e885cSRong Tao if (!_text_addr) {
175*4a1e885cSRong Tao fprintf(stderr, "ERROR: no '_text' in /proc/kallsyms\n");
176*4a1e885cSRong Tao return 3;
177*4a1e885cSRong Tao }
178*4a1e885cSRong Tao
17972874418SBrendan Gregg /* create perf FDs for each CPU */
180aa5e2af6SDaniel T. Lee nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
181aa5e2af6SDaniel T. Lee links = calloc(nr_cpus, sizeof(struct bpf_link *));
182aa5e2af6SDaniel T. Lee if (!links) {
183aa5e2af6SDaniel T. Lee fprintf(stderr, "ERROR: malloc of links\n");
184aa5e2af6SDaniel T. Lee goto cleanup;
185aa5e2af6SDaniel T. Lee }
186aa5e2af6SDaniel T. Lee
187aa5e2af6SDaniel T. Lee snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
188aa5e2af6SDaniel T. Lee obj = bpf_object__open_file(filename, NULL);
1890efdcefbSDaniel T. Lee if (libbpf_get_error(obj)) {
190aa5e2af6SDaniel T. Lee fprintf(stderr, "ERROR: opening BPF object file failed\n");
191aa5e2af6SDaniel T. Lee obj = NULL;
192aa5e2af6SDaniel T. Lee goto cleanup;
193aa5e2af6SDaniel T. Lee }
194aa5e2af6SDaniel T. Lee
195aa5e2af6SDaniel T. Lee prog = bpf_object__find_program_by_name(obj, "do_sample");
196aa5e2af6SDaniel T. Lee if (!prog) {
197aa5e2af6SDaniel T. Lee fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
198aa5e2af6SDaniel T. Lee goto cleanup;
19972874418SBrendan Gregg }
20072874418SBrendan Gregg
20172874418SBrendan Gregg /* load BPF program */
202aa5e2af6SDaniel T. Lee if (bpf_object__load(obj)) {
203aa5e2af6SDaniel T. Lee fprintf(stderr, "ERROR: loading BPF object file failed\n");
204aa5e2af6SDaniel T. Lee goto cleanup;
20572874418SBrendan Gregg }
206aa5e2af6SDaniel T. Lee
207aa5e2af6SDaniel T. Lee map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map");
208aa5e2af6SDaniel T. Lee if (map_fd < 0) {
209aa5e2af6SDaniel T. Lee fprintf(stderr, "ERROR: finding a map in obj file failed\n");
210aa5e2af6SDaniel T. Lee goto cleanup;
211aa5e2af6SDaniel T. Lee }
212aa5e2af6SDaniel T. Lee
21372874418SBrendan Gregg signal(SIGINT, int_exit);
214ad990dbeSAndy Gospodarek signal(SIGTERM, int_exit);
21572874418SBrendan Gregg
21672874418SBrendan Gregg /* do sampling */
21772874418SBrendan Gregg printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
21872874418SBrendan Gregg freq, secs);
219aa5e2af6SDaniel T. Lee if (sampling_start(freq, prog, links) != 0)
220aa5e2af6SDaniel T. Lee goto cleanup;
221aa5e2af6SDaniel T. Lee
22272874418SBrendan Gregg sleep(secs);
223aa5e2af6SDaniel T. Lee error = 0;
22472874418SBrendan Gregg
225aa5e2af6SDaniel T. Lee cleanup:
226aa5e2af6SDaniel T. Lee sampling_end(links);
22772874418SBrendan Gregg /* output sample counts */
228aa5e2af6SDaniel T. Lee if (!error)
229aa5e2af6SDaniel T. Lee print_ip_map(map_fd);
23072874418SBrendan Gregg
231aa5e2af6SDaniel T. Lee free(links);
232aa5e2af6SDaniel T. Lee bpf_object__close(obj);
233aa5e2af6SDaniel T. Lee return error;
23472874418SBrendan Gregg }
235