xref: /aosp_15_r20/external/bcc/libbpf-tools/cpufreq.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2020 Wenbo Zhang
3 //
4 // Based on cpufreq(8) from BPF-Perf-Tools-Book by Brendan Gregg.
5 // 10-OCT-2020   Wenbo Zhang   Created this.
6 #include <argp.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <linux/perf_event.h>
13 #include <asm/unistd.h>
14 #include <fcntl.h>
15 #include <bpf/libbpf.h>
16 #include <bpf/bpf.h>
17 #include "cpufreq.h"
18 #include "cpufreq.skel.h"
19 #include "trace_helpers.h"
20 
21 static struct env {
22 	int duration;
23 	int freq;
24 	bool verbose;
25 	char *cgroupspath;
26 	bool cg;
27 } env = {
28 	.duration = -1,
29 	.freq = 99,
30 };
31 
32 const char *argp_program_version = "cpufreq 0.1";
33 const char *argp_program_bug_address =
34 	"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
35 const char argp_program_doc[] =
36 "Sampling CPU freq system-wide & by process. Ctrl-C to end.\n"
37 "\n"
38 "USAGE: cpufreq [--help] [-d DURATION] [-f FREQUENCY] [-c CG]\n"
39 "\n"
40 "EXAMPLES:\n"
41 "    cpufreq         # sample CPU freq at 99HZ (default)\n"
42 "    cpufreq -d 5    # sample for 5 seconds only\n"
43 "    cpufreq -c CG   # Trace process under cgroupsPath CG\n"
44 "    cpufreq -f 199  # sample CPU freq at 199HZ\n";
45 
46 static const struct argp_option opts[] = {
47 	{ "duration", 'd', "DURATION", 0, "Duration to sample in seconds" },
48 	{ "frequency", 'f', "FREQUENCY", 0, "Sample with a certain frequency" },
49 	{ "cgroup", 'c', "/sys/fs/cgroup/unified", 0, "Trace process in cgroup path" },
50 	{ "verbose", 'v', NULL, 0, "Verbose debug output" },
51 	{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
52 	{},
53 };
54 
parse_arg(int key,char * arg,struct argp_state * state)55 static error_t parse_arg(int key, char *arg, struct argp_state *state)
56 {
57 	switch (key) {
58 	case 'h':
59 		argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
60 		break;
61 	case 'v':
62 		env.verbose = true;
63 		break;
64 	case 'd':
65 		errno = 0;
66 		env.duration = strtol(arg, NULL, 10);
67 		if (errno || env.duration <= 0) {
68 			fprintf(stderr, "Invalid duration: %s\n", arg);
69 			argp_usage(state);
70 		}
71 		break;
72 	case 'c':
73 		env.cgroupspath = arg;
74 		env.cg = true;
75 		break;
76 	case 'f':
77 		errno = 0;
78 		env.freq = strtol(arg, NULL, 10);
79 		if (errno || env.freq <= 0) {
80 			fprintf(stderr, "Invalid freq (in HZ): %s\n", arg);
81 			argp_usage(state);
82 		}
83 		break;
84 	default:
85 		return ARGP_ERR_UNKNOWN;
86 	}
87 	return 0;
88 }
89 
90 static int nr_cpus;
91 
open_and_attach_perf_event(int freq,struct bpf_program * prog,struct bpf_link * links[])92 static int open_and_attach_perf_event(int freq, struct bpf_program *prog,
93 				struct bpf_link *links[])
94 {
95 	struct perf_event_attr attr = {
96 		.type = PERF_TYPE_SOFTWARE,
97 		.freq = 1,
98 		.sample_period = freq,
99 		.config = PERF_COUNT_SW_CPU_CLOCK,
100 	};
101 	int i, fd;
102 
103 	for (i = 0; i < nr_cpus; i++) {
104 		fd = syscall(__NR_perf_event_open, &attr, -1, i, -1, 0);
105 		if (fd < 0) {
106 			/* Ignore CPU that is offline */
107 			if (errno == ENODEV)
108 				continue;
109 			fprintf(stderr, "failed to init perf sampling: %s\n",
110 				strerror(errno));
111 			return -1;
112 		}
113 		links[i] = bpf_program__attach_perf_event(prog, fd);
114 		if (!links[i]) {
115 			fprintf(stderr, "failed to attach perf event on cpu: %d\n", i);
116 			close(fd);
117 			return -1;
118 		}
119 	}
120 
121 	return 0;
122 }
123 
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)124 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
125 {
126 	if (level == LIBBPF_DEBUG && !env.verbose)
127 		return 0;
128 	return vfprintf(stderr, format, args);
129 }
130 
sig_handler(int sig)131 static void sig_handler(int sig)
132 {
133 }
134 
init_freqs_mhz(__u32 * freqs_mhz,struct bpf_link * links[])135 static int init_freqs_mhz(__u32 *freqs_mhz, struct bpf_link *links[])
136 {
137 	char path[64];
138 	FILE *f;
139 	int i;
140 
141 	for (i = 0; i < nr_cpus; i++) {
142 		if (!links[i]) {
143 			continue;
144 		}
145 		snprintf(path, sizeof(path),
146 			"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq",
147 			i);
148 
149 		f = fopen(path, "r");
150 		if (!f) {
151 			fprintf(stderr, "failed to open '%s': %s\n", path,
152 				strerror(errno));
153 			return -1;
154 		}
155 		if (fscanf(f, "%u\n", &freqs_mhz[i]) != 1) {
156 			fprintf(stderr, "failed to parse '%s': %s\n", path,
157 				strerror(errno));
158 			fclose(f);
159 			return -1;
160 		}
161 		/*
162 		 * scaling_cur_freq is in kHz. To be handled with
163 		 * a small data size, it's converted in mHz.
164 		 */
165 		freqs_mhz[i] /= 1000;
166 		fclose(f);
167 	}
168 
169 	return 0;
170 }
171 
print_linear_hists(struct bpf_map * hists,struct cpufreq_bpf__bss * bss)172 static void print_linear_hists(struct bpf_map *hists,
173 			struct cpufreq_bpf__bss *bss)
174 {
175 	struct hkey lookup_key = {}, next_key;
176 	int err, fd = bpf_map__fd(hists);
177 	struct hist hist;
178 
179 	while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
180 		err = bpf_map_lookup_elem(fd, &next_key, &hist);
181 		if (err < 0) {
182 			fprintf(stderr, "failed to lookup hist: %d\n", err);
183 			return;
184 		}
185 		print_linear_hist(hist.slots, MAX_SLOTS, 0, HIST_STEP_SIZE,
186 				next_key.comm);
187 		printf("\n");
188 		lookup_key = next_key;
189 	}
190 
191 	printf("\n");
192 	print_linear_hist(bss->syswide.slots, MAX_SLOTS, 0, HIST_STEP_SIZE,
193 			  "syswide");
194 }
195 
main(int argc,char ** argv)196 int main(int argc, char **argv)
197 {
198 	static const struct argp argp = {
199 		.options = opts,
200 		.parser = parse_arg,
201 		.doc = argp_program_doc,
202 	};
203 	struct bpf_link *links[MAX_CPU_NR] = {};
204 	struct cpufreq_bpf *obj;
205 	int err, i;
206 	int idx, cg_map_fd;
207 	int cgfd = -1;
208 
209 	err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
210 	if (err)
211 		return err;
212 
213 	libbpf_set_print(libbpf_print_fn);
214 
215 	nr_cpus = libbpf_num_possible_cpus();
216 	if (nr_cpus < 0) {
217 		fprintf(stderr, "failed to get # of possible cpus: '%s'!\n",
218 			strerror(-nr_cpus));
219 		return 1;
220 	}
221 	if (nr_cpus > MAX_CPU_NR) {
222 		fprintf(stderr, "the number of cpu cores is too big, please "
223 			"increase MAX_CPU_NR's value and recompile");
224 		return 1;
225 	}
226 
227 	obj = cpufreq_bpf__open_and_load();
228 	if (!obj) {
229 		fprintf(stderr, "failed to open and/or load BPF object\n");
230 		return 1;
231 	}
232 
233 	if (!obj->bss) {
234 		fprintf(stderr, "Memory-mapping BPF maps is supported starting from Linux 5.7, please upgrade.\n");
235 		goto cleanup;
236 	}
237 
238 	obj->bss->filter_cg = env.cg;
239 
240 	/* update cgroup path fd to map */
241 	if (env.cg) {
242 		idx = 0;
243 		cg_map_fd = bpf_map__fd(obj->maps.cgroup_map);
244 		cgfd = open(env.cgroupspath, O_RDONLY);
245 		if (cgfd < 0) {
246 			fprintf(stderr, "Failed opening Cgroup path: %s", env.cgroupspath);
247 			goto cleanup;
248 		}
249 		if (bpf_map_update_elem(cg_map_fd, &idx, &cgfd, BPF_ANY)) {
250 			fprintf(stderr, "Failed adding target cgroup to map");
251 			goto cleanup;
252 		}
253 	}
254 
255 	err = open_and_attach_perf_event(env.freq, obj->progs.do_sample, links);
256 	if (err)
257 		goto cleanup;
258 	err = init_freqs_mhz(obj->bss->freqs_mhz, links);
259 	if (err) {
260 		fprintf(stderr, "failed to init freqs\n");
261 		goto cleanup;
262 	}
263 
264 	err = cpufreq_bpf__attach(obj);
265 	if (err) {
266 		fprintf(stderr, "failed to attach BPF programs\n");
267 		goto cleanup;
268 	}
269 
270 	printf("Sampling CPU freq system-wide & by process. Ctrl-C to end.\n");
271 
272 	signal(SIGINT, sig_handler);
273 
274 	/*
275 	 * We'll get sleep interrupted when someone presses Ctrl-C (which will
276 	 * be "handled" with noop by sig_handler).
277 	 */
278 	sleep(env.duration);
279 	printf("\n");
280 
281 	print_linear_hists(obj->maps.hists, obj->bss);
282 
283 cleanup:
284 	for (i = 0; i < nr_cpus; i++)
285 		bpf_link__destroy(links[i]);
286 	cpufreq_bpf__destroy(obj);
287 	if (cgfd > 0)
288 		close(cgfd);
289 
290 	return err != 0;
291 }
292