tmf.core: add new constructor to AbstractTmfStateProvider
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Fri, 14 Oct 2016 20:47:29 +0000 (16:47 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 14 Feb 2017 20:54:50 +0000 (15:54 -0500)
This allows state providers with custom buffered blocking queues.

Change-Id: Id462f50b8cc48c96303d6d69e6143ab1e04b0ed8
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/83276
Reviewed-by: Hudson CI
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statesystem/AbstractTmfStateProvider.java

index 1dec0e0047e500963b13b6fe5dabd27b4222a30a..52be493fed11fe9e4455c35293f54efce7d3b962 100644 (file)
@@ -23,6 +23,8 @@ import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 
+import com.google.common.annotations.VisibleForTesting;
+
 /**
  * Instead of using IStateChangeInput directly, one can extend this class, which
  * defines a lot of the common functions of the state change input plugin.
@@ -60,15 +62,46 @@ public abstract class AbstractTmfStateProvider implements ITmfStateProvider {
      *            Name given to this state change input. Only used internally.
      */
     public AbstractTmfStateProvider(ITmfTrace trace, String id) {
+        this(trace, id, DEFAULT_EVENTS_QUEUE_SIZE, DEFAULT_EVENTS_CHUNK_SIZE);
+    }
+
+    /**
+     * Instantiate a new state provider. This constructor allows to fine-tune
+     * the size of the event processing queue. This can be useful to unit tests
+     * various situations.
+     *
+     * @param trace
+     *            The trace
+     * @param id
+     *            Name given to this state change input. Only used internally.
+     * @param queueSize
+     *            The size of the queue, a.k.a the number of chunks that fit
+     *            into the buffered queue.
+     * @param chunkSize
+     *            The number of events that fit inside a single chunk of the
+     *            queue
+     * @since 2.3
+     */
+    @VisibleForTesting
+    protected AbstractTmfStateProvider(ITmfTrace trace, String id, int queueSize, int chunkSize) {
+        if (queueSize <= 0 || chunkSize <= 0) {
+            throw new IllegalArgumentException("Cannot have negative sized buffer" + //$NON-NLS-1$
+                    formatError("queueSize", queueSize) + //$NON-NLS-1$
+                    formatError("chunkSize", chunkSize)); //$NON-NLS-1$
+        }
         fTrace = trace;
-        fEventsQueue = new BufferedBlockingQueue<>(DEFAULT_EVENTS_QUEUE_SIZE, DEFAULT_EVENTS_CHUNK_SIZE);
+        fEventsQueue = new BufferedBlockingQueue<>(queueSize, chunkSize);
         fStateSystemAssigned = false;
-        // set the safe time to before the trace start, the analysis has not yet started
+        // set the safe time to before the trace start, the analysis has not yet
+        // started
         fSafeTime = trace.getStartTime().toNanos() - 1;
-
         fEventHandlerThread = new Thread(new EventProcessor(), id + " Event Handler"); //$NON-NLS-1$
     }
 
+    private static String formatError(String name, int value) {
+        return (value <= 0) ? " " + name + " = " + value : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+    }
+
     /**
      * Get the state system builder of this provider (to insert states in).
      *
@@ -110,7 +143,9 @@ public abstract class AbstractTmfStateProvider implements ITmfStateProvider {
 
     @Override
     public void dispose() {
-        /* Insert a null event in the queue to stop the event handler's thread. */
+        /*
+         * Insert a null event in the queue to stop the event handler's thread.
+         */
         try {
             fEventsQueue.put(END_EVENT);
             fEventsQueue.flushInputBuffer();
@@ -158,7 +193,9 @@ public abstract class AbstractTmfStateProvider implements ITmfStateProvider {
     // Special event types
     // ------------------------------------------------------------------------
 
-    /** Fake event indicating the build is over, and the provider should close */
+    /**
+     * Fake event indicating the build is over, and the provider should close
+     */
     private static class EndEvent extends TmfEvent {
         public EndEvent() {
             super(null, ITmfContext.UNKNOWN_RANK, null, null, null);
@@ -194,7 +231,6 @@ public abstract class AbstractTmfStateProvider implements ITmfStateProvider {
                 return;
             }
 
-
             /*
              * We never insert null in the queue. Cannot be checked at
              * compile-time until Java 8 annotations...
@@ -219,8 +255,7 @@ public abstract class AbstractTmfStateProvider implements ITmfStateProvider {
 
         private void closeStateSystem() {
             ITmfEvent event = currentEvent;
-            final long endTime = (event == null) ? 0 :
-                    event.getTimestamp().toNanos();
+            final long endTime = (event == null) ? 0 : event.getTimestamp().toNanos();
 
             if (fSS != null) {
                 fSS.closeHistory(endTime);
This page took 0.042276 seconds and 5 git commands to generate.