LTTng: CPU usage analysis from the LTTng kernel trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfIterator.java
index d37a3efda1f34d93cdd33b0bdda0b4e4a113afc4..d5cece59e2814227468e0d948c257543a5c5d188 100644 (file)
+/*******************************************************************************
+ * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Matthew Khouzam - Initial API and implementation
+ *   Florian Wininger - Performance improvements
+ *******************************************************************************/
+
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
 
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
+import org.eclipse.linuxtools.internal.tmf.core.Activator;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
-import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
+import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
+
+/**
+ * The CTF trace reader iterator.
+ *
+ * It doesn't reserve a file handle, so many iterators can be used without
+ * worries of I/O errors or resource exhaustion.
+ *
+ * @author Matthew Khouzam
+ */
+public class CtfIterator extends CTFTraceReader
+        implements ITmfContext, Comparable<CtfIterator> {
+
+    /** An invalid location */
+    public static final CtfLocation NULL_LOCATION = new CtfLocation(CtfLocation.INVALID_LOCATION);
 
-public class CtfIterator extends CTFTraceReader implements ITmfContext,
-        Comparable<CtfIterator> {
+    private final CtfTmfTrace fTrace;
 
-    private final CtfTmfTrace ctfTmfTrace;
+    private CtfLocation fCurLocation;
+    private long fCurRank;
 
-    private CtfLocation curLocation;
-    private final long curRank;
+    private CtfLocation fPreviousLocation;
+    private CtfTmfEvent fPreviousEvent;
+
+    // ------------------------------------------------------------------------
+    // Constructors
+    // ------------------------------------------------------------------------
 
     /**
      * Create a new CTF trace iterator, which initially points at the first
      * event in the trace.
      *
      * @param trace
+     *            The trace to iterate over
+     * @throws CTFReaderException
+     *             If the iterator couldn't not be instantiated, probably due to
+     *             a read error.
      */
-    public CtfIterator(CtfTmfTrace trace) {
+    public CtfIterator(CtfTmfTrace trace) throws CTFReaderException {
         super(trace.getCTFTrace());
-        this.ctfTmfTrace = trace;
-
-        // FIXME put the real stuff here...
-        this.curLocation = new CtfLocation(trace.getStartTime());
-        this.curRank = 0;
+        fTrace = trace;
+        if (hasMoreEvents()) {
+            fCurLocation = new CtfLocation(trace.getStartTime());
+            fCurRank = 0;
+        } else {
+            setUnknownLocation();
+        }
     }
 
-    public CtfIterator(CtfTmfTrace trace, long timestampValue, long rank) {
+    /**
+     * Create a new CTF trace iterator, which will initially point to the given
+     * location/rank.
+     *
+     * @param trace
+     *            The trace to iterate over
+     * @param ctfLocationData
+     *            The initial timestamp the iterator will be pointing to
+     * @param rank
+     *            The initial rank
+     * @throws CTFReaderException
+     *             If the iterator couldn't not be instantiated, probably due to
+     *             a read error.
+     * @since 2.0
+     */
+    public CtfIterator(CtfTmfTrace trace, CtfLocationInfo ctfLocationData, long rank)
+            throws CTFReaderException {
         super(trace.getCTFTrace());
-        this.ctfTmfTrace = trace;
-        this.curLocation = (new CtfLocation(
-                this.getCurrentEvent().getTimestampValue()));
-        if (this.getCurrentEvent().getTimestampValue() != timestampValue) {
-            this.seek(timestampValue);
+
+        this.fTrace = trace;
+        if (this.hasMoreEvents()) {
+            this.fCurLocation = new CtfLocation(ctfLocationData);
+            if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData.getTimestamp()) {
+                this.seek(ctfLocationData);
+                this.fCurRank = rank;
+            }
+        } else {
+            setUnknownLocation();
         }
+    }
 
-        this.curRank = rank;
+    private void setUnknownLocation() {
+        fCurLocation = NULL_LOCATION;
+        fCurRank = UNKNOWN_RANK;
     }
 
+    // ------------------------------------------------------------------------
+    // Accessors
+    // ------------------------------------------------------------------------
+
+    /**
+     * Return this iterator's trace.
+     *
+     * @return CtfTmfTrace The iterator's trace
+     */
     public CtfTmfTrace getCtfTmfTrace() {
-        return ctfTmfTrace;
+        return fTrace;
     }
 
-    public CtfTmfEvent getCurrentEvent() {
-        StreamInputReader top = super.prio.peek();
+    /**
+     * Return the current event pointed to by the iterator.
+     *
+     * @return CtfTmfEvent The current event
+     */
+    public synchronized CtfTmfEvent getCurrentEvent() {
+        final StreamInputReader top = super.getPrio().peek();
         if (top != null) {
-            return new CtfTmfEvent(top.getCurrentEvent(), top, ctfTmfTrace);
+            if (!fCurLocation.equals(fPreviousLocation)) {
+                fPreviousLocation = fCurLocation;
+                fPreviousEvent = CtfTmfEventFactory.createEvent(top.getCurrentEvent(),
+                        top.getFilename(), fTrace);
+            }
+            return fPreviousEvent;
         }
         return null;
     }
 
-    @Override
-    public boolean seek(long timestamp) {
+    /**
+     * Seek this iterator to a given location.
+     *
+     * @param ctfLocationData
+     *            The LocationData representing the position to seek to
+     * @return boolean True if the seek was successful, false if there was an
+     *         error seeking.
+     * @since 2.0
+     */
+    public synchronized boolean seek(CtfLocationInfo ctfLocationData) {
         boolean ret = false;
-        long offsetTimestamp = timestamp - this.getCtfTmfTrace().getCTFTrace().getOffset();
-        if( offsetTimestamp < 0 ) {
-            ret = super.seek(timestamp);
-        } else {
-            ret = super.seek(offsetTimestamp);
+
+        /* Avoid the cost of seeking at the current location. */
+        if (fCurLocation.getLocationInfo().equals(ctfLocationData)) {
+            return super.hasMoreEvents();
+        }
+
+        /* Adjust the timestamp depending on the trace's offset */
+        long currTimestamp = ctfLocationData.getTimestamp();
+        final long offsetTimestamp = this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp);
+        try {
+            if (offsetTimestamp < 0) {
+                ret = super.seek(0L);
+            } else {
+                ret = super.seek(offsetTimestamp);
+            }
+        } catch (CTFReaderException e) {
+            Activator.logError(e.getMessage(), e);
+            return false;
         }
+        /*
+         * Check if there is already one or more events for that timestamp, and
+         * assign the location index correctly
+         */
+        long index = 0;
+        final CtfTmfEvent currentEvent = this.getCurrentEvent();
+        if (currentEvent != null) {
+            currTimestamp = currentEvent.getTimestamp().getValue();
 
+            for (long i = 0; i < ctfLocationData.getIndex(); i++) {
+                if (currTimestamp == currentEvent.getTimestamp().getValue()) {
+                    index++;
+                } else {
+                    index = 0;
+                }
+                this.advance();
+            }
+        } else {
+            ret = false;
+        }
+        /* Seek the current location accordingly */
         if (ret) {
-            curLocation.setLocation(getCurrentEvent().getTimestampValue());
+            fCurLocation = new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index));
+        } else {
+            fCurLocation = NULL_LOCATION;
         }
+
         return ret;
     }
 
-    public boolean seekRank(long rank) {
+    // ------------------------------------------------------------------------
+    // CTFTraceReader
+    // ------------------------------------------------------------------------
+
+    @Override
+    public boolean seek(long timestamp) {
+        return seek(new CtfLocationInfo(timestamp, 0));
+    }
+
+    @Override
+    public synchronized boolean advance() {
+        long index = fCurLocation.getLocationInfo().getIndex();
+        long timestamp = fCurLocation.getLocationInfo().getTimestamp();
         boolean ret = false;
-        ret = super.seekIndex(rank);
+        try {
+            ret = super.advance();
+        } catch (CTFReaderException e) {
+            Activator.logError(e.getMessage(), e);
+        }
 
         if (ret) {
-            curLocation.setLocation(getCurrentEvent().getTimestampValue());
+            final long timestampValue = getCurrentEvent().getTimestamp().getValue();
+            if (timestamp == timestampValue) {
+                fCurLocation = new CtfLocation(timestampValue, index + 1);
+            } else {
+                fCurLocation = new CtfLocation(timestampValue, 0L);
+            }
+        } else {
+            fCurLocation = NULL_LOCATION;
         }
         return ret;
     }
 
+    // ------------------------------------------------------------------------
+    // ITmfContext
+    // ------------------------------------------------------------------------
+
     @Override
     public long getRank() {
-        return super.getIndex();
+        return fCurRank;
     }
 
     @Override
     public void setRank(long rank) {
-        seekRank( rank );
+        fCurRank = rank;
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
-     */
     @Override
-    public CtfIterator clone() {
-        CtfIterator clone = null;
-        clone = new CtfIterator(ctfTmfTrace,
-                this.getCurrentEvent().getTimestampValue(), curRank);
-        return clone;
+    public void increaseRank() {
+        /* Only increase the rank if it's valid */
+        if (hasValidRank()) {
+            fCurRank++;
+        }
     }
 
     @Override
-    public void dispose() {
-        // FIXME add dispose() stuff to CTFTrace and call it here...
-
+    public boolean hasValidRank() {
+        return (getRank() >= 0);
     }
 
+    /**
+     * @since 3.0
+     */
     @Override
-    public void setLocation(ITmfLocation<?> location) {
+    public void setLocation(ITmfLocation location) {
         // FIXME alex: isn't there a cleaner way than a cast here?
-        this.curLocation = (CtfLocation) location;
-        seek(((CtfLocation)location).getLocation());
+        fCurLocation = (CtfLocation) location;
+        seek(((CtfLocation) location).getLocationInfo());
     }
 
     @Override
     public CtfLocation getLocation() {
-        return curLocation;
+        return fCurLocation;
     }
 
-    @Override
-    public void updateRank(int rank) {
-        // not needed I think
-    }
+    // ------------------------------------------------------------------------
+    // Comparable
+    // ------------------------------------------------------------------------
 
     @Override
-    public boolean isValidRank() {
-        return (getRank() > -1);
+    public int compareTo(final CtfIterator o) {
+        if (getRank() < o.getRank()) {
+            return -1;
+        } else if (getRank() > o.getRank()) {
+            return 1;
+        }
+        return 0;
     }
 
+    // ------------------------------------------------------------------------
+    // Object
+    // ------------------------------------------------------------------------
+
     @Override
-    public boolean advance() {
-        return super.advance();
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = (prime * result)
+                + ((fTrace == null) ? 0 : fTrace.hashCode());
+        result = (prime * result)
+                + ((fCurLocation == null) ? 0 : fCurLocation.hashCode());
+        result = (prime * result) + (int) (fCurRank ^ (fCurRank >>> 32));
+        return result;
     }
 
     @Override
-    public int compareTo(CtfIterator o) {
-        if (this.getRank() < o.getRank()) {
-            return -1;
-        } else if (this.getRank() > o.getRank()) {
-            return 1;
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
         }
-        return 0;
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (!(obj instanceof CtfIterator)) {
+            return false;
+        }
+        CtfIterator other = (CtfIterator) obj;
+        if (fTrace == null) {
+            if (other.fTrace != null) {
+                return false;
+            }
+        } else if (!fTrace.equals(other.fTrace)) {
+            return false;
+        }
+        if (fCurLocation == null) {
+            if (other.fCurLocation != null) {
+                return false;
+            }
+        } else if (!fCurLocation.equals(other.fCurLocation)) {
+            return false;
+        }
+        if (fCurRank != other.fCurRank) {
+            return false;
+        }
+        return true;
     }
-
-}
\ No newline at end of file
+}
This page took 0.028844 seconds and 5 git commands to generate.