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