Linux Audio

Check our new training course

In-person Linux kernel drivers training

Jun 16-20, 2025
Register
Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  3 */
  4#include <stdio.h>
  5#include <stdlib.h>
  6#include <signal.h>
  7#include <unistd.h>
  8#include <stdbool.h>
  9#include <string.h>
 
 10#include <sys/resource.h>
 11
 12#include <bpf/bpf.h>
 13#include <bpf/libbpf.h>
 14#include "bpf_util.h"
 15
 16#define SLOTS 100
 17
 18static void clear_stats(int fd)
 19{
 20	unsigned int nr_cpus = bpf_num_possible_cpus();
 21	__u64 values[nr_cpus];
 22	__u32 key;
 23
 24	memset(values, 0, sizeof(values));
 25	for (key = 0; key < SLOTS; key++)
 26		bpf_map_update_elem(fd, &key, values, BPF_ANY);
 27}
 28
 29const char *color[] = {
 30	"\033[48;5;255m",
 31	"\033[48;5;252m",
 32	"\033[48;5;250m",
 33	"\033[48;5;248m",
 34	"\033[48;5;246m",
 35	"\033[48;5;244m",
 36	"\033[48;5;242m",
 37	"\033[48;5;240m",
 38	"\033[48;5;238m",
 39	"\033[48;5;236m",
 40	"\033[48;5;234m",
 41	"\033[48;5;232m",
 42};
 43const int num_colors = ARRAY_SIZE(color);
 44
 45const char nocolor[] = "\033[00m";
 46
 47const char *sym[] = {
 48	" ",
 49	" ",
 50	".",
 51	".",
 52	"*",
 53	"*",
 54	"o",
 55	"o",
 56	"O",
 57	"O",
 58	"#",
 59	"#",
 60};
 61
 62bool full_range = false;
 63bool text_only = false;
 64
 65static void print_banner(void)
 66{
 67	if (full_range)
 68		printf("|1ns     |10ns     |100ns    |1us      |10us     |100us"
 69		       "    |1ms      |10ms     |100ms    |1s       |10s\n");
 70	else
 71		printf("|1us      |10us     |100us    |1ms      |10ms     "
 72		       "|100ms    |1s       |10s\n");
 73}
 74
 75static void print_hist(int fd)
 76{
 77	unsigned int nr_cpus = bpf_num_possible_cpus();
 78	__u64 total_events = 0;
 79	long values[nr_cpus];
 80	__u64 max_cnt = 0;
 81	__u64 cnt[SLOTS];
 82	__u64 value;
 83	__u32 key;
 84	int i;
 85
 86	for (key = 0; key < SLOTS; key++) {
 87		bpf_map_lookup_elem(fd, &key, values);
 88		value = 0;
 89		for (i = 0; i < nr_cpus; i++)
 90			value += values[i];
 91		cnt[key] = value;
 92		total_events += value;
 93		if (value > max_cnt)
 94			max_cnt = value;
 95	}
 96	clear_stats(fd);
 97	for (key = full_range ? 0 : 29; key < SLOTS; key++) {
 98		int c = num_colors * cnt[key] / (max_cnt + 1);
 99
100		if (text_only)
101			printf("%s", sym[c]);
102		else
103			printf("%s %s", color[c], nocolor);
104	}
105	printf(" # %lld\n", total_events);
106}
107
108int main(int ac, char **argv)
109{
110	struct rlimit r = {1024*1024, RLIM_INFINITY};
111	struct bpf_link *links[2];
112	struct bpf_program *prog;
113	struct bpf_object *obj;
114	char filename[256];
115	int map_fd, i, j = 0;
 
 
 
 
 
 
 
 
 
 
 
 
116
117	for (i = 1; i < ac; i++) {
118		if (strcmp(argv[i], "-a") == 0) {
119			full_range = true;
120		} else if (strcmp(argv[i], "-t") == 0) {
121			text_only = true;
122		} else if (strcmp(argv[i], "-h") == 0) {
123			printf("Usage:\n"
124			       "  -a display wider latency range\n"
125			       "  -t text only\n");
126			return 1;
127		}
128	}
129
130	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
131		perror("setrlimit(RLIMIT_MEMLOCK)");
132		return 1;
133	}
134
135	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
136	obj = bpf_object__open_file(filename, NULL);
137	if (libbpf_get_error(obj)) {
138		fprintf(stderr, "ERROR: opening BPF object file failed\n");
139		return 0;
140	}
141
142	/* load BPF program */
143	if (bpf_object__load(obj)) {
144		fprintf(stderr, "ERROR: loading BPF object file failed\n");
145		goto cleanup;
146	}
147
148	map_fd = bpf_object__find_map_fd_by_name(obj, "lat_map");
149	if (map_fd < 0) {
150		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
151		goto cleanup;
152	}
153
154	bpf_object__for_each_program(prog, obj) {
155		links[j] = bpf_program__attach(prog);
156		if (libbpf_get_error(links[j])) {
157			fprintf(stderr, "ERROR: bpf_program__attach failed\n");
158			links[j] = NULL;
159			goto cleanup;
160		}
161		j++;
162	}
163
164	printf("  heatmap of IO latency\n");
165	if (text_only)
166		printf("  %s", sym[num_colors - 1]);
167	else
168		printf("  %s %s", color[num_colors - 1], nocolor);
169	printf(" - many events with this latency\n");
170
171	if (text_only)
172		printf("  %s", sym[0]);
173	else
174		printf("  %s %s", color[0], nocolor);
175	printf(" - few events\n");
176
177	for (i = 0; ; i++) {
178		if (i % 20 == 0)
179			print_banner();
180		print_hist(map_fd);
181		sleep(2);
182	}
183
184cleanup:
185	for (j--; j >= 0; j--)
186		bpf_link__destroy(links[j]);
187
188	bpf_object__close(obj);
189	return 0;
190}
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  3 */
  4#include <stdio.h>
  5#include <stdlib.h>
  6#include <signal.h>
  7#include <unistd.h>
  8#include <stdbool.h>
  9#include <string.h>
 10#include <linux/bpf.h>
 11#include <sys/resource.h>
 12
 13#include <bpf/bpf.h>
 14#include "bpf_load.h"
 15#include "bpf_util.h"
 16
 17#define SLOTS 100
 18
 19static void clear_stats(int fd)
 20{
 21	unsigned int nr_cpus = bpf_num_possible_cpus();
 22	__u64 values[nr_cpus];
 23	__u32 key;
 24
 25	memset(values, 0, sizeof(values));
 26	for (key = 0; key < SLOTS; key++)
 27		bpf_map_update_elem(fd, &key, values, BPF_ANY);
 28}
 29
 30const char *color[] = {
 31	"\033[48;5;255m",
 32	"\033[48;5;252m",
 33	"\033[48;5;250m",
 34	"\033[48;5;248m",
 35	"\033[48;5;246m",
 36	"\033[48;5;244m",
 37	"\033[48;5;242m",
 38	"\033[48;5;240m",
 39	"\033[48;5;238m",
 40	"\033[48;5;236m",
 41	"\033[48;5;234m",
 42	"\033[48;5;232m",
 43};
 44const int num_colors = ARRAY_SIZE(color);
 45
 46const char nocolor[] = "\033[00m";
 47
 48const char *sym[] = {
 49	" ",
 50	" ",
 51	".",
 52	".",
 53	"*",
 54	"*",
 55	"o",
 56	"o",
 57	"O",
 58	"O",
 59	"#",
 60	"#",
 61};
 62
 63bool full_range = false;
 64bool text_only = false;
 65
 66static void print_banner(void)
 67{
 68	if (full_range)
 69		printf("|1ns     |10ns     |100ns    |1us      |10us     |100us"
 70		       "    |1ms      |10ms     |100ms    |1s       |10s\n");
 71	else
 72		printf("|1us      |10us     |100us    |1ms      |10ms     "
 73		       "|100ms    |1s       |10s\n");
 74}
 75
 76static void print_hist(int fd)
 77{
 78	unsigned int nr_cpus = bpf_num_possible_cpus();
 79	__u64 total_events = 0;
 80	long values[nr_cpus];
 81	__u64 max_cnt = 0;
 82	__u64 cnt[SLOTS];
 83	__u64 value;
 84	__u32 key;
 85	int i;
 86
 87	for (key = 0; key < SLOTS; key++) {
 88		bpf_map_lookup_elem(fd, &key, values);
 89		value = 0;
 90		for (i = 0; i < nr_cpus; i++)
 91			value += values[i];
 92		cnt[key] = value;
 93		total_events += value;
 94		if (value > max_cnt)
 95			max_cnt = value;
 96	}
 97	clear_stats(fd);
 98	for (key = full_range ? 0 : 29; key < SLOTS; key++) {
 99		int c = num_colors * cnt[key] / (max_cnt + 1);
100
101		if (text_only)
102			printf("%s", sym[c]);
103		else
104			printf("%s %s", color[c], nocolor);
105	}
106	printf(" # %lld\n", total_events);
107}
108
109int main(int ac, char **argv)
110{
111	struct rlimit r = {1024*1024, RLIM_INFINITY};
 
 
 
112	char filename[256];
113	int i;
114
115	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
116
117	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
118		perror("setrlimit(RLIMIT_MEMLOCK)");
119		return 1;
120	}
121
122	if (load_bpf_file(filename)) {
123		printf("%s", bpf_log_buf);
124		return 1;
125	}
126
127	for (i = 1; i < ac; i++) {
128		if (strcmp(argv[i], "-a") == 0) {
129			full_range = true;
130		} else if (strcmp(argv[i], "-t") == 0) {
131			text_only = true;
132		} else if (strcmp(argv[i], "-h") == 0) {
133			printf("Usage:\n"
134			       "  -a display wider latency range\n"
135			       "  -t text only\n");
136			return 1;
137		}
138	}
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140	printf("  heatmap of IO latency\n");
141	if (text_only)
142		printf("  %s", sym[num_colors - 1]);
143	else
144		printf("  %s %s", color[num_colors - 1], nocolor);
145	printf(" - many events with this latency\n");
146
147	if (text_only)
148		printf("  %s", sym[0]);
149	else
150		printf("  %s %s", color[0], nocolor);
151	printf(" - few events\n");
152
153	for (i = 0; ; i++) {
154		if (i % 20 == 0)
155			print_banner();
156		print_hist(map_fd[1]);
157		sleep(2);
158	}
159
 
 
 
 
 
160	return 0;
161}