Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How, if at all, do Erlang Processes map to Kernel Threads?

Erlang is known for being able to support MANY lightweight processes; it can do this because these are not processes in the traditional sense, or even threads like in P-threads, but threads entirely in user space.

This is well and good (fantastic actually). But how then are Erlang threads executed in parallel in a multicore/multiprocessor environment? Surely they have to somehow be mapped to kernel threads in order to be executed on separate cores?

Assuming that that's the case, how is this done? Are many lightweight processes mapped to a single kernel thread?

Or is there another way around this problem?

like image 858
Rob Lachlan Avatar asked Mar 03 '09 05:03

Rob Lachlan


People also ask

Is Erlang multithreaded?

In Erlang, each thread of execution is called a process. (Aside: the term "process" is usually used when the threads of execution share no data with each other and the term "thread" when they share data in some way. Threads of execution in Erlang share no data, that is why they are called processes).

How many threads does the kernel use?

These are the three kinds of threads. The kernel maintains thread- and process-related information in two types of structures. A process is always created with one thread, called the initial thread. The initial thread provides compatibility with previous single-threaded processes.

What do kernel threads do?

A kernel thread is a way to implement background tasks inside the kernel. The task can be busy handling asynchronous events or sleep-waiting for an event to occur. Kernel threads are similar to user processes, except that they live in kernel space and have access to kernel functions and data structures.


2 Answers

Answer depends on the VM which is used:

1) non-SMP: There is one scheduler (OS thread), which executes all Erlang processes, taken from the pool of runnable processes (i.e. those who are not blocked by e.g. receive)

2) SMP: There are K schedulers (OS threads, K is usually a number of CPU cores), which executes Erlang processes from the shared process queue. It is a simple FIFO queue (with locks to allow simultaneous access from multiple OS threads).

3) SMP in R13B and newer: There will be K schedulers (as before) which executes Erlang processes from multiple process queues. Each scheduler has it's own queue, so process migration logic from one scheduler to another will be added. This solution will improve performance by avoiding excessive locking in shared process queue.

For more information see this document prepared by Kenneth Lundin, Ericsson AB, for Erlang User Conference, Stockholm, November 13, 2008.

like image 118
gleber Avatar answered Sep 23 '22 16:09

gleber


I want to ammend previous answers.

Erlang, or rather the Erlang runtime system (erts), defaults the number of schedulers (OS threads) and the number of runqueues to number of processing elements on your platform. That is processors cores or hardware threads. You can change these settings in runtime using:

erlang:system_flag(schedulers_online, NP) -> PrevNP 

The Erlang processes does not have any affinity to any schedulers yet. The logic balancing the processes between the schedulers follows two rules. 1) A starving scheduler will steal work from another scheduler. 2) Migration paths are setup to push processes from schedulers with lots of processes to schedulers with less work. This is done to assure fairness in reduction count (execution time) for each process.

Schedulers however can be locked to specific processing elements. This not done by default. To let erts do the scheduler->core affinity use:

erlang:system_flag(scheduler_bind_type, default_bind) -> PrevBind 

Several other bind types can be found in the documentation. Using affinity can greatly improve performance in heavy load situations! Especially in high lock contention situations. Also, the linux kernel cannot handle hyperthreads to say the least. If you have hyperthreads on your platform you should really use this feature in erlang.

like image 33
psyeugenic Avatar answered Sep 19 '22 16:09

psyeugenic