Line data Source code
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 132 : Stats::Scope& stats) { 16 132 : const auto action_name = Server::OverloadActionNames::get().ShrinkHeap; 17 132 : if (overload_manager.registerForAction( 18 132 : action_name, dispatcher, 19 132 : [this](Server::OverloadActionState state) { active_ = state.isSaturated(); })) { 20 0 : Envoy::Stats::StatNameManagedStorage stat_name( 21 0 : absl::StrCat("overload.", action_name, ".shrink_count"), stats.symbolTable()); 22 0 : shrink_counter_ = &stats.counterFromStatName(stat_name.statName()); 23 0 : timer_ = dispatcher.createTimer([this] { 24 0 : shrinkHeap(); 25 0 : timer_->enableTimer(kTimerInterval); 26 0 : }); 27 0 : timer_->enableTimer(kTimerInterval); 28 0 : } 29 132 : } 30 : 31 0 : void HeapShrinker::shrinkHeap() { 32 0 : if (active_) { 33 0 : Utils::releaseFreeMemory(); 34 0 : shrink_counter_->inc(); 35 0 : } 36 0 : } 37 : 38 : } // namespace Memory 39 : } // namespace Envoy