diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-04-07 13:07:28 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-07 13:07:28 +0200 |
| commit | 519141f9af525a853b40eb821e70ca209bc104bf (patch) | |
| tree | 3c8a4a3db41ac5be3a67f6427289a858805f98dc /simulator/opendc-simulator/opendc-simulator-compute/src/main | |
| parent | 8f2855b9b9d81b15ff431775ba584c1e3f4add2c (diff) | |
| parent | 3860d9e1c042eefacd5accb771cf47990090f649 (diff) | |
simulator: Add CPUFreq subsystem
This pull request adds a CPUFreq subsystem to the simulator module. This subsystem allows a simulated machine to perform frequency scaling, which in turn should reduce energy consumption.
Diffstat (limited to 'simulator/opendc-simulator/opendc-simulator-compute/src/main')
17 files changed, 472 insertions, 188 deletions
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<Double> = 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<ProcessingUnit, SimResourceSource> = 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<Double> = 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..d109e4d8 --- /dev/null +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriver.kt @@ -0,0 +1,88 @@ +/* + * 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 +import kotlin.math.min + +/** + * 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<Double, PowerModel>) : 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<ScalingContextImpl>() + + override fun createContext(cpu: ProcessingUnit, resource: SimResourceSource): ScalingContext { + val ctx = ScalingContextImpl(machine, cpu, resource) + contexts.add(ctx) + return ctx + } + + override fun computePower(): Double { + var targetFreq = 0.0 + var totalSpeed = 0.0 + + for (ctx in contexts) { + targetFreq = max(ctx.target, targetFreq) + totalSpeed += ctx.resource.speed.value + } + + 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" + } + + 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..c9aea580 --- /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 the governor should re-decide the frequency limits. + */ + 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..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) : MachinePowerModel { - public override fun computeCpuPower(cpuUtil: 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 8e47f571..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 -) : MachinePowerModel { - 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 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 { + 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 69fc0514..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 <a href="http://www.spec.org/power_ssj2008/results/res2011q1/">Machines used in the SPEC benchmark</a> - * @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, -) : MachinePowerModel { +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<Double> = loadAveragePowerValue(hardwareName) - public override fun computeCpuPower(cpuUtil: Double): Double { - require(cpuUtil 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 powerFlr: Double = getAveragePowerValue(cpuUtilFlr) - val powerCil: Double = getAveragePowerValue(cpuUtilCil) + public override fun computePower(utilization: Double): Double { + 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 (cpuUtil % 0.1 == 0.0) - getAveragePowerValue((cpuUtil * 10).toInt()) + return if (utilization % 0.1 == 0.0) + getAveragePowerValue((clampedUtilization * 10).toInt()) else - powerFlr + delta * (cpuUtil - 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 14443aff..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 -) : MachinePowerModel { - 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 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 { + 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<Double, Long>() - private val pStatesToPower = TreeMap<Double, Double>() - private val pStatesRange: Pair<Double, Double> - 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 <a href="https://www.intel.vn/content/dam/www/public/us/en/documents/datasheets/10th-gen-core-families-datasheet-vol-1-datasheet.pdf"> - * 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/MachinePowerModel.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerModel.kt index 9bf03b87..1387e65a 100644 --- 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/PowerModel.kt @@ -5,14 +5,12 @@ import org.opendc.simulator.compute.SimMachine /** * A model for estimating the power usage of a [SimMachine]. */ -public interface MachinePowerModel { +public interface PowerModel { /** * Computes CPU power consumption for each host. * - * @param cpuUtil The CPU utilization percentage. + * @param utilization 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 + 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..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 -) : MachinePowerModel { - 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 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 { + 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 cbfad530..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 -) : MachinePowerModel { - 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 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 { + 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 01deac5b..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 @@ -3,10 +3,15 @@ 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) } + + override fun toString(): String = "ZeroIdlePowerDecorator[delegate=$delegate]" } |
