How to set processor affinity on OS X?

17

9

How do you set processor affinity in Snow Leopard on a MacBook Pro?I know in Windows you could just switch it in Task Manager.

MarshallBananas

Posted 2010-06-06T01:35:30.233

Reputation:

2<snarky-comment>Run OS X in a virtual machine, and set the affinity of the virtual machine</snarky-comment> – zildjohn01 – 2010-06-06T01:49:13.813

Why would you want to do this? It's generally a bad idea unless you're trying to get old programs running that are so badly coded they break on multicore systems. – jalf – 2010-06-06T01:52:41.953

2@jalf: processor affinity can improve performance since it reduces cache invalidation & trashing in some cases. – None – 2010-06-06T02:20:50.940

In our case, many applications don't support limiting the number of cores while crunching hard data, so the naïve approach to keeping a system responsive on the other platforms has been to deny the different apps access to all cores. – RandomInsano – 2016-08-11T18:01:26.617

1That's disappointing. Looks like Mac will never be an ideal platform for real-time software development. – Evan Plaice – 2011-02-11T00:11:46.717

1

@jweyrich Excepts in CPUs with QPI (Intel's NUMA) like those on the Mac Pro, where setting CPU affinity disables memory affinity and decreases performance. This does not apply to mobile processors though.

– Jano – 2012-08-29T21:50:48.103

Answers

14

OS X has supported a thread affinity API since version 10.5. Here is some relevant material from the webpage I linked to.

Affinity Set

An affinity set is a collection of threads which share memory resources and wish to share an L2 cache. Distinct affinity sets represent separate affinities—that is, threads belonging to a different set should use a separate L2 cache and hence be run on a different logical processors.

An affinity set is identified by a "tag". Threads are assigned to a particular affinity set by assigning it the tag identifying that set. A thread can belong to at most one affinity set; that is, it has one affinity tag.

Effect of Setting Distinct Affinity Tags

For example, an application wanting to run 2 threads on separate L2 caches would set the threads with different affinity tags. On a dual core machine, this affinity will effectively be ignored. However, on a 4-core MacPro, the scheduler will try to run threads on separate packages. Similarly, on an 8-core MacPro, the scheduler will try to run these threads on separate dies (which may or may not be in the same physical CPU package).

Example Usage

An application that wants to place a thread on every available processor would do the following:

  • Obtain the number of processors on the system using sysctl(3).
  • Create that number of threads.
  • Set each thread with a distinct affinity tag.
  • Start all threads.

Threads with default affinity policy will be scheduled more freely on any processor. These threads will be preferentially migrated to run on an idle processor. Threads with affinity tags will tend to remain in place.

Consult the source for code listings, and information about the sharing of affinity tags between parent and child processes, obtaining the CPU cache configuration, and more.

void-pointer

Posted 2010-06-06T01:35:30.233

Reputation: 241

1Is there a commandline utility, in addition to this API? – Victor Eijkhout – 2019-05-15T17:14:18.560

11

http://developer.apple.com/mac/library/releasenotes/Performance/RN-AffinityAPI/

Mac OS X does not export interfaces that identify processors or control thread placement—explicit thread to processor binding is not supported. Instead, the kernel manages all thread placement. Applications expect that the scheduler will, under most circumstances, run its threads using a good processor placement with respect to cache affinity.

ta.speot.is

Posted 2010-06-06T01:35:30.233

Reputation: 13 727

2OS X has supported a thread affinity API since version 10.5. See my answer for details. – void-pointer – 2014-06-19T10:45:25.967

4

Until now, the XNU (1504.3.12) scheduler doesn't implement processor affinity for processes nor threads.

So MacOSX doesn't provide any means to do that.

jweyrich

Posted 2010-06-06T01:35:30.233

Reputation: 1 106

2

From http://images.apple.com/macosx/docs/OSX_for_UNIX_Users_TB_July2011.pdf

• Efficient kernel threads. Each POSIX thread is queued onto a particular CPU, improving processor affinity and scalability while reducing lock contention. Threads conform to POSIX (1c), including support for cancellation and shared mutexes.

It looks like ad to me, my iMac running Lion looks to respect that most of the time, but it do not 'pin'a process to a core.

I could not find any API to control process affinity for darwin anyway.

Alan Baldo

Posted 2010-06-06T01:35:30.233

Reputation: 21