^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) * Copyright (c) 2015 BMW Car IT GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define MAX_ENTRIES 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define MAX_CPU 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define MAX_STARS 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct cpu_hist {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) long data[MAX_ENTRIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) long max;
^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 struct cpu_hist cpu_hist[MAX_CPU];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static void stars(char *str, long val, long max, int width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) str[i] = '*';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (val > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) str[i - 1] = '+';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) str[i] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static void print_hist(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) char starstr[MAX_STARS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct cpu_hist *hist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* clear screen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) printf("\033[2J");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) for (j = 0; j < MAX_CPU; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) hist = &cpu_hist[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* ignore CPUs without data (maybe offline?) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (hist->max == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) printf("CPU %d\n", j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) printf(" latency : count distribution\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for (i = 1; i <= MAX_ENTRIES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) stars(starstr, hist->data[i - 1], hist->max, MAX_STARS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) (1l << i) >> 1, (1l << i) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) hist->data[i - 1], MAX_STARS, starstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^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) static void get_data(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) long key, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int c, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) for (i = 0; i < MAX_CPU; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) cpu_hist[i].max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) for (c = 0; c < MAX_CPU; c++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) for (i = 0; i < MAX_ENTRIES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) key = c * MAX_ENTRIES + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) bpf_map_lookup_elem(fd, &key, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) cpu_hist[c].data[i] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (value > cpu_hist[c].max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) cpu_hist[c].max = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct bpf_link *links[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int map_fd, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) map_fd = bpf_object__find_map_fd_by_name(obj, "my_lat");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (map_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) fprintf(stderr, "ERROR: finding a map in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) goto cleanup;
^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) bpf_object__for_each_program(prog, obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) links[i] = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (libbpf_get_error(links[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) fprintf(stderr, "ERROR: bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) links[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) get_data(map_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) print_hist();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) sleep(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) for (i--; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) bpf_link__destroy(links[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }