Revert "TMF: Fix behavior of XML time graph views with experiments"
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / ui / views / timegraph / XmlTimeGraphView.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 É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 * Florian Wininger - Initial API and implementation
11 * Geneviève Bastien - Review of the initial implementation
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.timegraph;
15
16 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.TreeSet;
28
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.eclipse.jface.util.IPropertyChangeListener;
33 import org.eclipse.jface.util.PropertyChangeEvent;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
36 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.ITmfXmlStateAttribute;
37 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.readonly.TmfXmlReadOnlyModelFactory;
38 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
39 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils;
40 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
41 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator;
42 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
43 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.XmlViewInfo;
44 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.timegraph.XmlEntry.EntryDisplayType;
45 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
46 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
47 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
48 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
49 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
50 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
51 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
52 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
53 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
54 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
55 import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView;
56 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2;
57 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
58 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
59 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
60 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
61 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
62 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
63 import org.w3c.dom.Element;
64
65 import com.google.common.collect.Iterables;
66
67 /**
68 * This view displays state system data in a time graph view. It uses an XML
69 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
70 * element defines which entries from the state system will be shown and also
71 * gives additional information on the presentation of the view (states, colors,
72 * etc)
73 *
74 * @author Florian Wininger
75 */
76 public class XmlTimeGraphView extends AbstractTimeGraphView {
77
78 /** View ID. */
79 public static final @NonNull String ID = "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
80
81 private static final String[] DEFAULT_COLUMN_NAMES = new String[] {
82 Messages.XmlTimeGraphView_ColumnName,
83 Messages.XmlTimeGraphView_ColumnId,
84 Messages.XmlTimeGraphView_ColumnParentId,
85 };
86
87 private static final String[] DEFAULT_FILTER_COLUMN_NAMES = new String[] {
88 Messages.XmlTimeGraphView_ColumnName,
89 Messages.XmlTimeGraphView_ColumnId
90 };
91
92 /** The relative weight of the sash */
93 private static final int[] fWeight = { 1, 2 };
94
95 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
96 private static final @NonNull String SPLIT_STRING = "/"; //$NON-NLS-1$
97
98 private final @NonNull XmlViewInfo fViewInfo = new XmlViewInfo(ID);
99 private final ITmfXmlModelFactory fFactory;
100
101 // ------------------------------------------------------------------------
102 // Constructors
103 // ------------------------------------------------------------------------
104
105 /**
106 * Default constructor
107 */
108 public XmlTimeGraphView() {
109 super(ID, new XmlPresentationProvider());
110 setWeight(fWeight);
111 setTreeColumns(DEFAULT_COLUMN_NAMES);
112 setTreeLabelProvider(new XmlTreeLabelProvider());
113 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES);
114 setFilterLabelProvider(new XmlTreeLabelProvider());
115 setEntryComparator(new XmlEntryComparator());
116 this.addPartPropertyListener(new IPropertyChangeListener() {
117 @Override
118 public void propertyChange(PropertyChangeEvent event) {
119 if (event.getProperty().equals(TmfXmlUiStrings.XML_OUTPUT_DATA)) {
120 Object newValue = event.getNewValue();
121 if (newValue instanceof String) {
122 String data = (String) newValue;
123 fViewInfo.setViewData(data);
124 loadNewXmlView();
125 }
126 }
127 }
128 });
129
130 fFactory = TmfXmlReadOnlyModelFactory.getInstance();
131 }
132
133 private void loadNewXmlView() {
134 rebuild();
135 }
136
137 private void setViewTitle(final String title) {
138 Display.getDefault().asyncExec(new Runnable() {
139 @Override
140 public void run() {
141 setPartName(title);
142 }
143 });
144
145 }
146
147 @Override
148 protected String getNextText() {
149 return Messages.XmlTimeGraphView_NextText;
150 }
151
152 @Override
153 protected String getNextTooltip() {
154 return Messages.XmlTimeGraphView_NextTooltip;
155 }
156
157 @Override
158 protected String getPrevText() {
159 return Messages.XmlTimeGraphView_PreviousText;
160 }
161
162 @Override
163 protected String getPrevTooltip() {
164 return Messages.XmlTimeGraphView_PreviousInterval;
165 }
166
167 /**
168 * Default label provider, it shows name, id and parent columns
169 *
170 * TODO: There should be a way to define columns in the XML
171 */
172 private static class XmlTreeLabelProvider extends TreeLabelProvider {
173
174 @Override
175 public String getColumnText(Object element, int columnIndex) {
176 XmlEntry entry = (XmlEntry) element;
177
178 if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnName)) {
179 return entry.getName();
180 } else if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnId)) {
181 return entry.getId();
182 } else if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnParentId)) {
183 return entry.getParentId();
184 }
185 return EMPTY_STRING;
186 }
187
188 }
189
190 private static class XmlEntryComparator implements Comparator<ITimeGraphEntry> {
191
192 @Override
193 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
194
195 int result = 0;
196
197 if ((o1 instanceof XmlEntry) && (o2 instanceof XmlEntry)) {
198 XmlEntry entry1 = (XmlEntry) o1;
199 XmlEntry entry2 = (XmlEntry) o2;
200 result = entry1.getTrace().getStartTime().compareTo(entry2.getTrace().getStartTime());
201 if (result == 0) {
202 result = entry1.getTrace().getName().compareTo(entry2.getTrace().getName());
203 }
204 if (result == 0) {
205 result = entry1.getName().compareTo(entry2.getName());
206 }
207 }
208
209 if (result == 0) {
210 result = o1.getStartTime() < o2.getStartTime() ? -1 : o1.getStartTime() > o2.getStartTime() ? 1 : 0;
211 }
212
213 return result;
214 }
215 }
216
217 // ------------------------------------------------------------------------
218 // Internal
219 // ------------------------------------------------------------------------
220
221 @Override
222 protected void buildEntryList(ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
223
224 /*
225 * Get the view element from the XML file. If the element can't be
226 * found, return.
227 */
228 Element viewElement = fViewInfo.getViewElement(TmfXmlUiStrings.TIME_GRAPH_VIEW);
229 if (viewElement == null) {
230 return;
231 }
232 ITimeGraphPresentationProvider2 pres = this.getPresentationProvider();
233 if (pres instanceof XmlPresentationProvider) {
234 /*
235 * TODO: Each entry of a line could have their own states/color.
236 * That will require an update to the presentation provider
237 */
238 ((XmlPresentationProvider) pres).loadNewStates(viewElement);
239 }
240
241 String title = fViewInfo.getViewTitle(viewElement);
242 if (title == null) {
243 title = Messages.XmlTimeGraphView_DefaultTitle;
244 }
245 setViewTitle(title);
246 Set<String> analysisIds = fViewInfo.getViewAnalysisIds(viewElement);
247
248 List<Element> entries = XmlUtils.getChildElements(viewElement, TmfXmlUiStrings.ENTRY_ELEMENT);
249 Set<XmlEntry> entryList = new TreeSet<>(getEntryComparator());
250 if (monitor.isCanceled()) {
251 return;
252 }
253
254 Set<@NonNull ITmfAnalysisModuleWithStateSystems> stateSystemModules = new HashSet<>();
255 if (analysisIds.isEmpty()) {
256 /*
257 * No analysis specified, take all state system analysis modules
258 */
259 Iterables.addAll(stateSystemModules, TmfTraceUtils.getAnalysisModulesOfClass(trace, ITmfAnalysisModuleWithStateSystems.class));
260 } else {
261 for (String moduleId : analysisIds) {
262 moduleId = checkNotNull(moduleId);
263 ITmfAnalysisModuleWithStateSystems module = TmfTraceUtils.getAnalysisModuleOfClass(trace, ITmfAnalysisModuleWithStateSystems.class, moduleId);
264 if (module != null) {
265 stateSystemModules.add(module);
266 }
267 }
268 }
269
270 for (ITmfAnalysisModuleWithStateSystems module : stateSystemModules) {
271 IStatus status = module.schedule();
272 if (!status.isOK()) {
273 return;
274 }
275 if (!module.waitForInitialization()) {
276 return;
277 }
278 for (ITmfStateSystem ssq : module.getStateSystems()) {
279 ssq.waitUntilBuilt();
280
281 long startTime = ssq.getStartTime();
282 long endTime = ssq.getCurrentEndTime();
283 XmlEntry groupEntry = new XmlEntry(-1, trace, trace.getName(), ssq);
284 entryList.add(groupEntry);
285 setStartTime(Math.min(getStartTime(), startTime));
286 setEndTime(Math.max(getEndTime(), endTime));
287
288 /* Add children entry of this entry for each line */
289 for (Element entry : entries) {
290 buildEntry(entry, groupEntry, -1);
291 }
292 }
293 }
294
295 addToEntryList(parentTrace, new ArrayList<TimeGraphEntry>(entryList));
296
297 if (parentTrace.equals(getTrace())) {
298 refresh();
299 }
300 for (XmlEntry traceEntry : entryList) {
301 if (monitor.isCanceled()) {
302 return;
303 }
304 long startTime = traceEntry.getStateSystem().getStartTime();
305 long endTime = traceEntry.getStateSystem().getCurrentEndTime() + 1;
306 buildStatusEvent(traceEntry, monitor, startTime, endTime);
307 }
308 }
309
310 private void buildEntry(Element entryElement, XmlEntry parentEntry, int baseQuark) {
311 /* Get the attribute string to display */
312 String path = entryElement.getAttribute(TmfXmlUiStrings.PATH);
313 if (path.isEmpty()) {
314 path = TmfXmlStrings.WILDCARD;
315 }
316
317 /*
318 * Make sure the XML element has either a display attribute or entries,
319 * otherwise issue a warning
320 */
321
322 List<Element> displayElements = XmlUtils.getChildElements(entryElement, TmfXmlUiStrings.DISPLAY_ELEMENT);
323 List<Element> entryElements = XmlUtils.getChildElements(entryElement, TmfXmlUiStrings.ENTRY_ELEMENT);
324
325 if (displayElements.isEmpty() && entryElements.isEmpty()) {
326 Activator.logWarning(String.format("XML view: entry for %s should have either a display element or entry elements", path)); //$NON-NLS-1$
327 return;
328 }
329
330 ITmfStateSystem ss = parentEntry.getStateSystem();
331
332 /* Get the list of quarks to process with this path */
333 String[] paths = path.split(SPLIT_STRING);
334 int i = 0;
335 List<Integer> quarks = Collections.singletonList(baseQuark);
336
337 while (i < paths.length) {
338 List<Integer> subQuarks = new LinkedList<>();
339 /* Replace * by .* to have a regex string */
340 String name = paths[i].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
341 for (int relativeQuark : quarks) {
342 for (int quark : ss.getSubAttributes(relativeQuark, false, name)) {
343 subQuarks.add(quark);
344 }
345 }
346 quarks = subQuarks;
347 i++;
348 }
349
350 /* Process each quark */
351 XmlEntry currentEntry = parentEntry;
352 Element displayElement = null;
353 Map<String, XmlEntry> entryMap = new HashMap<>();
354 if (!displayElements.isEmpty()) {
355 displayElement = displayElements.get(0);
356 }
357 for (int quark : quarks) {
358 currentEntry = parentEntry;
359 /* Process the current entry, if specified */
360 if (displayElement != null) {
361 currentEntry = processEntry(entryElement, displayElement, parentEntry, quark, ss);
362 entryMap.put(currentEntry.getId(), currentEntry);
363 }
364 /* Process the children entry of this entry */
365 for (Element subEntryEl : entryElements) {
366 buildEntry(subEntryEl, currentEntry, quark);
367 }
368 }
369 if (!entryMap.isEmpty()) {
370 buildTree(entryMap, parentEntry);
371 }
372 }
373
374 private XmlEntry processEntry(@NonNull Element entryElement, @NonNull Element displayEl,
375 @NonNull XmlEntry parentEntry, int quark, ITmfStateSystem ss) {
376 /*
377 * Get the start time and end time of this entry from the display
378 * attribute
379 */
380 ITmfXmlStateAttribute display = fFactory.createStateAttribute(displayEl, parentEntry);
381 int displayQuark = display.getAttributeQuark(quark, null);
382 if (displayQuark == IXmlStateSystemContainer.ERROR_QUARK) {
383 return new XmlEntry(quark, parentEntry.getTrace(),
384 String.format("Unknown display quark for %s", ss.getAttributeName(quark)), ss); //$NON-NLS-1$
385 }
386
387 long entryStart = ss.getStartTime();
388 long entryEnd = ss.getCurrentEndTime();
389
390 try {
391
392 ITmfStateInterval oneInterval = ss.querySingleState(entryStart, displayQuark);
393
394 /* The entry start is the first non-null interval */
395 while (oneInterval.getStateValue().isNull()) {
396 long ts = oneInterval.getEndTime() + 1;
397 if (ts > ss.getCurrentEndTime()) {
398 break;
399 }
400 oneInterval = ss.querySingleState(ts, displayQuark);
401 }
402 entryStart = oneInterval.getStartTime();
403
404 /* The entry end is the last non-null interval */
405 oneInterval = ss.querySingleState(entryEnd, displayQuark);
406 while (oneInterval.getStateValue().isNull()) {
407 long ts = oneInterval.getStartTime() - 1;
408 if (ts < ss.getStartTime()) {
409 break;
410 }
411 oneInterval = ss.querySingleState(ts, displayQuark);
412 }
413 entryEnd = oneInterval.getEndTime();
414
415 } catch (StateSystemDisposedException e) {
416 }
417
418 return new XmlEntry(quark, displayQuark, parentEntry.getTrace(), ss.getAttributeName(quark),
419 entryStart, entryEnd, EntryDisplayType.DISPLAY, ss, entryElement);
420 }
421
422 private void buildStatusEvent(XmlEntry traceEntry, @NonNull IProgressMonitor monitor, long start, long end) {
423 long resolution = (end - start) / getDisplayWidth();
424 long startTime = Math.max(start, traceEntry.getStartTime());
425 long endTime = Math.min(end + 1, traceEntry.getEndTime());
426 List<ITimeEvent> eventList = getEventList(traceEntry, startTime, endTime, resolution, monitor);
427 if (monitor.isCanceled()) {
428 return;
429 }
430 traceEntry.setEventList(eventList);
431 redraw();
432
433 for (ITimeGraphEntry entry : traceEntry.getChildren()) {
434 if (monitor.isCanceled()) {
435 return;
436 }
437 XmlEntry xmlEntry = (XmlEntry) entry;
438 buildStatusEvent(xmlEntry, monitor, start, end);
439 }
440 }
441
442 /** Build a tree using getParentId() and getId() */
443 private static void buildTree(Map<String, XmlEntry> entryMap, XmlEntry rootEntry) {
444 for (XmlEntry entry : entryMap.values()) {
445 boolean root = true;
446 if (!entry.getParentId().isEmpty()) {
447 XmlEntry parent = entryMap.get(entry.getParentId());
448 /*
449 * Associate the parent entry only if their time overlap. A
450 * child entry may start before its parent, for example at the
451 * beginning of the trace if a parent has not yet appeared in
452 * the state system. We just want to make sure that the entry
453 * didn't start after the parent ended or ended before the
454 * parent started.
455 */
456 if (parent != null &&
457 !(entry.getStartTime() > parent.getEndTime() ||
458 entry.getEndTime() < parent.getStartTime())) {
459 parent.addChild(entry);
460 root = false;
461 }
462 }
463 if (root) {
464 rootEntry.addChild(entry);
465 }
466 }
467 }
468
469 @Override
470 protected List<ITimeEvent> getEventList(TimeGraphEntry entry, long startTime, long endTime, long resolution, IProgressMonitor monitor) {
471 if (!(entry instanceof XmlEntry)) {
472 return Collections.EMPTY_LIST;
473 }
474 XmlEntry xmlEntry = (XmlEntry) entry;
475 ITmfStateSystem ssq = xmlEntry.getStateSystem();
476 final long realStart = Math.max(startTime, entry.getStartTime());
477 final long realEnd = Math.min(endTime, entry.getEndTime());
478 if (realEnd <= realStart) {
479 return null;
480 }
481 List<ITimeEvent> eventList = null;
482 int quark = xmlEntry.getDisplayQuark();
483
484 try {
485 if (xmlEntry.getType() == EntryDisplayType.DISPLAY) {
486
487 List<ITmfStateInterval> statusIntervals = StateSystemUtils.queryHistoryRange(ssq, quark, realStart, realEnd - 1, resolution, monitor);
488 eventList = new ArrayList<>(statusIntervals.size());
489 long lastEndTime = -1;
490 for (ITmfStateInterval statusInterval : statusIntervals) {
491 if (monitor.isCanceled()) {
492 return null;
493 }
494 int status = statusInterval.getStateValue().unboxInt();
495 long time = statusInterval.getStartTime();
496 long duration = statusInterval.getEndTime() - time + 1;
497 if (!statusInterval.getStateValue().isNull()) {
498 if (lastEndTime != time && lastEndTime != -1) {
499 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
500 }
501 eventList.add(new TimeEvent(entry, time, duration, status));
502 } else if (lastEndTime == -1 || time + duration >= endTime) {
503 // add null event if it intersects the start or end time
504 eventList.add(new NullTimeEvent(entry, time, duration));
505 }
506 lastEndTime = time + duration;
507 }
508 }
509 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException | StateSystemDisposedException e) {
510 /* Ignored */
511 }
512 return eventList;
513 }
514
515 @Override
516 protected List<ILinkEvent> getLinkList(long startTime, long endTime, long resolution, IProgressMonitor monitor) {
517 /* TODO: not implemented yet, need XML to go along */
518 return Collections.EMPTY_LIST;
519 }
520
521 }
This page took 0.047221 seconds and 5 git commands to generate.