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