| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 19 | #include "alloc/Heap.h" |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | */ |
| Carl Shapiro | c8e06c8 | 2010-02-04 19:12:55 -0800 | [diff] [blame] | 29 | #define HEAP_SOURCE_MAX_HEAP_COUNT 2 |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Initializes the heap source; must be called before any other |
| 33 | * dvmHeapSource*() functions. |
| 34 | */ |
| 35 | GcHeap *dvmHeapSourceStartup(size_t startSize, size_t absoluteMaxSize); |
| 36 | |
| 37 | /* |
| 38 | * If the HeapSource was created while in zygote mode, this |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | * will create an additional zygote heap before the first fork(). |
| 40 | * Having a separate heap should reduce the number of shared |
| 41 | * pages subsequently touched by the zygote process. |
| 42 | */ |
| 43 | bool dvmHeapSourceStartupBeforeFork(void); |
| 44 | |
| 45 | /* |
| 46 | * Tears down the heap source and frees any resources associated with it. |
| 47 | */ |
| Carl Shapiro | a199eb7 | 2010-02-09 16:26:30 -0800 | [diff] [blame] | 48 | void dvmHeapSourceShutdown(GcHeap **gcHeap); |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | |
| 50 | /* |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 51 | * Initializes a vector of object and mark bits to the object and mark |
| 52 | * bits of to each heap. |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | */ |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 54 | void dvmHeapSourceGetObjectBitmaps(HeapBitmap objBits[], HeapBitmap markBits[], |
| 55 | size_t numHeaps); |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 58 | * Get the bitmap representing all live objects. |
| 59 | */ |
| 60 | HeapBitmap *dvmHeapSourceGetLiveBits(); |
| 61 | |
| 62 | /* |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | * Returns the requested value. If the per-heap stats are requested, fill |
| 64 | * them as well. |
| 65 | */ |
| 66 | enum HeapSourceValueSpec { |
| 67 | HS_FOOTPRINT, |
| 68 | HS_ALLOWED_FOOTPRINT, |
| 69 | HS_BYTES_ALLOCATED, |
| 70 | HS_OBJECTS_ALLOCATED, |
| 71 | HS_EXTERNAL_BYTES_ALLOCATED, |
| 72 | HS_EXTERNAL_LIMIT |
| 73 | }; |
| 74 | size_t dvmHeapSourceGetValue(enum HeapSourceValueSpec spec, |
| 75 | size_t perHeapStats[], size_t arrayLen); |
| 76 | |
| 77 | /* |
| 78 | * Allocates <n> bytes of zeroed data. |
| 79 | */ |
| 80 | void *dvmHeapSourceAlloc(size_t n); |
| 81 | |
| 82 | /* |
| 83 | * Allocates <n> bytes of zeroed data, growing up to absoluteMaxSize |
| 84 | * if necessary. |
| 85 | */ |
| 86 | void *dvmHeapSourceAllocAndGrow(size_t n); |
| 87 | |
| 88 | /* |
| 89 | * Frees the memory pointed to by <ptr>, which may be NULL. |
| 90 | */ |
| 91 | void dvmHeapSourceFree(void *ptr); |
| 92 | |
| 93 | /* |
| Barry Hayes | dde8ab0 | 2009-05-20 12:10:36 -0700 | [diff] [blame] | 94 | * Frees the first numPtrs objects in the ptrs list. The list must |
| 95 | * contain addresses all in the same mspace, and must be in increasing |
| 96 | * order. This implies that there are no duplicates, and no entries |
| 97 | * are NULL. |
| 98 | */ |
| 99 | void dvmHeapSourceFreeList(size_t numPtrs, void **ptrs); |
| 100 | |
| 101 | /* |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | * Returns true iff <ptr> was allocated from the heap source. |
| 103 | */ |
| 104 | bool dvmHeapSourceContains(const void *ptr); |
| 105 | |
| 106 | /* |
| 107 | * Returns the value of the requested flag. |
| 108 | */ |
| 109 | enum HeapSourcePtrFlag { |
| 110 | HS_CONTAINS, // identical to dvmHeapSourceContains() |
| 111 | HS_ALLOCATED_IN_ZYGOTE |
| 112 | }; |
| 113 | bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag); |
| 114 | |
| 115 | /* |
| 116 | * Returns the number of usable bytes in an allocated chunk; the size |
| 117 | * may be larger than the size passed to dvmHeapSourceAlloc(). |
| 118 | */ |
| 119 | size_t dvmHeapSourceChunkSize(const void *ptr); |
| 120 | |
| 121 | /* |
| 122 | * Returns the number of bytes that the heap source has allocated |
| 123 | * from the system using sbrk/mmap, etc. |
| 124 | */ |
| 125 | size_t dvmHeapSourceFootprint(void); |
| 126 | |
| 127 | /* |
| 128 | * Gets the maximum number of bytes that the heap source is allowed |
| 129 | * to allocate from the system. |
| 130 | */ |
| 131 | size_t dvmHeapSourceGetIdealFootprint(void); |
| 132 | |
| 133 | /* |
| 134 | * Given the current contents of the heap, increase the allowed |
| 135 | * heap footprint to match the target utilization ratio. This |
| 136 | * should only be called immediately after a full mark/sweep. |
| 137 | */ |
| 138 | void dvmHeapSourceGrowForUtilization(void); |
| 139 | |
| 140 | /* |
| 141 | * Return unused memory to the system if possible. If <bytesTrimmed> |
| 142 | * is non-NULL, the number of bytes returned to the system is written to it. |
| 143 | */ |
| 144 | void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen); |
| 145 | |
| 146 | /* |
| 147 | * Walks over the heap source and passes every allocated and |
| 148 | * free chunk to the callback. |
| 149 | */ |
| 150 | void dvmHeapSourceWalk(void(*callback)(const void *chunkptr, size_t chunklen, |
| 151 | const void *userptr, size_t userlen, |
| 152 | void *arg), |
| 153 | void *arg); |
| 154 | /* |
| 155 | * Gets the number of heaps available in the heap source. |
| 156 | */ |
| 157 | size_t dvmHeapSourceGetNumHeaps(void); |
| 158 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 159 | /* |
| 160 | * Exchanges the mark and object bitmaps and zeros the mark bitmap. |
| 161 | */ |
| 162 | void dvmHeapSourceSwapBitmaps(void); |
| 163 | |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 164 | /* |
| Barry Hayes | 425848f | 2010-05-04 13:32:12 -0700 | [diff] [blame^] | 165 | * Marks all objects inside the immune region of the heap. Addresses |
| 166 | * at or above this pointer are threatened, addresses below this |
| 167 | * pointer are immune. |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 168 | */ |
| Barry Hayes | 425848f | 2010-05-04 13:32:12 -0700 | [diff] [blame^] | 169 | void dvmMarkImmuneObjects(const char *immuneLimit); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Returns a pointer that demarcates the threatened region of the |
| 173 | * heap. Addresses at or above this pointer are threatened, addresses |
| Barry Hayes | 425848f | 2010-05-04 13:32:12 -0700 | [diff] [blame^] | 174 | * below this pointer are immune. |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 175 | */ |
| 176 | void *dvmHeapSourceGetImmuneLimit(GcMode mode); |
| 177 | |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 178 | #endif // _DALVIK_HEAP_SOURCE |