From 4e127b7cef0bbc186766af9631f538992ff7dfaf Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Tue, 10 Mar 2020 23:36:25 +0100 Subject: feat: Implement basic power model --- .../opendc/compute/metal/driver/BareMetalDriver.kt | 3 +- .../compute/metal/driver/SimpleBareMetalDriver.kt | 11 ++++-- .../opendc/compute/metal/power/PowerModels.kt | 42 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/power/PowerModels.kt (limited to 'opendc/opendc-compute/src/main') diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/BareMetalDriver.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/BareMetalDriver.kt index 57c62c31..330eecfd 100644 --- a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/BareMetalDriver.kt +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/BareMetalDriver.kt @@ -28,12 +28,13 @@ import com.atlarge.opendc.compute.core.image.Image import com.atlarge.opendc.compute.core.monitor.ServerMonitor import com.atlarge.opendc.compute.metal.Node import com.atlarge.opendc.compute.metal.PowerState +import com.atlarge.opendc.core.power.Powerable import kotlinx.coroutines.flow.Flow /** * A driver interface for the management interface of a bare-metal compute node. */ -public interface BareMetalDriver { +public interface BareMetalDriver : Powerable { /** * The load of the machine. */ diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt index 90080b91..a3738a1d 100644 --- a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt @@ -37,6 +37,8 @@ import com.atlarge.opendc.compute.core.image.Image import com.atlarge.opendc.compute.core.monitor.ServerMonitor import com.atlarge.opendc.compute.metal.Node import com.atlarge.opendc.compute.metal.PowerState +import com.atlarge.opendc.compute.metal.power.ConstantPowerModel +import com.atlarge.opendc.core.power.PowerModel import kotlinx.coroutines.CancellationException import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.FlowPreview @@ -56,18 +58,20 @@ import kotlinx.coroutines.withContext /** * A basic implementation of the [BareMetalDriver] that simulates an [Image] running on a bare-metal machine. * + * @param domain The simulation domain the driver runs in. * @param uid The unique identifier of the machine. * @param name An optional name of the machine. * @param cpus The CPUs available to the bare metal machine. * @param memoryUnits The memory units in this machine. - * @param domain The simulation domain the driver runs in. + * @param powerModel The power model of this machine. */ public class SimpleBareMetalDriver( + private val domain: Domain, uid: UUID, name: String, val cpus: List, val memoryUnits: List, - private val domain: Domain + powerModel: PowerModel = ConstantPowerModel(0.0) ) : BareMetalDriver { /** * The monitor to use. @@ -98,8 +102,11 @@ public class SimpleBareMetalDriver( @UseExperimental(FlowPreview::class) override val load: Flow = loadChannel.asFlow() + override val powerDraw: Flow + init { loadChannel.offer(0.0) + powerDraw = powerModel(this) } override suspend fun init(monitor: ServerMonitor): Node = withContext(domain.coroutineContext) { diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/power/PowerModels.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/power/PowerModels.kt new file mode 100644 index 00000000..a4f400ca --- /dev/null +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/power/PowerModels.kt @@ -0,0 +1,42 @@ +/* + * 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 com.atlarge.opendc.compute.metal.power + +import com.atlarge.opendc.compute.metal.driver.BareMetalDriver +import com.atlarge.opendc.core.power.PowerModel +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.map + +/** + * A power model which emits a single value. + */ +public fun ConstantPowerModel(value: Double): PowerModel = { _ -> flowOf(value) } + +/** + * A power model that assumes a naive linear relation between power usage and host CPU utilization. + */ +public fun LinearLoadPowerModel(base: Double, multiplier: Double): PowerModel = { driver -> + driver.load.map { load -> base + multiplier * load } +} -- cgit v1.2.3