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 | |
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.ctf.core; |
a3fc8213 | 15 | |
f357bcd4 AM |
16 | import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException; |
17 | import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader; | |
18 | import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader; | |
2bdf0193 AM |
19 | import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator; |
20 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; | |
21 | import org.eclipse.tracecompass.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() { |
d84419e1 | 130 | final CTFStreamInputReader 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 | ||
962fb72f PT |
142 | /** |
143 | * Return the current timestamp location pointed to by the iterator. | |
144 | * This is the timestamp for use in CtfLocation, not the event timestamp. | |
145 | * | |
146 | * @return long The current timestamp location | |
147 | */ | |
148 | public synchronized long getCurrentTimestamp() { | |
149 | final CTFStreamInputReader top = super.getPrio().peek(); | |
150 | if (top != null) { | |
151 | long ts = top.getCurrentEvent().getTimestamp(); | |
152 | return fTrace.getCTFTrace().timestampCyclesToNanos(ts); | |
153 | } | |
154 | return 0; | |
155 | } | |
156 | ||
b1baa808 | 157 | /** |
132a02b0 MK |
158 | * Seek this iterator to a given location. |
159 | * | |
160 | * @param ctfLocationData | |
161 | * The LocationData representing the position to seek to | |
db8e8f7d AM |
162 | * @return boolean True if the seek was successful, false if there was an |
163 | * error seeking. | |
132a02b0 | 164 | * @since 2.0 |
b1baa808 | 165 | */ |
5b020488 | 166 | public synchronized boolean seek(CtfLocationInfo ctfLocationData) { |
a3fc8213 | 167 | boolean ret = false; |
132a02b0 | 168 | |
92d542eb | 169 | /* Avoid the cost of seeking at the current location. */ |
5b020488 | 170 | if (fCurLocation.getLocationInfo().equals(ctfLocationData)) { |
92d542eb EB |
171 | return super.hasMoreEvents(); |
172 | } | |
173 | ||
132a02b0 MK |
174 | /* Adjust the timestamp depending on the trace's offset */ |
175 | long currTimestamp = ctfLocationData.getTimestamp(); | |
77ef700d | 176 | final long offsetTimestamp = this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp); |
db8e8f7d AM |
177 | try { |
178 | if (offsetTimestamp < 0) { | |
179 | ret = super.seek(0L); | |
180 | } else { | |
181 | ret = super.seek(offsetTimestamp); | |
182 | } | |
183 | } catch (CTFReaderException e) { | |
91e7f946 | 184 | Activator.getDefault().logError(e.getMessage(), e); |
db8e8f7d | 185 | return false; |
57c073c5 | 186 | } |
132a02b0 MK |
187 | /* |
188 | * Check if there is already one or more events for that timestamp, and | |
189 | * assign the location index correctly | |
190 | */ | |
132a02b0 | 191 | long index = 0; |
b6220b93 MK |
192 | final CtfTmfEvent currentEvent = this.getCurrentEvent(); |
193 | if (currentEvent != null) { | |
194 | currTimestamp = currentEvent.getTimestamp().getValue(); | |
77ef700d MK |
195 | |
196 | for (long i = 0; i < ctfLocationData.getIndex(); i++) { | |
b6220b93 | 197 | if (currTimestamp == currentEvent.getTimestamp().getValue()) { |
77ef700d MK |
198 | index++; |
199 | } else { | |
200 | index = 0; | |
201 | } | |
202 | this.advance(); | |
132a02b0 | 203 | } |
77ef700d | 204 | } else { |
ecb12461 | 205 | ret = false; |
132a02b0 | 206 | } |
132a02b0 | 207 | /* Seek the current location accordingly */ |
57c073c5 | 208 | if (ret) { |
5b020488 | 209 | fCurLocation = new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index)); |
f474d36b | 210 | } else { |
5b020488 | 211 | fCurLocation = NULL_LOCATION; |
57c073c5 | 212 | } |
ecb12461 | 213 | |
ce2388e0 FC |
214 | return ret; |
215 | } | |
216 | ||
5b020488 AM |
217 | // ------------------------------------------------------------------------ |
218 | // CTFTraceReader | |
219 | // ------------------------------------------------------------------------ | |
a3fc8213 AM |
220 | |
221 | @Override | |
5b020488 AM |
222 | public boolean seek(long timestamp) { |
223 | return seek(new CtfLocationInfo(timestamp, 0)); | |
a3fc8213 AM |
224 | } |
225 | ||
a3fc8213 | 226 | @Override |
5b020488 | 227 | public synchronized boolean advance() { |
5b020488 | 228 | boolean ret = false; |
db8e8f7d | 229 | try { |
5b020488 | 230 | ret = super.advance(); |
db8e8f7d | 231 | } catch (CTFReaderException e) { |
91e7f946 | 232 | Activator.getDefault().logError(e.getMessage(), e); |
db8e8f7d | 233 | } |
a3fc8213 | 234 | |
5b020488 | 235 | if (ret) { |
962fb72f PT |
236 | long timestamp = fCurLocation.getLocationInfo().getTimestamp(); |
237 | final long timestampValue = getCurrentTimestamp(); | |
5b020488 | 238 | if (timestamp == timestampValue) { |
962fb72f | 239 | long index = fCurLocation.getLocationInfo().getIndex(); |
5b020488 AM |
240 | fCurLocation = new CtfLocation(timestampValue, index + 1); |
241 | } else { | |
242 | fCurLocation = new CtfLocation(timestampValue, 0L); | |
243 | } | |
244 | } else { | |
245 | fCurLocation = NULL_LOCATION; | |
246 | } | |
247 | return ret; | |
a3fc8213 AM |
248 | } |
249 | ||
5b020488 AM |
250 | // ------------------------------------------------------------------------ |
251 | // ITmfContext | |
252 | // ------------------------------------------------------------------------ | |
253 | ||
a3fc8213 | 254 | @Override |
5b020488 AM |
255 | public long getRank() { |
256 | return fCurRank; | |
a3fc8213 AM |
257 | } |
258 | ||
259 | @Override | |
5b020488 AM |
260 | public void setRank(long rank) { |
261 | fCurRank = rank; | |
a3fc8213 AM |
262 | } |
263 | ||
264 | @Override | |
cbdacf03 | 265 | public void increaseRank() { |
4a110860 | 266 | /* Only increase the rank if it's valid */ |
ecb12461 | 267 | if (hasValidRank()) { |
5b020488 | 268 | fCurRank++; |
4a110860 | 269 | } |
a3fc8213 AM |
270 | } |
271 | ||
272 | @Override | |
cbdacf03 | 273 | public boolean hasValidRank() { |
bcbea6a6 | 274 | return (getRank() >= 0); |
a3fc8213 AM |
275 | } |
276 | ||
c4767854 AM |
277 | /** |
278 | * @since 3.0 | |
279 | */ | |
a3fc8213 | 280 | @Override |
5b020488 AM |
281 | public void setLocation(ITmfLocation location) { |
282 | // FIXME alex: isn't there a cleaner way than a cast here? | |
283 | fCurLocation = (CtfLocation) location; | |
284 | seek(((CtfLocation) location).getLocationInfo()); | |
285 | } | |
132a02b0 | 286 | |
5b020488 AM |
287 | @Override |
288 | public CtfLocation getLocation() { | |
289 | return fCurLocation; | |
a3fc8213 AM |
290 | } |
291 | ||
5b020488 AM |
292 | // ------------------------------------------------------------------------ |
293 | // Comparable | |
294 | // ------------------------------------------------------------------------ | |
295 | ||
a3fc8213 | 296 | @Override |
ce2388e0 | 297 | public int compareTo(final CtfIterator o) { |
9d819ac7 | 298 | if (getRank() < o.getRank()) { |
a3fc8213 | 299 | return -1; |
9d819ac7 | 300 | } else if (getRank() > o.getRank()) { |
a3fc8213 | 301 | return 1; |
57c073c5 | 302 | } |
a3fc8213 AM |
303 | return 0; |
304 | } | |
788ddcbc | 305 | |
5b020488 AM |
306 | // ------------------------------------------------------------------------ |
307 | // Object | |
308 | // ------------------------------------------------------------------------ | |
309 | ||
b1baa808 MK |
310 | @Override |
311 | public int hashCode() { | |
312 | final int prime = 31; | |
313 | int result = super.hashCode(); | |
314 | result = (prime * result) | |
5b020488 | 315 | + ((fTrace == null) ? 0 : fTrace.hashCode()); |
b1baa808 | 316 | result = (prime * result) |
5b020488 AM |
317 | + ((fCurLocation == null) ? 0 : fCurLocation.hashCode()); |
318 | result = (prime * result) + (int) (fCurRank ^ (fCurRank >>> 32)); | |
b1baa808 MK |
319 | return result; |
320 | } | |
a3fc8213 | 321 | |
b1baa808 MK |
322 | @Override |
323 | public boolean equals(Object obj) { | |
324 | if (this == obj) { | |
325 | return true; | |
326 | } | |
327 | if (!super.equals(obj)) { | |
328 | return false; | |
329 | } | |
330 | if (!(obj instanceof CtfIterator)) { | |
331 | return false; | |
332 | } | |
333 | CtfIterator other = (CtfIterator) obj; | |
5b020488 AM |
334 | if (fTrace == null) { |
335 | if (other.fTrace != null) { | |
b1baa808 MK |
336 | return false; |
337 | } | |
5b020488 | 338 | } else if (!fTrace.equals(other.fTrace)) { |
b1baa808 MK |
339 | return false; |
340 | } | |
5b020488 AM |
341 | if (fCurLocation == null) { |
342 | if (other.fCurLocation != null) { | |
b1baa808 MK |
343 | return false; |
344 | } | |
5b020488 | 345 | } else if (!fCurLocation.equals(other.fCurLocation)) { |
b1baa808 MK |
346 | return false; |
347 | } | |
5b020488 | 348 | if (fCurRank != other.fCurRank) { |
b1baa808 MK |
349 | return false; |
350 | } | |
351 | return true; | |
352 | } | |
ce2388e0 | 353 | } |