From 2aa2311a55872a9d68d4069f7024d173f81abd64 Mon Sep 17 00:00:00 2001 From: "Antonio S. Musumeci" Date: Thu, 9 Apr 2026 21:20:42 -0400 Subject: [PATCH] Add EMA smoothing and decline-streak to hill-climb monitor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A single noisy throughput sample would immediately flip the scaling direction, causing the monitor to oscillate under bursty workloads — continuously adding and removing threads every sample interval. Replace raw throughput comparison with an exponential moving average (alpha=0.3) and require two consecutive declining samples before reversing direction. This filters out transient dips and keeps the hill-climber from thrashing. Also use a 2-sample warmup period to seed the EMA before making any scaling decisions. Co-Authored-By: Claude Opus 4.6 (1M context) --- vendored/libfuse/include/thread_pool.hpp | 59 +++++++++++++++--------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/vendored/libfuse/include/thread_pool.hpp b/vendored/libfuse/include/thread_pool.hpp index 3f1a9c6e..1260820a 100644 --- a/vendored/libfuse/include/thread_pool.hpp +++ b/vendored/libfuse/include/thread_pool.hpp @@ -463,6 +463,8 @@ ThreadPool::start_routine_autoscale(void *arg_) // Hill-climbing monitor thread. // Periodically samples throughput, perturbs thread count by +/-1, // and moves in the direction that improves throughput. +// Uses an EMA to smooth throughput noise and requires two consecutive +// declining samples before reversing direction. inline void* ThreadPool::monitor_routine(void *arg_) @@ -471,9 +473,12 @@ ThreadPool::monitor_routine(void *arg_) const std::int64_t interval = btp->_scaling_config.sample_interval_usecs; int direction = 1; + int decline_streak = 0; std::uint64_t prev_completed = btp->_tasks_completed.load(std::memory_order_relaxed); - std::uint64_t prev_throughput = 0; - bool first_sample = true; + double ema_throughput = 0.0; + constexpr double ema_alpha = 0.3; // weight for new samples + constexpr int decline_threshold = 2; + int warmup_samples = 0; while(!btp->_stop.load(std::memory_order_acquire)) { @@ -483,20 +488,26 @@ ThreadPool::monitor_routine(void *arg_) break; std::uint64_t completed = btp->_tasks_completed.load(std::memory_order_relaxed); - std::uint64_t throughput = completed - prev_completed; + std::uint64_t raw_throughput = completed - prev_completed; prev_completed = completed; - if(first_sample) + // Warm up: seed the EMA with the first two samples before acting. + if(warmup_samples < 2) { - prev_throughput = throughput; - first_sample = false; + ema_throughput = (warmup_samples == 0) + ? (double)raw_throughput + : ema_alpha * (double)raw_throughput + (1.0 - ema_alpha) * ema_throughput; + ++warmup_samples; continue; } + double prev_ema = ema_throughput; + ema_throughput = ema_alpha * (double)raw_throughput + (1.0 - ema_alpha) * ema_throughput; + // Only adjust if there's meaningful work happening - if(throughput == 0 && prev_throughput == 0) + if(ema_throughput < 0.5 && prev_ema < 0.5) { - prev_throughput = throughput; + decline_streak = 0; continue; } @@ -506,15 +517,23 @@ ThreadPool::monitor_routine(void *arg_) count = btp->_threads.size(); } - // Hill-climb: did throughput improve since last adjustment? - if(throughput >= prev_throughput) + // Hill-climb: did smoothed throughput improve since last sample? + if(ema_throughput >= prev_ema) { - // Same direction is working - continue + decline_streak = 0; } else { - // Throughput dropped - reverse direction - direction = -direction; + ++decline_streak; + if(decline_streak >= decline_threshold) + { + direction = -direction; + decline_streak = 0; + } + else + { + continue; // wait for confirmation before reversing + } } if(direction > 0 && count < btp->_scaling_config.max_threads) @@ -524,11 +543,11 @@ ThreadPool::monitor_routine(void *arg_) std::memory_order_relaxed); syslog(LOG_DEBUG, "threadpool (%s): hill-climb grow to %zu threads " - "(throughput %lu -> %lu)", + "(ema %.1f -> %.1f)", btp->_name.c_str(), count + 1, - (unsigned long)prev_throughput, - (unsigned long)throughput); + prev_ema, + ema_throughput); } else if(direction < 0 && count > btp->_scaling_config.min_threads) { @@ -537,14 +556,12 @@ ThreadPool::monitor_routine(void *arg_) std::memory_order_relaxed); syslog(LOG_DEBUG, "threadpool (%s): hill-climb shrink to %zu threads " - "(throughput %lu -> %lu)", + "(ema %.1f -> %.1f)", btp->_name.c_str(), count - 1, - (unsigned long)prev_throughput, - (unsigned long)throughput); + prev_ema, + ema_throughput); } - - prev_throughput = throughput; } return nullptr;