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