Repository navigation
feat(scheduler): add fine-grained priority levels (0-20) for priority… - #22
Conversation
… tasks Previously, priority tasks only had a binary distinction (priority vs non-priority). This change introduces a 21-level priority system for more granular control over task scheduling. Priority level semantics: - Levels 0-9 (high priority): can preempt tasks with lower priority - Levels 10-20 (medium priority): receive vtime reduction (50% to 5%) Changes: - Add priority_tasks_prio BPF map to track priority levels - Add can_preempt_by_prio() and calc_vtime_reduction() helpers - Update enqueue logic in both kernel_mode and user-space mode - Add UpdatePriorityTaskWithPrio() Go API - Remove BPF-side map update functions (now managed by user-space)
|
@weiso131 FYI |
There was a problem hiding this comment.
Pull request overview
This PR introduces a fine-grained priority system for task scheduling, upgrading from a binary priority distinction to a 21-level system (0-20). The change enables more granular control over task scheduling with two priority tiers: high priority (0-9) with preemption capability, and medium priority (10-20) with vtime reduction but no preemption.
Changes:
- Added
priority_tasks_prioBPF map to track granular priority levels (0-20) for tasks - Implemented helper functions
can_preempt_by_prio()andcalc_vtime_reduction()for priority logic - Updated enqueue logic in both kernel and user-space modes to handle the new priority levels
- Added
UpdatePriorityTaskWithPrio()Go API for setting tasks with specific priority levels - Removed BPF-side map management (now handled by user-space via C wrapper functions)
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| wrapper.h | Added declaration for update_priority_task_with_prio function |
| wrapper.c | Implemented update_priority_task_with_prio to update both priority maps, enhanced remove_priority_task to clean up both maps |
| goland_core/priority_tasks.go | Added priority level constants and UpdatePriorityTaskWithPrio Go API with validation |
| ebpf/main.bpf.c | Added priority_tasks_prio map, implemented priority helper functions, updated enqueue logic in both kernel/user-space modes, removed old update_priority_task_map function and its usage |
Comments suppressed due to low confidence (2)
ebpf/main.bpf.c:1252
- For high priority tasks (0-9), when scx_bpf_pick_idle_cpu returns a negative value other than -EBUSY, the task falls back to normal dispatch without any priority handling. This is inconsistent with the medium priority path (10-20) where tasks always get vtime reduction regardless of CPU availability.
Consider either:
- Falling back to SHARED_DSQ with preemption flags for high priority tasks when CPU selection fails (similar to medium priority), or
- Documenting why high priority tasks should lose their priority status in this scenario.
This ensures high priority tasks maintain their priority semantics even when no ideal CPU is available.
prio_cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
if (prio_cpu == -EBUSY) {
prio_cpu = prev_cpu;
}
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_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;
}
}
scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | prio_cpu,
prio_slice, prio_enq_flags);
__sync_fetch_and_add(&nr_kernel_dispatches, 1);
scx_bpf_kick_cpu(prio_cpu, SCX_KICK_IDLE);
return;
}
}
ebpf/main.bpf.c:1445
- Same issue as in kernel mode: for high priority tasks (0-9), when scx_bpf_pick_idle_cpu returns a negative value other than -EBUSY, the task falls back to normal scheduling without any priority handling. This is inconsistent with the medium priority path (10-20) where tasks always get vtime reduction.
Consider applying consistent fallback behavior where high priority tasks maintain their priority semantics even when no ideal CPU is available.
prio_cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
if (prio_cpu == -EBUSY) {
prio_cpu = scx_bpf_task_cpu(p);
}
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_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;
}
}
scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | prio_cpu,
slice, prio_enq_flags);
__sync_fetch_and_add(&nr_user_dispatches, 1);
scx_bpf_kick_cpu(prio_cpu, SCX_KICK_IDLE);
return;
}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Document race condition in two-step priority update process - Add proper error handling for priority_tasks_prio deletion
weiso131
left a comment
There was a problem hiding this comment.
Some suggestions for code optimization and maintainability.
…y_prio Address PR #22 review feedback from weiso131: - Remove can_preempt_by_prio() function and replace with a simple comparison (task_prio >= running_prio), since all call sites already guarantee prio <= 9, making the extra function call and branch unnecessary overhead. - Extract duplicated priority task dispatch logic from enqueue_task_kernel_mode() and goland_enqueue() into a shared handle_priority_task() inline function. The only difference between the two callers is which dispatch counter to bump (nr_kernel_dispatches vs nr_user_dispatches), which is now passed as a parameter.
… tasks
Previously, priority tasks only had a binary distinction (priority vs non-priority). This change introduces a 21-level priority system for more granular control over task scheduling.
Priority level semantics:
Changes: