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.
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