From: Geneviève Bastien Date: Tue, 2 Sep 2014 19:11:23 +0000 (-0400) Subject: TMF: Improve the serialization of trace synchronization X-Git-Url: http://drtracing.org/?a=commitdiff_plain;ds=sidebyside;h=a1ff9910545f62d6e26a9c190c393e85be26dae4;p=deliverable%2Ftracecompass.git TMF: Improve the serialization of trace synchronization Made some fields transient, so they are not serialized and initialize the transient fields in the readObject method. Also added serialization unit tests. Change-Id: I103b01b36efa66d27c37afba35935e415f4f0cc4 Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/35345 Tested-by: Hudson CI Reviewed-by: Matthew Khouzam --- diff --git a/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/synchronization/SyncTest.java b/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/synchronization/SyncTest.java index 5b77a911aa..fb43683e48 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/synchronization/SyncTest.java +++ b/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/synchronization/SyncTest.java @@ -13,16 +13,26 @@ package org.eclipse.tracecompass.tmf.core.tests.synchronization; import static org.junit.Assert.assertEquals; - +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.Collection; import java.util.LinkedList; +import org.eclipse.tracecompass.internal.tmf.core.synchronization.SyncAlgorithmFullyIncremental; import org.eclipse.tracecompass.tmf.core.event.matching.TmfEventDependency; import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform; import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm; +import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm.SyncQuality; import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithmFactory; import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory; -import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm.SyncQuality; import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; import org.eclipse.tracecompass.tmf.tests.stubs.event.TmfSyncEventStub; @@ -69,59 +79,35 @@ public class SyncTest { syncAlgo.init(fTraces); assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(1)), - new TmfSyncEventStub(t1, new TmfTimestamp(1)) - )); + addSyncMatch(syncAlgo, t2, 1, t1, 1); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString()); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)), - new TmfSyncEventStub(t2, new TmfTimestamp(3)) - )); + addSyncMatch(syncAlgo, t1, 1, t2, 3); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString()); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(2)), - new TmfSyncEventStub(t1, new TmfTimestamp(3)) - )); + addSyncMatch(syncAlgo, t2, 2, t1, 3); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0.5 ]]", syncAlgo.toString()); assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)), - new TmfSyncEventStub(t2, new TmfTimestamp(5)) - )); + addSyncMatch(syncAlgo, t1, 3, t2, 5); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString()); assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)), - new TmfSyncEventStub(t2, new TmfTimestamp(8)) - )); + addSyncMatch(syncAlgo, t1, 4, t2, 8); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString()); assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(4)), - new TmfSyncEventStub(t1, new TmfTimestamp(5)) - )); + addSyncMatch(syncAlgo, t2, 4, t1, 5); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString()); assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(4)), - new TmfSyncEventStub(t1, new TmfTimestamp(6)) - )); + addSyncMatch(syncAlgo, t2, 4, t1, 6); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString()); assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(6)), - new TmfSyncEventStub(t2, new TmfTimestamp(7)) - )); + addSyncMatch(syncAlgo, t1, 6, t2, 7); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.725 beta 1.275 ]]", syncAlgo.toString()); assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2)); @@ -133,14 +119,8 @@ public class SyncTest { assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2); /* Make the two hulls intersect */ - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(7)), - new TmfSyncEventStub(t2, new TmfTimestamp(4)) - )); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(7)), - new TmfSyncEventStub(t1, new TmfTimestamp(3)) - )); + addSyncMatch(syncAlgo, t1, 7, t2, 4); + addSyncMatch(syncAlgo, t2, 7, t1, 3); assertEquals(SyncQuality.FAIL, syncAlgo.getSynchronizationQuality(t1, t2)); } @@ -157,29 +137,16 @@ public class SyncTest { assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)), - new TmfSyncEventStub(t2, new TmfTimestamp(3))) - ); + addSyncMatch(syncAlgo, t1, 1, t2, 3); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(2)), - new TmfSyncEventStub(t2, new TmfTimestamp(5))) - ); - + addSyncMatch(syncAlgo, t1, 2, t2, 5); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)), - new TmfSyncEventStub(t2, new TmfTimestamp(5))) - ); + addSyncMatch(syncAlgo, t1, 3, t2, 5); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)), - new TmfSyncEventStub(t2, new TmfTimestamp(7))) - ); + addSyncMatch(syncAlgo, t1, 4, t2, 7); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString()); @@ -199,49 +166,101 @@ public class SyncTest { assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)), - new TmfSyncEventStub(t2, new TmfTimestamp(3))) - ); + addSyncMatch(syncAlgo, t1, 1, t2, 3); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(2)), - new TmfSyncEventStub(t2, new TmfTimestamp(5))) - ); - + addSyncMatch(syncAlgo, t1, 2, t2, 5); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)), - new TmfSyncEventStub(t2, new TmfTimestamp(5))) - ); + addSyncMatch(syncAlgo, t1, 3, t2, 5); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)), - new TmfSyncEventStub(t2, new TmfTimestamp(7))) - ); + addSyncMatch(syncAlgo, t1, 4, t2, 7); assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2)); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString()); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(7)), - new TmfSyncEventStub(t1, new TmfTimestamp(6))) - ); + addSyncMatch(syncAlgo, t2, 7, t1, 6); assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(8)), - new TmfSyncEventStub(t1, new TmfTimestamp(6))) - ); + addSyncMatch(syncAlgo, t2, 8, t1, 6); assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2)); - syncAlgo.addMatch( - new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(10)), - new TmfSyncEventStub(t1, new TmfTimestamp(8))) - ); + addSyncMatch(syncAlgo, t2, 10, t1, 8); assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2)); assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 2.5 ]]", syncAlgo.toString()); } + + private static void addSyncMatch(SynchronizationAlgorithm algo, ITmfTrace sender, long sendTs, ITmfTrace receiver, long receiveTs) { + algo.addMatch( + new TmfEventDependency( + new TmfSyncEventStub(sender, new TmfTimestamp(sendTs)), + new TmfSyncEventStub(receiver, new TmfTimestamp(receiveTs)) + )); + } + + /** + * Testing the serialization of the fully incremental synchronization + * algorithm + */ + @Test + public void testFullyIncrementalSerialization() { + + /* Do a run of synchronization and check the results */ + SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm(); + + syncAlgo.init(fTraces); + + addSyncMatch(syncAlgo, t2, 1, t1, 1); + addSyncMatch(syncAlgo, t1, 1, t2, 3); + addSyncMatch(syncAlgo, t2, 2, t1, 3); + addSyncMatch(syncAlgo, t1, 3, t2, 5); + addSyncMatch(syncAlgo, t1, 4, t2, 8); + addSyncMatch(syncAlgo, t2, 4, t1, 5); + addSyncMatch(syncAlgo, t2, 4, t1, 6); + addSyncMatch(syncAlgo, t1, 6, t2, 7); + + ITmfTimestampTransform tt2 = syncAlgo.getTimestampTransform(t2); + ITmfTimestampTransform tt1 = syncAlgo.getTimestampTransform(t1); + + assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2)); + assertEquals(syncAlgo.getTimestampTransform(t1.getHostId()), tt1); + assertEquals(TimestampTransformFactory.getDefaultTransform(), tt1); + assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2); + + /* Serialize the object */ + String filePath = null; + try { + File temp = File.createTempFile("serialSyncAlgo", ".tmp"); + filePath = temp.getAbsolutePath(); + } catch (IOException e) { + fail("Could not create temporary file for serialization"); + } + assertNotNull(filePath); + + try (FileOutputStream fileOut = new FileOutputStream(filePath); + ObjectOutputStream out = new ObjectOutputStream(fileOut);) { + out.writeObject(syncAlgo); + + } catch (IOException e) { + fail("Error serializing the synchronization algorithm " + e.getMessage()); + } + + SynchronizationAlgorithm deserialAlgo = null; + /* De-Serialize the object */ + try (FileInputStream fileIn = new FileInputStream(filePath); + ObjectInputStream in = new ObjectInputStream(fileIn);) { + deserialAlgo = (SynchronizationAlgorithm) in.readObject(); + } catch (IOException | ClassNotFoundException e) { + fail("Error de-serializing the synchronization algorithm " + e.getMessage()); + } + + /* Check that the deserialize algorithm is equivalent to original */ + assertNotNull(deserialAlgo); + assertTrue(deserialAlgo instanceof SyncAlgorithmFullyIncremental); + assertEquals(SyncQuality.ACCURATE, deserialAlgo.getSynchronizationQuality(t1, t2)); + assertEquals(tt1, deserialAlgo.getTimestampTransform(t1)); + assertEquals(tt2, deserialAlgo.getTimestampTransform(t2)); + + } + } diff --git a/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/synchronization/SyncAlgorithmFullyIncremental.java b/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/synchronization/SyncAlgorithmFullyIncremental.java index 654740352f..06a826bb0f 100644 --- a/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/synchronization/SyncAlgorithmFullyIncremental.java +++ b/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/synchronization/SyncAlgorithmFullyIncremental.java @@ -14,7 +14,7 @@ package org.eclipse.tracecompass.internal.tmf.core.synchronization; import java.io.IOException; -import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; import java.io.Serializable; import java.math.BigDecimal; import java.math.MathContext; @@ -59,7 +59,7 @@ public class SyncAlgorithmFullyIncremental extends SynchronizationAlgorithm { /** @Serial */ private final List fSyncs; - private SyncSpanningTree fTree = null; + private transient SyncSpanningTree fTree = null; /** * Initialization of the attributes @@ -211,35 +211,34 @@ public class SyncAlgorithmFullyIncremental extends SynchronizationAlgorithm { private static final long serialVersionUID = 8309351175030935291L; + private final String fReferenceHost; + private final String fOtherHost; + + /** + * Slopes and ordinate at origin of respectively fLmin, fLmax and the + * bisector + */ + private BigDecimal fAlphamin, fBetamax, fAlphamax, fBetamin, fAlpha, fBeta; + private int fNbMatches, fNbAccurateMatches; + private SyncQuality fQuality; + /** * The list of meaningful points on the upper hull (received by the * reference trace, below in a graph) */ - private final LinkedList fUpperBoundList = new LinkedList<>(); - + private transient LinkedList fUpperBoundList = new LinkedList<>(); /** * The list of meaninful points on the lower hull (sent by the reference * trace, above in a graph) */ - private final LinkedList fLowerBoundList = new LinkedList<>(); + private transient LinkedList fLowerBoundList = new LinkedList<>(); /** Points forming the line with maximum slope */ - private final SyncPoint[] fLmax; - + private transient SyncPoint[] fLmax = new SyncPoint[2]; /** Points forming the line with minimum slope */ - private final SyncPoint[] fLmin; - - /** - * Slopes and ordinate at origin of respectively fLmin, fLmax and the - * bisector - */ - private BigDecimal fAlphamin, fBetamax, fAlphamax, fBetamin, fAlpha, fBeta; - - private int fNbMatches, fNbAccurateMatches; - private String fReferenceHost = "", fOtherHost = ""; //$NON-NLS-1$//$NON-NLS-2$ - private SyncQuality fQuality; + private transient SyncPoint[] fLmin = new SyncPoint[2]; - private Map fStats = new LinkedHashMap<>(); + private transient Map fStats = new LinkedHashMap<>(); /** * Initialization of the attributes @@ -257,8 +256,6 @@ public class SyncAlgorithmFullyIncremental extends SynchronizationAlgorithm { fReferenceHost = host1; fOtherHost = host2; } - fLmax = new SyncPoint[2]; - fLmin = new SyncPoint[2]; fAlpha = BigDecimal.ONE; fAlphamax = BigDecimal.ONE; fAlphamin = BigDecimal.ONE; @@ -516,19 +513,16 @@ public class SyncAlgorithmFullyIncremental extends SynchronizationAlgorithm { return ((fReferenceHost.equals(hostId1) && fOtherHost.equals(hostId2)) || (fReferenceHost.equals(hostId2) && fOtherHost.equals(hostId1))); } - private void writeObject(ObjectOutputStream s) - throws IOException { - /* - * Remove calculation data because most of it is not serializable. - * We have the statistics anyway - */ - fUpperBoundList.clear(); - fLowerBoundList.clear(); - fLmin[0] = null; - fLmin[1] = null; - fLmax[0] = null; - fLmax[1] = null; - s.defaultWriteObject(); + private void readObject(ObjectInputStream stream) + throws IOException, ClassNotFoundException { + stream.defaultReadObject(); + + /* Initialize transient fields */ + fUpperBoundList = new LinkedList<>(); + fLowerBoundList = new LinkedList<>(); + fLmax = new SyncPoint[2]; + fLmin = new SyncPoint[2]; + fStats = new LinkedHashMap<>(); } @SuppressWarnings("nls") @@ -611,13 +605,4 @@ public class SyncAlgorithmFullyIncremental extends SynchronizationAlgorithm { } } - private void writeObject(ObjectOutputStream s) - throws IOException { - /* - * Remove the tree because it is not serializable - */ - fTree = null; - s.defaultWriteObject(); - } - }