Does Mac OS X ignore CPU affinity by THREAD_AFFINITY_POLICY?

512 views
Skip to first unread message

Elazar Leibovich

unread,
Nov 19, 2014, 8:23:50 AM11/19/14
to mechanica...@googlegroups.com
I was trying to write a small library that portably (or, as portably as possible) pins a thread to a certain CPU.

It looks like, on recent Mac OS X it is only possible to some extent. You can ask the scheduler to put two threads in different affinity groups, which would make the scheduler try to schedule them on CPUs with different L2 caches.

In order to validate the code, I generated a trace of the context switches for this process with a simple dtrace script.

When looking at the graphs plotted by dtrace, I noticed two weird things:


1) The scheduling looks pretty dense. I expected that there would be many gaps where other things were scheduled on the CPU. I assume that interrupts happening during the process execution would still count as process time, but why didn't anything else got scheduled?
2) I tried it with the two threads in the same and in different affinity policy. In both cases, both threads were scheduled on spearate CPUs. The API usage doesn't seem to matter.


Is my tracing function correct? (dtrace probes of sched:::{on,off}-cpu) https://github.com/elazarl/cpu_affinity#readme

You can see a quick description of the code and idea at:


I'm sure something is wrong here, but I'm not where I erred.

Thanks,
Elazar

Jerry Shea

unread,
Nov 20, 2014, 3:57:19 AM11/20/14
to mechanica...@googlegroups.com
You could always try https://github.com/OpenHFT/Java-Thread-Affinity - this supports mac OS X.

You can see the .c code I wrote to support this here: https://github.com/peter-lawrey/Java-Thread-Affinity/commit/a9b8b461f1e92dadfd9298281bf299cb0b0c7874 - don't know if it will help you but it's there if you want to look!

Cheers

Elazar Leibovich

unread,
Nov 20, 2014, 5:37:40 AM11/20/14
to mechanica...@googlegroups.com
Thanks,
I've found out something that might be a small error in the call, but it looks like Java indeed used my approach, and dtrace still doesn't show any difference in the behavior.
I updated the repository, with new trace file.

Still, looks very similar.

--
You received this message because you are subscribed to a topic in the Google Groups "mechanical-sympathy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mechanical-sympathy/hYlQY8rXqVQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages