tmf: Provide a static method to retrieve state systems
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.ui / src / org / eclipse / linuxtools / internal / lttng2 / kernel / ui / views / resources / ResourcesPresentationProvider.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.linuxtools.internal.lttng2.kernel.ui.views.resources;
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;
22 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Activator;
23 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
24 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
25 import org.eclipse.linuxtools.lttng2.kernel.core.analysis.LttngKernelAnalysisModule;
26 import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
27 import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException;
28 import org.eclipse.linuxtools.statesystem.core.exceptions.StateSystemDisposedException;
29 import org.eclipse.linuxtools.statesystem.core.exceptions.StateValueTypeException;
30 import org.eclipse.linuxtools.statesystem.core.exceptions.TimeRangeException;
31 import org.eclipse.linuxtools.statesystem.core.interval.ITmfStateInterval;
32 import org.eclipse.linuxtools.statesystem.core.statevalue.ITmfStateValue;
33 import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
34 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem;
35 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider;
36 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
37 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
38 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.NullTimeEvent;
39 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
40 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.ITmfTimeGraphDrawingHelper;
41 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils;
42 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
43 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.graphics.GC;
46 import org.eclipse.swt.graphics.RGB;
47 import org.eclipse.swt.graphics.Rectangle;
48
49 /**
50 * Presentation provider for the Resource view, based on the generic TMF
51 * presentation provider.
52 *
53 * @author Patrick Tasse
54 */
55 public class ResourcesPresentationProvider extends TimeGraphPresentationProvider {
56
57 private long fLastThreadId = -1;
58
59 private enum State {
60 IDLE (new RGB(200, 200, 200)),
61 USERMODE (new RGB( 0, 200, 0)),
62 SYSCALL (new RGB( 0, 0, 200)),
63 IRQ (new RGB(200, 0, 100)),
64 SOFT_IRQ (new RGB(200, 150, 100)),
65 IRQ_ACTIVE (new RGB(200, 0, 100)),
66 SOFT_IRQ_RAISED (new RGB(200, 200, 0)),
67 SOFT_IRQ_ACTIVE (new RGB(200, 150, 100));
68
69 public final RGB rgb;
70
71 private State(RGB rgb) {
72 this.rgb = rgb;
73 }
74 }
75
76 /**
77 * Default constructor
78 */
79 public ResourcesPresentationProvider() {
80 super();
81 }
82
83 private static State[] getStateValues() {
84 return State.values();
85 }
86
87 private static State getEventState(TimeEvent event) {
88 if (event.hasValue()) {
89 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
90 int value = event.getValue();
91
92 if (entry.getType() == Type.CPU) {
93 if (value == StateValues.CPU_STATUS_IDLE) {
94 return State.IDLE;
95 } else if (value == StateValues.CPU_STATUS_RUN_USERMODE) {
96 return State.USERMODE;
97 } else if (value == StateValues.CPU_STATUS_RUN_SYSCALL) {
98 return State.SYSCALL;
99 } else if (value == StateValues.CPU_STATUS_IRQ) {
100 return State.IRQ;
101 } else if (value == StateValues.CPU_STATUS_SOFTIRQ) {
102 return State.SOFT_IRQ;
103 }
104 } else if (entry.getType() == Type.IRQ) {
105 return State.IRQ_ACTIVE;
106 } else if (entry.getType() == Type.SOFT_IRQ) {
107 if (value == StateValues.SOFT_IRQ_RAISED) {
108 return State.SOFT_IRQ_RAISED;
109 }
110 return State.SOFT_IRQ_ACTIVE;
111 }
112 }
113 return null;
114 }
115
116 @Override
117 public int getStateTableIndex(ITimeEvent event) {
118 State state = getEventState((TimeEvent) event);
119 if (state != null) {
120 return state.ordinal();
121 }
122 if (event instanceof NullTimeEvent) {
123 return INVISIBLE;
124 }
125 return TRANSPARENT;
126 }
127
128 @Override
129 public StateItem[] getStateTable() {
130 State[] states = getStateValues();
131 StateItem[] stateTable = new StateItem[states.length];
132 for (int i = 0; i < stateTable.length; i++) {
133 State state = states[i];
134 stateTable[i] = new StateItem(state.rgb, state.toString());
135 }
136 return stateTable;
137 }
138
139 @Override
140 public String getEventName(ITimeEvent event) {
141 State state = getEventState((TimeEvent) event);
142 if (state != null) {
143 return state.toString();
144 }
145 if (event instanceof NullTimeEvent) {
146 return null;
147 }
148 return Messages.ResourcesView_multipleStates;
149 }
150
151 @Override
152 public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event, long hoverTime) {
153
154 Map<String, String> retMap = new LinkedHashMap<>();
155 if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) {
156
157 TimeEvent tcEvent = (TimeEvent) event;
158 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
159
160 if (tcEvent.hasValue()) {
161 ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), LttngKernelAnalysisModule.ID);
162 if (ss == null) {
163 return retMap;
164 }
165 // Check for IRQ or Soft_IRQ type
166 if (entry.getType().equals(Type.IRQ) || entry.getType().equals(Type.SOFT_IRQ)) {
167
168 // Get CPU of IRQ or SoftIRQ and provide it for the tooltip display
169 int cpu = tcEvent.getValue();
170 if (cpu >= 0) {
171 retMap.put(Messages.ResourcesView_attributeCpuName, String.valueOf(cpu));
172 }
173 }
174
175 // Check for type CPU
176 else if (entry.getType().equals(Type.CPU)) {
177 int status = tcEvent.getValue();
178
179 if (status == StateValues.CPU_STATUS_IRQ) {
180 // In IRQ state get the IRQ that caused the interruption
181 int cpu = entry.getId();
182
183 try {
184 List<ITmfStateInterval> fullState = ss.queryFullState(event.getTime());
185 List<Integer> irqQuarks = ss.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$
186
187 for (int irqQuark : irqQuarks) {
188 if (fullState.get(irqQuark).getStateValue().unboxInt() == cpu) {
189 ITmfStateInterval value = ss.querySingleState(event.getTime(), irqQuark);
190 if (!value.getStateValue().isNull()) {
191 int irq = Integer.parseInt(ss.getAttributeName(irqQuark));
192 retMap.put(Messages.ResourcesView_attributeIrqName, String.valueOf(irq));
193 }
194 break;
195 }
196 }
197 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
198 Activator.getDefault().logError("Error in ResourcesPresentationProvider", e); //$NON-NLS-1$
199 } catch (StateSystemDisposedException e) {
200 /* Ignored */
201 }
202 } else if (status == StateValues.CPU_STATUS_SOFTIRQ) {
203 // In SOFT_IRQ state get the SOFT_IRQ that caused the interruption
204 int cpu = entry.getId();
205
206 try {
207 List<ITmfStateInterval> fullState = ss.queryFullState(event.getTime());
208 List<Integer> softIrqQuarks = ss.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
209
210 for (int softIrqQuark : softIrqQuarks) {
211 if (fullState.get(softIrqQuark).getStateValue().unboxInt() == cpu) {
212 ITmfStateInterval value = ss.querySingleState(event.getTime(), softIrqQuark);
213 if (!value.getStateValue().isNull()) {
214 int softIrq = Integer.parseInt(ss.getAttributeName(softIrqQuark));
215 retMap.put(Messages.ResourcesView_attributeSoftIrqName, String.valueOf(softIrq));
216 }
217 break;
218 }
219 }
220 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
221 Activator.getDefault().logError("Error in ResourcesPresentationProvider", e); //$NON-NLS-1$
222 } catch (StateSystemDisposedException e) {
223 /* Ignored */
224 }
225 } else if (status == StateValues.CPU_STATUS_RUN_USERMODE || status == StateValues.CPU_STATUS_RUN_SYSCALL) {
226 // In running state get the current tid
227
228 try {
229 retMap.put(Messages.ResourcesView_attributeHoverTime, Utils.formatTime(hoverTime, TimeFormat.CALENDAR, Resolution.NANOSEC));
230 int cpuQuark = entry.getQuark();
231 int currentThreadQuark = ss.getQuarkRelative(cpuQuark, Attributes.CURRENT_THREAD);
232 ITmfStateInterval interval = ss.querySingleState(hoverTime, currentThreadQuark);
233 if (!interval.getStateValue().isNull()) {
234 ITmfStateValue value = interval.getStateValue();
235 int currentThreadId = value.unboxInt();
236 retMap.put(Messages.ResourcesView_attributeTidName, Integer.toString(currentThreadId));
237 int execNameQuark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThreadId), Attributes.EXEC_NAME);
238 interval = ss.querySingleState(hoverTime, execNameQuark);
239 if (!interval.getStateValue().isNull()) {
240 value = interval.getStateValue();
241 retMap.put(Messages.ResourcesView_attributeProcessName, value.unboxStr());
242 }
243 if (status == StateValues.CPU_STATUS_RUN_SYSCALL) {
244 int syscallQuark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThreadId), Attributes.SYSTEM_CALL);
245 interval = ss.querySingleState(hoverTime, syscallQuark);
246 if (!interval.getStateValue().isNull()) {
247 value = interval.getStateValue();
248 retMap.put(Messages.ResourcesView_attributeSyscallName, value.unboxStr());
249 }
250 }
251 }
252 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
253 Activator.getDefault().logError("Error in ResourcesPresentationProvider", e); //$NON-NLS-1$
254 } catch (StateSystemDisposedException e) {
255 /* Ignored */
256 }
257 }
258 }
259 }
260 }
261
262 return retMap;
263 }
264
265 @Override
266 public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) {
267 ITmfTimeGraphDrawingHelper drawingHelper = getDrawingHelper();
268 if (bounds.width <= gc.getFontMetrics().getAverageCharWidth()) {
269 return;
270 }
271
272 if (!(event instanceof TimeEvent)) {
273 return;
274 }
275 TimeEvent tcEvent = (TimeEvent) event;
276 if (!tcEvent.hasValue()) {
277 return;
278 }
279
280 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
281 if (!entry.getType().equals(Type.CPU)) {
282 return;
283 }
284
285 int status = tcEvent.getValue();
286 if (status != StateValues.CPU_STATUS_RUN_USERMODE && status != StateValues.CPU_STATUS_RUN_SYSCALL) {
287 return;
288 }
289
290 ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), LttngKernelAnalysisModule.ID);
291 if (ss == null) {
292 return;
293 }
294 long time = event.getTime();
295 try {
296 while (time < event.getTime() + event.getDuration()) {
297 int cpuQuark = entry.getQuark();
298 int currentThreadQuark = ss.getQuarkRelative(cpuQuark, Attributes.CURRENT_THREAD);
299 ITmfStateInterval tidInterval = ss.querySingleState(time, currentThreadQuark);
300 if (!tidInterval.getStateValue().isNull()) {
301 ITmfStateValue value = tidInterval.getStateValue();
302 int currentThreadId = value.unboxInt();
303 if (status == StateValues.CPU_STATUS_RUN_USERMODE && currentThreadId != fLastThreadId) {
304 int execNameQuark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThreadId), Attributes.EXEC_NAME);
305 ITmfStateInterval interval = ss.querySingleState(time, execNameQuark);
306 if (!interval.getStateValue().isNull()) {
307 value = interval.getStateValue();
308 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
309 long startTime = Math.max(tidInterval.getStartTime(), event.getTime());
310 long endTime = Math.min(tidInterval.getEndTime() + 1, event.getTime() + event.getDuration());
311 if (drawingHelper.getXForTime(endTime) > bounds.x) {
312 int x = Math.max(drawingHelper.getXForTime(startTime), bounds.x);
313 int width = Math.min(drawingHelper.getXForTime(endTime), bounds.x + bounds.width) - x;
314 int drawn = Utils.drawText(gc, value.unboxStr(), x + 1, bounds.y - 2, width - 1, true, true);
315 if (drawn > 0) {
316 fLastThreadId = currentThreadId;
317 }
318 }
319 }
320 } else if (status == StateValues.CPU_STATUS_RUN_SYSCALL) {
321 int syscallQuark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThreadId), Attributes.SYSTEM_CALL);
322 ITmfStateInterval interval = ss.querySingleState(time, syscallQuark);
323 if (!interval.getStateValue().isNull()) {
324 value = interval.getStateValue();
325 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
326 long startTime = Math.max(tidInterval.getStartTime(), event.getTime());
327 long endTime = Math.min(tidInterval.getEndTime() + 1, event.getTime() + event.getDuration());
328 if (drawingHelper.getXForTime(endTime) > bounds.x) {
329 int x = Math.max(drawingHelper.getXForTime(startTime), bounds.x);
330 int width = Math.min(drawingHelper.getXForTime(endTime), bounds.x + bounds.width) - x;
331 Utils.drawText(gc, value.unboxStr().substring(4), x + 1, bounds.y - 2, width - 1, true, true);
332 }
333 }
334 }
335 }
336 time = tidInterval.getEndTime() + 1;
337 if (time < event.getTime() + event.getDuration()) {
338 int x = drawingHelper.getXForTime(time);
339 if (x >= bounds.x) {
340 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_GRAY));
341 gc.drawLine(x, bounds.y + 1, x, bounds.y + bounds.height - 2);
342 }
343 }
344 }
345 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
346 Activator.getDefault().logError("Error in ResourcesPresentationProvider", e); //$NON-NLS-1$
347 } catch (StateSystemDisposedException e) {
348 /* Ignored */
349 }
350 }
351
352 @Override
353 public void postDrawEntry(ITimeGraphEntry entry, Rectangle bounds, GC gc) {
354 fLastThreadId = -1;
355 }
356 }
This page took 0.041759 seconds and 5 git commands to generate.