tmf: Use tabs in statistics view for each traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / internal / lttng / ui / views / latency / model / LatencyGraphModel.java
1 /*******************************************************************************
2 * Copyright (c) 2011 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.lttng.ui.views.latency.model;
14
15 import java.util.Arrays;
16 import java.util.concurrent.locks.ReentrantLock;
17
18 import org.eclipse.core.runtime.ListenerList;
19 import org.eclipse.linuxtools.internal.lttng.ui.views.distribution.model.DistributionData;
20 import org.eclipse.linuxtools.internal.lttng.ui.views.distribution.model.HorDistributionData;
21 import org.eclipse.linuxtools.internal.lttng.ui.views.distribution.model.VerDistributionData;
22
23 /**
24 * <b><u>LatencyGraphModel</u></b>
25 * <p>
26 */
27 public class LatencyGraphModel implements IGraphDataModel {
28
29 // ------------------------------------------------------------------------
30 // Attributes
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;
37
38 // private listener lists
39 private final ListenerList fModelListeners;
40
41 private final ReentrantLock fLock;
42
43 // ------------------------------------------------------------------------
44 // Constructors
45 // ------------------------------------------------------------------------
46 public LatencyGraphModel() {
47 this(Config.DEFAULT_NUMBER_OF_BUCKETS);
48 }
49
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;
56
57 fModelListeners = new ListenerList();
58 fLock = new ReentrantLock();
59 clear();
60 }
61
62 // ------------------------------------------------------------------------
63 // Accessors
64 // ------------------------------------------------------------------------
65
66 public int getNbBuckets() {
67 return fNbBuckets;
68 }
69
70 public long getHorBucketDuration() {
71 fLock.lock();
72 try {
73 return fHorDistributionData.getBucketDuration();
74 } finally {
75 fLock.unlock();
76 }
77 }
78
79 public long getVerBucketDuration() {
80 fLock.lock();
81 try {
82 return fVerDistributionData.getBucketDuration();
83 } finally {
84 fLock.unlock();
85 }
86 }
87
88 public long getHorFirstBucketTime() {
89 fLock.lock();
90 try {
91 return fHorDistributionData.getFirstBucketTime();
92 } finally {
93 fLock.unlock();
94 }
95 }
96
97 public long getVerFirstBucketTime() {
98 fLock.lock();
99 try {
100 return fVerDistributionData.getFirstBucketTime();
101 } finally {
102 fLock.unlock();
103 }
104 }
105
106 public long getHorFirstEventTime() {
107 fLock.lock();
108 try {
109 return fHorDistributionData.getFirstEventTime();
110 } finally {
111 fLock.unlock();
112 }
113 }
114
115 public long getVerFirstEventTime() {
116 fLock.lock();
117 try {
118 return fVerDistributionData.getFirstEventTime();
119 } finally {
120 fLock.unlock();
121 }
122 }
123
124 public long getHorLastEventTime() {
125 fLock.lock();
126 try {
127 return fHorDistributionData.getLastEventTime();
128 } finally {
129 fLock.unlock();
130 }
131 }
132
133 public long getVerLastEventTime() {
134 fLock.lock();
135 try {
136 return fVerDistributionData.getLastEventTime();
137 } finally {
138 fLock.unlock();
139 }
140 }
141
142 public long getHorTimeLimit() {
143 fLock.lock();
144 try {
145 return fHorDistributionData.getTimeLimit();
146 } finally {
147 fLock.unlock();
148 }
149 }
150
151 public long getVerTimeLimit() {
152 fLock.lock();
153 try {
154 return fVerDistributionData.getTimeLimit();
155 } finally {
156 fLock.unlock();
157 }
158 }
159
160 public int getHorLastBucket() {
161 fLock.lock();
162 try {
163 return fHorDistributionData.getLastBucket();
164 } finally {
165 fLock.unlock();
166 }
167 }
168
169 public int getVerLastBucket() {
170 fLock.lock();
171 try {
172 return fVerDistributionData.getLastBucket();
173 } finally {
174 fLock.unlock();
175 }
176 }
177
178 public long getCurrentEventTime() {
179 fLock.lock();
180 try {
181 return fCurrentEventTime;
182 } finally {
183 fLock.unlock();
184 }
185 }
186
187 // ------------------------------------------------------------------------
188 // Listener interface
189 // ------------------------------------------------------------------------
190 public void addGraphModelListener(IGraphModelListener listener) {
191 fModelListeners.add(listener);
192 }
193
194 public void removeGraphModelListener(IGraphModelListener listener) {
195 fModelListeners.remove(listener);
196 }
197
198 // ------------------------------------------------------------------------
199 // Operations
200 // ------------------------------------------------------------------------
201 /*
202 * (non-Javadoc)
203 * @see org.eclipse.linuxtools.lttng.ui.views.distribution.model.IBaseDataModel#clear()
204 */
205 @Override
206 public void clear() {
207 fLock.lock();
208 try {
209 for (int[] row : fBuckets) {
210 Arrays.fill(row, 0, fNbBuckets, 0);
211 }
212 fHorDistributionData.clear();
213 fVerDistributionData.clear();
214 } finally {
215 fLock.unlock();
216 }
217 fireModelUpdateNotification();
218 }
219
220 /*
221 * (non-Javadoc)
222 * @see org.eclipse.linuxtools.lttng.ui.views.latency.model.IGraphDataModel#countEvent(int, long, long)
223 */
224 @Override
225 public void countEvent(int eventCount, long timestamp, long time) {
226 fLock.lock();
227 try {
228 int horIndex = fHorDistributionData.countEvent(timestamp);
229 int verIndex = fVerDistributionData.countEvent(time);
230
231 fBuckets[horIndex][verIndex]++;
232 } finally {
233 fLock.unlock();
234 }
235
236 fireModelUpdateNotification(eventCount);
237 }
238
239 /*
240 * (non-Javadoc)
241 * @see org.eclipse.linuxtools.lttng.ui.views.latency.model.IGraphDataModel#scaleTo(int, int, int)
242 */
243 @Override
244 public GraphScaledData scaleTo(int width, int height, int barWidth) {
245 GraphScaledData scaledData = new GraphScaledData(width, height, barWidth);
246 fLock.lock();
247 try {
248 if (!fHorDistributionData.isFirst() && !fVerDistributionData.isFirst() ) {
249
250 // Basic validation
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$
253
254 // Scale horizontally
255 int nbBars = width / barWidth;
256 int bucketsPerBar = fHorDistributionData.getLastBucket() / nbBars + 1;
257
258 int horData[][] = new int[nbBars][fNbBuckets];
259 for (int y = 0; y < fNbBuckets; y++) {
260 for (int i = 0; i < nbBars; i++) {
261 int count = 0;
262 for (int j = i * bucketsPerBar; j < (i + 1) * bucketsPerBar; j++) {
263 if (fNbBuckets <= j)
264 break;
265 count += fBuckets[j][y];
266 }
267 horData[i][y] = count;
268 }
269 }
270
271 // Scale vertically
272 int nbVerBars = height / barWidth;
273 int bucketsPerVerBar = fVerDistributionData.getLastBucket() / nbVerBars + 1;
274
275 int verData[][] = new int[nbBars][nbVerBars];
276 for (int x = 0; x < nbBars; x++) {
277 for (int i = 0; i < nbVerBars; i++) {
278 int count = 0;
279 for (int j = i * bucketsPerVerBar; j < (i + 1) * bucketsPerVerBar; j++) {
280 if (fNbBuckets <= j)
281 break;
282 count += horData[x][j];
283 }
284 verData[x][i] = count;
285 }
286 }
287
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);
300 }
301 } finally {
302 fLock.unlock();
303 }
304
305 return scaledData;
306 }
307
308 /*
309 * (non-Javadoc)
310 * @see org.eclipse.linuxtools.lttng.ui.views.distribution.model.IBaseDataModel#complete()
311 */
312 @Override
313 public void complete() {
314 fireModelUpdateNotification();
315 }
316
317 /**
318 * Sets the current event time but don't notify listeners.
319 *
320 * @param timestamp
321 */
322 public void setCurrentEvent(long timestamp) {
323 fLock.lock();
324 try {
325 fCurrentEventTime = timestamp;
326 } finally {
327 fLock.unlock();
328 }
329 }
330
331 /**
332 * Sets the current event time and notify listeners.
333 *
334 * @param timestamp
335 */
336 public void setCurrentEventNotifyListeners(long timestamp) {
337 fLock.lock();
338 try {
339 fCurrentEventTime = timestamp;
340 } finally {
341 fLock.unlock();
342 }
343 fireCurrentEventUpdateNotification();
344 }
345
346 // ------------------------------------------------------------------------
347 // Helper functions
348 // ------------------------------------------------------------------------
349
350 /*
351 * Notify listeners immediately
352 */
353 private void fireModelUpdateNotification() {
354 fireModelUpdateNotification(0);
355 }
356
357 /*
358 * Notify listeners with certain refresh rate.
359 */
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();
366 }
367 }
368 }
369
370 /*
371 * Notify listeners immediately
372 */
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);
378 }
379 }
380 }
This page took 0.049294 seconds and 5 git commands to generate.