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
.LttngTimestamp
;
20 import org
.eclipse
.linuxtools
.lttng
.request
.ILttngSyntEventRequest
;
21 import org
.eclipse
.linuxtools
.lttng
.request
.IRequestStatusListener
;
22 import org
.eclipse
.linuxtools
.lttng
.request
.LttngSyntEventRequest
;
23 import org
.eclipse
.linuxtools
.lttng
.request
.RequestCompletedSignal
;
24 import org
.eclipse
.linuxtools
.lttng
.request
.RequestStartedSignal
;
25 import org
.eclipse
.linuxtools
.lttng
.state
.evProcessor
.ITransEventProcessor
;
26 import org
.eclipse
.linuxtools
.lttng
.ui
.TraceDebug
;
27 import org
.eclipse
.linuxtools
.lttng
.ui
.model
.trange
.ItemContainer
;
28 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
29 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
31 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
.ExecutionType
;
32 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
33 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfRangeSynchSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalHandler
;
35 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalManager
;
36 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfTimeSynchSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.ITimeAnalysisViewer
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.TmfTimeScaleSelectionEvent
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.TmfTimeSelectionEvent
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITmfTimeAnalysisEntry
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
42 import org
.eclipse
.swt
.widgets
.Display
;
46 * Abstract class used as a base for views handling specific time range data
50 * The class handles a single element queue of data requests, i.e. request can
51 * be triggered from different sources e.g. opening a file as well as a new
52 * selected time window
58 public abstract class AbsTimeUpdateView
extends TmfView
implements IRequestStatusListener
{
60 // ========================================================================
62 // ========================================================================
64 // private static final long INITIAL_WINDOW_OFFSET = (1L * 1 * 1000 * 1000); // .001sec
65 // private static final long INITIAL_WINDOW_OFFSET = (1L * 10 * 1000 * 1000); // .01sec
66 private static final long INITIAL_WINDOW_OFFSET
= (1L * 100 * 1000 * 1000); // .1sec
67 // private static final long INITIAL_WINDOW_OFFSET = (1L * 1000 * 1000 * 1000); // 1sec
70 * Number of events before a GUI refresh
72 protected static final Long INPUT_CHANGED_REFRESH
= 75000L;
73 private static final long DEFAULT_OFFSET
= 0L;
74 private static final int DEFAULT_CHUNK
= 1;
76 protected boolean synch
= true; // time synchronization, used to be an option
77 protected ITimeAnalysisViewer tsfviewer
= null;
79 private LttngSyntEventRequest fCurrentRequest
= null;
81 protected LttngSyntheticEventProvider fProvider
= LttngCoreProviderFactory
.getEventProvider(getProviderId());
83 // ========================================================================
85 // ========================================================================
86 public AbsTimeUpdateView(String viewID
) {
88 // freqState = UiCommonFactory.getQueue(this);
91 // ========================================================================
93 // ========================================================================
96 * Returns the number of events after which the relevant display will
101 protected Long
getInputChangedRefresh() {
102 return INPUT_CHANGED_REFRESH
;
108 * @seeorg.eclipse.linuxtools.lttng.state.IStateDataRequestListener#
109 * processingStarted(org.eclipse.linuxtools.lttng.state.StateDataRequest)
113 public synchronized void processingStarted(RequestStartedSignal signal
) {
114 LttngSyntEventRequest request
= signal
.getRequest();
115 if (request
!= null) {
116 // update queue with the id of the current request.
117 // freqState.requestStarted(request);
119 // if there was no new request then this one is still on
120 // prepare for the reception of new data
123 // no new time range for zoom orders
124 TmfTimeRange trange
= null;
125 // Time Range will be used to filter out events which are
126 // not visible in one pixel
127 trange
= request
.getRange();
129 // indicate if the data model needs to be cleared e.g. a new
130 // experiment is being selected
131 boolean clearData
= request
.isclearDataInd();
132 // Indicate if current data needs to be cleared and if so
133 // specify the new experiment time range that applies
134 ModelUpdatePrep(trange
, clearData
);
141 * @seeorg.eclipse.linuxtools.lttng.state.IStateDataRequestListener#
142 * processingCompleted(org.eclipse.linuxtools.lttng.state.StateDataRequest)
146 public void processingCompleted(RequestCompletedSignal signal
) {
147 ILttngSyntEventRequest request
= signal
.getRequest();
149 if (request
== null) {
153 // Update wait cursor
156 // No data refresh actions for cancelled requests.
157 if (request
.isCancelled() || request
.isFailed()) {
158 if (TraceDebug
.isDEBUG()) {
159 TmfTimeRange trange
= request
.getRange();
160 if (request
.isCancelled()) {
161 TraceDebug
.debug("Request cancelled " //$NON-NLS-1$
162 + trange
.getStartTime() + "-" + trange
.getEndTime() //$NON-NLS-1$
163 + " Handled Events: " + request
.getSynEventCount() //$NON-NLS-1$
164 + " " + request
.toString(), 15); //$NON-NLS-1$
165 } else if (request
.isFailed()) {
166 TraceDebug
.debug("Request Failed " + trange
.getStartTime() //$NON-NLS-1$
167 + "-" + trange
.getEndTime() + " Handled Events: " //$NON-NLS-1$ //$NON-NLS-2$
168 + request
.getSynEventCount() + " " //$NON-NLS-1$
169 + request
.toString());
175 modelInputChanged(request
, true);
180 * Registers as listener of time selection from other views
184 public void synchToTime(TmfTimeSynchSignal signal
) {
186 Object source
= signal
.getSource();
187 if (signal
!= null && source
!= null && source
!= this) {
188 // Internal value is expected in nano seconds.
189 long selectedTime
= signal
.getCurrentTime().getValue();
190 if (tsfviewer
!= null) {
191 tsfviewer
.setSelectedTime(selectedTime
, true, source
);
193 ParamsUpdater paramUpdater
= getParamsUpdater();
194 Long savedSelTime
= paramUpdater
.getSelectedTime();
195 if ((savedSelTime
== null) || (savedSelTime
!= selectedTime
)) {
196 // Update the parameter updater to save the selected time
197 paramUpdater
.setSelectedTime(selectedTime
);
205 * Process the reception of time window adjustment in this view if the
206 * source of the update is not this view.
209 * @param clearingData
211 public void synchToTimeRange(TmfRangeSynchSignal signal
, boolean clearingData
) {
213 Object source
= signal
.getSource();
214 if (signal
!= null && source
!= null && source
!= this) {
215 // Internal value is expected in nano seconds.
216 TmfTimeRange trange
= signal
.getCurrentRange();
217 TmfExperiment
<?
> experiment
= TmfExperiment
.getCurrentExperiment();
218 if (experiment
== null) {
219 TraceDebug
.debug("Current selected experiment is null"); //$NON-NLS-1$
223 // Clearing of process data is configurable
224 dataRequest(trange
, experiment
.getTimeRange(), clearingData
, ExecutionType
.FOREGROUND
);
230 * Trigger time synchronisation to other views this method shall be called
231 * when a check has been performed to note that an actual change of time has
232 * been performed vs a pure re-selection of the same time
237 protected void synchTimeNotification(long time
, Object source
) {
238 // if synchronisation selected
240 // Notify other views
241 TmfSignalManager
.dispatchSignal(new TmfTimeSynchSignal(source
, new LttngTimestamp(time
)));
246 * Common implementation of ITmfTimeSelectionListener, not used by all the
247 * views extending this abstract class
251 protected void tsfTmProcessSelEvent(TmfTimeSelectionEvent event
) {
252 Object source
= event
.getSource();
253 if (source
== null) {
257 ParamsUpdater paramUpdater
= getParamsUpdater();
258 Long savedSelTime
= paramUpdater
.getSelectedTime();
260 long selTimens
= event
.getSelectedTime();
262 // make sure the new selected time is different than saved before
264 if (savedSelTime
== null || savedSelTime
!= selTimens
) {
265 // Notify listener views.
266 synchTimeNotification(selTimens
, source
);
268 // Update the parameter updater to save the selected time
269 paramUpdater
.setSelectedTime(selTimens
);
271 if (TraceDebug
.isDEBUG()) {
272 TraceDebug
.debug("Selected Time: " + new LttngTimestamp(selTimens
) + "\n\t\t" + getName()); //$NON-NLS-1$ //$NON-NLS-2$
278 * Common implementation of ITmfTimeScaleSelectionListener, not used by all
279 * the views extending this abstract class
283 protected void tsfTmProcessTimeScaleEvent(TmfTimeScaleSelectionEvent event
) {
284 // source needed to keep track of source values
285 Object source
= event
.getSource();
287 boolean newParams
= false;
288 TmfTimeRange trange
= null;
289 Long selectedTime
= null;
291 // update all information and get relevant data
292 synchronized (this) {
293 if (source
!= null) {
294 // Update the parameter updater before carrying out a read request
295 ParamsUpdater paramUpdater
= getParamsUpdater();
296 newParams
= paramUpdater
.processTimeScaleEvent(event
);
299 // Read the updated time window
300 trange
= paramUpdater
.getTrange();
301 if (trange
!= null) {
302 selectedTime
= paramUpdater
.getSelectedTime();
308 // Check for selectedTime is sufficient since it is only set if
309 // newParams is true and trange is not null
310 if (selectedTime
!= null) {
311 // Notify listener views. to perform data requests
312 // upon this notification
314 // Note that this has to be done outside the synchronized statement
315 // because otherwise we could end-up in a deadlock if a ongoing
316 // request needs to be canceled.
317 synchTimeRangeNotification(trange
, selectedTime
, source
);
322 * Inform registered listeners about the new time range
325 * @param selectedTime
328 protected void synchTimeRangeNotification(TmfTimeRange trange
, Long selectedTime
, Object source
) {
329 // if synchronisation selected
331 // Notify other views
332 TmfSignalManager
.dispatchSignal(new TmfRangeSynchSignal(source
, trange
, new LttngTimestamp(selectedTime
)));
337 * @param zoomedTRange
338 * @param experimentTRange
341 public void dataRequest(TmfTimeRange zoomedTRange
,
342 TmfTimeRange experimentTRange
, boolean clearingData
, ExecutionType execType
) {
344 // timeRange is the Experiment time range
345 boolean sent
= processDataRequest(zoomedTRange
, experimentTRange
, clearingData
, execType
);
353 // * @param zoomedTRange
354 // * @param experimentTRange
357 // public void dataRequest(TmfTimeRange zoomedTRange,
358 // TmfTimeRange experimentTRange, boolean clearingData) {
360 // // timeRange is the Experiment time range
361 // boolean sent = processDataRequest(zoomedTRange, experimentTRange, clearingData);
369 * send data request directly e.g. doesn't use a queue
371 * @param requestTrange
373 * @param experimentTRange
378 private boolean processDataRequest(TmfTimeRange requestTrange
,
379 TmfTimeRange experimentTRange
, boolean clearingData
, ExecutionType execType
) {
381 if (requestTrange
== null || experimentTRange
== null) {
382 TraceDebug
.debug("Invalid input"); //$NON-NLS-1$
386 // Cancel the currently executing request before starting a new one
387 fProvider
.conditionallyCancelRequests();
389 fCurrentRequest
= new LttngSyntEventRequest(
390 requestTrange
, DEFAULT_OFFSET
, TmfDataRequest
.ALL_DATA
,
391 DEFAULT_CHUNK
, this, experimentTRange
, getEventProcessor(),
392 TmfExperiment
.getCurrentExperiment().getName(), execType
) {
394 Long fCount
= getSynEventCount();
395 ITransEventProcessor processor
= getProcessor();
396 TmfTimestamp frunningTimeStamp
;
402 * org.eclipse.linuxtools.lttng.request.LttngSyntEventRequest#handleData
405 //// int handleDataCount = 0;
406 //// int handleDataValidCount = 0;
408 // public void handleData() {
409 // LttngSyntheticEvent[] result = getData();
411 // TmfEvent evt = (result.length > 0) ? result[0] : null;
412 //// handleDataCount++;
415 public void handleData(LttngSyntheticEvent event
) {
416 super.handleData(event
);
418 // handleDataValidCount++;
419 LttngSyntheticEvent synEvent
= (LttngSyntheticEvent
) event
;
421 switch (synEvent
.getSynType()) {
424 handleRequestStarted();
429 processor
.process(event
, synEvent
.getTraceModel());
431 if ((fCount
!= 0) && (fCount
% getInputChangedRefresh() == 0)) {
432 // send partial update
433 modelInputChanged(this, false);
435 if (TraceDebug
.isDEBUG()) {
436 frunningTimeStamp
= event
.getTimestamp();
437 TraceDebug
.debug("handled: " + fCount
+ " sequence: " + synEvent
.getSynType()); //$NON-NLS-1$ //$NON-NLS-2$
446 processor
.process(event
, synEvent
.getTraceModel());
457 public void handleRequestStarted() {
463 // if (TraceDebug.isDEBUG()) {
464 // TraceDebug.debug("AbsTimeUpdateView: Received=" + handleDataCount + ", Valid=" + handleDataCount + ", fCount=" + fCount);
470 public void handleCompleted() {
471 super.handleCompleted();
473 // Data is not complete and should be handled as such
474 if (isFailed() || isCancelled()) {
475 modelIncomplete(this);
478 if (TraceDebug
.isDEBUG()) {
479 if (frunningTimeStamp
!= null) {
480 TraceDebug
.debug("Last event time stamp: " + frunningTimeStamp
.getValue()); //$NON-NLS-1$
486 // send the request to TMF
487 fCurrentRequest
.startRequestInd(fProvider
);
488 fCurrentRequest
.setclearDataInd(clearingData
);
493 * Returns an initial smaller window to allow the user to select the area of
496 * @param experimentTRange
499 protected TmfTimeRange
getInitTRange(TmfTimeRange experimentTRange
) {
500 TmfTimestamp expStartTime
= experimentTRange
.getStartTime();
501 TmfTimestamp expEndTime
= experimentTRange
.getEndTime();
502 TmfTimestamp initialEndOfWindow
= new LttngTimestamp(expStartTime
504 + INITIAL_WINDOW_OFFSET
);
505 if (initialEndOfWindow
.compareTo(expEndTime
, false) < 0) {
506 return new TmfTimeRange(expStartTime
, initialEndOfWindow
);
509 // The original size of the experiment is smaller than proposed adjusted
511 return experimentTRange
;
515 * Request the Time Analysis widget to enable or disable the wait cursor
516 * e.g. data request in progress or data request completed
520 protected void waitCursor(final boolean waitInd
) {
521 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
522 Display display
= tsfviewer
.getControl().getDisplay();
524 // Perform the updates on the UI thread
525 display
.asyncExec(new Runnable() {
528 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
529 tsfviewer
.waitCursor(waitInd
);
537 * View preparation to override the current local information
540 * - new total time range e.g. Experiment level
541 * @param clearAllData
543 protected void ModelUpdatePrep(TmfTimeRange timeRange
, boolean clearAllData
) {
544 ItemContainer
<?
> itemContainer
= getItemContainer();
546 // start fresh e.g. new experiment selected
547 itemContainer
.clearItems();
549 // clear children but keep processes
550 itemContainer
.clearChildren();
553 // Obtain the current resource array
554 ITmfTimeAnalysisEntry
[] itemArr
= itemContainer
.readItems();
556 // clean up data and boundaries
557 displayModel(itemArr
, -1, -1, false, -1, -1, null);
559 ParamsUpdater updater
= getParamsUpdater();
560 if (updater
!= null) {
562 updater
.setEventsDiscarded(0);
564 // Update new visible time range if available
565 if (timeRange
!= null) {
566 updater
.update(timeRange
.getStartTime().getValue(), timeRange
.getEndTime().getValue());
572 * Initialize the model and view before reloading items
574 * @param boundaryRange
575 * @param visibleRange
578 protected void ModelUpdateInit(TmfTimeRange boundaryRange
, TmfTimeRange visibleRange
, Object source
) {
579 // Update the view boundaries
580 if (boundaryRange
!= null) {
581 ItemContainer
<?
> itemContainer
= getItemContainer();
582 if (itemContainer
!= null) {
583 itemContainer
.clearItems();
584 // Obtain the current process array
585 ITmfTimeAnalysisEntry
[] itemArr
= itemContainer
.readItems();
587 long startTime
= boundaryRange
.getStartTime().getValue();
588 long endTime
= boundaryRange
.getEndTime().getValue();
590 // Update the view part
591 displayModel(itemArr
, startTime
, endTime
, true, visibleRange
.getStartTime().getValue(), visibleRange
592 .getEndTime().getValue(), source
);
596 // update the view filtering parameters
597 if (visibleRange
!= null) {
598 ParamsUpdater updater
= getParamsUpdater();
599 if (updater
!= null) {
601 updater
.setEventsDiscarded(0);
602 // Update new visible time range if available
603 updater
.update(visibleRange
.getStartTime().getValue(), visibleRange
.getEndTime().getValue());
609 * Actions taken by the view to refresh its widget(s) with the updated data
614 * true: yes, false: partial update
616 protected void modelInputChanged(ILttngSyntEventRequest request
, boolean complete
) {
617 long experimentStartTime
= -1;
618 long experimentEndTime
= -1;
619 TmfTimeRange experimentTimeRange
= request
.getExperimentTimeRange();
620 if (experimentTimeRange
!= null) {
621 experimentStartTime
= experimentTimeRange
.getStartTime().getValue();
622 experimentEndTime
= experimentTimeRange
.getEndTime().getValue();
625 // Obtain the current resource list
626 ITmfTimeAnalysisEntry
[] itemArr
= getItemContainer().readItems();
628 if (itemArr
!= null) {
629 // Sort the array by pid
630 Arrays
.sort(itemArr
);
632 // Update the view part
633 displayModel(itemArr
, experimentStartTime
, experimentEndTime
, false, request
.getRange().getStartTime()
634 .getValue(), request
.getRange().getEndTime().getValue(), request
.getSource());
638 // reselect to original time
639 ParamsUpdater paramUpdater
= getParamsUpdater();
640 if ((paramUpdater
!= null) && (tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
641 final Long selTime
= paramUpdater
.getSelectedTime();
642 if (selTime
!= null) {
643 TraceDebug
.debug("View: " + getName() + "\n\t\tRestoring the selected time to: " + selTime
); //$NON-NLS-1$ //$NON-NLS-2$
644 Display display
= tsfviewer
.getControl().getDisplay();
645 display
.asyncExec(new Runnable() {
648 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
649 tsfviewer
.setSelectedTime(selTime
, false, this);
655 // System.out.println(System.currentTimeMillis() + ": AbsTimeUpdate (" + getName() + ") completed");
657 if (TraceDebug
.isDEBUG()) {
659 Long count
= request
.getSynEventCount();
660 for (int pos
= 0; pos
< itemArr
.length
; pos
++) {
661 eventCount
+= itemArr
[pos
].getTraceEvents().size();
664 int discarded
= paramUpdater
.getEventsDiscarded();
665 int discardedOutofOrder
= paramUpdater
.getEventsDiscardedWrongOrder();
666 int discardedOutofViewRange
= paramUpdater
.getEventsDiscardedOutOfViewRange();
667 int dicardedNotVisible
= paramUpdater
.getEventsDiscardedNotVisible();
669 TmfTimeRange range
= request
.getRange();
670 StringBuilder sb
= new StringBuilder("View: " + getName() + ", Events handled: " + count
//$NON-NLS-1$ //$NON-NLS-2$
671 + ", Events loaded in view: " + eventCount
+ ", Number of events discarded: " + discarded
//$NON-NLS-1$ //$NON-NLS-2$
672 + "\n\tNumber of events discarded with start time earlier than next good time: " //$NON-NLS-1$
673 + discardedOutofOrder
+ "\n\tDiscarded Not visible: " + dicardedNotVisible
//$NON-NLS-1$
674 + "\n\tDiscarded out of view Range: " + discardedOutofViewRange
); //$NON-NLS-1$
676 sb
.append("\n\t\tRequested Time Range: " + range
.getStartTime() + "-" + range
.getEndTime()); //$NON-NLS-1$ //$NON-NLS-2$
677 sb
.append("\n\t\tExperiment Time Range: " + experimentStartTime
+ "-" + experimentEndTime
); //$NON-NLS-1$ //$NON-NLS-2$
678 TraceDebug
.debug(sb
.toString());
686 // * Obtains the remainder fraction on unit Seconds of the entered value in
687 // * nanoseconds. e.g. input: 1241207054171080214 ns The number of seconds
689 // * be obtain by removing the last 9 digits: 1241207054 the fractional
690 // * portion of seconds, expressed in ns is: 171080214
695 // protected String formatNs(long v) {
696 // StringBuffer str = new StringBuffer();
697 // boolean neg = v < 0;
703 // String strVal = String.valueOf(v);
704 // if (v < 1000000000) {
708 // // Extract the last nine digits (e.g. fraction of a S expressed in ns
709 // return strVal.substring(strVal.length() - 9);
713 * The request was stopped, the data is incomplete
717 protected abstract void modelIncomplete(ILttngSyntEventRequest request
);
720 * Returns the Event processor instance related to a specific view
724 protected abstract ITransEventProcessor
getEventProcessor();
727 * To be overridden by some sub-classes although may not be needed in some
728 * e.g. statistics view
731 * @param startBoundTime
732 * @param endBoundTime
733 * @param updateTimeBounds
734 * - Time bounds updated needed e.g. if a new Experiment or trace
736 * @param startVisibleWindow
737 * @param endVisibleWindow
740 protected abstract void displayModel(final ITmfTimeAnalysisEntry
[] items
, final long startBoundTime
,
741 final long endBoundTime
, final boolean updateTimeBounds
, final long startVisibleWindow
,
742 final long endVisibleWindow
, final Object source
);
745 * To be overridden by some sub-classes although may not be needed in some
746 * e.g. statistics view
750 protected abstract ParamsUpdater
getParamsUpdater();
753 * Returns the model's item container
757 protected abstract ItemContainer
<?
> getItemContainer();
760 * Returns LTTng Synthetic Provider ID used for current view
764 protected abstract int getProviderId();