From 52eae4b9ed571573d995b7d47ecb7789a1b4d8ac Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 22 Oct 2021 12:22:45 +0200 Subject: fix(simulator): Compute energy usage in absence of convergence This change addresses an issue where energy usage was not computed correctly if the machine performed no work in between collection cycles. --- .../opendc/simulator/compute/SimBareMetalMachine.kt | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'opendc-simulator') 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) } /** -- cgit v1.2.3