diff options
Diffstat (limited to 'opendc-simulator/opendc-simulator-resources/src/main')
2 files changed, 27 insertions, 27 deletions
diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/consumer/SimTraceConsumer.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/consumer/SimTraceConsumer.kt index ad6b0108..4c0e075c 100644 --- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/consumer/SimTraceConsumer.kt +++ b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/consumer/SimTraceConsumer.kt @@ -31,12 +31,20 @@ import org.opendc.simulator.resources.SimResourceEvent * consumption for some period of time. */ public class SimTraceConsumer(private val trace: Sequence<Fragment>) : SimResourceConsumer { - private var iterator: Iterator<Fragment>? = null + private var _iterator: Iterator<Fragment>? = null + private var _nextTarget = Long.MIN_VALUE override fun onNext(ctx: SimResourceContext, now: Long, delta: Long): Long { - val iterator = checkNotNull(iterator) + // Check whether the trace fragment was fully consumed, otherwise wait until we have done so + val nextTarget = _nextTarget + if (nextTarget > now) { + return now - nextTarget + } + + val iterator = checkNotNull(_iterator) return if (iterator.hasNext()) { val fragment = iterator.next() + _nextTarget = now + fragment.duration ctx.push(fragment.usage) fragment.duration } else { @@ -48,11 +56,11 @@ public class SimTraceConsumer(private val trace: Sequence<Fragment>) : SimResour override fun onEvent(ctx: SimResourceContext, event: SimResourceEvent) { when (event) { SimResourceEvent.Start -> { - check(iterator == null) { "Consumer already running" } - iterator = trace.iterator() + check(_iterator == null) { "Consumer already running" } + _iterator = trace.iterator() } SimResourceEvent.Exit -> { - iterator = null + _iterator = null } else -> {} } diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt index 9cbf849d..1ac38946 100644 --- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt +++ b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/impl/SimResourceContextImpl.kt @@ -148,6 +148,10 @@ internal class SimResourceContextImpl( } override fun push(rate: Double) { + if (_limit == rate) { + return + } + _speed = min(capacity, rate) _limit = rate } @@ -163,7 +167,7 @@ internal class SimResourceContextImpl( /** * Update the state of the resource context. */ - fun doUpdate(timestamp: Long) { + fun doUpdate(now: Long) { val oldState = _state if (oldState != SimResourceState.Active) { return @@ -171,41 +175,29 @@ internal class SimResourceContextImpl( _updateActive = true - val flag = _flag - val isInterrupted = flag and FLAG_INTERRUPT != 0 - val reachedDeadline = _deadline <= timestamp - val delta = max(0, timestamp - _timestamp) + val reachedDeadline = _deadline <= now + val delta = max(0, now - _timestamp) try { - // Update the resource counters only if there is some progress - if (timestamp > _timestamp) { + if (now > _timestamp) { logic.onUpdate(this, delta, _limit, reachedDeadline) } - // We should only continue processing the next command if: - // 1. The resource consumption was finished. - // 2. The resource consumer should be interrupted (e.g., someone called .interrupt()) - val duration = if (reachedDeadline || isInterrupted) { - consumer.onNext(this, timestamp, delta) - } else { - _deadline - timestamp - } + val duration = consumer.onNext(this, now, delta) // Reset update flags _flag = 0 when (_state) { SimResourceState.Active -> { - val limit = _limit - push(limit) - _duration = duration - - val target = logic.onConsume(this, timestamp, limit, duration) + val target = logic.onConsume(this, now, _limit, duration) + _speed = min(capacity, _limit) + _duration = duration _deadline = target - scheduleUpdate(timestamp, target) + scheduleUpdate(now, target) } SimResourceState.Pending -> if (oldState != SimResourceState.Pending) { @@ -219,7 +211,7 @@ internal class SimResourceContextImpl( } catch (cause: Throwable) { doFail(cause) } finally { - _timestamp = timestamp + _timestamp = now _updateActive = false } } |
