1 /*******************************************************************************
2 * Copyright (c) 2012, 2016 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 views
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.ui
.views
.resources
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Collections
;
18 import java
.util
.Comparator
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.jdt
.annotation
.Nullable
;
28 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernel
.Attributes
;
29 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernel
.KernelAnalysisModule
;
30 import org
.eclipse
.jface
.action
.MenuManager
;
31 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
32 import org
.eclipse
.swt
.events
.MenuDetectListener
;
33 import org
.eclipse
.swt
.graphics
.Point
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Menu
;
36 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.signals
.TmfCpuSelectedSignal
;
37 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.ui
.views
.resources
.ResourcesEntry
.Type
;
38 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.Messages
;
39 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.actions
.FollowCpuAction
;
40 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.actions
.UnfollowCpuAction
;
41 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
42 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
43 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractStateSystemTimeGraphView
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphViewer
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphControl
;
57 * Main implementation for the LTTng 2.0 kernel Resource view
59 * @author Patrick Tasse
61 public class ResourcesView
extends AbstractStateSystemTimeGraphView
{
64 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
66 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
67 Messages
.ResourcesView_stateTypeName
70 private MenuManager fMenuMgr
= new MenuManager();
72 private int fCurrentCpu
= -1;
74 // Timeout between updates in the build thread in ms
75 private static final long BUILD_UPDATE_TIMEOUT
= 500;
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
84 public ResourcesView() {
85 super(ID
, new ResourcesPresentationProvider());
86 setFilterColumns(FILTER_COLUMN_NAMES
);
87 setFilterLabelProvider(new ResourcesFilterLabelProvider());
88 setEntryComparator(new ResourcesEntryComparator());
89 setAutoExpandLevel(1);
92 private static class ResourcesEntryComparator
implements Comparator
<ITimeGraphEntry
> {
94 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
95 ResourcesEntry entry1
= (ResourcesEntry
) o1
;
96 ResourcesEntry entry2
= (ResourcesEntry
) o2
;
97 if (entry1
.getType() == Type
.NULL
&& entry2
.getType() == Type
.NULL
) {
98 /* sort trace entries alphabetically */
99 return entry1
.getName().compareTo(entry2
.getName());
101 /* sort resource entries by their defined order */
102 return entry1
.compareTo(entry2
);
107 public void createPartControl(Composite parent
) {
108 super.createPartControl(parent
);
112 private void createContextMenu() {
113 fMenuMgr
= new MenuManager();
114 final TimeGraphViewer timeGraphViewer
= getTimeGraphViewer();
115 Menu menu
= fMenuMgr
.createContextMenu(timeGraphViewer
.getTimeGraphControl());
116 timeGraphViewer
.getTimeGraphControl().setMenu(menu
);
117 getTimeGraphViewer().getTimeGraphControl().addMenuDetectListener(new MenuDetectListener() {
119 public void menuDetected(MenuDetectEvent event
) {
120 fMenuMgr
.removeAll();
121 final TimeGraphControl timeGraphControl
= ResourcesView
.this.getTimeGraphViewer().getTimeGraphControl();
122 Point point
= timeGraphControl
.toControl(event
.x
, event
.y
);
123 // this is super important, it makes zoom still work. Do not try
124 // to extend to the time graph area.
125 if (point
.x
< timeGraphViewer
.getNameSpace()) {
126 ITimeGraphEntry item
= timeGraphControl
.getEntry(point
);
128 if (item
instanceof ResourcesEntry
) {
129 ResourcesEntry resourcesEntry
= (ResourcesEntry
) item
;
130 if (resourcesEntry
.getType().equals(ResourcesEntry
.Type
.CPU
)) {
131 if (fCurrentCpu
>= 0) {
132 fMenuMgr
.add(new UnfollowCpuAction(ResourcesView
.this, resourcesEntry
.getId(), resourcesEntry
.getTrace()));
134 fMenuMgr
.add(new FollowCpuAction(ResourcesView
.this, resourcesEntry
.getId(), resourcesEntry
.getTrace()));
143 private static class ResourcesFilterLabelProvider
extends TreeLabelProvider
{
145 public String
getColumnText(Object element
, int columnIndex
) {
146 ResourcesEntry entry
= (ResourcesEntry
) element
;
147 if (columnIndex
== 0) {
148 return entry
.getName();
150 return ""; //$NON-NLS-1$
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
160 protected String
getNextText() {
161 return Messages
.ResourcesView_nextResourceActionNameText
;
165 protected String
getNextTooltip() {
166 return Messages
.ResourcesView_nextResourceActionToolTipText
;
170 protected String
getPrevText() {
171 return Messages
.ResourcesView_previousResourceActionNameText
;
175 protected String
getPrevTooltip() {
176 return Messages
.ResourcesView_previousResourceActionToolTipText
;
180 protected void buildEventList(ITmfTrace trace
, ITmfTrace parentTrace
, final IProgressMonitor monitor
) {
181 final ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysisModule
.ID
);
186 Map
<Integer
, ResourcesEntry
> entryMap
= new HashMap
<>();
187 TimeGraphEntry traceEntry
= null;
189 long startTime
= ssq
.getStartTime();
190 long start
= startTime
;
191 setStartTime(Math
.min(getStartTime(), startTime
));
192 boolean complete
= false;
194 if (monitor
.isCanceled()) {
197 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
198 if (ssq
.isCancelled()) {
201 long end
= ssq
.getCurrentEndTime();
202 if (start
== end
&& !complete
) {
203 // when complete execute one last time regardless of end time
206 long endTime
= end
+ 1;
207 setEndTime(Math
.max(getEndTime(), endTime
));
209 if (traceEntry
== null) {
210 traceEntry
= new ResourcesEntry(trace
, trace
.getName(), startTime
, endTime
, 0);
211 List
<TimeGraphEntry
> entryList
= Collections
.singletonList(traceEntry
);
212 addToEntryList(parentTrace
, ssq
, entryList
);
214 traceEntry
.updateEndTime(endTime
);
216 List
<Integer
> cpuQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*"); //$NON-NLS-1$
217 createCpuEntriesWithQuark(trace
, ssq
, entryMap
, traceEntry
, startTime
, endTime
, cpuQuarks
);
218 if (parentTrace
.equals(getTrace())) {
221 final List
<?
extends ITimeGraphEntry
> traceEntryChildren
= traceEntry
.getChildren();
222 final long resolution
= Math
.max(1, (endTime
- ssq
.getStartTime()) / getDisplayWidth());
223 final long qStart
= start
;
224 final long qEnd
= end
;
225 queryFullStates(ssq
, qStart
, qEnd
, resolution
, monitor
, new IQueryHandler() {
227 public void handle(List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
) {
228 for (ITimeGraphEntry child
: traceEntryChildren
) {
229 if (!populateEventsRecursively(fullStates
, prevFullState
, child
).isOK()) {
235 private IStatus
populateEventsRecursively(@NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, ITimeGraphEntry entry
) {
236 if (monitor
.isCanceled()) {
237 return Status
.CANCEL_STATUS
;
239 if (entry
instanceof TimeGraphEntry
) {
240 TimeGraphEntry timeGraphEntry
= (TimeGraphEntry
) entry
;
241 List
<ITimeEvent
> eventList
= getEventList(timeGraphEntry
, ssq
, fullStates
, prevFullState
, monitor
);
242 if (eventList
!= null) {
243 for (ITimeEvent event
: eventList
) {
244 timeGraphEntry
.addEvent(event
);
248 for (ITimeGraphEntry child
: entry
.getChildren()) {
249 IStatus status
= populateEventsRecursively(fullStates
, prevFullState
, child
);
250 if (!status
.isOK()) {
254 return Status
.OK_STATUS
;
263 private static void createCpuEntriesWithQuark(@NonNull ITmfTrace trace
, final ITmfStateSystem ssq
, Map
<Integer
, ResourcesEntry
> entryMap
, TimeGraphEntry traceEntry
, long startTime
, long endTime
, List
<Integer
> cpuQuarks
) {
264 for (Integer cpuQuark
: cpuQuarks
) {
265 final @NonNull String cpuName
= ssq
.getAttributeName(cpuQuark
);
266 int cpu
= Integer
.parseInt(cpuName
);
267 ResourcesEntry cpuEntry
= entryMap
.get(cpuQuark
);
268 if (cpuEntry
== null) {
269 cpuEntry
= new ResourcesEntry(cpuQuark
, trace
, startTime
, endTime
, Type
.CPU
, cpu
);
270 entryMap
.put(cpuQuark
, cpuEntry
);
271 traceEntry
.addChild(cpuEntry
);
273 cpuEntry
.updateEndTime(endTime
);
275 List
<Integer
> irqQuarks
= ssq
.getQuarks(Attributes
.CPUS
, cpuName
, Attributes
.IRQS
, "*"); //$NON-NLS-1$
276 createCpuInterruptEntryWithQuark(trace
, ssq
, entryMap
, startTime
, endTime
, traceEntry
, cpuEntry
, irqQuarks
, Type
.IRQ
);
277 List
<Integer
> softIrqQuarks
= ssq
.getQuarks(Attributes
.CPUS
, cpuName
, Attributes
.SOFT_IRQS
, "*"); //$NON-NLS-1$
278 createCpuInterruptEntryWithQuark(trace
, ssq
, entryMap
, startTime
, endTime
, traceEntry
, cpuEntry
, softIrqQuarks
, Type
.SOFT_IRQ
);
283 * Create and add execution contexts to a cpu entry. Also creates an
284 * aggregate entry in the root trace entry. The execution context is
285 * basically what the cpu is doing in its execution stack. It can be in an
286 * IRQ, Soft IRQ. MCEs, NMIs, Userland and Kernel execution is not yet
296 * the start time in nanoseconds
298 * the end time in nanoseconds
300 * the trace timegraph entry
302 * the cpu timegraph entry (the entry under the trace entry
303 * @param childrenQuarks
304 * the quarks to add to cpu entry
306 * the type of entry being added
308 private static void createCpuInterruptEntryWithQuark(@NonNull ITmfTrace trace
,
309 final ITmfStateSystem ssq
, Map
<Integer
, ResourcesEntry
> entryMap
,
310 long startTime
, long endTime
,
311 TimeGraphEntry traceEntry
, ResourcesEntry cpuEntry
,
312 List
<Integer
> childrenQuarks
, Type type
) {
313 for (Integer quark
: childrenQuarks
) {
314 final @NonNull String resourceName
= ssq
.getAttributeName(quark
);
315 int resourceId
= Integer
.parseInt(resourceName
);
316 ResourcesEntry interruptEntry
= entryMap
.get(quark
);
317 if (interruptEntry
== null) {
318 interruptEntry
= new ResourcesEntry(quark
, trace
, startTime
, endTime
, type
, resourceId
);
319 entryMap
.put(quark
, interruptEntry
);
320 cpuEntry
.addChild(interruptEntry
);
321 boolean found
= false;
322 for (ITimeGraphEntry rootElem
: traceEntry
.getChildren()) {
323 if (rootElem
instanceof AggregateResourcesEntry
) {
324 AggregateResourcesEntry aggregateInterruptEntry
= (AggregateResourcesEntry
) rootElem
;
325 if (aggregateInterruptEntry
.getId() == resourceId
&& aggregateInterruptEntry
.getType().equals(type
)) {
327 aggregateInterruptEntry
.addContributor(interruptEntry
);
328 final AggregateResourcesEntry irqCpuEntry
= new AggregateResourcesEntry(trace
, cpuEntry
.getName(), startTime
, endTime
, type
, cpuEntry
.getId());
329 irqCpuEntry
.addContributor(interruptEntry
);
330 aggregateInterruptEntry
.addChild(irqCpuEntry
);
336 AggregateResourcesEntry aggregateInterruptEntry
= new AggregateResourcesEntry(trace
, startTime
, endTime
, type
, resourceId
);
337 aggregateInterruptEntry
.addContributor(interruptEntry
);
338 final AggregateResourcesEntry irqCpuEntry
= new AggregateResourcesEntry(trace
, cpuEntry
.getName(), startTime
, endTime
, type
, cpuEntry
.getId());
339 irqCpuEntry
.addContributor(interruptEntry
);
340 aggregateInterruptEntry
.addChild(irqCpuEntry
);
341 traceEntry
.addChild(aggregateInterruptEntry
);
344 interruptEntry
.updateEndTime(endTime
);
350 protected @Nullable List
<ITimeEvent
> getEventList(@NonNull TimeGraphEntry entry
, ITmfStateSystem ssq
,
351 @NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, @NonNull IProgressMonitor monitor
) {
352 ResourcesEntry resourcesEntry
= (ResourcesEntry
) entry
;
353 int quark
= resourcesEntry
.getQuark();
355 if (resourcesEntry
.getType().equals(Type
.CPU
)) {
356 return createCpuEventsList(entry
, ssq
, fullStates
, prevFullState
, monitor
, quark
);
357 } else if ((resourcesEntry
.getType().equals(Type
.IRQ
) || resourcesEntry
.getType().equals(Type
.SOFT_IRQ
)) && (quark
>= 0)) {
358 return createIrqEventsList(entry
, fullStates
, prevFullState
, monitor
, quark
);
364 private static List
<ITimeEvent
> createCpuEventsList(TimeGraphEntry entry
, ITmfStateSystem ssq
, List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
, IProgressMonitor monitor
, int quark
) {
365 List
<ITimeEvent
> eventList
;
368 statusQuark
= ssq
.getQuarkRelative(quark
, Attributes
.STATUS
);
369 } catch (AttributeNotFoundException e
) {
371 * The sub-attribute "status" is not available. May happen if the
372 * trace does not have sched_switch events enabled.
376 eventList
= new ArrayList
<>(fullStates
.size());
377 ITmfStateInterval lastInterval
= prevFullState
== null || statusQuark
>= prevFullState
.size() ?
null : prevFullState
.get(statusQuark
);
378 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
379 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
380 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
381 if (monitor
.isCanceled()) {
384 if (statusQuark
>= fullState
.size()) {
385 /* No information on this CPU (yet?), skip it for now */
388 ITmfStateInterval statusInterval
= fullState
.get(statusQuark
);
389 int status
= statusInterval
.getStateValue().unboxInt();
390 long time
= statusInterval
.getStartTime();
391 long duration
= statusInterval
.getEndTime() - time
+ 1;
392 if (time
== lastStartTime
) {
395 if (!statusInterval
.getStateValue().isNull()) {
396 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
397 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
399 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
401 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
403 lastStartTime
= time
;
404 lastEndTime
= time
+ duration
;
409 private static List
<ITimeEvent
> createIrqEventsList(TimeGraphEntry entry
, List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
, IProgressMonitor monitor
, int quark
) {
410 List
<ITimeEvent
> eventList
;
411 eventList
= new ArrayList
<>(fullStates
.size());
412 ITmfStateInterval lastInterval
= prevFullState
== null || quark
>= prevFullState
.size() ?
null : prevFullState
.get(quark
);
413 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
414 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
415 boolean lastIsNull
= lastInterval
== null ?
false : lastInterval
.getStateValue().isNull();
416 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
417 if (monitor
.isCanceled()) {
420 if (quark
>= fullState
.size()) {
421 /* No information on this IRQ (yet?), skip it for now */
424 ITmfStateInterval irqInterval
= fullState
.get(quark
);
425 long time
= irqInterval
.getStartTime();
426 long duration
= irqInterval
.getEndTime() - time
+ 1;
427 if (time
== lastStartTime
) {
430 if (!irqInterval
.getStateValue().isNull()) {
431 int cpu
= irqInterval
.getStateValue().unboxInt();
432 eventList
.add(new TimeEvent(entry
, time
, duration
, cpu
));
435 if (lastEndTime
!= time
&& lastIsNull
) {
437 * This is a special case where we want to show IRQ_ACTIVE
438 * state but we don't know the CPU (it is between two null
441 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
, -1));
443 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
446 lastStartTime
= time
;
447 lastEndTime
= time
+ duration
;
453 * Signal handler for a cpu selected signal.
456 * the cpu selected signal
460 public void listenToCpu(TmfCpuSelectedSignal signal
) {
461 if (signal
.getCore() >= 0) {
462 fCurrentCpu
= signal
.getCore();