From 69ac60c1c3cd624555dc32b9a098038fadeafa2c Mon Sep 17 00:00:00 2001 From: Brice Dutheil Date: Wed, 13 Apr 2022 15:11:15 +0200 Subject: [PATCH] refactor(flamegraph)!: Color function now takes a framebox BREAKING CHANGE: Signature change on the color function --- .../github/bric3/fireplace/JfrFrameColorMode.java | 13 +++++++------ .../bric3/fireplace/flamegraph/ColorMapper.java | 4 ++-- .../bric3/fireplace/flamegraph/FlameGraph.java | 4 ++-- .../fireplace/flamegraph/FlameGraphPainter.java | 8 ++++---- .../io/github/bric3/fireplace/FirePlaceSwtMain.java | 2 +- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/fireplace-app/src/main/java/io/github/bric3/fireplace/JfrFrameColorMode.java b/fireplace-app/src/main/java/io/github/bric3/fireplace/JfrFrameColorMode.java index 7d119734..47dd26f6 100644 --- a/fireplace-app/src/main/java/io/github/bric3/fireplace/JfrFrameColorMode.java +++ b/fireplace-app/src/main/java/io/github/bric3/fireplace/JfrFrameColorMode.java @@ -9,6 +9,7 @@ */ package io.github.bric3.fireplace; +import io.github.bric3.fireplace.flamegraph.FrameBox; import org.openjdk.jmc.common.IMCFrame.Type; import org.openjdk.jmc.flightrecorder.stacktrace.tree.Node; @@ -24,7 +25,7 @@ public enum JfrFrameColorMode { final Pattern runtimePrefixes = Pattern.compile("(java\\.|javax\\.|sun\\.|com\\.sun\\.|com\\.oracle\\.|com\\.ibm\\.)"); @Override - public Color getColor(Function colorMapper, Node frameNode) { + public Color getJfrNodeColor(Function colorMapper, Node frameNode) { if (frameNode.isRoot()) { return rootNodeColor; } @@ -42,7 +43,7 @@ public Color getColor(Function colorMapper, Node frameNode) { }, BY_MODULE { @Override - public Color getColor(Function colorMapper, Node frameNode) { + public Color getJfrNodeColor(Function colorMapper, Node frameNode) { if (frameNode.isRoot()) { return rootNodeColor; } @@ -51,7 +52,7 @@ public Color getColor(Function colorMapper, Node frameNode) { }, BY_FRAME_TYPE { @Override - public Color getColor(Function colorMapper, Node frameNode) { + public Color getJfrNodeColor(Function colorMapper, Node frameNode) { if (frameNode.isRoot()) { return rootNodeColor; } @@ -76,9 +77,9 @@ public Color getColor(Function colorMapper, Node frameNode) { public static Color inlinedColor = Color.pink; public static Color interpretedColor = Color.orange; - protected abstract Color getColor(Function colorMapper, Node frameNode); + protected abstract Color getJfrNodeColor(Function colorMapper, Node frameNode); - public Function colorMapperUsing(Function colorMapper) { - return frameNode -> getColor(colorMapper, frameNode); + public Function, Color> colorMapperUsing(Function colorMapper) { + return frameNode -> getJfrNodeColor(colorMapper, frameNode.actualNode); } } diff --git a/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/ColorMapper.java b/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/ColorMapper.java index d6e4b21a..77175852 100644 --- a/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/ColorMapper.java +++ b/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/ColorMapper.java @@ -27,9 +27,9 @@ public interface ColorMapper extends Function { * @return A color. */ static ColorMapper ofObjectHashUsing(Color... palette) { - return value -> value == null ? + return o -> o == null ? palette[0] : - palette[Math.abs(Objects.hashCode(value)) % palette.length]; + palette[Math.abs(Objects.hashCode(o)) % palette.length]; } /** diff --git a/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/FlameGraph.java b/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/FlameGraph.java index 9e62e185..2f66141f 100644 --- a/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/FlameGraph.java +++ b/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/FlameGraph.java @@ -146,7 +146,7 @@ public void configureCanvas(Consumer canvasConfigurer) { * * @param frameColorFunction A function that takes a frame and returns a color. */ - public void setColorFunction(Function frameColorFunction) { + public void setColorFunction(Function, Color> frameColorFunction) { Objects.requireNonNull(frameColorFunction); this.canvas.getFlameGraphPainter() .ifPresent(fgp -> fgp.frameColorFunction = frameColorFunction); @@ -236,7 +236,7 @@ public void setHoveringListener(HoveringListener hoverListener) { */ public void setData(List> frames, NodeDisplayStringProvider frameToString, - Function frameColorFunction, + Function, Color> frameColorFunction, Function, String> tooltipTextFunction) { var flameGraphPainter = new FlameGraphPainter<>( Objects.requireNonNull(frames), diff --git a/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/FlameGraphPainter.java b/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/FlameGraphPainter.java index f6fdbe02..c09acc99 100644 --- a/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/FlameGraphPainter.java +++ b/fireplace-swing/src/main/java/io/github/bric3/fireplace/flamegraph/FlameGraphPainter.java @@ -118,7 +118,7 @@ public class FlameGraphPainter { private final List> frames; private final NodeDisplayStringProvider nodeToTextProvider; - Function frameColorFunction; + Function, Color> frameColorFunction; private final int internalPadding = 2; /** @@ -138,7 +138,7 @@ public class FlameGraphPainter { */ public FlameGraphPainter(List> frames, NodeDisplayStringProvider nodeToTextProvider, - Function frameColorFunction) { + Function, Color> frameColorFunction) { this.frameLabelFont = new Font(Font.SANS_SERIF, Font.PLAIN, 12); this.partialFrameLabelFont = new Font(Font.SANS_SERIF, Font.ITALIC, 12); @@ -290,7 +290,7 @@ private void paint(Graphics2D g2, Rectangle2D bounds, Rectangle2D viewRect, bool rootFrame, intersection, tweakLabelFont(frameRect, intersection, false), - tweakBgColor(frameColorFunction.apply(rootFrame.actualNode), + tweakBgColor(frameColorFunction.apply(rootFrame), hoveredFrame == rootFrame, false, selectedFrame != null && rootFrame.stackDepth < selectedFrame.stackDepth), @@ -322,7 +322,7 @@ private void paint(Graphics2D g2, Rectangle2D bounds, Rectangle2D viewRect, bool paintableIntersection, // choose font depending on whether the left-side of the frame is clipped tweakLabelFont(frameRect, paintableIntersection, toHighlight.contains(frame)), - tweakBgColor(frameColorFunction.apply(frame.actualNode), + tweakBgColor(frameColorFunction.apply(frame), hoveredFrame == frame, toHighlight.contains(frame), selectedFrame != null && ( diff --git a/fireplace-swt-experiment/src/main/java/io/github/bric3/fireplace/FirePlaceSwtMain.java b/fireplace-swt-experiment/src/main/java/io/github/bric3/fireplace/FirePlaceSwtMain.java index fae4fb0c..b1a92d04 100644 --- a/fireplace-swt-experiment/src/main/java/io/github/bric3/fireplace/FirePlaceSwtMain.java +++ b/fireplace-swt-experiment/src/main/java/io/github/bric3/fireplace/FirePlaceSwtMain.java @@ -186,7 +186,7 @@ private void loadJfr(String[] args, Label text, FlameGraph fg) { frame -> frame.isRoot() ? "" : FormatToolkit.getHumanReadable(frame.actualNode.getFrame().getMethod(), false, false, false, false, true, false), frame -> frame.isRoot() ? "" : frame.actualNode.getFrame().getMethod().getMethodName() ), - node -> ColorMapper.ofObjectHashUsing(Palette.DATADOG.colors()).apply(node.getFrame().getMethod().getType().getPackage()), + frame -> ColorMapper.ofObjectHashUsing(Palette.DATADOG.colors()).apply(frame.actualNode.getFrame().getMethod().getType().getPackage()), frame -> "" // frame -> { // if (frame.stackDepth == 0) {