SwapGrowthGate class
#include <gtopt/work_pool.hpp>
Decides whether the process-VmSwap dispatch gate is engaged.
VmSwap (from /proc/self/status) counts pages that ARE on swap, not pages being moved. Cold pages the kernel evicted (because of other load on the host, vm.swappiness, ...) sit there forever with zero swap I/O, so a bare VmSwap >= limit test latches closed: nothing the pool does can bring the number back down, and the pool crawls at the one always-admitted task for the rest of the run (observed in production: Active: 1/31, VmSwap 3343 MB >= 2048 MB for over an hour on a host with ~120 GB available and 0 pg/s swap I/O).
The gate therefore engages only while the process is ACTIVELY being pushed to swap:
engaged <=> VmSwap >= limit AND VmSwap grew by more than
`growth_mb` over the last observation window
Growth is measured against the window's MINIMUM sample, so a churn pattern (pages faulted back in, then evicted again) is detected the same way as monotonic growth. The window restarts on every detected burst, so release needs one full quiet window — that is the only hysteresis. When updates arrive late (the pool was idle or saturated and nobody consulted the gate), the growth threshold scales with the elapsed time so the decision stays a RATE test (growth_mb per window) rather than "64 MB at any point in the last ten minutes".
Why process VmSwap growth rather than the system swap I/O rate (pswpin + pswpout from /proc/vmstat): the I/O counters are host-wide, so on a shared box another tenant's paging would throttle this process (the same collective-throttle failure that got the load-average gate removed), and they also count benign swap-IN. VmSwap growth is scoped to this process and means exactly "our pages
are being evicted right now", which is the only condition under which holding back new tasks helps. The host-wide rate stays available as the separate, opt-in max_swap_io_per_sec gate.
Pure state machine: no clock or /proc access of its own — the caller feeds (VmSwap, now) samples — so tests drive it deterministically. Not thread-safe; the pool calls it under queue_mutex_.
Public types
- using clock = std::chrono::steady_clock
- using duration = clock::duration
- using time_point = clock::time_point
- enum class Transition: std::uint8_t { none, engaged, released }
- What happened to the gate state on an
update().
Constructors, destructors, conversion operators
- SwapGrowthGate() defaulted constexpr noexcept
- SwapGrowthGate(double max_swap_mb, double growth_mb, duration window) constexpr noexcept
Public functions
- auto engaged() const -> bool noexcept
- True while dispatch must be throttled (over the limit AND growing).
- auto growing() const -> bool noexcept
- auto growth_mb() const -> double noexcept
- auto growth_seen_mb() const -> double noexcept
- Growth (MB) measured by the most recent window decision.
- auto max_swap_mb() const -> double noexcept
-
auto update(double vmswap_mb,
time_
point now) -> Transition noexcept - Feed one
(VmSwap, time)sample; returns the resulting transition. - auto vmswap_mb() const -> double noexcept
- Last VmSwap sample (MB).
- auto window() const -> duration noexcept
Enum documentation
enum class gtopt:: SwapGrowthGate:: Transition: std::uint8_t
What happened to the gate state on an update().
| Enumerators | |
|---|---|
| none |
State unchanged. |
| engaged |
Gate just closed (throttling starts). |
| released |
Gate just opened (throttling stops). |
Function documentation
bool gtopt:: SwapGrowthGate:: growing() const noexcept
True while VmSwap is growing faster than growth_mb per window, regardless of the absolute limit.