1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Google LLC. */
3 #include "vmlinux.h"
4 #include <bpf/bpf_core_read.h>
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include "funclatency.h"
8 #include "bits.bpf.h"
9
10 const volatile pid_t targ_tgid = 0;
11 const volatile int units = 0;
12
13 /* key: pid. value: start time */
14 struct {
15 __uint(type, BPF_MAP_TYPE_HASH);
16 __uint(max_entries, MAX_PIDS);
17 __type(key, u32);
18 __type(value, u64);
19 } starts SEC(".maps");
20
21 __u32 hist[MAX_SLOTS] = {};
22
23 SEC("kprobe/dummy_kprobe")
BPF_KPROBE(dummy_kprobe)24 int BPF_KPROBE(dummy_kprobe)
25 {
26 u64 id = bpf_get_current_pid_tgid();
27 u32 tgid = id >> 32;
28 u32 pid = id;
29 u64 nsec;
30
31 if (targ_tgid && targ_tgid != tgid)
32 return 0;
33 nsec = bpf_ktime_get_ns();
34 bpf_map_update_elem(&starts, &pid, &nsec, BPF_ANY);
35
36 return 0;
37 }
38
39 SEC("kretprobe/dummy_kretprobe")
BPF_KRETPROBE(dummy_kretprobe)40 int BPF_KRETPROBE(dummy_kretprobe)
41 {
42 u64 *start;
43 u64 nsec = bpf_ktime_get_ns();
44 u64 id = bpf_get_current_pid_tgid();
45 u32 pid = id;
46 u64 slot, delta;
47
48 start = bpf_map_lookup_elem(&starts, &pid);
49 if (!start)
50 return 0;
51
52 delta = nsec - *start;
53
54 switch (units) {
55 case USEC:
56 delta /= 1000;
57 break;
58 case MSEC:
59 delta /= 1000000;
60 break;
61 }
62
63 slot = log2l(delta);
64 if (slot >= MAX_SLOTS)
65 slot = MAX_SLOTS - 1;
66 __sync_fetch_and_add(&hist[slot], 1);
67
68 return 0;
69 }
70
71 char LICENSE[] SEC("license") = "GPL";
72