blob: 923b015ca765bcb3fbb9f503dddc7344d16160c8 [file] [log] [blame]
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001/*
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 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_HEAP_H_
18#define ART_SRC_HEAP_H_
19
Carl Shapiro58551df2011-07-24 03:09:51 -070020#include <vector>
21
Ian Rogers5d76c432011-10-31 21:42:49 -070022#include "card_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "globals.h"
Elliott Hughes5e71b522011-10-20 13:12:32 -070024#include "heap_bitmap.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070025#include "offsets.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070026
Elliott Hughes3e465b12011-09-02 18:26:12 -070027#define VERIFY_OBJECT_ENABLED 0
28
Carl Shapiro1fb86202011-06-27 17:43:13 -070029namespace art {
30
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031class Class;
Elliott Hughes410c0c82011-09-01 17:58:25 -070032class Mutex;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070033class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070034class Space;
35class HeapBitmap;
36
Carl Shapiro1fb86202011-06-27 17:43:13 -070037class Heap {
38 public:
Brian Carlstrom27ec9612011-09-19 20:20:38 -070039 static const size_t kInitialSize = 4 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070040
Brian Carlstrom27ec9612011-09-19 20:20:38 -070041 static const size_t kMaximumSize = 16 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070042
Elliott Hughes410c0c82011-09-01 17:58:25 -070043 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070044 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070045
Brian Carlstrom58ae9412011-10-04 00:56:06 -070046 // Create a heap with the requested sizes. The possible empty
47 // image_file_names names specify Spaces to load based on
48 // ImageWriter output.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070049 static void Init(bool is_verbose_heap, bool is_verbose_gc,
jeffhaoc1160702011-10-27 15:48:45 -070050 size_t starting_size, size_t maximum_size, size_t growth_size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070051 const std::vector<std::string>& image_file_names);
Carl Shapiro61e019d2011-07-14 16:53:09 -070052
Carl Shapiro69759ea2011-07-21 18:13:35 -070053 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070054
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070055 static bool IsVerboseHeap() {
Elliott Hughes352a4242011-10-31 15:15:21 -070056 return is_verbose_heap_ || is_verbose_gc_ /* TODO: remove when pause times are down */;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070057 }
58
59 static bool IsVerboseGc() {
60 return is_verbose_gc_;
61 }
62
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070063 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070064 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070065
Elliott Hughesa2501992011-08-26 19:39:54 -070066 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070067#if VERIFY_OBJECT_ENABLED
Elliott Hughescf4c6c42011-09-01 15:16:42 -070068 static void VerifyObject(const Object *obj);
Elliott Hughes3e465b12011-09-02 18:26:12 -070069#else
70 static void VerifyObject(const Object *obj) {}
71#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070072
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070073 // Check sanity of all live references. Requires the heap lock.
74 static void VerifyHeap();
75
Elliott Hughes6a5bd492011-10-28 14:33:57 -070076 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -070077 // and doesn't abort on error, allowing the caller to report more
78 // meaningful diagnostics.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070079 static bool IsHeapAddress(const Object* obj);
Elliott Hughes6a5bd492011-10-28 14:33:57 -070080 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
81 // Requires the heap lock to be held.
82 static bool IsLiveObjectLocked(const Object* obj);
Elliott Hughesa2501992011-08-26 19:39:54 -070083
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 // Initiates an explicit garbage collection.
85 static void CollectGarbage();
86
Elliott Hughesbf86d042011-08-31 17:53:14 -070087 // Implements java.lang.Runtime.maxMemory.
88 static int64_t GetMaxMemory();
89 // Implements java.lang.Runtime.totalMemory.
90 static int64_t GetTotalMemory();
91 // Implements java.lang.Runtime.freeMemory.
92 static int64_t GetFreeMemory();
93
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070094 // Implements VMDebug.countInstancesOfClass.
95 static int64_t CountInstances(Class* c, bool count_assignable);
96
Elliott Hughes7ede61e2011-09-14 18:18:06 -070097 // Implements dalvik.system.VMRuntime.clearGrowthLimit.
jeffhaoc1160702011-10-27 15:48:45 -070098 static void ClearGrowthLimit();
99
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700100 // Implements dalvik.system.VMRuntime.getTargetHeapUtilization.
101 static float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700102 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700103 }
104 // Implements dalvik.system.VMRuntime.setTargetHeapUtilization.
105 static void SetTargetHeapUtilization(float target) {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700106 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700107 }
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700108 // Sets the maximum number of bytes that the heap is allowed to allocate
109 // from the system. Clamps to the appropriate maximum value.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700110 static void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700111
Carl Shapiro69759ea2011-07-21 18:13:35 -0700112 // Blocks the caller until the garbage collector becomes idle.
113 static void WaitForConcurrentGcToComplete();
114
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700115 static pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700116 static void Lock();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700117 static void Unlock();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700118
Carl Shapiro58551df2011-07-24 03:09:51 -0700119 static const std::vector<Space*>& GetSpaces() {
120 return spaces_;
121 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700122
Carl Shapiro58551df2011-07-24 03:09:51 -0700123 static HeapBitmap* GetLiveBits() {
124 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700125 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700126
127 static HeapBitmap* GetMarkBits() {
128 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700129 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700130
Elliott Hughesadb460d2011-10-05 17:02:34 -0700131 static void SetWellKnownClasses(Class* java_lang_ref_FinalizerReference,
132 Class* java_lang_ref_ReferenceQueue);
133
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700134 static void SetReferenceOffsets(MemberOffset reference_referent_offset,
135 MemberOffset reference_queue_offset,
136 MemberOffset reference_queueNext_offset,
137 MemberOffset reference_pendingNext_offset,
Elliott Hughesadb460d2011-10-05 17:02:34 -0700138 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700139
Elliott Hughesadb460d2011-10-05 17:02:34 -0700140 static Object* GetReferenceReferent(Object* reference);
141 static void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700142
Elliott Hughesadb460d2011-10-05 17:02:34 -0700143 // Returns true if the reference object has not yet been enqueued.
144 static bool IsEnqueuable(const Object* ref);
145 static void EnqueueReference(Object* ref, Object** list);
146 static void EnqueuePendingReference(Object* ref, Object** list);
147 static Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700148
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700149 static MemberOffset GetReferencePendingNextOffset() {
150 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700151 return reference_pendingNext_offset_;
152 }
153
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154 static MemberOffset GetFinalizerReferenceZombieOffset() {
155 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700156 return finalizer_reference_zombie_offset_;
157 }
158
Elliott Hughes85d15452011-09-16 17:33:01 -0700159 static void EnableObjectValidation() {
160 verify_objects_ = true;
161 }
162
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700163 static void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700164 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700165 }
166
Elliott Hughes92b3b562011-09-08 16:32:26 -0700167 // Callers must hold the heap lock.
Elliott Hughes307f75d2011-10-12 18:04:40 -0700168 static void RecordFreeLocked(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700169
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700170 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
171 // The call is not needed if NULL is stored in the field.
Ian Rogers5d76c432011-10-31 21:42:49 -0700172 static void WriteBarrierField(const Object* dest, MemberOffset offset, const Object* new_val) {
173 if (!card_marking_disabled_) {
174 card_table_->MarkCard(dest);
175 }
176 }
177
178 // Write barrier for array operations that update many field positions
179 static void WriteBarrierArray(const Object* dest, int pos, size_t len) {
180 if (UNLIKELY(!card_marking_disabled_)) {
181 card_table_->MarkCard(dest);
182 }
183 }
184
185 static CardTable* GetCardTable() {
186 return card_table_;
187 }
188
189 static void DisableCardMarking() {
190 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
191 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700192 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700193
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700194 // dlmalloc_walk_heap-compatible heap walker.
195 static void WalkHeap(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg);
196
Elliott Hughesadb460d2011-10-05 17:02:34 -0700197 static void AddFinalizerReference(Object* object);
198
Elliott Hughes7162ad92011-10-27 14:08:42 -0700199 static size_t GetBytesAllocated() { return num_bytes_allocated_; }
200 static size_t GetObjectsAllocated() { return num_objects_allocated_; }
201
Ian Rogers5d76c432011-10-31 21:42:49 -0700202 static Space* GetAllocSpace() {
203 return alloc_space_;
204 }
205
Carl Shapiro58551df2011-07-24 03:09:51 -0700206 private:
207 // Allocates uninitialized storage.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700208 static Object* AllocateLocked(size_t num_bytes);
209 static Object* AllocateLocked(Space* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700210
Elliott Hughesadb460d2011-10-05 17:02:34 -0700211 // Pushes a list of cleared references out to the managed heap.
212 static void EnqueueClearedReferences(Object** cleared_references);
213
Elliott Hughes92b3b562011-09-08 16:32:26 -0700214 static void RecordAllocationLocked(Space* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700215 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216
217 static void CollectGarbageInternal();
218
219 static void GrowForUtilization();
220
Elliott Hughes92b3b562011-09-08 16:32:26 -0700221 static void VerifyObjectLocked(const Object *obj);
222
Brian Carlstrom78128a62011-09-15 17:21:19 -0700223 static void VerificationCallback(Object* obj, void* arg);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700224
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225 static Mutex* lock_;
226
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700227 static bool is_verbose_heap_;
228
229 static bool is_verbose_gc_;
230
Carl Shapiro58551df2011-07-24 03:09:51 -0700231 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700232
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700233 // default Space for allocations
234 static Space* alloc_space_;
235
Carl Shapiro69759ea2011-07-21 18:13:35 -0700236 static HeapBitmap* mark_bitmap_;
237
238 static HeapBitmap* live_bitmap_;
239
Ian Rogers5d76c432011-10-31 21:42:49 -0700240 static CardTable* card_table_;
241
242 // Used by the image writer to disable card marking on copied objects
243 // TODO: remove
244 static bool card_marking_disabled_;
245
Carl Shapiro58551df2011-07-24 03:09:51 -0700246 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247 static size_t maximum_size_;
248
jeffhaoc1160702011-10-27 15:48:45 -0700249 // The largest size the heap may grow. This value allows the app to limit the
250 // growth below the maximum size. This is a work around until we can
251 // dynamically set the maximum size. This value can range between the starting
252 // size and the maximum size but should never be set below the current
253 // footprint of the heap.
254 static size_t growth_size_;
255
Carl Shapiro58551df2011-07-24 03:09:51 -0700256 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257 static bool is_gc_running_;
258
Carl Shapiro58551df2011-07-24 03:09:51 -0700259 // Number of bytes allocated. Adjusted after each allocation and
260 // free.
261 static size_t num_bytes_allocated_;
262
263 // Number of objects allocated. Adjusted after each allocation and
264 // free.
265 static size_t num_objects_allocated_;
266
Elliott Hughesadb460d2011-10-05 17:02:34 -0700267 static Class* java_lang_ref_FinalizerReference_;
268 static Class* java_lang_ref_ReferenceQueue_;
269
Brian Carlstrom1f870082011-08-23 16:02:11 -0700270 // offset of java.lang.ref.Reference.referent
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271 static MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700272
273 // offset of java.lang.ref.Reference.queue
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700274 static MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700275
276 // offset of java.lang.ref.Reference.queueNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 static MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700278
279 // offset of java.lang.ref.Reference.pendingNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280 static MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700281
282 // offset of java.lang.ref.FinalizerReference.zombie
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 static MemberOffset finalizer_reference_zombie_offset_;
284
Brian Carlstrom395520e2011-09-25 19:35:00 -0700285 // Target ideal heap utilization ratio
286 static float target_utilization_;
287
Elliott Hughes85d15452011-09-16 17:33:01 -0700288 static bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700289
Carl Shapiro69759ea2011-07-21 18:13:35 -0700290 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
291};
292
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700293class ScopedHeapLock {
294 public:
295 ScopedHeapLock() {
296 Heap::Lock();
297 }
298
299 ~ScopedHeapLock() {
300 Heap::Unlock();
301 }
302
303 private:
304 DISALLOW_COPY_AND_ASSIGN(ScopedHeapLock);
305};
306
Carl Shapiro1fb86202011-06-27 17:43:13 -0700307} // namespace art
308
309#endif // ART_SRC_HEAP_H_