summaryrefslogtreecommitdiff
path: root/simulator/opendc-compute/src/main
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2020-10-03 16:29:55 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2020-10-03 16:29:55 +0200
commitc8567a567348e13c341bf1a1ec64ed34ce25815a (patch)
treee3818756de1c14846b57bef27adc3cef51a72279 /simulator/opendc-compute/src/main
parentac7016c4c5f15bf20b21e7d34e93d8b963aab231 (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/src/main')
-rw-r--r--simulator/opendc-compute/src/main/kotlin/org/opendc/compute/core/execution/ComputeSimExecutionContext.kt36
-rw-r--r--simulator/opendc-compute/src/main/kotlin/org/opendc/compute/metal/driver/SimBareMetalDriver.kt22
-rw-r--r--simulator/opendc-compute/src/main/kotlin/org/opendc/compute/virt/driver/SimVirtDriver.kt182
-rw-r--r--simulator/opendc-compute/src/main/kotlin/org/opendc/compute/virt/driver/SimVirtDriverWorkload.kt38
4 files changed, 274 insertions, 4 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()
+ }
+ }
+}