Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
ced6470 to
f2718d3
Compare
This comment has been minimized.
This comment has been minimized.
994b49f to
c1ea3dc
Compare
c1ea3dc to
74872d3
Compare
The esp32 receive_events loop, after running a listener -- which may ready a process by delivering it a message -- re-blocked in xQueueSelectFromSet with the original timeout instead of returning to the scheduler loop. It relied on the sys_signal sentinel from scheduler_make_ready to break out; that wake is lost when the event queue is full (xQueueSendToBack with no timeout silently drops it), and never sent at all on non-SMP builds with the task driver disabled, parking the readied process until the next unrelated event or timer tick. Drain pending events without re-blocking, then return. This is also a prerequisite for avoiding the redundant scheduler wake on process handoff (atomvm#2329): once scheduler_make_ready no longer wakes the poller for a process the readying scheduler can run itself, returning here is the only way that process gets dispatched. Observed there as test_socket's active {tcp, Socket, Data} delivery never waking the receiving process, whose receive then hit its 'after 10000'. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
The esp32 receive_events loop, after running a listener -- which may ready a process by delivering it a message -- re-blocked in xQueueSelectFromSet with the original timeout instead of returning to the scheduler loop. It relied on the sys_signal sentinel from scheduler_make_ready to break out; that wake is lost when the event queue is full (xQueueSendToBack with no timeout silently drops it), and never sent at all on non-SMP builds with the task driver disabled, parking the readied process until the next unrelated event or timer tick. Drain pending events without re-blocking, then return. This is also a prerequisite for avoiding the redundant scheduler wake on process handoff (atomvm#2329): once scheduler_make_ready no longer wakes the poller for a process the readying scheduler can run itself, returning here is the only way that process gets dispatched. Observed there as test_socket's active {tcp, Socket, Data} delivery never waking the receiving process, whose receive then hit its 'after 10000'. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
The esp32 receive_events loop, after running a listener -- which may ready a process by delivering it a message -- re-blocked in xQueueSelectFromSet with the original timeout instead of returning to the scheduler loop. It relied on the sys_signal sentinel from scheduler_make_ready to break out; that wake is lost when the event queue is full (xQueueSendToBack with no timeout silently drops it), and never sent at all on non-SMP builds with the task driver disabled, parking the readied process until the next unrelated event or timer tick. Drain pending events without re-blocking, then return. This is also a prerequisite for avoiding the redundant scheduler wake on process handoff (atomvm#2329): once scheduler_make_ready no longer wakes the poller for a process the readying scheduler can run itself, returning here is the only way that process gets dispatched. Observed there as test_socket's active {tcp, Socket, Data} delivery never waking the receiving process, whose receive then hit its 'after 10000'. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
The esp32 receive_events loop, after running a listener -- which may ready a process by delivering it a message -- re-blocked in xQueueSelectFromSet with the original timeout instead of returning to the scheduler loop. It relied on the sys_signal sentinel from scheduler_make_ready to break out; that wake is lost when the event queue is full (xQueueSendToBack with no timeout silently drops it), and never sent at all on non-SMP builds with the task driver disabled, parking the readied process until the next unrelated event or timer tick. Drain pending events without re-blocking, then return. This is also a prerequisite for avoiding the redundant scheduler wake on process handoff (atomvm#2329): once scheduler_make_ready no longer wakes the poller for a process the readying scheduler can run itself, returning here is the only way that process gets dispatched. Observed there as test_socket's active {tcp, Socket, Data} delivery never waking the receiving process, whose receive then hit its 'after 10000'. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
The esp32 receive_events loop, after running a listener -- which may ready a process by delivering it a message -- re-blocked in xQueueSelectFromSet with the original timeout instead of returning to the scheduler loop. It relied on the sys_signal sentinel from scheduler_make_ready to break out; that wake is lost when the event queue is full (xQueueSendToBack with no timeout silently drops it), and never sent at all on non-SMP builds with the task driver disabled, parking the readied process until the next unrelated event or timer tick. Drain pending events without re-blocking, then return. This is also a prerequisite for avoiding the redundant scheduler wake on process handoff (atomvm#2329): once scheduler_make_ready no longer wakes the poller for a process the readying scheduler can run itself, returning here is the only way that process gets dispatched. Observed there as test_socket's active {tcp, Socket, Data} delivery never waking the receiving process, whose receive then hit its 'after 10000'. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
The esp32 receive_events loop, after running a listener -- which may ready a process by delivering it a message -- re-blocked in xQueueSelectFromSet with the original timeout instead of returning to the scheduler loop. It relied on the sys_signal sentinel from scheduler_make_ready to break out; that wake is lost when the event queue is full (xQueueSendToBack with no timeout silently drops it), and never sent at all on non-SMP builds with the task driver disabled, parking the readied process until the next unrelated event or timer tick. Drain pending events without re-blocking, then return. This is also a prerequisite for avoiding the redundant scheduler wake on process handoff (atomvm#2329): once scheduler_make_ready no longer wakes the poller for a process the readying scheduler can run itself, returning here is the only way that process gets dispatched. Observed there as test_socket's active {tcp, Socket, Data} delivery never waking the receiving process, whose receive then hit its 'after 10000'. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
The esp32 receive_events loop, after running a listener -- which may ready a process by delivering it a message -- re-blocked in xQueueSelectFromSet with the original timeout instead of returning to the scheduler loop. It relied on the sys_signal sentinel from scheduler_make_ready to break out; that wake is lost when the event queue is full (xQueueSendToBack with no timeout silently drops it), and never sent at all on non-SMP builds with the task driver disabled, parking the readied process until the next unrelated event or timer tick. Drain pending events without re-blocking, then return. This is also a prerequisite for avoiding the redundant scheduler wake on process handoff (atomvm#2329): once scheduler_make_ready no longer wakes the poller for a process the readying scheduler can run itself, returning here is the only way that process gets dispatched. Observed there as test_socket's active {tcp, Socket, Data} delivery never waking the receiving process, whose receive then hit its 'after 10000'. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
The esp32 receive_events loop, after running a listener -- which may ready a process by delivering it a message -- re-blocked in xQueueSelectFromSet with the original timeout instead of returning to the scheduler loop. It relied on the sys_signal sentinel from scheduler_make_ready to break out; that wake is lost when the event queue is full (xQueueSendToBack with no timeout silently drops it), and never sent at all on non-SMP builds with the task driver disabled, parking the readied process until the next unrelated event or timer tick. Drain pending events without re-blocking, then return. This is also a prerequisite for avoiding the redundant scheduler wake on process handoff (atomvm#2329): once scheduler_make_ready no longer wakes the poller for a process the readying scheduler can run itself, returning here is the only way that process gets dispatched. Observed there as test_socket's active {tcp, Socket, Data} delivery never waking the receiving process, whose receive then hit its 'after 10000'. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
74872d3 to
780c619
Compare
This comment was marked as outdated.
This comment was marked as outdated.
Significantly improve performance of ping-pong patterns by avoiding cross-thread round trips. scheduler_make_ready now only wakes another scheduler when a runnable backlog already exists, as the readying thread is a scheduler that picks the process up itself. The browser main thread is not, so emscripten's synchronous run_script answers the caller with the _from_task variant. On ESP32, sys_signal gives a binary semaphore in the event queue set, so test VMs also drain it before starting. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
780c619 to
394f114
Compare
|
amp: PR #2329 review — SMP scheduler handoffRecommendation: request changes. Fix the post-publication context read and keep deferred task delivery single-consumer. Restore two-process parallelism without reintroducing per-message wakes for a single runnable handoff. Scope: the last commit, FindingsP1 — A spawned process can be freed before the publishing scheduler reads its flags
Snapshot the flag while publishing the context and use only that snapshot after the unlock: --- a/src/libAtomVM/scheduler.c
+++ b/src/libAtomVM/scheduler.c
@@ -411,8 +411,9 @@
// scheduler when there is already a *runnable* backlog to parallelize.
// ctx is not in ready_processes yet (it was just removed), and entries
// flagged Running are signaled contexts no scheduler can dispatch, so
// neither inflates the count.
bool ready_backlog = scheduler_has_runnable_ready(global);
+ bool was_running = context_get_flags(ctx, Running);
#endif
// Move to ready queue (from waiting or running)
// The process may be running (it would be signaled), so mark it
@@ -427,7 +428,7 @@
if (!global->waiting_scheduler
&& global->running_schedulers > 0
&& global->running_schedulers < global->online_schedulers
- && !context_get_flags(ctx, Running)) {
+ && !was_running) {
global->running_schedulers++;
smp_scheduler_start(global);
}Regression: In an ASan scheduler test with controlled barriers, pause A after unlocking and resume it only after B has destroyed the child and left the poller role. Keep an online slot free so the old code actually evaluates the dangling predicate. Uncoordinated spawn stress can supplement this, but a passing stress run would not rule out the race. P2 — Two schedulers can deliver deferred task messages out of orderThe non-poller fast path now leaves the condition-variable loop with work. It bypasses event polling but still reaches Keep the task-driver drain under the same single-owner guard as --- a/src/libAtomVM/scheduler.c
+++ b/src/libAtomVM/scheduler.c
@@ -296,14 +296,14 @@
if (result == NULL && !global->scheduler_stop_all) {
// The poller may block waiting for events.
sys_poll_events(global, wait_timeout);
} else {
sys_poll_events(global, SYS_POLL_EVENTS_DO_NOT_WAIT);
}
+#ifdef AVM_TASK_DRIVER_ENABLED
+ globalcontext_process_task_driver_queues(global);
+#endif
#ifndef AVM_NO_SMP
}
#endif
-#ifdef AVM_TASK_DRIVER_ENABLED
- globalcontext_process_task_driver_queues(global);
-#endif
SMP_MUTEX_LOCK(global->schedulers_mutex);
} while (result == NULL);Regression: The current P2 — Two CPU-bound processes can alternate on one scheduler indefinitelyWhen an active process spawns its only child, One small fix is to reuse the existing wake/start policy from --- a/src/libAtomVM/scheduler.c
+++ b/src/libAtomVM/scheduler.c
@@ -382,16 +382,42 @@
return result;
}
+#ifndef AVM_NO_SMP
+static void scheduler_wake_for_backlog(GlobalContext *global, bool can_start)
+{
+ if (SMP_MUTEX_TRYLOCK(global->schedulers_mutex)) {
+ if (global->waiting_scheduler) {
+ sys_signal(global);
+ } else if (can_start && global->running_schedulers > 0
+ && global->running_schedulers < global->online_schedulers) {
+ global->running_schedulers++;
+ smp_scheduler_start(global);
+ }
+ SMP_MUTEX_UNLOCK(global->schedulers_mutex);
+ } else {
+ sys_signal(global);
+ }
+}
+#endif
+
Context *scheduler_next(GlobalContext *global, Context *c)
{
// Remove c from running and append it at the end of ready list
// c could already be in ready queue, if it received a message.
SMP_SPINLOCK_LOCK(&global->processes_spinlock);
list_remove(&c->processes_list_head);
+#ifndef AVM_NO_SMP
+ bool ready_backlog = scheduler_has_runnable_ready(global);
+#endif
list_append(&global->ready_processes, &c->processes_list_head);
context_update_flags(c, ~Running, NoFlags);
SMP_SPINLOCK_UNLOCK(&global->processes_spinlock);
+#ifndef AVM_NO_SMP
+ if (ready_backlog) {
+ scheduler_wake_for_backlog(global, true);
+ }
+#endif
// Schedule.
return scheduler_run(global);
}
@@ -422,23 +448,8 @@
list_append(&global->ready_processes, &ctx->processes_list_head);
SMP_SPINLOCK_UNLOCK(&global->processes_spinlock);
#ifndef AVM_NO_SMP
if (ready_backlog) {
- if (SMP_MUTEX_TRYLOCK(global->schedulers_mutex)) {
- // Start a new scheduler if none are going to take this process.
- if (!global->waiting_scheduler
- && global->running_schedulers > 0
- && global->running_schedulers < global->online_schedulers
- && !was_running) {
- global->running_schedulers++;
- smp_scheduler_start(global);
- }
- if (global->waiting_scheduler) {
- sys_signal(global);
- }
- SMP_MUTEX_UNLOCK(global->schedulers_mutex);
- } else {
- sys_signal(global);
- }
+ scheduler_wake_for_backlog(global, !was_running);
}
#elif defined(AVM_TASK_DRIVER_ENABLED)
sys_signal(global);Missing coverage: --- /dev/null
+++ b/tests/erlang_tests/scheduler_two_workers.erl
@@ -0,0 +1,22 @@
+-module(scheduler_two_workers).
+-export([start/0]).
+
+start() ->
+ 2 = min(2, erlang:system_info(schedulers)),
+ erlang:system_flag(schedulers_online, 2),
+ Parent = self(),
+ Deadline = erlang:monotonic_time(millisecond) + 3000,
+ spawn(fun() ->
+ burn(Deadline, 10000, 1),
+ Parent ! done
+ end),
+ burn(Deadline, 10000, 1),
+ receive done -> ok end.
+
+burn(Deadline, 0, Value) ->
+ case erlang:monotonic_time(millisecond) >= Deadline of
+ true -> Value;
+ false -> burn(Deadline, 10000, Value)
+ end;
+burn(Deadline, N, Value) ->
+ burn(Deadline, N - 1, (Value * 33 + N) rem 1048576).Verification
|
Continuation of:
If SMP is enabled, instead of waking the polling scheduler, grab the first ready process immediately if any.
Also simplify scheduler_make_ready a little bit to reduce the number of locks.
Since schedulers no longer poll events at every iteration, resources selected with enif_select and stopped with ERL_NIF_SELECT_STOP_SCHEDULED are now released asynchronously by the scheduler polling events, staying within the boundaries of the BEAM enif_select specification. Adjust test_file's test_gc accordingly.
These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later