From 8e753b471e0f4725fe0bcbac0ac56128bfafe5c9 Mon Sep 17 00:00:00 2001 From: wubijie Date: Mon, 26 Jun 2023 13:49:11 +0800 Subject: [PATCH] Print the BPF map and an array of partition structs --- observation/src/biopattern/biopattern.c | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/observation/src/biopattern/biopattern.c b/observation/src/biopattern/biopattern.c index ebbe64aa..a677f8da 100644 --- a/observation/src/biopattern/biopattern.c +++ b/observation/src/biopattern/biopattern.c @@ -97,4 +97,49 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format, static void sig_handler(int sig) { exiting = 1; +} + +static int print_map(struct bpf_map *counters, struct partitions *partitions) +{ + __u32 total, lookup_key = -1, next_key; + int err, fd = bpf_map__fd(counters); + const struct partition *partition; + struct counter counter; + + while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) { + err = bpf_map_lookup_elem(fd, &next_key, &counter); + if (err < 0) { + warning("Failed to lookup counters: %d\n", err); + return -1; + } + + lookup_key = next_key; + total = counter.sequential + counter.random; + if (!total) + continue; + if (env.timestamp) { + char ts[32]; + + strftime_now(ts, sizeof(ts), "%H:%M:%S"); + printf("%-9s ", ts); + } + partition = partitions__get_by_dev(partitions, next_key); + printf("%-7s %5ld %5ld %8d %10lld\n", + partition ? partition->name : "Unknown", + counter.random * 100L / total, + counter.sequential * 100L / total, total, + counter.bytes / 1024); + } + + lookup_key = -1; + while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) { + err = bpf_map_delete_elem(fd, &next_key); + if (err < 0) { + warning("Failed to cleanup counters: %d\n", err); + return -1; + } + lookup_key = next_key; + } + + return 0; } \ No newline at end of file -- Gitee