blob: 839e220ca4e4f9996aec3ece43b374342a4553b1 [file] [log] [blame]
Richard Uhlerb730b782015-07-15 16:01:58 -07001/*
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
17package com.android.ahat;
18
19import com.android.tools.perflib.heap.Heap;
20import java.io.IOException;
21import java.util.Collections;
22import java.util.Comparator;
23import java.util.List;
24
Richard Uhler1af86f12015-10-29 14:55:00 -070025class 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 Uhlerb730b782015-07-15 16:01:58 -070032 public SiteHandler(AhatSnapshot snapshot) {
Richard Uhler1af86f12015-10-29 14:55:00 -070033 mSnapshot = snapshot;
Richard Uhlerb730b782015-07-15 16:01:58 -070034 }
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 Uhler1af86f12015-10-29 14:55:00 -070044 SitePrinter.printSite(mSnapshot, doc, query, ALLOCATION_SITE_ID, site);
Richard Uhlerb730b782015-07-15 16:01:58 -070045
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 Uhlerc21e4e62015-08-31 16:16:14 -070070 DocString.formattedUri("site?stack=%d&depth=%d",
Richard Uhlerb730b782015-07-15 16:01:58 -070071 element.getStackId(), element.getStackDepth()),
72 DocString.text(element.getName()));
73 }
74 };
75 return Collections.singletonList(value);
76 }
77 };
Richard Uhler1af86f12015-10-29 14:55:00 -070078 HeapTable.render(doc, query, SITES_CALLED_ID, table, mSnapshot, children);
Richard Uhlerb730b782015-07-15 16:01:58 -070079 }
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 Uhler1af86f12015-10-29 14:55:00 -070093 SubsetSelector<Site.ObjectsInfo> selector
94 = new SubsetSelector(query, OBJECTS_ALLOCATED_ID, infos);
95 for (Site.ObjectsInfo info : selector.selected()) {
Richard Uhlerb730b782015-07-15 16:01:58 -070096 String className = AhatSnapshot.getClassName(info.classObj);
97 doc.row(
Richard Uhlerc21e4e62015-08-31 16:16:14 -070098 DocString.format("%,14d", info.numBytes),
Richard Uhlerb730b782015-07-15 16:01:58 -070099 DocString.link(
Richard Uhlerc21e4e62015-08-31 16:16:14 -0700100 DocString.formattedUri("objects?stack=%d&depth=%d&heap=%s&class=%s",
Richard Uhlerb730b782015-07-15 16:01:58 -0700101 site.getStackId(), site.getStackDepth(), info.heap.getName(), className),
Richard Uhlerc21e4e62015-08-31 16:16:14 -0700102 DocString.format("%,14d", info.numInstances)),
Richard Uhlerb730b782015-07-15 16:01:58 -0700103 DocString.text(info.heap.getName()),
Richard Uhler38f9eba2015-11-11 08:31:52 -0800104 Value.render(mSnapshot, info.classObj));
Richard Uhlerb730b782015-07-15 16:01:58 -0700105 }
106 doc.end();
Richard Uhler1af86f12015-10-29 14:55:00 -0700107 selector.render(doc);
Richard Uhlerb730b782015-07-15 16:01:58 -0700108 }
109}
110