summaryrefslogtreecommitdiff
path: root/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler')
-rw-r--r--opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ExperimentScheduler.kt52
-rw-r--r--opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ExperimentSchedulerProvider.kt57
-rw-r--r--opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ThreadPoolExperimentScheduler.kt58
-rw-r--r--opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ThreadPoolExperimentSchedulerProvider.kt33
4 files changed, 200 insertions, 0 deletions
diff --git a/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ExperimentScheduler.kt b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ExperimentScheduler.kt
new file mode 100644
index 00000000..0265554a
--- /dev/null
+++ b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ExperimentScheduler.kt
@@ -0,0 +1,52 @@
+/*
+ * 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.harness.engine.scheduler
+
+import org.opendc.harness.api.Trial
+
+/**
+ * The [ExperimentScheduler] is responsible for scheduling the execution of experiment runs over some set of compute
+ * resources (e.g., threads or even multiple machines).
+ */
+public interface ExperimentScheduler : AutoCloseable {
+ /**
+ * Allocate a [Worker] for executing an experiment trial. This method may suspend in case no resources are directly
+ * available at the moment.
+ *
+ * @return The available worker.
+ */
+ public suspend fun allocate(): Worker
+
+ /**
+ * An isolated worker of an [ExperimentScheduler] that is responsible for conducting a single experiment trial.
+ */
+ public interface Worker {
+ /**
+ * Dispatch an experiment trial immediately to one of the available compute resources and block execution until
+ * the trial has finished.
+ *
+ * @param trial The trial to dispatch.
+ */
+ public suspend fun dispatch(trial: Trial)
+ }
+}
diff --git a/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ExperimentSchedulerProvider.kt b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ExperimentSchedulerProvider.kt
new file mode 100644
index 00000000..a93d4bf6
--- /dev/null
+++ b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ExperimentSchedulerProvider.kt
@@ -0,0 +1,57 @@
+/*
+ * 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.harness.engine.scheduler
+
+import java.util.*
+
+/**
+ * A factory for constructing an [ExperimentScheduler].
+ */
+public interface ExperimentSchedulerProvider {
+ /**
+ * A unique identifier for this scheduler implementation.
+ *
+ * Each experiment scheduler must provide a unique ID, so that they can be selected by the user.
+ * When in doubt, you may use the fully qualified name of your custom [ExperimentScheduler] implementation class.
+ */
+ public val id: String
+
+ /**
+ * Factory method for creating a new [ExperimentScheduler] instance.
+ */
+ public fun create(): ExperimentScheduler
+
+ public companion object {
+ /**
+ * The available [ExperimentSchedulerProvider]s.
+ */
+ private val providers by lazy { ServiceLoader.load(ExperimentSchedulerProvider::class.java) }
+
+ /**
+ * Obtain the [ExperimentScheduler] with the specified [id] or return `null`.
+ */
+ public fun findById(id: String): ExperimentSchedulerProvider? {
+ return providers.find { it.id == id }
+ }
+ }
+}
diff --git a/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ThreadPoolExperimentScheduler.kt b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ThreadPoolExperimentScheduler.kt
new file mode 100644
index 00000000..1ae533cf
--- /dev/null
+++ b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ThreadPoolExperimentScheduler.kt
@@ -0,0 +1,58 @@
+/*
+ * 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.harness.engine.scheduler
+
+import kotlinx.coroutines.asCoroutineDispatcher
+import kotlinx.coroutines.sync.Semaphore
+import kotlinx.coroutines.withContext
+import org.opendc.harness.api.Trial
+import java.util.concurrent.Executors
+
+/**
+ * An [ExperimentScheduler] that runs experiment trials using a local thread pool.
+ *
+ * @param parallelism The maximum amount of concurrent workers.
+ */
+public class ThreadPoolExperimentScheduler(parallelism: Int) : ExperimentScheduler {
+ private val dispatcher = Executors.newCachedThreadPool().asCoroutineDispatcher()
+ private val tickets = Semaphore(parallelism)
+
+ override suspend fun allocate(): ExperimentScheduler.Worker {
+ tickets.acquire()
+ return object : ExperimentScheduler.Worker {
+ override suspend fun dispatch(trial: Trial) {
+ try {
+ withContext(dispatcher) {
+ trial.scenario.experiment.evaluator(trial)
+ }
+ } finally {
+ tickets.release()
+ }
+ }
+ }
+ }
+
+ override fun close(): Unit = dispatcher.close()
+
+ override fun toString(): String = "ThreadPoolScheduler"
+}
diff --git a/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ThreadPoolExperimentSchedulerProvider.kt b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ThreadPoolExperimentSchedulerProvider.kt
new file mode 100644
index 00000000..cf9a132f
--- /dev/null
+++ b/opendc-harness/src/main/kotlin/org/opendc/harness/engine/scheduler/ThreadPoolExperimentSchedulerProvider.kt
@@ -0,0 +1,33 @@
+/*
+ * 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.harness.engine.scheduler
+
+/**
+ * An [ExperimentSchedulerProvider] for constructing a [ThreadPoolExperimentScheduler].
+ */
+public class ThreadPoolExperimentSchedulerProvider : ExperimentSchedulerProvider {
+ override val id: String = "thread-pool"
+
+ override fun create(): ExperimentScheduler =
+ ThreadPoolExperimentScheduler(Runtime.getRuntime().availableProcessors())
+}