Tmf: Trace synchronization using network events
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfExperiment.java
index 8ecd7ed5da814b10788fa385d808c6b37ae6626b..8f5d2c6181951ba0cb410f92a3b65814f1f8e0d2 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2013 Ericsson
+ * Copyright (c) 2009, 2013 Ericsson, École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  *   Francois Chouinard - Updated as per TMF Trace Model 1.0
  *   Patrick Tasse - Updated for removal of context clone
  *   Patrick Tasse - Updated for ranks in experiment location
+ *   Geneviève Bastien - Added support of experiment synchronization
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.trace;
 
+import java.io.File;
+
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.linuxtools.internal.tmf.core.Activator;
 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray;
+import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
@@ -30,6 +36,9 @@ import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
+import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSynchronizedSignal;
+import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationAlgorithm;
+import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationManager;
 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
@@ -49,6 +58,13 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // Constants
     // ------------------------------------------------------------------------
 
+    /**
+     * The file name of the Synchronization
+     *
+     * @since 3.0
+     */
+    public final static String SYNCHRONIZATION_FILE_NAME = "synchronization.bin"; //$NON-NLS-1$
+
     /**
      * The default index page size
      */
@@ -78,9 +94,12 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // ------------------------------------------------------------------------
 
     /**
-     * @param type the event type
-     * @param id the experiment id
-     * @param traces the experiment set of traces
+     * @param type
+     *            the event type
+     * @param id
+     *            the experiment id
+     * @param traces
+     *            the experiment set of traces
      */
     public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces) {
         this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, null);
@@ -102,12 +121,15 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
         this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, resource);
     }
 
-
     /**
-     * @param type the event type
-     * @param path the experiment path
-     * @param traces the experiment set of traces
-     * @param indexPageSize the experiment index page size
+     * @param type
+     *            the event type
+     * @param path
+     *            the experiment path
+     * @param traces
+     *            the experiment set of traces
+     * @param indexPageSize
+     *            the experiment index page size
      */
     public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize) {
         this(type, path, traces, indexPageSize, null);
@@ -140,6 +162,14 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
         }
 
         fTraces = traces;
+
+        if (resource != null) {
+            try {
+                this.synchronizeTraces();
+            } catch (TmfTraceException e) {
+                Activator.logError("Error synchronizing experiment", e); //$NON-NLS-1$
+            }
+        }
     }
 
     /**
@@ -195,7 +225,8 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
      * Returns the timestamp of the event at the requested index. If none,
      * returns null.
      *
-     * @param index the event index (rank)
+     * @param index
+     *            the event index (rank)
      * @return the corresponding event timestamp
      * @since 2.0
      */
@@ -209,7 +240,8 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     /**
      * Set the file to be used for bookmarks on this experiment
      *
-     * @param file the bookmarks file
+     * @param file
+     *            the bookmarks file
      */
     public void setBookmarksFile(final IFile file) {
         fBookmarksFile = file;
@@ -240,8 +272,8 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
         }
 
         if (request instanceof ITmfEventRequest
-            && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
-            && request.getIndex() == 0)
+                && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
+                && request.getIndex() == 0)
         {
             final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime());
             ((ITmfEventRequest) request).setStartIndex((int) context.getRank());
@@ -362,7 +394,8 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
 
         TmfExperimentContext expContext = (TmfExperimentContext) context;
 
-        // If an event was consumed previously, first get the next one from that trace
+        // If an event was consumed previously, first get the next one from that
+        // trace
         final int lastTrace = expContext.getLastTrace();
         if (lastTrace != TmfExperimentContext.NO_TRACE) {
             final ITmfContext traceContext = expContext.getContexts()[lastTrace];
@@ -425,6 +458,62 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
         return initTs;
     }
 
+    /**
+     * Synchronizes the traces of an experiment. By default it only tries to
+     * read a synchronization file if it exists
+     *
+     * @return The synchronization object
+     * @throws TmfTraceException
+     *             propagate TmfTraceExceptions
+     * @since 3.0
+     */
+    public synchronized SynchronizationAlgorithm synchronizeTraces() throws TmfTraceException {
+        return synchronizeTraces(false);
+    }
+
+    /**
+     * Synchronizes the traces of an experiment.
+     *
+     * @param doSync
+     *            Whether to actually synchronize or just try opening a sync
+     *            file
+     * @return The synchronization object
+     * @throws TmfTraceException
+     *             propagate TmfTraceExceptions
+     * @since 3.0
+     */
+    public synchronized SynchronizationAlgorithm synchronizeTraces(boolean doSync) throws TmfTraceException {
+
+        /* Set up the path to the synchronization file we'll use */
+        IResource resource = this.getResource();
+        String supplDirectory = null;
+
+        try {
+            /* get the directory where the file will be stored. */
+            if (resource != null) {
+                supplDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
+            }
+        } catch (CoreException e) {
+            throw new TmfTraceException(e.toString(), e);
+        }
+
+        final File syncFile = (supplDirectory != null) ? new File(supplDirectory + File.separator + SYNCHRONIZATION_FILE_NAME) : null;
+
+        final SynchronizationAlgorithm syncAlgo = SynchronizationManager.synchronizeTraces(syncFile, fTraces, doSync);
+
+        final TmfTraceSynchronizedSignal signal = new TmfTraceSynchronizedSignal(this, syncAlgo);
+
+        /* Broadcast in separate thread to prevent deadlock */
+        new Thread() {
+            @Override
+            public void run() {
+                broadcast(signal);
+            }
+        }.start();
+
+        return syncAlgo;
+    }
+
     @Override
     @SuppressWarnings("nls")
     public synchronized String toString() {
This page took 0.02754 seconds and 5 git commands to generate.