analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / analysis / os / linux / ui / views / resources / ResourcesView.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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.analysis.os.linux.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.analysis.os.linux.core.kernelanalysis.Attributes;
26 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.KernelAnalysisModule;
27 import org.eclipse.tracecompass.analysis.os.linux.ui.views.resources.ResourcesEntry.Type;
28 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Messages;
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.tracecompass.analysis.os.linux.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 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
101 if (ssq == null) {
102 return;
103 }
104 Comparator<ITimeGraphEntry> comparator = new Comparator<ITimeGraphEntry>() {
105 @Override
106 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
107 return ((ResourcesEntry) o1).compareTo(o2);
108 }
109 };
110
111 Map<Integer, ResourcesEntry> entryMap = new HashMap<>();
112 TimeGraphEntry traceEntry = null;
113
114 long startTime = ssq.getStartTime();
115 long start = startTime;
116 setStartTime(Math.min(getStartTime(), startTime));
117 boolean complete = false;
118 while (!complete) {
119 if (monitor.isCanceled()) {
120 return;
121 }
122 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
123 if (ssq.isCancelled()) {
124 return;
125 }
126 long end = ssq.getCurrentEndTime();
127 if (start == end && !complete) { // when complete execute one last time regardless of end time
128 continue;
129 }
130 long endTime = end + 1;
131 setEndTime(Math.max(getEndTime(), endTime));
132
133 if (traceEntry == null) {
134 traceEntry = new ResourcesEntry(trace, trace.getName(), startTime, endTime, 0);
135 traceEntry.sortChildren(comparator);
136 List<TimeGraphEntry> entryList = Collections.singletonList(traceEntry);
137 addToEntryList(parentTrace, entryList);
138 } else {
139 traceEntry.updateEndTime(endTime);
140 }
141
142 List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
143 for (Integer cpuQuark : cpuQuarks) {
144 int cpu = Integer.parseInt(ssq.getAttributeName(cpuQuark));
145 ResourcesEntry entry = entryMap.get(cpuQuark);
146 if (entry == null) {
147 entry = new ResourcesEntry(cpuQuark, trace, startTime, endTime, Type.CPU, cpu);
148 entryMap.put(cpuQuark, entry);
149 traceEntry.addChild(entry);
150 } else {
151 entry.updateEndTime(endTime);
152 }
153 }
154 List<Integer> irqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.IRQS, "*"); //$NON-NLS-1$
155 for (Integer irqQuark : irqQuarks) {
156 int irq = Integer.parseInt(ssq.getAttributeName(irqQuark));
157 ResourcesEntry entry = entryMap.get(irqQuark);
158 if (entry == null) {
159 entry = new ResourcesEntry(irqQuark, trace, startTime, endTime, Type.IRQ, irq);
160 entryMap.put(irqQuark, entry);
161 traceEntry.addChild(entry);
162 } else {
163 entry.updateEndTime(endTime);
164 }
165 }
166 List<Integer> softIrqQuarks = ssq.getQuarks(Attributes.RESOURCES, Attributes.SOFT_IRQS, "*"); //$NON-NLS-1$
167 for (Integer softIrqQuark : softIrqQuarks) {
168 int softIrq = Integer.parseInt(ssq.getAttributeName(softIrqQuark));
169 ResourcesEntry entry = entryMap.get(softIrqQuark);
170 if (entry == null) {
171 entry = new ResourcesEntry(softIrqQuark, trace, startTime, endTime, Type.SOFT_IRQ, softIrq);
172 entryMap.put(softIrqQuark, entry);
173 traceEntry.addChild(entry);
174 } else {
175 entry.updateEndTime(endTime);
176 }
177 }
178
179 if (parentTrace.equals(getTrace())) {
180 refresh();
181 }
182 long resolution = Math.max(1, (endTime - ssq.getStartTime()) / getDisplayWidth());
183 for (ITimeGraphEntry child : traceEntry.getChildren()) {
184 if (monitor.isCanceled()) {
185 return;
186 }
187 if (child instanceof TimeGraphEntry) {
188 TimeGraphEntry entry = (TimeGraphEntry) child;
189 List<ITimeEvent> eventList = getEventList(entry, start, endTime, resolution, monitor);
190 if (eventList != null) {
191 for (ITimeEvent event : eventList) {
192 entry.addEvent(event);
193 }
194 }
195 redraw();
196 }
197 }
198
199 start = end;
200 }
201 }
202
203 @Override
204 protected @Nullable List<ITimeEvent> getEventList(TimeGraphEntry entry,
205 long startTime, long endTime, long resolution,
206 IProgressMonitor monitor) {
207 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
208 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(resourcesEntry.getTrace(), KernelAnalysisModule.ID);
209 if (ssq == null) {
210 return null;
211 }
212 final long realStart = Math.max(startTime, ssq.getStartTime());
213 final long realEnd = Math.min(endTime, ssq.getCurrentEndTime() + 1);
214 if (realEnd <= realStart) {
215 return null;
216 }
217 List<ITimeEvent> eventList = null;
218 int quark = resourcesEntry.getQuark();
219
220 try {
221 if (resourcesEntry.getType().equals(Type.CPU)) {
222 int statusQuark;
223 try {
224 statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
225 } catch (AttributeNotFoundException e) {
226 /*
227 * The sub-attribute "status" is not available. May happen
228 * if the trace does not have sched_switch events enabled.
229 */
230 return null;
231 }
232 List<ITmfStateInterval> statusIntervals = StateSystemUtils.queryHistoryRange(ssq, statusQuark, realStart, realEnd - 1, resolution, monitor);
233 eventList = new ArrayList<>(statusIntervals.size());
234 long lastEndTime = -1;
235 for (ITmfStateInterval statusInterval : statusIntervals) {
236 if (monitor.isCanceled()) {
237 return null;
238 }
239 int status = statusInterval.getStateValue().unboxInt();
240 long time = statusInterval.getStartTime();
241 long duration = statusInterval.getEndTime() - time + 1;
242 if (!statusInterval.getStateValue().isNull()) {
243 if (lastEndTime != time && lastEndTime != -1) {
244 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
245 }
246 eventList.add(new TimeEvent(entry, time, duration, status));
247 } else if (lastEndTime == -1 || time + duration >= endTime) {
248 // add null event if it intersects the start or end time
249 eventList.add(new NullTimeEvent(entry, time, duration));
250 }
251 lastEndTime = time + duration;
252 }
253 } else if (resourcesEntry.getType().equals(Type.IRQ)) {
254 List<ITmfStateInterval> irqIntervals = StateSystemUtils.queryHistoryRange(ssq, quark, realStart, realEnd - 1, resolution, monitor);
255 eventList = new ArrayList<>(irqIntervals.size());
256 long lastEndTime = -1;
257 boolean lastIsNull = true;
258 for (ITmfStateInterval irqInterval : irqIntervals) {
259 if (monitor.isCanceled()) {
260 return null;
261 }
262 long time = irqInterval.getStartTime();
263 long duration = irqInterval.getEndTime() - time + 1;
264 if (!irqInterval.getStateValue().isNull()) {
265 int cpu = irqInterval.getStateValue().unboxInt();
266 eventList.add(new TimeEvent(entry, time, duration, cpu));
267 lastIsNull = false;
268 } else {
269 if (lastEndTime == -1) {
270 // add null event if it intersects the start time
271 eventList.add(new NullTimeEvent(entry, time, duration));
272 } else {
273 if (lastEndTime != time && lastIsNull) {
274 /* 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) */
275 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
276 }
277 if (time + duration >= endTime) {
278 // add null event if it intersects the end time
279 eventList.add(new NullTimeEvent(entry, time, duration));
280 }
281 }
282 lastIsNull = true;
283 }
284 lastEndTime = time + duration;
285 }
286 } else if (resourcesEntry.getType().equals(Type.SOFT_IRQ)) {
287 List<ITmfStateInterval> softIrqIntervals = StateSystemUtils.queryHistoryRange(ssq, quark, realStart, realEnd - 1, resolution, monitor);
288 eventList = new ArrayList<>(softIrqIntervals.size());
289 long lastEndTime = -1;
290 boolean lastIsNull = true;
291 for (ITmfStateInterval softIrqInterval : softIrqIntervals) {
292 if (monitor.isCanceled()) {
293 return null;
294 }
295 long time = softIrqInterval.getStartTime();
296 long duration = softIrqInterval.getEndTime() - time + 1;
297 if (!softIrqInterval.getStateValue().isNull()) {
298 int cpu = softIrqInterval.getStateValue().unboxInt();
299 eventList.add(new TimeEvent(entry, time, duration, cpu));
300 } else {
301 if (lastEndTime == -1) {
302 // add null event if it intersects the start time
303 eventList.add(new NullTimeEvent(entry, time, duration));
304 } else {
305 if (lastEndTime != time && lastIsNull) {
306 /* 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) */
307 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime, -1));
308 }
309 if (time + duration >= endTime) {
310 // add null event if it intersects the end time
311 eventList.add(new NullTimeEvent(entry, time, duration));
312 }
313 }
314 lastIsNull = true;
315 }
316 lastEndTime = time + duration;
317 }
318 }
319
320 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
321 e.printStackTrace();
322 } catch (StateSystemDisposedException e) {
323 /* Ignored */
324 }
325 return eventList;
326 }
327
328 }
This page took 0.052748 seconds and 5 git commands to generate.