diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-03-17 15:52:10 +0100 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-03-25 10:48:58 +0100 |
| commit | b3e8e3d196de8b8c1bb904bfb3c6641415cf72bb (patch) | |
| tree | 4637fc7a7d95dfd239854457a88384c8d79341ba /opendc/opendc-core | |
| parent | 43f1376a00342338f4d0affde5e1f2f540ab7e32 (diff) | |
feat: Use Weilbull distribution for failures
Diffstat (limited to 'opendc/opendc-core')
| -rw-r--r-- | opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/failure/UncorrelatedFaultInjector.kt | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/failure/UncorrelatedFaultInjector.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/failure/UncorrelatedFaultInjector.kt index 5155a25a..56706824 100644 --- a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/failure/UncorrelatedFaultInjector.kt +++ b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/failure/UncorrelatedFaultInjector.kt @@ -24,26 +24,35 @@ package com.atlarge.opendc.core.failure +import com.atlarge.odcsim.simulationContext import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import kotlin.math.ln +import kotlin.math.ln1p +import kotlin.math.pow import kotlin.random.Random /** * A [FaultInjector] that injects uncorrelated faults into the system, meaning that failures of the subsystems are * independent. */ -public class UncorrelatedFaultInjector(private val mu: Double = 1024.0, private val random: Random = Random.Default) : FaultInjector { +public class UncorrelatedFaultInjector(private val alpha: Double, private val beta: Double, private val random: Random = Random) : FaultInjector { /** * Enqueue the specified [FailureDomain] to fail some time in the future. */ override fun enqueue(domain: FailureDomain) { domain.scope.launch { - val d = random.expovariate(mu) - delay(d) + val d = random.weibull(alpha, beta) * 1e3 // Make sure to convert delay to milliseconds + + // Handle long overflow + if (simulationContext.clock.millis() + d <= 0) { + return@launch + } + + delay(d.toLong()) domain.fail() } } - private fun Random.expovariate(mu: Double) = (-mu * ln(1 - nextDouble())).toLong() + // XXX We should extract this in some common package later on. + private fun Random.weibull(alpha: Double, beta: Double) = (beta * (-ln1p(-nextDouble())).pow(1.0 / alpha)) } |
