lttng: Support live updating of Control Flow view and Resources view
[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.linuxtools.internal.lttng2.kernel.core.Attributes;
24 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
25 import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
26 import org.eclipse.linuxtools.lttng2.kernel.ui.analysis.LttngKernelAnalysisModule;
27 import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
28 import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
29 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
30 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
31 import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
32 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
33 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
34 import org.eclipse.linuxtools.tmf.ui.views.timegraph.AbstractTimeGraphView;
35 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
36 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.NullTimeEvent;
37 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
38 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
39
40 /**
41 * Main implementation for the LTTng 2.0 kernel Resource view
42 *
43 * @author Patrick Tasse
44 */
45 public class ResourcesView extends AbstractTimeGraphView {
46
47 /** View ID. */
48 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$
49
50 private static final String[] FILTER_COLUMN_NAMES = new String[] {
51 Messages.ResourcesView_stateTypeName
52 };
53
54 // Timeout between updates in the build thread in ms
55 private static final long BUILD_UPDATE_TIMEOUT = 500;
56
57 // ------------------------------------------------------------------------
58 // Constructors
59 // ------------------------------------------------------------------------
60
61 /**
62 * Default constructor
63 */
64 public ResourcesView() {
65 super(ID, new ResourcesPresentationProvider());
66 setFilterColumns(FILTER_COLUMN_NAMES);
67 }
68
69 // ------------------------------------------------------------------------
70 // Internal
71 // ------------------------------------------------------------------------
72
73 @Override
74 protected String getNextText() {
75 return Messages.ResourcesView_nextResourceActionNameText;
76 }
77
78 @Override
79 protected String getNextTooltip() {
80 return Messages.ResourcesView_nextResourceActionToolTipText;
81 }
82
83 @Override
84 protected String getPrevText() {
85 return Messages.ResourcesView_previousResourceActionNameText;
86 }
87
88 @Override
89 protected String getPrevTooltip() {
90 return Messages.ResourcesView_previousResourceActionToolTipText;
91 }
92
93 @Override
94 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
95 LttngKernelAnalysisModule module = trace.getAnalysisModules(LttngKernelAnalysisModule.class).get(LttngKernelAnalysisModule.ID);
96 if (module == null) {
97 return;
98 }
99 module.schedule();
100 module.waitForInitialization();
101 ITmfStateSystem ssq = module.getStateSystem();
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 (TimeGraphEntry entry : traceEntry.getChildren()) {
178 if (monitor.isCanceled()) {
179 return;
180 }
181 List<ITimeEvent> eventList = getEventList(entry, start, endTime, resolution, monitor);
182 if (eventList != null) {
183 for (ITimeEvent event : eventList) {
184 entry.addEvent(event);
185 }
186 }
187 redraw();
188 }
189
190 start = end;
191 }
192 }
193
194 @Override
195 protected List<ITimeEvent> getEventList(TimeGraphEntry entry,
196 long startTime, long endTime, long resolution,
197 IProgressMonitor monitor) {
198 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
199 LttngKernelAnalysisModule module = resourcesEntry.getTrace().getAnalysisModules(LttngKernelAnalysisModule.class).get(LttngKernelAnalysisModule.ID);
200 if (module == null) {
201 return null;
202 }
203 ITmfStateSystem ssq = module.getStateSystem();
204 if (ssq == null) {
205 return null;
206 }
207 final long realStart = Math.max(startTime, ssq.getStartTime());
208 final long realEnd = Math.min(endTime, ssq.getCurrentEndTime() + 1);
209 if (realEnd <= realStart) {
210 return null;
211 }
212 List<ITimeEvent> eventList = null;
213 int quark = resourcesEntry.getQuark();
214
215 try {
216 if (resourcesEntry.getType().equals(Type.CPU)) {
217 int statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
218 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor);
219 eventList = new ArrayList<>(statusIntervals.size());
220 long lastEndTime = -1;
221 for (ITmfStateInterval statusInterval : statusIntervals) {
222 if (monitor.isCanceled()) {
223 return null;
224 }
225 int status = statusInterval.getStateValue().unboxInt();
226 long time = statusInterval.getStartTime();
227 long duration = statusInterval.getEndTime() - time + 1;
228 if (!statusInterval.getStateValue().isNull()) {
229 if (lastEndTime != time && lastEndTime != -1) {
230 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
231 }
232 eventList.add(new TimeEvent(entry, time, duration, status));
233 } else if (lastEndTime == -1 || time + duration >= endTime) {
234 // add null event if it intersects the start or end time
235 eventList.add(new NullTimeEvent(entry, time, duration));
236 }
237 lastEndTime = time + duration;
238 }
239 } else if (resourcesEntry.getType().equals(Type.IRQ)) {
240 List<ITmfStateInterval> irqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
241 eventList = new ArrayList<>(irqIntervals.size());
242 long lastEndTime = -1;
243 boolean lastIsNull = true;
244 for (ITmfStateInterval irqInterval : irqIntervals) {
245 if (monitor.isCanceled()) {
246 return null;
247 }
248 long time = irqInterval.getStartTime();
249 long duration = irqInterval.getEndTime() - time + 1;
250 if (!irqInterval.getStateValue().isNull()) {
251 int cpu = irqInterval.getStateValue().unboxInt();
252 eventList.add(new TimeEvent(entry, time, duration, cpu));
253 lastIsNull = false;
254 } else {
255 if (lastEndTime == -1) {
256 // add null event if it intersects the start time
257 eventList.add(new NullTimeEvent(entry, time, duration));
258 } else {
259 if (lastEndTime != time && lastIsNull) {
260 /* 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) */
261 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
262 }
263 if (time + duration >= endTime) {
264 // add null event if it intersects the end time
265 eventList.add(new NullTimeEvent(entry, time, duration));
266 }
267 }
268 lastIsNull = true;
269 }
270 lastEndTime = time + duration;
271 }
272 } else if (resourcesEntry.getType().equals(Type.SOFT_IRQ)) {
273 List<ITmfStateInterval> softIrqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
274 eventList = new ArrayList<>(softIrqIntervals.size());
275 long lastEndTime = -1;
276 boolean lastIsNull = true;
277 for (ITmfStateInterval softIrqInterval : softIrqIntervals) {
278 if (monitor.isCanceled()) {
279 return null;
280 }
281 long time = softIrqInterval.getStartTime();
282 long duration = softIrqInterval.getEndTime() - time + 1;
283 if (!softIrqInterval.getStateValue().isNull()) {
284 int cpu = softIrqInterval.getStateValue().unboxInt();
285 eventList.add(new TimeEvent(entry, time, duration, cpu));
286 } else {
287 if (lastEndTime == -1) {
288 // add null event if it intersects the start time
289 eventList.add(new NullTimeEvent(entry, time, duration));
290 } else {
291 if (lastEndTime != time && lastIsNull) {
292 /* 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) */
293 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
294 }
295 if (time + duration >= endTime) {
296 // add null event if it intersects the end time
297 eventList.add(new NullTimeEvent(entry, time, duration));
298 }
299 }
300 lastIsNull = true;
301 }
302 lastEndTime = time + duration;
303 }
304 }
305
306 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
307 e.printStackTrace();
308 } catch (StateSystemDisposedException e) {
309 /* Ignored */
310 }
311 return eventList;
312 }
313
314 }
This page took 0.03797 seconds and 5 git commands to generate.