Skip to contentSkip to conversation

sched_ext: Add a core event and update scx schedulers

PATCH Applied Changwoo Min 3 participants 5 messages Feb 7, 2025

Subsystems (2)
lore
Share & tools
lore

Changwoo Min

author reviewer
#0 lore raw

v2 cover sched_ext: Add a core event and update scx schedulers

This patchset introduces a new event, SCX_EV_ENQ_SLICE_DFL, and updates two scx schedulers -- scx_qmap and scx_central -- to print out the new event.

SCX_EV_ENQ_SLICE_DFL counts how many times the tasks' time slice is set to the default value (SCX_SLICE_DFL) by the sched_ext core in the enqueue and pick_next paths.

Scheduling a task with SCX_SLICE_DFL unintentionally would be a source of latency spikes because SCX_SLICE_DFL is relatively long (20 msec). Thus, soaring the SCX_EV_ENQ_SLICE_DFL value would be a sign of BPF scheduler bugs, causing latency spikes.

ChangeLog v1 -> v2:
- Instrument the event at select_task_rq_scx() when ops.select_cpu() is not provided.

Changwoo Min (2):
sched_ext: Add an event, SCX_EV_ENQ_SLICE_DFL
sched_ext: Print an event, SCX_EV_ENQ_SLICE_DFL, in scx_qmap/central

 kernel/sched/ext.c                | 16 +++++++++++++++-
 tools/sched_ext/scx_central.bpf.c |  2 ++
 tools/sched_ext/scx_qmap.bpf.c    |  2 ++
 3 files changed, 19 insertions(+), 1 deletion(-)
Signature · 1 line
2.48.1
author reviewer
#1 lore raw

PATCH v2 1/2 sched_ext: Add an event, SCX_EV_ENQ_SLICE_DFL

Add a core event, SCX_EV_ENQ_SLICE_DFL, which represents how many tasks have been enqueued (or pick_task-ed or select_cpu-ed) with a default time slice (SCX_SLICE_DFL).

Scheduling a task with SCX_SLICE_DFL unintentionally would be a source of latency spikes because SCX_SLICE_DFL is relatively long (20 msec). Thus, soaring the SCX_EV_ENQ_SLICE_DFL value would be a sign of BPF scheduler bugs, causing latency spikes, especially when ops.select_cpu() is provided.

__scx_add_event() is used since the caller holds an rq lock or p->pi_lock, so the preemption has already been disabled.

---
 kernel/sched/ext.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/sched/ext.c +15 -1
index 8a9a30895381..5ef90d9bcdd2 100644
@@ -1468,6 +1468,12 @@ struct scx_event_stats {
 	 */
 	u64		SCX_EV_ENQ_SKIP_EXITING;
 
+	/*
+	 * The total number of tasks enqueued (or pick_task-ed) with a
+	 * default time slice (SCX_SLICE_DFL).
+	 */
+	u64		SCX_EV_ENQ_SLICE_DFL;
+
 	/*
 	 * The total duration of bypass modes in nanoseconds.
 	 */
@@ -2134,6 +2140,7 @@ static void do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags,
 	 */
 	touch_core_sched(rq, p);
 	p->scx.slice = SCX_SLICE_DFL;
+	__scx_add_event(SCX_EV_ENQ_SLICE_DFL, 1);
 local_norefill:
 	dispatch_enqueue(&rq->scx.local_dsq, p, enq_flags);
 	return;
@@ -2141,6 +2148,7 @@ static void do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags,
 global:
 	touch_core_sched(rq, p);	/* see the comment in local: */
 	p->scx.slice = SCX_SLICE_DFL;
+	__scx_add_event(SCX_EV_ENQ_SLICE_DFL, 1);
 	dispatch_enqueue(find_global_dsq(p), p, enq_flags);
 }
 
@@ -3202,8 +3210,10 @@ static struct task_struct *pick_task_scx(struct rq *rq)
 	 */
 	if (keep_prev) {
 		p = prev;
-		if (!p->scx.slice)
+		if (!p->scx.slice) {
 			p->scx.slice = SCX_SLICE_DFL;
+			__scx_add_event(SCX_EV_ENQ_SLICE_DFL, 1);
+		}
 	} else {
 		p = first_local_task(rq);
 		if (!p) {
@@ -3219,6 +3229,7 @@ static struct task_struct *pick_task_scx(struct rq *rq)
 				scx_warned_zero_slice = true;
 			}
 			p->scx.slice = SCX_SLICE_DFL;
+			__scx_add_event(SCX_EV_ENQ_SLICE_DFL, 1);
 		}
 	}
 
@@ -3306,6 +3317,7 @@ static int select_task_rq_scx(struct task_struct *p, int prev_cpu, int wake_flag
 		if (found) {
 			p->scx.slice = SCX_SLICE_DFL;
 			p->scx.ddsp_dsq_id = SCX_DSQ_LOCAL;
+			__scx_add_event(SCX_EV_ENQ_SLICE_DFL, 1);
 		}
 
 		if (rq_bypass)
@@ -5023,6 +5035,7 @@ static void scx_dump_state(struct scx_exit_info *ei, size_t dump_len)
 	scx_dump_event(s, &events, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE);
 	scx_dump_event(s, &events, SCX_EV_DISPATCH_KEEP_LAST);
 	scx_dump_event(s, &events, SCX_EV_ENQ_SKIP_EXITING);
+	scx_dump_event(s, &events, SCX_EV_ENQ_SLICE_DFL);
 	scx_dump_event(s, &events, SCX_EV_BYPASS_DURATION);
 	scx_dump_event(s, &events, SCX_EV_BYPASS_DISPATCH);
 	scx_dump_event(s, &events, SCX_EV_BYPASS_ACTIVATE);
@@ -7163,6 +7176,7 @@ __bpf_kfunc void scx_bpf_events(struct scx_event_stats *events,
 		scx_agg_event(&e_sys, e_cpu, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE);
 		scx_agg_event(&e_sys, e_cpu, SCX_EV_DISPATCH_KEEP_LAST);
 		scx_agg_event(&e_sys, e_cpu, SCX_EV_ENQ_SKIP_EXITING);
+		scx_agg_event(&e_sys, e_cpu, SCX_EV_ENQ_SLICE_DFL);
 		scx_agg_event(&e_sys, e_cpu, SCX_EV_BYPASS_DURATION);
 		scx_agg_event(&e_sys, e_cpu, SCX_EV_BYPASS_DISPATCH);
 		scx_agg_event(&e_sys, e_cpu, SCX_EV_BYPASS_ACTIVATE);
Signature · 1 line
2.48.1
author reviewer
#2 lore raw

PATCH v2 2/2 sched_ext: Print an event, SCX_EV_ENQ_SLICE_DFL, in scx_qmap/central

In reply to Changwoo Min

Modify the scx_qmap and scx_celtral schedulers to print the SCX_EV_ENQ_SLICE_DFL event every second.

---
 tools/sched_ext/scx_central.bpf.c | 2 ++
 tools/sched_ext/scx_qmap.bpf.c    | 2 ++
 2 files changed, 4 insertions(+)
tools/sched_ext/scx_central.bpf.c +2 -0
index 376c14d5dd0d..907a844723c1 100644
@@ -305,6 +305,8 @@ static int central_timerfn(void *map, int *key, struct bpf_timer *timer)
 			   scx_read_event(&events, SCX_EV_DISPATCH_KEEP_LAST));
 		bpf_printk("%35s: %llu\n", "SCX_EV_ENQ_SKIP_EXITING",
 			   scx_read_event(&events, SCX_EV_ENQ_SKIP_EXITING));
+		bpf_printk("%35s: %llu\n", "SCX_EV_ENQ_SLICE_DFL",
+			   scx_read_event(&events, SCX_EV_ENQ_SLICE_DFL));
 		bpf_printk("%35s: %llu\n", "SCX_EV_BYPASS_DURATION",
 			   scx_read_event(&events, SCX_EV_BYPASS_DURATION));
 		bpf_printk("%35s: %llu\n", "SCX_EV_BYPASS_DISPATCH",
tools/sched_ext/scx_qmap.bpf.c +2 -0
index 5edb79742e37..7d9d1e5d2358 100644
@@ -784,6 +784,8 @@ static int monitor_timerfn(void *map, int *key, struct bpf_timer *timer)
 		   scx_read_event(&events, SCX_EV_DISPATCH_KEEP_LAST));
 	bpf_printk("%35s: %llu\n", "SCX_EV_ENQ_SKIP_EXITING",
 		   scx_read_event(&events, SCX_EV_ENQ_SKIP_EXITING));
+	bpf_printk("%35s: %llu\n", "SCX_EV_ENQ_SLICE_DFL",
+		   scx_read_event(&events, SCX_EV_ENQ_SLICE_DFL));
 	bpf_printk("%35s: %llu\n", "SCX_EV_BYPASS_DURATION",
 		   scx_read_event(&events, SCX_EV_BYPASS_DURATION));
 	bpf_printk("%35s: %llu\n", "SCX_EV_BYPASS_DISPATCH",
Signature · 1 line
2.48.1
reviewer Acked-by on the v2 series
#3 lore raw
In reply to Changwoo Min
Quoted 4 lines · Changwoo Min

This patchset introduces a new event, SCX_EV_ENQ_SLICE_DFL, and updates two scx schedulers -- scx_qmap and scx_central -- to print out the new event.

Quoted 8 lines

SCX_EV_ENQ_SLICE_DFL counts how many times the tasks' time slice is set to the default value (SCX_SLICE_DFL) by the sched_ext core in the enqueue and pick_next paths.

Scheduling a task with SCX_SLICE_DFL unintentionally would be a source of latency spikes because SCX_SLICE_DFL is relatively long (20 msec). Thus, soaring the SCX_EV_ENQ_SLICE_DFL value would be a sign of BPF scheduler bugs, causing latency spikes.

Looks good to me, thanks!

Quoted 5 lines

ChangeLog v1 -> v2:
- Instrument the event at select_task_rq_scx() when ops.select_cpu() is not provided.

Quoted 12 lines

Changwoo Min (2):
sched_ext: Add an event, SCX_EV_ENQ_SLICE_DFL
sched_ext: Print an event, SCX_EV_ENQ_SLICE_DFL, in scx_qmap/central

 kernel/sched/ext.c                | 16 +++++++++++++++-
 tools/sched_ext/scx_central.bpf.c |  2 ++
 tools/sched_ext/scx_qmap.bpf.c    |  2 ++
 3 files changed, 19 insertions(+), 1 deletion(-)
Signature · 1 line
2.48.1

-Andrea

reviewer Applied
#4 lore raw
In reply to Changwoo Min
Quoted 4 lines · Changwoo Min

This patchset introduces a new event, SCX_EV_ENQ_SLICE_DFL, and updates two scx schedulers -- scx_qmap and scx_central -- to print out the new event.

Quoted 8 lines

SCX_EV_ENQ_SLICE_DFL counts how many times the tasks' time slice is set to the default value (SCX_SLICE_DFL) by the sched_ext core in the enqueue and pick_next paths.

Scheduling a task with SCX_SLICE_DFL unintentionally would be a source of latency spikes because SCX_SLICE_DFL is relatively long (20 msec). Thus, soaring the SCX_EV_ENQ_SLICE_DFL value would be a sign of BPF scheduler bugs, causing latency spikes.

Applied to sched_ext/for-6.15.

Thanks.

Signature · 1 line
tejun