Martin Berger
Martin Berger

Reputation: 1128

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:

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.

Upvotes: 6

Views: 3066

Answers (2)

Lukas
Lukas

Reputation: 5327

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

Upvotes: 6

I GIVE TERRIBLE ADVICE
I GIVE TERRIBLE ADVICE

Reputation: 9648

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

Upvotes: 4

Related Questions