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
.core
.trace
.TmfTraceUtils
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
62 import org
.w3c
.dom
.Element
;
65 * This view displays state system data in a time graph view. It uses an XML
66 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
67 * element defines which entries from the state system will be shown and also
68 * gives additional information on the presentation of the view (states, colors,
71 * @author Florian Wininger
73 public class XmlTimeGraphView
extends AbstractTimeGraphView
{
76 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
78 private static final String
[] DEFAULT_COLUMN_NAMES
= new String
[] {
79 Messages
.XmlTimeGraphView_ColumnName
,
80 Messages
.XmlTimeGraphView_ColumnId
,
81 Messages
.XmlTimeGraphView_ColumnParentId
,
84 private static final String
[] DEFAULT_FILTER_COLUMN_NAMES
= new String
[] {
85 Messages
.XmlTimeGraphView_ColumnName
,
86 Messages
.XmlTimeGraphView_ColumnId
89 /** The relative weight of the sash */
90 private static final int[] fWeight
= { 1, 2 };
92 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
93 private static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
95 private final @NonNull XmlViewInfo fViewInfo
= new XmlViewInfo(ID
);
96 private final ITmfXmlModelFactory fFactory
;
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 * Default constructor
105 public XmlTimeGraphView() {
106 super(ID
, new XmlPresentationProvider());
108 setTreeColumns(DEFAULT_COLUMN_NAMES
);
109 setTreeLabelProvider(new XmlTreeLabelProvider());
110 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES
);
111 setFilterLabelProvider(new XmlTreeLabelProvider());
112 setEntryComparator(new XmlEntryComparator());
113 this.addPartPropertyListener(new IPropertyChangeListener() {
115 public void propertyChange(PropertyChangeEvent event
) {
116 if (event
.getProperty().equals(TmfXmlUiStrings
.XML_OUTPUT_DATA
)) {
117 Object newValue
= event
.getNewValue();
118 if (newValue
instanceof String
) {
119 String data
= (String
) newValue
;
120 fViewInfo
.setViewData(data
);
127 fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
130 private void loadNewXmlView() {
134 private void setViewTitle(final String title
) {
135 Display
.getDefault().asyncExec(new Runnable() {
145 protected String
getNextText() {
146 return Messages
.XmlTimeGraphView_NextText
;
150 protected String
getNextTooltip() {
151 return Messages
.XmlTimeGraphView_NextTooltip
;
155 protected String
getPrevText() {
156 return Messages
.XmlTimeGraphView_PreviousText
;
160 protected String
getPrevTooltip() {
161 return Messages
.XmlTimeGraphView_PreviousInterval
;
165 * Default label provider, it shows name, id and parent columns
167 * TODO: There should be a way to define columns in the XML
169 private static class XmlTreeLabelProvider
extends TreeLabelProvider
{
172 public String
getColumnText(Object element
, int columnIndex
) {
173 XmlEntry entry
= (XmlEntry
) element
;
175 if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnName
)) {
176 return entry
.getName();
177 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnId
)) {
178 return entry
.getId();
179 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnParentId
)) {
180 return entry
.getParentId();
187 private static class XmlEntryComparator
implements Comparator
<ITimeGraphEntry
> {
190 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
194 if ((o1
instanceof XmlEntry
) && (o2
instanceof XmlEntry
)) {
195 XmlEntry entry1
= (XmlEntry
) o1
;
196 XmlEntry entry2
= (XmlEntry
) o2
;
197 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
199 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
202 result
= entry1
.getName().compareTo(entry2
.getName());
207 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
214 // ------------------------------------------------------------------------
216 // ------------------------------------------------------------------------
219 protected void buildEventList(ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
222 * Get the view element from the XML file. If the element can't be
225 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.TIME_GRAPH_VIEW
);
226 if (viewElement
== null) {
229 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
230 if (pres
instanceof XmlPresentationProvider
) {
232 * TODO: Each entry of a line could have their own states/color.
233 * That will require an update to the presentation provider
235 ((XmlPresentationProvider
) pres
).loadNewStates(viewElement
);
238 String title
= fViewInfo
.getViewTitle(viewElement
);
240 title
= Messages
.XmlTimeGraphView_DefaultTitle
;
243 Set
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
245 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
246 Set
<XmlEntry
> entryList
= new TreeSet
<>(getEntryComparator());
247 for (ITmfTrace aTrace
: TmfTraceManager
.getTraceSet(trace
)) {
248 if (monitor
.isCanceled()) {
252 List
<ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new LinkedList
<>();
253 if (analysisIds
.isEmpty()) {
255 * No analysis specified, take all state system analysis modules
257 for (ITmfAnalysisModuleWithStateSystems module
: TmfTraceUtils
.getAnalysisModulesOfClass(aTrace
, ITmfAnalysisModuleWithStateSystems
.class)) {
258 stateSystemModules
.add(module
);
261 for (String moduleId
: analysisIds
) {
262 ITmfAnalysisModuleWithStateSystems module
= TmfTraceUtils
.getAnalysisModuleOfClass(aTrace
, ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
263 if (module
!= null) {
264 stateSystemModules
.add(module
);
269 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
270 IStatus status
= module
.schedule();
271 if (!status
.isOK()) {
274 if (module
instanceof TmfStateSystemAnalysisModule
) {
275 ((TmfStateSystemAnalysisModule
) module
).waitForInitialization();
277 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
281 ssq
.waitUntilBuilt();
283 long startTime
= ssq
.getStartTime();
284 long endTime
= ssq
.getCurrentEndTime();
285 XmlEntry groupEntry
= new XmlEntry(-1, aTrace
, aTrace
.getName(), ssq
);
286 entryList
.add(groupEntry
);
287 setStartTime(Math
.min(getStartTime(), startTime
));
288 setEndTime(Math
.max(getEndTime(), endTime
));
290 /* Add children entry of this entry for each line */
291 for (Element entry
: entries
) {
292 buildEntry(entry
, groupEntry
, -1);
297 putEntryList(trace
, new ArrayList
<TimeGraphEntry
>(entryList
));
299 if (trace
.equals(getTrace())) {
302 for (XmlEntry traceEntry
: entryList
) {
303 if (monitor
.isCanceled()) {
306 long startTime
= traceEntry
.getStateSystem().getStartTime();
307 long endTime
= traceEntry
.getStateSystem().getCurrentEndTime() + 1;
308 buildStatusEvent(traceEntry
, monitor
, startTime
, endTime
);
312 private void buildEntry(Element entryElement
, XmlEntry parentEntry
, int baseQuark
) {
313 /* Get the attribute string to display */
314 String path
= entryElement
.getAttribute(TmfXmlUiStrings
.PATH
);
315 if (path
.isEmpty()) {
316 path
= TmfXmlStrings
.WILDCARD
;
320 * Make sure the XML element has either a display attribute or entries,
321 * otherwise issue a warning
324 List
<Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
325 List
<Element
> entryElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
327 if (displayElements
.isEmpty() && entryElements
.isEmpty()) {
328 Activator
.logWarning(String
.format("XML view: entry for %s should have either a display element or entry elements", path
)); //$NON-NLS-1$
332 ITmfStateSystem ss
= parentEntry
.getStateSystem();
334 /* Get the list of quarks to process with this path */
335 String
[] paths
= path
.split(SPLIT_STRING
);
337 List
<Integer
> quarks
= Collections
.singletonList(baseQuark
);
340 while (i
< paths
.length
) {
341 List
<Integer
> subQuarks
= new LinkedList
<>();
342 /* Replace * by .* to have a regex string */
343 String name
= paths
[i
].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
344 for (int relativeQuark
: quarks
) {
345 for (int quark
: ss
.getSubAttributes(relativeQuark
, false, name
)) {
346 subQuarks
.add(quark
);
353 /* Process each quark */
354 XmlEntry currentEntry
= parentEntry
;
355 Element displayElement
= null;
356 Map
<String
, XmlEntry
> entryMap
= new HashMap
<>();
357 if (!displayElements
.isEmpty()) {
358 displayElement
= displayElements
.get(0);
360 for (int quark
: quarks
) {
361 currentEntry
= parentEntry
;
362 /* Process the current entry, if specified */
363 if (displayElement
!= null) {
364 currentEntry
= processEntry(entryElement
, displayElement
, parentEntry
, quark
, ss
);
365 entryMap
.put(currentEntry
.getId(), currentEntry
);
367 /* Process the children entry of this entry */
368 for (Element subEntryEl
: entryElements
) {
369 buildEntry(subEntryEl
, currentEntry
, quark
);
372 if (!entryMap
.isEmpty()) {
373 buildTree(entryMap
, parentEntry
);
375 } catch (AttributeNotFoundException e
) {
379 private XmlEntry
processEntry(@NonNull Element entryElement
, @NonNull Element displayEl
,
380 @NonNull XmlEntry parentEntry
, int quark
, ITmfStateSystem ss
) {
382 * Get the start time and end time of this entry from the display
385 ITmfXmlStateAttribute display
= fFactory
.createStateAttribute(displayEl
, parentEntry
);
386 int displayQuark
= display
.getAttributeQuark(quark
);
387 if (displayQuark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
388 return new XmlEntry(quark
, parentEntry
.getTrace(),
389 String
.format("Unknown display quark for %s", ss
.getAttributeName(quark
)), ss
); //$NON-NLS-1$
392 long entryStart
= ss
.getStartTime();
393 long entryEnd
= ss
.getCurrentEndTime();
397 ITmfStateInterval oneInterval
= ss
.querySingleState(entryStart
, displayQuark
);
399 /* The entry start is the first non-null interval */
400 while (oneInterval
.getStateValue().isNull()) {
401 long ts
= oneInterval
.getEndTime() + 1;
402 if (ts
> ss
.getCurrentEndTime()) {
405 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
407 entryStart
= oneInterval
.getStartTime();
409 /* The entry end is the last non-null interval */
410 oneInterval
= ss
.querySingleState(entryEnd
, displayQuark
);
411 while (oneInterval
.getStateValue().isNull()) {
412 long ts
= oneInterval
.getStartTime() - 1;
413 if (ts
< ss
.getStartTime()) {
416 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
418 entryEnd
= oneInterval
.getEndTime();
420 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
423 return new XmlEntry(quark
, displayQuark
, parentEntry
.getTrace(), ss
.getAttributeName(quark
),
424 entryStart
, entryEnd
, EntryDisplayType
.DISPLAY
, ss
, entryElement
);
427 private void buildStatusEvent(XmlEntry traceEntry
, @NonNull IProgressMonitor monitor
, long start
, long end
) {
428 long resolution
= (end
- start
) / getDisplayWidth();
429 long startTime
= Math
.max(start
, traceEntry
.getStartTime());
430 long endTime
= Math
.min(end
+ 1, traceEntry
.getEndTime());
431 List
<ITimeEvent
> eventList
= getEventList(traceEntry
, startTime
, endTime
, resolution
, monitor
);
432 if (monitor
.isCanceled()) {
435 traceEntry
.setEventList(eventList
);
438 for (ITimeGraphEntry entry
: traceEntry
.getChildren()) {
439 if (monitor
.isCanceled()) {
442 XmlEntry xmlEntry
= (XmlEntry
) entry
;
443 buildStatusEvent(xmlEntry
, monitor
, start
, end
);
447 /** Build a tree using getParentId() and getId() */
448 private static void buildTree(Map
<String
, XmlEntry
> entryMap
, XmlEntry rootEntry
) {
449 for (XmlEntry entry
: entryMap
.values()) {
451 if (!entry
.getParentId().isEmpty()) {
452 XmlEntry parent
= entryMap
.get(entry
.getParentId());
454 * Associate the parent entry only if their time overlap. A
455 * child entry may start before its parent, for example at the
456 * beginning of the trace if a parent has not yet appeared in
457 * the state system. We just want to make sure that the entry
458 * didn't start after the parent ended or ended before the
461 if (parent
!= null &&
462 !(entry
.getStartTime() > parent
.getEndTime() ||
463 entry
.getEndTime() < parent
.getStartTime())) {
464 parent
.addChild(entry
);
469 rootEntry
.addChild(entry
);
475 protected List
<ITimeEvent
> getEventList(TimeGraphEntry entry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
476 if (!(entry
instanceof XmlEntry
)) {
477 return Collections
.EMPTY_LIST
;
479 XmlEntry xmlEntry
= (XmlEntry
) entry
;
480 ITmfStateSystem ssq
= xmlEntry
.getStateSystem();
481 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
482 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
483 if (realEnd
<= realStart
) {
486 List
<ITimeEvent
> eventList
= null;
487 int quark
= xmlEntry
.getDisplayQuark();
490 if (xmlEntry
.getType() == EntryDisplayType
.DISPLAY
) {
492 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, quark
, realStart
, realEnd
- 1, resolution
, monitor
);
493 eventList
= new ArrayList
<>(statusIntervals
.size());
494 long lastEndTime
= -1;
495 for (ITmfStateInterval statusInterval
: statusIntervals
) {
496 if (monitor
.isCanceled()) {
499 int status
= statusInterval
.getStateValue().unboxInt();
500 long time
= statusInterval
.getStartTime();
501 long duration
= statusInterval
.getEndTime() - time
+ 1;
502 if (!statusInterval
.getStateValue().isNull()) {
503 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
504 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
506 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
507 } else if (lastEndTime
== -1 || time
+ duration
>= endTime
) {
508 // add null event if it intersects the start or end time
509 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
511 lastEndTime
= time
+ duration
;
514 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException
| StateSystemDisposedException e
) {
521 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
522 /* TODO: not implemented yet, need XML to go along */
523 return Collections
.EMPTY_LIST
;