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
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
39 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.readonly
.TmfXmlReadOnlyModelFactory
;
40 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
41 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
43 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.Activator
;
44 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
45 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.XmlViewInfo
;
46 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.timegraph
.XmlEntry
.EntryDisplayType
;
47 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
48 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
49 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
50 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
51 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
52 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
53 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
54 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
62 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
63 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
64 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
65 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
66 import org
.w3c
.dom
.Element
;
68 import com
.google
.common
.collect
.Iterables
;
71 * This view displays state system data in a time graph view. It uses an XML
72 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
73 * element defines which entries from the state system will be shown and also
74 * gives additional information on the presentation of the view (states, colors,
77 * @author Florian Wininger
79 public class XmlTimeGraphView
extends AbstractTimeGraphView
{
82 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
84 private static final String
[] DEFAULT_COLUMN_NAMES
= new String
[] {
85 Messages
.XmlTimeGraphView_ColumnName
,
86 Messages
.XmlTimeGraphView_ColumnId
,
87 Messages
.XmlTimeGraphView_ColumnParentId
,
90 private static final String
[] DEFAULT_FILTER_COLUMN_NAMES
= new String
[] {
91 Messages
.XmlTimeGraphView_ColumnName
,
92 Messages
.XmlTimeGraphView_ColumnId
95 /** The relative weight of the sash */
96 private static final int[] fWeight
= { 1, 2 };
98 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
99 private static final @NonNull String SPLIT_STRING
= "/"; //$NON-NLS-1$
101 private final @NonNull XmlViewInfo fViewInfo
= new XmlViewInfo(ID
);
102 private final ITmfXmlModelFactory fFactory
;
103 private final Map
<String
, Integer
> fStringValueMap
= new HashMap
<>();
105 // ------------------------------------------------------------------------
107 // ------------------------------------------------------------------------
110 * Default constructor
112 public XmlTimeGraphView() {
113 super(ID
, new XmlPresentationProvider());
115 setTreeColumns(DEFAULT_COLUMN_NAMES
);
116 setTreeLabelProvider(new XmlTreeLabelProvider());
117 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES
);
118 setFilterLabelProvider(new XmlTreeLabelProvider());
119 setEntryComparator(new XmlEntryComparator());
120 this.addPartPropertyListener(new IPropertyChangeListener() {
122 public void propertyChange(PropertyChangeEvent event
) {
123 if (event
.getProperty().equals(TmfXmlUiStrings
.XML_OUTPUT_DATA
)) {
124 Object newValue
= event
.getNewValue();
125 if (newValue
instanceof String
) {
126 String data
= (String
) newValue
;
127 fViewInfo
.setViewData(data
);
133 fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
137 public void createPartControl(Composite parent
) {
138 super.createPartControl(parent
);
139 fViewInfo
.setName(NonNullUtils
.checkNotNull(getViewSite().getSecondaryId()));
142 private void loadNewXmlView() {
146 private void setViewTitle(final String title
) {
147 Display
.getDefault().asyncExec(new Runnable() {
157 protected String
getNextText() {
158 return Messages
.XmlTimeGraphView_NextText
;
162 protected String
getNextTooltip() {
163 return Messages
.XmlTimeGraphView_NextTooltip
;
167 protected String
getPrevText() {
168 return Messages
.XmlTimeGraphView_PreviousText
;
172 protected String
getPrevTooltip() {
173 return Messages
.XmlTimeGraphView_PreviousInterval
;
177 * Default label provider, it shows name, id and parent columns
179 * TODO: There should be a way to define columns in the XML
181 private static class XmlTreeLabelProvider
extends TreeLabelProvider
{
184 public String
getColumnText(Object element
, int columnIndex
) {
185 XmlEntry entry
= (XmlEntry
) element
;
187 if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnName
)) {
188 return entry
.getName();
189 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnId
)) {
190 return entry
.getId();
191 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnParentId
)) {
192 return entry
.getParentId();
199 private static class XmlEntryComparator
implements Comparator
<ITimeGraphEntry
> {
202 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
206 if ((o1
instanceof XmlEntry
) && (o2
instanceof XmlEntry
)) {
207 XmlEntry entry1
= (XmlEntry
) o1
;
208 XmlEntry entry2
= (XmlEntry
) o2
;
209 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
211 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
214 result
= entry1
.getName().compareTo(entry2
.getName());
219 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
226 // ------------------------------------------------------------------------
228 // ------------------------------------------------------------------------
231 protected void buildEntryList(ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
233 if (!fViewInfo
.waitForInitialization()) {
237 * Get the view element from the XML file. If the element can't be
240 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.TIME_GRAPH_VIEW
);
241 if (viewElement
== null) {
244 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
245 if (pres
instanceof XmlPresentationProvider
) {
247 * TODO: Each entry of a line could have their own states/color.
248 * That will require an update to the presentation provider
250 ((XmlPresentationProvider
) pres
).loadNewStates(viewElement
);
253 String title
= fViewInfo
.getViewTitle(viewElement
);
255 title
= Messages
.XmlTimeGraphView_DefaultTitle
;
259 // Empty the additional state values
260 fStringValueMap
.clear();
262 Set
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
264 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
265 Set
<XmlEntry
> entryList
= new TreeSet
<>(getEntryComparator());
266 if (monitor
.isCanceled()) {
270 Set
<@NonNull ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new HashSet
<>();
271 if (analysisIds
.isEmpty()) {
273 * No analysis specified, take all state system analysis modules
275 Iterables
.addAll(stateSystemModules
, TmfTraceUtils
.getAnalysisModulesOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class));
277 for (String moduleId
: analysisIds
) {
278 moduleId
= checkNotNull(moduleId
);
279 ITmfAnalysisModuleWithStateSystems module
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
280 if (module
!= null) {
281 stateSystemModules
.add(module
);
286 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
287 IStatus status
= module
.schedule();
288 if (!status
.isOK()) {
291 if (!module
.waitForInitialization()) {
294 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
295 ssq
.waitUntilBuilt();
297 long startTime
= ssq
.getStartTime();
298 long endTime
= ssq
.getCurrentEndTime();
299 XmlEntry groupEntry
= new XmlEntry(-1, trace
, trace
.getName(), ssq
);
300 entryList
.add(groupEntry
);
301 setStartTime(Math
.min(getStartTime(), startTime
));
302 setEndTime(Math
.max(getEndTime(), endTime
));
304 /* Add children entry of this entry for each line */
305 for (Element entry
: entries
) {
306 buildEntry(entry
, groupEntry
, -1);
311 addToEntryList(parentTrace
, new ArrayList
<TimeGraphEntry
>(entryList
));
313 if (parentTrace
.equals(getTrace())) {
316 for (XmlEntry traceEntry
: entryList
) {
317 if (monitor
.isCanceled()) {
320 long startTime
= traceEntry
.getStateSystem().getStartTime();
321 long endTime
= traceEntry
.getStateSystem().getCurrentEndTime() + 1;
322 buildStatusEvent(traceEntry
, monitor
, startTime
, endTime
);
326 private void buildEntry(Element entryElement
, XmlEntry parentEntry
, int baseQuark
) {
327 /* Get the attribute string to display */
328 String path
= entryElement
.getAttribute(TmfXmlUiStrings
.PATH
);
329 if (path
.isEmpty()) {
330 path
= TmfXmlStrings
.WILDCARD
;
334 * Make sure the XML element has either a display attribute or entries,
335 * otherwise issue a warning
338 List
<Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
339 List
<Element
> entryElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
341 if (displayElements
.isEmpty() && entryElements
.isEmpty()) {
342 Activator
.logWarning(String
.format("XML view: entry for %s should have either a display element or entry elements", path
)); //$NON-NLS-1$
346 ITmfStateSystem ss
= parentEntry
.getStateSystem();
348 /* Get the list of quarks to process with this path */
349 String
[] paths
= path
.split(SPLIT_STRING
);
351 List
<Integer
> quarks
= Collections
.singletonList(baseQuark
);
353 while (i
< paths
.length
) {
354 List
<Integer
> subQuarks
= new LinkedList
<>();
355 /* Replace * by .* to have a regex string */
356 String name
= paths
[i
].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
357 for (int relativeQuark
: quarks
) {
358 for (int quark
: ss
.getSubAttributes(relativeQuark
, false, name
)) {
359 subQuarks
.add(quark
);
366 /* Process each quark */
367 XmlEntry currentEntry
= parentEntry
;
368 Element displayElement
= null;
369 Map
<String
, XmlEntry
> entryMap
= new HashMap
<>();
370 if (!displayElements
.isEmpty()) {
371 displayElement
= displayElements
.get(0);
373 for (int quark
: quarks
) {
374 currentEntry
= parentEntry
;
375 /* Process the current entry, if specified */
376 if (displayElement
!= null) {
377 currentEntry
= processEntry(entryElement
, displayElement
, parentEntry
, quark
, ss
);
378 entryMap
.put(currentEntry
.getId(), currentEntry
);
380 /* Process the children entry of this entry */
381 for (Element subEntryEl
: entryElements
) {
382 buildEntry(subEntryEl
, currentEntry
, quark
);
385 if (!entryMap
.isEmpty()) {
386 buildTree(entryMap
, parentEntry
);
390 private XmlEntry
processEntry(@NonNull Element entryElement
, @NonNull Element displayEl
,
391 @NonNull XmlEntry parentEntry
, int quark
, ITmfStateSystem ss
) {
393 * Get the start time and end time of this entry from the display
396 ITmfXmlStateAttribute display
= fFactory
.createStateAttribute(displayEl
, parentEntry
);
397 int displayQuark
= display
.getAttributeQuark(quark
, null);
398 if (displayQuark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
399 return new XmlEntry(quark
, parentEntry
.getTrace(),
400 String
.format("Unknown display quark for %s", ss
.getAttributeName(quark
)), ss
); //$NON-NLS-1$
403 long entryStart
= ss
.getStartTime();
404 long entryEnd
= ss
.getCurrentEndTime();
408 ITmfStateInterval oneInterval
= ss
.querySingleState(entryStart
, displayQuark
);
410 /* The entry start is the first non-null interval */
411 while (oneInterval
.getStateValue().isNull()) {
412 long ts
= oneInterval
.getEndTime() + 1;
413 if (ts
> ss
.getCurrentEndTime()) {
416 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
418 entryStart
= oneInterval
.getStartTime();
420 /* The entry end is the last non-null interval */
421 oneInterval
= ss
.querySingleState(entryEnd
, displayQuark
);
422 while (oneInterval
.getStateValue().isNull()) {
423 long ts
= oneInterval
.getStartTime() - 1;
424 if (ts
< ss
.getStartTime()) {
427 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
429 entryEnd
= oneInterval
.getEndTime();
431 } catch (StateSystemDisposedException e
) {
434 return new XmlEntry(quark
, displayQuark
, parentEntry
.getTrace(), ss
.getAttributeName(quark
),
435 entryStart
, entryEnd
, EntryDisplayType
.DISPLAY
, ss
, entryElement
);
438 private void buildStatusEvent(XmlEntry traceEntry
, @NonNull IProgressMonitor monitor
, long start
, long end
) {
439 long resolution
= (end
- start
) / getDisplayWidth();
440 long startTime
= Math
.max(start
, traceEntry
.getStartTime());
441 long endTime
= Math
.min(end
+ 1, traceEntry
.getEndTime());
442 List
<ITimeEvent
> eventList
= getEventList(traceEntry
, startTime
, endTime
, resolution
, monitor
);
443 if (monitor
.isCanceled()) {
446 traceEntry
.setEventList(eventList
);
449 for (ITimeGraphEntry entry
: traceEntry
.getChildren()) {
450 if (monitor
.isCanceled()) {
453 XmlEntry xmlEntry
= (XmlEntry
) entry
;
454 buildStatusEvent(xmlEntry
, monitor
, start
, end
);
458 /** Build a tree using getParentId() and getId() */
459 private static void buildTree(Map
<String
, XmlEntry
> entryMap
, XmlEntry rootEntry
) {
460 for (XmlEntry entry
: entryMap
.values()) {
462 if (!entry
.getParentId().isEmpty()) {
463 XmlEntry parent
= entryMap
.get(entry
.getParentId());
465 * Associate the parent entry only if their time overlap. A
466 * child entry may start before its parent, for example at the
467 * beginning of the trace if a parent has not yet appeared in
468 * the state system. We just want to make sure that the entry
469 * didn't start after the parent ended or ended before the
472 if (parent
!= null &&
473 !(entry
.getStartTime() > parent
.getEndTime() ||
474 entry
.getEndTime() < parent
.getStartTime())) {
475 parent
.addChild(entry
);
480 rootEntry
.addChild(entry
);
486 protected List
<ITimeEvent
> getEventList(TimeGraphEntry entry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
487 if (!(entry
instanceof XmlEntry
)) {
488 return Collections
.EMPTY_LIST
;
490 XmlEntry xmlEntry
= (XmlEntry
) entry
;
491 ITmfStateSystem ssq
= xmlEntry
.getStateSystem();
492 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
493 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
494 if (realEnd
<= realStart
) {
497 List
<ITimeEvent
> eventList
= null;
498 int quark
= xmlEntry
.getDisplayQuark();
501 if (xmlEntry
.getType() == EntryDisplayType
.DISPLAY
) {
503 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, quark
, realStart
, realEnd
- 1, resolution
, monitor
);
504 eventList
= new ArrayList
<>(statusIntervals
.size());
505 long lastEndTime
= -1;
506 for (ITmfStateInterval statusInterval
: statusIntervals
) {
507 if (monitor
.isCanceled()) {
510 int status
= getStatusFromInterval(statusInterval
);
511 long time
= statusInterval
.getStartTime();
512 long duration
= statusInterval
.getEndTime() - time
+ 1;
513 if (!statusInterval
.getStateValue().isNull()) {
514 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
515 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
517 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
518 } else if (lastEndTime
== -1 || time
+ duration
>= endTime
) {
519 // add null event if it intersects the start or end time
520 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
522 lastEndTime
= time
+ duration
;
525 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException
| StateSystemDisposedException e
) {
531 private int getStatusFromInterval(ITmfStateInterval statusInterval
) {
532 ITmfStateValue stateValue
= statusInterval
.getStateValue();
534 switch (stateValue
.getType()) {
537 status
= stateValue
.unboxInt();
540 status
= (int) stateValue
.unboxLong();
543 String statusStr
= stateValue
.unboxStr();
544 Integer statusInt
= fStringValueMap
.get(statusStr
);
545 if (statusInt
!= null) {
549 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
550 if (pres
instanceof XmlPresentationProvider
) {
551 // Add this new state to the presentation provider
552 status
= ((XmlPresentationProvider
) pres
).addState(statusStr
);
553 fStringValueMap
.put(statusStr
, status
);
557 status
= (int) stateValue
.unboxDouble();
568 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
569 /* TODO: not implemented yet, need XML to go along */
570 return Collections
.EMPTY_LIST
;