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