blob: 4df1be5ac2016e34073118f37e44e8d0310a868d [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;
Richard Uhler38f9eba2015-11-11 08:31:52 -080026import com.android.tools.perflib.heap.RootType;
Richard Uhlerb730b782015-07-15 16:01:58 -070027import java.io.IOException;
28import java.util.ArrayList;
Richard Uhler1af86f12015-10-29 14:55:00 -070029import java.util.Arrays;
Richard Uhler38f9eba2015-11-11 08:31:52 -080030import java.util.Collection;
Richard Uhlerb730b782015-07-15 16:01:58 -070031import java.util.Collections;
32import java.util.List;
33import java.util.Map;
34
Richard Uhler1af86f12015-10-29 14:55:00 -070035class ObjectHandler implements AhatHandler {
36
37 private static final String ARRAY_ELEMENTS_ID = "elements";
38 private static final String DOMINATOR_PATH_ID = "dompath";
39 private static final String ALLOCATION_SITE_ID = "frames";
40 private static final String DOMINATED_OBJECTS_ID = "dominated";
41 private static final String INSTANCE_FIELDS_ID = "ifields";
42 private static final String STATIC_FIELDS_ID = "sfields";
43 private static final String HARD_REFS_ID = "refs";
44 private static final String SOFT_REFS_ID = "srefs";
45
46 private AhatSnapshot mSnapshot;
47
Richard Uhlerb730b782015-07-15 16:01:58 -070048 public ObjectHandler(AhatSnapshot snapshot) {
Richard Uhler1af86f12015-10-29 14:55:00 -070049 mSnapshot = snapshot;
Richard Uhlerb730b782015-07-15 16:01:58 -070050 }
51
52 @Override
53 public void handle(Doc doc, Query query) throws IOException {
54 long id = query.getLong("id", 0);
55 Instance inst = mSnapshot.findInstance(id);
56 if (inst == null) {
Richard Uhlerc21e4e62015-08-31 16:16:14 -070057 doc.println(DocString.format("No object with id %08xl", id));
Richard Uhlerb730b782015-07-15 16:01:58 -070058 return;
59 }
60
61 doc.title("Object %08x", inst.getUniqueId());
Richard Uhler38f9eba2015-11-11 08:31:52 -080062 doc.big(Value.render(mSnapshot, inst));
Richard Uhlerb730b782015-07-15 16:01:58 -070063
Richard Uhler1af86f12015-10-29 14:55:00 -070064 printAllocationSite(doc, query, inst);
65 printDominatorPath(doc, query, inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070066
67 doc.section("Object Info");
68 ClassObj cls = inst.getClassObj();
69 doc.descriptions();
Richard Uhler38f9eba2015-11-11 08:31:52 -080070 doc.description(DocString.text("Class"), Value.render(mSnapshot, cls));
Richard Uhlerc21e4e62015-08-31 16:16:14 -070071 doc.description(DocString.text("Size"), DocString.format("%d", inst.getSize()));
Richard Uhlerb730b782015-07-15 16:01:58 -070072 doc.description(
73 DocString.text("Retained Size"),
Richard Uhlerc21e4e62015-08-31 16:16:14 -070074 DocString.format("%d", inst.getTotalRetainedSize()));
Richard Uhlerb730b782015-07-15 16:01:58 -070075 doc.description(DocString.text("Heap"), DocString.text(inst.getHeap().getName()));
Richard Uhler38f9eba2015-11-11 08:31:52 -080076
77 Collection<RootType> rootTypes = mSnapshot.getRootTypes(inst);
78 if (rootTypes != null) {
79 DocString types = new DocString();
80 String comma = "";
81 for (RootType type : rootTypes) {
82 types.append(comma);
83 types.append(type.getName());
84 comma = ", ";
85 }
86 doc.description(DocString.text("Root Types"), types);
87 }
88
Richard Uhlerb730b782015-07-15 16:01:58 -070089 doc.end();
90
91 printBitmap(doc, inst);
92 if (inst instanceof ClassInstance) {
Richard Uhler38f9eba2015-11-11 08:31:52 -080093 printClassInstanceFields(doc, query, mSnapshot, (ClassInstance)inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070094 } else if (inst instanceof ArrayInstance) {
Richard Uhler38f9eba2015-11-11 08:31:52 -080095 printArrayElements(doc, query, mSnapshot, (ArrayInstance)inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070096 } else if (inst instanceof ClassObj) {
Richard Uhler38f9eba2015-11-11 08:31:52 -080097 printClassInfo(doc, query, mSnapshot, (ClassObj)inst);
Richard Uhlerb730b782015-07-15 16:01:58 -070098 }
Richard Uhler38f9eba2015-11-11 08:31:52 -080099 printReferences(doc, query, mSnapshot, inst);
Richard Uhlerb730b782015-07-15 16:01:58 -0700100 printDominatedObjects(doc, query, inst);
101 }
102
Richard Uhler38f9eba2015-11-11 08:31:52 -0800103 private static void printClassInstanceFields(
104 Doc doc, Query query, AhatSnapshot snapshot, ClassInstance inst) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700105 doc.section("Fields");
106 doc.table(new Column("Type"), new Column("Name"), new Column("Value"));
Richard Uhler1af86f12015-10-29 14:55:00 -0700107 SubsetSelector<ClassInstance.FieldValue> selector
108 = new SubsetSelector(query, INSTANCE_FIELDS_ID, inst.getValues());
109 for (ClassInstance.FieldValue field : selector.selected()) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700110 doc.row(
111 DocString.text(field.getField().getType().toString()),
112 DocString.text(field.getField().getName()),
Richard Uhler38f9eba2015-11-11 08:31:52 -0800113 Value.render(snapshot, field.getValue()));
Richard Uhlerb730b782015-07-15 16:01:58 -0700114 }
115 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700116 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700117 }
118
Richard Uhler38f9eba2015-11-11 08:31:52 -0800119 private static void printArrayElements(
120 Doc doc, Query query, AhatSnapshot snapshot, ArrayInstance array) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700121 doc.section("Array Elements");
122 doc.table(new Column("Index", Column.Align.RIGHT), new Column("Value"));
Richard Uhler1af86f12015-10-29 14:55:00 -0700123 List<Object> elements = Arrays.asList(array.getValues());
124 SubsetSelector<Object> selector = new SubsetSelector(query, ARRAY_ELEMENTS_ID, elements);
125 int i = 0;
126 for (Object elem : selector.selected()) {
Richard Uhler38f9eba2015-11-11 08:31:52 -0800127 doc.row(DocString.format("%d", i), Value.render(snapshot, elem));
Richard Uhler1af86f12015-10-29 14:55:00 -0700128 i++;
Richard Uhlerb730b782015-07-15 16:01:58 -0700129 }
130 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700131 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700132 }
133
Richard Uhler38f9eba2015-11-11 08:31:52 -0800134 private static void printClassInfo(
135 Doc doc, Query query, AhatSnapshot snapshot, ClassObj clsobj) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700136 doc.section("Class Info");
137 doc.descriptions();
Richard Uhler38f9eba2015-11-11 08:31:52 -0800138 doc.description(DocString.text("Super Class"),
139 Value.render(snapshot, clsobj.getSuperClassObj()));
140 doc.description(DocString.text("Class Loader"),
141 Value.render(snapshot, clsobj.getClassLoader()));
Richard Uhlerb730b782015-07-15 16:01:58 -0700142 doc.end();
143
144 doc.section("Static Fields");
145 doc.table(new Column("Type"), new Column("Name"), new Column("Value"));
Richard Uhler1af86f12015-10-29 14:55:00 -0700146 List<Map.Entry<Field, Object>> fields
147 = new ArrayList<Map.Entry<Field, Object>>(clsobj.getStaticFieldValues().entrySet());
148 SubsetSelector<Map.Entry<Field, Object>> selector
149 = new SubsetSelector(query, STATIC_FIELDS_ID, fields);
150 for (Map.Entry<Field, Object> field : selector.selected()) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700151 doc.row(
152 DocString.text(field.getKey().getType().toString()),
153 DocString.text(field.getKey().getName()),
Richard Uhler38f9eba2015-11-11 08:31:52 -0800154 Value.render(snapshot, field.getValue()));
Richard Uhlerb730b782015-07-15 16:01:58 -0700155 }
156 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700157 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700158 }
159
Richard Uhler38f9eba2015-11-11 08:31:52 -0800160 private static void printReferences(
161 Doc doc, Query query, AhatSnapshot snapshot, Instance inst) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700162 doc.section("Objects with References to this Object");
Richard Uhler6919a012015-12-15 09:15:00 -0800163 if (inst.getHardReverseReferences().isEmpty()) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700164 doc.println(DocString.text("(none)"));
165 } else {
166 doc.table(new Column("Object"));
Richard Uhler6919a012015-12-15 09:15:00 -0800167 List<Instance> references = inst.getHardReverseReferences();
Richard Uhler1af86f12015-10-29 14:55:00 -0700168 SubsetSelector<Instance> selector = new SubsetSelector(query, HARD_REFS_ID, references);
169 for (Instance ref : selector.selected()) {
Richard Uhler38f9eba2015-11-11 08:31:52 -0800170 doc.row(Value.render(snapshot, ref));
Richard Uhlerb730b782015-07-15 16:01:58 -0700171 }
172 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700173 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700174 }
175
Richard Uhler6919a012015-12-15 09:15:00 -0800176 if (inst.getSoftReverseReferences() != null) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700177 doc.section("Objects with Soft References to this Object");
178 doc.table(new Column("Object"));
Richard Uhler6919a012015-12-15 09:15:00 -0800179 List<Instance> references = inst.getSoftReverseReferences();
Richard Uhler1af86f12015-10-29 14:55:00 -0700180 SubsetSelector<Instance> selector = new SubsetSelector(query, SOFT_REFS_ID, references);
181 for (Instance ref : selector.selected()) {
Richard Uhler38f9eba2015-11-11 08:31:52 -0800182 doc.row(Value.render(snapshot, ref));
Richard Uhlerb730b782015-07-15 16:01:58 -0700183 }
184 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700185 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700186 }
187 }
188
Richard Uhler1af86f12015-10-29 14:55:00 -0700189 private void printAllocationSite(Doc doc, Query query, Instance inst) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700190 doc.section("Allocation Site");
191 Site site = mSnapshot.getSiteForInstance(inst);
Richard Uhler1af86f12015-10-29 14:55:00 -0700192 SitePrinter.printSite(mSnapshot, doc, query, ALLOCATION_SITE_ID, site);
Richard Uhlerb730b782015-07-15 16:01:58 -0700193 }
194
195 // Draw the bitmap corresponding to this instance if there is one.
196 private static void printBitmap(Doc doc, Instance inst) {
197 Instance bitmap = InstanceUtils.getAssociatedBitmapInstance(inst);
198 if (bitmap != null) {
199 doc.section("Bitmap Image");
200 doc.println(DocString.image(
Richard Uhlerc21e4e62015-08-31 16:16:14 -0700201 DocString.formattedUri("bitmap?id=%d", bitmap.getId()), "bitmap image"));
Richard Uhlerb730b782015-07-15 16:01:58 -0700202 }
203 }
204
Richard Uhler1af86f12015-10-29 14:55:00 -0700205 private void printDominatorPath(Doc doc, Query query, Instance inst) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700206 doc.section("Dominator Path from Root");
207 List<Instance> path = new ArrayList<Instance>();
208 for (Instance parent = inst;
209 parent != null && !(parent instanceof RootObj);
210 parent = parent.getImmediateDominator()) {
211 path.add(parent);
212 }
213
214 // Add 'null' as a marker for the root.
215 path.add(null);
216 Collections.reverse(path);
217
218 HeapTable.TableConfig<Instance> table = new HeapTable.TableConfig<Instance>() {
219 public String getHeapsDescription() {
220 return "Bytes Retained by Heap";
221 }
222
223 public long getSize(Instance element, Heap heap) {
224 if (element == null) {
225 return mSnapshot.getHeapSize(heap);
226 }
227 int index = mSnapshot.getHeapIndex(heap);
228 return element.getRetainedSize(index);
229 }
230
231 public List<HeapTable.ValueConfig<Instance>> getValueConfigs() {
232 HeapTable.ValueConfig<Instance> value = new HeapTable.ValueConfig<Instance>() {
233 public String getDescription() {
234 return "Object";
235 }
236
237 public DocString render(Instance element) {
238 if (element == null) {
Richard Uhler7a16adb2015-11-11 09:13:23 -0800239 return DocString.link(DocString.uri("rooted"), DocString.text("ROOT"));
Richard Uhlerb730b782015-07-15 16:01:58 -0700240 } else {
Richard Uhler38f9eba2015-11-11 08:31:52 -0800241 return DocString.text("→ ").append(Value.render(mSnapshot, element));
Richard Uhlerb730b782015-07-15 16:01:58 -0700242 }
243 }
244 };
245 return Collections.singletonList(value);
246 }
247 };
Richard Uhler1af86f12015-10-29 14:55:00 -0700248 HeapTable.render(doc, query, DOMINATOR_PATH_ID, table, mSnapshot, path);
Richard Uhlerb730b782015-07-15 16:01:58 -0700249 }
250
251 public void printDominatedObjects(Doc doc, Query query, Instance inst) {
252 doc.section("Immediately Dominated Objects");
253 List<Instance> instances = mSnapshot.getDominated(inst);
254 if (instances != null) {
Richard Uhler1af86f12015-10-29 14:55:00 -0700255 DominatedList.render(mSnapshot, doc, query, DOMINATED_OBJECTS_ID, instances);
Richard Uhlerb730b782015-07-15 16:01:58 -0700256 } else {
257 doc.println(DocString.text("(none)"));
258 }
259 }
260}
261