Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 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 | |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 17 | package com.android.ahat.heapdump; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 18 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.Arrays; |
| 21 | import java.util.Comparator; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 22 | import java.util.Iterator; |
Gus Smith | e6a6387 | 2016-06-09 16:50:44 -0700 | [diff] [blame] | 23 | import java.util.List; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Provides Comparators and helper functions for sorting Instances, Sites, and |
| 27 | * other things. |
| 28 | * |
| 29 | * Note: The Comparators defined here impose orderings that are inconsistent |
| 30 | * with equals. They should not be used for element lookup or search. They |
| 31 | * should only be used for showing elements to the user in different orders. |
| 32 | */ |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 33 | public class Sort { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Compare instances by their total retained size. |
| 36 | * Different instances with the same total retained size are considered |
| 37 | * equal for the purposes of comparison. |
| 38 | * This sorts instances from larger retained size to smaller retained size. |
| 39 | */ |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 40 | public static final Comparator<AhatInstance> INSTANCE_BY_TOTAL_RETAINED_SIZE |
| 41 | = new Comparator<AhatInstance>() { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 42 | @Override |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 43 | public int compare(AhatInstance a, AhatInstance b) { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 44 | return Long.compare(b.getTotalRetainedSize(), a.getTotalRetainedSize()); |
| 45 | } |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 46 | }; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Compare instances by their retained size for a given heap index. |
| 50 | * Different instances with the same total retained size are considered |
| 51 | * equal for the purposes of comparison. |
| 52 | * This sorts instances from larger retained size to smaller retained size. |
| 53 | */ |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 54 | public static class InstanceByHeapRetainedSize implements Comparator<AhatInstance> { |
| 55 | private AhatHeap mHeap; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 56 | |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 57 | public InstanceByHeapRetainedSize(AhatHeap heap) { |
| 58 | mHeap = heap; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 62 | public int compare(AhatInstance a, AhatInstance b) { |
| 63 | return Long.compare(b.getRetainedSize(mHeap), a.getRetainedSize(mHeap)); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Compare objects based on a list of comparators, giving priority to the |
| 69 | * earlier comparators in the list. |
| 70 | */ |
| 71 | public static class WithPriority<T> implements Comparator<T> { |
| 72 | private List<Comparator<T>> mComparators; |
| 73 | |
| 74 | public WithPriority(Comparator<T>... comparators) { |
| 75 | mComparators = Arrays.asList(comparators); |
| 76 | } |
| 77 | |
| 78 | public WithPriority(List<Comparator<T>> comparators) { |
| 79 | mComparators = comparators; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int compare(T a, T b) { |
| 84 | int res = 0; |
| 85 | Iterator<Comparator<T>> iter = mComparators.iterator(); |
| 86 | while (res == 0 && iter.hasNext()) { |
| 87 | res = iter.next().compare(a, b); |
| 88 | } |
| 89 | return res; |
| 90 | } |
| 91 | } |
| 92 | |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 93 | public static Comparator<AhatInstance> defaultInstanceCompare(AhatSnapshot snapshot) { |
| 94 | List<Comparator<AhatInstance>> comparators = new ArrayList<Comparator<AhatInstance>>(); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 95 | |
| 96 | // Priority goes to the app heap, if we can find one. |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 97 | AhatHeap appHeap = snapshot.getHeap("app"); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 98 | if (appHeap != null) { |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 99 | comparators.add(new InstanceByHeapRetainedSize(appHeap)); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Next is by total retained size. |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 103 | comparators.add(INSTANCE_BY_TOTAL_RETAINED_SIZE); |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 104 | return new WithPriority<AhatInstance>(comparators); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Compare Sites by the size of objects allocated on a given heap. |
| 109 | * Different object infos with the same size on the given heap are |
| 110 | * considered equal for the purposes of comparison. |
| 111 | * This sorts sites from larger size to smaller size. |
| 112 | */ |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 113 | public static class SiteByHeapSize implements Comparator<Site> { |
| 114 | AhatHeap mHeap; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 115 | |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 116 | public SiteByHeapSize(AhatHeap heap) { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 117 | mHeap = heap; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public int compare(Site a, Site b) { |
| 122 | return Long.compare(b.getSize(mHeap), a.getSize(mHeap)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 127 | * Compare Sites by the total size of objects allocated. |
| 128 | * This sorts sites from larger size to smaller size. |
| 129 | */ |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 130 | public static final Comparator<Site> SITE_BY_TOTAL_SIZE = new Comparator<Site>() { |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 131 | @Override |
| 132 | public int compare(Site a, Site b) { |
| 133 | return Long.compare(b.getTotalSize(), a.getTotalSize()); |
| 134 | } |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 135 | }; |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 136 | |
| 137 | public static Comparator<Site> defaultSiteCompare(AhatSnapshot snapshot) { |
| 138 | List<Comparator<Site>> comparators = new ArrayList<Comparator<Site>>(); |
| 139 | |
| 140 | // Priority goes to the app heap, if we can find one. |
| 141 | AhatHeap appHeap = snapshot.getHeap("app"); |
| 142 | if (appHeap != null) { |
| 143 | comparators.add(new SiteByHeapSize(appHeap)); |
| 144 | } |
| 145 | |
| 146 | // Next is by total size. |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 147 | comparators.add(SITE_BY_TOTAL_SIZE); |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 148 | return new WithPriority<Site>(comparators); |
| 149 | } |
| 150 | |
| 151 | /** |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 152 | * Compare Site.ObjectsInfo by their size. |
| 153 | * Different object infos with the same total retained size are considered |
| 154 | * equal for the purposes of comparison. |
| 155 | * This sorts object infos from larger retained size to smaller size. |
| 156 | */ |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 157 | public static final Comparator<Site.ObjectsInfo> OBJECTS_INFO_BY_SIZE |
| 158 | = new Comparator<Site.ObjectsInfo>() { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public int compare(Site.ObjectsInfo a, Site.ObjectsInfo b) { |
| 161 | return Long.compare(b.numBytes, a.numBytes); |
| 162 | } |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 163 | }; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Compare Site.ObjectsInfo by heap name. |
| 167 | * Different object infos with the same heap name are considered equal for |
| 168 | * the purposes of comparison. |
| 169 | */ |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 170 | public static final Comparator<Site.ObjectsInfo> OBJECTS_INFO_BY_HEAP_NAME |
| 171 | = new Comparator<Site.ObjectsInfo>() { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 172 | @Override |
| 173 | public int compare(Site.ObjectsInfo a, Site.ObjectsInfo b) { |
| 174 | return a.heap.getName().compareTo(b.heap.getName()); |
| 175 | } |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 176 | }; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Compare Site.ObjectsInfo by class name. |
| 180 | * Different object infos with the same class name are considered equal for |
| 181 | * the purposes of comparison. |
| 182 | */ |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 183 | public static final Comparator<Site.ObjectsInfo> OBJECTS_INFO_BY_CLASS_NAME |
| 184 | = new Comparator<Site.ObjectsInfo>() { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public int compare(Site.ObjectsInfo a, Site.ObjectsInfo b) { |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 187 | String aName = a.getClassName(); |
| 188 | String bName = b.getClassName(); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 189 | return aName.compareTo(bName); |
| 190 | } |
Richard Uhler | f629cfd | 2016-12-12 13:11:26 +0000 | [diff] [blame] | 191 | }; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 192 | } |
| 193 | |