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