1
#include "source/common/memory/heap_shrinker.h"
2

            
3
#include "source/common/memory/utils.h"
4
#include "source/common/stats/symbol_table.h"
5

            
6
#include "absl/strings/str_cat.h"
7

            
8
namespace Envoy {
9
namespace Memory {
10

            
11
// TODO(eziskind): make this configurable
12
constexpr std::chrono::milliseconds kTimerInterval = std::chrono::milliseconds(10000);
13

            
14
HeapShrinker::HeapShrinker(Event::Dispatcher& dispatcher, Server::OverloadManager& overload_manager,
15
10673
                           Stats::Scope& stats) {
16
10673
  const auto action_name = Server::OverloadActionNames::get().ShrinkHeap;
17
10673
  if (overload_manager.registerForAction(
18
10673
          action_name, dispatcher,
19
10673
          [this](Server::OverloadActionState state) { active_ = state.isSaturated(); })) {
20
1
    Envoy::Stats::StatNameManagedStorage stat_name(
21
1
        absl::StrCat("overload.", action_name, ".shrink_count"), stats.symbolTable());
22
1
    shrink_counter_ = &stats.counterFromStatName(stat_name.statName());
23
4
    timer_ = dispatcher.createTimer([this] {
24
4
      shrinkHeap();
25
4
      timer_->enableTimer(kTimerInterval);
26
4
    });
27
1
    timer_->enableTimer(kTimerInterval);
28
1
  }
29
10673
}
30

            
31
4
void HeapShrinker::shrinkHeap() {
32
4
  if (active_) {
33
2
    Utils::releaseFreeMemory();
34
2
    shrink_counter_->inc();
35
2
  }
36
4
}
37

            
38
} // namespace Memory
39
} // namespace Envoy