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
= 20000L;
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
);
198 * Process the reception of time window adjustment in this view if the
199 * source of the update is not this view.
202 * @param clearingData
204 public void synchToTimeRange(TmfRangeSynchSignal signal
, boolean clearingData
) {
206 Object source
= signal
.getSource();
207 if (signal
!= null && source
!= null && source
!= this) {
208 // Internal value is expected in nano seconds.
209 TmfTimeRange trange
= signal
.getCurrentRange();
210 TmfExperiment
<?
> experiment
= TmfExperiment
.getCurrentExperiment();
211 if (experiment
== null) {
212 TraceDebug
.debug("Current selected experiment is null"); //$NON-NLS-1$
216 // Clearing of process data is configurable
217 dataRequest(trange
, experiment
.getTimeRange(), clearingData
, ExecutionType
.FOREGROUND
);
223 * Trigger time synchronisation to other views this method shall be called
224 * when a check has been performed to note that an actual change of time has
225 * been performed vs a pure re-selection of the same time
230 protected void synchTimeNotification(long time
, Object source
) {
231 // if synchronisation selected
233 // Notify other views
234 TmfSignalManager
.dispatchSignal(new TmfTimeSynchSignal(source
, new LttngTimestamp(time
)));
239 * Common implementation of ITmfTimeSelectionListener, not used by all the
240 * views extending this abstract class
244 protected void tsfTmProcessSelEvent(TmfTimeSelectionEvent event
) {
245 Object source
= event
.getSource();
246 if (source
== null) {
250 ParamsUpdater paramUpdater
= getParamsUpdater();
251 Long savedSelTime
= paramUpdater
.getSelectedTime();
253 long selTimens
= event
.getSelectedTime();
255 // make sure the new selected time is different than saved before
257 if (savedSelTime
== null || savedSelTime
!= selTimens
) {
258 // Notify listener views.
259 synchTimeNotification(selTimens
, source
);
261 // Update the parameter updater to save the selected time
262 paramUpdater
.setSelectedTime(selTimens
);
264 if (TraceDebug
.isDEBUG()) {
265 TraceDebug
.debug("Selected Time: " + new LttngTimestamp(selTimens
) + "\n\t\t" + getName()); //$NON-NLS-1$ //$NON-NLS-2$
271 * Common implementation of ITmfTimeScaleSelectionListener, not used by all
272 * the views extending this abstract class
276 protected void tsfTmProcessTimeScaleEvent(TmfTimeScaleSelectionEvent event
) {
277 // source needed to keep track of source values
278 Object source
= event
.getSource();
280 boolean newParams
= false;
281 TmfTimeRange trange
= null;
282 Long selectedTime
= null;
284 // update all information and get relevant data
285 synchronized (this) {
286 if (source
!= null) {
287 // Update the parameter updater before carrying out a read request
288 ParamsUpdater paramUpdater
= getParamsUpdater();
289 newParams
= paramUpdater
.processTimeScaleEvent(event
);
292 // Read the updated time window
293 trange
= paramUpdater
.getTrange();
294 if (trange
!= null) {
295 selectedTime
= paramUpdater
.getSelectedTime();
301 // Check for selectedTime is sufficient since it is only set if
302 // newParams is true and trange is not null
303 if (selectedTime
!= null) {
304 // Notify listener views. to perform data requests
305 // upon this notification
307 // Note that this has to be done outside the synchronized statement
308 // because otherwise we could end-up in a deadlock if a ongoing
309 // request needs to be canceled.
310 synchTimeRangeNotification(trange
, selectedTime
, source
);
315 * Inform registered listeners about the new time range
318 * @param selectedTime
321 protected void synchTimeRangeNotification(TmfTimeRange trange
, Long selectedTime
, Object source
) {
322 // if synchronisation selected
324 // Notify other views
325 TmfSignalManager
.dispatchSignal(new TmfRangeSynchSignal(source
, trange
, new LttngTimestamp(selectedTime
)));
330 * @param zoomedTRange
331 * @param experimentTRange
334 public void dataRequest(TmfTimeRange zoomedTRange
,
335 TmfTimeRange experimentTRange
, boolean clearingData
, ExecutionType execType
) {
337 // timeRange is the Experiment time range
338 boolean sent
= processDataRequest(zoomedTRange
, experimentTRange
, clearingData
, execType
);
346 // * @param zoomedTRange
347 // * @param experimentTRange
350 // public void dataRequest(TmfTimeRange zoomedTRange,
351 // TmfTimeRange experimentTRange, boolean clearingData) {
353 // // timeRange is the Experiment time range
354 // boolean sent = processDataRequest(zoomedTRange, experimentTRange, clearingData);
362 * send data request directly e.g. doesn't use a queue
364 * @param requestTrange
366 * @param experimentTRange
371 private boolean processDataRequest(TmfTimeRange requestTrange
,
372 TmfTimeRange experimentTRange
, boolean clearingData
, ExecutionType execType
) {
374 if (requestTrange
== null || experimentTRange
== null) {
375 TraceDebug
.debug("Invalid input"); //$NON-NLS-1$
379 // Cancel the currently executing request before starting a new one
380 fProvider
.conditionallyCancelRequests();
382 fCurrentRequest
= new LttngSyntEventRequest(
383 requestTrange
, DEFAULT_OFFSET
, TmfDataRequest
.ALL_DATA
,
384 DEFAULT_CHUNK
, this, experimentTRange
, getEventProcessor(),
385 TmfExperiment
.getCurrentExperiment().getName(), execType
) {
387 Long fCount
= getSynEventCount();
388 ITransEventProcessor processor
= getProcessor();
389 TmfTimestamp frunningTimeStamp
;
395 * org.eclipse.linuxtools.lttng.request.LttngSyntEventRequest#handleData
398 //// int handleDataCount = 0;
399 //// int handleDataValidCount = 0;
401 // public void handleData() {
402 // LttngSyntheticEvent[] result = getData();
404 // TmfEvent evt = (result.length > 0) ? result[0] : null;
405 //// handleDataCount++;
408 public void handleData(LttngSyntheticEvent event
) {
409 super.handleData(event
);
411 // handleDataValidCount++;
412 LttngSyntheticEvent synEvent
= (LttngSyntheticEvent
) event
;
414 switch (synEvent
.getSynType()) {
417 handleRequestStarted();
422 processor
.process(event
, synEvent
.getTraceModel());
424 if ((fCount
!= 0) && (fCount
% getInputChangedRefresh() == 0)) {
425 // send partial update
426 modelInputChanged(this, false);
428 if (TraceDebug
.isDEBUG()) {
429 frunningTimeStamp
= event
.getTimestamp();
430 TraceDebug
.debug("handled: " + fCount
+ " sequence: " + synEvent
.getSynType()); //$NON-NLS-1$ //$NON-NLS-2$
439 processor
.process(event
, synEvent
.getTraceModel());
450 public void handleRequestStarted() {
456 // if (TraceDebug.isDEBUG()) {
457 // TraceDebug.debug("AbsTimeUpdateView: Received=" + handleDataCount + ", Valid=" + handleDataCount + ", fCount=" + fCount);
463 public void handleCompleted() {
464 super.handleCompleted();
466 // Data is not complete and should be handled as such
467 if (isFailed() || isCancelled()) {
468 modelIncomplete(this);
471 if (TraceDebug
.isDEBUG()) {
472 if (frunningTimeStamp
!= null) {
473 TraceDebug
.debug("Last event time stamp: " + frunningTimeStamp
.getValue()); //$NON-NLS-1$
479 // send the request to TMF
480 fCurrentRequest
.startRequestInd(fProvider
);
481 fCurrentRequest
.setclearDataInd(clearingData
);
486 * Returns an initial smaller window to allow the user to select the area of
489 * @param experimentTRange
492 protected TmfTimeRange
getInitTRange(TmfTimeRange experimentTRange
) {
493 TmfTimestamp expStartTime
= experimentTRange
.getStartTime();
494 TmfTimestamp expEndTime
= experimentTRange
.getEndTime();
495 TmfTimestamp initialEndOfWindow
= new LttngTimestamp(expStartTime
497 + INITIAL_WINDOW_OFFSET
);
498 if (initialEndOfWindow
.compareTo(expEndTime
, false) < 0) {
499 return new TmfTimeRange(expStartTime
, initialEndOfWindow
);
502 // The original size of the experiment is smaller than proposed adjusted
504 return experimentTRange
;
508 * Request the Time Analysis widget to enable or disable the wait cursor
509 * e.g. data request in progress or data request completed
513 protected void waitCursor(final boolean waitInd
) {
514 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
515 Display display
= tsfviewer
.getControl().getDisplay();
517 // Perform the updates on the UI thread
518 display
.asyncExec(new Runnable() {
521 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
522 tsfviewer
.waitCursor(waitInd
);
530 * View preparation to override the current local information
533 * - new total time range e.g. Experiment level
534 * @param clearAllData
536 protected void ModelUpdatePrep(TmfTimeRange timeRange
, boolean clearAllData
) {
537 ItemContainer
<?
> itemContainer
= getItemContainer();
539 // start fresh e.g. new experiment selected
540 itemContainer
.clearItems();
542 // clear children but keep processes
543 itemContainer
.clearChildren();
546 // Obtain the current resource array
547 ITmfTimeAnalysisEntry
[] itemArr
= itemContainer
.readItems();
549 // clean up data and boundaries
550 displayModel(itemArr
, -1, -1, false, -1, -1, null);
552 ParamsUpdater updater
= getParamsUpdater();
553 if (updater
!= null) {
555 updater
.setEventsDiscarded(0);
557 // Update new visible time range if available
558 if (timeRange
!= null) {
559 updater
.update(timeRange
.getStartTime().getValue(), timeRange
.getEndTime().getValue());
565 * Initialize the model and view before reloading items
567 * @param boundaryRange
568 * @param visibleRange
571 protected void ModelUpdateInit(TmfTimeRange boundaryRange
, TmfTimeRange visibleRange
, Object source
) {
572 // Update the view boundaries
573 if (boundaryRange
!= null) {
574 ItemContainer
<?
> itemContainer
= getItemContainer();
575 if (itemContainer
!= null) {
576 itemContainer
.clearItems();
577 // Obtain the current process array
578 ITmfTimeAnalysisEntry
[] itemArr
= itemContainer
.readItems();
580 long startTime
= boundaryRange
.getStartTime().getValue();
581 long endTime
= boundaryRange
.getEndTime().getValue();
583 // Update the view part
584 displayModel(itemArr
, startTime
, endTime
, true, visibleRange
.getStartTime().getValue(), visibleRange
585 .getEndTime().getValue(), source
);
589 // update the view filtering parameters
590 if (visibleRange
!= null) {
591 ParamsUpdater updater
= getParamsUpdater();
592 if (updater
!= null) {
594 updater
.setEventsDiscarded(0);
595 // Update new visible time range if available
596 updater
.update(visibleRange
.getStartTime().getValue(), visibleRange
.getEndTime().getValue());
602 * Actions taken by the view to refresh its widget(s) with the updated data
607 * true: yes, false: partial update
609 protected void modelInputChanged(ILttngSyntEventRequest request
, boolean complete
) {
610 long experimentStartTime
= -1;
611 long experimentEndTime
= -1;
612 TmfTimeRange experimentTimeRange
= request
.getExperimentTimeRange();
613 if (experimentTimeRange
!= null) {
614 experimentStartTime
= experimentTimeRange
.getStartTime().getValue();
615 experimentEndTime
= experimentTimeRange
.getEndTime().getValue();
618 // Obtain the current resource list
619 ITmfTimeAnalysisEntry
[] itemArr
= getItemContainer().readItems();
621 if (itemArr
!= null) {
622 // Sort the array by pid
623 Arrays
.sort(itemArr
);
625 // Update the view part
626 displayModel(itemArr
, experimentStartTime
, experimentEndTime
, false, request
.getRange().getStartTime()
627 .getValue(), request
.getRange().getEndTime().getValue(), request
.getSource());
631 // reselect to original time
632 ParamsUpdater paramUpdater
= getParamsUpdater();
633 if ((paramUpdater
!= null) && (tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
634 final Long selTime
= paramUpdater
.getSelectedTime();
635 if (selTime
!= null) {
636 TraceDebug
.debug("View: " + getName() + "\n\t\tRestoring the selected time to: " + selTime
); //$NON-NLS-1$ //$NON-NLS-2$
637 Display display
= tsfviewer
.getControl().getDisplay();
638 display
.asyncExec(new Runnable() {
641 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
642 tsfviewer
.setSelectedTime(selTime
, false, this);
648 // System.out.println(System.currentTimeMillis() + ": AbsTimeUpdate (" + getName() + ") completed");
650 if (TraceDebug
.isDEBUG()) {
652 Long count
= request
.getSynEventCount();
653 for (int pos
= 0; pos
< itemArr
.length
; pos
++) {
654 eventCount
+= itemArr
[pos
].getTraceEvents().size();
657 int discarded
= paramUpdater
.getEventsDiscarded();
658 int discardedOutofOrder
= paramUpdater
.getEventsDiscardedWrongOrder();
659 int discardedOutofViewRange
= paramUpdater
.getEventsDiscardedOutOfViewRange();
660 int dicardedNotVisible
= paramUpdater
.getEventsDiscardedNotVisible();
662 TmfTimeRange range
= request
.getRange();
663 StringBuilder sb
= new StringBuilder("View: " + getName() + ", Events handled: " + count
//$NON-NLS-1$ //$NON-NLS-2$
664 + ", Events loaded in view: " + eventCount
+ ", Number of events discarded: " + discarded
//$NON-NLS-1$ //$NON-NLS-2$
665 + "\n\tNumber of events discarded with start time earlier than next good time: " //$NON-NLS-1$
666 + discardedOutofOrder
+ "\n\tDiscarded Not visible: " + dicardedNotVisible
//$NON-NLS-1$
667 + "\n\tDiscarded out of view Range: " + discardedOutofViewRange
); //$NON-NLS-1$
669 sb
.append("\n\t\tRequested Time Range: " + range
.getStartTime() + "-" + range
.getEndTime()); //$NON-NLS-1$ //$NON-NLS-2$
670 sb
.append("\n\t\tExperiment Time Range: " + experimentStartTime
+ "-" + experimentEndTime
); //$NON-NLS-1$ //$NON-NLS-2$
671 TraceDebug
.debug(sb
.toString());
679 // * Obtains the remainder fraction on unit Seconds of the entered value in
680 // * nanoseconds. e.g. input: 1241207054171080214 ns The number of seconds
682 // * be obtain by removing the last 9 digits: 1241207054 the fractional
683 // * portion of seconds, expressed in ns is: 171080214
688 // protected String formatNs(long v) {
689 // StringBuffer str = new StringBuffer();
690 // boolean neg = v < 0;
696 // String strVal = String.valueOf(v);
697 // if (v < 1000000000) {
701 // // Extract the last nine digits (e.g. fraction of a S expressed in ns
702 // return strVal.substring(strVal.length() - 9);
706 * The request was stopped, the data is incomplete
710 protected abstract void modelIncomplete(ILttngSyntEventRequest request
);
713 * Returns the Event processor instance related to a specific view
717 protected abstract ITransEventProcessor
getEventProcessor();
720 * To be overridden by some sub-classes although may not be needed in some
721 * e.g. statistics view
724 * @param startBoundTime
725 * @param endBoundTime
726 * @param updateTimeBounds
727 * - Time bounds updated needed e.g. if a new Experiment or trace
729 * @param startVisibleWindow
730 * @param endVisibleWindow
733 protected abstract void displayModel(final ITmfTimeAnalysisEntry
[] items
, final long startBoundTime
,
734 final long endBoundTime
, final boolean updateTimeBounds
, final long startVisibleWindow
,
735 final long endVisibleWindow
, final Object source
);
738 * To be overridden by some sub-classes although may not be needed in some
739 * e.g. statistics view
743 protected abstract ParamsUpdater
getParamsUpdater();
746 * Returns the model's item container
750 protected abstract ItemContainer
<?
> getItemContainer();
753 * Returns LTTng Synthetic Provider ID used for current view
757 protected abstract int getProviderId();