1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
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
10 * Matthew Khouzam - Initial API and implementation
11 * Florian Wininger - Performance improvements
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.equalsNullable
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStreamInputReader
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
23 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.Activator
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
26 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocation
;
27 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocationInfo
;
28 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
29 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
32 * The CTF trace reader iterator.
34 * It doesn't reserve a file handle, so many iterators can be used without
35 * worries of I/O errors or resource exhaustion.
37 * @author Matthew Khouzam
39 public class CtfIterator
extends CTFTraceReader
40 implements ITmfContext
, Comparable
<CtfIterator
> {
42 /** An invalid location */
43 public static final CtfLocation NULL_LOCATION
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
45 private final CtfTmfTrace fTrace
;
47 private CtfLocation fCurLocation
;
48 private long fCurRank
;
50 private CtfLocation fPreviousLocation
;
51 private CtfTmfEvent fPreviousEvent
;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * Create a new CTF trace iterator, which initially points at the first
62 * The {@link CTFTrace} linked to the trace. It should be
63 * provided by the corresponding 'ctfTmfTrace'.
66 * The {@link CtfTmfTrace} to iterate over
67 * @throws CTFException
68 * If the iterator couldn't not be instantiated, probably due to
71 public CtfIterator(CTFTrace ctfTrace
, CtfTmfTrace ctfTmfTrace
) throws CTFException
{
74 if (hasMoreEvents()) {
75 fCurLocation
= new CtfLocation(ctfTmfTrace
.getStartTime());
83 * Create a new CTF trace iterator, which will initially point to the given
87 * The {@link CTFTrace} linked to the trace. It should be
88 * provided by the corresponding 'ctfTmfTrace'.
90 * The {@link CtfTmfTrace} to iterate over
91 * @param ctfLocationData
92 * The initial timestamp the iterator will be pointing to
95 * @throws CTFException
96 * If the iterator couldn't not be instantiated, probably due to
99 public CtfIterator(CTFTrace ctfTrace
, CtfTmfTrace ctfTmfTrace
, CtfLocationInfo ctfLocationData
, long rank
)
100 throws CTFException
{
103 this.fTrace
= ctfTmfTrace
;
104 if (this.hasMoreEvents()) {
105 this.fCurLocation
= new CtfLocation(ctfLocationData
);
106 if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData
.getTimestamp()) {
107 this.seek(ctfLocationData
);
108 this.fCurRank
= rank
;
111 setUnknownLocation();
116 public void dispose() {
120 private void setUnknownLocation() {
121 fCurLocation
= NULL_LOCATION
;
122 fCurRank
= UNKNOWN_RANK
;
125 // ------------------------------------------------------------------------
127 // ------------------------------------------------------------------------
130 * Return this iterator's trace.
132 * @return CtfTmfTrace The iterator's trace
134 public CtfTmfTrace
getCtfTmfTrace() {
139 * Return the current event pointed to by the iterator.
141 * @return CtfTmfEvent The current event
143 public synchronized CtfTmfEvent
getCurrentEvent() {
144 final CTFStreamInputReader top
= super.getPrio().peek();
146 if (!fCurLocation
.equals(fPreviousLocation
)) {
147 fPreviousLocation
= fCurLocation
;
148 fPreviousEvent
= fTrace
.getEventFactory().createEvent(fTrace
, top
.getCurrentEvent(), top
.getFilename());
150 return fPreviousEvent
;
156 * Return the current timestamp location pointed to by the iterator. This is
157 * the timestamp for use in CtfLocation, not the event timestamp.
159 * @return long The current timestamp location
161 public synchronized long getCurrentTimestamp() {
162 final CTFStreamInputReader top
= super.getPrio().peek();
164 EventDefinition currentEvent
= top
.getCurrentEvent();
165 if (currentEvent
!= null) {
166 long ts
= currentEvent
.getTimestamp();
167 return fTrace
.timestampCyclesToNanos(ts
);
174 * Seek this iterator to a given location.
176 * @param ctfLocationData
177 * The LocationData representing the position to seek to
178 * @return boolean True if the seek was successful, false if there was an
181 public synchronized boolean seek(CtfLocationInfo ctfLocationData
) {
183 if (ctfLocationData
.equals(CtfLocation
.INVALID_LOCATION
)) {
184 fCurLocation
= NULL_LOCATION
;
188 /* Avoid the cost of seeking at the current location. */
189 if (fCurLocation
.getLocationInfo().equals(ctfLocationData
)) {
190 return super.hasMoreEvents();
192 /* Update location to make sure the current event is updated */
193 fCurLocation
= new CtfLocation(ctfLocationData
);
195 /* Adjust the timestamp depending on the trace's offset */
196 long currTimestamp
= ctfLocationData
.getTimestamp();
197 final long offsetTimestamp
= this.getCtfTmfTrace().timestampNanoToCycles(currTimestamp
);
199 if (offsetTimestamp
< 0) {
200 ret
= super.seek(0L);
202 ret
= super.seek(offsetTimestamp
);
204 } catch (CTFException e
) {
205 Activator
.getDefault().logError(e
.getMessage(), e
);
209 * Check if there is already one or more events for that timestamp, and
210 * assign the location index correctly
213 final CtfTmfEvent currentEvent
= this.getCurrentEvent();
214 if (currentEvent
!= null) {
215 currTimestamp
= currentEvent
.getTimestamp().getValue();
217 for (long i
= 0; i
< ctfLocationData
.getIndex(); i
++) {
218 if (currTimestamp
== currentEvent
.getTimestamp().getValue()) {
228 /* Seek the current location accordingly */
230 fCurLocation
= new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index
));
232 fCurLocation
= NULL_LOCATION
;
238 // ------------------------------------------------------------------------
240 // ------------------------------------------------------------------------
243 public boolean seek(long timestamp
) {
244 return seek(new CtfLocationInfo(timestamp
, 0));
248 public synchronized boolean advance() {
251 ret
= super.advance();
252 } catch (CTFException e
) {
253 Activator
.getDefault().logError(e
.getMessage(), e
);
257 long timestamp
= fCurLocation
.getLocationInfo().getTimestamp();
258 final long timestampValue
= getCurrentTimestamp();
259 if (timestamp
== timestampValue
) {
260 long index
= fCurLocation
.getLocationInfo().getIndex();
261 fCurLocation
= new CtfLocation(timestampValue
, index
+ 1);
263 fCurLocation
= new CtfLocation(timestampValue
, 0L);
266 fCurLocation
= NULL_LOCATION
;
271 // ------------------------------------------------------------------------
273 // ------------------------------------------------------------------------
276 public long getRank() {
281 public void setRank(long rank
) {
286 public void increaseRank() {
287 /* Only increase the rank if it's valid */
288 if (hasValidRank()) {
294 public boolean hasValidRank() {
295 return (getRank() >= 0);
299 public void setLocation(ITmfLocation location
) {
300 // FIXME alex: isn't there a cleaner way than a cast here?
301 fCurLocation
= (CtfLocation
) location
;
302 seek(((CtfLocation
) location
).getLocationInfo());
306 public CtfLocation
getLocation() {
310 // ------------------------------------------------------------------------
312 // ------------------------------------------------------------------------
315 public int compareTo(final CtfIterator o
) {
316 if (getRank() < o
.getRank()) {
318 } else if (getRank() > o
.getRank()) {
324 // ------------------------------------------------------------------------
326 // ------------------------------------------------------------------------
329 public int hashCode() {
330 final int prime
= 31;
331 int result
= super.hashCode();
332 result
= (prime
* result
)
333 + ((fTrace
== null) ?
0 : fTrace
.hashCode());
334 result
= (prime
* result
)
335 + ((fCurLocation
== null) ?
0 : fCurLocation
.hashCode());
336 result
= (prime
* result
) + (int) (fCurRank ^
(fCurRank
>>> 32));
341 public boolean equals(Object obj
) {
345 if (!super.equals(obj
)) {
348 if (!(obj
instanceof CtfIterator
)) {
351 CtfIterator other
= (CtfIterator
) obj
;
352 if (!equalsNullable(fTrace
, other
.fTrace
)) {
355 if (!equalsNullable(fCurLocation
, other
.fCurLocation
)) {
358 if (fCurRank
!= other
.fCurRank
) {