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