diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-03-24 13:05:33 +0100 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-03-24 13:05:33 +0100 |
| commit | 69598598be2c248acc49e40607b3dd0998ec1ca5 (patch) | |
| tree | 62e154b6cc5b22f79f3aa67cf245c40c1332f7b4 /simulator/opendc-compute | |
| parent | 0ad600eb8e14c0ef3ba5529c59d300dc20c85ab2 (diff) | |
simulator: Move power models to simulator module
This change moves the power models from the `opendc-compute-simulator`
to the `opendc-simulator-compute` module, since it better fits the scope
of the models and allows them to be re-used for other purposes.
Diffstat (limited to 'simulator/opendc-compute')
14 files changed, 8 insertions, 467 deletions
diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt index 694676bc..3c4b4410 100644 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt +++ b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt @@ -29,13 +29,12 @@ import org.opendc.compute.api.Flavor import org.opendc.compute.api.Server import org.opendc.compute.api.ServerState import org.opendc.compute.service.driver.* -import org.opendc.compute.simulator.power.api.CpuPowerModel -import org.opendc.compute.simulator.power.api.Powerable -import org.opendc.compute.simulator.power.models.ConstantPowerModel import org.opendc.simulator.compute.* import org.opendc.simulator.compute.interference.IMAGE_PERF_INTERFERENCE_MODEL import org.opendc.simulator.compute.interference.PerformanceInterferenceModel import org.opendc.simulator.compute.model.MemoryUnit +import org.opendc.simulator.compute.power.ConstantPowerModel +import org.opendc.simulator.compute.power.MachinePowerModel import org.opendc.simulator.failures.FailureDomain import org.opendc.utils.flow.EventFlow import java.time.Clock @@ -54,9 +53,9 @@ public class SimHost( context: CoroutineContext, clock: Clock, hypervisor: SimHypervisorProvider, - cpuPowerModel: CpuPowerModel = ConstantPowerModel(0.0), - private val mapper: SimWorkloadMapper = SimMetaWorkloadMapper() -) : Host, FailureDomain, Powerable, AutoCloseable { + powerModel: MachinePowerModel = ConstantPowerModel(0.0), + private val mapper: SimWorkloadMapper = SimMetaWorkloadMapper(), +) : Host, FailureDomain, AutoCloseable { /** * The [CoroutineScope] of the host bounded by the lifecycle of the host. */ @@ -84,7 +83,7 @@ public class SimHost( /** * The machine to run on. */ - public val machine: SimBareMetalMachine = SimBareMetalMachine(context, clock, model) + public val machine: SimBareMetalMachine = SimBareMetalMachine(context, clock, model, powerModel) /** * The hypervisor to run multiple workloads. @@ -133,8 +132,6 @@ public class SimHost( override val model: HostModel = HostModel(model.cpus.size, model.memory.map { it.size }.sum()) - override val powerDraw: Flow<Double> = cpuPowerModel.getPowerDraw(machine) - init { // Launch hypervisor onto machine scope.launch { @@ -223,7 +220,7 @@ public class SimHost( } private fun onGuestStart(vm: Guest) { - guests.forEach { _, guest -> + guests.forEach { (_, guest) -> if (guest.state == ServerState.RUNNING) { vm.performanceInterferenceModel?.onStart(vm.server.image.name) } @@ -233,7 +230,7 @@ public class SimHost( } private fun onGuestStop(vm: Guest) { - guests.forEach { _, guest -> + guests.forEach { (_, guest) -> if (guest.state == ServerState.RUNNING) { vm.performanceInterferenceModel?.onStop(vm.server.image.name) } diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/api/CpuPowerModel.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/api/CpuPowerModel.kt deleted file mode 100644 index 893f7ab1..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/api/CpuPowerModel.kt +++ /dev/null @@ -1,28 +0,0 @@ -package org.opendc.compute.simulator.power.api - -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map -import org.opendc.simulator.compute.SimMachine - -public interface CpuPowerModel { - /** - * 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 - - /** - * Emits the values of power consumption for servers. - * - * @param machine The [SimMachine] that the model is measuring. - * @return A [Flow] of values representing the server power draw. - */ - public fun getPowerDraw(machine: SimMachine): Flow<Double> = - machine.usage.map { - computeCpuPower(it) - } -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/api/Powerable.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/api/Powerable.kt deleted file mode 100644 index 780f2a29..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/api/Powerable.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 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.compute.simulator.power.api - -import kotlinx.coroutines.flow.Flow - -/** - * An entity that is uses power from some power source. - */ -public interface Powerable { - /** - * The power draw at the device's power supply in watts (W).w - */ - public val powerDraw: Flow<Double> -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/ConstantPowerModel.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/ConstantPowerModel.kt deleted file mode 100644 index 5e80827b..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/ConstantPowerModel.kt +++ /dev/null @@ -1,10 +0,0 @@ -package org.opendc.compute.simulator.power.models - -import org.opendc.compute.simulator.power.api.CpuPowerModel - -/** - * A power model which produces a constant value [constant]. - */ -public class ConstantPowerModel(private val constant: Double) : CpuPowerModel { - public override fun computeCpuPower(cpuUtil: Double): Double = constant -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/CubicPowerModel.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/CubicPowerModel.kt deleted file mode 100644 index 9008a987..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/CubicPowerModel.kt +++ /dev/null @@ -1,26 +0,0 @@ -package org.opendc.compute.simulator.power.models - -import org.opendc.compute.simulator.power.api.CpuPowerModel -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. - */ -public class CubicPowerModel( - private var maxPower: Double, - staticPowerPercent: Double -) : CpuPowerModel { - 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) - } -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/InterpolationPowerModel.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/InterpolationPowerModel.kt deleted file mode 100644 index b01957e4..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/InterpolationPowerModel.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.opendc.compute.simulator.power.models - -import org.opendc.compute.simulator.power.api.CpuPowerModel -import kotlin.math.ceil -import kotlin.math.floor - -/** - * The linear interpolation 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. - */ -public abstract class InterpolationPowerModel : CpuPowerModel { - - 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 power1: Double = getPowerData(cpuUtilFlr) - val power2: Double = getPowerData(cpuUtilCil) - val delta = (power2 - power1) / 10 - - return if (cpuUtil % 0.1 == 0.0) - getPowerData((cpuUtil * 10).toInt()) - else - power1 + delta * (cpuUtil - cpuUtilFlr.toDouble() / 10) * 100 - } - - public abstract fun getPowerData(index: Int): Double -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/LinearPowerModel.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/LinearPowerModel.kt deleted file mode 100644 index 913095ad..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/LinearPowerModel.kt +++ /dev/null @@ -1,25 +0,0 @@ -package org.opendc.compute.simulator.power.models - -import org.opendc.compute.simulator.power.api.CpuPowerModel - -/** - * 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. - */ -public class LinearPowerModel( - private var maxPower: Double, - staticPowerPercent: Double -) : CpuPowerModel { - 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 - } -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/PStatePowerModel.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/PStatePowerModel.kt deleted file mode 100644 index aea089da..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/PStatePowerModel.kt +++ /dev/null @@ -1,96 +0,0 @@ -package org.opendc.compute.simulator.power.models - -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-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/SqrtPowerModel.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/SqrtPowerModel.kt deleted file mode 100644 index 85d94ffc..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/SqrtPowerModel.kt +++ /dev/null @@ -1,26 +0,0 @@ -package org.opendc.compute.simulator.power.models - -import org.opendc.compute.simulator.power.api.CpuPowerModel -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. - */ -public class SqrtPowerModel( - private var maxPower: Double, - staticPowerPercent: Double -) : CpuPowerModel { - 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) - } -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/SquarePowerModel.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/SquarePowerModel.kt deleted file mode 100644 index 5f44aa3c..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/SquarePowerModel.kt +++ /dev/null @@ -1,26 +0,0 @@ -package org.opendc.compute.simulator.power.models - -import org.opendc.compute.simulator.power.api.CpuPowerModel -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. - */ -public class SquarePowerModel( - private var maxPower: Double, - staticPowerPercent: Double -) : CpuPowerModel { - 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) - } -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/ZeroIdlePowerDecorator.kt b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/ZeroIdlePowerDecorator.kt deleted file mode 100644 index b0c3fa4c..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/ZeroIdlePowerDecorator.kt +++ /dev/null @@ -1,14 +0,0 @@ -package org.opendc.compute.simulator.power.models - -import org.opendc.compute.simulator.power.api.CpuPowerModel - -/** - * A decorator for ignoring the idle power when computing energy consumption of components. - * - * @param delegate The [CpuPowerModel] to delegate to. - */ -public class ZeroIdlePowerDecorator(private val delegate: CpuPowerModel) : CpuPowerModel { - override fun computeCpuPower(cpuUtil: Double): Double { - return if (cpuUtil == 0.0) 0.0 else delegate.computeCpuPower(cpuUtil) - } -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/package-info.java b/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/package-info.java deleted file mode 100644 index 9354f1f9..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/power/models/package-info.java +++ /dev/null @@ -1,12 +0,0 @@ -/** - * Contains a set of energy models, which are developed based on that of the following cloud simulators. - * N.B. Different configurations and new features may have been introduced. - * - * CloudSim: - * @see <a href="http://dx.doi.org/10.1002/cpe.1867">Anton Beloglazov, and Rajkumar Buyya, "Optimal Online Deterministic - * Algorithms and Adaptive Heuristics for Energy and Performance Efficient Dynamic Consolidation of Virtual Machines in - * Cloud Data Centers", Concurrency and Computation: Practice and Experience (CCPE), Volume 24, - * Issue 13, Pages: 1397-1420, John Wiley & Sons, Ltd, New York, USA, 2012</a> - */ -package org.opendc.compute.simulator.power.models; -// rest of the file is empty diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/power/CpuPowerModelTest.kt b/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/power/CpuPowerModelTest.kt deleted file mode 100644 index cd18b120..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/power/CpuPowerModelTest.kt +++ /dev/null @@ -1,77 +0,0 @@ -package org.opendc.compute.simulator.power - -import io.mockk.* -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.flow.* -import kotlinx.coroutines.test.runBlockingTest -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.Arguments -import org.junit.jupiter.params.provider.MethodSource -import org.opendc.compute.simulator.power.api.CpuPowerModel -import org.opendc.compute.simulator.power.models.* -import org.opendc.simulator.compute.SimBareMetalMachine -import java.util.stream.Stream -import kotlin.math.pow - -@OptIn(ExperimentalCoroutinesApi::class) -internal class CpuPowerModelTest { - private val epsilon = 10.0.pow(-3) - private val cpuUtil = 0.9 - - @ParameterizedTest - @MethodSource("cpuPowerModelArgs") - fun `compute power consumption given CPU loads`( - powerModel: CpuPowerModel, - expectedPowerConsumption: Double - ) { - val computedPowerConsumption = powerModel.computeCpuPower(cpuUtil) - assertEquals(expectedPowerConsumption, computedPowerConsumption, epsilon) - } - - @ParameterizedTest - @MethodSource("cpuPowerModelArgs") - fun `ignore idle power when computing power consumptions`( - powerModel: CpuPowerModel, - expectedPowerConsumption: Double - ) { - val zeroPowerModel = ZeroIdlePowerDecorator(powerModel) - val computedPowerConsumption = zeroPowerModel.computeCpuPower(0.0) - assertEquals(0.0, computedPowerConsumption) - } - - @ParameterizedTest - @MethodSource("cpuPowerModelArgs") - fun `emit power draw for hosts by different models`( - powerModel: CpuPowerModel, - expectedPowerConsumption: Double - ) { - runBlockingTest { - val cpuLoads = flowOf(cpuUtil, cpuUtil, cpuUtil).stateIn(this) - val machine = mockkClass(SimBareMetalMachine::class) - every { machine.usage } returns cpuLoads - - val serverPowerDraw = powerModel.getPowerDraw(machine) - - assertEquals( - serverPowerDraw.first().toDouble(), - flowOf(expectedPowerConsumption).first().toDouble(), - epsilon - ) - - verify(exactly = 1) { machine.usage } - } - } - - @Suppress("unused") - private companion object { - @JvmStatic - fun cpuPowerModelArgs(): Stream<Arguments> = Stream.of( - Arguments.of(ConstantPowerModel(0.0), 0.0), - Arguments.of(LinearPowerModel(350.0, 200 / 350.0), 335.0), - Arguments.of(SquarePowerModel(350.0, 200 / 350.0), 321.5), - Arguments.of(CubicPowerModel(350.0, 200 / 350.0), 309.35), - Arguments.of(SqrtPowerModel(350.0, 200 / 350.0), 342.302), - ) - } -} diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/power/PStatePowerModelTest.kt b/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/power/PStatePowerModelTest.kt deleted file mode 100644 index e144e541..00000000 --- a/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/power/PStatePowerModelTest.kt +++ /dev/null @@ -1,45 +0,0 @@ -package org.opendc.compute.simulator.power - -import io.mockk.* -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.opendc.compute.simulator.power.models.PStatePowerModel -import org.opendc.simulator.compute.SimBareMetalMachine -import java.time.Clock - -internal class PStatePowerModelTest { - @Test - fun `update CPU power meter with P-states`() { - val p0Power = 8.0 - val p3Power = 94.0 - val p4Power = 103.0 - val expectedP0Power = 8.0 * 10 - val expectedP0P4Power = expectedP0Power + 103.0 * 10 - - val clock = mockkClass(Clock::class) - val machine = mockkClass(SimBareMetalMachine::class) - every { clock.millis() } returnsMany listOf(0L, 0L, 10_000L, 20_000L) - every { machine.speed } returns - listOf(2.8, 2.8, 2.8, 2.8).map { it * 1000 } andThen // Max. 2.8MHz covered by P0 - listOf(1.5, 3.1, 3.3, 3.6).map { it * 1000 } andThen // Max. 3.6MHz covered by P4 - listOf(1.5, 3.1, 3.1, 3.3).map { it * 1000 } // Max. 3.3MHz covered by P3 - - // Power meter initialization. - val pStatePowerModel = PStatePowerModel(machine, clock) - verify(exactly = 2) { clock.millis() } - verify(exactly = 1) { machine.speed } - assertEquals(p0Power, pStatePowerModel.getInstantCpuPower()) - - // The first measure. - pStatePowerModel.updateCpuPowerMeter() - assertEquals(p4Power, pStatePowerModel.getInstantCpuPower()) - assertEquals(expectedP0Power, pStatePowerModel.getAccumulatedCpuPower()) - - // The second measure. - pStatePowerModel.updateCpuPowerMeter() - assertEquals(p3Power, pStatePowerModel.getInstantCpuPower()) - assertEquals(expectedP0P4Power, pStatePowerModel.getAccumulatedCpuPower()) - - verify(exactly = 4) { clock.millis() } - } -} |
