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 | } | |
5b17357d PT |
186 | /* Update location to make sure the current event is updated */ |
187 | fCurLocation = new CtfLocation(ctfLocationData); | |
92d542eb | 188 | |
132a02b0 MK |
189 | /* Adjust the timestamp depending on the trace's offset */ |
190 | long currTimestamp = ctfLocationData.getTimestamp(); | |
fe71057b | 191 | final long offsetTimestamp = this.getCtfTmfTrace().timestampNanoToCycles(currTimestamp); |
db8e8f7d AM |
192 | try { |
193 | if (offsetTimestamp < 0) { | |
194 | ret = super.seek(0L); | |
195 | } else { | |
196 | ret = super.seek(offsetTimestamp); | |
197 | } | |
680f9173 | 198 | } catch (CTFException e) { |
91e7f946 | 199 | Activator.getDefault().logError(e.getMessage(), e); |
db8e8f7d | 200 | return false; |
57c073c5 | 201 | } |
132a02b0 MK |
202 | /* |
203 | * Check if there is already one or more events for that timestamp, and | |
204 | * assign the location index correctly | |
205 | */ | |
132a02b0 | 206 | long index = 0; |
b6220b93 MK |
207 | final CtfTmfEvent currentEvent = this.getCurrentEvent(); |
208 | if (currentEvent != null) { | |
209 | currTimestamp = currentEvent.getTimestamp().getValue(); | |
77ef700d MK |
210 | |
211 | for (long i = 0; i < ctfLocationData.getIndex(); i++) { | |
b6220b93 | 212 | if (currTimestamp == currentEvent.getTimestamp().getValue()) { |
77ef700d MK |
213 | index++; |
214 | } else { | |
215 | index = 0; | |
216 | } | |
217 | this.advance(); | |
132a02b0 | 218 | } |
77ef700d | 219 | } else { |
ecb12461 | 220 | ret = false; |
132a02b0 | 221 | } |
132a02b0 | 222 | /* Seek the current location accordingly */ |
57c073c5 | 223 | if (ret) { |
5b020488 | 224 | fCurLocation = new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index)); |
f474d36b | 225 | } else { |
5b020488 | 226 | fCurLocation = NULL_LOCATION; |
57c073c5 | 227 | } |
ecb12461 | 228 | |
ce2388e0 FC |
229 | return ret; |
230 | } | |
231 | ||
5b020488 AM |
232 | // ------------------------------------------------------------------------ |
233 | // CTFTraceReader | |
234 | // ------------------------------------------------------------------------ | |
a3fc8213 AM |
235 | |
236 | @Override | |
5b020488 AM |
237 | public boolean seek(long timestamp) { |
238 | return seek(new CtfLocationInfo(timestamp, 0)); | |
a3fc8213 AM |
239 | } |
240 | ||
a3fc8213 | 241 | @Override |
5b020488 | 242 | public synchronized boolean advance() { |
5b020488 | 243 | boolean ret = false; |
db8e8f7d | 244 | try { |
5b020488 | 245 | ret = super.advance(); |
680f9173 | 246 | } catch (CTFException e) { |
91e7f946 | 247 | Activator.getDefault().logError(e.getMessage(), e); |
db8e8f7d | 248 | } |
a3fc8213 | 249 | |
5b020488 | 250 | if (ret) { |
962fb72f PT |
251 | long timestamp = fCurLocation.getLocationInfo().getTimestamp(); |
252 | final long timestampValue = getCurrentTimestamp(); | |
5b020488 | 253 | if (timestamp == timestampValue) { |
962fb72f | 254 | long index = fCurLocation.getLocationInfo().getIndex(); |
5b020488 AM |
255 | fCurLocation = new CtfLocation(timestampValue, index + 1); |
256 | } else { | |
257 | fCurLocation = new CtfLocation(timestampValue, 0L); | |
258 | } | |
259 | } else { | |
260 | fCurLocation = NULL_LOCATION; | |
261 | } | |
262 | return ret; | |
a3fc8213 AM |
263 | } |
264 | ||
5b020488 AM |
265 | // ------------------------------------------------------------------------ |
266 | // ITmfContext | |
267 | // ------------------------------------------------------------------------ | |
268 | ||
a3fc8213 | 269 | @Override |
5b020488 AM |
270 | public long getRank() { |
271 | return fCurRank; | |
a3fc8213 AM |
272 | } |
273 | ||
274 | @Override | |
5b020488 AM |
275 | public void setRank(long rank) { |
276 | fCurRank = rank; | |
a3fc8213 AM |
277 | } |
278 | ||
279 | @Override | |
cbdacf03 | 280 | public void increaseRank() { |
4a110860 | 281 | /* Only increase the rank if it's valid */ |
ecb12461 | 282 | if (hasValidRank()) { |
5b020488 | 283 | fCurRank++; |
4a110860 | 284 | } |
a3fc8213 AM |
285 | } |
286 | ||
287 | @Override | |
cbdacf03 | 288 | public boolean hasValidRank() { |
bcbea6a6 | 289 | return (getRank() >= 0); |
a3fc8213 AM |
290 | } |
291 | ||
292 | @Override | |
5b020488 AM |
293 | public void setLocation(ITmfLocation location) { |
294 | // FIXME alex: isn't there a cleaner way than a cast here? | |
295 | fCurLocation = (CtfLocation) location; | |
296 | seek(((CtfLocation) location).getLocationInfo()); | |
297 | } | |
132a02b0 | 298 | |
5b020488 AM |
299 | @Override |
300 | public CtfLocation getLocation() { | |
301 | return fCurLocation; | |
a3fc8213 AM |
302 | } |
303 | ||
5b020488 AM |
304 | // ------------------------------------------------------------------------ |
305 | // Comparable | |
306 | // ------------------------------------------------------------------------ | |
307 | ||
a3fc8213 | 308 | @Override |
ce2388e0 | 309 | public int compareTo(final CtfIterator o) { |
9d819ac7 | 310 | if (getRank() < o.getRank()) { |
a3fc8213 | 311 | return -1; |
9d819ac7 | 312 | } else if (getRank() > o.getRank()) { |
a3fc8213 | 313 | return 1; |
57c073c5 | 314 | } |
a3fc8213 AM |
315 | return 0; |
316 | } | |
788ddcbc | 317 | |
5b020488 AM |
318 | // ------------------------------------------------------------------------ |
319 | // Object | |
320 | // ------------------------------------------------------------------------ | |
321 | ||
b1baa808 MK |
322 | @Override |
323 | public int hashCode() { | |
324 | final int prime = 31; | |
325 | int result = super.hashCode(); | |
326 | result = (prime * result) | |
5b020488 | 327 | + ((fTrace == null) ? 0 : fTrace.hashCode()); |
b1baa808 | 328 | result = (prime * result) |
5b020488 AM |
329 | + ((fCurLocation == null) ? 0 : fCurLocation.hashCode()); |
330 | result = (prime * result) + (int) (fCurRank ^ (fCurRank >>> 32)); | |
b1baa808 MK |
331 | return result; |
332 | } | |
a3fc8213 | 333 | |
b1baa808 MK |
334 | @Override |
335 | public boolean equals(Object obj) { | |
336 | if (this == obj) { | |
337 | return true; | |
338 | } | |
339 | if (!super.equals(obj)) { | |
340 | return false; | |
341 | } | |
342 | if (!(obj instanceof CtfIterator)) { | |
343 | return false; | |
344 | } | |
345 | CtfIterator other = (CtfIterator) obj; | |
c052c341 | 346 | if (!equalsNullable(fTrace, other.fTrace)) { |
b1baa808 MK |
347 | return false; |
348 | } | |
c052c341 | 349 | if (!equalsNullable(fCurLocation, other.fCurLocation)) { |
b1baa808 MK |
350 | return false; |
351 | } | |
5b020488 | 352 | if (fCurRank != other.fCurRank) { |
b1baa808 MK |
353 | return false; |
354 | } | |
355 | return true; | |
356 | } | |
ce2388e0 | 357 | } |