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
.StateSystemUtils
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
40 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
41 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
42 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
43 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
44 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.readonly
.TmfXmlReadOnlyModelFactory
;
45 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
46 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
47 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
48 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.ui
.views
.timegraph
.XmlEntry
.EntryDisplayType
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
61 import org
.w3c
.dom
.Element
;
64 * This view displays state system data in a time graph view. It uses an XML
65 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
66 * element defines which entries from the state system will be shown and also
67 * gives additional information on the presentation of the view (states, colors,
70 * @author Florian Wininger
72 public class XmlTimeGraphView
extends AbstractTimeGraphView
{
75 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
77 private static final String
[] DEFAULT_COLUMN_NAMES
= new String
[] {
78 Messages
.XmlTimeGraphView_ColumnName
,
79 Messages
.XmlTimeGraphView_ColumnId
,
80 Messages
.XmlTimeGraphView_ColumnParentId
,
83 private static final String
[] DEFAULT_FILTER_COLUMN_NAMES
= new String
[] {
84 Messages
.XmlTimeGraphView_ColumnName
,
85 Messages
.XmlTimeGraphView_ColumnId
88 /** The relative weight of the sash */
89 private static final int[] fWeight
= { 1, 2 };
91 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
92 private static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
94 private final @NonNull XmlViewInfo fViewInfo
= new XmlViewInfo(ID
);
95 private final ITmfXmlModelFactory fFactory
;
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 * Default constructor
104 public XmlTimeGraphView() {
105 super(ID
, new XmlPresentationProvider());
107 setTreeColumns(DEFAULT_COLUMN_NAMES
);
108 setTreeLabelProvider(new XmlTreeLabelProvider());
109 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES
);
110 setFilterLabelProvider(new XmlTreeLabelProvider());
111 setEntryComparator(new XmlEntryComparator());
112 this.addPartPropertyListener(new IPropertyChangeListener() {
114 public void propertyChange(PropertyChangeEvent event
) {
115 if (event
.getProperty().equals(TmfXmlUiStrings
.XML_OUTPUT_DATA
)) {
116 Object newValue
= event
.getNewValue();
117 if (newValue
instanceof String
) {
118 String data
= (String
) newValue
;
119 fViewInfo
.setViewData(data
);
126 fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
129 private void loadNewXmlView() {
133 private void setViewTitle(final String title
) {
134 Display
.getDefault().asyncExec(new Runnable() {
144 protected String
getNextText() {
145 return Messages
.XmlTimeGraphView_NextText
;
149 protected String
getNextTooltip() {
150 return Messages
.XmlTimeGraphView_NextTooltip
;
154 protected String
getPrevText() {
155 return Messages
.XmlTimeGraphView_PreviousText
;
159 protected String
getPrevTooltip() {
160 return Messages
.XmlTimeGraphView_PreviousInterval
;
164 * Default label provider, it shows name, id and parent columns
166 * TODO: There should be a way to define columns in the XML
168 private static class XmlTreeLabelProvider
extends TreeLabelProvider
{
171 public String
getColumnText(Object element
, int columnIndex
) {
172 XmlEntry entry
= (XmlEntry
) element
;
174 if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnName
)) {
175 return entry
.getName();
176 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnId
)) {
177 return entry
.getId();
178 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnParentId
)) {
179 return entry
.getParentId();
186 private static class XmlEntryComparator
implements Comparator
<ITimeGraphEntry
> {
189 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
193 if ((o1
instanceof XmlEntry
) && (o2
instanceof XmlEntry
)) {
194 XmlEntry entry1
= (XmlEntry
) o1
;
195 XmlEntry entry2
= (XmlEntry
) o2
;
196 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
198 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
201 result
= entry1
.getName().compareTo(entry2
.getName());
206 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
213 // ------------------------------------------------------------------------
215 // ------------------------------------------------------------------------
218 protected void buildEventList(ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
221 * Get the view element from the XML file. If the element can't be
224 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.TIME_GRAPH_VIEW
);
225 if (viewElement
== null) {
228 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
229 if (pres
instanceof XmlPresentationProvider
) {
231 * TODO: Each entry of a line could have their own states/color.
232 * That will require an update to the presentation provider
234 ((XmlPresentationProvider
) pres
).loadNewStates(viewElement
);
237 String title
= fViewInfo
.getViewTitle(viewElement
);
239 title
= Messages
.XmlTimeGraphView_DefaultTitle
;
242 Set
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
244 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
245 Set
<XmlEntry
> entryList
= new TreeSet
<>(getEntryComparator());
246 for (ITmfTrace aTrace
: TmfTraceManager
.getTraceSet(trace
)) {
247 if (monitor
.isCanceled()) {
251 List
<ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new LinkedList
<>();
252 if (analysisIds
.isEmpty()) {
254 * No analysis specified, take all state system analysis modules
256 for (ITmfAnalysisModuleWithStateSystems module
: aTrace
.getAnalysisModulesOfClass(ITmfAnalysisModuleWithStateSystems
.class)) {
257 stateSystemModules
.add(module
);
260 for (String moduleId
: analysisIds
) {
261 ITmfAnalysisModuleWithStateSystems module
= aTrace
.getAnalysisModuleOfClass(ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
262 if (module
!= null) {
263 stateSystemModules
.add(module
);
268 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
269 IStatus status
= module
.schedule();
270 if (!status
.isOK()) {
273 if (module
instanceof TmfStateSystemAnalysisModule
) {
274 ((TmfStateSystemAnalysisModule
) module
).waitForInitialization();
276 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
280 ssq
.waitUntilBuilt();
282 long startTime
= ssq
.getStartTime();
283 long endTime
= ssq
.getCurrentEndTime();
284 XmlEntry groupEntry
= new XmlEntry(-1, aTrace
, aTrace
.getName(), ssq
);
285 entryList
.add(groupEntry
);
286 setStartTime(Math
.min(getStartTime(), startTime
));
287 setEndTime(Math
.max(getEndTime(), endTime
));
289 /* Add children entry of this entry for each line */
290 for (Element entry
: entries
) {
291 buildEntry(entry
, groupEntry
, -1);
296 putEntryList(trace
, new ArrayList
<TimeGraphEntry
>(entryList
));
298 if (trace
.equals(getTrace())) {
301 for (XmlEntry traceEntry
: entryList
) {
302 if (monitor
.isCanceled()) {
305 long startTime
= traceEntry
.getStateSystem().getStartTime();
306 long endTime
= traceEntry
.getStateSystem().getCurrentEndTime() + 1;
307 buildStatusEvent(traceEntry
, monitor
, startTime
, endTime
);
311 private void buildEntry(Element entryElement
, XmlEntry parentEntry
, int baseQuark
) {
312 /* Get the attribute string to display */
313 String path
= entryElement
.getAttribute(TmfXmlUiStrings
.PATH
);
314 if (path
.isEmpty()) {
315 path
= TmfXmlStrings
.WILDCARD
;
319 * Make sure the XML element has either a display attribute or entries,
320 * otherwise issue a warning
323 List
<Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
324 List
<Element
> entryElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
326 if (displayElements
.isEmpty() && entryElements
.isEmpty()) {
327 Activator
.logWarning(String
.format("XML view: entry for %s should have either a display element or entry elements", path
)); //$NON-NLS-1$
331 ITmfStateSystem ss
= parentEntry
.getStateSystem();
333 /* Get the list of quarks to process with this path */
334 String
[] paths
= path
.split(SPLIT_STRING
);
336 List
<Integer
> quarks
= Collections
.singletonList(baseQuark
);
339 while (i
< paths
.length
) {
340 List
<Integer
> subQuarks
= new LinkedList
<>();
341 /* Replace * by .* to have a regex string */
342 String name
= paths
[i
].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
343 for (int relativeQuark
: quarks
) {
344 for (int quark
: ss
.getSubAttributes(relativeQuark
, false, name
)) {
345 subQuarks
.add(quark
);
352 /* Process each quark */
353 XmlEntry currentEntry
= parentEntry
;
354 Element displayElement
= null;
355 Map
<String
, XmlEntry
> entryMap
= new HashMap
<>();
356 if (!displayElements
.isEmpty()) {
357 displayElement
= displayElements
.get(0);
359 for (int quark
: quarks
) {
360 currentEntry
= parentEntry
;
361 /* Process the current entry, if specified */
362 if (displayElement
!= null) {
363 currentEntry
= processEntry(entryElement
, displayElement
, parentEntry
, quark
, ss
);
364 entryMap
.put(currentEntry
.getId(), currentEntry
);
366 /* Process the children entry of this entry */
367 for (Element subEntryEl
: entryElements
) {
368 buildEntry(subEntryEl
, currentEntry
, quark
);
371 if (!entryMap
.isEmpty()) {
372 buildTree(entryMap
, parentEntry
);
374 } catch (AttributeNotFoundException e
) {
378 private XmlEntry
processEntry(@NonNull Element entryElement
, @NonNull Element displayEl
,
379 XmlEntry parentEntry
, int quark
, ITmfStateSystem ss
) {
381 * Get the start time and end time of this entry from the display
384 ITmfXmlStateAttribute display
= fFactory
.createStateAttribute(displayEl
, parentEntry
);
385 int displayQuark
= display
.getAttributeQuark(quark
);
386 if (displayQuark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
387 return new XmlEntry(quark
, parentEntry
.getTrace(),
388 String
.format("Unknown display quark for %s", ss
.getAttributeName(quark
)), ss
); //$NON-NLS-1$
391 long entryStart
= ss
.getStartTime();
392 long entryEnd
= ss
.getCurrentEndTime();
396 ITmfStateInterval oneInterval
= ss
.querySingleState(entryStart
, displayQuark
);
398 /* The entry start is the first non-null interval */
399 while (oneInterval
.getStateValue().isNull()) {
400 long ts
= oneInterval
.getEndTime() + 1;
401 if (ts
> ss
.getCurrentEndTime()) {
404 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
406 entryStart
= oneInterval
.getStartTime();
408 /* The entry end is the last non-null interval */
409 oneInterval
= ss
.querySingleState(entryEnd
, displayQuark
);
410 while (oneInterval
.getStateValue().isNull()) {
411 long ts
= oneInterval
.getStartTime() - 1;
412 if (ts
< ss
.getStartTime()) {
415 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
417 entryEnd
= oneInterval
.getEndTime();
419 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
422 return new XmlEntry(quark
, displayQuark
, parentEntry
.getTrace(), ss
.getAttributeName(quark
),
423 entryStart
, entryEnd
, EntryDisplayType
.DISPLAY
, ss
, entryElement
);
426 private void buildStatusEvent(XmlEntry traceEntry
, IProgressMonitor monitor
, long start
, long end
) {
427 long resolution
= (end
- start
) / getDisplayWidth();
428 long startTime
= Math
.max(start
, traceEntry
.getStartTime());
429 long endTime
= Math
.min(end
+ 1, traceEntry
.getEndTime());
430 List
<ITimeEvent
> eventList
= getEventList(traceEntry
, startTime
, endTime
, resolution
, monitor
);
431 if (monitor
.isCanceled()) {
434 traceEntry
.setEventList(eventList
);
437 for (ITimeGraphEntry entry
: traceEntry
.getChildren()) {
438 if (monitor
.isCanceled()) {
441 XmlEntry xmlEntry
= (XmlEntry
) entry
;
442 buildStatusEvent(xmlEntry
, monitor
, start
, end
);
446 /** Build a tree using getParentId() and getId() */
447 private static void buildTree(Map
<String
, XmlEntry
> entryMap
, XmlEntry rootEntry
) {
448 for (XmlEntry entry
: entryMap
.values()) {
450 if (!entry
.getParentId().isEmpty()) {
451 XmlEntry parent
= entryMap
.get(entry
.getParentId());
452 if (parent
!= null &&
453 entry
.getStartTime() >= parent
.getStartTime() &&
454 entry
.getStartTime() <= parent
.getEndTime()) {
455 parent
.addChild(entry
);
460 rootEntry
.addChild(entry
);
466 protected List
<ITimeEvent
> getEventList(TimeGraphEntry entry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
467 if (!(entry
instanceof XmlEntry
)) {
468 return Collections
.EMPTY_LIST
;
470 XmlEntry xmlEntry
= (XmlEntry
) entry
;
471 ITmfStateSystem ssq
= xmlEntry
.getStateSystem();
472 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
473 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
474 if (realEnd
<= realStart
) {
477 List
<ITimeEvent
> eventList
= null;
478 int quark
= xmlEntry
.getDisplayQuark();
481 if (xmlEntry
.getType() == EntryDisplayType
.DISPLAY
) {
483 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, quark
, realStart
, realEnd
- 1, resolution
, monitor
);
484 eventList
= new ArrayList
<>(statusIntervals
.size());
485 long lastEndTime
= -1;
486 for (ITmfStateInterval statusInterval
: statusIntervals
) {
487 if (monitor
.isCanceled()) {
490 int status
= statusInterval
.getStateValue().unboxInt();
491 long time
= statusInterval
.getStartTime();
492 long duration
= statusInterval
.getEndTime() - time
+ 1;
493 if (!statusInterval
.getStateValue().isNull()) {
494 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
495 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
497 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
498 } else if (lastEndTime
== -1 || time
+ duration
>= endTime
) {
499 // add null event if it intersects the start or end time
500 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
502 lastEndTime
= time
+ duration
;
505 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException
| StateSystemDisposedException e
) {
512 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
513 /* TODO: not implemented yet, need XML to go along */
514 return Collections
.EMPTY_LIST
;