1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made 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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.callstack
;
15 import java
.util
.Optional
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.graphics
.GC
;
19 import org
.eclipse
.swt
.graphics
.RGB
;
20 import org
.eclipse
.swt
.graphics
.Rectangle
;
21 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
22 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
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
.ITimeGraphEntry
;
31 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
34 import com
.google
.common
.cache
.CacheBuilder
;
35 import com
.google
.common
.cache
.CacheLoader
;
36 import com
.google
.common
.cache
.LoadingCache
;
39 * Presentation provider for the Call Stack view, based on the generic TMF
40 * presentation provider.
42 * @author Patrick Tasse
44 public class CallStackPresentationProvider
extends TimeGraphPresentationProvider
{
46 /** Number of colors used for call stack events */
47 public static final int NUM_COLORS
= 360;
49 private CallStackView fView
;
51 private Integer fAverageCharWidth
;
53 private final LoadingCache
<CallStackEvent
, Optional
<String
>> fTimeEventNames
= CacheBuilder
.newBuilder()
55 .build(new CacheLoader
<CallStackEvent
, Optional
<String
>>() {
57 public Optional
<String
> load(CallStackEvent event
) {
58 CallStackEntry entry
= event
.getEntry();
59 ITmfStateSystem ss
= entry
.getStateSystem();
61 ITmfStateValue value
= ss
.querySingleState(event
.getTime(), entry
.getQuark()).getStateValue();
62 if (!value
.isNull()) {
63 String name
= fView
.getFunctionName(entry
.getTrace(), entry
.getProcessId(), event
.getTime(), value
);
64 return Optional
.ofNullable(name
);
66 } catch (TimeRangeException e
) {
67 Activator
.getDefault().logError("Error querying state system", e
); //$NON-NLS-1$
68 } catch (StateSystemDisposedException e
) {
71 return Optional
.empty();
76 MULTIPLE (new RGB(100, 100, 100)),
77 EXEC (new RGB(0, 200, 0));
79 private final RGB rgb
;
81 private State (RGB rgb
) {
91 public CallStackPresentationProvider() {
95 * Sets the call stack view
98 * The call stack view that will contain the time events
101 public void setCallStackView(CallStackView view
) {
106 public String
getStateTypeName(ITimeGraphEntry entry
) {
107 return Messages
.CallStackPresentationProvider_Thread
;
111 public StateItem
[] getStateTable() {
112 final float saturation
= 0.6f
;
113 final float brightness
= 0.6f
;
114 StateItem
[] stateTable
= new StateItem
[NUM_COLORS
+ 1];
115 stateTable
[0] = new StateItem(State
.MULTIPLE
.rgb
, State
.MULTIPLE
.toString());
116 for (int i
= 0; i
< NUM_COLORS
; i
++) {
117 RGB rgb
= new RGB(i
, saturation
, brightness
);
118 stateTable
[i
+ 1] = new StateItem(rgb
, State
.EXEC
.toString());
124 public int getStateTableIndex(ITimeEvent event
) {
125 if (event
instanceof CallStackEvent
) {
126 CallStackEvent callStackEvent
= (CallStackEvent
) event
;
127 return callStackEvent
.getValue() + 1;
128 } else if (event
instanceof NullTimeEvent
) {
131 return State
.MULTIPLE
.ordinal();
135 public String
getEventName(ITimeEvent event
) {
136 if (event
instanceof CallStackEvent
) {
137 return fTimeEventNames
.getUnchecked((CallStackEvent
) event
).orElse(null);
139 return State
.MULTIPLE
.toString();
143 public void postDrawEvent(ITimeEvent event
, Rectangle bounds
, GC gc
) {
144 if (fAverageCharWidth
== null) {
145 fAverageCharWidth
= gc
.getFontMetrics().getAverageCharWidth();
147 if (bounds
.width
<= fAverageCharWidth
) {
150 if (!(event
instanceof CallStackEvent
)) {
153 String name
= fTimeEventNames
.getUnchecked((CallStackEvent
) event
).orElse(null);
154 gc
.setForeground(gc
.getDevice().getSystemColor(SWT
.COLOR_WHITE
));
155 Utils
.drawText(gc
, name
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
, true, true);
159 * Indicate that the provider of function names has changed, so any cached
160 * values must be reset.
162 void resetFunctionNames() {
163 fTimeEventNames
.invalidateAll();