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 java.io.IOException; 20 import java.util.Collections; 21 import java.util.Comparator; 22 import java.util.List; 23 24 class NativeAllocationsHandler implements AhatHandler { 25 private static final String ALLOCATIONS_ID = "allocations"; 26 27 private AhatSnapshot mSnapshot; 28 29 public NativeAllocationsHandler(AhatSnapshot snapshot) { 30 mSnapshot = snapshot; 31 } 32 33 @Override 34 public void handle(Doc doc, Query query) throws IOException { 35 List<InstanceUtils.NativeAllocation> allocs = mSnapshot.getNativeAllocations(); 36 37 doc.title("Registered Native Allocations"); 38 39 doc.section("Overview"); 40 long totalSize = 0; 41 for (InstanceUtils.NativeAllocation alloc : allocs) { 42 totalSize += alloc.size; 43 } 44 doc.descriptions(); 45 doc.description(DocString.text("Number of Registered Native Allocations"), 46 DocString.format("%,14d", allocs.size())); 47 doc.description(DocString.text("Total Size of Registered Native Allocations"), 48 DocString.format("%,14d", totalSize)); 49 doc.end(); 50 51 doc.section("List of Allocations"); 52 if (allocs.isEmpty()) { 53 doc.println(DocString.text("(none)")); 54 } else { 55 doc.table( 56 new Column("Size", Column.Align.RIGHT), 57 new Column("Heap"), 58 new Column("Native Pointer"), 59 new Column("Referent")); 60 Comparator<InstanceUtils.NativeAllocation> compare 61 = new Sort.WithPriority<InstanceUtils.NativeAllocation>( 62 new Sort.NativeAllocationByHeapName(), 63 new Sort.NativeAllocationBySize()); 64 Collections.sort(allocs, compare); 65 SubsetSelector<InstanceUtils.NativeAllocation> selector 66 = new SubsetSelector(query, ALLOCATIONS_ID, allocs); 67 for (InstanceUtils.NativeAllocation alloc : selector.selected()) { 68 doc.row( 69 DocString.format("%,14d", alloc.size), 70 DocString.text(alloc.heap.getName()), 71 DocString.format("0x%x", alloc.pointer), 72 Value.render(mSnapshot, alloc.referent)); 73 } 74 75 // Print a summary of the remaining entries if there are any. 76 List<InstanceUtils.NativeAllocation> remaining = selector.remaining(); 77 if (!remaining.isEmpty()) { 78 long total = 0; 79 for (InstanceUtils.NativeAllocation alloc : remaining) { 80 total += alloc.size; 81 } 82 83 doc.row( 84 DocString.format("%,14d", total), 85 DocString.text("..."), 86 DocString.text("..."), 87 DocString.text("...")); 88 } 89 90 doc.end(); 91 selector.render(doc); 92 } 93 } 94 } 95 96