From c0c80863046a938044f9bc87931840f6b8385799 Mon Sep 17 00:00:00 2001 From: chendexi Date: Sat, 1 Jul 2023 13:58:23 +0800 Subject: [PATCH] Add BPF programs to track cache references and cache miss events --- src/llcstat/llcstat.bpf.c | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/llcstat/llcstat.bpf.c diff --git a/src/llcstat/llcstat.bpf.c b/src/llcstat/llcstat.bpf.c new file mode 100644 index 00000000..f0be7999 --- /dev/null +++ b/src/llcstat/llcstat.bpf.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include +#include +#include +#include "maps.bpf.h" +#include "llcstat.h" + +#define MAX_ENTRIES 10240 + +const volatile bool target_per_thread = false; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, MAX_ENTRIES); + __type(key, struct key_info); + __type(value, struct value_info); +} infos SEC(".maps"); + +static __always_inline +int trace_event(__u64 sample_period, bool miss) +{ + struct key_info key = {}; + struct value_info *infop, zero = {}; + + u64 pid_tgid = bpf_get_current_pid_tgid(); + key.cpu = bpf_get_smp_processor_id(); + key.pid = pid_tgid >> 32; + if (target_per_thread) + key.tid = (u32)pid_tgid; + else + key.tid = key.pid; + + infop = bpf_map_lookup_or_try_init(&infos, &key, &zero); + if (!infop) + return 0; + if (miss) + infop->miss += sample_period; + else + infop->ref += sample_period; + bpf_get_current_comm(infop->comm, sizeof(infop->comm)); + + return 0; +} + +SEC("perf_event") +int on_cache_miss(struct bpf_perf_event_data *ctx) +{ + return trace_event(ctx->sample_period, true); +} + +SEC("perf_event") +int on_cache_ref(struct bpf_perf_event_data *ctx) +{ + return trace_event(ctx->sample_period, false); +} + +char LICENSE[] SEC("license") = "GPL"; -- Gitee