Support for upgrading Linux Tools features to Trace Compass
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.ui / src / org / eclipse / tracecompass / internal / lttng2 / kernel / ui / views / controlflow / ControlFlowPresentationProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Move code to provide base classes for time graph view
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.lttng2.kernel.ui.views.controlflow;
15
16 import java.util.LinkedHashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.GC;
22 import org.eclipse.swt.graphics.RGB;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.tracecompass.internal.lttng2.kernel.core.Attributes;
25 import org.eclipse.tracecompass.internal.lttng2.kernel.core.StateValues;
26 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.IKernelAnalysisEventLayout;
27 import org.eclipse.tracecompass.internal.lttng2.kernel.ui.Activator;
28 import org.eclipse.tracecompass.internal.lttng2.kernel.ui.Messages;
29 import org.eclipse.tracecompass.lttng2.kernel.core.analysis.kernel.LttngKernelAnalysis;
30 import org.eclipse.tracecompass.lttng2.kernel.core.trace.LttngKernelTrace;
31 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
33 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
34 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
35 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
36 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
37 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
38 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
39 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
40 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem;
41 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider;
42 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
43 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
44 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils;
45
46 /**
47 * Presentation provider for the control flow view
48 */
49 public class ControlFlowPresentationProvider extends TimeGraphPresentationProvider {
50
51 private enum State {
52 UNKNOWN (new RGB(100, 100, 100)),
53 WAIT_BLOCKED (new RGB(200, 200, 0)),
54 WAIT_FOR_CPU (new RGB(200, 100, 0)),
55 USERMODE (new RGB(0, 200, 0)),
56 SYSCALL (new RGB(0, 0, 200)),
57 INTERRUPTED (new RGB(200, 0, 100));
58
59 public final RGB rgb;
60
61 private State(RGB rgb) {
62 this.rgb = rgb;
63 }
64
65 }
66
67 /**
68 * Default constructor
69 */
70 public ControlFlowPresentationProvider() {
71 super(Messages.ControlFlowView_stateTypeName);
72 }
73
74 private static State[] getStateValues() {
75 return State.values();
76 }
77
78 @Override
79 public StateItem[] getStateTable() {
80 State[] states = getStateValues();
81 StateItem[] stateTable = new StateItem[states.length];
82 for (int i = 0; i < stateTable.length; i++) {
83 State state = states[i];
84 stateTable[i] = new StateItem(state.rgb, state.toString());
85 }
86 return stateTable;
87 }
88
89 @Override
90 public int getStateTableIndex(ITimeEvent event) {
91 if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) {
92 int status = ((TimeEvent) event).getValue();
93 return getMatchingState(status).ordinal();
94 }
95 return TRANSPARENT;
96 }
97
98 @Override
99 public String getEventName(ITimeEvent event) {
100 if (event instanceof TimeEvent) {
101 TimeEvent ev = (TimeEvent) event;
102 if (ev.hasValue()) {
103 return getMatchingState(ev.getValue()).toString();
104 }
105 }
106 return Messages.ControlFlowView_multipleStates;
107 }
108
109 private static State getMatchingState(int status) {
110 switch (status) {
111 case StateValues.PROCESS_STATUS_WAIT_BLOCKED:
112 return State.WAIT_BLOCKED;
113 case StateValues.PROCESS_STATUS_WAIT_FOR_CPU:
114 return State.WAIT_FOR_CPU;
115 case StateValues.PROCESS_STATUS_RUN_USERMODE:
116 return State.USERMODE;
117 case StateValues.PROCESS_STATUS_RUN_SYSCALL:
118 return State.SYSCALL;
119 case StateValues.PROCESS_STATUS_INTERRUPTED:
120 return State.INTERRUPTED;
121 default:
122 return State.UNKNOWN;
123 }
124 }
125
126 @Override
127 public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event) {
128 Map<String, String> retMap = new LinkedHashMap<>();
129 if (!(event instanceof TimeEvent) || !((TimeEvent) event).hasValue() ||
130 !(event.getEntry() instanceof ControlFlowEntry)) {
131 return retMap;
132 }
133 ControlFlowEntry entry = (ControlFlowEntry) event.getEntry();
134 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), LttngKernelAnalysis.ID);
135 if (ssq == null) {
136 return retMap;
137 }
138 int tid = entry.getThreadId();
139
140 try {
141 // Find every CPU first, then get the current thread
142 int cpusQuark = ssq.getQuarkAbsolute(Attributes.CPUS);
143 List<Integer> cpuQuarks = ssq.getSubAttributes(cpusQuark, false);
144 for (Integer cpuQuark : cpuQuarks) {
145 int currentThreadQuark = ssq.getQuarkRelative(cpuQuark, Attributes.CURRENT_THREAD);
146 ITmfStateInterval interval = ssq.querySingleState(event.getTime(), currentThreadQuark);
147 if (!interval.getStateValue().isNull()) {
148 ITmfStateValue state = interval.getStateValue();
149 int currentThreadId = state.unboxInt();
150 if (tid == currentThreadId) {
151 retMap.put(Messages.ControlFlowView_attributeCpuName, ssq.getAttributeName(cpuQuark));
152 break;
153 }
154 }
155 }
156
157 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
158 Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$
159 } catch (StateSystemDisposedException e) {
160 /* Ignored */
161 }
162 int status = ((TimeEvent) event).getValue();
163 if (status == StateValues.PROCESS_STATUS_RUN_SYSCALL) {
164 try {
165 int syscallQuark = ssq.getQuarkRelative(entry.getThreadQuark(), Attributes.SYSTEM_CALL);
166 ITmfStateInterval value = ssq.querySingleState(event.getTime(), syscallQuark);
167 if (!value.getStateValue().isNull()) {
168 ITmfStateValue state = value.getStateValue();
169 retMap.put(Messages.ControlFlowView_attributeSyscallName, state.toString());
170 }
171
172 } catch (AttributeNotFoundException | TimeRangeException e) {
173 Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$
174 } catch (StateSystemDisposedException e) {
175 /* Ignored */
176 }
177 }
178
179 return retMap;
180 }
181
182 @Override
183 public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) {
184 if (bounds.width <= gc.getFontMetrics().getAverageCharWidth()) {
185 return;
186 }
187 if (!(event instanceof TimeEvent)) {
188 return;
189 }
190 ControlFlowEntry entry = (ControlFlowEntry) event.getEntry();
191 ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), LttngKernelAnalysis.ID);
192 if (ss == null) {
193 return;
194 }
195 int status = ((TimeEvent) event).getValue();
196
197 if (status != StateValues.PROCESS_STATUS_RUN_SYSCALL) {
198 return;
199 }
200 try {
201 int syscallQuark = ss.getQuarkRelative(entry.getThreadQuark(), Attributes.SYSTEM_CALL);
202 ITmfStateInterval value = ss.querySingleState(event.getTime(), syscallQuark);
203 if (!value.getStateValue().isNull()) {
204 ITmfStateValue state = value.getStateValue();
205 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
206
207 /*
208 * Remove the "sys_" or "syscall_entry_" or similar from what we
209 * draw in the rectangle. This depends on the trace's event layout.
210 */
211 int beginIndex = 0;
212 ITmfTrace trace = entry.getTrace();
213 if (trace instanceof LttngKernelTrace) {
214 IKernelAnalysisEventLayout layout = ((LttngKernelTrace) trace).getEventLayout();
215 beginIndex = layout.eventSyscallEntryPrefix().length();
216 }
217
218 Utils.drawText(gc, state.toString().substring(beginIndex), bounds.x, bounds.y - 2, bounds.width, true, true);
219 }
220 } catch (AttributeNotFoundException | TimeRangeException e) {
221 Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$
222 } catch (StateSystemDisposedException e) {
223 /* Ignored */
224 }
225 }
226 }
This page took 0.035833 seconds and 5 git commands to generate.