blob: 53d1073ed187996e91a9fe9a322de813775b44ab [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 com.android.tools.perflib.heap.Instance;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27
28/**
29 * Class for rendering a list of instances dominated by a single instance in a
30 * pretty way.
31 */
32class DominatedList {
33 private static final int kIncrAmount = 100;
34 private static final int kDefaultShown = 100;
35
36 /**
37 * Render a table to the given HtmlWriter showing a pretty list of
38 * instances.
39 *
40 * Rather than show all of the instances (which may be very many), we use
41 * the query parameter "dominated" to specify a limited number of
42 * instances to show. The 'uri' parameter should be the current page URI, so
43 * that we can add links to "show more" and "show less" objects that go to
44 * the same page with only the number of objects adjusted.
45 */
46 public static void render(final AhatSnapshot snapshot, Doc doc,
47 Collection<Instance> instances, Query query) {
48 List<Instance> insts = new ArrayList<Instance>(instances);
49 Collections.sort(insts, Sort.defaultInstanceCompare(snapshot));
50
51 int numInstancesToShow = getNumInstancesToShow(query, insts.size());
52 List<Instance> shown = new ArrayList<Instance>(insts.subList(0, numInstancesToShow));
53 List<Instance> hidden = insts.subList(numInstancesToShow, insts.size());
54
55 // Add 'null' as a marker for "all the rest of the objects".
56 if (!hidden.isEmpty()) {
57 shown.add(null);
58 }
59 HeapTable.render(doc, new TableConfig(snapshot, hidden), snapshot, shown);
60
61 if (insts.size() > kDefaultShown) {
62 printMenu(doc, query, numInstancesToShow, insts.size());
63 }
64 }
65
66 private static class TableConfig implements HeapTable.TableConfig<Instance> {
67 AhatSnapshot mSnapshot;
68
69 // Map from heap name to the total size of the instances not shown in the
70 // table.
71 Map<Heap, Long> mHiddenSizes;
72
73 public TableConfig(AhatSnapshot snapshot, List<Instance> hidden) {
74 mSnapshot = snapshot;
75 mHiddenSizes = new HashMap<Heap, Long>();
76 for (Heap heap : snapshot.getHeaps()) {
77 mHiddenSizes.put(heap, 0L);
78 }
79
80 if (!hidden.isEmpty()) {
81 for (Instance inst : hidden) {
82 for (Heap heap : snapshot.getHeaps()) {
83 int index = snapshot.getHeapIndex(heap);
84 long size = inst.getRetainedSize(index);
85 mHiddenSizes.put(heap, mHiddenSizes.get(heap) + size);
86 }
87 }
88 }
89 }
90
91 @Override
92 public String getHeapsDescription() {
93 return "Bytes Retained by Heap";
94 }
95
96 @Override
97 public long getSize(Instance element, Heap heap) {
98 if (element == null) {
99 return mHiddenSizes.get(heap);
100 }
101 int index = mSnapshot.getHeapIndex(heap);
102 return element.getRetainedSize(index);
103 }
104
105 @Override
106 public List<HeapTable.ValueConfig<Instance>> getValueConfigs() {
107 HeapTable.ValueConfig<Instance> value = new HeapTable.ValueConfig<Instance>() {
108 public String getDescription() {
109 return "Object";
110 }
111
112 public DocString render(Instance element) {
113 if (element == null) {
114 return DocString.text("...");
115 } else {
116 return Value.render(element);
117 }
118 }
119 };
120 return Collections.singletonList(value);
121 }
122 }
123
124 // Figure out how many objects to show based on the query parameter.
125 // The resulting value is guaranteed to be at least zero, and no greater
126 // than the number of total objects.
127 private static int getNumInstancesToShow(Query query, int totalNumInstances) {
128 String value = query.get("dominated", null);
129 try {
130 int count = Math.min(totalNumInstances, Integer.parseInt(value));
131 return Math.max(0, count);
132 } catch (NumberFormatException e) {
133 // We can't parse the value as a number. Ignore it.
134 }
135 return Math.min(kDefaultShown, totalNumInstances);
136 }
137
138 // Print a menu line after the table to control how many objects are shown.
139 // It has the form:
140 // (showing X of Y objects - show none - show less - show more - show all)
141 private static void printMenu(Doc doc, Query query, int shown, int all) {
142 DocString menu = new DocString();
143 menu.append("(%d of %d objects shown - ", shown, all);
144 if (shown > 0) {
145 int less = Math.max(0, shown - kIncrAmount);
146 menu.appendLink(query.with("dominated", 0), DocString.text("show none"));
147 menu.append(" - ");
148 menu.appendLink(query.with("dominated", less), DocString.text("show less"));
149 menu.append(" - ");
150 } else {
151 menu.append("show none - show less - ");
152 }
153 if (shown < all) {
154 int more = Math.min(shown + kIncrAmount, all);
155 menu.appendLink(query.with("dominated", more), DocString.text("show more"));
156 menu.append(" - ");
157 menu.appendLink(query.with("dominated", all), DocString.text("show all"));
158 menu.append(")");
159 } else {
160 menu.append("show more - show all)");
161 }
162 doc.println(menu);
163 }
164}
165