summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--buildSrc/src/main/kotlin/kotlin-library-convention.gradle.kts1
-rw-r--r--odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/signal/Signal.kt90
-rw-r--r--opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt29
3 files changed, 100 insertions, 20 deletions
diff --git a/buildSrc/src/main/kotlin/kotlin-library-convention.gradle.kts b/buildSrc/src/main/kotlin/kotlin-library-convention.gradle.kts
index 8b71ad91..c0c37a09 100644
--- a/buildSrc/src/main/kotlin/kotlin-library-convention.gradle.kts
+++ b/buildSrc/src/main/kotlin/kotlin-library-convention.gradle.kts
@@ -42,6 +42,7 @@ java {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = "1.8"
+ kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}
tasks.test {
diff --git a/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/signal/Signal.kt b/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/signal/Signal.kt
new file mode 100644
index 00000000..da6298a3
--- /dev/null
+++ b/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/signal/Signal.kt
@@ -0,0 +1,90 @@
+/*
+ * MIT License
+ *
+ * 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 com.atlarge.odcsim.signal
+
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.FlowPreview
+import kotlinx.coroutines.InternalCoroutinesApi
+import kotlinx.coroutines.channels.BroadcastChannel
+import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.FlowCollector
+import kotlinx.coroutines.flow.asFlow
+
+/**
+ * A [Flow] that contains a single value that changes over time.
+ *
+ * This class exists to implement the DataFlow/StateFlow functionality that will be implemented in `kotlinx-coroutines`
+ * in the future, but is not available yet.
+ * See: https://github.com/Kotlin/kotlinx.coroutines/pull/1354
+ */
+public interface Signal<T> : Flow<T> {
+ /**
+ * The current value of this signal.
+ *
+ * Setting a value that is [equal][Any.equals] to the previous one does nothing.
+ */
+ public var value: T
+}
+
+/**
+ * Creates a [Signal] with a given initial [value].
+ */
+@Suppress("FunctionName")
+public fun <T> Signal(value: T): Signal<T> = SignalImpl(value)
+
+/**
+ * Internal implementation of the [Signal] interface.
+ */
+private class SignalImpl<T>(initialValue: T) : Signal<T> {
+ /**
+ * The [BroadcastChannel] to back this signal.
+ */
+ @OptIn(ExperimentalCoroutinesApi::class)
+ private val chan = BroadcastChannel<T>(Channel.CONFLATED)
+
+ /**
+ * The internal [Flow] backing this signal.
+ */
+ @OptIn(FlowPreview::class)
+ private val flow = chan.asFlow()
+
+ init {
+ @OptIn(ExperimentalCoroutinesApi::class)
+ chan.offer(initialValue)
+ }
+
+ @OptIn(ExperimentalCoroutinesApi::class)
+ public override var value: T = initialValue
+ set(value) {
+ if (field != value) {
+ chan.offer(value)
+ field = value
+ }
+ }
+
+ @InternalCoroutinesApi
+ override suspend fun collect(collector: FlowCollector<T>) = flow.collect(collector)
+}
diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt
index e9317aff..637432db 100644
--- a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt
+++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt
@@ -25,6 +25,7 @@
package com.atlarge.opendc.compute.metal.driver
import com.atlarge.odcsim.Domain
+import com.atlarge.odcsim.signal.Signal
import com.atlarge.odcsim.simulationContext
import com.atlarge.opendc.compute.core.ProcessingUnit
import com.atlarge.opendc.compute.core.Server
@@ -40,14 +41,9 @@ import com.atlarge.opendc.compute.metal.PowerState
import com.atlarge.opendc.compute.metal.power.ConstantPowerModel
import com.atlarge.opendc.core.power.PowerModel
import kotlinx.coroutines.CancellationException
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
-import kotlinx.coroutines.channels.BroadcastChannel
-import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
-import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.launch
import java.util.UUID
import kotlin.math.ceil
@@ -94,20 +90,13 @@ public class SimpleBareMetalDriver(
private var job: Job? = null
/**
- * The channel containing the load of the server.
+ * The signal containing the load of the server.
*/
- @UseExperimental(ExperimentalCoroutinesApi::class)
- private val loadChannel = BroadcastChannel<Double>(Channel.CONFLATED)
+ private val usageSignal = Signal(0.0)
- @UseExperimental(FlowPreview::class)
- override val usage: Flow<Double> = loadChannel.asFlow()
+ override val usage: Flow<Double> = usageSignal
- override val powerDraw: Flow<Double>
-
- init {
- loadChannel.offer(0.0)
- powerDraw = powerModel(this)
- }
+ override val powerDraw: Flow<Double> = powerModel(this)
override suspend fun init(monitor: ServerMonitor): Node = withContext(domain.coroutineContext) {
this@SimpleBareMetalDriver.monitor = monitor
@@ -205,7 +194,7 @@ public class SimpleBareMetalDriver(
val start = simulationContext.clock.millis()
var duration = max(0, deadline - start)
- var load = 0.0
+ var totalUsage = 0.0
// Determine the duration of the first CPU to finish
for (i in 0 until min(cpus.size, burst.size)) {
@@ -213,14 +202,14 @@ public class SimpleBareMetalDriver(
val usage = min(limit[i], cpu.frequency) * 1_000_000 // Usage from MHz to Hz
val cpuDuration = ceil(burst[i] / usage * 1000).toLong() // Convert from seconds to milliseconds
- load += usage / (cpu.frequency * 1_000_000)
+ totalUsage += usage / (cpu.frequency * 1_000_000)
if (cpuDuration != 0L) { // We only wait for processor cores with a non-zero burst
duration = min(duration, cpuDuration)
}
}
- loadChannel.offer(load)
+ usageSignal.value = totalUsage
try {
delay(duration)
@@ -232,7 +221,7 @@ public class SimpleBareMetalDriver(
// Flush the load if the do not receive a new run call for the same timestamp
flush = domain.launch {
delay(1)
- loadChannel.offer(0.0)
+ usageSignal.value = 0.0
}
flush!!.invokeOnCompletion {
flush = null