From 8f58ae2b28518c6a2ed2fe3657984f417b3d3ddb Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 25 Aug 2021 20:35:01 +0200 Subject: refactor(simulator): Remove usage and speed fields from SimMachine This change removes the usage and speed fields from SimMachine. We currently use other ways to capture the usage and speed and these fields cause an additional maintenance burden and performance impact. Hence the removal of these fields. --- .../opendc/simulator/compute/SimAbstractMachine.kt | 40 ---------------------- .../simulator/compute/SimBareMetalMachine.kt | 12 ++++--- .../org/opendc/simulator/compute/SimMachine.kt | 6 ---- .../compute/kernel/SimAbstractHypervisor.kt | 2 ++ .../simulator/compute/power/SimplePowerDriver.kt | 11 +++++- 5 files changed, 20 insertions(+), 51 deletions(-) (limited to 'opendc-simulator/opendc-simulator-compute/src/main') diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimAbstractMachine.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimAbstractMachine.kt index f416643e..266db0dd 100644 --- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimAbstractMachine.kt +++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimAbstractMachine.kt @@ -23,8 +23,6 @@ package org.opendc.simulator.compute import kotlinx.coroutines.* -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow import org.opendc.simulator.compute.device.SimNetworkAdapter import org.opendc.simulator.compute.device.SimPeripheral import org.opendc.simulator.compute.model.MachineModel @@ -48,20 +46,6 @@ public abstract class SimAbstractMachine( final override val parent: SimResourceSystem?, final override val model: MachineModel ) : SimMachine, SimResourceSystem { - /** - * A [StateFlow] representing the CPU usage of the simulated machine. - */ - private val _usage = MutableStateFlow(0.0) - public final override val usage: StateFlow - get() = _usage - - /** - * The speed of the CPU cores. - */ - public val speed: DoubleArray - get() = _speed - private var _speed = doubleArrayOf() - /** * The resources allocated for this machine. */ @@ -106,10 +90,6 @@ public abstract class SimAbstractMachine( val ctx = Context(meta) - // Before the workload starts, initialize the initial power draw - _speed = DoubleArray(model.cpus.size) { 0.0 } - updateUsage(0.0) - return suspendCancellableCoroutine { cont -> this.cont = cont @@ -136,26 +116,6 @@ public abstract class SimAbstractMachine( cancel() } - /* SimResourceSystem */ - override fun onConverge(timestamp: Long) { - val totalCapacity = model.cpus.sumOf { it.frequency } - val cpus = cpus - var totalSpeed = 0.0 - for (cpu in cpus) { - _speed[cpu.model.id] = cpu.speed - totalSpeed += cpu.speed - } - - updateUsage(totalSpeed / totalCapacity) - } - - /** - * This method is invoked when the usage of the machine is updated. - */ - protected open fun updateUsage(usage: Double) { - _usage.value = usage - } - /** * Cancel the workload that is currently running on the machine. */ 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 887f0885..2c711945 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 @@ -32,7 +32,7 @@ import org.opendc.simulator.resources.SimResourceInterpreter /** * A simulated bare-metal machine that is able to run a single workload. * - * A [SimBareMetalMachine] is a stateful object and you should be careful when operating this object concurrently. For + * A [SimBareMetalMachine] is a stateful object, and you should be careful when operating this object concurrently. For * example, the class expects only a single concurrent call to [run]. * * @param interpreter The [SimResourceInterpreter] to drive the simulation. @@ -55,13 +55,17 @@ public class SimBareMetalMachine( Cpu(SimResourceSource(cpu.frequency, interpreter, this@SimBareMetalMachine), cpu) } - override fun updateUsage(usage: Double) { - super.updateUsage(usage) + /** + * The logic of the power driver. + */ + private val powerDriverLogic = powerDriver.createLogic(this, cpus) + + override fun onConverge(timestamp: Long) { psu.update() } init { - psu.connect(powerDriver.createLogic(this, cpus)) + psu.connect(powerDriverLogic) } /** diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimMachine.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimMachine.kt index 0f4674d5..d8dd8205 100644 --- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimMachine.kt +++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimMachine.kt @@ -22,7 +22,6 @@ package org.opendc.simulator.compute -import kotlinx.coroutines.flow.StateFlow import org.opendc.simulator.compute.device.SimPeripheral import org.opendc.simulator.compute.model.MachineModel import org.opendc.simulator.compute.workload.SimWorkload @@ -41,11 +40,6 @@ public interface SimMachine : AutoCloseable { */ public val peripherals: List - /** - * A [StateFlow] representing the CPU usage of the simulated machine. - */ - public val usage: StateFlow - /** * Run the specified [SimWorkload] on this machine and suspend execution util the workload has finished. */ diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/kernel/SimAbstractHypervisor.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/kernel/SimAbstractHypervisor.kt index 6002270a..98271fb0 100644 --- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/kernel/SimAbstractHypervisor.kt +++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/kernel/SimAbstractHypervisor.kt @@ -145,6 +145,8 @@ public abstract class SimAbstractHypervisor( interferenceDomain?.leave(interferenceKey) } } + + override fun onConverge(timestamp: Long) {} } /** diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SimplePowerDriver.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SimplePowerDriver.kt index e43c89ac..bf7aeff1 100644 --- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SimplePowerDriver.kt +++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SimplePowerDriver.kt @@ -30,8 +30,17 @@ import org.opendc.simulator.compute.SimProcessingUnit */ public class SimplePowerDriver(private val model: PowerModel) : PowerDriver { override fun createLogic(machine: SimMachine, cpus: List): PowerDriver.Logic = object : PowerDriver.Logic { + override fun computePower(): Double { - return model.computePower(machine.usage.value) + var targetFreq = 0.0 + var totalSpeed = 0.0 + + for (cpu in cpus) { + targetFreq += cpu.capacity + totalSpeed += cpu.speed + } + + return model.computePower(totalSpeed / targetFreq) } } -- cgit v1.2.3