forked from typelevel/cats-effect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
IORuntimeMetrics
and CpuStarvationMetrics
- Loading branch information
Showing
17 changed files
with
212 additions
and
178 deletions.
There are no files selected for viewing
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
14 changes: 14 additions & 0 deletions
14
core/js-native/src/main/scala/cats/effect/metrics/IORuntimeMetricsCompanionPlatform.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,14 @@ | ||
package cats.effect.metrics | ||
|
||
private[metrics] abstract class IORuntimeMetricsCompanionPlatform { | ||
this: IORuntimeMetrics.type => | ||
|
||
private[effect] def apply(): IORuntimeMetrics = | ||
new IORuntimeMetrics { | ||
private[effect] val cpuStarvationSampler: CpuStarvationSampler = | ||
CpuStarvationSampler() | ||
|
||
val cpuStarvation: CpuStarvationMetrics = | ||
CpuStarvationMetrics(cpuStarvationSampler) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
core/js-native/src/main/scala/cats/effect/metrics/IORuntimeMetricsPlatform.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,3 @@ | ||
package cats.effect.metrics | ||
|
||
private[metrics] trait IORuntimeMetricsPlatform { this: IORuntimeMetrics => } |
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
31 changes: 0 additions & 31 deletions
31
core/js/src/main/scala/cats/effect/metrics/JsCpuStarvationMetrics.scala
This file was deleted.
Oops, something went wrong.
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
59 changes: 8 additions & 51 deletions
59
core/jvm/src/main/scala/cats/effect/metrics/CpuStarvation.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 |
---|---|---|
@@ -1,57 +1,14 @@ | ||
/* | ||
* Copyright 2020-2024 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect.metrics | ||
|
||
import cats.effect.IO | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
private[metrics] class CpuStarvation private ( | ||
counter: AtomicLong, | ||
currentClockDrift: AtomicLong, | ||
maxClockDrift: AtomicLong) | ||
extends CpuStarvationMBean { | ||
|
||
override def getCpuStarvationCount(): Long = counter.get() | ||
|
||
override def getMaxClockDriftMs(): Long = maxClockDrift.get() | ||
private final class CpuStarvation(sampler: CpuStarvationSampler) extends CpuStarvationMBean { | ||
def getCpuStarvationCount(): Long = | ||
sampler.cpuStarvationCount() | ||
|
||
override def getCurrentClockDriftMs(): Long = currentClockDrift.get() | ||
|
||
def incStarvationCount: IO[Unit] = IO.delay(counter.incrementAndGet()).void | ||
|
||
def recordDrift(drift: FiniteDuration): IO[Unit] = { | ||
val driftMs = drift.toMillis | ||
|
||
val maxDrift = | ||
if (driftMs > 0) IO.delay(maxClockDrift.updateAndGet(math.max(_, driftMs))).void | ||
else IO.unit | ||
|
||
IO.delay(currentClockDrift.set(driftMs)) >> maxDrift | ||
} | ||
def getMaxClockDriftMs(): Long = | ||
sampler.clockDriftMaxMs() | ||
|
||
def getCurrentClockDriftMs(): Long = | ||
sampler.clockDriftCurrentMs() | ||
} | ||
|
||
private[metrics] object CpuStarvation { | ||
private[metrics] def apply(): IO[CpuStarvation] = for { | ||
counter <- IO.delay(new AtomicLong(0)) | ||
currentClockDrift <- IO.delay(new AtomicLong(0)) | ||
maxClockDrift <- IO.delay(new AtomicLong(0)) | ||
} yield new CpuStarvation(counter, currentClockDrift, maxClockDrift) | ||
} | ||
private object CpuStarvation |
15 changes: 15 additions & 0 deletions
15
core/jvm/src/main/scala/cats/effect/metrics/IORuntimeMetricsCompanionPlatform.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,15 @@ | ||
package cats.effect.metrics | ||
|
||
private[metrics] abstract class IORuntimeMetricsCompanionPlatform { | ||
this: IORuntimeMetrics.type => | ||
|
||
private[effect] def apply(): IORuntimeMetrics = | ||
new IORuntimeMetrics { | ||
private[effect] val cpuStarvationSampler: CpuStarvationSampler = | ||
CpuStarvationSampler() | ||
|
||
val cpuStarvation: CpuStarvationMetrics = | ||
CpuStarvationMetrics(cpuStarvationSampler) | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
core/jvm/src/main/scala/cats/effect/metrics/IORuntimeMetricsPlatform.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,3 @@ | ||
package cats.effect.metrics | ||
|
||
private[metrics] trait IORuntimeMetricsPlatform { this: IORuntimeMetrics => } |
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
31 changes: 0 additions & 31 deletions
31
core/native/src/main/scala/cats/effect/metrics/NativeCpuStarvationMetrics.scala
This file was deleted.
Oops, something went wrong.
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
61 changes: 40 additions & 21 deletions
61
core/shared/src/main/scala/cats/effect/metrics/CpuStarvationMetrics.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 |
---|---|---|
@@ -1,27 +1,46 @@ | ||
/* | ||
* Copyright 2020-2024 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect.metrics | ||
|
||
import cats.effect.IO | ||
import scala.concurrent.duration._ | ||
|
||
sealed trait CpuStarvationMetrics { | ||
|
||
/** | ||
* Returns the current number of times CPU starvation has occurred. | ||
* | ||
* @note | ||
* the value may differ between invocations | ||
*/ | ||
def starvationCount(): Long | ||
|
||
/** | ||
* Returns the current (last) observed clock drift. | ||
* | ||
* @note | ||
* the value may differ between invocations | ||
*/ | ||
def clockDriftCurrent(): FiniteDuration | ||
|
||
/** | ||
* Returns the maximum clock drift observed since the launch. | ||
* | ||
* @note | ||
* the value may differ between invocations | ||
*/ | ||
def clockDriftMax(): FiniteDuration | ||
} | ||
|
||
object CpuStarvationMetrics { | ||
|
||
private[metrics] def apply(sampler: CpuStarvationSampler): CpuStarvationMetrics = | ||
new CpuStarvationMetrics { | ||
def starvationCount(): Long = | ||
sampler.cpuStarvationCount() | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
def clockDriftCurrent(): FiniteDuration = | ||
sampler.clockDriftCurrentMs().millis | ||
|
||
private[effect] trait CpuStarvationMetrics { | ||
def incCpuStarvationCount: IO[Unit] | ||
def clockDriftMax(): FiniteDuration = | ||
sampler.clockDriftMaxMs().millis | ||
} | ||
|
||
def recordClockDrift(drift: FiniteDuration): IO[Unit] | ||
} |
Oops, something went wrong.