lttng: More luna annotation updates
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfCheckpointIndexer.java
index 29af87f6813ce7dc86b244fc3b4e9335329e05f3..8171bddceb654a1ed620e6cc07835ce2253af4e9 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * 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
@@ -20,16 +20,16 @@ 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.trace.TmfExperimentContext;
+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;
 
 /**
  * A simple indexer that manages the trace index as an array of trace
@@ -56,13 +56,13 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
     // Attributes
     // ------------------------------------------------------------------------
 
-    // The event trace to index
+    /** The event trace to index */
     protected final ITmfTrace fTrace;
 
-    // The interval between checkpoints
+    /** The interval between checkpoints */
     private final int fCheckpointInterval;
 
-    // The event trace to index
+    /** The event trace to index */
     private boolean fIsIndexing;
 
     /**
@@ -103,9 +103,6 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
         fIsIndexing = false;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#dispose()
-     */
     @Override
     public void dispose() {
         if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) {
@@ -118,9 +115,6 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
     // ITmfTraceIndexer - isIndexing
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#isIndexing()
-     */
     @Override
     public boolean isIndexing() {
         return fIsIndexing;
@@ -130,15 +124,8 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
     // ITmfTraceIndexer - buildIndex
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     *
-     * 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
-     *
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#buildIndex(long, org.eclipse.linuxtools.tmf.core.event.TmfTimeRange, boolean)
+    /**
+     * @since 2.0
      */
     @Override
     public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
@@ -155,9 +142,16 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
         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;
                     }
@@ -171,21 +165,12 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
         // Build a background request for all the trace data. The index is
         // updated as we go by readNextEvent().
         fIndexingRequest = new TmfEventRequest(ITmfEvent.class,
-                range, offset, TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND)
-        {
-            private ITmfTimestamp startTime = null;
-            private ITmfTimestamp lastTime = null;
-
+                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();
@@ -206,8 +191,8 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
             }
 
             private void updateTraceStatus() {
-                if (getNbRead() != 0) {
-                    signalNewTimeRange(startTime, lastTime);
+                if (fTrace.getNbEvents() > 0) {
+                    signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime());
                 }
             }
         };
@@ -236,18 +221,17 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
     // 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) {
-                fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), saveContext(context)));
+                fTraceIndex.add(new TmfCheckpoint(timestamp, context.getLocation()));
             }
         }
     }
@@ -256,8 +240,8 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
     // 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) {
@@ -274,15 +258,16 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
         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 restoreCheckpoint(index);
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
-     */
     @Override
     public ITmfContext seekIndex(final long rank) {
 
@@ -305,7 +290,7 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
      * @return the corresponding context
      */
     private ITmfContext restoreCheckpoint(final int checkpoint) {
-        ITmfLocation<?> location = null;
+        ITmfLocation location = null;
         int index = 0;
         synchronized (fTraceIndex) {
             if (!fTraceIndex.isEmpty()) {
@@ -313,7 +298,7 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
                 if (index >= fTraceIndex.size()) {
                     index = fTraceIndex.size() - 1;
                 }
-                return restoreContext(fTraceIndex.get(index).getContext());
+                location = fTraceIndex.get(index).getLocation();
             }
         }
         final ITmfContext context = fTrace.seekEvent(location);
@@ -332,65 +317,4 @@ public class TmfCheckpointIndexer implements ITmfTraceIndexer {
         return fTraceIndex;
     }
 
-    // ------------------------------------------------------------------------
-    // Context conversion functions
-    // ------------------------------------------------------------------------
-
-    private static ITmfContext saveContext(ITmfContext context) {
-        if (context instanceof TmfExperimentContext) {
-            return saveExpContext(context);
-        }
-        TmfContext ctx = new TmfContext(context.getLocation().clone(), context.getRank());
-        return ctx;
-    }
-
-    private static ITmfContext saveExpContext(ITmfContext context) {
-        TmfExperimentContext expContext = (TmfExperimentContext) context;
-        int size = expContext.getContexts().length;
-        ITmfContext[] trcCtxts = new TmfContext[size];
-        for (int i = 0; i < size; i++) {
-            ITmfContext ctx = expContext.getContexts()[i];
-            trcCtxts[i] = (ctx != null) ? new TmfContext(ctx.getLocation().clone(), ctx.getRank()) : null;
-        }
-        TmfExperimentContext expCtx = new TmfExperimentContext(trcCtxts);
-        expCtx.setLocation(context.getLocation().clone());
-        expCtx.setRank(context.getRank());
-        ITmfEvent[] trcEvts = expCtx.getEvents();
-        for (int i = 0; i < size; i++) {
-            ITmfEvent event = expContext.getEvents()[i];
-            trcEvts[i] = (event != null) ? event.clone() : null;
-        }
-        return expCtx;
-    }
-
-    private ITmfContext restoreContext(ITmfContext context) {
-        if (context instanceof TmfExperimentContext) {
-            return restoreExpContext(context);
-        }
-        ITmfContext ctx = fTrace.seekEvent(context.getLocation());
-        ctx.setRank(context.getRank());
-        return ctx;
-    }
-
-    private ITmfContext restoreExpContext(ITmfContext context) {
-        TmfExperimentContext expContext = (TmfExperimentContext) context;
-        int size = expContext.getContexts().length;
-        ITmfContext[] trcCtxts = new ITmfContext[size];
-        for (int i = 0; i < size; i++) {
-            ITmfTrace trace = ((TmfExperiment) fTrace).getTraces()[i];
-            ITmfContext ctx = expContext.getContexts()[i];
-            trcCtxts[i] = trace.seekEvent(ctx.getLocation().clone());
-            trcCtxts[i].setRank(ctx.getRank());
-        }
-        TmfExperimentContext ctx = new TmfExperimentContext(trcCtxts);
-        ctx.setLocation(context.getLocation().clone());
-        ctx.setRank(context.getRank());
-        ITmfEvent[] trcEvts = expContext.getEvents();
-        for (int i = 0; i < size; i++) {
-            ITmfEvent event = trcEvts[i];
-            ctx.getEvents()[i] = (event != null) ? event.clone() : null;
-        }
-        return ctx;
-    }
-
 }
This page took 0.039793 seconds and 5 git commands to generate.