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;
89 public void handleData(LttngEvent event
) {
90 super.handleData(event
);
94 // The request by timerange only does not work! (see constructor above)
95 // However, the request with number of events will loop until it reach its number or EOF
96 // We have to filter out ourself the extra useless events!
100 // Tracer.trace("Hst: " + event.getTimestamp());
102 // This check is linked to the evil fix mentionned above
103 if ( ( event
.getTimestamp().getValue() >= parentCanvas
.getHistogramContent().getStartTime() ) &&
104 ( event
.getTimestamp().getValue() <= parentCanvas
.getHistogramContent().getEndTime() ) )
107 // Distance (in time) between this event and the last one we read
108 long distance
= ( event
.getTimestamp().getValue() - lastRangeTime
);
110 // Check if we changed of interval (the distance is higher than the interval time)
111 if ( distance
> parentCanvas
.getHistogramContent().getElementsTimeInterval() ) {
113 parentCanvas
.getHistogramContent().getElementByIndex(lastInterval
).intervalNbEvents
= nbEventsInInterval
;
114 lastRangeTime
= event
.getTimestamp().getValue();
117 // We can skip several interval at once, so we need to find what was our interval now
118 lastInterval
= (int)((lastRangeTime
- parentCanvas
.getHistogramContent().getStartTime()) / parentCanvas
.getHistogramContent().getElementsTimeInterval() );
121 // Because of the threads, weird phenomenons seem to happen here, like a position after the
122 // element range because another request was issued.
123 // This enforce the position but may result in slightly inconsistent result (i.e. a weird misplaced bar sometime).
124 if ( lastInterval
< 0 ) {
127 else if ( lastInterval
>= parentCanvas
.getHistogramContent().getNbElement() ) {
128 lastInterval
= (parentCanvas
.getHistogramContent().getNbElement()-1);
132 // We save the time we have here. This mean only the FIRST time read in an interval will be saved.
133 parentCanvas
.getHistogramContent().getElementByIndex(lastInterval
).firstIntervalTimestamp
= lastRangeTime
;
134 parentCanvas
.getHistogramContent().setReadyUpToPosition(lastInterval
);
136 nbIntervalNotEmpty
++;
137 nbEventsInInterval
= 1L;
139 // We are still in the same interval, just keep counting
141 nbEventsInInterval
++;
144 if ( nbEventsInInterval
> parentCanvas
.getHistogramContent().getHeighestEventCount() ) {
145 parentCanvas
.getHistogramContent().setHeighestEventCount(nbEventsInInterval
);
149 // Call an asynchronous redraw every REDRAW_EVERY_NB_EVENTS events
150 // That way we don't need to wait until to end to have something on the screen
151 if ( nbEventRead
% HistogramConstant
.REDRAW_EVERY_NB_EVENTS
== 0 ) {
152 redrawAsyncronously();
156 // We got a null event! This mean we reach the end of the request.
157 // Save the last interval we had, so we won't miss the very last events at the end.
159 // Save the last events
160 parentCanvas
.getHistogramContent().getElementByIndex(lastInterval
).intervalNbEvents
= nbEventsInInterval
;
161 // We reached the end of the request, so assume we fill up the content as well
162 parentCanvas
.getHistogramContent().setReadyUpToPosition(parentCanvas
.getHistogramContent().getNbElement());
164 // If the interval wasn't null, count this as a "non empty" interval
165 if (nbEventsInInterval
> 0) {
166 nbIntervalNotEmpty
++;
172 * Function that is called when the request completed (successful or not).<p>
173 * Update information and redraw the screen.
176 public void handleCompleted() {
177 setIsCompleted(true);
178 parentCanvas
.notifyParentUpdatedInformationAsynchronously();
179 redrawAsyncronously();
180 super.handleCompleted();
181 // System.out.println(System.currentTimeMillis() + ": HistogramView (" + ((getExecType() == ExecutionType.LONG) ? "long" : "short") + ") completed");
185 // * Function that is called when the request completed successfully.<p>
188 // public void handleSuccess() {
189 // // Nothing different from completed.
193 // * Function that is called when the request completed in failure.<p>
196 // public void handleFailure() {
197 // // Nothing different from cancel.
201 * Function that is called when the request was cancelled.<p>
202 * Redraw and set the requestCompleted flag to true;
205 public void handleCancel() {
206 redrawAsyncronously();
207 super.handleCancel();
211 * Update the HistogramContent with the latest information.<p>
212 * This will perform some calculation that might be a bit harsh so it should'nt be called too often.
214 public void updateEventsInfo() {
216 // The average number of event is calculated while skipping empty interval if asked
217 int averageNumberOfEvents
= 0;
218 if ( HistogramConstant
.SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE
) {
219 averageNumberOfEvents
= (int)Math
.ceil((double)nbEventRead
/ (double)nbIntervalNotEmpty
);
222 averageNumberOfEvents
= (int)Math
.ceil((double)nbEventRead
/ (double)parentCanvas
.getHistogramContent().getNbElement());
225 parentCanvas
.getHistogramContent().setAverageNumberOfEvents(averageNumberOfEvents
);
227 // It is possible that the height factor didn't change;
228 // If not, we only need to redraw the updated section, no the whole content
229 // Save the actual height, recalculate the height and check if there was any changes
230 double previousHeightFactor
= parentCanvas
.getHistogramContent().getHeightFactor();
231 parentCanvas
.getHistogramContent().recalculateHeightFactor();
232 if ( parentCanvas
.getHistogramContent().getHeightFactor() != previousHeightFactor
) {
233 parentCanvas
.getHistogramContent().recalculateEventHeight();
236 parentCanvas
.getHistogramContent().recalculateEventHeightInInterval(lastDrawPosition
, parentCanvas
.getHistogramContent().getReadyUpToPosition());
239 lastDrawPosition
= parentCanvas
.getHistogramContent().getReadyUpToPosition();
243 * Perform an asynchonous redraw of the screen.
245 public void redrawAsyncronously() {
247 // Canvas redraw is already asynchronous
248 parentCanvas
.redrawAsynchronously();
252 * Getter for isCompleted variable
253 * @return true if the request is completed
255 public boolean getIsCompleted() {
260 * Setter for isCompleted variable
261 * @param isCompleted value to set the completed flag
263 public void setIsCompleted(boolean isCompleted
) {
264 this.isCompleted
= isCompleted
;