ctf: better enum and variant verification
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputPacketReader.java
index 87e474b545bda71045c809e99426ef39e4261754..f0a5d46bbf21d8d5d7dbfd7eaa6fabe6f4f71702 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * 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
@@ -15,10 +15,11 @@ import java.io.IOException;
 import java.nio.MappedByteBuffer;
 import java.nio.channels.FileChannel.MapMode;
 import java.util.Collection;
-import java.util.HashMap;
 
-import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
+import org.eclipse.linuxtools.ctf.core.CTFStrings;
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
+import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
@@ -26,8 +27,7 @@ import org.eclipse.linuxtools.ctf.core.event.types.SimpleDatatypeDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
-import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
+import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
 
 /**
@@ -58,12 +58,9 @@ public class StreamInputPacketReader implements IDefinitionScope {
     /** Stream event header definition. */
     private final StructDefinition streamEventHeaderDef;
 
-    /** Stream event context definition.*/
+    /** Stream event context definition. */
     private final StructDefinition streamEventContextDef;
 
-    /** Maps event ID to event definitions. */
-    private final HashMap<Long, EventDefinition> events;
-
     /** Reference to the index entry of the current packet. */
     private StreamInputPacketIndexEntry currentPacket = null;
 
@@ -78,11 +75,12 @@ public class StreamInputPacketReader implements IDefinitionScope {
     /** CPU id of current packet. */
     private int currentCpu = 0;
 
-    /** number of lost events in this packet */
-    private int lostSoFar;
-
     private int lostEventsInThisPacket;
 
+    private long lostEventsDuration;
+
+    private boolean hasLost = false;
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -100,9 +98,6 @@ public class StreamInputPacketReader implements IDefinitionScope {
         bitBuffer = new BitBuffer();
         bitBuffer.setByteOrder(streamInputReader.getByteOrder());
 
-        events = streamInputReader.getStreamInput().getStream().getTrace().getEventDefs(streamInputReader.getStreamInput());
-        lostSoFar = 0;
-
         /* Create trace packet header definition. */
         final Stream currentStream = streamInputReader.getStreamInput().getStream();
         StructDeclaration tracePacketHeaderDecl = currentStream.getTrace().getPacketHeader();
@@ -137,16 +132,25 @@ public class StreamInputPacketReader implements IDefinitionScope {
         }
 
         /* Create event definitions */
-        Collection<EventDeclaration> eventDecls = streamInputReader.getStreamInput().getStream().getEvents().values();
+        Collection<IEventDeclaration> eventDecls = streamInputReader.getStreamInput().getStream().getEvents().values();
 
-        for (EventDeclaration event : eventDecls) {
-            if (!events.containsKey(event.getId())) {
+        for (IEventDeclaration event : eventDecls) {
+            if (!streamInputReader.getEventDefinitions().containsKey(event.getId())) {
                 EventDefinition eventDef = event.createDefinition(streamInputReader);
-                events.put(event.getId(), eventDef);
+                streamInputReader.addEventDefinition(event.getId(), eventDef);
             }
         }
     }
 
+    /**
+     * Dispose the StreamInputPacketReader
+     *
+     * @since 2.0
+     */
+    public void dispose() {
+        bitBuffer.setByteBuffer(null);
+    }
+
     // ------------------------------------------------------------------------
     // Getters/Setters/Predicates
     // ------------------------------------------------------------------------
@@ -156,7 +160,7 @@ public class StreamInputPacketReader implements IDefinitionScope {
      *
      * @return the current packet
      */
-    public StreamInputPacketIndexEntry getCurrentPacket() {
+    StreamInputPacketIndexEntry getCurrentPacket() {
         return this.currentPacket;
     }
 
@@ -169,6 +173,15 @@ public class StreamInputPacketReader implements IDefinitionScope {
         return this.streamPacketContextDef;
     }
 
+    /**
+     * Gets the stream's event context definition.
+     *
+     * @return The streamEventContext definition
+     */
+    public StructDefinition getStreamEventContextDef() {
+        return streamEventContextDef;
+    }
+
     /**
      * Gets the CPU (core) number
      *
@@ -192,8 +205,11 @@ public class StreamInputPacketReader implements IDefinitionScope {
      *
      * @param currentPacket
      *            The index entry of the packet to switch to.
+     * @throws CTFReaderException
+     *             If we get an error reading the packet
      */
-    public void setCurrentPacket(StreamInputPacketIndexEntry currentPacket) {
+    void setCurrentPacket(StreamInputPacketIndexEntry currentPacket) throws CTFReaderException {
+        StreamInputPacketIndexEntry prevPacket = null;
         this.currentPacket = currentPacket;
 
         if (this.currentPacket != null) {
@@ -207,11 +223,7 @@ public class StreamInputPacketReader implements IDefinitionScope {
                                 this.currentPacket.getOffsetBytes(),
                                 (this.currentPacket.getPacketSizeBits() + 7) / 8);
             } catch (IOException e) {
-                /*
-                 * The streamInputReader object is already allocated, so this
-                 * shouldn't fail bar some very bad kernel or RAM errors...
-                 */
-                e.printStackTrace();
+                throw new CTFReaderException(e.getMessage(), e);
             }
 
             bitBuffer.setByteBuffer(bb);
@@ -236,8 +248,23 @@ public class StreamInputPacketReader implements IDefinitionScope {
 
                 /* Read number of lost events */
                 lostEventsInThisPacket = (int) this.getCurrentPacket().getLostEvents();
-                lostSoFar = 0;
-
+                if (lostEventsInThisPacket != 0) {
+                    hasLost = true;
+                    /*
+                     * Compute the duration of the lost event time range. If the
+                     * current packet is the first packet, duration will be set
+                     * to 1.
+                     */
+                    long lostEventsStartTime;
+                    int index = this.streamInputReader.getStreamInput().getIndex().getEntries().indexOf(currentPacket);
+                    if (index == 0) {
+                        lostEventsStartTime = currentPacket.getTimestampBegin() + 1;
+                    } else {
+                        prevPacket = this.streamInputReader.getStreamInput().getIndex().getEntries().get(index - 1);
+                        lostEventsStartTime = prevPacket.getTimestampEnd();
+                    }
+                    lostEventsDuration = Math.abs(lostEventsStartTime - currentPacket.getTimestampBegin());
+                }
             }
 
             /*
@@ -259,7 +286,7 @@ public class StreamInputPacketReader implements IDefinitionScope {
      */
     public boolean hasMoreEvents() {
         if (currentPacket != null) {
-            return bitBuffer.position() < currentPacket.getContentSizeBits();
+            return hasLost || (bitBuffer.position() < currentPacket.getContentSizeBits());
         }
         return false;
     }
@@ -274,19 +301,20 @@ public class StreamInputPacketReader implements IDefinitionScope {
      */
     public EventDefinition readNextEvent() throws CTFReaderException {
         /* Default values for those fields */
-        long eventID = 0;
+        long eventID = EventDeclaration.UNSET_EVENT_ID;
         long timestamp = 0;
-
-        if (lostEventsInThisPacket > lostSoFar) {
+        if (hasLost) {
+            hasLost = false;
             EventDefinition eventDef = EventDeclaration.getLostEventDeclaration().createDefinition(streamInputReader);
+            ((IntegerDefinition) eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD)).setValue(lostEventsInThisPacket);
+            ((IntegerDefinition) eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION)).setValue(lostEventsDuration);
             eventDef.setTimestamp(this.lastTimestamp);
-            ++lostSoFar;
             return eventDef;
         }
 
         final StructDefinition sehd = streamEventHeaderDef;
         final BitBuffer currentBitBuffer = bitBuffer;
-
+        final long posStart = currentBitBuffer.position();
         /* Read the stream event header. */
         if (sehd != null) {
             sehd.read(currentBitBuffer);
@@ -295,12 +323,17 @@ public class StreamInputPacketReader implements IDefinitionScope {
             Definition idDef = sehd.lookupDefinition("id"); //$NON-NLS-1$
             if (idDef instanceof SimpleDatatypeDefinition) {
                 eventID = ((SimpleDatatypeDefinition) idDef).getIntegerValue();
-            } // else, eventID remains 0
+            } else if (idDef != null) {
+                throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$
+            }
 
-            /* Get the timestamp from the event header (may be overridden later on) */
-            Definition timestampDef = sehd.lookupInteger("timestamp"); //$NON-NLS-1$
-            if (timestampDef instanceof IntegerDefinition) {
-                timestamp = calculateTimestamp((IntegerDefinition) timestampDef);
+            /*
+             * Get the timestamp from the event header (may be overridden later
+             * on)
+             */
+            IntegerDefinition timestampDef = sehd.lookupInteger("timestamp"); //$NON-NLS-1$
+            if (timestampDef != null) {
+                timestamp = calculateTimestamp(timestampDef);
             } // else timestamp remains 0
 
             /* Check for the variant v. */
@@ -319,10 +352,13 @@ public class StreamInputPacketReader implements IDefinitionScope {
                     eventID = ((IntegerDefinition) idIntegerDef).getValue();
                 }
 
-                /* Get the timestamp. This would overwrite any previous timestamp definition */
-                timestampDef = variantCurrentField.lookupDefinition("timestamp"); //$NON-NLS-1$
-                if (timestampDef instanceof IntegerDefinition) {
-                    timestamp = calculateTimestamp((IntegerDefinition) timestampDef);
+                /*
+                 * Get the timestamp. This would overwrite any previous
+                 * timestamp definition
+                 */
+                Definition def = variantCurrentField.lookupDefinition("timestamp"); //$NON-NLS-1$
+                if (def instanceof IntegerDefinition) {
+                    timestamp = calculateTimestamp((IntegerDefinition) def);
                 }
             }
         }
@@ -333,14 +369,14 @@ public class StreamInputPacketReader implements IDefinitionScope {
         }
 
         /* Get the right event definition using the event id. */
-        EventDefinition eventDef = events.get(eventID);
+        EventDefinition eventDef = streamInputReader.getEventDefinitions().get(eventID);
         if (eventDef == null) {
             throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$
         }
 
         /* Read the event context. */
-        if (eventDef.getContext() != null) {
-            eventDef.getContext().read(currentBitBuffer);
+        if (eventDef.getEventContext() != null) {
+            eventDef.getEventContext().read(currentBitBuffer);
         }
 
         /* Read the event fields. */
@@ -354,6 +390,10 @@ public class StreamInputPacketReader implements IDefinitionScope {
          */
         eventDef.setTimestamp(timestamp);
 
+        if (posStart == currentBitBuffer.position()) {
+            throw new CTFReaderException("Empty event not allowed, event: " + eventDef.getDeclaration().getName()); //$NON-NLS-1$
+        }
+
         return eventDef;
     }
 
@@ -403,7 +443,6 @@ public class StreamInputPacketReader implements IDefinitionScope {
 
     @Override
     public Definition lookupDefinition(String lookupPath) {
-        // TODO Auto-generated method stub
         return null;
     }
 }
This page took 0.027447 seconds and 5 git commands to generate.