File 6702-Trivial-refactoring-of-try_steal_task.patch of Package erlang
From 110ff4aaa8e84f824eb015a7d442d1a3d025c813 Mon Sep 17 00:00:00 2001
From: Robin Morisset <rmorisset@meta.com>
Date: Mon, 14 Apr 2025 01:50:24 -0700
Subject: [PATCH 2/4] Trivial refactoring of try_steal_task
---
erts/emulator/beam/erl_process.c | 87 ++++++++++++++++----------------
1 file changed, 44 insertions(+), 43 deletions(-)
diff --git a/erts/emulator/beam/erl_process.c b/erts/emulator/beam/erl_process.c
index 536750116d..f09ff0adda 100644
--- a/erts/emulator/beam/erl_process.c
+++ b/erts/emulator/beam/erl_process.c
@@ -4647,7 +4647,7 @@ try_steal_task(ErtsRunQueue *rq, Process **result_proc)
flags = empty_runq_get_old_flags(rq);
if (flags & ERTS_RUNQ_FLG_SUSPENDED)
- return 0; /* go suspend instead... */
+ return 0; /* go suspend instead... */
ERTS_LC_ASSERT(erts_lc_runq_is_locked(rq));
erts_runq_unlock(rq);
@@ -4655,57 +4655,58 @@ try_steal_task(ErtsRunQueue *rq, Process **result_proc)
get_no_runqs(&active_rqs, &blnc_rqs);
if (active_rqs > blnc_rqs)
- active_rqs = blnc_rqs;
+ active_rqs = blnc_rqs;
if (erts_atomic32_read_acqb(&no_empty_run_queues) >= blnc_rqs)
goto end_try_steal_task;
- if (rq->ix < active_rqs) {
- /* First try to steal from an inactive run queue... */
- if (active_rqs < blnc_rqs) {
- int no = blnc_rqs - active_rqs;
- int stop_ix = vix = active_rqs + rq->ix % no;
- while (1) {
- res = check_possible_steal_victim(rq, vix, result_proc, &contended_runqueues);
- if (res) {
- DESTROY_WSTACK(contended_runqueues);
- return res;
- }
- vix++;
- if (vix >= blnc_rqs)
- vix = active_rqs;
- if (vix == stop_ix)
- break;
- }
- }
-
- vix = rq->ix;
-
- /* ... then try to steal a job from another active queue... */
- while (1) {
- vix++;
- if (vix >= active_rqs)
- vix = 0;
- if (vix == rq->ix)
- break;
-
- res = check_possible_steal_victim(rq, vix, result_proc, &contended_runqueues);
- if (res) {
- DESTROY_WSTACK(contended_runqueues);
- return res;
- }
- }
+ if (rq->ix >= active_rqs)
+ goto end_try_steal_task;
- /* ... and finally re-try stealing from the queues that were skipped because contended.
- We recheck the number of empty runqueues in each iteration, as taking the runqueue lock in check_possible_steal_victim can take quite a while. */
- while (!WSTACK_ISEMPTY(contended_runqueues)
- && (erts_atomic32_read_acqb(&no_empty_run_queues) < blnc_rqs)) {
- vix = WSTACK_POP(contended_runqueues);
- res = check_possible_steal_victim(rq, vix, result_proc, NULL);
+ /* First try to steal from an inactive run queue... */
+ if (active_rqs < blnc_rqs) {
+ int no = blnc_rqs - active_rqs;
+ int stop_ix = vix = active_rqs + rq->ix % no;
+ while (1) {
+ res = check_possible_steal_victim(rq, vix, result_proc, &contended_runqueues);
if (res) {
DESTROY_WSTACK(contended_runqueues);
return res;
}
+ vix++;
+ if (vix >= blnc_rqs)
+ vix = active_rqs;
+ if (vix == stop_ix)
+ break;
+ }
+ }
+
+ vix = rq->ix;
+
+ /* ... then try to steal a job from another active queue... */
+ while (1) {
+ vix++;
+ if (vix >= active_rqs)
+ vix = 0;
+ if (vix == rq->ix)
+ break;
+
+ res = check_possible_steal_victim(rq, vix, result_proc, &contended_runqueues);
+ if (res) {
+ DESTROY_WSTACK(contended_runqueues);
+ return res;
+ }
+ }
+
+ /* ... and finally re-try stealing from the queues that were skipped because contended.
+ We recheck the number of empty runqueues in each iteration, as taking the runqueue lock in check_possible_steal_victim can take quite a while. */
+ while (!WSTACK_ISEMPTY(contended_runqueues)
+ && (erts_atomic32_read_acqb(&no_empty_run_queues) < blnc_rqs)) {
+ vix = WSTACK_POP(contended_runqueues);
+ res = check_possible_steal_victim(rq, vix, result_proc, NULL);
+ if (res) {
+ DESTROY_WSTACK(contended_runqueues);
+ return res;
}
}
--
2.51.0