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
.jdt
.annotation
.NonNull
;
28 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
29 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.Activator
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.XmlViewInfo
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
40 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
41 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
42 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.readonly
.TmfXmlReadOnlyModelFactory
;
43 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
44 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
45 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
46 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.ui
.views
.timegraph
.XmlEntry
.EntryDisplayType
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
59 import org
.w3c
.dom
.Element
;
62 * This view displays state system data in a time graph view. It uses an XML
63 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
64 * element defines which entries from the state system will be shown and also
65 * gives additional information on the presentation of the view (states, colors,
68 * @author Florian Wininger
70 public class XmlTimeGraphView
extends AbstractTimeGraphView
{
73 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
75 private static final String
[] DEFAULT_COLUMN_NAMES
= new String
[] {
76 Messages
.XmlTimeGraphView_ColumnName
,
77 Messages
.XmlTimeGraphView_ColumnId
,
78 Messages
.XmlTimeGraphView_ColumnParentId
,
81 private static final String
[] DEFAULT_FILTER_COLUMN_NAMES
= new String
[] {
82 Messages
.XmlTimeGraphView_ColumnName
,
83 Messages
.XmlTimeGraphView_ColumnId
86 /** The relative weight of the sash */
87 private static final int[] fWeight
= { 1, 2 };
89 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
90 private static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
92 private final @NonNull XmlViewInfo fViewInfo
= new XmlViewInfo(ID
);
93 private final ITmfXmlModelFactory fFactory
;
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 * Default constructor
102 public XmlTimeGraphView() {
103 super(ID
, new XmlPresentationProvider());
105 setTreeColumns(DEFAULT_COLUMN_NAMES
);
106 setTreeLabelProvider(new XmlTreeLabelProvider());
107 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES
);
108 setFilterLabelProvider(new XmlTreeLabelProvider());
109 setEntryComparator(new XmlEntryComparator());
110 this.addPartPropertyListener(new IPropertyChangeListener() {
112 public void propertyChange(PropertyChangeEvent event
) {
113 if (event
.getProperty().equals(TmfXmlUiStrings
.XML_OUTPUT_DATA
)) {
114 Object newValue
= event
.getNewValue();
115 if (newValue
instanceof String
) {
116 String data
= (String
) newValue
;
117 fViewInfo
.setViewData(data
);
124 fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
127 private void loadNewXmlView() {
131 private void setViewTitle(final String title
) {
132 Display
.getDefault().asyncExec(new Runnable() {
142 protected String
getNextText() {
143 return Messages
.XmlTimeGraphView_NextText
;
147 protected String
getNextTooltip() {
148 return Messages
.XmlTimeGraphView_NextTooltip
;
152 protected String
getPrevText() {
153 return Messages
.XmlTimeGraphView_PreviousText
;
157 protected String
getPrevTooltip() {
158 return Messages
.XmlTimeGraphView_PreviousInterval
;
162 * Default label provider, it shows name, id and parent columns
164 * TODO: There should be a way to define columns in the XML
166 private static class XmlTreeLabelProvider
extends TreeLabelProvider
{
169 public String
getColumnText(Object element
, int columnIndex
) {
170 XmlEntry entry
= (XmlEntry
) element
;
172 if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnName
)) {
173 return entry
.getName();
174 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnId
)) {
175 return entry
.getId();
176 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnParentId
)) {
177 return entry
.getParentId();
184 private static class XmlEntryComparator
implements Comparator
<ITimeGraphEntry
> {
187 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
191 if ((o1
instanceof XmlEntry
) && (o2
instanceof XmlEntry
)) {
192 XmlEntry entry1
= (XmlEntry
) o1
;
193 XmlEntry entry2
= (XmlEntry
) o2
;
194 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
196 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
199 result
= entry1
.getName().compareTo(entry2
.getName());
204 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
211 // ------------------------------------------------------------------------
213 // ------------------------------------------------------------------------
216 protected void buildEventList(ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
219 * Get the view element from the XML file. If the element can't be
222 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.TIME_GRAPH_VIEW
);
223 if (viewElement
== null) {
226 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
227 if (pres
instanceof XmlPresentationProvider
) {
229 * TODO: Each entry of a line could have their own states/color.
230 * That will require an update to the presentation provider
232 ((XmlPresentationProvider
) pres
).loadNewStates(viewElement
);
235 String title
= fViewInfo
.getViewTitle(viewElement
);
237 title
= Messages
.XmlTimeGraphView_DefaultTitle
;
240 Set
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
242 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
243 Set
<XmlEntry
> entryList
= new TreeSet
<>(getEntryComparator());
244 for (ITmfTrace aTrace
: TmfTraceManager
.getTraceSet(trace
)) {
245 if (monitor
.isCanceled()) {
249 List
<ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new LinkedList
<>();
250 if (analysisIds
.isEmpty()) {
252 * No analysis specified, take all state system analysis modules
254 for (ITmfAnalysisModuleWithStateSystems module
: aTrace
.getAnalysisModulesOfClass(ITmfAnalysisModuleWithStateSystems
.class)) {
255 stateSystemModules
.add(module
);
258 for (String moduleId
: analysisIds
) {
259 ITmfAnalysisModuleWithStateSystems module
= aTrace
.getAnalysisModuleOfClass(ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
260 if (module
!= null) {
261 stateSystemModules
.add(module
);
266 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
268 if (module
instanceof TmfStateSystemAnalysisModule
) {
269 ((TmfStateSystemAnalysisModule
) module
).waitForInitialization();
271 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
275 ssq
.waitUntilBuilt();
277 long startTime
= ssq
.getStartTime();
278 long endTime
= ssq
.getCurrentEndTime();
279 XmlEntry groupEntry
= new XmlEntry(-1, aTrace
, aTrace
.getName(), ssq
);
280 entryList
.add(groupEntry
);
281 setStartTime(Math
.min(getStartTime(), startTime
));
282 setEndTime(Math
.max(getEndTime(), endTime
));
284 /* Add children entry of this entry for each line */
285 for (Element entry
: entries
) {
286 buildEntry(entry
, groupEntry
, -1);
291 putEntryList(trace
, new ArrayList
<TimeGraphEntry
>(entryList
));
293 if (trace
.equals(getTrace())) {
296 for (XmlEntry traceEntry
: entryList
) {
297 if (monitor
.isCanceled()) {
300 long startTime
= traceEntry
.getStateSystem().getStartTime();
301 long endTime
= traceEntry
.getStateSystem().getCurrentEndTime() + 1;
302 buildStatusEvent(traceEntry
, monitor
, startTime
, endTime
);
306 private void buildEntry(Element entryElement
, XmlEntry parentEntry
, int baseQuark
) {
307 /* Get the attribute string to display */
308 String path
= entryElement
.getAttribute(TmfXmlUiStrings
.PATH
);
309 if (path
.isEmpty()) {
310 path
= TmfXmlStrings
.WILDCARD
;
314 * Make sure the XML element has either a display attribute or entries,
315 * otherwise issue a warning
318 List
<Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
319 List
<Element
> entryElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
321 if (displayElements
.isEmpty() && entryElements
.isEmpty()) {
322 Activator
.logWarning(String
.format("XML view: entry for %s should have either a display element or entry elements", path
)); //$NON-NLS-1$
326 ITmfStateSystem ss
= parentEntry
.getStateSystem();
328 /* Get the list of quarks to process with this path */
329 String
[] paths
= path
.split(SPLIT_STRING
);
331 List
<Integer
> quarks
= Collections
.singletonList(baseQuark
);
334 while (i
< paths
.length
) {
335 List
<Integer
> subQuarks
= new LinkedList
<>();
336 /* Replace * by .* to have a regex string */
337 String name
= paths
[i
].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
338 for (int relativeQuark
: quarks
) {
339 for (int quark
: ss
.getSubAttributes(relativeQuark
, false, name
)) {
340 subQuarks
.add(quark
);
347 /* Process each quark */
348 XmlEntry currentEntry
= parentEntry
;
349 Element displayElement
= null;
350 Map
<String
, XmlEntry
> entryMap
= new HashMap
<>();
351 if (!displayElements
.isEmpty()) {
352 displayElement
= displayElements
.get(0);
354 for (int quark
: quarks
) {
355 currentEntry
= parentEntry
;
356 /* Process the current entry, if specified */
357 if (displayElement
!= null) {
358 currentEntry
= processEntry(entryElement
, displayElement
, parentEntry
, quark
, ss
);
359 entryMap
.put(currentEntry
.getId(), currentEntry
);
361 /* Process the children entry of this entry */
362 for (Element subEntryEl
: entryElements
) {
363 buildEntry(subEntryEl
, currentEntry
, quark
);
366 if (!entryMap
.isEmpty()) {
367 buildTree(entryMap
, parentEntry
);
369 } catch (AttributeNotFoundException e
) {
373 private XmlEntry
processEntry(@NonNull Element entryElement
, @NonNull Element displayEl
,
374 XmlEntry parentEntry
, int quark
, ITmfStateSystem ss
) {
376 * Get the start time and end time of this entry from the display
379 ITmfXmlStateAttribute display
= fFactory
.createStateAttribute(displayEl
, parentEntry
);
380 int displayQuark
= display
.getAttributeQuark(quark
);
381 if (displayQuark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
382 return new XmlEntry(quark
, parentEntry
.getTrace(),
383 String
.format("Unknown display quark for %s", ss
.getAttributeName(quark
)), ss
); //$NON-NLS-1$
386 long entryStart
= ss
.getStartTime();
387 long entryEnd
= ss
.getCurrentEndTime();
391 ITmfStateInterval oneInterval
= ss
.querySingleState(entryStart
, displayQuark
);
393 /* The entry start is the first non-null interval */
394 while (oneInterval
.getStateValue().isNull()) {
395 long ts
= oneInterval
.getEndTime() + 1;
396 if (ts
> ss
.getCurrentEndTime()) {
399 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
401 entryStart
= oneInterval
.getStartTime();
403 /* The entry end is the last non-null interval */
404 oneInterval
= ss
.querySingleState(entryEnd
, displayQuark
);
405 while (oneInterval
.getStateValue().isNull()) {
406 long ts
= oneInterval
.getStartTime() - 1;
407 if (ts
< ss
.getStartTime()) {
410 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
412 entryEnd
= oneInterval
.getEndTime();
414 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
417 return new XmlEntry(quark
, displayQuark
, parentEntry
.getTrace(), ss
.getAttributeName(quark
),
418 entryStart
, entryEnd
, EntryDisplayType
.DISPLAY
, ss
, entryElement
);
421 private void buildStatusEvent(XmlEntry traceEntry
, IProgressMonitor monitor
, long start
, long end
) {
422 long resolution
= (end
- start
) / getDisplayWidth();
423 long startTime
= Math
.max(start
, traceEntry
.getStartTime());
424 long endTime
= Math
.min(end
+ 1, traceEntry
.getEndTime());
425 List
<ITimeEvent
> eventList
= getEventList(traceEntry
, startTime
, endTime
, resolution
, monitor
);
426 if (monitor
.isCanceled()) {
429 traceEntry
.setEventList(eventList
);
432 for (ITimeGraphEntry entry
: traceEntry
.getChildren()) {
433 if (monitor
.isCanceled()) {
436 XmlEntry xmlEntry
= (XmlEntry
) entry
;
437 buildStatusEvent(xmlEntry
, monitor
, start
, end
);
441 /** Build a tree using getParentId() and getId() */
442 private static void buildTree(Map
<String
, XmlEntry
> entryMap
, XmlEntry rootEntry
) {
443 for (XmlEntry entry
: entryMap
.values()) {
445 if (!entry
.getParentId().isEmpty()) {
446 XmlEntry parent
= entryMap
.get(entry
.getParentId());
447 if (parent
!= null &&
448 entry
.getStartTime() >= parent
.getStartTime() &&
449 entry
.getStartTime() <= parent
.getEndTime()) {
450 parent
.addChild(entry
);
455 rootEntry
.addChild(entry
);
461 protected List
<ITimeEvent
> getEventList(TimeGraphEntry entry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
462 if (!(entry
instanceof XmlEntry
)) {
463 return Collections
.EMPTY_LIST
;
465 XmlEntry xmlEntry
= (XmlEntry
) entry
;
466 ITmfStateSystem ssq
= xmlEntry
.getStateSystem();
467 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
468 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
469 if (realEnd
<= realStart
) {
472 List
<ITimeEvent
> eventList
= null;
473 int quark
= xmlEntry
.getDisplayQuark();
476 if (xmlEntry
.getType() == EntryDisplayType
.DISPLAY
) {
478 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(quark
, realStart
, realEnd
- 1, resolution
, monitor
);
479 eventList
= new ArrayList
<>(statusIntervals
.size());
480 long lastEndTime
= -1;
481 for (ITmfStateInterval statusInterval
: statusIntervals
) {
482 if (monitor
.isCanceled()) {
485 int status
= statusInterval
.getStateValue().unboxInt();
486 long time
= statusInterval
.getStartTime();
487 long duration
= statusInterval
.getEndTime() - time
+ 1;
488 if (!statusInterval
.getStateValue().isNull()) {
489 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
490 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
492 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
493 } else if (lastEndTime
== -1 || time
+ duration
>= endTime
) {
494 // add null event if it intersects the start or end time
495 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
497 lastEndTime
= time
+ duration
;
500 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException
| StateSystemDisposedException e
) {
507 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
508 /* TODO: not implemented yet, need XML to go along */
509 return Collections
.EMPTY_LIST
;