Skip to content

Commit

Permalink
chore: Make set asynchronous, also don't compute model on EDT
Browse files Browse the repository at this point in the history
  • Loading branch information
bric3 committed Apr 8, 2024
1 parent 080f086 commit aed627d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ class ExperimentalButterfly(private val jfrBinder: JFRLoaderBinder) : ViewPanel
override val view by lazy {
val butterflyPane = ButterflyPane()

jfrBinder.bindEvents(JfrAnalyzer::executionSamples) {
val stacktraceButterflyModel = it.stacktraceButterflyModel(nodeSelector = { frame ->
jfrBinder.bindEvents({
val executionSamples = JfrAnalyzer.executionSamples(it)
executionSamples.stacktraceButterflyModel(nodeSelector = { frame ->
(frame.method.type.typeName == "StupidMain" && frame.method.methodName == "work") ||
(frame.method.type.typeName.contains("CoreProgressManager") && frame.method.methodName.contains("computeUnderProgress"))
(frame.method.type.typeName.contains("CoreProgressManager") && frame.method.methodName.contains(
"computeUnderProgress"
))
})

butterflyPane.setStacktraceButterflyModel(stacktraceButterflyModel)
}) {
butterflyPane.setStacktraceButterflyModelAsync(it)
}

butterflyPane
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package io.github.bric3.fireplace.ui

import io.github.bric3.fireplace.Utils
import io.github.bric3.fireplace.core.ui.Colors
import io.github.bric3.fireplace.core.ui.SwingUtils
import io.github.bric3.fireplace.flamegraph.ButterflyView
import io.github.bric3.fireplace.flamegraph.ColorMapper
import io.github.bric3.fireplace.flamegraph.DimmingFrameColorProvider
Expand All @@ -33,6 +34,7 @@ import java.awt.Color
import java.awt.event.ActionListener
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.util.concurrent.CompletableFuture
import java.util.function.Consumer
import java.util.function.Function
import java.util.regex.Pattern
Expand Down Expand Up @@ -145,31 +147,36 @@ class ButterflyPane : JPanel(BorderLayout()) {
return refreshToggle
}

fun setStacktraceButterflyModel(stacktraceButterflyModel: StacktraceButterflyModel) {
dataApplier = dataApplier(stacktraceButterflyModel).also {
it.accept(jfrButterflyView)
fun setStacktraceButterflyModelAsync(stacktraceButterflyModel: StacktraceButterflyModel) {
CompletableFuture.runAsync {
dataApplier = dataApplier(stacktraceButterflyModel).also {
it.accept(jfrButterflyView)
}
}
}

private fun dataApplier(stacktraceButterflyModel: StacktraceButterflyModel): Consumer<ButterflyView<Node>> {
val predecessorsFlatFrameList = convertButterfly(stacktraceButterflyModel.predecessorsRoot, predecessorsWeight())
val predecessorsFlatFrameList =
convertButterfly(stacktraceButterflyModel.predecessorsRoot, predecessorsWeight())
val successorsFlatFrameList = convertButterfly(stacktraceButterflyModel.successorsRoot, successorsWeight())
val title = stacktraceButterflyModel.focusedMethod.method.methodName
return Consumer { flameGraph ->
val frameEquality: (a: FrameBox<Node>, b: FrameBox<Node>) -> Boolean =
{ a, b -> a.actualNode.frame == b.actualNode.frame }
flameGraph.setModel(
FrameModel(
title,
frameEquality,
predecessorsFlatFrameList
).withDescription(title),
FrameModel(
title,
frameEquality,
successorsFlatFrameList
).withDescription(title),
)
SwingUtils.invokeLater {
flameGraph.setModel(
FrameModel(
title,
frameEquality,
predecessorsFlatFrameList
).withDescription(title),
FrameModel(
title,
frameEquality,
successorsFlatFrameList
).withDescription(title),
)
}
}
}

Expand All @@ -181,13 +188,13 @@ class ButterflyPane : JPanel(BorderLayout()) {
* Stupid method mimicking [JfrFrameColorMode.BY_PACKAGE.getJfrNodeColor] to accommodate
* [io.github.bric3.fireplace.jfr.tree.Node] duplicata.
*/
fun colorMapping(colorMapper: ColorMapper<Any?>): Function<FrameBox<io.github.bric3.fireplace.jfr.tree.Node>, Color> {
fun colorMapping(colorMapper: ColorMapper<Any?>): Function<FrameBox<Node>, Color> {
val runtimePrefixes =
Pattern.compile("(java\\.|javax\\.|sun\\.|com\\.sun\\.|com\\.oracle\\.|com\\.ibm\\.|jdk\\.)")

fun byPackage(
colorMapper: ColorMapper<Any?>,
frameNode: io.github.bric3.fireplace.jfr.tree.Node
frameNode: Node
): Color {
if (frameNode.isRoot) {
return JfrFrameColorMode.rootNodeColor
Expand All @@ -206,7 +213,7 @@ class ButterflyPane : JPanel(BorderLayout()) {
}
return colorMapper.apply(name)
}
return Function { frameBox: FrameBox<io.github.bric3.fireplace.jfr.tree.Node> ->
return Function { frameBox: FrameBox<Node> ->
byPackage(colorMapper, frameBox.actualNode)
}
}
Expand Down

0 comments on commit aed627d

Please sign in to comment.