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.Site;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 
26 class SitePrinter {
printSite(AhatSnapshot snapshot, Doc doc, Query query, String id, Site site)27   public static void printSite(AhatSnapshot snapshot, Doc doc, Query query, String id, Site site) {
28     List<Site> path = new ArrayList<Site>();
29     for (Site parent = site; parent != null; parent = parent.getParent()) {
30       path.add(parent);
31     }
32     Collections.reverse(path);
33 
34 
35     HeapTable.TableConfig<Site> table = new HeapTable.TableConfig<Site>() {
36       public String getHeapsDescription() {
37         return "Reachable Bytes Allocated on Heap";
38       }
39 
40       public long getSize(Site element, AhatHeap heap) {
41         return element.getSize(heap).getSize();
42       }
43 
44       public List<HeapTable.ValueConfig<Site>> getValueConfigs() {
45         HeapTable.ValueConfig<Site> value = new HeapTable.ValueConfig<Site>() {
46           public String getDescription() {
47             return "Stack Frame";
48           }
49 
50           public DocString render(Site element) {
51             DocString str = new DocString();
52             if (element.getParent() != null) {
53               str.append("→ ");
54             }
55             return str.append(Summarizer.summarize(element));
56           }
57         };
58         return Collections.singletonList(value);
59       }
60     };
61     HeapTable.render(doc, query, id, table, snapshot, path);
62   }
63 }
64