Click to See Complete Forum and Search --> : How to do cpu affinity in Linux?
hayafirst
March 15th, 2004, 11:00 AM
Hi all,
I tried affinity_test.c from http://www.kernel.org/pub/linux/kernel/people/rml/cpu-affinity/
and got such EFAULT set in errno (sched_getaffinity and sechd_setaffinity returned -1). My machine is dual CPU Intel Xeon with Hyperthreading enabled, running Gentoo with Kernel 2.6.3-r1
Could someone please tell me what doe EFAULT mean is this situation?
BTW, these functions can get and set processes, but how to use them to set cpu affinity for threads?
Thanks.
dimm_coder
March 16th, 2004, 02:40 AM
Here the man page:
http://www.squarebox.co.uk/cgi-squarebox/manServer/usr/share/man/man2/sched_getaffinity.2
Try to remove the first call to sched_getaffinity()
ret = sched_getaffinity(p, len, NULL);
printf(" sched_getaffinity = %d, len = %u\n", ret, len);
This one gives nothing and that NULL (3-d param) seems to be an inappropriate param here.
In any case, other calls should work.
Try again.
dimm_coder
March 16th, 2004, 03:28 AM
Yes.
That call with 3-d param as NULL is inappropriate for the kernel call. Of course, if your glibc has support for those calls, it might take care about it, providing temp variable for system call but returning nothing to the user. But that sample works without that kind of direct support from glibc side, so NULL is not valid.
In addition, if things are OK, sched_getaffinity() returns the real size of mask param (your len should be equal to that value) and == 4 (bytes) for 32-bit platforms.
Ok, U may take a look on the sources. These pieces are small and easy to understand.
linux/kernel/sched.c
serach for sys_sched_getaffinity and sys_sched_setaffinity.
Then U'll be able to see why it returns EFAULT for the call with NULL passed as 3-d argument :)
hayafirst
March 18th, 2004, 09:19 PM
Thanks. It's helpful. Also I noticed the NPTL provides pthread_setaffinity_np and pthread_getaffinity_np. And it looks they work. Happy. :)
Here I have one more question: How can I know for a specified thread, which processor is it running on? Thanks!
dimm_coder
March 19th, 2004, 02:29 AM
Originally posted by hayafirst
Here I have one more question: How can I know for a specified thread, which processor is it running on? Thanks!
As U know, sched(/thread)_setaffitity() sets the cpus which may be used by this process/thread to run. It's worth noting that old pthread threads are processes internally. They are based on __clone() system call and may share memory/files/etc, thus they look like threads, but have its own process id. That why sched_setaffinity() sets mask for specified thread (or process if it has the only one thread). NPTL uses another design for threads. That's why it has pthread_setaffinity() ,etc.
U can set the set of cpus allowed to run your thread. U can set the only one cpu, thus binding your thread to that cpu. But there's no need for thread to know what cpu it's using this moment (which one from the allowed set of course), at least from user space (u can get that kind info from kernel space (driver)). The kernel scheduler can bind any of allowed cpus to that thread in the next moment. But it doesn't do that offten, it's bad when thread is migrating from one cpu to other, that causes bad cache using. So leave that policy to the scheduler.
In any case, U can froce the thread to migrate on the other cpu, by calling *_setaffinity() with different mask (say, 1 - for 1 cpu, 2 - 2, 4 - 3, etc).
aboteler
April 28th, 2005, 09:13 AM
I am running RedHat Enterprise 3 up 2 WS and tried using the pthread_attr_setaffinity_np call. I call the function and something happens with the CPU load distribution. However, I do not have a tool to determine what threads are where. Is there such a tool? Also I noticed half the time when I specify what CPU to run on that CPU is not utilized viewing in 'top'. Any information would be helpful thanks.
Quote:
"Thanks. It's helpful. Also I noticed the NPTL provides pthread_setaffinity_np and pthread_getaffinity_np. And it looks they work. Happy. "
Thanks,
Aaron
TheGoodStuff
December 1st, 2005, 04:34 AM
Try the Intel Thread profiler, theres a version for linux I believe.
hzmonte
January 30th, 2006, 01:48 AM
Like hayafirst, I want to know for a specified thread, which processor it is running on. I have my own reason to know that. Actually it would be nice if Rober Love's test program contains such function - it is the ultimate test whether the thread is running on one of the CPUs selected in the sched_setaffinity() system call. I found a Linux kernel-level function smp_processor_id() which appears to return the logical processor id the calling thread is running on. Unfortunately one cannot call it from user space. So, does anyone know how to write a kernel patch or something that can expose this function to the user space? (Of course this kernel patch is just for my own use, but if you want to submit it to Mr Trivolds, I certainly would not object.)
And can anyone tell me for what purpose you are using sched_setaffinity() ? I mean, what kind of apps that you want to fix its place of execution?
kaushalgoa
November 5th, 2008, 11:20 PM
If you want to know which thread runs on which processor you can use pidstat -> http://pagesperso-orange.fr/sebastien.godard/tutorial.html#Section2.
Edit by admin: no contact info permitted on the forum, thank you
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.