Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process CPU affinity #238

Closed
giampaolo opened this issue May 23, 2014 · 10 comments
Closed

Process CPU affinity #238

giampaolo opened this issue May 23, 2014 · 10 comments

Comments

@giampaolo
Copy link
Owner

From g.rodola on December 20, 2011 20:08:06

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

Original issue: http://code.google.com/p/psutil/issues/detail?id=238

@giampaolo giampaolo self-assigned this May 23, 2014
@giampaolo
Copy link
Owner Author

From g.rodola on December 20, 2011 16:05:40

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

Attachment: cpu-affinity-linux.patch

@giampaolo
Copy link
Owner Author

From [email protected] on December 20, 2011 23:52:06

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.

@giampaolo
Copy link
Owner Author

From g.rodola on December 21, 2011 03:25:53

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

@giampaolo
Copy link
Owner Author

From g.rodola on December 23, 2011 13:03:01

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

Status: Started

@giampaolo
Copy link
Owner Author

From g.rodola on December 25, 2011 06:56:59

Windows implementation committed in r1249 .

Status: FixedInSVN

@giampaolo
Copy link
Owner Author

From g.rodola on February 17, 2012 03:54:00

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.

@giampaolo
Copy link
Owner Author

From g.rodola on June 27, 2012 11:54:03

0.5.0 is finally out. Closing out as fixed.

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 04:06:11

Updated csets after the SVN -> Mercurial migration: r1246 == revision 
9235dc5f5a15 r1249 == revision 566889617340

@giampaolo
Copy link
Owner Author

From [email protected] on May 09, 2013 12:16:18

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.

@giampaolo
Copy link
Owner Author

From g.rodola on May 09, 2013 16:05:39

When I looked into OSX implementation it seemed this wasn't possible.
If you have any reference / pointers please share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant