diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2017-09-18 00:12:24 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2017-09-18 00:12:24 +0200 |
| commit | 62895f71b7a7479652d9b86f7036b6580b40b7c7 (patch) | |
| tree | 780dbb3ea34c957acd92453af6679ea47d5ce82a /opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt | |
| parent | c4816f18fa1ab4528a6966d636c3bfd7eac7b82a (diff) | |
Refactor and split up code base
This change splits up the current code base into three different
module:
- opendc-core - This module defines the API which you can use to write
simulatable entities in a topology.
- opendc-omega - This module is the reference implementation of the API
defined the `opendc-core` module.
- opendc-stdlib - This module provides a standard library of entities
which can be used for datacenter simulation.
Diffstat (limited to 'opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt')
| -rw-r--r-- | opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt | 87 |
1 files changed, 87 insertions, 0 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 new file mode 100644 index 00000000..dba0fe1b --- /dev/null +++ b/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt @@ -0,0 +1,87 @@ +/* + * MIT License + * + * Copyright (c) 2017 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 nl.atlarge.opendc.topology.machine + +import nl.atlarge.opendc.experiment.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 + * work until finished or interrupted. + * + * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) + */ +class Machine : Entity<Machine.State>, Process<Machine> { + /** + * 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) + + /** + * Run the simulation kernel for this entity. + */ + override suspend fun Context<Machine>.run() { + update(state.copy(status = 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 delay = Random().nextInt(1000) + 1 + wait(delay) + + loop@ while (true) { + val msg = receive() + when (msg) { + is Task -> { + task = msg + break@loop + } + else -> println("warning: unhandled message $msg") + } + } + + update(state.copy(status = Status.RUNNING)) + while (tick()) { + task.consume(speed.toLong()) + } + update(state.copy(status = Status.HALT)) + } +} |
