Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.ahat; |
| 18 | |
| 19 | import com.android.ahat.heapdump.AhatClassObj; |
| 20 | import com.android.ahat.heapdump.AhatHeap; |
| 21 | import com.android.ahat.heapdump.AhatInstance; |
| 22 | import com.android.ahat.heapdump.AhatSnapshot; |
| 23 | import com.android.ahat.heapdump.PathElement; |
Richard Uhler | 3ee4bff | 2017-05-16 13:31:01 +0100 | [diff] [blame] | 24 | import com.android.ahat.heapdump.Size; |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 25 | import com.android.ahat.heapdump.Value; |
| 26 | import com.android.tools.perflib.heap.hprof.HprofClassDump; |
| 27 | import com.android.tools.perflib.heap.hprof.HprofConstant; |
| 28 | import com.android.tools.perflib.heap.hprof.HprofDumpRecord; |
| 29 | import com.android.tools.perflib.heap.hprof.HprofHeapDump; |
| 30 | import com.android.tools.perflib.heap.hprof.HprofInstanceDump; |
| 31 | import com.android.tools.perflib.heap.hprof.HprofInstanceField; |
| 32 | import com.android.tools.perflib.heap.hprof.HprofLoadClass; |
| 33 | import com.android.tools.perflib.heap.hprof.HprofPrimitiveArrayDump; |
| 34 | import com.android.tools.perflib.heap.hprof.HprofRecord; |
| 35 | import com.android.tools.perflib.heap.hprof.HprofRootDebugger; |
| 36 | import com.android.tools.perflib.heap.hprof.HprofStaticField; |
| 37 | import com.android.tools.perflib.heap.hprof.HprofStringBuilder; |
| 38 | import com.android.tools.perflib.heap.hprof.HprofType; |
| 39 | import com.google.common.io.ByteArrayDataOutput; |
| 40 | import com.google.common.io.ByteStreams; |
| 41 | import java.io.IOException; |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.List; |
| 44 | import org.junit.Test; |
| 45 | |
| 46 | import static org.junit.Assert.assertEquals; |
| 47 | import static org.junit.Assert.assertFalse; |
| 48 | import static org.junit.Assert.assertNotNull; |
| 49 | import static org.junit.Assert.assertNull; |
| 50 | import static org.junit.Assert.assertTrue; |
| 51 | |
| 52 | public class InstanceTest { |
| 53 | @Test |
| 54 | public void asStringBasic() throws IOException { |
| 55 | TestDump dump = TestDump.getTestDump(); |
| 56 | AhatInstance str = dump.getDumpedAhatInstance("basicString"); |
| 57 | assertEquals("hello, world", str.asString()); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void asStringNonAscii() throws IOException { |
| 62 | TestDump dump = TestDump.getTestDump(); |
| 63 | AhatInstance str = dump.getDumpedAhatInstance("nonAscii"); |
| 64 | assertEquals("Sigma (Ʃ) is not ASCII", str.asString()); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void asStringEmbeddedZero() throws IOException { |
| 69 | TestDump dump = TestDump.getTestDump(); |
| 70 | AhatInstance str = dump.getDumpedAhatInstance("embeddedZero"); |
| 71 | assertEquals("embedded\0...", str.asString()); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void asStringCharArray() throws IOException { |
| 76 | TestDump dump = TestDump.getTestDump(); |
| 77 | AhatInstance str = dump.getDumpedAhatInstance("charArray"); |
| 78 | assertEquals("char thing", str.asString()); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void asStringTruncated() throws IOException { |
| 83 | TestDump dump = TestDump.getTestDump(); |
| 84 | AhatInstance str = dump.getDumpedAhatInstance("basicString"); |
| 85 | assertEquals("hello", str.asString(5)); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void asStringTruncatedNonAscii() throws IOException { |
| 90 | TestDump dump = TestDump.getTestDump(); |
| 91 | AhatInstance str = dump.getDumpedAhatInstance("nonAscii"); |
| 92 | assertEquals("Sigma (Ʃ)", str.asString(9)); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void asStringTruncatedEmbeddedZero() throws IOException { |
| 97 | TestDump dump = TestDump.getTestDump(); |
| 98 | AhatInstance str = dump.getDumpedAhatInstance("embeddedZero"); |
| 99 | assertEquals("embed", str.asString(5)); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void asStringCharArrayTruncated() throws IOException { |
| 104 | TestDump dump = TestDump.getTestDump(); |
| 105 | AhatInstance str = dump.getDumpedAhatInstance("charArray"); |
| 106 | assertEquals("char ", str.asString(5)); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void asStringExactMax() throws IOException { |
| 111 | TestDump dump = TestDump.getTestDump(); |
| 112 | AhatInstance str = dump.getDumpedAhatInstance("basicString"); |
| 113 | assertEquals("hello, world", str.asString(12)); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void asStringExactMaxNonAscii() throws IOException { |
| 118 | TestDump dump = TestDump.getTestDump(); |
| 119 | AhatInstance str = dump.getDumpedAhatInstance("nonAscii"); |
| 120 | assertEquals("Sigma (Ʃ) is not ASCII", str.asString(22)); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void asStringExactMaxEmbeddedZero() throws IOException { |
| 125 | TestDump dump = TestDump.getTestDump(); |
| 126 | AhatInstance str = dump.getDumpedAhatInstance("embeddedZero"); |
| 127 | assertEquals("embedded\0...", str.asString(12)); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void asStringCharArrayExactMax() throws IOException { |
| 132 | TestDump dump = TestDump.getTestDump(); |
| 133 | AhatInstance str = dump.getDumpedAhatInstance("charArray"); |
| 134 | assertEquals("char thing", str.asString(10)); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void asStringNotTruncated() throws IOException { |
| 139 | TestDump dump = TestDump.getTestDump(); |
| 140 | AhatInstance str = dump.getDumpedAhatInstance("basicString"); |
| 141 | assertEquals("hello, world", str.asString(50)); |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void asStringNotTruncatedNonAscii() throws IOException { |
| 146 | TestDump dump = TestDump.getTestDump(); |
| 147 | AhatInstance str = dump.getDumpedAhatInstance("nonAscii"); |
| 148 | assertEquals("Sigma (Ʃ) is not ASCII", str.asString(50)); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void asStringNotTruncatedEmbeddedZero() throws IOException { |
| 153 | TestDump dump = TestDump.getTestDump(); |
| 154 | AhatInstance str = dump.getDumpedAhatInstance("embeddedZero"); |
| 155 | assertEquals("embedded\0...", str.asString(50)); |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | public void asStringCharArrayNotTruncated() throws IOException { |
| 160 | TestDump dump = TestDump.getTestDump(); |
| 161 | AhatInstance str = dump.getDumpedAhatInstance("charArray"); |
| 162 | assertEquals("char thing", str.asString(50)); |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void asStringNegativeMax() throws IOException { |
| 167 | TestDump dump = TestDump.getTestDump(); |
| 168 | AhatInstance str = dump.getDumpedAhatInstance("basicString"); |
| 169 | assertEquals("hello, world", str.asString(-3)); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void asStringNegativeMaxNonAscii() throws IOException { |
| 174 | TestDump dump = TestDump.getTestDump(); |
| 175 | AhatInstance str = dump.getDumpedAhatInstance("nonAscii"); |
| 176 | assertEquals("Sigma (Ʃ) is not ASCII", str.asString(-3)); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void asStringNegativeMaxEmbeddedZero() throws IOException { |
| 181 | TestDump dump = TestDump.getTestDump(); |
| 182 | AhatInstance str = dump.getDumpedAhatInstance("embeddedZero"); |
| 183 | assertEquals("embedded\0...", str.asString(-3)); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void asStringCharArrayNegativeMax() throws IOException { |
| 188 | TestDump dump = TestDump.getTestDump(); |
| 189 | AhatInstance str = dump.getDumpedAhatInstance("charArray"); |
| 190 | assertEquals("char thing", str.asString(-3)); |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void asStringNull() throws IOException { |
| 195 | TestDump dump = TestDump.getTestDump(); |
| 196 | AhatInstance obj = dump.getDumpedAhatInstance("nullString"); |
| 197 | assertNull(obj); |
| 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void asStringNotString() throws IOException { |
| 202 | TestDump dump = TestDump.getTestDump(); |
| 203 | AhatInstance obj = dump.getDumpedAhatInstance("anObject"); |
| 204 | assertNotNull(obj); |
| 205 | assertNull(obj.asString()); |
| 206 | } |
| 207 | |
| 208 | @Test |
| 209 | public void basicReference() throws IOException { |
| 210 | TestDump dump = TestDump.getTestDump(); |
| 211 | |
| 212 | AhatInstance pref = dump.getDumpedAhatInstance("aPhantomReference"); |
| 213 | AhatInstance wref = dump.getDumpedAhatInstance("aWeakReference"); |
| 214 | AhatInstance nref = dump.getDumpedAhatInstance("aNullReferentReference"); |
| 215 | AhatInstance referent = dump.getDumpedAhatInstance("anObject"); |
| 216 | assertNotNull(pref); |
| 217 | assertNotNull(wref); |
| 218 | assertNotNull(nref); |
| 219 | assertNotNull(referent); |
| 220 | assertEquals(referent, pref.getReferent()); |
| 221 | assertEquals(referent, wref.getReferent()); |
| 222 | assertNull(nref.getReferent()); |
| 223 | assertNull(referent.getReferent()); |
| 224 | } |
| 225 | |
| 226 | @Test |
Richard Uhler | d640e29 | 2016-12-28 15:46:03 +0000 | [diff] [blame] | 227 | public void unreachableReferent() throws IOException { |
| 228 | // The test dump program should never be under enough GC pressure for the |
| 229 | // soft reference to be cleared. Ensure that ahat will show the soft |
| 230 | // reference as having a non-null referent. |
| 231 | TestDump dump = TestDump.getTestDump(); |
| 232 | AhatInstance ref = dump.getDumpedAhatInstance("aSoftReference"); |
| 233 | assertNotNull(ref.getReferent()); |
| 234 | } |
| 235 | |
| 236 | @Test |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 237 | public void gcRootPath() throws IOException { |
| 238 | TestDump dump = TestDump.getTestDump(); |
| 239 | |
| 240 | AhatClassObj main = dump.getAhatSnapshot().findClass("Main"); |
| 241 | AhatInstance gcPathArray = dump.getDumpedAhatInstance("gcPathArray"); |
| 242 | Value value = gcPathArray.asArrayInstance().getValue(2); |
| 243 | AhatInstance base = value.asAhatInstance(); |
| 244 | AhatInstance left = base.getRefField("left"); |
| 245 | AhatInstance right = base.getRefField("right"); |
| 246 | AhatInstance target = left.getRefField("right"); |
| 247 | |
| 248 | List<PathElement> path = target.getPathFromGcRoot(); |
| 249 | assertEquals(6, path.size()); |
| 250 | |
| 251 | assertEquals(main, path.get(0).instance); |
| 252 | assertEquals(".stuff", path.get(0).field); |
| 253 | assertTrue(path.get(0).isDominator); |
| 254 | |
| 255 | assertEquals(".gcPathArray", path.get(1).field); |
| 256 | assertTrue(path.get(1).isDominator); |
| 257 | |
| 258 | assertEquals(gcPathArray, path.get(2).instance); |
| 259 | assertEquals("[2]", path.get(2).field); |
| 260 | assertTrue(path.get(2).isDominator); |
| 261 | |
| 262 | assertEquals(base, path.get(3).instance); |
| 263 | assertTrue(path.get(3).isDominator); |
| 264 | |
| 265 | // There are two possible paths. Either it can go through the 'left' node, |
| 266 | // or the 'right' node. |
| 267 | if (path.get(3).field.equals(".left")) { |
| 268 | assertEquals(".left", path.get(3).field); |
| 269 | |
| 270 | assertEquals(left, path.get(4).instance); |
| 271 | assertEquals(".right", path.get(4).field); |
| 272 | assertFalse(path.get(4).isDominator); |
| 273 | |
| 274 | } else { |
| 275 | assertEquals(".right", path.get(3).field); |
| 276 | |
| 277 | assertEquals(right, path.get(4).instance); |
| 278 | assertEquals(".left", path.get(4).field); |
| 279 | assertFalse(path.get(4).isDominator); |
| 280 | } |
| 281 | |
| 282 | assertEquals(target, path.get(5).instance); |
| 283 | assertEquals("", path.get(5).field); |
| 284 | assertTrue(path.get(5).isDominator); |
| 285 | } |
| 286 | |
| 287 | @Test |
| 288 | public void retainedSize() throws IOException { |
| 289 | TestDump dump = TestDump.getTestDump(); |
| 290 | |
| 291 | // anObject should not be an immediate dominator of any other object. This |
| 292 | // means its retained size should be equal to its size for the heap it was |
| 293 | // allocated on, and should be 0 for all other heaps. |
| 294 | AhatInstance anObject = dump.getDumpedAhatInstance("anObject"); |
| 295 | AhatSnapshot snapshot = dump.getAhatSnapshot(); |
Richard Uhler | 3ee4bff | 2017-05-16 13:31:01 +0100 | [diff] [blame] | 296 | Size size = anObject.getSize(); |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 297 | assertEquals(size, anObject.getTotalRetainedSize()); |
| 298 | assertEquals(size, anObject.getRetainedSize(anObject.getHeap())); |
| 299 | for (AhatHeap heap : snapshot.getHeaps()) { |
| 300 | if (!heap.equals(anObject.getHeap())) { |
| 301 | assertEquals(String.format("For heap '%s'", heap.getName()), |
Richard Uhler | 3ee4bff | 2017-05-16 13:31:01 +0100 | [diff] [blame] | 302 | Size.ZERO, anObject.getRetainedSize(heap)); |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | @Test |
| 308 | public void objectNotABitmap() throws IOException { |
| 309 | TestDump dump = TestDump.getTestDump(); |
| 310 | AhatInstance obj = dump.getDumpedAhatInstance("anObject"); |
| 311 | assertNull(obj.asBitmap()); |
| 312 | } |
| 313 | |
| 314 | @Test |
| 315 | public void arrayNotABitmap() throws IOException { |
| 316 | TestDump dump = TestDump.getTestDump(); |
| 317 | AhatInstance obj = dump.getDumpedAhatInstance("gcPathArray"); |
| 318 | assertNull(obj.asBitmap()); |
| 319 | } |
| 320 | |
| 321 | @Test |
| 322 | public void classObjNotABitmap() throws IOException { |
| 323 | TestDump dump = TestDump.getTestDump(); |
| 324 | AhatInstance obj = dump.getAhatSnapshot().findClass("Main"); |
| 325 | assertNull(obj.asBitmap()); |
| 326 | } |
| 327 | |
| 328 | @Test |
| 329 | public void classInstanceToString() throws IOException { |
| 330 | TestDump dump = TestDump.getTestDump(); |
| 331 | AhatInstance obj = dump.getDumpedAhatInstance("aPhantomReference"); |
| 332 | long id = obj.getId(); |
| 333 | assertEquals(String.format("java.lang.ref.PhantomReference@%08x", id), obj.toString()); |
| 334 | } |
| 335 | |
| 336 | @Test |
| 337 | public void classObjToString() throws IOException { |
| 338 | TestDump dump = TestDump.getTestDump(); |
| 339 | AhatInstance obj = dump.getAhatSnapshot().findClass("Main"); |
Richard Uhler | 2687050 | 2017-07-04 15:55:19 +0100 | [diff] [blame^] | 340 | assertEquals("class Main", obj.toString()); |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Test |
| 344 | public void arrayInstanceToString() throws IOException { |
| 345 | TestDump dump = TestDump.getTestDump(); |
| 346 | AhatInstance obj = dump.getDumpedAhatInstance("gcPathArray"); |
| 347 | long id = obj.getId(); |
| 348 | |
| 349 | // There's a bug in perfib's proguard deobfuscation for arrays. |
| 350 | // To work around that bug for the time being, only test the suffix of |
| 351 | // the toString result. Ideally we test for string equality against |
| 352 | // "Main$ObjectTree[4]@%08x", id. |
| 353 | assertTrue(obj.toString().endsWith(String.format("[4]@%08x", id))); |
| 354 | } |
| 355 | |
| 356 | @Test |
| 357 | public void primArrayInstanceToString() throws IOException { |
| 358 | TestDump dump = TestDump.getTestDump(); |
| 359 | AhatInstance obj = dump.getDumpedAhatInstance("bigArray"); |
| 360 | long id = obj.getId(); |
| 361 | assertEquals(String.format("byte[1000000]@%08x", id), obj.toString()); |
| 362 | } |
| 363 | |
| 364 | @Test |
| 365 | public void isNotRoot() throws IOException { |
| 366 | TestDump dump = TestDump.getTestDump(); |
| 367 | AhatInstance obj = dump.getDumpedAhatInstance("anObject"); |
| 368 | assertFalse(obj.isRoot()); |
| 369 | assertNull(obj.getRootTypes()); |
| 370 | } |
| 371 | |
| 372 | @Test |
Richard Uhler | 2687050 | 2017-07-04 15:55:19 +0100 | [diff] [blame^] | 373 | public void reverseReferences() throws IOException { |
| 374 | TestDump dump = TestDump.getTestDump(); |
| 375 | AhatInstance obj = dump.getDumpedAhatInstance("anObject"); |
| 376 | AhatInstance ref = dump.getDumpedAhatInstance("aReference"); |
| 377 | AhatInstance weak = dump.getDumpedAhatInstance("aWeakReference"); |
| 378 | assertTrue(obj.getHardReverseReferences().contains(ref)); |
| 379 | assertFalse(obj.getHardReverseReferences().contains(weak)); |
| 380 | assertFalse(obj.getSoftReverseReferences().contains(ref)); |
| 381 | assertTrue(obj.getSoftReverseReferences().contains(weak)); |
| 382 | } |
| 383 | |
| 384 | @Test |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 385 | public void asStringEmbedded() throws IOException { |
| 386 | // Set up a heap dump with an instance of java.lang.String of |
| 387 | // "hello" with instance id 0x42 that is backed by a char array that is |
| 388 | // bigger. This is how ART used to represent strings, and we should still |
| 389 | // support it in case the heap dump is from a previous platform version. |
| 390 | HprofStringBuilder strings = new HprofStringBuilder(0); |
| 391 | List<HprofRecord> records = new ArrayList<HprofRecord>(); |
| 392 | List<HprofDumpRecord> dump = new ArrayList<HprofDumpRecord>(); |
| 393 | |
| 394 | final int stringClassObjectId = 1; |
| 395 | records.add(new HprofLoadClass(0, 0, stringClassObjectId, 0, strings.get("java.lang.String"))); |
| 396 | dump.add(new HprofClassDump(stringClassObjectId, 0, 0, 0, 0, 0, 0, 0, 0, |
| 397 | new HprofConstant[0], new HprofStaticField[0], |
| 398 | new HprofInstanceField[]{ |
| 399 | new HprofInstanceField(strings.get("count"), HprofType.TYPE_INT), |
| 400 | new HprofInstanceField(strings.get("hashCode"), HprofType.TYPE_INT), |
| 401 | new HprofInstanceField(strings.get("offset"), HprofType.TYPE_INT), |
| 402 | new HprofInstanceField(strings.get("value"), HprofType.TYPE_OBJECT)})); |
| 403 | |
| 404 | dump.add(new HprofPrimitiveArrayDump(0x41, 0, HprofType.TYPE_CHAR, |
| 405 | new long[]{'n', 'o', 't', ' ', 'h', 'e', 'l', 'l', 'o', 'o', 'p'})); |
| 406 | |
| 407 | ByteArrayDataOutput values = ByteStreams.newDataOutput(); |
| 408 | values.writeInt(5); // count |
| 409 | values.writeInt(0); // hashCode |
| 410 | values.writeInt(4); // offset |
| 411 | values.writeInt(0x41); // value |
| 412 | dump.add(new HprofInstanceDump(0x42, 0, stringClassObjectId, values.toByteArray())); |
| 413 | dump.add(new HprofRootDebugger(stringClassObjectId)); |
| 414 | dump.add(new HprofRootDebugger(0x42)); |
| 415 | |
| 416 | records.add(new HprofHeapDump(0, dump.toArray(new HprofDumpRecord[0]))); |
| 417 | AhatSnapshot snapshot = SnapshotBuilder.makeSnapshot(strings, records); |
| 418 | AhatInstance chars = snapshot.findInstance(0x41); |
| 419 | assertNotNull(chars); |
| 420 | assertEquals("not helloop", chars.asString()); |
| 421 | |
| 422 | AhatInstance stringInstance = snapshot.findInstance(0x42); |
| 423 | assertNotNull(stringInstance); |
| 424 | assertEquals("hello", stringInstance.asString()); |
| 425 | } |
| 426 | } |