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
.Display
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.readonly
.TmfXmlReadOnlyModelFactory
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
39 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
40 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
41 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.Activator
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
43 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.XmlViewInfo
;
44 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.timegraph
.XmlEntry
.EntryDisplayType
;
45 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
46 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
47 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
48 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
49 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
50 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
51 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.NullTimeEvent
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
62 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
63 import org
.w3c
.dom
.Element
;
65 import com
.google
.common
.collect
.Iterables
;
68 * This view displays state system data in a time graph view. It uses an XML
69 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
70 * element defines which entries from the state system will be shown and also
71 * gives additional information on the presentation of the view (states, colors,
74 * @author Florian Wininger
76 public class XmlTimeGraphView
extends AbstractTimeGraphView
{
79 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
81 private static final String
[] DEFAULT_COLUMN_NAMES
= new String
[] {
82 Messages
.XmlTimeGraphView_ColumnName
,
83 Messages
.XmlTimeGraphView_ColumnId
,
84 Messages
.XmlTimeGraphView_ColumnParentId
,
87 private static final String
[] DEFAULT_FILTER_COLUMN_NAMES
= new String
[] {
88 Messages
.XmlTimeGraphView_ColumnName
,
89 Messages
.XmlTimeGraphView_ColumnId
92 /** The relative weight of the sash */
93 private static final int[] fWeight
= { 1, 2 };
95 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
96 private static final @NonNull String SPLIT_STRING
= "/"; //$NON-NLS-1$
98 private final @NonNull XmlViewInfo fViewInfo
= new XmlViewInfo(ID
);
99 private final ITmfXmlModelFactory fFactory
;
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * Default constructor
108 public XmlTimeGraphView() {
109 super(ID
, new XmlPresentationProvider());
111 setTreeColumns(DEFAULT_COLUMN_NAMES
);
112 setTreeLabelProvider(new XmlTreeLabelProvider());
113 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES
);
114 setFilterLabelProvider(new XmlTreeLabelProvider());
115 setEntryComparator(new XmlEntryComparator());
116 this.addPartPropertyListener(new IPropertyChangeListener() {
118 public void propertyChange(PropertyChangeEvent event
) {
119 if (event
.getProperty().equals(TmfXmlUiStrings
.XML_OUTPUT_DATA
)) {
120 Object newValue
= event
.getNewValue();
121 if (newValue
instanceof String
) {
122 String data
= (String
) newValue
;
123 fViewInfo
.setViewData(data
);
130 fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
133 private void loadNewXmlView() {
137 private void setViewTitle(final String title
) {
138 Display
.getDefault().asyncExec(new Runnable() {
148 protected String
getNextText() {
149 return Messages
.XmlTimeGraphView_NextText
;
153 protected String
getNextTooltip() {
154 return Messages
.XmlTimeGraphView_NextTooltip
;
158 protected String
getPrevText() {
159 return Messages
.XmlTimeGraphView_PreviousText
;
163 protected String
getPrevTooltip() {
164 return Messages
.XmlTimeGraphView_PreviousInterval
;
168 * Default label provider, it shows name, id and parent columns
170 * TODO: There should be a way to define columns in the XML
172 private static class XmlTreeLabelProvider
extends TreeLabelProvider
{
175 public String
getColumnText(Object element
, int columnIndex
) {
176 XmlEntry entry
= (XmlEntry
) element
;
178 if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnName
)) {
179 return entry
.getName();
180 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnId
)) {
181 return entry
.getId();
182 } else if (DEFAULT_COLUMN_NAMES
[columnIndex
].equals(Messages
.XmlTimeGraphView_ColumnParentId
)) {
183 return entry
.getParentId();
190 private static class XmlEntryComparator
implements Comparator
<ITimeGraphEntry
> {
193 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
197 if ((o1
instanceof XmlEntry
) && (o2
instanceof XmlEntry
)) {
198 XmlEntry entry1
= (XmlEntry
) o1
;
199 XmlEntry entry2
= (XmlEntry
) o2
;
200 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
202 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
205 result
= entry1
.getName().compareTo(entry2
.getName());
210 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
217 // ------------------------------------------------------------------------
219 // ------------------------------------------------------------------------
222 protected void buildEntryList(ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
225 * Get the view element from the XML file. If the element can't be
228 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.TIME_GRAPH_VIEW
);
229 if (viewElement
== null) {
232 ITimeGraphPresentationProvider2 pres
= this.getPresentationProvider();
233 if (pres
instanceof XmlPresentationProvider
) {
235 * TODO: Each entry of a line could have their own states/color.
236 * That will require an update to the presentation provider
238 ((XmlPresentationProvider
) pres
).loadNewStates(viewElement
);
241 String title
= fViewInfo
.getViewTitle(viewElement
);
243 title
= Messages
.XmlTimeGraphView_DefaultTitle
;
246 Set
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
248 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
249 Set
<XmlEntry
> entryList
= new TreeSet
<>(getEntryComparator());
250 if (monitor
.isCanceled()) {
254 Set
<@NonNull ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new HashSet
<>();
255 if (analysisIds
.isEmpty()) {
257 * No analysis specified, take all state system analysis modules
259 Iterables
.addAll(stateSystemModules
, TmfTraceUtils
.getAnalysisModulesOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class));
261 for (String moduleId
: analysisIds
) {
262 moduleId
= checkNotNull(moduleId
);
263 ITmfAnalysisModuleWithStateSystems module
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
264 if (module
!= null) {
265 stateSystemModules
.add(module
);
270 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
271 IStatus status
= module
.schedule();
272 if (!status
.isOK()) {
275 if (!module
.waitForInitialization()) {
278 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
279 ssq
.waitUntilBuilt();
281 long startTime
= ssq
.getStartTime();
282 long endTime
= ssq
.getCurrentEndTime();
283 XmlEntry groupEntry
= new XmlEntry(-1, trace
, trace
.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 addToEntryList(parentTrace
, new ArrayList
<TimeGraphEntry
>(entryList
));
297 if (parentTrace
.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
);
337 while (i
< paths
.length
) {
338 List
<Integer
> subQuarks
= new LinkedList
<>();
339 /* Replace * by .* to have a regex string */
340 String name
= paths
[i
].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
341 for (int relativeQuark
: quarks
) {
342 for (int quark
: ss
.getSubAttributes(relativeQuark
, false, name
)) {
343 subQuarks
.add(quark
);
350 /* Process each quark */
351 XmlEntry currentEntry
= parentEntry
;
352 Element displayElement
= null;
353 Map
<String
, XmlEntry
> entryMap
= new HashMap
<>();
354 if (!displayElements
.isEmpty()) {
355 displayElement
= displayElements
.get(0);
357 for (int quark
: quarks
) {
358 currentEntry
= parentEntry
;
359 /* Process the current entry, if specified */
360 if (displayElement
!= null) {
361 currentEntry
= processEntry(entryElement
, displayElement
, parentEntry
, quark
, ss
);
362 entryMap
.put(currentEntry
.getId(), currentEntry
);
364 /* Process the children entry of this entry */
365 for (Element subEntryEl
: entryElements
) {
366 buildEntry(subEntryEl
, currentEntry
, quark
);
369 if (!entryMap
.isEmpty()) {
370 buildTree(entryMap
, parentEntry
);
374 private XmlEntry
processEntry(@NonNull Element entryElement
, @NonNull Element displayEl
,
375 @NonNull XmlEntry parentEntry
, int quark
, ITmfStateSystem ss
) {
377 * Get the start time and end time of this entry from the display
380 ITmfXmlStateAttribute display
= fFactory
.createStateAttribute(displayEl
, parentEntry
);
381 int displayQuark
= display
.getAttributeQuark(quark
, null);
382 if (displayQuark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
383 return new XmlEntry(quark
, parentEntry
.getTrace(),
384 String
.format("Unknown display quark for %s", ss
.getAttributeName(quark
)), ss
); //$NON-NLS-1$
387 long entryStart
= ss
.getStartTime();
388 long entryEnd
= ss
.getCurrentEndTime();
392 ITmfStateInterval oneInterval
= ss
.querySingleState(entryStart
, displayQuark
);
394 /* The entry start is the first non-null interval */
395 while (oneInterval
.getStateValue().isNull()) {
396 long ts
= oneInterval
.getEndTime() + 1;
397 if (ts
> ss
.getCurrentEndTime()) {
400 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
402 entryStart
= oneInterval
.getStartTime();
404 /* The entry end is the last non-null interval */
405 oneInterval
= ss
.querySingleState(entryEnd
, displayQuark
);
406 while (oneInterval
.getStateValue().isNull()) {
407 long ts
= oneInterval
.getStartTime() - 1;
408 if (ts
< ss
.getStartTime()) {
411 oneInterval
= ss
.querySingleState(ts
, displayQuark
);
413 entryEnd
= oneInterval
.getEndTime();
415 } catch (StateSystemDisposedException e
) {
418 return new XmlEntry(quark
, displayQuark
, parentEntry
.getTrace(), ss
.getAttributeName(quark
),
419 entryStart
, entryEnd
, EntryDisplayType
.DISPLAY
, ss
, entryElement
);
422 private void buildStatusEvent(XmlEntry traceEntry
, @NonNull IProgressMonitor monitor
, long start
, long end
) {
423 long resolution
= (end
- start
) / getDisplayWidth();
424 long startTime
= Math
.max(start
, traceEntry
.getStartTime());
425 long endTime
= Math
.min(end
+ 1, traceEntry
.getEndTime());
426 List
<ITimeEvent
> eventList
= getEventList(traceEntry
, startTime
, endTime
, resolution
, monitor
);
427 if (monitor
.isCanceled()) {
430 traceEntry
.setEventList(eventList
);
433 for (ITimeGraphEntry entry
: traceEntry
.getChildren()) {
434 if (monitor
.isCanceled()) {
437 XmlEntry xmlEntry
= (XmlEntry
) entry
;
438 buildStatusEvent(xmlEntry
, monitor
, start
, end
);
442 /** Build a tree using getParentId() and getId() */
443 private static void buildTree(Map
<String
, XmlEntry
> entryMap
, XmlEntry rootEntry
) {
444 for (XmlEntry entry
: entryMap
.values()) {
446 if (!entry
.getParentId().isEmpty()) {
447 XmlEntry parent
= entryMap
.get(entry
.getParentId());
449 * Associate the parent entry only if their time overlap. A
450 * child entry may start before its parent, for example at the
451 * beginning of the trace if a parent has not yet appeared in
452 * the state system. We just want to make sure that the entry
453 * didn't start after the parent ended or ended before the
456 if (parent
!= null &&
457 !(entry
.getStartTime() > parent
.getEndTime() ||
458 entry
.getEndTime() < parent
.getStartTime())) {
459 parent
.addChild(entry
);
464 rootEntry
.addChild(entry
);
470 protected List
<ITimeEvent
> getEventList(TimeGraphEntry entry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
471 if (!(entry
instanceof XmlEntry
)) {
472 return Collections
.EMPTY_LIST
;
474 XmlEntry xmlEntry
= (XmlEntry
) entry
;
475 ITmfStateSystem ssq
= xmlEntry
.getStateSystem();
476 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
477 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
478 if (realEnd
<= realStart
) {
481 List
<ITimeEvent
> eventList
= null;
482 int quark
= xmlEntry
.getDisplayQuark();
485 if (xmlEntry
.getType() == EntryDisplayType
.DISPLAY
) {
487 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, quark
, realStart
, realEnd
- 1, resolution
, monitor
);
488 eventList
= new ArrayList
<>(statusIntervals
.size());
489 long lastEndTime
= -1;
490 for (ITmfStateInterval statusInterval
: statusIntervals
) {
491 if (monitor
.isCanceled()) {
494 int status
= statusInterval
.getStateValue().unboxInt();
495 long time
= statusInterval
.getStartTime();
496 long duration
= statusInterval
.getEndTime() - time
+ 1;
497 if (!statusInterval
.getStateValue().isNull()) {
498 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
499 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
501 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
502 } else if (lastEndTime
== -1 || time
+ duration
>= endTime
) {
503 // add null event if it intersects the start or end time
504 eventList
.add(new NullTimeEvent(entry
, time
, duration
));
506 lastEndTime
= time
+ duration
;
509 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException
| StateSystemDisposedException e
) {
516 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
517 /* TODO: not implemented yet, need XML to go along */
518 return Collections
.EMPTY_LIST
;