Skip to content

Commit 344b69e

Browse files
committed
scx_p2dq: Prefer dispatching into local CPU DSQs
When the local CPU is available prefer dispatching into the per CPU local DSQ. This gives slightly better locality and reduces the number of CPU migrations. Signed-off-by: Daniel Hodges <hodgesd@meta.com>
1 parent b087d2a commit 344b69e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

scheds/rust/scx_p2dq/src/bpf/main.bpf.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,32 @@ static __always_inline void async_p2dq_enqueue(struct enqueue_promise *ret,
864864
return;
865865
}
866866

867+
// If the last CPU is idle just reenque to the CPUs local DSQ. This
868+
// should reduce the number of migrations.
869+
if (scx_bpf_dsq_nr_queued(taskc->last_cpu_dsq_id) == 0 &&
870+
scx_bpf_dsq_nr_queued(SCX_DSQ_LOCAL_ON|taskc->last_cpu) == 0) {
871+
if (!(llcx = lookup_llc_ctx(taskc->llc_id))) {
872+
scx_bpf_error("invalid lookup");
873+
ret->kind = P2DQ_ENQUEUE_PROMISE_COMPLETE;
874+
return;
875+
}
876+
if (llcx->id != taskc->llc_id)
877+
p->scx.dsq_vtime = llcx->vtime;
878+
else
879+
p->scx.dsq_vtime = max(p->scx.dsq_vtime,
880+
llcx->vtime - max_dsq_time_slice());
881+
scx_bpf_dsq_insert_vtime(p,
882+
taskc->last_cpu_dsq_id,
883+
taskc->slice_ns,
884+
p->scx.dsq_vtime, enq_flags);
885+
if (scx_bpf_test_and_clear_cpu_idle(taskc->last_cpu)) {
886+
stat_inc(P2DQ_STAT_IDLE);
887+
scx_bpf_kick_cpu(cpu, SCX_KICK_IDLE);
888+
}
889+
ret->kind = P2DQ_ENQUEUE_PROMISE_COMPLETE;
890+
return;
891+
}
892+
867893
if (!(cpuc = lookup_cpu_ctx(scx_bpf_task_cpu(p))) ||
868894
!(llcx = lookup_llc_ctx(cpuc->llc_id))) {
869895
scx_bpf_error("invalid lookup");
@@ -967,6 +993,8 @@ static __always_inline int p2dq_running_impl(struct task_struct *p)
967993
taskc->llc_id = llcx->id;
968994
taskc->node_id = llcx->node_id;
969995
taskc->was_nice = p->scx.weight < 100;
996+
taskc->last_cpu = task_cpu;
997+
taskc->last_cpu_dsq_id = cpuc->dsq_id;
970998
cpuc->interactive = taskc->interactive;
971999
cpuc->dsq_index = taskc->dsq_index;
9721000
cpuc->nice_task = p->scx.weight < 100;

scheds/rust/scx_p2dq/src/bpf/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct p2dq_timer {
1010
struct cpu_ctx {
1111
int id;
1212
u32 llc_id;
13+
u64 dsq_id;
1314
u64 affn_dsq;
1415
u32 dsq_index;
1516
u64 slice_ns;
@@ -61,10 +62,12 @@ struct task_p2dq {
6162
u32 node_id;
6263
u64 used;
6364
u64 last_dsq_id;
65+
u64 last_cpu_dsq_id;
6466
u64 last_run_started;
6567
u64 last_run_at;
6668
u64 llc_runs; /* how many runs on the current LLC */
6769
int last_dsq_index;
70+
s32 last_cpu;
6871
bool interactive;
6972
bool was_nice;
7073

0 commit comments

Comments
 (0)