Skip to content

Commit bc714bf

Browse files
committed
Implement UNIX-style load averages
The existing `Scheduler#load` uses a 1-second time window, which is good for fast reactivity for things like `Idler`, but less good if you want it as a metric for health/autoscaling. Additionally, it performed the 1s time window normalization directly, which has the downside that how often you called `#load` could change how quickly the load values would update. Introduce a 1-minute, 5-minute, and 15-minute time window exponentially- weighted moving load average, similar to traditional UNIX getloadavg(3). To avoid expensive math on every fiber switch, we update these averages at most once a second. This same 1 second buffer is now used by `#load` which becomes fully read-only.
1 parent e8ecf58 commit bc714bf

3 files changed

Lines changed: 120 additions & 20 deletions

File tree

‎lib/async/scheduler.rb‎

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class Scheduler < Node
3232
value == "true" ? true : nil
3333
end
3434

35+
# The periods over which exponentially weighted load averages are computed, in seconds.
36+
LOAD_AVERAGE_PERIODS = [60.0, 5 * 60.0, 15 * 60.0].freeze
37+
38+
# How much run loop time to accumulate before updating the load averages.
39+
LOAD_AVERAGE_UPDATE_INTERVAL = 1.0
40+
private_constant :LOAD_AVERAGE_UPDATE_INTERVAL
41+
3542
# Raised when an operation is attempted on a closed scheduler.
3643
class ClosedError < RuntimeError
3744
# Create a new error.
@@ -85,8 +92,10 @@ def initialize(parent = nil, selector: nil, profiler: Profiler&.default, worker_
8592

8693
@blocked = 0
8794

88-
@busy_time = 0.0
89-
@idle_time = 0.0
95+
@load = 0.0
96+
@load_average = Array.new(LOAD_AVERAGE_PERIODS.size, 0.0).freeze
97+
@load_busy_time = 0.0
98+
@load_total_time = 0.0
9099

91100
@timers = ::IO::Event::Timers.new
92101

@@ -105,24 +114,44 @@ def initialize(parent = nil, selector: nil, profiler: Profiler&.default, worker_
105114
#
106115
# @returns [Float] The load of the scheduler. 0.0 means no load, 1.0 means fully loaded or over-loaded.
107116
def load
108-
total_time = @busy_time + @idle_time
117+
# Weight the last completed window by the portion of the update interval that the current window has not yet covered:
118+
remaining = LOAD_AVERAGE_UPDATE_INTERVAL - @load_total_time
109119

110-
# If the total time is zero, then the load is zero:
111-
return 0.0 if total_time.zero?
112-
113-
# We normalize to a 1 second window:
114-
if total_time > 1.0
115-
ratio = 1.0 / total_time
116-
@busy_time *= ratio
117-
@idle_time *= ratio
118-
119-
# We don't need to divide here as we've already normalised it to a 1s window:
120-
return @busy_time
120+
if remaining > 0.0
121+
return (@load * remaining + @load_busy_time) / LOAD_AVERAGE_UPDATE_INTERVAL
121122
else
122-
return @busy_time / total_time
123+
return @load_busy_time / @load_total_time
123124
end
124125
end
125126

127+
# The scheduler load averaged over one, five, and fifteen minutes.
128+
#
129+
# @public Since *Async v2.46*.
130+
# @returns [Array(Float)] The scheduler load averages.
131+
def load_averages
132+
return @load_average
133+
end
134+
135+
private def update_load_average(busy_duration, total_duration)
136+
return if total_duration <= 0.0
137+
138+
@load_busy_time += busy_duration
139+
@load_total_time += total_duration
140+
return if @load_total_time < LOAD_AVERAGE_UPDATE_INTERVAL
141+
142+
total_time = @load_total_time
143+
load = @load_busy_time / total_time
144+
@load = load
145+
146+
@load_average = LOAD_AVERAGE_PERIODS.map.with_index do |period, index|
147+
decay = Math.exp(-total_time / period)
148+
@load_average[index] * decay + load * (1.0 - decay)
149+
end.freeze
150+
151+
@load_busy_time = 0.0
152+
@load_total_time = 0.0
153+
end
154+
126155
# Invoked when the fiber scheduler is being closed.
127156
#
128157
# Executes the run loop until all tasks are finished, then closes the scheduler.
@@ -483,8 +512,7 @@ def io_select(...)
483512
idle_duration = @selector.idle_duration
484513
busy_duration = total_duration - idle_duration
485514

486-
@busy_time += busy_duration
487-
@idle_time += idle_duration
515+
update_load_average(busy_duration, total_duration)
488516

489517
# The reactor still has work to do:
490518
return true

‎releases.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Added `Async::Scheduler#load_averages`, which returns one, five, and fifteen minute exponentially weighted moving averages of the scheduler load, suitable for monitoring and autoscaling signals.
6+
- `Async::Scheduler#load` now reports the load over a sliding one-second window and no longer mutates internal state, so reading it has no side effects.
7+
38
## v2.45.1
49

510
- Fixed `Scheduler#io_wait` returning `nil` instead of `false` when an explicit timeout expired. Native callers such as `Socket#connect` with `connect_timeout:` distinguish a timeout by checking for `false`, so the `nil` caused `TypeError: no implicit conversion from nil to integer` instead of the intended `IO::TimeoutError`.

‎test/async/scheduler.rb‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,83 @@
7272
end
7373

7474
with "#load" do
75-
it "normalizes load over a one second window" do
75+
it "returns the load over the last update window" do
7676
scheduler = Async::Scheduler.new
7777

78-
scheduler.instance_variable_set(:@busy_time, 2.0)
79-
scheduler.instance_variable_set(:@idle_time, 2.0)
78+
scheduler.send(:update_load_average, 30.0, 60.0)
8079

8180
expect(scheduler.load).to be == 0.5
8281
ensure
8382
scheduler&.close
8483
end
84+
85+
it "blends the current window into the last completed window" do
86+
scheduler = Async::Scheduler.new
87+
88+
# Complete a fully loaded window:
89+
scheduler.send(:update_load_average, 1.0, 1.0)
90+
expect(scheduler.load).to be == 1.0
91+
92+
# Half of the next window elapses fully idle:
93+
scheduler.send(:update_load_average, 0.0, 0.5)
94+
expect(scheduler.load).to be == 0.5
95+
ensure
96+
scheduler&.close
97+
end
98+
99+
it "reflects a partial window before the first update" do
100+
scheduler = Async::Scheduler.new
101+
102+
scheduler.send(:update_load_average, 0.25, 0.5)
103+
104+
expect(scheduler.load).to be == 0.25
105+
ensure
106+
scheduler&.close
107+
end
108+
109+
it "does not change based on how often it is read" do
110+
scheduler = Async::Scheduler.new
111+
scheduler.send(:update_load_average, 30.0, 60.0)
112+
113+
load = scheduler.load
114+
3.times do
115+
expect(scheduler.load).to be == load
116+
end
117+
ensure
118+
scheduler&.close
119+
end
120+
end
121+
122+
with "#load_averages" do
123+
it "batches updates over one second" do
124+
scheduler = Async::Scheduler.new
125+
scheduler.send(:update_load_average, 0.25, 0.5)
126+
127+
expect(scheduler.load_averages).to be == [0.0, 0.0, 0.0]
128+
129+
scheduler.send(:update_load_average, 0.5, 0.5)
130+
131+
expected = Async::Scheduler::LOAD_AVERAGE_PERIODS.map do |period|
132+
0.75 * (1.0 - Math.exp(-1.0 / period))
133+
end
134+
135+
expect(scheduler.load_averages).to be == expected
136+
ensure
137+
scheduler&.close
138+
end
139+
140+
it "tracks one, five, and fifteen minute exponentially weighted averages" do
141+
scheduler = Async::Scheduler.new
142+
scheduler.send(:update_load_average, 60.0, 60.0)
143+
144+
expected = Async::Scheduler::LOAD_AVERAGE_PERIODS.map do |period|
145+
1.0 - Math.exp(-60.0 / period)
146+
end
147+
148+
expect(scheduler.load_averages).to be == expected
149+
ensure
150+
scheduler&.close
151+
end
85152
end
86153

87154
with "#close" do

0 commit comments

Comments
 (0)