TMF: Add possibility to cancel update thread in XY line viewer
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Mon, 24 Feb 2014 17:31:30 +0000 (12:31 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Tue, 25 Feb 2014 14:27:28 +0000 (09:27 -0500)
When the update takes a long time to run (or when it is updated too frequently)
many threads may be running at the same time and the results are not accurate.
The update is now done in its own thread class that can be cancelled and the
updateData method has a monitor to cancel when necessary.

Change-Id: I1430e0d2329b896d85c48326dcd2bef4a013d7a9
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/22460
Tested-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
IP-Clean: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
org.eclipse.linuxtools.lttng2.ust.ui/src/org/eclipse/linuxtools/internal/lttng2/ust/ui/views/memusage/MemoryUsageViewer.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/xycharts/linecharts/TmfCommonXLineChartViewer.java

index 66e75d8b0f6f216ce8609de9e25a444cd09cc9cc..b804951d00359e1920647778dbfc0f01eab90eb2 100644 (file)
@@ -17,6 +17,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.linuxtools.internal.lttng2.ust.core.memoryusage.UstMemoryStrings;
 import org.eclipse.linuxtools.internal.tmf.core.Activator;
 import org.eclipse.linuxtools.lttng2.ust.ui.analysis.memory.UstMemoryAnalysisModule;
@@ -68,7 +69,7 @@ public class MemoryUsageViewer extends TmfCommonXLineChartViewer {
     }
 
     @Override
-    protected void updateData(long start, long end, int nb) {
+    protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
         try {
             if (getTrace() == null || fModule == null) {
                 return;
@@ -110,6 +111,9 @@ public class MemoryUsageViewer extends TmfCommonXLineChartViewer {
              */
             double yvalue = 0.0;
             for (int i = 0; i < xvalues.length; i++) {
+                if (monitor.isCanceled()) {
+                    return;
+                }
                 double x = xvalues[i];
                 long time = (long) x + offset;
                 // make sure that time is in the trace range after double to
index e2240bc03b6720e7290dfaccd6388bfbb4ed1763..755b8298e54fe54608d892ae8cebef65b308bdf7 100644 (file)
@@ -16,6 +16,8 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
 import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfChartTimeStampFormat;
 import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfXYChartViewer;
@@ -59,6 +61,8 @@ public abstract class TmfCommonXLineChartViewer extends TmfXYChartViewer {
     private double[] fXValues;
     private double fResolution;
 
+    private UpdateThread fUpdateThread;
+
     /**
      * Constructor
      *
@@ -121,20 +125,58 @@ public abstract class TmfCommonXLineChartViewer extends TmfXYChartViewer {
 
     }
 
+    private class UpdateThread extends Thread {
+        private final IProgressMonitor fMonitor;
+        private final int fNumRequests;
+
+        public UpdateThread(int numRequests) {
+            super("Line chart update"); //$NON-NLS-1$
+            fNumRequests = numRequests;
+            fMonitor = new NullProgressMonitor();
+        }
+
+        @Override
+        public void run() {
+            updateData(getWindowStartTime(), getWindowEndTime(), fNumRequests, fMonitor);
+            updateThreadFinished(this);
+        }
+
+        public void cancel() {
+            fMonitor.setCanceled(true);
+        }
+    }
+
+    private synchronized void newUpdateThread() {
+        cancelUpdate();
+        final int numRequests = (int) (getSwtChart().getPlotArea().getBounds().width * fResolution);
+        fUpdateThread = new UpdateThread(numRequests);
+        fUpdateThread.start();
+    }
+
+    private synchronized void updateThreadFinished(UpdateThread thread) {
+        if (thread == fUpdateThread) {
+            fUpdateThread = null;
+        }
+    }
+
+    /**
+     * Cancels the currently running update thread. It is automatically called
+     * when the content is updated, but child viewers may want to call it
+     * manually to do some operations before calling
+     * {@link TmfCommonXLineChartViewer#updateContent}
+     */
+    protected synchronized void cancelUpdate() {
+        if (fUpdateThread != null) {
+            fUpdateThread.cancel();
+        }
+    }
+
     @Override
     protected void updateContent() {
         getDisplay().asyncExec(new Runnable() {
-
             @Override
             public void run() {
-                final int numRequests = (int) (getSwtChart().getPlotArea().getBounds().width * fResolution);
-                Thread thread = new Thread("Line chart update") { //$NON-NLS-1$
-                    @Override
-                    public void run() {
-                        updateData(getWindowStartTime(), getWindowEndTime(), numRequests);
-                    }
-                };
-                thread.start();
+                newUpdateThread();
             }
         });
     }
@@ -202,8 +244,10 @@ public abstract class TmfCommonXLineChartViewer extends TmfXYChartViewer {
      *            The end time of the range
      * @param nb
      *            The number of 'points' in the chart.
+     * @param monitor
+     *            The progress monitor object
      */
-    protected abstract void updateData(long start, long end, int nb);
+    protected abstract void updateData(long start, long end, int nb, IProgressMonitor monitor);
 
     /**
      * Set the data for a given series of the graph. The series does not need to
This page took 0.027388 seconds and 5 git commands to generate.