|
Lines 7-13
Link Here
|
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
* |
8 |
* |
| 9 |
* Contributors: |
9 |
* Contributors: |
| 10 |
* Francois Chouinard (fchouinard@gmail.com) - Initial API and implementation |
10 |
* Francois Chouinard - Initial API and implementation |
| 11 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
| 12 |
|
12 |
|
| 13 |
package org.eclipse.linuxtools.tmf.event; |
13 |
package org.eclipse.linuxtools.tmf.event; |
|
Lines 19-38
Link Here
|
| 19 |
* <p> |
19 |
* <p> |
| 20 |
* It provides a generic timestamp implementation in its most basic form: |
20 |
* It provides a generic timestamp implementation in its most basic form: |
| 21 |
* <ul> |
21 |
* <ul> |
| 22 |
* <li> an unstructured integer value |
22 |
* <li>an unstructured integer value |
| 23 |
* <li> a time scale corresponding to the magnitude of the value wrt some |
23 |
* <li>a time scale corresponding to the magnitude of the value wrt some |
| 24 |
* application-specific base unit (e.g. the second) |
24 |
* application-specific base unit (e.g. the second) |
| 25 |
* <li> a precision to indicate the error on the value (useful for comparing |
25 |
* <li>a precision to indicate the error on the value (useful for comparing |
| 26 |
* timestamps in different scales). Default: 0. |
26 |
* timestamps in different scales). Default: 0. |
| 27 |
* </ul> |
27 |
* </ul> |
| 28 |
* To allow synchronization of timestamps from different reference clocks, |
28 |
* To allow synchronization of timestamps from different reference clocks, there |
| 29 |
* there is a possibility to "adjust" the timestamp both by changing its |
29 |
* is a possibility to "adjust" the timestamp both by changing its scale (traces |
| 30 |
* scale (traces of different scale) and by adding an offset to its value |
30 |
* of different scale) and by adding an offset to its value (clock drift between |
| 31 |
* (clock drift between traces). |
31 |
* traces). |
| 32 |
* <p> |
32 |
* <p> |
| 33 |
* Note that the adjusted timestamp value could be negative e.g. for events |
33 |
* Note that the adjusted timestamp value could be negative e.g. for events that |
| 34 |
* that occurred before t0 of the reference clock. |
34 |
* occurred before t0 of the reference clock. |
| 35 |
* |
35 |
* |
| 36 |
*/ |
36 |
*/ |
| 37 |
public class TmfTimestamp { |
37 |
public class TmfTimestamp { |
| 38 |
|
38 |
|
|
Lines 40-55
Link Here
|
| 40 |
// Attributes |
40 |
// Attributes |
| 41 |
// ======================================================================== |
41 |
// ======================================================================== |
| 42 |
|
42 |
|
| 43 |
private final long fValue; // The timestamp value |
43 |
protected final long fValue; // The timestamp value |
| 44 |
private final byte fScale; // The time scale |
44 |
protected final byte fScale; // The time scale |
| 45 |
private final long fPrecision; // The value precision (tolerance) |
45 |
protected final long fPrecision; // The value precision (tolerance) |
| 46 |
|
46 |
|
| 47 |
// ======================================================================== |
47 |
// ======================================================================== |
| 48 |
// Constants |
48 |
// Constants |
| 49 |
// ======================================================================== |
49 |
// ======================================================================== |
| 50 |
|
50 |
|
| 51 |
// The beginning and end of time |
51 |
// The beginning and end of time |
| 52 |
public static final TmfTimestamp BigBang = new TmfTimestamp(0, (byte) 0, 0); |
52 |
public static final TmfTimestamp BigBang = new TmfTimestamp(0, (byte) 0, 0); |
| 53 |
public static final TmfTimestamp BigCrunch = new TmfTimestamp(Long.MAX_VALUE, Byte.MAX_VALUE, 0); |
53 |
public static final TmfTimestamp BigCrunch = new TmfTimestamp(Long.MAX_VALUE, Byte.MAX_VALUE, 0); |
| 54 |
|
54 |
|
| 55 |
// ======================================================================== |
55 |
// ======================================================================== |
|
Lines 57-73
Link Here
|
| 57 |
// ======================================================================== |
57 |
// ======================================================================== |
| 58 |
|
58 |
|
| 59 |
/** |
59 |
/** |
| 60 |
* Default constructor. |
60 |
* Default constructor. |
| 61 |
*/ |
61 |
*/ |
| 62 |
public TmfTimestamp() { |
62 |
public TmfTimestamp() { |
| 63 |
this(0, (byte) 0, 0); |
63 |
this(0, (byte) 0, 0); |
| 64 |
} |
64 |
} |
| 65 |
|
65 |
|
| 66 |
/** |
66 |
/** |
|
|
67 |
* Simple constructor. |
| 68 |
*/ |
| 69 |
public TmfTimestamp(long value) { |
| 70 |
this(value, (byte) 0, 0); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 67 |
* Simple constructor with default error value |
74 |
* Simple constructor with default error value |
| 68 |
* |
75 |
* |
| 69 |
* @param value - the original time value |
76 |
* @param value |
| 70 |
* @param scale - the time scale |
77 |
* @param scale |
| 71 |
*/ |
78 |
*/ |
| 72 |
public TmfTimestamp(long value, byte scale) { |
79 |
public TmfTimestamp(long value, byte scale) { |
| 73 |
this(value, scale, 0); |
80 |
this(value, scale, 0); |
|
Lines 76-84
Link Here
|
| 76 |
/** |
83 |
/** |
| 77 |
* Constructor with measurement error. |
84 |
* Constructor with measurement error. |
| 78 |
* |
85 |
* |
| 79 |
* @param value - the time value |
86 |
* @param value |
| 80 |
* @param scale - the time scale |
87 |
* @param scale |
| 81 |
* @param precision - the value precision (tolerance) |
88 |
* @param precision |
| 82 |
*/ |
89 |
*/ |
| 83 |
public TmfTimestamp(long value, byte scale, long precision) { |
90 |
public TmfTimestamp(long value, byte scale, long precision) { |
| 84 |
fValue = value; |
91 |
fValue = value; |
|
Lines 89-95
Link Here
|
| 89 |
/** |
96 |
/** |
| 90 |
* Copy constructor. |
97 |
* Copy constructor. |
| 91 |
* |
98 |
* |
| 92 |
* @param other - the timestamp to clone |
99 |
* @param other |
| 93 |
*/ |
100 |
*/ |
| 94 |
public TmfTimestamp(TmfTimestamp other) { |
101 |
public TmfTimestamp(TmfTimestamp other) { |
| 95 |
this(other.fValue, other.fScale, other.fPrecision); |
102 |
this(other.fValue, other.fScale, other.fPrecision); |
|
Lines 124-134
Link Here
|
| 124 |
// Operators |
131 |
// Operators |
| 125 |
// ======================================================================== |
132 |
// ======================================================================== |
| 126 |
|
133 |
|
| 127 |
@Override |
|
|
| 128 |
public String toString() { |
| 129 |
return "[TmfTimestamp:" + fValue + "," + fScale + "," + fPrecision + "]"; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
134 |
/** |
| 133 |
* Return a shifted and scaled timestamp. |
135 |
* Return a shifted and scaled timestamp. |
| 134 |
* |
136 |
* |
|
Lines 137-145
Link Here
|
| 137 |
* meaning beyond that scale difference and it's not even worth the trouble |
139 |
* meaning beyond that scale difference and it's not even worth the trouble |
| 138 |
* to switch to BigDecimal arithmetics. |
140 |
* to switch to BigDecimal arithmetics. |
| 139 |
* |
141 |
* |
| 140 |
* @param offset - the shift value (in the same scale as newScale) |
142 |
* @param offset |
| 141 |
* @param newScale - the new scale |
143 |
* - the shift value (in the same scale as newScale) |
| 142 |
* @return The synchronized timestamp |
144 |
* @param newScale |
|
|
145 |
* - the new scale |
| 146 |
* @return The synchronized timestamp |
| 143 |
*/ |
147 |
*/ |
| 144 |
|
148 |
|
| 145 |
/* |
149 |
/* |
|
Lines 150-155
Link Here
|
| 150 |
* difference in scale exceeds that value. |
154 |
* difference in scale exceeds that value. |
| 151 |
*/ |
155 |
*/ |
| 152 |
private static int MAX_SCALING = 19; |
156 |
private static int MAX_SCALING = 19; |
|
|
157 |
|
| 153 |
public TmfTimestamp synchronize(long offset, byte newScale) throws ArithmeticException { |
158 |
public TmfTimestamp synchronize(long offset, byte newScale) throws ArithmeticException { |
| 154 |
long newValue = fValue; |
159 |
long newValue = fValue; |
| 155 |
long newPrecision = fPrecision; |
160 |
long newPrecision = fPrecision; |
|
Lines 161-168
Link Here
|
| 161 |
if (scaleDiff > MAX_SCALING) { |
166 |
if (scaleDiff > MAX_SCALING) { |
| 162 |
throw new ArithmeticException("Scaling exception"); |
167 |
throw new ArithmeticException("Scaling exception"); |
| 163 |
} |
168 |
} |
|
|
169 |
// Not pretty... |
| 164 |
long scalingFactor = 1; |
170 |
long scalingFactor = 1; |
| 165 |
for (int i = 0; i < Math.abs(fScale - newScale); i++) { |
171 |
for (int i = 0; i < scaleDiff; i++) { |
| 166 |
scalingFactor *= 10; |
172 |
scalingFactor *= 10; |
| 167 |
} |
173 |
} |
| 168 |
if (newScale < fScale) { |
174 |
if (newScale < fScale) { |
|
Lines 179-187
Link Here
|
| 179 |
|
185 |
|
| 180 |
/** |
186 |
/** |
| 181 |
* Compute the adjustment, in the reference scale, needed to synchronize |
187 |
* Compute the adjustment, in the reference scale, needed to synchronize |
| 182 |
* this timestamp with a reference timestamp. |
188 |
* this timestamp with a reference timestamp. |
| 183 |
* |
189 |
* |
| 184 |
* @param reference - the reference timestamp to synchronize with |
190 |
* @param reference |
|
|
191 |
* - the reference timestamp to synchronize with |
| 185 |
* @return The adjustment term in the reference time scale |
192 |
* @return The adjustment term in the reference time scale |
| 186 |
* @throws TmfNumericalException |
193 |
* @throws TmfNumericalException |
| 187 |
*/ |
194 |
*/ |
|
Lines 193-203
Link Here
|
| 193 |
/** |
200 |
/** |
| 194 |
* Compare with another timestamp |
201 |
* Compare with another timestamp |
| 195 |
* |
202 |
* |
| 196 |
* @param other - the otehr timestamp |
203 |
* @param other |
| 197 |
* @param withinPrecision - indicates if precision is to be take into consideration |
204 |
* - the other timestamp |
| 198 |
* @return <li> -1: this timestamp is lower |
205 |
* @param withinPrecision |
| 199 |
* <li> 0: timestamps are equal (within precision if requested) |
206 |
* - indicates if precision is to be take into consideration |
| 200 |
* <li> 1: this timestamp is higher |
207 |
* @return <li>-1: this timestamp is lower <li>0: timestamps are equal |
|
|
208 |
* (within precision if requested) <li>1: this timestamp is higher |
| 201 |
* @throws TmfNumericalException |
209 |
* @throws TmfNumericalException |
| 202 |
*/ |
210 |
*/ |
| 203 |
public int compareTo(final TmfTimestamp other, boolean withinPrecision) { |
211 |
public int compareTo(final TmfTimestamp other, boolean withinPrecision) { |
|
Lines 211-218
Link Here
|
| 211 |
return 1; |
219 |
return 1; |
| 212 |
return 0; |
220 |
return 0; |
| 213 |
} |
221 |
} |
| 214 |
return (fValue == other.fValue) ? 0 : |
222 |
return (fValue == other.fValue) ? 0 : (fValue < other.fValue) ? -1 |
| 215 |
(fValue < other.fValue) ? -1 : 1; |
223 |
: 1; |
| 216 |
} |
224 |
} |
| 217 |
|
225 |
|
| 218 |
// If values have different time scales, adjust to the finest one and |
226 |
// If values have different time scales, adjust to the finest one and |
|
Lines 226-232
Link Here
|
| 226 |
return ts1.compareTo(ts2, withinPrecision); |
234 |
return ts1.compareTo(ts2, withinPrecision); |
| 227 |
} catch (ArithmeticException e) { |
235 |
} catch (ArithmeticException e) { |
| 228 |
if ((fValue == 0) || (other.fValue == 0)) { |
236 |
if ((fValue == 0) || (other.fValue == 0)) { |
| 229 |
return (fValue == other.fValue) ? 0 : (fValue < other.fValue) ? -1 : 1; |
237 |
return (fValue == other.fValue) ? 0 |
|
|
238 |
: (fValue < other.fValue) ? -1 : 1; |
| 230 |
} |
239 |
} |
| 231 |
if ((fValue > 0) && (other.fValue > 0)) { |
240 |
if ((fValue > 0) && (other.fValue > 0)) { |
| 232 |
return (fScale < other.fScale) ? -1 : 1; |
241 |
return (fScale < other.fScale) ? -1 : 1; |
|
Lines 238-244
Link Here
|
| 238 |
} |
247 |
} |
| 239 |
} |
248 |
} |
| 240 |
|
249 |
|
| 241 |
/* (non-Javadoc) |
250 |
/* |
|
|
251 |
* (non-Javadoc) |
| 252 |
* |
| 242 |
* @see java.lang.Object#equals(java.lang.Object) |
253 |
* @see java.lang.Object#equals(java.lang.Object) |
| 243 |
*/ |
254 |
*/ |
| 244 |
@Override |
255 |
@Override |
|
Lines 248-251
Link Here
|
| 248 |
return super.equals(other); |
259 |
return super.equals(other); |
| 249 |
} |
260 |
} |
| 250 |
|
261 |
|
|
|
262 |
/* (non-Javadoc) |
| 263 |
* @see java.lang.Object#toString() |
| 264 |
*/ |
| 265 |
@Override |
| 266 |
public String toString() { |
| 267 |
return "[TmfTimestamp:" + fValue + "," + fScale + "," + fPrecision + "]"; |
| 268 |
} |
| 269 |
|
| 251 |
} |
270 |
} |