From 07930570dc66faa424f0cd0d00de1ec905578de2 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 1 Sep 2017 11:23:26 +0200 Subject: Add notion of initial states of entities This change adds a notion of entities having an initial state specified in the topology. --- .../main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt | 9 ++++----- .../nl/atlarge/opendc/kernel/impl/MachineKernel.kt | 3 +++ .../main/kotlin/nl/atlarge/opendc/topology/Entity.kt | 7 ++++++- .../atlarge/opendc/topology/container/Datacenter.kt | 4 +++- .../nl/atlarge/opendc/topology/container/Rack.kt | 4 +++- .../kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt | 4 +++- .../kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt | 4 +++- .../nl/atlarge/opendc/topology/machine/Machine.kt | 19 ++++++++++++++++++- .../nl/atlarge/opendc/topology/power/PowerUnit.kt | 4 +++- 9 files changed, 46 insertions(+), 12 deletions(-) (limited to 'opendc-core') diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt index ee8d5072..3ad16da5 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt @@ -148,13 +148,12 @@ class Simulator(val topology: Topology, private val mapping: Map, C */ suspend override fun receive(block: Envelope<*>.(Any?) -> T): T = suspendCoroutine {} - /** - * The observable state of an [Entity] within the simulation is provided by context. - */ + * The observable state of an [Entity] within the simulation is provided by context. + */ @Suppress("UNCHECKED_CAST") override val Entity.state: S - get() = states[this] as S + get() = states.computeIfAbsent(this, { initialState }) as S /** * Update the state of the entity being simulated. @@ -201,7 +200,7 @@ class Simulator(val topology: Topology, private val mapping: Map, C */ @Suppress("UNCHECKED_CAST") override val Entity.state: S - get() = states[this] as S + get() = states.computeIfAbsent(this, { initialState }) as S /** * Suspend the simulation kernel for n ticks before resuming the execution. diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/impl/MachineKernel.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/impl/MachineKernel.kt index e34c7060..f81014e0 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/impl/MachineKernel.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/impl/MachineKernel.kt @@ -34,6 +34,9 @@ class MachineKernel(ctx: EntityContext): AbstractEntityKernel( suspend override fun EntityContext.run() { println("${this}: Initialising!") + println("${this}: ${component.entity.state}") + update(Machine.State(Machine.Status.IDLE)) + println("${this}: ${component.entity.state}") val cpus = component.outgoingEdges().filter { it.tag == "cpu" }.map { it.to.entity as Cpu } val speed = cpus.fold(0, { acc, (speed, cores) -> acc + speed * cores }) diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt index 44264a35..e6f37cf6 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt @@ -36,4 +36,9 @@ package nl.atlarge.opendc.topology * a simulation. * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -interface Entity +interface Entity { + /** + * The initial state of the entity. + */ + val initialState: S +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt index 2b464f15..b484f70e 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt @@ -31,4 +31,6 @@ import nl.atlarge.opendc.topology.Entity * * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -class Datacenter: Entity +class Datacenter: Entity { + override val initialState = Unit +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt index 043ad31a..f9595adc 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt @@ -32,4 +32,6 @@ import nl.atlarge.opendc.topology.Entity * * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -class Rack: Entity +class Rack: Entity { + override val initialState = Unit +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt index e06ad00c..1e2135e8 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt @@ -33,4 +33,6 @@ data class Cpu( override val speed: Int, override val cores: Int, override val energyConsumption: Int -): ProcessingUnit +): ProcessingUnit { + override val initialState = Unit +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt index f15847e4..a294db38 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt @@ -33,5 +33,7 @@ class Gpu( override val speed: Int, override val cores: Int, override val energyConsumption: Int -): ProcessingUnit +): ProcessingUnit { + override val initialState = Unit +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt index 396339a2..138abee7 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt @@ -32,4 +32,21 @@ import nl.atlarge.opendc.topology.Entity * * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -class Machine: Entity +class Machine: Entity { + /** + * The status of a machine. + */ + enum class Status { + HALT, IDLE, RUNNING + } + + /** + * The shape of the state of a [Machine] entity. + */ + data class State(val status: Status) + + /** + * The initial state of a [Machine] entity. + */ + override val initialState = State(Status.HALT) +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt index e016a12b..2938e530 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt @@ -32,4 +32,6 @@ import nl.atlarge.opendc.topology.Entity * @param output The power output of the power unit in Watt. * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -class PowerUnit(val output: Double): Entity +class PowerUnit(val output: Double): Entity { + override val initialState = Unit +} -- cgit v1.2.3