blob: 60bb38780a25a2d14277f6628eed3318dc520fc6 [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.util.ArrayList;
21import java.util.List;
22
23/**
24 * Class for rendering a table that includes sizes of some kind for each heap.
25 */
26class HeapTable {
27 /**
28 * Configuration for a value column of a heap table.
29 */
30 public static interface ValueConfig<T> {
31 public String getDescription();
32 public DocString render(T element);
33 }
34
35 /**
36 * Configuration for the HeapTable.
37 */
38 public static interface TableConfig<T> {
39 public String getHeapsDescription();
40 public long getSize(T element, Heap heap);
41 public List<ValueConfig<T>> getValueConfigs();
42 }
43
44 public static <T> void render(Doc doc, TableConfig<T> config,
45 AhatSnapshot snapshot, List<T> elements) {
46 // Only show the heaps that have non-zero entries.
47 List<Heap> heaps = new ArrayList<Heap>();
48 for (Heap heap : snapshot.getHeaps()) {
49 if (hasNonZeroEntry(snapshot, heap, config, elements)) {
50 heaps.add(heap);
51 }
52 }
53
54 List<ValueConfig<T>> values = config.getValueConfigs();
55
56 // Print the heap and values descriptions.
57 boolean showTotal = heaps.size() > 1;
58 List<Column> subcols = new ArrayList<Column>();
59 for (Heap heap : heaps) {
60 subcols.add(new Column(heap.getName(), Column.Align.RIGHT));
61 }
62 if (showTotal) {
63 subcols.add(new Column("Total", Column.Align.RIGHT));
64 }
65 List<Column> cols = new ArrayList<Column>();
66 for (ValueConfig value : values) {
67 cols.add(new Column(value.getDescription()));
68 }
69 doc.table(DocString.text(config.getHeapsDescription()), subcols, cols);
70
71 // Print the entries.
72 ArrayList<DocString> vals = new ArrayList<DocString>();
73 for (T elem : elements) {
74 vals.clear();
75 long total = 0;
76 for (Heap heap : heaps) {
77 long size = config.getSize(elem, heap);
78 total += size;
79 vals.add(DocString.text("%,14d", size));
80 }
81 if (showTotal) {
82 vals.add(DocString.text("%,14d", total));
83 }
84
85 for (ValueConfig<T> value : values) {
86 vals.add(value.render(elem));
87 }
88 doc.row(vals.toArray(new DocString[0]));
89 }
90 doc.end();
91 }
92
93 // Returns true if the given heap has a non-zero size entry.
94 public static <T> boolean hasNonZeroEntry(AhatSnapshot snapshot, Heap heap,
95 TableConfig<T> config, List<T> elements) {
96 if (snapshot.getHeapSize(heap) > 0) {
97 for (T element : elements) {
98 if (config.getSize(element, heap) > 0) {
99 return true;
100 }
101 }
102 }
103 return false;
104 }
105}
106