blob: 4372d2da2f9e48bf2aab12f5a788bf5434501d40 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Brendan Gregg72874412016-09-01 18:37:26 -07002/*
3 * sampleip: sample instruction pointer and frequency count in a BPF map.
4 *
5 * Copyright 2016 Netflix, Inc.
Brendan Gregg72874412016-09-01 18:37:26 -07006 */
7#include <stdio.h>
8#include <stdlib.h>
Brendan Gregg72874412016-09-01 18:37:26 -07009#include <unistd.h>
10#include <errno.h>
11#include <signal.h>
12#include <string.h>
Brendan Gregg72874412016-09-01 18:37:26 -070013#include <linux/perf_event.h>
14#include <linux/ptrace.h>
15#include <linux/bpf.h>
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090016#include <bpf/bpf.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010017#include <bpf/libbpf.h>
Joe Stringer205c8ad2016-12-08 18:46:19 -080018#include "perf-sys.h"
Yonghong Song28dbf862018-04-28 22:28:13 -070019#include "trace_helpers.h"
Brendan Gregg72874412016-09-01 18:37:26 -070020
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090021#define __must_check
22#include <linux/err.h>
23
Brendan Gregg72874412016-09-01 18:37:26 -070024#define DEFAULT_FREQ 99
25#define DEFAULT_SECS 5
26#define MAX_IPS 8192
27#define PAGE_OFFSET 0xffff880000000000
28
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090029static int map_fd;
Brendan Gregg72874412016-09-01 18:37:26 -070030static int nr_cpus;
31
32static void usage(void)
33{
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");
37}
38
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090039static int sampling_start(int freq, struct bpf_program *prog,
40 struct bpf_link *links[])
Brendan Gregg72874412016-09-01 18:37:26 -070041{
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090042 int i, pmu_fd;
Brendan Gregg72874412016-09-01 18:37:26 -070043
44 struct perf_event_attr pe_sample_attr = {
45 .type = PERF_TYPE_SOFTWARE,
46 .freq = 1,
47 .sample_period = freq,
48 .config = PERF_COUNT_SW_CPU_CLOCK,
49 .inherit = 1,
50 };
51
52 for (i = 0; i < nr_cpus; i++) {
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090053 pmu_fd = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
Brendan Gregg72874412016-09-01 18:37:26 -070054 -1 /* group_fd */, 0 /* flags */);
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090055 if (pmu_fd < 0) {
Brendan Gregg72874412016-09-01 18:37:26 -070056 fprintf(stderr, "ERROR: Initializing perf sampling\n");
57 return 1;
58 }
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090059 links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
60 if (IS_ERR(links[i])) {
61 fprintf(stderr, "ERROR: Attach perf event\n");
62 links[i] = NULL;
63 close(pmu_fd);
64 return 1;
65 }
Brendan Gregg72874412016-09-01 18:37:26 -070066 }
67
68 return 0;
69}
70
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090071static void sampling_end(struct bpf_link *links[])
Brendan Gregg72874412016-09-01 18:37:26 -070072{
73 int i;
74
75 for (i = 0; i < nr_cpus; i++)
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +090076 bpf_link__destroy(links[i]);
Brendan Gregg72874412016-09-01 18:37:26 -070077}
78
79struct ipcount {
80 __u64 ip;
81 __u32 count;
82};
83
84/* used for sorting */
85struct ipcount counts[MAX_IPS];
86
87static int count_cmp(const void *p1, const void *p2)
88{
89 return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count;
90}
91
92static void print_ip_map(int fd)
93{
94 struct ksym *sym;
95 __u64 key, next_key;
96 __u32 value;
97 int i, max;
98
99 printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
100
101 /* fetch IPs and counts */
102 key = 0, i = 0;
Joe Stringerd40fc182016-12-14 14:43:38 -0800103 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
104 bpf_map_lookup_elem(fd, &next_key, &value);
Brendan Gregg72874412016-09-01 18:37:26 -0700105 counts[i].ip = next_key;
106 counts[i++].count = value;
107 key = next_key;
108 }
109 max = i;
110
111 /* sort and print */
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);
Daniel T. Leee67b2c72019-04-04 07:17:56 +0900116 if (!sym) {
117 printf("ksym not found. Is kallsyms loaded?\n");
118 continue;
119 }
120
Brendan Gregg72874412016-09-01 18:37:26 -0700121 printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
122 counts[i].count);
123 } else {
124 printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)",
125 counts[i].count);
126 }
127 }
128
129 if (max == MAX_IPS) {
130 printf("WARNING: IP hash was full (max %d entries); ", max);
131 printf("may have dropped samples\n");
132 }
133}
134
135static void int_exit(int sig)
136{
137 printf("\n");
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900138 print_ip_map(map_fd);
Brendan Gregg72874412016-09-01 18:37:26 -0700139 exit(0);
140}
141
142int main(int argc, char **argv)
143{
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900144 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;
Brendan Gregg72874412016-09-01 18:37:26 -0700148 char filename[256];
Brendan Gregg72874412016-09-01 18:37:26 -0700149
150 /* process arguments */
151 while ((opt = getopt(argc, argv, "F:h")) != -1) {
152 switch (opt) {
153 case 'F':
154 freq = atoi(optarg);
155 break;
156 case 'h':
157 default:
158 usage();
159 return 0;
160 }
161 }
162 if (argc - optind == 1)
163 secs = atoi(argv[optind]);
164 if (freq == 0 || secs == 0) {
165 usage();
166 return 1;
167 }
168
169 /* initialize kernel symbol translation */
170 if (load_kallsyms()) {
171 fprintf(stderr, "ERROR: loading /proc/kallsyms\n");
172 return 2;
173 }
174
175 /* create perf FDs for each CPU */
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900176 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
177 links = calloc(nr_cpus, sizeof(struct bpf_link *));
178 if (!links) {
179 fprintf(stderr, "ERROR: malloc of links\n");
180 goto cleanup;
181 }
182
183 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
184 obj = bpf_object__open_file(filename, NULL);
185 if (IS_ERR(obj)) {
186 fprintf(stderr, "ERROR: opening BPF object file failed\n");
187 obj = NULL;
188 goto cleanup;
189 }
190
191 prog = bpf_object__find_program_by_name(obj, "do_sample");
192 if (!prog) {
193 fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
194 goto cleanup;
Brendan Gregg72874412016-09-01 18:37:26 -0700195 }
196
197 /* load BPF program */
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900198 if (bpf_object__load(obj)) {
199 fprintf(stderr, "ERROR: loading BPF object file failed\n");
200 goto cleanup;
Brendan Gregg72874412016-09-01 18:37:26 -0700201 }
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900202
203 map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map");
204 if (map_fd < 0) {
205 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
206 goto cleanup;
207 }
208
Brendan Gregg72874412016-09-01 18:37:26 -0700209 signal(SIGINT, int_exit);
Andy Gospodarekad990db2017-05-11 15:52:30 -0400210 signal(SIGTERM, int_exit);
Brendan Gregg72874412016-09-01 18:37:26 -0700211
212 /* do sampling */
213 printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
214 freq, secs);
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900215 if (sampling_start(freq, prog, links) != 0)
216 goto cleanup;
217
Brendan Gregg72874412016-09-01 18:37:26 -0700218 sleep(secs);
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900219 error = 0;
Brendan Gregg72874412016-09-01 18:37:26 -0700220
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900221cleanup:
222 sampling_end(links);
Brendan Gregg72874412016-09-01 18:37:26 -0700223 /* output sample counts */
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900224 if (!error)
225 print_ip_map(map_fd);
Brendan Gregg72874412016-09-01 18:37:26 -0700226
Daniel T. Leeaa5e2af2020-03-21 19:04:24 +0900227 free(links);
228 bpf_object__close(obj);
229 return error;
Brendan Gregg72874412016-09-01 18:37:26 -0700230}