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 * Returns true iff <ptr> was allocated from the heap source. 109 */ 110 bool dvmHeapSourceContains(const void *ptr); 111 112 /* 113 * Returns the value of the requested flag. 114 */ 115 enum HeapSourcePtrFlag { 116 HS_CONTAINS, // identical to dvmHeapSourceContains() 117 HS_ALLOCATED_IN_ZYGOTE 118 }; 119 bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag); 120 121 /* 122 * Returns the number of usable bytes in an allocated chunk; the size 123 * may be larger than the size passed to dvmHeapSourceAlloc(). 124 */ 125 size_t dvmHeapSourceChunkSize(const void *ptr); 126 127 /* 128 * Returns the number of bytes that the heap source has allocated 129 * from the system using sbrk/mmap, etc. 130 */ 131 size_t dvmHeapSourceFootprint(void); 132 133 /* 134 * Gets the maximum number of bytes that the heap source is allowed 135 * to allocate from the system. 136 */ 137 size_t dvmHeapSourceGetIdealFootprint(void); 138 139 /* 140 * Given the current contents of the heap, increase the allowed 141 * heap footprint to match the target utilization ratio. This 142 * should only be called immediately after a full mark/sweep. 143 */ 144 void dvmHeapSourceGrowForUtilization(void); 145 146 /* 147 * Return unused memory to the system if possible. If <bytesTrimmed> 148 * is non-NULL, the number of bytes returned to the system is written to it. 149 */ 150 void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen); 151 152 /* 153 * Walks over the heap source and passes every allocated and 154 * free chunk to the callback. 155 */ 156 void dvmHeapSourceWalk(void(*callback)(const void *chunkptr, size_t chunklen, 157 const void *userptr, size_t userlen, 158 void *arg), 159 void *arg); 160 /* 161 * Gets the number of heaps available in the heap source. 162 */ 163 size_t dvmHeapSourceGetNumHeaps(void); 164 165 #endif // _DALVIK_HEAP_SOURCE 166