c21816d19f85833c630614981fe3ffbef81e5461
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / HistogramRequest.java
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 *
12 * Modifications:
13 * 2010-07-16 Yuriy Vashchuk - Heritage correction.
14 *******************************************************************************/
15 package org.eclipse.linuxtools.lttng.ui.views.histogram;
16
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;
21
22 /**
23 * <b><u>HistogramRequest</u></b>
24 * <p>
25 * Request class, to perform a request to TMF for the histograms.
26 * <p>
27 */
28 public class HistogramRequest extends TmfEventRequest<LttngEvent> {
29 /*
30 private HistogramContent histogramContent = null;
31 */
32
33 private int lastInterval = 0;
34 private long lastRangeTime = 0L;
35 private long nbEventsInInterval = 0L;
36
37 private int nbIntervalNotEmpty = 1;
38 private int nbEventRead = 0;
39
40 private int lastDrawPosition = 0;
41
42 private HistogramCanvas parentCanvas = null;
43
44 private boolean isCompleted = false;
45
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 */
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);
59
60 setIsCompleted(false);
61
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);
66
67 parentCanvas = newParentCanvas;
68
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);
72
73 lastRangeTime = range.getStartTime().getValue();
74
75 // Notify the UI even before the request started, so we set the timestamp already.
76 parentCanvas.notifyParentUpdatedInformationAsynchronously();
77 }
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 */
83 // @Override
84 // public void handleData() {
85 // LttngEvent[] result = getData();
86 // LttngEvent event = (result.length > 0) ? result[0] : null;
87
88 @Override
89 public void handleData(LttngEvent event) {
90 super.handleData(event);
91
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 //
98 if (event != null) {
99
100 // Tracer.trace("Hst: " + event.getTimestamp());
101
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() ) )
105 {
106
107 // Distance (in time) between this event and the last one we read
108 long distance = ( event.getTimestamp().getValue() - lastRangeTime );
109
110 // Check if we changed of interval (the distance is higher than the interval time)
111 if ( distance > parentCanvas.getHistogramContent().getElementsTimeInterval() ) {
112
113 parentCanvas.getHistogramContent().getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval;
114 lastRangeTime = event.getTimestamp().getValue();
115
116 // * NOTE *
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() );
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).
124 if ( lastInterval < 0 ) {
125 lastInterval = 0;
126 }
127 else if ( lastInterval >= parentCanvas.getHistogramContent().getNbElement() ) {
128 lastInterval = (parentCanvas.getHistogramContent().getNbElement()-1);
129 }
130
131 // * NOTE *
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);
135
136 nbIntervalNotEmpty++;
137 nbEventsInInterval = 1L;
138 }
139 // We are still in the same interval, just keep counting
140 else {
141 nbEventsInInterval++;
142 }
143
144 if ( nbEventsInInterval > parentCanvas.getHistogramContent().getHeighestEventCount() ) {
145 parentCanvas.getHistogramContent().setHeighestEventCount(nbEventsInInterval);
146 }
147 nbEventRead++;
148
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();
153 }
154 }
155 }
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
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());
163
164 // If the interval wasn't null, count this as a "non empty" interval
165 if (nbEventsInInterval > 0) {
166 nbIntervalNotEmpty++;
167 }
168 }
169 }
170
171 /**
172 * Function that is called when the request completed (successful or not).<p>
173 * Update information and redraw the screen.
174 */
175 @Override
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");
182 }
183
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 // }
191
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 // }
199
200 /**
201 * Function that is called when the request was cancelled.<p>
202 * Redraw and set the requestCompleted flag to true;
203 */
204 @Override
205 public void handleCancel() {
206 redrawAsyncronously();
207 }
208
209 /**
210 * Update the HistogramContent with the latest information.<p>
211 * This will perform some calculation that might be a bit harsh so it should'nt be called too often.
212 */
213 public void updateEventsInfo() {
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 ) {
218 averageNumberOfEvents = (int)Math.ceil((double)nbEventRead / (double)nbIntervalNotEmpty);
219 }
220 else {
221 averageNumberOfEvents = (int)Math.ceil((double)nbEventRead / (double)parentCanvas.getHistogramContent().getNbElement());
222 }
223
224 parentCanvas.getHistogramContent().setAverageNumberOfEvents(averageNumberOfEvents);
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
229 double previousHeightFactor = parentCanvas.getHistogramContent().getHeightFactor();
230 parentCanvas.getHistogramContent().recalculateHeightFactor();
231 if ( parentCanvas.getHistogramContent().getHeightFactor() != previousHeightFactor ) {
232 parentCanvas.getHistogramContent().recalculateEventHeight();
233 }
234 else {
235 parentCanvas.getHistogramContent().recalculateEventHeightInInterval(lastDrawPosition, parentCanvas.getHistogramContent().getReadyUpToPosition());
236 }
237
238 lastDrawPosition = parentCanvas.getHistogramContent().getReadyUpToPosition();
239 }
240
241 /**
242 * Perform an asynchonous redraw of the screen.
243 */
244 public void redrawAsyncronously() {
245 updateEventsInfo();
246 // Canvas redraw is already asynchronous
247 parentCanvas.redrawAsynchronously();
248 }
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 }
265
266 }
This page took 0.05631 seconds and 4 git commands to generate.