1 /*
2  * Copyright (C) 2016 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.heapdump;
18 
19 /**
20  * Used to identify and access basic information about a particular
21  * heap from the heap dump. Standard Java heap dumps have a single heap,
22  * called the "default" heap. Android heap dumps distinguish among "zygote",
23  * "image", and "app" heaps. There will be a single instance of AhatHeap for
24  * each different heap in the heap dump.
25  */
26 public class AhatHeap implements Diffable<AhatHeap> {
27   private String mName;
28   private Size mSize = Size.ZERO;
29   private int mIndex;
30   private AhatHeap mBaseline;
31   private boolean mIsPlaceHolder = false;
32 
AhatHeap(String name, int index)33   AhatHeap(String name, int index) {
34     mName = name;
35     mIndex = index;
36     mBaseline = this;
37   }
38 
39   /**
40    * Construct a placeholder heap.
41    */
AhatHeap(String name, AhatHeap baseline)42   private AhatHeap(String name, AhatHeap baseline) {
43     mName = name;
44     mIndex = -1;
45     mBaseline = baseline;
46     baseline.setBaseline(this);
47     mIsPlaceHolder = true;
48   }
49 
50   /**
51    * Construct a new placeholder heap that has the given baseline heap.
52    */
newPlaceHolderHeap(String name, AhatHeap baseline)53   static AhatHeap newPlaceHolderHeap(String name, AhatHeap baseline) {
54     return new AhatHeap(name, baseline);
55   }
56 
addToSize(Size size)57   void addToSize(Size size) {
58     mSize = mSize.plus(size);
59   }
60 
61   /**
62    * Returns a unique instance for this heap between 0 and the total number of
63    * heaps in this snapshot, or -1 if this is a placeholder heap.
64    */
getIndex()65   int getIndex() {
66     return mIndex;
67   }
68 
69   /**
70    * Returns the name of this heap.
71    * For example, "default", "app", "image", or "zygote".
72    *
73    * @return The name of the heap.
74    */
getName()75   public String getName() {
76     return mName;
77   }
78 
79   /**
80    * Returns the total number of bytes allocated on this heap.
81    *
82    * @return the total number of bytes allocated on this heap.
83    */
getSize()84   public Size getSize() {
85     return mSize;
86   }
87 
setBaseline(AhatHeap baseline)88   void setBaseline(AhatHeap baseline) {
89     mBaseline = baseline;
90   }
91 
92   @Override
getBaseline()93   public AhatHeap getBaseline() {
94     return mBaseline;
95   }
96 
97   @Override
isPlaceHolder()98   public boolean isPlaceHolder() {
99     return mIsPlaceHolder;
100   }
101 }
102