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