我在 OpenJDK11 中使用 HotSpot 的ExecutionSample 事件。
它有一个线程状态字段,但我只看到该字段的一个值:STATE_RUNNABLE
HotSpot 如何选择要采样的线程?为什么它们总是可运行的?
Kotlin 中的简单复制代码:
import jdk.jfr.Recording
import jdk.jfr.consumer.RecordingFile
import java.nio.file.Path
object ExecutionSampleTest {
private const val EXECUTION_SAMPLE = "jdk.ExecutionSample"
private val RECORDING_PATH = Path.of("/tmp/recording.jfr")
@JvmStatic
fun main(args: Array<String>) {
Recording().use { recording ->
recording.enable(EXECUTION_SAMPLE)
recording.settings = recording.settings.plus("$EXECUTION_SAMPLE#period" to "1 ms")
recording.start()
repeat(100) {
// start some sleeping threads, just so we've got something to sample
Thread { Thread.sleep(20_000) }.start()
}
Thread.sleep(20_000)
recording.stop()
recording.dump(RECORDING_PATH)
RecordingFile.readAllEvents(RECORDING_PATH).forEach {
println("Thread state: ${it.getString("state")}")
}
}
}
}
只打印:Thread state: STATE_RUNNABLE