1 /*******************************************************************************
2 * Copyright (c) 2014 É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
.tmf
.analysis
.xml
.ui
.views
.timegraph
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Collections
;
18 import java
.util
.Comparator
;
19 import java
.util
.HashMap
;
20 import java
.util
.LinkedList
;
21 import java
.util
.List
;
24 import java
.util
.TreeSet
;
26 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
30 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.Activator
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
34 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.XmlViewInfo
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
40 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
41 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
42 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
43 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.readonly
.TmfXmlReadOnlyModelFactory
;
44 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
45 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
46 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
47 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.ui
.views
.timegraph
.XmlEntry
.EntryDisplayType
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
60 import org
.w3c
.dom
.Element
;
63 * This view displays state system data in a time graph view. It uses an XML
64 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
65 * element defines which entries from the state system will be shown and also
66 * gives additional information on the presentation of the view (states, colors,
69 * @author Florian Wininger
71 public class XmlTimeGraphView
extends AbstractTimeGraphView
{
74 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
76 private static final String
[] DEFAULT_COLUMN_NAMES
= new String
[] {
77 Messages
.XmlTimeGraphView_ColumnName
,
78 Messages
.XmlTimeGraphView_ColumnId
,
79 Messages
.XmlTimeGraphView_ColumnParentId
,
82 private static final String
[] DEFAULT_FILTER_COLUMN_NAMES
= new String
[] {
83 Messages
.XmlTimeGraphView_ColumnName
,
84 Messages
.XmlTimeGraphView_ColumnId
87 /** The relative weight of the sash */
88 private static final int[] fWeight
= { 1, 2 };
90 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
91 private static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
93 private final @NonNull XmlViewInfo fViewInfo
= new XmlViewInfo(ID
);
94 private final ITmfXmlModelFactory fFactory
;
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
101 * Default constructor
103 public XmlTimeGraphView() {
104 super(ID
, new XmlPresentationProvider());
106 setTreeColumns(DEFAULT_COLUMN_NAMES
);
107 setTreeLabelProvider(new XmlTreeLabelProvider());
108 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES
);
109 setFilterLabelProvider(new XmlTreeLabelProvider());
110 setEntryComparator(new XmlEntryComparator());
111 this.addPartPropertyListener(new IPropertyChangeListener() {
113 public void propertyChange(PropertyChangeEvent event
) {
114 if (event
.getProperty().equals(TmfXmlUiStrings
.XML_OUTPUT_DATA
)) {
115 Object newValue
= event
.getNewValue();
116 if (newValue
instanceof String
) {
117 String data
= (String
) newValue
;
118 fViewInfo
.setViewData(data
);
125 fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
128 private void loadNewXmlView() {
132 private void setViewTitle(final String title
) {
133 Display
.getDefault().asyncExec(new Runnable() {
143 protected String
getNextText() {
144 return Messages
.XmlTimeGraphView_NextText
;
148 protected String
getNextTooltip() {
149 return Messages
.XmlTimeGraphView_NextTooltip
;
153 protected String
getPrevText() {
154 return Messages
.XmlTimeGraphView_PreviousText
;
158 protected String
getPrevTooltip() {
159 return Messages
.XmlTimeGraphView_PreviousInterval
;
163 * Default label provider, it shows name, id and parent columns
165 * TODO: There should be a way to define columns in the XML
167 private static class XmlTreeLabelProvider
extends TreeLabelProvider
{
170 public String
getColumnText(Object element
, int columnIndex
) {
171 XmlEntry entry
= (XmlEntry
) element
;
173 if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnName
)) {
174 return entry
.getName();
175 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnId
)) {
176 return entry
.getId();
177 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnParentId
)) {
178 return entry
.getParentId();
185 private static class XmlEntryComparator
implements Comparator
<ITimeGraphEntry
> {
188 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
192 if ((o1
instanceof XmlEntry
) && (o2
instanceof XmlEntry
)) {
193 XmlEntry entry1
= (XmlEntry
) o1
;
194 XmlEntry entry2
= (XmlEntry
) o2
;
195 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
197 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
200 result
= entry1
.getName().compareTo(entry2
.getName());
205 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
212 // ------------------------------------------------------------------------
214 // ------------------------------------------------------------------------
217 protected void buildEventList(ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
220 * Get the view element from the XML file. If the element can't be
223 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.TIME_GRAPH_VIEW
);
224 if (viewElement
== null) {
227 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
228 if (pres
instanceof XmlPresentationProvider
) {
230 * TODO: Each entry of a line could have their own states/color.
231 * That will require an update to the presentation provider
233 ((XmlPresentationProvider
) pres
).loadNewStates(viewElement
);
236 String title
= fViewInfo
.getViewTitle(viewElement
);
238 title
= Messages
.XmlTimeGraphView_DefaultTitle
;
241 Set
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
243 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
244 Set
<XmlEntry
> entryList
= new TreeSet
<>(getEntryComparator());
245 for (ITmfTrace aTrace
: TmfTraceManager
.getTraceSet(trace
)) {
246 if (monitor
.isCanceled()) {
250 List
<ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new LinkedList
<>();
251 if (analysisIds
.isEmpty()) {
253 * No analysis specified, take all state system analysis modules
255 for (ITmfAnalysisModuleWithStateSystems module
: aTrace
.getAnalysisModulesOfClass(ITmfAnalysisModuleWithStateSystems
.class)) {
256 stateSystemModules
.add(module
);
259 for (String moduleId
: analysisIds
) {
260 ITmfAnalysisModuleWithStateSystems module
= aTrace
.getAnalysisModuleOfClass(ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
261 if (module
!= null) {
262 stateSystemModules
.add(module
);
267 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
268 IStatus status
= module
.schedule();
269 if (!status
.isOK()) {
272 if (module
instanceof TmfStateSystemAnalysisModule
) {
273 ((TmfStateSystemAnalysisModule
) module
).waitForInitialization();
275 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
279 ssq
.waitUntilBuilt();
281 long startTime
= ssq
.getStartTime();
282 long endTime
= ssq
.getCurrentEndTime();
283 XmlEntry groupEntry
= new XmlEntry(-1, aTrace
, aTrace
.getName(), ssq
);
284 entryList
.add(groupEntry
);
285 setStartTime(Math
.min(getStartTime(), startTime
));
286 setEndTime(Math
.max(getEndTime(), endTime
));
288 /* Add children entry of this entry for each line */
289 for (Element entry
: entries
) {
290 buildEntry(entry
, groupEntry
, -1);
295 putEntryList(trace
, new ArrayList
<TimeGraphEntry
>(entryList
));
297 if (trace
.equals(getTrace())) {
300 for (XmlEntry traceEntry
: entryList
) {
301 if (monitor
.isCanceled()) {
304 long startTime
= traceEntry
.getStateSystem().getStartTime();
305 long endTime
= traceEntry
.getStateSystem().getCurrentEndTime() + 1;
306 buildStatusEvent(traceEntry
, monitor
, startTime
, endTime
);
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
;
318 * Make sure the XML element has either a display attribute or entries,
319 * otherwise issue a warning
322 List
<Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
323 List
<Element
> entryElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
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$
330 ITmfStateSystem ss
= parentEntry
.getStateSystem();
332 /* Get the list of quarks to process with this path */
333 String
[] paths
= path
.split(SPLIT_STRING
);
335 List
<Integer
> quarks
= Collections
.singletonList(baseQuark
);
338 while (i
< paths
.length
) {
339 List
<Integer
> subQuarks
= new LinkedList
<>();
340 /* Replace * by .* to have a regex string */
341 String name
= paths
[i
].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
342 for (int relativeQuark
: quarks
) {
343 for (int quark
: ss
.getSubAttributes(relativeQuark
, false, name
)) {
344 subQuarks
.add(quark
);
351 /* Process each quark */
352 XmlEntry currentEntry
= parentEntry
;
353 Element displayElement
= null;
354 Map
<String
, XmlEntry
> entryMap
= new HashMap
<>();
355 if (!displayElements
.isEmpty()) {
356 displayElement
= displayElements
.get(0);
358 for (int quark
: quarks
) {
359 currentEntry
= parentEntry
;
360 /* Process the current entry, if specified */
361 if (displayElement
!= null) {
362 currentEntry
= processEntry(entryElement
, displayElement
, parentEntry
, quark
, ss
);
363 entryMap
.put(currentEntry
.getId(), currentEntry
);
365 /* Process the children entry of this entry */
366 for (Element subEntryEl
: entryElements
) {
367 buildEntry(subEntryEl
, currentEntry
, quark
);
370 if (!entryMap
.isEmpty()) {
371 buildTree(entryMap
, parentEntry
);
373 } catch (AttributeNotFoundException e
) {
377 private XmlEntry
processEntry(@NonNull Element entryElement
, @NonNull Element displayEl
,
378 XmlEntry parentEntry
, int quark
, ITmfStateSystem ss
) {
380 * Get the start time and end time of this entry from the display
383 ITmfXmlStateAttribute display
= fFactory
.createStateAttribute(displayEl
, parentEntry
);
384 int displayQuark
= display
.getAttributeQuark(quark
);
385 if (displayQuark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
386 return new XmlEntry(quark
, parentEntry
.getTrace(),
387 String
.format("Unknown display quark for %s", ss
.getAttributeName(quark
)), ss
); //$NON-NLS-1$
390 long entryStart
= ss
.getStartTime();
391 long entryEnd
= ss
.getCurrentEndTime();
395 ITmfStateInterval oneInterval
= ss
.querySingleState(entryStart
, displayQuark
);
397 /* The entry start is the first non-null interval */
398 while (oneInterval
.getStateValue().isNull()) {
399 long ts
= oneInterval
.getEndTime() + 1;
400 if (ts
> ss
.getCurrentEndTime()) {
403 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
405 entryStart
= oneInterval
.getStartTime();
407 /* The entry end is the last non-null interval */
408 oneInterval
= ss
.querySingleState(entryEnd
, displayQuark
);
409 while (oneInterval
.getStateValue().isNull()) {
410 long ts
= oneInterval
.getStartTime() - 1;
411 if (ts
< ss
.getStartTime()) {
414 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
416 entryEnd
= oneInterval
.getEndTime();
418 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
421 return new XmlEntry(quark
, displayQuark
, parentEntry
.getTrace(), ss
.getAttributeName(quark
),
422 entryStart
, entryEnd
, EntryDisplayType
.DISPLAY
, ss
, entryElement
);
425 private void buildStatusEvent(XmlEntry traceEntry
, IProgressMonitor monitor
, long start
, long end
) {
426 long resolution
= (end
- start
) / getDisplayWidth();
427 long startTime
= Math
.max(start
, traceEntry
.getStartTime());
428 long endTime
= Math
.min(end
+ 1, traceEntry
.getEndTime());
429 List
<ITimeEvent
> eventList
= getEventList(traceEntry
, startTime
, endTime
, resolution
, monitor
);
430 if (monitor
.isCanceled()) {
433 traceEntry
.setEventList(eventList
);
436 for (ITimeGraphEntry entry
: traceEntry
.getChildren()) {
437 if (monitor
.isCanceled()) {
440 XmlEntry xmlEntry
= (XmlEntry
) entry
;
441 buildStatusEvent(xmlEntry
, monitor
, start
, end
);
445 /** Build a tree using getParentId() and getId() */
446 private static void buildTree(Map
<String
, XmlEntry
> entryMap
, XmlEntry rootEntry
) {
447 for (XmlEntry entry
: entryMap
.values()) {
449 if (!entry
.getParentId().isEmpty()) {
450 XmlEntry parent
= entryMap
.get(entry
.getParentId());
451 if (parent
!= null &&
452 entry
.getStartTime() >= parent
.getStartTime() &&
453 entry
.getStartTime() <= parent
.getEndTime()) {
454 parent
.addChild(entry
);
459 rootEntry
.addChild(entry
);
465 protected List
<ITimeEvent
> getEventList(TimeGraphEntry entry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
466 if (!(entry
instanceof XmlEntry
)) {
467 return Collections
.EMPTY_LIST
;
469 XmlEntry xmlEntry
= (XmlEntry
) entry
;
470 ITmfStateSystem ssq
= xmlEntry
.getStateSystem();
471 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
472 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
473 if (realEnd
<= realStart
) {
476 List
<ITimeEvent
> eventList
= null;
477 int quark
= xmlEntry
.getDisplayQuark();
480 if (xmlEntry
.getType() == EntryDisplayType
.DISPLAY
) {
482 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(quark
, realStart
, realEnd
- 1, resolution
, monitor
);
483 eventList
= new ArrayList
<>(statusIntervals
.size());
484 long lastEndTime
= -1;
485 for (ITmfStateInterval statusInterval
: statusIntervals
) {
486 if (monitor
.isCanceled()) {
489 int status
= statusInterval
.getStateValue().unboxInt();
490 long time
= statusInterval
.getStartTime();
491 long duration
= statusInterval
.getEndTime() - time
+ 1;
492 if (!statusInterval
.getStateValue().isNull()) {
493 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
494 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
496 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
497 } else if (lastEndTime
== -1 || time
+ duration
>= endTime
) {
498 // add null event if it intersects the start or end time
499 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
501 lastEndTime
= time
+ duration
;
504 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException
| StateSystemDisposedException e
) {
511 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
512 /* TODO: not implemented yet, need XML to go along */
513 return Collections
.EMPTY_LIST
;