1 /*******************************************************************************
2 * Copyright (c) 2009 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
13 * 2010-07-16 Yuriy Vashchuk - Heritage correction.
14 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.histogram
;
17 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEvent
;
18 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
19 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
;
20 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequest
;
23 * <b><u>HistogramRequest</u></b>
25 * Request class, to perform a request to TMF for the histograms.
28 public class HistogramRequest
extends TmfEventRequest
<LttngEvent
> {
30 private HistogramContent histogramContent = null;
33 private int lastInterval
= 0;
34 private long lastRangeTime
= 0L;
35 private long nbEventsInInterval
= 0L;
37 private int nbIntervalNotEmpty
= 1;
38 private int nbEventRead
= 0;
40 private int lastDrawPosition
= 0;
42 private HistogramCanvas parentCanvas
= null;
44 private boolean isCompleted
= false;
47 * Constructor for HistogramRequest.<p>
48 * Prepare the request in TMF and reset the histogram content.
50 * @param range Range of the request.
51 * @param nbRequested Nb events requested. Can be "Infinity" for all.
52 * @param newParentCanvas HistogramCanvas related to the request.
53 * @param timeInterval Time interval to consider (i.e. : 1 interval is 1 bar in the histogram)
55 * @see org.eclipse.linuxtools.tmf.request.TmfEventRequest
57 public HistogramRequest(TmfTimeRange range
, int nbRequested
, HistogramCanvas newParentCanvas
, long timeInterval
, ITmfDataRequest
.ExecutionType execType
) {
58 super((Class
<LttngEvent
>)LttngEvent
.class, range
, nbRequested
, HistogramConstant
.MAX_EVENTS_PER_READ
, execType
);
60 setIsCompleted(false);
63 // This does not work! The request won't be processed or the number of events returned is wrong!
64 // We cannot use this !
65 //super((Class<LttngEvent>)dataType, range);
67 parentCanvas
= newParentCanvas
;
69 // Reset the content of the HistogramContent... the given data better be valid or this will fail.
70 parentCanvas
.getHistogramContent().clearContentData();
71 parentCanvas
.getHistogramContent().resetTable(range
.getStartTime().getValue(), range
.getEndTime().getValue(), timeInterval
);
73 lastRangeTime
= range
.getStartTime().getValue();
75 // Notify the UI even before the request started, so we set the timestamp already.
76 parentCanvas
.notifyParentUpdatedInformationAsynchronously();
80 * HandleData function : will be called by TMF each time a new event is receive for the request.<p>
81 * Calculation for the content is done here.
84 public void handleData() {
85 LttngEvent
[] result
= getData();
86 LttngEvent event
= (result
.length
> 0) ? result
[0] : null;
90 // The request by timerange only does not work! (see constructor above)
91 // However, the request with number of events will loop until it reach its number or EOF
92 // We have to filter out ourself the extra useless events!
96 LttngEvent tmpEvent
= (LttngEvent
) event
;
98 // Tracer.trace("Hst: " + event.getTimestamp());
100 // This check is linked to the evil fix mentionned above
101 if ( ( tmpEvent
.getTimestamp().getValue() >= parentCanvas
.getHistogramContent().getStartTime() ) &&
102 ( tmpEvent
.getTimestamp().getValue() <= parentCanvas
.getHistogramContent().getEndTime() ) )
105 // Distance (in time) between this event and the last one we read
106 long distance
= ( tmpEvent
.getTimestamp().getValue() - lastRangeTime
);
108 // Check if we changed of interval (the distance is higher than the interval time)
109 if ( distance
> parentCanvas
.getHistogramContent().getElementsTimeInterval() ) {
111 parentCanvas
.getHistogramContent().getElementByIndex(lastInterval
).intervalNbEvents
= nbEventsInInterval
;
112 lastRangeTime
= tmpEvent
.getTimestamp().getValue();
115 // We can skip several interval at once, so we need to find what was our interval now
116 lastInterval
= (int)((lastRangeTime
- parentCanvas
.getHistogramContent().getStartTime()) / parentCanvas
.getHistogramContent().getElementsTimeInterval() );
119 // Because of the threads, weird phenomenons seem to happen here, like a position after the
120 // element range because another request was issued.
121 // This enforce the position but may result in slightly inconsistent result (i.e. a weird misplaced bar sometime).
122 if ( lastInterval
< 0 ) {
125 else if ( lastInterval
>= parentCanvas
.getHistogramContent().getNbElement() ) {
126 lastInterval
= (parentCanvas
.getHistogramContent().getNbElement()-1);
130 // We save the time we have here. This mean only the FIRST time read in an interval will be saved.
131 parentCanvas
.getHistogramContent().getElementByIndex(lastInterval
).firstIntervalTimestamp
= lastRangeTime
;
132 parentCanvas
.getHistogramContent().setReadyUpToPosition(lastInterval
);
134 nbIntervalNotEmpty
++;
135 nbEventsInInterval
= 1L;
137 // We are still in the same interval, just keep counting
139 nbEventsInInterval
++;
142 if ( nbEventsInInterval
> parentCanvas
.getHistogramContent().getHeighestEventCount() ) {
143 parentCanvas
.getHistogramContent().setHeighestEventCount(nbEventsInInterval
);
147 // Call an asynchronous redraw every REDRAW_EVERY_NB_EVENTS events
148 // That way we don't need to wait until to end to have something on the screen
149 if ( nbEventRead
% HistogramConstant
.REDRAW_EVERY_NB_EVENTS
== 0 ) {
150 redrawAsyncronously();
154 // We got a null event! This mean we reach the end of the request.
155 // Save the last interval we had, so we won't miss the very last events at the end.
157 // Save the last events
158 parentCanvas
.getHistogramContent().getElementByIndex(lastInterval
).intervalNbEvents
= nbEventsInInterval
;
159 // We reached the end of the request, so assume we fill up the content as well
160 parentCanvas
.getHistogramContent().setReadyUpToPosition(parentCanvas
.getHistogramContent().getNbElement());
162 // If the interval wasn't null, count this as a "non empty" interval
163 if (nbEventsInInterval
> 0) {
164 nbIntervalNotEmpty
++;
170 * Function that is called when the request completed (successful or not).<p>
171 * Update information and redraw the screen.
174 public void handleCompleted() {
175 setIsCompleted(true);
176 parentCanvas
.notifyParentUpdatedInformationAsynchronously();
177 redrawAsyncronously();
178 super.handleCompleted();
179 // System.out.println(System.currentTimeMillis() + ": HistogramView (" + ((getExecType() == ExecutionType.LONG) ? "long" : "short") + ") completed");
183 // * Function that is called when the request completed successfully.<p>
186 // public void handleSuccess() {
187 // // Nothing different from completed.
191 // * Function that is called when the request completed in failure.<p>
194 // public void handleFailure() {
195 // // Nothing different from cancel.
199 * Function that is called when the request was cancelled.<p>
200 * Redraw and set the requestCompleted flag to true;
203 public void handleCancel() {
204 redrawAsyncronously();
208 * Update the HistogramContent with the latest information.<p>
209 * This will perform some calculation that might be a bit harsh so it should'nt be called too often.
211 public void updateEventsInfo() {
213 // The average number of event is calculated while skipping empty interval if asked
214 int averageNumberOfEvents
= 0;
215 if ( HistogramConstant
.SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE
) {
216 averageNumberOfEvents
= (int)Math
.ceil((double)nbEventRead
/ (double)nbIntervalNotEmpty
);
219 averageNumberOfEvents
= (int)Math
.ceil((double)nbEventRead
/ (double)parentCanvas
.getHistogramContent().getNbElement());
222 parentCanvas
.getHistogramContent().setAverageNumberOfEvents(averageNumberOfEvents
);
224 // It is possible that the height factor didn't change;
225 // If not, we only need to redraw the updated section, no the whole content
226 // Save the actual height, recalculate the height and check if there was any changes
227 double previousHeightFactor
= parentCanvas
.getHistogramContent().getHeightFactor();
228 parentCanvas
.getHistogramContent().recalculateHeightFactor();
229 if ( parentCanvas
.getHistogramContent().getHeightFactor() != previousHeightFactor
) {
230 parentCanvas
.getHistogramContent().recalculateEventHeight();
233 parentCanvas
.getHistogramContent().recalculateEventHeightInInterval(lastDrawPosition
, parentCanvas
.getHistogramContent().getReadyUpToPosition());
236 lastDrawPosition
= parentCanvas
.getHistogramContent().getReadyUpToPosition();
240 * Perform an asynchonous redraw of the screen.
242 public void redrawAsyncronously() {
244 // Canvas redraw is already asynchronous
245 parentCanvas
.redrawAsynchronously();
249 * Getter for isCompleted variable
250 * @return true if the request is completed
252 public boolean getIsCompleted() {
257 * Setter for isCompleted variable
258 * @param isCompleted value to set the completed flag
260 public void setIsCompleted(boolean isCompleted
) {
261 this.isCompleted
= isCompleted
;