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.AhatHeap; 20 import com.android.ahat.heapdump.AhatInstance; 21 import com.android.ahat.heapdump.AhatSnapshot; 22 import com.android.ahat.heapdump.Site; 23 import com.android.ahat.heapdump.Sort; 24 import java.io.IOException; 25 import java.util.ArrayList; 26 import java.util.Collections; 27 import java.util.List; 28 import java.util.function.Predicate; 29 30 class ObjectsHandler implements AhatHandler { 31 private static final String OBJECTS_ID = "objects"; 32 33 private AhatSnapshot mSnapshot; 34 ObjectsHandler(AhatSnapshot snapshot)35 public ObjectsHandler(AhatSnapshot snapshot) { 36 mSnapshot = snapshot; 37 } 38 39 /** 40 * Get the list of instances that match the given site, class, and heap 41 * filters. This method is public to facilitate testing. 42 * 43 * @param site the site to get instances from 44 * @param className non-null name of the class to restrict instances to. 45 * @param subclass if true, include instances of subclasses of the named class. 46 * @param heapName name of the heap to restrict instances to. May be null to 47 * allow instances on any heap. 48 * @return list of matching instances 49 */ getObjects( Site site, String className, boolean subclass, String heapName)50 public static List<AhatInstance> getObjects( 51 Site site, String className, boolean subclass, String heapName) { 52 Predicate<AhatInstance> predicate = (x) -> { 53 return (heapName == null || x.getHeap().getName().equals(heapName)) 54 && (subclass ? x.isInstanceOfClass(className) : className.equals(x.getClassName())); 55 }; 56 57 List<AhatInstance> insts = new ArrayList<AhatInstance>(); 58 site.getObjects(predicate, x -> insts.add(x)); 59 return insts; 60 } 61 62 @Override handle(Doc doc, Query query)63 public void handle(Doc doc, Query query) throws IOException { 64 int id = query.getInt("id", 0); 65 String className = query.get("class", "java.lang.Object"); 66 String heapName = query.get("heap", null); 67 boolean subclass = (query.getInt("subclass", 0) != 0); 68 Site site = mSnapshot.getSite(id); 69 70 List<AhatInstance> insts = getObjects(site, className, subclass, heapName); 71 Collections.sort(insts, Sort.defaultInstanceCompare(mSnapshot)); 72 73 doc.title("Instances"); 74 75 // Write a description of the current settings, with links to adjust the 76 // settings, such as: 77 // Site: ROOT - 78 // Class: android.os.Binder 79 // Subclasses: excluded (switch to included) 80 // Heap: any (switch to app, image, zygote) 81 // Count: 17,424 82 doc.descriptions(); 83 doc.description(DocString.text("Site"), Summarizer.summarize(site)); 84 doc.description(DocString.text("Class"), DocString.text(className)); 85 86 DocString subclassChoice = DocString.text(subclass ? "included" : "excluded"); 87 subclassChoice.append(" (switch to "); 88 subclassChoice.appendLink(query.with("subclass", subclass ? 0 : 1), 89 DocString.text(subclass ? "excluded" : "included")); 90 subclassChoice.append(")"); 91 doc.description(DocString.text("Subclasses"), subclassChoice); 92 93 DocString heapChoice = DocString.text(heapName == null ? "any" : heapName); 94 heapChoice.append(" (switch to "); 95 String comma = ""; 96 for (AhatHeap heap : mSnapshot.getHeaps()) { 97 if (!heap.getName().equals(heapName)) { 98 heapChoice.append(comma); 99 heapChoice.appendLink( 100 query.with("heap", heap.getName()), 101 DocString.text(heap.getName())); 102 comma = ", "; 103 } 104 } 105 if (heapName != null) { 106 heapChoice.append(comma); 107 heapChoice.appendLink( 108 query.with("heap", null), 109 DocString.text("any")); 110 } 111 heapChoice.append(")"); 112 doc.description(DocString.text("Heap"), heapChoice); 113 114 doc.description(DocString.text("Count"), DocString.format("%,14d", insts.size())); 115 doc.end(); 116 doc.println(DocString.text("")); 117 118 if (insts.isEmpty()) { 119 doc.println(DocString.text("(none)")); 120 } else { 121 SizeTable.table(doc, mSnapshot.isDiffed(), 122 new Column("Heap"), 123 new Column("Object")); 124 125 SubsetSelector<AhatInstance> selector = new SubsetSelector(query, OBJECTS_ID, insts); 126 for (AhatInstance inst : selector.selected()) { 127 AhatInstance base = inst.getBaseline(); 128 SizeTable.row(doc, inst.getSize(), base.getSize(), 129 DocString.text(inst.getHeap().getName()), 130 Summarizer.summarize(inst)); 131 } 132 SizeTable.end(doc); 133 selector.render(doc); 134 } 135 } 136 } 137 138