From 788c007599ac61a41460589f65454aac1857eb81 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 28 Oct 2022 11:55:41 +0200 Subject: refactor(sim/compute): Provide workload constructors in SimWorkloads This change introduces a new class SimWorkloads which provides construction methods for the standard workloads available in OpenDC. --- .../compute/workload/SimFlopsWorkload.java | 4 +- .../compute/workload/SimRuntimeWorkload.java | 4 +- .../compute/workload/SimWorkloadLifecycle.java | 60 --------------------- .../simulator/compute/workload/SimWorkloads.java | 63 ++++++++++++++++++++++ 4 files changed, 67 insertions(+), 64 deletions(-) delete mode 100644 opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.java create mode 100644 opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimWorkloads.java (limited to 'opendc-simulator/opendc-simulator-compute/src/main/java') diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimFlopsWorkload.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimFlopsWorkload.java index 255fd1b2..f3efbebb 100644 --- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimFlopsWorkload.java +++ b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimFlopsWorkload.java @@ -49,9 +49,9 @@ public class SimFlopsWorkload implements SimWorkload, FlowStageLogic { * Construct a new {@link SimFlopsWorkload}. * * @param flops The number of floating point operations to perform for this task in MFLOPs. - * @param utilization A model of the CPU utilization of the application. + * @param utilization The CPU utilization of the workload. */ - public SimFlopsWorkload(long flops, double utilization) { + SimFlopsWorkload(long flops, double utilization) { if (flops < 0) { throw new IllegalArgumentException("Number of FLOPs must be positive"); } else if (utilization <= 0.0 || utilization > 1.0) { diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimRuntimeWorkload.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimRuntimeWorkload.java index c3380b31..194efafd 100644 --- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimRuntimeWorkload.java +++ b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimRuntimeWorkload.java @@ -48,9 +48,9 @@ public class SimRuntimeWorkload implements SimWorkload, FlowStageLogic { * Construct a new {@link SimRuntimeWorkload}. * * @param duration The duration of the workload in milliseconds. - * @param utilization A model of the CPU utilization of the application. + * @param utilization The CPU utilization of the workload. */ - public SimRuntimeWorkload(long duration, double utilization) { + SimRuntimeWorkload(long duration, double utilization) { if (duration < 0) { throw new IllegalArgumentException("Duration must be positive"); } else if (utilization <= 0.0 || utilization > 1.0) { diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.java deleted file mode 100644 index f0e2561f..00000000 --- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2022 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.simulator.compute.workload; - -import java.util.HashSet; -import org.opendc.simulator.compute.SimMachineContext; - -/** - * A helper class to manage the lifecycle of a {@link SimWorkload}. - */ -public final class SimWorkloadLifecycle { - private final SimMachineContext ctx; - private final HashSet waiting = new HashSet<>(); - - /** - * Construct a {@link SimWorkloadLifecycle} instance. - * - * @param ctx The {@link SimMachineContext} of the workload. - */ - public SimWorkloadLifecycle(SimMachineContext ctx) { - this.ctx = ctx; - } - - /** - * Register a "completer" callback that must be invoked before ending the lifecycle of the workload. - */ - public Runnable newCompleter() { - Runnable completer = new Runnable() { - @Override - public void run() { - final HashSet waiting = SimWorkloadLifecycle.this.waiting; - if (waiting.remove(this) && waiting.isEmpty()) { - ctx.shutdown(); - } - } - }; - waiting.add(completer); - return completer; - } -} diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimWorkloads.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimWorkloads.java new file mode 100644 index 00000000..8655fd0a --- /dev/null +++ b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/workload/SimWorkloads.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 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.simulator.compute.workload; + +import java.time.Duration; + +/** + * Helper methods for constructing {@link SimWorkload}s. + */ +public class SimWorkloads { + private SimWorkloads() {} + + /** + * Create a {@link SimWorkload} that executes a specified number of floating point operations (FLOPs) at the given + * utilization. + * + * @param flops The number of floating point operations to perform for this task in MFLOPs. + * @param utilization The CPU utilization of the workload. + */ + public static SimWorkload flops(long flops, double utilization) { + return new SimFlopsWorkload(flops, utilization); + } + + /** + * Create a {@link SimWorkload} that consumes the CPU resources for a specified duration at the given utilization. + * + * @param duration The duration of the workload in milliseconds. + * @param utilization The CPU utilization of the workload. + */ + public static SimWorkload runtime(long duration, double utilization) { + return new SimRuntimeWorkload(duration, utilization); + } + + /** + * Create a {@link SimWorkload} that consumes the CPU resources for a specified duration at the given utilization. + * + * @param duration The duration of the workload. + * @param utilization The CPU utilization of the workload. + */ + public static SimWorkload runtime(Duration duration, double utilization) { + return runtime(duration.toMillis(), utilization); + } +} -- cgit v1.2.3