Commit | Line | Data |
---|---|---|
b1baa808 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * 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: Matthew Khouzam - Initial API and implementation | |
10 | *******************************************************************************/ | |
a3fc8213 AM |
11 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
12 | ||
13 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; | |
14 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; | |
15 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
16 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; | |
17 | ||
b1baa808 | 18 | /** |
d09f973b | 19 | * The CTF trace reader iterator. |
3eaea7e6 | 20 | * |
d09f973b FC |
21 | * It doesn't reserve a file handle, so many iterators can be used without worries |
22 | * of I/O errors or resource exhaustion. | |
3eaea7e6 | 23 | * |
d09f973b FC |
24 | * @version 1.0 |
25 | * @author Matthew Khouzam | |
b1baa808 | 26 | */ |
132a02b0 MK |
27 | public class CtfIterator extends CTFTraceReader implements ITmfContext, |
28 | Comparable<CtfIterator>, Cloneable { | |
a3fc8213 AM |
29 | |
30 | private final CtfTmfTrace ctfTmfTrace; | |
31 | ||
9ac2eb62 MK |
32 | /** |
33 | * An invalid location | |
34 | */ | |
132a02b0 MK |
35 | final public static CtfLocation NULL_LOCATION = new CtfLocation(CtfLocation.INVALID_LOCATION); |
36 | ||
a3fc8213 AM |
37 | private CtfLocation curLocation; |
38 | private long curRank; | |
39 | ||
40 | /** | |
41 | * Create a new CTF trace iterator, which initially points at the first | |
42 | * event in the trace. | |
43 | * | |
132a02b0 MK |
44 | * @param trace |
45 | * the trace to iterate over | |
a3fc8213 | 46 | */ |
ce2388e0 | 47 | public CtfIterator(final CtfTmfTrace trace) { |
a3fc8213 AM |
48 | super(trace.getCTFTrace()); |
49 | this.ctfTmfTrace = trace; | |
57c073c5 | 50 | if (this.hasMoreEvents()) { |
57c073c5 MK |
51 | this.curLocation = new CtfLocation(trace.getStartTime()); |
52 | this.curRank = 0; | |
53 | } else { | |
54 | setUnknownLocation(); | |
55 | } | |
a3fc8213 AM |
56 | } |
57 | ||
57c073c5 | 58 | private void setUnknownLocation() { |
f474d36b | 59 | this.curLocation = NULL_LOCATION; |
57c073c5 MK |
60 | this.curRank = UNKNOWN_RANK; |
61 | } | |
62 | ||
b1baa808 MK |
63 | /** |
64 | * Constructor for CtfIterator. | |
132a02b0 MK |
65 | * |
66 | * @param trace | |
67 | * CtfTmfTrace the trace | |
68 | * @param ctfLocationData | |
69 | * long the timestamp in ns of the trace for positioning | |
70 | * @param rank | |
71 | * long the index of the trace for positioning | |
72 | * @since 2.0 | |
b1baa808 | 73 | */ |
132a02b0 MK |
74 | public CtfIterator(final CtfTmfTrace trace, |
75 | final CtfLocationData ctfLocationData, final long rank) { | |
a3fc8213 | 76 | super(trace.getCTFTrace()); |
57c073c5 | 77 | |
a3fc8213 | 78 | this.ctfTmfTrace = trace; |
57c073c5 | 79 | if (this.hasMoreEvents()) { |
132a02b0 | 80 | this.curLocation = new CtfLocation(ctfLocationData); |
58f3bc52 | 81 | if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData.getTimestamp()) { |
132a02b0 | 82 | this.seek(ctfLocationData); |
57c073c5 MK |
83 | this.curRank = rank; |
84 | } | |
85 | } else { | |
86 | setUnknownLocation(); | |
87 | } | |
a3fc8213 | 88 | |
a3fc8213 AM |
89 | } |
90 | ||
b1baa808 MK |
91 | /** |
92 | * Method getCtfTmfTrace. gets a CtfTmfTrace | |
93 | * @return CtfTmfTrace | |
94 | */ | |
a3fc8213 AM |
95 | public CtfTmfTrace getCtfTmfTrace() { |
96 | return ctfTmfTrace; | |
97 | } | |
98 | ||
b1baa808 MK |
99 | /** |
100 | * Method getCurrentEvent. gets the current event | |
101 | * @return CtfTmfEvent | |
102 | */ | |
a3fc8213 | 103 | public CtfTmfEvent getCurrentEvent() { |
ce2388e0 | 104 | final StreamInputReader top = super.prio.peek(); |
57c073c5 MK |
105 | if (top != null) { |
106 | return new CtfTmfEvent(top.getCurrentEvent(), top.getFilename(), | |
107 | ctfTmfTrace); | |
108 | } | |
a3fc8213 AM |
109 | return null; |
110 | } | |
111 | ||
b1baa808 | 112 | /** |
132a02b0 MK |
113 | * Seek this iterator to a given location. |
114 | * | |
115 | * @param ctfLocationData | |
116 | * The LocationData representing the position to seek to | |
b1baa808 | 117 | * @return boolean |
132a02b0 | 118 | * @since 2.0 |
b1baa808 | 119 | */ |
132a02b0 | 120 | public boolean seek(final CtfLocationData ctfLocationData) { |
a3fc8213 | 121 | boolean ret = false; |
132a02b0 MK |
122 | |
123 | /* Adjust the timestamp depending on the trace's offset */ | |
124 | long currTimestamp = ctfLocationData.getTimestamp(); | |
77ef700d | 125 | final long offsetTimestamp = this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp); |
57c073c5 | 126 | if (offsetTimestamp < 0) { |
3eaea7e6 | 127 | ret = super.seek(0L); |
57c073c5 | 128 | } else { |
ce2388e0 | 129 | ret = super.seek(offsetTimestamp); |
57c073c5 | 130 | } |
a3fc8213 | 131 | |
132a02b0 MK |
132 | /* |
133 | * Check if there is already one or more events for that timestamp, and | |
134 | * assign the location index correctly | |
135 | */ | |
132a02b0 | 136 | long index = 0; |
77ef700d | 137 | if (this.getCurrentEvent() != null) { |
58f3bc52 | 138 | currTimestamp = this.getCurrentEvent().getTimestamp().getValue(); |
77ef700d MK |
139 | |
140 | for (long i = 0; i < ctfLocationData.getIndex(); i++) { | |
58f3bc52 | 141 | if (currTimestamp == this.getCurrentEvent().getTimestamp().getValue()) { |
77ef700d MK |
142 | index++; |
143 | } else { | |
144 | index = 0; | |
145 | } | |
146 | this.advance(); | |
132a02b0 | 147 | } |
77ef700d MK |
148 | } else { |
149 | ret= false; | |
132a02b0 | 150 | } |
132a02b0 | 151 | /* Seek the current location accordingly */ |
57c073c5 | 152 | if (ret) { |
58f3bc52 | 153 | curLocation.setLocation(new CtfLocationData(getCurrentEvent().getTimestamp().getValue(), index)); |
f474d36b PT |
154 | } else { |
155 | curLocation = NULL_LOCATION; | |
57c073c5 | 156 | } |
ce2388e0 FC |
157 | return ret; |
158 | } | |
159 | ||
b1baa808 MK |
160 | /** |
161 | * Method getRank. | |
162 | * @return long | |
163 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank() | |
164 | */ | |
a3fc8213 AM |
165 | @Override |
166 | public long getRank() { | |
f474d36b | 167 | return curRank; |
a3fc8213 AM |
168 | } |
169 | ||
b1baa808 MK |
170 | /** |
171 | * Method setRank. | |
172 | * @param rank long | |
173 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long) | |
174 | */ | |
a3fc8213 | 175 | @Override |
ce2388e0 | 176 | public void setRank(final long rank) { |
f474d36b | 177 | curRank = rank; |
a3fc8213 AM |
178 | } |
179 | ||
180 | /* | |
181 | * (non-Javadoc) | |
182 | * | |
183 | * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone() | |
184 | */ | |
185 | @Override | |
186 | public CtfIterator clone() { | |
187 | CtfIterator clone = null; | |
5976d44a | 188 | clone = new CtfIterator(ctfTmfTrace, this.getLocation().getLocationInfo(), curRank); |
a3fc8213 AM |
189 | return clone; |
190 | } | |
191 | ||
b1baa808 MK |
192 | /** |
193 | * Method dispose. | |
194 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose() | |
195 | */ | |
a3fc8213 AM |
196 | @Override |
197 | public void dispose() { | |
198 | // FIXME add dispose() stuff to CTFTrace and call it here... | |
199 | ||
200 | } | |
201 | ||
b1baa808 MK |
202 | /** |
203 | * Method setLocation. | |
204 | * @param location ITmfLocation<?> | |
b1baa808 | 205 | */ |
a3fc8213 | 206 | @Override |
1e1bef82 | 207 | public void setLocation(final ITmfLocation location) { |
a3fc8213 AM |
208 | // FIXME alex: isn't there a cleaner way than a cast here? |
209 | this.curLocation = (CtfLocation) location; | |
5976d44a | 210 | seek(((CtfLocation) location).getLocationInfo()); |
a3fc8213 AM |
211 | } |
212 | ||
b1baa808 MK |
213 | /** |
214 | * Method getLocation. | |
215 | * @return CtfLocation | |
216 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation() | |
217 | */ | |
a3fc8213 AM |
218 | @Override |
219 | public CtfLocation getLocation() { | |
220 | return curLocation; | |
221 | } | |
222 | ||
b1baa808 MK |
223 | /** |
224 | * Method increaseRank. | |
225 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank() | |
226 | */ | |
a3fc8213 | 227 | @Override |
cbdacf03 | 228 | public void increaseRank() { |
4a110860 AM |
229 | /* Only increase the rank if it's valid */ |
230 | if(hasValidRank()) { | |
231 | curRank++; | |
232 | } | |
a3fc8213 AM |
233 | } |
234 | ||
b1baa808 MK |
235 | /** |
236 | * Method hasValidRank, if the iterator is valid | |
237 | * @return boolean | |
238 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank() | |
239 | */ | |
a3fc8213 | 240 | @Override |
cbdacf03 | 241 | public boolean hasValidRank() { |
bcbea6a6 | 242 | return (getRank() >= 0); |
a3fc8213 AM |
243 | } |
244 | ||
b1baa808 MK |
245 | /** |
246 | * Method advance go to the next event | |
247 | * @return boolean successful or not | |
248 | */ | |
a3fc8213 AM |
249 | @Override |
250 | public boolean advance() { | |
5976d44a FC |
251 | long index = curLocation.getLocationInfo().getIndex(); |
252 | long timestamp = curLocation.getLocationInfo().getTimestamp(); | |
f474d36b | 253 | boolean ret = super.advance(); |
132a02b0 | 254 | |
f474d36b | 255 | if (ret) { |
58f3bc52 | 256 | final long timestampValue = getCurrentEvent().getTimestamp().getValue(); |
132a02b0 MK |
257 | if (timestamp == timestampValue) { |
258 | curLocation.setLocation(timestampValue, index + 1); | |
259 | } else { | |
260 | curLocation.setLocation(timestampValue, 0L); | |
261 | } | |
f474d36b PT |
262 | } else { |
263 | curLocation = NULL_LOCATION; | |
264 | } | |
265 | return ret; | |
a3fc8213 AM |
266 | } |
267 | ||
b1baa808 MK |
268 | /** |
269 | * Method compareTo. | |
270 | * @param o CtfIterator | |
271 | * @return int -1, 0, 1 | |
272 | */ | |
a3fc8213 | 273 | @Override |
ce2388e0 | 274 | public int compareTo(final CtfIterator o) { |
57c073c5 | 275 | if (this.getRank() < o.getRank()) { |
a3fc8213 | 276 | return -1; |
57c073c5 | 277 | } else if (this.getRank() > o.getRank()) { |
a3fc8213 | 278 | return 1; |
57c073c5 | 279 | } |
a3fc8213 AM |
280 | return 0; |
281 | } | |
788ddcbc | 282 | |
b1baa808 MK |
283 | /* (non-Javadoc) |
284 | * @see java.lang.Object#hashCode() | |
285 | */ | |
286 | @Override | |
287 | public int hashCode() { | |
288 | final int prime = 31; | |
289 | int result = super.hashCode(); | |
290 | result = (prime * result) | |
291 | + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode()); | |
292 | result = (prime * result) | |
293 | + ((curLocation == null) ? 0 : curLocation.hashCode()); | |
294 | result = (prime * result) + (int) (curRank ^ (curRank >>> 32)); | |
295 | return result; | |
296 | } | |
a3fc8213 | 297 | |
b1baa808 MK |
298 | /* (non-Javadoc) |
299 | * @see java.lang.Object#equals(java.lang.Object) | |
300 | */ | |
301 | @Override | |
302 | public boolean equals(Object obj) { | |
303 | if (this == obj) { | |
304 | return true; | |
305 | } | |
306 | if (!super.equals(obj)) { | |
307 | return false; | |
308 | } | |
309 | if (!(obj instanceof CtfIterator)) { | |
310 | return false; | |
311 | } | |
312 | CtfIterator other = (CtfIterator) obj; | |
313 | if (ctfTmfTrace == null) { | |
314 | if (other.ctfTmfTrace != null) { | |
315 | return false; | |
316 | } | |
317 | } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) { | |
318 | return false; | |
319 | } | |
320 | if (curLocation == null) { | |
321 | if (other.curLocation != null) { | |
322 | return false; | |
323 | } | |
324 | } else if (!curLocation.equals(other.curLocation)) { | |
325 | return false; | |
326 | } | |
327 | if (curRank != other.curRank) { | |
328 | return false; | |
329 | } | |
330 | return true; | |
331 | } | |
ce2388e0 | 332 | } |