]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/build/src/engine/sysinfo.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / tools / build / src / engine / sysinfo.cpp
1 /* Copyright 2019 Rene Rivera
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
4 */
5
6 #include "sysinfo.h"
7 #include "jam.h"
8 #include "output.h"
9
10 #include <thread>
11
12 #if defined(OS_MACOSX)
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
15 #endif
16
17 #if !defined(OS_NT)
18 #include <unistd.h>
19 #else
20 #include <windows.h>
21 #endif
22
23 #if defined(OS_LINUX) || defined(__GLIBC__)
24 // Need to define this in case it's not as that's the only way to get the
25 // sched_* APIs.
26 #ifndef _GNU_SOURCE
27 #define _GNU_SOURCE
28 #endif
29 #include <sched.h>
30 #endif
31
32
33 b2::system_info::system_info()
34 {
35 }
36
37 namespace
38 {
39 unsigned int macosx_physicalcpu()
40 {
41 #if defined(OS_MACOSX)
42 int out_hw_ncpu = 0;
43 size_t len_hw_ncpu = sizeof(out_hw_ncpu);
44 int result = ::sysctlbyname(
45 "hw.physicalcpu", &out_hw_ncpu, &len_hw_ncpu, nullptr, 0);
46 if (result == 0) return out_hw_ncpu;
47 #endif
48 return 0;
49 }
50
51 unsigned int macosx_logicalcpu()
52 {
53 #if defined(OS_MACOSX)
54 int out_hw_ncpu = 0;
55 size_t len_hw_ncpu = sizeof(out_hw_ncpu);
56 int result = ::sysctlbyname(
57 "hw.logicalcpu", &out_hw_ncpu, &len_hw_ncpu, nullptr, 0);
58 if (result == 0) return out_hw_ncpu;
59 #endif
60 return 0;
61 }
62
63 unsigned int sched_affinity_cpu_count()
64 {
65 #if defined(CPU_COUNT_S)
66 ::cpu_set_t cpu_set;
67 if (::sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set) == 0)
68 {
69 return CPU_COUNT_S(sizeof(cpu_set_t), &cpu_set);
70 }
71 #endif
72 return 0;
73 }
74
75 unsigned int sysconf_nprocs_configured()
76 {
77 #if defined(_SC_NPROCESSORS_ONLN)
78 return ::sysconf(_SC_NPROCESSORS_CONF);
79 #else
80 return 0;
81 #endif
82 }
83
84 unsigned int sysconf_nprocs_online()
85 {
86 #if defined(_SC_NPROCESSORS_ONLN)
87 return ::sysconf(_SC_NPROCESSORS_ONLN);
88 #else
89 return 0;
90 #endif
91 }
92
93 unsigned int std_thread_hardware_concurrency()
94 {
95 // [2020-08-19] Mingw-w64 (e.g. i686-w64-mingw-32-g++ from Cygwin,
96 // g++-mingw-w64-i686-win32) does not have std::thread etc. But we
97 // should still allow building the engine with this (important) toolset:
98 // - It is free, lightweight, standards-conforming.
99 // - It might be the only C++11 toolset for Windows XP.
100 // (Please see http://www.crouchingtigerhiddenfruitbat.org/Cygwin/timemachine.html !)
101 // - It is powerful enough even without std::thread etc. For example, it
102 // can build-and-use Boost.Thread.
103 // - The only thing currently used from std::thread is this call to
104 // hardware_concurrency !
105 #if ! defined (_WIN32)
106 return std::thread::hardware_concurrency();
107 #else
108 return 0;
109 #endif
110 }
111
112 unsigned int win32_logicalcpu()
113 {
114 #if defined (_WIN32)
115 SYSTEM_INFO si;
116 GetSystemInfo (&si);
117 return si.dwNumberOfProcessors;
118 #else
119 return 0;
120 #endif
121 }
122 }
123
124 unsigned int b2::system_info::cpu_core_count()
125 {
126 if (cpu_core_count_ == 0)
127 {
128 cpu_thread_count_ = macosx_physicalcpu();
129 }
130 if (cpu_thread_count_ == 0)
131 {
132 cpu_thread_count_ = sysconf_nprocs_configured();
133 }
134 if (cpu_core_count_ <= 0)
135 {
136 cpu_core_count_ = 1;
137 }
138 return cpu_core_count_;
139 }
140
141 unsigned int b2::system_info::cpu_thread_count()
142 {
143 if (cpu_thread_count_ == 0)
144 {
145 cpu_thread_count_ = macosx_logicalcpu();
146 }
147 if (cpu_thread_count_ == 0)
148 {
149 cpu_thread_count_ = sched_affinity_cpu_count();
150 }
151 if (cpu_thread_count_ == 0)
152 {
153 cpu_thread_count_ = sysconf_nprocs_online();
154 }
155 if (cpu_thread_count_ == 0)
156 {
157 cpu_thread_count_ = std_thread_hardware_concurrency();
158 }
159 if (cpu_thread_count_ == 0)
160 {
161 cpu_thread_count_ = win32_logicalcpu();
162 }
163 if (cpu_thread_count_ == 0)
164 {
165 cpu_thread_count_ = cpu_core_count();
166 }
167 return cpu_thread_count_;
168 }