xref: /openbmc/linux/samples/bpf/sampleip_user.c (revision 9a29ad52)
1 /*
2  * sampleip: sample instruction pointer and frequency count in a BPF map.
3  *
4  * Copyright 2016 Netflix, Inc.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <assert.h>
18 #include <linux/perf_event.h>
19 #include <linux/ptrace.h>
20 #include <linux/bpf.h>
21 #include <sys/ioctl.h>
22 #include "libbpf.h"
23 #include "bpf_load.h"
24 #include "perf-sys.h"
25 #include "trace_helpers.h"
26 
27 #define DEFAULT_FREQ	99
28 #define DEFAULT_SECS	5
29 #define MAX_IPS		8192
30 #define PAGE_OFFSET	0xffff880000000000
31 
32 static int nr_cpus;
33 
34 static void usage(void)
35 {
36 	printf("USAGE: sampleip [-F freq] [duration]\n");
37 	printf("       -F freq    # sample frequency (Hertz), default 99\n");
38 	printf("       duration   # sampling duration (seconds), default 5\n");
39 }
40 
41 static int sampling_start(int *pmu_fd, int freq)
42 {
43 	int i;
44 
45 	struct perf_event_attr pe_sample_attr = {
46 		.type = PERF_TYPE_SOFTWARE,
47 		.freq = 1,
48 		.sample_period = freq,
49 		.config = PERF_COUNT_SW_CPU_CLOCK,
50 		.inherit = 1,
51 	};
52 
53 	for (i = 0; i < nr_cpus; i++) {
54 		pmu_fd[i] = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
55 					    -1 /* group_fd */, 0 /* flags */);
56 		if (pmu_fd[i] < 0) {
57 			fprintf(stderr, "ERROR: Initializing perf sampling\n");
58 			return 1;
59 		}
60 		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF,
61 			     prog_fd[0]) == 0);
62 		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
63 	}
64 
65 	return 0;
66 }
67 
68 static void sampling_end(int *pmu_fd)
69 {
70 	int i;
71 
72 	for (i = 0; i < nr_cpus; i++)
73 		close(pmu_fd[i]);
74 }
75 
76 struct ipcount {
77 	__u64 ip;
78 	__u32 count;
79 };
80 
81 /* used for sorting */
82 struct ipcount counts[MAX_IPS];
83 
84 static int count_cmp(const void *p1, const void *p2)
85 {
86 	return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count;
87 }
88 
89 static void print_ip_map(int fd)
90 {
91 	struct ksym *sym;
92 	__u64 key, next_key;
93 	__u32 value;
94 	int i, max;
95 
96 	printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
97 
98 	/* fetch IPs and counts */
99 	key = 0, i = 0;
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;
104 		key = next_key;
105 	}
106 	max = i;
107 
108 	/* sort and print */
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);
113 			printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
114 			       counts[i].count);
115 		} else {
116 			printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)",
117 			       counts[i].count);
118 		}
119 	}
120 
121 	if (max == MAX_IPS) {
122 		printf("WARNING: IP hash was full (max %d entries); ", max);
123 		printf("may have dropped samples\n");
124 	}
125 }
126 
127 static void int_exit(int sig)
128 {
129 	printf("\n");
130 	print_ip_map(map_fd[0]);
131 	exit(0);
132 }
133 
134 int main(int argc, char **argv)
135 {
136 	char filename[256];
137 	int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
138 
139 	/* process arguments */
140 	while ((opt = getopt(argc, argv, "F:h")) != -1) {
141 		switch (opt) {
142 		case 'F':
143 			freq = atoi(optarg);
144 			break;
145 		case 'h':
146 		default:
147 			usage();
148 			return 0;
149 		}
150 	}
151 	if (argc - optind == 1)
152 		secs = atoi(argv[optind]);
153 	if (freq == 0 || secs == 0) {
154 		usage();
155 		return 1;
156 	}
157 
158 	/* initialize kernel symbol translation */
159 	if (load_kallsyms()) {
160 		fprintf(stderr, "ERROR: loading /proc/kallsyms\n");
161 		return 2;
162 	}
163 
164 	/* create perf FDs for each CPU */
165 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
166 	pmu_fd = malloc(nr_cpus * sizeof(int));
167 	if (pmu_fd == NULL) {
168 		fprintf(stderr, "ERROR: malloc of pmu_fd\n");
169 		return 1;
170 	}
171 
172 	/* load BPF program */
173 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
174 	if (load_bpf_file(filename)) {
175 		fprintf(stderr, "ERROR: loading BPF program (errno %d):\n",
176 			errno);
177 		if (strcmp(bpf_log_buf, "") == 0)
178 			fprintf(stderr, "Try: ulimit -l unlimited\n");
179 		else
180 			fprintf(stderr, "%s", bpf_log_buf);
181 		return 1;
182 	}
183 	signal(SIGINT, int_exit);
184 	signal(SIGTERM, int_exit);
185 
186 	/* do sampling */
187 	printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
188 	       freq, secs);
189 	if (sampling_start(pmu_fd, freq) != 0)
190 		return 1;
191 	sleep(secs);
192 	sampling_end(pmu_fd);
193 	free(pmu_fd);
194 
195 	/* output sample counts */
196 	print_ip_map(map_fd[0]);
197 
198 	return 0;
199 }
200