blob: 9a5c1f008fe60302d36df1fcb259b14767011b57 [file] [log] [blame]
Andrii Nakryiko9c015462020-01-12 23:31:42 -08001// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2019 Facebook
3#include "vmlinux.h"
Toke Høiland-Jørgensen5b554ce2020-01-20 14:06:44 +01004#include <bpf/bpf_helpers.h>
Andrii Nakryiko9c015462020-01-12 23:31:42 -08005#include "runqslower.h"
6
7#define TASK_RUNNING 0
Andrii Nakryiko37ccc122020-03-10 21:30:10 -07008#define BPF_F_CURRENT_CPU 0xffffffffULL
Andrii Nakryiko9c015462020-01-12 23:31:42 -08009
Andrii Nakryiko9c015462020-01-12 23:31:42 -080010const volatile __u64 min_us = 0;
11const volatile pid_t targ_pid = 0;
12
13struct {
Song Liuced47e32021-02-25 15:43:19 -080014 __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
15 __uint(map_flags, BPF_F_NO_PREALLOC);
16 __type(key, int);
Andrii Nakryiko9c015462020-01-12 23:31:42 -080017 __type(value, u64);
18} start SEC(".maps");
19
20struct {
21 __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
22 __uint(key_size, sizeof(u32));
23 __uint(value_size, sizeof(u32));
24} events SEC(".maps");
25
26/* record enqueue timestamp */
27__always_inline
Song Liuced47e32021-02-25 15:43:19 -080028static int trace_enqueue(struct task_struct *t)
Andrii Nakryiko9c015462020-01-12 23:31:42 -080029{
Song Liuced47e32021-02-25 15:43:19 -080030 u32 pid = t->pid;
31 u64 *ptr;
Andrii Nakryiko9c015462020-01-12 23:31:42 -080032
33 if (!pid || (targ_pid && targ_pid != pid))
34 return 0;
35
Song Liuced47e32021-02-25 15:43:19 -080036 ptr = bpf_task_storage_get(&start, t, 0,
37 BPF_LOCAL_STORAGE_GET_F_CREATE);
38 if (!ptr)
39 return 0;
40
41 *ptr = bpf_ktime_get_ns();
Andrii Nakryiko9c015462020-01-12 23:31:42 -080042 return 0;
43}
44
45SEC("tp_btf/sched_wakeup")
46int handle__sched_wakeup(u64 *ctx)
47{
48 /* TP_PROTO(struct task_struct *p) */
49 struct task_struct *p = (void *)ctx[0];
50
Song Liuced47e32021-02-25 15:43:19 -080051 return trace_enqueue(p);
Andrii Nakryiko9c015462020-01-12 23:31:42 -080052}
53
54SEC("tp_btf/sched_wakeup_new")
55int handle__sched_wakeup_new(u64 *ctx)
56{
57 /* TP_PROTO(struct task_struct *p) */
58 struct task_struct *p = (void *)ctx[0];
59
Song Liuced47e32021-02-25 15:43:19 -080060 return trace_enqueue(p);
Andrii Nakryiko9c015462020-01-12 23:31:42 -080061}
62
63SEC("tp_btf/sched_switch")
64int handle__sched_switch(u64 *ctx)
65{
66 /* TP_PROTO(bool preempt, struct task_struct *prev,
67 * struct task_struct *next)
68 */
69 struct task_struct *prev = (struct task_struct *)ctx[1];
70 struct task_struct *next = (struct task_struct *)ctx[2];
Naveen N. Rao88a71082022-01-06 17:15:08 +053071 struct runq_event event = {};
Andrii Nakryiko9c015462020-01-12 23:31:42 -080072 u64 *tsp, delta_us;
73 long state;
74 u32 pid;
75
76 /* ivcsw: treat like an enqueue event and store timestamp */
SanjayKumar Jeyakumar5616e892021-07-07 10:59:14 +053077 if (prev->__state == TASK_RUNNING)
Song Liuced47e32021-02-25 15:43:19 -080078 trace_enqueue(prev);
Andrii Nakryiko9c015462020-01-12 23:31:42 -080079
80 pid = next->pid;
81
Song Liuced47e32021-02-25 15:43:19 -080082 /* For pid mismatch, save a bpf_task_storage_get */
83 if (!pid || (targ_pid && targ_pid != pid))
84 return 0;
85
Andrii Nakryiko9c015462020-01-12 23:31:42 -080086 /* fetch timestamp and calculate delta */
Song Liuced47e32021-02-25 15:43:19 -080087 tsp = bpf_task_storage_get(&start, next, 0, 0);
Andrii Nakryiko9c015462020-01-12 23:31:42 -080088 if (!tsp)
89 return 0; /* missed enqueue */
90
91 delta_us = (bpf_ktime_get_ns() - *tsp) / 1000;
92 if (min_us && delta_us <= min_us)
93 return 0;
94
95 event.pid = pid;
96 event.delta_us = delta_us;
97 bpf_get_current_comm(&event.task, sizeof(event.task));
98
99 /* output */
100 bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU,
101 &event, sizeof(event));
102
Song Liuced47e32021-02-25 15:43:19 -0800103 bpf_task_storage_delete(&start, next);
Andrii Nakryiko9c015462020-01-12 23:31:42 -0800104 return 0;
105}
106
107char LICENSE[] SEC("license") = "GPL";