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 | |
| 17 | package com.android.ahat; |
| 18 | |
| 19 | import com.android.tools.perflib.heap.Heap; |
| 20 | import java.io.IOException; |
| 21 | import java.util.Collections; |
| 22 | import java.util.Comparator; |
| 23 | import java.util.List; |
| 24 | |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 25 | class SiteHandler implements AhatHandler { |
| 26 | private static final String ALLOCATION_SITE_ID = "frames"; |
| 27 | private static final String SITES_CALLED_ID = "called"; |
| 28 | private static final String OBJECTS_ALLOCATED_ID = "objects"; |
| 29 | |
| 30 | private AhatSnapshot mSnapshot; |
| 31 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 32 | public SiteHandler(AhatSnapshot snapshot) { |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 33 | mSnapshot = snapshot; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public void handle(Doc doc, Query query) throws IOException { |
| 38 | int stackId = query.getInt("stack", 0); |
| 39 | int depth = query.getInt("depth", -1); |
| 40 | Site site = mSnapshot.getSite(stackId, depth); |
| 41 | |
| 42 | doc.title("Site %s", site.getName()); |
| 43 | doc.section("Allocation Site"); |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 44 | SitePrinter.printSite(mSnapshot, doc, query, ALLOCATION_SITE_ID, site); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 45 | |
| 46 | doc.section("Sites Called from Here"); |
| 47 | List<Site> children = site.getChildren(); |
| 48 | if (children.isEmpty()) { |
| 49 | doc.println(DocString.text("(none)")); |
| 50 | } else { |
| 51 | Collections.sort(children, new Sort.SiteBySize("app")); |
| 52 | |
| 53 | HeapTable.TableConfig<Site> table = new HeapTable.TableConfig<Site>() { |
| 54 | public String getHeapsDescription() { |
| 55 | return "Reachable Bytes Allocated on Heap"; |
| 56 | } |
| 57 | |
| 58 | public long getSize(Site element, Heap heap) { |
| 59 | return element.getSize(heap.getName()); |
| 60 | } |
| 61 | |
| 62 | public List<HeapTable.ValueConfig<Site>> getValueConfigs() { |
| 63 | HeapTable.ValueConfig<Site> value = new HeapTable.ValueConfig<Site>() { |
| 64 | public String getDescription() { |
| 65 | return "Child Site"; |
| 66 | } |
| 67 | |
| 68 | public DocString render(Site element) { |
| 69 | return DocString.link( |
Richard Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 70 | DocString.formattedUri("site?stack=%d&depth=%d", |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 71 | element.getStackId(), element.getStackDepth()), |
| 72 | DocString.text(element.getName())); |
| 73 | } |
| 74 | }; |
| 75 | return Collections.singletonList(value); |
| 76 | } |
| 77 | }; |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 78 | HeapTable.render(doc, query, SITES_CALLED_ID, table, mSnapshot, children); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | doc.section("Objects Allocated"); |
| 82 | doc.table( |
| 83 | new Column("Reachable Bytes Allocated", Column.Align.RIGHT), |
| 84 | new Column("Instances", Column.Align.RIGHT), |
| 85 | new Column("Heap"), |
| 86 | new Column("Class")); |
| 87 | List<Site.ObjectsInfo> infos = site.getObjectsInfos(); |
| 88 | Comparator<Site.ObjectsInfo> compare = new Sort.WithPriority<Site.ObjectsInfo>( |
| 89 | new Sort.ObjectsInfoByHeapName(), |
| 90 | new Sort.ObjectsInfoBySize(), |
| 91 | new Sort.ObjectsInfoByClassName()); |
| 92 | Collections.sort(infos, compare); |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 93 | SubsetSelector<Site.ObjectsInfo> selector |
| 94 | = new SubsetSelector(query, OBJECTS_ALLOCATED_ID, infos); |
| 95 | for (Site.ObjectsInfo info : selector.selected()) { |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 96 | String className = AhatSnapshot.getClassName(info.classObj); |
| 97 | doc.row( |
Richard Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 98 | DocString.format("%,14d", info.numBytes), |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 99 | DocString.link( |
Richard Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 100 | DocString.formattedUri("objects?stack=%d&depth=%d&heap=%s&class=%s", |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 101 | site.getStackId(), site.getStackDepth(), info.heap.getName(), className), |
Richard Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 102 | DocString.format("%,14d", info.numInstances)), |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 103 | DocString.text(info.heap.getName()), |
Richard Uhler | 38f9eba | 2015-11-11 08:31:52 -0800 | [diff] [blame] | 104 | Value.render(mSnapshot, info.classObj)); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 105 | } |
| 106 | doc.end(); |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 107 | selector.render(doc); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |