lttng: More luna annotation updates
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfCheckpointIndexer.java
index ed82a926cf8d7f9c4763e14e8f0ad7d72f87f098..8171bddceb654a1ed620e6cc07835ce2253af4e9 100644 (file)
@@ -1,38 +1,40 @@
 /*******************************************************************************
- * Copyright (c) 2012 Ericsson
- * 
+ * Copyright (c) 2012, 2013 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:
  *   Francois Chouinard - Initial API and implementation
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.trace;
 
+import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Vector;
+import java.util.List;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.linuxtools.internal.tmf.core.Messages;
+import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
-import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
-import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
+import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
+import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
 
 /**
- * <b><u>TmfTraceIndexer</u></b>
- * <p>
- * A simple trace indexer that builds an array of trace checkpoints. Checkpoints
- * are stored at fixed intervals (event rank) in ascending timestamp order.
+ * A simple indexer that manages the trace index as an array of trace
+ * checkpoints. Checkpoints are stored at fixed intervals (event rank) in
+ * ascending timestamp order.
  * <p>
  * The goal being to access a random trace event reasonably fast from the user's
  * standpoint, picking the right interval value becomes a trade-off between speed
@@ -41,28 +43,38 @@ import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
  * <p>
  * Locating a specific checkpoint is trivial for both rank (rank % interval) and
  * timestamp (bsearch in the array).
+ *
+ * @version 1.0
+ * @author Francois Chouinard
+ *
+ * @see ITmfTrace
+ * @see ITmfEvent
  */
-public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITmfTraceIndexer<T> {
+public class TmfCheckpointIndexer implements ITmfTraceIndexer {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    /**
-     * The event trace to index
-     */
-    private final ITmfTrace<ITmfEvent> fTrace;
+    /** The event trace to index */
+    protected final ITmfTrace fTrace;
 
-    /**
-     * The interval between checkpoints
-     */
-    protected final int fCheckpointInterval;
+    /** The interval between checkpoints */
+    private final int fCheckpointInterval;
+
+    /** The event trace to index */
+    private boolean fIsIndexing;
 
     /**
      * The trace index. It is composed of checkpoints taken at intervals of
      * fCheckpointInterval events.
      */
-    protected final Vector<TmfCheckpoint> fTraceIndex;
+    protected final List<ITmfCheckpoint> fTraceIndex;
+
+    /**
+     * The indexing request
+     */
+    private ITmfEventRequest fIndexingRequest = null;
 
     // ------------------------------------------------------------------------
     // Construction
@@ -71,48 +83,75 @@ public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITm
     /**
      * Basic constructor that uses the default trace block size as checkpoints
      * intervals
-     * 
+     *
      * @param trace the trace to index
      */
-    public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace) {
-        this(trace, TmfTrace.DEFAULT_BLOCK_SIZE);
+    public TmfCheckpointIndexer(final ITmfTrace trace) {
+        this(trace, TmfDataProvider.DEFAULT_BLOCK_SIZE);
     }
 
     /**
      * Full trace indexer
-     * 
+     *
      * @param trace the trace to index
      * @param interval the checkpoints interval
      */
-    public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace, final int interval) {
+    public TmfCheckpointIndexer(final ITmfTrace trace, final int interval) {
         fTrace = trace;
         fCheckpointInterval = interval;
-        fTraceIndex = new Vector<TmfCheckpoint>();
+        fTraceIndex = new ArrayList<ITmfCheckpoint>();
+        fIsIndexing = false;
+    }
+
+    @Override
+    public void dispose() {
+        if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) {
+            fIndexingRequest.cancel();
+            fTraceIndex.clear();
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    // ITmfTraceIndexer - isIndexing
+    // ------------------------------------------------------------------------
+
+    @Override
+    public boolean isIndexing() {
+        return fIsIndexing;
     }
 
     // ------------------------------------------------------------------------
     // ITmfTraceIndexer - buildIndex
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#indexTrace(boolean)
-     * 
-     * The index is a list of contexts that point to events at regular interval
-     * (rank-wise) in the trace. After it is built, the index can be used to
-     * quickly access any event by rank or timestamp (using seekIndex()).
-     * 
-     * The index is built simply by reading the trace
+    /**
+     * @since 2.0
      */
     @Override
-    public void buildIndex(final boolean waitForCompletion) {
+    public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
+
+        // Don't do anything if we are already indexing
+        synchronized (fTraceIndex) {
+            if (fIsIndexing) {
+                return;
+            }
+            fIsIndexing = true;
+        }
 
         // The monitoring job
         final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
             @Override
             protected IStatus run(final IProgressMonitor monitor) {
+                monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
                 while (!monitor.isCanceled()) {
                     try {
-                        Thread.sleep(100);
+                        long prevNbEvents = fTrace.getNbEvents();
+                        Thread.sleep(250);
+                        long nbEvents = fTrace.getNbEvents();
+                        setName(Messages.TmfCheckpointIndexer_Indexing + ' ' + fTrace.getName() + " (" + nbEvents + ")"); //$NON-NLS-1$ //$NON-NLS-2$
+                        // setName doesn't refresh the UI, setTaskName does
+                        long rate = (nbEvents - prevNbEvents) * 4;
+                        monitor.setTaskName(rate + " " + Messages.TmfCheckpointIndexer_EventsPerSecond); //$NON-NLS-1$
                     } catch (final InterruptedException e) {
                         return Status.OK_STATUS;
                     }
@@ -123,27 +162,15 @@ public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITm
         };
         job.schedule();
 
-        // Clear the checkpoints
-        fTraceIndex.clear();
-
         // Build a background request for all the trace data. The index is
-        // updated as we go by getNextEvent().
-        final ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, TmfTimeRange.ETERNITY,
-                TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND)
-        {
-            ITmfTimestamp startTime = null;
-            ITmfTimestamp lastTime = null;
-
+        // updated as we go by readNextEvent().
+        fIndexingRequest = new TmfEventRequest(ITmfEvent.class,
+                range, offset, TmfDataRequest.ALL_DATA,
+                ITmfDataRequest.ExecutionType.BACKGROUND) {
             @Override
             public void handleData(final ITmfEvent event) {
                 super.handleData(event);
                 if (event != null) {
-                    final ITmfTimestamp timestamp = event.getTimestamp();
-                    if (startTime == null) {
-                        startTime = timestamp.clone();
-                    }
-                    lastTime = timestamp.clone();
-
                     // Update the trace status at regular intervals
                     if ((getNbRead() % fCheckpointInterval) == 0) {
                         updateTraceStatus();
@@ -160,20 +187,21 @@ public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITm
             public void handleCompleted() {
                 job.cancel();
                 super.handleCompleted();
+                fIsIndexing = false;
             }
 
             private void updateTraceStatus() {
-                if (getNbRead() != 0) {
-                    signalNewTimeRange(startTime, lastTime);
+                if (fTrace.getNbEvents() > 0) {
+                    signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime());
                 }
             }
         };
 
         // Submit the request and wait for completion if required
-        fTrace.sendRequest(request);
+        fTrace.sendRequest(fIndexingRequest);
         if (waitForCompletion) {
             try {
-                request.waitForCompletion();
+                fIndexingRequest.waitForCompletion();
             } catch (final InterruptedException e) {
             }
         }
@@ -181,7 +209,7 @@ public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITm
 
     /**
      * Notify the interested parties that the trace time range has changed
-     * 
+     *
      * @param startTime the new start time
      * @param endTime the new end time
      */
@@ -193,19 +221,17 @@ public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITm
     // ITmfTraceIndexer - updateIndex
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
+    /**
+     * @since 2.0
      */
     @Override
     public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
-        final long rank = context.getRank();
-        if ((rank % fCheckpointInterval) == 0) {
+        if ((context.getRank() % fCheckpointInterval) == 0) {
             // Determine the table position
-            final long position = rank / fCheckpointInterval;
+            final long position = context.getRank() / fCheckpointInterval;
             // Add new entry at proper location (if empty)
             if (fTraceIndex.size() == position) {
-                final ITmfLocation<?> location = context.getLocation().clone();
-                fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), location));
+                fTraceIndex.add(new TmfCheckpoint(timestamp, context.getLocation()));
             }
         }
     }
@@ -214,15 +240,16 @@ public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITm
     // ITmfTraceIndexer - seekIndex
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
+    /**
+     * @since 2.0
      */
     @Override
     public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
 
         // A null timestamp indicates to seek the first event
-        if (timestamp == null)
+        if (timestamp == null) {
             return fTrace.seekEvent(0);
+        }
 
         // Find the checkpoint at or before the requested timestamp.
         // In the very likely event that the timestamp is not at a checkpoint
@@ -231,46 +258,47 @@ public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITm
         int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(timestamp, null));
         if (index < 0) {
             index = Math.max(0, -(index + 2));
+        } else {
+            // If timestamp was in the list, use previous index to be able to find the
+            // first event with the same timestamp before the checkpoint
+            index = Math.max(0, index - 1);
         }
 
         // Position the trace at the checkpoint
-        return seekCheckpoint(index);
+        return restoreCheckpoint(index);
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
-     */
     @Override
     public ITmfContext seekIndex(final long rank) {
 
-      // A rank < 0 indicates to seek the first event
-      if (rank < 0)
-          return fTrace.seekEvent(0);
+        // A rank < 0 indicates to seek the first event
+        if (rank < 0) {
+            return fTrace.seekEvent(0);
+        }
 
-      // Find the checkpoint at or before the requested rank.
-      final int index = (int) rank / fCheckpointInterval;
+        // Find the checkpoint at or before the requested rank.
+        final int index = (int) rank / fCheckpointInterval;
 
-      // Position the trace at the checkpoint
-      return seekCheckpoint(index);
+        // Position the trace at the checkpoint
+        return restoreCheckpoint(index);
     }
 
     /**
      * Position the trace at the given checkpoint
-     * 
-     * @param index
-     *            the checkpoint index
+     *
+     * @param checkpoint the checkpoint index
      * @return the corresponding context
      */
-    private ITmfContext seekCheckpoint(int index) {
-        ITmfLocation<?> location;
+    private ITmfContext restoreCheckpoint(final int checkpoint) {
+        ITmfLocation location = null;
+        int index = 0;
         synchronized (fTraceIndex) {
             if (!fTraceIndex.isEmpty()) {
+                index = checkpoint;
                 if (index >= fTraceIndex.size()) {
                     index = fTraceIndex.size() - 1;
                 }
-                location = fTraceIndex.elementAt(index).getLocation();
-            } else {
-                location = null;
+                location = fTraceIndex.get(index).getLocation();
             }
         }
         final ITmfContext context = fTrace.seekEvent(location);
@@ -278,4 +306,15 @@ public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITm
         return context;
     }
 
+    // ------------------------------------------------------------------------
+    // Getters
+    // ------------------------------------------------------------------------
+
+    /**
+     * @return the trace index
+     */
+    protected List<ITmfCheckpoint> getTraceIndex() {
+        return fTraceIndex;
+    }
+
 }
This page took 0.028625 seconds and 5 git commands to generate.