Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions be/src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1597,14 +1597,15 @@ DEFINE_mInt64(max_csv_line_reader_output_buffer_size, "4294967296");
// -1 means auto: use 80% of the available CPU cores.
DEFINE_Int32(omp_threads_limit, "-1");
DEFINE_Validator(omp_threads_limit, [](const int config) -> bool {
if (config > 0) {
omp_threads_limit = config;
return true;
}
CpuInfo::init();
int core_cap = config::num_cores > 0 ? config::num_cores : CpuInfo::num_cores();
core_cap = std::max(1, core_cap);
int limit = config;
if (limit < 0) {
limit = std::max(1, core_cap * 4 / 5);
}
omp_threads_limit = std::max(1, std::min(limit, core_cap));
// Use at most 80% of the available CPU cores.
omp_threads_limit = std::max(1, core_cap * 4 / 5);
return true;
});
// The capacity of segment partial column cache, used to cache column readers for each segment.
Expand Down
46 changes: 46 additions & 0 deletions be/test/common/config_validator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

#include <gtest/gtest.h>

#include <algorithm>

#include "common/config.h"
#include "common/status.h"
#include "util/cpu_info.h"

namespace doris {
using namespace config;
Expand Down Expand Up @@ -46,4 +49,47 @@ TEST(ConfigValidatorTest, Validator) {
EXPECT_TRUE(s.ok());
EXPECT_EQ(cfg_validator_2, 8);
}

// When a positive value is specified, validator should use it directly.
TEST(ConfigValidatorTest, OmpThreadsLimitExplicitValue) {
int32_t original_limit = config::omp_threads_limit;

Status st = config::set_config("omp_threads_limit", "7", false, true);
ASSERT_TRUE(st.ok());
EXPECT_EQ(7, config::omp_threads_limit);

config::omp_threads_limit = original_limit;
}

// When value is -1 and config::num_cores is set,
// validator should pick 80% of that core count.
TEST(ConfigValidatorTest, OmpThreadsLimitAutoUsesConfiguredNumCores) {
int32_t original_limit = config::omp_threads_limit;
int32_t original_num_cores = config::num_cores;

config::num_cores = 20;
Status st = config::set_config("omp_threads_limit", "-1", false, true);
ASSERT_TRUE(st.ok());
EXPECT_EQ(16, config::omp_threads_limit);

config::num_cores = original_num_cores;
config::omp_threads_limit = original_limit;
}

// When value is -1 and config::num_cores is 0,
// validator should fall back to CpuInfo::num_cores().
TEST(ConfigValidatorTest, OmpThreadsLimitAutoFallsBackToCpuInfo) {
int32_t original_limit = config::omp_threads_limit;
int32_t original_num_cores = config::num_cores;

config::num_cores = 0;
Status st = config::set_config("omp_threads_limit", "-1", false, true);
ASSERT_TRUE(st.ok());
CpuInfo::init();
int expected = std::max(1, CpuInfo::num_cores() * 4 / 5);
EXPECT_EQ(expected, config::omp_threads_limit);

config::num_cores = original_num_cores;
config::omp_threads_limit = original_limit;
}
} // namespace doris
Loading