1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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
.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
.swt
.SWT
;
34 import org
.eclipse
.swt
.graphics
.Color
;
35 import org
.eclipse
.swt
.graphics
.GC
;
36 import org
.eclipse
.swt
.graphics
.RGB
;
37 import org
.eclipse
.swt
.graphics
.Rectangle
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphPresentationProvider
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.ITmfTimeGraphDrawingHelper
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
51 * Presentation provider for the Resource view, based on the generic TMF
52 * presentation provider.
54 * @author Patrick Tasse
56 public class ResourcesPresentationProvider
extends TimeGraphPresentationProvider
{
58 private long fLastThreadId
= -1;
59 private Color fColorWhite
;
60 private Color fColorGray
;
61 private Integer fAverageCharWidth
;
64 IDLE (new RGB(200, 200, 200)),
65 USERMODE (new RGB( 0, 200, 0)),
66 SYSCALL (new RGB( 0, 0, 200)),
67 IRQ (new RGB(200, 0, 100)),
68 SOFT_IRQ (new RGB(200, 150, 100)),
69 IRQ_ACTIVE (new RGB(200, 0, 100)),
70 SOFT_IRQ_RAISED (new RGB(200, 200, 0)),
71 SOFT_IRQ_ACTIVE (new RGB(200, 150, 100));
75 private State(RGB rgb
) {
83 public ResourcesPresentationProvider() {
87 private static State
[] getStateValues() {
88 return State
.values();
91 private static State
getEventState(TimeEvent event
) {
92 if (event
.hasValue()) {
93 ResourcesEntry entry
= (ResourcesEntry
) event
.getEntry();
94 int value
= event
.getValue();
96 if (entry
.getType() == Type
.CPU
) {
97 if (value
== StateValues
.CPU_STATUS_IDLE
) {
99 } else if (value
== StateValues
.CPU_STATUS_RUN_USERMODE
) {
100 return State
.USERMODE
;
101 } else if (value
== StateValues
.CPU_STATUS_RUN_SYSCALL
) {
102 return State
.SYSCALL
;
103 } else if (value
== StateValues
.CPU_STATUS_IRQ
) {
105 } else if (value
== StateValues
.CPU_STATUS_SOFTIRQ
) {
106 return State
.SOFT_IRQ
;
108 } else if (entry
.getType() == Type
.IRQ
) {
109 return State
.IRQ_ACTIVE
;
110 } else if (entry
.getType() == Type
.SOFT_IRQ
) {
111 if (value
== StateValues
.SOFT_IRQ_RAISED
) {
112 return State
.SOFT_IRQ_RAISED
;
114 return State
.SOFT_IRQ_ACTIVE
;
121 public int getStateTableIndex(ITimeEvent event
) {
122 State state
= getEventState((TimeEvent
) event
);
124 return state
.ordinal();
126 if (event
instanceof NullTimeEvent
) {
133 public StateItem
[] getStateTable() {
134 State
[] states
= getStateValues();
135 StateItem
[] stateTable
= new StateItem
[states
.length
];
136 for (int i
= 0; i
< stateTable
.length
; i
++) {
137 State state
= states
[i
];
138 stateTable
[i
] = new StateItem(state
.rgb
, state
.toString());
144 public String
getEventName(ITimeEvent event
) {
145 State state
= getEventState((TimeEvent
) event
);
147 return state
.toString();
149 if (event
instanceof NullTimeEvent
) {
152 return Messages
.ResourcesView_multipleStates
;
156 public Map
<String
, String
> getEventHoverToolTipInfo(ITimeEvent event
, long hoverTime
) {
158 Map
<String
, String
> retMap
= new LinkedHashMap
<>();
159 if (event
instanceof TimeEvent
&& ((TimeEvent
) event
).hasValue()) {
161 TimeEvent tcEvent
= (TimeEvent
) event
;
162 ResourcesEntry entry
= (ResourcesEntry
) event
.getEntry();
164 if (tcEvent
.hasValue()) {
165 ITmfStateSystem ss
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), LttngKernelAnalysisModule
.ID
);
169 // Check for IRQ or Soft_IRQ type
170 if (entry
.getType().equals(Type
.IRQ
) || entry
.getType().equals(Type
.SOFT_IRQ
)) {
172 // Get CPU of IRQ or SoftIRQ and provide it for the tooltip display
173 int cpu
= tcEvent
.getValue();
175 retMap
.put(Messages
.ResourcesView_attributeCpuName
, String
.valueOf(cpu
));
179 // Check for type CPU
180 else if (entry
.getType().equals(Type
.CPU
)) {
181 int status
= tcEvent
.getValue();
183 if (status
== StateValues
.CPU_STATUS_IRQ
) {
184 // In IRQ state get the IRQ that caused the interruption
185 int cpu
= entry
.getId();
188 List
<ITmfStateInterval
> fullState
= ss
.queryFullState(event
.getTime());
189 List
<Integer
> irqQuarks
= ss
.getQuarks(Attributes
.RESOURCES
, Attributes
.IRQS
, "*"); //$NON-NLS-1$
191 for (int irqQuark
: irqQuarks
) {
192 if (fullState
.get(irqQuark
).getStateValue().unboxInt() == cpu
) {
193 ITmfStateInterval value
= ss
.querySingleState(event
.getTime(), irqQuark
);
194 if (!value
.getStateValue().isNull()) {
195 int irq
= Integer
.parseInt(ss
.getAttributeName(irqQuark
));
196 retMap
.put(Messages
.ResourcesView_attributeIrqName
, String
.valueOf(irq
));
201 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
202 Activator
.getDefault().logError("Error in ResourcesPresentationProvider", e
); //$NON-NLS-1$
203 } catch (StateSystemDisposedException e
) {
206 } else if (status
== StateValues
.CPU_STATUS_SOFTIRQ
) {
207 // In SOFT_IRQ state get the SOFT_IRQ that caused the interruption
208 int cpu
= entry
.getId();
211 List
<ITmfStateInterval
> fullState
= ss
.queryFullState(event
.getTime());
212 List
<Integer
> softIrqQuarks
= ss
.getQuarks(Attributes
.RESOURCES
, Attributes
.SOFT_IRQS
, "*"); //$NON-NLS-1$
214 for (int softIrqQuark
: softIrqQuarks
) {
215 if (fullState
.get(softIrqQuark
).getStateValue().unboxInt() == cpu
) {
216 ITmfStateInterval value
= ss
.querySingleState(event
.getTime(), softIrqQuark
);
217 if (!value
.getStateValue().isNull()) {
218 int softIrq
= Integer
.parseInt(ss
.getAttributeName(softIrqQuark
));
219 retMap
.put(Messages
.ResourcesView_attributeSoftIrqName
, String
.valueOf(softIrq
));
224 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
225 Activator
.getDefault().logError("Error in ResourcesPresentationProvider", e
); //$NON-NLS-1$
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
233 retMap
.put(Messages
.ResourcesView_attributeHoverTime
, Utils
.formatTime(hoverTime
, TimeFormat
.CALENDAR
, Resolution
.NANOSEC
));
234 int cpuQuark
= entry
.getQuark();
235 int currentThreadQuark
= ss
.getQuarkRelative(cpuQuark
, Attributes
.CURRENT_THREAD
);
236 ITmfStateInterval interval
= ss
.querySingleState(hoverTime
, currentThreadQuark
);
237 if (!interval
.getStateValue().isNull()) {
238 ITmfStateValue value
= interval
.getStateValue();
239 int currentThreadId
= value
.unboxInt();
240 retMap
.put(Messages
.ResourcesView_attributeTidName
, Integer
.toString(currentThreadId
));
241 int execNameQuark
= ss
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThreadId
), Attributes
.EXEC_NAME
);
242 interval
= ss
.querySingleState(hoverTime
, execNameQuark
);
243 if (!interval
.getStateValue().isNull()) {
244 value
= interval
.getStateValue();
245 retMap
.put(Messages
.ResourcesView_attributeProcessName
, value
.unboxStr());
247 if (status
== StateValues
.CPU_STATUS_RUN_SYSCALL
) {
248 int syscallQuark
= ss
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThreadId
), Attributes
.SYSTEM_CALL
);
249 interval
= ss
.querySingleState(hoverTime
, syscallQuark
);
250 if (!interval
.getStateValue().isNull()) {
251 value
= interval
.getStateValue();
252 retMap
.put(Messages
.ResourcesView_attributeSyscallName
, value
.unboxStr());
256 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
257 Activator
.getDefault().logError("Error in ResourcesPresentationProvider", e
); //$NON-NLS-1$
258 } catch (StateSystemDisposedException e
) {
270 public void postDrawEvent(ITimeEvent event
, Rectangle bounds
, GC gc
) {
271 if (fColorGray
== null) {
272 fColorGray
= gc
.getDevice().getSystemColor(SWT
.COLOR_GRAY
);
274 if (fColorWhite
== null) {
275 fColorWhite
= gc
.getDevice().getSystemColor(SWT
.COLOR_WHITE
);
277 if (fAverageCharWidth
== null) {
278 fAverageCharWidth
= gc
.getFontMetrics().getAverageCharWidth();
281 ITmfTimeGraphDrawingHelper drawingHelper
= getDrawingHelper();
282 if (bounds
.width
<= fAverageCharWidth
) {
286 if (!(event
instanceof TimeEvent
)) {
289 TimeEvent tcEvent
= (TimeEvent
) event
;
290 if (!tcEvent
.hasValue()) {
294 ResourcesEntry entry
= (ResourcesEntry
) event
.getEntry();
295 if (!entry
.getType().equals(Type
.CPU
)) {
299 int status
= tcEvent
.getValue();
300 if (status
!= StateValues
.CPU_STATUS_RUN_USERMODE
&& status
!= StateValues
.CPU_STATUS_RUN_SYSCALL
) {
304 ITmfStateSystem ss
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), LttngKernelAnalysisModule
.ID
);
308 long time
= event
.getTime();
310 while (time
< event
.getTime() + event
.getDuration()) {
311 int cpuQuark
= entry
.getQuark();
312 int currentThreadQuark
= ss
.getQuarkRelative(cpuQuark
, Attributes
.CURRENT_THREAD
);
313 ITmfStateInterval tidInterval
= ss
.querySingleState(time
, currentThreadQuark
);
314 long startTime
= Math
.max(tidInterval
.getStartTime(), event
.getTime());
315 int x
= Math
.max(drawingHelper
.getXForTime(startTime
), bounds
.x
);
316 if (x
>= bounds
.x
+ bounds
.width
) {
319 if (!tidInterval
.getStateValue().isNull()) {
320 ITmfStateValue value
= tidInterval
.getStateValue();
321 int currentThreadId
= value
.unboxInt();
322 long endTime
= Math
.min(tidInterval
.getEndTime() + 1, event
.getTime() + event
.getDuration());
323 int xForEndTime
= drawingHelper
.getXForTime(endTime
);
324 if (xForEndTime
> bounds
.x
) {
325 int width
= Math
.min(xForEndTime
, bounds
.x
+ bounds
.width
) - x
- 1;
327 String attribute
= null;
329 if (status
== StateValues
.CPU_STATUS_RUN_USERMODE
&& currentThreadId
!= fLastThreadId
) {
330 attribute
= Attributes
.EXEC_NAME
;
331 } else if (status
== StateValues
.CPU_STATUS_RUN_SYSCALL
) {
332 attribute
= Attributes
.SYSTEM_CALL
;
333 beginIndex
= 4; // skip the 'sys_'
335 if (attribute
!= null) {
336 int quark
= ss
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThreadId
), attribute
);
337 ITmfStateInterval interval
= ss
.querySingleState(time
, quark
);
338 if (!interval
.getStateValue().isNull()) {
339 value
= interval
.getStateValue();
340 gc
.setForeground(fColorWhite
);
341 int drawn
= Utils
.drawText(gc
, value
.unboxStr().substring(beginIndex
), x
+ 1, bounds
.y
- 2, width
, true, true);
343 fLastThreadId
= currentThreadId
;
347 if (xForEndTime
< bounds
.x
+ bounds
.width
) {
348 gc
.setForeground(fColorGray
);
349 gc
.drawLine(xForEndTime
, bounds
.y
+ 1, xForEndTime
, bounds
.y
+ bounds
.height
- 2);
354 // make sure next time is at least at the next pixel
355 time
= Math
.max(tidInterval
.getEndTime() + 1, drawingHelper
.getTimeAtX(x
+ 1));
357 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
358 Activator
.getDefault().logError("Error in ResourcesPresentationProvider", e
); //$NON-NLS-1$
359 } catch (StateSystemDisposedException e
) {
365 public void postDrawEntry(ITimeGraphEntry entry
, Rectangle bounds
, GC gc
) {