xref: /netbsd-src/external/gpl3/gcc/dist/libgomp/config/linux/affinity.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /* Copyright (C) 2006-2013 Free Software Foundation, Inc.
2    Contributed by Jakub Jelinek <jakub@redhat.com>.
3 
4    This file is part of the GNU OpenMP Library (libgomp).
5 
6    Libgomp is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14    more details.
15 
16    Under Section 7 of GPL version 3, you are granted additional
17    permissions described in the GCC Runtime Library Exception, version
18    3.1, as published by the Free Software Foundation.
19 
20    You should have received a copy of the GNU General Public License and
21    a copy of the GCC Runtime Library Exception along with this program;
22    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23    <http://www.gnu.org/licenses/>.  */
24 
25 /* This is a Linux specific implementation of a CPU affinity setting.  */
26 
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE 1
29 #endif
30 #include "libgomp.h"
31 #include "proc.h"
32 #include <stdlib.h>
33 #include <unistd.h>
34 
35 #ifdef HAVE_PTHREAD_AFFINITY_NP
36 
37 static unsigned int affinity_counter;
38 
39 void
40 gomp_init_affinity (void)
41 {
42   cpu_set_t cpuset, cpusetnew;
43   size_t idx, widx;
44   unsigned long cpus = 0;
45 
46   if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset))
47     {
48       gomp_error ("could not get CPU affinity set");
49       free (gomp_cpu_affinity);
50       gomp_cpu_affinity = NULL;
51       gomp_cpu_affinity_len = 0;
52       return;
53     }
54 
55   CPU_ZERO (&cpusetnew);
56   if (gomp_cpu_affinity_len == 0)
57     {
58       unsigned long count = gomp_cpuset_popcount (&cpuset);
59       if (count >= 65536)
60 	count = 65536;
61       gomp_cpu_affinity = malloc (count * sizeof (unsigned short));
62       if (gomp_cpu_affinity == NULL)
63 	{
64 	  gomp_error ("not enough memory to store CPU affinity list");
65 	  return;
66 	}
67       for (widx = idx = 0; widx < count && idx < 65536; idx++)
68 	if (CPU_ISSET (idx, &cpuset))
69 	  {
70 	    cpus++;
71 	    gomp_cpu_affinity[widx++] = idx;
72 	  }
73     }
74   else
75     for (widx = idx = 0; idx < gomp_cpu_affinity_len; idx++)
76       if (gomp_cpu_affinity[idx] < CPU_SETSIZE
77 	  && CPU_ISSET (gomp_cpu_affinity[idx], &cpuset))
78 	{
79 	  if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpusetnew))
80 	    {
81 	      cpus++;
82 	      CPU_SET (gomp_cpu_affinity[idx], &cpusetnew);
83 	    }
84 	  gomp_cpu_affinity[widx++] = gomp_cpu_affinity[idx];
85 	}
86 
87   if (widx == 0)
88     {
89       gomp_error ("no CPUs left for affinity setting");
90       free (gomp_cpu_affinity);
91       gomp_cpu_affinity = NULL;
92       gomp_cpu_affinity_len = 0;
93       return;
94     }
95 
96   gomp_cpu_affinity_len = widx;
97   if (cpus < gomp_available_cpus)
98     gomp_available_cpus = cpus;
99   CPU_ZERO (&cpuset);
100   CPU_SET (gomp_cpu_affinity[0], &cpuset);
101   pthread_setaffinity_np (pthread_self (), sizeof (cpuset), &cpuset);
102   affinity_counter = 1;
103 }
104 
105 void
106 gomp_init_thread_affinity (pthread_attr_t *attr)
107 {
108   unsigned int cpu;
109   cpu_set_t cpuset;
110 
111   cpu = __atomic_fetch_add (&affinity_counter, 1, MEMMODEL_RELAXED);
112   cpu %= gomp_cpu_affinity_len;
113   CPU_ZERO (&cpuset);
114   CPU_SET (gomp_cpu_affinity[cpu], &cpuset);
115   pthread_attr_setaffinity_np (attr, sizeof (cpu_set_t), &cpuset);
116 }
117 
118 #else
119 
120 #include "../posix/affinity.c"
121 
122 #endif
123