-
Notifications
You must be signed in to change notification settings - Fork 0
/
sched_ext.bpf.c
45 lines (37 loc) · 1.45 KB
/
sched_ext.bpf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
// Define a shared Dispatch Queue (DSQ) ID
#define SHARED_DSQ_ID 0
#define BPF_STRUCT_OPS(name, args...) \
SEC("struct_ops/"#name) BPF_PROG(name, ##args)
#define BPF_STRUCT_OPS_SLEEPABLE(name, args...) \
SEC("struct_ops.s/"#name) \
BPF_PROG(name, ##args)
// Initialize the scheduler by creating a shared dispatch queue (DSQ)
s32 BPF_STRUCT_OPS_SLEEPABLE(sched_init) {
return scx_bpf_create_dsq(SHARED_DSQ_ID, -1);
}
// Enqueue a task to the shared DSQ, dispatching it with a time slice
int BPF_STRUCT_OPS(sched_enqueue, struct task_struct *p, u64 enq_flags) {
// Calculate the time slice for the task based on the number of tasks in the queue
u64 slice = 5000000u / scx_bpf_dsq_nr_queued(SHARED_DSQ_ID);
scx_bpf_dispatch(p, SHARED_DSQ_ID, slice, enq_flags);
return 0;
}
// Dispatch a task from the shared DSQ to a CPU
int BPF_STRUCT_OPS(sched_dispatch, s32 cpu, struct task_struct *prev) {
scx_bpf_consume(SHARED_DSQ_ID);
return 0;
}
// Define the main scheduler operations structure (sched_ops)
SEC(".struct_ops.link")
struct sched_ext_ops sched_ops = {
.enqueue = (void *)sched_enqueue,
.dispatch = (void *)sched_dispatch,
.init = (void *)sched_init,
.flags = SCX_OPS_ENQ_LAST | SCX_OPS_KEEP_BUILTIN_IDLE,
.name = "minimal_scheduler"
};
// License for the BPF program
char _license[] SEC("license") = "GPL";