summaryrefslogtreecommitdiff
path: root/opendc-simulator
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-simulator')
-rw-r--r--opendc-simulator/opendc-simulator-compute/build.gradle.kts1
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt23
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimPsu.kt79
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerDriver.kt10
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt9
-rw-r--r--opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSource.kt6
6 files changed, 110 insertions, 18 deletions
diff --git a/opendc-simulator/opendc-simulator-compute/build.gradle.kts b/opendc-simulator/opendc-simulator-compute/build.gradle.kts
index 4eb0be33..41cdd40c 100644
--- a/opendc-simulator/opendc-simulator-compute/build.gradle.kts
+++ b/opendc-simulator/opendc-simulator-compute/build.gradle.kts
@@ -32,6 +32,7 @@ plugins {
dependencies {
api(platform(projects.opendcPlatform))
api(projects.opendcSimulator.opendcSimulatorResources)
+ api(projects.opendcSimulator.opendcSimulatorPower)
implementation(projects.opendcSimulator.opendcSimulatorCore)
implementation(projects.opendcUtils)
implementation(libs.yaml)
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt
index 642873fd..45d15692 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt
+++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimBareMetalMachine.kt
@@ -31,7 +31,7 @@ import org.opendc.simulator.resources.SimResourceInterpreter
* A simulated bare-metal machine that is able to run a single workload.
*
* A [SimBareMetalMachine] is a stateful object and you should be careful when operating this object concurrently. For
- * example. The class expects only a single concurrent call to [run].
+ * example, the class expects only a single concurrent call to [run].
*
* @param interpreter The [SimResourceInterpreter] to drive the simulation.
* @param model The machine model to simulate.
@@ -44,25 +44,28 @@ public class SimBareMetalMachine(
powerDriver: PowerDriver,
parent: SimResourceSystem? = null,
) : SimAbstractMachine(interpreter, parent, model) {
+ /**
+ * The processing units of the machine.
+ */
override val cpus: List<SimProcessingUnit> = model.cpus.map { cpu ->
Cpu(SimResourceSource(cpu.frequency, interpreter, this@SimBareMetalMachine), cpu)
}
/**
- * Construct the [PowerDriver.Logic] for this machine.
+ * The power supply of this bare-metal machine.
*/
- private val powerDriver = powerDriver.createLogic(this, cpus)
+ public val psu: SimPsu = object : SimPsu() {
+ /**
+ * The logic for the CPU power driver.
+ */
+ private val cpuLogic = powerDriver.createLogic(this@SimBareMetalMachine, cpus)
- /**
- * The power draw of the machine.
- */
- public var powerDraw: Double = 0.0
- private set
+ override fun computePower(): Double = cpuLogic.computePower()
+ }
override fun updateUsage(usage: Double) {
super.updateUsage(usage)
-
- powerDraw = powerDriver.computePower()
+ psu.update()
}
/**
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimPsu.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimPsu.kt
new file mode 100644
index 00000000..8837eff3
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/SimPsu.kt
@@ -0,0 +1,79 @@
+/*
+ * 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
+
+import org.opendc.simulator.power.SimPowerInlet
+import org.opendc.simulator.resources.SimResourceCommand
+import org.opendc.simulator.resources.SimResourceConsumer
+import org.opendc.simulator.resources.SimResourceContext
+import org.opendc.simulator.resources.SimResourceEvent
+
+/**
+ * A power supply of a [SimBareMetalMachine].
+ */
+public abstract class SimPsu : SimPowerInlet() {
+ /**
+ * The power draw of the machine at this instant.
+ */
+ public val powerDraw: Double
+ get() = _powerDraw
+ private var _powerDraw = 0.0
+
+ /**
+ * The consumer context.
+ */
+ private var _ctx: SimResourceContext? = null
+
+ override fun createConsumer(): SimResourceConsumer = object : SimResourceConsumer {
+ override fun onNext(ctx: SimResourceContext): SimResourceCommand {
+ val powerDraw = _powerDraw
+ return if (powerDraw > 0.0)
+ SimResourceCommand.Consume(Double.POSITIVE_INFINITY, powerDraw, Long.MAX_VALUE)
+ else
+ SimResourceCommand.Idle()
+ }
+
+ override fun onEvent(ctx: SimResourceContext, event: SimResourceEvent) {
+ when (event) {
+ SimResourceEvent.Start -> _ctx = ctx
+ SimResourceEvent.Exit -> _ctx = null
+ else -> {}
+ }
+ }
+ }
+
+ /**
+ * Update the power draw of the PSU.
+ */
+ public fun update() {
+ _powerDraw = computePower()
+ _ctx?.interrupt()
+ }
+
+ /**
+ * Compute the power draw of the PSU.
+ */
+ protected abstract fun computePower(): Double
+
+ override fun toString(): String = "SimPsu[draw=$_powerDraw]"
+}
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerDriver.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerDriver.kt
index a1a2b911..1a46dd4a 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerDriver.kt
+++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/power/PowerDriver.kt
@@ -26,22 +26,22 @@ import org.opendc.simulator.compute.SimMachine
import org.opendc.simulator.compute.SimProcessingUnit
/**
- * A [PowerDriver] is responsible for switching the processor to the correct frequency.
+ * A [PowerDriver] is responsible for tracking the power usage for a component of the machine.
*/
public interface PowerDriver {
/**
- * Create the scaling logic for the specified [machine]
+ * Create the driver logic for the specified [machine].
*/
public fun createLogic(machine: SimMachine, cpus: List<SimProcessingUnit>): Logic
/**
- * The logic of the scaling driver.
+ * The logic of the power driver.
*/
public interface Logic {
/**
- * Compute the power consumption of the processor.
+ * Compute the power consumption of the component.
*
- * @return The power consumption of the processor in W.
+ * @return The power consumption of the component in W.
*/
public fun computePower(): Double
}
diff --git a/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt b/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt
index 0c686aa0..b9cfb06b 100644
--- a/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt
+++ b/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt
@@ -40,6 +40,7 @@ import org.opendc.simulator.compute.util.SimWorkloadLifecycle
import org.opendc.simulator.compute.workload.SimFlopsWorkload
import org.opendc.simulator.compute.workload.SimWorkload
import org.opendc.simulator.core.runBlockingSimulation
+import org.opendc.simulator.power.SimPowerSource
import org.opendc.simulator.resources.SimResourceInterpreter
import org.opendc.simulator.resources.consumer.SimWorkConsumer
@@ -142,16 +143,20 @@ class SimMachineTest {
@Test
fun testPower() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
val machine = SimBareMetalMachine(
- SimResourceInterpreter(coroutineContext, clock),
+ interpreter,
machineModel,
SimplePowerDriver(LinearPowerModel(100.0, 50.0))
)
+ val source = SimPowerSource(interpreter, capacity = 1000.0)
+ source.connect(machine.psu)
try {
coroutineScope {
launch { machine.run(SimFlopsWorkload(2_000, utilization = 1.0)) }
- assertEquals(100.0, machine.powerDraw)
+ assertEquals(100.0, machine.psu.powerDraw)
+ assertEquals(100.0, source.powerDraw)
}
} finally {
machine.close()
diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSource.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSource.kt
index 9f062cc3..2f70e3cc 100644
--- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSource.kt
+++ b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSource.kt
@@ -44,7 +44,11 @@ public class SimResourceSource(
}
override fun onConsume(ctx: SimResourceControllableContext, work: Double, limit: Double, deadline: Long): Long {
- return min(deadline, ctx.clock.millis() + getDuration(work, speed))
+ return if (work.isInfinite()) {
+ Long.MAX_VALUE
+ } else {
+ min(deadline, ctx.clock.millis() + getDuration(work, speed))
+ }
}
override fun onUpdate(ctx: SimResourceControllableContext, work: Double) {