diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-10-25 16:16:13 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-25 16:16:13 +0200 |
| commit | a41cd2504f15f3e3e49eb533faca390911cc5110 (patch) | |
| tree | f35e7e5c65e2985cf34ad7689526d5b5c0815230 /opendc-simulator | |
| parent | aa9b32f8cd1467e9718959f400f6777e5d71737d (diff) | |
| parent | fe8cd32c3f79d2a6c898a1c8809792e35440a539 (diff) | |
merge: Address several regressions in simulator
This pull request addresses several regressions that have been introduced in the past few pull requests.
- Fix queue resizing logic
- Change clock resolution from milliseconds to nanoseconds in `OtelClockAdapter`
- Compute energy usage in absence of convergence
- Fix duplicate classpath entries
- Fix release workflow
**Breaking API Changes**
- `OtelClockAdapter` now exports time in nanoseconds as the method contract describes.
Diffstat (limited to 'opendc-simulator')
4 files changed, 31 insertions, 5 deletions
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) } /** 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) } 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<FlowConsumerContextImpl>(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) |
