4 ms·
Excellent overview of scheduling with code examples and animations. One minor correction about Erlang is that that scheduler doesn't get invoked just on functi
by rdtsc 3y ago
Excellent overview of scheduling with code examples and animations.
One minor correction about Erlang is that that scheduler doesn't get invoked just on function calls. It will be invoked as soon as the lightweight process consumes a certain number of allowed operations. So even if you have one huge function that never calls others, just computes something, it will still consume operations and will be preempted. Some internal C utility functions also consume some virtual number of ops as well and may yield.
- tontinton 3y agoYeah I might have been wrong, simply went with https://blog.stenmans.org/theBeamBook/#_reductions https://blog.stenmans.org/theBeamBook/#_reductions Now that I reread that section, it also depends on whether you call a BIF or not, I'll think about how to phrase that better in the blog post. Thanks!
- rdtsc 3y agoEven some BIFs and NIFs (BIF are mostly just an built-in NIFs) can yield [1] and choose to reschedule themselves later. But inside the C code it is voluntary, of course. An example of this can be seen when running hash functions [2]. Another interesting idea is that a NIF which may not reschedule themselves with yield, can still signal the scheduler that they are consuming so many "reductions" if they did some lengthy work. So if, say, they run for 10 msec, they might want to bump the reduction counter a bit: enif_consume_timeslice. Otherwise a tight recursive loop calling the same NIF could still effectively block the VM. [1] https://www.erlang.org/doc/man/erl_nif#enif_schedule_nif https://www.erlang.org/doc/man/erl_nif#enif_schedule_nif [2] https://github.com/erlang/otp/blob/ab7b354c37dac92704faac4551939662d7ff5a73/erts/lib_src/yielding_c_fun/test/examples/sha256_erlang_nif/c_src/sha256_nif.c#L87 https://github.com/erlang/otp/blob/ab7b354c37dac92704faac455... [3] https://www.erlang.org/doc/man/erl_nif#enif_consume_timeslice https://www.erlang.org/doc/man/erl_nif#enif_consume_timeslic...