public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* getcpu after sched_setaffinity
@ 2007-05-10 22:24 Ulrich Drepper
  2007-05-10 23:44 ` Andi Kleen
  2007-05-12 10:16 ` Bert Wesarg
  0 siblings, 2 replies; 5+ messages in thread
From: Ulrich Drepper @ 2007-05-10 22:24 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Linux Kernel

[-- Attachment #1: Type: text/plain, Size: 822 bytes --]

The attached test program fails on a dual core (and probably SMP) 
machine on x86-64.  Depending on where the thread starts, in one of the 
iterations the sched_setffinity() call succeeds but then sched_getcpu() 
fails to report the correct CPU.

In set_cpus_allowed migrate_task() is called if the new CPU set does not 
include the current CPU.  I hope that migrate_task() also works for 
p==current.

This leaves the x86-64 vgetcpu() implementation as the weak point.  Is 
the caching causing problems?  Should migrate_task() make sure the cache 
is reset?

You need a very recent glibc to compile (glibc-2.5.90-22 in rawhide). 
If this is not available replace the sched_getcpu() call.  But make sure 
you pass a pointer to a cache.

-- 
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖

[-- Attachment #2: tst-getcpu.c --]
[-- Type: text/plain, Size: 875 bytes --]

#include <errno.h>
#include <stdio.h>
#include <sched.h>


int
main (void)
{
  cpu_set_t cs;
  if (sched_getaffinity (getpid (), sizeof (cs), &cs) != 0)
    {
      printf ("getaffinity failed: %m\n");
      return 1;
    }

  int result = 0;
  int cpu = 0;
  while (CPU_COUNT (&cs) != 0)
    {
      if (CPU_ISSET (cpu, &cs))
	{
	  cpu_set_t cs2;
	  CPU_ZERO (&cs2);
	  CPU_SET (cpu, &cs2);
	  if (sched_setaffinity (getpid (), sizeof (cs2), &cs2) != 0)
	    {
	      printf ("setaffinity(%d) failed: %m\n", cpu);
	      result = 1;
	    }
	  else
	    {
	      int cpu2 = sched_getcpu ();
	      if (cpu2 == -1 && errno == ENOSYS)
		{
		  puts ("getcpu syscall not implemented");
		  return 0;
		}
	      if (cpu2 != cpu)
		{
		  printf ("getcpu results %d not possible\n", cpu2);
		  result = 1;
		}
	    }
	  CPU_CLR (cpu, &cs);
	}
      ++cpu;
    }

  return result;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-05-12 10:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-10 22:24 getcpu after sched_setaffinity Ulrich Drepper
2007-05-10 23:44 ` Andi Kleen
2007-05-10 23:09   ` Ulrich Drepper
2007-05-11  7:08     ` Andi Kleen
2007-05-12 10:16 ` Bert Wesarg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox