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.ahat.heapdump.AhatHeap;
     20 import com.android.ahat.heapdump.AhatInstance;
     21 import com.android.ahat.heapdump.AhatSnapshot;
     22 import com.android.ahat.heapdump.Sort;
     23 import java.util.ArrayList;
     24 import java.util.Collection;
     25 import java.util.Collections;
     26 import java.util.List;
     27 
     28 /**
     29  * Class for rendering a list of instances dominated by a single instance in a
     30  * pretty way.
     31  */
     32 class DominatedList {
     33   /**
     34    * Render a table to the given HtmlWriter showing a pretty list of
     35    * instances.
     36    *
     37    * @param snapshot  the snapshot where the instances reside
     38    * @param doc       the document to render the dominated list to
     39    * @param query     the current page query
     40    * @param id        a unique identifier to use for the dominated list in the current page
     41    * @param instances the collection of instances to generate a list for
     42    */
     43   public static void render(final AhatSnapshot snapshot,
     44       Doc doc, Query query, String id, Collection<AhatInstance> instances) {
     45     List<AhatInstance> insts = new ArrayList<AhatInstance>(instances);
     46     Collections.sort(insts, Sort.defaultInstanceCompare(snapshot));
     47     HeapTable.render(doc, query, id, new TableConfig(), snapshot, insts);
     48   }
     49 
     50   private static class TableConfig implements HeapTable.TableConfig<AhatInstance> {
     51     @Override
     52     public String getHeapsDescription() {
     53       return "Bytes Retained by Heap";
     54     }
     55 
     56     @Override
     57     public long getSize(AhatInstance element, AhatHeap heap) {
     58       return element.getRetainedSize(heap).getSize();
     59     }
     60 
     61     @Override
     62     public List<HeapTable.ValueConfig<AhatInstance>> getValueConfigs() {
     63       HeapTable.ValueConfig<AhatInstance> value = new HeapTable.ValueConfig<AhatInstance>() {
     64         public String getDescription() {
     65           return "Object";
     66         }
     67 
     68         public DocString render(AhatInstance element) {
     69           return Summarizer.summarize(element);
     70         }
     71       };
     72       return Collections.singletonList(value);
     73     }
     74   }
     75 }
     76