summaryrefslogtreecommitdiff
path: root/opendc-compute/opendc-compute-simulator
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-compute/opendc-compute-simulator')
-rw-r--r--opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt55
-rw-r--r--opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt10
2 files changed, 65 insertions, 0 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 20e5a9db..e12bd37b 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
@@ -72,6 +72,11 @@ public class SimHost(
override val scope: CoroutineScope = CoroutineScope(context + Job())
/**
+ * The clock instance used by the host.
+ */
+ private val clock = interpreter.clock
+
+ /**
* The logger instance of this server.
*/
private val logger = KotlinLogging.logger {}
@@ -115,6 +120,8 @@ public class SimHost(
_cpuDemand.record(cpuDemand)
_cpuUsage.record(cpuUsage)
_powerUsage.record(machine.powerDraw)
+
+ reportTime()
}
}
)
@@ -221,6 +228,33 @@ public class SimHost(
.build()
.bind(Attributes.of(ResourceAttributes.HOST_ID, uid.toString()))
+ /**
+ * The amount of time in the system.
+ */
+ private val _totalTime = meter.counterBuilder("host.time.total")
+ .setDescription("The amount of time in the system")
+ .setUnit("ms")
+ .build()
+ .bind(Attributes.of(ResourceAttributes.HOST_ID, uid.toString()))
+
+ /**
+ * The uptime of the host.
+ */
+ private val _upTime = meter.counterBuilder("host.time.up")
+ .setDescription("The uptime of the host")
+ .setUnit("ms")
+ .build()
+ .bind(Attributes.of(ResourceAttributes.HOST_ID, uid.toString()))
+
+ /**
+ * The downtime of the host.
+ */
+ private val _downTime = meter.counterBuilder("host.time.down")
+ .setDescription("The downtime of the host")
+ .setUnit("ms")
+ .build()
+ .bind(Attributes.of(ResourceAttributes.HOST_ID, uid.toString()))
+
init {
// Launch hypervisor onto machine
scope.launch {
@@ -238,6 +272,24 @@ public class SimHost(
}
}
+ private var _lastReport = clock.millis()
+
+ private fun reportTime() {
+ if (!scope.isActive)
+ return
+
+ val now = clock.millis()
+ val duration = now - _lastReport
+
+ _totalTime.add(duration)
+ when (_state) {
+ HostState.UP -> _upTime.add(duration)
+ HostState.DOWN -> _downTime.add(duration)
+ }
+
+ _lastReport = now
+ }
+
override fun canFit(server: Server): Boolean {
val sufficientMemory = availableMemory > server.flavor.memorySize
val enoughCpus = machine.model.cpus.size >= server.flavor.cpuCount
@@ -291,6 +343,7 @@ public class SimHost(
}
override fun close() {
+ reportTime()
scope.cancel()
machine.close()
}
@@ -320,6 +373,7 @@ public class SimHost(
}
override suspend fun fail() {
+ reportTime()
_state = HostState.DOWN
for (guest in guests.values) {
guest.fail()
@@ -327,6 +381,7 @@ public class SimHost(
}
override suspend fun recover() {
+ reportTime()
_state = HostState.UP
for (guest in guests.values) {
guest.start()
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 1ba3a9a1..0a2ced7b 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
@@ -189,6 +189,8 @@ internal class SimHostTest {
fun testFailure() = runBlockingSimulation {
var requestedWork = 0L
var grantedWork = 0L
+ var totalTime = 0L
+ var downTime = 0L
val meterProvider: MeterProvider = SdkMeterProvider
.builder()
@@ -238,6 +240,12 @@ internal class SimHostTest {
metricsByName["cpu.work.granted"]?.let {
grantedWork = it.doubleSumData.points.sumOf { point -> point.value }.toLong()
}
+ metricsByName["host.time.total"]?.let {
+ totalTime = it.longSumData.points.first().value
+ }
+ metricsByName["host.time.down"]?.let {
+ downTime = it.longSumData.points.first().value
+ }
return CompletableResultCode.ofSuccess()
}
@@ -275,6 +283,8 @@ internal class SimHostTest {
assertAll(
{ 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(5000, downTime, "Down time does not match") },
)
}