mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-05-16 15:29:41 +00:00
Merge branch 'master' into feat/env-port-override
This commit is contained in:
commit
6a6c2093c8
@ -1,58 +1,78 @@
|
|||||||
{.push raises: [].}
|
{.push raises: [].}
|
||||||
|
|
||||||
|
import std/math
|
||||||
import chronos, chronicles, metrics
|
import chronos, chronicles, metrics
|
||||||
|
|
||||||
logScope:
|
logScope:
|
||||||
topics = "waku event_loop_monitor"
|
topics = "waku event_loop_monitor"
|
||||||
|
|
||||||
const CheckInterval = 5.seconds
|
declarePublicGauge event_loop_load,
|
||||||
|
"chronos event loop load EWMA by window (1.0 = sustained lag at MaxAcceptedLag)",
|
||||||
declarePublicGauge event_loop_lag_seconds,
|
labels = ["window"]
|
||||||
"chronos event loop lag in seconds: difference between actual and expected wake-up interval"
|
|
||||||
|
|
||||||
type OnLagChange* = proc(lagTooHigh: bool) {.gcsafe, raises: [].}
|
type OnLagChange* = proc(lagTooHigh: bool) {.gcsafe, raises: [].}
|
||||||
|
|
||||||
proc eventLoopMonitorLoop*(onLagChange: OnLagChange = nil) {.async.} =
|
proc eventLoopMonitorLoop*(onLagChange: OnLagChange = nil) {.async.} =
|
||||||
## Monitors chronos event loop responsiveness.
|
## Monitors chronos event loop responsiveness by measuring how much each
|
||||||
|
## iteration oversleeps its `CheckInterval`.
|
||||||
##
|
##
|
||||||
## Schedules a task every `CheckInterval`. Because chronos is single-threaded
|
## The lag is normalised against `MaxAcceptedLag` and tracked as an EWMA
|
||||||
## and cooperative, the task can only resume after all previously queued work
|
## over 1, 5, and 15-minute windows (Unix load-average decay model),
|
||||||
## completes. The actual elapsed time between iterations therefore reflects
|
## exposed via the `event_loop_load` gauge (labelled by window: 1m/5m/15m):
|
||||||
## how saturated the event loop is:
|
|
||||||
##
|
##
|
||||||
## actual_elapsed ≈ CheckInterval → loop is healthy
|
## load < 1.0 → within budget
|
||||||
## actual_elapsed >> CheckInterval → tasks are accumulating / loop is stalling
|
## load = 1.0 → sustained lag at MaxAcceptedLag (fully loaded)
|
||||||
|
## load > 1.0 → over budget; e.g. 2.0 means twice the accepted lag
|
||||||
##
|
##
|
||||||
## The lag (actual - expected) is exposed via `event_loop_lag_seconds`.
|
## `onLagChange` is called when instantaneous lag crosses `MaxAcceptedLag`.
|
||||||
## When lag transitions above or below `CheckInterval`, `onLagChange` is called.
|
|
||||||
|
|
||||||
var lastWakeup = Moment.now()
|
const CheckInterval = 5.seconds
|
||||||
|
const MaxAcceptedLag = 50.milliseconds
|
||||||
|
|
||||||
|
# Decay factors: α = 1 − e^(−CheckInterval_secs / window_secs)
|
||||||
|
# Mirrors the Unix load-average convention so each EWMA has a half-life equal
|
||||||
|
# to its named window.
|
||||||
|
const alpha1m = 1.0 - exp(-5.0 / 60.0) # ≈ 0.0821
|
||||||
|
const alpha5m = 1.0 - exp(-5.0 / 300.0) # ≈ 0.0165
|
||||||
|
const alpha15m = 1.0 - exp(-5.0 / 900.0) # ≈ 0.0055
|
||||||
|
|
||||||
|
var ewma1m = 0.0
|
||||||
|
var ewma5m = 0.0
|
||||||
|
var ewma15m = 0.0
|
||||||
|
|
||||||
|
var now = Moment.now()
|
||||||
var lagWasHigh = false
|
var lagWasHigh = false
|
||||||
|
|
||||||
while true:
|
while true:
|
||||||
|
let lastWakeup = now
|
||||||
await sleepAsync(CheckInterval)
|
await sleepAsync(CheckInterval)
|
||||||
|
now = Moment.now()
|
||||||
|
|
||||||
let now = Moment.now()
|
|
||||||
let actualElapsed = now - lastWakeup
|
let actualElapsed = now - lastWakeup
|
||||||
let lag = actualElapsed - CheckInterval
|
let lag = max(ZeroDuration, actualElapsed - CheckInterval)
|
||||||
|
const maxAcceptedLagSecs = MaxAcceptedLag.nanoseconds.float64 / 1_000_000_000.0
|
||||||
|
|
||||||
let lagSecs = lag.nanoseconds.float64 / 1_000_000_000.0
|
let lagSecs = lag.nanoseconds.float64 / 1_000_000_000.0
|
||||||
|
let load = lagSecs / maxAcceptedLagSecs
|
||||||
|
|
||||||
event_loop_lag_seconds.set(lagSecs)
|
ewma1m = alpha1m * load + (1.0 - alpha1m) * ewma1m
|
||||||
|
ewma5m = alpha5m * load + (1.0 - alpha5m) * ewma5m
|
||||||
|
ewma15m = alpha15m * load + (1.0 - alpha15m) * ewma15m
|
||||||
|
|
||||||
let lagIsHigh = lag > CheckInterval
|
event_loop_load.set(round(ewma1m, 4), labelValues = ["1m"])
|
||||||
|
event_loop_load.set(round(ewma5m, 4), labelValues = ["5m"])
|
||||||
|
event_loop_load.set(round(ewma15m, 4), labelValues = ["15m"])
|
||||||
|
|
||||||
|
let lagIsHigh = lag > MaxAcceptedLag
|
||||||
|
|
||||||
if lag > CheckInterval:
|
if lag > CheckInterval:
|
||||||
warn "chronos event loop severely lagging, many tasks may be accumulating",
|
warn "chronos event loop severely lagging, many tasks may be accumulating",
|
||||||
expected_secs = CheckInterval.seconds,
|
expected_secs = CheckInterval.seconds,
|
||||||
actual_secs = actualElapsed.nanoseconds.float64 / 1_000_000_000.0,
|
lag_secs = round(lagSecs, 4),
|
||||||
lag_secs = lagSecs
|
load_1m = round(ewma1m, 4),
|
||||||
elif lag > (CheckInterval div 2):
|
load_5m = round(ewma5m, 4),
|
||||||
info "chronos event loop lag detected",
|
load_15m = round(ewma15m, 4)
|
||||||
expected_secs = CheckInterval.seconds,
|
|
||||||
actual_secs = actualElapsed.nanoseconds.float64 / 1_000_000_000.0,
|
|
||||||
lag_secs = lagSecs
|
|
||||||
|
|
||||||
if not isNil(onLagChange) and lagIsHigh != lagWasHigh:
|
if not onLagChange.isNil() and lagIsHigh != lagWasHigh:
|
||||||
lagWasHigh = lagIsHigh
|
lagWasHigh = lagIsHigh
|
||||||
onLagChange(lagIsHigh)
|
onLagChange(lagIsHigh)
|
||||||
|
|
||||||
lastWakeup = now
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user