1 /*
2  * Copyright (C) 2017 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.ahat.heapdump.Size;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 
24 /**
25  * Class for rendering a table that includes all categories of Size.
26  * Two table formats are supported, one where a custom left column can be
27  * added before the size columns:
28  *    |left column|Java Size|Native Size|...|Total Size|custom columns...|
29  *
30  * The other without the custom left column:
31  *    |Java Size|Native Size|...|Total Size|custom columns...|
32  */
33 class SizeTable {
34   /**
35    * Start a size table with a custom left column.
36    *
37    * |left column|Java Size|Native Size|...|Total Size|custom columns...|
38    *
39    * This should be followed by calls to the 'row' method to fill in the table
40    * contents and the 'end' method to end the table.
41    *
42    * Set showDiff to true if size diffs should be shown.
43    */
table(Doc doc, Column left, boolean showDiff, Column... columns)44   static void table(Doc doc, Column left, boolean showDiff, Column... columns) {
45     List<Column> cols = new ArrayList<Column>();
46     cols.add(left);
47     cols.add(new Column("Java Size", Column.Align.RIGHT));
48     cols.add(new Column("Δ", Column.Align.RIGHT, showDiff));
49     cols.add(new Column("Registered Native Size", Column.Align.RIGHT));
50     cols.add(new Column("Δ", Column.Align.RIGHT, showDiff));
51     cols.add(new Column("Total Size", Column.Align.RIGHT));
52     cols.add(new Column("Δ", Column.Align.RIGHT, showDiff));
53     cols.addAll(Arrays.asList(columns));
54     doc.table(cols.toArray(new Column[cols.size()]));
55   }
56 
57   /**
58    * Add a row to the currently active size table with custom left column.
59    * The number of values must match the number of columns provided for the
60    * currently active table.
61    */
row(Doc doc, DocString left, Size size, Size base, DocString... values)62   static void row(Doc doc, DocString left, Size size, Size base, DocString... values) {
63     List<DocString> vals = new ArrayList<DocString>();
64     vals.add(left);
65     vals.add(DocString.size(size.getJavaSize(), false));
66     vals.add(DocString.delta(false, false, size.getJavaSize(), base.getJavaSize()));
67     vals.add(DocString.size(size.getRegisteredNativeSize(), false));
68     vals.add(DocString.delta(false, false,
69           size.getRegisteredNativeSize(), base.getRegisteredNativeSize()));
70     vals.add(DocString.size(size.getSize(), false));
71     vals.add(DocString.delta(false, false, size.getSize(), base.getSize()));
72     vals.addAll(Arrays.asList(values));
73     doc.row(vals.toArray(new DocString[vals.size()]));
74   }
75 
76   /**
77    * Start a size table without a custom left column.
78    *
79    * |Java Size|Native Size|...|Total Size|custom columns...|
80    * This should be followed by calls to the 'row' method to fill in the table
81    * contents and the 'end' method to end the table.
82    *
83    * Set showDiff to true if size diffs should be shown.
84    */
table(Doc doc, boolean showDiff, Column... columns)85   static void table(Doc doc, boolean showDiff, Column... columns) {
86     // Re-use the code for a size table with custom left column by having an
87     // invisible custom left column.
88     table(doc, new Column("", Column.Align.LEFT, false), showDiff, columns);
89   }
90 
91   /**
92    * Add a row to the currently active size table without a custom left column.
93    * The number of values must match the number of columns provided for the
94    * currently active table.
95    */
row(Doc doc, Size size, Size base, DocString... values)96   static void row(Doc doc, Size size, Size base, DocString... values) {
97     row(doc, new DocString(), size, base, values);
98   }
99 
100   /**
101    * End the currently active table.
102    */
end(Doc doc)103   static void end(Doc doc) {
104     doc.end();
105   }
106 }
107