CTF: Do not recalculate the single clock each time in CTFTrace#getClock()
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTrace.java
index 5982864a463d95c0a6c64779a925978fe07b8287..4bb35847b7eb804f33a9a698b2460dc1b7ed7f38 100644 (file)
@@ -40,13 +40,14 @@ import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
-import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
+import org.eclipse.linuxtools.ctf.core.event.types.IDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.internal.ctf.core.event.CTFCallsiteComparator;
 import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
+import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDefinition;
 
 /**
  * A CTF trace on the file system.
@@ -101,7 +102,7 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
     /**
      * The clock of the trace
      */
-    private CTFClock fSingleClock;
+    private CTFClock fSingleClock = null;
 
     /**
      * Packet header structure definition
@@ -446,13 +447,6 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
         return (fPath != null) ? fPath.getPath() : ""; //$NON-NLS-1$
     }
 
-    /**
-     * @since 3.0
-     */
-    @Override
-    public LexicalScope getScopePath() {
-        return LexicalScope.TRACE;
-    }
 
     // ------------------------------------------------------------------------
     // Operations
@@ -504,6 +498,9 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
                 FileChannel fc = fis.getChannel()) {
             /* Map one memory page of 4 kiB */
             byteBuffer = fc.map(MapMode.READ_ONLY, 0, (int) Math.min(fc.size(), 4096L));
+            if( byteBuffer == null){
+                throw new IllegalStateException("Failed to allocate memory"); //$NON-NLS-1$
+            }
         } catch (IOException e) {
             /* Shouldn't happen at this stage if every other check passed */
             throw new CTFReaderException(e);
@@ -514,7 +511,7 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
 
         if (fPacketHeaderDecl != null) {
             /* Read the packet header */
-            fPacketHeaderDef = fPacketHeaderDecl.createDefinition(null, LexicalScope.PACKET_HEADER.getName(), streamBitBuffer);
+            fPacketHeaderDef = fPacketHeaderDecl.createDefinition(this, LexicalScope.PACKET_HEADER, streamBitBuffer);
 
             /* Check the magic number */
             IntegerDefinition magicDef = (IntegerDefinition) fPacketHeaderDef.lookupDefinition("magic"); //$NON-NLS-1$
@@ -524,7 +521,7 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
             }
 
             /* Check UUID */
-            Definition lookupDefinition = fPacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
+            IDefinition lookupDefinition = fPacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
             ArrayDefinition uuidDef = (ArrayDefinition) lookupDefinition;
             if (uuidDef != null) {
                 UUID otheruuid = Utils.getUUIDfromDefinition(uuidDef);
@@ -535,7 +532,7 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
             }
 
             /* Read the stream ID */
-            Definition streamIDDef = fPacketHeaderDef.lookupDefinition("stream_id"); //$NON-NLS-1$
+            IDefinition streamIDDef = fPacketHeaderDef.lookupDefinition("stream_id"); //$NON-NLS-1$
 
             if (streamIDDef instanceof IntegerDefinition) { // this doubles as a
                                                             // null check
@@ -564,6 +561,18 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
         return stream;
     }
 
+    // ------------------------------------------------------------------------
+    // IDefinitionScope
+    // ------------------------------------------------------------------------
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public LexicalScope getScopePath() {
+        return LexicalScope.TRACE;
+    }
+
     /**
      * Looks up a definition from packet
      *
@@ -574,12 +583,16 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
      */
     @Override
     public Definition lookupDefinition(String lookupPath) {
-        if (lookupPath.equals("trace.packet.header")) { //$NON-NLS-1$
+        if (lookupPath.equals(LexicalScope.TRACE_PACKET_HEADER.toString())) {
             return fPacketHeaderDef;
         }
         return null;
     }
 
+    // ------------------------------------------------------------------------
+    // Live trace reading
+    // ------------------------------------------------------------------------
+
     /**
      * Add a new stream file to support new streams while the trace is being
      * read.
@@ -685,6 +698,9 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
      * @return the clock
      */
     public final CTFClock getClock() {
+        if (fSingleClock != null && fClocks.size() == 1) {
+            return fSingleClock;
+        }
         if (fClocks.size() == 1) {
             fSingleClock = fClocks.get(fClocks.keySet().iterator().next());
             return fSingleClock;
This page took 0.025865 seconds and 5 git commands to generate.