summaryrefslogtreecommitdiffstats
path: root/src/boost/tools/build/src/engine/sysinfo.cpp
blob: 4aa4183da80b95241da1a92129d8c72871e41245 (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
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
/*  Copyright 2019 Rene Rivera
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
 */

#include "sysinfo.h"
#include "jam.h"
#include "output.h"

#include <thread>

#if defined(OS_MACOSX)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif

#if !defined(OS_NT)
#include <unistd.h>
#else
#include <windows.h>
#endif

#if defined(OS_LINUX) || defined(__GLIBC__)
// Need to define this in case it's not as that's the only way to get the
// sched_* APIs.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sched.h>
#endif


b2::system_info::system_info()
{
}

namespace
{
    unsigned int macosx_physicalcpu()
    {
        #if defined(OS_MACOSX)
        int out_hw_ncpu = 0;
        size_t len_hw_ncpu = sizeof(out_hw_ncpu);
        int result = ::sysctlbyname(
            "hw.physicalcpu", &out_hw_ncpu, &len_hw_ncpu, nullptr, 0);
        if (result == 0) return out_hw_ncpu;
        #endif
        return 0;
    }

    unsigned int macosx_logicalcpu()
    {
        #if defined(OS_MACOSX)
        int out_hw_ncpu = 0;
        size_t len_hw_ncpu = sizeof(out_hw_ncpu);
        int result = ::sysctlbyname(
            "hw.logicalcpu", &out_hw_ncpu, &len_hw_ncpu, nullptr, 0);
        if (result == 0) return out_hw_ncpu;
        #endif
        return 0;
    }

    unsigned int sched_affinity_cpu_count()
    {
        #if defined(CPU_COUNT_S)
        ::cpu_set_t cpu_set;
        if (::sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set) == 0)
        {
            return CPU_COUNT_S(sizeof(cpu_set_t), &cpu_set);
        }
        #endif
        return 0;
    }

    unsigned int sysconf_nprocs_configured()
    {
        #if defined(_SC_NPROCESSORS_ONLN)
        return ::sysconf(_SC_NPROCESSORS_CONF);
        #else
        return 0;
        #endif
    }

    unsigned int sysconf_nprocs_online()
    {
        #if defined(_SC_NPROCESSORS_ONLN)
        return ::sysconf(_SC_NPROCESSORS_ONLN);
        #else
        return 0;
        #endif
    }

    unsigned int std_thread_hardware_concurrency()
    {
        // [2020-08-19] Mingw-w64 (e.g. i686-w64-mingw-32-g++ from Cygwin,
        // g++-mingw-w64-i686-win32) does not have std::thread etc. But we
        // should still allow building the engine with this (important) toolset:
        // - It is free, lightweight, standards-conforming.
        // - It might be the only C++11 toolset for Windows XP.
        //   (Please see http://www.crouchingtigerhiddenfruitbat.org/Cygwin/timemachine.html !)
        // - It is powerful enough even without std::thread etc. For example, it
        //   can build-and-use Boost.Thread.
        // - The only thing currently used from std::thread is this call to
        //   hardware_concurrency !
        #if ! defined (_WIN32)
        return std::thread::hardware_concurrency();
        #else
        return 0;
        #endif
    }

    unsigned int win32_logicalcpu()
    {
        #if defined (_WIN32)
        SYSTEM_INFO si;
        GetSystemInfo (&si);
        return si.dwNumberOfProcessors;
        #else
        return 0;
        #endif
    }
}

unsigned int b2::system_info::cpu_core_count()
{
    if (cpu_core_count_ == 0)
    {
        cpu_thread_count_ = macosx_physicalcpu();
    }
    if (cpu_thread_count_ == 0)
    {
        cpu_thread_count_ = sysconf_nprocs_configured();
    }
    if (cpu_core_count_ <= 0)
    {
        cpu_core_count_ = 1;
    }
    return cpu_core_count_;
}

unsigned int b2::system_info::cpu_thread_count()
{
    if (cpu_thread_count_ == 0)
    {
        cpu_thread_count_ = macosx_logicalcpu();
    }
    if (cpu_thread_count_ == 0)
    {
        cpu_thread_count_ = sched_affinity_cpu_count();
    }
    if (cpu_thread_count_ == 0)
    {
        cpu_thread_count_ = sysconf_nprocs_online();
    }
    if (cpu_thread_count_ == 0)
    {
        cpu_thread_count_ = std_thread_hardware_concurrency();
    }
    if (cpu_thread_count_ == 0)
    {
        cpu_thread_count_ = win32_logicalcpu();
    }
    if (cpu_thread_count_ == 0)
    {
        cpu_thread_count_ = cpu_core_count();
    }
    return cpu_thread_count_;
}