Blame sysdeps/unix/sysv/linux/tst-skeleton-thread-affinity.c

Packit 6c4009
/* Generic test for CPU affinity functions, multi-threaded variant.
Packit 6c4009
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Before including this file, a test has to declare the helper
Packit 6c4009
   getaffinity and setaffinity functions described in
Packit 6c4009
   tst-skeleton-affinity.c, which is included below.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
Packit 6c4009
struct conf;
Packit 6c4009
static bool early_test (struct conf *);
Packit 6c4009
Packit 6c4009
/* Arbitrary run time for each pass.  */
Packit 6c4009
#define PASS_TIMEOUT 2
Packit 6c4009
Packit 6c4009
/* There are two passes (one with sched_yield, one without), and we
Packit 6c4009
   double the timeout to be on the safe side.  */
Packit 6c4009
#define TIMEOUT (2 * PASS_TIMEOUT * 2)
Packit 6c4009
Packit 6c4009
#include "tst-skeleton-affinity.c"
Packit 6c4009
Packit 6c4009
/* 0 if still running, 1 of stopping requested.  */
Packit 6c4009
static int still_running;
Packit 6c4009
Packit 6c4009
/* 0 if no scheduling failures, 1 if failures are encountered.  */
Packit 6c4009
static int failed;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
thread_burn_one_cpu (void *closure)
Packit 6c4009
{
Packit 6c4009
  int cpu = (uintptr_t) closure;
Packit 6c4009
  while (__atomic_load_n (&still_running, __ATOMIC_RELAXED) == 0)
Packit 6c4009
    {
Packit 6c4009
      int current = sched_getcpu ();
Packit 6c4009
      if (sched_getcpu () != cpu)
Packit 6c4009
	{
Packit 6c4009
	  printf ("error: Pinned thread %d ran on impossible cpu %d\n",
Packit 6c4009
		  cpu, current);
Packit 6c4009
	  __atomic_store_n (&failed, 1, __ATOMIC_RELAXED);
Packit 6c4009
	  /* Terminate early.  */
Packit 6c4009
	  __atomic_store_n (&still_running, 1, __ATOMIC_RELAXED);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
struct burn_thread
Packit 6c4009
{
Packit 6c4009
  pthread_t self;
Packit 6c4009
  struct conf *conf;
Packit 6c4009
  cpu_set_t *initial_set;
Packit 6c4009
  cpu_set_t *seen_set;
Packit 6c4009
  int thread;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
thread_burn_any_cpu (void *closure)
Packit 6c4009
{
Packit 6c4009
  struct burn_thread *param = closure;
Packit 6c4009
Packit 6c4009
  /* Schedule this thread around a bit to see if it lands on another
Packit 6c4009
     CPU.  Run this for 2 seconds, once with sched_yield, once
Packit 6c4009
     without.  */
Packit 6c4009
  for (int pass = 1; pass <= 2; ++pass)
Packit 6c4009
    {
Packit 6c4009
      time_t start = time (NULL);
Packit 6c4009
      while (time (NULL) - start <= PASS_TIMEOUT)
Packit 6c4009
	{
Packit 6c4009
	  int cpu = sched_getcpu ();
Packit 6c4009
	  if (cpu > param->conf->last_cpu
Packit 6c4009
	      || !CPU_ISSET_S (cpu, CPU_ALLOC_SIZE (param->conf->set_size),
Packit 6c4009
			       param->initial_set))
Packit 6c4009
	    {
Packit 6c4009
	      printf ("error: Unpinned thread %d ran on impossible CPU %d\n",
Packit 6c4009
		      param->thread, cpu);
Packit 6c4009
	      __atomic_store_n (&failed, 1, __ATOMIC_RELAXED);
Packit 6c4009
	      return NULL;
Packit 6c4009
	    }
Packit 6c4009
	  CPU_SET_S (cpu, CPU_ALLOC_SIZE (param->conf->set_size),
Packit 6c4009
		     param->seen_set);
Packit 6c4009
	  if (pass == 1)
Packit 6c4009
	    sched_yield ();
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
stop_and_join_threads (struct conf *conf, cpu_set_t *set,
Packit 6c4009
		       pthread_t *pinned_first, pthread_t *pinned_last,
Packit 6c4009
		       struct burn_thread *other_first,
Packit 6c4009
		       struct burn_thread *other_last)
Packit 6c4009
{
Packit 6c4009
  __atomic_store_n (&still_running, 1, __ATOMIC_RELAXED);
Packit 6c4009
  for (pthread_t *p = pinned_first; p < pinned_last; ++p)
Packit 6c4009
    {
Packit 6c4009
      int cpu = p - pinned_first;
Packit 6c4009
      if (!CPU_ISSET_S (cpu, CPU_ALLOC_SIZE (conf->set_size), set))
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      int ret = pthread_join (*p, NULL);
Packit 6c4009
      if (ret != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("error: Failed to join thread %d: %s\n", cpu, strerror (ret));
Packit 6c4009
	  fflush (stdout);
Packit 6c4009
	  /* Cannot shut down cleanly with threads still running.  */
Packit 6c4009
	  abort ();
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (struct burn_thread *p = other_first; p < other_last; ++p)
Packit 6c4009
    {
Packit 6c4009
      int cpu = p - other_first;
Packit 6c4009
      if (!CPU_ISSET_S (cpu, CPU_ALLOC_SIZE (conf->set_size), set))
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      int ret = pthread_join (p->self, NULL);
Packit 6c4009
      if (ret != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("error: Failed to join thread %d: %s\n", cpu, strerror (ret));
Packit 6c4009
	  fflush (stdout);
Packit 6c4009
	  /* Cannot shut down cleanly with threads still running.  */
Packit 6c4009
	  abort ();
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Tries to check that the initial set of CPUs is complete and that
Packit 6c4009
   the main thread will not run on any other threads.  */
Packit 6c4009
static bool
Packit 6c4009
early_test (struct conf *conf)
Packit 6c4009
{
Packit 6c4009
  pthread_t *pinned_threads
Packit 6c4009
    = calloc (conf->last_cpu + 1, sizeof (*pinned_threads));
Packit 6c4009
  struct burn_thread *other_threads
Packit 6c4009
    = calloc (conf->last_cpu + 1, sizeof (*other_threads));
Packit 6c4009
  cpu_set_t *initial_set = CPU_ALLOC (conf->set_size);
Packit 6c4009
  cpu_set_t *scratch_set = CPU_ALLOC (conf->set_size);
Packit 6c4009
Packit 6c4009
  if (pinned_threads == NULL || other_threads == NULL
Packit 6c4009
      || initial_set == NULL || scratch_set == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("error: Memory allocation failure");
Packit 6c4009
      return false;
Packit 6c4009
    }
Packit 6c4009
  if (getaffinity (CPU_ALLOC_SIZE (conf->set_size), initial_set) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: pthread_getaffinity_np failed: %m\n");
Packit 6c4009
      return false;
Packit 6c4009
    }
Packit 6c4009
  for (int cpu = 0; cpu <= conf->last_cpu; ++cpu)
Packit 6c4009
    {
Packit 6c4009
      if (!CPU_ISSET_S (cpu, CPU_ALLOC_SIZE (conf->set_size), initial_set))
Packit 6c4009
	continue;
Packit 6c4009
      other_threads[cpu].conf = conf;
Packit 6c4009
      other_threads[cpu].initial_set = initial_set;
Packit 6c4009
      other_threads[cpu].thread = cpu;
Packit 6c4009
      other_threads[cpu].seen_set = CPU_ALLOC (conf->set_size);
Packit 6c4009
      if (other_threads[cpu].seen_set == NULL)
Packit 6c4009
	{
Packit 6c4009
	  puts ("error: Memory allocation failure");
Packit 6c4009
	  return false;
Packit 6c4009
	}
Packit 6c4009
      CPU_ZERO_S (CPU_ALLOC_SIZE (conf->set_size),
Packit 6c4009
		  other_threads[cpu].seen_set);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pthread_attr_t attr;
Packit 6c4009
  int ret = pthread_attr_init (&attr);
Packit 6c4009
  if (ret != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: pthread_attr_init failed: %s\n", strerror (ret));
Packit 6c4009
      return false;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Spawn a thread pinned to each available CPU.  */
Packit 6c4009
  for (int cpu = 0; cpu <= conf->last_cpu; ++cpu)
Packit 6c4009
    {
Packit 6c4009
      if (!CPU_ISSET_S (cpu, CPU_ALLOC_SIZE (conf->set_size), initial_set))
Packit 6c4009
	continue;
Packit 6c4009
      CPU_ZERO_S (CPU_ALLOC_SIZE (conf->set_size), scratch_set);
Packit 6c4009
      CPU_SET_S (cpu, CPU_ALLOC_SIZE (conf->set_size), scratch_set);
Packit 6c4009
      ret = pthread_attr_setaffinity_np
Packit 6c4009
	(&attr, CPU_ALLOC_SIZE (conf->set_size), scratch_set);
Packit 6c4009
      if (ret != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("error: pthread_attr_setaffinity_np for CPU %d failed: %s\n",
Packit 6c4009
		  cpu, strerror (ret));
Packit 6c4009
	  stop_and_join_threads (conf, initial_set,
Packit 6c4009
				 pinned_threads, pinned_threads + cpu,
Packit 6c4009
				 NULL, NULL);
Packit 6c4009
	  return false;
Packit 6c4009
	}
Packit 6c4009
      ret = pthread_create (pinned_threads + cpu, &attr,
Packit 6c4009
			    thread_burn_one_cpu, (void *) (uintptr_t) cpu);
Packit 6c4009
      if (ret != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("error: pthread_create for CPU %d failed: %s\n",
Packit 6c4009
		  cpu, strerror (ret));
Packit 6c4009
	  stop_and_join_threads (conf, initial_set,
Packit 6c4009
				 pinned_threads, pinned_threads + cpu,
Packit 6c4009
				 NULL, NULL);
Packit 6c4009
	  return false;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Spawn another set of threads running on all CPUs.  */
Packit 6c4009
  for (int cpu = 0; cpu <= conf->last_cpu; ++cpu)
Packit 6c4009
    {
Packit 6c4009
      if (!CPU_ISSET_S (cpu, CPU_ALLOC_SIZE (conf->set_size), initial_set))
Packit 6c4009
	continue;
Packit 6c4009
      ret = pthread_create (&other_threads[cpu].self, NULL,
Packit 6c4009
			    thread_burn_any_cpu, other_threads + cpu);
Packit 6c4009
      if (ret != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("error: pthread_create for thread %d failed: %s\n",
Packit 6c4009
		  cpu, strerror (ret));
Packit 6c4009
	  stop_and_join_threads (conf, initial_set,
Packit 6c4009
				 pinned_threads,
Packit 6c4009
				 pinned_threads + conf->last_cpu + 1,
Packit 6c4009
				 other_threads, other_threads + cpu);
Packit 6c4009
	  return false;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Main thread.  */
Packit 6c4009
  struct burn_thread main_thread;
Packit 6c4009
  main_thread.conf = conf;
Packit 6c4009
  main_thread.initial_set = initial_set;
Packit 6c4009
  main_thread.seen_set = scratch_set;
Packit 6c4009
  main_thread.thread = -1;
Packit 6c4009
  CPU_ZERO_S (CPU_ALLOC_SIZE (conf->set_size), main_thread.seen_set);
Packit 6c4009
  thread_burn_any_cpu (&main_thread);
Packit 6c4009
  stop_and_join_threads (conf, initial_set,
Packit 6c4009
			 pinned_threads,
Packit 6c4009
			 pinned_threads + conf->last_cpu + 1,
Packit 6c4009
			 other_threads, other_threads + conf->last_cpu + 1);
Packit 6c4009
Packit 6c4009
  printf ("info: Main thread ran on %d CPU(s) of %d available CPU(s)\n",
Packit 6c4009
	  CPU_COUNT_S (CPU_ALLOC_SIZE (conf->set_size), scratch_set),
Packit 6c4009
	  CPU_COUNT_S (CPU_ALLOC_SIZE (conf->set_size), initial_set));
Packit 6c4009
  CPU_ZERO_S (CPU_ALLOC_SIZE (conf->set_size), scratch_set);
Packit 6c4009
  for (int cpu = 0; cpu <= conf->last_cpu; ++cpu)
Packit 6c4009
    {
Packit 6c4009
      if (!CPU_ISSET_S (cpu, CPU_ALLOC_SIZE (conf->set_size), initial_set))
Packit 6c4009
	continue;
Packit 6c4009
      CPU_OR_S (CPU_ALLOC_SIZE (conf->set_size),
Packit 6c4009
		scratch_set, scratch_set, other_threads[cpu].seen_set);
Packit 6c4009
      CPU_FREE (other_threads[cpu].seen_set);
Packit 6c4009
    }
Packit 6c4009
  printf ("info: Other threads ran on %d CPU(s)\n",
Packit 6c4009
	  CPU_COUNT_S (CPU_ALLOC_SIZE (conf->set_size), scratch_set));;
Packit 6c4009
Packit 6c4009
Packit 6c4009
  pthread_attr_destroy (&attr);
Packit 6c4009
  CPU_FREE (scratch_set);
Packit 6c4009
  CPU_FREE (initial_set);
Packit 6c4009
  free (pinned_threads);
Packit 6c4009
  free (other_threads);
Packit 6c4009
  return failed == 0;
Packit 6c4009
}