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
.tmf
.ctf
.core
;
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
;
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
;
24 * The CTF trace reader iterator.
26 * It doesn't reserve a file handle, so many iterators can be used without
27 * worries of I/O errors or resource exhaustion.
29 * @author Matthew Khouzam
31 public class CtfIterator
extends CTFTraceReader
32 implements ITmfContext
, Comparable
<CtfIterator
> {
34 /** An invalid location */
35 public static final CtfLocation NULL_LOCATION
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
37 private final CtfTmfTrace fTrace
;
39 private CtfLocation fCurLocation
;
40 private long fCurRank
;
42 private CtfLocation fPreviousLocation
;
43 private CtfTmfEvent fPreviousEvent
;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 * Create a new CTF trace iterator, which initially points at the first
54 * The trace to iterate over
55 * @throws CTFReaderException
56 * If the iterator couldn't not be instantiated, probably due to
59 public CtfIterator(CtfTmfTrace trace
) throws CTFReaderException
{
60 super(trace
.getCTFTrace());
62 if (hasMoreEvents()) {
63 fCurLocation
= new CtfLocation(trace
.getStartTime());
71 * Create a new CTF trace iterator, which will initially point to the given
75 * The trace to iterate over
76 * @param ctfLocationData
77 * The initial timestamp the iterator will be pointing to
80 * @throws CTFReaderException
81 * If the iterator couldn't not be instantiated, probably due to
85 public CtfIterator(CtfTmfTrace trace
, CtfLocationInfo ctfLocationData
, long rank
)
86 throws CTFReaderException
{
87 super(trace
.getCTFTrace());
90 if (this.hasMoreEvents()) {
91 this.fCurLocation
= new CtfLocation(ctfLocationData
);
92 if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData
.getTimestamp()) {
93 this.seek(ctfLocationData
);
102 public void dispose() {
106 private void setUnknownLocation() {
107 fCurLocation
= NULL_LOCATION
;
108 fCurRank
= UNKNOWN_RANK
;
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
116 * Return this iterator's trace.
118 * @return CtfTmfTrace The iterator's trace
120 public CtfTmfTrace
getCtfTmfTrace() {
125 * Return the current event pointed to by the iterator.
127 * @return CtfTmfEvent The current event
129 public synchronized CtfTmfEvent
getCurrentEvent() {
130 final CTFStreamInputReader top
= super.getPrio().peek();
132 if (!fCurLocation
.equals(fPreviousLocation
)) {
133 fPreviousLocation
= fCurLocation
;
134 fPreviousEvent
= CtfTmfEventFactory
.createEvent(top
.getCurrentEvent(),
135 top
.getFilename(), fTrace
);
137 return fPreviousEvent
;
143 * Return the current timestamp location pointed to by the iterator.
144 * This is the timestamp for use in CtfLocation, not the event timestamp.
146 * @return long The current timestamp location
148 public synchronized long getCurrentTimestamp() {
149 final CTFStreamInputReader top
= super.getPrio().peek();
151 long ts
= top
.getCurrentEvent().getTimestamp();
152 return fTrace
.getCTFTrace().timestampCyclesToNanos(ts
);
158 * Seek this iterator to a given location.
160 * @param ctfLocationData
161 * The LocationData representing the position to seek to
162 * @return boolean True if the seek was successful, false if there was an
166 public synchronized boolean seek(CtfLocationInfo ctfLocationData
) {
169 /* Avoid the cost of seeking at the current location. */
170 if (fCurLocation
.getLocationInfo().equals(ctfLocationData
)) {
171 return super.hasMoreEvents();
174 /* Adjust the timestamp depending on the trace's offset */
175 long currTimestamp
= ctfLocationData
.getTimestamp();
176 final long offsetTimestamp
= this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp
);
178 if (offsetTimestamp
< 0) {
179 ret
= super.seek(0L);
181 ret
= super.seek(offsetTimestamp
);
183 } catch (CTFReaderException e
) {
184 Activator
.getDefault().logError(e
.getMessage(), e
);
188 * Check if there is already one or more events for that timestamp, and
189 * assign the location index correctly
192 final CtfTmfEvent currentEvent
= this.getCurrentEvent();
193 if (currentEvent
!= null) {
194 currTimestamp
= currentEvent
.getTimestamp().getValue();
196 for (long i
= 0; i
< ctfLocationData
.getIndex(); i
++) {
197 if (currTimestamp
== currentEvent
.getTimestamp().getValue()) {
207 /* Seek the current location accordingly */
209 fCurLocation
= new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index
));
211 fCurLocation
= NULL_LOCATION
;
217 // ------------------------------------------------------------------------
219 // ------------------------------------------------------------------------
222 public boolean seek(long timestamp
) {
223 return seek(new CtfLocationInfo(timestamp
, 0));
227 public synchronized boolean advance() {
230 ret
= super.advance();
231 } catch (CTFReaderException e
) {
232 Activator
.getDefault().logError(e
.getMessage(), e
);
236 long timestamp
= fCurLocation
.getLocationInfo().getTimestamp();
237 final long timestampValue
= getCurrentTimestamp();
238 if (timestamp
== timestampValue
) {
239 long index
= fCurLocation
.getLocationInfo().getIndex();
240 fCurLocation
= new CtfLocation(timestampValue
, index
+ 1);
242 fCurLocation
= new CtfLocation(timestampValue
, 0L);
245 fCurLocation
= NULL_LOCATION
;
250 // ------------------------------------------------------------------------
252 // ------------------------------------------------------------------------
255 public long getRank() {
260 public void setRank(long rank
) {
265 public void increaseRank() {
266 /* Only increase the rank if it's valid */
267 if (hasValidRank()) {
273 public boolean hasValidRank() {
274 return (getRank() >= 0);
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());
288 public CtfLocation
getLocation() {
292 // ------------------------------------------------------------------------
294 // ------------------------------------------------------------------------
297 public int compareTo(final CtfIterator o
) {
298 if (getRank() < o
.getRank()) {
300 } else if (getRank() > o
.getRank()) {
306 // ------------------------------------------------------------------------
308 // ------------------------------------------------------------------------
311 public int hashCode() {
312 final int prime
= 31;
313 int result
= super.hashCode();
314 result
= (prime
* result
)
315 + ((fTrace
== null) ?
0 : fTrace
.hashCode());
316 result
= (prime
* result
)
317 + ((fCurLocation
== null) ?
0 : fCurLocation
.hashCode());
318 result
= (prime
* result
) + (int) (fCurRank ^
(fCurRank
>>> 32));
323 public boolean equals(Object obj
) {
327 if (!super.equals(obj
)) {
330 if (!(obj
instanceof CtfIterator
)) {
333 CtfIterator other
= (CtfIterator
) obj
;
334 if (fTrace
== null) {
335 if (other
.fTrace
!= null) {
338 } else if (!fTrace
.equals(other
.fTrace
)) {
341 if (fCurLocation
== null) {
342 if (other
.fCurLocation
!= null) {
345 } else if (!fCurLocation
.equals(other
.fCurLocation
)) {
348 if (fCurRank
!= other
.fCurRank
) {