From 16caf2ca135da7b4ce5c2e2dbd8b2c5a4f88e847 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Mon, 11 Oct 2021 20:45:25 +0200 Subject: fix(simulator): Fix queue resizing logic This change fixes two issues with the resizing logic of the specialized queue implementations used by the FlowEngine implementation. --- .../main/kotlin/org/opendc/simulator/flow/internal/FlowDeque.kt | 7 +++++-- .../kotlin/org/opendc/simulator/flow/internal/FlowTimerQueue.kt | 7 ++++++- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'opendc-simulator') diff --git a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/internal/FlowDeque.kt b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/internal/FlowDeque.kt index c6cba4b7..94232954 100644 --- a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/internal/FlowDeque.kt +++ b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/internal/FlowDeque.kt @@ -25,7 +25,10 @@ package org.opendc.simulator.flow.internal import java.util.* /** - * A specialized [ArrayDeque] for [FlowConsumerContextImpl] implementations. + * A specialized [ArrayDeque] that tracks the [FlowConsumerContextImpl] instances that have updated in an interpreter + * cycle. + * + * By using a specialized class, we reduce the overhead caused by type-erasure. */ internal class FlowDeque(initialCapacity: Int = 256) { /** @@ -106,7 +109,7 @@ internal class FlowDeque(initialCapacity: Int = 256) { val a = arrayOfNulls(newCapacity) - _elements.copyInto(a, 0, p, r) + _elements.copyInto(a, 0, p, n) _elements.copyInto(a, r, 0, p) _elements = a diff --git a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/internal/FlowTimerQueue.kt b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/internal/FlowTimerQueue.kt index 22a390e6..47061a91 100644 --- a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/internal/FlowTimerQueue.kt +++ b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/internal/FlowTimerQueue.kt @@ -24,6 +24,9 @@ package org.opendc.simulator.flow.internal /** * Specialized priority queue for flow timers. + * + * By using a specialized priority queue, we reduce the overhead caused by the default priority queue implementation + * being generic. */ internal class FlowTimerQueue(initialCapacity: Int = 256) { /** @@ -46,9 +49,11 @@ internal class FlowTimerQueue(initialCapacity: Int = 256) { */ fun add(ctx: FlowConsumerContextImpl, deadline: Long) { val i = size - val deadlines = _deadlines + var deadlines = _deadlines if (i >= deadlines.size) { grow() + // Re-fetch the resized array + deadlines = _deadlines } siftUp(deadlines, _pending, i, ctx, deadline) -- cgit v1.2.3 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 From 1b0cf98d4d7b842fffcc62a1c7cf50cba0f97277 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Tue, 19 Oct 2021 10:59:47 +0200 Subject: build(jmh): Fix duplicate classpath entries This change fixes an issue with the JMH plugin where entries would be included twice on the classpath or entries that did not belong on the classpath were also included. --- opendc-simulator/opendc-simulator-flow/build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'opendc-simulator') diff --git a/opendc-simulator/opendc-simulator-flow/build.gradle.kts b/opendc-simulator/opendc-simulator-flow/build.gradle.kts index 05e21c3c..f5b67851 100644 --- a/opendc-simulator/opendc-simulator-flow/build.gradle.kts +++ b/opendc-simulator/opendc-simulator-flow/build.gradle.kts @@ -36,4 +36,6 @@ dependencies { testImplementation(projects.opendcSimulator.opendcSimulatorCore) testImplementation(libs.slf4j.simple) + + jmhImplementation(projects.opendcSimulator.opendcSimulatorCore) } -- cgit v1.2.3