diff options
author | 2017-09-11 13:04:05 +0100 | |
---|---|---|
committer | 2017-09-13 12:48:59 +0100 | |
commit | 8d02c895a872926fba8618ae30d5c855ef337fe7 (patch) | |
tree | 86d5e218e78cea29dc891ec1892d18be150402f4 | |
parent | 21f89d2cc47e9f688b0d7143a282b17a241a50a2 (diff) |
Remove findClass and mClasses from AhatSnapshot.
The mClasses data structure mapping class names to class objects was
only used for testing purposes. Remove this data structure from
AhatSnapshot and find an alternative approach for the test cases so
that users don't incur the runtime and memory costs of the data
structure.
Test: m ahat-test
Change-Id: If7804f943c6155448a5fded92ff71e946435b584
-rw-r--r-- | tools/ahat/src/heapdump/AhatSnapshot.java | 14 | ||||
-rw-r--r-- | tools/ahat/test/InstanceTest.java | 6 | ||||
-rw-r--r-- | tools/ahat/test/ObjectHandlerTest.java | 4 | ||||
-rw-r--r-- | tools/ahat/test/TestDump.java | 53 |
4 files changed, 50 insertions, 27 deletions
diff --git a/tools/ahat/src/heapdump/AhatSnapshot.java b/tools/ahat/src/heapdump/AhatSnapshot.java index fa41362cae..1b2cf3c59f 100644 --- a/tools/ahat/src/heapdump/AhatSnapshot.java +++ b/tools/ahat/src/heapdump/AhatSnapshot.java @@ -35,7 +35,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -48,9 +47,6 @@ public class AhatSnapshot implements Diffable<AhatSnapshot> { // List of all ahat instances stored in increasing order by id. private final List<AhatInstance> mInstances = new ArrayList<AhatInstance>(); - // Map from class name to class object. - private final Map<String, AhatClassObj> mClasses = new HashMap<String, AhatClassObj>(); - private final List<AhatHeap> mHeaps = new ArrayList<AhatHeap>(); private AhatSnapshot mBaseline = this; @@ -113,7 +109,6 @@ public class AhatSnapshot implements Diffable<AhatSnapshot> { } else if (inst instanceof ClassObj) { AhatClassObj classObj = new AhatClassObj(id); mInstances.add(classObj); - mClasses.put(((ClassObj)inst).getClassName(), classObj); } return true; } @@ -212,15 +207,6 @@ public class AhatSnapshot implements Diffable<AhatSnapshot> { } /** - * Returns the class object for the class with given name. - * Returns null if there is no class object for the given name. - * Note: This method is exposed for testing purposes. - */ - public AhatClassObj findClass(String name) { - return mClasses.get(name); - } - - /** * Returns the heap with the given name, if any. * Returns null if no heap with the given name could be found. */ diff --git a/tools/ahat/test/InstanceTest.java b/tools/ahat/test/InstanceTest.java index e05782c0b6..63055db93d 100644 --- a/tools/ahat/test/InstanceTest.java +++ b/tools/ahat/test/InstanceTest.java @@ -237,7 +237,7 @@ public class InstanceTest { public void gcRootPath() throws IOException { TestDump dump = TestDump.getTestDump(); - AhatClassObj main = dump.getAhatSnapshot().findClass("Main"); + AhatClassObj main = dump.findClass("Main"); AhatInstance gcPathArray = dump.getDumpedAhatInstance("gcPathArray"); Value value = gcPathArray.asArrayInstance().getValue(2); AhatInstance base = value.asAhatInstance(); @@ -333,7 +333,7 @@ public class InstanceTest { @Test public void classObjNotABitmap() throws IOException { TestDump dump = TestDump.getTestDump(); - AhatInstance obj = dump.getAhatSnapshot().findClass("Main"); + AhatInstance obj = dump.findClass("Main"); assertNull(obj.asBitmap()); } @@ -348,7 +348,7 @@ public class InstanceTest { @Test public void classObjToString() throws IOException { TestDump dump = TestDump.getTestDump(); - AhatInstance obj = dump.getAhatSnapshot().findClass("Main"); + AhatInstance obj = dump.findClass("Main"); assertEquals("class Main", obj.toString()); } diff --git a/tools/ahat/test/ObjectHandlerTest.java b/tools/ahat/test/ObjectHandlerTest.java index cd0ba23013..1b8a781e0c 100644 --- a/tools/ahat/test/ObjectHandlerTest.java +++ b/tools/ahat/test/ObjectHandlerTest.java @@ -42,7 +42,7 @@ public class ObjectHandlerTest { AhatSnapshot snapshot = dump.getAhatSnapshot(); AhatHandler handler = new ObjectHandler(snapshot); - AhatInstance object = snapshot.findClass("Main"); + AhatInstance object = dump.findClass("Main"); assertNotNull(object); TestHandler.testNoCrash(handler, "http://localhost:7100/object?id=" + object.getId()); @@ -55,7 +55,7 @@ public class ObjectHandlerTest { AhatSnapshot snapshot = dump.getAhatSnapshot(); AhatHandler handler = new ObjectHandler(snapshot); - AhatInstance object = snapshot.findClass("java.lang.String"); + AhatInstance object = dump.findClass("java.lang.String"); assertNotNull(object); TestHandler.testNoCrash(handler, "http://localhost:7100/object?id=" + object.getId()); diff --git a/tools/ahat/test/TestDump.java b/tools/ahat/test/TestDump.java index 3dce2dccfe..db9b25646a 100644 --- a/tools/ahat/test/TestDump.java +++ b/tools/ahat/test/TestDump.java @@ -21,11 +21,14 @@ import com.android.ahat.heapdump.AhatInstance; import com.android.ahat.heapdump.AhatSnapshot; import com.android.ahat.heapdump.Diff; import com.android.ahat.heapdump.FieldValue; +import com.android.ahat.heapdump.Site; import com.android.ahat.heapdump.Value; import com.android.tools.perflib.heap.ProguardMap; import java.io.File; import java.io.IOException; import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collection; /** * The TestDump class is used to get an AhatSnapshot for the test-dump @@ -45,8 +48,10 @@ public class TestDump { // fails and don't try to load it again. private static boolean mTestDumpFailed = false; - private AhatSnapshot mSnapshot = null; - private AhatSnapshot mBaseline = null; + private AhatSnapshot mSnapshot; + private AhatSnapshot mBaseline; + private AhatClassObj mMain; + private AhatClassObj mBaselineMain; /** * Load the test-dump.hprof and test-dump-base.hprof files. @@ -79,6 +84,12 @@ public class TestDump { mSnapshot = AhatSnapshot.fromHprof(new File(hprof), map); mBaseline = AhatSnapshot.fromHprof(new File(hprofBase), map); Diff.snapshots(mSnapshot, mBaseline); + + mMain = findClass(mSnapshot, "Main"); + assert(mMain != null); + + mBaselineMain = findClass(mBaseline, "Main"); + assert(mBaselineMain != null); } /** @@ -100,7 +111,7 @@ public class TestDump { * snapshot for the test-dump program. */ public Value getDumpedValue(String name) { - return getDumpedValue(name, mSnapshot); + return getDumpedValue(name, mMain); } /** @@ -108,15 +119,14 @@ public class TestDump { * baseline snapshot for the test-dump program. */ public Value getBaselineDumpedValue(String name) { - return getDumpedValue(name, mBaseline); + return getDumpedValue(name, mBaselineMain); } /** - * Returns the value of a field in the DumpedStuff instance in the - * given snapshot for the test-dump program. + * Returns the value of a field in the DumpedStuff instance given the Main + * class object for the snapshot. */ - private Value getDumpedValue(String name, AhatSnapshot snapshot) { - AhatClassObj main = snapshot.findClass("Main"); + private static Value getDumpedValue(String name, AhatClassObj main) { AhatInstance stuff = null; for (FieldValue field : main.getStaticFieldValues()) { if ("stuff".equals(field.name)) { @@ -127,6 +137,33 @@ public class TestDump { } /** + * Returns a class object in the given heap dump whose name matches the + * given name, or null if no such class object could be found. + */ + private static AhatClassObj findClass(AhatSnapshot snapshot, String name) { + Site root = snapshot.getRootSite(); + Collection<AhatInstance> classes = new ArrayList<AhatInstance>(); + root.getObjects(null, "java.lang.Class", classes); + for (AhatInstance inst : classes) { + if (inst.isClassObj()) { + AhatClassObj cls = inst.asClassObj(); + if (name.equals(cls.getName())) { + return cls; + } + } + } + return null; + } + + /** + * Returns a class object in the heap dump whose name matches the given + * name, or null if no such class object could be found. + */ + public AhatClassObj findClass(String name) { + return findClass(mSnapshot, name); + } + + /** * Returns the value of a non-primitive field in the DumpedStuff instance in * the snapshot for the test-dump program. */ |