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