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 org.eclipse.jface.viewers.ITableLabelProvider; 23 import org.eclipse.jface.viewers.LabelProvider; 24 import org.eclipse.swt.graphics.Image; 25 26 /** 27 * A Label Provider for the Native Heap TreeViewer in {@link NativeHeapPanel}. 28 */ 29 public class NativeHeapLabelProvider extends LabelProvider implements ITableLabelProvider { 30 private long mTotalSize; 31 32 public Image getColumnImage(Object arg0, int arg1) { 33 return null; 34 } 35 36 public String getColumnText(Object element, int index) { 37 if (element instanceof NativeAllocationInfo) { 38 return getColumnTextForNativeAllocation((NativeAllocationInfo) element, index); 39 } 40 41 if (element instanceof NativeLibraryAllocationInfo) { 42 return getColumnTextForNativeLibrary((NativeLibraryAllocationInfo) element, index); 43 } 44 45 return null; 46 } 47 48 private String getColumnTextForNativeAllocation(NativeAllocationInfo info, int index) { 49 NativeStackCallInfo stackInfo = info.getRelevantStackCallInfo(); 50 51 switch (index) { 52 case 0: 53 return stackInfo == null ? stackResolutionStatus(info) : stackInfo.getLibraryName(); 54 case 1: 55 return Integer.toString(info.getSize() * info.getAllocationCount()); 56 case 2: 57 return getPercentageString(info.getSize() * info.getAllocationCount(), mTotalSize); 58 case 3: 59 String prefix = ""; 60 if (!info.isZygoteChild()) { 61 prefix = "Z "; 62 } 63 return prefix + Integer.toString(info.getAllocationCount()); 64 case 4: 65 return Integer.toString(info.getSize()); 66 case 5: 67 return stackInfo == null ? stackResolutionStatus(info) : stackInfo.getMethodName(); 68 default: 69 return null; 70 } 71 } 72 73 private String getColumnTextForNativeLibrary(NativeLibraryAllocationInfo info, int index) { 74 switch (index) { 75 case 0: 76 return info.getLibraryName(); 77 case 1: 78 return Long.toString(info.getTotalSize()); 79 case 2: 80 return getPercentageString(info.getTotalSize(), mTotalSize); 81 default: 82 return null; 83 } 84 } 85 86 private String getPercentageString(long size, long total) { 87 if (total == 0) { 88 return ""; 89 } 90 91 return String.format("%.1f%%", (float)(size * 100)/(float)total); 92 } 93 94 private String stackResolutionStatus(NativeAllocationInfo info) { 95 if (info.isStackCallResolved()) { 96 return "?"; // resolved and unknown 97 } else { 98 return "Resolving..."; // still resolving... 99 } 100 } 101 102 /** 103 * Set the total size of the heap dump for use in percentage calculations. 104 * This value should be set whenever the input to the tree changes so that the percentages 105 * are computed correctly. 106 */ 107 public void setTotalSize(long totalSize) { 108 mTotalSize = totalSize; 109 } 110 } 111