Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does spawning new processes use all CPU cores in elixir

Tags:

elixir

Suppose, I'm on a 4-core CPU machine.

If I run the following in my elixir VM:

 1..4 |> Enum.map fn(x) -> spawn(computationally_heavy_process) end

Does this use all 4 cores of my machine. One of each of the computationally heavy processes?

like image 813
User314159 Avatar asked May 26 '15 02:05

User314159


2 Answers

Yes. By default, Erlang starts one scheduler (OS thread) for each CPU, and attempts to distribute load evenly across the schedulers. However, it is not guaranteed that the four processes end up being processed in four different CPUs, because there are usually much more things happening in parallel. If you want to know how many schedulers Erlang has started, just fire up iex (or erl, for that matter).

~$ iex
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
...

For example, my computer has a CPU with 8 cores. In the first line of the output, you can see the current configuration. From the part [smp:8:8], you can see that SMP is configured with 8 schedulers, 8 of which are online. The number of schedulers/threads can be overridden with the +S flag.

+S Schedulers:SchedulerOnline

Sets the number of scheduler threads to create and scheduler threads to set online when SMP support has been enabled. The maximum for both values is 1024. If the Erlang runtime system is able to determine the amount of logical processors configured and logical processors available, Schedulers will default to logical processors configured, and SchedulersOnline will default to logical processors available; otherwise, the default values will be 1. [...]

The number of schedulers online can also be changed at runtime via :erlang.system_flag(:schedulers_online, n). However, I would advise against changing any of the defaults unless you run into a specific problem.

like image 132
Patrick Oscity Avatar answered Oct 02 '22 17:10

Patrick Oscity


Well the elixir VM is simply the Erlang VM - which schedules how spawned processes get assigned to machine CPU. You don't have direct control AFAIK. However you can see how the 4 schedulers are being occupied within the VM by using the visualisation of the Charts by issuing :observer.start() before you run your own code. I've found it seems to do a pretty reasonable job. For a reality check you can also run top on a linux OS. This can show how much of each cpu is being actually used - try pressing 1 in top from a different console on the same machine.

like image 33
GavinBrelstaff Avatar answered Oct 02 '22 15:10

GavinBrelstaff