Home | History | Annotate | Download | only in src
      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.tools.perflib.heap.Heap;
     20 import java.io.IOException;
     21 import java.util.Collections;
     22 import java.util.Comparator;
     23 import java.util.List;
     24 
     25 class SiteHandler implements AhatHandler {
     26   private static final String ALLOCATION_SITE_ID = "frames";
     27   private static final String SITES_CALLED_ID = "called";
     28   private static final String OBJECTS_ALLOCATED_ID = "objects";
     29 
     30   private AhatSnapshot mSnapshot;
     31 
     32   public SiteHandler(AhatSnapshot snapshot) {
     33     mSnapshot = snapshot;
     34   }
     35 
     36   @Override
     37   public void handle(Doc doc, Query query) throws IOException {
     38     int stackId = query.getInt("stack", 0);
     39     int depth = query.getInt("depth", -1);
     40     Site site = mSnapshot.getSite(stackId, depth);
     41 
     42     doc.title("Site %s", site.getName());
     43     doc.section("Allocation Site");
     44     SitePrinter.printSite(mSnapshot, doc, query, ALLOCATION_SITE_ID, site);
     45 
     46     doc.section("Sites Called from Here");
     47     List<Site> children = site.getChildren();
     48     if (children.isEmpty()) {
     49       doc.println(DocString.text("(none)"));
     50     } else {
     51       Collections.sort(children, new Sort.SiteBySize("app"));
     52 
     53       HeapTable.TableConfig<Site> table = new HeapTable.TableConfig<Site>() {
     54         public String getHeapsDescription() {
     55           return "Reachable Bytes Allocated on Heap";
     56         }
     57 
     58         public long getSize(Site element, Heap heap) {
     59           return element.getSize(heap.getName());
     60         }
     61 
     62         public List<HeapTable.ValueConfig<Site>> getValueConfigs() {
     63           HeapTable.ValueConfig<Site> value = new HeapTable.ValueConfig<Site>() {
     64             public String getDescription() {
     65               return "Child Site";
     66             }
     67 
     68             public DocString render(Site element) {
     69               return DocString.link(
     70                   DocString.formattedUri("site?stack=%d&depth=%d",
     71                     element.getStackId(), element.getStackDepth()),
     72                   DocString.text(element.getName()));
     73             }
     74           };
     75           return Collections.singletonList(value);
     76         }
     77       };
     78       HeapTable.render(doc, query, SITES_CALLED_ID, table, mSnapshot, children);
     79     }
     80 
     81     doc.section("Objects Allocated");
     82     doc.table(
     83         new Column("Reachable Bytes Allocated", Column.Align.RIGHT),
     84         new Column("Instances", Column.Align.RIGHT),
     85         new Column("Heap"),
     86         new Column("Class"));
     87     List<Site.ObjectsInfo> infos = site.getObjectsInfos();
     88     Comparator<Site.ObjectsInfo> compare = new Sort.WithPriority<Site.ObjectsInfo>(
     89         new Sort.ObjectsInfoByHeapName(),
     90         new Sort.ObjectsInfoBySize(),
     91         new Sort.ObjectsInfoByClassName());
     92     Collections.sort(infos, compare);
     93     SubsetSelector<Site.ObjectsInfo> selector
     94       = new SubsetSelector(query, OBJECTS_ALLOCATED_ID, infos);
     95     for (Site.ObjectsInfo info : selector.selected()) {
     96       String className = AhatSnapshot.getClassName(info.classObj);
     97       doc.row(
     98           DocString.format("%,14d", info.numBytes),
     99           DocString.link(
    100             DocString.formattedUri("objects?stack=%d&depth=%d&heap=%s&class=%s",
    101                 site.getStackId(), site.getStackDepth(), info.heap.getName(), className),
    102             DocString.format("%,14d", info.numInstances)),
    103           DocString.text(info.heap.getName()),
    104           Value.render(mSnapshot, info.classObj));
    105     }
    106     doc.end();
    107     selector.render(doc);
    108   }
    109 }
    110 
    111