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 | ||
23974d8a PT |
112 | /* (non-Javadoc) |
113 | * @see org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader#seek(long) | |
114 | */ | |
115 | @Override | |
116 | public boolean seek(long timestamp) { | |
117 | return seek(new CtfLocationData(timestamp, 0)); | |
118 | } | |
119 | ||
b1baa808 | 120 | /** |
132a02b0 MK |
121 | * Seek this iterator to a given location. |
122 | * | |
123 | * @param ctfLocationData | |
124 | * The LocationData representing the position to seek to | |
b1baa808 | 125 | * @return boolean |
132a02b0 | 126 | * @since 2.0 |
b1baa808 | 127 | */ |
132a02b0 | 128 | public boolean seek(final CtfLocationData ctfLocationData) { |
a3fc8213 | 129 | boolean ret = false; |
132a02b0 MK |
130 | |
131 | /* Adjust the timestamp depending on the trace's offset */ | |
132 | long currTimestamp = ctfLocationData.getTimestamp(); | |
77ef700d | 133 | final long offsetTimestamp = this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp); |
57c073c5 | 134 | if (offsetTimestamp < 0) { |
3eaea7e6 | 135 | ret = super.seek(0L); |
57c073c5 | 136 | } else { |
ce2388e0 | 137 | ret = super.seek(offsetTimestamp); |
57c073c5 | 138 | } |
a3fc8213 | 139 | |
132a02b0 MK |
140 | /* |
141 | * Check if there is already one or more events for that timestamp, and | |
142 | * assign the location index correctly | |
143 | */ | |
132a02b0 | 144 | long index = 0; |
77ef700d | 145 | if (this.getCurrentEvent() != null) { |
58f3bc52 | 146 | currTimestamp = this.getCurrentEvent().getTimestamp().getValue(); |
77ef700d MK |
147 | |
148 | for (long i = 0; i < ctfLocationData.getIndex(); i++) { | |
58f3bc52 | 149 | if (currTimestamp == this.getCurrentEvent().getTimestamp().getValue()) { |
77ef700d MK |
150 | index++; |
151 | } else { | |
152 | index = 0; | |
153 | } | |
154 | this.advance(); | |
132a02b0 | 155 | } |
77ef700d MK |
156 | } else { |
157 | ret= false; | |
132a02b0 | 158 | } |
132a02b0 | 159 | /* Seek the current location accordingly */ |
57c073c5 | 160 | if (ret) { |
d62bb185 | 161 | curLocation = new CtfLocation(new CtfLocationData(getCurrentEvent().getTimestamp().getValue(), index)); |
f474d36b PT |
162 | } else { |
163 | curLocation = NULL_LOCATION; | |
57c073c5 | 164 | } |
ce2388e0 FC |
165 | return ret; |
166 | } | |
167 | ||
b1baa808 MK |
168 | /** |
169 | * Method getRank. | |
170 | * @return long | |
171 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank() | |
172 | */ | |
a3fc8213 AM |
173 | @Override |
174 | public long getRank() { | |
f474d36b | 175 | return curRank; |
a3fc8213 AM |
176 | } |
177 | ||
b1baa808 MK |
178 | /** |
179 | * Method setRank. | |
180 | * @param rank long | |
181 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long) | |
182 | */ | |
a3fc8213 | 183 | @Override |
ce2388e0 | 184 | public void setRank(final long rank) { |
f474d36b | 185 | curRank = rank; |
a3fc8213 AM |
186 | } |
187 | ||
188 | /* | |
189 | * (non-Javadoc) | |
190 | * | |
191 | * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone() | |
192 | */ | |
193 | @Override | |
194 | public CtfIterator clone() { | |
195 | CtfIterator clone = null; | |
5976d44a | 196 | clone = new CtfIterator(ctfTmfTrace, this.getLocation().getLocationInfo(), curRank); |
a3fc8213 AM |
197 | return clone; |
198 | } | |
199 | ||
b1baa808 MK |
200 | /** |
201 | * Method dispose. | |
202 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose() | |
203 | */ | |
a3fc8213 AM |
204 | @Override |
205 | public void dispose() { | |
5d1c6919 | 206 | super.dispose(); |
a3fc8213 AM |
207 | } |
208 | ||
b1baa808 MK |
209 | /** |
210 | * Method setLocation. | |
211 | * @param location ITmfLocation<?> | |
b1baa808 | 212 | */ |
a3fc8213 | 213 | @Override |
1e1bef82 | 214 | public void setLocation(final ITmfLocation location) { |
a3fc8213 AM |
215 | // FIXME alex: isn't there a cleaner way than a cast here? |
216 | this.curLocation = (CtfLocation) location; | |
5976d44a | 217 | seek(((CtfLocation) location).getLocationInfo()); |
a3fc8213 AM |
218 | } |
219 | ||
b1baa808 MK |
220 | /** |
221 | * Method getLocation. | |
222 | * @return CtfLocation | |
223 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation() | |
224 | */ | |
a3fc8213 AM |
225 | @Override |
226 | public CtfLocation getLocation() { | |
227 | return curLocation; | |
228 | } | |
229 | ||
b1baa808 MK |
230 | /** |
231 | * Method increaseRank. | |
232 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank() | |
233 | */ | |
a3fc8213 | 234 | @Override |
cbdacf03 | 235 | public void increaseRank() { |
4a110860 AM |
236 | /* Only increase the rank if it's valid */ |
237 | if(hasValidRank()) { | |
238 | curRank++; | |
239 | } | |
a3fc8213 AM |
240 | } |
241 | ||
b1baa808 MK |
242 | /** |
243 | * Method hasValidRank, if the iterator is valid | |
244 | * @return boolean | |
245 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank() | |
246 | */ | |
a3fc8213 | 247 | @Override |
cbdacf03 | 248 | public boolean hasValidRank() { |
bcbea6a6 | 249 | return (getRank() >= 0); |
a3fc8213 AM |
250 | } |
251 | ||
b1baa808 MK |
252 | /** |
253 | * Method advance go to the next event | |
254 | * @return boolean successful or not | |
255 | */ | |
a3fc8213 AM |
256 | @Override |
257 | public boolean advance() { | |
5976d44a FC |
258 | long index = curLocation.getLocationInfo().getIndex(); |
259 | long timestamp = curLocation.getLocationInfo().getTimestamp(); | |
f474d36b | 260 | boolean ret = super.advance(); |
132a02b0 | 261 | |
f474d36b | 262 | if (ret) { |
58f3bc52 | 263 | final long timestampValue = getCurrentEvent().getTimestamp().getValue(); |
132a02b0 | 264 | if (timestamp == timestampValue) { |
d62bb185 | 265 | curLocation = new CtfLocation(timestampValue, index + 1); |
132a02b0 | 266 | } else { |
d62bb185 | 267 | curLocation = new CtfLocation(timestampValue, 0L); |
132a02b0 | 268 | } |
f474d36b PT |
269 | } else { |
270 | curLocation = NULL_LOCATION; | |
271 | } | |
272 | return ret; | |
a3fc8213 AM |
273 | } |
274 | ||
b1baa808 MK |
275 | /** |
276 | * Method compareTo. | |
277 | * @param o CtfIterator | |
278 | * @return int -1, 0, 1 | |
279 | */ | |
a3fc8213 | 280 | @Override |
ce2388e0 | 281 | public int compareTo(final CtfIterator o) { |
57c073c5 | 282 | if (this.getRank() < o.getRank()) { |
a3fc8213 | 283 | return -1; |
57c073c5 | 284 | } else if (this.getRank() > o.getRank()) { |
a3fc8213 | 285 | return 1; |
57c073c5 | 286 | } |
a3fc8213 AM |
287 | return 0; |
288 | } | |
788ddcbc | 289 | |
b1baa808 MK |
290 | /* (non-Javadoc) |
291 | * @see java.lang.Object#hashCode() | |
292 | */ | |
293 | @Override | |
294 | public int hashCode() { | |
295 | final int prime = 31; | |
296 | int result = super.hashCode(); | |
297 | result = (prime * result) | |
298 | + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode()); | |
299 | result = (prime * result) | |
300 | + ((curLocation == null) ? 0 : curLocation.hashCode()); | |
301 | result = (prime * result) + (int) (curRank ^ (curRank >>> 32)); | |
302 | return result; | |
303 | } | |
a3fc8213 | 304 | |
b1baa808 MK |
305 | /* (non-Javadoc) |
306 | * @see java.lang.Object#equals(java.lang.Object) | |
307 | */ | |
308 | @Override | |
309 | public boolean equals(Object obj) { | |
310 | if (this == obj) { | |
311 | return true; | |
312 | } | |
313 | if (!super.equals(obj)) { | |
314 | return false; | |
315 | } | |
316 | if (!(obj instanceof CtfIterator)) { | |
317 | return false; | |
318 | } | |
319 | CtfIterator other = (CtfIterator) obj; | |
320 | if (ctfTmfTrace == null) { | |
321 | if (other.ctfTmfTrace != null) { | |
322 | return false; | |
323 | } | |
324 | } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) { | |
325 | return false; | |
326 | } | |
327 | if (curLocation == null) { | |
328 | if (other.curLocation != null) { | |
329 | return false; | |
330 | } | |
331 | } else if (!curLocation.equals(other.curLocation)) { | |
332 | return false; | |
333 | } | |
334 | if (curRank != other.curRank) { | |
335 | return false; | |
336 | } | |
337 | return true; | |
338 | } | |
ce2388e0 | 339 | } |