1 /*
2  * Copyright (C) 2008 Google Inc.
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.hit;
18 
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 
22 /*
23  * State is a snapshot of all of the heaps, and related meta-data, for
24  * the runtime at a given instant.
25  *
26  * During parsing of the HPROF file HEAP_DUMP_INFO chunks change which heap
27  * is being referenced.
28  */
29 public class State {
30     HashMap<Integer, Heap> mHeaps;
31     Heap mCurrentHeap;
32 
State()33     public State() {
34         mHeaps = new HashMap<Integer, Heap>();
35         setToDefaultHeap();
36     }
37 
setToDefaultHeap()38     public Heap setToDefaultHeap() {
39         return setHeapTo(0, "default");
40     }
41 
setHeapTo(int id, String name)42     public Heap setHeapTo(int id, String name) {
43         Heap heap = mHeaps.get(id);
44 
45         if (heap == null) {
46             heap = new Heap(name);
47             heap.mState = this;
48             mHeaps.put(id, heap);
49         }
50 
51         mCurrentHeap = heap;
52 
53         return mCurrentHeap;
54     }
55 
getHeap(int id)56     public Heap getHeap(int id) {
57         return mHeaps.get(id);
58     }
59 
getHeap(String name)60     public Heap getHeap(String name) {
61         for (Heap heap: mHeaps.values()) {
62             if (heap.mName.equals(name)) {
63                 return heap;
64             }
65         }
66 
67         return null;
68     }
69 
addStackFrame(StackFrame theFrame)70     public final void addStackFrame(StackFrame theFrame) {
71         mCurrentHeap.addStackFrame(theFrame);
72     }
73 
getStackFrame(long id)74     public final StackFrame getStackFrame(long id) {
75         return mCurrentHeap.getStackFrame(id);
76     }
77 
addStackTrace(StackTrace theTrace)78     public final void addStackTrace(StackTrace theTrace) {
79         mCurrentHeap.addStackTrace(theTrace);
80     }
81 
getStackTrace(int traceSerialNumber)82     public final StackTrace getStackTrace(int traceSerialNumber) {
83         return mCurrentHeap.getStackTrace(traceSerialNumber);
84     }
85 
getStackTraceAtDepth(int traceSerialNumber, int depth)86     public final StackTrace getStackTraceAtDepth(int traceSerialNumber,
87             int depth) {
88         return mCurrentHeap.getStackTraceAtDepth(traceSerialNumber, depth);
89     }
90 
addRoot(RootObj root)91     public final void addRoot(RootObj root) {
92         mCurrentHeap.addRoot(root);
93     }
94 
addThread(ThreadObj thread, int serialNumber)95     public final void addThread(ThreadObj thread, int serialNumber) {
96         mCurrentHeap.addThread(thread, serialNumber);
97     }
98 
getThread(int serialNumber)99     public final ThreadObj getThread(int serialNumber) {
100         return mCurrentHeap.getThread(serialNumber);
101     }
102 
addInstance(long id, Instance instance)103     public final void addInstance(long id, Instance instance) {
104         mCurrentHeap.addInstance(id, instance);
105     }
106 
addClass(long id, ClassObj theClass)107     public final void addClass(long id, ClassObj theClass) {
108         mCurrentHeap.addClass(id, theClass);
109     }
110 
findReference(long id)111     public final Instance findReference(long id) {
112         for (Heap heap: mHeaps.values()) {
113             Instance instance = heap.getInstance(id);
114 
115             if (instance != null) {
116                 return instance;
117             }
118         }
119 
120         //  Couldn't find an instance of a class, look for a class object
121         return findClass(id);
122     }
123 
findClass(long id)124     public final ClassObj findClass(long id) {
125         for (Heap heap: mHeaps.values()) {
126             ClassObj theClass = heap.getClass(id);
127 
128             if (theClass != null) {
129                 return theClass;
130             }
131         }
132 
133         return null;
134     }
135 
findClass(String name)136     public final ClassObj findClass(String name) {
137         for (Heap heap: mHeaps.values()) {
138             ClassObj theClass = heap.getClass(name);
139 
140             if (theClass != null) {
141                 return theClass;
142             }
143         }
144 
145         return null;
146     }
147 
dumpInstanceCounts()148     public final void dumpInstanceCounts() {
149         for (Heap heap: mHeaps.values()) {
150             System.out.println(
151                 "+------------------ instance counts for heap: " + heap.mName);
152             heap.dumpInstanceCounts();
153         }
154     }
155 
dumpSizes()156     public final void dumpSizes() {
157         for (Heap heap: mHeaps.values()) {
158             System.out.println(
159                 "+------------------ sizes for heap: " + heap.mName);
160             heap.dumpSizes();
161         }
162     }
163 
dumpSubclasses()164     public final void dumpSubclasses() {
165         for (Heap heap: mHeaps.values()) {
166             System.out.println(
167                 "+------------------ subclasses for heap: " + heap.mName);
168             heap.dumpSubclasses();
169         }
170     }
171 
resolveReferences()172     public final void resolveReferences() {
173         for (Heap heap: mHeaps.values()) {
174             heap.resolveInstanceRefs(this);
175             heap.resolveClassStatics(this);
176             heap.resolveRoots(this);
177         }
178     }
179 }
180