ctf: Fix lost events in a more elegant way
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 2 Aug 2012 14:47:22 +0000 (10:47 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Tue, 7 Aug 2012 14:41:19 +0000 (10:41 -0400)
- Read the stream context packet once instead of at each load
  of the packet.
- Clean up code.
- Change the API to handle seeks to a specific event in a timestamp.
- Update test cases.

Change-Id: I0dddf56645bc0b8a69256ac947f3a806d1cfedf3
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/7075
Reviewed-by: Bernd Hufmann <bhufmann@gmail.com>
IP-Clean: Bernd Hufmann <bhufmann@gmail.com>
Tested-by: Bernd Hufmann <bhufmann@gmail.com>
16 files changed:
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputReader.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/trace/StreamInput.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/trace/StreamInputPacketIndexEntry.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfIteratorTest.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfLocationDataTest.java [new file with mode: 0644]
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfLocationTest.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfLightweightContextTest.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfTraceTest.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/TestAll.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIteratorManager.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocation.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocationData.java [new file with mode: 0644]
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfLightweightContext.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java

index 333390b419aba0404993b00b87844760ccec27c4..0e301b0b8a2158383286aa62caba0328e1d98253 100644 (file)
@@ -99,8 +99,6 @@ public class StreamInputPacketReader implements IDefinitionScope {
     /**
      * number of lost events in this packet
      */
-    private int lostEvents;
-
     private int lostSoFar;
 
     private int lostEventsInThisPacket;
@@ -130,7 +128,6 @@ public class StreamInputPacketReader implements IDefinitionScope {
          */
         createDefinitions();
 
-        lostEvents = 0;
         lostSoFar = 0;
     }
 
@@ -290,23 +287,14 @@ public class StreamInputPacketReader implements IDefinitionScope {
              */
             if (getStreamPacketContextDef() != null) {
                 getStreamPacketContextDef().read(getBitBuffer());
-                /*
-                 * Read CPU ID
-                 */
 
+                /* Read CPU ID */
                 if (this.getCurrentPacket().getTarget() != null) {
-                    this.currentCpu = (int) this.getCurrentPacket()
-                            .getTargetId();
+                    this.currentCpu = (int) this.getCurrentPacket().getTargetId();
                 }
-                /*
-                 * Read number of lost events
-                 */
 
-                int totalLostEvents = (int) this.getCurrentPacket()
-                        .getLostEvents();
-                lostEventsInThisPacket = totalLostEvents - lostEvents;
-                lostEvents = totalLostEvents;
-                currentPacket.setLostEvents(lostEventsInThisPacket);
+                /* Read number of lost events */
+                lostEventsInThisPacket = (int) this.getCurrentPacket().getLostEvents();
                 lostSoFar = 0;
 
             }
@@ -357,6 +345,7 @@ public class StreamInputPacketReader implements IDefinitionScope {
             ++lostSoFar;
             return eventDef;
         }
+
         StructDefinition sehd = getStreamEventHeaderDef(); // acronym for a long
                                                            // variable name
         BitBuffer currentBitBuffer = getBitBuffer();
index 0ec21d72e750fe7a3d482a725a81bfb75da744a1..e18e068018380f8f9de14a98a015ac1d19fe6cf7 100644 (file)
@@ -175,8 +175,6 @@ public class StreamInputReader {
                     .getCurrentPacket();
             if (prevPacket != null) {
                 goToNextPacket();
-                @SuppressWarnings("unused")
-                final StreamInputPacketIndexEntry currentPacket = this.packetReader.getCurrentPacket();
             }
         }
 
index d5f4609853944fece2235ad2aa1fab1b86faf8e5..006ab8dc3fd04169755bffd1b07e0aa084609776 100644 (file)
@@ -74,6 +74,11 @@ public class StreamInput implements IDefinitionScope {
      */
     StructDefinition streamPacketContextDef = null;
 
+    /*
+     * Total number of lost events in this stream
+     */
+    long lostSoFar = 0;
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -416,30 +421,25 @@ public class StreamInput implements IDefinitionScope {
                 packetIndex.addAttribute(field,
                         ((StringDefinition) id).getValue());
             }
-
         }
 
         Long contentSize = (Long) packetIndex.lookupAttribute("content_size"); //$NON-NLS-1$
         Long packetSize = (Long) packetIndex.lookupAttribute("packet_size"); //$NON-NLS-1$
-        Long timestampBegin = (Long) packetIndex
-                .lookupAttribute("timestamp_begin"); //$NON-NLS-1$
+        Long timestampBegin = (Long) packetIndex.lookupAttribute("timestamp_begin"); //$NON-NLS-1$
         Long timestampEnd = (Long) packetIndex.lookupAttribute("timestamp_end"); //$NON-NLS-1$
         String device = (String) packetIndex.lookupAttribute("device"); //$NON-NLS-1$
         // LTTng Specific
         Long CPU_ID = (Long) packetIndex.lookupAttribute("cpu_id"); //$NON-NLS-1$
-        /*
-         * Read the content size in bits
-         */
+        Long lostEvents = (Long) packetIndex.lookupAttribute("events_discarded");  //$NON-NLS-1$
+
+        /* Read the content size in bits */
         if (contentSize != null) {
             packetIndex.setContentSizeBits(contentSize.intValue());
         } else {
             packetIndex.setContentSizeBits((int) (fileSizeBytes * 8));
         }
 
-        /*
-         * Read the packet size in bits
-         */
-
+        /* Read the packet size in bits */
         if (packetSize != null) {
             packetIndex.setPacketSizeBits(packetSize.intValue());
         } else {
@@ -450,17 +450,12 @@ public class StreamInput implements IDefinitionScope {
             }
         }
 
-        /*
-         * Read the begin timestamp
-         */
-
+        /* Read the begin timestamp */
         if (timestampBegin != null) {
             packetIndex.setTimestampBegin(timestampBegin.longValue());
         }
 
-        /*
-         * Read the end timestamp
-         */
+        /* Read the end timestamp */
         if (timestampEnd != null) {
             if( timestampEnd == -1 ) {
                 timestampEnd = Long.MAX_VALUE;
@@ -476,6 +471,11 @@ public class StreamInput implements IDefinitionScope {
         if (CPU_ID != null) {
             packetIndex.setTarget("CPU" + CPU_ID.toString()); //$NON-NLS-1$
         }
+
+        if (lostEvents != null) {
+            packetIndex.setLostEvents(lostEvents - lostSoFar);
+            this.lostSoFar = lostEvents;
+        }
     }
 
     /*
index fa3343b727414ce31c539219a1083823084a558c..3a0379082351e5698953c6ca79820ec42fc9d9f4 100644 (file)
@@ -203,7 +203,7 @@ public class StreamInputPacketIndexEntry {
     }
 
     /**
-     * @return the lostEvents
+     * @return the lostEvents in this packet
      */
     public long getLostEvents() {
         return lostEvents;
@@ -220,7 +220,7 @@ public class StreamInputPacketIndexEntry {
      * Add an attribute to this index entry
      *
      * @param field
-     *            The name of the atrribute
+     *            The name of the attribute
      * @param value
      *            The value to insert
      */
index 4b59579ffe90cb141a4de9b14ac6dcf1fd6f8788..96d4cb93a2dcf806038ba1ea3e0a5a102872f36c 100644 (file)
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertTrue;
 
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocation;
+import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocationData;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
@@ -57,8 +58,8 @@ public class CtfIteratorTest {
     @Before
     public void setUp() throws TmfTraceException {
         fixture = new CtfIterator(createTrace());
-        CtfLocation ctfLocation = new CtfLocation(Long.valueOf(1));
-        ctfLocation.setLocation(Long.valueOf(1));
+        CtfLocation ctfLocation = new CtfLocation(new CtfLocationData(1, 0));
+        ctfLocation.setLocation(new CtfLocationData(1, 0));
         fixture.setLocation(ctfLocation);
         fixture.increaseRank();
     }
@@ -116,7 +117,7 @@ public class CtfIteratorTest {
         CtfTmfTrace trace = createTrace();
         long timestampValue = 1L;
         long rank = 1L;
-        CtfIterator result = new CtfIterator(trace, timestampValue, rank);
+        CtfIterator result = new CtfIterator(trace, new CtfLocationData(timestampValue, 0), rank);
 
         assertNotNull(result);
     }
@@ -164,8 +165,8 @@ public class CtfIteratorTest {
     @Test
     public void testEquals_other() throws TmfTraceException {
         CtfIterator obj = new CtfIterator(createTrace());
-        CtfLocation ctfLocation1 = new CtfLocation(Long.valueOf(1));
-        ctfLocation1.setLocation(Long.valueOf(1));
+        CtfLocation ctfLocation1 = new CtfLocation(new CtfLocationData(1, 0));
+        ctfLocation1.setLocation(new CtfLocationData(1, 0));
         obj.setLocation(ctfLocation1);
         obj.increaseRank();
 
@@ -262,8 +263,8 @@ public class CtfIteratorTest {
      */
     @Test
     public void testSetLocation() {
-        CtfLocation location = new CtfLocation(Long.valueOf(1));
-        location.setLocation(Long.valueOf(1));
+        CtfLocation location = new CtfLocation(new CtfLocationData(1, 0));
+        location.setLocation(new CtfLocationData(1, 0));
         fixture.setLocation(location);
     }
 }
diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfLocationDataTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfLocationDataTest.java
new file mode 100644 (file)
index 0000000..2597bda
--- /dev/null
@@ -0,0 +1,119 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Ericsson
+ *
+ * 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:
+ *   Alexandre Montplaisir - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocationData;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Collection of tests for the {@link CtfLocationData}
+ *
+ * @author alexmont
+ */
+public class CtfLocationDataTest {
+
+    private CtfLocationData fixture;
+
+    /**
+     * Launch the test.
+     *
+     * @param args
+     *            the command line arguments
+     */
+    public static void main(String[] args) {
+        new org.junit.runner.JUnitCore().run(CtfLocationDataTest.class);
+    }
+
+    /**
+     * Perform pre-test initialization.
+     */
+    @Before
+    public void setUp() {
+        fixture = new CtfLocationData(1, 0);
+    }
+
+    /**
+     * Perform post-test clean-up.
+     */
+    @After
+    public void tearDown() {
+        // Add additional tear down code here
+    }
+
+
+    /**
+     * Test for the .getTimestamp() and .getIndex() methods
+     */
+    @Test
+    public void testGetters() {
+        long timestamp = fixture.getTimestamp();
+        long index = fixture.getIndex();
+
+        assertEquals(1, timestamp);
+        assertEquals(0, index);
+    }
+
+    /**
+     * Test for the .hashCode() method
+     */
+    @Test
+    public void testHashCode() {
+        int code = fixture.hashCode();
+        assertEquals(962, code);
+    }
+
+    /**
+     * Test for the .equals() method
+     */
+    @Test
+    public void testEquals() {
+        CtfLocationData same = new CtfLocationData(1, 0);
+        CtfLocationData diff1 = new CtfLocationData(100, 0);
+        CtfLocationData diff2 = new CtfLocationData(1, 10);
+
+        assertTrue(fixture.equals(same));
+        assertFalse(fixture.equals(diff1));
+        assertFalse(fixture.equals(diff2));
+    }
+
+    /**
+     * Test for the .compareTo() method
+     */
+    @Test
+    public void testCompareTo() {
+        CtfLocationData same = new CtfLocationData(1, 0);
+        CtfLocationData smaller = new CtfLocationData(0, 0);
+        CtfLocationData bigger1 = new CtfLocationData(1000, 500);
+        CtfLocationData bigger2 = new CtfLocationData(1, 1);
+
+        assertEquals(0, same.compareTo(fixture));
+        assertEquals(-1, smaller.compareTo(fixture));
+        assertEquals(1, bigger1.compareTo(fixture));
+        assertEquals(1, bigger2.compareTo(fixture));
+    }
+
+    /**
+     * Test for the .toString() method
+     */
+    @Test
+    public void testToString() {
+        String expected = "Element [1/0]"; //$NON-NLS-1$
+        assertEquals(expected, fixture.toString());
+    }
+}
index 6f337cb81b394395f3057f0f35c2ced514512ade..087c22fd8b101ddb907b1191be157a00c21b123f 100644 (file)
@@ -17,6 +17,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocation;
+import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocationData;
 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
 import org.junit.After;
@@ -49,8 +50,8 @@ public class CtfLocationTest {
      */
     @Before
     public void setUp() {
-        fixture = new CtfLocation(Long.valueOf(1));
-        fixture.setLocation(Long.valueOf(1));
+        fixture = new CtfLocation(new CtfLocationData(1, 0));
+        fixture.setLocation(new CtfLocationData(1, 0));
     }
 
     /**
@@ -67,11 +68,11 @@ public class CtfLocationTest {
      */
     @Test
     public void testCtfLocation_long() {
-        Long location = Long.valueOf(1);
+        CtfLocationData location = new CtfLocationData(1, 0);
         CtfLocation result = new CtfLocation(location);
 
         assertNotNull(result);
-        assertEquals(Long.valueOf(1), result.getLocation());
+        assertEquals(Long.valueOf(1), (Long)result.getLocation().getTimestamp());
     }
 
     /**
@@ -83,7 +84,7 @@ public class CtfLocationTest {
         CtfLocation result = new CtfLocation(timestamp);
 
         assertNotNull(result);
-        assertEquals(new Long(0L), result.getLocation());
+        assertEquals(new Long(0L), (Long)result.getLocation().getTimestamp());
     }
 
     /**
@@ -94,7 +95,7 @@ public class CtfLocationTest {
         CtfLocation result = fixture.clone();
 
         assertNotNull(result);
-        assertEquals(Long.valueOf(1), result.getLocation());
+        assertEquals(Long.valueOf(1), (Long)result.getLocation().getTimestamp());
     }
 
     /**
@@ -102,8 +103,8 @@ public class CtfLocationTest {
      */
     @Test
     public void testGetLocation() {
-        Long result = fixture.getLocation();
-
+        CtfLocationData location = fixture.getLocation();
+        Long result = location.getTimestamp();
         assertNotNull(result);
         assertEquals("1", result.toString()); //$NON-NLS-1$
         assertEquals((byte) 1, result.byteValue());
@@ -119,7 +120,7 @@ public class CtfLocationTest {
      */
     @Test
     public void testSetLocation() {
-        Long location = Long.valueOf(1);
+        CtfLocationData location = new CtfLocationData(1337, 7331);
         fixture.setLocation(location);
     }
 
@@ -128,8 +129,8 @@ public class CtfLocationTest {
      */
     @Test
     public void testToString_valid(){
-        CtfLocation fixture2 = new CtfLocation(new Long(1337L));
-        assertEquals("CtfLocation: 1337",fixture2.toString()); //$NON-NLS-1$
+        CtfLocation fixture2 = new CtfLocation(new CtfLocationData(1337, 7331));
+        assertEquals("CtfLocation: Element [1337/7331]",fixture2.toString()); //$NON-NLS-1$
     }
 
     /**
@@ -137,7 +138,7 @@ public class CtfLocationTest {
      */
     @Test
     public void testToString_invalid(){
-        CtfLocation fixture2 = new CtfLocation(new Long(-1L));
+        CtfLocation fixture2 = new CtfLocation(new CtfLocationData(-1, -1));
         assertEquals("CtfLocation: INVALID",fixture2.toString()); //$NON-NLS-1$
     }
 }
index 9e346f7ec617d469a15bd7fc6719c4c40e93d044..874d40395ef10bb1be99b99f5316646e2b78d6d3 100644 (file)
@@ -97,7 +97,9 @@ public class CtfTmfLightweightContextTest {
                     lwc.seek(val);
                     fixture.getNext(lwc);
                     synchronized(fixture){
-                        vals.add(lwc.getCurrentEvent().getTimestampValue());
+                        if (lwc.getCurrentEvent() != null) {
+                            vals.add(lwc.getCurrentEvent().getTimestampValue());
+                        }
                         tooManyContexts.add(lwc);
                     }
                 }
index bb23c0cd5dc7ba4d7e113bcd34d2b409c870471d..5d81111557ab8f779cadae766c160b4c5c24fbfd 100644 (file)
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocation;
+import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocationData;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
@@ -211,8 +212,9 @@ public class CtfTmfTraceTest {
      */
     @Test
     public void testGetLocationRatio() {
-        CtfLocation location = new CtfLocation(Long.valueOf(1));
-        location.setLocation(Long.valueOf(1));
+        final CtfLocationData location2 = new CtfLocationData(1, 0);
+        CtfLocation location = new CtfLocation(location2);
+        location.setLocation(location2);
         double result = fixture.getLocationRatio(location);
 
         assertEquals(Double.NEGATIVE_INFINITY, result, 0.1);
@@ -242,7 +244,7 @@ public class CtfTmfTraceTest {
     @Test
     public void testGetNbEvents() {
         long result = fixture.getNbEvents();
-        assertEquals(0L, result);
+        assertEquals(1L, result);
     }
 
     /**
@@ -354,7 +356,8 @@ public class CtfTmfTraceTest {
      */
     @Test
     public void testSeekEvent_location() {
-        CtfLocation ctfLocation = new CtfLocation(new Long(1L));
+        final CtfLocationData location2 = new CtfLocationData(1L, 0L);
+        CtfLocation ctfLocation = new CtfLocation(location2);
         ITmfContext result = fixture.seekEvent(ctfLocation);
         assertNotNull(result);
     }
index 92d283f84981ec0728328a3995946fd9f16dc6dd..2de83ec3c6a0caceb1a057f14940f9fe1a092e09 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.runners.Suite;
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
     CtfIteratorTest.class,
+    CtfLocationDataTest.class,
     CtfLocationTest.class,
     CtfTmfContentTest.class,
     CtfTmfEventFieldTest.class,
index 39ffbf8343ae64cb023e93d83418412a55ff9cea..d215d76371f84bf47187d779a949d8797be0d00b 100644 (file)
@@ -24,15 +24,16 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
  * @version 1.0
  * @author Matthew Khouzam
  */
-public class CtfIterator extends CTFTraceReader implements ITmfContext, Comparable<CtfIterator>, Cloneable {
+public class CtfIterator extends CTFTraceReader implements ITmfContext,
+        Comparable<CtfIterator>, Cloneable {
 
     private final CtfTmfTrace ctfTmfTrace;
 
     /**
      * An invalid location
      */
-    final public static CtfLocation NULL_LOCATION = new CtfLocation(
-            CtfLocation.INVALID_LOCATION);
+    final public static CtfLocation NULL_LOCATION = new CtfLocation(CtfLocation.INVALID_LOCATION);
+
     private CtfLocation curLocation;
     private long curRank;
 
@@ -40,13 +41,13 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, Comparab
      * Create a new CTF trace iterator, which initially points at the first
      * event in the trace.
      *
-     * @param trace the trace to iterate over
+     * @param trace
+     *            the trace to iterate over
      */
     public CtfIterator(final CtfTmfTrace trace) {
         super(trace.getCTFTrace());
         this.ctfTmfTrace = trace;
         if (this.hasMoreEvents()) {
-
             this.curLocation = new CtfLocation(trace.getStartTime());
             this.curRank = 0;
         } else {
@@ -54,9 +55,6 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, Comparab
         }
     }
 
-    /**
-     *
-     */
     private void setUnknownLocation() {
         this.curLocation = NULL_LOCATION;
         this.curRank = UNKNOWN_RANK;
@@ -64,20 +62,24 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, Comparab
 
     /**
      * Constructor for CtfIterator.
-     * @param trace CtfTmfTrace the trace
-     * @param timestampValue long the timestamp in ns of the trace for positioning
-     * @param rank long the index of the trace for positioning
+     *
+     * @param trace
+     *            CtfTmfTrace the trace
+     * @param ctfLocationData
+     *            long the timestamp in ns of the trace for positioning
+     * @param rank
+     *            long the index of the trace for positioning
+     * @since 2.0
      */
-    public CtfIterator(final CtfTmfTrace trace, final long timestampValue,
-            final long rank) {
+    public CtfIterator(final CtfTmfTrace trace,
+            final CtfLocationData ctfLocationData, final long rank) {
         super(trace.getCTFTrace());
 
         this.ctfTmfTrace = trace;
         if (this.hasMoreEvents()) {
-            this.curLocation = (new CtfLocation(this.getCurrentEvent()
-                    .getTimestampValue()));
-            if (this.getCurrentEvent().getTimestampValue() != timestampValue) {
-                this.seek(timestampValue);
+            this.curLocation = new CtfLocation(ctfLocationData);
+            if (this.getCurrentEvent().getTimestampValue() != ctfLocationData.getTimestamp()) {
+                this.seek(ctfLocationData);
                 this.curRank = rank;
             }
         } else {
@@ -108,23 +110,43 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, Comparab
     }
 
     /**
-     * Method seek. Seeks to a given timestamp
-     * @param timestamp long the timestamp in ns (utc)
+     * Seek this iterator to a given location.
+     *
+     * @param ctfLocationData
+     *            The LocationData representing the position to seek to
      * @return boolean
+     * @since 2.0
      */
-    @Override
-    public boolean seek(final long timestamp) {
+    public boolean seek(final CtfLocationData ctfLocationData) {
         boolean ret = false;
-        final long offsetTimestamp = timestamp
-                - this.getTrace().getOffset();
+
+        /* Adjust the timestamp depending on the trace's offset */
+        long currTimestamp = ctfLocationData.getTimestamp();
+        final long offsetTimestamp = currTimestamp - this.getTrace().getOffset();
         if (offsetTimestamp < 0) {
             ret = super.seek(0L);
         } else {
             ret = super.seek(offsetTimestamp);
         }
 
+        /*
+         * Check if there is already one or more events for that timestamp, and
+         * assign the location index correctly
+         */
+        currTimestamp = this.getCurrentEvent().getTimestampValue();
+        long index = 0;
+        for (long i = 0; i < ctfLocationData.getIndex(); i++) {
+            if (currTimestamp == this.getCurrentEvent().getTimestampValue()) {
+                index++;
+            } else {
+                index = 0;
+            }
+            this.advance();
+        }
+
+        /* Seek the current location accordingly */
         if (ret) {
-            curLocation.setLocation(getCurrentEvent().getTimestampValue());
+            curLocation.setLocation(new CtfLocationData(getCurrentEvent().getTimestampValue(), index));
         } else {
             curLocation = NULL_LOCATION;
         }
@@ -159,7 +181,7 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, Comparab
     @Override
     public CtfIterator clone() {
         CtfIterator clone = null;
-        clone = new CtfIterator(ctfTmfTrace, this.getCurrentEvent().getTimestampValue(), curRank);
+        clone = new CtfIterator(ctfTmfTrace, this.getLocation().getLocation(), curRank);
         return clone;
     }
 
@@ -222,9 +244,17 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, Comparab
      */
     @Override
     public boolean advance() {
+        long index = curLocation.getLocation().getIndex();
+        long timestamp = curLocation.getLocation().getTimestamp();
         boolean ret = super.advance();
+
         if (ret) {
-            curLocation.setLocation(getCurrentEvent().getTimestampValue());
+            final long timestampValue = getCurrentEvent().getTimestampValue();
+            if (timestamp == timestampValue) {
+                curLocation.setLocation(timestampValue, index + 1);
+            } else {
+                curLocation.setLocation(timestampValue, 0L);
+            }
         } else {
             curLocation = NULL_LOCATION;
         }
index 865c551544b42094c3ce2383133dd44f7ea4e10c..b03fdbfd9c9a456dd1ed01db55cd10bc7c66f462 100644 (file)
@@ -142,7 +142,8 @@ class CtfTraceManager {
                  */
                 retVal = replaceRandomElement(context);
             }
-            retVal.seek((Long) context.getLocation().getLocation());
+            final CtfLocationData location = (CtfLocationData) context.getLocation().getLocation();
+            retVal.seek( location);
         }
         return retVal;
     }
index d5566053a17fa051f07800c414466aaa47c52f40..f218d3ff538e15ec661399a89d3a86760a1db388 100644 (file)
@@ -15,65 +15,99 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
 
 /**
  * The nugget of information that is unique to a location in a CTF trace.
- * 
+ *
  * It can be copied and used to restore a position in a given trace.
- * 
+ *
  * @version 1.0
  * @author Matthew Khouzam
  */
-public class CtfLocation implements ITmfLocation<Long>, Cloneable {
+public class CtfLocation implements ITmfLocation<CtfLocationData>, Cloneable {
+
+    private CtfLocationData fLocation;
 
     /**
      * An invalid location
      */
-    public static final Long INVALID_LOCATION = -1L;
+    public static final CtfLocationData INVALID_LOCATION = new CtfLocationData(-1, -1);
 
     /**
-     * Constructor for CtfLocation.
-     * @param location Long
+     * Copy constructor
+     *
+     * @param location
+     *            Other location to copy
+     * @since 2.0
      */
-    public CtfLocation(Long location) {
+    public CtfLocation(CtfLocationData location) {
         setLocation(location);
     }
 
     /**
-     * Constructor for CtfLocation.
-     * @param timestamp ITmfTimestamp
+     * Standard constructor
+     *
+     * @param timestamp
+     *            The timestamp of this location
+     * @param index
+     *            The index of this location for this timestamp
+     * @since 2.0
+     */
+    public CtfLocation(ITmfTimestamp timestamp, long index) {
+        setLocation(new CtfLocationData(timestamp.getValue(), index));
+    }
+
+    /**
+     * Constructor for CtfLocation. Uses a default index of 0.
+     *
+     * @param timestamp
+     *            The timestamp of this location
      */
+    @Deprecated
     public CtfLocation(ITmfTimestamp timestamp) {
-        setLocation(timestamp.getValue());
+        setLocation(new CtfLocationData(timestamp.getValue(), 0));
     }
 
-    private Long fTimestamp;
+    /**
+     * Move this location to another location's position.
+     *
+     * @param location
+     *            The location to seek to
+     * @since 2.0
+     */
+    public void setLocation(CtfLocationData location) {
+        this.fLocation = location;
+    }
 
     /**
-     * Method setLocation.
-     * @param location Long
+     * Change this location's timestamp and index values.
+     *
+     * @param timestampValue
+     *            The new timestamp
+     * @param index
+     *            The new index
+     * @since 2.0
      */
-    public void setLocation(Long location) {
-        this.fTimestamp = location;
+    public void setLocation(long timestampValue, long index) {
+       this.fLocation = new CtfLocationData(timestampValue, index);
     }
 
+
     /**
-     * Method getLocation.
-     * @return Long
+     * Get the Location Data of this location
+     *
+     * @return The CtfLocationData
      * @see org.eclipse.linuxtools.tmf.core.trace.ITmfLocation#getLocation()
+     * @since 2.0
      */
     @Override
-    public Long getLocation() {
-        return this.fTimestamp;
+    public CtfLocationData getLocation() {
+        return fLocation;
     }
 
-    /**
-     * Method clone.
-     * @return CtfLocation
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfLocation#clone()
-     */
     @Override
     public CtfLocation clone() {
-        return new CtfLocation(getLocation().longValue());
+        return new CtfLocation(new CtfLocationData(fLocation.getTimestamp(), fLocation.getIndex()));
     }
 
+
     /* (non-Javadoc)
      * @see java.lang.Object#hashCode()
      */
@@ -82,7 +116,7 @@ public class CtfLocation implements ITmfLocation<Long>, Cloneable {
         final int prime = 31;
         int result = 1;
         result = (prime * result)
-                + ((fTimestamp == null) ? 0 : fTimestamp.hashCode());
+                + ((fLocation == null) ? 0 : fLocation.hashCode());
         return result;
     }
 
@@ -101,11 +135,11 @@ public class CtfLocation implements ITmfLocation<Long>, Cloneable {
             return false;
         }
         CtfLocation other = (CtfLocation) obj;
-        if (fTimestamp == null) {
-            if (other.fTimestamp != null) {
+        if (fLocation == null) {
+            if (other.fLocation != null) {
                 return false;
             }
-        } else if (!fTimestamp.equals(other.fTimestamp)) {
+        } else if (!fLocation.equals(other.fLocation)) {
             return false;
         }
         return true;
@@ -122,4 +156,5 @@ public class CtfLocation implements ITmfLocation<Long>, Cloneable {
         return "CtfLocation: " + getLocation().toString(); //$NON-NLS-1$
     }
 
+
 }
diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocationData.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocationData.java
new file mode 100644 (file)
index 0000000..dbff2f4
--- /dev/null
@@ -0,0 +1,117 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Ericsson
+ *
+ * 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
+ *******************************************************************************/
+package org.eclipse.linuxtools.tmf.core.ctfadaptor;
+
+/**
+ * CtfLocationData, the data in a CTF location.
+ *
+ * @author Matthew Khouzam
+ * @since 2.0
+ */
+public class CtfLocationData implements Comparable<CtfLocationData> {
+
+    private final long timestamp;
+    private final long index;
+
+    /**
+     * @param ts
+     *            Timestamp
+     * @param index
+     *            Index of this event (if there are N elements with the same
+     *            timestamp, which one is it.)
+     */
+    public CtfLocationData(long ts, long index) {
+        this.timestamp = ts;
+        this.index = index;
+    }
+
+    /**
+     * @return The timestamp
+     */
+    public long getTimestamp() {
+        return timestamp;
+    }
+
+    /**
+     * @return The index of the element
+     */
+    public long getIndex() {
+        return index;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = (prime * result) + (int) (index ^ (index >>> 32));
+        result = (prime * result) + (int) (timestamp ^ (timestamp >>> 32));
+        return result;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (!(obj instanceof CtfLocationData)) {
+            return false;
+        }
+        CtfLocationData other = (CtfLocationData) obj;
+        if (index != other.index) {
+            return false;
+        }
+        if (timestamp != other.timestamp) {
+            return false;
+        }
+        return true;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return "Element [" + timestamp + '/' + index + ']'; //$NON-NLS-1$
+    }
+
+    @Override
+    public int compareTo(CtfLocationData other) {
+        if (this.timestamp > other.getTimestamp()) {
+            return 1;
+        }
+        if (this.timestamp < other.getTimestamp()) {
+            return -1;
+        }
+        if (this.index > other.getIndex()) {
+            return 1;
+        }
+        if (this.index < other.getIndex()) {
+            return -1;
+        }
+        return 0;
+    }
+
+}
index ef0b7e7060ee9441928a0e4da64960a0878dc410..1b79f303e763e4e674fb72c3233e6200ce0d487f 100644 (file)
@@ -21,7 +21,7 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
  * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to
  * a boxed Long, a long and an int.
  *
- * @versionj 1.0
+ * @version 1.0
  * @author Matthew Khouzam
  */
 public class CtfTmfLightweightContext implements ITmfContext {
@@ -49,7 +49,7 @@ public class CtfTmfLightweightContext implements ITmfContext {
     public CtfTmfLightweightContext(ArrayList<CtfIterator> iters,
             ListIterator<CtfIterator> pos) {
         fTrace = iters.get(0).getCtfTmfTrace();
-        curLocation = new CtfLocation((Long) null);
+        curLocation = new CtfLocation(new CtfLocationData(0, 0));
     }
 
     /**
@@ -60,7 +60,7 @@ public class CtfTmfLightweightContext implements ITmfContext {
      */
     public CtfTmfLightweightContext(CtfTmfTrace ctfTmfTrace) {
         fTrace = ctfTmfTrace;
-        curLocation = new CtfLocation((Long) null);
+        curLocation = new CtfLocation(new CtfLocationData(0, 0));
     }
 
     // -------------------------------------------
@@ -79,7 +79,7 @@ public class CtfTmfLightweightContext implements ITmfContext {
 
     @Override
     public boolean hasValidRank() {
-        return curRank != CtfLocation.INVALID_LOCATION;
+        return curRank != CtfLocation.INVALID_LOCATION.getTimestamp();
     }
 
     @Override
@@ -120,10 +120,17 @@ public class CtfTmfLightweightContext implements ITmfContext {
      * @return success or not
      */
     public synchronized boolean advance() {
+        final CtfLocationData curLocationData = this.curLocation.getLocation();
         boolean retVal = getIterator().advance();
         CtfTmfEvent currentEvent = getIterator().getCurrentEvent();
+
         if (currentEvent != null) {
-            curLocation.setLocation(currentEvent.getTimestampValue());
+            final long timestampValue = currentEvent.getTimestampValue();
+            if (curLocationData.getTimestamp() == timestampValue) {
+                curLocation.setLocation(timestampValue, curLocationData.getIndex() + 1);
+            } else {
+                curLocation.setLocation(timestampValue, 0L);
+            }
         } else {
             curLocation.setLocation(CtfLocation.INVALID_LOCATION);
         }
@@ -144,10 +151,23 @@ public class CtfTmfLightweightContext implements ITmfContext {
      * @return success or not
      */
     public synchronized boolean seek(final long timestamp) {
-        curLocation.setLocation(timestamp);
+        curLocation.setLocation(timestamp, 0);
         return getIterator().seek(timestamp);
     }
 
+    /**
+     * Seeks to a given location. Wrapper to help CtfTmfTrace
+     * @param location
+     *              unique location to find the event.
+     *
+     * @return success or not
+     * @since 2.0
+     */
+    public synchronized boolean seek(final CtfLocationData location) {
+        curLocation.setLocation(location);
+        return getIterator().seek(location);
+    }
+
     /*
      * (non-Javadoc)
      *
index 8ae3189e165bf7bfcdecfa240fd3a4c799903fb5..dcf21ed4dfc6e6600c0ff57ff94a69f637501536 100644 (file)
@@ -89,13 +89,13 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
             CtfIteratorManager.addTrace(this);
             CtfTmfLightweightContext ctx;
             /* Set the start and (current) end times for this trace */
-            ctx = new CtfTmfLightweightContext(this);
-            ctx.setLocation(new CtfLocation(0L));
+            ctx = (CtfTmfLightweightContext) seekEvent(0L);
+            CtfTmfEvent event = getNext(ctx);
             if(ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) {
                 /* Handle the case where the trace is empty */
                 this.setStartTime(TmfTimestamp.BIG_BANG);
             } else {
-                final ITmfTimestamp curTime = ctx.getCurrentEvent().getTimestamp();
+                final ITmfTimestamp curTime = event.getTimestamp();
                 this.setStartTime(curTime);
                 this.setEndTime(curTime);
             }
@@ -167,10 +167,10 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
         final CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
         context.setLocation(curLocation);
         context.seek(curLocation.getLocation());
-        final long currentTime = ((Long)context.getLocation().getLocation());
+        final CtfLocationData currentTime = ((CtfLocationData)context.getLocation().getLocation());
         final long startTime = getIterator(this, context).getStartTime();
         final long endTime = getIterator(this, context).getEndTime();
-        return ((double) currentTime - startTime)
+        return ((double) currentTime.getTimestamp() - startTime)
                 / (endTime - startTime);
     }
 
@@ -206,12 +206,12 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
          * by rank for now.
          */
         if (currentLocation == null) {
-            currentLocation = new CtfLocation(0L);
+            currentLocation = new CtfLocation(new CtfLocationData(0L, 0L));
             context.setRank(0);
         }
         if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
             ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
-            currentLocation.setLocation(getEndTime().getValue() + 1);
+            currentLocation.setLocation(getEndTime().getValue() + 1, 0L);
         }
         context.setLocation(currentLocation);
         if(context.getRank() != 0) {
This page took 0.044588 seconds and 5 git commands to generate.