1 /*******************************************************************************
2 * Copyright (c) 2011 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.latency
.model
;
15 import java
.util
.Arrays
;
16 import java
.util
.concurrent
.locks
.ReentrantLock
;
18 import org
.eclipse
.core
.runtime
.ListenerList
;
19 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.distribution
.model
.DistributionData
;
20 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.distribution
.model
.HorDistributionData
;
21 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.distribution
.model
.VerDistributionData
;
24 * <b><u>LatencyGraphModel</u></b>
27 public class LatencyGraphModel
implements IGraphDataModel
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
32 private final int fNbBuckets
;
33 private final int [][] fBuckets
;
34 private final DistributionData fHorDistributionData
;
35 private final DistributionData fVerDistributionData
;
36 private long fCurrentEventTime
;
38 // private listener lists
39 private final ListenerList fModelListeners
;
41 private final ReentrantLock fLock
;
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
46 public LatencyGraphModel() {
47 this(Config
.DEFAULT_NUMBER_OF_BUCKETS
);
50 public LatencyGraphModel(int nbBuckets
) {
51 fNbBuckets
= nbBuckets
;
52 fBuckets
= new int[nbBuckets
][nbBuckets
];
53 fHorDistributionData
= new HorDistributionData(nbBuckets
, fBuckets
);
54 fVerDistributionData
= new VerDistributionData(nbBuckets
, fBuckets
);
55 fCurrentEventTime
= Config
.INVALID_EVENT_TIME
;
57 fModelListeners
= new ListenerList();
58 fLock
= new ReentrantLock();
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 public int getNbBuckets() {
70 public long getHorBucketDuration() {
73 return fHorDistributionData
.getBucketDuration();
79 public long getVerBucketDuration() {
82 return fVerDistributionData
.getBucketDuration();
88 public long getHorFirstBucketTime() {
91 return fHorDistributionData
.getFirstBucketTime();
97 public long getVerFirstBucketTime() {
100 return fVerDistributionData
.getFirstBucketTime();
106 public long getHorFirstEventTime() {
109 return fHorDistributionData
.getFirstEventTime();
115 public long getVerFirstEventTime() {
118 return fVerDistributionData
.getFirstEventTime();
124 public long getHorLastEventTime() {
127 return fHorDistributionData
.getLastEventTime();
133 public long getVerLastEventTime() {
136 return fVerDistributionData
.getLastEventTime();
142 public long getHorTimeLimit() {
145 return fHorDistributionData
.getTimeLimit();
151 public long getVerTimeLimit() {
154 return fVerDistributionData
.getTimeLimit();
160 public int getHorLastBucket() {
163 return fHorDistributionData
.getLastBucket();
169 public int getVerLastBucket() {
172 return fVerDistributionData
.getLastBucket();
178 public long getCurrentEventTime() {
181 return fCurrentEventTime
;
187 // ------------------------------------------------------------------------
188 // Listener interface
189 // ------------------------------------------------------------------------
190 public void addGraphModelListener(IGraphModelListener listener
) {
191 fModelListeners
.add(listener
);
194 public void removeGraphModelListener(IGraphModelListener listener
) {
195 fModelListeners
.remove(listener
);
198 // ------------------------------------------------------------------------
200 // ------------------------------------------------------------------------
203 * @see org.eclipse.linuxtools.lttng.ui.views.distribution.model.IBaseDataModel#clear()
206 public void clear() {
209 for (int[] row
: fBuckets
) {
210 Arrays
.fill(row
, 0, fNbBuckets
, 0);
212 fHorDistributionData
.clear();
213 fVerDistributionData
.clear();
217 fireModelUpdateNotification();
222 * @see org.eclipse.linuxtools.lttng.ui.views.latency.model.IGraphDataModel#countEvent(int, long, long)
225 public void countEvent(int eventCount
, long timestamp
, long time
) {
228 int horIndex
= fHorDistributionData
.countEvent(timestamp
);
229 int verIndex
= fVerDistributionData
.countEvent(time
);
231 fBuckets
[horIndex
][verIndex
]++;
236 fireModelUpdateNotification(eventCount
);
241 * @see org.eclipse.linuxtools.lttng.ui.views.latency.model.IGraphDataModel#scaleTo(int, int, int)
244 public GraphScaledData
scaleTo(int width
, int height
, int barWidth
) {
245 GraphScaledData scaledData
= new GraphScaledData(width
, height
, barWidth
);
248 if (!fHorDistributionData
.isFirst() && !fVerDistributionData
.isFirst() ) {
251 if (width
<= 0 || height
<= 0 || barWidth
<= 0)
252 throw new AssertionError("Invalid histogram dimensions (" + width
+ "x" + height
+ ", barWidth=" + barWidth
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
254 // Scale horizontally
255 int nbBars
= width
/ barWidth
;
256 int bucketsPerBar
= fHorDistributionData
.getLastBucket() / nbBars
+ 1;
258 int horData
[][] = new int[nbBars
][fNbBuckets
];
259 for (int y
= 0; y
< fNbBuckets
; y
++) {
260 for (int i
= 0; i
< nbBars
; i
++) {
262 for (int j
= i
* bucketsPerBar
; j
< (i
+ 1) * bucketsPerBar
; j
++) {
265 count
+= fBuckets
[j
][y
];
267 horData
[i
][y
] = count
;
272 int nbVerBars
= height
/ barWidth
;
273 int bucketsPerVerBar
= fVerDistributionData
.getLastBucket() / nbVerBars
+ 1;
275 int verData
[][] = new int[nbBars
][nbVerBars
];
276 for (int x
= 0; x
< nbBars
; x
++) {
277 for (int i
= 0; i
< nbVerBars
; i
++) {
279 for (int j
= i
* bucketsPerVerBar
; j
< (i
+ 1) * bucketsPerVerBar
; j
++) {
282 count
+= horData
[x
][j
];
284 verData
[x
][i
] = count
;
288 scaledData
.setData(verData
);
289 scaledData
.setHorFirstBucketTime(fHorDistributionData
.getFirstBucketTime());
290 scaledData
.setVerFirstBucketTime(fVerDistributionData
.getFirstBucketTime());
291 scaledData
.setHorFirstEventTime(fHorDistributionData
.getFirstEventTime());
292 scaledData
.setVerFirstEventTime(fVerDistributionData
.getFirstEventTime());
293 scaledData
.setHorLastEventTime(fHorDistributionData
.getLastEventTime());
294 scaledData
.setVerLastEventTime(fVerDistributionData
.getLastEventTime());
295 scaledData
.setHorBucketDuration(bucketsPerBar
* fHorDistributionData
.getBucketDuration());
296 scaledData
.setVerBucketDuration(bucketsPerVerBar
* fVerDistributionData
.getBucketDuration());
297 scaledData
.setHorLastBucket(fHorDistributionData
.getLastBucket() / bucketsPerBar
);
298 scaledData
.setVerLastBucket(fVerDistributionData
.getLastBucket() / bucketsPerVerBar
);
299 scaledData
.setCurrentEventTime(fCurrentEventTime
);
310 * @see org.eclipse.linuxtools.lttng.ui.views.distribution.model.IBaseDataModel#complete()
313 public void complete() {
314 fireModelUpdateNotification();
318 * Sets the current event time but don't notify listeners.
322 public void setCurrentEvent(long timestamp
) {
325 fCurrentEventTime
= timestamp
;
332 * Sets the current event time and notify listeners.
336 public void setCurrentEventNotifyListeners(long timestamp
) {
339 fCurrentEventTime
= timestamp
;
343 fireCurrentEventUpdateNotification();
346 // ------------------------------------------------------------------------
348 // ------------------------------------------------------------------------
351 * Notify listeners immediately
353 private void fireModelUpdateNotification() {
354 fireModelUpdateNotification(0);
358 * Notify listeners with certain refresh rate.
360 private void fireModelUpdateNotification(int count
) {
361 if (count
% Config
.POINT_BUFFER_SIZE
== 0) {
362 Object
[] listeners
= fModelListeners
.getListeners();
363 for (int i
= 0; i
< listeners
.length
; i
++) {
364 IGraphModelListener listener
= (IGraphModelListener
) listeners
[i
];
365 listener
.graphModelUpdated();
371 * Notify listeners immediately
373 private void fireCurrentEventUpdateNotification() {
374 Object
[] listeners
= fModelListeners
.getListeners();
375 for (int i
= 0; i
< listeners
.length
; i
++) {
376 IGraphModelListener listener
= (IGraphModelListener
) listeners
[i
];
377 listener
.currentEventUpdated(fCurrentEventTime
);