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 | |
11 | *******************************************************************************/ | |
6e512b93 ASL |
12 | package org.eclipse.linuxtools.lttng.ui.views.histogram; |
13 | ||
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; | |
18 | ||
378e7718 WB |
19 | /** |
20 | * <b><u>HistogramRequest</u></b> | |
21 | * <p> | |
22 | * Request class, to perform a request to TMF for the histograms. | |
23 | * <p> | |
24 | */ | |
6e512b93 | 25 | public class HistogramRequest extends TmfEventRequest<LttngEvent> { |
b59134e1 | 26 | private HistogramContent histogramContent = null; |
6e512b93 | 27 | |
378e7718 | 28 | private int lastInterval = 0; |
b59134e1 | 29 | private long lastRangeTime = 0L; |
378e7718 | 30 | private long nbEventsInInterval = 1; |
6e512b93 | 31 | |
378e7718 | 32 | private int nbIntervalNotEmpty = 1; |
b59134e1 | 33 | private int nbEventRead = 0; |
6e512b93 | 34 | |
378e7718 | 35 | private HistogramCanvas parentCanvas = null; |
b59134e1 | 36 | |
ecfd1d41 WB |
37 | private boolean requestCompleted = false; |
38 | ||
378e7718 WB |
39 | /** |
40 | * Constructor for HistogramRequest.<p> | |
41 | * Prepare the request in TMF and reset the histogram content. | |
42 | * | |
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) | |
47 | * | |
48 | * @see org.eclipse.linuxtools.tmf.request.TmfEventRequest | |
49 | */ | |
50 | public HistogramRequest(TmfTimeRange range, int nbRequested, HistogramCanvas newParentCanvas, Long timeInterval) { | |
51 | super((Class<LttngEvent>)LttngEvent.class, range, nbRequested, HistogramConstant.MAX_EVENTS_PER_READ); | |
6e512b93 | 52 | |
ecfd1d41 WB |
53 | // *** FIXME *** |
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); | |
833a21aa | 57 | |
6e512b93 | 58 | parentCanvas = newParentCanvas; |
378e7718 | 59 | histogramContent = parentCanvas.getHistogramContent(); |
6e512b93 | 60 | |
378e7718 | 61 | // Reset the content of the HistogramContent... the given data better be valid or this will fail. |
833a21aa WB |
62 | histogramContent.clearContentData(); |
63 | histogramContent.resetTable(range.getStartTime().getValue(), range.getEndTime().getValue(), timeInterval); | |
64 | ||
65 | lastRangeTime = range.getStartTime().getValue(); | |
ecfd1d41 | 66 | |
833a21aa WB |
67 | // Notify the UI even before the request started, so we set the timestamp already. |
68 | parentCanvas.notifyParentUpdatedInformationAsynchronously(); | |
6e512b93 | 69 | } |
378e7718 WB |
70 | |
71 | /** | |
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. | |
74 | */ | |
6e512b93 ASL |
75 | @Override |
76 | public void handleData() { | |
77 | TmfEvent[] result = getData(); | |
78 | TmfEvent[] evt = new TmfEvent[1]; | |
79 | ||
80 | evt[0] = (result.length > 0) ? result[0] : null; | |
81 | ||
ecfd1d41 WB |
82 | // *** FIXME *** |
83 | // *** EVIL BUG *** | |
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! | |
87 | // | |
88 | if ( (evt[0] != null) && (requestCompleted == false) ) { | |
6e512b93 ASL |
89 | LttngEvent tmpEvent = (LttngEvent)evt[0]; |
90 | ||
ecfd1d41 | 91 | // This check is linked to the evil fix mentionned above |
833a21aa WB |
92 | if ( ( tmpEvent.getTimestamp().getValue() >= histogramContent.getStartTime() ) && |
93 | ( tmpEvent.getTimestamp().getValue() <= histogramContent.getEndTime() ) ) | |
94 | { | |
378e7718 WB |
95 | |
96 | // Distance (in time) between this event and the last one we read | |
ecfd1d41 | 97 | long distance = ( tmpEvent.getTimestamp().getValue() - lastRangeTime ); |
6e512b93 | 98 | |
378e7718 | 99 | // Check if we changed of interval (the distance is higher than the interval time) |
ecfd1d41 WB |
100 | if ( distance > histogramContent.getIntervalTime() ) { |
101 | ||
378e7718 | 102 | histogramContent.getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval; |
ecfd1d41 WB |
103 | lastRangeTime = tmpEvent.getTimestamp().getValue(); |
104 | ||
378e7718 WB |
105 | // * NOTE * |
106 | // We can skip several interval at once, so we need to find what was our interval now | |
107 | lastInterval = (int)((lastRangeTime - histogramContent.getStartTime()) / histogramContent.getIntervalTime() ); | |
ecfd1d41 WB |
108 | |
109 | // *** HACK *** | |
110 | // Because of the threads, weird phenomenons seem to happen here, like a position after the | |
111 | // element range because another request was issued. | |
112 | // This enforce the position but may result in slightly inconsistent result (i.e. a weird misplaced bar sometime). | |
378e7718 WB |
113 | if ( lastInterval < 0 ) { |
114 | lastInterval = 0; | |
ecfd1d41 | 115 | } |
378e7718 WB |
116 | else if ( lastInterval >= histogramContent.getNbElement() ) { |
117 | lastInterval = (histogramContent.getNbElement()-1); | |
ecfd1d41 WB |
118 | } |
119 | ||
378e7718 | 120 | // * NOTE * |
050df4a5 | 121 | // We save the time we have here. This mean only the FIRST time read in an interval will be saved. |
378e7718 WB |
122 | histogramContent.getElementByIndex(lastInterval).firstIntervalTimestamp = lastRangeTime; |
123 | histogramContent.setReadyUpToPosition(lastInterval); | |
ecfd1d41 | 124 | |
378e7718 WB |
125 | nbIntervalNotEmpty++; |
126 | nbEventsInInterval = 1; | |
6e512b93 | 127 | } |
378e7718 | 128 | // We are still in the same interval, just keep counting |
ecfd1d41 | 129 | else { |
378e7718 WB |
130 | nbEventsInInterval++; |
131 | if ( nbEventsInInterval > histogramContent.getHeighestEventCount() ) { | |
132 | histogramContent.setHeighestEventCount(nbEventsInInterval); | |
ecfd1d41 WB |
133 | } |
134 | } | |
135 | ||
136 | nbEventRead++; | |
6cf16d22 | 137 | |
378e7718 WB |
138 | // Call an asynchronous redraw every REDRAW_EVERY_NB_EVENTS events |
139 | // That way we don't need to wait until to end to have something on the screen | |
140 | if ( nbEventRead % HistogramConstant.REDRAW_EVERY_NB_EVENTS == 0 ) { | |
6cf16d22 WB |
141 | redrawAsyncronously(); |
142 | } | |
ecfd1d41 WB |
143 | } |
144 | else { | |
833a21aa WB |
145 | //System.out.println("Requested Timerange is : " + histogramContent.getStartTime() + " / " + histogramContent.getEndTime()); |
146 | //System.out.println("Time is : " + tmpEvent.getTimestamp().getValue()); | |
ecfd1d41 WB |
147 | // *** FIXME *** |
148 | // *** EVIL FIX *** | |
149 | // Because of the other evil bug (see above), we have to ignore extra useless events we will get | |
150 | // However, we might be far away from the end so we better start a redraw now | |
833a21aa WB |
151 | if (tmpEvent.getTimestamp().getValue() >= histogramContent.getEndTime()) { |
152 | redrawAsyncronously(); | |
153 | requestCompleted = true; | |
154 | } | |
ecfd1d41 | 155 | } |
b59134e1 | 156 | } |
6e512b93 ASL |
157 | } |
158 | ||
378e7718 WB |
159 | /** |
160 | * Function that is called when the request completed (successful or not).<p> | |
161 | * Update information and redraw the screen. | |
162 | */ | |
6e512b93 ASL |
163 | @Override |
164 | public void handleCompleted() { | |
833a21aa | 165 | parentCanvas.notifyParentUpdatedInformationAsynchronously(); |
6e512b93 ASL |
166 | redrawAsyncronously(); |
167 | } | |
168 | ||
378e7718 WB |
169 | /** |
170 | * Function that is called when the request completed successfully.<p> | |
171 | */ | |
6e512b93 ASL |
172 | @Override |
173 | public void handleSuccess() { | |
6cf16d22 | 174 | // Nothing different from completed. |
6e512b93 ASL |
175 | } |
176 | ||
378e7718 WB |
177 | /** |
178 | * Function that is called when the request completed in failure.<p> | |
179 | */ | |
6e512b93 ASL |
180 | @Override |
181 | public void handleFailure() { | |
6cf16d22 | 182 | // Nothing different from cancel. |
6e512b93 ASL |
183 | } |
184 | ||
378e7718 WB |
185 | /** |
186 | * Function that is called when the request was cancelled.<p> | |
187 | * Redraw and set the requestCompleted flag to true; | |
188 | */ | |
6e512b93 ASL |
189 | @Override |
190 | public void handleCancel() { | |
6cf16d22 WB |
191 | redrawAsyncronously(); |
192 | requestCompleted = true; | |
6e512b93 ASL |
193 | } |
194 | ||
378e7718 WB |
195 | /** |
196 | * Update the HistogramContent with the latest information.<p> | |
050df4a5 | 197 | * This will perform some calculation that might be a bit harsh so it should'nt be called too often. |
378e7718 | 198 | */ |
6e512b93 | 199 | public void updateEventsInfo() { |
050df4a5 WB |
200 | // *** Note *** |
201 | // The average number of event is calculated while skipping empty interval if asked | |
202 | int averageNumberOfEvents = 0; | |
203 | if ( HistogramConstant.SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE ) { | |
204 | averageNumberOfEvents = nbEventRead / nbIntervalNotEmpty; | |
205 | } | |
206 | else { | |
207 | averageNumberOfEvents = nbEventRead / histogramContent.getNbElement(); | |
208 | } | |
209 | ||
b59134e1 WB |
210 | histogramContent.setAverageNumberOfEvents(averageNumberOfEvents); |
211 | histogramContent.recalculateEventHeight(); | |
6e512b93 ASL |
212 | } |
213 | ||
378e7718 WB |
214 | /** |
215 | * Perform an asynchonous redraw of the screen. | |
216 | */ | |
6e512b93 ASL |
217 | public void redrawAsyncronously() { |
218 | updateEventsInfo(); | |
378e7718 | 219 | // Canvas redraw is already asynchronous |
6cf16d22 | 220 | parentCanvas.redrawAsynchronously(); |
6e512b93 ASL |
221 | } |
222 | ||
223 | } |