1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 * 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 * Patrick Tasse - Support selection range
16 * Jean-Christian Kouamé, Simon Delisle - Added support to manage lost events
17 *******************************************************************************/
19 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
21 import java
.util
.Arrays
;
23 import org
.eclipse
.core
.runtime
.ListenerList
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
27 * Histogram-independent data model.
29 * It has the following characteristics:
31 * <li>The <i>basetime</i> is the timestamp of the first event
32 * <li>There is a fixed number (<i>n</i>) of buckets of uniform duration
34 * <li>The <i>timespan</i> of the model is thus: <i>n</i> * <i>d</i> time units
35 * <li>Bucket <i>i</i> holds the number of events that occurred in time range:
36 * [<i>basetime</i> + <i>i</i> * <i>d</i>, <i>basetime</i> + (<i>i</i> + 1) *
39 * Initially, the bucket durations is set to 1ns. As the events are read, they
40 * are tallied (using <i>countEvent()</i>) in the appropriate bucket (relative
41 * to the <i>basetime</i>).
43 * Eventually, an event will have a timestamp that exceeds the <i>timespan</i>
44 * high end (determined by <i>n</i>, the number of buckets, and <i>d</i>, the
45 * bucket duration). At this point, the histogram needs to be compacted. This is
46 * done by simply merging adjacent buckets by pair, in effect doubling the
47 * <i>timespan</i> (<i>timespan'</i> = <i>n</i> * <i>d'</i>, where <i>d'</i> =
48 * 2<i>d</i>). This compaction happens as needed as the trace is read.
50 * The model allows for timestamps in not increasing order. The timestamps can
51 * be fed to the model in any order. If an event has a timestamp less than the
52 * <i>basetime</i>, the buckets will be moved to the right to account for the
53 * new smaller timestamp. The new <i>basetime</i> is a multiple of the bucket
54 * duration smaller then the previous <i>basetime</i>. Note that the
55 * <i>basetime</i> might no longer be the timestamp of an event. If necessary,
56 * the buckets will be compacted before moving to the right. This might be
57 * necessary to not lose any event counts at the end of the buckets array.
59 * The mapping from the model to the UI is performed by the <i>scaleTo()</i>
60 * method. By keeping the number of buckets <i>n</i> relatively large with
61 * respect to to the number of pixels in the actual histogram, we should achieve
62 * a nice result when visualizing the histogram.
66 * @author Francois Chouinard
68 public class HistogramDataModel
implements IHistogramDataModel
{
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * The default number of buckets
77 public static final int DEFAULT_NUMBER_OF_BUCKETS
= 16 * 1000;
80 * Number of events after which listeners will be notified.
82 public static final int REFRESH_FREQUENCY
= DEFAULT_NUMBER_OF_BUCKETS
;
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 private final int fNbBuckets
;
90 private final long[] fBuckets
;
91 private final long[] fLostEventsBuckets
;
92 private long fBucketDuration
;
93 private long fNbEvents
;
94 private int fLastBucket
;
97 private long fFirstBucketTime
; // could be negative when analyzing events with descending order!!!
98 private long fFirstEventTime
;
99 private long fEndTime
;
100 private long fSelectionBegin
;
101 private long fSelectionEnd
;
102 private long fTimeLimit
;
104 // Private listener lists
105 private final ListenerList fModelListeners
;
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
112 * Default constructor with default number of buckets.
114 public HistogramDataModel() {
115 this(0, DEFAULT_NUMBER_OF_BUCKETS
);
119 * Default constructor with default number of buckets.
122 * The histogram start time
125 public HistogramDataModel(long startTime
) {
126 this(startTime
, DEFAULT_NUMBER_OF_BUCKETS
);
130 * Constructor with non-default number of buckets.
133 * A number of buckets.
135 public HistogramDataModel(int nbBuckets
) {
140 * Constructor with non-default number of buckets.
143 * the histogram start time
145 * A number of buckets.
148 public HistogramDataModel(long startTime
, int nbBuckets
) {
149 fFirstBucketTime
= fFirstEventTime
= fEndTime
= startTime
;
150 fNbBuckets
= nbBuckets
;
151 fBuckets
= new long[nbBuckets
];
152 fLostEventsBuckets
= new long[nbBuckets
];
153 fModelListeners
= new ListenerList();
163 public HistogramDataModel(HistogramDataModel other
) {
164 fNbBuckets
= other
.fNbBuckets
;
165 fBuckets
= Arrays
.copyOf(other
.fBuckets
, fNbBuckets
);
166 fLostEventsBuckets
= Arrays
.copyOf(other
.fLostEventsBuckets
, fNbBuckets
);
167 fBucketDuration
= Math
.max(other
.fBucketDuration
, 1);
168 fNbEvents
= other
.fNbEvents
;
169 fLastBucket
= other
.fLastBucket
;
170 fFirstBucketTime
= other
.fFirstBucketTime
;
171 fFirstEventTime
= other
.fFirstEventTime
;
172 fEndTime
= other
.fEndTime
;
173 fSelectionBegin
= other
.fSelectionBegin
;
174 fSelectionEnd
= other
.fSelectionEnd
;
175 fTimeLimit
= other
.fTimeLimit
;
176 fModelListeners
= new ListenerList();
177 Object
[] listeners
= other
.fModelListeners
.getListeners();
178 for (Object listener
: listeners
) {
179 fModelListeners
.add(listener
);
183 // ------------------------------------------------------------------------
185 // ------------------------------------------------------------------------
188 * Returns the number of events in the data model.
190 * @return number of events.
192 public long getNbEvents() {
197 * Returns the number of buckets in the model.
199 * @return number of buckets.
201 public int getNbBuckets() {
206 * Returns the current bucket duration.
208 * @return bucket duration
210 public long getBucketDuration() {
211 return fBucketDuration
;
215 * Returns the time value of the first bucket in the model.
217 * @return time of first bucket.
219 public long getFirstBucketTime() {
220 return fFirstBucketTime
;
224 * Returns the time of the first event in the model.
226 * @return time of first event.
228 public long getStartTime() {
229 return fFirstEventTime
;
233 * Sets the model start time
236 * the histogram range start time
238 * the histogram range end time
241 public void setTimeRange(long startTime
, long endTime
) {
242 fFirstBucketTime
= fFirstEventTime
= fEndTime
= startTime
;
245 while (endTime
>= fTimeLimit
) {
251 * Set the end time. Setting this ensures that the corresponding bucket is
252 * displayed regardless of the event counts.
255 * the time of the last used bucket
258 public void setEndTime(long endTime
) {
260 fLastBucket
= (int) ((endTime
- fFirstBucketTime
) / fBucketDuration
);
264 * Returns the end time.
266 * @return the time of the last used bucket
268 public long getEndTime() {
273 * Returns the time of the current event in the model.
275 * @return the time of the current event.
276 * @deprecated As of 2.1, use {@link #getSelectionBegin()} and
277 * {@link #getSelectionEnd()}
280 public long getCurrentEventTime() {
281 return fSelectionBegin
;
285 * Returns the begin time of the current selection in the model.
287 * @return the begin time of the current selection.
290 public long getSelectionBegin() {
291 return fSelectionBegin
;
295 * Returns the end time of the current selection in the model.
297 * @return the end time of the current selection.
300 public long getSelectionEnd() {
301 return fSelectionEnd
;
305 * Returns the time limit with is: start time + nbBuckets * bucketDuration
307 * @return the time limit.
309 public long getTimeLimit() {
313 // ------------------------------------------------------------------------
315 // ------------------------------------------------------------------------
318 * Add a listener to the model to be informed about model changes.
323 public void addHistogramListener(IHistogramModelListener listener
) {
324 fModelListeners
.add(listener
);
328 * Remove a given model listener.
331 * A listener to remove.
333 public void removeHistogramListener(IHistogramModelListener listener
) {
334 fModelListeners
.remove(listener
);
337 // Notify listeners (always)
338 private void fireModelUpdateNotification() {
339 fireModelUpdateNotification(0);
342 // Notify listener on boundary
343 private void fireModelUpdateNotification(long count
) {
344 if ((count
% REFRESH_FREQUENCY
) == 0) {
345 Object
[] listeners
= fModelListeners
.getListeners();
346 for (Object listener2
: listeners
) {
347 IHistogramModelListener listener
= (IHistogramModelListener
) listener2
;
348 listener
.modelUpdated();
353 // ------------------------------------------------------------------------
355 // ------------------------------------------------------------------------
358 public void complete() {
359 fireModelUpdateNotification();
363 * Clear the histogram model.
365 * @see org.eclipse.linuxtools.tmf.ui.views.distribution.model.IBaseDistributionModel#clear()
368 public void clear() {
369 Arrays
.fill(fBuckets
, 0);
370 Arrays
.fill(fLostEventsBuckets
, 0);
372 fFirstBucketTime
= 0;
379 fireModelUpdateNotification();
383 * Sets the current event time (no notification of listeners)
386 * A time stamp to set.
387 * @deprecated As of 2.1, use {@link #setSelection(long, long)}
390 public void setCurrentEvent(long timestamp
) {
391 fSelectionBegin
= timestamp
;
392 fSelectionEnd
= timestamp
;
396 * Sets the current event time with notification of listeners
399 * A time stamp to set.
400 * @deprecated As of 2.1, use
401 * {@link #setSelectionNotifyListeners(long, long)}
404 public void setCurrentEventNotifyListeners(long timestamp
) {
405 fSelectionBegin
= timestamp
;
406 fSelectionEnd
= timestamp
;
407 fireModelUpdateNotification();
411 * Sets the current selection time range (no notification of listeners)
414 * The selection begin time.
416 * The selection end time.
419 public void setSelection(long beginTime
, long endTime
) {
420 fSelectionBegin
= beginTime
;
421 fSelectionEnd
= endTime
;
425 * Sets the current selection time range with notification of listeners
428 * The selection begin time.
430 * The selection end time.
433 public void setSelectionNotifyListeners(long beginTime
, long endTime
) {
434 fSelectionBegin
= beginTime
;
435 fSelectionEnd
= endTime
;
436 fireModelUpdateNotification();
440 * Add event to the correct bucket, compacting the if needed.
443 * The current event Count (for notification purposes)
445 * The timestamp of the event to count
449 public void countEvent(long eventCount
, long timestamp
) {
456 // Set the start/end time if not already done
457 if ((fFirstBucketTime
== 0) && (fLastBucket
== 0) && (fBuckets
[0] == 0) && (timestamp
> 0)) {
458 fFirstBucketTime
= timestamp
;
459 fFirstEventTime
= timestamp
;
463 if (timestamp
< fFirstEventTime
) {
464 fFirstEventTime
= timestamp
;
467 if (fEndTime
< timestamp
) {
468 fEndTime
= timestamp
;
471 if (timestamp
>= fFirstBucketTime
) {
474 while (timestamp
>= fTimeLimit
) {
480 // get offset for adjustment
481 int offset
= getOffset(timestamp
);
484 while ((fLastBucket
+ offset
) >= fNbBuckets
) {
486 offset
= getOffset(timestamp
);
491 fLastBucket
= fLastBucket
+ offset
;
493 fFirstBucketTime
= fFirstBucketTime
- (offset
* fBucketDuration
);
497 // Increment the right bucket
498 int index
= (int) ((timestamp
- fFirstBucketTime
) / fBucketDuration
);
501 if (fLastBucket
< index
) {
505 fireModelUpdateNotification(eventCount
);
509 * Add lost event to the correct bucket, compacting the if needed.
512 * time range of a lost event
513 * @param nbLostEvents
514 * the number of lost events
516 * Full range or time range for histogram request
519 public void countLostEvent(TmfTimeRange timeRange
, long nbLostEvents
, boolean fullRange
) {
522 if (timeRange
.getStartTime().getValue() < 0 || timeRange
.getEndTime().getValue() < 0) {
528 while (timeRange
.getEndTime().getValue() >= fTimeLimit
) {
533 int indexStart
= (int) ((timeRange
.getStartTime().getValue() - fFirstBucketTime
) / fBucketDuration
);
534 int indexEnd
= (int) ((timeRange
.getEndTime().getValue() - fFirstBucketTime
) / fBucketDuration
);
535 int nbBucketRange
= (indexEnd
- indexStart
) + 1;
537 int lostEventPerBucket
= (int) Math
.ceil((double) nbLostEvents
/ nbBucketRange
);
538 long lastLostCol
= Math
.max(1, nbLostEvents
- lostEventPerBucket
* (nbBucketRange
- 1));
540 // Increment the right bucket, bear in mind that ranges make it almost certain that some lost events are out of range
541 for (int index
= indexStart
; index
<= indexEnd
&& index
< fLostEventsBuckets
.length
; index
++) {
542 if (index
== (indexStart
+ nbBucketRange
- 1)) {
543 fLostEventsBuckets
[index
] += lastLostCol
;
545 fLostEventsBuckets
[index
] += lostEventPerBucket
;
551 fireModelUpdateNotification(nbLostEvents
);
555 * Scale the model data to the width, height and bar width requested.
558 * A width of the histogram canvas
560 * A height of the histogram canvas
562 * A width (in pixel) of a histogram bar
563 * @return the result array of size [width] and where the highest value
564 * doesn't exceed [height]
566 * @see org.eclipse.linuxtools.tmf.ui.views.histogram.IHistogramDataModel#scaleTo(int,
570 public HistogramScaledData
scaleTo(int width
, int height
, int barWidth
) {
572 if ((width
<= 0) || (height
<= 0) || (barWidth
<= 0))
574 throw new AssertionError("Invalid histogram dimensions (" + width
+ "x" + height
+ ", barWidth=" + barWidth
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
577 // The result structure
578 HistogramScaledData result
= new HistogramScaledData(width
, height
, barWidth
);
580 // Scale horizontally
581 result
.fMaxValue
= 0;
583 int nbBars
= width
/ barWidth
;
584 int bucketsPerBar
= (fLastBucket
/ nbBars
) + 1;
585 result
.fBucketDuration
= Math
.max(bucketsPerBar
* fBucketDuration
, 1);
586 for (int i
= 0; i
< nbBars
; i
++) {
588 int countLostEvent
= 0;
589 for (int j
= i
* bucketsPerBar
; j
< ((i
+ 1) * bucketsPerBar
); j
++) {
590 if (fNbBuckets
<= j
) {
593 count
+= fBuckets
[j
];
594 countLostEvent
+= fLostEventsBuckets
[j
];
596 result
.fData
[i
] = count
;
597 result
.fLostEventsData
[i
] = countLostEvent
;
598 result
.fLastBucket
= i
;
599 if (result
.fMaxValue
< count
) {
600 result
.fMaxValue
= count
;
602 if (result
.fMaxCombinedValue
< count
+ countLostEvent
) {
603 result
.fMaxCombinedValue
= count
+ countLostEvent
;
608 if (result
.fMaxValue
> 0) {
609 result
.fScalingFactor
= (double) height
/ result
.fMaxValue
;
611 if (result
.fMaxCombinedValue
> 0) {
612 result
.fScalingFactorCombined
= (double) height
/ result
.fMaxCombinedValue
;
615 fBucketDuration
= Math
.max(fBucketDuration
, 1);
616 // Set selection begin and end index in the scaled histogram
617 result
.fSelectionBeginBucket
= (int) ((fSelectionBegin
- fFirstBucketTime
) / fBucketDuration
) / bucketsPerBar
;
618 result
.fSelectionEndBucket
= (int) ((fSelectionEnd
- fFirstBucketTime
) / fBucketDuration
) / bucketsPerBar
;
620 result
.fFirstBucketTime
= fFirstBucketTime
;
621 result
.fFirstEventTime
= fFirstEventTime
;
625 // ------------------------------------------------------------------------
627 // ------------------------------------------------------------------------
629 private void updateEndTime() {
630 fTimeLimit
= fFirstBucketTime
+ (fNbBuckets
* fBucketDuration
);
633 private void mergeBuckets() {
634 for (int i
= 0; i
< (fNbBuckets
/ 2); i
++) {
635 fBuckets
[i
] = fBuckets
[2 * i
] + fBuckets
[(2 * i
) + 1];
636 fLostEventsBuckets
[i
] = fLostEventsBuckets
[2 * i
] + fLostEventsBuckets
[(2 * i
) + 1];
638 Arrays
.fill(fBuckets
, fNbBuckets
/ 2, fNbBuckets
, 0);
639 Arrays
.fill(fLostEventsBuckets
, fNbBuckets
/ 2, fNbBuckets
, 0);
640 fBucketDuration
*= 2;
642 fLastBucket
= (fNbBuckets
/ 2) - 1;
645 private void moveBuckets(int offset
) {
646 for (int i
= fNbBuckets
- 1; i
>= offset
; i
--) {
647 fBuckets
[i
] = fBuckets
[i
- offset
];
648 fLostEventsBuckets
[i
] = fLostEventsBuckets
[i
- offset
];
651 for (int i
= 0; i
< offset
; i
++) {
653 fLostEventsBuckets
[i
] = 0;
657 private int getOffset(long timestamp
) {
658 int offset
= (int) ((fFirstBucketTime
- timestamp
) / fBucketDuration
);
659 if (((fFirstBucketTime
- timestamp
) % fBucketDuration
) != 0) {