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