New issue 238 by g.rod...@gmail.com: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238
Proposal
This follows ml proposal:
https://groups.google.com/forum/#!topic/psutil/q9nJ1SZdqe0
...and it is well explained here:
http://www.linuxjournal.com/article/6799?page=0,0
CPU affinity consists in telling the OS to run a certain process on a
limited set of CPUs only.
On Linux, this can be done via taskset command.
For example, to run process with PID 7824 on CPUs 0 and 1 only:
giampaolo@ubuntu:~$ taskset -c -p 0,1 7824
pid 7824's current affinity list: 0
pid 7824's new affinity list: 0,1
On what platforms would this be available?
Linux and Windows.
Proposed API
The API I have in mind follows the same we used for psutil.Process.nice: a
r/w property which can be used for both getting and setting the new value.
More or less:
>>> p = psutil.Process(os.getpid())
>>> p.cpu_affinity
[0]
>>> p.cpu_affinity = [0, 1]
>>> p.cpu_affinity
[0, 1]
>>> p.cpu_affinity = [0, 1, 2, 30]
Traceback:
...
ValueError: invalid cpu 30
Are there existent implementations we can use as an example?
http://pypi.python.org/pypi/affinity/0.1.0
Preliminary Linux patch in attachment.
I'm still not sure whether I like the property-based API though.
Alternatives:
>>> p.cpu_affinity() # get
[0]
>>> p.cpu_affinity([0]) # set
...or:
>>> p.get_cpu_affinity() # get
[0]
>>> p.set_cpu_affinity([0]) # set
Attachments:
cpu-affinity-linux.patch 7.0 KB
One tip regarding implementation - original affinity implementation has a
bug, which makes it unusable in some Linux environments (for example CentOS
5.5). I am using a alternative implementation from:
http://alioth.debian.org/tracker/index.php?func=detail&aid=311537&group_id=30402&atid=411002
which seems to work for my cases.
Would it be possible to call it in following manner:
p = psutil.Process(0)
p.cpu_affinity = [0, 1]
sp = subprocess.Popen(...)
With current affinity implementation it allows to set an affinity for
spawned process in advance.
What's the problem? PID 0?
This works for me:
>>> import psutil
>>> p = psutil.Process(0)
>>> p.cpu_affinity
[0, 1]
>>> p.cpu_affinity = [0]
>>> p.cpu_affinity
[0]
Comment #4 on issue 238 by g.rod...@gmail.com: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238
Linux implementation committed in r1246.
As for now I choose the following API but I know myself, and I might change
my mind later =):
>>> get_cpu_affinity()
[0,1]
>>> set_cpu_affinity([0,1])
Comment #5 on issue 238 by g.rod...@gmail.com: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238
Windows implementation committed in r1249.
Note to self - python 3.3. exposed different sched.h functions:
http://docs.python.org/dev/whatsnew/3.3.html#os
http://bugs.python.org/issue12655
http://docs.python.org/dev/library/os.html#interface-to-the-scheduler
It might be worth to take a look and figure out whether we can include some
of those in psutil.