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.ahat.heapdump.AhatHeap;
20 import com.android.ahat.heapdump.AhatSnapshot;
21 import com.android.ahat.heapdump.Reachability;
22 import com.android.ahat.heapdump.Size;
23 import java.io.File;
24 import java.io.IOException;
25 
26 class OverviewHandler implements AhatHandler {
27 
28   private AhatSnapshot mSnapshot;
29   private File mHprof;
30   private File mBaseHprof;
31   private Reachability mRetained;
32 
OverviewHandler(AhatSnapshot snapshot, File hprof, File basehprof, Reachability retained)33   public OverviewHandler(AhatSnapshot snapshot, File hprof, File basehprof, Reachability retained) {
34     mSnapshot = snapshot;
35     mHprof = hprof;
36     mBaseHprof = basehprof;
37     mRetained = retained;
38   }
39 
40   @Override
handle(Doc doc, Query query)41   public void handle(Doc doc, Query query) throws IOException {
42     doc.title("Overview");
43 
44     doc.section("General Information");
45     doc.descriptions();
46     doc.description(
47         DocString.text("ahat version"),
48         DocString.format("ahat-%s", OverviewHandler.class.getPackage().getImplementationVersion()));
49     doc.description(
50         DocString.text("--retained"),
51         DocString.text(mRetained.toString()));
52     doc.description(DocString.text("hprof file"), DocString.text(mHprof.toString()));
53     if (mBaseHprof != null) {
54       doc.description(DocString.text("baseline hprof file"), DocString.text(mBaseHprof.toString()));
55     }
56     doc.end();
57 
58     doc.section("Bytes Retained by Heap");
59     printHeapSizes(doc);
60 
61     doc.big(Menu.getMenu());
62   }
63 
printHeapSizes(Doc doc)64   private void printHeapSizes(Doc doc) {
65     SizeTable.table(doc, new Column("Heap"), mSnapshot.isDiffed());
66     Size totalSize = Size.ZERO;
67     Size totalBase = Size.ZERO;
68     for (AhatHeap heap : mSnapshot.getHeaps()) {
69       Size size = heap.getSize();
70       Size base = heap.getBaseline().getSize();
71       if (!size.isZero() || !base.isZero()) {
72         SizeTable.row(doc, DocString.text(heap.getName()), size, base);
73         totalSize = totalSize.plus(size);
74         totalBase = totalBase.plus(base);
75       }
76     }
77     SizeTable.row(doc, DocString.text("Total"), totalSize, totalBase);
78     SizeTable.end(doc);
79   }
80 }
81 
82