From a67b87be9e14d6d3c23e1e6aff5051176171e6ef Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 20 Sep 2017 00:36:47 +0200 Subject: Improve simulation time management --- .../nl/atlarge/opendc/topology/machine/Machine.kt | 41 ++++++++++------------ 1 file changed, 19 insertions(+), 22 deletions(-) (limited to 'opendc-stdlib') diff --git a/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt b/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt index dba0fe1b..0884a725 100644 --- a/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt +++ b/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt @@ -24,11 +24,11 @@ package nl.atlarge.opendc.topology.machine -import nl.atlarge.opendc.experiment.Task +import nl.atlarge.opendc.extension.destinations +import nl.atlarge.opendc.workload.Task import nl.atlarge.opendc.kernel.Context import nl.atlarge.opendc.kernel.Process import nl.atlarge.opendc.topology.Entity -import java.util.* /** * A Physical Machine (PM) inside a rack of a datacenter. It has a speed, and can be given a workload on which it will @@ -47,7 +47,7 @@ class Machine : Entity, Process { /** * The shape of the state of a [Machine] entity. */ - data class State(val status: Status) + data class State(val status: Status, val task: Task? = null) /** * The initial state of a [Machine] entity. @@ -58,30 +58,27 @@ class Machine : Entity, Process { * Run the simulation kernel for this entity. */ override suspend fun Context.run() { - update(state.copy(status = Status.IDLE)) + update(State(Status.IDLE)) - val cpus = outgoingEdges.filter { it.tag == "cpu" }.map { it.to as Cpu } - val speed = cpus.fold(0, { acc, (speed, cores) -> acc + speed * cores }) - val task: Task + val cpus = outgoingEdges.destinations("cpu") + val speed = cpus.fold(0, { acc, (speed, cores) -> acc + speed * cores }).toLong() + var task: Task? = null - val delay = Random().nextInt(1000) + 1 - wait(delay) - - loop@ while (true) { - val msg = receive() - when (msg) { - is Task -> { - task = msg - break@loop + while (true) { + if (task != null) { + if (task.finished) { + task = null + update(State(Status.IDLE)) + } else { + task.consume(speed * delta) } - else -> println("warning: unhandled message $msg") } - } - update(state.copy(status = Status.RUNNING)) - while (tick()) { - task.consume(speed.toLong()) + val msg = receive() + if (msg is Task) { + task = msg + update(State(Status.RUNNING, task)) + } } - update(state.copy(status = Status.HALT)) } } -- cgit v1.2.3