1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Xavier Raynaud - Support multi-trace coloring
18 *******************************************************************************/
20 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
22 import java
.util
.Arrays
;
23 import java
.util
.LinkedHashMap
;
26 import org
.eclipse
.core
.runtime
.ListenerList
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
32 * Histogram-independent data model.
34 * It has the following characteristics:
36 * <li>The <i>basetime</i> is the timestamp of the first event
37 * <li>There is a fixed number (<i>n</i>) of buckets of uniform duration
39 * <li>The <i>timespan</i> of the model is thus: <i>n</i> * <i>d</i> time units
40 * <li>Bucket <i>i</i> holds the number of events that occurred in time range:
41 * [<i>basetime</i> + <i>i</i> * <i>d</i>, <i>basetime</i> + (<i>i</i> + 1) *
44 * Initially, the bucket durations is set to 1ns. As the events are read, they
45 * are tallied (using <i>countEvent()</i>) in the appropriate bucket (relative
46 * to the <i>basetime</i>).
48 * Eventually, an event will have a timestamp that exceeds the <i>timespan</i>
49 * high end (determined by <i>n</i>, the number of buckets, and <i>d</i>, the
50 * bucket duration). At this point, the histogram needs to be compacted. This is
51 * done by simply merging adjacent buckets by pair, in effect doubling the
52 * <i>timespan</i> (<i>timespan'</i> = <i>n</i> * <i>d'</i>, where <i>d'</i> =
53 * 2<i>d</i>). This compaction happens as needed as the trace is read.
55 * The model allows for timestamps in not increasing order. The timestamps can
56 * be fed to the model in any order. If an event has a timestamp less than the
57 * <i>basetime</i>, the buckets will be moved to the right to account for the
58 * new smaller timestamp. The new <i>basetime</i> is a multiple of the bucket
59 * duration smaller then the previous <i>basetime</i>. Note that the
60 * <i>basetime</i> might no longer be the timestamp of an event. If necessary,
61 * the buckets will be compacted before moving to the right. This might be
62 * necessary to not lose any event counts at the end of the buckets array.
64 * The mapping from the model to the UI is performed by the <i>scaleTo()</i>
65 * method. By keeping the number of buckets <i>n</i> relatively large with
66 * respect to to the number of pixels in the actual histogram, we should achieve
67 * a nice result when visualizing the histogram.
71 * @author Francois Chouinard
73 public class HistogramDataModel
implements IHistogramDataModel
{
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 * The default number of buckets
82 public static final int DEFAULT_NUMBER_OF_BUCKETS
= 16 * 1000;
85 * Number of events after which listeners will be notified.
87 public static final int REFRESH_FREQUENCY
= DEFAULT_NUMBER_OF_BUCKETS
;
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 private ITmfTrace fTrace
= null;
95 private final Map
<ITmfTrace
, Integer
> fTraceMap
= new LinkedHashMap
<>();
98 private final int fNbBuckets
;
99 private final HistogramBucket
[] fBuckets
;
100 private final long[] fLostEventsBuckets
;
101 private long fBucketDuration
;
102 private long fNbEvents
;
103 private int fLastBucket
;
106 private long fFirstBucketTime
; // could be negative when analyzing events with descending order!!!
107 private long fFirstEventTime
;
108 private long fEndTime
;
109 private long fSelectionBegin
;
110 private long fSelectionEnd
;
111 private long fTimeLimit
;
113 // Private listener lists
114 private final ListenerList fModelListeners
;
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
121 * Default constructor with default number of buckets.
123 public HistogramDataModel() {
124 this(0, DEFAULT_NUMBER_OF_BUCKETS
);
128 * Default constructor with default number of buckets.
131 * The histogram start time
134 public HistogramDataModel(long startTime
) {
135 this(startTime
, DEFAULT_NUMBER_OF_BUCKETS
);
139 * Constructor with non-default number of buckets.
142 * A number of buckets.
144 public HistogramDataModel(int nbBuckets
) {
149 * Constructor with non-default number of buckets.
152 * the histogram start time
154 * A number of buckets.
157 public HistogramDataModel(long startTime
, int nbBuckets
) {
158 fFirstBucketTime
= fFirstEventTime
= fEndTime
= startTime
;
159 fNbBuckets
= nbBuckets
;
160 fBuckets
= new HistogramBucket
[nbBuckets
];
161 fLostEventsBuckets
= new long[nbBuckets
];
162 fModelListeners
= new ListenerList();
172 public HistogramDataModel(HistogramDataModel other
) {
173 fNbBuckets
= other
.fNbBuckets
;
174 fBuckets
= new HistogramBucket
[fNbBuckets
];
175 for (int i
= 0; i
< fNbBuckets
; i
++) {
176 fBuckets
[i
] = new HistogramBucket(other
.fBuckets
[i
]);
178 fLostEventsBuckets
= Arrays
.copyOf(other
.fLostEventsBuckets
, fNbBuckets
);
179 fBucketDuration
= Math
.max(other
.fBucketDuration
, 1);
180 fNbEvents
= other
.fNbEvents
;
181 fLastBucket
= other
.fLastBucket
;
182 fFirstBucketTime
= other
.fFirstBucketTime
;
183 fFirstEventTime
= other
.fFirstEventTime
;
184 fEndTime
= other
.fEndTime
;
185 fSelectionBegin
= other
.fSelectionBegin
;
186 fSelectionEnd
= other
.fSelectionEnd
;
187 fTimeLimit
= other
.fTimeLimit
;
188 fModelListeners
= new ListenerList();
189 Object
[] listeners
= other
.fModelListeners
.getListeners();
190 for (Object listener
: listeners
) {
191 fModelListeners
.add(listener
);
197 * Disposes the data model
200 public void dispose() {
205 // ------------------------------------------------------------------------
207 // ------------------------------------------------------------------------
210 * Returns the number of events in the data model.
212 * @return number of events.
214 public long getNbEvents() {
219 * Returns the number of buckets in the model.
221 * @return number of buckets.
223 public int getNbBuckets() {
228 * Returns the current bucket duration.
230 * @return bucket duration
232 public long getBucketDuration() {
233 return fBucketDuration
;
237 * Returns the time value of the first bucket in the model.
239 * @return time of first bucket.
241 public long getFirstBucketTime() {
242 return fFirstBucketTime
;
246 * Returns the time of the first event in the model.
248 * @return time of first event.
250 public long getStartTime() {
251 return fFirstEventTime
;
255 * Sets the trace of this model.
256 * @param trace - a {@link ITmfTrace}
259 public void setTrace(ITmfTrace trace
) {
262 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(fTrace
);
263 if (traces
!= null) {
265 for (ITmfTrace tr
: traces
) {
266 fTraceMap
.put(tr
, i
);
273 * Gets the trace of this model.
274 * @return a {@link ITmfTrace}
277 public ITmfTrace
getTrace() {
282 * Gets the traces names of this model.
283 * @return an array of trace names
286 public String
[] getTraceNames() {
287 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(fTrace
);
288 if (traces
== null) {
289 return new String
[0];
291 String
[] traceNames
= new String
[traces
.length
];
293 for (ITmfTrace tr
: traces
) {
294 traceNames
[i
] = tr
.getName();
301 * Gets the number of traces of this model.
302 * @return the number of traces of this model.
305 public int getNbTraces() {
306 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(fTrace
);
307 if (traces
== null) {
310 return traces
.length
;
314 * Sets the model start time
317 * the histogram range start time
319 * the histogram range end time
322 public void setTimeRange(long startTime
, long endTime
) {
323 fFirstBucketTime
= fFirstEventTime
= fEndTime
= startTime
;
326 while (endTime
>= fTimeLimit
) {
332 * Set the end time. Setting this ensures that the corresponding bucket is
333 * displayed regardless of the event counts.
336 * the time of the last used bucket
339 public void setEndTime(long endTime
) {
341 fLastBucket
= (int) ((endTime
- fFirstBucketTime
) / fBucketDuration
);
345 * Returns the end time.
347 * @return the time of the last used bucket
349 public long getEndTime() {
354 * Returns the begin time of the current selection in the model.
356 * @return the begin time of the current selection.
359 public long getSelectionBegin() {
360 return fSelectionBegin
;
364 * Returns the end time of the current selection in the model.
366 * @return the end time of the current selection.
369 public long getSelectionEnd() {
370 return fSelectionEnd
;
374 * Returns the time limit with is: start time + nbBuckets * bucketDuration
376 * @return the time limit.
378 public long getTimeLimit() {
382 // ------------------------------------------------------------------------
384 // ------------------------------------------------------------------------
387 * Add a listener to the model to be informed about model changes.
392 public void addHistogramListener(IHistogramModelListener listener
) {
393 fModelListeners
.add(listener
);
397 * Remove a given model listener.
400 * A listener to remove.
402 public void removeHistogramListener(IHistogramModelListener listener
) {
403 fModelListeners
.remove(listener
);
406 // Notify listeners (always)
407 private void fireModelUpdateNotification() {
408 fireModelUpdateNotification(0);
411 // Notify listener on boundary
412 private void fireModelUpdateNotification(long count
) {
413 if ((count
% REFRESH_FREQUENCY
) == 0) {
414 Object
[] listeners
= fModelListeners
.getListeners();
415 for (Object listener2
: listeners
) {
416 IHistogramModelListener listener
= (IHistogramModelListener
) listener2
;
417 listener
.modelUpdated();
422 // ------------------------------------------------------------------------
424 // ------------------------------------------------------------------------
427 public void complete() {
428 fireModelUpdateNotification();
432 * Clear the histogram model.
434 * @see org.eclipse.linuxtools.tmf.ui.views.distribution.model.IBaseDistributionModel#clear()
437 public void clear() {
438 Arrays
.fill(fBuckets
, null);
439 Arrays
.fill(fLostEventsBuckets
, 0);
441 fFirstBucketTime
= 0;
448 fireModelUpdateNotification();
452 * Sets the current selection time range (no notification of listeners)
455 * The selection begin time.
457 * The selection end time.
460 public void setSelection(long beginTime
, long endTime
) {
461 fSelectionBegin
= beginTime
;
462 fSelectionEnd
= endTime
;
466 * Sets the current selection time range with notification of listeners
469 * The selection begin time.
471 * The selection end time.
474 public void setSelectionNotifyListeners(long beginTime
, long endTime
) {
475 fSelectionBegin
= beginTime
;
476 fSelectionEnd
= endTime
;
477 fireModelUpdateNotification();
481 * Add event to the correct bucket, compacting the if needed.
484 * The current event Count (for notification purposes)
486 * The timestamp of the event to count
492 public void countEvent(long eventCount
, long timestamp
, ITmfTrace trace
) {
499 // Set the start/end time if not already done
500 if ((fFirstBucketTime
== 0) && (fLastBucket
== 0) && (fBuckets
[0] == null) && (timestamp
> 0)) {
501 fFirstBucketTime
= timestamp
;
502 fFirstEventTime
= timestamp
;
506 if (timestamp
< fFirstEventTime
) {
507 fFirstEventTime
= timestamp
;
510 if (fEndTime
< timestamp
) {
511 fEndTime
= timestamp
;
514 if (timestamp
>= fFirstBucketTime
) {
517 while (timestamp
>= fTimeLimit
) {
523 // get offset for adjustment
524 int offset
= getOffset(timestamp
);
527 while ((fLastBucket
+ offset
) >= fNbBuckets
) {
529 offset
= getOffset(timestamp
);
534 fLastBucket
= fLastBucket
+ offset
;
536 fFirstBucketTime
= fFirstBucketTime
- (offset
* fBucketDuration
);
540 // Increment the right bucket
541 int index
= (int) ((timestamp
- fFirstBucketTime
) / fBucketDuration
);
542 if (fBuckets
[index
] == null) {
543 fBuckets
[index
] = new HistogramBucket(getNbTraces());
545 Integer traceIndex
= fTraceMap
.get(trace
);
546 if (traceIndex
== null) {
549 fBuckets
[index
].addEvent(traceIndex
);
551 if (fLastBucket
< index
) {
555 fireModelUpdateNotification(eventCount
);
559 * Add lost event to the correct bucket, compacting the if needed.
562 * time range of a lost event
563 * @param nbLostEvents
564 * the number of lost events
566 * Full range or time range for histogram request
569 public void countLostEvent(TmfTimeRange timeRange
, long nbLostEvents
, boolean fullRange
) {
572 if (timeRange
.getStartTime().getValue() < 0 || timeRange
.getEndTime().getValue() < 0) {
578 while (timeRange
.getEndTime().getValue() >= fTimeLimit
) {
583 int indexStart
= (int) ((timeRange
.getStartTime().getValue() - fFirstBucketTime
) / fBucketDuration
);
584 int indexEnd
= (int) ((timeRange
.getEndTime().getValue() - fFirstBucketTime
) / fBucketDuration
);
585 int nbBucketRange
= (indexEnd
- indexStart
) + 1;
587 int lostEventPerBucket
= (int) Math
.ceil((double) nbLostEvents
/ nbBucketRange
);
588 long lastLostCol
= Math
.max(1, nbLostEvents
- lostEventPerBucket
* (nbBucketRange
- 1));
590 // Increment the right bucket, bear in mind that ranges make it almost certain that some lost events are out of range
591 for (int index
= indexStart
; index
<= indexEnd
&& index
< fLostEventsBuckets
.length
; index
++) {
592 if (index
== (indexStart
+ nbBucketRange
- 1)) {
593 fLostEventsBuckets
[index
] += lastLostCol
;
595 fLostEventsBuckets
[index
] += lostEventPerBucket
;
601 fireModelUpdateNotification(nbLostEvents
);
605 * Scale the model data to the width, height and bar width requested.
608 * A width of the histogram canvas
610 * A height of the histogram canvas
612 * A width (in pixel) of a histogram bar
613 * @return the result array of size [width] and where the highest value
614 * doesn't exceed [height]
616 * @see org.eclipse.linuxtools.tmf.ui.views.histogram.IHistogramDataModel#scaleTo(int,
620 public HistogramScaledData
scaleTo(int width
, int height
, int barWidth
) {
622 if ((width
<= 0) || (height
<= 0) || (barWidth
<= 0))
624 throw new AssertionError("Invalid histogram dimensions (" + width
+ "x" + height
+ ", barWidth=" + barWidth
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
627 // The result structure
628 HistogramScaledData result
= new HistogramScaledData(width
, height
, barWidth
);
630 // Scale horizontally
631 result
.fMaxValue
= 0;
633 int nbBars
= width
/ barWidth
;
634 int bucketsPerBar
= (fLastBucket
/ nbBars
) + 1;
635 result
.fBucketDuration
= Math
.max(bucketsPerBar
* fBucketDuration
, 1);
636 for (int i
= 0; i
< nbBars
; i
++) {
638 int countLostEvent
= 0;
639 result
.fData
[i
] = new HistogramBucket(getNbTraces());
640 for (int j
= i
* bucketsPerBar
; j
< ((i
+ 1) * bucketsPerBar
); j
++) {
641 if (fNbBuckets
<= j
) {
644 if (fBuckets
[j
] != null) {
645 count
+= fBuckets
[j
].getNbEvents();
646 result
.fData
[i
].add(fBuckets
[j
]);
648 countLostEvent
+= fLostEventsBuckets
[j
];
650 result
.fLostEventsData
[i
] = countLostEvent
;
651 result
.fLastBucket
= i
;
652 if (result
.fMaxValue
< count
) {
653 result
.fMaxValue
= count
;
655 if (result
.fMaxCombinedValue
< count
+ countLostEvent
) {
656 result
.fMaxCombinedValue
= count
+ countLostEvent
;
661 if (result
.fMaxValue
> 0) {
662 result
.fScalingFactor
= (double) height
/ result
.fMaxValue
;
664 if (result
.fMaxCombinedValue
> 0) {
665 result
.fScalingFactorCombined
= (double) height
/ result
.fMaxCombinedValue
;
668 fBucketDuration
= Math
.max(fBucketDuration
, 1);
669 // Set selection begin and end index in the scaled histogram
670 result
.fSelectionBeginBucket
= (int) ((fSelectionBegin
- fFirstBucketTime
) / fBucketDuration
) / bucketsPerBar
;
671 result
.fSelectionEndBucket
= (int) ((fSelectionEnd
- fFirstBucketTime
) / fBucketDuration
) / bucketsPerBar
;
673 result
.fFirstBucketTime
= fFirstBucketTime
;
674 result
.fFirstEventTime
= fFirstEventTime
;
678 // ------------------------------------------------------------------------
680 // ------------------------------------------------------------------------
682 private void updateEndTime() {
683 fTimeLimit
= fFirstBucketTime
+ (fNbBuckets
* fBucketDuration
);
686 private void mergeBuckets() {
687 for (int i
= 0; i
< (fNbBuckets
/ 2); i
++) {
688 fBuckets
[i
] = new HistogramBucket(fBuckets
[2 * i
], fBuckets
[(2 * i
) + 1]);
689 fLostEventsBuckets
[i
] = fLostEventsBuckets
[2 * i
] + fLostEventsBuckets
[(2 * i
) + 1];
691 Arrays
.fill(fBuckets
, fNbBuckets
/ 2, fNbBuckets
, null);
692 Arrays
.fill(fLostEventsBuckets
, fNbBuckets
/ 2, fNbBuckets
, 0);
693 fBucketDuration
*= 2;
695 fLastBucket
= (fNbBuckets
/ 2) - 1;
698 private void moveBuckets(int offset
) {
699 for (int i
= fNbBuckets
- 1; i
>= offset
; i
--) {
700 fBuckets
[i
] = new HistogramBucket(fBuckets
[i
- offset
]);
701 fLostEventsBuckets
[i
] = fLostEventsBuckets
[i
- offset
];
704 for (int i
= 0; i
< offset
; i
++) {
706 fLostEventsBuckets
[i
] = 0;
710 private int getOffset(long timestamp
) {
711 int offset
= (int) ((fFirstBucketTime
- timestamp
) / fBucketDuration
);
712 if (((fFirstBucketTime
- timestamp
) % fBucketDuration
) != 0) {