1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal
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
10 * Florian Wininger - Initial API and implementation
11 * Geneviève Bastien - Review of the initial implementation
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.timegraph
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
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
;
27 import java
.util
.TreeSet
;
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
.statesystem
.core
.statevalue
.ITmfStateValue
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
62 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
63 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
64 import org
.w3c
.dom
.Element
;
66 import com
.google
.common
.collect
.Iterables
;
69 * This view displays state system data in a time graph view. It uses an XML
70 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
71 * element defines which entries from the state system will be shown and also
72 * gives additional information on the presentation of the view (states, colors,
75 * @author Florian Wininger
77 public class XmlTimeGraphView
extends AbstractTimeGraphView
{
80 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
82 private static final String
[] DEFAULT_COLUMN_NAMES
= new String
[] {
83 Messages
.XmlTimeGraphView_ColumnName
,
84 Messages
.XmlTimeGraphView_ColumnId
,
85 Messages
.XmlTimeGraphView_ColumnParentId
,
88 private static final String
[] DEFAULT_FILTER_COLUMN_NAMES
= new String
[] {
89 Messages
.XmlTimeGraphView_ColumnName
,
90 Messages
.XmlTimeGraphView_ColumnId
93 /** The relative weight of the sash */
94 private static final int[] fWeight
= { 1, 2 };
96 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
97 private static final @NonNull String SPLIT_STRING
= "/"; //$NON-NLS-1$
99 private final @NonNull XmlViewInfo fViewInfo
= new XmlViewInfo(ID
);
100 private final ITmfXmlModelFactory fFactory
;
101 private final Map
<String
, Integer
> fStringValueMap
= new HashMap
<>();
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
108 * Default constructor
110 public XmlTimeGraphView() {
111 super(ID
, new XmlPresentationProvider());
113 setTreeColumns(DEFAULT_COLUMN_NAMES
);
114 setTreeLabelProvider(new XmlTreeLabelProvider());
115 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES
);
116 setFilterLabelProvider(new XmlTreeLabelProvider());
117 setEntryComparator(new XmlEntryComparator());
118 this.addPartPropertyListener(new IPropertyChangeListener() {
120 public void propertyChange(PropertyChangeEvent event
) {
121 if (event
.getProperty().equals(TmfXmlUiStrings
.XML_OUTPUT_DATA
)) {
122 Object newValue
= event
.getNewValue();
123 if (newValue
instanceof String
) {
124 String data
= (String
) newValue
;
125 fViewInfo
.setViewData(data
);
132 fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
135 private void loadNewXmlView() {
139 private void setViewTitle(final String title
) {
140 Display
.getDefault().asyncExec(new Runnable() {
150 protected String
getNextText() {
151 return Messages
.XmlTimeGraphView_NextText
;
155 protected String
getNextTooltip() {
156 return Messages
.XmlTimeGraphView_NextTooltip
;
160 protected String
getPrevText() {
161 return Messages
.XmlTimeGraphView_PreviousText
;
165 protected String
getPrevTooltip() {
166 return Messages
.XmlTimeGraphView_PreviousInterval
;
170 * Default label provider, it shows name, id and parent columns
172 * TODO: There should be a way to define columns in the XML
174 private static class XmlTreeLabelProvider
extends TreeLabelProvider
{
177 public String
getColumnText(Object element
, int columnIndex
) {
178 XmlEntry entry
= (XmlEntry
) element
;
180 if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnName
)) {
181 return entry
.getName();
182 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnId
)) {
183 return entry
.getId();
184 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnParentId
)) {
185 return entry
.getParentId();
192 private static class XmlEntryComparator
implements Comparator
<ITimeGraphEntry
> {
195 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
199 if ((o1
instanceof XmlEntry
) && (o2
instanceof XmlEntry
)) {
200 XmlEntry entry1
= (XmlEntry
) o1
;
201 XmlEntry entry2
= (XmlEntry
) o2
;
202 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
204 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
207 result
= entry1
.getName().compareTo(entry2
.getName());
212 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
219 // ------------------------------------------------------------------------
221 // ------------------------------------------------------------------------
224 protected void buildEntryList(ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
227 * Get the view element from the XML file. If the element can't be
230 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.TIME_GRAPH_VIEW
);
231 if (viewElement
== null) {
234 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
235 if (pres
instanceof XmlPresentationProvider
) {
237 * TODO: Each entry of a line could have their own states/color.
238 * That will require an update to the presentation provider
240 ((XmlPresentationProvider
) pres
).loadNewStates(viewElement
);
243 String title
= fViewInfo
.getViewTitle(viewElement
);
245 title
= Messages
.XmlTimeGraphView_DefaultTitle
;
249 // Empty the additional state values
250 fStringValueMap
.clear();
252 Set
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
254 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
255 Set
<XmlEntry
> entryList
= new TreeSet
<>(getEntryComparator());
256 if (monitor
.isCanceled()) {
260 Set
<@NonNull ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new HashSet
<>();
261 if (analysisIds
.isEmpty()) {
263 * No analysis specified, take all state system analysis modules
265 Iterables
.addAll(stateSystemModules
, TmfTraceUtils
.getAnalysisModulesOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class));
267 for (String moduleId
: analysisIds
) {
268 moduleId
= checkNotNull(moduleId
);
269 ITmfAnalysisModuleWithStateSystems module
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
270 if (module
!= null) {
271 stateSystemModules
.add(module
);
276 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
277 IStatus status
= module
.schedule();
278 if (!status
.isOK()) {
281 if (!module
.waitForInitialization()) {
284 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
285 ssq
.waitUntilBuilt();
287 long startTime
= ssq
.getStartTime();
288 long endTime
= ssq
.getCurrentEndTime();
289 XmlEntry groupEntry
= new XmlEntry(-1, trace
, trace
.getName(), ssq
);
290 entryList
.add(groupEntry
);
291 setStartTime(Math
.min(getStartTime(), startTime
));
292 setEndTime(Math
.max(getEndTime(), endTime
));
294 /* Add children entry of this entry for each line */
295 for (Element entry
: entries
) {
296 buildEntry(entry
, groupEntry
, -1);
301 addToEntryList(parentTrace
, new ArrayList
<TimeGraphEntry
>(entryList
));
303 if (parentTrace
.equals(getTrace())) {
306 for (XmlEntry traceEntry
: entryList
) {
307 if (monitor
.isCanceled()) {
310 long startTime
= traceEntry
.getStateSystem().getStartTime();
311 long endTime
= traceEntry
.getStateSystem().getCurrentEndTime() + 1;
312 buildStatusEvent(traceEntry
, monitor
, startTime
, endTime
);
316 private void buildEntry(Element entryElement
, XmlEntry parentEntry
, int baseQuark
) {
317 /* Get the attribute string to display */
318 String path
= entryElement
.getAttribute(TmfXmlUiStrings
.PATH
);
319 if (path
.isEmpty()) {
320 path
= TmfXmlStrings
.WILDCARD
;
324 * Make sure the XML element has either a display attribute or entries,
325 * otherwise issue a warning
328 List
<Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
329 List
<Element
> entryElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
331 if (displayElements
.isEmpty() && entryElements
.isEmpty()) {
332 Activator
.logWarning(String
.format("XML view: entry for %s should have either a display element or entry elements", path
)); //$NON-NLS-1$
336 ITmfStateSystem ss
= parentEntry
.getStateSystem();
338 /* Get the list of quarks to process with this path */
339 String
[] paths
= path
.split(SPLIT_STRING
);
341 List
<Integer
> quarks
= Collections
.singletonList(baseQuark
);
343 while (i
< paths
.length
) {
344 List
<Integer
> subQuarks
= new LinkedList
<>();
345 /* Replace * by .* to have a regex string */
346 String name
= paths
[i
].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
347 for (int relativeQuark
: quarks
) {
348 for (int quark
: ss
.getSubAttributes(relativeQuark
, false, name
)) {
349 subQuarks
.add(quark
);
356 /* Process each quark */
357 XmlEntry currentEntry
= parentEntry
;
358 Element displayElement
= null;
359 Map
<String
, XmlEntry
> entryMap
= new HashMap
<>();
360 if (!displayElements
.isEmpty()) {
361 displayElement
= displayElements
.get(0);
363 for (int quark
: quarks
) {
364 currentEntry
= parentEntry
;
365 /* Process the current entry, if specified */
366 if (displayElement
!= null) {
367 currentEntry
= processEntry(entryElement
, displayElement
, parentEntry
, quark
, ss
);
368 entryMap
.put(currentEntry
.getId(), currentEntry
);
370 /* Process the children entry of this entry */
371 for (Element subEntryEl
: entryElements
) {
372 buildEntry(subEntryEl
, currentEntry
, quark
);
375 if (!entryMap
.isEmpty()) {
376 buildTree(entryMap
, parentEntry
);
380 private XmlEntry
processEntry(@NonNull Element entryElement
, @NonNull Element displayEl
,
381 @NonNull XmlEntry parentEntry
, int quark
, ITmfStateSystem ss
) {
383 * Get the start time and end time of this entry from the display
386 ITmfXmlStateAttribute display
= fFactory
.createStateAttribute(displayEl
, parentEntry
);
387 int displayQuark
= display
.getAttributeQuark(quark
, null);
388 if (displayQuark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
389 return new XmlEntry(quark
, parentEntry
.getTrace(),
390 String
.format("Unknown display quark for %s", ss
.getAttributeName(quark
)), ss
); //$NON-NLS-1$
393 long entryStart
= ss
.getStartTime();
394 long entryEnd
= ss
.getCurrentEndTime();
398 ITmfStateInterval oneInterval
= ss
.querySingleState(entryStart
, displayQuark
);
400 /* The entry start is the first non-null interval */
401 while (oneInterval
.getStateValue().isNull()) {
402 long ts
= oneInterval
.getEndTime() + 1;
403 if (ts
> ss
.getCurrentEndTime()) {
406 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
408 entryStart
= oneInterval
.getStartTime();
410 /* The entry end is the last non-null interval */
411 oneInterval
= ss
.querySingleState(entryEnd
, displayQuark
);
412 while (oneInterval
.getStateValue().isNull()) {
413 long ts
= oneInterval
.getStartTime() - 1;
414 if (ts
< ss
.getStartTime()) {
417 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
419 entryEnd
= oneInterval
.getEndTime();
421 } catch (StateSystemDisposedException e
) {
424 return new XmlEntry(quark
, displayQuark
, parentEntry
.getTrace(), ss
.getAttributeName(quark
),
425 entryStart
, entryEnd
, EntryDisplayType
.DISPLAY
, ss
, entryElement
);
428 private void buildStatusEvent(XmlEntry traceEntry
, @NonNull IProgressMonitor monitor
, long start
, long end
) {
429 long resolution
= (end
- start
) / getDisplayWidth();
430 long startTime
= Math
.max(start
, traceEntry
.getStartTime());
431 long endTime
= Math
.min(end
+ 1, traceEntry
.getEndTime());
432 List
<ITimeEvent
> eventList
= getEventList(traceEntry
, startTime
, endTime
, resolution
, monitor
);
433 if (monitor
.isCanceled()) {
436 traceEntry
.setEventList(eventList
);
439 for (ITimeGraphEntry entry
: traceEntry
.getChildren()) {
440 if (monitor
.isCanceled()) {
443 XmlEntry xmlEntry
= (XmlEntry
) entry
;
444 buildStatusEvent(xmlEntry
, monitor
, start
, end
);
448 /** Build a tree using getParentId() and getId() */
449 private static void buildTree(Map
<String
, XmlEntry
> entryMap
, XmlEntry rootEntry
) {
450 for (XmlEntry entry
: entryMap
.values()) {
452 if (!entry
.getParentId().isEmpty()) {
453 XmlEntry parent
= entryMap
.get(entry
.getParentId());
455 * Associate the parent entry only if their time overlap. A
456 * child entry may start before its parent, for example at the
457 * beginning of the trace if a parent has not yet appeared in
458 * the state system. We just want to make sure that the entry
459 * didn't start after the parent ended or ended before the
462 if (parent
!= null &&
463 !(entry
.getStartTime() > parent
.getEndTime() ||
464 entry
.getEndTime() < parent
.getStartTime())) {
465 parent
.addChild(entry
);
470 rootEntry
.addChild(entry
);
476 protected List
<ITimeEvent
> getEventList(TimeGraphEntry entry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
477 if (!(entry
instanceof XmlEntry
)) {
478 return Collections
.EMPTY_LIST
;
480 XmlEntry xmlEntry
= (XmlEntry
) entry
;
481 ITmfStateSystem ssq
= xmlEntry
.getStateSystem();
482 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
483 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
484 if (realEnd
<= realStart
) {
487 List
<ITimeEvent
> eventList
= null;
488 int quark
= xmlEntry
.getDisplayQuark();
491 if (xmlEntry
.getType() == EntryDisplayType
.DISPLAY
) {
493 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, quark
, realStart
, realEnd
- 1, resolution
, monitor
);
494 eventList
= new ArrayList
<>(statusIntervals
.size());
495 long lastEndTime
= -1;
496 for (ITmfStateInterval statusInterval
: statusIntervals
) {
497 if (monitor
.isCanceled()) {
500 int status
= getStatusFromInterval(statusInterval
);
501 long time
= statusInterval
.getStartTime();
502 long duration
= statusInterval
.getEndTime() - time
+ 1;
503 if (!statusInterval
.getStateValue().isNull()) {
504 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
505 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
507 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
508 } else if (lastEndTime
== -1 || time
+ duration
>= endTime
) {
509 // add null event if it intersects the start or end time
510 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
512 lastEndTime
= time
+ duration
;
515 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException
| StateSystemDisposedException e
) {
521 private int getStatusFromInterval(ITmfStateInterval statusInterval
) {
522 ITmfStateValue stateValue
= statusInterval
.getStateValue();
524 switch (stateValue
.getType()) {
527 status
= stateValue
.unboxInt();
530 status
= (int) stateValue
.unboxLong();
533 String statusStr
= stateValue
.unboxStr();
534 Integer statusInt
= fStringValueMap
.get(statusStr
);
535 if (statusInt
!= null) {
539 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
540 if (pres
instanceof XmlPresentationProvider
) {
541 // Add this new state to the presentation provider
542 status
= ((XmlPresentationProvider
) pres
).addState(statusStr
);
543 fStringValueMap
.put(statusStr
, status
);
547 status
= (int) stateValue
.unboxDouble();
558 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
559 /* TODO: not implemented yet, need XML to go along */
560 return Collections
.EMPTY_LIST
;