Erlang is a well-known programming language that is famous (among other things) for it's lightweight threading. Erlang is usually implemented with the BEAM machine. The description (H'97) of the Erlang BEAM machine says
To guarantee a fair scheduling a process is suspended after a fixed number of reductions and then the first process from the queue is resumed.
I'm interested in this notion of reduction. According to (H'97) only the following BEAM commands count as a reduction:
All of those involve a function call. In contrast, calls to C-functions (e.g. TrC/TrCO) and calls to built-in functions (e.g. called by Bif_0_) don't count as reductions.
Questions. After this preamble, here is what I would like to know.
(H'97) B. Hausman, The Erlang BEAM Virtual Machine Specification.
If a process exceeds its execution time slot, the Erlang VM pauses the process, puts it back on the queue and moves on to the next item on the list — the entire mechanism is called “preemption”. This way, BEAM executes Erlang processes concurrently by quickly switching back and forth between processes.
The BEAM uses one OS thread per core. It runs a scheduler on each of these threads. Each scheduler pulls processes to run from its very own run queue. The BEAM is also responsible for populating these run queues with Erlang processes to execute.
Unlike the JVM, which maps its threads to OS threads and lets the operating system schedule them, the BEAM comes with its own scheduler. The scheduler starts, by default, an OS thread for every core and optimises the workload between them.
BEAM is the virtual machine at the core of the Erlang Open Telecom Platform (OTP). BEAM is part of the Erlang Run-Time System (ERTS), which compiles Erlang source code into bytecode, which is then executed on the BEAM. BEAM bytecode files have the . beam file extension.
I'll try to answer you questions.
1) The main reason for not using time slices is performance and portability. It is quite expensive to read a monotonic time value from the operating system, and if we have to do it for every function call, the overhead becomes quite large. The cost also varies quite a lot on different OS's. The reduction counting mechanic however only requires the machine to be good at decrementing integers, which most machines are.
2) They don't. That list, as you say, is very outdated. Much of the way the VM works has been rewritten since. As a general rule of thumb; a function call (not the return) or anything that may take an unknown amount of time counts reductions. This includes bifs, nifs, gc, sending/receiving messages and probably more that I cannot think of right now.
3) Scheduling and pre-emption are very different things. You may want to see my webinar I did a couple a years ago about how scheduling is done: https://www.youtube.com/watch?v=tBAM_N9qPno
I only know the answer to the first question:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With