Skip to content

Commit

Permalink
refactor(flamegraph)!: Color function now takes a framebox
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Signature change on the color function
  • Loading branch information
bric3 committed Apr 13, 2022
1 parent d903d95 commit 69ac60c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Object, Color> colorMapper, Node frameNode) {
public Color getJfrNodeColor(Function<Object, Color> colorMapper, Node frameNode) {
if (frameNode.isRoot()) {
return rootNodeColor;
}
Expand All @@ -42,7 +43,7 @@ public Color getColor(Function<Object, Color> colorMapper, Node frameNode) {
},
BY_MODULE {
@Override
public Color getColor(Function<Object, Color> colorMapper, Node frameNode) {
public Color getJfrNodeColor(Function<Object, Color> colorMapper, Node frameNode) {
if (frameNode.isRoot()) {
return rootNodeColor;
}
Expand All @@ -51,7 +52,7 @@ public Color getColor(Function<Object, Color> colorMapper, Node frameNode) {
},
BY_FRAME_TYPE {
@Override
public Color getColor(Function<Object, Color> colorMapper, Node frameNode) {
public Color getJfrNodeColor(Function<Object, Color> colorMapper, Node frameNode) {
if (frameNode.isRoot()) {
return rootNodeColor;
}
Expand All @@ -76,9 +77,9 @@ public Color getColor(Function<Object, Color> colorMapper, Node frameNode) {
public static Color inlinedColor = Color.pink;
public static Color interpretedColor = Color.orange;

protected abstract Color getColor(Function<Object, Color> colorMapper, Node frameNode);
protected abstract Color getJfrNodeColor(Function<Object, Color> colorMapper, Node frameNode);

public Function<Node, Color> colorMapperUsing(Function<Object, Color> colorMapper) {
return frameNode -> getColor(colorMapper, frameNode);
public Function<FrameBox<Node>, Color> colorMapperUsing(Function<Object, Color> colorMapper) {
return frameNode -> getJfrNodeColor(colorMapper, frameNode.actualNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public interface ColorMapper<T> extends Function<T, Color> {
* @return A color.
*/
static ColorMapper<Object> 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];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void configureCanvas(Consumer<JComponent> canvasConfigurer) {
*
* @param frameColorFunction A function that takes a frame and returns a color.
*/
public void setColorFunction(Function<T, Color> frameColorFunction) {
public void setColorFunction(Function<FrameBox<T>, Color> frameColorFunction) {
Objects.requireNonNull(frameColorFunction);
this.canvas.getFlameGraphPainter()
.ifPresent(fgp -> fgp.frameColorFunction = frameColorFunction);
Expand Down Expand Up @@ -236,7 +236,7 @@ public void setHoveringListener(HoveringListener<T> hoverListener) {
*/
public void setData(List<FrameBox<T>> frames,
NodeDisplayStringProvider<T> frameToString,
Function<T, Color> frameColorFunction,
Function<FrameBox<T>, Color> frameColorFunction,
Function<FrameBox<T>, String> tooltipTextFunction) {
var flameGraphPainter = new FlameGraphPainter<>(
Objects.requireNonNull(frames),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public class FlameGraphPainter<T> {

private final List<FrameBox<T>> frames;
private final NodeDisplayStringProvider<T> nodeToTextProvider;
Function<T, Color> frameColorFunction;
Function<FrameBox<T>, Color> frameColorFunction;
private final int internalPadding = 2;

/**
Expand All @@ -138,7 +138,7 @@ public class FlameGraphPainter<T> {
*/
public FlameGraphPainter(List<FrameBox<T>> frames,
NodeDisplayStringProvider<T> nodeToTextProvider,
Function<T, Color> frameColorFunction) {
Function<FrameBox<T>, Color> frameColorFunction) {

this.frameLabelFont = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
this.partialFrameLabelFont = new Font(Font.SANS_SERIF, Font.ITALIC, 12);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void loadJfr(String[] args, Label text, FlameGraph<Node> 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) {
Expand Down

0 comments on commit 69ac60c

Please sign in to comment.