summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt54
-rw-r--r--opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt10
2 files changed, 63 insertions, 1 deletions
diff --git a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt
index 4526537d..9a1f05fc 100644
--- a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt
+++ b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt
@@ -22,6 +22,7 @@
package org.opendc.compute.simulator
+import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.metrics.Meter
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes
@@ -59,7 +60,7 @@ public class SimHost(
override val meta: Map<String, Any>,
context: CoroutineContext,
interpreter: SimResourceInterpreter,
- meter: Meter,
+ private val meter: Meter,
hypervisor: SimHypervisorProvider,
scalingGovernor: ScalingGovernor = PerformanceScalingGovernor(),
powerDriver: PowerDriver = SimplePowerDriver(ConstantPowerModel(0.0)),
@@ -282,6 +283,11 @@ public class SimHost(
HostState.DOWN -> _downTime.add(duration)
}
+ // Track time of guests
+ for (guest in guests.values) {
+ guest.reportTime()
+ }
+
_lastReport = now
}
@@ -385,6 +391,33 @@ public class SimHost(
private inner class Guest(val server: Server, val machine: SimMachine) {
var state: ServerState = ServerState.TERMINATED
+ /**
+ * The amount of time in the system.
+ */
+ private val _totalTime = meter.counterBuilder("guest.time.total")
+ .setDescription("The amount of time in the system")
+ .setUnit("ms")
+ .build()
+ .bind(Attributes.of(AttributeKey.stringKey("server.id"), server.uid.toString()))
+
+ /**
+ * The uptime of the guest.
+ */
+ private val _runningTime = meter.counterBuilder("guest.time.running")
+ .setDescription("The uptime of the guest")
+ .setUnit("ms")
+ .build()
+ .bind(Attributes.of(AttributeKey.stringKey("server.id"), server.uid.toString()))
+
+ /**
+ * The time the guest is in an error state.
+ */
+ private val _errorTime = meter.counterBuilder("guest.time.error")
+ .setDescription("The time the guest is in an error state")
+ .setUnit("ms")
+ .build()
+ .bind(Attributes.of(AttributeKey.stringKey("server.id"), server.uid.toString()))
+
suspend fun start() {
when (state) {
ServerState.TERMINATED, ServerState.ERROR -> {
@@ -462,5 +495,24 @@ public class SimHost(
onGuestStop(this)
}
+
+ private var _lastReport = clock.millis()
+
+ fun reportTime() {
+ if (state == ServerState.DELETED)
+ return
+
+ val now = clock.millis()
+ val duration = now - _lastReport
+
+ _totalTime.add(duration)
+ when (state) {
+ ServerState.RUNNING -> _runningTime.add(duration)
+ ServerState.ERROR -> _errorTime.add(duration)
+ else -> {}
+ }
+
+ _lastReport = now
+ }
}
}
diff --git a/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt b/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt
index 0a2ced7b..31215e9a 100644
--- a/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt
+++ b/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt
@@ -191,6 +191,8 @@ internal class SimHostTest {
var grantedWork = 0L
var totalTime = 0L
var downTime = 0L
+ var guestTotalTime = 0L
+ var guestDownTime = 0L
val meterProvider: MeterProvider = SdkMeterProvider
.builder()
@@ -246,6 +248,12 @@ internal class SimHostTest {
metricsByName["host.time.down"]?.let {
downTime = it.longSumData.points.first().value
}
+ metricsByName["guest.time.total"]?.let {
+ guestTotalTime = it.longSumData.points.first().value
+ }
+ metricsByName["guest.time.error"]?.let {
+ guestDownTime = it.longSumData.points.first().value
+ }
return CompletableResultCode.ofSuccess()
}
@@ -284,7 +292,9 @@ internal class SimHostTest {
{ assertEquals(2226039, requestedWork, "Total time does not match") },
{ assertEquals(1086039, grantedWork, "Down time does not match") },
{ assertEquals(1200001, totalTime, "Total time does not match") },
+ { assertEquals(1200001, guestTotalTime, "Guest total time does not match") },
{ assertEquals(5000, downTime, "Down time does not match") },
+ { assertEquals(5000, guestDownTime, "Guest down time does not match") },
)
}