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
CommitLineData
6151d86c 1/*******************************************************************************
1cf25311 2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
6151d86c
PT
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
4999a196 11 * Geneviève Bastien - Move code to provide base classes for time graph views
6151d86c
PT
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources;
15
16import java.util.ArrayList;
1cf25311
PT
17import java.util.Collections;
18import java.util.HashMap;
6151d86c 19import java.util.List;
1cf25311 20import java.util.Map;
6151d86c
PT
21
22import org.eclipse.core.runtime.IProgressMonitor;
d2120fb6 23import org.eclipse.jdt.annotation.Nullable;
6151d86c
PT
24import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
25import org.eclipse.linuxtools.internal.lttng2.kernel.ui.Messages;
26import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesEntry.Type;
e3366401 27import org.eclipse.linuxtools.lttng2.kernel.core.analysis.LttngKernelAnalysisModule;
bcec0116
AM
28import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
29import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException;
30import org.eclipse.linuxtools.statesystem.core.exceptions.StateSystemDisposedException;
31import org.eclipse.linuxtools.statesystem.core.exceptions.StateValueTypeException;
32import org.eclipse.linuxtools.statesystem.core.exceptions.TimeRangeException;
33import org.eclipse.linuxtools.statesystem.core.interval.ITmfStateInterval;
72221aa4 34import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
6151d86c 35import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
4999a196 36import org.eclipse.linuxtools.tmf.ui.views.timegraph.AbstractTimeGraphView;
6151d86c 37import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
a3188982 38import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
1d46dc38 39import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.NullTimeEvent;
6151d86c 40import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
4999a196 41import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
6151d86c
PT
42
43/**
44 * Main implementation for the LTTng 2.0 kernel Resource view
45 *
46 * @author Patrick Tasse
47 */
4999a196 48public class ResourcesView extends AbstractTimeGraphView {
6151d86c
PT
49
50 /** View ID. */
51 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$
52
4999a196 53 private static final String[] FILTER_COLUMN_NAMES = new String[] {
747adf5c 54 Messages.ResourcesView_stateTypeName
4999a196 55 };
6151d86c 56
1cf25311
PT
57 // Timeout between updates in the build thread in ms
58 private static final long BUILD_UPDATE_TIMEOUT = 500;
59
6151d86c
PT
60 // ------------------------------------------------------------------------
61 // Constructors
62 // ------------------------------------------------------------------------
63
64 /**
65 * Default constructor
66 */
67 public ResourcesView() {
747adf5c
PT
68 super(ID, new ResourcesPresentationProvider());
69 setFilterColumns(FILTER_COLUMN_NAMES);
6151d86c
PT
70 }
71
1cf25311
PT
72 // ------------------------------------------------------------------------
73 // Internal
74 // ------------------------------------------------------------------------
75
6151d86c 76 @Override
4999a196
GB
77 protected String getNextText() {
78 return Messages.ResourcesView_nextResourceActionNameText;
6151d86c
PT
79 }
80
6151d86c 81 @Override
4999a196
GB
82 protected String getNextTooltip() {
83 return Messages.ResourcesView_nextResourceActionToolTipText;
fec1ac0b
BH
84 }
85
4999a196
GB
86 @Override
87 protected String getPrevText() {
88 return Messages.ResourcesView_previousResourceActionNameText;
6151d86c
PT
89 }
90
4999a196
GB
91 @Override
92 protected String getPrevTooltip() {
93 return Messages.ResourcesView_previousResourceActionToolTipText;
6151d86c
PT
94 }
95
4999a196 96 @Override
1cf25311 97 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
72221aa4 98 if (trace == null) {
1cf25311
PT
99 return;
100 }
72221aa4 101 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, LttngKernelAnalysisModule.ID);
1cf25311
PT
102 if (ssq == null) {
103 return;
104 }
fec1ac0b 105
1cf25311
PT
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) {
faa38350
PT
114 if (monitor.isCanceled()) {
115 return;
116 }
1cf25311
PT
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);
6151d86c 146 }
1cf25311
PT
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);
6151d86c 158 }
1cf25311
PT
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);
6151d86c
PT
170 }
171 }
4999a196 172
1cf25311
PT
173 if (parentTrace.equals(getTrace())) {
174 refresh();
faa38350 175 }
1cf25311 176 long resolution = Math.max(1, (endTime - ssq.getStartTime()) / getDisplayWidth());
a3188982 177 for (ITimeGraphEntry child : traceEntry.getChildren()) {
1cf25311
PT
178 if (monitor.isCanceled()) {
179 return;
180 }
a3188982
PT
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 }
1cf25311 188 }
a3188982 189 redraw();
1cf25311 190 }
6151d86c 191 }
1cf25311
PT
192
193 start = end;
6151d86c
PT
194 }
195 }
196
4999a196 197 @Override
d2120fb6 198 protected @Nullable List<ITimeEvent> getEventList(TimeGraphEntry entry,
4999a196 199 long startTime, long endTime, long resolution,
6151d86c 200 IProgressMonitor monitor) {
1d46dc38 201 ResourcesEntry resourcesEntry = (ResourcesEntry) entry;
72221aa4 202 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(resourcesEntry.getTrace(), LttngKernelAnalysisModule.ID);
4bc53929
GB
203 if (ssq == null) {
204 return null;
205 }
41b5c37f
AM
206 final long realStart = Math.max(startTime, ssq.getStartTime());
207 final long realEnd = Math.min(endTime, ssq.getCurrentEndTime() + 1);
208 if (realEnd <= realStart) {
6151d86c
PT
209 return null;
210 }
211 List<ITimeEvent> eventList = null;
4999a196
GB
212 int quark = resourcesEntry.getQuark();
213
6151d86c 214 try {
4999a196 215 if (resourcesEntry.getType().equals(Type.CPU)) {
6151d86c 216 int statusQuark = ssq.getQuarkRelative(quark, Attributes.STATUS);
41b5c37f 217 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(statusQuark, realStart, realEnd - 1, resolution, monitor);
e0838ca1 218 eventList = new ArrayList<>(statusIntervals.size());
6151d86c
PT
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 }
4999a196 231 eventList.add(new TimeEvent(entry, time, duration, status));
1d46dc38
PT
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));
6151d86c 235 }
1d46dc38 236 lastEndTime = time + duration;
6151d86c 237 }
4999a196 238 } else if (resourcesEntry.getType().equals(Type.IRQ)) {
41b5c37f 239 List<ITmfStateInterval> irqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
e0838ca1 240 eventList = new ArrayList<>(irqIntervals.size());
6151d86c
PT
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();
4999a196 251 eventList.add(new TimeEvent(entry, time, duration, cpu));
6151d86c
PT
252 lastIsNull = false;
253 } else {
1d46dc38
PT
254 if (lastEndTime == -1) {
255 // add null event if it intersects the start time
256 eventList.add(new NullTimeEvent(entry, time, duration));
4999a196 257 } else {
1d46dc38
PT
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 }
6151d86c
PT
266 }
267 lastIsNull = true;
268 }
269 lastEndTime = time + duration;
270 }
4999a196 271 } else if (resourcesEntry.getType().equals(Type.SOFT_IRQ)) {
41b5c37f 272 List<ITmfStateInterval> softIrqIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
e0838ca1 273 eventList = new ArrayList<>(softIrqIntervals.size());
6151d86c
PT
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();
4999a196 284 eventList.add(new TimeEvent(entry, time, duration, cpu));
6151d86c 285 } else {
1d46dc38
PT
286 if (lastEndTime == -1) {
287 // add null event if it intersects the start time
288 eventList.add(new NullTimeEvent(entry, time, duration));
4999a196 289 } else {
1d46dc38
PT
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 }
6151d86c
PT
298 }
299 lastIsNull = true;
300 }
301 lastEndTime = time + duration;
302 }
303 }
4999a196 304
1cf25311 305 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
6151d86c 306 e.printStackTrace();
96345c5a
AM
307 } catch (StateSystemDisposedException e) {
308 /* Ignored */
6151d86c
PT
309 }
310 return eventList;
311 }
312
6151d86c 313}
This page took 0.060141 seconds and 5 git commands to generate.