Fix alignment issue. Now traces using more than one alignment can be
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Mon, 23 Apr 2012 21:55:39 +0000 (17:55 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 1 May 2012 13:47:44 +0000 (09:47 -0400)
read.

Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTraceReader.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/IOStructGen.java

index e7824197d74280f763865df9cc96c5518f3bc0fc..308b05a4c28b28146e3359ef75042e49fe5ad994 100644 (file)
@@ -67,6 +67,9 @@ public class EnumDefinition extends Definition {
 
     @Override
     public void read(BitBuffer input) {
+        int align = (int) declaration.getAlignment();
+        int pos = input.position() + ((align-(input.position() % align))%align);
+        input.position(pos);
         integerValue.read(input);
         long val = integerValue.getValue();
 
index 6ac0d93d9f9b96d9d0fd6bc37c81578af86930ae..499c7fd38ddcac262e1ac212ef14eb33db61f16e 100644 (file)
@@ -61,6 +61,9 @@ public class IntegerDefinition extends Definition {
 
     @Override
     public void read(BitBuffer input) {
+        int align = (int) declaration.getAlignment();
+        int pos = input.position() + ((align-(input.position() % align))%align);
+        input.position(pos);
         boolean signed = declaration.isSigned();
         int length = declaration.getLength();
         long bits = 0;
index 2f0ef53b1df62bb2b18706fbcecc6f5f1104459e..271a3fba2a9e924d43293546796379223ac9884d 100644 (file)
@@ -27,26 +27,22 @@ public class StructDeclaration implements IDeclaration {
 
     private final HashMap<String, IDeclaration> fields = new HashMap<String, IDeclaration>();
     private final List<String> fieldsList = new LinkedList<String>();
-    private long minAlign;
+    private long maxAlign;
 
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
 
-    public StructDeclaration(long minAlign) {
-        this.minAlign = minAlign;
+    public StructDeclaration(long align) {
+        this.maxAlign = Math.max(align, 1);
     }
 
     // ------------------------------------------------------------------------
     // Getters/Setters/Predicates
     // ------------------------------------------------------------------------
 
-    public long getMinAlign() {
-        return this.minAlign;
-    }
-
-    public void setMinAlign(long minAlign) {
-        this.minAlign = minAlign;
+    public long getMaxAlign() {
+        return maxAlign;
     }
 
     public boolean hasField(String name) {
@@ -63,7 +59,7 @@ public class StructDeclaration implements IDeclaration {
 
     @Override
     public long getAlignment() {
-        return getMinAlign();
+        return this.maxAlign;
     }
     // ------------------------------------------------------------------------
     // Operations
@@ -76,10 +72,13 @@ public class StructDeclaration implements IDeclaration {
     }
 
     public void addField(String name, IDeclaration declaration) {
-        // TODO: update the minAlign to be the max of minAlign and the align
-        // value of the new field.
         this.fields.put(name, declaration);
         this.fieldsList.add(name);
+        maxAlign = Math.max(maxAlign, declaration.getAlignment());
+        if( maxAlign == 1 )
+        {
+            maxAlign =1;
+        }
     }
 
     @Override
index e29a6d0d680211f7b548509b9d17c5330a7c640f..b5d8210514a08aee934073f9b93e27584e605990 100644 (file)
@@ -13,6 +13,7 @@
 package org.eclipse.linuxtools.ctf.core.event.types;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.ListIterator;
 
 import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
@@ -71,10 +72,13 @@ public class StructDefinition extends Definition implements IDefinitionScope {
 
     @Override
     public void read(BitBuffer input) {
-        for (String fName : declaration.getFieldsList()) {
+        final int align = (int) declaration.getAlignment();
+        int pos = input.position() + ((align-(input.position() % align))%align);
+        input.position(pos);
+        final List<String> fieldList = declaration.getFieldsList();
+        for (String fName : fieldList) {
             Definition def = definitions.get(fName);
             assert (def != null);
-
             def.read(input);
         }
     }
index c0338cf35a0f44e768f73f47621e20243a14f838..6592edda9327d41b053e7104929d539b38610d53 100644 (file)
@@ -24,7 +24,7 @@ public class VariantDeclaration implements IDeclaration {
     // ------------------------------------------------------------------------
 
     private String tag = null;
-    private long alignment;
+    final private long alignment = 1;
     private final HashMap<String, IDeclaration> fields = new HashMap<String, IDeclaration>();
 
     // ------------------------------------------------------------------------
@@ -32,6 +32,7 @@ public class VariantDeclaration implements IDeclaration {
     // ------------------------------------------------------------------------
 
     public VariantDeclaration() {
+
     }
 
     // ------------------------------------------------------------------------
@@ -74,7 +75,6 @@ public class VariantDeclaration implements IDeclaration {
 
     public void addField(String fieldTag, IDeclaration declaration) {
         fields.put(fieldTag, declaration);
-        alignment = Math.max(alignment, declaration.getAlignment());
     }
 
     @Override
index 104bc6e59c1340aaf1d9a8de63596aece2a6d0e6..59d80c756dd8ff6237d92f14716bade3c333eea6 100644 (file)
@@ -353,8 +353,23 @@ public class CTFTraceReader {
                  */
                 final long streamIndex = streamInputReader.seekIndex(index);
                 tempIndex = Math.max(tempIndex, streamIndex);
-                tempTimestamp = Math.max(tempTimestamp,
-                        streamInputReader.getCurrentEvent().timestamp);
+                EventDefinition currentEvent = streamInputReader.getCurrentEvent();
+                /*
+                 * Maybe we're at the beginning of a trace.
+                 */
+                if( currentEvent == null ){
+                    streamInputReader.readNextEvent();
+                    currentEvent = streamInputReader.getCurrentEvent();
+                }
+                if( currentEvent != null ) {
+                    tempTimestamp = Math.max(tempTimestamp,
+                            currentEvent.timestamp);
+                } else {
+                    /*
+                     * probably beyond the last event
+                     */
+                    tempIndex = goToZero();
+                }
 
             }
         } catch (CTFReaderException e) {
@@ -362,13 +377,7 @@ public class CTFTraceReader {
              * Important, if it failed, it's because it's not yet indexed, so we
              * have to manually advance to the right value.
              */
-            for (StreamInputReader streamInputReader : this.streamInputReaders) {
-                /*
-                 * Seek the trace reader.
-                 */
-                streamInputReader.seek(0);
-            }
-            tempIndex = 0;
+            tempIndex = goToZero();
         }
         for (StreamInputReader streamInputReader : this.streamInputReaders) {
             /*
@@ -400,6 +409,22 @@ 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;
+    }
+
     public StreamInputReader getTopStream() {
         return this.prio.peek();
     }
index 9b4b2cbbf67bab71b36fb9a801a2b01742d48c43..debc33527727b46f5c66ceec123c00ab45db7156 100644 (file)
@@ -289,20 +289,22 @@ class StreamInputPacketReader implements IDefinitionScope {
      */
     public EventDefinition readNextEvent() throws CTFReaderException {
         /* WARNING: This is very LTTng-specific. */
-
         Long eventID = null;
         long timestamp = 0;
 
+        StructDefinition sehd = getStreamEventHeaderDef(); // acronym for a long variable name
+        BitBuffer currentBitBuffer = getBitBuffer();
         /*
          * Read the stream event header.
          */
-        if (getStreamEventHeaderDef() != null) {
-            getStreamEventHeaderDef().read(getBitBuffer());
+
+        if (sehd != null) {
+            sehd.read(currentBitBuffer);
 
             /*
              * Check for an event id.
              */
-            EnumDefinition idEnumDef = (EnumDefinition) getStreamEventHeaderDef().lookupDefinition("id"); //$NON-NLS-1$
+            EnumDefinition idEnumDef = (EnumDefinition) sehd.lookupDefinition("id"); //$NON-NLS-1$
             assert (idEnumDef != null);
 
             eventID = idEnumDef.getIntegerValue();
@@ -310,7 +312,7 @@ class StreamInputPacketReader implements IDefinitionScope {
             /*
              * Check for the variant v.
              */
-            VariantDefinition variantDef = (VariantDefinition) getStreamEventHeaderDef().lookupDefinition("v"); //$NON-NLS-1$
+            VariantDefinition variantDef = (VariantDefinition) sehd.lookupDefinition("v"); //$NON-NLS-1$
             assert (variantDef != null);
 
             /*
@@ -326,7 +328,6 @@ class StreamInputPacketReader implements IDefinitionScope {
             IntegerDefinition idIntegerDef = (IntegerDefinition) variantCurrentField.lookupDefinition("id"); //$NON-NLS-1$
             if (idIntegerDef != null) {
                 eventID = idIntegerDef.getValue();
-
             }
 
             /*
@@ -345,7 +346,7 @@ class StreamInputPacketReader implements IDefinitionScope {
          * Read the stream event context.
          */
         if (getStreamEventContextDef() != null) {
-            getStreamEventContextDef().read(getBitBuffer());
+            getStreamEventContextDef().read(currentBitBuffer);
         }
 
         /*
@@ -360,19 +361,14 @@ class StreamInputPacketReader implements IDefinitionScope {
          * Read the event context.
          */
         if (eventDef.context != null) {
-            eventDef.context.read(getBitBuffer());
+            eventDef.context.read(currentBitBuffer);
         }
 
         /*
          * Read the event fields.
          */
         if (eventDef.fields != null) {
-            int pos = getBitBuffer().position();
-            int minAlign = (int) eventDef.fields.getDeclaration().getMinAlign();
-            int offset = pos % minAlign;
-            pos += (minAlign - offset)%minAlign;
-            getBitBuffer().position(pos);
-            eventDef.fields.read(getBitBuffer());
+            eventDef.fields.read(currentBitBuffer);
         }
 
         /*
index 7e3071960d424357027bff3dbc61bb1fbe9dfdeb..1380de49be9eafb2a3e14a79f54bd8ac2cdb55aa 100644 (file)
@@ -1545,18 +1545,11 @@ public class IOStructGen {
                 throw new ParseException("struct " + structName //$NON-NLS-1$
                         + " already defined."); //$NON-NLS-1$
             }
-
             /* Create the declaration */
             structDeclaration = new StructDeclaration(structAlign);
 
             /* Parse the body */
             parseStructBody(structBody, structDeclaration);
-            long maxFieldAlign = -1;
-            for( IDeclaration field : structDeclaration.getFields().values())
-            {
-                maxFieldAlign = Math.max(maxFieldAlign, field.getAlignment());
-            }
-            structDeclaration.setMinAlign(maxFieldAlign);
 
             /* If struct has name, add it to the current scope. */
             if (hasName) {
This page took 0.03143 seconds and 5 git commands to generate.