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