xref: /aosp_15_r20/external/leakcanary2/shark-graph/src/main/java/shark/HprofIndex.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1 package shark
2 
3 import shark.internal.HprofInMemoryIndex
4 import java.util.EnumSet
5 
6 /**
7  * An index on a Hprof file. See [openHeapGraph].
8  */
9 class HprofIndex private constructor(
10   private val sourceProvider: RandomAccessSourceProvider,
11   private val header: HprofHeader,
12   private val index: HprofInMemoryIndex
13 ) {
14 
15   /**
16    * Opens a [CloseableHeapGraph] which you can use to navigate the indexed hprof and then close.
17    */
openHeapGraphnull18   fun openHeapGraph(): CloseableHeapGraph {
19     val reader = RandomAccessHprofReader.openReaderFor(sourceProvider, header)
20     return HprofHeapGraph(header, reader, index)
21   }
22 
23   companion object {
24     /**
25      * Creates an in memory index of an hprof source provided by [hprofSourceProvider].
26      */
indexRecordsOfnull27     fun indexRecordsOf(
28       hprofSourceProvider: DualSourceProvider,
29       hprofHeader: HprofHeader,
30       proguardMapping: ProguardMapping? = null,
31       indexedGcRootTags: Set<HprofRecordTag> = defaultIndexedGcRootTags()
32     ): HprofIndex {
33       val reader = StreamingHprofReader.readerFor(hprofSourceProvider, hprofHeader)
34       val index = HprofInMemoryIndex.indexHprof(
35         reader = reader,
36         hprofHeader = hprofHeader,
37         proguardMapping = proguardMapping,
38         indexedGcRootTags = indexedGcRootTags
39       )
40       return HprofIndex(hprofSourceProvider, hprofHeader, index)
41     }
42 
defaultIndexedGcRootTagsnull43     fun defaultIndexedGcRootTags(): EnumSet<HprofRecordTag> = EnumSet.of(
44       HprofRecordTag.ROOT_JNI_GLOBAL,
45       HprofRecordTag.ROOT_JAVA_FRAME,
46       HprofRecordTag.ROOT_JNI_LOCAL,
47       HprofRecordTag.ROOT_MONITOR_USED,
48       HprofRecordTag.ROOT_NATIVE_STACK,
49       HprofRecordTag.ROOT_STICKY_CLASS,
50       HprofRecordTag.ROOT_THREAD_BLOCK,
51       // ThreadObject points to threads, which we need to find the thread that a JavaLocalPattern
52       // belongs to
53       HprofRecordTag.ROOT_THREAD_OBJECT,
54       HprofRecordTag.ROOT_JNI_MONITOR
55       /*
56       Not included here:
57 
58       VmInternal: Ignoring because we've got 150K of it, but is this the right thing
59       to do? What's VmInternal exactly? History does not go further than
60       https://android.googlesource.com/platform/dalvik2/+/refs/heads/master/hit/src/com/android/hit/HprofParser.java#77
61       We should log to figure out what objects VmInternal points to.
62 
63       ReferenceCleanup: We used to keep it, but the name doesn't seem like it should create a leak.
64 
65       Unknown: it's unknown, should we care?
66 
67       We definitely don't care about those for leak finding: InternedString, Finalizing, Debugger, Unreachable
68        */
69     )
70   }
71 }
72