Issue 238 in psutil: Process CPU affinity

0 views
Skip to first unread message

psu...@googlecode.com

unread,
Dec 20, 2011, 2:08:26 PM12/20/11
to psutil-...@googlegroups.com
Status: New
Owner: g.rod...@gmail.com
Labels: OpSys-Windows OpSys-Linux Milestone-0.5.0 Type-Enhancement

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

psu...@googlecode.com

unread,
Dec 20, 2011, 7:05:50 PM12/20/11
to psutil-...@googlegroups.com

Comment #1 on issue 238 by g.rod...@gmail.com: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238

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

psu...@googlecode.com

unread,
Dec 21, 2011, 2:52:35 AM12/21/11
to psutil-...@googlegroups.com

Comment #2 on issue 238 by maciej.l...@gmail.com: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238

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.

psu...@googlecode.com

unread,
Dec 21, 2011, 6:26:10 AM12/21/11
to psutil-...@googlegroups.com

Comment #3 on issue 238 by g.rod...@gmail.com: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238

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]


psu...@googlecode.com

unread,
Dec 23, 2011, 4:05:23 PM12/23/11
to psutil-...@googlegroups.com
Updates:
Status: Started

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])

psu...@googlecode.com

unread,
Dec 25, 2011, 9:59:59 AM12/25/11
to psutil-...@googlegroups.com
Updates:
Status: FixedInSVN

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.

psu...@googlecode.com

unread,
Feb 17, 2012, 6:54:31 AM2/17/12
to psutil-...@googlegroups.com

Comment #6 on issue 238 by g.rodola: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238

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.

psu...@googlecode.com

unread,
May 9, 2013, 3:16:25 PM5/9/13
to psutil-...@googlegroups.com

Comment #10 on issue 238 by paul.qu...@gmail.com: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238

Hi, is there a plan to pursue the implementation of cpu_affinity on OS X in
the future? I know Apple has made it difficult by "not exporting
interfaces to processor control"... Thanks.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

psu...@googlecode.com

unread,
May 9, 2013, 7:05:52 PM5/9/13
to psutil-...@googlegroups.com

Comment #11 on issue 238 by g.rodola: Process CPU affinity
http://code.google.com/p/psutil/issues/detail?id=238

When I looked into OSX implementation it seemed this wasn't possible.
If you have any reference / pointers please share.
Reply all
Reply to author
Forward
0 new messages