Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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