lttng: Fix Resource View for traces without sched_switch events
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.ui / src / org / eclipse / tracecompass / internal / lttng2 / kernel / ui / views / resources / ResourcesView.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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.internal.lttng2.kernel.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.Nullable;
25 import org.eclipse.tracecompass.internal.lttng2.kernel.core.Attributes;
26 import org.eclipse.tracecompass.internal.lttng2.kernel.ui.Messages;
27 import org.eclipse.tracecompass.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
28 import org.eclipse.tracecompass.lttng2.kernel.core.analysis.kernel.LttngKernelAnalysis;
29 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
30 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
31 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
33 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
34 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
35 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
36 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
37 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
38 import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView;
39 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
40 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
41 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
42 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
43 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
44
45 /**
46 * Main implementation for the LTTng 2.0 kernel Resource view
47 *
48 * @author Patrick Tasse
49 */
50 public class ResourcesView extends AbstractTimeGraphView {
51
52 /** View ID. */
53 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$
54
55 private static final String[] FILTER_COLUMN_NAMES = new String[] {
56 Messages.ResourcesView_stateTypeName
57 };
58
59 // Timeout between updates in the build thread in ms
60 private static final long BUILD_UPDATE_TIMEOUT = 500;
61
62 // ------------------------------------------------------------------------
63 // Constructors
64 // ------------------------------------------------------------------------
65
66 /**
67 * Default constructor
68 */
69 public ResourcesView() {
70 super(ID, new ResourcesPresentationProvider());
71 setFilterColumns(FILTER_COLUMN_NAMES);
72 }
73
74 // ------------------------------------------------------------------------
75 // Internal
76 // ------------------------------------------------------------------------
77
78 @Override
79 protected String getNextText() {
80 return Messages.ResourcesView_nextResourceActionNameText;
81 }
82
83 @Override
84 protected String getNextTooltip() {
85 return Messages.ResourcesView_nextResourceActionToolTipText;
86 }
87
88 @Override
89 protected String getPrevText() {
90 return Messages.ResourcesView_previousResourceActionNameText;
91 }
92
93 @Override
94 protected String getPrevTooltip() {
95 return Messages.ResourcesView_previousResourceActionToolTipText;
96 }
97
98 @Override
99 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
100 if (trace == null) {
101 return;
102 }
103 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, LttngKernelAnalysis.ID);
104 if (ssq == null) {
105 return;
106 }
107 Comparator<ITimeGraphEntry> comparator = new Comparator<ITimeGraphEntry>() {
108 @Override
109 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
110 return ((ResourcesEntry) o1).compareTo(o2);
111 }
112 };
113
114 Map<Integer, ResourcesEntry> entryMap = new HashMap<>();
115 TimeGraphEntry traceEntry = null;
116
117 long startTime = ssq.getStartTime();
118 long start = startTime;
119 setStartTime(Math.min(getStartTime(), startTime));
120 boolean complete = false;
121 while (!complete) {
122 if (monitor.isCanceled()) {
123 return;
124 }
125 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
126 if (ssq.isCancelled()) {
127 return;
128 }
129 long end = ssq.getCurrentEndTime();
130 if (start == end && !complete) { // when complete execute one last time regardless of end time
131 continue;
132 }
133 long endTime = end + 1;
134 setEndTime(Math.max(getEndTime(), endTime));
135
136 if (traceEntry == null) {
137 traceEntry = new ResourcesEntry(trace, trace.getName(), startTime, endTime, 0);
138 traceEntry.sortChildren(comparator);
139 List<TimeGraphEntry> entryList = Collections.singletonList(traceEntry);
140 addToEntryList(parentTrace, entryList);
141 } else {
142 traceEntry.updateEndTime(endTime);
143 }
144
145 List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
146 for (Integer cpuQuark : cpuQuarks) {
147 int cpu = Integer.parseInt(ssq.getAttributeName(cpuQuark));
148 ResourcesEntry entry = entryMap.get(cpuQuark);
149 if (entry == null) {
150 entry = new ResourcesEntry(cpuQuark, trace, startTime, endTime, Type.CPU, cpu);
151 entryMap.put(cpuQuark, entry);
152 traceEntry.addChild(entry);
153 } else {
154 entry.updateEndTime(endTime);
155 }
156 }
157 List<Integer> irqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$
158 for (Integer irqQuark : irqQuarks) {
159 int irq = Integer.parseInt(ssq.getAttributeName(irqQuark));
160 ResourcesEntry entry = entryMap.get(irqQuark);
161 if (entry == null) {
162 entry = new ResourcesEntry(irqQuark, trace, startTime, endTime, Type.IRQ, irq);
163 entryMap.put(irqQuark, entry);
164 traceEntry.addChild(entry);
165 } else {
166 entry.updateEndTime(endTime);
167 }
168 }
169 List<Integer> softIrqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
170 for (Integer softIrqQuark : softIrqQuarks) {
171 int softIrq = Integer.parseInt(ssq.getAttributeName(softIrqQuark));
172 ResourcesEntry entry = entryMap.get(softIrqQuark);
173 if (entry == null) {
174 entry = new ResourcesEntry(softIrqQuark, trace, startTime, endTime, Type.SOFT_IRQ, softIrq);
175 entryMap.put(softIrqQuark, entry);
176 traceEntry.addChild(entry);
177 } else {
178 entry.updateEndTime(endTime);
179 }
180 }
181
182 if (parentTrace.equals(getTrace())) {
183 refresh();
184 }
185 long resolution = Math.max(1, (endTime - ssq.getStartTime()) / getDisplayWidth());
186 for (ITimeGraphEntry child : traceEntry.getChildren()) {
187 if (monitor.isCanceled()) {
188 return;
189 }
190 if (child instanceof TimeGraphEntry) {
191 TimeGraphEntry entry = (TimeGraphEntry) child;
192 List<ITimeEvent> eventList = getEventList(entry, start, endTime, resolution, monitor);
193 if (eventList != null) {
194 for (ITimeEvent event : eventList) {
195 entry.addEvent(event);
196 }
197 }
198 redraw();
199 }
200 }
201
202 start = end;
203 }
204 }
205
206 @Override
207 protected @Nullable List<ITimeEvent> getEventList(TimeGraphEntry entry,
208 long startTime, long endTime, long resolution,
209 IProgressMonitor monitor) {
210 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
211 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(resourcesEntry.getTrace(), LttngKernelAnalysis.ID);
212 if (ssq == null) {
213 return null;
214 }
215 final long realStart = Math.max(startTime, ssq.getStartTime());
216 final long realEnd = Math.min(endTime, ssq.getCurrentEndTime() + 1);
217 if (realEnd <= realStart) {
218 return null;
219 }
220 List<ITimeEvent> eventList = null;
221 int quark = resourcesEntry.getQuark();
222
223 try {
224 if (resourcesEntry.getType().equals(Type.CPU)) {
225 int statusQuark;
226 try {
227 statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
228 } catch (AttributeNotFoundException e) {
229 /*
230 * The sub-attribute "status" is not available. May happen
231 * if the trace does not have sched_switch events enabled.
232 */
233 return null;
234 }
235 List<ITmfStateInterval> statusIntervals = StateSystemUtils.queryHistoryRange(ssq, statusQuark, realStart, realEnd - 1, resolution, monitor);
236 eventList = new ArrayList<>(statusIntervals.size());
237 long lastEndTime = -1;
238 for (ITmfStateInterval statusInterval : statusIntervals) {
239 if (monitor.isCanceled()) {
240 return null;
241 }
242 int status = statusInterval.getStateValue().unboxInt();
243 long time = statusInterval.getStartTime();
244 long duration = statusInterval.getEndTime() - time + 1;
245 if (!statusInterval.getStateValue().isNull()) {
246 if (lastEndTime != time && lastEndTime != -1) {
247 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
248 }
249 eventList.add(new TimeEvent(entry, time, duration, status));
250 } else if (lastEndTime == -1 || time + duration >= endTime) {
251 // add null event if it intersects the start or end time
252 eventList.add(new NullTimeEvent(entry, time, duration));
253 }
254 lastEndTime = time + duration;
255 }
256 } else if (resourcesEntry.getType().equals(Type.IRQ)) {
257 List<ITmfStateInterval> irqIntervals = StateSystemUtils.queryHistoryRange(ssq, quark, realStart, realEnd - 1, resolution, monitor);
258 eventList = new ArrayList<>(irqIntervals.size());
259 long lastEndTime = -1;
260 boolean lastIsNull = true;
261 for (ITmfStateInterval irqInterval : irqIntervals) {
262 if (monitor.isCanceled()) {
263 return null;
264 }
265 long time = irqInterval.getStartTime();
266 long duration = irqInterval.getEndTime() - time + 1;
267 if (!irqInterval.getStateValue().isNull()) {
268 int cpu = irqInterval.getStateValue().unboxInt();
269 eventList.add(new TimeEvent(entry, time, duration, cpu));
270 lastIsNull = false;
271 } else {
272 if (lastEndTime == -1) {
273 // add null event if it intersects the start time
274 eventList.add(new NullTimeEvent(entry, time, duration));
275 } else {
276 if (lastEndTime != time && lastIsNull) {
277 /* 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) */
278 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
279 }
280 if (time + duration >= endTime) {
281 // add null event if it intersects the end time
282 eventList.add(new NullTimeEvent(entry, time, duration));
283 }
284 }
285 lastIsNull = true;
286 }
287 lastEndTime = time + duration;
288 }
289 } else if (resourcesEntry.getType().equals(Type.SOFT_IRQ)) {
290 List<ITmfStateInterval> softIrqIntervals = StateSystemUtils.queryHistoryRange(ssq, quark, realStart, realEnd - 1, resolution, monitor);
291 eventList = new ArrayList<>(softIrqIntervals.size());
292 long lastEndTime = -1;
293 boolean lastIsNull = true;
294 for (ITmfStateInterval softIrqInterval : softIrqIntervals) {
295 if (monitor.isCanceled()) {
296 return null;
297 }
298 long time = softIrqInterval.getStartTime();
299 long duration = softIrqInterval.getEndTime() - time + 1;
300 if (!softIrqInterval.getStateValue().isNull()) {
301 int cpu = softIrqInterval.getStateValue().unboxInt();
302 eventList.add(new TimeEvent(entry, time, duration, cpu));
303 } else {
304 if (lastEndTime == -1) {
305 // add null event if it intersects the start time
306 eventList.add(new NullTimeEvent(entry, time, duration));
307 } else {
308 if (lastEndTime != time && lastIsNull) {
309 /* 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) */
310 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
311 }
312 if (time + duration >= endTime) {
313 // add null event if it intersects the end time
314 eventList.add(new NullTimeEvent(entry, time, duration));
315 }
316 }
317 lastIsNull = true;
318 }
319 lastEndTime = time + duration;
320 }
321 }
322
323 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
324 e.printStackTrace();
325 } catch (StateSystemDisposedException e) {
326 /* Ignored */
327 }
328 return eventList;
329 }
330
331 }
This page took 0.037139 seconds and 5 git commands to generate.