|
|
| Next: How to write continuously with NFS. |
| Author |
Message |
lhnewsgrps External

Since: Jul 31, 2007 Posts: 1
|
Posted: Tue Jul 31, 2007 1:47 pm Post subject: Allocation of threads on a dual CPU system? Archived from groups: comp>os>linux>development>apps (more info?) |
|
|
I'm working on an RHEL 3 box with 2 CPUs. If a process is started on
one processor and spins up 200 POSIX threads, will the threads be
distributed among the 2 CPUs or just run on the CPU that the process
started on?
Is there a way to for a thread to obtain the number of the CPU its
running on? I'm scanning through the POSIX documentation but haven't
found it yet, if it exists.
Thanks.
Les |
|
| Back to top |
|
 |
Olivier Croquette External

Since: Aug 20, 2006 Posts: 6
|
Posted: Tue Jul 31, 2007 11:56 pm Post subject: Re: Allocation of threads on a dual CPU system? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
lhnewsgrps RemoveThis @yahoo.com wrote, On 31/07/07 22:47:
> I'm working on an RHEL 3 box with 2 CPUs. If a process is started on
> one processor and spins up 200 POSIX threads, will the threads be
> distributed among the 2 CPUs or just run on the CPU that the process
> started on?
In the general case, the threads will run on any process and may jump
from one to another at anytime.
If you want them to stick, use sched_setaffinity().
Note that:
- the affinity is inherited by the children of a process
- the kernel does a good job at load-balancing, so using the affinity is
not necessarily a good idea.
> Is there a way to for a thread to obtain the number of the CPU its
> running on? I'm scanning through the POSIX documentation but haven't
> found it yet, if it exists.
It's not POSIX, but it's available under Linux: gethostid
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man...ethosti
Note that the result of this call may change several times per second
and may have become inaccurate before it even returns. |
|
| Back to top |
|
 |
Paul Pluzhnikov External

Since: Nov 07, 2004 Posts: 355
|
Posted: Tue Jul 31, 2007 11:56 pm Post subject: Re: Allocation of threads on a dual CPU system? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Olivier Croquette <ocroquette.TakeThisOut@ocroquette.free.fr> writes:
> lhnewsgrps.TakeThisOut@yahoo.com wrote, On 31/07/07 22:47:
>> Is there a way to for a thread to obtain the number of the CPU its
>> running on?
Only kernel knows that, so it would have to be a system call.
I do not believe there is such a system call.
Additional complication: the kernel keeps track of physical processor
id, and core id witin that processor. What is "the number of the CPU"
a thread is running on, on a dual-processor system where every
processor is quad-core?
> It's not POSIX, but it's available under Linux: gethostid
Gethostid returns an arbitrary number contained in /etc/hostid file,
or lacking that file, an IPv4 address of the host.
What does this have to do with the numer of the CPU this thread is
currently running on?
> Note that the result of this call may change several times per second
> and may have become inaccurate before it even returns.
I believe you mis-understood what gethostid returns.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email. |
|
| Back to top |
|
 |
ellis External

Since: Apr 02, 2004 Posts: 193
|
Posted: Wed Aug 01, 2007 7:00 am Post subject: Re: Allocation of threads on a dual CPU system? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
In article <1185914841.833028.254020.TakeThisOut@x40g2000prg.googlegroups.com>,
<lhnewsgrps.TakeThisOut@yahoo.com> wrote:
>Is there a way to for a thread to obtain the number of the CPU its
>running on?
Why would you care?
--
http://www.spinics.net/lists/kvm-devel/ |
|
| Back to top |
|
 |
Olivier Croquette External

Since: Aug 20, 2006 Posts: 6
|
Posted: Wed Aug 01, 2007 8:25 pm Post subject: Re: Allocation of threads on a dual CPU system? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Paul Pluzhnikov wrote, On 1/08/07 7:19:
> Only kernel knows that, so it would have to be a system call.
There are instructions giving information about the CPU on common
platforms, so I can imagine it's more or less doable from userspace on
these ones.
> I do not believe there is such a system call.
[...]
> Gethostid returns an arbitrary number contained in /etc/hostid file,
> or lacking that file, an IPv4 address of the host.
You're right. I don't know what went through my mind at the moment I
posted that...
>> Note that the result of this call may change several times per second
>> and may have become inaccurate before it even returns.
>
> I believe you mis-understood what gethostid returns.
Well, if there was such a call, the above statement about it would be
correct
Some resources on this topic:
http://sourceware.org/ml/libc-alpha/2006-06/msg00024.html
smp_processor_id() (kernel):
http://kernel.org/doc/htmldocs/kernel-hacking.html#routines-processorids
So it seems there is no /easy/ way. |
|
| Back to top |
|
 |
David Schwartz External

Since: Jun 01, 2007 Posts: 87
|
Posted: Thu Aug 02, 2007 7:31 pm Post subject: Re: Allocation of threads on a dual CPU system? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
On Jul 31, 1:47 pm, lhnewsg....DeleteThis@yahoo.com wrote:
> I'm working on an RHEL 3 box with 2 CPUs. If a process is started on
> one processor and spins up 200 POSIX threads, will the threads be
> distributed among the 2 CPUs or just run on the CPU that the process
> started on?
You can't say that they will, because there are too many variables.
They well might be. For example, if the other CPU was being used by a
task using a privileged scheduler, these ordinary threads would not be
scheduled on it unless that other task voluntarily yielded.
> Is there a way to for a thread to obtain the number of the CPU its
> running on? I'm scanning through the POSIX documentation but haven't
> found it yet, if it exists.
Nope, because the kernel is free to deschedule or migrate the thread
at any time. So such a function would be impossible to write except in
conjunction with some way to lock a thread to a particular processor.
You could, for example, tell the scheduler to only schedule you on a
particular CPU and then yield. On some OSes (I think Linux too, but
I'd have to check), you are guaranteed only to resume on a CPU that
you are currently allowed to run on.
DS |
|
| Back to top |
|
 |
David Schwartz External

Since: Jun 01, 2007 Posts: 87
|
Posted: Thu Aug 02, 2007 8:05 pm Post subject: Re: Allocation of threads on a dual CPU system? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Interestingly, recent versions of Linux (with glibc-2.6) have
'sched_getcpu'. It's not particularly useful, but it does exist.
Here's some sample code:
#include <sched.h>
#include <stdio.h>
#include <pthread.h>
void *tf(void *t)
{
int i, j, q, cpu;
for(i=0; i<25600; i++)
for(j=0; j<25600; j++)
{ /* burn some CPU */
q>>=2;
q+=i;
q-=j;
}
cpu=sched_getcpu();
for(i=0; i<25600; i++)
for(j=0; j<25600; j++)
{ /* keep burning */
q<<=2;
q+=i;
}
printf("%d Running on CPU %d\n",
(int) t, cpu);
*(volatile int *) &q=q+1; /* don't let compiler optimize q away */
return (void *) cpu;
}
int main(void)
{ /* create 8 CPU burners and see what CPUs they run on */
pthread_t t[8];
int i;
void *r;
for(i=0; i<8; i++)
pthread_create(&t[i], NULL, tf, (void *) (i+1));
for(i=0; i<8; i++)
pthread_join(t[i], &r);
}
On my quad-core, this is typical:
8 Running on CPU 1
1 Running on CPU 1
4 Running on CPU 3
3 Running on CPU 3
5 Running on CPU 2
6 Running on CPU 0
2 Running on CPU 2
7 Running on CPU 0
DS |
|
| Back to top |
|
 |
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|
|