tmf/lttng: Remove unneeded (non-Javadoc) comments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramDataModel.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Implementation of new interfaces/listeners and support for
12 * time stamp in any order
13 * Francois Chouinard - Moved from LTTng to TMF
14 * Francois Chouinard - Added support for empty initial buckets
15 *******************************************************************************/
16
17 package org.eclipse.linuxtools.tmf.ui.views.histogram;
18
19 import java.util.Arrays;
20
21 import org.eclipse.core.runtime.ListenerList;
22
23 /**
24 * Histogram-independent data model.
25 *
26 * It has the following characteristics:
27 * <ul>
28 * <li>The <i>basetime</i> is the timestamp of the first event
29 * <li>There is a fixed number (<i>n</i>) of buckets of uniform duration
30 * (<i>d</i>)
31 * <li>The <i>timespan</i> of the model is thus: <i>n</i> * <i>d</i> time units
32 * <li>Bucket <i>i</i> holds the number of events that occurred in time range:
33 * [<i>basetime</i> + <i>i</i> * <i>d</i>, <i>basetime</i> + (<i>i</i> + 1) *
34 * <i>d</i>)
35 * </ul>
36 * Initially, the bucket durations is set to 1ns. As the events are read, they
37 * are tallied (using <i>countEvent()</i>) in the appropriate bucket (relative
38 * to the <i>basetime</i>).
39 * <p>
40 * Eventually, an event will have a timestamp that exceeds the <i>timespan</i>
41 * high end (determined by <i>n</i>, the number of buckets, and <i>d</i>, the
42 * bucket duration). At this point, the histogram needs to be compacted. This is
43 * done by simply merging adjacent buckets by pair, in effect doubling the
44 * <i>timespan</i> (<i>timespan'</i> = <i>n</i> * <i>d'</i>, where <i>d'</i> =
45 * 2<i>d</i>). This compaction happens as needed as the trace is read.
46 * <p>
47 * The model allows for timestamps in not increasing order. The timestamps can
48 * be fed to the model in any order. If an event has a timestamp less than the
49 * <i>basetime</i>, the buckets will be moved to the right to account for the
50 * new smaller timestamp. The new <i>basetime</i> is a multiple of the bucket
51 * duration smaller then the previous <i>basetime</i>. Note that the <i>basetime</i>
52 * might not be anymore a timestamp of an event. If necessary, the buckets will
53 * be compacted before moving to the right. This might be necessary to not
54 * loose any event counts at the end of the buckets array.
55 * <p>
56 * The mapping from the model to the UI is performed by the <i>scaleTo()</i>
57 * method. By keeping the number of buckets <i>n</i> relatively large with
58 * respect to to the number of pixels in the actual histogram, we should achieve
59 * a nice result when visualizing the histogram.
60 * <p>
61 *
62 * @version 2.0
63 * @author Francois Chouinard
64 */
65 public class HistogramDataModel implements IHistogramDataModel {
66
67 // ------------------------------------------------------------------------
68 // Constants
69 // ------------------------------------------------------------------------
70
71 /**
72 * The default number of buckets
73 */
74 public static final int DEFAULT_NUMBER_OF_BUCKETS = 16 * 1000;
75
76 /**
77 * Number of events after which listeners will be notified.
78 */
79 public static final int REFRESH_FREQUENCY = DEFAULT_NUMBER_OF_BUCKETS;
80
81 // ------------------------------------------------------------------------
82 // Attributes
83 // ------------------------------------------------------------------------
84
85 // Bucket management
86 private final int fNbBuckets;
87 private final long[] fBuckets;
88 private long fBucketDuration;
89 private long fNbEvents;
90 private int fLastBucket;
91
92 // Timestamps
93 private long fFirstBucketTime; // could be negative when analyzing events with descending order!!!
94 private long fFirstEventTime;
95 private long fLastEventTime;
96 private long fCurrentEventTime;
97 private long fTimeLimit;
98
99 // Private listener lists
100 private final ListenerList fModelListeners;
101
102 // ------------------------------------------------------------------------
103 // Constructors
104 // ------------------------------------------------------------------------
105
106 /**
107 * Default constructor with default number of buckets.
108 */
109 public HistogramDataModel() {
110 this(0, DEFAULT_NUMBER_OF_BUCKETS);
111 }
112
113 /**
114 * Default constructor with default number of buckets.
115 * @param startTime The histogram start time
116 * @since 2.0
117 */
118 public HistogramDataModel(long startTime) {
119 this(startTime, DEFAULT_NUMBER_OF_BUCKETS);
120 }
121
122 /**
123 * Constructor with non-default number of buckets.
124 * @param nbBuckets A number of buckets.
125 */
126 public HistogramDataModel(int nbBuckets) {
127 this(0, nbBuckets);
128 }
129
130 /**
131 * Constructor with non-default number of buckets.
132 * @param startTime the histogram start time
133 * @param nbBuckets A number of buckets.
134 * @since 2.0
135 */
136 public HistogramDataModel(long startTime, int nbBuckets) {
137 fFirstBucketTime = fFirstEventTime = fLastEventTime = startTime;
138 fNbBuckets = nbBuckets;
139 fBuckets = new long[nbBuckets];
140 fModelListeners = new ListenerList();
141 clear();
142 }
143
144 /**
145 * Copy constructor.
146 * @param other A model to copy.
147 */
148 public HistogramDataModel(HistogramDataModel other) {
149 fNbBuckets = other.fNbBuckets;
150 fBuckets = Arrays.copyOf(other.fBuckets, fNbBuckets);
151 fBucketDuration = Math.max(other.fBucketDuration, 1);
152 fNbEvents = other.fNbEvents;
153 fLastBucket = other.fLastBucket;
154 fFirstBucketTime = other.fFirstBucketTime;
155 fFirstEventTime = other.fFirstEventTime;
156 fLastEventTime = other.fLastEventTime;
157 fCurrentEventTime = other.fCurrentEventTime;
158 fTimeLimit = other.fTimeLimit;
159 fModelListeners = new ListenerList();
160 Object[] listeners = other.fModelListeners.getListeners();
161 for (Object listener : listeners) {
162 fModelListeners.add(listener);
163 }
164 }
165
166 // ------------------------------------------------------------------------
167 // Accessors
168 // ------------------------------------------------------------------------
169
170 /**
171 * Returns the number of events in the data model.
172 * @return number of events.
173 */
174 public long getNbEvents() {
175 return fNbEvents;
176 }
177
178 /**
179 * Returns the number of buckets in the model.
180 * @return number of buckets.
181 */
182 public int getNbBuckets() {
183 return fNbBuckets;
184 }
185
186 /**
187 * Returns the current bucket duration.
188 * @return bucket duration
189 */
190 public long getBucketDuration() {
191 return fBucketDuration;
192 }
193
194 /**
195 * Returns the time value of the first bucket in the model.
196 * @return time of first bucket.
197 */
198 public long getFirstBucketTime() {
199 return fFirstBucketTime;
200 }
201
202 /**
203 * Returns the time of the first event in the model.
204 * @return time of first event.
205 */
206 public long getStartTime() {
207 return fFirstEventTime;
208 }
209
210 /**
211 * Sets the model start time
212 * @param startTime the histogram range start time
213 * @param endTime the histogram range end time
214 * @since 2.0
215 */
216 public void setTimeRange(long startTime, long endTime) {
217 fFirstBucketTime = fFirstEventTime = fLastEventTime = startTime;
218 fBucketDuration = 1;
219 updateEndTime();
220 while (endTime >= fTimeLimit) {
221 mergeBuckets();
222 }
223 }
224
225 /**
226 * Returns the time of the last event in the model.
227 * @return the time of last event.
228 */
229 public long getEndTime() {
230 return fLastEventTime;
231 }
232
233 /**
234 * Returns the time of the current event in the model.
235 * @return the time of the current event.
236 */
237 public long getCurrentEventTime() {
238 return fCurrentEventTime;
239 }
240
241 /**
242 * Returns the time limit with is: start time + nbBuckets * bucketDuration
243 * @return the time limit.
244 */
245 public long getTimeLimit() {
246 return fTimeLimit;
247 }
248
249 // ------------------------------------------------------------------------
250 // Listener handling
251 // ------------------------------------------------------------------------
252
253 /**
254 * Add a listener to the model to be informed about model changes.
255 * @param listener A listener to add.
256 */
257 public void addHistogramListener(IHistogramModelListener listener) {
258 fModelListeners.add(listener);
259 }
260
261 /**
262 * Remove a given model listener.
263 * @param listener A listener to remove.
264 */
265 public void removeHistogramListener(IHistogramModelListener listener) {
266 fModelListeners.remove(listener);
267 }
268
269 // Notify listeners (always)
270 private void fireModelUpdateNotification() {
271 fireModelUpdateNotification(0);
272 }
273
274 // Notify listener on boundary
275 private void fireModelUpdateNotification(long count) {
276 if ((count % REFRESH_FREQUENCY) == 0) {
277 Object[] listeners = fModelListeners.getListeners();
278 for (Object listener2 : listeners) {
279 IHistogramModelListener listener = (IHistogramModelListener) listener2;
280 listener.modelUpdated();
281 }
282 }
283 }
284
285 // ------------------------------------------------------------------------
286 // Operations
287 // ------------------------------------------------------------------------
288
289 @Override
290 public void complete() {
291 fireModelUpdateNotification();
292 }
293
294 /**
295 * Clear the histogram model.
296 * @see org.eclipse.linuxtools.tmf.ui.views.distribution.model.IBaseDistributionModel#clear()
297 */
298 @Override
299 public void clear() {
300 Arrays.fill(fBuckets, 0);
301 fNbEvents = 0;
302 fFirstBucketTime = 0;
303 fLastEventTime = 0;
304 fCurrentEventTime = 0;
305 fLastBucket = 0;
306 fBucketDuration = 1;
307 updateEndTime();
308 fireModelUpdateNotification();
309 }
310
311 /**
312 * Sets the current event time (no notification of listeners)
313 *
314 * @param timestamp A time stamp to set.
315 */
316 public void setCurrentEvent(long timestamp) {
317 fCurrentEventTime = timestamp;
318 }
319
320 /**
321 * Sets the current event time with notification of listeners
322 *
323 * @param timestamp A time stamp to set.
324 */
325 public void setCurrentEventNotifyListeners(long timestamp) {
326 fCurrentEventTime = timestamp;
327 fireModelUpdateNotification();
328 }
329
330 /**
331 * Add event to the correct bucket, compacting the if needed.
332 *
333 * @param eventCount The current event Count (for notification purposes)
334 * @param timestamp The timestamp of the event to count
335 *
336 */
337 @Override
338 public void countEvent(long eventCount, long timestamp) {
339
340 // Validate
341 if (timestamp < 0) {
342 return;
343 }
344
345 // Set the start/end time if not already done
346 if ((fFirstBucketTime == 0) && (fLastBucket == 0) && (fBuckets[0] == 0) && (timestamp > 0)) {
347 fFirstBucketTime = timestamp;
348 fFirstEventTime = timestamp;
349 updateEndTime();
350 }
351
352 if (timestamp < fFirstEventTime) {
353 fFirstEventTime = timestamp;
354 }
355
356 if (fLastEventTime < timestamp) {
357 fLastEventTime = timestamp;
358 }
359
360 if (timestamp >= fFirstBucketTime) {
361
362 // Compact as needed
363 while (timestamp >= fTimeLimit) {
364 mergeBuckets();
365 }
366
367 } else {
368
369 // get offset for adjustment
370 int offset = getOffset(timestamp);
371
372 // Compact as needed
373 while((fLastBucket + offset) >= fNbBuckets) {
374 mergeBuckets();
375 offset = getOffset(timestamp);
376 }
377
378 moveBuckets(offset);
379
380 fLastBucket = fLastBucket + offset;
381
382 fFirstBucketTime = fFirstBucketTime - (offset*fBucketDuration);
383 updateEndTime();
384 }
385
386 // Increment the right bucket
387 int index = (int) ((timestamp - fFirstBucketTime) / fBucketDuration);
388 fBuckets[index]++;
389 fNbEvents++;
390 if (fLastBucket < index) {
391 fLastBucket = index;
392 }
393
394 fireModelUpdateNotification(eventCount);
395 }
396
397 /**
398 * Scale the model data to the width, height and bar width requested.
399 *
400 * @param width A width of the histogram canvas
401 * @param height A height of the histogram canvas
402 * @param barWidth A width (in pixel) of a histogram bar
403 * @return the result array of size [width] and where the highest value doesn't exceed [height]
404 *
405 * @see org.eclipse.linuxtools.tmf.ui.views.histogram.IHistogramDataModel#scaleTo(int, int, int)
406 */
407 @Override
408 public HistogramScaledData scaleTo(int width, int height, int barWidth) {
409 // Basic validation
410 if ((width <= 0) || (height <= 0) || (barWidth <= 0))
411 {
412 throw new AssertionError("Invalid histogram dimensions (" + width + "x" + height + ", barWidth=" + barWidth + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
413 }
414
415 // The result structure
416 HistogramScaledData result = new HistogramScaledData(width, height, barWidth);
417
418 // Scale horizontally
419 result.fMaxValue = 0;
420
421 int nbBars = width / barWidth;
422 int bucketsPerBar = (fLastBucket / nbBars) + 1;
423 result.fBucketDuration = Math.max(bucketsPerBar * fBucketDuration,1);
424 for (int i = 0; i < nbBars; i++) {
425 int count = 0;
426 for (int j = i * bucketsPerBar; j < ((i + 1) * bucketsPerBar); j++) {
427 if (fNbBuckets <= j) {
428 break;
429 }
430 count += fBuckets[j];
431 }
432 result.fData[i] = count;
433 result.fLastBucket = i;
434 if (result.fMaxValue < count) {
435 result.fMaxValue = count;
436 }
437 }
438
439 // Scale vertically
440 if (result.fMaxValue > 0) {
441 result.fScalingFactor = (double) height / result.fMaxValue;
442 }
443
444 fBucketDuration = Math.max(fBucketDuration, 1);
445 // Set the current event index in the scaled histogram
446 if ((fCurrentEventTime >= fFirstBucketTime) && (fCurrentEventTime <= fLastEventTime)) {
447 result.fCurrentBucket = (int) ((fCurrentEventTime - fFirstBucketTime) / fBucketDuration) / bucketsPerBar;
448 } else {
449 result.fCurrentBucket = HistogramScaledData.OUT_OF_RANGE_BUCKET;
450 }
451
452 result.fFirstBucketTime = fFirstBucketTime;
453 result.fFirstEventTime = fFirstEventTime;
454 return result;
455 }
456
457 // ------------------------------------------------------------------------
458 // Helper functions
459 // ------------------------------------------------------------------------
460
461 private void updateEndTime() {
462 fTimeLimit = fFirstBucketTime + (fNbBuckets * fBucketDuration);
463 }
464
465 private void mergeBuckets() {
466 for (int i = 0; i < (fNbBuckets / 2); i++) {
467 fBuckets[i] = fBuckets[2 * i] + fBuckets[(2 * i) + 1];
468 }
469 Arrays.fill(fBuckets, fNbBuckets / 2, fNbBuckets, 0);
470 fBucketDuration *= 2;
471 updateEndTime();
472 fLastBucket = (fNbBuckets / 2) - 1;
473 }
474
475 private void moveBuckets(int offset) {
476 for(int i = fNbBuckets - 1; i >= offset; i--) {
477 fBuckets[i] = fBuckets[i-offset];
478 }
479
480 for (int i = 0; i < offset; i++) {
481 fBuckets[i] = 0;
482 }
483 }
484
485 private int getOffset(long timestamp) {
486 int offset = (int) ((fFirstBucketTime - timestamp) / fBucketDuration);
487 if (((fFirstBucketTime - timestamp) % fBucketDuration) != 0) {
488 offset++;
489 }
490 return offset;
491 }
492
493 }
This page took 0.044088 seconds and 6 git commands to generate.