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 /*
     17  * Types and macros used internally by the heap.
     18  */
     19 #ifndef _DALVIK_ALLOC_HEAP_INTERNAL
     20 #define _DALVIK_ALLOC_HEAP_INTERNAL
     21 
     22 #include <time.h>  // for struct timespec
     23 
     24 #include "HeapTable.h"
     25 #include "MarkSweep.h"
     26 
     27 struct GcHeap {
     28     HeapSource      *heapSource;
     29 
     30     /* List of heap objects that will require finalization when
     31      * collected.  I.e., instance objects
     32      *
     33      *     a) whose class definitions override java.lang.Object.finalize()
     34      *
     35      * *** AND ***
     36      *
     37      *     b) that have never been finalized.
     38      *
     39      * Note that this does not exclude non-garbage objects;  this
     40      * is not the list of pending finalizations, but of objects that
     41      * potentially have finalization in their futures.
     42      */
     43     LargeHeapRefTable  *finalizableRefs;
     44 
     45     /* The list of objects that need to have finalize() called
     46      * on themselves.  These references are part of the root set.
     47      *
     48      * This table is protected by gDvm.heapWorkerListLock, which must
     49      * be acquired after the heap lock.
     50      */
     51     LargeHeapRefTable  *pendingFinalizationRefs;
     52 
     53     /* Linked lists of subclass instances of java/lang/ref/Reference
     54      * that we find while recursing.  The "next" pointers are hidden
     55      * in the objects' <code>int Reference.vmData</code> fields.
     56      * These lists are cleared and rebuilt each time the GC runs.
     57      */
     58     Object         *softReferences;
     59     Object         *weakReferences;
     60     Object         *phantomReferences;
     61 
     62     /* The list of Reference objects that need to be cleared and/or
     63      * enqueued.  The bottom two bits of the object pointers indicate
     64      * whether they should be cleared and/or enqueued.
     65      *
     66      * This table is protected by gDvm.heapWorkerListLock, which must
     67      * be acquired after the heap lock.
     68      */
     69     LargeHeapRefTable  *referenceOperations;
     70 
     71     /* If non-null, the method that the HeapWorker is currently
     72      * executing.
     73      */
     74     Object *heapWorkerCurrentObject;
     75     Method *heapWorkerCurrentMethod;
     76 
     77     /* If heapWorkerCurrentObject is non-null, this gives the time when
     78      * HeapWorker started executing that method.  The time value must come
     79      * from dvmGetRelativeTimeUsec().
     80      *
     81      * The "Cpu" entry tracks the per-thread CPU timer (when available).
     82      */
     83     u8 heapWorkerInterpStartTime;
     84     u8 heapWorkerInterpCpuStartTime;
     85 
     86     /* If any fields are non-zero, indicates the next (absolute) time that
     87      * the HeapWorker thread should call dvmHeapSourceTrim().
     88      */
     89     struct timespec heapWorkerNextTrim;
     90 
     91     /* The current state of the mark step.
     92      * Only valid during a GC.
     93      */
     94     GcMarkContext   markContext;
     95 
     96     /* GC's card table */
     97     u1*             cardTableBase;
     98     size_t          cardTableLength;
     99 
    100     /* Is the GC running?  Used to avoid recursive calls to GC.
    101      */
    102     bool            gcRunning;
    103 
    104     /*
    105      * Debug control values
    106      */
    107 
    108     int             ddmHpifWhen;
    109     int             ddmHpsgWhen;
    110     int             ddmHpsgWhat;
    111     int             ddmNhsgWhen;
    112     int             ddmNhsgWhat;
    113 
    114 #if WITH_HPROF
    115     bool            hprofDumpOnGc;
    116     const char*     hprofFileName;
    117     int             hprofFd;
    118     hprof_context_t *hprofContext;
    119     int             hprofResult;
    120     bool            hprofDirectToDdms;
    121 #endif
    122 };
    123 
    124 bool dvmLockHeap(void);
    125 void dvmUnlockHeap(void);
    126 void dvmLogGcStats(size_t numFreed, size_t sizeFreed, size_t gcTimeMs);
    127 void dvmLogMadviseStats(size_t madvisedSizes[], size_t arrayLen);
    128 
    129 /*
    130  * Logging helpers
    131  */
    132 
    133 #define HEAP_LOG_TAG      LOG_TAG "-heap"
    134 
    135 #if LOG_NDEBUG
    136 #define LOGV_HEAP(...)    ((void)0)
    137 #define LOGD_HEAP(...)    ((void)0)
    138 #else
    139 #define LOGV_HEAP(...)    LOG(LOG_VERBOSE, HEAP_LOG_TAG, __VA_ARGS__)
    140 #define LOGD_HEAP(...)    LOG(LOG_DEBUG, HEAP_LOG_TAG, __VA_ARGS__)
    141 #endif
    142 #define LOGI_HEAP(...)    LOG(LOG_INFO, HEAP_LOG_TAG, __VA_ARGS__)
    143 #define LOGW_HEAP(...)    LOG(LOG_WARN, HEAP_LOG_TAG, __VA_ARGS__)
    144 #define LOGE_HEAP(...)    LOG(LOG_ERROR, HEAP_LOG_TAG, __VA_ARGS__)
    145 
    146 #define QUIET_ZYGOTE_GC 1
    147 #if QUIET_ZYGOTE_GC
    148 #undef LOGI_HEAP
    149 #define LOGI_HEAP(...) \
    150     do { \
    151         if (!gDvm.zygote) { \
    152             LOG(LOG_INFO, HEAP_LOG_TAG, __VA_ARGS__); \
    153         } \
    154     } while (false)
    155 #endif
    156 
    157 #define FRACTIONAL_MB(n)    (n) / (1024 * 1024), \
    158                             ((((n) % (1024 * 1024)) / 1024) * 1000) / 1024
    159 #define FRACTIONAL_PCT(n,max)    ((n) * 100) / (max), \
    160                                  (((n) * 1000) / (max)) % 10
    161 
    162 #endif  // _DALVIK_ALLOC_HEAP_INTERNAL
    163