Commit | Line | Data |
---|---|---|
378e7718 WB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * William Bourque - Initial API and implementation | |
f05aabed FC |
11 | * |
12 | * Modifications: | |
13 | * 2010-07-16 Yuriy Vashchuk - Heritage correction. | |
378e7718 | 14 | *******************************************************************************/ |
6e512b93 ASL |
15 | package org.eclipse.linuxtools.lttng.ui.views.histogram; |
16 | ||
17 | import org.eclipse.linuxtools.lttng.event.LttngEvent; | |
6e512b93 | 18 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; |
550d787e | 19 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; |
6e512b93 ASL |
20 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; |
21 | ||
378e7718 WB |
22 | /** |
23 | * <b><u>HistogramRequest</u></b> | |
24 | * <p> | |
25 | * Request class, to perform a request to TMF for the histograms. | |
26 | * <p> | |
27 | */ | |
6e512b93 | 28 | public class HistogramRequest extends TmfEventRequest<LttngEvent> { |
f05aabed FC |
29 | /* |
30 | private HistogramContent histogramContent = null; | |
31 | */ | |
6e512b93 | 32 | |
f05aabed FC |
33 | private int lastInterval = 0; |
34 | private long lastRangeTime = 0L; | |
35 | private long nbEventsInInterval = 0L; | |
6e512b93 | 36 | |
f05aabed FC |
37 | private int nbIntervalNotEmpty = 1; |
38 | private int nbEventRead = 0; | |
6e512b93 | 39 | |
f05aabed | 40 | private int lastDrawPosition = 0; |
3fda53ab | 41 | |
f05aabed FC |
42 | private HistogramCanvas parentCanvas = null; |
43 | ||
44 | private boolean isCompleted = false; | |
ecfd1d41 | 45 | |
378e7718 WB |
46 | /** |
47 | * Constructor for HistogramRequest.<p> | |
48 | * Prepare the request in TMF and reset the histogram content. | |
49 | * | |
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) | |
54 | * | |
55 | * @see org.eclipse.linuxtools.tmf.request.TmfEventRequest | |
56 | */ | |
550d787e FC |
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); | |
6e512b93 | 59 | |
f05aabed FC |
60 | setIsCompleted(false); |
61 | ||
ecfd1d41 WB |
62 | // *** FIXME *** |
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); | |
833a21aa | 66 | |
6e512b93 | 67 | parentCanvas = newParentCanvas; |
6e512b93 | 68 | |
378e7718 | 69 | // Reset the content of the HistogramContent... the given data better be valid or this will fail. |
f05aabed FC |
70 | parentCanvas.getHistogramContent().clearContentData(); |
71 | parentCanvas.getHistogramContent().resetTable(range.getStartTime().getValue(), range.getEndTime().getValue(), timeInterval); | |
833a21aa WB |
72 | |
73 | lastRangeTime = range.getStartTime().getValue(); | |
ecfd1d41 | 74 | |
833a21aa WB |
75 | // Notify the UI even before the request started, so we set the timestamp already. |
76 | parentCanvas.notifyParentUpdatedInformationAsynchronously(); | |
6e512b93 | 77 | } |
378e7718 WB |
78 | |
79 | /** | |
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. | |
82 | */ | |
f9673903 FC |
83 | // @Override |
84 | // public void handleData() { | |
85 | // LttngEvent[] result = getData(); | |
86 | // LttngEvent event = (result.length > 0) ? result[0] : null; | |
6e512b93 | 87 | |
f9673903 FC |
88 | @Override |
89 | public void handleData(LttngEvent event) { | |
90 | super.handleData(event); | |
91 | ||
ecfd1d41 WB |
92 | // *** FIXME *** |
93 | // *** EVIL BUG *** | |
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! | |
97 | // | |
cb866e08 | 98 | if (event != null) { |
7ef9ae3f | 99 | |
9b635e61 | 100 | // Tracer.trace("Hst: " + event.getTimestamp()); |
6e512b93 | 101 | |
ecfd1d41 | 102 | // This check is linked to the evil fix mentionned above |
f9673903 FC |
103 | if ( ( event.getTimestamp().getValue() >= parentCanvas.getHistogramContent().getStartTime() ) && |
104 | ( event.getTimestamp().getValue() <= parentCanvas.getHistogramContent().getEndTime() ) ) | |
833a21aa | 105 | { |
378e7718 WB |
106 | |
107 | // Distance (in time) between this event and the last one we read | |
f9673903 | 108 | long distance = ( event.getTimestamp().getValue() - lastRangeTime ); |
6e512b93 | 109 | |
378e7718 | 110 | // Check if we changed of interval (the distance is higher than the interval time) |
f05aabed | 111 | if ( distance > parentCanvas.getHistogramContent().getElementsTimeInterval() ) { |
ecfd1d41 | 112 | |
f05aabed | 113 | parentCanvas.getHistogramContent().getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval; |
f9673903 | 114 | lastRangeTime = event.getTimestamp().getValue(); |
ecfd1d41 | 115 | |
378e7718 WB |
116 | // * NOTE * |
117 | // We can skip several interval at once, so we need to find what was our interval now | |
f05aabed | 118 | lastInterval = (int)((lastRangeTime - parentCanvas.getHistogramContent().getStartTime()) / parentCanvas.getHistogramContent().getElementsTimeInterval() ); |
ecfd1d41 WB |
119 | |
120 | // *** HACK *** | |
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). | |
378e7718 WB |
124 | if ( lastInterval < 0 ) { |
125 | lastInterval = 0; | |
ecfd1d41 | 126 | } |
f05aabed FC |
127 | else if ( lastInterval >= parentCanvas.getHistogramContent().getNbElement() ) { |
128 | lastInterval = (parentCanvas.getHistogramContent().getNbElement()-1); | |
ecfd1d41 WB |
129 | } |
130 | ||
378e7718 | 131 | // * NOTE * |
050df4a5 | 132 | // We save the time we have here. This mean only the FIRST time read in an interval will be saved. |
f05aabed FC |
133 | parentCanvas.getHistogramContent().getElementByIndex(lastInterval).firstIntervalTimestamp = lastRangeTime; |
134 | parentCanvas.getHistogramContent().setReadyUpToPosition(lastInterval); | |
ecfd1d41 | 135 | |
378e7718 | 136 | nbIntervalNotEmpty++; |
544fe9b7 | 137 | nbEventsInInterval = 1L; |
6e512b93 | 138 | } |
378e7718 | 139 | // We are still in the same interval, just keep counting |
ecfd1d41 | 140 | else { |
378e7718 | 141 | nbEventsInInterval++; |
ecfd1d41 WB |
142 | } |
143 | ||
f05aabed FC |
144 | if ( nbEventsInInterval > parentCanvas.getHistogramContent().getHeighestEventCount() ) { |
145 | parentCanvas.getHistogramContent().setHeighestEventCount(nbEventsInInterval); | |
1406f802 | 146 | } |
ecfd1d41 | 147 | nbEventRead++; |
6cf16d22 | 148 | |
378e7718 WB |
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 ) { | |
6cf16d22 WB |
152 | redrawAsyncronously(); |
153 | } | |
ecfd1d41 | 154 | } |
b59134e1 | 155 | } |
7ef9ae3f WB |
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. | |
158 | else { | |
159 | // Save the last events | |
f05aabed | 160 | parentCanvas.getHistogramContent().getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval; |
7ef9ae3f | 161 | // We reached the end of the request, so assume we fill up the content as well |
f05aabed | 162 | parentCanvas.getHistogramContent().setReadyUpToPosition(parentCanvas.getHistogramContent().getNbElement()); |
7ef9ae3f WB |
163 | |
164 | // If the interval wasn't null, count this as a "non empty" interval | |
165 | if (nbEventsInInterval > 0) { | |
166 | nbIntervalNotEmpty++; | |
167 | } | |
168 | } | |
6e512b93 ASL |
169 | } |
170 | ||
378e7718 WB |
171 | /** |
172 | * Function that is called when the request completed (successful or not).<p> | |
173 | * Update information and redraw the screen. | |
174 | */ | |
6e512b93 ASL |
175 | @Override |
176 | public void handleCompleted() { | |
f05aabed | 177 | setIsCompleted(true); |
833a21aa | 178 | parentCanvas.notifyParentUpdatedInformationAsynchronously(); |
6e512b93 | 179 | redrawAsyncronously(); |
9b635e61 FC |
180 | super.handleCompleted(); |
181 | // System.out.println(System.currentTimeMillis() + ": HistogramView (" + ((getExecType() == ExecutionType.LONG) ? "long" : "short") + ") completed"); | |
6e512b93 ASL |
182 | } |
183 | ||
9b635e61 FC |
184 | // /** |
185 | // * Function that is called when the request completed successfully.<p> | |
186 | // */ | |
187 | // @Override | |
188 | // public void handleSuccess() { | |
189 | // // Nothing different from completed. | |
190 | // } | |
6e512b93 | 191 | |
9b635e61 FC |
192 | // /** |
193 | // * Function that is called when the request completed in failure.<p> | |
194 | // */ | |
195 | // @Override | |
196 | // public void handleFailure() { | |
197 | // // Nothing different from cancel. | |
198 | // } | |
6e512b93 | 199 | |
378e7718 WB |
200 | /** |
201 | * Function that is called when the request was cancelled.<p> | |
202 | * Redraw and set the requestCompleted flag to true; | |
203 | */ | |
6e512b93 ASL |
204 | @Override |
205 | public void handleCancel() { | |
6cf16d22 | 206 | redrawAsyncronously(); |
6e512b93 ASL |
207 | } |
208 | ||
378e7718 WB |
209 | /** |
210 | * Update the HistogramContent with the latest information.<p> | |
050df4a5 | 211 | * This will perform some calculation that might be a bit harsh so it should'nt be called too often. |
378e7718 | 212 | */ |
6e512b93 | 213 | public void updateEventsInfo() { |
050df4a5 WB |
214 | // *** Note *** |
215 | // The average number of event is calculated while skipping empty interval if asked | |
216 | int averageNumberOfEvents = 0; | |
217 | if ( HistogramConstant.SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE ) { | |
1406f802 | 218 | averageNumberOfEvents = (int)Math.ceil((double)nbEventRead / (double)nbIntervalNotEmpty); |
050df4a5 WB |
219 | } |
220 | else { | |
f05aabed | 221 | averageNumberOfEvents = (int)Math.ceil((double)nbEventRead / (double)parentCanvas.getHistogramContent().getNbElement()); |
050df4a5 | 222 | } |
1406f802 | 223 | |
f05aabed | 224 | parentCanvas.getHistogramContent().setAverageNumberOfEvents(averageNumberOfEvents); |
3fda53ab WB |
225 | |
226 | // It is possible that the height factor didn't change; | |
227 | // If not, we only need to redraw the updated section, no the whole content | |
228 | // Save the actual height, recalculate the height and check if there was any changes | |
f05aabed FC |
229 | double previousHeightFactor = parentCanvas.getHistogramContent().getHeightFactor(); |
230 | parentCanvas.getHistogramContent().recalculateHeightFactor(); | |
231 | if ( parentCanvas.getHistogramContent().getHeightFactor() != previousHeightFactor ) { | |
232 | parentCanvas.getHistogramContent().recalculateEventHeight(); | |
3fda53ab WB |
233 | } |
234 | else { | |
f05aabed | 235 | parentCanvas.getHistogramContent().recalculateEventHeightInInterval(lastDrawPosition, parentCanvas.getHistogramContent().getReadyUpToPosition()); |
3fda53ab | 236 | } |
050df4a5 | 237 | |
f05aabed | 238 | lastDrawPosition = parentCanvas.getHistogramContent().getReadyUpToPosition(); |
6e512b93 ASL |
239 | } |
240 | ||
378e7718 WB |
241 | /** |
242 | * Perform an asynchonous redraw of the screen. | |
243 | */ | |
6e512b93 ASL |
244 | public void redrawAsyncronously() { |
245 | updateEventsInfo(); | |
378e7718 | 246 | // Canvas redraw is already asynchronous |
6cf16d22 | 247 | parentCanvas.redrawAsynchronously(); |
6e512b93 | 248 | } |
f05aabed FC |
249 | |
250 | /** | |
251 | * Getter for isCompleted variable | |
252 | * @return true if the request is completed | |
253 | */ | |
254 | public boolean getIsCompleted() { | |
255 | return isCompleted; | |
256 | } | |
257 | ||
258 | /** | |
259 | * Setter for isCompleted variable | |
260 | * @param isCompleted value to set the completed flag | |
261 | */ | |
262 | public void setIsCompleted(boolean isCompleted) { | |
263 | this.isCompleted = isCompleted; | |
264 | } | |
6e512b93 ASL |
265 | |
266 | } |