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