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