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