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)
101 public synchronized void processingStarted(RequestStartedSignal signal
) {
102 LttngSyntEventRequest request
= signal
.getRequest();
103 if (request
!= null) {
104 // update queue with the id of the current request.
105 // freqState.requestStarted(request);
107 // if there was no new request then this one is still on
108 // prepare for the reception of new data
111 // no new time range for zoom orders
112 TmfTimeRange trange
= null;
113 // Time Range will be used to filter out events which are
114 // not visible in one pixel
115 trange
= request
.getRange();
117 // indicate if the data model needs to be cleared e.g. a new
118 // experiment is being selected
119 boolean clearData
= request
.isclearDataInd();
120 // Indicate if current data needs to be cleared and if so
121 // specify the new experiment time range that applies
122 ModelUpdatePrep(trange
, clearData
);
129 * @seeorg.eclipse.linuxtools.lttng.state.IStateDataRequestListener#
130 * processingCompleted(org.eclipse.linuxtools.lttng.state.StateDataRequest)
133 public void processingCompleted(RequestCompletedSignal signal
) {
134 ILttngSyntEventRequest request
= signal
.getRequest();
136 if (request
== null) {
140 // Update wait cursor
143 // No data refresh actions for cancelled requests.
144 if (request
.isCancelled() || request
.isFailed()) {
145 if (TraceDebug
.isDEBUG()) {
146 TmfTimeRange trange
= request
.getRange();
147 if (request
.isCancelled()) {
148 TraceDebug
.debug("Request cancelled "
149 + trange
.getStartTime() + "-" + trange
.getEndTime()
150 + " Handled Events: " + request
.getSynEventCount()
151 + " " + request
.toString(), 15);
152 } else if (request
.isFailed()) {
153 TraceDebug
.debug("Request Failed " + trange
.getStartTime()
154 + "-" + trange
.getEndTime() + " Handled Events: "
155 + request
.getSynEventCount() + " "
156 + request
.toString());
162 modelInputChanged(request
, true);
167 * Registers as listener of time selection from other views
171 public void synchToTime(TmfTimeSynchSignal signal
) {
173 Object source
= signal
.getSource();
174 if (signal
!= null && source
!= null && source
!= this) {
175 // Internal value is expected in nano seconds.
176 long selectedTime
= signal
.getCurrentTime().getValue();
177 if (tsfviewer
!= null) {
178 tsfviewer
.setSelectedTime(selectedTime
, true, source
);
185 * Process the reception of time window adjustment in this view if the
186 * source of the update is not this view.
189 * @param clearingData
191 public void synchToTimeRange(TmfRangeSynchSignal signal
, boolean clearingData
) {
193 Object source
= signal
.getSource();
194 if (signal
!= null && source
!= null && source
!= this) {
195 // Internal value is expected in nano seconds.
196 TmfTimeRange trange
= signal
.getCurrentRange();
197 TmfExperiment
<?
> experiment
= TmfExperiment
.getCurrentExperiment();
198 if (experiment
== null) {
199 TraceDebug
.debug("Current selected experiment is null");
203 // Clearing of process data is configurable
204 dataRequest(trange
, experiment
.getTimeRange(), clearingData
, ExecutionType
.FOREGROUND
);
210 * Trigger time synchronisation to other views this method shall be called
211 * when a check has been performed to note that an actual change of time has
212 * been performed vs a pure re-selection of the same time
217 protected void synchTimeNotification(long time
, Object source
) {
218 // if synchronisation selected
220 // Notify other views
221 TmfSignalManager
.dispatchSignal(new TmfTimeSynchSignal(source
, new LttngTimestamp(time
)));
226 * Common implementation of ITmfTimeSelectionListener, not used by all the
227 * views extending this abstract class
231 protected void tsfTmProcessSelEvent(TmfTimeSelectionEvent event
) {
232 Object source
= event
.getSource();
233 if (source
== null) {
237 ParamsUpdater paramUpdater
= getParamsUpdater();
238 Long savedSelTime
= paramUpdater
.getSelectedTime();
240 long selTimens
= event
.getSelectedTime();
242 // make sure the new selected time is different than saved before
244 if (savedSelTime
== null || savedSelTime
!= selTimens
) {
245 // Notify listener views.
246 synchTimeNotification(selTimens
, source
);
248 // Update the parameter updater to save the selected time
249 paramUpdater
.setSelectedTime(selTimens
);
251 if (TraceDebug
.isDEBUG()) {
252 TraceDebug
.debug("Selected Time: " + new LttngTimestamp(selTimens
) + "\n\t\t" + getName());
258 * Common implementation of ITmfTimeScaleSelectionListener, not used by all
259 * the views extending this abstract class
263 protected synchronized void tsfTmProcessTimeScaleEvent(TmfTimeScaleSelectionEvent event
) {
264 // source needed to keep track of source values
265 Object source
= event
.getSource();
267 if (source
!= null) {
268 // Update the parameter updater before carrying out a read request
269 ParamsUpdater paramUpdater
= getParamsUpdater();
270 boolean newParams
= paramUpdater
.processTimeScaleEvent(event
);
273 // Read the updated time window
274 TmfTimeRange trange
= paramUpdater
.getTrange();
275 if (trange
!= null) {
277 // Notify listener views. to perform data requests
278 // upon this notification
279 synchTimeRangeNotification(trange
, paramUpdater
.getSelectedTime(), source
);
286 * Inform registered listeners about the new time range
289 * @param selectedTime
292 protected void synchTimeRangeNotification(TmfTimeRange trange
, Long selectedTime
, Object source
) {
293 // if synchronisation selected
295 // Notify other views
296 TmfSignalManager
.dispatchSignal(new TmfRangeSynchSignal(source
, trange
, new LttngTimestamp(selectedTime
)));
301 * @param zoomedTRange
302 * @param experimentTRange
305 public void dataRequest(TmfTimeRange zoomedTRange
,
306 TmfTimeRange experimentTRange
, boolean clearingData
, ExecutionType execType
) {
308 // timeRange is the Experiment time range
309 boolean sent
= processDataRequest(zoomedTRange
, experimentTRange
, clearingData
, execType
);
317 // * @param zoomedTRange
318 // * @param experimentTRange
321 // public void dataRequest(TmfTimeRange zoomedTRange,
322 // TmfTimeRange experimentTRange, boolean clearingData) {
324 // // timeRange is the Experiment time range
325 // boolean sent = processDataRequest(zoomedTRange, experimentTRange, clearingData);
333 * send data request directly e.g. doesn't use a queue
335 * @param requestTrange
337 * @param experimentTRange
342 private boolean processDataRequest(TmfTimeRange requestTrange
,
343 TmfTimeRange experimentTRange
, boolean clearingData
, ExecutionType execType
) {
345 if (requestTrange
== null || experimentTRange
== null) {
346 TraceDebug
.debug("Invalid input");
350 // Cancel the currently executing request before starting a new one
351 if (fCurrentRequest
!= null && !fCurrentRequest
.isCompleted()) {
352 // System.out.println("Cancelling request");
353 // fCurrentRequest.cancel();
356 fCurrentRequest
= new LttngSyntEventRequest(
357 requestTrange
, DEFAULT_OFFSET
, TmfDataRequest
.ALL_DATA
,
358 DEFAULT_CHUNK
, this, experimentTRange
, getEventProcessor(), execType
) {
360 Long fCount
= getSynEventCount();
361 ITransEventProcessor processor
= getProcessor();
362 TmfTimestamp frunningTimeStamp
;
368 * org.eclipse.linuxtools.lttng.request.LttngSyntEventRequest#handleData
371 //// int handleDataCount = 0;
372 //// int handleDataValidCount = 0;
374 // public void handleData() {
375 // LttngSyntheticEvent[] result = getData();
377 // TmfEvent evt = (result.length > 0) ? result[0] : null;
378 //// handleDataCount++;
381 public void handleData(LttngSyntheticEvent event
) {
382 super.handleData(event
);
384 // handleDataValidCount++;
385 LttngSyntheticEvent synEvent
= (LttngSyntheticEvent
) event
;
387 SequenceInd indicator
= synEvent
.getSynType();
388 if (indicator
== SequenceInd
.BEFORE
389 || indicator
== SequenceInd
.AFTER
) {
390 processor
.process(event
, synEvent
.getTraceModel());
391 } else if (indicator
== SequenceInd
.STARTREQ
) {
392 handleRequestStarted();
393 } else if (indicator
== SequenceInd
.ENDREQ
) {
394 processor
.process(event
, synEvent
.getTraceModel());
395 // handleCompleted();
398 if (indicator
== SequenceInd
.BEFORE
) {
400 if (fCount
!= 0 && fCount
% INPUT_CHANGED_REFRESH
== 0) {
401 // send partial update
402 modelInputChanged(this, false);
404 if (TraceDebug
.isDEBUG()) {
405 frunningTimeStamp
= event
.getTimestamp();
406 TraceDebug
.debug("handled: " + fCount
+ " sequence: " + synEvent
.getSynType());
414 public void handleRequestStarted() {
420 // if (TraceDebug.isDEBUG()) {
421 // TraceDebug.debug("AbsTimeUpdateView: Received=" + handleDataCount + ", Valid=" + handleDataCount + ", fCount=" + fCount);
427 public void handleCompleted() {
428 super.handleCompleted();
430 // Data is not complete and should be handled as such
431 if (isFailed() || isCancelled()) {
432 modelIncomplete(this);
435 if (TraceDebug
.isDEBUG()) {
436 if (frunningTimeStamp
!= null) {
437 TraceDebug
.debug("Last event time stamp: "
438 + frunningTimeStamp
.getValue());
444 // obtain singleton core provider
445 LttngSyntheticEventProvider provider
= LttngCoreProviderFactory
448 // send the request to TMF
449 fCurrentRequest
.startRequestInd(provider
);
450 fCurrentRequest
.setclearDataInd(clearingData
);
455 * Returns an initial smaller window to allow the user to select the area of
458 * @param experimentTRange
461 protected TmfTimeRange
getInitTRange(TmfTimeRange experimentTRange
) {
462 TmfTimestamp expStartTime
= experimentTRange
.getStartTime();
463 TmfTimestamp expEndTime
= experimentTRange
.getEndTime();
464 TmfTimestamp initialEndOfWindow
= new LttngTimestamp(expStartTime
466 + INITIAL_WINDOW_OFFSET
);
467 if (initialEndOfWindow
.compareTo(expEndTime
, false) < 0) {
468 return new TmfTimeRange(expStartTime
, initialEndOfWindow
);
471 // The original size of the experiment is smaller than proposed adjusted
473 return experimentTRange
;
477 * Request the Time Analysis widget to enable or disable the wait cursor
478 * e.g. data request in progress or data request completed
482 protected void waitCursor(final boolean waitInd
) {
483 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
484 Display display
= tsfviewer
.getControl().getDisplay();
486 // Perform the updates on the UI thread
487 display
.asyncExec(new Runnable() {
489 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
490 tsfviewer
.waitCursor(waitInd
);
498 * View preparation to override the current local information
501 * - new total time range e.g. Experiment level
502 * @param clearAllData
504 protected void ModelUpdatePrep(TmfTimeRange timeRange
, boolean clearAllData
) {
505 ItemContainer
<?
> itemContainer
= getItemContainer();
507 // start fresh e.g. new experiment selected
508 itemContainer
.clearItems();
510 // clear children but keep processes
511 itemContainer
.clearChildren();
514 // Obtain the current resource array
515 ITmfTimeAnalysisEntry
[] itemArr
= itemContainer
.readItems();
517 // clean up data and boundaries
518 displayModel(itemArr
, -1, -1, false, -1, -1, null);
520 ParamsUpdater updater
= getParamsUpdater();
521 if (updater
!= null) {
523 updater
.setEventsDiscarded(0);
525 // Update new visible time range if available
526 if (timeRange
!= null) {
527 updater
.update(timeRange
.getStartTime().getValue(), timeRange
.getEndTime().getValue());
533 * Initialize the model and view before reloading items
535 * @param boundaryRange
536 * @param visibleRange
539 protected void ModelUpdateInit(TmfTimeRange boundaryRange
, TmfTimeRange visibleRange
, Object source
) {
540 // Update the view boundaries
541 if (boundaryRange
!= null) {
542 ItemContainer
<?
> itemContainer
= getItemContainer();
543 if (itemContainer
!= null) {
544 itemContainer
.clearItems();
545 // Obtain the current process array
546 ITmfTimeAnalysisEntry
[] itemArr
= itemContainer
.readItems();
548 long startTime
= boundaryRange
.getStartTime().getValue();
549 long endTime
= boundaryRange
.getEndTime().getValue();
551 // Update the view part
552 displayModel(itemArr
, startTime
, endTime
, true, visibleRange
.getStartTime().getValue(), visibleRange
553 .getEndTime().getValue(), source
);
557 // update the view filtering parameters
558 if (visibleRange
!= null) {
559 ParamsUpdater updater
= getParamsUpdater();
560 if (updater
!= null) {
562 updater
.setEventsDiscarded(0);
563 // Update new visible time range if available
564 updater
.update(visibleRange
.getStartTime().getValue(), visibleRange
.getEndTime().getValue());
570 * Actions taken by the view to refresh its widget(s) with the updated data
575 * true: yes, false: partial update
577 protected void modelInputChanged(ILttngSyntEventRequest request
, boolean complete
) {
578 long experimentStartTime
= -1;
579 long experimentEndTime
= -1;
580 TmfTimeRange experimentTimeRange
= request
.getExperimentTimeRange();
581 if (experimentTimeRange
!= null) {
582 experimentStartTime
= experimentTimeRange
.getStartTime().getValue();
583 experimentEndTime
= experimentTimeRange
.getEndTime().getValue();
586 // Obtain the current resource list
587 ITmfTimeAnalysisEntry
[] itemArr
= getItemContainer().readItems();
589 if (itemArr
!= null) {
590 // Sort the array by pid
591 Arrays
.sort(itemArr
);
593 // Update the view part
594 displayModel(itemArr
, experimentStartTime
, experimentEndTime
, false, request
.getRange().getStartTime()
595 .getValue(), request
.getRange().getEndTime().getValue(), request
.getSource());
599 // reselect to original time
600 ParamsUpdater paramUpdater
= getParamsUpdater();
601 if ((paramUpdater
!= null) && (tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
602 final Long selTime
= paramUpdater
.getSelectedTime();
603 if (selTime
!= null) {
604 TraceDebug
.debug("View: " + getName() + "\n\t\tRestoring the selected time to: " + selTime
);
605 Display display
= tsfviewer
.getControl().getDisplay();
606 display
.asyncExec(new Runnable() {
608 if ((tsfviewer
!= null) && (!tsfviewer
.getControl().isDisposed())) {
609 tsfviewer
.setSelectedTime(selTime
, false, this);
615 // System.out.println(System.currentTimeMillis() + ": AbsTimeUpdate (" + getName() + ") completed");
617 if (TraceDebug
.isDEBUG()) {
619 Long count
= request
.getSynEventCount();
620 for (int pos
= 0; pos
< itemArr
.length
; pos
++) {
621 eventCount
+= itemArr
[pos
].getTraceEvents().size();
624 int discarded
= paramUpdater
.getEventsDiscarded();
625 int discardedOutofOrder
= paramUpdater
.getEventsDiscardedWrongOrder();
626 int discardedOutofViewRange
= paramUpdater
.getEventsDiscardedOutOfViewRange();
627 int dicardedNotVisible
= paramUpdater
.getEventsDiscardedNotVisible();
629 TmfTimeRange range
= request
.getRange();
630 StringBuilder sb
= new StringBuilder("View: " + getName() + ", Events handled: " + count
631 + ", Events loaded in view: " + eventCount
+ ", Number of events discarded: " + discarded
632 + "\n\tNumber of events discarded with start time earlier than next good time: "
633 + discardedOutofOrder
+ "\n\tDiscarded Not visible: " + dicardedNotVisible
634 + "\n\tDiscarded out of view Range: " + discardedOutofViewRange
);
636 sb
.append("\n\t\tRequested Time Range: " + range
.getStartTime() + "-" + range
.getEndTime());
637 sb
.append("\n\t\tExperiment Time Range: " + experimentStartTime
+ "-" + experimentEndTime
);
638 TraceDebug
.debug(sb
.toString());
646 // * Obtains the remainder fraction on unit Seconds of the entered value in
647 // * nanoseconds. e.g. input: 1241207054171080214 ns The number of seconds
649 // * be obtain by removing the last 9 digits: 1241207054 the fractional
650 // * portion of seconds, expressed in ns is: 171080214
655 // protected String formatNs(long v) {
656 // StringBuffer str = new StringBuffer();
657 // boolean neg = v < 0;
663 // String strVal = String.valueOf(v);
664 // if (v < 1000000000) {
668 // // Extract the last nine digits (e.g. fraction of a S expressed in ns
669 // return strVal.substring(strVal.length() - 9);
673 * The request was stopped, the data is incomplete
677 protected abstract void modelIncomplete(ILttngSyntEventRequest request
);
680 * Returns the Event processor instance related to a specific view
684 protected abstract ITransEventProcessor
getEventProcessor();
687 * To be overridden by some sub-classes although may not be needed in some
688 * e.g. statistics view
691 * @param startBoundTime
692 * @param endBoundTime
693 * @param updateTimeBounds
694 * - Time bounds updated needed e.g. if a new Experiment or trace
696 * @param startVisibleWindow
697 * @param endVisibleWindow
700 protected abstract void displayModel(final ITmfTimeAnalysisEntry
[] items
, final long startBoundTime
,
701 final long endBoundTime
, final boolean updateTimeBounds
, final long startVisibleWindow
,
702 final long endVisibleWindow
, final Object source
);
705 * To be overridden by some sub-classes although may not be needed in some
706 * e.g. statistics view
710 protected abstract ParamsUpdater
getParamsUpdater();
713 * Returns the model's item container
717 protected abstract ItemContainer
<?
> getItemContainer();