Commit | Line | Data |
---|---|---|
fbd124dd BH |
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 | package org.eclipse.linuxtools.lttng.ui.views.distribution.model; | |
13 | ||
14 | import org.eclipse.linuxtools.lttng.ui.views.latency.model.Config; | |
e0752744 | 15 | import org.eclipse.linuxtools.tmf.ui.views.distribution.model.BaseDistributionData; |
fbd124dd BH |
16 | |
17 | /** | |
18 | * <b><u>DistributionData</u></b> | |
19 | * <p> | |
20 | * The algorithm is based on the algorithm for the Histogram. The difference is that | |
21 | * it supports two dimensions. For more details about the model principle | |
22 | * @see org.eclipse.linuxtools.lttng.ui.views.histogram.HistogramDataModel | |
23 | * <p> | |
24 | */ | |
25 | abstract public class DistributionData extends BaseDistributionData { | |
26 | ||
27 | // ------------------------------------------------------------------------ | |
28 | // Attributes | |
29 | // ------------------------------------------------------------------------ | |
30 | ||
31 | /** | |
32 | * Flag to indicate if given timestamp is the first one to count | |
33 | */ | |
34 | protected boolean fIsFirst; | |
35 | ||
36 | /** | |
37 | * reference to fBuckets | |
38 | */ | |
39 | protected final int [][] fBuckets; | |
40 | ||
41 | /** | |
42 | * Time limit (current available longest time) | |
43 | */ | |
44 | protected long fTimeLimit; | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Constructors | |
48 | // ------------------------------------------------------------------------ | |
49 | public DistributionData(int[][] buckets) { | |
50 | this(Config.DEFAULT_NUMBER_OF_BUCKETS, buckets); | |
51 | } | |
52 | ||
53 | public DistributionData(int nbBuckets, int[][] buckets) { | |
54 | super(nbBuckets); | |
55 | fBuckets = buckets; | |
56 | clear(); | |
57 | } | |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // Accessors | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
63 | public long getTimeLimit() { | |
64 | return fTimeLimit; | |
65 | } | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // Operations | |
69 | // ------------------------------------------------------------------------ | |
70 | ||
71 | /* | |
72 | * (non-Javadoc) | |
73 | * @see org.eclipse.linuxtools.lttng.ui.views.distribution.model.BaseDistributionData#clear() | |
74 | */ | |
75 | @Override | |
76 | public void clear() { | |
77 | super.clear(); | |
78 | fIsFirst = true; | |
79 | updateEndTime(); | |
80 | } | |
81 | ||
82 | public boolean isFirst() { | |
83 | return fIsFirst; | |
84 | } | |
85 | ||
86 | public int countEvent(long timestamp) { | |
87 | ||
88 | // Set the start/end time if not already done | |
89 | if (fIsFirst) { | |
90 | fIsFirst = false; | |
91 | fFirstBucketTime = timestamp; | |
92 | fFirstEventTime = timestamp; | |
93 | ||
94 | updateEndTime(); | |
95 | } | |
96 | ||
97 | // save first event time if necessary | |
98 | if (timestamp < fFirstEventTime) { | |
99 | fFirstEventTime = timestamp; | |
100 | } | |
101 | ||
102 | // save last event time if necessary | |
103 | if (fLastEventTime < timestamp) { | |
104 | fLastEventTime = timestamp; | |
105 | } | |
106 | ||
107 | ||
108 | if (timestamp >= fFirstBucketTime) { | |
109 | // Compact as needed | |
110 | while (timestamp >= fTimeLimit) { | |
111 | mergeBuckets(); | |
112 | } | |
113 | ||
114 | } else { | |
115 | ||
116 | // Get offset for buckets adjustment | |
117 | int offset = getOffset(timestamp); | |
118 | ||
119 | // Compact as needed | |
120 | while (fLastBucket + offset >= fNbBuckets) { | |
121 | mergeBuckets(); | |
122 | offset = getOffset(timestamp); | |
123 | } | |
124 | ||
125 | // Move buckets with offset (to right) | |
126 | moveBuckets(offset); | |
127 | ||
128 | // Adjust start/end time and index | |
129 | fLastBucket = fLastBucket + offset; | |
130 | fFirstBucketTime = fFirstBucketTime - offset * fBucketDuration; | |
131 | updateEndTime(); | |
132 | } | |
133 | ||
134 | // Increment the right bucket | |
135 | int index = (int) ((timestamp - fFirstBucketTime) / fBucketDuration); | |
136 | ||
137 | if (fLastBucket < index) { | |
138 | fLastBucket = index; | |
139 | } | |
140 | ||
141 | return index; | |
142 | } | |
143 | ||
144 | // ------------------------------------------------------------------------ | |
145 | // Abstract | |
146 | // ------------------------------------------------------------------------ | |
147 | ||
148 | /** | |
149 | * Moves content of buckets with the given offset in positive direction. | |
150 | * It has to be implemented accordingly in the relevant sub-classes for | |
151 | * horizontal and vertical direction. | |
152 | * | |
153 | * @param buckets - 2-dimensional array of buckets | |
154 | * @param offset - offset to move | |
155 | */ | |
156 | abstract protected void moveBuckets(int offset); | |
157 | ||
158 | /** | |
159 | * Merges buckets if end time is exceeded. It has to be implemented | |
160 | * accordingly in the relevant sub-classes for horizontal and | |
161 | * vertical direction. | |
162 | * @param buckets | |
163 | */ | |
164 | abstract protected void mergeBuckets(); | |
165 | ||
166 | // ------------------------------------------------------------------------ | |
167 | // Helper Functions | |
168 | // ------------------------------------------------------------------------ | |
169 | protected int getOffset(long timestamp) { | |
170 | int offset = (int) ((fFirstBucketTime - timestamp) / fBucketDuration); | |
171 | if ((fFirstBucketTime - timestamp) % fBucketDuration != 0) { | |
172 | offset++; | |
173 | } | |
174 | return offset; | |
175 | } | |
176 | ||
177 | protected void updateEndTime() { | |
178 | fTimeLimit = fFirstBucketTime + fNbBuckets * fBucketDuration; | |
179 | } | |
180 | } |