blob: 46f5cc148aee8321fa82345947930f4e1d4f9669 [file] [log] [blame]
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001/*
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
Carl Shapirod25566d2010-03-11 20:39:47 -080019#include "alloc/Heap.h"
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070020#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 */
Carl Shapiroc8e06c82010-02-04 19:12:55 -080029#define HEAP_SOURCE_MAX_HEAP_COUNT 2
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070030
31/*
32 * Initializes the heap source; must be called before any other
33 * dvmHeapSource*() functions.
34 */
35GcHeap *dvmHeapSourceStartup(size_t startSize, size_t absoluteMaxSize);
36
37/*
38 * If the HeapSource was created while in zygote mode, this
Carl Shapiroec805ea2010-06-28 16:28:26 -070039 * will create a new heap for post-zygote allocations.
40 * Having a separate heap should maximize the number of pages
41 * that a given app_process shares with the zygote process.
42 */
43bool dvmHeapSourceStartupAfterZygote(void);
44
45/*
46 * If the HeapSource was created while in zygote mode, this
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070047 * will create an additional zygote heap before the first fork().
48 * Having a separate heap should reduce the number of shared
49 * pages subsequently touched by the zygote process.
50 */
51bool dvmHeapSourceStartupBeforeFork(void);
52
53/*
Carl Shapiroec805ea2010-06-28 16:28:26 -070054 * Shutdown any threads internal to the heap source. This should be
55 * called before the heap source itself is shutdown.
56 */
57void dvmHeapSourceThreadShutdown(void);
58
59/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070060 * Tears down the heap source and frees any resources associated with it.
61 */
Carl Shapiroa199eb72010-02-09 16:26:30 -080062void dvmHeapSourceShutdown(GcHeap **gcHeap);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070063
64/*
Carl Shapirof373efd2010-02-19 00:46:33 -080065 * Initializes a vector of object and mark bits to the object and mark
Barry Hayese168ebd2010-05-07 09:19:46 -070066 * bits of each heap.
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070067 */
Carl Shapirof373efd2010-02-19 00:46:33 -080068void dvmHeapSourceGetObjectBitmaps(HeapBitmap objBits[], HeapBitmap markBits[],
69 size_t numHeaps);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070070
71/*
Barry Hayes962adba2010-03-17 12:12:39 -070072 * Get the bitmap representing all live objects.
73 */
Carl Shapiroec805ea2010-06-28 16:28:26 -070074HeapBitmap *dvmHeapSourceGetLiveBits(void);
Barry Hayes962adba2010-03-17 12:12:39 -070075
76/*
Barry Hayesb874ab92010-07-14 08:13:18 -070077 * Gets the begining of the allocation for the HeapSource.
78 */
79void *dvmHeapSourceGetBase(void);
80
81/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070082 * Returns the requested value. If the per-heap stats are requested, fill
83 * them as well.
84 */
85enum HeapSourceValueSpec {
86 HS_FOOTPRINT,
87 HS_ALLOWED_FOOTPRINT,
88 HS_BYTES_ALLOCATED,
89 HS_OBJECTS_ALLOCATED,
90 HS_EXTERNAL_BYTES_ALLOCATED,
91 HS_EXTERNAL_LIMIT
92};
Carl Shapirode750892010-06-08 16:37:12 -070093size_t dvmHeapSourceGetValue(enum HeapSourceValueSpec spec,
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070094 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 memory pointed to by <ptr>, which may be NULL.
109 */
110void dvmHeapSourceFree(void *ptr);
111
112/*
Barry Hayesdde8ab02009-05-20 12:10:36 -0700113 * Frees the first numPtrs objects in the ptrs list. The list must
114 * contain addresses all in the same mspace, and must be in increasing
115 * order. This implies that there are no duplicates, and no entries
116 * are NULL.
117 */
118void dvmHeapSourceFreeList(size_t numPtrs, void **ptrs);
119
120/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700121 * Returns true iff <ptr> was allocated from the heap source.
122 */
123bool dvmHeapSourceContains(const void *ptr);
124
125/*
Barry Hayes364f9d92010-06-11 16:12:47 -0700126 * Returns true iff <ptr> is within the address space managed by heap source.
127 */
128bool dvmHeapSourceContainsAddress(const void *ptr);
129
130/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700131 * Returns the value of the requested flag.
132 */
133enum HeapSourcePtrFlag {
134 HS_CONTAINS, // identical to dvmHeapSourceContains()
135 HS_ALLOCATED_IN_ZYGOTE
136};
137bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag);
138
139/*
140 * Returns the number of usable bytes in an allocated chunk; the size
141 * may be larger than the size passed to dvmHeapSourceAlloc().
142 */
143size_t dvmHeapSourceChunkSize(const void *ptr);
144
145/*
146 * Returns the number of bytes that the heap source has allocated
147 * from the system using sbrk/mmap, etc.
148 */
149size_t dvmHeapSourceFootprint(void);
150
151/*
152 * Gets the maximum number of bytes that the heap source is allowed
153 * to allocate from the system.
154 */
155size_t dvmHeapSourceGetIdealFootprint(void);
156
157/*
158 * Given the current contents of the heap, increase the allowed
159 * heap footprint to match the target utilization ratio. This
160 * should only be called immediately after a full mark/sweep.
161 */
162void dvmHeapSourceGrowForUtilization(void);
163
164/*
165 * Return unused memory to the system if possible. If <bytesTrimmed>
166 * is non-NULL, the number of bytes returned to the system is written to it.
167 */
168void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen);
169
170/*
171 * Walks over the heap source and passes every allocated and
172 * free chunk to the callback.
173 */
174void dvmHeapSourceWalk(void(*callback)(const void *chunkptr, size_t chunklen,
175 const void *userptr, size_t userlen,
176 void *arg),
177 void *arg);
178/*
179 * Gets the number of heaps available in the heap source.
180 */
181size_t dvmHeapSourceGetNumHeaps(void);
182
Carl Shapirof373efd2010-02-19 00:46:33 -0800183/*
Barry Hayes81010a42010-07-19 14:07:01 -0700184 * Exchanges the mark and object bitmaps.
Carl Shapirof373efd2010-02-19 00:46:33 -0800185 */
186void dvmHeapSourceSwapBitmaps(void);
187
Carl Shapirod25566d2010-03-11 20:39:47 -0800188/*
Barry Hayes81010a42010-07-19 14:07:01 -0700189 * Zeroes the mark bitmap.
190 */
191void dvmHeapSourceZeroMarkBitmap(void);
192
193/*
Barry Hayes425848f2010-05-04 13:32:12 -0700194 * Marks all objects inside the immune region of the heap. Addresses
195 * at or above this pointer are threatened, addresses below this
196 * pointer are immune.
Carl Shapirod25566d2010-03-11 20:39:47 -0800197 */
Barry Hayes425848f2010-05-04 13:32:12 -0700198void dvmMarkImmuneObjects(const char *immuneLimit);
Carl Shapirod25566d2010-03-11 20:39:47 -0800199
200/*
201 * Returns a pointer that demarcates the threatened region of the
202 * heap. Addresses at or above this pointer are threatened, addresses
Barry Hayes425848f2010-05-04 13:32:12 -0700203 * below this pointer are immune.
Carl Shapirod25566d2010-03-11 20:39:47 -0800204 */
205void *dvmHeapSourceGetImmuneLimit(GcMode mode);
206
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700207#endif // _DALVIK_HEAP_SOURCE