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
.kernelanalysis
.Attributes
;
29 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelAnalysisModule
;
30 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.ui
.views
.resources
.ResourcesEntry
.Type
;
31 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.Messages
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractStateSystemTimeGraphView
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
45 * Main implementation for the LTTng 2.0 kernel Resource view
47 * @author Patrick Tasse
49 public class ResourcesView
extends AbstractStateSystemTimeGraphView
{
52 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
54 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
55 Messages
.ResourcesView_stateTypeName
58 // Timeout between updates in the build thread in ms
59 private static final long BUILD_UPDATE_TIMEOUT
= 500;
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
68 public ResourcesView() {
69 super(ID
, new ResourcesPresentationProvider());
70 setFilterColumns(FILTER_COLUMN_NAMES
);
71 setFilterLabelProvider(new ResourcesFilterLabelProvider());
72 setEntryComparator(new ResourcesEntryComparator());
73 setAutoExpandLevel(1);
76 private static class ResourcesEntryComparator
implements Comparator
<ITimeGraphEntry
> {
78 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
79 ResourcesEntry entry1
= (ResourcesEntry
) o1
;
80 ResourcesEntry entry2
= (ResourcesEntry
) o2
;
81 if (entry1
.getType() == Type
.NULL
&& entry2
.getType() == Type
.NULL
) {
82 /* sort trace entries alphabetically */
83 return entry1
.getName().compareTo(entry2
.getName());
85 /* sort resource entries by their defined order */
86 return entry1
.compareTo(entry2
);
90 private static class ResourcesFilterLabelProvider
extends TreeLabelProvider
{
92 public String
getColumnText(Object element
, int columnIndex
) {
93 ResourcesEntry entry
= (ResourcesEntry
) element
;
94 if (columnIndex
== 0) {
95 return entry
.getName();
97 return ""; //$NON-NLS-1$
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 protected String
getNextText() {
108 return Messages
.ResourcesView_nextResourceActionNameText
;
112 protected String
getNextTooltip() {
113 return Messages
.ResourcesView_nextResourceActionToolTipText
;
117 protected String
getPrevText() {
118 return Messages
.ResourcesView_previousResourceActionNameText
;
122 protected String
getPrevTooltip() {
123 return Messages
.ResourcesView_previousResourceActionToolTipText
;
127 protected void buildEventList(ITmfTrace trace
, ITmfTrace parentTrace
, final IProgressMonitor monitor
) {
128 final ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysisModule
.ID
);
133 Map
<Integer
, ResourcesEntry
> entryMap
= new HashMap
<>();
134 TimeGraphEntry traceEntry
= null;
136 long startTime
= ssq
.getStartTime();
137 long start
= startTime
;
138 setStartTime(Math
.min(getStartTime(), startTime
));
139 boolean complete
= false;
141 if (monitor
.isCanceled()) {
144 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
145 if (ssq
.isCancelled()) {
148 long end
= ssq
.getCurrentEndTime();
149 if (start
== end
&& !complete
) {
150 // when complete execute one last time regardless of end time
153 long endTime
= end
+ 1;
154 setEndTime(Math
.max(getEndTime(), endTime
));
156 if (traceEntry
== null) {
157 traceEntry
= new ResourcesEntry(trace
, trace
.getName(), startTime
, endTime
, 0);
158 List
<TimeGraphEntry
> entryList
= Collections
.singletonList(traceEntry
);
159 addToEntryList(parentTrace
, ssq
, entryList
);
161 traceEntry
.updateEndTime(endTime
);
163 List
<Integer
> cpuQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*"); //$NON-NLS-1$
164 createCpuEntriesWithQuark(trace
, ssq
, entryMap
, traceEntry
, startTime
, endTime
, cpuQuarks
);
165 if (parentTrace
.equals(getTrace())) {
168 final List
<?
extends ITimeGraphEntry
> traceEntryChildren
= traceEntry
.getChildren();
169 final long resolution
= Math
.max(1, (endTime
- ssq
.getStartTime()) / getDisplayWidth());
170 final long qStart
= start
;
171 final long qEnd
= end
;
172 queryFullStates(ssq
, qStart
, qEnd
, resolution
, monitor
, new IQueryHandler() {
174 public void handle(List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
) {
175 for (ITimeGraphEntry child
: traceEntryChildren
) {
176 if (!populateEventsRecursively(fullStates
, prevFullState
, child
).isOK()) {
182 private IStatus
populateEventsRecursively(@NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, ITimeGraphEntry entry
) {
183 if (monitor
.isCanceled()) {
184 return Status
.CANCEL_STATUS
;
186 if (entry
instanceof TimeGraphEntry
) {
187 TimeGraphEntry timeGraphEntry
= (TimeGraphEntry
) entry
;
188 List
<ITimeEvent
> eventList
= getEventList(timeGraphEntry
, ssq
, fullStates
, prevFullState
, monitor
);
189 if (eventList
!= null) {
190 for (ITimeEvent event
: eventList
) {
191 timeGraphEntry
.addEvent(event
);
195 for (ITimeGraphEntry child
: entry
.getChildren()) {
196 IStatus status
= populateEventsRecursively(fullStates
, prevFullState
, child
);
197 if (!status
.isOK()) {
201 return Status
.OK_STATUS
;
210 private static void createCpuEntriesWithQuark(@NonNull ITmfTrace trace
, final ITmfStateSystem ssq
, Map
<Integer
, ResourcesEntry
> entryMap
, TimeGraphEntry traceEntry
, long startTime
, long endTime
, List
<Integer
> cpuQuarks
) {
211 for (Integer cpuQuark
: cpuQuarks
) {
212 final @NonNull String cpuName
= ssq
.getAttributeName(cpuQuark
);
213 int cpu
= Integer
.parseInt(cpuName
);
214 ResourcesEntry cpuEntry
= entryMap
.get(cpuQuark
);
215 if (cpuEntry
== null) {
216 cpuEntry
= new ResourcesEntry(cpuQuark
, trace
, startTime
, endTime
, Type
.CPU
, cpu
);
217 entryMap
.put(cpuQuark
, cpuEntry
);
218 traceEntry
.addChild(cpuEntry
);
220 cpuEntry
.updateEndTime(endTime
);
222 List
<Integer
> irqQuarks
= ssq
.getQuarks(Attributes
.CPUS
, cpuName
, Attributes
.IRQS
, "*"); //$NON-NLS-1$
223 createCpuInterruptEntryWithQuark(trace
, ssq
, entryMap
, startTime
, endTime
, traceEntry
, cpuEntry
, irqQuarks
, Type
.IRQ
);
224 List
<Integer
> softIrqQuarks
= ssq
.getQuarks(Attributes
.CPUS
, cpuName
, Attributes
.SOFT_IRQS
, "*"); //$NON-NLS-1$
225 createCpuInterruptEntryWithQuark(trace
, ssq
, entryMap
, startTime
, endTime
, traceEntry
, cpuEntry
, softIrqQuarks
, Type
.SOFT_IRQ
);
230 * Create and add execution contexts to a cpu entry. Also creates an
231 * aggregate entry in the root trace entry. The execution context is
232 * basically what the cpu is doing in its execution stack. It can be in an
233 * IRQ, Soft IRQ. MCEs, NMIs, Userland and Kernel execution is not yet
243 * the start time in nanoseconds
245 * the end time in nanoseconds
247 * the trace timegraph entry
249 * the cpu timegraph entry (the entry under the trace entry
250 * @param childrenQuarks
251 * the quarks to add to cpu entry
253 * the type of entry being added
255 private static void createCpuInterruptEntryWithQuark(@NonNull ITmfTrace trace
,
256 final ITmfStateSystem ssq
, Map
<Integer
, ResourcesEntry
> entryMap
,
257 long startTime
, long endTime
,
258 TimeGraphEntry traceEntry
, ResourcesEntry cpuEntry
,
259 List
<Integer
> childrenQuarks
, Type type
) {
260 for (Integer quark
: childrenQuarks
) {
261 final @NonNull String resourceName
= ssq
.getAttributeName(quark
);
262 int resourceId
= Integer
.parseInt(resourceName
);
263 ResourcesEntry interruptEntry
= entryMap
.get(quark
);
264 if (interruptEntry
== null) {
265 interruptEntry
= new ResourcesEntry(quark
, trace
, startTime
, endTime
, type
, resourceId
);
266 entryMap
.put(quark
, interruptEntry
);
267 cpuEntry
.addChild(interruptEntry
);
268 boolean found
= false;
269 for (ITimeGraphEntry rootElem
: traceEntry
.getChildren()) {
270 if (rootElem
instanceof AggregateResourcesEntry
) {
271 AggregateResourcesEntry aggregateInterruptEntry
= (AggregateResourcesEntry
) rootElem
;
272 if (aggregateInterruptEntry
.getId() == resourceId
&& aggregateInterruptEntry
.getType().equals(type
)) {
274 aggregateInterruptEntry
.addContributor(interruptEntry
);
280 AggregateResourcesEntry aggregateInterruptEntry
= new AggregateResourcesEntry(trace
, startTime
, endTime
, type
, resourceId
);
281 aggregateInterruptEntry
.addContributor(interruptEntry
);
282 traceEntry
.addChild(aggregateInterruptEntry
);
285 interruptEntry
.updateEndTime(endTime
);
291 protected @Nullable List
<ITimeEvent
> getEventList(@NonNull TimeGraphEntry entry
, ITmfStateSystem ssq
,
292 @NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, @NonNull IProgressMonitor monitor
) {
293 ResourcesEntry resourcesEntry
= (ResourcesEntry
) entry
;
294 int quark
= resourcesEntry
.getQuark();
296 if (resourcesEntry
.getType().equals(Type
.CPU
)) {
297 return createCpuEventsList(entry
, ssq
, fullStates
, prevFullState
, monitor
, quark
);
298 } else if ((resourcesEntry
.getType().equals(Type
.IRQ
) || resourcesEntry
.getType().equals(Type
.SOFT_IRQ
)) && (quark
>= 0)) {
299 return createIrqEventsList(entry
, fullStates
, prevFullState
, monitor
, quark
);
305 private static List
<ITimeEvent
> createCpuEventsList(TimeGraphEntry entry
, ITmfStateSystem ssq
, List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
, IProgressMonitor monitor
, int quark
) {
306 List
<ITimeEvent
> eventList
;
309 statusQuark
= ssq
.getQuarkRelative(quark
, Attributes
.STATUS
);
310 } catch (AttributeNotFoundException e
) {
312 * The sub-attribute "status" is not available. May happen if the
313 * trace does not have sched_switch events enabled.
317 eventList
= new ArrayList
<>(fullStates
.size());
318 ITmfStateInterval lastInterval
= prevFullState
== null || statusQuark
>= prevFullState
.size() ?
null : prevFullState
.get(statusQuark
);
319 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
320 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
321 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
322 if (monitor
.isCanceled()) {
325 if (statusQuark
>= fullState
.size()) {
326 /* No information on this CPU (yet?), skip it for now */
329 ITmfStateInterval statusInterval
= fullState
.get(statusQuark
);
330 int status
= statusInterval
.getStateValue().unboxInt();
331 long time
= statusInterval
.getStartTime();
332 long duration
= statusInterval
.getEndTime() - time
+ 1;
333 if (time
== lastStartTime
) {
336 if (!statusInterval
.getStateValue().isNull()) {
337 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
338 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
340 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
342 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
344 lastStartTime
= time
;
345 lastEndTime
= time
+ duration
;
350 private static List
<ITimeEvent
> createIrqEventsList(TimeGraphEntry entry
, List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
, IProgressMonitor monitor
, int quark
) {
351 List
<ITimeEvent
> eventList
;
352 eventList
= new ArrayList
<>(fullStates
.size());
353 ITmfStateInterval lastInterval
= prevFullState
== null || quark
>= prevFullState
.size() ?
null : prevFullState
.get(quark
);
354 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
355 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
356 boolean lastIsNull
= lastInterval
== null ?
false : lastInterval
.getStateValue().isNull();
357 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
358 if (monitor
.isCanceled()) {
361 if (quark
>= fullState
.size()) {
362 /* No information on this IRQ (yet?), skip it for now */
365 ITmfStateInterval irqInterval
= fullState
.get(quark
);
366 long time
= irqInterval
.getStartTime();
367 long duration
= irqInterval
.getEndTime() - time
+ 1;
368 if (time
== lastStartTime
) {
371 if (!irqInterval
.getStateValue().isNull()) {
372 int cpu
= irqInterval
.getStateValue().unboxInt();
373 eventList
.add(new TimeEvent(entry
, time
, duration
, cpu
));
376 if (lastEndTime
!= time
&& lastIsNull
) {
378 * This is a special case where we want to show IRQ_ACTIVE
379 * state but we don't know the CPU (it is between two null
382 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
, -1));
384 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
387 lastStartTime
= time
;
388 lastEndTime
= time
+ duration
;