1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
4 #include <bpf/bpf_helpers.h>
5 #include "runqslower.h"
8 #define BPF_F_CURRENT_CPU 0xffffffffULL
10 const volatile __u64 min_us
= 0;
11 const volatile pid_t targ_pid
= 0;
14 __uint(type
, BPF_MAP_TYPE_TASK_STORAGE
);
15 __uint(map_flags
, BPF_F_NO_PREALLOC
);
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");
26 /* record enqueue timestamp */
28 static int trace_enqueue(struct task_struct
*t
)
33 if (!pid
|| (targ_pid
&& targ_pid
!= pid
))
36 ptr
= bpf_task_storage_get(&start
, t
, 0,
37 BPF_LOCAL_STORAGE_GET_F_CREATE
);
41 *ptr
= bpf_ktime_get_ns();
45 SEC("tp_btf/sched_wakeup")
46 int handle__sched_wakeup(u64
*ctx
)
48 /* TP_PROTO(struct task_struct *p) */
49 struct task_struct
*p
= (void *)ctx
[0];
51 return trace_enqueue(p
);
54 SEC("tp_btf/sched_wakeup_new")
55 int handle__sched_wakeup_new(u64
*ctx
)
57 /* TP_PROTO(struct task_struct *p) */
58 struct task_struct
*p
= (void *)ctx
[0];
60 return trace_enqueue(p
);
63 SEC("tp_btf/sched_switch")
64 int handle__sched_switch(u64
*ctx
)
66 /* TP_PROTO(bool preempt, struct task_struct *prev,
67 * struct task_struct *next)
69 struct task_struct
*prev
= (struct task_struct
*)ctx
[1];
70 struct task_struct
*next
= (struct task_struct
*)ctx
[2];
71 struct event event
= {};
76 /* ivcsw: treat like an enqueue event and store timestamp */
77 if (prev
->state
== TASK_RUNNING
)
82 /* For pid mismatch, save a bpf_task_storage_get */
83 if (!pid
|| (targ_pid
&& targ_pid
!= pid
))
86 /* fetch timestamp and calculate delta */
87 tsp
= bpf_task_storage_get(&start
, next
, 0, 0);
89 return 0; /* missed enqueue */
91 delta_us
= (bpf_ktime_get_ns() - *tsp
) / 1000;
92 if (min_us
&& delta_us
<= min_us
)
96 event
.delta_us
= delta_us
;
97 bpf_get_current_comm(&event
.task
, sizeof(event
.task
));
100 bpf_perf_event_output(ctx
, &events
, BPF_F_CURRENT_CPU
,
101 &event
, sizeof(event
));
103 bpf_task_storage_delete(&start
, next
);
107 char LICENSE
[] SEC("license") = "GPL";