Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
Doris-Extras committed Sep 21, 2024
1 parent cca827d commit ab40b18
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
12 changes: 4 additions & 8 deletions be/src/common/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "runtime/memory/memory_reclamation.h"
#include "runtime/runtime_query_statistics_mgr.h"
#include "runtime/workload_group/workload_group_manager.h"
#include "util/algorithm_util.h"
#include "util/cpu_info.h"
#include "util/debug_util.h"
#include "util/disk_info.h"
Expand Down Expand Up @@ -242,21 +243,16 @@ void refresh_memory_state_after_memory_change() {

void refresh_cache_capacity() {
if (refresh_cache_capacity_sleep_time_ms <= 0) {
auto cache_capacity_reduce_mem_limit = uint64_t(
auto cache_capacity_reduce_mem_limit = int64_t(
doris::MemInfo::soft_mem_limit() * config::cache_capacity_reduce_mem_limit_frac);
int64_t process_memory_usage = doris::GlobalMemoryArbitrator::process_memory_usage();
// the rule is like this:
// 1. if the process mem usage < soft memlimit * 0.6, then do not need adjust cache capacity.
// 2. if the process mem usage > soft memlimit * 0.6 and process mem usage < soft memlimit, then it will be adjusted to a lower value.
// 3. if the process mem usage > soft memlimit, then the capacity is adjusted to 0.
double new_cache_capacity_adjust_weighted =
process_memory_usage <= cache_capacity_reduce_mem_limit
? 1
: std::max<double>(
1 - (process_memory_usage - cache_capacity_reduce_mem_limit) /
(doris::MemInfo::soft_mem_limit() -
cache_capacity_reduce_mem_limit),
0);
AlgoUtil::descent_by_step(10, cache_capacity_reduce_mem_limit,
doris::MemInfo::soft_mem_limit(), process_memory_usage);
if (new_cache_capacity_adjust_weighted !=
doris::GlobalMemoryArbitrator::last_cache_capacity_adjust_weighted) {
doris::GlobalMemoryArbitrator::last_cache_capacity_adjust_weighted =
Expand Down
12 changes: 11 additions & 1 deletion be/src/util/algorithm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
namespace doris {
class AlgoUtil {
// descent the value step by step not linear continuity
double descent_by_step(int step_num, int64_t low_bound, int64_t high_bound, int64_t current) {
// If the result is linear continuity, then the value will changed very quickly and will cost
// a lot of CPU and cache will not stable and will hold some lock.
// Its better to use step num to be 10, do not use 3, the divide value is not stable.
// For example, if step num is 3, then the result will be 0.33333... 0.66666..., the double value
// is not stable.
static double descent_by_step(int step_num, int64_t low_bound, int64_t high_bound,
int64_t current) {
if (current <= low_bound) {
return 1;
}
Expand All @@ -35,8 +41,12 @@ class AlgoUtil {
// Invalid
return 0;
}
// Use floor value, so that the step size is a little smaller than the actual value.
// And then the used step will be a little larger than the actual value.
int64_t step_size = (int64_t)std::floor((high_bound - low_bound) / step_num);
int64_t used_step = (int64_t)std::ceil((current - low_bound) / step_size);
// Then the left step is smaller than actual value.
// This elimation algo will elimate more cache than actual.
int64_t left_step = step_num - used_step;
return left_step / step_num;
}
Expand Down
5 changes: 5 additions & 0 deletions be/test/util/algo_util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ TEST(TestAlgoUtil, DescentByStep) {
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 100), 1);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 200), 0);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 300), 0);

EXPECT_EQ(AlgoUtil::descent_by_step(4, 100, 200, 133), 0.5);
EXPECT_EQ(AlgoUtil::descent_by_step(4, 100, 200, 110), 0.75);
EXPECT_EQ(AlgoUtil::descent_by_step(4, 100, 200, 125), 0.75);
EXPECT_EQ(AlgoUtil::descent_by_step(4, 100, 200, 126), 0.5);
}

} // namespace doris

0 comments on commit ab40b18

Please sign in to comment.