summaryrefslogtreecommitdiff
path: root/opendc-stdlib/src/main/kotlin
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-20 00:36:47 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-20 00:36:47 +0200
commita67b87be9e14d6d3c23e1e6aff5051176171e6ef (patch)
treee3c0e780a878d455796598809773309bbb9fec3f /opendc-stdlib/src/main/kotlin
parent62895f71b7a7479652d9b86f7036b6580b40b7c7 (diff)
Improve simulation time management
Diffstat (limited to 'opendc-stdlib/src/main/kotlin')
-rw-r--r--opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt41
1 files changed, 19 insertions, 22 deletions
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<Machine.State>, Process<Machine> {
/**
* 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<Machine.State>, Process<Machine> {
* Run the simulation kernel for this entity.
*/
override suspend fun Context<Machine>.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>("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))
}
}