]>
git.proxmox.com Git - ceph.git/blob - 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)
12 #if defined(OS_MACOSX)
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
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
33 b2::system_info::system_info()
39 unsigned int macosx_physicalcpu()
41 #if defined(OS_MACOSX)
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
;
51 unsigned int macosx_logicalcpu()
53 #if defined(OS_MACOSX)
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
;
63 unsigned int sched_affinity_cpu_count()
65 #if defined(CPU_COUNT_S)
67 if (::sched_getaffinity(0, sizeof(cpu_set_t
), &cpu_set
) == 0)
69 return CPU_COUNT_S(sizeof(cpu_set_t
), &cpu_set
);
75 unsigned int sysconf_nprocs_configured()
77 #if defined(_SC_NPROCESSORS_ONLN)
78 return ::sysconf(_SC_NPROCESSORS_CONF
);
84 unsigned int sysconf_nprocs_online()
86 #if defined(_SC_NPROCESSORS_ONLN)
87 return ::sysconf(_SC_NPROCESSORS_ONLN
);
93 unsigned int std_thread_hardware_concurrency()
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();
112 unsigned int win32_logicalcpu()
117 return si
.dwNumberOfProcessors
;
124 unsigned int b2::system_info::cpu_core_count()
126 if (cpu_core_count_
== 0)
128 cpu_thread_count_
= macosx_physicalcpu();
130 if (cpu_thread_count_
== 0)
132 cpu_thread_count_
= sysconf_nprocs_configured();
134 if (cpu_core_count_
<= 0)
138 return cpu_core_count_
;
141 unsigned int b2::system_info::cpu_thread_count()
143 if (cpu_thread_count_
== 0)
145 cpu_thread_count_
= macosx_logicalcpu();
147 if (cpu_thread_count_
== 0)
149 cpu_thread_count_
= sched_affinity_cpu_count();
151 if (cpu_thread_count_
== 0)
153 cpu_thread_count_
= sysconf_nprocs_online();
155 if (cpu_thread_count_
== 0)
157 cpu_thread_count_
= std_thread_hardware_concurrency();
159 if (cpu_thread_count_
== 0)
161 cpu_thread_count_
= win32_logicalcpu();
163 if (cpu_thread_count_
== 0)
165 cpu_thread_count_
= cpu_core_count();
167 return cpu_thread_count_
;