HeapSource.h revision cc6f5118d19bae06cb80841386a0c95f24616a65
1/*
2 * Copyright (C) 2008 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#ifndef _DALVIK_HEAP_SOURCE
17#define _DALVIK_HEAP_SOURCE
18
19#include "alloc/Heap.h"
20#include "alloc/HeapInternal.h" // for GcHeap
21
22/* dlmalloc uses one size_t per allocated chunk.
23 */
24#define HEAP_SOURCE_CHUNK_OVERHEAD         (1 * sizeof (size_t))
25#define HEAP_SOURCE_WORST_CHUNK_OVERHEAD   (32 * sizeof (size_t))
26
27/* The largest number of separate heaps we can handle.
28 */
29#define HEAP_SOURCE_MAX_HEAP_COUNT 2
30
31/*
32 * Initializes the heap source; must be called before any other
33 * dvmHeapSource*() functions.
34 */
35GcHeap *dvmHeapSourceStartup(size_t startingSize,
36                             size_t maximumSize,
37                             size_t growthLimit);
38
39/*
40 * If the HeapSource was created while in zygote mode, this
41 * will create a new heap for post-zygote allocations.
42 * Having a separate heap should maximize the number of pages
43 * that a given app_process shares with the zygote process.
44 */
45bool dvmHeapSourceStartupAfterZygote(void);
46
47/*
48 * If the HeapSource was created while in zygote mode, this
49 * will create an additional zygote heap before the first fork().
50 * Having a separate heap should reduce the number of shared
51 * pages subsequently touched by the zygote process.
52 */
53bool dvmHeapSourceStartupBeforeFork(void);
54
55/*
56 * Shutdown any threads internal to the heap source.  This should be
57 * called before the heap source itself is shutdown.
58 */
59void dvmHeapSourceThreadShutdown(void);
60
61/*
62 * Tears down the heap source and frees any resources associated with it.
63 */
64void dvmHeapSourceShutdown(GcHeap **gcHeap);
65
66/*
67 * Initializes a vector of object and mark bits to the object and mark
68 * bits of each heap.
69 */
70void dvmHeapSourceGetObjectBitmaps(HeapBitmap liveBits[], HeapBitmap markBits[],
71                                   size_t numHeaps);
72
73/*
74 * Get the bitmap representing all live objects.
75 */
76HeapBitmap *dvmHeapSourceGetLiveBits(void);
77
78/*
79 * Gets the begining of the allocation for the HeapSource.
80 */
81void *dvmHeapSourceGetBase(void);
82
83/*
84 * Returns the requested value. If the per-heap stats are requested, fill
85 * them as well.
86 */
87enum HeapSourceValueSpec {
88    HS_FOOTPRINT,
89    HS_ALLOWED_FOOTPRINT,
90    HS_BYTES_ALLOCATED,
91    HS_OBJECTS_ALLOCATED
92};
93size_t dvmHeapSourceGetValue(enum HeapSourceValueSpec spec,
94                             size_t perHeapStats[], size_t arrayLen);
95
96/*
97 * Allocates <n> bytes of zeroed data.
98 */
99void *dvmHeapSourceAlloc(size_t n);
100
101/*
102 * Allocates <n> bytes of zeroed data, growing up to absoluteMaxSize
103 * if necessary.
104 */
105void *dvmHeapSourceAllocAndGrow(size_t n);
106
107/*
108 * Frees the first numPtrs objects in the ptrs list and returns the
109 * amount of reclaimed storage.  The list must contain addresses all
110 * in the same mspace, and must be in increasing order. This implies
111 * that there are no duplicates, and no entries are NULL.
112 */
113size_t dvmHeapSourceFreeList(size_t numPtrs, void **ptrs);
114
115/*
116 * Returns true iff <ptr> was allocated from the heap source.
117 */
118bool dvmHeapSourceContains(const void *ptr);
119
120/*
121 * Returns true iff <ptr> is within the address space managed by heap source.
122 */
123bool dvmHeapSourceContainsAddress(const void *ptr);
124
125/*
126 * Returns the value of the requested flag.
127 */
128enum HeapSourcePtrFlag {
129    HS_CONTAINS,    // identical to dvmHeapSourceContains()
130    HS_ALLOCATED_IN_ZYGOTE
131};
132bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag);
133
134/*
135 * Returns the number of usable bytes in an allocated chunk; the size
136 * may be larger than the size passed to dvmHeapSourceAlloc().
137 */
138size_t dvmHeapSourceChunkSize(const void *ptr);
139
140/*
141 * Returns the number of bytes that the heap source has allocated
142 * from the system using sbrk/mmap, etc.
143 */
144size_t dvmHeapSourceFootprint(void);
145
146/*
147 * Gets the maximum number of bytes that the heap source is allowed
148 * to allocate from the system.
149 */
150size_t dvmHeapSourceGetIdealFootprint(void);
151
152/*
153 * Given the current contents of the heap, increase the allowed
154 * heap footprint to match the target utilization ratio.  This
155 * should only be called immediately after a full mark/sweep.
156 */
157void dvmHeapSourceGrowForUtilization(void);
158
159/*
160 * Return unused memory to the system if possible.  If <bytesTrimmed>
161 * is non-NULL, the number of bytes returned to the system is written to it.
162 */
163void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen);
164
165/*
166 * Walks over the heap source and passes every allocated and
167 * free chunk to the callback.
168 */
169void dvmHeapSourceWalk(void(*callback)(const void *chunkptr, size_t chunklen,
170                                      const void *userptr, size_t userlen,
171                                      void *arg),
172                       void *arg);
173/*
174 * Gets the number of heaps available in the heap source.
175 */
176size_t dvmHeapSourceGetNumHeaps(void);
177
178/*
179 * Exchanges the mark and object bitmaps.
180 */
181void dvmHeapSourceSwapBitmaps(void);
182
183/*
184 * Zeroes the mark bitmap.
185 */
186void dvmHeapSourceZeroMarkBitmap(void);
187
188/*
189 * Marks all objects inside the immune region of the heap. Addresses
190 * at or above this pointer are threatened, addresses below this
191 * pointer are immune.
192 */
193void dvmMarkImmuneObjects(const char *immuneLimit);
194
195/*
196 * Returns a pointer that demarcates the threatened region of the
197 * heap.  Addresses at or above this pointer are threatened, addresses
198 * below this pointer are immune.
199 */
200void *dvmHeapSourceGetImmuneLimit(bool isPartial);
201
202/*
203 * Returns the maximum size of the heap.  This value will be either
204 * the value of -Xmx or a user supplied growth limit.
205 */
206size_t dvmHeapSourceGetMaximumSize(void);
207
208#endif  // _DALVIK_HEAP_SOURCE
209