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