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
.internal
.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
.jface
.action
.IMenuManager
;
29 import org
.eclipse
.jface
.viewers
.ISelection
;
30 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
31 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernel
.KernelAnalysisModule
;
32 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.signals
.TmfCpuSelectedSignal
;
33 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.core
.kernel
.Attributes
;
34 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.Messages
;
35 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.actions
.FollowCpuAction
;
36 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.actions
.UnfollowCpuAction
;
37 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.views
.resources
.ResourcesEntry
.Type
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
40 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceContext
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractStateSystemTimeGraphView
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
54 * Main implementation for the LTTng 2.0 kernel Resource view
56 * @author Patrick Tasse
58 public class ResourcesView
extends AbstractStateSystemTimeGraphView
{
61 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
63 /** ID of the followed CPU in the map data in {@link TmfTraceContext} */
64 public static final @NonNull String RESOURCES_FOLLOW_CPU
= ID
+ ".FOLLOW_CPU"; //$NON-NLS-1$
66 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
67 Messages
.ResourcesView_stateTypeName
70 // Timeout between updates in the build thread in ms
71 private static final long BUILD_UPDATE_TIMEOUT
= 500;
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
80 public ResourcesView() {
81 super(ID
, new ResourcesPresentationProvider());
82 setFilterColumns(FILTER_COLUMN_NAMES
);
83 setFilterLabelProvider(new ResourcesFilterLabelProvider());
84 setEntryComparator(new ResourcesEntryComparator());
85 setAutoExpandLevel(1);
88 private static class ResourcesEntryComparator
implements Comparator
<ITimeGraphEntry
> {
90 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
91 ResourcesEntry entry1
= (ResourcesEntry
) o1
;
92 ResourcesEntry entry2
= (ResourcesEntry
) o2
;
93 if (entry1
.getType() == Type
.NULL
&& entry2
.getType() == Type
.NULL
) {
94 /* sort trace entries alphabetically */
95 return entry1
.getName().compareTo(entry2
.getName());
97 /* sort resource entries by their defined order */
98 return entry1
.compareTo(entry2
);
107 protected void fillTimeGraphEntryContextMenu(@NonNull IMenuManager menuManager
) {
108 ISelection selection
= getSite().getSelectionProvider().getSelection();
109 if (selection
instanceof IStructuredSelection
) {
110 IStructuredSelection sSel
= (IStructuredSelection
) selection
;
111 if (sSel
.getFirstElement() instanceof ResourcesEntry
) {
112 ResourcesEntry resourcesEntry
= (ResourcesEntry
) sSel
.getFirstElement();
113 if (resourcesEntry
.getType().equals(ResourcesEntry
.Type
.CPU
)) {
114 TmfTraceContext ctx
= TmfTraceManager
.getInstance().getCurrentTraceContext();
115 Integer data
= (Integer
) ctx
.getData(RESOURCES_FOLLOW_CPU
);
116 int cpu
= data
!= null ? data
.intValue() : -1;
118 menuManager
.add(new UnfollowCpuAction(ResourcesView
.this, resourcesEntry
.getId(), resourcesEntry
.getTrace()));
120 menuManager
.add(new FollowCpuAction(ResourcesView
.this, resourcesEntry
.getId(), resourcesEntry
.getTrace()));
127 private static class ResourcesFilterLabelProvider
extends TreeLabelProvider
{
129 public String
getColumnText(Object element
, int columnIndex
) {
130 ResourcesEntry entry
= (ResourcesEntry
) element
;
131 if (columnIndex
== 0) {
132 return entry
.getName();
134 return ""; //$NON-NLS-1$
139 // ------------------------------------------------------------------------
141 // ------------------------------------------------------------------------
144 protected String
getNextText() {
145 return Messages
.ResourcesView_nextResourceActionNameText
;
149 protected String
getNextTooltip() {
150 return Messages
.ResourcesView_nextResourceActionToolTipText
;
154 protected String
getPrevText() {
155 return Messages
.ResourcesView_previousResourceActionNameText
;
159 protected String
getPrevTooltip() {
160 return Messages
.ResourcesView_previousResourceActionToolTipText
;
164 protected void buildEntryList(ITmfTrace trace
, ITmfTrace parentTrace
, final IProgressMonitor monitor
) {
165 final ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysisModule
.ID
);
170 Map
<Integer
, ResourcesEntry
> entryMap
= new HashMap
<>();
171 TimeGraphEntry traceEntry
= null;
173 long startTime
= ssq
.getStartTime();
174 long start
= startTime
;
175 setStartTime(Math
.min(getStartTime(), startTime
));
176 boolean complete
= false;
178 if (monitor
.isCanceled()) {
181 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
182 if (ssq
.isCancelled()) {
185 long end
= ssq
.getCurrentEndTime();
186 if (start
== end
&& !complete
) {
187 // when complete execute one last time regardless of end time
190 long endTime
= end
+ 1;
191 setEndTime(Math
.max(getEndTime(), endTime
));
193 if (traceEntry
== null) {
194 traceEntry
= new ResourcesEntry(trace
, trace
.getName(), startTime
, endTime
, 0);
195 List
<TimeGraphEntry
> entryList
= Collections
.singletonList(traceEntry
);
196 addToEntryList(parentTrace
, ssq
, entryList
);
198 traceEntry
.updateEndTime(endTime
);
200 List
<Integer
> cpuQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*"); //$NON-NLS-1$
201 createCpuEntriesWithQuark(trace
, ssq
, entryMap
, traceEntry
, startTime
, endTime
, cpuQuarks
);
202 if (parentTrace
.equals(getTrace())) {
205 final List
<@NonNull TimeGraphEntry
> traceEntryChildren
= traceEntry
.getChildren();
206 final long resolution
= Math
.max(1, (endTime
- ssq
.getStartTime()) / getDisplayWidth());
207 queryFullStates(ssq
, ssq
.getStartTime(), end
, resolution
, monitor
, new IQueryHandler() {
209 public void handle(List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
) {
210 for (TimeGraphEntry child
: traceEntryChildren
) {
211 if (!populateEventsRecursively(fullStates
, prevFullState
, child
).isOK()) {
217 private IStatus
populateEventsRecursively(@NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, @NonNull TimeGraphEntry entry
) {
218 if (monitor
.isCanceled()) {
219 return Status
.CANCEL_STATUS
;
221 List
<ITimeEvent
> eventList
= getEventList(entry
, ssq
, fullStates
, prevFullState
, monitor
);
222 if (eventList
!= null) {
223 /* Start a new event list on first iteration, then append to it */
224 if (prevFullState
== null) {
225 entry
.setEventList(eventList
);
227 for (ITimeEvent event
: eventList
) {
228 entry
.addEvent(event
);
232 for (TimeGraphEntry child
: entry
.getChildren()) {
233 IStatus status
= populateEventsRecursively(fullStates
, prevFullState
, child
);
234 if (!status
.isOK()) {
238 return Status
.OK_STATUS
;
247 private static void createCpuEntriesWithQuark(@NonNull ITmfTrace trace
, final ITmfStateSystem ssq
, Map
<Integer
, ResourcesEntry
> entryMap
, TimeGraphEntry traceEntry
, long startTime
, long endTime
, List
<Integer
> cpuQuarks
) {
248 for (Integer cpuQuark
: cpuQuarks
) {
249 final @NonNull String cpuName
= ssq
.getAttributeName(cpuQuark
);
250 int cpu
= Integer
.parseInt(cpuName
);
251 ResourcesEntry cpuEntry
= entryMap
.get(cpuQuark
);
252 if (cpuEntry
== null) {
253 cpuEntry
= new ResourcesEntry(cpuQuark
, trace
, startTime
, endTime
, Type
.CPU
, cpu
);
254 entryMap
.put(cpuQuark
, cpuEntry
);
255 traceEntry
.addChild(cpuEntry
);
257 cpuEntry
.updateEndTime(endTime
);
259 List
<Integer
> irqQuarks
= ssq
.getQuarks(Attributes
.CPUS
, cpuName
, Attributes
.IRQS
, "*"); //$NON-NLS-1$
260 createCpuInterruptEntryWithQuark(trace
, ssq
, entryMap
, startTime
, endTime
, traceEntry
, cpuEntry
, irqQuarks
, Type
.IRQ
);
261 List
<Integer
> softIrqQuarks
= ssq
.getQuarks(Attributes
.CPUS
, cpuName
, Attributes
.SOFT_IRQS
, "*"); //$NON-NLS-1$
262 createCpuInterruptEntryWithQuark(trace
, ssq
, entryMap
, startTime
, endTime
, traceEntry
, cpuEntry
, softIrqQuarks
, Type
.SOFT_IRQ
);
267 * Create and add execution contexts to a cpu entry. Also creates an
268 * aggregate entry in the root trace entry. The execution context is
269 * basically what the cpu is doing in its execution stack. It can be in an
270 * IRQ, Soft IRQ. MCEs, NMIs, Userland and Kernel execution is not yet
280 * the start time in nanoseconds
282 * the end time in nanoseconds
284 * the trace timegraph entry
286 * the cpu timegraph entry (the entry under the trace entry
287 * @param childrenQuarks
288 * the quarks to add to cpu entry
290 * the type of entry being added
292 private static void createCpuInterruptEntryWithQuark(@NonNull ITmfTrace trace
,
293 final ITmfStateSystem ssq
, Map
<Integer
, ResourcesEntry
> entryMap
,
294 long startTime
, long endTime
,
295 TimeGraphEntry traceEntry
, ResourcesEntry cpuEntry
,
296 List
<Integer
> childrenQuarks
, Type type
) {
297 for (Integer quark
: childrenQuarks
) {
298 final @NonNull String resourceName
= ssq
.getAttributeName(quark
);
299 int resourceId
= Integer
.parseInt(resourceName
);
300 ResourcesEntry interruptEntry
= entryMap
.get(quark
);
301 if (interruptEntry
== null) {
302 interruptEntry
= new ResourcesEntry(quark
, trace
, startTime
, endTime
, type
, resourceId
);
303 entryMap
.put(quark
, interruptEntry
);
304 cpuEntry
.addChild(interruptEntry
);
305 boolean found
= false;
306 for (ITimeGraphEntry rootElem
: traceEntry
.getChildren()) {
307 if (rootElem
instanceof AggregateResourcesEntry
) {
308 AggregateResourcesEntry aggregateInterruptEntry
= (AggregateResourcesEntry
) rootElem
;
309 if (aggregateInterruptEntry
.getId() == resourceId
&& aggregateInterruptEntry
.getType().equals(type
)) {
311 aggregateInterruptEntry
.addContributor(interruptEntry
);
312 final AggregateResourcesEntry irqCpuEntry
= new AggregateResourcesEntry(trace
, cpuEntry
.getName(), startTime
, endTime
, type
, cpuEntry
.getId());
313 irqCpuEntry
.addContributor(interruptEntry
);
314 aggregateInterruptEntry
.addChild(irqCpuEntry
);
320 AggregateResourcesEntry aggregateInterruptEntry
= new AggregateResourcesEntry(trace
, startTime
, endTime
, type
, resourceId
);
321 aggregateInterruptEntry
.addContributor(interruptEntry
);
322 final AggregateResourcesEntry irqCpuEntry
= new AggregateResourcesEntry(trace
, cpuEntry
.getName(), startTime
, endTime
, type
, cpuEntry
.getId());
323 irqCpuEntry
.addContributor(interruptEntry
);
324 aggregateInterruptEntry
.addChild(irqCpuEntry
);
325 traceEntry
.addChild(aggregateInterruptEntry
);
328 interruptEntry
.updateEndTime(endTime
);
334 protected @Nullable List
<ITimeEvent
> getEventList(@NonNull TimeGraphEntry entry
, ITmfStateSystem ssq
,
335 @NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, @NonNull IProgressMonitor monitor
) {
336 ResourcesEntry resourcesEntry
= (ResourcesEntry
) entry
;
337 int quark
= resourcesEntry
.getQuark();
339 if (resourcesEntry
.getType().equals(Type
.CPU
)) {
340 return createCpuEventsList(entry
, ssq
, fullStates
, prevFullState
, monitor
, quark
);
341 } else if ((resourcesEntry
.getType().equals(Type
.IRQ
) || resourcesEntry
.getType().equals(Type
.SOFT_IRQ
)) && (quark
>= 0)) {
342 return createIrqEventsList(entry
, fullStates
, prevFullState
, monitor
, quark
);
348 private static List
<ITimeEvent
> createCpuEventsList(ITimeGraphEntry entry
, ITmfStateSystem ssq
, List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
, IProgressMonitor monitor
, int quark
) {
349 List
<ITimeEvent
> eventList
;
352 statusQuark
= ssq
.getQuarkRelative(quark
, Attributes
.STATUS
);
353 } catch (AttributeNotFoundException e
) {
355 * The sub-attribute "status" is not available. May happen if the
356 * trace does not have sched_switch events enabled.
360 boolean isZoomThread
= Thread
.currentThread() instanceof ZoomThread
;
361 eventList
= new ArrayList
<>(fullStates
.size());
362 ITmfStateInterval lastInterval
= prevFullState
== null || statusQuark
>= prevFullState
.size() ?
null : prevFullState
.get(statusQuark
);
363 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
364 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
365 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
366 if (monitor
.isCanceled()) {
369 if (statusQuark
>= fullState
.size()) {
370 /* No information on this CPU (yet?), skip it for now */
373 ITmfStateInterval statusInterval
= fullState
.get(statusQuark
);
374 int status
= statusInterval
.getStateValue().unboxInt();
375 long time
= statusInterval
.getStartTime();
376 long duration
= statusInterval
.getEndTime() - time
+ 1;
377 if (time
== lastStartTime
) {
380 if (!statusInterval
.getStateValue().isNull()) {
381 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
382 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
384 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
385 } else if (isZoomThread
) {
386 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
388 lastStartTime
= time
;
389 lastEndTime
= time
+ duration
;
394 private static List
<ITimeEvent
> createIrqEventsList(ITimeGraphEntry entry
, List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
, IProgressMonitor monitor
, int quark
) {
395 List
<ITimeEvent
> eventList
;
396 boolean isZoomThread
= Thread
.currentThread() instanceof ZoomThread
;
397 eventList
= new ArrayList
<>(fullStates
.size());
398 ITmfStateInterval lastInterval
= prevFullState
== null || quark
>= prevFullState
.size() ?
null : prevFullState
.get(quark
);
399 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
400 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
401 boolean lastIsNull
= lastInterval
== null ?
false : lastInterval
.getStateValue().isNull();
402 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
403 if (monitor
.isCanceled()) {
406 if (quark
>= fullState
.size()) {
407 /* No information on this IRQ (yet?), skip it for now */
410 ITmfStateInterval irqInterval
= fullState
.get(quark
);
411 long time
= irqInterval
.getStartTime();
412 long duration
= irqInterval
.getEndTime() - time
+ 1;
413 if (time
== lastStartTime
) {
416 if (!irqInterval
.getStateValue().isNull()) {
417 int cpu
= irqInterval
.getStateValue().unboxInt();
418 eventList
.add(new TimeEvent(entry
, time
, duration
, cpu
));
421 if (lastEndTime
!= time
&& lastIsNull
) {
423 * This is a special case where we want to show IRQ_ACTIVE
424 * state but we don't know the CPU (it is between two null
427 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
, -1));
430 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
434 lastStartTime
= time
;
435 lastEndTime
= time
+ duration
;
441 * Signal handler for a cpu selected signal.
444 * the cpu selected signal
448 public void listenToCpu(TmfCpuSelectedSignal signal
) {
449 int data
= signal
.getCore() >= 0 ? signal
.getCore() : -1;
450 TmfTraceContext ctx
= TmfTraceManager
.getInstance().getCurrentTraceContext();
451 ctx
.setData(RESOURCES_FOLLOW_CPU
, data
);