1 /******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 * France Lapointe Nguyen - Initial API and implementation
11 * Bernd Hufmann - Extracted abstract class from LatencyScatterGraphViewer
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Collection
;
18 import java
.util
.Collections
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.core
.runtime
.Status
;
25 import org
.eclipse
.core
.runtime
.jobs
.Job
;
26 import org
.eclipse
.jdt
.annotation
.Nullable
;
27 import org
.eclipse
.swt
.graphics
.Point
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.AbstractSegmentStoreAnalysisModule
;
31 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.IAnalysisProgressListener
;
32 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
33 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.Activator
;
34 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.views
.segmentstore
.Messages
;
35 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
36 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
37 import org
.eclipse
.tracecompass
.segmentstore
.core
.SegmentComparators
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfWindowRangeUpdatedSignal
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.TmfChartTimeStampFormat
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
52 import org
.swtchart
.Chart
;
53 import org
.swtchart
.IAxis
;
54 import org
.swtchart
.IAxisTick
;
55 import org
.swtchart
.ILineSeries
;
56 import org
.swtchart
.ILineSeries
.PlotSymbolType
;
57 import org
.swtchart
.ISeries
.SeriesType
;
58 import org
.swtchart
.ISeriesSet
;
59 import org
.swtchart
.LineStyle
;
60 import org
.swtchart
.Range
;
63 * Displays the segment store analysis data in a scatter graph
65 * @author France Lapointe Nguyen
66 * @author Matthew Khouzam - reduced memory usage
69 public abstract class AbstractSegmentStoreScatterGraphViewer
extends TmfCommonXLineChartViewer
{
71 private final class CompactingSegmentStoreQuery
extends Job
{
72 private static final long MAX_POINTS
= 1000;
73 private final TmfTimeRange fCurrentRange
;
75 private CompactingSegmentStoreQuery(TmfTimeRange currentRange
) {
76 super(Messages
.SegmentStoreScatterGraphViewer_compactTitle
);
77 fCurrentRange
= currentRange
;
81 protected IStatus
run(@Nullable IProgressMonitor monitor
) {
82 final IProgressMonitor statusMonitor
= monitor
;
83 if (statusMonitor
== null) {
84 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "Monitor is null"); //$NON-NLS-1$
87 AbstractSegmentStoreAnalysisModule module
= getAnalysisModule();
88 final long startTimeInNanos
= getTimeInNanos(fCurrentRange
.getStartTime());
89 final long endTimeInNanos
= getTimeInNanos(fCurrentRange
.getEndTime());
91 setWindowRange(startTimeInNanos
, endTimeInNanos
);
92 redraw(statusMonitor
, startTimeInNanos
, startTimeInNanos
, Collections
.EMPTY_LIST
);
93 return new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
, "Analysis module not available"); //$NON-NLS-1$
96 final ISegmentStore
<ISegment
> results
= module
.getResults();
97 if (results
== null) {
98 setWindowRange(startTimeInNanos
, endTimeInNanos
);
99 redraw(statusMonitor
, startTimeInNanos
, startTimeInNanos
, Collections
.EMPTY_LIST
);
100 return new Status(IStatus
.INFO
, Activator
.PLUGIN_ID
, "Analysis module does not have results"); //$NON-NLS-1$
103 final long startTime
= fCurrentRange
.getStartTime().getValue();
104 final long endTime
= fCurrentRange
.getEndTime().getValue();
105 fPixelStart
= startTime
;
106 fPixelSize
= (endTime
- startTime
) / MAX_POINTS
;
107 final Iterable
<ISegment
> intersectingElements
= results
.getIntersectingElements(startTime
, endTime
);
109 final List
<ISegment
> list
= convertIterableToList(intersectingElements
, statusMonitor
);
110 final List
<ISegment
> displayData
= (!list
.isEmpty()) ?
compactList(startTime
, list
, statusMonitor
) : list
;
112 setWindowRange(startTimeInNanos
, endTimeInNanos
);
113 redraw(statusMonitor
, startTime
, endTime
, displayData
);
115 if (statusMonitor
.isCanceled()) {
116 return NonNullUtils
.checkNotNull(Status
.CANCEL_STATUS
);
118 return NonNullUtils
.checkNotNull(Status
.OK_STATUS
);
122 private void redraw(final IProgressMonitor statusMonitor
, final long startTime
, final long endTime
, final List
<ISegment
> displayData
) {
123 fDisplayData
= displayData
;
124 Display
.getDefault().asyncExec(new Runnable() {
128 updateData(startTime
, endTime
, displayData
.size(), statusMonitor
);
133 private List
<ISegment
> compactList(final long startTime
, final List
<ISegment
> listToCompact
, final IProgressMonitor statusMonitor
) {
134 List
<ISegment
> displayData
= new ArrayList
<>();
135 ISegment last
= listToCompact
.get(0);
136 if (last
.getStart() >= startTime
) {
137 displayData
.add(last
);
139 for (ISegment next
: listToCompact
) {
140 if (next
.getStart() < startTime
) {
143 if (statusMonitor
.isCanceled()) {
144 return Collections
.EMPTY_LIST
;
146 if (!overlaps(last
, next
)) {
147 displayData
.add(next
);
154 private List
<ISegment
> convertIterableToList(final Iterable
<ISegment
> iterable
, final IProgressMonitor statusMonitor
) {
155 final List
<ISegment
> list
= new ArrayList
<>();
156 for (ISegment seg
: iterable
) {
157 if (statusMonitor
.isCanceled()) {
158 return Collections
.EMPTY_LIST
;
162 Collections
.sort(list
, SegmentComparators
.INTERVAL_START_COMPARATOR
);
166 private boolean overlaps(ISegment last
, ISegment next
) {
167 long timePerPix
= fPixelSize
;
168 final long start
= last
.getStart();
169 final long pixelStart
= fPixelStart
;
170 final long pixelDuration
= start
- pixelStart
;
171 long startPixBoundL
= pixelDuration
/ timePerPix
* timePerPix
+ pixelStart
;
172 long startPixBoundR
= startPixBoundL
+ timePerPix
;
173 final long currentStart
= next
.getStart();
174 if (currentStart
>= startPixBoundL
&& currentStart
<= startPixBoundR
) {
175 long length
= last
.getLength();
176 long lengthNext
= next
.getLength();
177 long lengthLow
= length
/ timePerPix
* timePerPix
;
178 long lengthHigh
= lengthLow
+ timePerPix
;
179 return (lengthNext
>= lengthLow
&& lengthNext
<= lengthHigh
);
185 // ------------------------------------------------------------------------
187 // ------------------------------------------------------------------------
190 * Listener to update the model with the semgent store analysis results
191 * once the analysis is fully completed
193 private final class AnalysisProgressListener
implements IAnalysisProgressListener
{
196 public void onComplete(AbstractSegmentStoreAnalysisModule activeAnalysis
, ISegmentStore
<ISegment
> results
) {
197 // Only update the model if trace that was analyzed is active trace
198 if (activeAnalysis
.equals(getAnalysisModule())) {
199 updateModel(results
);
200 updateRange(TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange());
205 private long fPixelSize
= -1;
207 private long fPixelStart
= 0;
211 private Collection
<ISegment
> fDisplayData
= Collections
.EMPTY_LIST
;
214 * Analysis completion listener
216 private AnalysisProgressListener fListener
;
219 * Current analysis module
221 private @Nullable AbstractSegmentStoreAnalysisModule fAnalysisModule
;
223 private @Nullable Job fCompactingJob
;
225 // ------------------------------------------------------------------------
227 // ------------------------------------------------------------------------
241 public AbstractSegmentStoreScatterGraphViewer(Composite parent
, String title
, String xLabel
, String yLabel
) {
242 super(parent
, title
, xLabel
, yLabel
);
243 setTooltipProvider(new SegmentStoreScatterGraphTooltipProvider(this));
244 fListener
= new AnalysisProgressListener();
245 ITmfTrace trace
= TmfTraceManager
.getInstance().getActiveTrace();
246 initializeModule(trace
);
247 getSwtChart().getLegend().setVisible(false);
250 private final void initializeModule(@Nullable ITmfTrace trace
) {
252 final AbstractSegmentStoreAnalysisModule analysisModuleOfClass
= getSegmentStoreAnalysisModule(trace
);
253 if (analysisModuleOfClass
!= null) {
254 analysisModuleOfClass
.addListener(fListener
);
255 setData(analysisModuleOfClass
);
256 updateRange(TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange());
261 // ------------------------------------------------------------------------
263 // ------------------------------------------------------------------------
266 * Update the data in the graph
271 public void updateModel(@Nullable ISegmentStore
<ISegment
> dataInput
) {
272 // Update new window range
273 TmfTimeRange currentRange
= TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange();
274 long currentStart
= getTimeInNanos(currentRange
.getStartTime());
275 long currentEnd
= getTimeInNanos(currentRange
.getEndTime());
276 if (dataInput
== null) {
277 if (!getDisplay().isDisposed()) {
278 Display
.getDefault().syncExec(new Runnable() {
285 fDisplayData
= Collections
.EMPTY_LIST
;
287 Collection
<ISegment
> elements
= (Collection
<ISegment
>) dataInput
.getIntersectingElements(currentStart
, currentEnd
);
288 // getIntersectingElements can return an unsorted iterable, make
289 // sure our collection is sorted
290 ArrayList
<ISegment
> list
= new ArrayList
<>(elements
);
291 Collections
.sort(list
, SegmentComparators
.INTERVAL_START_COMPARATOR
);
294 setWindowRange(currentStart
, currentEnd
);
295 updateRange(currentRange
);
299 protected void initializeDataSource() {
300 ITmfTrace trace
= getTrace();
301 initializeModule(trace
);
303 setData(getSegmentStoreAnalysisModule(trace
));
308 protected void updateData(final long start
, final long end
, int nb
, @Nullable IProgressMonitor monitor
) {
309 // Third parameter is not used by implementation
310 // Determine data that needs to be visible
311 Collection
<ISegment
> data
= fDisplayData
;
313 final int dataSize
= (nb
== 0) ? data
.size() : nb
;
314 if (dataSize
== 0 || end
== start
) {
318 final double[] xSeries
= new double[dataSize
];
319 final double[] ySeries
= new double[dataSize
];
320 // For each visible segments, add start time to x value and duration
322 Iterator
<ISegment
> modelIter
= data
.iterator();
324 for (int i
= 0; i
< dataSize
; i
++) {
325 if (modelIter
.hasNext()) {
326 ISegment segment
= modelIter
.next();
327 xSeries
[i
] = segment
.getStart() - start
;
328 ySeries
[i
] = segment
.getLength();
329 maxTempY
= Math
.max(maxTempY
, segment
.getLength());
332 final long maxY
= maxTempY
;
334 final Chart swtChart
= getSwtChart();
335 if (swtChart
.isDisposed() || xSeries
.length
< 1) {
338 swtChart
.updateLayout();
339 setSeries(Messages
.SegmentStoreScatterGraphViewer_legend
, ySeries
); // $NON-NLS-1$
340 final TmfChartTimeStampFormat tmfChartTimeStampFormat
= new TmfChartTimeStampFormat(getTimeOffset());
341 ILineSeries series
= (ILineSeries
) swtChart
.getSeriesSet().getSeries(Messages
.SegmentStoreScatterGraphViewer_legend
);
342 if (series
== null) {
343 series
= addSeries(Messages
.SegmentStoreScatterGraphViewer_legend
);
345 series
.setXSeries(xSeries
);
346 /* Find the minimal and maximum values in this series */
347 series
.setYSeries(ySeries
);
349 final IAxis xAxis
= swtChart
.getAxisSet().getXAxis(0);
350 IAxisTick xTick
= xAxis
.getTick();
351 xTick
.setFormat(tmfChartTimeStampFormat
);
352 xAxis
.setRange(new Range(0.0, end
- start
));
354 swtChart
.getAxisSet().getYAxis(0).setRange(new Range(0.0, maxY
));
358 if (isSendTimeAlignSignals()) {
359 // The width of the chart might have changed and its
360 // time axis might be misaligned with the other views
361 Point viewPos
= AbstractSegmentStoreScatterGraphViewer
.this.getParent().getParent().toDisplay(0, 0);
362 int axisPos
= swtChart
.toDisplay(0, 0).x
+ getPointAreaOffset();
363 int timeAxisOffset
= axisPos
- viewPos
.x
;
364 TmfTimeViewAlignmentInfo timeAlignmentInfo
= new TmfTimeViewAlignmentInfo(getControl().getShell(), viewPos
, timeAxisOffset
);
365 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(AbstractSegmentStoreScatterGraphViewer
.this, timeAlignmentInfo
, true));
370 protected void setWindowRange(final long windowStartTime
, final long windowEndTime
) {
371 super.setWindowRange(windowStartTime
, windowEndTime
);
375 protected ILineSeries
addSeries(@Nullable String seriesName
) {
376 ISeriesSet seriesSet
= getSwtChart().getSeriesSet();
377 ILineSeries series
= (ILineSeries
) seriesSet
.createSeries(SeriesType
.LINE
, seriesName
);
378 series
.setVisible(true);
379 series
.enableArea(false);
380 series
.setLineStyle(LineStyle
.NONE
);
381 series
.setSymbolType(PlotSymbolType
.DIAMOND
);
386 * Set the data into the viewer. Will update model is analysis is completed
387 * or run analysis if not completed
390 * Segment store analysis module
392 public void setData(@Nullable AbstractSegmentStoreAnalysisModule analysis
) {
393 if (analysis
== null) {
397 ISegmentStore
<ISegment
> results
= analysis
.getResults();
398 // If results are not null, then analysis is completed and model can be
400 if (results
!= null) {
401 updateModel(results
);
402 setAnalysisModule(analysis
);
406 analysis
.addListener(fListener
);
408 setAnalysisModule(analysis
);
412 * Returns the segment store analysis module
415 * The trace to consider
416 * @return the analysis module
418 protected @Nullable abstract AbstractSegmentStoreAnalysisModule
getSegmentStoreAnalysisModule(ITmfTrace trace
);
420 // ------------------------------------------------------------------------
422 // ------------------------------------------------------------------------
426 * Signal received when a different trace is selected
430 public void traceSelected(@Nullable TmfTraceSelectedSignal signal
) {
431 super.traceSelected(signal
);
432 if (signal
== null) {
435 ITmfTrace trace
= signal
.getTrace();
438 final TmfTimeRange timeRange
= TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange();
440 timeRange
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue(),
441 timeRange
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
442 setData(getSegmentStoreAnalysisModule(trace
));
443 updateRange(timeRange
);
449 * Signal received when trace is opened
453 public void traceOpened(@Nullable TmfTraceOpenedSignal signal
) {
454 super.traceOpened(signal
);
455 if (signal
== null) {
458 ITmfTrace trace
= signal
.getTrace();
462 final AbstractSegmentStoreAnalysisModule analysisModuleOfClass
= getSegmentStoreAnalysisModule(trace
);
463 final TmfTimeRange timeRange
= TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange();
465 getTimeInNanos(timeRange
.getStartTime()),
466 getTimeInNanos(timeRange
.getEndTime()));
467 setData(analysisModuleOfClass
);
472 private void updateRange(final @Nullable TmfTimeRange timeRange
) {
473 Job compactingJob
= fCompactingJob
;
474 if (compactingJob
!= null && compactingJob
.getState() == Job
.RUNNING
) {
475 compactingJob
.cancel();
477 compactingJob
= new CompactingSegmentStoreQuery(NonNullUtils
.checkNotNull(timeRange
));
478 fCompactingJob
= compactingJob
;
479 compactingJob
.schedule();
484 * Signal received when last opened trace is closed
488 public void traceClosed(@Nullable TmfTraceClosedSignal signal
) {
489 super.traceClosed(signal
);
490 if (signal
!= null) {
491 // Check if there is no more opened trace
492 if (TmfTraceManager
.getInstance().getActiveTrace() == null) {
493 AbstractSegmentStoreAnalysisModule analysis
= getAnalysisModule();
494 if (analysis
!= null) {
495 analysis
.removeListener(fListener
);
505 * Signal received when window range is updated
509 public void windowRangeUpdated(@Nullable TmfWindowRangeUpdatedSignal signal
) {
510 super.windowRangeUpdated(signal
);
511 if (signal
== null) {
514 if (getTrace() != null) {
515 final TmfTimeRange currentRange
= signal
.getCurrentRange();
516 updateRange(currentRange
);
518 Activator
.getDefault().logInfo("No Trace to update"); //$NON-NLS-1$
522 private @Nullable AbstractSegmentStoreAnalysisModule
getAnalysisModule() {
523 return fAnalysisModule
;
526 private void setAnalysisModule(AbstractSegmentStoreAnalysisModule analysisModule
) {
527 fAnalysisModule
= analysisModule
;
530 private static long getTimeInNanos(final ITmfTimestamp currentTime
) {
531 return currentTime
.normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();