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
.checkNotNull
;
17 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.equalsNullable
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFStreamInputReader
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
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
;
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
;
30 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
33 * The CTF trace reader iterator.
35 * It doesn't reserve a file handle, so many iterators can be used without
36 * worries of I/O errors or resource exhaustion.
38 * @author Matthew Khouzam
40 public class CtfIterator
extends CTFTraceReader
41 implements ITmfContext
, Comparable
<CtfIterator
> {
43 /** An invalid location */
44 public static final CtfLocation NULL_LOCATION
= new CtfLocation(CtfLocation
.INVALID_LOCATION
);
46 private final CtfTmfTrace fTrace
;
48 private CtfLocation fCurLocation
;
49 private long fCurRank
;
51 private CtfLocation fPreviousLocation
;
52 private CtfTmfEvent fPreviousEvent
;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * Create a new CTF trace iterator, which initially points at the first
63 * The {@link CTFTrace} linked to the trace. It should be
64 * provided by the corresponding 'ctfTmfTrace'.
67 * The {@link CtfTmfTrace} to iterate over
68 * @throws CTFException
69 * If the iterator couldn't not be instantiated, probably due to
72 public CtfIterator(CTFTrace ctfTrace
, CtfTmfTrace ctfTmfTrace
) throws CTFException
{
75 if (hasMoreEvents()) {
76 fCurLocation
= new CtfLocation(ctfTmfTrace
.getStartTime());
84 * Create a new CTF trace iterator, which will initially point to the given
88 * The {@link CTFTrace} linked to the trace. It should be
89 * provided by the corresponding 'ctfTmfTrace'.
91 * The {@link CtfTmfTrace} to iterate over
92 * @param ctfLocationData
93 * The initial timestamp the iterator will be pointing to
96 * @throws CTFException
97 * If the iterator couldn't not be instantiated, probably due to
100 public CtfIterator(CTFTrace ctfTrace
, CtfTmfTrace ctfTmfTrace
, CtfLocationInfo ctfLocationData
, long rank
)
101 throws CTFException
{
104 this.fTrace
= ctfTmfTrace
;
105 if (this.hasMoreEvents()) {
106 this.fCurLocation
= new CtfLocation(ctfLocationData
);
107 if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData
.getTimestamp()) {
108 this.seek(ctfLocationData
);
109 this.fCurRank
= rank
;
112 setUnknownLocation();
117 public void dispose() {
121 private void setUnknownLocation() {
122 fCurLocation
= NULL_LOCATION
;
123 fCurRank
= UNKNOWN_RANK
;
126 // ------------------------------------------------------------------------
128 // ------------------------------------------------------------------------
131 * Return this iterator's trace.
133 * @return CtfTmfTrace The iterator's trace
135 public CtfTmfTrace
getCtfTmfTrace() {
140 * Return the current event pointed to by the iterator.
142 * @return CtfTmfEvent The current event
144 public synchronized CtfTmfEvent
getCurrentEvent() {
145 final CTFStreamInputReader top
= super.getPrio().peek();
147 if (!fCurLocation
.equals(fPreviousLocation
)) {
148 fPreviousLocation
= fCurLocation
;
149 fPreviousEvent
= fTrace
.getEventFactory().createEvent(fTrace
, checkNotNull(top
.getCurrentEvent()), top
.getFilename());
151 return fPreviousEvent
;
157 * Return the current timestamp location pointed to by the iterator. This is
158 * the timestamp for use in CtfLocation, not the event timestamp.
160 * @return long The current timestamp location
162 public synchronized long getCurrentTimestamp() {
163 final CTFStreamInputReader top
= super.getPrio().peek();
165 EventDefinition currentEvent
= top
.getCurrentEvent();
166 if (currentEvent
!= null) {
167 long ts
= currentEvent
.getTimestamp();
168 return fTrace
.timestampCyclesToNanos(ts
);
175 * Seek this iterator to a given location.
177 * @param ctfLocationData
178 * The LocationData representing the position to seek to
179 * @return boolean True if the seek was successful, false if there was an
182 public synchronized boolean seek(CtfLocationInfo ctfLocationData
) {
184 if (ctfLocationData
.equals(CtfLocation
.INVALID_LOCATION
)) {
185 fCurLocation
= NULL_LOCATION
;
189 /* Avoid the cost of seeking at the current location. */
190 if (fCurLocation
.getLocationInfo().equals(ctfLocationData
)) {
191 return super.hasMoreEvents();
193 /* Update location to make sure the current event is updated */
194 fCurLocation
= new CtfLocation(ctfLocationData
);
196 /* Adjust the timestamp depending on the trace's offset */
197 long currTimestamp
= ctfLocationData
.getTimestamp();
198 final long offsetTimestamp
= this.getCtfTmfTrace().timestampNanoToCycles(currTimestamp
);
200 if (offsetTimestamp
< 0) {
201 ret
= super.seek(0L);
203 ret
= super.seek(offsetTimestamp
);
205 } catch (CTFException e
) {
206 Activator
.getDefault().logError(e
.getMessage(), e
);
210 * Check if there is already one or more events for that timestamp, and
211 * assign the location index correctly
214 final CtfTmfEvent currentEvent
= this.getCurrentEvent();
215 if (currentEvent
!= null) {
216 currTimestamp
= currentEvent
.getTimestamp().getValue();
218 for (long i
= 0; i
< ctfLocationData
.getIndex(); i
++) {
219 if (currTimestamp
== currentEvent
.getTimestamp().getValue()) {
229 /* Seek the current location accordingly */
231 fCurLocation
= new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index
));
233 fCurLocation
= NULL_LOCATION
;
239 // ------------------------------------------------------------------------
241 // ------------------------------------------------------------------------
244 public boolean seek(long timestamp
) {
245 return seek(new CtfLocationInfo(timestamp
, 0));
249 public synchronized boolean advance() {
252 ret
= super.advance();
253 } catch (CTFException e
) {
254 Activator
.getDefault().logError(e
.getMessage(), e
);
258 long timestamp
= fCurLocation
.getLocationInfo().getTimestamp();
259 final long timestampValue
= getCurrentTimestamp();
260 if (timestamp
== timestampValue
) {
261 long index
= fCurLocation
.getLocationInfo().getIndex();
262 fCurLocation
= new CtfLocation(timestampValue
, index
+ 1);
264 fCurLocation
= new CtfLocation(timestampValue
, 0L);
267 fCurLocation
= NULL_LOCATION
;
272 // ------------------------------------------------------------------------
274 // ------------------------------------------------------------------------
277 public long getRank() {
282 public void setRank(long rank
) {
287 public void increaseRank() {
288 /* Only increase the rank if it's valid */
289 if (hasValidRank()) {
295 public boolean hasValidRank() {
296 return (getRank() >= 0);
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());
307 public CtfLocation
getLocation() {
311 // ------------------------------------------------------------------------
313 // ------------------------------------------------------------------------
316 public int compareTo(final CtfIterator o
) {
317 if (getRank() < o
.getRank()) {
319 } else if (getRank() > o
.getRank()) {
325 // ------------------------------------------------------------------------
327 // ------------------------------------------------------------------------
330 public int hashCode() {
331 final int prime
= 31;
332 int result
= super.hashCode();
333 result
= (prime
* result
)
334 + ((fTrace
== null) ?
0 : fTrace
.hashCode());
335 result
= (prime
* result
)
336 + ((fCurLocation
== null) ?
0 : fCurLocation
.hashCode());
337 result
= (prime
* result
) + (int) (fCurRank ^
(fCurRank
>>> 32));
342 public boolean equals(Object obj
) {
346 if (!super.equals(obj
)) {
349 if (!(obj
instanceof CtfIterator
)) {
352 CtfIterator other
= (CtfIterator
) obj
;
353 if (!equalsNullable(fTrace
, other
.fTrace
)) {
356 if (!equalsNullable(fCurLocation
, other
.fCurLocation
)) {
359 if (fCurRank
!= other
.fCurRank
) {