diff options
| author | qrort <[email protected]> | 2022-11-30 23:47:12 +0300 |
|---|---|---|
| committer | qrort <[email protected]> | 2022-11-30 23:47:12 +0300 |
| commit | 22f8ae0e3f5d68b92aecccdf96c1d841a0334311 (patch) | |
| tree | bffa27765faf54126ad44bcafa89fadecb7a73d7 /library/cpp/cpulimit/cpu.h | |
| parent | 332b99e2173f0425444abb759eebcb2fafaa9209 (diff) | |
validate canons without yatest_common
Diffstat (limited to 'library/cpp/cpulimit/cpu.h')
| -rw-r--r-- | library/cpp/cpulimit/cpu.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/library/cpp/cpulimit/cpu.h b/library/cpp/cpulimit/cpu.h new file mode 100644 index 00000000000..eb5ee9b011e --- /dev/null +++ b/library/cpp/cpulimit/cpu.h @@ -0,0 +1,59 @@ +#pragma once + +#include "cpu.h" + +#include <util/datetime/base.h> +#include <util/system/types.h> +#include <util/thread/factory.h> + +#include <deque> + +namespace NCpuLimit { + class TCpuMeasurer { + struct TProbe { + TDuration Period; + std::atomic<double> Usage; + THolder<IThreadFactory::IThread> MeasurerThread; + std::deque<std::pair<TInstant, TDuration>> Window; + TDuration WindowDuration{}; + TDuration WindowUsage{}; + }; + + public: + explicit TCpuMeasurer(TDuration probePeriod); + ~TCpuMeasurer(); + + double CpuUsageFast() const { + return FastProbe_.Usage.load(); + } + + double CpuUsageSlow() const { + return SlowProbe_.Usage.load(); + } + + private: + void UpdateProbeThread(TProbe& probe); + + std::atomic<bool> Finished_ = false; + + TProbe FastProbe_; + TProbe SlowProbe_; + }; + + class TCpuLimiter { + public: + TCpuLimiter(double slowThreshold, double fastThresholdBegin, double fastThresholdEnd); + + // Only throttle (all requests) when LA is greater than FastThresholdEnd_ + bool ThrottleSoft(double slowUsage, double fastUsage) const; + + // Throttle requests with some probability when LA is greater than + // FastThresholdBegin_ + bool ThrottleHard(double slowUsage, double fastUsage) const; + + private: + double SlowThreshold_; + double FastThresholdBegin_; + double FastThresholdEnd_; + }; +} |
