Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlockHound integration #1821

Merged
merged 15 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Intermediate value
  • Loading branch information
dkhalanskyjb committed Mar 13, 2020
commit d1e47985ff61ad9b73d02cf2322d50fb3312de0c
Original file line number Diff line number Diff line change
Expand Up @@ -951,3 +951,12 @@ internal class CoroutineScheduler(
TERMINATED
}
}

@Suppress("UNUSED")
@JvmName("isSchedulerWorker")
internal fun isSchedulerWorker(thread: Thread) = thread is CoroutineScheduler.Worker
qwwdfsad marked this conversation as resolved.
Show resolved Hide resolved

@Suppress("UNUSED")
@JvmName("mayNotBlock")
internal fun mayNotBlock(thread: Thread) = thread is CoroutineScheduler.Worker &&
thread.state == CoroutineScheduler.WorkerState.CPU_ACQUIRED
26 changes: 5 additions & 21 deletions kotlinx-coroutines-debug/src/internal/BlockHoundIntegration.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
package kotlinx.coroutines.debug.internal

import reactor.blockhound.BlockHound
import kotlin.reflect.KClass
import kotlin.reflect.full.*
import kotlinx.coroutines.scheduling.*

internal object BlockHoundIntegration {

init {
val cls = Class.forName("kotlinx.coroutines.scheduling.CoroutineScheduler\$Worker").kotlin
initializerHelper(cls)
}

private fun <T : Any> initializerHelper(cls: KClass<T>) {
val field = cls.declaredMemberProperties.find { it.name == "state" }!!
BlockHound.builder()
.addDynamicThreadPredicate(cls::isInstance)
.nonBlockingThreadPredicate { p ->
p.or { thread ->
val castThread = cls.safeCast(thread)
if (!enabled || castThread == null) {
false
} else {
val state = field(castThread) as Enum<*>
state.name == "CPU_ACQUIRED"
}
}
}
.install()
.addDynamicThreadPredicate { isSchedulerWorker(it) }
.nonBlockingThreadPredicate { p -> p.or { thread -> enabled && mayNotBlock(thread) } }
.install()
}

@Volatile
Expand Down