| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are made |
| 5 | * available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | *******************************************************************************/ |
| 9 | package org.eclipse.tracecompass.internal.analysis.timing.ui.flamegraph; |
| 10 | |
| 11 | import java.text.Format; |
| 12 | import java.text.NumberFormat; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | import org.eclipse.swt.SWT; |
| 16 | import org.eclipse.swt.graphics.GC; |
| 17 | import org.eclipse.swt.graphics.RGB; |
| 18 | import org.eclipse.swt.graphics.Rectangle; |
| 19 | import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.SubSecondTimeWithUnitFormat; |
| 20 | import org.eclipse.tracecompass.internal.analysis.timing.core.callgraph.AggregatedCalledFunctionStatistics; |
| 21 | import org.eclipse.tracecompass.internal.analysis.timing.core.callgraph.ICalledFunction; |
| 22 | import org.eclipse.tracecompass.internal.analysis.timing.core.callgraph.SymbolAspect; |
| 23 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 24 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 25 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem; |
| 26 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider; |
| 27 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; |
| 28 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent; |
| 29 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils; |
| 30 | |
| 31 | import com.google.common.collect.ImmutableMap; |
| 32 | |
| 33 | /** |
| 34 | * Presentation provider for the flame graph view, based on the generic TMF |
| 35 | * presentation provider. |
| 36 | * |
| 37 | * @author Sonia Farrah |
| 38 | */ |
| 39 | public class FlameGraphPresentationProvider extends TimeGraphPresentationProvider { |
| 40 | /** Number of colors used for flameGraph events */ |
| 41 | public static final int NUM_COLORS = 360; |
| 42 | |
| 43 | private static final Format FORMATTER = new SubSecondTimeWithUnitFormat(); |
| 44 | |
| 45 | private FlameGraphView fView; |
| 46 | |
| 47 | private Integer fAverageCharWidth; |
| 48 | |
| 49 | private enum State { |
| 50 | MULTIPLE(new RGB(100, 100, 100)), EXEC(new RGB(0, 200, 0)); |
| 51 | |
| 52 | private final RGB rgb; |
| 53 | |
| 54 | private State(RGB rgb) { |
| 55 | this.rgb = rgb; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Constructor |
| 61 | */ |
| 62 | public FlameGraphPresentationProvider() { |
| 63 | // Do nothing |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public StateItem[] getStateTable() { |
| 68 | final float saturation = 0.6f; |
| 69 | final float brightness = 0.6f; |
| 70 | StateItem[] stateTable = new StateItem[NUM_COLORS + 1]; |
| 71 | stateTable[0] = new StateItem(State.MULTIPLE.rgb, State.MULTIPLE.toString()); |
| 72 | for (int i = 0; i < NUM_COLORS; i++) { |
| 73 | RGB rgb = new RGB(i, saturation, brightness); |
| 74 | stateTable[i + 1] = new StateItem(rgb, State.EXEC.toString()); |
| 75 | } |
| 76 | return stateTable; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean displayTimesInTooltip() { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String getStateTypeName() { |
| 86 | return Messages.FlameGraph_Depth; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event, long hoverTime) { |
| 91 | AggregatedCalledFunctionStatistics statistics = ((FlamegraphEvent) event).getStatistics(); |
| 92 | ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>(); |
| 93 | builder.put(Messages.FlameGraph_NbCalls, NumberFormat.getIntegerInstance().format(statistics.getDurationStatistics().getNbElements())); // $NON-NLS-1$ |
| 94 | builder.put(String.valueOf(Messages.FlameGraph_Durations), ""); //$NON-NLS-1$ |
| 95 | builder.put("\t" + Messages.FlameGraph_Duration, FORMATTER.format(event.getDuration())); //$NON-NLS-1$ |
| 96 | builder.put("\t" + Messages.FlameGraph_AverageDuration, FORMATTER.format(statistics.getDurationStatistics().getMean())); // $NON-NLS-1$ //$NON-NLS-1$ |
| 97 | builder.put("\t" + Messages.FlameGraph_MaxDuration, FORMATTER.format((statistics.getDurationStatistics().getMax()))); // $NON-NLS-1$ //$NON-NLS-1$ |
| 98 | builder.put("\t" + Messages.FlameGraph_MinDuration, FORMATTER.format(statistics.getDurationStatistics().getMin())); // $NON-NLS-1$ //$NON-NLS-1$ |
| 99 | builder.put("\t" + Messages.FlameGraph_Deviation, FORMATTER.format(statistics.getDurationStatistics().getStdDev())); //$NON-NLS-1$ |
| 100 | builder.put(Messages.FlameGraph_SelfTimes, ""); //$NON-NLS-1$ |
| 101 | builder.put("\t" + Messages.FlameGraph_SelfTime, FORMATTER.format(((FlamegraphEvent) event).getSelfTime())); //$NON-NLS-1$ |
| 102 | builder.put("\t" + Messages.FlameGraph_AverageSelfTime, FORMATTER.format(statistics.getSelfTimeStatistics().getMean())); // $NON-NLS-1$ //$NON-NLS-1$ |
| 103 | builder.put("\t" + Messages.FlameGraph_MaxSelfTime, FORMATTER.format(statistics.getSelfTimeStatistics().getMax())); // $NON-NLS-1$ //$NON-NLS-1$ |
| 104 | builder.put("\t" + Messages.FlameGraph_MinSelfTime, FORMATTER.format(statistics.getSelfTimeStatistics().getMin())); // $NON-NLS-1$ //$NON-NLS-1$ |
| 105 | builder.put("\t" + Messages.FlameGraph_SelfTimeDeviation, FORMATTER.format(statistics.getSelfTimeStatistics().getStdDev())); //$NON-NLS-1$ |
| 106 | return builder.build(); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int getStateTableIndex(ITimeEvent event) { |
| 112 | if (event instanceof FlamegraphEvent) { |
| 113 | FlamegraphEvent flameGraphEvent = (FlamegraphEvent) event; |
| 114 | return flameGraphEvent.getValue() + 1; |
| 115 | } else if (event instanceof NullTimeEvent) { |
| 116 | return INVISIBLE; |
| 117 | } |
| 118 | return State.MULTIPLE.ordinal(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the event's symbol.It could be an address or a name. |
| 123 | * |
| 124 | * @param fGEvent |
| 125 | * An event |
| 126 | */ |
| 127 | private static String getFunctionSymbol(FlamegraphEvent event) { |
| 128 | String funcSymbol = ""; //$NON-NLS-1$ |
| 129 | if (event.getSymbol() instanceof Long || event.getSymbol() instanceof Integer) { |
| 130 | |
| 131 | ICalledFunction segment = event.getStatistics().getDurationStatistics().getMinObject(); |
| 132 | if (segment == null) { |
| 133 | long longAddress = ((Long) event.getSymbol()).longValue(); |
| 134 | return "0x" + Long.toHexString(longAddress); //$NON-NLS-1$ |
| 135 | } |
| 136 | Object symbol = SymbolAspect.SYMBOL_ASPECT.resolve(segment); |
| 137 | if (symbol != null) { |
| 138 | return symbol.toString(); |
| 139 | } |
| 140 | } else { |
| 141 | return event.getSymbol().toString(); |
| 142 | } |
| 143 | return funcSymbol; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) { |
| 148 | if (fAverageCharWidth == null) { |
| 149 | fAverageCharWidth = gc.getFontMetrics().getAverageCharWidth(); |
| 150 | } |
| 151 | if (bounds.width <= fAverageCharWidth) { |
| 152 | return; |
| 153 | } |
| 154 | if (!(event instanceof FlamegraphEvent)) { |
| 155 | return; |
| 156 | } |
| 157 | String funcSymbol = ""; //$NON-NLS-1$ |
| 158 | ITmfTrace activeTrace = TmfTraceManager.getInstance().getActiveTrace(); |
| 159 | if (activeTrace != null) { |
| 160 | FlamegraphEvent fgEvent = (FlamegraphEvent) event; |
| 161 | funcSymbol = getFunctionSymbol(fgEvent); |
| 162 | } |
| 163 | gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE)); |
| 164 | Utils.drawText(gc, funcSymbol, bounds.x, bounds.y, bounds.width, bounds.height, true, true); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * The flame graph view |
| 169 | * |
| 170 | * @return The flame graph view |
| 171 | */ |
| 172 | public FlameGraphView getView() { |
| 173 | return fView; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * The flame graph view |
| 178 | * |
| 179 | * @param view |
| 180 | * The flame graph view |
| 181 | */ |
| 182 | public void setView(FlameGraphView view) { |
| 183 | fView = view; |
| 184 | } |
| 185 | |
| 186 | } |