-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
js7-base/jvm/src/main/scala/js7/base/catsutils/CpuStarvationCheck.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package js7.base.catsutils | ||
|
||
import cats.effect.metrics.CpuStarvationWarningMetrics | ||
import cats.effect.{IO, IOApp} | ||
import js7.base.catsutils.CpuStarvationCheck.* | ||
import js7.base.log.Logger.syntax.* | ||
import js7.base.log.{LogLevel, Logger} | ||
import js7.base.time.ScalaTime.* | ||
import scala.concurrent.duration.{Duration, FiniteDuration} | ||
|
||
transparent trait CpuStarvationCheck extends IOApp: | ||
|
||
override def runtimeConfig = | ||
super.runtimeConfig.copy( | ||
cpuStarvationCheckInterval = cpuStarvationCheckInterval, | ||
cpuStarvationCheckThreshold = cpuStarvationCheckThreshold / cpuStarvationCheckInterval, | ||
cpuStarvationCheckInitialDelay = cpuStarvationCheckInitialDelay) | ||
|
||
override protected def onCpuStarvationWarn(metrics: CpuStarvationWarningMetrics): IO[Unit] = | ||
// See https://typelevel.org/cats-effect/docs/core/starvation-and-tuning | ||
val d = metrics.starvationInterval * metrics.starvationThreshold | ||
IO(logger.log(cpuStarvationCheckLogLevel, | ||
s"🐌 Responsiveness was slower than ${d.pretty}")) | ||
|
||
|
||
|
||
object CpuStarvationCheck: | ||
/** Use only after Logger.initialize! */ | ||
private lazy val logger = Logger[this.type] | ||
|
||
private val cpuStarvationCheckLogLevel: LogLevel = | ||
sys.props.get("js7.cats.effect.cpuStarvationCheckLogLevel").fold(LogLevel.Info)(LogLevel(_)) | ||
|
||
private val cpuStarvationCheckInitialDelay: Duration = | ||
if cpuStarvationCheckLogLevel == LogLevel.None then | ||
Duration.Inf | ||
else | ||
sys.props.get("js7.cats.effect.cpuStarvationCheckInitialDelay").fold(60.s)(StringAsDuration) | ||
|
||
private val cpuStarvationCheckThreshold: FiniteDuration = | ||
sys.props.get("js7.cats.effect.cpuStarvationCheckThreshold").fold(100.ms)(StringAsDuration) | ||
|
||
private val cpuStarvationCheckInterval: FiniteDuration = | ||
sys.props.get("js7.cats.effect.cpuStarvationCheckInterval").fold(1.s)(StringAsDuration) | ||
.max(cpuStarvationCheckThreshold) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters