Commit | Line | Data |
---|---|---|
74ccf789 SF |
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.common.core.NonNullUtils; | |
21 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
22 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; | |
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.symbols.ISymbolProvider; | |
26 | import org.eclipse.tracecompass.tmf.ui.symbols.SymbolProviderManager; | |
27 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem; | |
28 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider; | |
29 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; | |
30 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent; | |
31 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils; | |
32 | ||
33 | import com.google.common.collect.ImmutableMap; | |
34 | ||
35 | /** | |
36 | * Presentation provider for the flame graph view, based on the generic TMF | |
37 | * presentation provider. | |
38 | * | |
39 | * @author Sonia Farrah | |
40 | */ | |
41 | public class FlameGraphPresentationProvider extends TimeGraphPresentationProvider { | |
42 | ||
43 | /** Number of colors used for flameGraph events */ | |
44 | public static final int NUM_COLORS = 360; | |
45 | ||
46 | private static final Format FORMATTER = new SubSecondTimeWithUnitFormat(); | |
47 | ||
48 | private FlameGraphView fView; | |
49 | ||
50 | private Integer fAverageCharWidth; | |
51 | ||
52 | private enum State { | |
53 | MULTIPLE(new RGB(100, 100, 100)), EXEC(new RGB(0, 200, 0)); | |
54 | ||
55 | private final RGB rgb; | |
56 | ||
57 | private State(RGB rgb) { | |
58 | this.rgb = rgb; | |
59 | } | |
60 | } | |
61 | ||
62 | /** | |
63 | * Constructor | |
64 | * | |
65 | */ | |
66 | public FlameGraphPresentationProvider() { | |
67 | } | |
68 | ||
69 | @Override | |
70 | public StateItem[] getStateTable() { | |
71 | final float saturation = 0.6f; | |
72 | final float brightness = 0.6f; | |
73 | StateItem[] stateTable = new StateItem[NUM_COLORS + 1]; | |
74 | stateTable[0] = new StateItem(State.MULTIPLE.rgb, State.MULTIPLE.toString()); | |
75 | for (int i = 0; i < NUM_COLORS; i++) { | |
76 | RGB rgb = new RGB(i, saturation, brightness); | |
77 | stateTable[i + 1] = new StateItem(rgb, State.EXEC.toString()); | |
78 | } | |
79 | return stateTable; | |
80 | } | |
81 | ||
82 | @Override | |
83 | public boolean displayTimesInTooltip() { | |
84 | return false; | |
85 | } | |
86 | ||
87 | @Override | |
88 | public String getStateTypeName() { | |
89 | return Messages.FlameGraph_Depth; | |
90 | } | |
91 | ||
92 | @Override | |
93 | public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event, long hoverTime) { | |
94 | return ImmutableMap.of( | |
95 | NonNullUtils.checkNotNull(Messages.FlameGraph_Duration), String.format("%s", FORMATTER.format(event.getDuration())), //$NON-NLS-1$ | |
96 | NonNullUtils.checkNotNull(Messages.FlameGraph_SelfTime), String.format("%s", FORMATTER.format(((FlamegraphEvent) event).getSelfTime())), //$NON-NLS-1$ | |
97 | NonNullUtils.checkNotNull(Messages.FlameGraph_NbCalls), NonNullUtils.checkNotNull(NumberFormat.getIntegerInstance().format(((FlamegraphEvent) event).getNbCalls())) // $NON-NLS-1$ | |
98 | ); | |
99 | } | |
100 | ||
101 | @Override | |
102 | public int getStateTableIndex(ITimeEvent event) { | |
103 | if (event instanceof FlamegraphEvent) { | |
104 | FlamegraphEvent flameGraphEvent = (FlamegraphEvent) event; | |
105 | return flameGraphEvent.getValue() + 1; | |
106 | } else if (event instanceof NullTimeEvent) { | |
107 | return INVISIBLE; | |
108 | } | |
109 | return State.MULTIPLE.ordinal(); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Get the event's symbol. It could be an address or a name. | |
114 | * | |
115 | * @param fGEvent | |
116 | * An event | |
117 | * @param symbolProvider | |
118 | * A symbol provider | |
119 | */ | |
120 | private static String getFuntionSymbol(FlamegraphEvent event, ISymbolProvider symbolProvider) { | |
121 | String funcSymbol = ""; //$NON-NLS-1$ | |
122 | if (event.getSymbol() instanceof TmfStateValue) { | |
123 | ITmfStateValue symbol = (ITmfStateValue) event.getSymbol(); | |
124 | switch (symbol.getType()) { | |
125 | case LONG: | |
126 | Long longAddress = symbol.unboxLong(); | |
127 | funcSymbol = symbolProvider.getSymbolText(longAddress); | |
128 | if (funcSymbol == null) { | |
129 | return "0x" + Long.toHexString(longAddress); //$NON-NLS-1$ | |
130 | } | |
131 | return funcSymbol; | |
132 | case STRING: | |
133 | return symbol.unboxStr(); | |
134 | case INTEGER: | |
135 | Integer intAddress = symbol.unboxInt(); | |
136 | funcSymbol = symbolProvider.getSymbolText(intAddress); | |
137 | if (funcSymbol == null) { | |
138 | return "0x" + Integer.toHexString(intAddress); //$NON-NLS-1$ | |
139 | } | |
140 | return funcSymbol; | |
141 | case CUSTOM: | |
142 | case DOUBLE: | |
143 | case NULL: | |
144 | default: | |
145 | break; | |
146 | } | |
147 | } | |
148 | return funcSymbol; | |
149 | } | |
150 | ||
151 | @Override | |
152 | public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) { | |
153 | if (fAverageCharWidth == null) { | |
154 | fAverageCharWidth = gc.getFontMetrics().getAverageCharWidth(); | |
155 | } | |
156 | if (bounds.width <= fAverageCharWidth) { | |
157 | return; | |
158 | } | |
159 | if (!(event instanceof FlamegraphEvent)) { | |
160 | return; | |
161 | } | |
162 | String funcSymbol = ""; //$NON-NLS-1$ | |
163 | ITmfTrace activeTrace = TmfTraceManager.getInstance().getActiveTrace(); | |
164 | if (activeTrace != null) { | |
165 | FlamegraphEvent fgEvent = (FlamegraphEvent) event; | |
166 | ISymbolProvider symbolProvider = SymbolProviderManager.getInstance().getSymbolProvider(activeTrace); | |
167 | funcSymbol = getFuntionSymbol(fgEvent, symbolProvider); | |
168 | } | |
169 | gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE)); | |
170 | Utils.drawText(gc, funcSymbol, bounds.x, bounds.y, bounds.width, bounds.height, true, true); | |
171 | } | |
172 | ||
173 | /** | |
174 | * The flame graph view | |
175 | * | |
176 | * @return The flame graph view | |
177 | */ | |
178 | public FlameGraphView getView() { | |
179 | return fView; | |
180 | } | |
181 | ||
182 | /** | |
183 | * The flame graph view | |
184 | * | |
185 | * @param view | |
186 | * The flame graph view | |
187 | */ | |
188 | public void setView(FlameGraphView view) { | |
189 | fView = view; | |
190 | } | |
191 | ||
192 | } |