1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011, 2012 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 * William Bourque - Initial API and implementation
11 * Yuriy Vashchuk - GUI reorganisation, simplification and some related code improvements.
12 * Yuriy Vashchuk - Histograms optimisation.
13 * Yuriy Vashchuk - Histogram Canvas Heritage correction
14 * Francois Chouinard - Cleanup and refactoring
15 * Francois Chouinard - Moved from LTTng to TMF
16 *******************************************************************************/
18 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
.ExecutionType
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.ITmfTraceEditor
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.layout
.GridData
;
40 import org
.eclipse
.swt
.layout
.GridLayout
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.ui
.IEditorPart
;
43 import org
.eclipse
.ui
.PlatformUI
;
46 * The purpose of this view is to provide graphical time distribution statistics about the trace events.
48 * The view is composed of two histograms and two controls:
50 * <li>an event distribution histogram for the whole trace;
51 * <li>an event distribution histogram for current time window (window span);
52 * <li>the timestamp of the currently selected event;
53 * <li>the window span (size of the time window of the smaller histogram).
55 * The histograms x-axis show their respective time range.
58 * @author Francois Chouinard
60 public class HistogramView
extends TmfView
{
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * The view ID as defined in plugin.xml
69 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.views.histogram"; //$NON-NLS-1$
72 * The initial window span (in nanoseconds)
74 public static final long INITIAL_WINDOW_SPAN
= (1L * 100 * 1000 * 1000); // .1sec
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
81 private Composite fParent
;
84 private ITmfTrace fTrace
;
86 // Current timestamp/time window - everything in the TIME_SCALE
87 private long fTraceStartTime
;
88 private long fTraceEndTime
;
89 private long fWindowStartTime
;
90 private long fWindowEndTime
;
91 private long fWindowSpan
= INITIAL_WINDOW_SPAN
;
92 private long fCurrentTimestamp
;
95 private HistogramTextControl fCurrentEventTimeControl
;
96 private HistogramTextControl fTimeSpanControl
;
98 // Histogram/request for the full trace range
99 private static FullTraceHistogram fFullTraceHistogram
;
100 private HistogramRequest fFullTraceRequest
;
102 // Histogram/request for the selected time range
103 private static TimeRangeHistogram fTimeRangeHistogram
;
104 private HistogramRequest fTimeRangeRequest
;
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
111 * Default constructor
113 public HistogramView() {
118 public void dispose() {
119 if ((fTimeRangeRequest
!= null) && !fTimeRangeRequest
.isCompleted()) {
120 fTimeRangeRequest
.cancel();
122 if ((fFullTraceRequest
!= null) && !fFullTraceRequest
.isCompleted()) {
123 fFullTraceRequest
.cancel();
125 fFullTraceHistogram
.dispose();
126 fTimeRangeHistogram
.dispose();
127 fCurrentEventTimeControl
.dispose();
128 fTimeSpanControl
.dispose();
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
137 public void createPartControl(Composite parent
) {
142 final String currentEventLabel
= Messages
.HistogramView_currentEventLabel
;
143 final String windowSpanLabel
= Messages
.HistogramView_windowSpanLabel
;
145 // --------------------------------------------------------------------
146 // Set the HistogramView layout
147 // --------------------------------------------------------------------
149 Composite viewComposite
= new Composite(fParent
, SWT
.FILL
);
150 GridLayout gridLayout
= new GridLayout();
151 gridLayout
.numColumns
= 2;
152 gridLayout
.horizontalSpacing
= 5;
153 gridLayout
.verticalSpacing
= 0;
154 gridLayout
.marginHeight
= 0;
155 gridLayout
.marginWidth
= 0;
156 viewComposite
.setLayout(gridLayout
);
158 // Use all available space
159 GridData gridData
= new GridData();
160 gridData
.horizontalAlignment
= SWT
.FILL
;
161 gridData
.verticalAlignment
= SWT
.FILL
;
162 gridData
.grabExcessHorizontalSpace
= true;
163 viewComposite
.setLayoutData(gridData
);
165 // --------------------------------------------------------------------
167 // --------------------------------------------------------------------
169 Composite controlsComposite
= new Composite(viewComposite
, SWT
.FILL
);
170 gridLayout
= new GridLayout();
171 gridLayout
.numColumns
= 2;
172 gridLayout
.marginHeight
= 0;
173 gridLayout
.marginWidth
= 0;
174 gridLayout
.horizontalSpacing
= 5;
175 gridLayout
.verticalSpacing
= 0;
176 gridLayout
.makeColumnsEqualWidth
= false;
177 gridLayout
.marginLeft
= 5;
178 gridLayout
.marginRight
= 5;
179 controlsComposite
.setLayout(gridLayout
);
181 // Current event time control
182 gridData
= new GridData();
183 gridData
.horizontalAlignment
= SWT
.CENTER
;
184 gridData
.verticalAlignment
= SWT
.CENTER
;
185 fCurrentEventTimeControl
= new HistogramCurrentTimeControl(this, controlsComposite
, currentEventLabel
, 0L);
186 fCurrentEventTimeControl
.setLayoutData(gridData
);
187 fCurrentEventTimeControl
.setValue(0L);
189 // Window span time control
190 gridData
= new GridData();
191 gridData
.horizontalAlignment
= SWT
.CENTER
;
192 gridData
.verticalAlignment
= SWT
.CENTER
;
193 fTimeSpanControl
= new HistogramTimeRangeControl(this, controlsComposite
, windowSpanLabel
, 0L);
194 fTimeSpanControl
.setLayoutData(gridData
);
195 fTimeSpanControl
.setValue(0L);
197 // --------------------------------------------------------------------
198 // Time range histogram
199 // --------------------------------------------------------------------
201 Composite timeRangeComposite
= new Composite(viewComposite
, SWT
.FILL
);
202 gridLayout
= new GridLayout();
203 gridLayout
.numColumns
= 1;
204 gridLayout
.marginHeight
= 0;
205 gridLayout
.marginWidth
= 0;
206 gridLayout
.marginTop
= 5;
207 gridLayout
.horizontalSpacing
= 0;
208 gridLayout
.verticalSpacing
= 0;
209 gridLayout
.marginLeft
= 5;
210 gridLayout
.marginRight
= 5;
211 timeRangeComposite
.setLayout(gridLayout
);
213 // Use remaining horizontal space
214 gridData
= new GridData();
215 gridData
.horizontalAlignment
= SWT
.FILL
;
216 gridData
.verticalAlignment
= SWT
.FILL
;
217 gridData
.grabExcessHorizontalSpace
= true;
218 timeRangeComposite
.setLayoutData(gridData
);
221 fTimeRangeHistogram
= new TimeRangeHistogram(this, timeRangeComposite
);
223 // --------------------------------------------------------------------
224 // Full range histogram
225 // --------------------------------------------------------------------
227 Composite fullRangeComposite
= new Composite(viewComposite
, SWT
.FILL
);
228 gridLayout
= new GridLayout();
229 gridLayout
.numColumns
= 1;
230 gridLayout
.marginHeight
= 0;
231 gridLayout
.marginWidth
= 0;
232 gridLayout
.marginTop
= 5;
233 gridLayout
.horizontalSpacing
= 0;
234 gridLayout
.verticalSpacing
= 0;
235 gridLayout
.marginLeft
= 5;
236 gridLayout
.marginRight
= 5;
237 fullRangeComposite
.setLayout(gridLayout
);
239 // Use remaining horizontal space
240 gridData
= new GridData();
241 gridData
.horizontalAlignment
= SWT
.FILL
;
242 gridData
.verticalAlignment
= SWT
.FILL
;
243 gridData
.horizontalSpan
= 2;
244 gridData
.grabExcessHorizontalSpace
= true;
245 fullRangeComposite
.setLayoutData(gridData
);
248 fFullTraceHistogram
= new FullTraceHistogram(this, fullRangeComposite
);
250 IEditorPart editor
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
251 if (editor
instanceof ITmfTraceEditor
) {
252 ITmfTrace trace
= ((ITmfTraceEditor
) editor
).getTrace();
254 traceSelected(new TmfTraceSelectedSignal(this, trace
));
260 public void setFocus() {
261 fFullTraceHistogram
.fCanvas
.setFocus();
265 fParent
.layout(true);
268 // ------------------------------------------------------------------------
270 // ------------------------------------------------------------------------
273 * Returns the current trace handled by the view
275 * @return the current trace
278 public ITmfTrace
getTrace() {
283 * Returns the time range of the current selected window (base on default time scale).
285 * @return the time range of current selected window.
287 public TmfTimeRange
getTimeRange() {
288 return new TmfTimeRange(
289 new TmfTimestamp(fWindowStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
),
290 new TmfTimestamp(fWindowEndTime
, ITmfTimestamp
.NANOSECOND_SCALE
));
293 // ------------------------------------------------------------------------
295 // ------------------------------------------------------------------------
298 * Broadcast TmfSignal about new current time value.
299 * @param newTime the new current time.
301 void updateCurrentEventTime(long newTime
) {
302 if (fTrace
!= null) {
303 TmfTimeRange timeRange
= new TmfTimeRange(new TmfTimestamp(newTime
, ITmfTimestamp
.NANOSECOND_SCALE
), TmfTimestamp
.BIG_CRUNCH
);
304 HistogramRequest request
= new HistogramRequest(fTimeRangeHistogram
.getDataModel(), timeRange
, 0, 1, 0, ExecutionType
.FOREGROUND
) {
306 public void handleData(ITmfEvent event
) {
308 TmfTimeSynchSignal signal
= new TmfTimeSynchSignal(this, event
.getTimestamp());
309 TmfSignalManager
.dispatchSignal(signal
);
313 fTrace
.sendRequest(request
);
318 * Broadcast TmfSignal about new selected time range.
319 * @param startTime the new start time
320 * @param endTime the new end time
322 void updateTimeRange(long startTime
, long endTime
) {
323 if (fTrace
!= null) {
324 // Build the new time range; keep the current time
325 TmfTimeRange timeRange
= new TmfTimeRange(
326 new TmfTimestamp(startTime
, ITmfTimestamp
.NANOSECOND_SCALE
),
327 new TmfTimestamp(endTime
, ITmfTimestamp
.NANOSECOND_SCALE
));
328 ITmfTimestamp currentTime
= new TmfTimestamp(fCurrentTimestamp
, ITmfTimestamp
.NANOSECOND_SCALE
);
329 fTimeSpanControl
.setValue(endTime
- startTime
);
331 // Send the FW signal
332 TmfRangeSynchSignal signal
= new TmfRangeSynchSignal(this, timeRange
, currentTime
);
333 TmfSignalManager
.dispatchSignal(signal
);
338 * Broadcast TmfSignal about new selected time range.
339 * @param newDuration new duration (relative to current start time)
341 public synchronized void updateTimeRange(long newDuration
) {
342 if (fTrace
!= null) {
343 long delta
= newDuration
- fWindowSpan
;
344 long newStartTime
= fWindowStartTime
+ (delta
/ 2);
345 setNewRange(newStartTime
, newDuration
);
349 private void setNewRange(long startTime
, long duration
) {
350 if (startTime
< fTraceStartTime
) {
351 startTime
= fTraceStartTime
;
354 long endTime
= startTime
+ duration
;
355 if (endTime
> fTraceEndTime
) {
356 endTime
= fTraceEndTime
;
357 if ((endTime
- duration
) > fTraceEndTime
) {
358 startTime
= endTime
- duration
;
360 startTime
= fTraceStartTime
;
363 updateTimeRange(startTime
, endTime
);
366 // ------------------------------------------------------------------------
368 // ------------------------------------------------------------------------
371 * Handles trace opened signal. Loads histogram if new trace time range is not
372 * equal <code>TmfTimeRange.NULL_RANGE</code>
373 * @param signal the trace selected signal
377 public void traceOpened(TmfTraceOpenedSignal signal
) {
378 assert (signal
!= null);
379 fTrace
= signal
.getTrace();
384 * Handles trace selected signal. Loads histogram if new trace time range is not
385 * equal <code>TmfTimeRange.NULL_RANGE</code>
386 * @param signal the trace selected signal
390 public void traceSelected(TmfTraceSelectedSignal signal
) {
391 assert (signal
!= null);
392 if (fTrace
!= signal
.getTrace()) {
393 fTrace
= signal
.getTrace();
398 private void loadTrace() {
399 initializeHistograms();
404 * Handles trace closed signal. Clears the view and data model and cancels requests.
405 * @param signal the trace closed signal
409 public void traceClosed(TmfTraceClosedSignal signal
) {
411 if (signal
.getTrace() != fTrace
) {
415 // Kill any running request
416 if ((fTimeRangeRequest
!= null) && !fTimeRangeRequest
.isCompleted()) {
417 fTimeRangeRequest
.cancel();
419 if ((fFullTraceRequest
!= null) && !fFullTraceRequest
.isCompleted()) {
420 fFullTraceRequest
.cancel();
423 // Initialize the internal data
425 fTraceStartTime
= 0L;
427 fWindowStartTime
= 0L;
429 fWindowSpan
= INITIAL_WINDOW_SPAN
;
430 fCurrentTimestamp
= 0L;
432 // Clear the UI widgets
433 fFullTraceHistogram
.clear();
434 fTimeRangeHistogram
.clear();
435 fCurrentEventTimeControl
.setValue(0L);
437 fTimeSpanControl
.setValue(0);
441 * Handles trace range updated signal. Extends histogram according to the new time range. If a
442 * HistogramRequest is already ongoing, it will be cancelled and a new request with the new range
445 * @param signal the trace range updated signal
449 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal
) {
451 if (signal
.getTrace() != fTrace
) {
455 boolean drawTimeRangeHistogram
= fTraceStartTime
== 0;
456 TmfTimeRange fullRange
= signal
.getRange();
458 fTraceStartTime
= fullRange
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
459 fTraceEndTime
= fullRange
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
461 fFullTraceHistogram
.setFullRange(fTraceStartTime
, fTraceEndTime
);
462 fTimeRangeHistogram
.setFullRange(fTraceStartTime
, fTraceEndTime
);
464 if (drawTimeRangeHistogram
) {
465 fCurrentTimestamp
= fTraceStartTime
;
466 fCurrentEventTimeControl
.setValue(fCurrentTimestamp
);
467 fFullTraceHistogram
.setTimeRange(fTraceStartTime
, INITIAL_WINDOW_SPAN
);
468 fTimeRangeHistogram
.setTimeRange(fTraceStartTime
, INITIAL_WINDOW_SPAN
);
469 sendTimeRangeRequest(fTraceStartTime
, fTraceStartTime
+ INITIAL_WINDOW_SPAN
);
472 sendFullRangeRequest(fullRange
);
476 * Handles the trace updated signal. Used to update time limits (start and end time)
477 * @param signal the trace updated signal
481 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
482 if (signal
.getTrace() != fTrace
) {
485 TmfTimeRange fullRange
= signal
.getTrace().getTimeRange();
486 fTraceStartTime
= fullRange
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
487 fTraceEndTime
= fullRange
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
489 fFullTraceHistogram
.setFullRange(fTraceStartTime
, fTraceEndTime
);
490 fTimeRangeHistogram
.setFullRange(fTraceStartTime
, fTraceEndTime
);
492 if ((fFullTraceRequest
!= null) && fFullTraceRequest
.getRange().getEndTime().compareTo(signal
.getRange().getEndTime()) < 0) {
493 sendFullRangeRequest(fullRange
);
498 * Handles the current time updated signal. Sets the current time in the time range
499 * histogram as well as the full histogram.
501 * @param signal the signal to process
504 public void currentTimeUpdated(TmfTimeSynchSignal signal
) {
505 // Because this can't happen :-)
506 assert (signal
!= null);
508 // Update the selected event time
509 ITmfTimestamp currentTime
= signal
.getCurrentTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
);
510 fCurrentTimestamp
= currentTime
.getValue();
512 // Notify the relevant widgets
513 fFullTraceHistogram
.setCurrentEvent(fCurrentTimestamp
);
514 fTimeRangeHistogram
.setCurrentEvent(fCurrentTimestamp
);
515 fCurrentEventTimeControl
.setValue(fCurrentTimestamp
);
519 * Updates the current time range in the time range histogram and full range histogram.
520 * @param signal the signal to process
523 public void timeRangeUpdated(TmfRangeSynchSignal signal
) {
524 // Because this can't happen :-)
525 assert (signal
!= null);
527 if (fTrace
!= null) {
528 // Update the time range
529 fWindowStartTime
= signal
.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
530 fWindowEndTime
= signal
.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
531 fWindowSpan
= fWindowEndTime
- fWindowStartTime
;
533 // Notify the relevant widgets
534 sendTimeRangeRequest(fWindowStartTime
, fWindowEndTime
);
535 fFullTraceHistogram
.setTimeRange(fWindowStartTime
, fWindowSpan
);
537 fTimeSpanControl
.setValue(fWindowSpan
);
541 // ------------------------------------------------------------------------
543 // ------------------------------------------------------------------------
545 private void initializeHistograms() {
546 TmfTimeRange fullRange
= updateTraceTimeRange();
548 if ((fTimeRangeRequest
!= null) && !fTimeRangeRequest
.isCompleted()) {
549 fTimeRangeRequest
.cancel();
551 fTimeRangeHistogram
.clear();
552 fTimeRangeHistogram
.setFullRange(fTraceStartTime
, fTraceEndTime
);
553 fTimeRangeHistogram
.setTimeRange(fTraceStartTime
, INITIAL_WINDOW_SPAN
);
554 fTimeRangeHistogram
.setCurrentEvent(fTraceStartTime
);
556 if ((fFullTraceRequest
!= null) && !fFullTraceRequest
.isCompleted()) {
557 fFullTraceRequest
.cancel();
559 fFullTraceHistogram
.clear();
560 fFullTraceHistogram
.setFullRange(fTraceStartTime
, fTraceEndTime
);
561 fFullTraceHistogram
.setTimeRange(fTraceStartTime
, INITIAL_WINDOW_SPAN
);
562 fFullTraceHistogram
.setCurrentEvent(fTraceStartTime
);
564 fWindowStartTime
= fTraceStartTime
;
565 fWindowSpan
= INITIAL_WINDOW_SPAN
;
566 fWindowEndTime
= fWindowStartTime
+ fWindowSpan
;
568 fCurrentEventTimeControl
.setValue(fTraceStartTime
);
570 fTimeSpanControl
.setValue(fWindowSpan
);
572 if (!fullRange
.equals(TmfTimeRange
.NULL_RANGE
)) {
573 sendTimeRangeRequest(fTraceStartTime
, fTraceStartTime
+ fWindowSpan
);
574 sendFullRangeRequest(fullRange
);
578 private TmfTimeRange
updateTraceTimeRange() {
579 fTraceStartTime
= 0L;
581 fCurrentTimestamp
= 0L;
583 TmfTimeRange timeRange
= fTrace
.getTimeRange();
584 if (!timeRange
.equals(TmfTimeRange
.NULL_RANGE
)) {
585 fTraceStartTime
= timeRange
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
586 fTraceEndTime
= timeRange
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
587 fCurrentTimestamp
= fTraceStartTime
;
592 private void sendTimeRangeRequest(long startTime
, long endTime
) {
593 if ((fTimeRangeRequest
!= null) && !fTimeRangeRequest
.isCompleted()) {
594 fTimeRangeRequest
.cancel();
596 TmfTimestamp startTS
= new TmfTimestamp(startTime
, ITmfTimestamp
.NANOSECOND_SCALE
);
597 TmfTimestamp endTS
= new TmfTimestamp(endTime
, ITmfTimestamp
.NANOSECOND_SCALE
);
598 TmfTimeRange timeRange
= new TmfTimeRange(startTS
, endTS
);
600 fTimeRangeHistogram
.clear();
601 fTimeRangeHistogram
.setTimeRange(startTime
, endTime
- startTime
);
603 int cacheSize
= fTrace
.getCacheSize();
604 fTimeRangeRequest
= new HistogramRequest(fTimeRangeHistogram
.getDataModel(), timeRange
, 0, TmfDataRequest
.ALL_DATA
, cacheSize
, ExecutionType
.FOREGROUND
);
605 fTrace
.sendRequest(fTimeRangeRequest
);
608 private void sendFullRangeRequest(TmfTimeRange fullRange
) {
609 if ((fFullTraceRequest
!= null) && !fFullTraceRequest
.isCompleted()) {
610 fFullTraceRequest
.cancel();
612 int cacheSize
= fTrace
.getCacheSize();
613 fFullTraceRequest
= new HistogramRequest(fFullTraceHistogram
.getDataModel(), fullRange
, (int) fFullTraceHistogram
.fDataModel
.getNbEvents(),
614 TmfDataRequest
.ALL_DATA
, cacheSize
, ExecutionType
.BACKGROUND
);
615 fTrace
.sendRequest(fFullTraceRequest
);