• 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 
7 struct {
8 	__uint(type, BPF_MAP_TYPE_HASH);
9 	__uint(max_entries, 10240);
10 	__type(key, u32);
11 	__type(value, u64);
12 	__uint(map_flags, BPF_F_NO_PREALLOC);
13 } start SEC(".maps");
14 
15 __u64 latency = 0;
16 __u64 num = 0;
17 
18 SEC("fentry/migrate_misplaced_page")
BPF_PROG(migrate_misplaced_page)19 int BPF_PROG(migrate_misplaced_page)
20 {
21 	u32 pid = bpf_get_current_pid_tgid();
22 	u64 ts = bpf_ktime_get_ns();
23 
24 	bpf_map_update_elem(&start, &pid, &ts, 0);
25 	return 0;
26 }
27 
28 SEC("fexit/migrate_misplaced_page")
BPF_PROG(migrate_misplaced_page_exit)29 int BPF_PROG(migrate_misplaced_page_exit)
30 {
31 	u32 pid = bpf_get_current_pid_tgid();
32 	u64 *tsp, ts = bpf_ktime_get_ns();
33 	s64 delta;
34 
35 	tsp = bpf_map_lookup_elem(&start, &pid);
36 	if (!tsp)
37 		return 0;
38 	delta = (s64)(ts - *tsp);
39 	if (delta < 0)
40 		goto cleanup;
41 	__sync_fetch_and_add(&latency, delta / 1000000U);
42 	__sync_fetch_and_add(&num, 1);
43 
44 cleanup:
45 	bpf_map_delete_elem(&start, &pid);
46 	return 0;
47 }
48 
49 char LICENSE[] SEC("license") = "GPL";
50