Honor CPU counts from CFS. (#7654)
This commit is contained in:
51
src/common/threading_utils.cc
Normal file
51
src/common/threading_utils.cc
Normal file
@@ -0,0 +1,51 @@
|
||||
/*!
|
||||
* Copyright 2022 by XGBoost Contributors
|
||||
*/
|
||||
#include "threading_utils.h"
|
||||
#if defined(__linux__)
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif // defined(__linux__)
|
||||
#include "xgboost/logging.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
/**
|
||||
* \brief Get thread limit from CFS
|
||||
*
|
||||
* Modified from
|
||||
* github.com/psiha/sweater/blob/master/include/boost/sweater/hardware_concurrency.hpp
|
||||
*
|
||||
* MIT License: Copyright (c) 2016 Domagoj Šarić
|
||||
*/
|
||||
int32_t GetCfsCPUCount() noexcept {
|
||||
#if defined(__linux__)
|
||||
// https://bugs.openjdk.java.net/browse/JDK-8146115
|
||||
// http://hg.openjdk.java.net/jdk/hs/rev/7f22774a5f42
|
||||
// RAM limit /sys/fs/cgroup/memory.limit_in_bytes
|
||||
// swap limt /sys/fs/cgroup/memory.memsw.limit_in_bytes
|
||||
|
||||
auto read_int = [](char const* const file_path) noexcept {
|
||||
auto const fd(::open(file_path, O_RDONLY, 0));
|
||||
if (fd == -1) {
|
||||
return -1;
|
||||
}
|
||||
char value[64];
|
||||
CHECK(::read(fd, value, sizeof(value)) < signed(sizeof(value)));
|
||||
try {
|
||||
return std::stoi(value);
|
||||
} catch (std::exception const&) {
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
// complete fair scheduler from Linux
|
||||
auto const cfs_quota(read_int("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"));
|
||||
auto const cfs_period(read_int("/sys/fs/cgroup/cpu/cpu.cfs_period_us"));
|
||||
if ((cfs_quota > 0) && (cfs_period > 0)) {
|
||||
return std::max(cfs_quota / cfs_period, 1);
|
||||
}
|
||||
#endif // defined(__linux__)
|
||||
return -1;
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
@@ -1,7 +1,5 @@
|
||||
/*!
|
||||
* Copyright 2015-2019 by Contributors
|
||||
* \file common.h
|
||||
* \brief Threading utilities
|
||||
* Copyright 2019-2022 by XGBoost Contributors
|
||||
*/
|
||||
#ifndef XGBOOST_COMMON_THREADING_UTILS_H_
|
||||
#define XGBOOST_COMMON_THREADING_UTILS_H_
|
||||
@@ -238,6 +236,8 @@ inline int32_t OmpGetThreadLimit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
int32_t GetCfsCPUCount() noexcept;
|
||||
|
||||
inline int32_t OmpGetNumThreads(int32_t n_threads) {
|
||||
if (n_threads <= 0) {
|
||||
n_threads = std::min(omp_get_num_procs(), omp_get_max_threads());
|
||||
|
||||
Reference in New Issue
Block a user