1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Wenbo Zhang
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 #include "maps.bpf.h"
7 #include "llcstat.h"
8
9 #define MAX_ENTRIES 10240
10
11 const volatile bool targ_per_thread = false;
12
13 struct {
14 __uint(type, BPF_MAP_TYPE_HASH);
15 __uint(max_entries, MAX_ENTRIES);
16 __type(key, struct key_info);
17 __type(value, struct value_info);
18 } infos SEC(".maps");
19
20 static __always_inline
trace_event(__u64 sample_period,bool miss)21 int trace_event(__u64 sample_period, bool miss)
22 {
23 struct key_info key = {};
24 struct value_info *infop, zero = {};
25
26 u64 pid_tgid = bpf_get_current_pid_tgid();
27 key.cpu = bpf_get_smp_processor_id();
28 key.pid = pid_tgid >> 32;
29 if (targ_per_thread)
30 key.tid = (u32)pid_tgid;
31 else
32 key.tid = key.pid;
33
34 infop = bpf_map_lookup_or_try_init(&infos, &key, &zero);
35 if (!infop)
36 return 0;
37 if (miss)
38 infop->miss += sample_period;
39 else
40 infop->ref += sample_period;
41 bpf_get_current_comm(infop->comm, sizeof(infop->comm));
42
43 return 0;
44 }
45
46 SEC("perf_event")
on_cache_miss(struct bpf_perf_event_data * ctx)47 int on_cache_miss(struct bpf_perf_event_data *ctx)
48 {
49 return trace_event(ctx->sample_period, true);
50 }
51
52 SEC("perf_event")
on_cache_ref(struct bpf_perf_event_data * ctx)53 int on_cache_ref(struct bpf_perf_event_data *ctx)
54 {
55 return trace_event(ctx->sample_period, false);
56 }
57
58 char LICENSE[] SEC("license") = "GPL";
59