lttng: Bug 487163: Resources view traces are unsorted
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / analysis / os / linux / ui / views / resources / ResourcesView.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2016 Ericsson, École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 * Geneviève Bastien - Move code to provide base classes for time graph views
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.analysis.os.linux.ui.views.resources;
15
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;
21 import java.util.Map;
22
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;
41
42 /**
43 * Main implementation for the LTTng 2.0 kernel Resource view
44 *
45 * @author Patrick Tasse
46 */
47 public class ResourcesView extends AbstractStateSystemTimeGraphView {
48
49 /** View ID. */
50 public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
51
52 private static final String[] FILTER_COLUMN_NAMES = new String[] {
53 Messages.ResourcesView_stateTypeName
54 };
55
56 // Timeout between updates in the build thread in ms
57 private static final long BUILD_UPDATE_TIMEOUT = 500;
58
59 // ------------------------------------------------------------------------
60 // Constructors
61 // ------------------------------------------------------------------------
62
63 /**
64 * Default constructor
65 */
66 public ResourcesView() {
67 super(ID, new ResourcesPresentationProvider());
68 setFilterColumns(FILTER_COLUMN_NAMES);
69 setFilterLabelProvider(new ResourcesFilterLabelProvider());
70 setEntryComparator(new ResourcesEntryComparator());
71 }
72
73 private static class ResourcesEntryComparator implements Comparator<ITimeGraphEntry> {
74 @Override
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());
81 }
82 /* sort resource entries by their defined order */
83 return entry1.compareTo(entry2);
84 }
85 }
86
87 private static class ResourcesFilterLabelProvider extends TreeLabelProvider {
88 @Override
89 public String getColumnText(Object element, int columnIndex) {
90 ResourcesEntry entry = (ResourcesEntry) element;
91 if (columnIndex == 0) {
92 return entry.getName();
93 }
94 return ""; //$NON-NLS-1$
95 }
96
97 }
98
99 // ------------------------------------------------------------------------
100 // Internal
101 // ------------------------------------------------------------------------
102
103 @Override
104 protected String getNextText() {
105 return Messages.ResourcesView_nextResourceActionNameText;
106 }
107
108 @Override
109 protected String getNextTooltip() {
110 return Messages.ResourcesView_nextResourceActionToolTipText;
111 }
112
113 @Override
114 protected String getPrevText() {
115 return Messages.ResourcesView_previousResourceActionNameText;
116 }
117
118 @Override
119 protected String getPrevTooltip() {
120 return Messages.ResourcesView_previousResourceActionToolTipText;
121 }
122
123 @Override
124 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, final IProgressMonitor monitor) {
125 final ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
126 if (ssq == null) {
127 return;
128 }
129
130 Map<Integer, ResourcesEntry> entryMap = new HashMap<>();
131 TimeGraphEntry traceEntry = null;
132
133 long startTime = ssq.getStartTime();
134 long start = startTime;
135 setStartTime(Math.min(getStartTime(), startTime));
136 boolean complete = false;
137 while (!complete) {
138 if (monitor.isCanceled()) {
139 return;
140 }
141 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
142 if (ssq.isCancelled()) {
143 return;
144 }
145 long end = ssq.getCurrentEndTime();
146 if (start == end && !complete) { // when complete execute one last time regardless of end time
147 continue;
148 }
149 long endTime = end + 1;
150 setEndTime(Math.max(getEndTime(), endTime));
151
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);
156 } else {
157 traceEntry.updateEndTime(endTime);
158 }
159
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);
164 if (entry == null) {
165 entry = new ResourcesEntry(cpuQuark, trace, startTime, endTime, Type.CPU, cpu);
166 entryMap.put(cpuQuark, entry);
167 traceEntry.addChild(entry);
168 } else {
169 entry.updateEndTime(endTime);
170 }
171 }
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);
176 if (entry == null) {
177 entry = new ResourcesEntry(irqQuark, trace, startTime, endTime, Type.IRQ, irq);
178 entryMap.put(irqQuark, entry);
179 traceEntry.addChild(entry);
180 } else {
181 entry.updateEndTime(endTime);
182 }
183 }
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);
188 if (entry == null) {
189 entry = new ResourcesEntry(softIrqQuark, trace, startTime, endTime, Type.SOFT_IRQ, softIrq);
190 entryMap.put(softIrqQuark, entry);
191 traceEntry.addChild(entry);
192 } else {
193 entry.updateEndTime(endTime);
194 }
195 }
196
197 if (parentTrace.equals(getTrace())) {
198 refresh();
199 }
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() {
205 @Override
206 public void handle(List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState) {
207 for (ITimeGraphEntry child : traceEntryChildren) {
208 if (monitor.isCanceled()) {
209 return;
210 }
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);
217 }
218 }
219 }
220 }
221 }
222 });
223
224 start = end;
225 }
226 }
227
228 @Override
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();
234
235 if (resourcesEntry.getType().equals(Type.CPU)) {
236 int statusQuark;
237 try {
238 statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
239 } catch (AttributeNotFoundException e) {
240 /*
241 * The sub-attribute "status" is not available. May happen
242 * if the trace does not have sched_switch events enabled.
243 */
244 return null;
245 }
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()) {
252 return null;
253 }
254 if (statusQuark >= fullState.size()) {
255 /* No information on this CPU (yet?), skip it for now */
256 continue;
257 }
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) {
263 continue;
264 }
265 if (!statusInterval.getStateValue().isNull()) {
266 if (lastEndTime != time && lastEndTime != -1) {
267 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
268 }
269 eventList.add(new TimeEvent(entry, time, duration, status));
270 } else {
271 eventList.add(new NullTimeEvent(entry, time, duration));
272 }
273 lastStartTime = time;
274 lastEndTime = time + duration;
275 }
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()) {
284 return null;
285 }
286 if (quark >= fullState.size()) {
287 /* No information on this IRQ (yet?), skip it for now */
288 continue;
289 }
290 ITmfStateInterval irqInterval = fullState.get(quark);
291 long time = irqInterval.getStartTime();
292 long duration = irqInterval.getEndTime() - time + 1;
293 if (time == lastStartTime) {
294 continue;
295 }
296 if (!irqInterval.getStateValue().isNull()) {
297 int cpu = irqInterval.getStateValue().unboxInt();
298 eventList.add(new TimeEvent(entry, time, duration, cpu));
299 lastIsNull = false;
300 } else {
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));
304 }
305 eventList.add(new NullTimeEvent(entry, time, duration));
306 lastIsNull = true;
307 }
308 lastStartTime = time;
309 lastEndTime = time + duration;
310 }
311 }
312
313 return eventList;
314 }
315
316 }
This page took 0.038833 seconds and 6 git commands to generate.