[erlang-questions] [Q] Why is Erlang VM better than traditional OS like Linux?

Björn Gustavsson bjorn@REDACTED
Fri Aug 8 12:34:21 CEST 2014

On Fri, Aug 8, 2014 at 1:58 AM,  <ok@REDACTED> wrote:

> Let's start with the very low level.  A modern x86_64 has
> 16 64-bit general registers
> 16 256-bit vector registers
>  8 80-bit floating-point registers
>  1 32-bit flags register
>  1 64-bit program counter
> other assorted things like performance counters.
> Switching between *threads* in the same UNIX process
> could therefore involve storing 732 bytes of registers
> from the old thread and load 732 byte of registers
> from the new thread.
> Something like the Erlang VM can arrange to switch only
> at points where all of the floating point registers and
> the flags are known to be dead, so a context switch
> between Erlang processes only needs to save and restore
> about 18% of the information that a Linux *thread* switch
> does.  (Oh wait, I forgot that threads in Linux use the
> FS segment register for thread-locals.  The 732 byte
> figure is *definitely* low.)

Here are some details about how BEAM does context

The state for an Erlang process is kept in a struct
called Process. In principle, all you need to do
when switching processes is to change the pointer
to the current Process struct. In practice, however,
the BEAM virtual machine keeps some of the state
in local variables/CPU registers (for efficiency), so
it will have to save that part to the Process struct.
The following pieces of data need to be saved:

  the instruction pointer (I)
  the stack pointer (E)
  the heap top (HTOP)
  the reduction counter
  all X registers currently live and the number
  of live X registers

When doing a context switch in a receive,
there are no live X registers; when doing a
context switch when calling a function, the
number of live X registers is the number of
arguments for the function being called.


Björn Gustavsson, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list