blob: 1305070b0f62a0a3e738ca1553307336ee354825 [file] [log] [blame]
Richard Uhlerb730b782015-07-15 16:01:58 -07001/*
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
17package com.android.ahat;
18
19import com.android.tools.perflib.heap.ArrayInstance;
20import com.android.tools.perflib.heap.ClassInstance;
21import com.android.tools.perflib.heap.ClassObj;
22import com.android.tools.perflib.heap.Field;
23import com.android.tools.perflib.heap.Heap;
24import com.android.tools.perflib.heap.Instance;
25import com.android.tools.perflib.heap.RootObj;
26import java.io.IOException;
27import java.util.ArrayList;
Richard Uhler1af86f12015-10-29 14:55:00 -070028import java.util.Arrays;
Richard Uhlerb730b782015-07-15 16:01:58 -070029import java.util.Collections;
30import java.util.List;
31import java.util.Map;
32
Richard Uhler1af86f12015-10-29 14:55:00 -070033class ObjectHandler implements AhatHandler {
34
35 private static final String ARRAY_ELEMENTS_ID = "elements";
36 private static final String DOMINATOR_PATH_ID = "dompath";
37 private static final String ALLOCATION_SITE_ID = "frames";
38 private static final String DOMINATED_OBJECTS_ID = "dominated";
39 private static final String INSTANCE_FIELDS_ID = "ifields";
40 private static final String STATIC_FIELDS_ID = "sfields";
41 private static final String HARD_REFS_ID = "refs";
42 private static final String SOFT_REFS_ID = "srefs";
43
44 private AhatSnapshot mSnapshot;
45
Richard Uhlerb730b782015-07-15 16:01:58 -070046 public ObjectHandler(AhatSnapshot snapshot) {
Richard Uhler1af86f12015-10-29 14:55:00 -070047 mSnapshot = snapshot;
Richard Uhlerb730b782015-07-15 16:01:58 -070048 }
49
50 @Override
51 public void handle(Doc doc, Query query) throws IOException {
52 long id = query.getLong("id", 0);
53 Instance inst = mSnapshot.findInstance(id);
54 if (inst == null) {
Richard Uhlerc21e4e62015-08-31 16:16:14 -070055 doc.println(DocString.format("No object with id %08xl", id));
Richard Uhlerb730b782015-07-15 16:01:58 -070056 return;
57 }
58
59 doc.title("Object %08x", inst.getUniqueId());
60 doc.big(Value.render(inst));
61
Richard Uhler1af86f12015-10-29 14:55:00 -070062 printAllocationSite(doc, query, inst);
63 printDominatorPath(doc, query, inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070064
65 doc.section("Object Info");
66 ClassObj cls = inst.getClassObj();
67 doc.descriptions();
68 doc.description(DocString.text("Class"), Value.render(cls));
Richard Uhlerc21e4e62015-08-31 16:16:14 -070069 doc.description(DocString.text("Size"), DocString.format("%d", inst.getSize()));
Richard Uhlerb730b782015-07-15 16:01:58 -070070 doc.description(
71 DocString.text("Retained Size"),
Richard Uhlerc21e4e62015-08-31 16:16:14 -070072 DocString.format("%d", inst.getTotalRetainedSize()));
Richard Uhlerb730b782015-07-15 16:01:58 -070073 doc.description(DocString.text("Heap"), DocString.text(inst.getHeap().getName()));
74 doc.end();
75
76 printBitmap(doc, inst);
77 if (inst instanceof ClassInstance) {
Richard Uhler1af86f12015-10-29 14:55:00 -070078 printClassInstanceFields(doc, query, (ClassInstance)inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070079 } else if (inst instanceof ArrayInstance) {
Richard Uhler1af86f12015-10-29 14:55:00 -070080 printArrayElements(doc, query, (ArrayInstance)inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070081 } else if (inst instanceof ClassObj) {
Richard Uhler1af86f12015-10-29 14:55:00 -070082 printClassInfo(doc, query, (ClassObj)inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070083 }
Richard Uhler1af86f12015-10-29 14:55:00 -070084 printReferences(doc, query, inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070085 printDominatedObjects(doc, query, inst);
86 }
87
Richard Uhler1af86f12015-10-29 14:55:00 -070088 private static void printClassInstanceFields(Doc doc, Query query, ClassInstance inst) {
Richard Uhlerb730b782015-07-15 16:01:58 -070089 doc.section("Fields");
90 doc.table(new Column("Type"), new Column("Name"), new Column("Value"));
Richard Uhler1af86f12015-10-29 14:55:00 -070091 SubsetSelector<ClassInstance.FieldValue> selector
92 = new SubsetSelector(query, INSTANCE_FIELDS_ID, inst.getValues());
93 for (ClassInstance.FieldValue field : selector.selected()) {
Richard Uhlerb730b782015-07-15 16:01:58 -070094 doc.row(
95 DocString.text(field.getField().getType().toString()),
96 DocString.text(field.getField().getName()),
97 Value.render(field.getValue()));
98 }
99 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700100 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700101 }
102
Richard Uhler1af86f12015-10-29 14:55:00 -0700103 private static void printArrayElements(Doc doc, Query query, ArrayInstance array) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700104 doc.section("Array Elements");
105 doc.table(new Column("Index", Column.Align.RIGHT), new Column("Value"));
Richard Uhler1af86f12015-10-29 14:55:00 -0700106 List<Object> elements = Arrays.asList(array.getValues());
107 SubsetSelector<Object> selector = new SubsetSelector(query, ARRAY_ELEMENTS_ID, elements);
108 int i = 0;
109 for (Object elem : selector.selected()) {
110 doc.row(DocString.format("%d", i), Value.render(elem));
111 i++;
Richard Uhlerb730b782015-07-15 16:01:58 -0700112 }
113 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700114 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700115 }
116
Richard Uhler1af86f12015-10-29 14:55:00 -0700117 private static void printClassInfo(Doc doc, Query query, ClassObj clsobj) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700118 doc.section("Class Info");
119 doc.descriptions();
120 doc.description(DocString.text("Super Class"), Value.render(clsobj.getSuperClassObj()));
121 doc.description(DocString.text("Class Loader"), Value.render(clsobj.getClassLoader()));
122 doc.end();
123
124 doc.section("Static Fields");
125 doc.table(new Column("Type"), new Column("Name"), new Column("Value"));
Richard Uhler1af86f12015-10-29 14:55:00 -0700126 List<Map.Entry<Field, Object>> fields
127 = new ArrayList<Map.Entry<Field, Object>>(clsobj.getStaticFieldValues().entrySet());
128 SubsetSelector<Map.Entry<Field, Object>> selector
129 = new SubsetSelector(query, STATIC_FIELDS_ID, fields);
130 for (Map.Entry<Field, Object> field : selector.selected()) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700131 doc.row(
132 DocString.text(field.getKey().getType().toString()),
133 DocString.text(field.getKey().getName()),
134 Value.render(field.getValue()));
135 }
136 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700137 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700138 }
139
Richard Uhler1af86f12015-10-29 14:55:00 -0700140 private static void printReferences(Doc doc, Query query, Instance inst) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700141 doc.section("Objects with References to this Object");
142 if (inst.getHardReferences().isEmpty()) {
143 doc.println(DocString.text("(none)"));
144 } else {
145 doc.table(new Column("Object"));
Richard Uhler1af86f12015-10-29 14:55:00 -0700146 List<Instance> references = inst.getHardReferences();
147 SubsetSelector<Instance> selector = new SubsetSelector(query, HARD_REFS_ID, references);
148 for (Instance ref : selector.selected()) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700149 doc.row(Value.render(ref));
150 }
151 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700152 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700153 }
154
155 if (inst.getSoftReferences() != null) {
156 doc.section("Objects with Soft References to this Object");
157 doc.table(new Column("Object"));
Richard Uhler1af86f12015-10-29 14:55:00 -0700158 List<Instance> references = inst.getSoftReferences();
159 SubsetSelector<Instance> selector = new SubsetSelector(query, SOFT_REFS_ID, references);
160 for (Instance ref : selector.selected()) {
161 doc.row(Value.render(ref));
Richard Uhlerb730b782015-07-15 16:01:58 -0700162 }
163 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700164 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700165 }
166 }
167
Richard Uhler1af86f12015-10-29 14:55:00 -0700168 private void printAllocationSite(Doc doc, Query query, Instance inst) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700169 doc.section("Allocation Site");
170 Site site = mSnapshot.getSiteForInstance(inst);
Richard Uhler1af86f12015-10-29 14:55:00 -0700171 SitePrinter.printSite(mSnapshot, doc, query, ALLOCATION_SITE_ID, site);
Richard Uhlerb730b782015-07-15 16:01:58 -0700172 }
173
174 // Draw the bitmap corresponding to this instance if there is one.
175 private static void printBitmap(Doc doc, Instance inst) {
176 Instance bitmap = InstanceUtils.getAssociatedBitmapInstance(inst);
177 if (bitmap != null) {
178 doc.section("Bitmap Image");
179 doc.println(DocString.image(
Richard Uhlerc21e4e62015-08-31 16:16:14 -0700180 DocString.formattedUri("bitmap?id=%d", bitmap.getId()), "bitmap image"));
Richard Uhlerb730b782015-07-15 16:01:58 -0700181 }
182 }
183
Richard Uhler1af86f12015-10-29 14:55:00 -0700184 private void printDominatorPath(Doc doc, Query query, Instance inst) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700185 doc.section("Dominator Path from Root");
186 List<Instance> path = new ArrayList<Instance>();
187 for (Instance parent = inst;
188 parent != null && !(parent instanceof RootObj);
189 parent = parent.getImmediateDominator()) {
190 path.add(parent);
191 }
192
193 // Add 'null' as a marker for the root.
194 path.add(null);
195 Collections.reverse(path);
196
197 HeapTable.TableConfig<Instance> table = new HeapTable.TableConfig<Instance>() {
198 public String getHeapsDescription() {
199 return "Bytes Retained by Heap";
200 }
201
202 public long getSize(Instance element, Heap heap) {
203 if (element == null) {
204 return mSnapshot.getHeapSize(heap);
205 }
206 int index = mSnapshot.getHeapIndex(heap);
207 return element.getRetainedSize(index);
208 }
209
210 public List<HeapTable.ValueConfig<Instance>> getValueConfigs() {
211 HeapTable.ValueConfig<Instance> value = new HeapTable.ValueConfig<Instance>() {
212 public String getDescription() {
213 return "Object";
214 }
215
216 public DocString render(Instance element) {
217 if (element == null) {
Richard Uhler7a16adb2015-11-11 09:13:23 -0800218 return DocString.link(DocString.uri("rooted"), DocString.text("ROOT"));
Richard Uhlerb730b782015-07-15 16:01:58 -0700219 } else {
220 return DocString.text("→ ").append(Value.render(element));
221 }
222 }
223 };
224 return Collections.singletonList(value);
225 }
226 };
Richard Uhler1af86f12015-10-29 14:55:00 -0700227 HeapTable.render(doc, query, DOMINATOR_PATH_ID, table, mSnapshot, path);
Richard Uhlerb730b782015-07-15 16:01:58 -0700228 }
229
230 public void printDominatedObjects(Doc doc, Query query, Instance inst) {
231 doc.section("Immediately Dominated Objects");
232 List<Instance> instances = mSnapshot.getDominated(inst);
233 if (instances != null) {
Richard Uhler1af86f12015-10-29 14:55:00 -0700234 DominatedList.render(mSnapshot, doc, query, DOMINATED_OBJECTS_ID, instances);
Richard Uhlerb730b782015-07-15 16:01:58 -0700235 } else {
236 doc.println(DocString.text("(none)"));
237 }
238 }
239}
240