1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson, École Polytechnique de Montréal
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
10 * Patrick Tasse - Initial API and implementation
11 * Geneviève Bastien - Move code to provide base classes for time graph view
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.views
.resources
;
16 import java
.util
.LinkedHashMap
;
17 import java
.util
.List
;
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
;
48 * Presentation provider for the Resource view, based on the generic TMF
49 * presentation provider.
51 * @author Patrick Tasse
53 public class ResourcesPresentationProvider
extends TimeGraphPresentationProvider
{
55 private long fLastThreadId
= -1;
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));
69 private State(RGB rgb
) {
77 public ResourcesPresentationProvider() {
81 private static State
[] getStateValues() {
82 return State
.values();
85 private static State
getEventState(TimeEvent event
) {
86 if (event
.hasValue()) {
87 ResourcesEntry entry
= (ResourcesEntry
) event
.getEntry();
88 int value
= event
.getValue();
90 if (entry
.getType() == Type
.CPU
) {
91 if (value
== StateValues
.CPU_STATUS_IDLE
) {
93 } else if (value
== StateValues
.CPU_STATUS_RUN_USERMODE
) {
94 return State
.USERMODE
;
95 } else if (value
== StateValues
.CPU_STATUS_RUN_SYSCALL
) {
97 } else if (value
== StateValues
.CPU_STATUS_IRQ
) {
99 } else if (value
== StateValues
.CPU_STATUS_SOFTIRQ
) {
100 return State
.SOFT_IRQ
;
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
;
108 return State
.SOFT_IRQ_ACTIVE
;
115 public int getStateTableIndex(ITimeEvent event
) {
116 State state
= getEventState((TimeEvent
) event
);
118 return state
.ordinal();
120 if (event
instanceof NullTimeEvent
) {
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());
138 public String
getEventName(ITimeEvent event
) {
139 State state
= getEventState((TimeEvent
) event
);
141 return state
.toString();
143 if (event
instanceof NullTimeEvent
) {
146 return Messages
.ResourcesView_multipleStates
;
150 public Map
<String
, String
> getEventHoverToolTipInfo(ITimeEvent event
, long hoverTime
) {
152 Map
<String
, String
> retMap
= new LinkedHashMap
<String
, String
>();
153 if (event
instanceof TimeEvent
&& ((TimeEvent
) event
).hasValue()) {
155 TimeEvent tcEvent
= (TimeEvent
) event
;
156 ResourcesEntry entry
= (ResourcesEntry
) event
.getEntry();
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
)) {
162 // Get CPU of IRQ or SoftIRQ and provide it for the tooltip display
163 int cpu
= tcEvent
.getValue();
165 retMap
.put(Messages
.ResourcesView_attributeCpuName
, String
.valueOf(cpu
));
169 // Check for type CPU
170 else if (entry
.getType().equals(Type
.CPU
)) {
171 int status
= tcEvent
.getValue();
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();
179 List
<ITmfStateInterval
> fullState
= ss
.queryFullState(event
.getTime());
180 List
<Integer
> irqQuarks
= ss
.getQuarks(Attributes
.RESOURCES
, Attributes
.IRQS
, "*"); //$NON-NLS-1$
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
));
192 } catch (AttributeNotFoundException e
) {
194 } catch (TimeRangeException e
) {
196 } catch (StateValueTypeException e
) {
198 } catch (StateSystemDisposedException e
) {
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();
207 List
<ITmfStateInterval
> fullState
= ss
.queryFullState(event
.getTime());
208 List
<Integer
> softIrqQuarks
= ss
.getQuarks(Attributes
.RESOURCES
, Attributes
.SOFT_IRQS
, "*"); //$NON-NLS-1$
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
));
220 } catch (AttributeNotFoundException e
) {
222 } catch (TimeRangeException e
) {
224 } catch (StateValueTypeException e
) {
226 } catch (StateSystemDisposedException e
) {
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
);
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());
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());
257 } catch (AttributeNotFoundException e
) {
259 } catch (TimeRangeException e
) {
261 } catch (StateValueTypeException e
) {
263 } catch (StateSystemDisposedException e
) {
275 public void postDrawEvent(ITimeEvent event
, Rectangle bounds
, GC gc
) {
276 ITmfTimeGraphDrawingHelper drawingHelper
= getDrawingHelper();
277 if (bounds
.width
<= gc
.getFontMetrics().getAverageCharWidth()) {
281 if (!(event
instanceof TimeEvent
)) {
284 TimeEvent tcEvent
= (TimeEvent
) event
;
285 if (!tcEvent
.hasValue()) {
289 ResourcesEntry entry
= (ResourcesEntry
) event
.getEntry();
290 if (!entry
.getType().equals(Type
.CPU
)) {
294 int status
= tcEvent
.getValue();
295 if (status
!= StateValues
.CPU_STATUS_RUN_USERMODE
&& status
!= StateValues
.CPU_STATUS_RUN_SYSCALL
) {
299 ITmfStateSystem ss
= entry
.getTrace().getStateSystems().get(LttngKernelTrace
.STATE_ID
);
300 long time
= event
.getTime();
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);
322 fLastThreadId
= currentThreadId
;
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);
342 time
= tidInterval
.getEndTime() + 1;
343 if (time
< event
.getTime() + event
.getDuration()) {
344 int x
= drawingHelper
.getXForTime(time
);
346 gc
.setForeground(gc
.getDevice().getSystemColor(SWT
.COLOR_GRAY
));
347 gc
.drawLine(x
, bounds
.y
+ 1, x
, bounds
.y
+ bounds
.height
- 2);
351 } catch (AttributeNotFoundException e
) {
353 } catch (TimeRangeException e
) {
355 } catch (StateValueTypeException e
) {
357 } catch (StateSystemDisposedException e
) {
363 public void postDrawEntry(ITimeGraphEntry entry
, Rectangle bounds
, GC gc
) {