File 1337-erts-Improve-erts_sched_local_random-to-work-without.patch of Package erlang

From 184634a23120fde29029bc7a765c45838f19c510 Mon Sep 17 00:00:00 2001
From: Sverker Eriksson <sverker@erlang.org>
Date: Thu, 9 Jun 2022 23:09:19 +0200
Subject: [PATCH 1/2] erts: Improve erts_sched_local_random to work without
 process

Seen to cause problem (GH-5981) when crash dump is iterating over
ETS ordered_set with write_concurrency (catree) and the
thread doing it does not have a 'current_process'.

Change erts_sched_local_random to instead use a single mutated rand_state.
---
 erts/emulator/beam/erl_process.c |  1 +
 erts/emulator/beam/erl_process.h | 13 +++++--------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/erts/emulator/beam/erl_process.c b/erts/emulator/beam/erl_process.c
index 65a6e1c6ad..96596355ff 100644
--- a/erts/emulator/beam/erl_process.c
+++ b/erts/emulator/beam/erl_process.c
@@ -5873,6 +5873,7 @@ init_scheduler_data(ErtsSchedulerData* esdp, int num,
     }
 
     esdp->reductions = 0;
+    esdp->rand_state = (Uint64)(UWord)esdp + ((Uint64)(UWord)esdp << 32);
 
     init_sched_wall_time(esdp, time_stamp);
     erts_port_task_handle_init(&esdp->nosuspend_port_task_handle);
diff --git a/erts/emulator/beam/erl_process.h b/erts/emulator/beam/erl_process.h
index 9e5ea73869..0dfd878481 100644
--- a/erts/emulator/beam/erl_process.h
+++ b/erts/emulator/beam/erl_process.h
@@ -673,6 +673,7 @@ struct ErtsSchedulerData_ {
     } pending_signal;
 
     Uint64 reductions;
+    Uint64 rand_state;
     ErtsSchedWallTime sched_wall_time;
     ErtsGCInfo gc_info;
     ErtsPortTaskHandle nosuspend_port_task_handle;
@@ -2798,19 +2799,15 @@ Uint32 erts_sched_local_random_hash_64_to_32_shift(Uint64 key)
 
 /*
  * This function attempts to return a random number based on the state
- * of the scheduler, the current process and the additional_seed
- * parameter.
+ * of the scheduler and the additional_seed parameter.
  */
 ERTS_GLB_INLINE
 Uint32 erts_sched_local_random(Uint additional_seed)
 {
     ErtsSchedulerData *esdp = erts_get_scheduler_data();
-    Uint64 seed =
-        additional_seed +
-        esdp->reductions +
-        esdp->current_process->fcalls +
-        (((Uint64)esdp->no) << 32);
-    return erts_sched_local_random_hash_64_to_32_shift(seed);
+    esdp->rand_state++;
+    return erts_sched_local_random_hash_64_to_32_shift(esdp->rand_state
+                                                       + additional_seed);
 }
 
 #endif /* #if ERTS_GLB_INLINE_INCL_FUNC_DEF */
-- 
2.35.3

openSUSE Build Service is sponsored by