1*387f9dfdSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0
2*387f9dfdSAndroid Build Coastguard Worker // Copyright (c) 2020 Wenbo Zhang
3*387f9dfdSAndroid Build Coastguard Worker #include <vmlinux.h>
4*387f9dfdSAndroid Build Coastguard Worker #include <bpf/bpf_helpers.h>
5*387f9dfdSAndroid Build Coastguard Worker #include <bpf/bpf_core_read.h>
6*387f9dfdSAndroid Build Coastguard Worker #include <bpf/bpf_tracing.h>
7*387f9dfdSAndroid Build Coastguard Worker #include "runqlat.h"
8*387f9dfdSAndroid Build Coastguard Worker #include "bits.bpf.h"
9*387f9dfdSAndroid Build Coastguard Worker #include "maps.bpf.h"
10*387f9dfdSAndroid Build Coastguard Worker #include "core_fixes.bpf.h"
11*387f9dfdSAndroid Build Coastguard Worker
12*387f9dfdSAndroid Build Coastguard Worker #define MAX_ENTRIES 10240
13*387f9dfdSAndroid Build Coastguard Worker #define TASK_RUNNING 0
14*387f9dfdSAndroid Build Coastguard Worker
15*387f9dfdSAndroid Build Coastguard Worker const volatile bool filter_cg = false;
16*387f9dfdSAndroid Build Coastguard Worker const volatile bool targ_per_process = false;
17*387f9dfdSAndroid Build Coastguard Worker const volatile bool targ_per_thread = false;
18*387f9dfdSAndroid Build Coastguard Worker const volatile bool targ_per_pidns = false;
19*387f9dfdSAndroid Build Coastguard Worker const volatile bool targ_ms = false;
20*387f9dfdSAndroid Build Coastguard Worker const volatile pid_t targ_tgid = 0;
21*387f9dfdSAndroid Build Coastguard Worker
22*387f9dfdSAndroid Build Coastguard Worker struct {
23*387f9dfdSAndroid Build Coastguard Worker __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
24*387f9dfdSAndroid Build Coastguard Worker __type(key, u32);
25*387f9dfdSAndroid Build Coastguard Worker __type(value, u32);
26*387f9dfdSAndroid Build Coastguard Worker __uint(max_entries, 1);
27*387f9dfdSAndroid Build Coastguard Worker } cgroup_map SEC(".maps");
28*387f9dfdSAndroid Build Coastguard Worker
29*387f9dfdSAndroid Build Coastguard Worker struct {
30*387f9dfdSAndroid Build Coastguard Worker __uint(type, BPF_MAP_TYPE_HASH);
31*387f9dfdSAndroid Build Coastguard Worker __uint(max_entries, MAX_ENTRIES);
32*387f9dfdSAndroid Build Coastguard Worker __type(key, u32);
33*387f9dfdSAndroid Build Coastguard Worker __type(value, u64);
34*387f9dfdSAndroid Build Coastguard Worker } start SEC(".maps");
35*387f9dfdSAndroid Build Coastguard Worker
36*387f9dfdSAndroid Build Coastguard Worker static struct hist zero;
37*387f9dfdSAndroid Build Coastguard Worker
38*387f9dfdSAndroid Build Coastguard Worker struct {
39*387f9dfdSAndroid Build Coastguard Worker __uint(type, BPF_MAP_TYPE_HASH);
40*387f9dfdSAndroid Build Coastguard Worker __uint(max_entries, MAX_ENTRIES);
41*387f9dfdSAndroid Build Coastguard Worker __type(key, u32);
42*387f9dfdSAndroid Build Coastguard Worker __type(value, struct hist);
43*387f9dfdSAndroid Build Coastguard Worker } hists SEC(".maps");
44*387f9dfdSAndroid Build Coastguard Worker
trace_enqueue(u32 tgid,u32 pid)45*387f9dfdSAndroid Build Coastguard Worker static int trace_enqueue(u32 tgid, u32 pid)
46*387f9dfdSAndroid Build Coastguard Worker {
47*387f9dfdSAndroid Build Coastguard Worker u64 ts;
48*387f9dfdSAndroid Build Coastguard Worker
49*387f9dfdSAndroid Build Coastguard Worker if (!pid)
50*387f9dfdSAndroid Build Coastguard Worker return 0;
51*387f9dfdSAndroid Build Coastguard Worker if (targ_tgid && targ_tgid != tgid)
52*387f9dfdSAndroid Build Coastguard Worker return 0;
53*387f9dfdSAndroid Build Coastguard Worker
54*387f9dfdSAndroid Build Coastguard Worker ts = bpf_ktime_get_ns();
55*387f9dfdSAndroid Build Coastguard Worker bpf_map_update_elem(&start, &pid, &ts, BPF_ANY);
56*387f9dfdSAndroid Build Coastguard Worker return 0;
57*387f9dfdSAndroid Build Coastguard Worker }
58*387f9dfdSAndroid Build Coastguard Worker
pid_namespace(struct task_struct * task)59*387f9dfdSAndroid Build Coastguard Worker static unsigned int pid_namespace(struct task_struct *task)
60*387f9dfdSAndroid Build Coastguard Worker {
61*387f9dfdSAndroid Build Coastguard Worker struct pid *pid;
62*387f9dfdSAndroid Build Coastguard Worker unsigned int level;
63*387f9dfdSAndroid Build Coastguard Worker struct upid upid;
64*387f9dfdSAndroid Build Coastguard Worker unsigned int inum;
65*387f9dfdSAndroid Build Coastguard Worker
66*387f9dfdSAndroid Build Coastguard Worker /* get the pid namespace by following task_active_pid_ns(),
67*387f9dfdSAndroid Build Coastguard Worker * pid->numbers[pid->level].ns
68*387f9dfdSAndroid Build Coastguard Worker */
69*387f9dfdSAndroid Build Coastguard Worker pid = BPF_CORE_READ(task, thread_pid);
70*387f9dfdSAndroid Build Coastguard Worker level = BPF_CORE_READ(pid, level);
71*387f9dfdSAndroid Build Coastguard Worker bpf_core_read(&upid, sizeof(upid), &pid->numbers[level]);
72*387f9dfdSAndroid Build Coastguard Worker inum = BPF_CORE_READ(upid.ns, ns.inum);
73*387f9dfdSAndroid Build Coastguard Worker
74*387f9dfdSAndroid Build Coastguard Worker return inum;
75*387f9dfdSAndroid Build Coastguard Worker }
76*387f9dfdSAndroid Build Coastguard Worker
handle_switch(bool preempt,struct task_struct * prev,struct task_struct * next)77*387f9dfdSAndroid Build Coastguard Worker static int handle_switch(bool preempt, struct task_struct *prev, struct task_struct *next)
78*387f9dfdSAndroid Build Coastguard Worker {
79*387f9dfdSAndroid Build Coastguard Worker struct hist *histp;
80*387f9dfdSAndroid Build Coastguard Worker u64 *tsp, slot;
81*387f9dfdSAndroid Build Coastguard Worker u32 pid, hkey;
82*387f9dfdSAndroid Build Coastguard Worker s64 delta;
83*387f9dfdSAndroid Build Coastguard Worker u64 udelta;
84*387f9dfdSAndroid Build Coastguard Worker
85*387f9dfdSAndroid Build Coastguard Worker if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
86*387f9dfdSAndroid Build Coastguard Worker return 0;
87*387f9dfdSAndroid Build Coastguard Worker
88*387f9dfdSAndroid Build Coastguard Worker if (get_task_state(prev) == TASK_RUNNING)
89*387f9dfdSAndroid Build Coastguard Worker trace_enqueue(BPF_CORE_READ(prev, tgid), BPF_CORE_READ(prev, pid));
90*387f9dfdSAndroid Build Coastguard Worker
91*387f9dfdSAndroid Build Coastguard Worker pid = BPF_CORE_READ(next, pid);
92*387f9dfdSAndroid Build Coastguard Worker
93*387f9dfdSAndroid Build Coastguard Worker tsp = bpf_map_lookup_elem(&start, &pid);
94*387f9dfdSAndroid Build Coastguard Worker if (!tsp)
95*387f9dfdSAndroid Build Coastguard Worker return 0;
96*387f9dfdSAndroid Build Coastguard Worker delta = bpf_ktime_get_ns() - *tsp;
97*387f9dfdSAndroid Build Coastguard Worker if (delta < 0)
98*387f9dfdSAndroid Build Coastguard Worker goto cleanup;
99*387f9dfdSAndroid Build Coastguard Worker udelta = (u64)delta;
100*387f9dfdSAndroid Build Coastguard Worker
101*387f9dfdSAndroid Build Coastguard Worker if (targ_per_process)
102*387f9dfdSAndroid Build Coastguard Worker hkey = BPF_CORE_READ(next, tgid);
103*387f9dfdSAndroid Build Coastguard Worker else if (targ_per_thread)
104*387f9dfdSAndroid Build Coastguard Worker hkey = pid;
105*387f9dfdSAndroid Build Coastguard Worker else if (targ_per_pidns)
106*387f9dfdSAndroid Build Coastguard Worker hkey = pid_namespace(next);
107*387f9dfdSAndroid Build Coastguard Worker else
108*387f9dfdSAndroid Build Coastguard Worker hkey = -1;
109*387f9dfdSAndroid Build Coastguard Worker histp = bpf_map_lookup_or_try_init(&hists, &hkey, &zero);
110*387f9dfdSAndroid Build Coastguard Worker if (!histp)
111*387f9dfdSAndroid Build Coastguard Worker goto cleanup;
112*387f9dfdSAndroid Build Coastguard Worker if (!histp->comm[0])
113*387f9dfdSAndroid Build Coastguard Worker bpf_probe_read_kernel_str(&histp->comm, sizeof(histp->comm),
114*387f9dfdSAndroid Build Coastguard Worker next->comm);
115*387f9dfdSAndroid Build Coastguard Worker if (targ_ms)
116*387f9dfdSAndroid Build Coastguard Worker udelta /= 1000000U;
117*387f9dfdSAndroid Build Coastguard Worker else
118*387f9dfdSAndroid Build Coastguard Worker udelta /= 1000U;
119*387f9dfdSAndroid Build Coastguard Worker slot = log2l(udelta);
120*387f9dfdSAndroid Build Coastguard Worker if (slot >= MAX_SLOTS)
121*387f9dfdSAndroid Build Coastguard Worker slot = MAX_SLOTS - 1;
122*387f9dfdSAndroid Build Coastguard Worker __sync_fetch_and_add(&histp->slots[slot], 1);
123*387f9dfdSAndroid Build Coastguard Worker
124*387f9dfdSAndroid Build Coastguard Worker cleanup:
125*387f9dfdSAndroid Build Coastguard Worker bpf_map_delete_elem(&start, &pid);
126*387f9dfdSAndroid Build Coastguard Worker return 0;
127*387f9dfdSAndroid Build Coastguard Worker }
128*387f9dfdSAndroid Build Coastguard Worker
129*387f9dfdSAndroid Build Coastguard Worker SEC("tp_btf/sched_wakeup")
BPF_PROG(sched_wakeup,struct task_struct * p)130*387f9dfdSAndroid Build Coastguard Worker int BPF_PROG(sched_wakeup, struct task_struct *p)
131*387f9dfdSAndroid Build Coastguard Worker {
132*387f9dfdSAndroid Build Coastguard Worker if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
133*387f9dfdSAndroid Build Coastguard Worker return 0;
134*387f9dfdSAndroid Build Coastguard Worker
135*387f9dfdSAndroid Build Coastguard Worker return trace_enqueue(p->tgid, p->pid);
136*387f9dfdSAndroid Build Coastguard Worker }
137*387f9dfdSAndroid Build Coastguard Worker
138*387f9dfdSAndroid Build Coastguard Worker SEC("tp_btf/sched_wakeup_new")
BPF_PROG(sched_wakeup_new,struct task_struct * p)139*387f9dfdSAndroid Build Coastguard Worker int BPF_PROG(sched_wakeup_new, struct task_struct *p)
140*387f9dfdSAndroid Build Coastguard Worker {
141*387f9dfdSAndroid Build Coastguard Worker if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
142*387f9dfdSAndroid Build Coastguard Worker return 0;
143*387f9dfdSAndroid Build Coastguard Worker
144*387f9dfdSAndroid Build Coastguard Worker return trace_enqueue(p->tgid, p->pid);
145*387f9dfdSAndroid Build Coastguard Worker }
146*387f9dfdSAndroid Build Coastguard Worker
147*387f9dfdSAndroid Build Coastguard Worker SEC("tp_btf/sched_switch")
BPF_PROG(sched_switch,bool preempt,struct task_struct * prev,struct task_struct * next)148*387f9dfdSAndroid Build Coastguard Worker int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, struct task_struct *next)
149*387f9dfdSAndroid Build Coastguard Worker {
150*387f9dfdSAndroid Build Coastguard Worker return handle_switch(preempt, prev, next);
151*387f9dfdSAndroid Build Coastguard Worker }
152*387f9dfdSAndroid Build Coastguard Worker
153*387f9dfdSAndroid Build Coastguard Worker SEC("raw_tp/sched_wakeup")
BPF_PROG(handle_sched_wakeup,struct task_struct * p)154*387f9dfdSAndroid Build Coastguard Worker int BPF_PROG(handle_sched_wakeup, struct task_struct *p)
155*387f9dfdSAndroid Build Coastguard Worker {
156*387f9dfdSAndroid Build Coastguard Worker if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
157*387f9dfdSAndroid Build Coastguard Worker return 0;
158*387f9dfdSAndroid Build Coastguard Worker
159*387f9dfdSAndroid Build Coastguard Worker return trace_enqueue(BPF_CORE_READ(p, tgid), BPF_CORE_READ(p, pid));
160*387f9dfdSAndroid Build Coastguard Worker }
161*387f9dfdSAndroid Build Coastguard Worker
162*387f9dfdSAndroid Build Coastguard Worker SEC("raw_tp/sched_wakeup_new")
BPF_PROG(handle_sched_wakeup_new,struct task_struct * p)163*387f9dfdSAndroid Build Coastguard Worker int BPF_PROG(handle_sched_wakeup_new, struct task_struct *p)
164*387f9dfdSAndroid Build Coastguard Worker {
165*387f9dfdSAndroid Build Coastguard Worker if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
166*387f9dfdSAndroid Build Coastguard Worker return 0;
167*387f9dfdSAndroid Build Coastguard Worker
168*387f9dfdSAndroid Build Coastguard Worker return trace_enqueue(BPF_CORE_READ(p, tgid), BPF_CORE_READ(p, pid));
169*387f9dfdSAndroid Build Coastguard Worker }
170*387f9dfdSAndroid Build Coastguard Worker
171*387f9dfdSAndroid Build Coastguard Worker SEC("raw_tp/sched_switch")
BPF_PROG(handle_sched_switch,bool preempt,struct task_struct * prev,struct task_struct * next)172*387f9dfdSAndroid Build Coastguard Worker int BPF_PROG(handle_sched_switch, bool preempt, struct task_struct *prev, struct task_struct *next)
173*387f9dfdSAndroid Build Coastguard Worker {
174*387f9dfdSAndroid Build Coastguard Worker return handle_switch(preempt, prev, next);
175*387f9dfdSAndroid Build Coastguard Worker }
176*387f9dfdSAndroid Build Coastguard Worker
177*387f9dfdSAndroid Build Coastguard Worker char LICENSE[] SEC("license") = "GPL";
178