tmf: Cache CallStackEvent names
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / callstack / CallStackPresentationProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.views.callstack;
14
15 import java.util.Optional;
16
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;
33
34 import com.google.common.cache.CacheBuilder;
35 import com.google.common.cache.CacheLoader;
36 import com.google.common.cache.LoadingCache;
37
38 /**
39 * Presentation provider for the Call Stack view, based on the generic TMF
40 * presentation provider.
41 *
42 * @author Patrick Tasse
43 */
44 public class CallStackPresentationProvider extends TimeGraphPresentationProvider {
45
46 /** Number of colors used for call stack events */
47 public static final int NUM_COLORS = 360;
48
49 private CallStackView fView;
50
51 private Integer fAverageCharWidth;
52
53 private final LoadingCache<CallStackEvent, Optional<String>> fTimeEventNames = CacheBuilder.newBuilder()
54 .maximumSize(1000)
55 .build(new CacheLoader<CallStackEvent, Optional<String>>() {
56 @Override
57 public Optional<String> load(CallStackEvent event) {
58 CallStackEntry entry = event.getEntry();
59 ITmfStateSystem ss = entry.getStateSystem();
60 try {
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);
65 }
66 } catch (TimeRangeException e) {
67 Activator.getDefault().logError("Error querying state system", e); //$NON-NLS-1$
68 } catch (StateSystemDisposedException e) {
69 /* Ignored */
70 }
71 return Optional.empty();
72 }
73 });
74
75 private enum State {
76 MULTIPLE (new RGB(100, 100, 100)),
77 EXEC (new RGB(0, 200, 0));
78
79 private final RGB rgb;
80
81 private State (RGB rgb) {
82 this.rgb = rgb;
83 }
84 }
85
86 /**
87 * Constructor
88 *
89 * @since 1.2
90 */
91 public CallStackPresentationProvider() {
92 }
93
94 /**
95 * Sets the call stack view
96 *
97 * @param view
98 * The call stack view that will contain the time events
99 * @since 1.2
100 */
101 public void setCallStackView(CallStackView view) {
102 fView = view;
103 }
104
105 @Override
106 public String getStateTypeName(ITimeGraphEntry entry) {
107 return Messages.CallStackPresentationProvider_Thread;
108 }
109
110 @Override
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());
119 }
120 return stateTable;
121 }
122
123 @Override
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) {
129 return INVISIBLE;
130 }
131 return State.MULTIPLE.ordinal();
132 }
133
134 @Override
135 public String getEventName(ITimeEvent event) {
136 if (event instanceof CallStackEvent) {
137 return fTimeEventNames.getUnchecked((CallStackEvent) event).orElse(null);
138 }
139 return State.MULTIPLE.toString();
140 }
141
142 @Override
143 public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) {
144 if (fAverageCharWidth == null) {
145 fAverageCharWidth = gc.getFontMetrics().getAverageCharWidth();
146 }
147 if (bounds.width <= fAverageCharWidth) {
148 return;
149 }
150 if (!(event instanceof CallStackEvent)) {
151 return;
152 }
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);
156 }
157
158 /**
159 * Indicate that the provider of function names has changed, so any cached
160 * values must be reset.
161 */
162 void resetFunctionNames() {
163 fTimeEventNames.invalidateAll();
164 }
165
166 }
This page took 0.037406 seconds and 5 git commands to generate.