1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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 * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.common
;
14 import java
.util
.Arrays
;
16 import org
.eclipse
.linuxtools
.lttng
.control
.LttngCoreProviderFactory
;
17 import org
.eclipse
.linuxtools
.lttng
.control
.LttngSyntheticEventProvider
;
18 import org
.eclipse
.linuxtools
.lttng
.event
.LttngSyntheticEvent
;
19 import org
.eclipse
.linuxtools
.lttng
.event
.LttngSyntheticEvent
.SequenceInd
;
20 import org
.eclipse
.linuxtools
.lttng
.event
.LttngTimestamp
;
21 import org
.eclipse
.linuxtools
.lttng
.request
.ILttngSyntEventRequest
;
22 import org
.eclipse
.linuxtools
.lttng
.request
.IRequestStatusListener
;
23 import org
.eclipse
.linuxtools
.lttng
.request
.LttngSyntEventRequest
;
24 import org
.eclipse
.linuxtools
.lttng
.request
.RequestCompletedSignal
;
25 import org
.eclipse
.linuxtools
.lttng
.request
.RequestStartedSignal
;
26 import org
.eclipse
.linuxtools
.lttng
.state
.evProcessor
.ITransEventProcessor
;
27 import org
.eclipse
.linuxtools
.lttng
.ui
.TraceDebug
;
28 import org
.eclipse
.linuxtools
.lttng
.ui
.model
.trange
.ItemContainer
;
29 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
30 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
32 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
.ExecutionType
;
33 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
34 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfRangeSynchSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalHandler
;
36 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalManager
;
37 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfTimeSynchSignal
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.ITimeAnalysisViewer
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.TmfTimeScaleSelectionEvent
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.TmfTimeSelectionEvent
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITmfTimeAnalysisEntry
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
43 import org
.eclipse
.swt
.widgets
.Display
;
47 * Abstract class used as a base for views handling specific time range data
51 * The class handles a single element queue of data requests, i.e. request can
52 * be triggered from different sources e.g. opening a file as well as a new
53 * selected time window
59 public abstract class AbsTimeUpdateView
extends TmfView
implements
60 IRequestStatusListener
{
62 // ========================================================================
64 // ========================================================================
66 // private static final long INITIAL_WINDOW_OFFSET = (1L * 1 * 1000 * 1000); // .001sec
67 // private static final long INITIAL_WINDOW_OFFSET = (1L * 10 * 1000 * 1000); // .01sec
68 private static final long INITIAL_WINDOW_OFFSET
= (1L * 100 * 1000 * 1000); // .1sec
69 // private static final long INITIAL_WINDOW_OFFSET = (1L * 1000 * 1000 * 1000); // 1sec
72 * Number of events before a GUI refresh
74 private static final Long INPUT_CHANGED_REFRESH
= 3000L;
75 private static final long DEFAULT_OFFSET
= 0L;
76 private static final int DEFAULT_CHUNK
= 1;
78 protected boolean synch
= true; // time synchronization, used to be an option
79 protected ITimeAnalysisViewer tsfviewer
= null;
81 private LttngSyntEventRequest fCurrentRequest
= null;
83 // ========================================================================
85 // ========================================================================
86 public AbsTimeUpdateView(String viewID
) {
88 // freqState = UiCommonFactory.getQueue(this);
91 // ========================================================================
93 // ========================================================================
97 * @seeorg.eclipse.linuxtools.lttng.state.IStateDataRequestListener#
98 * processingStarted(org.eclipse.linuxtools.lttng.state.StateDataRequest)
102 public synchronized void processingStarted(RequestStartedSignal signal
) {
103 LttngSyntEventRequest request
= signal
.getRequest();
104 if (request
!= null) {
105 // update queue with the id of the current request.
106 // freqState.requestStarted(request);
108 // if there was no new request then this one is still on
109 // prepare for the reception of new data
112 // no new time range for zoom orders
113 TmfTimeRange trange
= null;
114 // Time Range will be used to filter out events which are
115 // not visible in one pixel
116 trange
= request
.getRange();
118 // indicate if the data model needs to be cleared e.g. a new
119 // experiment is being selected
120 boolean clearData
= request
.isclearDataInd();
121 // Indicate if current data needs to be cleared and if so
122 // specify the new experiment time range that applies
123 ModelUpdatePrep(trange
, clearData
);
130 * @seeorg.eclipse.linuxtools.lttng.state.IStateDataRequestListener#
131 * processingCompleted(org.eclipse.linuxtools.lttng.state.StateDataRequest)
135 public void processingCompleted(RequestCompletedSignal signal
) {
136 ILttngSyntEventRequest request
= signal
.getRequest();
138 if (request
== null) {
142 // Update wait cursor
145 // No data refresh actions for cancelled requests.
146 if (request
.isCancelled() || request
.isFailed()) {
147 if (TraceDebug
.isDEBUG()) {
148 TmfTimeRange trange
= request
.getRange();
149 if (request
.isCancelled()) {
150 TraceDebug
.debug("Request cancelled "
151 + trange
.getStartTime() + "-" + trange
.getEndTime()
152 + " Handled Events: " + request
.getSynEventCount()
153 + " " + request
.toString(), 15);
154 } else if (request
.isFailed()) {
155 TraceDebug
.debug("Request Failed " + trange
.getStartTime()
156 + "-" + trange
.getEndTime() + " Handled Events: "
157 + request
.getSynEventCount() + " "
158 + request
.toString());
164 modelInputChanged(request
, true);
169 * Registers as listener of time selection from other views
173 public void synchToTime(TmfTimeSynchSignal signal
) {
175 Object source
= signal
.getSource();
176 if (signal
!= null && source
!= null && source
!= this) {
177 // Internal value is expected in nano seconds.
178 long selectedTime
= signal
.getCurrentTime().getValue();
179 if (tsfviewer
!= null) {
180 tsfviewer
.setSelectedTime(selectedTime
, true, source
);
187 * Process the reception of time window adjustment in this view if the
188 * source of the update is not this view.
191 * @param clearingData
193 public void synchToTimeRange(TmfRangeSynchSignal signal
, boolean clearingData
) {
195 Object source
= signal
.getSource();
196 if (signal
!= null && source
!= null && source
!= this) {
197 // Internal value is expected in nano seconds.
198 TmfTimeRange trange
= signal
.getCurrentRange();
199 TmfExperiment
<?
> experiment
= TmfExperiment
.getCurrentExperiment();
200 if (experiment
== null) {
201 TraceDebug
.debug("Current selected experiment is null");
205 // Clearing of process data is configurable
206 dataRequest(trange
, experiment
.getTimeRange(), clearingData
, ExecutionType
.FOREGROUND
);
212 * Trigger time synchronisation to other views this method shall be called
213 * when a check has been performed to note that an actual change of time has
214 * been performed vs a pure re-selection of the same time
219 protected void synchTimeNotification(long time
, Object source
) {
220 // if synchronisation selected
222 // Notify other views
223 TmfSignalManager
.dispatchSignal(new TmfTimeSynchSignal(source
, new LttngTimestamp(time
)));
228 * Common implementation of ITmfTimeSelectionListener, not used by all the
229 * views extending this abstract class
233 protected void tsfTmProcessSelEvent(TmfTimeSelectionEvent event
) {
234 Object source
= event
.getSource();
235 if (source
== null) {
239 ParamsUpdater paramUpdater
= getParamsUpdater();
240 Long savedSelTime
= paramUpdater
.getSelectedTime();
242 long selTimens
= event
.getSelectedTime();
244 // make sure the new selected time is different than saved before
246 if (savedSelTime
== null || savedSelTime
!= selTimens
) {
247 // Notify listener views.
248 synchTimeNotification(selTimens
, source
);
250 // Update the parameter updater to save the selected time
251 paramUpdater
.setSelectedTime(selTimens
);
253 if (TraceDebug
.isDEBUG()) {
254 TraceDebug
.debug("Selected Time: " + new LttngTimestamp(selTimens
) + "\n\t\t" + getName());
260 * Common implementation of ITmfTimeScaleSelectionListener, not used by all
261 * the views extending this abstract class
265 protected synchronized void tsfTmProcessTimeScaleEvent(TmfTimeScaleSelectionEvent event
) {
266 // source needed to keep track of source values
267 Object source
= event
.getSource();
269 if (source
!= null) {
270 // Update the parameter updater before carrying out a read request
271 ParamsUpdater paramUpdater
= getParamsUpdater();
272 boolean newParams
= paramUpdater
.processTimeScaleEvent(event
);
275 // Read the updated time window
276 TmfTimeRange trange
= paramUpdater
.getTrange();
277 if (trange
!= null) {
279 // Notify listener views. to perform data requests
280 // upon this notification
281 synchTimeRangeNotification(trange
, paramUpdater
.getSelectedTime(), source
);
288 * Inform registered listeners about the new time range
291 * @param selectedTime
294 protected void synchTimeRangeNotification(TmfTimeRange trange
, Long selectedTime
, Object source
) {
295 // if synchronisation selected
297 // Notify other views
298 TmfSignalManager
.dispatchSignal(new TmfRangeSynchSignal(source
, trange
, new LttngTimestamp(selectedTime
)));
303 * @param zoomedTRange
304 * @param experimentTRange
307 public void dataRequest(TmfTimeRange zoomedTRange
,
308 TmfTimeRange experimentTRange
, boolean clearingData
, ExecutionType execType
) {
310 // timeRange is the Experiment time range
311 boolean sent
= processDataRequest(zoomedTRange
, experimentTRange
, clearingData
, execType
);
319 // * @param zoomedTRange
320 // * @param experimentTRange
323 // public void dataRequest(TmfTimeRange zoomedTRange,
324 // TmfTimeRange experimentTRange, boolean clearingData) {
326 // // timeRange is the Experiment time range
327 // boolean sent = processDataRequest(zoomedTRange, experimentTRange, clearingData);
335 * send data request directly e.g. doesn't use a queue
337 * @param requestTrange
339 * @param experimentTRange
344 private boolean processDataRequest(TmfTimeRange requestTrange
,
345 TmfTimeRange experimentTRange
, boolean clearingData
, ExecutionType execType
) {
347 if (requestTrange
== null || experimentTRange
== null) {
348 TraceDebug
.debug("Invalid input");
352 // Cancel the currently executing request before starting a new one
353 if (fCurrentRequest
!= null && !fCurrentRequest
.isCompleted()) {
354 // System.out.println("Cancelling request");
355 // fCurrentRequest.cancel();
358 fCurrentRequest
= new LttngSyntEventRequest(
359 requestTrange
, DEFAULT_OFFSET
, TmfDataRequest
.ALL_DATA
,
360 DEFAULT_CHUNK
, this, experimentTRange
, getEventProcessor(), execType
) {
362 Long fCount
= getSynEventCount();
363 ITransEventProcessor processor
= getProcessor();
364 TmfTimestamp frunningTimeStamp
;
370 * org.eclipse.linuxtools.lttng.request.LttngSyntEventRequest#handleData
373 //// int handleDataCount = 0;
374 //// int handleDataValidCount = 0;
376 // public void handleData() {
377 // LttngSyntheticEvent[] result = getData();
379 // TmfEvent evt = (result.length > 0) ? result[0] : null;
380 //// handleDataCount++;
383 public void handleData(LttngSyntheticEvent event
) {
384 super.handleData(event
);
386 // handleDataValidCount++;
387 LttngSyntheticEvent synEvent
= (LttngSyntheticEvent
) event
;
389 SequenceInd indicator
= synEvent
.getSynType();
390 if (indicator
== SequenceInd
.BEFORE
391 || indicator
== SequenceInd
.AFTER
) {
392 processor
.process(event
, synEvent
.getTraceModel());
393 } else if (indicator
== SequenceInd
.STARTREQ
) {
394 handleRequestStarted();
395 } else if (indicator
== SequenceInd
.ENDREQ
) {
396 processor
.process(event
, synEvent
.getTraceModel());
397 // handleCompleted();
400 if (indicator
== SequenceInd
.BEFORE
) {
402 if (fCount
!= 0 && fCount
% INPUT_CHANGED_REFRESH
== 0) {
403 // send partial update
404 modelInputChanged(this, false);
406 if (TraceDebug
.isDEBUG()) {
407 frunningTimeStamp
= event
.getTimestamp();
408 TraceDebug
.debug("handled: " + fCount
+ " sequence: " + synEvent
.getSynType());
416 public void handleRequestStarted() {
422 // if (TraceDebug.isDEBUG()) {
423 // TraceDebug.debug("AbsTimeUpdateView: Received=" + handleDataCount + ", Valid=" + handleDataCount + ", fCount=" + fCount);
429 public void handleCompleted() {
430 super.handleCompleted();
432 // Data is not complete and should be handled as such
433 if (isFailed() || isCancelled()) {
434 modelIncomplete(this);
437 if (TraceDebug
.isDEBUG()) {
438 if (frunningTimeStamp
!= null) {
439 TraceDebug
.debug("Last event time stamp: "
440 + frunningTimeStamp
.getValue());
446 // obtain singleton core provider
447 LttngSyntheticEventProvider provider
= LttngCoreProviderFactory
450 // send the request to TMF
451 fCurrentRequest
.startRequestInd(provider
);
452 fCurrentRequest
.setclearDataInd(clearingData
);
457 * Returns an initial smaller window to allow the user to select the area of
460 * @param experimentTRange
463 protected TmfTimeRange
getInitTRange(TmfTimeRange experimentTRange
) {
464 TmfTimestamp expStartTime
= experimentTRange
.getStartTime();
465 TmfTimestamp expEndTime
= experimentTRange
.getEndTime();
466 TmfTimestamp initialEndOfWindow
= new LttngTimestamp(expStartTime
468 + INITIAL_WINDOW_OFFSET
);
469 if (initialEndOfWindow
.compareTo(expEndTime
, false) < 0) {
470 return new TmfTimeRange(expStartTime
, initialEndOfWindow
);
473 // The original size of the experiment is smaller than proposed adjusted
475 return experimentTRange
;
479 * Request the Time Analysis widget to enable or disable the wait cursor
480 * e.g. data request in progress or data request completed
484 protected void waitCursor(final boolean waitInd
) {
485 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
486 Display display
= tsfviewer
.getControl().getDisplay();
488 // Perform the updates on the UI thread
489 display
.asyncExec(new Runnable() {
492 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
493 tsfviewer
.waitCursor(waitInd
);
501 * View preparation to override the current local information
504 * - new total time range e.g. Experiment level
505 * @param clearAllData
507 protected void ModelUpdatePrep(TmfTimeRange timeRange
, boolean clearAllData
) {
508 ItemContainer
<?
> itemContainer
= getItemContainer();
510 // start fresh e.g. new experiment selected
511 itemContainer
.clearItems();
513 // clear children but keep processes
514 itemContainer
.clearChildren();
517 // Obtain the current resource array
518 ITmfTimeAnalysisEntry
[] itemArr
= itemContainer
.readItems();
520 // clean up data and boundaries
521 displayModel(itemArr
, -1, -1, false, -1, -1, null);
523 ParamsUpdater updater
= getParamsUpdater();
524 if (updater
!= null) {
526 updater
.setEventsDiscarded(0);
528 // Update new visible time range if available
529 if (timeRange
!= null) {
530 updater
.update(timeRange
.getStartTime().getValue(), timeRange
.getEndTime().getValue());
536 * Initialize the model and view before reloading items
538 * @param boundaryRange
539 * @param visibleRange
542 protected void ModelUpdateInit(TmfTimeRange boundaryRange
, TmfTimeRange visibleRange
, Object source
) {
543 // Update the view boundaries
544 if (boundaryRange
!= null) {
545 ItemContainer
<?
> itemContainer
= getItemContainer();
546 if (itemContainer
!= null) {
547 itemContainer
.clearItems();
548 // Obtain the current process array
549 ITmfTimeAnalysisEntry
[] itemArr
= itemContainer
.readItems();
551 long startTime
= boundaryRange
.getStartTime().getValue();
552 long endTime
= boundaryRange
.getEndTime().getValue();
554 // Update the view part
555 displayModel(itemArr
, startTime
, endTime
, true, visibleRange
.getStartTime().getValue(), visibleRange
556 .getEndTime().getValue(), source
);
560 // update the view filtering parameters
561 if (visibleRange
!= null) {
562 ParamsUpdater updater
= getParamsUpdater();
563 if (updater
!= null) {
565 updater
.setEventsDiscarded(0);
566 // Update new visible time range if available
567 updater
.update(visibleRange
.getStartTime().getValue(), visibleRange
.getEndTime().getValue());
573 * Actions taken by the view to refresh its widget(s) with the updated data
578 * true: yes, false: partial update
580 protected void modelInputChanged(ILttngSyntEventRequest request
, boolean complete
) {
581 long experimentStartTime
= -1;
582 long experimentEndTime
= -1;
583 TmfTimeRange experimentTimeRange
= request
.getExperimentTimeRange();
584 if (experimentTimeRange
!= null) {
585 experimentStartTime
= experimentTimeRange
.getStartTime().getValue();
586 experimentEndTime
= experimentTimeRange
.getEndTime().getValue();
589 // Obtain the current resource list
590 ITmfTimeAnalysisEntry
[] itemArr
= getItemContainer().readItems();
592 if (itemArr
!= null) {
593 // Sort the array by pid
594 Arrays
.sort(itemArr
);
596 // Update the view part
597 displayModel(itemArr
, experimentStartTime
, experimentEndTime
, false, request
.getRange().getStartTime()
598 .getValue(), request
.getRange().getEndTime().getValue(), request
.getSource());
602 // reselect to original time
603 ParamsUpdater paramUpdater
= getParamsUpdater();
604 if ((paramUpdater
!= null) && (tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
605 final Long selTime
= paramUpdater
.getSelectedTime();
606 if (selTime
!= null) {
607 TraceDebug
.debug("View: " + getName() + "\n\t\tRestoring the selected time to: " + selTime
);
608 Display display
= tsfviewer
.getControl().getDisplay();
609 display
.asyncExec(new Runnable() {
612 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
613 tsfviewer
.setSelectedTime(selTime
, false, this);
619 // System.out.println(System.currentTimeMillis() + ": AbsTimeUpdate (" + getName() + ") completed");
621 if (TraceDebug
.isDEBUG()) {
623 Long count
= request
.getSynEventCount();
624 for (int pos
= 0; pos
< itemArr
.length
; pos
++) {
625 eventCount
+= itemArr
[pos
].getTraceEvents().size();
628 int discarded
= paramUpdater
.getEventsDiscarded();
629 int discardedOutofOrder
= paramUpdater
.getEventsDiscardedWrongOrder();
630 int discardedOutofViewRange
= paramUpdater
.getEventsDiscardedOutOfViewRange();
631 int dicardedNotVisible
= paramUpdater
.getEventsDiscardedNotVisible();
633 TmfTimeRange range
= request
.getRange();
634 StringBuilder sb
= new StringBuilder("View: " + getName() + ", Events handled: " + count
635 + ", Events loaded in view: " + eventCount
+ ", Number of events discarded: " + discarded
636 + "\n\tNumber of events discarded with start time earlier than next good time: "
637 + discardedOutofOrder
+ "\n\tDiscarded Not visible: " + dicardedNotVisible
638 + "\n\tDiscarded out of view Range: " + discardedOutofViewRange
);
640 sb
.append("\n\t\tRequested Time Range: " + range
.getStartTime() + "-" + range
.getEndTime());
641 sb
.append("\n\t\tExperiment Time Range: " + experimentStartTime
+ "-" + experimentEndTime
);
642 TraceDebug
.debug(sb
.toString());
650 // * Obtains the remainder fraction on unit Seconds of the entered value in
651 // * nanoseconds. e.g. input: 1241207054171080214 ns The number of seconds
653 // * be obtain by removing the last 9 digits: 1241207054 the fractional
654 // * portion of seconds, expressed in ns is: 171080214
659 // protected String formatNs(long v) {
660 // StringBuffer str = new StringBuffer();
661 // boolean neg = v < 0;
667 // String strVal = String.valueOf(v);
668 // if (v < 1000000000) {
672 // // Extract the last nine digits (e.g. fraction of a S expressed in ns
673 // return strVal.substring(strVal.length() - 9);
677 * The request was stopped, the data is incomplete
681 protected abstract void modelIncomplete(ILttngSyntEventRequest request
);
684 * Returns the Event processor instance related to a specific view
688 protected abstract ITransEventProcessor
getEventProcessor();
691 * To be overridden by some sub-classes although may not be needed in some
692 * e.g. statistics view
695 * @param startBoundTime
696 * @param endBoundTime
697 * @param updateTimeBounds
698 * - Time bounds updated needed e.g. if a new Experiment or trace
700 * @param startVisibleWindow
701 * @param endVisibleWindow
704 protected abstract void displayModel(final ITmfTimeAnalysisEntry
[] items
, final long startBoundTime
,
705 final long endBoundTime
, final boolean updateTimeBounds
, final long startVisibleWindow
,
706 final long endVisibleWindow
, final Object source
);
709 * To be overridden by some sub-classes although may not be needed in some
710 * e.g. statistics view
714 protected abstract ParamsUpdater
getParamsUpdater();
717 * Returns the model's item container
721 protected abstract ItemContainer
<?
> getItemContainer();