diff options
Diffstat (limited to 'opendc-simulator/opendc-simulator-compute/src/main')
5 files changed, 20 insertions, 51 deletions
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 @@ -49,20 +47,6 @@ public abstract class SimAbstractMachine( 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<Double> - get() = _usage - - /** - * The speed of the CPU cores. - */ - public val speed: DoubleArray - get() = _speed - private var _speed = doubleArrayOf() - - /** * The resources allocated for this machine. */ protected abstract val cpus: List<SimProcessingUnit> @@ -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 @@ -42,11 +41,6 @@ public interface SimMachine : AutoCloseable { public val peripherals: List<SimPeripheral> /** - * A [StateFlow] representing the CPU usage of the simulated machine. - */ - public val usage: StateFlow<Double> - - /** * Run the specified [SimWorkload] on this machine and suspend execution util the workload has finished. */ public suspend fun run(workload: SimWorkload, meta: Map<String, Any> = emptyMap()) 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<SimProcessingUnit>): 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) } } |
