Skip to content

feat(scheduler): add fine-grained priority levels (0-20) for priority… - #22

Merged
ianchen0119 merged 6 commits into
mainfrom
enhance/priority-task-granularity
Feb 20, 2026
Merged

ianchen0119 merged 6 commits into
mainfrom
enhance/priority-task-granularity

Conversation

@ianchen0119

Copy link
Copy Markdown
Member

… 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)

… 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)
Copilot AI review requested due to automatic review settings February 2, 2026 14:15
@ianchen0119

Copy link
Copy Markdown
Member Author

@weiso131 FYI

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_prio BPF map to track granular priority levels (0-20) for tasks
  • Implemented helper functions can_preempt_by_prio() and calc_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:

  1. Falling back to SHARED_DSQ with preemption flags for high priority tasks when CPU selection fails (similar to medium priority), or
  2. 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.

Comment thread ebpf/main.bpf.c Resolved
Comment thread goland_core/priority_tasks.go Resolved
Comment thread wrapper.c Resolved
Comment thread wrapper.c Resolved
- Document race condition in two-step priority update process
- Add proper error handling for priority_tasks_prio deletion

@weiso131 weiso131 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions for code optimization and maintainability.

Comment thread ebpf/main.bpf.c Resolved
Comment thread ebpf/main.bpf.c Resolved
…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.
@ianchen0119
ianchen0119 merged commit 74e7bd0 into main Feb 20, 2026
1 check failed
@ianchen0119
ianchen0119 deleted the enhance/priority-task-granularity branch February 20, 2026 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants