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