From 1c0568c31d60d4e690b4b9aec2e14f660b72a5c8 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 21 Apr 2021 14:30:55 +0200 Subject: simulator: Introduce SimulationCoroutineDispatcher This change introduces the SimulationCoroutineDispatcher implementation which replaces the TestCoroutineDispatcher for running single-threaded simulations. Previously, we used the TestCoroutineDispatcher from the kotlinx-coroutines-test modules for running simulations. However, this module is aimed at coroutine tests and not at simulations. In particular, having to construct a Clock object each time for the TestCoroutineDispatcher caused a lot of unnecessary lines. With the new approach, the SimulationCoroutineDispatcher automatically exposes a usable Clock object. In addition to ergonomic benefits, the SimulationCoroutineDispatcher is much faster than the TestCoroutineDispatcher due to the assumption that simulations run in only a single thread. As a result, the dispatcher does not need to perform synchronization and can use the fast PriorityQueue implementation. --- .../src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'simulator/opendc-simulator/opendc-simulator-compute/src/jmh') diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt index c2a29f5b..99a5fa02 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt @@ -34,7 +34,7 @@ import org.opendc.simulator.compute.model.ProcessingNode import org.opendc.simulator.compute.model.ProcessingUnit import org.opendc.simulator.compute.power.ConstantPowerModel import org.opendc.simulator.compute.workload.SimWorkload -import org.opendc.simulator.utils.DelayControllerClockAdapter +import org.opendc.simulator.core.DelayControllerClockAdapter import org.opendc.utils.TimerScheduler import org.openjdk.jmh.annotations.* import java.time.Clock -- cgit v1.2.3 From 62678b2890a7f3640836b99ca2fec9efd7485929 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 21 Apr 2021 16:35:52 +0200 Subject: simulator: Migrate to SimulationCoroutineDispatcher This change migrates the remainder of the codebase to the SimulationCoroutineDispatcher implementation. --- .../simulator/compute/SimMachineBenchmarks.kt | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'simulator/opendc-simulator/opendc-simulator-compute/src/jmh') diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt index 99a5fa02..7b97a665 100644 --- a/simulator/opendc-simulator/opendc-simulator-compute/src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt +++ b/simulator/opendc-simulator/opendc-simulator-compute/src/jmh/kotlin/org/opendc/simulator/compute/SimMachineBenchmarks.kt @@ -25,8 +25,6 @@ package org.opendc.simulator.compute import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch -import kotlinx.coroutines.test.TestCoroutineScope -import kotlinx.coroutines.test.runBlockingTest import org.opendc.simulator.compute.cpufreq.PerformanceScalingGovernor import org.opendc.simulator.compute.cpufreq.SimpleScalingDriver import org.opendc.simulator.compute.model.MemoryUnit @@ -34,7 +32,8 @@ import org.opendc.simulator.compute.model.ProcessingNode import org.opendc.simulator.compute.model.ProcessingUnit import org.opendc.simulator.compute.power.ConstantPowerModel import org.opendc.simulator.compute.workload.SimWorkload -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.SimulationCoroutineScope +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.utils.TimerScheduler import org.openjdk.jmh.annotations.* import java.time.Clock @@ -46,15 +45,14 @@ import java.util.concurrent.TimeUnit @Measurement(iterations = 5, time = 3, timeUnit = TimeUnit.SECONDS) @OptIn(ExperimentalCoroutinesApi::class) class SimMachineBenchmarks { - private lateinit var scope: TestCoroutineScope + private lateinit var scope: SimulationCoroutineScope private lateinit var clock: Clock private lateinit var scheduler: TimerScheduler private lateinit var machineModel: SimMachineModel @Setup fun setUp() { - scope = TestCoroutineScope() - clock = DelayControllerClockAdapter(scope) + scope = SimulationCoroutineScope() scheduler = TimerScheduler(scope.coroutineContext, clock) val cpuNode = ProcessingNode("Intel", "Xeon", "amd64", 2) @@ -77,18 +75,18 @@ class SimMachineBenchmarks { @Benchmark fun benchmarkBareMetal(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val machine = SimBareMetalMachine( coroutineContext, clock, machineModel, PerformanceScalingGovernor(), SimpleScalingDriver(ConstantPowerModel(0.0)) ) - return@runBlockingTest machine.run(state.workloads[0]) + return@runBlockingSimulation machine.run(state.workloads[0]) } } @Benchmark fun benchmarkSpaceSharedHypervisor(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val machine = SimBareMetalMachine( coroutineContext, clock, machineModel, PerformanceScalingGovernor(), SimpleScalingDriver(ConstantPowerModel(0.0)) @@ -100,7 +98,7 @@ class SimMachineBenchmarks { val vm = hypervisor.createMachine(machineModel) try { - return@runBlockingTest vm.run(state.workloads[0]) + return@runBlockingSimulation vm.run(state.workloads[0]) } finally { vm.close() machine.close() @@ -110,7 +108,7 @@ class SimMachineBenchmarks { @Benchmark fun benchmarkFairShareHypervisorSingle(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val machine = SimBareMetalMachine( coroutineContext, clock, machineModel, PerformanceScalingGovernor(), SimpleScalingDriver(ConstantPowerModel(0.0)) @@ -122,7 +120,7 @@ class SimMachineBenchmarks { val vm = hypervisor.createMachine(machineModel) try { - return@runBlockingTest vm.run(state.workloads[0]) + return@runBlockingSimulation vm.run(state.workloads[0]) } finally { vm.close() machine.close() @@ -132,7 +130,7 @@ class SimMachineBenchmarks { @Benchmark fun benchmarkFairShareHypervisorDouble(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val machine = SimBareMetalMachine( coroutineContext, clock, machineModel, PerformanceScalingGovernor(), SimpleScalingDriver(ConstantPowerModel(0.0)) -- cgit v1.2.3