Skip to content

Commit

Permalink
Add metric to count I/O polling
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Dec 19, 2024
1 parent bc80b8d commit 2dc9288
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ private[effect] final class WorkerThread[P <: AnyRef](
while (!done.get()) {
// Park the thread until further notice.
val start = System.nanoTime()
metrics.incrementPolledCount()
val polled = system.poll(_poller, -1, reportFailure)
now = System.nanoTime() // update now
metrics.addIdleTime(now - start)
Expand Down Expand Up @@ -462,6 +463,7 @@ private[effect] final class WorkerThread[P <: AnyRef](

if (nanos > 0L) {
val start = now
metrics.incrementPolledCount()
val polled = system.poll(_poller, nanos, reportFailure)
// we already parked and time passed, so update time again
// it doesn't matter if we timed out or were awakened, the update is free-ish
Expand Down Expand Up @@ -579,6 +581,7 @@ private[effect] final class WorkerThread[P <: AnyRef](
// Clean up any externally canceled timers
sleepers.packIfNeeded()
// give the polling system a chance to discover events
metrics.incrementPolledCount()
system.poll(_poller, 0, reportFailure)

// Obtain a fiber or batch of fibers from the external queue.
Expand Down Expand Up @@ -981,6 +984,10 @@ private[effect] object WorkerThread {
def getParkedCount(): Long = parkedCount
def incrementParkedCount(): Unit = parkedCount += 1

private[this] var polledCount: Long = 0
def getPolledCount(): Long = polledCount
def incrementPolledCount(): Unit = polledCount += 1

private[this] var blockingCount: Long = 0
def getBlockingCount(): Long = blockingCount
def incrementBlockingCount(): Unit = blockingCount += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ trait WorkerThreadMetrics {
*/
def parkedCount(): Long

/**
* The total number of times that this WorkerThread has polled for I/O events.
*/
def polledCount(): Long

/**
* The total number of times that this WorkerThread has switched to a blocking thread and been
* replaced.
Expand Down Expand Up @@ -262,6 +267,7 @@ object WorkStealingPoolMetrics {
private val metrics = wstp.metrices(idx)
def idleTime(): Long = metrics.getIdleTime()
def parkedCount(): Long = metrics.getParkedCount()
def polledCount(): Long = metrics.getPolledCount()
def blockingCount(): Long = metrics.getBlockingCount()
def respawnCount(): Long = metrics.getBlockingCount()

Expand Down

0 comments on commit 2dc9288

Please sign in to comment.