[go: nahoru, domu]

Better tracking of differences in heap allocations

The previous change to track differences between two allocations
was incorrect - it would change the allocation info itself.

This CL fixes that by actually having a separate object that
stores the differences in allocation count between two snapshots,
but delegating to one of the snapshots for all other information.

Change-Id: I7106a6b2060739db1b3e1d8c048ffc3d082a0d25
diff --git a/ddms/ddmuilib/src/main/java/com/android/ddmuilib/heap/NativeDiffAllocationInfo.java b/ddms/ddmuilib/src/main/java/com/android/ddmuilib/heap/NativeDiffAllocationInfo.java
new file mode 100644
index 0000000..bdd9b39
--- /dev/null
+++ b/ddms/ddmuilib/src/main/java/com/android/ddmuilib/heap/NativeDiffAllocationInfo.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ddmuilib.heap;
+
+import com.android.ddmlib.NativeAllocationInfo;
+import com.android.ddmlib.NativeStackCallInfo;
+
+import java.util.List;
+
+/**
+ * {@link NativeDiffAllocationInfo} stores the difference in the allocation
+ * counts between two allocations with the same stack trace.
+ *
+ * Since only the allocation counts are different, it delegates all other functionality to
+ * one of the allocations and just maintains the allocation count.
+ */
+public class NativeDiffAllocationInfo extends NativeAllocationInfo {
+    private final NativeAllocationInfo info;
+
+    public NativeDiffAllocationInfo(NativeAllocationInfo cur, NativeAllocationInfo prev) {
+        super(cur.getSize(), getNewAllocations(cur, prev));
+        info = cur;
+    }
+
+    private static int getNewAllocations(NativeAllocationInfo n1, NativeAllocationInfo n2) {
+        return n1.getAllocationCount() - n2.getAllocationCount();
+    }
+
+    @Override
+    public boolean isStackCallResolved() {
+        return info.isStackCallResolved();
+    }
+
+    @Override
+    public List<Long> getStackCallAddresses() {
+        return info.getStackCallAddresses();
+    }
+
+    @Override
+    public synchronized List<NativeStackCallInfo> getResolvedStackCall() {
+        return info.getResolvedStackCall();
+    }
+
+    @Override
+    public synchronized NativeStackCallInfo getRelevantStackCallInfo() {
+        return info.getRelevantStackCallInfo();
+    }
+
+    @Override
+    public boolean isZygoteChild() {
+        return info.isZygoteChild();
+    }
+}
diff --git a/ddms/ddmuilib/src/main/java/com/android/ddmuilib/heap/NativeHeapDiffSnapshot.java b/ddms/ddmuilib/src/main/java/com/android/ddmuilib/heap/NativeHeapDiffSnapshot.java
index 783e5e4..e7977bb 100644
--- a/ddms/ddmuilib/src/main/java/com/android/ddmuilib/heap/NativeHeapDiffSnapshot.java
+++ b/ddms/ddmuilib/src/main/java/com/android/ddmuilib/heap/NativeHeapDiffSnapshot.java
@@ -17,6 +17,7 @@
 package com.android.ddmuilib.heap;
 
 import com.android.ddmlib.NativeAllocationInfo;
+import com.google.common.collect.Sets;
 
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -58,17 +59,32 @@
         // 5 allocations at the stack trace. We need to subtract out the 3 from the first allocation
         Set<NativeAllocationInfo> >
                 new HashSet<NativeAllocationInfo>(oldSnapshot.getAllocations());
+        Set<NativeAllocationInfo> newAllocations =
+                Sets.newHashSetWithExpectedSize(allocations.size());
+
         onlyInPrevious.removeAll(newSnapshot.getAllocations());
-        for (NativeAllocationInfo allocation : allocations) {
-            for (NativeAllocationInfo old : onlyInPrevious) {
-                if (allocation.stackEquals(old)) {
-                    allocation.subtract(old);
-                    break;
-                }
+        for (NativeAllocationInfo current : allocations) {
+            NativeAllocationInfo old = getOldAllocationWithSameStack(current, onlyInPrevious);
+            if (old == null) {
+                newAllocations.add(current);
+            } else if (current.getAllocationCount() > old.getAllocationCount()) {
+                newAllocations.add(new NativeDiffAllocationInfo(current, old));
             }
         }
 
-        return new ArrayList<NativeAllocationInfo>(allocations);
+        return new ArrayList<NativeAllocationInfo>(newAllocations);
+    }
+
+    private static NativeAllocationInfo getOldAllocationWithSameStack(
+            NativeAllocationInfo info,
+            Set<NativeAllocationInfo> allocations) {
+        for (NativeAllocationInfo a : allocations) {
+            if (info.getSize() == a.getSize() && info.stackEquals(a)) {
+                return a;
+            }
+        }
+
+        return null;
     }
 
     @Override