analysis: improve graph statistics scalability
authorFrancis Giraldeau <francis.giraldeau@gmail.com>
Fri, 11 Sep 2015 19:44:36 +0000 (15:44 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Mon, 14 Sep 2015 18:36:38 +0000 (14:36 -0400)
The TmfGraphStatistics was keeping all duration values. Individual
values are not required to display summary statistics. Instead, this
patch updates the sum per IGraphWorker. Also, we introduce the fTotal
field and remove the special placeholder String key in the map, allowing
to rectify the keys type from Object to IGraphWorker.

* update v2:
  * synchronize only on fWorkerStats
  * make only one map lookup to update worker sum

Change-Id: I202945bdad6a0cb759b2fc4cbb791b3afef7c3cc
Signed-off-by: Francis Giraldeau <francis.giraldeau@gmail.com>
Reviewed-on: https://git.eclipse.org/r/55780
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
analysis/org.eclipse.tracecompass.analysis.graph.core.tests/src/org/eclipse/tracecompass/analysis/graph/core/tests/graph/TmfGraphTest.java
analysis/org.eclipse.tracecompass.analysis.graph.core/src/org/eclipse/tracecompass/internal/analysis/graph/core/base/TmfGraphStatistics.java

index f3dae33041cfbed3c0a645add8cbdd5f8eade426..946714b819b645c41d2ec75ede5c7b42cb9dcf72 100644 (file)
@@ -400,7 +400,7 @@ public class TmfGraphTest {
     public void testGraphStatistics() {
         TmfGraph graph = buildFullGraph();
         TmfGraphStatistics stats = new TmfGraphStatistics();
-        stats.getGraphStatistics(graph, WORKER1);
+        stats.computeGraphStatistics(graph, WORKER1);
         assertEquals(12, stats.getSum(WORKER1).longValue());
         assertEquals(11, stats.getSum(WORKER2).longValue());
         assertEquals(23, stats.getSum().longValue());
index 53af8bedc7abd0c299de3ceaa9b57eefae0972eb..0d5814a240ec9361aaf6da33287bd878fbac6429 100644 (file)
 
 package org.eclipse.tracecompass.internal.analysis.graph.core.base;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
 import org.eclipse.tracecompass.analysis.graph.core.base.ITmfGraphVisitor;
 import org.eclipse.tracecompass.analysis.graph.core.base.TmfEdge;
 import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
 import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
-import org.eclipse.tracecompass.common.core.NonNullUtils;
-
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Multimap;
 
 /**
  * Class that computes statistics on time spent in the elements (objects) of a
@@ -30,19 +29,20 @@ import com.google.common.collect.Multimap;
  *
  * @author Francis Giraldeau
  * @author Geneviève Bastien
+ * @author Matthew Khouzam
  */
 public class TmfGraphStatistics implements ITmfGraphVisitor {
 
-    private static final String STATS_TOTAL = "total"; //$NON-NLS-1$
-
-    private final Multimap<Object, Long> fWorkerStats;
+    private final Map<IGraphWorker, Long> fWorkerStats;
+    private Long fTotal;
     private @Nullable TmfGraph fGraph;
 
     /**
      * Constructor
      */
     public TmfGraphStatistics() {
-        fWorkerStats = NonNullUtils.checkNotNull(ArrayListMultimap.<Object, Long> create());
+        fWorkerStats = new HashMap<>();
+        fTotal = 0L;
     }
 
     /**
@@ -53,10 +53,11 @@ public class TmfGraphStatistics implements ITmfGraphVisitor {
      * @param current
      *            The element from which to start calculations
      */
-    public void getGraphStatistics(TmfGraph graph, @Nullable IGraphWorker current) {
+    public void computeGraphStatistics(TmfGraph graph, @Nullable IGraphWorker current) {
         if (current == null) {
             return;
         }
+        clear();
         fGraph = graph;
         fGraph.scanLineTraverse(fGraph.getHead(current), this);
     }
@@ -77,10 +78,14 @@ public class TmfGraphStatistics implements ITmfGraphVisitor {
         TmfGraph graph = fGraph;
         synchronized (fWorkerStats) {
             if (horizontal && graph != null) {
-                fWorkerStats.put(graph.getParentOf(edge.getVertexFrom()),
-                        edge.getVertexTo().getTs() - edge.getVertexFrom().getTs());
-                fWorkerStats.put(STATS_TOTAL,
-                        edge.getVertexTo().getTs() - edge.getVertexFrom().getTs());
+                IGraphWorker worker = graph.getParentOf(edge.getVertexFrom());
+                Long duration = edge.getDuration();
+                Long currentTotal = fWorkerStats.get(worker);
+                if (currentTotal != null) {
+                    duration += currentTotal;
+                }
+                fWorkerStats.put(worker, duration);
+                fTotal += edge.getDuration();
             }
         }
     }
@@ -92,11 +97,12 @@ public class TmfGraphStatistics implements ITmfGraphVisitor {
      *            The object to get the time spent for
      * @return The sum of all durations
      */
-    public Long getSum(@Nullable Object worker) {
-        long sum = 0L;
+    public Long getSum(@Nullable IGraphWorker worker) {
+        Long sum = 0L;
         synchronized (fWorkerStats) {
-            for (long duration : fWorkerStats.get(worker)) {
-                sum += duration;
+            Long elapsed = fWorkerStats.get(worker);
+            if (elapsed != null) {
+                sum += elapsed;
             }
         }
         return sum;
@@ -108,7 +114,9 @@ public class TmfGraphStatistics implements ITmfGraphVisitor {
      * @return The sum of all durations
      */
     public Long getSum() {
-        return getSum(STATS_TOTAL);
+        synchronized (fWorkerStats) {
+            return fTotal;
+        }
     }
 
     /**
@@ -118,11 +126,24 @@ public class TmfGraphStatistics implements ITmfGraphVisitor {
      *            The object to get the percentage for
      * @return The percentage time spent in this element
      */
-    public Double getPercent(@Nullable Object worker) {
-        if (getSum() == 0) {
-            return 0D;
+    public Double getPercent(@Nullable IGraphWorker worker) {
+        synchronized (fWorkerStats) {
+            if (getSum() == 0) {
+                return 0D;
+            }
+            return (double) getSum(worker) / (double) getSum();
+        }
+    }
+
+    /**
+     * Clear statistics
+     */
+
+    public void clear() {
+        synchronized (fWorkerStats) {
+            fTotal = 0L;
+            fWorkerStats.clear();
         }
-        return (double) getSum(worker) / (double) getSum();
     }
 
 }
This page took 0.029357 seconds and 5 git commands to generate.