Skip to content

Commit 61a2ad9

Browse files
authored
Merge pull request #4186 from iRevive/metrics-pool-id
Use global counter as an MBean ID for WSTP
2 parents 9feee5c + bb172dd commit 61a2ad9

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

core/jvm/src/main/scala/cats/effect/unsafe/IORuntimeCompanionPlatform.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ private[unsafe] abstract class IORuntimeCompanionPlatform { this: IORuntime.type
104104
if (mBeanServer ne null) {
105105
val registeredMBeans = mutable.Set.empty[ObjectName]
106106

107-
val hash = System.identityHashCode(threadPool).toHexString
107+
val threadPoolId = threadPool.id
108108

109109
try {
110110
val computePoolSamplerName = new ObjectName(
111-
s"cats.effect.unsafe.metrics:type=ComputePoolSampler-$hash")
111+
s"cats.effect.unsafe.metrics:type=ComputePoolSampler-$threadPoolId")
112112
val computePoolSampler = new ComputePoolSampler(threadPool)
113113
mBeanServer.registerMBean(computePoolSampler, computePoolSamplerName)
114114
registeredMBeans += computePoolSamplerName
@@ -125,7 +125,7 @@ private[unsafe] abstract class IORuntimeCompanionPlatform { this: IORuntime.type
125125

126126
try {
127127
val localQueueSamplerName = new ObjectName(
128-
s"cats.effect.unsafe.metrics:type=LocalQueueSampler-$hash-$i")
128+
s"cats.effect.unsafe.metrics:type=LocalQueueSampler-$threadPoolId-$i")
129129
val localQueueSampler = new LocalQueueSampler(localQueue)
130130
mBeanServer.registerMBean(localQueueSampler, localQueueSamplerName)
131131
registeredMBeans += localQueueSamplerName
@@ -264,11 +264,10 @@ private[unsafe] abstract class IORuntimeCompanionPlatform { this: IORuntime.type
264264
}
265265

266266
if (mBeanServer ne null) {
267-
val hash = System.identityHashCode(fiberMonitor).toHexString
268-
269267
try {
268+
val mbeanId = LiveFiberSnapshotTrigger.IdCounter.getAndIncrement()
270269
val liveFiberSnapshotTriggerName = new ObjectName(
271-
s"cats.effect.unsafe.metrics:type=LiveFiberSnapshotTrigger-$hash")
270+
s"cats.effect.unsafe.metrics:type=LiveFiberSnapshotTrigger-$mbeanId")
272271
val liveFiberSnapshotTrigger = new LiveFiberSnapshotTrigger(fiberMonitor)
273272
mBeanServer.registerMBean(liveFiberSnapshotTrigger, liveFiberSnapshotTriggerName)
274273

core/jvm/src/main/scala/cats/effect/unsafe/WorkStealingThreadPool.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import java.time.Instant
4242
import java.time.temporal.ChronoField
4343
import java.util.Comparator
4444
import java.util.concurrent.{ConcurrentSkipListSet, ThreadLocalRandom}
45-
import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger, AtomicReference}
45+
import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger, AtomicLong, AtomicReference}
4646

4747
import WorkStealingThreadPool._
4848

@@ -77,6 +77,10 @@ private[effect] final class WorkStealingThreadPool[P <: AnyRef](
7777
import TracingConstants._
7878
import WorkStealingThreadPoolConstants._
7979

80+
// a unique identifier of the thread pool within a JVM
81+
// used in the MBean name and as an identifier in metrics
82+
private[unsafe] val id = WorkStealingThreadPool.IdCounter.getAndIncrement()
83+
8084
/**
8185
* References to worker threads and their local queues.
8286
*/
@@ -840,6 +844,8 @@ private[effect] final class WorkStealingThreadPool[P <: AnyRef](
840844

841845
private object WorkStealingThreadPool {
842846

847+
private val IdCounter: AtomicLong = new AtomicLong(0)
848+
843849
/**
844850
* A wrapper for a cancelation callback that is created asynchronously.
845851
*/

core/jvm/src/main/scala/cats/effect/unsafe/metrics/LiveFiberSnapshotTrigger.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package metrics
1919

2020
import scala.collection.mutable.ArrayBuffer
2121

22+
import java.util.concurrent.atomic.AtomicLong
23+
2224
/**
2325
* An implementation of the [[LiveFiberSnapshotTriggerMBean]] interface which simply delegates
2426
* to the corresponding method of the backing [[cats.effect.unsafe.FiberMonitor]].
@@ -34,3 +36,7 @@ private[unsafe] final class LiveFiberSnapshotTrigger(monitor: FiberMonitor)
3436
buffer.toArray
3537
}
3638
}
39+
40+
private[unsafe] object LiveFiberSnapshotTrigger {
41+
val IdCounter: AtomicLong = new AtomicLong(0)
42+
}

core/jvm/src/main/scala/cats/effect/unsafe/metrics/WorkStealingPoolMetrics.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import scala.concurrent.ExecutionContext
2525
sealed trait WorkStealingPoolMetrics {
2626

2727
/**
28-
* The hash code of the instrumented work-stealing thread pool. This hash uniquely identifies
29-
* the specific thread pool.
28+
* The identifier of the instrumented work-stealing thread pool. Uniquely identifies a
29+
* specific thread pool within a JVM.
3030
*/
31-
def hash: String
31+
def identifier: String
3232

3333
/**
3434
* Compute-specific metrics of the work-stealing thread pool.
@@ -205,8 +205,8 @@ object WorkStealingPoolMetrics {
205205
ec match {
206206
case wstp: WorkStealingThreadPool[_] =>
207207
val metrics = new WorkStealingPoolMetrics {
208-
val hash: String =
209-
System.identityHashCode(wstp).toHexString
208+
val identifier: String =
209+
wstp.id.toString
210210

211211
val compute: ComputeMetrics =
212212
computeMetrics(wstp)

0 commit comments

Comments
 (0)