Integrate Babeltrace CTF tests and fix parsing problems
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTraceReader.java
index 42ae663ee006015699ef3598876775887ecb331e..615efdb773083042771e4cf3d4cf9302977a8b14 100644 (file)
@@ -1,13 +1,14 @@
 /*******************************************************************************
- * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
  *
  * 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
- * Contributors: Alexandre Montplaisir - Initial API and implementation
+ * Contributors:
+ *     Matthew Khouzam - Initial API and implementation
+ *     Alexandre Montplaisir - Initial API and implementation
  *******************************************************************************/
 
 package org.eclipse.linuxtools.ctf.core.trace;
@@ -19,14 +20,15 @@ import java.util.Vector;
 
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.internal.ctf.core.Activator;
-import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
-import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput;
 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator;
 
 /**
- * Reads the events of a trace.
+ * A CTF trace reader. Reads the events of a trace.
+ *
+ * @version 1.0
+ * @author Matthew Khouzam
+ * @author Alexandre Montplaisir
  */
-
 public class CTFTraceReader {
 
     // ------------------------------------------------------------------------
@@ -63,11 +65,6 @@ public class CTFTraceReader {
      */
     private long endTime;
 
-
-    protected void setEndTime(long endTime) {
-        this.endTime = endTime;
-    }
-
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -77,7 +74,6 @@ public class CTFTraceReader {
      *
      * @param trace
      *            The trace to read from.
-     * @throws CTFReaderException
      */
     public CTFTraceReader(CTFTrace trace) {
         this.trace = trace;
@@ -106,6 +102,8 @@ public class CTFTraceReader {
 
     /**
      * Copy constructor
+     *
+     * @return The new CTFTraceReader
      */
     public CTFTraceReader copyFrom() {
         CTFTraceReader newReader = null;
@@ -116,6 +114,19 @@ public class CTFTraceReader {
         return newReader;
     }
 
+    /**
+     * Dispose the CTFTraceReader
+     * @since 2.0
+     */
+    public void dispose() {
+        for (StreamInputReader reader : streamInputReaders) {
+            if (reader != null) {
+                reader.dispose();
+            }
+        }
+        streamInputReaders.clear();
+    }
+
     // ------------------------------------------------------------------------
     // Getters/Setters/Predicates
     // ------------------------------------------------------------------------
@@ -129,6 +140,17 @@ public class CTFTraceReader {
         return this.startTime;
     }
 
+    /**
+     * Set the trace's end time
+     *
+     * @param endTime
+     *            The end time to use
+     */
+    protected void setEndTime(long endTime) {
+        this.endTime = endTime;
+    }
+
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
@@ -149,7 +171,6 @@ public class CTFTraceReader {
              * For each trace file of the stream.
              */
             for (StreamInput streamInput : streamInputs) {
-                streamInput.getIndex().getEntries().clear();
                 /*
                  * Create a reader.
                  */
@@ -242,12 +263,9 @@ public class CTFTraceReader {
              * Add it back in the queue.
              */
             this.prio.add(top);
-            final long topEnd = top.getCurrentEvent().getTimestamp() + this.getTrace().getOffset();
-            this.setEndTime( Math.max(topEnd, this.getEndTime()));
+            final long topEnd = this.trace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
+            this.setEndTime(Math.max(topEnd, this.getEndTime()));
             this.eventCountPerTraceFile[top.getName()]++;
-            /*
-             * increment the index
-             */
 
             if (top.getCurrentEvent() != null) {
                 this.endTime = Math.max(top.getCurrentEvent().getTimestamp(),
@@ -263,8 +281,6 @@ public class CTFTraceReader {
 
     /**
      * Go to the last event in the trace.
-     *
-     * @throws CTFReaderException
      */
     public void goToLastEvent() {
         seek(this.getEndTime());
@@ -309,23 +325,28 @@ public class CTFTraceReader {
         return hasMoreEvents();
     }
 
+//    /**
+//     * Go to the first entry of a trace
+//     *
+//     * @return 0, the first index.
+//     */
+//    private long goToZero() {
+//        long tempIndex;
+//        for (StreamInputReader streamInputReader : this.streamInputReaders) {
+//            /*
+//             * Seek the trace reader.
+//             */
+//            streamInputReader.seek(0);
+//        }
+//        tempIndex = 0;
+//        return tempIndex;
+//    }
+
     /**
-     * Go to the first entry of a trace
+     * gets the stream with the oldest event
      *
-     * @return 0, the first index.
+     * @return the stream with the oldest event
      */
-    private long goToZero() {
-        long tempIndex;
-        for (StreamInputReader streamInputReader : this.streamInputReaders) {
-            /*
-             * Seek the trace reader.
-             */
-            streamInputReader.seek(0);
-        }
-        tempIndex = 0;
-        return tempIndex;
-    }
-
     public StreamInputReader getTopStream() {
         return this.prio.peek();
     }
@@ -379,10 +400,16 @@ public class CTFTraceReader {
             }
 
             sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
-            Activator.getDefault().log(sb.toString());
+            Activator.log(sb.toString());
         }
     }
 
+    /**
+     * gets the last event timestamp that was read. This is NOT necessarily the
+     * last event in a trace, just the last one read so far.
+     *
+     * @return the last event
+     */
     public long getEndTime() {
         return this.endTime;
     }
@@ -392,9 +419,7 @@ public class CTFTraceReader {
         final int prime = 31;
         int result = 1;
         result = (prime * result) + (int) (startTime ^ (startTime >>> 32));
-        result = (prime * result)
-                + ((streamInputReaders == null) ? 0 : streamInputReaders
-                        .hashCode());
+        result = (prime * result) + streamInputReaders.hashCode();
         result = (prime * result) + ((trace == null) ? 0 : trace.hashCode());
         return result;
     }
@@ -411,11 +436,7 @@ public class CTFTraceReader {
             return false;
         }
         CTFTraceReader other = (CTFTraceReader) obj;
-        if (streamInputReaders == null) {
-            if (other.streamInputReaders != null) {
-                return false;
-            }
-        } else if (!streamInputReaders.equals(other.streamInputReaders)) {
+        if (!streamInputReaders.equals(other.streamInputReaders)) {
             return false;
         }
         if (trace == null) {
@@ -428,17 +449,17 @@ public class CTFTraceReader {
         return true;
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see java.lang.Object#toString()
-     */
     @Override
     public String toString() {
         /* Only for debugging, shouldn't be externalized */
         return "CTFTraceReader [trace=" + trace + ']'; //$NON-NLS-1$
     }
 
+    /**
+     * Gets the parent trace
+     *
+     * @return the parent trace
+     */
     public CTFTrace getTrace() {
         return trace;
     }
This page took 0.026445 seconds and 5 git commands to generate.