Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 170 additions & 27 deletions ebpf/main.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ struct {
__uint(max_entries, MAX_ENQUEUED_TASKS);
} priority_tasks SEC(".maps");

/*
* Map to track priority levels (0-20) for priority tasks.
*
* Priority levels:
* 0-9: High priority - can preempt tasks with lower priority
* 10-20: Medium priority - gets vtime reduction but no preemption
*
* Tasks not in this map are considered normal priority (no special handling).
*/
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, u32); /* PID */
__type(value, u32); /* priority level 0-20 */
__uint(max_entries, MAX_ENQUEUED_TASKS);
} priority_tasks_prio SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, s32); /* CPU */
Expand Down Expand Up @@ -468,16 +484,43 @@ static u64 cpu_to_dsq(s32 cpu)
}

/*
* Helper function to update priority tasks map based on vtime.
* If vtime == 0, add PID to map. If vtime != 0, remove PID from map.
* Check if task with prio1 can preempt task with prio2.
* Only priority levels 0-9 can preempt, and only tasks with lower priority.
* Returns true if preemption is allowed.
*/
static void update_priority_task_map(u32 pid, u64 vtime, u64 slice)
static bool can_preempt_by_prio(u32 prio1, u32 prio2)
Comment thread
ianchen0119 marked this conversation as resolved.
Resolved
{
if (vtime == 0) {
bpf_map_update_elem(&priority_tasks, &pid, &slice, BPF_ANY);
} else {
bpf_map_delete_elem(&priority_tasks, &pid);
}
/* Only priority 0-9 can preempt */
if (prio1 > 9)
return false;
/* Can only preempt lower priority (higher number) tasks */
return prio1 < prio2;
}

/*
* Calculate vtime reduction for priority levels 10-20.
* Higher priority (lower number) gets more vtime reduction.
* Returns the vtime reduction amount.
*/
static u64 calc_vtime_reduction(u32 prio, u64 base_vtime)
{
/*
* Priority 10-20 get vtime reduction.
* Priority 10 gets 50% reduction, priority 20 gets 5% reduction.
* Linear interpolation: reduction = 5 + (20 - prio) * 4.5% of base_vtime
* Simplified: reduction = (50 - (prio - 10) * 4.5)% = (5 + (20 - prio) * 4.5)%
* Using integer math: (55 - prio * 4.5) -> (110 - prio * 9) / 2
*/
if (prio < 10 || prio > 20)
return 0;

/*
* New formula: priority 10 = 50%, priority 20 = 5%
* reduction_pct = 50 - (prio - 10) * 4.5
* Using integer: (100 - (prio - 10) * 9) / 2 = (100 - 9*prio + 90) / 2 = (190 - 9*prio) / 2
*/
u64 reduction_pct = (190 - prio * 9) / 2;
return (base_vtime * reduction_pct) / 100;
}

/*
Expand Down Expand Up @@ -682,7 +725,6 @@ static void dispatch_task(const struct dispatched_task_ctx *task)
__sync_fetch_and_add(&nr_user_dispatches, 1);
}
}
update_priority_task_map(task->pid, task->vtime, task->slice_ns);

/*
* If the cpumask is not valid anymore, ignore the dispatch event.
Expand Down Expand Up @@ -1130,33 +1172,81 @@ static void enqueue_task_kernel_mode(struct task_struct *p, u64 enq_flags)
/*
* Handle priority tasks with custom dispatch logic (similar to user-space mode).
Comment thread
ianchen0119 marked this conversation as resolved.
Resolved
* Check if the task is in priority_tasks map and dispatch with preemption.
*
* Priority levels 0-9: can preempt tasks with lower priority
* Priority levels 10-20: get vtime reduction but no preemption
*/
u64 *prio_elem;
u64 *running_prio_elem;
u32 *task_prio_ptr;
u32 *running_prio_ptr;
u64 prio_slice;
u32 pid = p->pid;
s32 prio_cpu = -EBUSY;
u64 prio_enq_flags = SCX_ENQ_PREEMPT;
u64 prio_enq_flags = 0;
u32 *cur_pid_val;
u32 cur_pid;
u32 task_prio = 21; /* Default: no priority (lowest) */
u32 running_prio = 21;

prio_elem = bpf_map_lookup_elem(&priority_tasks, &pid);
if (prio_elem) {
task_prio_ptr = bpf_map_lookup_elem(&priority_tasks_prio, &pid);
if (task_prio_ptr)
task_prio = *task_prio_ptr;
else
task_prio = 0; /* Default to highest if in priority_tasks but no prio set */

prio_slice = *prio_elem;

/*
* Priority 10-20: apply vtime reduction and use normal scheduling
*/
if (task_prio >= 10 && task_prio <= 20) {
u64 vtime_reduction = calc_vtime_reduction(task_prio, p->scx.dsq_vtime);
u64 adjusted_vtime = p->scx.dsq_vtime > vtime_reduction ?
p->scx.dsq_vtime - vtime_reduction : 0;

prio_cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
if (prio_cpu >= 0) {
scx_bpf_dsq_insert_vtime(p, cpu_to_dsq(prio_cpu),
prio_slice, adjusted_vtime, enq_flags);
__sync_fetch_and_add(&nr_kernel_dispatches, 1);
scx_bpf_kick_cpu(prio_cpu, SCX_KICK_IDLE);
return;
}
/* No idle CPU, dispatch to SHARED_DSQ with reduced vtime */
scx_bpf_dsq_insert_vtime(p, SHARED_DSQ,
prio_slice, adjusted_vtime, enq_flags);
__sync_fetch_and_add(&nr_kernel_dispatches, 1);
scx_bpf_kick_cpu(prev_cpu, SCX_KICK_IDLE);
return;
}

/*
* Priority 0-9: can preempt lower priority tasks
*/
prio_cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
if (prio_cpu == -EBUSY) {
prio_cpu = prev_cpu;
}
prio_slice = *prio_elem;

if (prio_cpu >= 0) {
prio_enq_flags = SCX_ENQ_PREEMPT; /* Default: preempt */

cur_pid_val = bpf_map_lookup_elem(&running_task, &prio_cpu);
if (cur_pid_val) {
cur_pid = *cur_pid_val;
running_prio_elem = bpf_map_lookup_elem(&priority_tasks, &cur_pid);
running_prio_ptr = bpf_map_lookup_elem(&priority_tasks_prio, &cur_pid);
if (running_prio_ptr)
running_prio = *running_prio_ptr;
else if (bpf_map_lookup_elem(&priority_tasks, &cur_pid))
running_prio = 0; /* In priority_tasks but no prio level */

/*
* If current running task is prioritized, do not preempt it (use SCX_ENQ_HEAD).
* Otherwise, keep the flag equals to SCX_ENQ_PREEMPT.
* Only preempt if this task has higher priority (lower number)
* than the running task.
*/
if (running_prio_elem) {
if (!can_preempt_by_prio(task_prio, running_prio)) {
prio_enq_flags = SCX_ENQ_HEAD;
}
}
Expand Down Expand Up @@ -1275,30 +1365,81 @@ void BPF_STRUCT_OPS(goland_enqueue, struct task_struct *p, u64 enq_flags)

/*
* Handle priority tasks with custom dispatch logic.
*
* Priority levels 0-9: can preempt tasks with lower priority
* Priority levels 10-20: get vtime reduction but no preemption
*/
u64* elem;
u64 *elem;
u32 *task_prio_ptr;
u32 *running_prio_ptr;
u64 slice;
u32 pid = p->pid;
s32 prio_cpu = -EBUSY;
u64 prio_enq_flags = SCX_ENQ_PREEMPT;
u32* cur_pid_val;
u64 prio_enq_flags = 0;
u32 *cur_pid_val;
u32 cur_pid;
u32 task_prio = 21; /* Default: no priority (lowest) */
u32 running_prio = 21;

elem = bpf_map_lookup_elem(&priority_tasks, &pid);
if (elem) {
task_prio_ptr = bpf_map_lookup_elem(&priority_tasks_prio, &pid);
if (task_prio_ptr)
task_prio = *task_prio_ptr;
else
task_prio = 0; /* Default to highest if in priority_tasks but no prio set */

slice = *elem;

/*
* Priority 10-20: apply vtime reduction and use normal scheduling
*/
if (task_prio >= 10 && task_prio <= 20) {
u64 vtime_reduction = calc_vtime_reduction(task_prio, p->scx.dsq_vtime);
u64 adjusted_vtime = p->scx.dsq_vtime > vtime_reduction ?
p->scx.dsq_vtime - vtime_reduction : 0;

prio_cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
if (prio_cpu >= 0) {
scx_bpf_dsq_insert_vtime(p, cpu_to_dsq(prio_cpu),
slice, adjusted_vtime, enq_flags);
__sync_fetch_and_add(&nr_user_dispatches, 1);
scx_bpf_kick_cpu(prio_cpu, SCX_KICK_IDLE);
return;
}
/* No idle CPU, dispatch to SHARED_DSQ with reduced vtime */
scx_bpf_dsq_insert_vtime(p, SHARED_DSQ,
slice, adjusted_vtime, enq_flags);
__sync_fetch_and_add(&nr_user_dispatches, 1);
scx_bpf_kick_cpu(prev_cpu, SCX_KICK_IDLE);
return;
}

/*
* Priority 0-9: can preempt lower priority tasks
*/
prio_cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
if (prio_cpu == -EBUSY) {
prio_cpu = scx_bpf_task_cpu(p);
}
slice = *elem;

if (prio_cpu >= 0) {
prio_enq_flags = SCX_ENQ_PREEMPT; /* Default: preempt */

cur_pid_val = bpf_map_lookup_elem(&running_task, &prio_cpu);
if (cur_pid_val) {
cur_pid = *cur_pid_val;
elem = bpf_map_lookup_elem(&priority_tasks, &cur_pid);
// If current running task is prioritized, do not preempt it (SCX_ENQ_HEAD).
// Otherwise, keep the flag equals to SCX_ENQ_PREEMPT
if (elem) {
running_prio_ptr = bpf_map_lookup_elem(&priority_tasks_prio, &cur_pid);
if (running_prio_ptr)
running_prio = *running_prio_ptr;
else if (bpf_map_lookup_elem(&priority_tasks, &cur_pid))
running_prio = 0; /* In priority_tasks but no prio level */

/*
* Only preempt if this task has higher priority (lower number)
* than the running task.
*/
if (!can_preempt_by_prio(task_prio, running_prio)) {
prio_enq_flags = SCX_ENQ_HEAD;
}
}
Expand Down Expand Up @@ -1889,13 +2030,15 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(goland_init)
/*
* A task is being destroyed.
*
* Clean up the task from priority tasks map.
* Clean up the task from priority tasks maps.
*/
void BPF_STRUCT_OPS(goland_exit_task, struct task_struct *p,
struct scx_exit_task_args *args)
{
/* Remove task from priority tasks map */
update_priority_task_map(p->pid, 1, 0);
u32 pid = p->pid;
/* Remove task from priority tasks maps */
bpf_map_delete_elem(&priority_tasks, &pid);
bpf_map_delete_elem(&priority_tasks_prio, &pid);
}

/*
Expand Down
35 changes: 35 additions & 0 deletions goland_core/priority_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ import (
"fmt"
)

// Priority level constants
const (
// PrioHighMin is the minimum (highest) priority for preemptive tasks
PrioHighMin = 0
// PrioHighMax is the maximum priority for preemptive tasks (0-9 can preempt)
PrioHighMax = 9
// PrioMedMin is the minimum priority for vtime-reduced tasks
PrioMedMin = 10
// PrioMedMax is the maximum priority for vtime-reduced tasks
PrioMedMax = 20
)

// UpdatePriorityTask adds or updates a task in the priority_tasks BPF map.
// When a task is in this map, it will be prioritized during scheduling.
// This function uses the default highest priority (0).
// pid: the process ID of the task
// slice: the time slice to assign to this priority task (in nanoseconds)
func (s *Sched) UpdatePriorityTask(pid uint32, slice uint64) error {
Expand All @@ -26,6 +39,28 @@ func (s *Sched) UpdatePriorityTask(pid uint32, slice uint64) error {
return nil
}

// UpdatePriorityTaskWithPrio adds or updates a task in the priority_tasks BPF map
// with a specific priority level.
//
// Priority levels:
// - 0-9: High priority - can preempt tasks with lower priority (higher number)
// - 10-20: Medium priority - gets vtime reduction but no preemption.
// Priority 10 gets 50% vtime reduction, priority 20 gets 5% reduction.
Comment thread
ianchen0119 marked this conversation as resolved.
Resolved
//
// pid: the process ID of the task
// slice: the time slice to assign to this priority task (in nanoseconds)
// prio: priority level (0-20), where lower is higher priority
func (s *Sched) UpdatePriorityTaskWithPrio(pid uint32, slice uint64, prio uint32) error {
if prio > PrioMedMax {
return fmt.Errorf("invalid priority level: %d (must be 0-%d)", prio, PrioMedMax)
}
ret := C.update_priority_task_with_prio(C.u32(pid), C.u64(slice), C.u32(prio))
if ret != 0 {
return fmt.Errorf("failed to update priority task with prio: pid=%d, prio=%d, ret=%d", pid, prio, ret)
}
return nil
}

// RemovePriorityTask removes a task from the priority_tasks BPF map.
// After removal, the task will no longer be treated as a priority task.
// pid: the process ID of the task to remove
Expand Down
44 changes: 42 additions & 2 deletions wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,51 @@ int update_priority_task(u32 pid, u64 slice) {
BPF_ANY);
}

/*
* Note: There is a potential race condition in the two-step update process.
* When priority_tasks is updated first and then priority_tasks_prio, there's
* a brief window where the BPF code could observe a task in priority_tasks
* but not in priority_tasks_prio. During this window, the BPF code will
* default the task to priority 0 (highest priority with preemption).
* This is considered acceptable as it's a transient state and the BPF code
* handles missing priority gracefully. A more atomic approach is limited
* by BPF map API constraints.
*/
int update_priority_task_with_prio(u32 pid, u64 slice, u32 prio) {
if (!global_obj || !global_obj->maps.priority_tasks ||
!global_obj->maps.priority_tasks_prio)
return -1;

int ret = bpf_map__update_elem(global_obj->maps.priority_tasks,
&pid, sizeof(pid),
&slice, sizeof(slice),
BPF_ANY);
if (ret != 0)
return ret;

return bpf_map__update_elem(global_obj->maps.priority_tasks_prio,
&pid, sizeof(pid),
&prio, sizeof(prio),
BPF_ANY);
}
Comment thread
ianchen0119 marked this conversation as resolved.
Resolved

int remove_priority_task(u32 pid) {
if (!global_obj || !global_obj->maps.priority_tasks)
return -1;
return bpf_map__delete_elem(global_obj->maps.priority_tasks,
&pid, sizeof(pid),

int ret;

/* Also remove from priority_tasks_prio if it exists */
if (global_obj->maps.priority_tasks_prio) {
ret = bpf_map__delete_elem(global_obj->maps.priority_tasks_prio,
&pid, sizeof(pid),
0);
if (ret != 0)
return ret;
}

return bpf_map__delete_elem(global_obj->maps.priority_tasks,
&pid, sizeof(pid),
0);
}

Expand Down
2 changes: 2 additions & 0 deletions wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ void destroy_skel(void *);

int update_priority_task(u32 pid, u64 slice);

int update_priority_task_with_prio(u32 pid, u64 slice, u32 prio);

int remove_priority_task(u32 pid);

void set_scx_enums(
Expand Down
Loading