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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/* Copyright (C) 2006-2015 Free Software Foundation, Inc.
Contributed by Jakub Jelinek <jakub@redhat.com>.
This file is part of the GNU Offloading and Multi Processing Library
(libgomp).
Libgomp is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Libgomp 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 General Public License for
more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
/* This is a Linux specific implementation of a CPU affinity setting. */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include "libgomp.h"
#include "proc.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_PTHREAD_AFFINITY_NP
#ifndef CPU_ALLOC_SIZE
#define CPU_ISSET_S(idx, size, set) CPU_ISSET(idx, set)
#define CPU_ZERO_S(size, set) CPU_ZERO(set)
#define CPU_SET_S(idx, size, set) CPU_SET(idx, set)
#define CPU_CLR_S(idx, size, set) CPU_CLR(idx, set)
#endif
void
gomp_init_affinity (void)
{
if (gomp_places_list == NULL)
{
if (!gomp_affinity_init_level (1, ULONG_MAX, true))
return;
}
struct gomp_thread *thr = gomp_thread ();
pthread_setaffinity_np (pthread_self (), gomp_cpuset_size,
(cpu_set_t *) gomp_places_list[0]);
thr->place = 1;
thr->ts.place_partition_off = 0;
thr->ts.place_partition_len = gomp_places_list_len;
}
void
gomp_init_thread_affinity (pthread_attr_t *attr, unsigned int place)
{
pthread_attr_setaffinity_np (attr, gomp_cpuset_size,
(cpu_set_t *) gomp_places_list[place]);
}
void **
gomp_affinity_alloc (unsigned long count, bool quiet)
{
unsigned long i;
void **ret;
char *p;
if (gomp_cpusetp == NULL)
{
if (!quiet)
gomp_error ("Could not get CPU affinity set");
return NULL;
}
ret = malloc (count * sizeof (void *) + count * gomp_cpuset_size);
if (ret == NULL)
{
if (!quiet)
gomp_error ("Out of memory trying to allocate places list");
return NULL;
}
p = (char *) (ret + count);
for (i = 0; i < count; i++, p += gomp_cpuset_size)
ret[i] = p;
return ret;
}
void
gomp_affinity_init_place (void *p)
{
cpu_set_t *cpusetp = (cpu_set_t *) p;
CPU_ZERO_S (gomp_cpuset_size, cpusetp);
}
bool
gomp_affinity_add_cpus (void *p, unsigned long num,
unsigned long len, long stride, bool quiet)
{
cpu_set_t *cpusetp = (cpu_set_t *) p;
unsigned long max = 8 * gomp_cpuset_size;
for (;;)
{
if (num >= max)
{
if (!quiet)
gomp_error ("Logical CPU number %lu out of range", num);
return false;
}
CPU_SET_S (num, gomp_cpuset_size, cpusetp);
if (--len == 0)
return true;
if ((stride < 0 && num + stride > num)
|| (stride > 0 && num + stride < num))
{
if (!quiet)
gomp_error ("Logical CPU number %lu+%ld out of range",
num, stride);
return false;
}
num += stride;
}
}
bool
gomp_affinity_remove_cpu (void *p, unsigned long num)
{
cpu_set_t *cpusetp = (cpu_set_t *) p;
if (num >= 8 * gomp_cpuset_size)
{
gomp_error ("Logical CPU number %lu out of range", num);
return false;
}
if (!CPU_ISSET_S (num, gomp_cpuset_size, cpusetp))
{
gomp_error ("Logical CPU %lu to be removed is not in the set", num);
return false;
}
CPU_CLR_S (num, gomp_cpuset_size, cpusetp);
return true;
}
bool
gomp_affinity_copy_place (void *p, void *q, long stride)
{
unsigned long i, max = 8 * gomp_cpuset_size;
cpu_set_t *destp = (cpu_set_t *) p;
cpu_set_t *srcp = (cpu_set_t *) q;
CPU_ZERO_S (gomp_cpuset_size, destp);
for (i = 0; i < max; i++)
if (CPU_ISSET_S (i, gomp_cpuset_size, srcp))
{
if ((stride < 0 && i + stride > i)
|| (stride > 0 && (i + stride < i || i + stride >= max)))
{
gomp_error ("Logical CPU number %lu+%ld out of range", i, stride);
return false;
}
CPU_SET_S (i + stride, gomp_cpuset_size, destp);
}
return true;
}
bool
gomp_affinity_same_place (void *p, void *q)
{
#ifdef CPU_EQUAL_S
return CPU_EQUAL_S (gomp_cpuset_size, (cpu_set_t *) p, (cpu_set_t *) q);
#else
return memcmp (p, q, gomp_cpuset_size) == 0;
#endif
}
bool
gomp_affinity_finalize_place_list (bool quiet)
{
unsigned long i, j;
for (i = 0, j = 0; i < gomp_places_list_len; i++)
{
cpu_set_t *cpusetp = (cpu_set_t *) gomp_places_list[i];
bool nonempty = false;
#ifdef CPU_AND_S
CPU_AND_S (gomp_cpuset_size, cpusetp, cpusetp, gomp_cpusetp);
nonempty = gomp_cpuset_popcount (gomp_cpuset_size, cpusetp) != 0;
#else
unsigned long k, max = gomp_cpuset_size / sizeof (cpusetp->__bits[0]);
for (k = 0; k < max; k++)
if ((cpusetp->__bits[k] &= gomp_cpusetp->__bits[k]) != 0)
nonempty = true;
#endif
if (nonempty)
gomp_places_list[j++] = gomp_places_list[i];
}
if (j == 0)
{
if (!quiet)
gomp_error ("None of the places contain usable logical CPUs");
return false;
}
else if (j < gomp_places_list_len)
{
if (!quiet)
gomp_error ("Number of places reduced from %ld to %ld because some "
"places didn't contain any usable logical CPUs",
gomp_places_list_len, j);
gomp_places_list_len = j;
}
return true;
}
bool
gomp_affinity_init_level (int level, unsigned long count, bool quiet)
{
unsigned long i, max = 8 * gomp_cpuset_size;
if (gomp_cpusetp)
{
unsigned long maxcount
= gomp_cpuset_popcount (gomp_cpuset_size, gomp_cpusetp);
if (count > maxcount)
count = maxcount;
}
gomp_places_list = gomp_affinity_alloc (count, quiet);
gomp_places_list_len = 0;
if (gomp_places_list == NULL)
return false;
/* SMT (threads). */
if (level == 1)
{
for (i = 0; i < max && gomp_places_list_len < count; i++)
if (CPU_ISSET_S (i, gomp_cpuset_size, gomp_cpusetp))
{
gomp_affinity_init_place (gomp_places_list[gomp_places_list_len]);
gomp_affinity_add_cpus (gomp_places_list[gomp_places_list_len],
i, 1, 0, true);
++gomp_places_list_len;
}
return true;
}
else
{
char name[sizeof ("/sys/devices/system/cpu/cpu/topology/"
"thread_siblings_list") + 3 * sizeof (unsigned long)];
size_t prefix_len = sizeof ("/sys/devices/system/cpu/cpu") - 1;
cpu_set_t *copy = gomp_alloca (gomp_cpuset_size);
FILE *f;
char *line = NULL;
size_t linelen = 0;
memcpy (name, "/sys/devices/system/cpu/cpu", prefix_len);
memcpy (copy, gomp_cpusetp, gomp_cpuset_size);
for (i = 0; i < max && gomp_places_list_len < count; i++)
if (CPU_ISSET_S (i, gomp_cpuset_size, copy))
{
sprintf (name + prefix_len, "%lu/topology/%s_siblings_list",
i, level == 2 ? "thread" : "core");
f = fopen (name, "r");
if (f != NULL)
{
if (getline (&line, &linelen, f) > 0)
{
char *p = line;
bool seen_i = false;
void *pl = gomp_places_list[gomp_places_list_len];
gomp_affinity_init_place (pl);
while (*p && *p != '\n')
{
unsigned long first, last;
errno = 0;
first = strtoul (p, &p, 10);
if (errno)
break;
last = first;
if (*p == '-')
{
errno = 0;
last = strtoul (p + 1, &p, 10);
if (errno || last < first)
break;
}
for (; first <= last; first++)
if (CPU_ISSET_S (first, gomp_cpuset_size, copy)
&& gomp_affinity_add_cpus (pl, first, 1, 0,
true))
{
CPU_CLR_S (first, gomp_cpuset_size, copy);
if (first == i)
seen_i = true;
}
if (*p == ',')
++p;
}
if (seen_i)
gomp_places_list_len++;
}
fclose (f);
}
}
if (gomp_places_list_len == 0)
{
if (!quiet)
gomp_error ("Error reading %s topology",
level == 2 ? "core" : "socket");
free (gomp_places_list);
gomp_places_list = NULL;
return false;
}
return true;
}
return false;
}
void
gomp_affinity_print_place (void *p)
{
unsigned long i, max = 8 * gomp_cpuset_size, len;
cpu_set_t *cpusetp = (cpu_set_t *) p;
bool notfirst = false;
for (i = 0, len = 0; i < max; i++)
if (CPU_ISSET_S (i, gomp_cpuset_size, cpusetp))
{
if (len == 0)
{
if (notfirst)
fputc (',', stderr);
notfirst = true;
fprintf (stderr, "%lu", i);
}
++len;
}
else
{
if (len > 1)
fprintf (stderr, ":%lu", len);
len = 0;
}
if (len > 1)
fprintf (stderr, ":%lu", len);
}
#else
#include "../posix/affinity.c"
#endif
|