Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reductions in the Erlang BEAM machine

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:

  • C/CO/ResC: calls local/resident Erlang function
  • CL: Discard the current stack frame. Call a local Erlang function.
  • CEx/TrCEx: Call an external Erlang function (traced or otherwise).
  • CExL/TrCExL: Discard the current stack frame and call an external Erlang fuction (traced or otherwise).
  • M_C_r: Load the argument register x(0). Call a resident Erlang function.
  • M_CL_r: Load the argument register x(0). Discard the current stack frame. Call a local Erlang function.

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.

  1. Why are reductions used for scheduling between threads, and not time-slices?
  2. Why do only the above commands advance the reduction counter?
  3. The description in (H'97) is a bit dated, how does contemporary Erlang handle scheduling?

(H'97) B. Hausman, The Erlang BEAM Virtual Machine Specification.

like image 548
Martin Berger Avatar asked Jul 31 '15 17:07

Martin Berger


People also ask

How does the Erlang VM work?

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.

How does BEAM VM work?

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.

What is the BEAM and how is it different from the JVM?

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.

What is BEAM language?

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.


2 Answers

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

like image 113
Lukas Avatar answered Nov 15 '22 06:11

Lukas


I only know the answer to the first question:

  1. Time slices are not necessarily accurate on all platforms and operating systems; using reductions ensures a uniform behaviour in all environments
like image 36
I GIVE TERRIBLE ADVICE Avatar answered Nov 15 '22 07:11

I GIVE TERRIBLE ADVICE