There will be a more detailed description of how multi core works and
on the future plans available
in a couple of weeks. I plan to include some of this in my
presentation at the ICFP2008, Erlang Workshop in Victoria BC,
September 27
The Erlang VM without SMP support has 1 scheduler which runs in the
main process thread. The scheduler
picks runnable Erlang processes and IO-jobs from the run-queue and
there is no need to lock data structures since
there is only one thread accessing them.
The Erlang VM with SMP support can have 1 to many schedulers which are
run in 1 thread each. The schedulers pick runnable Erlang processes
and IO-jobs from one common run-queue. In the SMP VM all shared data
structures are
protected with locks, the run-queue is one example of a data structure
protected with locks.
>From OTP R12B the SMP version of the VM is automatically started as
default if the OS reports more than 1 CPU (or Core) and with the same
number of schedulers as CPU's or Cores.
You can see what was chosen at the first line of printout from the
"erl" command. E.g.
Erlang (BEAM) emulator version 5.6.4 [source] [smp:4] [asynch-threads:0] .....
The "[smp:4]" above tells that the SMP VM is run and with 4 schedulers.
The default behaviour can be overridden with the
"-smp [enable|disable|auto]" auto is default
and to set the number of schedulers, if smp is set to enable or auto
"+S Number" where Number is the number of schedulers (1..1024)
Note ! that it is normally nothing to gain from running with more
schedulers than the number of CPU's or Cores.
Note2 ! On some operating systems the number of CPU's or Cores to be
used by a process can be restricted
with commands. For example on Linux the command "taskset" can be used
for this. The Erlang VM will
currently only detect number of available CPU's or Cores and will not
take the mask set by "taskset" into account.
Because of this it can happen and has happened that e.g. only 2 Cores
are used even if the Erlang VM
runs with 4 schedulers. It is the OS that limits this because it take
the mask from "taskset" into account.
The schedulers in the Erlang VM are run on one OS-thread each and it
is the OS that decides if the threads are
executed on different Cores. Normally the OS will do this just fine
and will also keep the thread on the same Core throughout the
execution.
The Erlang processes will be run by different schedulers because they
are picked from a common run-queue by
the first scheduler that becomes available.
Performance and scalability
------------------------------------
- The SMP VM with only one scheduler is slightly slower than the non
SMP VM. The SMP VM need to to use all the locks inside but as long as
there are no lock-conflicts the overhead caused by locking is not
significant (it is the lock conflicts that takes time). This explains
why it in some cases can be more efficient to run several SMP VM's
with one scheduler each
instead on one SMP VM with several schedulers. Of course the running
of several VM's require that the application can run
in many parallel tasks which has no or very little communication with
each other.
- If a program scale well with the SMP VM over many cores depends very
much on the characteristics of the program, some programs scale
linearly up to 8 and even 16 cores while other programs barely scale
at all even on 2 cores.
This might sound bad, but in practice many real programs scale well on
the number of cores that are common on the
market today, see below.
- Real telecoms products supporting a massive number if simultaneously
ongoing "calls" represented as one or several
Erlang processes per core have shown very good scalability on dual and
quad core processors. Note, that these products
was written in the normal Erlang style long before the SMP VM and
multi core processors where available and they
could benefit from the Erlang SMP VM without changes and even without
need to recompile the code.
SMP performance is continually improved
------------------------------------------------------
The SMP implementation is continually improved in order to get better
performance and scalability. In each service release
R12B-1, 2, 3, 4, 5 , ..., R13B etc. you will find new optimizations.
Some known bottlenecks
---------------------------------
- The single common run-queue will become a dominant bottleneck when
the number of CPU's or Cores increase.
Will be visible from 4 cores and upwards, but 4 cores will probably
still give ok performance for many applications.
We are working on a solution with one run-queue per scheduler as the
most important improvement right now.
- Ets tables involves locking. Before R12B-4 there was 2 locks
involved in every access to an ets-table, but
in R12B-4 the locking of the meta-table is optimized to reduce the
conflicts significantly (as mentioned earlier it is the conflicts that
are expensive).
If many Erlang processes access the same table there will be a lot of
lock conflicts causing bad performance especially if these processes
spend a majority of their work accessing ets-tables.
The locking is on table-level not on record level.
Note! that this will have impact on Mnesia as well since Mnesia is a
heavy user of ets-tables.
...
Our strategy with SMP
-----------------------------
Already from the beginning when we started implementation of the SMP
VM we decided on the strategy:
"First make it work, then measure, then optimize".
We are still following this strategy consistently since the first
stable working SMP VM that we released in May 2006 (R11B).
There are more known things to improve and we address them one by one
taking the one we think gives most
performance per implementation effort first and so on.
We are putting most focus on getting consistent better scaling on many
cores (more than 4).
Best in class
-----------------
Even if there are a number of known bottlenecks
the SMP system already has good overall performance and scalability
and I believe we are best in class
when it comes to letting the programmer utilize multi -core machines
in an easy productive way.
/Kenneth Erlang/OTP team, Ericsson
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions
Thanks, Kenneth!
/Vlad
On Tue, Sep 16, 2008 at 11:55 AM, Vlad Dumitrescu <vlad...@gmail.com> wrote:
> On Tue, Sep 16, 2008 at 11:34, Kenneth Lundin <kenneth...@gmail.com> wrote:
>> Here are some short facts about how the Erlang SMP implementation
>> works and how it
>> relates to performance and scalability.
>
> Hi and thank you for the detailed explanations!
>
> This may be a silly question, but does SMP interact with the async thread pool?
>
> best regards,
> Vlad
Hi,
The async thread pool works exactly the same in both the SMP VM and
the non SMP VM i.e I suppose
you can say that they don't interact.
The asynch thread pool is only used by the file driver in the code we
deliver but there is a documented
interface where you as a user can implement your own driver which also
used the asynch thread pool.
/Kenneth
> Here are some short facts about how the Erlang SMP implementation
> works and how it
> relates to performance and scalability.
...
Kenneth,
Do you have any insight into why I might be seeing much higher CPU
cost for SMP on linux vs. Mac? Is there some difference in the lock
implementations that might be relevant?
-kevin
+1000. Erlang (and the SMP VM in particular) really shines when it
comes to building system that naturally scale out. It makes me so
happy to see apps distribute reasonably evenly across 8 cores, without
me having to spend a lot of time focused on locking/sharing/etc. It
Just Works (TM).
Thanks, Kenneth and the OTP team. Your hard work is much appreciated. :)
D.
In general I don't think the measured CPU cost on different operating
systems is a reliable way of measuring how well a program performs. I
think is is more reliable to create measurements where the wall clock
is used , e.g how long time does a certain operation take or how many
calls per second can a system handle etc.
Anyway the lock mechanisms used in the VM are:
pthread mutex, for the run-queue
pthread rwlock, for ets-tables
inline spinlock and inline atomics
these are part of the VM source code, for x86 32 and 64 bit, ppc 32
bit, sparc 32 and 64 bit. For other architectures the build will fall
back to pthread spinlock and if that
does not exist there is another fall back to pthread mutex.
Linux and Mac OSx for x86 will use exactly the same spinlock and atomics but the
pthread mutex and rwlock will be OS specific as well as the thread
scheduling in general and this can of course give differences in
performance even on the same HW.
/Kenneth Erlang/OTP, Ericsson
I'm having some difficulties with the "it's only when locks conflict
that locking time is significant" idea. My understanding with the
pthreads locks is that you start with a CAS, and if that fails then you
spin for a while (with CAS), and if you spin for too long then you go a
kernel lock. If that's wrong then I'm sorry, as most of the following
will also be wrong!
Now assuming the above is correct (or at least close enough for jazz),
in the conflict case, sure, you're going to block, which is the point,
and then some time in the future either the kernel will wake you up
(pretty slow), or the spinning finally succeeds in which case the
wakeup should be really fast.
If there's not a conflict then the first CAS will succeed. But a CAS is
many hundreds of cycles as you've got to do cache-coherency and then
really go all the way out to RAM and load the values in fresh,
especially in a multisocket box. So hundreds of cycles does not seem
like "not significant", at least to me. Or have I missed something
obvious here?
I rather expect that you'll end up with lock-free per-cpu task-queues
with workstealing algorithms which as a colleague has just pointed out,
has been used for these purposes for some time (at least 10 years);
though I do appreciate the "get it out working and then tune it"
approach.
Matthew
I'm having some difficulties with the "it's only when locks conflict
that locking time is significant" idea. My understanding with the
pthreads locks is that you start with a CAS, and if that fails then you
spin for a while (with CAS), and if you spin for too long then you go a
kernel lock. If that's wrong then I'm sorry, as most of the following
will also be wrong!
Now assuming the above is correct (or at least close enough for jazz),
in the conflict case, sure, you're going to block, which is the point,
and then some time in the future either the kernel will wake you up
(pretty slow), or the spinning finally succeeds in which case the
wakeup should be really fast.
If there's not a conflict then the first CAS will succeed. But a CAS is
many hundreds of cycles as you've got to do cache-coherency and then
really go all the way out to RAM and load the values in fresh,
especially in a multisocket box. So hundreds of cycles does not seem
like "not significant", at least to me. Or have I missed something
obvious here?