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
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.jdt
.annotation
.Nullable
;
26 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
27 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelAnalysisModule
;
28 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.ui
.views
.resources
.ResourcesEntry
.Type
;
29 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.Messages
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractStateSystemTimeGraphView
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
43 * Main implementation for the LTTng 2.0 kernel Resource view
45 * @author Patrick Tasse
47 public class ResourcesView
extends AbstractStateSystemTimeGraphView
{
50 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
52 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
53 Messages
.ResourcesView_stateTypeName
56 // Timeout between updates in the build thread in ms
57 private static final long BUILD_UPDATE_TIMEOUT
= 500;
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
66 public ResourcesView() {
67 super(ID
, new ResourcesPresentationProvider());
68 setFilterColumns(FILTER_COLUMN_NAMES
);
69 setFilterLabelProvider(new ResourcesFilterLabelProvider());
70 setEntryComparator(new ResourcesEntryComparator());
73 private static class ResourcesEntryComparator
implements Comparator
<ITimeGraphEntry
> {
75 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
76 ResourcesEntry entry1
= (ResourcesEntry
) o1
;
77 ResourcesEntry entry2
= (ResourcesEntry
) o2
;
78 if (entry1
.getType() == Type
.NULL
&& entry2
.getType() == Type
.NULL
) {
79 /* sort trace entries alphabetically */
80 return entry1
.getName().compareTo(entry2
.getName());
82 /* sort resource entries by their defined order */
83 return entry1
.compareTo(entry2
);
87 private static class ResourcesFilterLabelProvider
extends TreeLabelProvider
{
89 public String
getColumnText(Object element
, int columnIndex
) {
90 ResourcesEntry entry
= (ResourcesEntry
) element
;
91 if (columnIndex
== 0) {
92 return entry
.getName();
94 return ""; //$NON-NLS-1$
99 // ------------------------------------------------------------------------
101 // ------------------------------------------------------------------------
104 protected String
getNextText() {
105 return Messages
.ResourcesView_nextResourceActionNameText
;
109 protected String
getNextTooltip() {
110 return Messages
.ResourcesView_nextResourceActionToolTipText
;
114 protected String
getPrevText() {
115 return Messages
.ResourcesView_previousResourceActionNameText
;
119 protected String
getPrevTooltip() {
120 return Messages
.ResourcesView_previousResourceActionToolTipText
;
124 protected void buildEventList(ITmfTrace trace
, ITmfTrace parentTrace
, final IProgressMonitor monitor
) {
125 final ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysisModule
.ID
);
130 Map
<Integer
, ResourcesEntry
> entryMap
= new HashMap
<>();
131 TimeGraphEntry traceEntry
= null;
133 long startTime
= ssq
.getStartTime();
134 long start
= startTime
;
135 setStartTime(Math
.min(getStartTime(), startTime
));
136 boolean complete
= false;
138 if (monitor
.isCanceled()) {
141 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
142 if (ssq
.isCancelled()) {
145 long end
= ssq
.getCurrentEndTime();
146 if (start
== end
&& !complete
) { // when complete execute one last time regardless of end time
149 long endTime
= end
+ 1;
150 setEndTime(Math
.max(getEndTime(), endTime
));
152 if (traceEntry
== null) {
153 traceEntry
= new ResourcesEntry(trace
, trace
.getName(), startTime
, endTime
, 0);
154 List
<TimeGraphEntry
> entryList
= Collections
.singletonList(traceEntry
);
155 addToEntryList(parentTrace
, ssq
, entryList
);
157 traceEntry
.updateEndTime(endTime
);
160 List
<Integer
> cpuQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*"); //$NON-NLS-1$
161 for (Integer cpuQuark
: cpuQuarks
) {
162 int cpu
= Integer
.parseInt(ssq
.getAttributeName(cpuQuark
));
163 ResourcesEntry entry
= entryMap
.get(cpuQuark
);
165 entry
= new ResourcesEntry(cpuQuark
, trace
, startTime
, endTime
, Type
.CPU
, cpu
);
166 entryMap
.put(cpuQuark
, entry
);
167 traceEntry
.addChild(entry
);
169 entry
.updateEndTime(endTime
);
172 List
<Integer
> irqQuarks
= ssq
.getQuarks(Attributes
.RESOURCES
, Attributes
.IRQS
, "*"); //$NON-NLS-1$
173 for (Integer irqQuark
: irqQuarks
) {
174 int irq
= Integer
.parseInt(ssq
.getAttributeName(irqQuark
));
175 ResourcesEntry entry
= entryMap
.get(irqQuark
);
177 entry
= new ResourcesEntry(irqQuark
, trace
, startTime
, endTime
, Type
.IRQ
, irq
);
178 entryMap
.put(irqQuark
, entry
);
179 traceEntry
.addChild(entry
);
181 entry
.updateEndTime(endTime
);
184 List
<Integer
> softIrqQuarks
= ssq
.getQuarks(Attributes
.RESOURCES
, Attributes
.SOFT_IRQS
, "*"); //$NON-NLS-1$
185 for (Integer softIrqQuark
: softIrqQuarks
) {
186 int softIrq
= Integer
.parseInt(ssq
.getAttributeName(softIrqQuark
));
187 ResourcesEntry entry
= entryMap
.get(softIrqQuark
);
189 entry
= new ResourcesEntry(softIrqQuark
, trace
, startTime
, endTime
, Type
.SOFT_IRQ
, softIrq
);
190 entryMap
.put(softIrqQuark
, entry
);
191 traceEntry
.addChild(entry
);
193 entry
.updateEndTime(endTime
);
197 if (parentTrace
.equals(getTrace())) {
200 final List
<?
extends ITimeGraphEntry
> traceEntryChildren
= traceEntry
.getChildren();
201 final long resolution
= Math
.max(1, (endTime
- ssq
.getStartTime()) / getDisplayWidth());
202 final long qStart
= start
;
203 final long qEnd
= end
;
204 queryFullStates(ssq
, qStart
, qEnd
, resolution
, monitor
, new IQueryHandler() {
206 public void handle(List
<List
<ITmfStateInterval
>> fullStates
, List
<ITmfStateInterval
> prevFullState
) {
207 for (ITimeGraphEntry child
: traceEntryChildren
) {
208 if (monitor
.isCanceled()) {
211 if (child
instanceof TimeGraphEntry
) {
212 TimeGraphEntry entry
= (TimeGraphEntry
) child
;
213 List
<ITimeEvent
> eventList
= getEventList(entry
, ssq
, fullStates
, prevFullState
, monitor
);
214 if (eventList
!= null) {
215 for (ITimeEvent event
: eventList
) {
216 entry
.addEvent(event
);
229 protected @Nullable List
<ITimeEvent
> getEventList(@NonNull TimeGraphEntry entry
, ITmfStateSystem ssq
,
230 @NonNull List
<List
<ITmfStateInterval
>> fullStates
, @Nullable List
<ITmfStateInterval
> prevFullState
, @NonNull IProgressMonitor monitor
) {
231 ResourcesEntry resourcesEntry
= (ResourcesEntry
) entry
;
232 List
<ITimeEvent
> eventList
= null;
233 int quark
= resourcesEntry
.getQuark();
235 if (resourcesEntry
.getType().equals(Type
.CPU
)) {
238 statusQuark
= ssq
.getQuarkRelative(quark
, Attributes
.STATUS
);
239 } catch (AttributeNotFoundException e
) {
241 * The sub-attribute "status" is not available. May happen
242 * if the trace does not have sched_switch events enabled.
246 eventList
= new ArrayList
<>(fullStates
.size());
247 ITmfStateInterval lastInterval
= prevFullState
== null || statusQuark
>= prevFullState
.size() ?
null : prevFullState
.get(statusQuark
);
248 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
249 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
250 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
251 if (monitor
.isCanceled()) {
254 if (statusQuark
>= fullState
.size()) {
255 /* No information on this CPU (yet?), skip it for now */
258 ITmfStateInterval statusInterval
= fullState
.get(statusQuark
);
259 int status
= statusInterval
.getStateValue().unboxInt();
260 long time
= statusInterval
.getStartTime();
261 long duration
= statusInterval
.getEndTime() - time
+ 1;
262 if (time
== lastStartTime
) {
265 if (!statusInterval
.getStateValue().isNull()) {
266 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
267 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
269 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
271 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
273 lastStartTime
= time
;
274 lastEndTime
= time
+ duration
;
276 } else if (resourcesEntry
.getType().equals(Type
.IRQ
) || resourcesEntry
.getType().equals(Type
.SOFT_IRQ
)) {
277 eventList
= new ArrayList
<>(fullStates
.size());
278 ITmfStateInterval lastInterval
= prevFullState
== null || quark
>= prevFullState
.size() ?
null : prevFullState
.get(quark
);
279 long lastStartTime
= lastInterval
== null ?
-1 : lastInterval
.getStartTime();
280 long lastEndTime
= lastInterval
== null ?
-1 : lastInterval
.getEndTime() + 1;
281 boolean lastIsNull
= lastInterval
== null ?
false : lastInterval
.getStateValue().isNull();
282 for (List
<ITmfStateInterval
> fullState
: fullStates
) {
283 if (monitor
.isCanceled()) {
286 if (quark
>= fullState
.size()) {
287 /* No information on this IRQ (yet?), skip it for now */
290 ITmfStateInterval irqInterval
= fullState
.get(quark
);
291 long time
= irqInterval
.getStartTime();
292 long duration
= irqInterval
.getEndTime() - time
+ 1;
293 if (time
== lastStartTime
) {
296 if (!irqInterval
.getStateValue().isNull()) {
297 int cpu
= irqInterval
.getStateValue().unboxInt();
298 eventList
.add(new TimeEvent(entry
, time
, duration
, cpu
));
301 if (lastEndTime
!= time
&& lastIsNull
) {
302 /* This is a special case where we want to show IRQ_ACTIVE state but we don't know the CPU (it is between two null samples) */
303 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
, -1));
305 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
308 lastStartTime
= time
;
309 lastEndTime
= time
+ duration
;