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/HeapInternal.h" // for GcHeap 20 21 /* dlmalloc uses one size_t per allocated chunk. 22 */ 23 #define HEAP_SOURCE_CHUNK_OVERHEAD (1 * sizeof (size_t)) 24 #define HEAP_SOURCE_WORST_CHUNK_OVERHEAD (32 * sizeof (size_t)) 25 26 /* The largest number of separate heaps we can handle. 27 */ 28 #define HEAP_SOURCE_MAX_HEAP_COUNT 3 29 30 /* 31 * Initializes the heap source; must be called before any other 32 * dvmHeapSource*() functions. 33 */ 34 GcHeap *dvmHeapSourceStartup(size_t startSize, size_t absoluteMaxSize); 35 36 /* 37 * If the HeapSource was created while in zygote mode, this 38 * will create a new heap for post-zygote allocations. 39 * Having a separate heap should maximize the number of pages 40 * that a given app_process shares with the zygote process. 41 */ 42 bool dvmHeapSourceStartupAfterZygote(void); 43 44 /* 45 * If the HeapSource was created while in zygote mode, this 46 * will create an additional zygote heap before the first fork(). 47 * Having a separate heap should reduce the number of shared 48 * pages subsequently touched by the zygote process. 49 */ 50 bool dvmHeapSourceStartupBeforeFork(void); 51 52 /* 53 * Tears down the heap source and frees any resources associated with it. 54 */ 55 void dvmHeapSourceShutdown(GcHeap *gcHeap); 56 57 /* 58 * Writes shallow copies of the currently-used bitmaps into outBitmaps, 59 * returning the number of bitmaps written. Returns <0 if the array 60 * was not long enough. 61 */ 62 ssize_t dvmHeapSourceGetObjectBitmaps(HeapBitmap outBitmaps[], 63 size_t maxBitmaps); 64 65 /* 66 * Replaces the object location HeapBitmaps with the elements of 67 * <objectBitmaps>. The elements of <objectBitmaps> are overwritten 68 * with shallow copies of the old bitmaps. 69 * 70 * Returns false if the number of bitmaps doesn't match the number 71 * of heaps. 72 */ 73 bool dvmHeapSourceReplaceObjectBitmaps(HeapBitmap objectBitmaps[], 74 size_t nBitmaps); 75 76 /* 77 * Returns the requested value. If the per-heap stats are requested, fill 78 * them as well. 79 */ 80 enum HeapSourceValueSpec { 81 HS_FOOTPRINT, 82 HS_ALLOWED_FOOTPRINT, 83 HS_BYTES_ALLOCATED, 84 HS_OBJECTS_ALLOCATED, 85 HS_EXTERNAL_BYTES_ALLOCATED, 86 HS_EXTERNAL_LIMIT 87 }; 88 size_t dvmHeapSourceGetValue(enum HeapSourceValueSpec spec, 89 size_t perHeapStats[], size_t arrayLen); 90 91 /* 92 * Allocates <n> bytes of zeroed data. 93 */ 94 void *dvmHeapSourceAlloc(size_t n); 95 96 /* 97 * Allocates <n> bytes of zeroed data, growing up to absoluteMaxSize 98 * if necessary. 99 */ 100 void *dvmHeapSourceAllocAndGrow(size_t n); 101 102 /* 103 * Frees the memory pointed to by <ptr>, which may be NULL. 104 */ 105 void dvmHeapSourceFree(void *ptr); 106 107 /* 108 * Frees the first numPtrs objects in the ptrs list. The list must 109 * contain addresses all in the same mspace, and must be in increasing 110 * order. This implies that there are no duplicates, and no entries 111 * are NULL. 112 */ 113 void dvmHeapSourceFreeList(size_t numPtrs, void **ptrs); 114 115 /* 116 * Returns true iff <ptr> was allocated from the heap source. 117 */ 118 bool dvmHeapSourceContains(const void *ptr); 119 120 /* 121 * Returns the value of the requested flag. 122 */ 123 enum HeapSourcePtrFlag { 124 HS_CONTAINS, // identical to dvmHeapSourceContains() 125 HS_ALLOCATED_IN_ZYGOTE 126 }; 127 bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag); 128 129 /* 130 * Returns the number of usable bytes in an allocated chunk; the size 131 * may be larger than the size passed to dvmHeapSourceAlloc(). 132 */ 133 size_t dvmHeapSourceChunkSize(const void *ptr); 134 135 /* 136 * Returns the number of bytes that the heap source has allocated 137 * from the system using sbrk/mmap, etc. 138 */ 139 size_t dvmHeapSourceFootprint(void); 140 141 /* 142 * Gets the maximum number of bytes that the heap source is allowed 143 * to allocate from the system. 144 */ 145 size_t dvmHeapSourceGetIdealFootprint(void); 146 147 /* 148 * Given the current contents of the heap, increase the allowed 149 * heap footprint to match the target utilization ratio. This 150 * should only be called immediately after a full mark/sweep. 151 */ 152 void dvmHeapSourceGrowForUtilization(void); 153 154 /* 155 * Return unused memory to the system if possible. If <bytesTrimmed> 156 * is non-NULL, the number of bytes returned to the system is written to it. 157 */ 158 void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen); 159 160 /* 161 * Walks over the heap source and passes every allocated and 162 * free chunk to the callback. 163 */ 164 void dvmHeapSourceWalk(void(*callback)(const void *chunkptr, size_t chunklen, 165 const void *userptr, size_t userlen, 166 void *arg), 167 void *arg); 168 /* 169 * Gets the number of heaps available in the heap source. 170 */ 171 size_t dvmHeapSourceGetNumHeaps(void); 172 173 #endif // _DALVIK_HEAP_SOURCE 174