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
CommitLineData
6151d86c 1/*******************************************************************************
263c3747 2 * Copyright (c) 2012, 2016 Ericsson, École Polytechnique de Montréal
6151d86c
PT
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
4999a196 11 * Geneviève Bastien - Move code to provide base classes for time graph views
6151d86c
PT
12 *******************************************************************************/
13
e363eae1 14package org.eclipse.tracecompass.analysis.os.linux.ui.views.resources;
6151d86c
PT
15
16import java.util.ArrayList;
1cf25311 17import java.util.Collections;
9ba941e9 18import java.util.Comparator;
1cf25311 19import java.util.HashMap;
6151d86c 20import java.util.List;
1cf25311 21import java.util.Map;
6151d86c
PT
22
23import org.eclipse.core.runtime.IProgressMonitor;
8213a0c0 24import org.eclipse.jdt.annotation.NonNull;
d2120fb6 25import org.eclipse.jdt.annotation.Nullable;
e363eae1 26import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.Attributes;
6d16f5a9 27import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.KernelAnalysisModule;
e363eae1
AM
28import org.eclipse.tracecompass.analysis.os.linux.ui.views.resources.ResourcesEntry.Type;
29import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Messages;
e894a508
AM
30import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
31import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
e894a508 32import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
2bdf0193
AM
33import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
34import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
8213a0c0 35import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractStateSystemTimeGraphView;
2bdf0193
AM
36import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
37import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
38import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
39import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
40import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
6151d86c
PT
41
42/**
43 * Main implementation for the LTTng 2.0 kernel Resource view
44 *
45 * @author Patrick Tasse
46 */
8213a0c0 47public class ResourcesView extends AbstractStateSystemTimeGraphView {
6151d86c
PT
48
49 /** View ID. */
e363eae1 50 public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.resources"; //$NON-NLS-1$
6151d86c 51
4999a196 52 private static final String[] FILTER_COLUMN_NAMES = new String[] {
747adf5c 53 Messages.ResourcesView_stateTypeName
4999a196 54 };
6151d86c 55
1cf25311
PT
56 // Timeout between updates in the build thread in ms
57 private static final long BUILD_UPDATE_TIMEOUT = 500;
58
6151d86c
PT
59 // ------------------------------------------------------------------------
60 // Constructors
61 // ------------------------------------------------------------------------
62
63 /**
64 * Default constructor
65 */
66 public ResourcesView() {
747adf5c
PT
67 super(ID, new ResourcesPresentationProvider());
68 setFilterColumns(FILTER_COLUMN_NAMES);
bb447fcb 69 setFilterLabelProvider(new ResourcesFilterLabelProvider());
263c3747
PT
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 }
bb447fcb
PT
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
6151d86c
PT
97 }
98
1cf25311
PT
99 // ------------------------------------------------------------------------
100 // Internal
101 // ------------------------------------------------------------------------
102
6151d86c 103 @Override
4999a196
GB
104 protected String getNextText() {
105 return Messages.ResourcesView_nextResourceActionNameText;
6151d86c
PT
106 }
107
6151d86c 108 @Override
4999a196
GB
109 protected String getNextTooltip() {
110 return Messages.ResourcesView_nextResourceActionToolTipText;
fec1ac0b
BH
111 }
112
4999a196
GB
113 @Override
114 protected String getPrevText() {
115 return Messages.ResourcesView_previousResourceActionNameText;
6151d86c
PT
116 }
117
4999a196
GB
118 @Override
119 protected String getPrevTooltip() {
120 return Messages.ResourcesView_previousResourceActionToolTipText;
6151d86c
PT
121 }
122
4999a196 123 @Override
8213a0c0
PT
124 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, final IProgressMonitor monitor) {
125 final ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
1cf25311
PT
126 if (ssq == null) {
127 return;
128 }
fec1ac0b 129
1cf25311
PT
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) {
faa38350
PT
138 if (monitor.isCanceled()) {
139 return;
140 }
1cf25311
PT
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);
8213a0c0 155 addToEntryList(parentTrace, ssq, entryList);
1cf25311
PT
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);
6151d86c 170 }
1cf25311
PT
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);
6151d86c 182 }
1cf25311
PT
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);
6151d86c
PT
194 }
195 }
4999a196 196
1cf25311
PT
197 if (parentTrace.equals(getTrace())) {
198 refresh();
faa38350 199 }
8213a0c0
PT
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 }
a3188982 219 }
1cf25311
PT
220 }
221 }
8213a0c0 222 });
1cf25311
PT
223
224 start = end;
6151d86c
PT
225 }
226 }
227
4999a196 228 @Override
8213a0c0
PT
229 protected @Nullable List<ITimeEvent> getEventList(@NonNull TimeGraphEntry entry, ITmfStateSystem ssq,
230 @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) {
1d46dc38 231 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
6151d86c 232 List<ITimeEvent> eventList = null;
4999a196
GB
233 int quark = resourcesEntry.getQuark();
234
8213a0c0
PT
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()) {
8f9ad413
AM
252 return null;
253 }
8213a0c0 254 if (statusQuark >= fullState.size()) {
0bacba1a 255 /* No information on this CPU (yet?), skip it for now */
8213a0c0 256 continue;
6151d86c 257 }
8213a0c0
PT
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;
6151d86c 264 }
8213a0c0
PT
265 if (!statusInterval.getStateValue().isNull()) {
266 if (lastEndTime != time && lastEndTime != -1) {
267 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
6151d86c 268 }
8213a0c0
PT
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());
0bacba1a 278 ITmfStateInterval lastInterval = prevFullState == null || quark >= prevFullState.size() ? null : prevFullState.get(quark);
8213a0c0
PT
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 }
0bacba1a
PT
286 if (quark >= fullState.size()) {
287 /* No information on this IRQ (yet?), skip it for now */
288 continue;
289 }
8213a0c0
PT
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));
6151d86c 304 }
8213a0c0
PT
305 eventList.add(new NullTimeEvent(entry, time, duration));
306 lastIsNull = true;
6151d86c 307 }
8213a0c0
PT
308 lastStartTime = time;
309 lastEndTime = time + duration;
6151d86c 310 }
6151d86c 311 }
8213a0c0 312
6151d86c
PT
313 return eventList;
314 }
315
6151d86c 316}
This page took 0.082521 seconds and 5 git commands to generate.