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