From e38e6b9341907e28d029054995cf43cbd5e8bb4d Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Tue, 30 Mar 2021 22:20:13 +0200 Subject: simulator: Add initial design of CPUFreq model This change adds a model implementing Dynamic Voltage Frequency Scaling (DVFS) to OpenDC. --- .../simulator/compute/SimBareMetalMachine.kt | 38 ++++++++-- .../compute/cpufreq/DemandScalingGovernor.kt | 36 +++++++++ .../compute/cpufreq/PStateScalingDriver.kt | 87 ++++++++++++++++++++++ .../compute/cpufreq/PerformanceScalingGovernor.kt | 36 +++++++++ .../simulator/compute/cpufreq/ScalingContext.kt | 52 +++++++++++++ .../simulator/compute/cpufreq/ScalingDriver.kt | 54 ++++++++++++++ .../simulator/compute/cpufreq/ScalingGovernor.kt | 54 ++++++++++++++ .../compute/cpufreq/SimpleScalingDriver.kt | 52 +++++++++++++ .../simulator/compute/power/ConstantPowerModel.kt | 4 +- .../simulator/compute/power/CubicPowerModel.kt | 8 +- .../compute/power/InterpolationPowerModel.kt | 16 ++-- .../simulator/compute/power/LinearPowerModel.kt | 8 +- .../simulator/compute/power/MachinePowerModel.kt | 18 ----- .../opendc/simulator/compute/power/PowerModel.kt | 16 ++++ .../simulator/compute/power/SqrtPowerModel.kt | 8 +- .../simulator/compute/power/SquarePowerModel.kt | 8 +- .../compute/power/ZeroIdlePowerDecorator.kt | 8 +- 17 files changed, 447 insertions(+), 56 deletions(-) create mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/DemandScalingGovernor.kt create mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt create mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PerformanceScalingGovernor.kt create mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingContext.kt create mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingDriver.kt create mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt create mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/SimpleScalingDriver.kt delete mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/MachinePowerModel.kt create mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt (limited to 'simulator/opendc-simulator/opendc-simulator-compute/src/main') diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt index 830ff70e..51b807d2 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt @@ -24,9 +24,9 @@ package org.opendc.simulator.compute import kotlinx.coroutines.* import kotlinx.coroutines.flow.* +import org.opendc.simulator.compute.cpufreq.ScalingDriver +import org.opendc.simulator.compute.cpufreq.ScalingGovernor import org.opendc.simulator.compute.model.ProcessingUnit -import org.opendc.simulator.compute.power.ConstantPowerModel -import org.opendc.simulator.compute.power.MachinePowerModel import org.opendc.simulator.resources.* import org.opendc.utils.TimerScheduler import java.time.Clock @@ -47,18 +47,14 @@ public class SimBareMetalMachine( context: CoroutineContext, private val clock: Clock, override val model: SimMachineModel, - private val powerModel: MachinePowerModel = ConstantPowerModel(0.0) + scalingGovernor: ScalingGovernor, + scalingDriver: ScalingDriver ) : SimAbstractMachine(clock) { /** * The [Job] associated with this machine. */ private val scope = CoroutineScope(context + Job()) - /** - * The power draw of the machine. - */ - public val powerDraw: StateFlow = usage.map { powerModel.computeCpuPower(it) }.stateIn(scope, SharingStarted.Eagerly, 0.0) - override val context: CoroutineContext = scope.coroutineContext /** @@ -69,6 +65,32 @@ public class SimBareMetalMachine( override val resources: Map = model.cpus.associateWith { SimResourceSource(it.frequency, clock, scheduler) } + /** + * Construct the [ScalingDriver.Logic] for this machine. + */ + private val scalingDriver = scalingDriver.createLogic(this) + + /** + * The scaling contexts associated with each CPU. + */ + private val scalingGovernors = resources.map { (cpu, resource) -> + scalingGovernor.createLogic(this.scalingDriver.createContext(cpu, resource)) + } + + init { + scalingGovernors.forEach { it.onStart() } + } + + /** + * The power draw of the machine. + */ + public val powerDraw: StateFlow = usage + .map { + this.scalingGovernors.forEach { it.onLimit() } + this.scalingDriver.computePower() + } + .stateIn(scope, SharingStarted.Eagerly, 0.0) + override fun close() { super.close() diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/DemandScalingGovernor.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/DemandScalingGovernor.kt new file mode 100644 index 00000000..b4bbf9fb --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/DemandScalingGovernor.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.simulator.compute.cpufreq + +/** + * A CPUFreq [ScalingGovernor] that requests the frequency based on the utilization of the machine. + */ +public class DemandScalingGovernor : ScalingGovernor { + override fun createLogic(ctx: ScalingContext): ScalingGovernor.Logic = object : ScalingGovernor.Logic { + override fun onLimit() { + ctx.setTarget(ctx.resource.speed.value) + } + + override fun toString(): String = "DemandScalingGovernor.Logic" + } +} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt new file mode 100644 index 00000000..8f3eacea --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.simulator.compute.cpufreq + +import org.opendc.simulator.compute.SimMachine +import org.opendc.simulator.compute.model.ProcessingUnit +import org.opendc.simulator.compute.power.PowerModel +import org.opendc.simulator.resources.SimResourceSource +import java.util.* +import kotlin.math.max + +/** + * A [ScalingDriver] that scales the frequency of the processor based on a discrete set of frequencies. + * + * @param states A map describing the states of the driver. + */ +public class PStateScalingDriver(states: Map) : ScalingDriver { + /** + * The P-States defined by the user and ordered by key. + */ + private val states = TreeMap(states) + + override fun createLogic(machine: SimMachine): ScalingDriver.Logic = object : ScalingDriver.Logic { + /** + * The scaling contexts. + */ + private val contexts = mutableListOf() + + override fun createContext(cpu: ProcessingUnit, resource: SimResourceSource): ScalingContext { + val ctx = ScalingContextImpl(machine, cpu, resource) + contexts.add(ctx) + return ctx + } + + override fun computePower(): Double { + var freq = 0.0 + var totalSpeed = 0.0 + var totalFreq = 0.0 + + for (ctx in contexts) { + freq = max(ctx.target, freq) + totalSpeed += ctx.resource.speed.value + totalFreq += ctx.target + } + + val (_, model) = states.ceilingEntry(freq) + return model.computePower(totalSpeed / totalFreq) + } + + override fun toString(): String = "PStateScalingDriver.Logic" + } + + private class ScalingContextImpl( + override val machine: SimMachine, + override val cpu: ProcessingUnit, + override val resource: SimResourceSource + ) : ScalingContext { + var target = cpu.frequency + private set + + override fun setTarget(freq: Double) { + target = freq + } + + override fun toString(): String = "PStateScalingDriver.Context" + } +} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PerformanceScalingGovernor.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PerformanceScalingGovernor.kt new file mode 100644 index 00000000..c574d5d1 --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PerformanceScalingGovernor.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.simulator.compute.cpufreq + +/** + * A CPUFreq [ScalingGovernor] that causes the highest possible frequency to be requested from the resource. + */ +public class PerformanceScalingGovernor : ScalingGovernor { + override fun createLogic(ctx: ScalingContext): ScalingGovernor.Logic = object : ScalingGovernor.Logic { + override fun onLimit() { + ctx.setTarget(ctx.resource.capacity) + } + + override fun toString(): String = "PerformanceScalingGovernor.Logic" + } +} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingContext.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingContext.kt new file mode 100644 index 00000000..44cd0168 --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingContext.kt @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.simulator.compute.cpufreq + +import org.opendc.simulator.compute.SimMachine +import org.opendc.simulator.compute.model.ProcessingUnit +import org.opendc.simulator.resources.SimResourceSource + +/** + * A [ScalingContext] is used to communicate frequency scaling changes between the [ScalingGovernor] and driver. + */ +public interface ScalingContext { + /** + * The machine the processing unit belongs to. + */ + public val machine: SimMachine + + /** + * The processing unit associated with this context. + */ + public val cpu: ProcessingUnit + + /** + * The resource associated with this context. + */ + public val resource: SimResourceSource + + /** + * Target the processor to run at the specified target [frequency][freq]. + */ + public fun setTarget(freq: Double) +} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingDriver.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingDriver.kt new file mode 100644 index 00000000..2414eabb --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingDriver.kt @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.simulator.compute.cpufreq + +import org.opendc.simulator.compute.SimMachine +import org.opendc.simulator.compute.model.ProcessingUnit +import org.opendc.simulator.resources.SimResourceSource + +/** + * A [ScalingDriver] is responsible for switching the processor to the correct frequency. + */ +public interface ScalingDriver { + /** + * Create the scaling logic for the specified [machine] + */ + public fun createLogic(machine: SimMachine): Logic + + /** + * The logic of the scaling driver. + */ + public interface Logic { + /** + * Create the [ScalingContext] for the specified [cpu] and [resource] instance. + */ + public fun createContext(cpu: ProcessingUnit, resource: SimResourceSource): ScalingContext + + /** + * Compute the power consumption of the processor. + * + * @return The power consumption of the processor in W. + */ + public fun computePower(): Double + } +} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt new file mode 100644 index 00000000..9c6eb9ed --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.simulator.compute.cpufreq + +/** + * A [ScalingGovernor] in the CPUFreq subsystem of OpenDC is responsible for scaling the frequency of simulated CPUs + * independent of the particular implementation of the CPU. + * + * Each of the scaling governors implements a single, possibly parametrized, performance scaling algorithm. + * + * For more information, see the documentation of the Linux CPUFreq subsystem: + * https://www.kernel.org/doc/html/latest/admin-guide/pm/cpufreq.html + */ +public interface ScalingGovernor { + /** + * Create the scaling logic for the specified [context] + */ + public fun createLogic(ctx: ScalingContext): Logic + + /** + * The logic of the scaling governor. + */ + public interface Logic { + /** + * This method is invoked when the governor is started. + */ + public fun onStart() {} + + /** + * This method is invoked when + */ + public fun onLimit() {} + } +} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/SimpleScalingDriver.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/SimpleScalingDriver.kt new file mode 100644 index 00000000..b4b564ce --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/SimpleScalingDriver.kt @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.simulator.compute.cpufreq + +import org.opendc.simulator.compute.SimMachine +import org.opendc.simulator.compute.model.ProcessingUnit +import org.opendc.simulator.compute.power.PowerModel +import org.opendc.simulator.resources.SimResourceSource + +/** + * A [ScalingDriver] that ignores the instructions of the [ScalingGovernor] and directly computes the power consumption + * based on the specified [power model][model]. + */ +public class SimpleScalingDriver(private val model: PowerModel) : ScalingDriver { + override fun createLogic(machine: SimMachine): ScalingDriver.Logic = object : ScalingDriver.Logic { + override fun createContext(cpu: ProcessingUnit, resource: SimResourceSource): ScalingContext { + return object : ScalingContext { + override val machine: SimMachine = machine + + override val cpu: ProcessingUnit = cpu + + override val resource: SimResourceSource = resource + + override fun setTarget(freq: Double) {} + } + } + + override fun computePower(): Double = model.computePower(machine.usage.value) + + override fun toString(): String = "SimpleScalingDriver.Logic" + } +} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ConstantPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ConstantPowerModel.kt index 5d7ae8ad..1b9733d1 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ConstantPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ConstantPowerModel.kt @@ -3,6 +3,6 @@ package org.opendc.simulator.compute.power /** * A power model which produces a constant value [constant]. */ -public class ConstantPowerModel(private val constant: Double) : MachinePowerModel { - public override fun computeCpuPower(cpuUtil: Double): Double = constant +public class ConstantPowerModel(private val constant: Double) : PowerModel { + public override fun computePower(utilization: Double): Double = constant } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/CubicPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/CubicPowerModel.kt index 8e47f571..e5bc013c 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/CubicPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/CubicPowerModel.kt @@ -14,12 +14,12 @@ import kotlin.math.pow public class CubicPowerModel( private var maxPower: Double, staticPowerPercent: Double -) : MachinePowerModel { +) : PowerModel { private var staticPower: Double = staticPowerPercent * maxPower private var constPower: Double = (maxPower - staticPower) / 100.0.pow(3) - public override fun computeCpuPower(cpuUtil: Double): Double { - require(cpuUtil in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - return staticPower + constPower * (cpuUtil * 100).pow(3) + public override fun computePower(utilization: Double): Double { + require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } + return staticPower + constPower * (utilization * 100).pow(3) } } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/InterpolationPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/InterpolationPowerModel.kt index 69fc0514..ce9b8197 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/InterpolationPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/InterpolationPowerModel.kt @@ -16,22 +16,22 @@ import kotlin.math.floor */ public class InterpolationPowerModel( hardwareName: String, -) : MachinePowerModel { +) : PowerModel { private val averagePowerValues: List = loadAveragePowerValue(hardwareName) - public override fun computeCpuPower(cpuUtil: Double): Double { - require(cpuUtil in 0.0..1.0) { "CPU utilization must be in [0, 1]" } + public override fun computePower(utilization: Double): Double { + require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - val cpuUtilFlr = floor(cpuUtil * 10).toInt() - val cpuUtilCil = ceil(cpuUtil * 10).toInt() + val cpuUtilFlr = floor(utilization * 10).toInt() + val cpuUtilCil = ceil(utilization * 10).toInt() val powerFlr: Double = getAveragePowerValue(cpuUtilFlr) val powerCil: Double = getAveragePowerValue(cpuUtilCil) val delta = (powerCil - powerFlr) / 10 - return if (cpuUtil % 0.1 == 0.0) - getAveragePowerValue((cpuUtil * 10).toInt()) + return if (utilization % 0.1 == 0.0) + getAveragePowerValue((utilization * 10).toInt()) else - powerFlr + delta * (cpuUtil - cpuUtilFlr.toDouble() / 10) * 100 + powerFlr + delta * (utilization - cpuUtilFlr.toDouble() / 10) * 100 } /** diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/LinearPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/LinearPowerModel.kt index 14443aff..56a75bb6 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/LinearPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/LinearPowerModel.kt @@ -12,12 +12,12 @@ package org.opendc.simulator.compute.power public class LinearPowerModel( private var maxPower: Double, staticPowerPercent: Double -) : MachinePowerModel { +) : PowerModel { private var staticPower: Double = staticPowerPercent * maxPower private var constPower: Double = (maxPower - staticPower) / 100 - public override fun computeCpuPower(cpuUtil: Double): Double { - require(cpuUtil in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - return staticPower + constPower * cpuUtil * 100 + public override fun computePower(utilization: Double): Double { + require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } + return staticPower + constPower * utilization * 100 } } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/MachinePowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/MachinePowerModel.kt deleted file mode 100644 index 9bf03b87..00000000 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/MachinePowerModel.kt +++ /dev/null @@ -1,18 +0,0 @@ -package org.opendc.simulator.compute.power - -import org.opendc.simulator.compute.SimMachine - -/** - * A model for estimating the power usage of a [SimMachine]. - */ -public interface MachinePowerModel { - /** - * Computes CPU power consumption for each host. - * - * @param cpuUtil The CPU utilization percentage. - * @return A [Double] value of CPU power consumption. - * @throws IllegalArgumentException Will throw an error if [cpuUtil] is out of range. - */ - @Throws(IllegalArgumentException::class) - public fun computeCpuPower(cpuUtil: Double): Double -} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt new file mode 100644 index 00000000..19dff88c --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt @@ -0,0 +1,16 @@ +package org.opendc.simulator.compute.power + +import org.opendc.simulator.compute.SimMachine + +/** + * A model for estimating the power usage of a [SimMachine]. + */ +public interface PowerModel { + /** + * Compute the power consumption of the CPU based on its utilization. + * + * @param utilization The CPU utilization percentage in [0, 1]. + * @return The power consumption of the CPU in terms of watts (W). + */ + public fun computePower(utilization: Double): Double +} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SqrtPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SqrtPowerModel.kt index bf177aff..b78421d8 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SqrtPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SqrtPowerModel.kt @@ -14,12 +14,12 @@ import kotlin.math.sqrt public class SqrtPowerModel( private var maxPower: Double, staticPowerPercent: Double -) : MachinePowerModel { +) : PowerModel { private var staticPower: Double = staticPowerPercent * maxPower private var constPower: Double = (maxPower - staticPower) / sqrt(100.0) - override fun computeCpuPower(cpuUtil: Double): Double { - require(cpuUtil in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - return staticPower + constPower * sqrt(cpuUtil * 100) + override fun computePower(utilization: Double): Double { + require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } + return staticPower + constPower * sqrt(utilization * 100) } } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SquarePowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SquarePowerModel.kt index cbfad530..b5953daa 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SquarePowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SquarePowerModel.kt @@ -14,12 +14,12 @@ import kotlin.math.pow public class SquarePowerModel( private var maxPower: Double, staticPowerPercent: Double -) : MachinePowerModel { +) : PowerModel { private var staticPower: Double = staticPowerPercent * maxPower private var constPower: Double = (maxPower - staticPower) / 100.0.pow(2) - override fun computeCpuPower(cpuUtil: Double): Double { - require(cpuUtil in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - return staticPower + constPower * (cpuUtil * 100).pow(2) + override fun computePower(utilization: Double): Double { + require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } + return staticPower + constPower * (utilization * 100).pow(2) } } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ZeroIdlePowerDecorator.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ZeroIdlePowerDecorator.kt index 01deac5b..50f04a4f 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ZeroIdlePowerDecorator.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ZeroIdlePowerDecorator.kt @@ -3,10 +3,10 @@ package org.opendc.simulator.compute.power /** * A decorator for ignoring the idle power when computing energy consumption of components. * - * @param delegate The [MachinePowerModel] to delegate to. + * @param delegate The [PowerModel] to delegate to. */ -public class ZeroIdlePowerDecorator(private val delegate: MachinePowerModel) : MachinePowerModel { - override fun computeCpuPower(cpuUtil: Double): Double { - return if (cpuUtil == 0.0) 0.0 else delegate.computeCpuPower(cpuUtil) +public class ZeroIdlePowerDecorator(private val delegate: PowerModel) : PowerModel { + override fun computePower(utilization: Double): Double { + return if (utilization == 0.0) 0.0 else delegate.computePower(utilization) } } -- cgit v1.2.3 From f3384ca33f84fc261aeb20ca7752ab052971dcf4 Mon Sep 17 00:00:00 2001 From: Hongyu Date: Mon, 5 Apr 2021 12:50:19 +0200 Subject: simulator: Polish power models This change updates the power models by fixing some of the documentation and adding toString() methods. --- .../simulator/compute/power/ConstantPowerModel.kt | 8 +- .../simulator/compute/power/CubicPowerModel.kt | 20 ++--- .../compute/power/InterpolationPowerModel.kt | 30 +++---- .../simulator/compute/power/LinearPowerModel.kt | 23 +++--- .../simulator/compute/power/PStatePowerModel.kt | 96 ---------------------- .../opendc/simulator/compute/power/PowerModel.kt | 6 +- .../simulator/compute/power/SqrtPowerModel.kt | 20 ++--- .../simulator/compute/power/SquarePowerModel.kt | 20 ++--- .../compute/power/ZeroIdlePowerDecorator.kt | 7 +- 9 files changed, 61 insertions(+), 169 deletions(-) delete mode 100644 simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PStatePowerModel.kt (limited to 'simulator/opendc-simulator/opendc-simulator-compute/src/main') diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ConstantPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ConstantPowerModel.kt index 1b9733d1..b8cb8412 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ConstantPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ConstantPowerModel.kt @@ -1,8 +1,10 @@ package org.opendc.simulator.compute.power /** - * A power model which produces a constant value [constant]. + * A power model which produces a constant value [power]. */ -public class ConstantPowerModel(private val constant: Double) : PowerModel { - public override fun computePower(utilization: Double): Double = constant +public class ConstantPowerModel(private val power: Double) : PowerModel { + public override fun computePower(utilization: Double): Double = power + + override fun toString(): String = "ConstantPowerModel[power=$power]" } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/CubicPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/CubicPowerModel.kt index e5bc013c..48edace6 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/CubicPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/CubicPowerModel.kt @@ -5,21 +5,15 @@ import kotlin.math.pow /** * The cubic power model partially adapted from CloudSim. * - * @param maxPower The maximum power draw in Watts of the server. - * @param staticPowerPercent The static power percentage. - * @property staticPower The static power consumption that is not dependent on resource usage. - * It is the amount of energy consumed even when the host is idle. - * @property constPower The constant power consumption for each fraction of resource used. + * @param maxPower The maximum power draw of the server in W. + * @param idlePower The power draw of the server in idle state in W. */ -public class CubicPowerModel( - private var maxPower: Double, - staticPowerPercent: Double -) : PowerModel { - private var staticPower: Double = staticPowerPercent * maxPower - private var constPower: Double = (maxPower - staticPower) / 100.0.pow(3) +public class CubicPowerModel(private val maxPower: Double, private val idlePower: Double) : PowerModel { + private val factor: Double = (maxPower - idlePower) / 100.0.pow(3) public override fun computePower(utilization: Double): Double { - require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - return staticPower + constPower * (utilization * 100).pow(3) + return idlePower + factor * (utilization * 100).pow(3) } + + override fun toString(): String = "CubicPowerModel[max=$maxPower,idle=$idlePower]" } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/InterpolationPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/InterpolationPowerModel.kt index ce9b8197..85613a57 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/InterpolationPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/InterpolationPowerModel.kt @@ -3,6 +3,8 @@ package org.opendc.simulator.compute.power import org.yaml.snakeyaml.Yaml import kotlin.math.ceil import kotlin.math.floor +import kotlin.math.max +import kotlin.math.min /** * The linear interpolation power model partially adapted from CloudSim. @@ -10,30 +12,30 @@ import kotlin.math.floor * * @param hardwareName The name of the hardware vendor. * @see Machines used in the SPEC benchmark - * @property averagePowerValues A [List] of average active power measured by the power analyzer(s) - * and accumulated by the PTDaemon (Power and Temperature Daemon) for this - * measurement interval, displayed as watts (W). */ -public class InterpolationPowerModel( - hardwareName: String, -) : PowerModel { +public class InterpolationPowerModel(hardwareName: String) : PowerModel { + /** + * A [List] of average active power measured by the power analyzer(s) and accumulated by the + * PTDaemon (Power and Temperature Daemon) for this measurement interval, displayed as watts (W). + */ private val averagePowerValues: List = loadAveragePowerValue(hardwareName) public override fun computePower(utilization: Double): Double { - require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - - val cpuUtilFlr = floor(utilization * 10).toInt() - val cpuUtilCil = ceil(utilization * 10).toInt() - val powerFlr: Double = getAveragePowerValue(cpuUtilFlr) - val powerCil: Double = getAveragePowerValue(cpuUtilCil) + val clampedUtilization = min(1.0, max(0.0, utilization)) + val utilizationFlr = floor(clampedUtilization * 10).toInt() + val utilizationCil = ceil(clampedUtilization * 10).toInt() + val powerFlr: Double = getAveragePowerValue(utilizationFlr) + val powerCil: Double = getAveragePowerValue(utilizationCil) val delta = (powerCil - powerFlr) / 10 return if (utilization % 0.1 == 0.0) - getAveragePowerValue((utilization * 10).toInt()) + getAveragePowerValue((clampedUtilization * 10).toInt()) else - powerFlr + delta * (utilization - cpuUtilFlr.toDouble() / 10) * 100 + powerFlr + delta * (clampedUtilization - utilizationFlr.toDouble() / 10) * 100 } + override fun toString(): String = "InterpolationPowerModel[entries=${averagePowerValues.size}]" + /** * Gets the power consumption for a given utilization percentage. * diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/LinearPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/LinearPowerModel.kt index 56a75bb6..1a0cc07b 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/LinearPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/LinearPowerModel.kt @@ -3,21 +3,18 @@ package org.opendc.simulator.compute.power /** * The linear power model partially adapted from CloudSim. * - * @param maxPower The maximum power draw in Watts of the server. - * @param staticPowerPercent The static power percentage. - * @property staticPower The static power consumption that is not dependent on resource usage. - * It is the amount of energy consumed even when the host is idle. - * @property constPower The constant power consumption for each fraction of resource used. + * @param maxPower The maximum power draw of the server in W. + * @param idlePower The power draw of the server in idle state in W. */ -public class LinearPowerModel( - private var maxPower: Double, - staticPowerPercent: Double -) : PowerModel { - private var staticPower: Double = staticPowerPercent * maxPower - private var constPower: Double = (maxPower - staticPower) / 100 +public class LinearPowerModel(private val maxPower: Double, private val idlePower: Double) : PowerModel { + /** + * The linear interpolation factor of the model. + */ + private val factor: Double = (maxPower - idlePower) / 100 public override fun computePower(utilization: Double): Double { - require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - return staticPower + constPower * utilization * 100 + return idlePower + factor * utilization * 100 } + + override fun toString(): String = "LinearPowerModel[max=$maxPower,idle=$idlePower]" } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PStatePowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PStatePowerModel.kt deleted file mode 100644 index 722f478d..00000000 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PStatePowerModel.kt +++ /dev/null @@ -1,96 +0,0 @@ -package org.opendc.simulator.compute.power - -import org.opendc.simulator.compute.SimBareMetalMachine -import java.time.Clock -import java.util.* - -/** - * The CPU power model derived from the iCanCloud simulator. - * - * @param machine The [SimBareMetalMachine] that the model is measuring. - * @param clock The virtual [Clock] to track the time spent at each p-state. - * @property cpuPowerMeter A [MutableMap] that contains CPU frequencies ([Double]) in GHz - * as keys and the time ([Long]) spent in that frequency range in seconds. - * @property pStatesToPower A [TreeMap] that contains the frequency ([Double]) of corresponding p-state in GHz - * as keys and the energy ([Double]) consumption of that state in Watts. - * @property pStatesRange A [Pair] in which the fist and second elements are the lower and upper bounds of the - * consumption values of the p-states respectively. - * @property lastMeasureTime The last update time of the [cpuPowerMeter]. - * @property currPState The p-state that the model is currently in. - */ -public class PStatePowerModel( - private val machine: SimBareMetalMachine, - private val clock: Clock, -) { - // TODO: Extract the power meter out of the model. - private val cpuPowerMeter = mutableMapOf() - private val pStatesToPower = TreeMap() - private val pStatesRange: Pair - private var lastMeasureTime: Long - private var currPState: Double - - init { - loadPStates(this) - pStatesRange = Pair(pStatesToPower.keys.first(), pStatesToPower.keys.last()) - pStatesToPower.keys.forEach { cpuPowerMeter[it] = 0L } - currPState = pStatesRange.first - lastMeasureTime = getClockInstant() - updateCpuPowerMeter() - } - - /** Recorde the elapsed time to the corresponding p-state. */ - public fun updateCpuPowerMeter() { - val newMeasureTime = getClockInstant() - val newMaxFreq: Double = getMaxCpuSpeedInGHz() - assert(newMaxFreq in pStatesRange.first..pStatesRange.second) { - "The maximum frequency $newMaxFreq is not in the range of the P-state frequency " + - "from ${pStatesRange.first} to ${pStatesRange.second}." - } - - // Update the current p-state level on which the CPU is running. - val newPState = pStatesToPower.ceilingKey(newMaxFreq) - - // Add the time elapsed to the previous state. - cpuPowerMeter.merge(currPState, newMeasureTime - lastMeasureTime, Long::plus) - - // Update the current states. - currPState = newPState - lastMeasureTime = newMeasureTime - } - - /** Get the power value of the energy consumption level at which the CPU is working. */ - public fun getInstantCpuPower(): Double = - pStatesToPower.getOrDefault(currPState, 0.0) - - /** Get the accumulated power consumption up until now. */ - public fun getAccumulatedCpuPower(): Double = - pStatesToPower.keys - .map { - pStatesToPower.getOrDefault(it, 0.0) * - cpuPowerMeter.getOrDefault(it, 0.0).toDouble() - }.sum() - - private fun getClockInstant() = clock.millis() / 1000 - - /** Get the maximum frequency of the CPUs in GHz as that of the package. - * @see - * on page 34. - */ - private fun getMaxCpuSpeedInGHz() = (machine.speed.maxOrNull() ?: 0.0) / 1000 - - public companion object PStatesLoader { - private fun loadPStates(pStatePowerModel: PStatePowerModel) { - // TODO: Dynamically load configuration. - // See P4 of https://www.intel.com/content/dam/support/us/en/documents/motherboards/server/sb/power_management_of_intel_architecture_servers.pdf - pStatePowerModel.pStatesToPower.putAll( - sortedMapOf( - 3.6 to 103.0, - 3.4 to 94.0, - 3.2 to 85.0, - 3.0 to 76.0, - 2.8 to 8.0, - ) - ) - } - } -} diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt index 19dff88c..1387e65a 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt @@ -7,10 +7,10 @@ import org.opendc.simulator.compute.SimMachine */ public interface PowerModel { /** - * Compute the power consumption of the CPU based on its utilization. + * Computes CPU power consumption for each host. * - * @param utilization The CPU utilization percentage in [0, 1]. - * @return The power consumption of the CPU in terms of watts (W). + * @param utilization The CPU utilization percentage. + * @return A [Double] value of CPU power consumption. */ public fun computePower(utilization: Double): Double } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SqrtPowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SqrtPowerModel.kt index b78421d8..da40ca85 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SqrtPowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SqrtPowerModel.kt @@ -5,21 +5,15 @@ import kotlin.math.sqrt /** * The square root power model partially adapted from CloudSim. * - * @param maxPower The maximum power draw in Watts of the server. - * @param staticPowerPercent The static power percentage. - * @property staticPower The static power consumption that is not dependent on resource usage. - * It is the amount of energy consumed even when the host is idle. - * @property constPower The constant power consumption for each fraction of resource used. + * @param maxPower The maximum power draw of the server in W. + * @param idlePower The power draw of the server in idle state in W. */ -public class SqrtPowerModel( - private var maxPower: Double, - staticPowerPercent: Double -) : PowerModel { - private var staticPower: Double = staticPowerPercent * maxPower - private var constPower: Double = (maxPower - staticPower) / sqrt(100.0) +public class SqrtPowerModel(private val maxPower: Double, private val idlePower: Double) : PowerModel { + private val factor: Double = (maxPower - idlePower) / sqrt(100.0) override fun computePower(utilization: Double): Double { - require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - return staticPower + constPower * sqrt(utilization * 100) + return idlePower + factor * sqrt(utilization * 100) } + + override fun toString(): String = "SqrtPowerModel[max=$maxPower,idle=$idlePower]" } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SquarePowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SquarePowerModel.kt index b5953daa..4f914ddf 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SquarePowerModel.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/SquarePowerModel.kt @@ -5,21 +5,15 @@ import kotlin.math.pow /** * The square power model partially adapted from CloudSim. * - * @param maxPower The maximum power draw in Watts of the server. - * @param staticPowerPercent The static power percentage. - * @property staticPower The static power consumption that is not dependent on resource usage. - * It is the amount of energy consumed even when the host is idle. - * @property constPower The constant power consumption for each fraction of resource used. + * @param maxPower The maximum power draw of the server in W. + * @param idlePower The power draw of the server in idle state in W. */ -public class SquarePowerModel( - private var maxPower: Double, - staticPowerPercent: Double -) : PowerModel { - private var staticPower: Double = staticPowerPercent * maxPower - private var constPower: Double = (maxPower - staticPower) / 100.0.pow(2) +public class SquarePowerModel(private val maxPower: Double, private val idlePower: Double) : PowerModel { + private val factor: Double = (maxPower - idlePower) / 100.0.pow(2) override fun computePower(utilization: Double): Double { - require(utilization in 0.0..1.0) { "CPU utilization must be in [0, 1]" } - return staticPower + constPower * (utilization * 100).pow(2) + return idlePower + factor * (utilization * 100).pow(2) } + + override fun toString(): String = "SquarePowerModel[max=$maxPower,idle=$idlePower]" } diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ZeroIdlePowerDecorator.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ZeroIdlePowerDecorator.kt index 50f04a4f..19dfcadd 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ZeroIdlePowerDecorator.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/ZeroIdlePowerDecorator.kt @@ -7,6 +7,11 @@ package org.opendc.simulator.compute.power */ public class ZeroIdlePowerDecorator(private val delegate: PowerModel) : PowerModel { override fun computePower(utilization: Double): Double { - return if (utilization == 0.0) 0.0 else delegate.computePower(utilization) + return if (utilization == 0.0) + 0.0 + else + delegate.computePower(utilization) } + + override fun toString(): String = "ZeroIdlePowerDecorator[delegate=$delegate]" } -- cgit v1.2.3 From 3860d9e1c042eefacd5accb771cf47990090f649 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 7 Apr 2021 11:11:19 +0200 Subject: simulator: Add tests for CPUFreq subsystem --- .../opendc/simulator/compute/cpufreq/PStateScalingDriver.kt | 13 +++++++------ .../org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'simulator/opendc-simulator/opendc-simulator-compute/src/main') diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt index 8f3eacea..d109e4d8 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt @@ -28,6 +28,7 @@ import org.opendc.simulator.compute.power.PowerModel import org.opendc.simulator.resources.SimResourceSource import java.util.* import kotlin.math.max +import kotlin.math.min /** * A [ScalingDriver] that scales the frequency of the processor based on a discrete set of frequencies. @@ -53,18 +54,18 @@ public class PStateScalingDriver(states: Map) : ScalingDrive } override fun computePower(): Double { - var freq = 0.0 + var targetFreq = 0.0 var totalSpeed = 0.0 - var totalFreq = 0.0 for (ctx in contexts) { - freq = max(ctx.target, freq) + targetFreq = max(ctx.target, targetFreq) totalSpeed += ctx.resource.speed.value - totalFreq += ctx.target } - val (_, model) = states.ceilingEntry(freq) - return model.computePower(totalSpeed / totalFreq) + val maxFreq = states.lastKey() + val (actualFreq, model) = states.ceilingEntry(min(maxFreq, targetFreq)) + val utilization = totalSpeed / (actualFreq * contexts.size) + return model.computePower(utilization) } override fun toString(): String = "PStateScalingDriver.Logic" diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt index 9c6eb9ed..c9aea580 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/ScalingGovernor.kt @@ -47,7 +47,7 @@ public interface ScalingGovernor { public fun onStart() {} /** - * This method is invoked when + * This method is invoked when the governor should re-decide the frequency limits. */ public fun onLimit() {} } -- cgit v1.2.3