mirror of
https://github.com/boostorg/build.git
synced 2026-02-17 13:42:14 +00:00
Default to available cpu threads for -j option.
This adds a `b2::system_info` class to obtain available information on system we are running in. Currently provides CPU counts. And currently only implemented for macOS.
This commit is contained in:
62
src/engine/sysinfo.cpp
Normal file
62
src/engine/sysinfo.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
/* Copyright 2019 Rene Rivera
|
||||
* Distributed under the Boost Software License, Version 1.0.
|
||||
* (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
|
||||
#include "sysinfo.h"
|
||||
#include "jam.h"
|
||||
#include "output.h"
|
||||
|
||||
#ifdef OS_MACOSX
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
|
||||
|
||||
b2::system_info::system_info()
|
||||
{
|
||||
}
|
||||
|
||||
unsigned int b2::system_info::cpu_core_count()
|
||||
{
|
||||
if (cpu_core_count_ == 0)
|
||||
{
|
||||
#ifdef 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)
|
||||
{
|
||||
cpu_core_count_ = out_hw_ncpu;
|
||||
}
|
||||
#endif
|
||||
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)
|
||||
{
|
||||
#ifdef 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)
|
||||
{
|
||||
cpu_thread_count_ = out_hw_ncpu;
|
||||
}
|
||||
#endif
|
||||
if (cpu_thread_count_ == 0)
|
||||
{
|
||||
cpu_thread_count_ = cpu_core_count();
|
||||
}
|
||||
}
|
||||
return cpu_thread_count_;
|
||||
}
|
||||
Reference in New Issue
Block a user