diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-10-03 16:29:55 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-10-03 16:29:55 +0200 |
| commit | c8567a567348e13c341bf1a1ec64ed34ce25815a (patch) | |
| tree | e3818756de1c14846b57bef27adc3cef51a72279 /simulator/opendc-compute | |
| parent | ac7016c4c5f15bf20b21e7d34e93d8b963aab231 (diff) | |
Implement VirtDriver using opendc-simulator-compute module
This change adds an implementation of the VirtDriver interface that uses
the functionality provided by the opendc-simulator-compute module.
Diffstat (limited to 'simulator/opendc-compute')
6 files changed, 423 insertions, 8 deletions
diff --git a/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/core/execution/ComputeSimExecutionContext.kt b/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/core/execution/ComputeSimExecutionContext.kt new file mode 100644 index 00000000..3295a8e8 --- /dev/null +++ b/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/core/execution/ComputeSimExecutionContext.kt @@ -0,0 +1,36 @@ +/* + * 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.core.execution + +import org.opendc.compute.core.Server +import org.opendc.simulator.compute.SimExecutionContext + +/** + * Extended [SimExecutionContext] in which workloads within the OpenDC Compute module run. + */ +public interface ComputeSimExecutionContext : SimExecutionContext { + /** + * The server on which the image runs. + */ + public val server: Server +} diff --git a/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/metal/driver/SimBareMetalDriver.kt b/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/metal/driver/SimBareMetalDriver.kt index e9346a6c..4f440342 100644 --- a/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/metal/driver/SimBareMetalDriver.kt +++ b/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/metal/driver/SimBareMetalDriver.kt @@ -28,6 +28,7 @@ import org.opendc.compute.core.Flavor import org.opendc.compute.core.Server import org.opendc.compute.core.ServerEvent import org.opendc.compute.core.ServerState +import org.opendc.compute.core.execution.ComputeSimExecutionContext import org.opendc.compute.core.execution.ShutdownException import org.opendc.compute.core.image.EmptyImage import org.opendc.compute.core.image.Image @@ -38,8 +39,10 @@ import org.opendc.compute.metal.NodeState import org.opendc.compute.metal.power.ConstantPowerModel import org.opendc.core.power.PowerModel import org.opendc.core.services.ServiceRegistry -import org.opendc.simulator.compute.SimMachine +import org.opendc.simulator.compute.SimBareMetalMachine +import org.opendc.simulator.compute.SimExecutionContext import org.opendc.simulator.compute.SimMachineModel +import org.opendc.simulator.compute.workload.SimWorkload import org.opendc.utils.flow.EventFlow import org.opendc.utils.flow.StateFlow import java.time.Clock @@ -99,9 +102,9 @@ public class SimBareMetalDriver( private val random = Random(uid.leastSignificantBits xor uid.mostSignificantBits) /** - * The [SimMachine] we use to run the workload. + * The [SimBareMetalMachine] we use to run the workload. */ - private val machine = SimMachine(coroutineScope, clock, machine) + private val machine = SimBareMetalMachine(coroutineScope, clock, machine) /** * The [Job] that runs the simulated workload. @@ -136,11 +139,22 @@ public class SimBareMetalDriver( events ) + // Wrap the workload to pass in a ComputeSimExecutionContext + val workload = object : SimWorkload { + override suspend fun run(ctx: SimExecutionContext) { + val wrappedCtx = object : ComputeSimExecutionContext, SimExecutionContext by ctx { + override val server: Server + get() = nodeState.value.server!! + } + (node.image as SimWorkloadImage).workload.run(wrappedCtx) + } + } + job = coroutineScope.launch { delay(1) // TODO Introduce boot time initMachine() try { - machine.run((node.image as SimWorkloadImage).workload) + machine.run(workload) exitMachine(null) } catch (_: CancellationException) { // Ignored diff --git a/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/virt/driver/SimVirtDriver.kt b/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/virt/driver/SimVirtDriver.kt new file mode 100644 index 00000000..758315aa --- /dev/null +++ b/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/virt/driver/SimVirtDriver.kt @@ -0,0 +1,182 @@ +/* + * 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.virt.driver + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.launch +import org.opendc.compute.core.* +import org.opendc.compute.core.execution.ComputeSimExecutionContext +import org.opendc.compute.core.execution.ShutdownException +import org.opendc.compute.core.image.Image +import org.opendc.compute.core.image.SimWorkloadImage +import org.opendc.compute.virt.HypervisorEvent +import org.opendc.core.services.ServiceRegistry +import org.opendc.simulator.compute.SimExecutionContext +import org.opendc.simulator.compute.SimHypervisor +import org.opendc.simulator.compute.SimMachine +import org.opendc.simulator.compute.workload.SimWorkload +import org.opendc.utils.flow.EventFlow +import java.time.Clock +import java.util.* + +/** + * A [VirtDriver] that is simulates virtual machines on a physical machine using [SimHypervisor]. + */ +public class SimVirtDriver( + private val coroutineScope: CoroutineScope, + clock: Clock, + private val ctx: SimExecutionContext +) : VirtDriver { + + /** + * The [EventFlow] to emit the events. + */ + internal val eventFlow = EventFlow<HypervisorEvent>() + + override val events: Flow<HypervisorEvent> = eventFlow + + /** + * Current total memory use of the images on this hypervisor. + */ + private var availableMemory: Long = ctx.machine.memory.map { it.size }.sum() + + /** + * The hypervisor to run multiple workloads. + */ + private val hypervisor = SimHypervisor( + coroutineScope, + clock, + object : SimHypervisor.Listener { + override fun onSliceFinish( + hypervisor: SimHypervisor, + requestedBurst: Long, + grantedBurst: Long, + overcommissionedBurst: Long, + interferedBurst: Long, + cpuUsage: Double, + cpuDemand: Double + ) { + eventFlow.emit( + HypervisorEvent.SliceFinished( + this@SimVirtDriver, + requestedBurst, + grantedBurst, + overcommissionedBurst, + interferedBurst, + cpuUsage, + cpuDemand, + vms.size, + (ctx as ComputeSimExecutionContext).server + ) + ) + } + } + ) + + /** + * The virtual machines running on the hypervisor. + */ + private val vms = HashSet<VirtualMachine>() + + override suspend fun spawn(name: String, image: Image, flavor: Flavor): Server { + val requiredMemory = flavor.memorySize + if (availableMemory - requiredMemory < 0) { + throw InsufficientMemoryOnServerException() + } + require(flavor.cpuCount <= ctx.machine.cpus.size) { "Machine does not fit" } + + val events = EventFlow<ServerEvent>() + val server = Server( + UUID.randomUUID(), + name, + emptyMap(), + flavor, + image, + ServerState.BUILD, + ServiceRegistry(), + events + ) + availableMemory -= requiredMemory + vms.add(VirtualMachine(server, events, hypervisor.createMachine(ctx.machine))) + eventFlow.emit(HypervisorEvent.VmsUpdated(this, vms.size, availableMemory)) + return server + } + + /** + * A virtual machine instance that the driver manages. + */ + private inner class VirtualMachine(server: Server, val events: EventFlow<ServerEvent>, machine: SimMachine) { + val job = coroutineScope.launch { + val workload = object : SimWorkload { + override suspend fun run(ctx: SimExecutionContext) { + val wrappedCtx = object : ComputeSimExecutionContext, SimExecutionContext by ctx { + override val server: Server + get() = this@VirtualMachine.server + } + (server.image as SimWorkloadImage).workload.run(wrappedCtx) + } + } + + delay(1) // TODO Introduce boot time + init() + try { + machine.run(workload) + exit(null) + } catch (cause: Throwable) { + exit(cause) + } + } + + var server: Server = server + set(value) { + if (field.state != value.state) { + events.emit(ServerEvent.StateChanged(value, field.state)) + } + + field = value + } + + private fun init() { + server = server.copy(state = ServerState.ACTIVE) + } + + private fun exit(cause: Throwable?) { + val serverState = + if (cause == null || (cause is ShutdownException && cause.cause == null)) + ServerState.SHUTOFF + else + ServerState.ERROR + server = server.copy(state = serverState) + availableMemory += server.flavor.memorySize + vms.remove(this) + eventFlow.emit(HypervisorEvent.VmsUpdated(this@SimVirtDriver, vms.size, availableMemory)) + events.close() + } + } + + public suspend fun run() { + hypervisor.run(ctx) + } +} diff --git a/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/virt/driver/SimVirtDriverWorkload.kt b/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/virt/driver/SimVirtDriverWorkload.kt new file mode 100644 index 00000000..91d400a4 --- /dev/null +++ b/simulator/opendc-compute/src/main/kotlin/org/opendc/compute/virt/driver/SimVirtDriverWorkload.kt @@ -0,0 +1,38 @@ +/* + * 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.virt.driver + +import kotlinx.coroutines.coroutineScope +import org.opendc.simulator.compute.SimExecutionContext +import org.opendc.simulator.compute.workload.SimWorkload + +public class SimVirtDriverWorkload : SimWorkload { + public lateinit var driver: SimVirtDriver + + override suspend fun run(ctx: SimExecutionContext) { + coroutineScope { + driver = SimVirtDriver(this, ctx.clock, ctx) + driver.run() + } + } +} diff --git a/simulator/opendc-compute/src/test/kotlin/org/opendc/compute/virt/HypervisorTest.kt b/simulator/opendc-compute/src/test/kotlin/org/opendc/compute/virt/HypervisorTest.kt index 369b9538..82a82044 100644 --- a/simulator/opendc-compute/src/test/kotlin/org/opendc/compute/virt/HypervisorTest.kt +++ b/simulator/opendc-compute/src/test/kotlin/org/opendc/compute/virt/HypervisorTest.kt @@ -29,7 +29,6 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.test.TestCoroutineScope import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertAll import org.opendc.compute.core.Flavor @@ -53,7 +52,6 @@ internal class HypervisorTest { */ @OptIn(ExperimentalCoroutinesApi::class) @Test - @Disabled fun smoke() { val testScope = TestCoroutineScope() val clock = DelayControllerClockAdapter(testScope) @@ -75,7 +73,7 @@ internal class HypervisorTest { delay(5) - val flavor = org.opendc.compute.core.Flavor(1, 0) + val flavor = Flavor(1, 0) val vmDriver = metalDriver.refresh().server!!.services[VirtDriver] vmDriver.events.onEach { println(it) }.launchIn(this) val vmA = vmDriver.spawn("a", workloadA, flavor) @@ -140,7 +138,7 @@ internal class HypervisorTest { delay(5) - val flavor = org.opendc.compute.core.Flavor(2, 0) + val flavor = Flavor(2, 0) val vmDriver = metalDriver.refresh().server!!.services[VirtDriver] vmDriver.events .onEach { event -> diff --git a/simulator/opendc-compute/src/test/kotlin/org/opendc/compute/virt/SimHypervisorTest.kt b/simulator/opendc-compute/src/test/kotlin/org/opendc/compute/virt/SimHypervisorTest.kt new file mode 100644 index 00000000..9d33d395 --- /dev/null +++ b/simulator/opendc-compute/src/test/kotlin/org/opendc/compute/virt/SimHypervisorTest.kt @@ -0,0 +1,147 @@ +/* + * 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.virt + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.TestCoroutineScope +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll +import org.opendc.compute.core.Flavor +import org.opendc.compute.core.image.SimWorkloadImage +import org.opendc.compute.metal.driver.SimBareMetalDriver +import org.opendc.compute.virt.driver.SimVirtDriverWorkload +import org.opendc.simulator.compute.SimMachineModel +import org.opendc.simulator.compute.model.MemoryUnit +import org.opendc.simulator.compute.model.ProcessingNode +import org.opendc.simulator.compute.model.ProcessingUnit +import org.opendc.simulator.compute.workload.SimTraceWorkload +import org.opendc.simulator.utils.DelayControllerClockAdapter +import java.time.Clock +import java.util.UUID + +/** + * Basic test-suite for the hypervisor. + */ +@OptIn(ExperimentalCoroutinesApi::class) +internal class SimHypervisorTest { + private lateinit var scope: TestCoroutineScope + private lateinit var clock: Clock + private lateinit var machineModel: SimMachineModel + + @BeforeEach + fun setUp() { + scope = TestCoroutineScope() + clock = DelayControllerClockAdapter(scope) + + val cpuNode = ProcessingNode("Intel", "Xeon", "amd64", 2) + + machineModel = SimMachineModel( + cpus = List(cpuNode.coreCount) { ProcessingUnit(cpuNode, it, 3200.0) }, + memory = List(4) { MemoryUnit("Crucial", "MTA18ASF4G72AZ-3G2B1", 3200.0, 32_000) } + ) + } + + /** + * Test overcommissioning of a hypervisor. + */ + @Test + fun overcommission() { + var requestedBurst = 0L + var grantedBurst = 0L + var overcommissionedBurst = 0L + + scope.launch { + val virtDriverWorkload = SimVirtDriverWorkload() + val vmm = SimWorkloadImage(UUID.randomUUID(), "vmm", emptyMap(), virtDriverWorkload) + val duration = 5 * 60L + val vmImageA = SimWorkloadImage( + UUID.randomUUID(), + "<unnamed>", + emptyMap(), + SimTraceWorkload( + sequenceOf( + SimTraceWorkload.Fragment(0, 28L * duration, duration * 1000, 28.0, 2), + SimTraceWorkload.Fragment(0, 3500L * duration, duration * 1000, 3500.0, 2), + SimTraceWorkload.Fragment(0, 0, duration * 1000, 0.0, 2), + SimTraceWorkload.Fragment(0, 183L * duration, duration * 1000, 183.0, 2) + ), + ) + ) + val vmImageB = SimWorkloadImage( + UUID.randomUUID(), + "<unnamed>", + emptyMap(), + SimTraceWorkload( + sequenceOf( + SimTraceWorkload.Fragment(0, 28L * duration, duration * 1000, 28.0, 2), + SimTraceWorkload.Fragment(0, 3100L * duration, duration * 1000, 3100.0, 2), + SimTraceWorkload.Fragment(0, 0, duration * 1000, 0.0, 2), + SimTraceWorkload.Fragment(0, 73L * duration, duration * 1000, 73.0, 2) + ) + ), + ) + + val metalDriver = + SimBareMetalDriver(this, clock, UUID.randomUUID(), "test", emptyMap(), machineModel) + + metalDriver.init() + metalDriver.setImage(vmm) + metalDriver.start() + + delay(5) + + val flavor = Flavor(2, 0) + val vmDriver = virtDriverWorkload.driver + vmDriver.events + .onEach { event -> + when (event) { + is HypervisorEvent.SliceFinished -> { + requestedBurst += event.requestedBurst + grantedBurst += event.grantedBurst + overcommissionedBurst += event.overcommissionedBurst + } + } + } + .launchIn(this) + + vmDriver.spawn("a", vmImageA, flavor) + vmDriver.spawn("b", vmImageB, flavor) + } + + scope.advanceUntilIdle() + + assertAll( + { assertEquals(emptyList<Throwable>(), scope.uncaughtExceptions, "No errors") }, + { assertEquals(2073600, requestedBurst, "Requested Burst does not match") }, + { assertEquals(2013600, grantedBurst, "Granted Burst does not match") }, + { assertEquals(60000, overcommissionedBurst, "Overcommissioned Burst does not match") }, + { assertEquals(1200007, scope.currentTime) } + ) + } +} |
