aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/linux/tst-sched-affinity-inheritance.c
blob: 8a42d275fce35e8470d5394383c03ed2b948d903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* CPU Affinity inheritance test - sched_{gs}etaffinity.
   Copyright The GNU Toolchain Authors.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* See top level comment in nptl/tst-skeleton-affinity-inheritance.c for a
   description of this test.  */

#include <sched.h>
#include <string.h>
#include <stdio.h>
#include <support/check.h>

static void
set_my_affinity (size_t size, const cpu_set_t *set)
{
  int ret = sched_setaffinity (0, size, set);

  if (ret != 0)
    FAIL ("sched_setaffinity returned %d (%s)", ret, strerror (ret));
}

static void
verify_my_affinity (int nproc, int nproc_configured, size_t size,
		    const cpu_set_t *expected_set)
{
  cpu_set_t *set = CPU_ALLOC (nproc_configured);
  cpu_set_t *xor_set = CPU_ALLOC (nproc_configured);

  if (set == NULL || xor_set== NULL)
    FAIL_EXIT1 ("verify_my_affinity: Failed to allocate cpuset: %m\n");

  int ret = sched_getaffinity (0, size, set);
  if (ret != 0)
    FAIL ("sched_getaffinity returned %d (%s)", ret, strerror (ret));

  CPU_XOR_S (size, xor_set, expected_set, set);

  int cpucount = CPU_COUNT_S (size, xor_set);

  if (cpucount > 0)
    {
      FAIL ("Affinity mask not inherited, "
	    "following %d CPUs mismatched in the expected and actual sets:\n",
	    cpucount);
      for (int cur = 0; cur < nproc && cpucount >= 0; cur++)
	if (CPU_ISSET_S (size, cur, xor_set))
	  {
	    printf ("%d ", cur);
	    cpucount--;
	  }
      printf ("\n");
    }

  CPU_FREE (set);
  CPU_FREE (xor_set);
}

#include <nptl/tst-skeleton-affinity-inheritance.c>