more metrics on dispatcher

This commit is contained in:
loucass003 committed 2026-09-11 15:57:15 +02:00
1 parent bd2c4da2f2
commit e8196425da
6 files changed
+46 -12

No files matched your search

@@ -54,6 +54,10 @@ class Subscription<T : Any, P : Any> @PublishedApi internal constructor(
* quarter of [capacity], so a dispatcher that is starting to fall behind says so while it is still
* keeping up. Capacity stays large enough to absorb a GC pause without dropping; the warning
* threshold is what makes it early, not a small buffer.
*
* Each report also carries the window's slowest handler run and longest queue wait. A high handler
* time means the consumer is stuck inside a handler; a high wait next to a low handler time means
* the drain coroutine is not getting scheduled.
*/
class EventDispatcher<T : Any>(
private val name: String,
@@ -79,6 +83,11 @@ class EventDispatcher<T : Any>(
private val depth = AtomicInteger()
private val peakDepth = AtomicInteger()
// Windowed maxima surfaced in the saturation report. The drain coroutine is the only writer;
// saturationReport reads and clears them. A dropped sample under that race is acceptable here.
private val peakHandlerNanos = AtomicLong()
private val peakWaitNanos = AtomicLong()
// Warn on how full the mailbox gets, not on it overflowing. Overflow is the failure; depth is the
// leading indicator, and separating the two means the buffer can stay big enough to ride out a GC
// pause without that also delaying the warning.
@@ -98,17 +107,22 @@ class EventDispatcher<T : Any>(
depth.decrementAndGet()
val type = envelope.payload::class
val waitNanos = System.nanoTime() - envelope.enqueuedAtNanos
if (waitNanos > peakWaitNanos.get()) peakWaitNanos.set(waitNanos)
metrics?.recordQueueWait(waitNanos, envelope.enqueueDepth)
val handlersStartNanos = System.nanoTime()
if (metrics == null) {
for (sub in handlersFor(type)) sub.action(envelope.payload)
continue
}
metrics?.recordQueueWait(System.nanoTime() - envelope.enqueuedAtNanos, envelope.enqueueDepth)
for (sub in handlersFor(type)) {
val start = System.nanoTime()
sub.action(envelope.payload)
metrics?.recordHandler(System.nanoTime() - start, type)
} else {
for (sub in handlersFor(type)) {
val start = System.nanoTime()
sub.action(envelope.payload)
metrics?.recordHandler(System.nanoTime() - start, type)
}
}
val handlersNanos = System.nanoTime() - handlersStartNanos
if (handlersNanos > peakHandlerNanos.get()) peakHandlerNanos.set(handlersNanos)
}
}
@@ -176,6 +190,8 @@ class EventDispatcher<T : Any>(
val peak = peakDepth.getAndSet(depth.get().coerceAtLeast(0))
val drops = dropped.getAndSet(0)
val stalls = stalled.getAndSet(0)
val slowestHandlerMicros = peakHandlerNanos.getAndSet(0) / 1000
val longestWaitMicros = peakWaitNanos.getAndSet(0) / 1000
if (drops == 0L && stalls == 0L && peak < warnDepth) return null
val outcome = when {
@@ -183,7 +199,8 @@ class EventDispatcher<T : Any>(
stalls > 0L -> "$stalls producer stalls"
else -> "nothing lost yet"
}
return "$name mailbox peak $peak/$capacity in $SATURATION_REPORT_INTERVAL -> $outcome"
return "$name mailbox peak $peak/$capacity in $SATURATION_REPORT_INTERVAL -> $outcome" +
" | slowest handler ${slowestHandlerMicros}us, longest queue wait ${longestWaitMicros}us"
}
}
@@ -211,6 +211,11 @@ class DataFeedInitBehaviour(
receiver.datafeedTimers.forEach { it.cancelAndJoin() }
AppLogger.solarxr.info(
"SolarXR[${receiver.id}] data feed started: " +
dataFeeds.joinToString { "every ${it.minimumTimeSinceLast}ms" },
)
val timers = dataFeeds.mapIndexed { index, config ->
receiver.context.scope.launch {
val interval = config.minimumTimeSinceLast.toLong().milliseconds