summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opendc-experiments/opendc-experiments-capelin/src/test/kotlin/org/opendc/experiments/capelin/CapelinIntegrationTest.kt4
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt20
2 files changed, 20 insertions, 4 deletions
diff --git a/opendc-experiments/opendc-experiments-capelin/src/test/kotlin/org/opendc/experiments/capelin/CapelinIntegrationTest.kt b/opendc-experiments/opendc-experiments-capelin/src/test/kotlin/org/opendc/experiments/capelin/CapelinIntegrationTest.kt
index e34c5bdc..94e92c1b 100644
--- a/opendc-experiments/opendc-experiments-capelin/src/test/kotlin/org/opendc/experiments/capelin/CapelinIntegrationTest.kt
+++ b/opendc-experiments/opendc-experiments-capelin/src/test/kotlin/org/opendc/experiments/capelin/CapelinIntegrationTest.kt
@@ -120,7 +120,7 @@ class CapelinIntegrationTest {
{ assertEquals(67006560, this@CapelinIntegrationTest.exporter.activeTime) { "Incorrect active time" } },
{ assertEquals(3159377, this@CapelinIntegrationTest.exporter.stealTime) { "Incorrect steal time" } },
{ assertEquals(0, this@CapelinIntegrationTest.exporter.lostTime) { "Incorrect lost time" } },
- { assertEquals(5.840212485920686E9, this@CapelinIntegrationTest.exporter.energyUsage, 0.01) { "Incorrect power draw" } },
+ { assertEquals(5.840862926294953E9, this@CapelinIntegrationTest.exporter.energyUsage, 0.01) { "Incorrect power draw" } },
)
}
@@ -164,7 +164,7 @@ class CapelinIntegrationTest {
{ assertEquals(9740289, this@CapelinIntegrationTest.exporter.activeTime) { "Active time incorrect" } },
{ assertEquals(0, this@CapelinIntegrationTest.exporter.stealTime) { "Steal time incorrect" } },
{ assertEquals(0, this@CapelinIntegrationTest.exporter.lostTime) { "Lost time incorrect" } },
- { assertEquals(7.0099453912813E8, this@CapelinIntegrationTest.exporter.energyUsage, 0.01) { "Incorrect power draw" } }
+ { assertEquals(7.010642279990053E8, this@CapelinIntegrationTest.exporter.energyUsage, 0.01) { "Incorrect power draw" } }
)
}
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt
index 9140d31b..5df03d45 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt
+++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt
@@ -60,8 +60,12 @@ public class SimBareMetalMachine(
* The total energy usage of the machine (without PSU loss) in Joules.
*/
public val energyUsage: Double
- get() = _energyUsage
+ get() {
+ computeEnergyUsage(engine.clock.millis())
+ return _energyUsage
+ }
private var _energyUsage = 0.0
+ private var _energyLastComputation = 0L
/**
* The processing units of the machine.
@@ -86,13 +90,25 @@ public class SimBareMetalMachine(
val duration = max(0, now - lastConverge)
if (duration > 0) {
// Compute the power and energy usage of the machine
- _energyUsage += _powerUsage * (duration / 1000.0)
+ computeEnergyUsage(now)
_powerUsage = powerDriverLogic.computePower()
}
}
init {
psu.connect(powerDriverLogic)
+ _powerUsage = powerDriverLogic.computePower()
+ }
+
+ /**
+ * Helper method to compute total energy usage.
+ */
+ private fun computeEnergyUsage(now: Long) {
+ val duration = max(0, now - _energyLastComputation)
+ _energyLastComputation = now
+
+ // Compute the energy usage of the machine
+ _energyUsage += _powerUsage * (duration / 1000.0)
}
/**