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
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.histogram
;
14 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEvent
;
15 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
16 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
17 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequest
;
20 * <b><u>HistogramRequest</u></b>
22 * Request class, to perform a request to TMF for the histograms.
25 public class HistogramRequest
extends TmfEventRequest
<LttngEvent
> {
26 protected HistogramContent histogramContent
= null;
28 protected int lastInterval
= 0;
29 protected long lastRangeTime
= 0L;
30 protected long nbEventsInInterval
= 0L;
32 protected int nbIntervalNotEmpty
= 1;
33 protected int nbEventRead
= 0;
35 protected int lastDrawPosition
= 0;
37 protected HistogramCanvas parentCanvas
= null;
40 * Constructor for HistogramRequest.<p>
41 * Prepare the request in TMF and reset the histogram content.
43 * @param range Range of the request.
44 * @param nbRequested Nb events requested. Can be "Infinity" for all.
45 * @param newParentCanvas HistogramCanvas related to the request.
46 * @param timeInterval Time interval to consider (i.e. : 1 interval is 1 bar in the histogram)
48 * @see org.eclipse.linuxtools.tmf.request.TmfEventRequest
50 public HistogramRequest(TmfTimeRange range
, int nbRequested
, HistogramCanvas newParentCanvas
, long timeInterval
) {
51 super((Class
<LttngEvent
>)LttngEvent
.class, range
, nbRequested
, HistogramConstant
.MAX_EVENTS_PER_READ
);
54 // This does not work! The request won't be processed or the number of events returned is wrong!
55 // We cannot use this !
56 //super((Class<LttngEvent>)dataType, range);
58 parentCanvas
= newParentCanvas
;
59 histogramContent
= parentCanvas
.getHistogramContent();
61 // Reset the content of the HistogramContent... the given data better be valid or this will fail.
62 histogramContent
.clearContentData();
63 histogramContent
.resetTable(range
.getStartTime().getValue(), range
.getEndTime().getValue(), timeInterval
);
65 lastRangeTime
= range
.getStartTime().getValue();
67 // Notify the UI even before the request started, so we set the timestamp already.
68 parentCanvas
.notifyParentUpdatedInformationAsynchronously();
72 * HandleData function : will be called by TMF each time a new event is receive for the request.<p>
73 * Calculation for the content is done here.
76 public void handleData() {
77 TmfEvent
[] result
= getData();
78 TmfEvent
[] evt
= new TmfEvent
[1];
80 evt
[0] = (result
.length
> 0) ? result
[0] : null;
84 // The request by timerange only does not work! (see constructor above)
85 // However, the request with number of events will loop until it reach its number or EOF
86 // We have to filter out ourself the extra useless events!
90 LttngEvent tmpEvent
= (LttngEvent
)evt
[0];
92 // This check is linked to the evil fix mentionned above
93 if ( ( tmpEvent
.getTimestamp().getValue() >= histogramContent
.getStartTime() ) &&
94 ( tmpEvent
.getTimestamp().getValue() <= histogramContent
.getEndTime() ) )
97 // Distance (in time) between this event and the last one we read
98 long distance
= ( tmpEvent
.getTimestamp().getValue() - lastRangeTime
);
100 // Check if we changed of interval (the distance is higher than the interval time)
101 if ( distance
> histogramContent
.getElementsTimeInterval() ) {
103 histogramContent
.getElementByIndex(lastInterval
).intervalNbEvents
= nbEventsInInterval
;
104 lastRangeTime
= tmpEvent
.getTimestamp().getValue();
107 // We can skip several interval at once, so we need to find what was our interval now
108 lastInterval
= (int)((lastRangeTime
- histogramContent
.getStartTime()) / histogramContent
.getElementsTimeInterval() );
111 // Because of the threads, weird phenomenons seem to happen here, like a position after the
112 // element range because another request was issued.
113 // This enforce the position but may result in slightly inconsistent result (i.e. a weird misplaced bar sometime).
114 if ( lastInterval
< 0 ) {
117 else if ( lastInterval
>= histogramContent
.getNbElement() ) {
118 lastInterval
= (histogramContent
.getNbElement()-1);
122 // We save the time we have here. This mean only the FIRST time read in an interval will be saved.
123 histogramContent
.getElementByIndex(lastInterval
).firstIntervalTimestamp
= lastRangeTime
;
124 histogramContent
.setReadyUpToPosition(lastInterval
);
126 nbIntervalNotEmpty
++;
127 nbEventsInInterval
= 1L;
129 // We are still in the same interval, just keep counting
131 nbEventsInInterval
++;
134 if ( nbEventsInInterval
> histogramContent
.getHeighestEventCount() ) {
135 histogramContent
.setHeighestEventCount(nbEventsInInterval
);
139 // Call an asynchronous redraw every REDRAW_EVERY_NB_EVENTS events
140 // That way we don't need to wait until to end to have something on the screen
141 if ( nbEventRead
% HistogramConstant
.REDRAW_EVERY_NB_EVENTS
== 0 ) {
142 redrawAsyncronously();
146 // We got a null event! This mean we reach the end of the request.
147 // Save the last interval we had, so we won't miss the very last events at the end.
149 // Save the last events
150 histogramContent
.getElementByIndex(lastInterval
).intervalNbEvents
= nbEventsInInterval
;
151 // We reached the end of the request, so assume we fill up the content as well
152 histogramContent
.setReadyUpToPosition(histogramContent
.getNbElement());
154 // If the interval wasn't null, count this as a "non empty" interval
155 if (nbEventsInInterval
> 0) {
156 nbIntervalNotEmpty
++;
162 * Function that is called when the request completed (successful or not).<p>
163 * Update information and redraw the screen.
166 public void handleCompleted() {
167 parentCanvas
.notifyParentUpdatedInformationAsynchronously();
168 redrawAsyncronously();
172 * Function that is called when the request completed successfully.<p>
175 public void handleSuccess() {
176 // Nothing different from completed.
180 * Function that is called when the request completed in failure.<p>
183 public void handleFailure() {
184 // Nothing different from cancel.
188 * Function that is called when the request was cancelled.<p>
189 * Redraw and set the requestCompleted flag to true;
192 public void handleCancel() {
193 redrawAsyncronously();
197 * Update the HistogramContent with the latest information.<p>
198 * This will perform some calculation that might be a bit harsh so it should'nt be called too often.
200 public void updateEventsInfo() {
202 // The average number of event is calculated while skipping empty interval if asked
203 int averageNumberOfEvents
= 0;
204 if ( HistogramConstant
.SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE
) {
205 averageNumberOfEvents
= (int)Math
.ceil((double)nbEventRead
/ (double)nbIntervalNotEmpty
);
208 averageNumberOfEvents
= (int)Math
.ceil((double)nbEventRead
/ (double)histogramContent
.getNbElement());
211 histogramContent
.setAverageNumberOfEvents(averageNumberOfEvents
);
213 // It is possible that the height factor didn't change;
214 // If not, we only need to redraw the updated section, no the whole content
215 // Save the actual height, recalculate the height and check if there was any changes
216 double previousHeightFactor
= histogramContent
.getHeightFactor();
217 histogramContent
.recalculateHeightFactor();
218 if ( histogramContent
.getHeightFactor() != previousHeightFactor
) {
219 histogramContent
.recalculateEventHeight();
222 histogramContent
.recalculateEventHeightInInterval(lastDrawPosition
, histogramContent
.getReadyUpToPosition());
225 lastDrawPosition
= histogramContent
.getReadyUpToPosition();
229 * Perform an asynchonous redraw of the screen.
231 public void redrawAsyncronously() {
233 // Canvas redraw is already asynchronous
234 parentCanvas
.redrawAsynchronously();