summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-power/src/main
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-06-14 13:57:25 +0200
committerGitHub <noreply@github.com>2021-06-14 13:57:25 +0200
commite24e7c1601257cf27d71db8ca3ae273b24ab06ed (patch)
tree913777f9e7c0008abaaf18445932583e4b01c972 /opendc-simulator/opendc-simulator-power/src/main
parente11cc719201b1e09a30fc88a30524219a17a1af0 (diff)
parentb5826e9dcf4a6b510d26168ba02b1781b3b6c521 (diff)
simulator: Add energy modelling subsystem to OpenDC
This pull request adds a subsystem to OpenDC for modelling power components in datacenters, such as UPSes, PDUs and PSUs. These components also take into account electrical losses that occur in real-world scenarios. - Add module for datacenter power components (UPS, PDU) - Integrate power subsystem with compute subsystem (PSU) - Model power loss in power components **Breaking API Changes** 1. `SimBareMetalMachine.powerDraw` is replaced by `SimBareMetalMachine.psu.powerDraw`
Diffstat (limited to 'opendc-simulator/opendc-simulator-power/src/main')
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt104
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt48
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerOutlet.kt80
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt54
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt107
5 files changed, 393 insertions, 0 deletions
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt
new file mode 100644
index 00000000..11034a57
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt
@@ -0,0 +1,104 @@
+/*
+ * 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.power
+
+import org.opendc.simulator.resources.*
+
+/**
+ * A model of a Power Distribution Unit (PDU).
+ *
+ * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ * @param idlePower The idle power consumption of the PDU independent of the load on the PDU.
+ * @param lossCoefficient The coefficient for the power loss of the PDU proportional to the square load.
+ */
+public class SimPdu(
+ interpreter: SimResourceInterpreter,
+ private val idlePower: Double = 0.0,
+ private val lossCoefficient: Double = 0.0,
+) : SimPowerInlet() {
+ /**
+ * The [SimResourceDistributor] that distributes the electricity over the PDU outlets.
+ */
+ private val distributor = SimResourceDistributorMaxMin(interpreter)
+
+ /**
+ * Create a new PDU outlet.
+ */
+ public fun newOutlet(): Outlet = Outlet(distributor.newOutput())
+
+ override fun createConsumer(): SimResourceConsumer = object : SimResourceConsumer by distributor {
+ override fun onNext(ctx: SimResourceContext): SimResourceCommand {
+ return when (val cmd = distributor.onNext(ctx)) {
+ is SimResourceCommand.Consume -> {
+ val duration = cmd.work / cmd.limit
+ val loss = computePowerLoss(cmd.limit)
+ val newLimit = cmd.limit + loss
+
+ SimResourceCommand.Consume(duration * newLimit, newLimit, cmd.deadline)
+ }
+ is SimResourceCommand.Idle -> {
+ val loss = computePowerLoss(0.0)
+ if (loss > 0.0)
+ SimResourceCommand.Consume(Double.POSITIVE_INFINITY, loss, cmd.deadline)
+ else
+ cmd
+ }
+ else -> cmd
+ }
+ }
+
+ override fun toString(): String = "SimPdu.Consumer"
+ }
+
+ override fun toString(): String = "SimPdu"
+
+ /**
+ * Compute the power loss that occurs in the PDU.
+ */
+ private fun computePowerLoss(load: Double): Double {
+ // See https://download.schneider-electric.com/files?p_Doc_Ref=SPD_NRAN-66CK3D_EN
+ return idlePower + lossCoefficient * (load * load)
+ }
+
+ /**
+ * A PDU outlet.
+ */
+ public class Outlet(private val provider: SimResourceProvider) : SimPowerOutlet(), AutoCloseable {
+ override fun onConnect(inlet: SimPowerInlet) {
+ provider.startConsumer(inlet.createConsumer())
+ }
+
+ override fun onDisconnect(inlet: SimPowerInlet) {
+ provider.cancel()
+ }
+
+ /**
+ * Remove the outlet from the PDU.
+ */
+ override fun close() {
+ provider.close()
+ }
+
+ override fun toString(): String = "SimPdu.Outlet"
+ }
+}
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt
new file mode 100644
index 00000000..0ac1f199
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt
@@ -0,0 +1,48 @@
+/*
+ * 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.power
+
+import org.opendc.simulator.resources.SimResourceConsumer
+
+/**
+ * An abstract inlet that consumes electricity from a power outlet.
+ */
+public abstract class SimPowerInlet {
+ /**
+ * A flag to indicate that the inlet is currently connected to an outlet.
+ */
+ public val isConnected: Boolean
+ get() = _outlet != null
+
+ /**
+ * The [SimPowerOutlet] to which the inlet is connected.
+ */
+ public val outlet: SimPowerOutlet?
+ get() = _outlet
+ internal var _outlet: SimPowerOutlet? = null
+
+ /**
+ * Create a [SimResourceConsumer] which represents the consumption of electricity from the power outlet.
+ */
+ public abstract fun createConsumer(): SimResourceConsumer
+}
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerOutlet.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerOutlet.kt
new file mode 100644
index 00000000..72f52acc
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerOutlet.kt
@@ -0,0 +1,80 @@
+/*
+ * 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.power
+
+/**
+ * An abstract outlet that provides a source of electricity for datacenter components.
+ */
+public abstract class SimPowerOutlet {
+ /**
+ * A flag to indicate that the inlet is currently connected to an outlet.
+ */
+ public val isConnected: Boolean
+ get() = _inlet != null
+
+ /**
+ * The inlet that is connected to this outlet currently.
+ */
+ public val inlet: SimPowerInlet?
+ get() = _inlet
+ private var _inlet: SimPowerInlet? = null
+
+ /**
+ * Connect the specified power [inlet] to this outlet.
+ *
+ * @param inlet The inlet to connect to the outlet.
+ */
+ public fun connect(inlet: SimPowerInlet) {
+ check(!isConnected) { "Outlet already connected" }
+ check(!inlet.isConnected) { "Inlet already connected" }
+
+ _inlet = inlet
+ inlet._outlet = this
+
+ onConnect(inlet)
+ }
+
+ /**
+ * Disconnect the connected power outlet from this inlet
+ */
+ public fun disconnect() {
+ val inlet = _inlet
+ if (inlet != null) {
+ _inlet = null
+ assert(inlet._outlet == this) { "Inlet state incorrect" }
+ inlet._outlet = null
+
+ onDisconnect(inlet)
+ }
+ }
+
+ /**
+ * This method is invoked when an inlet is connected to the outlet.
+ */
+ protected abstract fun onConnect(inlet: SimPowerInlet)
+
+ /**
+ * This method is invoked when an inlet is disconnected from the outlet.
+ */
+ protected abstract fun onDisconnect(inlet: SimPowerInlet)
+}
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt
new file mode 100644
index 00000000..3ef8ccc6
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.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.power
+
+import org.opendc.simulator.resources.SimResourceInterpreter
+import org.opendc.simulator.resources.SimResourceSource
+
+/**
+ * A [SimPowerOutlet] that represents a source of electricity.
+ *
+ * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ */
+public class SimPowerSource(interpreter: SimResourceInterpreter, public val capacity: Double) : SimPowerOutlet() {
+ /**
+ * The resource source that drives this power source.
+ */
+ private val source = SimResourceSource(capacity, interpreter)
+
+ /**
+ * The power draw at this instant.
+ */
+ public val powerDraw: Double
+ get() = source.speed
+
+ override fun onConnect(inlet: SimPowerInlet) {
+ source.startConsumer(inlet.createConsumer())
+ }
+
+ override fun onDisconnect(inlet: SimPowerInlet) {
+ source.cancel()
+ }
+
+ override fun toString(): String = "SimPowerSource"
+}
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt
new file mode 100644
index 00000000..f9431d21
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt
@@ -0,0 +1,107 @@
+/*
+ * 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.power
+
+import org.opendc.simulator.resources.*
+
+/**
+ * A model of an Uninterruptible Power Supply (UPS).
+ *
+ * This model aggregates multiple power sources into a single source in order to ensure that power is always available.
+ *
+ * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ * @param idlePower The idle power consumption of the UPS independent of the load.
+ * @param lossCoefficient The coefficient for the power loss of the UPS proportional to the load.
+ */
+public class SimUps(
+ interpreter: SimResourceInterpreter,
+ private val idlePower: Double = 0.0,
+ private val lossCoefficient: Double = 0.0,
+) : SimPowerOutlet() {
+ /**
+ * The resource aggregator used to combine the input sources.
+ */
+ private val aggregator = SimResourceAggregatorMaxMin(interpreter)
+
+ /**
+ * Create a new UPS outlet.
+ */
+ public fun newInlet(): SimPowerInlet {
+ val forward = SimResourceForwarder(isCoupled = true)
+ aggregator.addInput(forward)
+ return Inlet(forward)
+ }
+
+ override fun onConnect(inlet: SimPowerInlet) {
+ val consumer = inlet.createConsumer()
+ aggregator.startConsumer(object : SimResourceConsumer by consumer {
+ override fun onNext(ctx: SimResourceContext): SimResourceCommand {
+ return when (val cmd = consumer.onNext(ctx)) {
+ is SimResourceCommand.Consume -> {
+ val duration = cmd.work / cmd.limit
+ val loss = computePowerLoss(cmd.limit)
+ val newLimit = cmd.limit + loss
+
+ SimResourceCommand.Consume(duration * newLimit, newLimit, cmd.deadline)
+ }
+ is SimResourceCommand.Idle -> {
+ val loss = computePowerLoss(0.0)
+ if (loss > 0.0)
+ SimResourceCommand.Consume(Double.POSITIVE_INFINITY, loss, cmd.deadline)
+ else
+ cmd
+ }
+ else -> cmd
+ }
+ }
+ })
+ }
+
+ override fun onDisconnect(inlet: SimPowerInlet) {
+ aggregator.cancel()
+ }
+
+ /**
+ * Compute the power loss that occurs in the UPS.
+ */
+ private fun computePowerLoss(load: Double): Double {
+ // See https://download.schneider-electric.com/files?p_Doc_Ref=SPD_NRAN-66CK3D_EN
+ return idlePower + lossCoefficient * load
+ }
+
+ /**
+ * A UPS inlet.
+ */
+ public inner class Inlet(private val forwarder: SimResourceTransformer) : SimPowerInlet(), AutoCloseable {
+ override fun createConsumer(): SimResourceConsumer = forwarder
+
+ /**
+ * Remove the inlet from the PSU.
+ */
+ override fun close() {
+ forwarder.close()
+ }
+
+ override fun toString(): String = "SimPsu.Inlet"
+ }
+}