From be6e41b9e32a7eac9fb1a2fb0656a85a8a2bdc46 Mon Sep 17 00:00:00 2001 From: Marlon Etheredge Date: Tue, 24 Mar 2026 21:08:11 +0100 Subject: [PATCH 1/3] fix: fixing event latency recording --- .../cirrina/execution/object/StateMachine.kt | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt b/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt index b51538d8..88331f2c 100644 --- a/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt +++ b/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt @@ -12,6 +12,7 @@ import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import java.util.concurrent.TimeUnit import kotlin.properties.Delegates +import kotlin.time.Clock import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.onFailure @@ -155,7 +156,12 @@ internal constructor( private fun processEvent(event: Event) { if (isTerminated()) return - recordLatency(event) + if (event.channel == EventChannel.EXTERNAL) { + val now = Clock.System.now() + val epochNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond + + eventTimer.update(epochNanos - event.emittedTime, TimeUnit.NANOSECONDS) + } handleEvent(event)?.let { transition -> step(transition) } @@ -284,19 +290,15 @@ internal constructor( private fun stopTimeout(name: String) = timeoutActionManager.stop(name) - private fun recordLatency(event: Event) { - if (event.source == name) return - if (event.target != name) return - if (event.emittedTime == 0L) return - - eventTimer.update((System.currentTimeMillis() - event.emittedTime), TimeUnit.MILLISECONDS) - } - override fun toString() = "StateMachine(name='$name')" inner class StateMachineEventHandler(val eventHandler: EventHandler) { - fun emit(event: Event) = - eventHandler.emit(event.copy(source = name, emittedTime = System.currentTimeMillis())) + fun emit(event: Event) { + val now = Clock.System.now() + val epochNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond + + eventHandler.emit(event.copy(source = name, emittedTime = epochNanos)) + } fun propagateToParent(event: Event) { parent?.stateMachineEventHandler?.propagateToParent(event) ?: pushEvent(event) From 84828863b6307d5f7326cbd09f8fd384c3d2a09a Mon Sep 17 00:00:00 2001 From: Marlon Etheredge Date: Wed, 25 Mar 2026 11:00:11 +0100 Subject: [PATCH 2/3] fix: adding coerce at least zero to event timer update --- .../at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt b/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt index 88331f2c..3f81b83f 100644 --- a/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt +++ b/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt @@ -158,9 +158,11 @@ internal constructor( if (event.channel == EventChannel.EXTERNAL) { val now = Clock.System.now() - val epochNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond + val nowNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond + + val deltaNanos = (nowNanos - event.emittedTime).coerceAtLeast(0L) - eventTimer.update(epochNanos - event.emittedTime, TimeUnit.NANOSECONDS) + eventTimer.update(deltaNanos, TimeUnit.NANOSECONDS) } handleEvent(event)?.let { transition -> step(transition) } From 817e61e3d50b89fce115f114a407531cb84edc99 Mon Sep 17 00:00:00 2001 From: Marlon Etheredge Date: Wed, 25 Mar 2026 11:03:49 +0100 Subject: [PATCH 3/3] chore: fixing formatting --- .../at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt b/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt index 3f81b83f..50e83a7d 100644 --- a/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt +++ b/src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt @@ -159,7 +159,6 @@ internal constructor( if (event.channel == EventChannel.EXTERNAL) { val now = Clock.System.now() val nowNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond - val deltaNanos = (nowNanos - event.emittedTime).coerceAtLeast(0L) eventTimer.update(deltaNanos, TimeUnit.NANOSECONDS)