• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "llcstat.h"
7 
8 #define MAX_ENTRIES	10240
9 
10 struct {
11 	__uint(type, BPF_MAP_TYPE_HASH);
12 	__uint(max_entries, MAX_ENTRIES);
13 	__type(key, u64);
14 	__type(value, struct info);
15 } infos SEC(".maps");
16 
17 static __always_inline
trace_event(__u64 sample_period,bool miss)18 int trace_event(__u64 sample_period, bool miss)
19 {
20 	u64 pid = bpf_get_current_pid_tgid();
21 	u32 cpu = bpf_get_smp_processor_id();
22 	struct info *infop, info = {};
23 	u64 key = pid << 32 | cpu;
24 
25 	infop = bpf_map_lookup_elem(&infos, &key);
26 	if (!infop) {
27 		bpf_get_current_comm(info.comm, sizeof(info.comm));
28 		infop = &info;
29 	}
30 	if (miss)
31 		infop->miss += sample_period;
32 	else
33 		infop->ref += sample_period;
34 	if (infop == &info)
35 		bpf_map_update_elem(&infos, &key, infop, 0);
36 	return 0;
37 }
38 
39 SEC("perf_event")
on_cache_miss(struct bpf_perf_event_data * ctx)40 int on_cache_miss(struct bpf_perf_event_data *ctx)
41 {
42 	return trace_event(ctx->sample_period, true);
43 }
44 
45 SEC("perf_event")
on_cache_ref(struct bpf_perf_event_data * ctx)46 int on_cache_ref(struct bpf_perf_event_data *ctx)
47 {
48 	return trace_event(ctx->sample_period, false);
49 }
50 
51 char LICENSE[] SEC("license") = "GPL";
52