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) 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 <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) struct pair {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	long long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	__u64 ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static __u64 time_get_ns(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct timespec ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	clock_gettime(CLOCK_MONOTONIC, &ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	return ts.tv_sec * 1000000000ull + ts.tv_nsec;
^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) static void print_old_objects(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	long long val = time_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	__u64 key, next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct pair v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	key = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		bpf_map_lookup_elem(fd, &next_key, &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		key = next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		if (val - v.val < 1000000000ll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			/* object was allocated more then 1 sec ago */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		       next_key, (val - v.val) / 1000000000ll, v.ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) int main(int ac, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct bpf_link *links[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int map_fd, i, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (map_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	bpf_object__for_each_program(prog, obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		links[j] = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		if (libbpf_get_error(links[j])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			fprintf(stderr, "ERROR: bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			links[j] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	for (i = 0; ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		print_old_objects(map_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		sleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	for (j--; j >= 0; j--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		bpf_link__destroy(links[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }