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