1 /* 2 * Copyright (C) 2011 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.ddmuilib.heap; 18 19 import com.android.ddmlib.NativeAllocationInfo; 20 import com.android.ddmlib.NativeStackCallInfo; 21 22 import java.util.ArrayList; 23 import java.util.Collections; 24 import java.util.Comparator; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 29 /** 30 * A heap dump representation where each call site is associated with its source library. 31 */ 32 public final class NativeLibraryAllocationInfo { 33 /** Library name to use when grouping before symbol resolution is complete. */ 34 public static final String UNRESOLVED_LIBRARY_NAME = "Resolving.."; 35 36 /** Any call site that cannot be resolved to a specific library goes under this name. */ 37 private static final String UNKNOWN_LIBRARY_NAME = "unknown"; 38 39 private final String mLibraryName; 40 private final List<NativeAllocationInfo> mHeapAllocations; 41 private int mTotalSize; 42 43 private NativeLibraryAllocationInfo(String libraryName) { 44 mLibraryName = libraryName; 45 mHeapAllocations = new ArrayList<NativeAllocationInfo>(); 46 } 47 48 private void addAllocation(NativeAllocationInfo info) { 49 mHeapAllocations.add(info); 50 } 51 52 private void updateTotalSize() { 53 mTotalSize = 0; 54 for (NativeAllocationInfo i : mHeapAllocations) { 55 mTotalSize += i.getAllocationCount() * i.getSize(); 56 } 57 } 58 59 public String getLibraryName() { 60 return mLibraryName; 61 } 62 63 public long getTotalSize() { 64 return mTotalSize; 65 } 66 67 public List<NativeAllocationInfo> getAllocations() { 68 return mHeapAllocations; 69 } 70 71 /** 72 * Factory method to create a list of {@link NativeLibraryAllocationInfo} objects, 73 * given the list of {@link NativeAllocationInfo} objects. 74 * 75 * If the {@link NativeAllocationInfo} objects do not have their symbols resolved, 76 * then they are grouped under the library {@link #UNRESOLVED_LIBRARY_NAME}. If they do 77 * have their symbols resolved, but map to an unknown library, then they are grouped under 78 * the library {@link #UNKNOWN_LIBRARY_NAME}. 79 */ 80 public static List<NativeLibraryAllocationInfo> constructFrom( 81 List<NativeAllocationInfo> allocations) { 82 if (allocations == null) { 83 return null; 84 } 85 86 Map<String, NativeLibraryAllocationInfo> allocationsByLibrary = 87 new HashMap<String, NativeLibraryAllocationInfo>(); 88 89 // go through each native allocation and assign it to the appropriate library 90 for (NativeAllocationInfo info : allocations) { 91 String libName = UNRESOLVED_LIBRARY_NAME; 92 93 if (info.isStackCallResolved()) { 94 NativeStackCallInfo relevantStackCall = info.getRelevantStackCallInfo(); 95 if (relevantStackCall != null) { 96 libName = relevantStackCall.getLibraryName(); 97 } else { 98 libName = UNKNOWN_LIBRARY_NAME; 99 } 100 } 101 102 addtoLibrary(allocationsByLibrary, libName, info); 103 } 104 105 List<NativeLibraryAllocationInfo> libraryAllocations = 106 new ArrayList<NativeLibraryAllocationInfo>(allocationsByLibrary.values()); 107 108 // now update some summary statistics for each library 109 for (NativeLibraryAllocationInfo l : libraryAllocations) { 110 l.updateTotalSize(); 111 } 112 113 // finally, sort by total size 114 Collections.sort(libraryAllocations, new Comparator<NativeLibraryAllocationInfo>() { 115 public int compare(NativeLibraryAllocationInfo o1, 116 NativeLibraryAllocationInfo o2) { 117 return (int) (o2.getTotalSize() - o1.getTotalSize()); 118 } 119 }); 120 121 return libraryAllocations; 122 } 123 124 private static void addtoLibrary(Map<String, NativeLibraryAllocationInfo> libraryAllocations, 125 String libName, NativeAllocationInfo info) { 126 NativeLibraryAllocationInfo libAllocationInfo = libraryAllocations.get(libName); 127 if (libAllocationInfo == null) { 128 libAllocationInfo = new NativeLibraryAllocationInfo(libName); 129 libraryAllocations.put(libName, libAllocationInfo); 130 } 131 132 libAllocationInfo.addAllocation(info); 133 } 134 } 135