Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 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 | ||
14 | package org.eclipse.linuxtools.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.linuxtools.internal.lttng2.kernel.core.Attributes; | |
21 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.StateValues; | |
4bc53929 | 22 | import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Activator; |
be222f56 | 23 | import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages; |
e3366401 | 24 | import org.eclipse.linuxtools.lttng2.kernel.core.analysis.LttngKernelAnalysisModule; |
bcec0116 AM |
25 | import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem; |
26 | import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException; | |
27 | import org.eclipse.linuxtools.statesystem.core.exceptions.StateSystemDisposedException; | |
28 | import org.eclipse.linuxtools.statesystem.core.exceptions.StateValueTypeException; | |
29 | import org.eclipse.linuxtools.statesystem.core.exceptions.TimeRangeException; | |
30 | import org.eclipse.linuxtools.statesystem.core.interval.ITmfStateInterval; | |
31 | import org.eclipse.linuxtools.statesystem.core.statevalue.ITmfStateValue; | |
be222f56 PT |
32 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem; |
33 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider; | |
34 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent; | |
4999a196 | 35 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent; |
713a70ae PT |
36 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils; |
37 | import org.eclipse.swt.SWT; | |
38 | import org.eclipse.swt.graphics.GC; | |
be222f56 | 39 | import org.eclipse.swt.graphics.RGB; |
713a70ae | 40 | import org.eclipse.swt.graphics.Rectangle; |
be222f56 PT |
41 | |
42 | /** | |
43 | * Presentation provider for the control flow view | |
44 | */ | |
45 | public class ControlFlowPresentationProvider extends TimeGraphPresentationProvider { | |
46 | ||
47 | private enum State { | |
4999a196 GB |
48 | UNKNOWN (new RGB(100, 100, 100)), |
49 | WAIT_BLOCKED (new RGB(200, 200, 0)), | |
50 | WAIT_FOR_CPU (new RGB(200, 100, 0)), | |
51 | USERMODE (new RGB(0, 200, 0)), | |
52 | SYSCALL (new RGB(0, 0, 200)), | |
53 | INTERRUPTED (new RGB(200, 0, 100)); | |
be222f56 PT |
54 | |
55 | public final RGB rgb; | |
56 | ||
4999a196 | 57 | private State(RGB rgb) { |
be222f56 PT |
58 | this.rgb = rgb; |
59 | } | |
4999a196 | 60 | |
be222f56 PT |
61 | } |
62 | ||
4999a196 GB |
63 | /** |
64 | * Default constructor | |
65 | */ | |
66 | public ControlFlowPresentationProvider() { | |
67 | super(Messages.ControlFlowView_stateTypeName); | |
68 | } | |
69 | ||
70 | private static State[] getStateValues() { | |
71 | return State.values(); | |
be222f56 PT |
72 | } |
73 | ||
74 | @Override | |
75 | public StateItem[] getStateTable() { | |
4999a196 GB |
76 | State[] states = getStateValues(); |
77 | StateItem[] stateTable = new StateItem[states.length]; | |
be222f56 | 78 | for (int i = 0; i < stateTable.length; i++) { |
4999a196 | 79 | State state = states[i]; |
be222f56 PT |
80 | stateTable[i] = new StateItem(state.rgb, state.toString()); |
81 | } | |
82 | return stateTable; | |
83 | } | |
84 | ||
85 | @Override | |
86 | public int getStateTableIndex(ITimeEvent event) { | |
4999a196 GB |
87 | if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) { |
88 | int status = ((TimeEvent) event).getValue(); | |
f2338178 | 89 | return getMatchingState(status).ordinal(); |
be222f56 | 90 | } |
af10fe06 | 91 | return TRANSPARENT; |
be222f56 PT |
92 | } |
93 | ||
94 | @Override | |
95 | public String getEventName(ITimeEvent event) { | |
4999a196 GB |
96 | if (event instanceof TimeEvent) { |
97 | TimeEvent ev = (TimeEvent) event; | |
98 | if (ev.hasValue()) { | |
99 | return getMatchingState(ev.getValue()).toString(); | |
100 | } | |
be222f56 | 101 | } |
af10fe06 | 102 | return Messages.ControlFlowView_multipleStates; |
be222f56 | 103 | } |
f2338178 MD |
104 | |
105 | private static State getMatchingState(int status) { | |
106 | switch (status) { | |
107 | case StateValues.PROCESS_STATUS_WAIT_BLOCKED: | |
108 | return State.WAIT_BLOCKED; | |
109 | case StateValues.PROCESS_STATUS_WAIT_FOR_CPU: | |
110 | return State.WAIT_FOR_CPU; | |
111 | case StateValues.PROCESS_STATUS_RUN_USERMODE: | |
112 | return State.USERMODE; | |
113 | case StateValues.PROCESS_STATUS_RUN_SYSCALL: | |
114 | return State.SYSCALL; | |
115 | case StateValues.PROCESS_STATUS_INTERRUPTED: | |
116 | return State.INTERRUPTED; | |
117 | default: | |
118 | return State.UNKNOWN; | |
119 | } | |
120 | } | |
be222f56 PT |
121 | |
122 | @Override | |
123 | public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event) { | |
e0838ca1 | 124 | Map<String, String> retMap = new LinkedHashMap<>(); |
1d46dc38 PT |
125 | if (!(event instanceof TimeEvent) || !((TimeEvent) event).hasValue() || |
126 | !(event.getEntry() instanceof ControlFlowEntry)) { | |
127 | return retMap; | |
128 | } | |
129 | ControlFlowEntry entry = (ControlFlowEntry) event.getEntry(); | |
ff3f02c8 | 130 | LttngKernelAnalysisModule module = entry.getTrace().getAnalysisModuleOfClass(LttngKernelAnalysisModule.class, LttngKernelAnalysisModule.ID); |
1cf25311 PT |
131 | if (module == null) { |
132 | return retMap; | |
133 | } | |
4bc53929 GB |
134 | ITmfStateSystem ssq = module.getStateSystem(); |
135 | if (ssq == null) { | |
136 | return retMap; | |
137 | } | |
1d46dc38 PT |
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; | |
be222f56 | 153 | } |
be222f56 | 154 | } |
1d46dc38 PT |
155 | } |
156 | ||
4bc53929 GB |
157 | } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) { |
158 | Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$ | |
1d46dc38 PT |
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()); | |
4999a196 | 170 | } |
1d46dc38 | 171 | |
4bc53929 GB |
172 | } catch (AttributeNotFoundException | TimeRangeException e) { |
173 | Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$ | |
1d46dc38 PT |
174 | } catch (StateSystemDisposedException e) { |
175 | /* Ignored */ | |
be222f56 PT |
176 | } |
177 | } | |
178 | ||
179 | return retMap; | |
180 | } | |
181 | ||
713a70ae PT |
182 | @Override |
183 | public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) { | |
184 | if (bounds.width <= gc.getFontMetrics().getAverageCharWidth()) { | |
185 | return; | |
186 | } | |
4999a196 | 187 | if (!(event instanceof TimeEvent)) { |
713a70ae PT |
188 | return; |
189 | } | |
190 | ControlFlowEntry entry = (ControlFlowEntry) event.getEntry(); | |
ff3f02c8 | 191 | LttngKernelAnalysisModule module = entry.getTrace().getAnalysisModuleOfClass(LttngKernelAnalysisModule.class, LttngKernelAnalysisModule.ID); |
1cf25311 PT |
192 | if (module == null) { |
193 | return; | |
194 | } | |
4bc53929 GB |
195 | ITmfStateSystem ss = module.getStateSystem(); |
196 | if (ss == null) { | |
197 | return; | |
198 | } | |
4999a196 GB |
199 | int status = ((TimeEvent) event).getValue(); |
200 | ||
713a70ae PT |
201 | if (status != StateValues.PROCESS_STATUS_RUN_SYSCALL) { |
202 | return; | |
203 | } | |
204 | try { | |
205 | int syscallQuark = ss.getQuarkRelative(entry.getThreadQuark(), Attributes.SYSTEM_CALL); | |
206 | ITmfStateInterval value = ss.querySingleState(event.getTime(), syscallQuark); | |
207 | if (!value.getStateValue().isNull()) { | |
208 | ITmfStateValue state = value.getStateValue(); | |
209 | gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE)); | |
210 | Utils.drawText(gc, state.toString().substring(4), bounds.x, bounds.y - 2, bounds.width, true, true); | |
211 | } | |
4bc53929 GB |
212 | } catch (AttributeNotFoundException | TimeRangeException e) { |
213 | Activator.getDefault().logError("Error in ControlFlowPresentationProvider", e); //$NON-NLS-1$ | |
713a70ae PT |
214 | } catch (StateSystemDisposedException e) { |
215 | /* Ignored */ | |
216 | } | |
217 | } | |
be222f56 | 218 | } |