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
.linuxtools
.tmf
.core
.ctfadaptor
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTraceReader
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.StreamInputReader
;
19 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
21 import org
.eclipse
.linuxtools
.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
);
101 private void setUnknownLocation() {
102 fCurLocation
= NULL_LOCATION
;
103 fCurRank
= UNKNOWN_RANK
;
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
111 * Return this iterator's trace.
113 * @return CtfTmfTrace The iterator's trace
115 public CtfTmfTrace
getCtfTmfTrace() {
120 * Return the current event pointed to by the iterator.
122 * @return CtfTmfEvent The current event
124 public synchronized CtfTmfEvent
getCurrentEvent() {
125 final StreamInputReader top
= super.getPrio().peek();
127 if (!fCurLocation
.equals(fPreviousLocation
)) {
128 fPreviousLocation
= fCurLocation
;
129 fPreviousEvent
= CtfTmfEventFactory
.createEvent(top
.getCurrentEvent(),
130 top
.getFilename(), fTrace
);
132 return fPreviousEvent
;
138 * Seek this iterator to a given location.
140 * @param ctfLocationData
141 * The LocationData representing the position to seek to
142 * @return boolean True if the seek was successful, false if there was an
146 public synchronized boolean seek(CtfLocationInfo ctfLocationData
) {
149 /* Avoid the cost of seeking at the current location. */
150 if (fCurLocation
.getLocationInfo().equals(ctfLocationData
)) {
151 return super.hasMoreEvents();
154 /* Adjust the timestamp depending on the trace's offset */
155 long currTimestamp
= ctfLocationData
.getTimestamp();
156 final long offsetTimestamp
= this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp
);
158 if (offsetTimestamp
< 0) {
159 ret
= super.seek(0L);
161 ret
= super.seek(offsetTimestamp
);
163 } catch (CTFReaderException e
) {
164 Activator
.logError(e
.getMessage(), e
);
168 * Check if there is already one or more events for that timestamp, and
169 * assign the location index correctly
172 final CtfTmfEvent currentEvent
= this.getCurrentEvent();
173 if (currentEvent
!= null) {
174 currTimestamp
= currentEvent
.getTimestamp().getValue();
176 for (long i
= 0; i
< ctfLocationData
.getIndex(); i
++) {
177 if (currTimestamp
== currentEvent
.getTimestamp().getValue()) {
187 /* Seek the current location accordingly */
189 fCurLocation
= new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index
));
191 fCurLocation
= NULL_LOCATION
;
197 // ------------------------------------------------------------------------
199 // ------------------------------------------------------------------------
202 public boolean seek(long timestamp
) {
203 return seek(new CtfLocationInfo(timestamp
, 0));
207 public synchronized boolean advance() {
208 long index
= fCurLocation
.getLocationInfo().getIndex();
209 long timestamp
= fCurLocation
.getLocationInfo().getTimestamp();
212 ret
= super.advance();
213 } catch (CTFReaderException e
) {
214 Activator
.logError(e
.getMessage(), e
);
218 final long timestampValue
= getCurrentEvent().getTimestamp().getValue();
219 if (timestamp
== timestampValue
) {
220 fCurLocation
= new CtfLocation(timestampValue
, index
+ 1);
222 fCurLocation
= new CtfLocation(timestampValue
, 0L);
225 fCurLocation
= NULL_LOCATION
;
230 // ------------------------------------------------------------------------
232 // ------------------------------------------------------------------------
235 public long getRank() {
240 public void setRank(long rank
) {
245 public void increaseRank() {
246 /* Only increase the rank if it's valid */
247 if (hasValidRank()) {
253 public boolean hasValidRank() {
254 return (getRank() >= 0);
261 public void setLocation(ITmfLocation location
) {
262 // FIXME alex: isn't there a cleaner way than a cast here?
263 fCurLocation
= (CtfLocation
) location
;
264 seek(((CtfLocation
) location
).getLocationInfo());
268 public CtfLocation
getLocation() {
272 // ------------------------------------------------------------------------
274 // ------------------------------------------------------------------------
277 public int compareTo(final CtfIterator o
) {
278 if (getRank() < o
.getRank()) {
280 } else if (getRank() > o
.getRank()) {
286 // ------------------------------------------------------------------------
288 // ------------------------------------------------------------------------
291 public int hashCode() {
292 final int prime
= 31;
293 int result
= super.hashCode();
294 result
= (prime
* result
)
295 + ((fTrace
== null) ?
0 : fTrace
.hashCode());
296 result
= (prime
* result
)
297 + ((fCurLocation
== null) ?
0 : fCurLocation
.hashCode());
298 result
= (prime
* result
) + (int) (fCurRank ^
(fCurRank
>>> 32));
303 public boolean equals(Object obj
) {
307 if (!super.equals(obj
)) {
310 if (!(obj
instanceof CtfIterator
)) {
313 CtfIterator other
= (CtfIterator
) obj
;
314 if (fTrace
== null) {
315 if (other
.fTrace
!= null) {
318 } else if (!fTrace
.equals(other
.fTrace
)) {
321 if (fCurLocation
== null) {
322 if (other
.fCurLocation
!= null) {
325 } else if (!fCurLocation
.equals(other
.fCurLocation
)) {
328 if (fCurRank
!= other
.fCurRank
) {