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