summaryrefslogtreecommitdiffstats
path: root/Source/cmAffinity.cxx
blob: a6d89aaea13f152e8cab8bfb6fa429a1ae0ad1d4 (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
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmAffinity.h"

#include "cm_uv.h"

#ifndef CMAKE_USE_SYSTEM_LIBUV
#  ifdef _WIN32
#    define CM_HAVE_CPU_AFFINITY
#    include <windows.h>
#  elif defined(__linux__) || defined(__FreeBSD__)
#    define CM_HAVE_CPU_AFFINITY
#    include <pthread.h>
#    include <sched.h>
// On some platforms CPU_ZERO needs memset but sched.h forgets string.h
#    include <string.h> // IWYU pragma: keep
#    if defined(__FreeBSD__)
#      include <pthread_np.h>
#      include <sys/cpuset.h>
#      include <sys/param.h>
#    endif
#    if defined(__linux__)
using cm_cpuset_t = cpu_set_t;
#    else
using cm_cpuset_t = cpuset_t;
#    endif
#  endif
#endif

namespace cmAffinity {

std::set<size_t> GetProcessorsAvailable()
{
  std::set<size_t> processorsAvailable;
#ifdef CM_HAVE_CPU_AFFINITY
  int cpumask_size = uv_cpumask_size();
  if (cpumask_size > 0) {
#  ifdef _WIN32
    DWORD_PTR procmask;
    DWORD_PTR sysmask;
    if (GetProcessAffinityMask(GetCurrentProcess(), &procmask, &sysmask) !=
        0) {
      for (int i = 0; i < cpumask_size; ++i) {
        if (procmask & (((DWORD_PTR)1) << i)) {
          processorsAvailable.insert(i);
        }
      }
    }
#  else
    cm_cpuset_t cpuset;
    CPU_ZERO(&cpuset); // NOLINT(clang-tidy)
    if (pthread_getaffinity_np(pthread_self(), sizeof(cpuset), &cpuset) == 0) {
      for (int i = 0; i < cpumask_size; ++i) {
        if (CPU_ISSET(i, &cpuset)) {
          processorsAvailable.insert(i);
        }
      }
    }
#  endif
  }
#endif
  return processorsAvailable;
}
}