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
.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
.analysis
.os
.linux
.ui
.views
.resources
.ResourcesEntry
.Type
;
34 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.core
.kernel
.Attributes
;
35 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.Messages
;
36 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.actions
.FollowCpuAction
;
37 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.actions
.UnfollowCpuAction
;
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
.ui
.views
.timegraph
.AbstractStateSystemTimeGraphView
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
52 * Main implementation for the LTTng 2.0 kernel Resource view
54 * @author Patrick Tasse
56 public class ResourcesView
extends AbstractStateSystemTimeGraphView
{
59 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
61 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
62 Messages
.ResourcesView_stateTypeName
65 private int fCurrentCpu
= -1;
67 // Timeout between updates in the build thread in ms
68 private static final long BUILD_UPDATE_TIMEOUT
= 500;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
77 public ResourcesView() {
78 super(ID
, new ResourcesPresentationProvider());
79 setFilterColumns(FILTER_COLUMN_NAMES
);
80 setFilterLabelProvider(new ResourcesFilterLabelProvider());
81 setEntryComparator(new ResourcesEntryComparator());
82 setAutoExpandLevel(1);
85 private static class ResourcesEntryComparator
implements Comparator
<ITimeGraphEntry
> {
87 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
88 ResourcesEntry entry1
= (ResourcesEntry
) o1
;
89 ResourcesEntry entry2
= (ResourcesEntry
) o2
;
90 if (entry1
.getType() == Type
.NULL
&& entry2
.getType() == Type
.NULL
) {
91 /* sort trace entries alphabetically */
92 return entry1
.getName().compareTo(entry2
.getName());
94 /* sort resource entries by their defined order */
95 return entry1
.compareTo(entry2
);
104 protected void fillTimeGraphEntryContextMenu(@NonNull IMenuManager menuManager
) {
105 ISelection selection
= getSite().getSelectionProvider().getSelection();
106 if (selection
instanceof IStructuredSelection
) {
107 IStructuredSelection sSel
= (IStructuredSelection
) selection
;
108 if (sSel
.getFirstElement() instanceof ResourcesEntry
) {
109 ResourcesEntry resourcesEntry
= (ResourcesEntry
) sSel
.getFirstElement();
110 if (resourcesEntry
.getType().equals(ResourcesEntry
.Type
.CPU
)) {
111 if (fCurrentCpu
>= 0) {
112 menuManager
.add(new UnfollowCpuAction(ResourcesView
.this, resourcesEntry
.getId(), resourcesEntry
.getTrace()));
114 menuManager
.add(new FollowCpuAction(ResourcesView
.this, resourcesEntry
.getId(), resourcesEntry
.getTrace()));
121 private static class ResourcesFilterLabelProvider
extends TreeLabelProvider
{
123 public String
getColumnText(Object element
, int columnIndex
) {
124 ResourcesEntry entry
= (ResourcesEntry
) element
;
125 if (columnIndex
== 0) {
126 return entry
.getName();
128 return ""; //$NON-NLS-1$
133 // ------------------------------------------------------------------------
135 // ------------------------------------------------------------------------
138 protected String
getNextText() {
139 return Messages
.ResourcesView_nextResourceActionNameText
;
143 protected String
getNextTooltip() {
144 return Messages
.ResourcesView_nextResourceActionToolTipText
;
148 protected String
getPrevText() {
149 return Messages
.ResourcesView_previousResourceActionNameText
;
153 protected String
getPrevTooltip() {
154 return Messages
.ResourcesView_previousResourceActionToolTipText
;
158 protected void buildEventList(ITmfTrace trace
, ITmfTrace parentTrace
, final IProgressMonitor monitor
) {
159 final ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysisModule
.ID
);
164 Map
<Integer
, ResourcesEntry
> entryMap
= new HashMap
<>();
165 TimeGraphEntry traceEntry
= null;
167 long startTime
= ssq
.getStartTime();
168 long start
= startTime
;
169 setStartTime(Math
.min(getStartTime(), startTime
));
170 boolean complete
= false;
172 if (monitor
.isCanceled()) {
175 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
176 if (ssq
.isCancelled()) {
179 long end
= ssq
.getCurrentEndTime();
180 if (start
== end
&& !complete
) {
181 // when complete execute one last time regardless of end time
184 long endTime
= end
+ 1;
185 setEndTime(Math
.max(getEndTime(), endTime
));
187 if (traceEntry
== null) {
188 traceEntry
= new ResourcesEntry(trace
, trace
.getName(), startTime
, endTime
, 0);
189 List
<TimeGraphEntry
> entryList
= Collections
.singletonList(traceEntry
);
190 addToEntryList(parentTrace
, ssq
, entryList
);
192 traceEntry
.updateEndTime(endTime
);
194 List
<Integer
> cpuQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*"); //$NON-NLS-1$
195 createCpuEntriesWithQuark(trace
, ssq
, entryMap
, traceEntry
, startTime
, endTime
, cpuQuarks
);
196 if (parentTrace
.equals(getTrace())) {
199 final List
<?
extends ITimeGraphEntry
> traceEntryChildren
= traceEntry
.getChildren();
200 final long resolution
= Math
.max(1, (endTime
- ssq
.getStartTime()) / getDisplayWidth());
201 final long qStart
= start
;
202 final long qEnd
= end
;
203 queryFullStates(ssq
, qStart
, qEnd
, resolution
, monitor
, new IQueryHandler() {
205 public void handle(List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
) {
206 for (ITimeGraphEntry child
: traceEntryChildren
) {
207 if (!populateEventsRecursively(fullStates
, prevFullState
, child
).isOK()) {
213 private IStatus
populateEventsRecursively(@NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, ITimeGraphEntry entry
) {
214 if (monitor
.isCanceled()) {
215 return Status
.CANCEL_STATUS
;
217 if (entry
instanceof TimeGraphEntry
) {
218 TimeGraphEntry timeGraphEntry
= (TimeGraphEntry
) entry
;
219 List
<ITimeEvent
> eventList
= getEventList(timeGraphEntry
, ssq
, fullStates
, prevFullState
, monitor
);
220 if (eventList
!= null) {
221 for (ITimeEvent event
: eventList
) {
222 timeGraphEntry
.addEvent(event
);
226 for (ITimeGraphEntry child
: entry
.getChildren()) {
227 IStatus status
= populateEventsRecursively(fullStates
, prevFullState
, child
);
228 if (!status
.isOK()) {
232 return Status
.OK_STATUS
;
241 private static void createCpuEntriesWithQuark(@NonNull ITmfTrace trace
, final ITmfStateSystem ssq
, Map
<Integer
, ResourcesEntry
> entryMap
, TimeGraphEntry traceEntry
, long startTime
, long endTime
, List
<Integer
> cpuQuarks
) {
242 for (Integer cpuQuark
: cpuQuarks
) {
243 final @NonNull String cpuName
= ssq
.getAttributeName(cpuQuark
);
244 int cpu
= Integer
.parseInt(cpuName
);
245 ResourcesEntry cpuEntry
= entryMap
.get(cpuQuark
);
246 if (cpuEntry
== null) {
247 cpuEntry
= new ResourcesEntry(cpuQuark
, trace
, startTime
, endTime
, Type
.CPU
, cpu
);
248 entryMap
.put(cpuQuark
, cpuEntry
);
249 traceEntry
.addChild(cpuEntry
);
251 cpuEntry
.updateEndTime(endTime
);
253 List
<Integer
> irqQuarks
= ssq
.getQuarks(Attributes
.CPUS
, cpuName
, Attributes
.IRQS
, "*"); //$NON-NLS-1$
254 createCpuInterruptEntryWithQuark(trace
, ssq
, entryMap
, startTime
, endTime
, traceEntry
, cpuEntry
, irqQuarks
, Type
.IRQ
);
255 List
<Integer
> softIrqQuarks
= ssq
.getQuarks(Attributes
.CPUS
, cpuName
, Attributes
.SOFT_IRQS
, "*"); //$NON-NLS-1$
256 createCpuInterruptEntryWithQuark(trace
, ssq
, entryMap
, startTime
, endTime
, traceEntry
, cpuEntry
, softIrqQuarks
, Type
.SOFT_IRQ
);
261 * Create and add execution contexts to a cpu entry. Also creates an
262 * aggregate entry in the root trace entry. The execution context is
263 * basically what the cpu is doing in its execution stack. It can be in an
264 * IRQ, Soft IRQ. MCEs, NMIs, Userland and Kernel execution is not yet
274 * the start time in nanoseconds
276 * the end time in nanoseconds
278 * the trace timegraph entry
280 * the cpu timegraph entry (the entry under the trace entry
281 * @param childrenQuarks
282 * the quarks to add to cpu entry
284 * the type of entry being added
286 private static void createCpuInterruptEntryWithQuark(@NonNull ITmfTrace trace
,
287 final ITmfStateSystem ssq
, Map
<Integer
, ResourcesEntry
> entryMap
,
288 long startTime
, long endTime
,
289 TimeGraphEntry traceEntry
, ResourcesEntry cpuEntry
,
290 List
<Integer
> childrenQuarks
, Type type
) {
291 for (Integer quark
: childrenQuarks
) {
292 final @NonNull String resourceName
= ssq
.getAttributeName(quark
);
293 int resourceId
= Integer
.parseInt(resourceName
);
294 ResourcesEntry interruptEntry
= entryMap
.get(quark
);
295 if (interruptEntry
== null) {
296 interruptEntry
= new ResourcesEntry(quark
, trace
, startTime
, endTime
, type
, resourceId
);
297 entryMap
.put(quark
, interruptEntry
);
298 cpuEntry
.addChild(interruptEntry
);
299 boolean found
= false;
300 for (ITimeGraphEntry rootElem
: traceEntry
.getChildren()) {
301 if (rootElem
instanceof AggregateResourcesEntry
) {
302 AggregateResourcesEntry aggregateInterruptEntry
= (AggregateResourcesEntry
) rootElem
;
303 if (aggregateInterruptEntry
.getId() == resourceId
&& aggregateInterruptEntry
.getType().equals(type
)) {
305 aggregateInterruptEntry
.addContributor(interruptEntry
);
306 final AggregateResourcesEntry irqCpuEntry
= new AggregateResourcesEntry(trace
, cpuEntry
.getName(), startTime
, endTime
, type
, cpuEntry
.getId());
307 irqCpuEntry
.addContributor(interruptEntry
);
308 aggregateInterruptEntry
.addChild(irqCpuEntry
);
314 AggregateResourcesEntry aggregateInterruptEntry
= new AggregateResourcesEntry(trace
, startTime
, endTime
, type
, resourceId
);
315 aggregateInterruptEntry
.addContributor(interruptEntry
);
316 final AggregateResourcesEntry irqCpuEntry
= new AggregateResourcesEntry(trace
, cpuEntry
.getName(), startTime
, endTime
, type
, cpuEntry
.getId());
317 irqCpuEntry
.addContributor(interruptEntry
);
318 aggregateInterruptEntry
.addChild(irqCpuEntry
);
319 traceEntry
.addChild(aggregateInterruptEntry
);
322 interruptEntry
.updateEndTime(endTime
);
328 protected @Nullable List
<ITimeEvent
> getEventList(@NonNull TimeGraphEntry entry
, ITmfStateSystem ssq
,
329 @NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, @NonNull IProgressMonitor monitor
) {
330 ResourcesEntry resourcesEntry
= (ResourcesEntry
) entry
;
331 int quark
= resourcesEntry
.getQuark();
333 if (resourcesEntry
.getType().equals(Type
.CPU
)) {
334 return createCpuEventsList(entry
, ssq
, fullStates
, prevFullState
, monitor
, quark
);
335 } else if ((resourcesEntry
.getType().equals(Type
.IRQ
) || resourcesEntry
.getType().equals(Type
.SOFT_IRQ
)) && (quark
>= 0)) {
336 return createIrqEventsList(entry
, fullStates
, prevFullState
, monitor
, quark
);
342 private static List
<ITimeEvent
> createCpuEventsList(TimeGraphEntry entry
, ITmfStateSystem ssq
, List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
, IProgressMonitor monitor
, int quark
) {
343 List
<ITimeEvent
> eventList
;
346 statusQuark
= ssq
.getQuarkRelative(quark
, Attributes
.STATUS
);
347 } catch (AttributeNotFoundException e
) {
349 * The sub-attribute "status" is not available. May happen if the
350 * trace does not have sched_switch events enabled.
354 eventList
= new ArrayList
<>(fullStates
.size());
355 ITmfStateInterval lastInterval
= prevFullState
== null || statusQuark
>= prevFullState
.size() ?
null : prevFullState
.get(statusQuark
);
356 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
357 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
358 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
359 if (monitor
.isCanceled()) {
362 if (statusQuark
>= fullState
.size()) {
363 /* No information on this CPU (yet?), skip it for now */
366 ITmfStateInterval statusInterval
= fullState
.get(statusQuark
);
367 int status
= statusInterval
.getStateValue().unboxInt();
368 long time
= statusInterval
.getStartTime();
369 long duration
= statusInterval
.getEndTime() - time
+ 1;
370 if (time
== lastStartTime
) {
373 if (!statusInterval
.getStateValue().isNull()) {
374 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
375 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
377 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
379 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
381 lastStartTime
= time
;
382 lastEndTime
= time
+ duration
;
387 private static List
<ITimeEvent
> createIrqEventsList(TimeGraphEntry entry
, List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
, IProgressMonitor monitor
, int quark
) {
388 List
<ITimeEvent
> eventList
;
389 eventList
= new ArrayList
<>(fullStates
.size());
390 ITmfStateInterval lastInterval
= prevFullState
== null || quark
>= prevFullState
.size() ?
null : prevFullState
.get(quark
);
391 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
392 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
393 boolean lastIsNull
= lastInterval
== null ?
false : lastInterval
.getStateValue().isNull();
394 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
395 if (monitor
.isCanceled()) {
398 if (quark
>= fullState
.size()) {
399 /* No information on this IRQ (yet?), skip it for now */
402 ITmfStateInterval irqInterval
= fullState
.get(quark
);
403 long time
= irqInterval
.getStartTime();
404 long duration
= irqInterval
.getEndTime() - time
+ 1;
405 if (time
== lastStartTime
) {
408 if (!irqInterval
.getStateValue().isNull()) {
409 int cpu
= irqInterval
.getStateValue().unboxInt();
410 eventList
.add(new TimeEvent(entry
, time
, duration
, cpu
));
413 if (lastEndTime
!= time
&& lastIsNull
) {
415 * This is a special case where we want to show IRQ_ACTIVE
416 * state but we don't know the CPU (it is between two null
419 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
, -1));
421 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
424 lastStartTime
= time
;
425 lastEndTime
= time
+ duration
;
431 * Signal handler for a cpu selected signal.
434 * the cpu selected signal
438 public void listenToCpu(TmfCpuSelectedSignal signal
) {
439 if (signal
.getCore() >= 0) {
440 fCurrentCpu
= signal
.getCore();