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
CommitLineData
dedc7dec 1/*******************************************************************************
4999a196 2 * Copyright (c) 2012, 2013 Ericsson, École Polytechnique de Montréal
dedc7dec
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
dedc7dec
PT
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources;
15
16import java.util.LinkedHashMap;
17import java.util.List;
18import java.util.Map;
19
20import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
21import org.eclipse.linuxtools.internal.lttng2.kernel.core.StateValues;
22import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
23import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
d3ba47d4 24import org.eclipse.linuxtools.lttng2.kernel.core.trace.LttngKernelTrace;
dedc7dec 25import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
96345c5a 26import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
dedc7dec
PT
27import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
28import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
29import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
f1f86dfb 30import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
dedc7dec
PT
31import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
32import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem;
33import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider;
34import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
713a70ae 35import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
1d46dc38 36import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.NullTimeEvent;
4999a196
GB
37import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
38import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.ITmfTimeGraphDrawingHelper;
dedc7dec
PT
39import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils;
40import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
41import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
713a70ae
PT
42import org.eclipse.swt.SWT;
43import org.eclipse.swt.graphics.GC;
dedc7dec 44import org.eclipse.swt.graphics.RGB;
713a70ae 45import org.eclipse.swt.graphics.Rectangle;
dedc7dec
PT
46
47/**
48 * Presentation provider for the Resource view, based on the generic TMF
49 * presentation provider.
50 *
51 * @author Patrick Tasse
52 */
53public class ResourcesPresentationProvider extends TimeGraphPresentationProvider {
54
4999a196 55 private long fLastThreadId = -1;
713a70ae 56
dedc7dec 57 private enum State {
4999a196 58 IDLE (new RGB(200, 200, 200)),
1d46dc38
PT
59 USERMODE (new RGB( 0, 200, 0)),
60 SYSCALL (new RGB( 0, 0, 200)),
4999a196
GB
61 IRQ (new RGB(200, 0, 100)),
62 SOFT_IRQ (new RGB(200, 150, 100)),
63 IRQ_ACTIVE (new RGB(200, 0, 100)),
1d46dc38 64 SOFT_IRQ_RAISED (new RGB(200, 200, 0)),
4999a196 65 SOFT_IRQ_ACTIVE (new RGB(200, 150, 100));
dedc7dec
PT
66
67 public final RGB rgb;
68
4999a196 69 private State(RGB rgb) {
dedc7dec
PT
70 this.rgb = rgb;
71 }
72 }
73
713a70ae
PT
74 /**
75 * Default constructor
713a70ae 76 */
4999a196 77 public ResourcesPresentationProvider() {
713a70ae 78 super();
713a70ae
PT
79 }
80
4999a196
GB
81 private static State[] getStateValues() {
82 return State.values();
dedc7dec
PT
83 }
84
1d46dc38
PT
85 private static State getEventState(TimeEvent event) {
86 if (event.hasValue()) {
4999a196 87 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
1d46dc38 88 int value = event.getValue();
4999a196
GB
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 }
1d46dc38 102 } else if (entry.getType() == Type.IRQ) {
4999a196 103 return State.IRQ_ACTIVE;
1d46dc38 104 } else if (entry.getType() == Type.SOFT_IRQ) {
4999a196
GB
105 if (value == StateValues.SOFT_IRQ_RAISED) {
106 return State.SOFT_IRQ_RAISED;
107 }
108 return State.SOFT_IRQ_ACTIVE;
109 }
dedc7dec 110 }
4999a196 111 return null;
dedc7dec
PT
112 }
113
114 @Override
115 public int getStateTableIndex(ITimeEvent event) {
1d46dc38 116 State state = getEventState((TimeEvent) event);
4999a196
GB
117 if (state != null) {
118 return state.ordinal();
119 }
1d46dc38
PT
120 if (event instanceof NullTimeEvent) {
121 return INVISIBLE;
dedc7dec 122 }
af10fe06 123 return TRANSPARENT;
dedc7dec
PT
124 }
125
4999a196
GB
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
dedc7dec
PT
137 @Override
138 public String getEventName(ITimeEvent event) {
1d46dc38 139 State state = getEventState((TimeEvent) event);
4999a196
GB
140 if (state != null) {
141 return state.toString();
142 }
1d46dc38
PT
143 if (event instanceof NullTimeEvent) {
144 return null;
dedc7dec 145 }
af10fe06 146 return Messages.ResourcesView_multipleStates;
dedc7dec
PT
147 }
148
149 @Override
150 public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event, long hoverTime) {
151
152 Map<String, String> retMap = new LinkedHashMap<String, String>();
4999a196 153 if (event instanceof TimeEvent && ((TimeEvent) event).hasValue()) {
dedc7dec 154
4999a196
GB
155 TimeEvent tcEvent = (TimeEvent) event;
156 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
dedc7dec 157
4999a196
GB
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)) {
dedc7dec 161
4999a196
GB
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 }
dedc7dec 167 }
dedc7dec 168
4999a196
GB
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;
dedc7dec 190 }
dedc7dec 191 }
4999a196
GB
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 */
dedc7dec 200 }
4999a196
GB
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;
dedc7dec 218 }
dedc7dec 219 }
4999a196
GB
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 */
dedc7dec 228 }
4999a196
GB
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);
dedc7dec 238 if (!interval.getStateValue().isNull()) {
4999a196
GB
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);
dedc7dec
PT
244 if (!interval.getStateValue().isNull()) {
245 value = interval.getStateValue();
4999a196
GB
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 }
dedc7dec
PT
255 }
256 }
4999a196
GB
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 */
dedc7dec 265 }
dedc7dec 266 }
96345c5a 267 }
dedc7dec
PT
268 }
269 }
270
271 return retMap;
272 }
273
713a70ae
PT
274 @Override
275 public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) {
4999a196 276 ITmfTimeGraphDrawingHelper drawingHelper = getDrawingHelper();
713a70ae
PT
277 if (bounds.width <= gc.getFontMetrics().getAverageCharWidth()) {
278 return;
279 }
4999a196
GB
280
281 if (!(event instanceof TimeEvent)) {
713a70ae
PT
282 return;
283 }
4999a196
GB
284 TimeEvent tcEvent = (TimeEvent) event;
285 if (!tcEvent.hasValue()) {
713a70ae
PT
286 return;
287 }
4999a196
GB
288
289 ResourcesEntry entry = (ResourcesEntry) event.getEntry();
290 if (!entry.getType().equals(Type.CPU)) {
291 return;
292 }
293
294 int status = tcEvent.getValue();
713a70ae
PT
295 if (status != StateValues.CPU_STATUS_RUN_USERMODE && status != StateValues.CPU_STATUS_RUN_SYSCALL) {
296 return;
297 }
4999a196 298
d3ba47d4 299 ITmfStateSystem ss = entry.getTrace().getStateSystems().get(LttngKernelTrace.STATE_ID);
713a70ae 300 long time = event.getTime();
a3fbd3f4
PT
301 try {
302 while (time < event.getTime() + event.getDuration()) {
713a70ae
PT
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());
4999a196
GB
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;
713a70ae
PT
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());
4999a196
GB
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;
713a70ae
PT
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()) {
4999a196 344 int x = drawingHelper.getXForTime(time);
713a70ae
PT
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 }
713a70ae 350 }
a3fbd3f4
PT
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 */
713a70ae
PT
359 }
360 }
361
362 @Override
363 public void postDrawEntry(ITimeGraphEntry entry, Rectangle bounds, GC gc) {
364 fLastThreadId = -1;
365 }
dedc7dec 366}
This page took 0.068678 seconds and 5 git commands to generate.