From dd24782f3710678151c80f8ad365eecc7389b6f8 Mon Sep 17 00:00:00 2001 From: Hongyu Date: Tue, 16 Mar 2021 09:08:51 +0100 Subject: simulator: Add the CPU power model from iCanCloud/E-mc2 This change implements the CPU energy model with p-states from iCanCloud/E-mc2: - Only pushed a portion of the code for discussion as not sure if the idea is on track. - Inline comments have been added, and formal documents will follow once the model is finalized. - The p-state power consumptions are currently hard-coded in a companion object, which should be improved in the next PR(s). **Breaking Changes** - CpuPowerModel: directly interact with the machine it is measuring. - SimBareMetalMachine: expose the speeds of its CPU cores and its clock instant. --- .../org/opendc/simulator/compute/SimAbstractMachine.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'simulator/opendc-simulator') diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimAbstractMachine.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimAbstractMachine.kt index 1bdbb7e8..39ae34fe 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimAbstractMachine.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimAbstractMachine.kt @@ -38,14 +38,19 @@ import kotlin.coroutines.CoroutineContext /** * Abstract implementation of the [SimMachine] interface. - * - * @param context The [CoroutineContext] in which the machine runs. */ public abstract class SimAbstractMachine(private val clock: Clock) : SimMachine { private val _usage = MutableStateFlow(0.0) override val usage: StateFlow get() = _usage + /** + * The speed of the CPU cores. + */ + public val speed: List + get() = _speed + private var _speed = mutableListOf() + /** * A flag to indicate that the machine is terminated. */ @@ -89,13 +94,16 @@ public abstract class SimAbstractMachine(private val clock: Clock) : SimMachine val ctx = Context(resources, meta + mapOf("coroutine-context" to context)) val totalCapacity = model.cpus.sumByDouble { it.frequency } + _speed = MutableList(model.cpus.size) { 0.0 } + workload.onStart(ctx) for ((cpu, source) in resources) { val consumer = workload.getConsumer(ctx, cpu) val job = source.speed .onEach { - _usage.value = resources.values.sumByDouble { it.speed.value } / totalCapacity + _speed[cpu.id] = source.speed.value + _usage.value = _speed.sum() / totalCapacity } .launchIn(this) -- cgit v1.2.3