[erlang-questions] CPU affinity and enif_thread_create

Rickard Green rickard@REDACTED
Tue Aug 23 11:51:10 CEST 2011

On Aug 22, 2011, at 9:19 PM, Max Lapshin wrote:

> On Mon, Aug 22, 2011 at 8:08 PM, Rickard Green <rickard@REDACTED> wrote:
>> enif_thread_create() and erl_drv_thread_create() will set the affinity of the newly created thread to the same as the affinity of the main thread (or you have found a bug). Binding of scheduler threads does not effect the affinity of the main thread. However, if you create the thread any other way (e.g. by calling pthread_create() directly), the newly created thread will inherit the affinity of the scheduler thread it happens to execute on (and it may also inherit other unwanted stuff).
> I use R14B03 and I threads are started via pthread_create (inside of libx264).

In this case the new threads will inherit the affinity of the scheduler thread that calls this functionality.

It is always preferred to create all threads using the functionality provided by ERTS. This might, however, not always be possible.

You may fix affinity and other stuff you know about (by inspecting the emulator code) manually. This is, however, no good solution since this other stuff have and most likely will vary between releases also in the future.

A better solution would be to create a new thread using enif_thread_create(), and call the functionality that creates new threads from this thread. By this, the newly created threads wont inherit scheduler specific stuff, but instead inherit settings that are appropriate for other threads in the system.

> But after this happens very strange situation: htop shows that started
> threads doesn't have any affinity.

How do you get htop to show affinity (I just installed it for the first time)? I only found the PROCESSOR column, but this doesn' t show affinity. I think it shows current processor assignment.

> If I set affinity, threads are spread across cores. Looks like erlang
> has nothing to do with it.

Rickard Green, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list