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.AhatInstance; 20 import com.android.ahat.heapdump.AhatSnapshot; 21 import com.android.ahat.heapdump.Site; 22 import com.android.ahat.heapdump.Sort; 23 import java.io.IOException; 24 import java.util.ArrayList; 25 import java.util.Collections; 26 import java.util.List; 27 28 class ObjectsHandler implements AhatHandler { 29 private static final String OBJECTS_ID = "objects"; 30 31 private AhatSnapshot mSnapshot; 32 ObjectsHandler(AhatSnapshot snapshot)33 public ObjectsHandler(AhatSnapshot snapshot) { 34 mSnapshot = snapshot; 35 } 36 37 @Override handle(Doc doc, Query query)38 public void handle(Doc doc, Query query) throws IOException { 39 int id = query.getInt("id", 0); 40 int depth = query.getInt("depth", 0); 41 String className = query.get("class", null); 42 String heapName = query.get("heap", null); 43 Site site = mSnapshot.getSite(id, depth); 44 45 List<AhatInstance> insts = new ArrayList<AhatInstance>(); 46 for (AhatInstance inst : site.getObjects()) { 47 if ((heapName == null || inst.getHeap().getName().equals(heapName)) 48 && (className == null || inst.getClassName().equals(className))) { 49 insts.add(inst); 50 } 51 } 52 53 Collections.sort(insts, Sort.defaultInstanceCompare(mSnapshot)); 54 55 doc.title("Objects"); 56 57 doc.table( 58 new Column("Size", Column.Align.RIGHT), 59 new Column("Δ", Column.Align.RIGHT, mSnapshot.isDiffed()), 60 new Column("Heap"), 61 new Column("Object")); 62 63 SubsetSelector<AhatInstance> selector = new SubsetSelector(query, OBJECTS_ID, insts); 64 for (AhatInstance inst : selector.selected()) { 65 AhatInstance base = inst.getBaseline(); 66 doc.row( 67 DocString.format("%,14d", inst.getSize()), 68 DocString.delta(inst.isPlaceHolder(), base.isPlaceHolder(), 69 inst.getSize(), base.getSize()), 70 DocString.text(inst.getHeap().getName()), 71 Summarizer.summarize(inst)); 72 } 73 doc.end(); 74 selector.render(doc); 75 } 76 } 77 78