From 27213c574d35eec10b07d603419f54e5d4e0ef59 Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Thu, 27 Mar 2014 15:53:52 -0400 Subject: [PATCH] tmf: Replace event matching API to use Collection instead of straight arrays. This is more flexible, and indicates that there is no notion of ordering of the traces. Had to replace one instance of List.get(i) with a map of traces. Change-Id: Ia25d79b4735b0acb5d4c56975e5e384ad39ad583 Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/24078 Tested-by: Hudson CI Reviewed-by: Genevieve Bastien IP-Clean: Genevieve Bastien Tested-by: Genevieve Bastien --- .../event/matchandsync/MatchAndSyncTest.java | 10 ++-- .../core/tests/synchronization/SyncTest.java | 11 ++++- .../event/matching/IMatchProcessingUnit.java | 4 +- .../core/event/matching/TmfEventMatches.java | 3 +- .../core/event/matching/TmfEventMatching.java | 30 ++++++------ .../matching/TmfNetworkEventMatching.java | 47 ++++++++----------- .../SyncAlgorithmFullyIncremental.java | 10 ++-- .../SynchronizationManager.java | 7 +-- .../tmf/core/trace/TmfExperiment.java | 3 +- 9 files changed, 67 insertions(+), 58 deletions(-) diff --git a/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/event/matchandsync/MatchAndSyncTest.java b/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/event/matchandsync/MatchAndSyncTest.java index 6d8856db81..1347e700a4 100644 --- a/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/event/matchandsync/MatchAndSyncTest.java +++ b/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/event/matchandsync/MatchAndSyncTest.java @@ -16,12 +16,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; +import java.util.LinkedList; +import java.util.List; + import org.eclipse.linuxtools.lttng2.kernel.core.event.matching.TcpEventMatching; import org.eclipse.linuxtools.lttng2.kernel.core.event.matching.TcpLttngEventMatching; import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventMatching; import org.eclipse.linuxtools.tmf.core.event.matching.TmfNetworkEventMatching; import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; +import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; import org.junit.Test; /** @@ -43,9 +47,9 @@ public class MatchAndSyncTest { CtfTmfTrace trace1 = CtfTmfTestTrace.SYNC_SRC.getTrace(); CtfTmfTrace trace2 = CtfTmfTestTrace.SYNC_DEST.getTrace(); - CtfTmfTrace[] tracearr = new CtfTmfTrace[2]; - tracearr[0] = trace1; - tracearr[1] = trace2; + List tracearr = new LinkedList<>(); + tracearr.add(trace1); + tracearr.add(trace2); TmfEventMatching.registerMatchObject(new TcpEventMatching()); TmfEventMatching.registerMatchObject(new TcpLttngEventMatching()); diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/SyncTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/SyncTest.java index 46742f4de6..7a815603c5 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/SyncTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/SyncTest.java @@ -14,6 +14,9 @@ package org.eclipse.linuxtools.tmf.core.tests.synchronization; import static org.junit.Assert.assertEquals; +import java.util.Collection; +import java.util.LinkedList; + import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventDependency; import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform; import org.eclipse.linuxtools.tmf.core.synchronization.SyncAlgorithmFullyIncremental; @@ -21,6 +24,7 @@ import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationAlgorithm; import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationAlgorithm.SyncQuality; import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform; import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; +import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; import org.eclipse.linuxtools.tmf.tests.stubs.event.TmfSyncEventStub; import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; import org.junit.Before; @@ -35,7 +39,7 @@ import org.junit.Test; public class SyncTest { private TmfTraceStub t1, t2; - private TmfTraceStub[] fTraces; + private Collection fTraces; /** * Initializing the traces @@ -46,7 +50,10 @@ public class SyncTest { t1.init("t1"); t2 = new TmfTraceStub(); t2.init("t2"); - TmfTraceStub[] traces = { t1, t2 }; + + Collection traces = new LinkedList<>(); + traces.add(t1); + traces.add(t2); fTraces = traces; } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/IMatchProcessingUnit.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/IMatchProcessingUnit.java index 063f15cefc..4fc90cc843 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/IMatchProcessingUnit.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/IMatchProcessingUnit.java @@ -12,6 +12,8 @@ package org.eclipse.linuxtools.tmf.core.event.matching; +import java.util.Collection; + import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; /** @@ -30,7 +32,7 @@ public interface IMatchProcessingUnit { * * @param fTraces the set of traces that will be synchronized */ - void init(ITmfTrace[] fTraces); + void init(Collection fTraces); /** * Function called when a match is found diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfEventMatches.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfEventMatches.java index 1eb0bfdacc..d966f84c11 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfEventMatches.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfEventMatches.java @@ -13,6 +13,7 @@ package org.eclipse.linuxtools.tmf.core.event.matching; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; @@ -44,7 +45,7 @@ public class TmfEventMatches implements IMatchProcessingUnit { */ @Override - public void init(ITmfTrace[] fTraces) { + public void init(Collection fTraces) { } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfEventMatching.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfEventMatching.java index a7fcf7377d..7b39263253 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfEventMatching.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfEventMatching.java @@ -13,6 +13,7 @@ package org.eclipse.linuxtools.tmf.core.event.matching; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -49,7 +50,7 @@ public abstract class TmfEventMatching implements ITmfEventMatching { /** * The array of traces to match */ - private final ITmfTrace[] fTraces; + private final Collection fTraces; /** * The class to call once a match is found @@ -68,7 +69,7 @@ public abstract class TmfEventMatching implements ITmfEventMatching { * @param tmfEventMatches * The match processing class */ - public TmfEventMatching(ITmfTrace[] traces, IMatchProcessingUnit tmfEventMatches) { + public TmfEventMatching(Collection traces, IMatchProcessingUnit tmfEventMatches) { if (tmfEventMatches == null) { throw new IllegalArgumentException(); } @@ -81,7 +82,7 @@ public abstract class TmfEventMatching implements ITmfEventMatching { * * @return The traces */ - protected ITmfTrace[] getTraces() { + protected Collection getTraces() { return fTraces; } @@ -149,10 +150,10 @@ public abstract class TmfEventMatching implements ITmfEventMatching { * * @param event * The event to match - * @param traceno - * The number of the trace this event belongs to + * @param trace + * The trace to which this event belongs */ - protected abstract void matchEvent(ITmfEvent event, int traceno); + protected abstract void matchEvent(ITmfEvent event, ITmfTrace trace); /** * Returns the matching type this class implements @@ -170,7 +171,7 @@ public abstract class TmfEventMatching implements ITmfEventMatching { public boolean matchEvents() { /* Are there traces to match? If no, return false */ - if (!(fTraces.length > 0)) { + if (!(fTraces.size() > 0)) { return false; } @@ -196,15 +197,14 @@ public abstract class TmfEventMatching implements ITmfEventMatching { * would be preferable to have experiment * */ - for (int i = 0; i < fTraces.length; i++) { - - EventMatchingBuildRequest request = new EventMatchingBuildRequest(this, i); + for (ITmfTrace trace : fTraces) { + EventMatchingBuildRequest request = new EventMatchingBuildRequest(this, trace); /* * Send the request to the trace here, since there is probably no * experiment. */ - fTraces[i].sendRequest(request); + trace.sendRequest(request); try { request.waitForCompletion(); } catch (InterruptedException e) { @@ -237,23 +237,23 @@ public abstract class TmfEventMatching implements ITmfEventMatching { class EventMatchingBuildRequest extends TmfEventRequest { private final TmfEventMatching matching; - private final int traceno; + private final ITmfTrace trace; - EventMatchingBuildRequest(TmfEventMatching matching, int traceno) { + EventMatchingBuildRequest(TmfEventMatching matching, ITmfTrace trace) { super(CtfTmfEvent.class, TmfTimeRange.ETERNITY, 0, ITmfEventRequest.ALL_DATA, ITmfEventRequest.ExecutionType.FOREGROUND); this.matching = matching; - this.traceno = traceno; + this.trace = trace; } @Override public void handleData(final ITmfEvent event) { super.handleData(event); if (event != null) { - matching.matchEvent(event, traceno); + matching.matchEvent(event, trace); } } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfNetworkEventMatching.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfNetworkEventMatching.java index 5e75c7d6cc..384f4634aa 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfNetworkEventMatching.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/matching/TmfNetworkEventMatching.java @@ -12,15 +12,13 @@ package org.eclipse.linuxtools.tmf.core.event.matching; -import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; -import org.eclipse.linuxtools.tmf.core.event.matching.IMatchProcessingUnit; -import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventDependency; -import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventMatching; import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; /** @@ -35,12 +33,12 @@ public class TmfNetworkEventMatching extends TmfEventMatching { /** * Hashtables for unmatches incoming events */ - private final List, ITmfEvent>> fUnmatchedIn = new ArrayList<>(); + private final Map, ITmfEvent>> fUnmatchedIn = new LinkedHashMap<>(); /** * Hashtables for unmatches outgoing events */ - private final List, ITmfEvent>> fUnmatchedOut = new ArrayList<>(); + private final Map, ITmfEvent>> fUnmatchedOut = new LinkedHashMap<>(); /** * Enum for in and out types @@ -62,7 +60,7 @@ public class TmfNetworkEventMatching extends TmfEventMatching { * @param traces * The set of traces for which to match events */ - public TmfNetworkEventMatching(ITmfTrace[] traces) { + public TmfNetworkEventMatching(Collection traces) { this(traces, new TmfEventMatches()); } @@ -74,7 +72,7 @@ public class TmfNetworkEventMatching extends TmfEventMatching { * @param tmfEventMatches * The match processing class */ - public TmfNetworkEventMatching(ITmfTrace[] traces, IMatchProcessingUnit tmfEventMatches) { + public TmfNetworkEventMatching(Collection traces, IMatchProcessingUnit tmfEventMatches) { super(traces, tmfEventMatches); } @@ -86,9 +84,9 @@ public class TmfNetworkEventMatching extends TmfEventMatching { // Initialize the matching infrastructure (unmatched event lists) fUnmatchedIn.clear(); fUnmatchedOut.clear(); - for (int i = 0; i < getTraces().length; i++) { - fUnmatchedIn.add(new HashMap, ITmfEvent>()); - fUnmatchedOut.add(new HashMap, ITmfEvent>()); + for (ITmfTrace trace : getTraces()) { + fUnmatchedIn.put(trace, new HashMap, ITmfEvent>()); + fUnmatchedOut.put(trace, new HashMap, ITmfEvent>()); } super.initMatching(); } @@ -109,16 +107,8 @@ public class TmfNetworkEventMatching extends TmfEventMatching { return MatchingType.NETWORK; } - /** - * Matches one event - * - * @param event - * The event to match - * @param traceno - * The number of the trace this event belongs to - */ @Override - public void matchEvent(ITmfEvent event, int traceno) { + public void matchEvent(ITmfEvent event, ITmfTrace trace) { if (!(getEventDefinition(event.getTrace()) instanceof ITmfNetworkMatchDefinition)) { return; } @@ -131,7 +121,7 @@ public class TmfNetworkEventMatching extends TmfEventMatching { /* Get the event's unique fields */ List eventKey = def.getUniqueField(event); - List, ITmfEvent>> unmatchedTbl, companionTbl; + Map, ITmfEvent>> unmatchedTbl, companionTbl; /* Point to the appropriate table */ switch (evType) { @@ -150,7 +140,7 @@ public class TmfNetworkEventMatching extends TmfEventMatching { boolean found = false; TmfEventDependency dep = null; /* Search for the event in the companion table */ - for (Map, ITmfEvent> map : companionTbl) { + for (Map, ITmfEvent> map : companionTbl.values()) { if (map.containsKey(eventKey)) { found = true; ITmfEvent companionEvent = map.get(eventKey); @@ -191,8 +181,8 @@ public class TmfNetworkEventMatching extends TmfEventMatching { * events as value for the unmatched table. Not necessary right now * though */ - if (!unmatchedTbl.get(traceno).containsKey(eventKey)) { - unmatchedTbl.get(traceno).put(eventKey, event); + if (!unmatchedTbl.get(trace).containsKey(eventKey)) { + unmatchedTbl.get(trace).put(eventKey, event); } } @@ -209,10 +199,11 @@ public class TmfNetworkEventMatching extends TmfEventMatching { final String cr = System.getProperty("line.separator"); StringBuilder b = new StringBuilder(); b.append(getProcessingUnit()); - for (int i = 0; i < getTraces().length; i++) { - b.append("Trace " + i + ":" + cr + - " " + countEvents(fUnmatchedIn.get(i)) + " unmatched incoming events" + cr + - " " + countEvents(fUnmatchedOut.get(i)) + " unmatched outgoing events" + cr); + int i = 0; + for (ITmfTrace trace : getTraces()) { + b.append("Trace " + i++ + ":" + cr + + " " + countEvents(fUnmatchedIn.get(trace)) + " unmatched incoming events" + cr + + " " + countEvents(fUnmatchedOut.get(trace)) + " unmatched outgoing events" + cr); } return b.toString(); diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/SyncAlgorithmFullyIncremental.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/SyncAlgorithmFullyIncremental.java index 6cb695ab3f..dee9c92497 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/SyncAlgorithmFullyIncremental.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/SyncAlgorithmFullyIncremental.java @@ -17,6 +17,7 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.math.BigDecimal; import java.math.MathContext; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; @@ -70,12 +71,13 @@ public class SyncAlgorithmFullyIncremental extends SynchronizationAlgorithm { } @Override - public void init(ITmfTrace[] fTraces) { + public void init(Collection traces) { + ITmfTrace[] traceArr = traces.toArray(new ITmfTrace[traces.size()]); fSyncs.clear(); /* Create a convex hull for all trace pairs */ - for (int i = 0; i < fTraces.length; i++) { - for (int j = i + 1; j < fTraces.length; j++) { - ConvexHull algo = new ConvexHull(fTraces[i].getName(), fTraces[j].getName()); + for (int i = 0; i < traceArr.length; i++) { + for (int j = i + 1; j < traceArr.length; j++) { + ConvexHull algo = new ConvexHull(traceArr[i].getName(), traceArr[j].getName()); fSyncs.add(algo); } } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/SynchronizationManager.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/SynchronizationManager.java index fac513386b..cf7144dda3 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/SynchronizationManager.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/SynchronizationManager.java @@ -14,6 +14,7 @@ package org.eclipse.linuxtools.tmf.core.synchronization; import java.io.File; import java.io.IOException; +import java.util.Collection; import org.eclipse.linuxtools.internal.tmf.core.Activator; import org.eclipse.linuxtools.tmf.core.component.TmfComponent; @@ -45,7 +46,7 @@ public abstract class SynchronizationManager extends TmfComponent { * file * @return The synchronization object */ - public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, final ITmfTrace[] traces, boolean doSync) { + public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, final Collection traces, boolean doSync) { SynchronizationAlgorithm syncAlgo; if (doSync) { @@ -78,7 +79,7 @@ public abstract class SynchronizationManager extends TmfComponent { * file * @return The synchronization object */ - public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, final ITmfTrace[] traces, SynchronizationAlgorithm algo, boolean doSync) { + public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, final Collection traces, SynchronizationAlgorithm algo, boolean doSync) { SynchronizationAlgorithm syncAlgo; if (doSync) { @@ -116,7 +117,7 @@ public abstract class SynchronizationManager extends TmfComponent { return null; } - private static SynchronizationAlgorithm synchronize(final File syncFile, final ITmfTrace[] traces, SynchronizationAlgorithm syncAlgo) { + private static SynchronizationAlgorithm synchronize(final File syncFile, final Collection traces, SynchronizationAlgorithm syncAlgo) { ITmfEventMatching matching = new TmfNetworkEventMatching(traces, syncAlgo); matching.matchEvents(); diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java index 013934fcc4..2985e45f48 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfExperiment.java @@ -19,6 +19,7 @@ package org.eclipse.linuxtools.tmf.core.trace; import java.io.File; import java.nio.ByteBuffer; +import java.util.Arrays; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -538,7 +539,7 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser, ITmfPers final File syncFile = (supplDirectory != null) ? new File(supplDirectory + File.separator + SYNCHRONIZATION_FILE_NAME) : null; - final SynchronizationAlgorithm syncAlgo = SynchronizationManager.synchronizeTraces(syncFile, fTraces, doSync); + final SynchronizationAlgorithm syncAlgo = SynchronizationManager.synchronizeTraces(syncFile, Arrays.asList(fTraces), doSync); final TmfTraceSynchronizedSignal signal = new TmfTraceSynchronizedSignal(this, syncAlgo); -- 2.34.1