blob: ebb1c85deb4f39a1eac5b26532418be3e8368c03 [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"
Ian Rogers30fab402012-01-23 15:43:46 -080024#include "gtest/gtest.h"
Elliott Hughes5e71b522011-10-20 13:12:32 -070025#include "heap_bitmap.h"
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080026#include "mutex.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070027#include "offsets.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070028
Elliott Hughes3e465b12011-09-02 18:26:12 -070029#define VERIFY_OBJECT_ENABLED 0
30
Carl Shapiro1fb86202011-06-27 17:43:13 -070031namespace art {
32
Ian Rogers30fab402012-01-23 15:43:46 -080033class AllocSpace;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070034class Class;
35class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070036class Space;
Ian Rogers5d4bdc22011-11-02 22:15:43 -070037class Thread;
Carl Shapiro69759ea2011-07-21 18:13:35 -070038class HeapBitmap;
Ian Rogers30fab402012-01-23 15:43:46 -080039class SpaceTest;
Carl Shapiro69759ea2011-07-21 18:13:35 -070040
Carl Shapiro1fb86202011-06-27 17:43:13 -070041class Heap {
42 public:
Ian Rogers30fab402012-01-23 15:43:46 -080043 static const size_t kInitialSize = 2 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070044
Ian Rogers30fab402012-01-23 15:43:46 -080045 static const size_t kMaximumSize = 32 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070046
Elliott Hughes410c0c82011-09-01 17:58:25 -070047 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070048 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070049
Brian Carlstrom58ae9412011-10-04 00:56:06 -070050 // Create a heap with the requested sizes. The possible empty
51 // image_file_names names specify Spaces to load based on
52 // ImageWriter output.
Ian Rogers30fab402012-01-23 15:43:46 -080053 static void Init(size_t starting_size, size_t growth_limit, size_t capacity,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070054 const std::vector<std::string>& image_file_names);
Carl Shapiro61e019d2011-07-14 16:53:09 -070055
Carl Shapiro69759ea2011-07-21 18:13:35 -070056 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070057
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070058 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070059 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070060
Elliott Hughesa2501992011-08-26 19:39:54 -070061 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070062#if VERIFY_OBJECT_ENABLED
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063 static void VerifyObject(const Object *obj);
Elliott Hughes3e465b12011-09-02 18:26:12 -070064#else
65 static void VerifyObject(const Object *obj) {}
66#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070067
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070068 // Check sanity of all live references. Requires the heap lock.
69 static void VerifyHeap();
70
Elliott Hughes6a5bd492011-10-28 14:33:57 -070071 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -070072 // and doesn't abort on error, allowing the caller to report more
73 // meaningful diagnostics.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074 static bool IsHeapAddress(const Object* obj);
Elliott Hughes6a5bd492011-10-28 14:33:57 -070075 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
76 // Requires the heap lock to be held.
77 static bool IsLiveObjectLocked(const Object* obj);
Elliott Hughesa2501992011-08-26 19:39:54 -070078
Carl Shapiro69759ea2011-07-21 18:13:35 -070079 // Initiates an explicit garbage collection.
Ian Rogers30fab402012-01-23 15:43:46 -080080 static void CollectGarbage(bool clear_soft_references);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081
Elliott Hughesbf86d042011-08-31 17:53:14 -070082 // Implements java.lang.Runtime.maxMemory.
83 static int64_t GetMaxMemory();
84 // Implements java.lang.Runtime.totalMemory.
85 static int64_t GetTotalMemory();
86 // Implements java.lang.Runtime.freeMemory.
87 static int64_t GetFreeMemory();
88
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070089 // Implements VMDebug.countInstancesOfClass.
90 static int64_t CountInstances(Class* c, bool count_assignable);
91
Ian Rogers3bb17a62012-01-27 23:56:44 -080092 // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
93 // implement dalvik.system.VMRuntime.clearGrowthLimit.
jeffhaoc1160702011-10-27 15:48:45 -070094 static void ClearGrowthLimit();
95
Ian Rogers30fab402012-01-23 15:43:46 -080096 // Target ideal heap utilization ratio, implements
97 // dalvik.system.VMRuntime.getTargetHeapUtilization.
Elliott Hughes7ede61e2011-09-14 18:18:06 -070098 static float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -070099 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700100 }
Ian Rogers30fab402012-01-23 15:43:46 -0800101 // Set target ideal heap utilization ratio, implements
102 // dalvik.system.VMRuntime.setTargetHeapUtilization.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700103 static void SetTargetHeapUtilization(float target) {
Ian Rogers30fab402012-01-23 15:43:46 -0800104 DCHECK_GT(target, 0.0f); // asserted in Java code
105 DCHECK_LT(target, 1.0f);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700106 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700107 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800108
109 // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
110 // from the system. Doesn't allow the space to exceed its growth limit.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700111 static void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700112
Carl Shapiro69759ea2011-07-21 18:13:35 -0700113 // Blocks the caller until the garbage collector becomes idle.
114 static void WaitForConcurrentGcToComplete();
115
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700116 static pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700117 static void Lock();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700118 static void Unlock();
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800119 static void AssertLockHeld() {
120 lock_->AssertHeld();
121 }
122 static void AssertLockNotHeld() {
123 lock_->AssertNotHeld();
124 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700125
Carl Shapiro58551df2011-07-24 03:09:51 -0700126 static const std::vector<Space*>& GetSpaces() {
127 return spaces_;
128 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700129
Carl Shapiro58551df2011-07-24 03:09:51 -0700130 static HeapBitmap* GetLiveBits() {
131 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700132 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700133
134 static HeapBitmap* GetMarkBits() {
135 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700136 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700137
Elliott Hughesadb460d2011-10-05 17:02:34 -0700138 static void SetWellKnownClasses(Class* java_lang_ref_FinalizerReference,
139 Class* java_lang_ref_ReferenceQueue);
140
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700141 static void SetReferenceOffsets(MemberOffset reference_referent_offset,
142 MemberOffset reference_queue_offset,
143 MemberOffset reference_queueNext_offset,
144 MemberOffset reference_pendingNext_offset,
Elliott Hughesadb460d2011-10-05 17:02:34 -0700145 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700146
Elliott Hughesadb460d2011-10-05 17:02:34 -0700147 static Object* GetReferenceReferent(Object* reference);
148 static void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700149
Elliott Hughesadb460d2011-10-05 17:02:34 -0700150 // Returns true if the reference object has not yet been enqueued.
151 static bool IsEnqueuable(const Object* ref);
152 static void EnqueueReference(Object* ref, Object** list);
153 static void EnqueuePendingReference(Object* ref, Object** list);
154 static Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700155
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700156 static MemberOffset GetReferencePendingNextOffset() {
157 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700158 return reference_pendingNext_offset_;
159 }
160
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161 static MemberOffset GetFinalizerReferenceZombieOffset() {
162 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700163 return finalizer_reference_zombie_offset_;
164 }
165
Elliott Hughes85d15452011-09-16 17:33:01 -0700166 static void EnableObjectValidation() {
Ian Rogers30fab402012-01-23 15:43:46 -0800167#if VERIFY_OBJECT_ENABLED
168 Heap::VerifyHeap();
169#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700170 verify_objects_ = true;
171 }
172
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700173 static void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700174 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700175 }
176
Elliott Hughes92b3b562011-09-08 16:32:26 -0700177 // Callers must hold the heap lock.
Elliott Hughes307f75d2011-10-12 18:04:40 -0700178 static void RecordFreeLocked(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700179
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700180 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
181 // The call is not needed if NULL is stored in the field.
Ian Rogers5d76c432011-10-31 21:42:49 -0700182 static void WriteBarrierField(const Object* dest, MemberOffset offset, const Object* new_val) {
183 if (!card_marking_disabled_) {
184 card_table_->MarkCard(dest);
185 }
186 }
187
188 // Write barrier for array operations that update many field positions
189 static void WriteBarrierArray(const Object* dest, int pos, size_t len) {
190 if (UNLIKELY(!card_marking_disabled_)) {
191 card_table_->MarkCard(dest);
192 }
193 }
194
195 static CardTable* GetCardTable() {
196 return card_table_;
197 }
198
199 static void DisableCardMarking() {
200 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
201 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700202 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700203
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700204 static void AddFinalizerReference(Thread* self, Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700205
Elliott Hughes7162ad92011-10-27 14:08:42 -0700206 static size_t GetBytesAllocated() { return num_bytes_allocated_; }
207 static size_t GetObjectsAllocated() { return num_objects_allocated_; }
208
Ian Rogers30fab402012-01-23 15:43:46 -0800209 static AllocSpace* GetAllocSpace() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700210 return alloc_space_;
211 }
212
Carl Shapiro58551df2011-07-24 03:09:51 -0700213 private:
214 // Allocates uninitialized storage.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700215 static Object* AllocateLocked(size_t num_bytes);
Ian Rogers30fab402012-01-23 15:43:46 -0800216 static Object* AllocateLocked(AllocSpace* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700217
Elliott Hughesadb460d2011-10-05 17:02:34 -0700218 // Pushes a list of cleared references out to the managed heap.
219 static void EnqueueClearedReferences(Object** cleared_references);
220
Ian Rogers30fab402012-01-23 15:43:46 -0800221 static void RecordAllocationLocked(AllocSpace* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700222 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223
Ian Rogers30fab402012-01-23 15:43:46 -0800224 static void CollectGarbageInternal(bool clear_soft_references);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225
Ian Rogers3bb17a62012-01-27 23:56:44 -0800226 // Given the current contents of the alloc space, increase the allowed heap footprint to match
227 // the target utilization ratio. This should only be called immediately after a full garbage
228 // collection.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700229 static void GrowForUtilization();
230
Ian Rogers30fab402012-01-23 15:43:46 -0800231 static void AddSpace(Space* space) {
232 spaces_.push_back(space);
233 }
234
Elliott Hughes92b3b562011-09-08 16:32:26 -0700235 static void VerifyObjectLocked(const Object *obj);
236
Brian Carlstrom78128a62011-09-15 17:21:19 -0700237 static void VerificationCallback(Object* obj, void* arg);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700238
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239 static Mutex* lock_;
240
Carl Shapiro58551df2011-07-24 03:09:51 -0700241 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700243 // default Space for allocations
Ian Rogers30fab402012-01-23 15:43:46 -0800244 static AllocSpace* alloc_space_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700245
Carl Shapiro69759ea2011-07-21 18:13:35 -0700246 static HeapBitmap* mark_bitmap_;
247
248 static HeapBitmap* live_bitmap_;
249
Ian Rogers5d76c432011-10-31 21:42:49 -0700250 static CardTable* card_table_;
251
252 // Used by the image writer to disable card marking on copied objects
253 // TODO: remove
254 static bool card_marking_disabled_;
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
Ian Rogers30fab402012-01-23 15:43:46 -0800290 FRIEND_TEST(SpaceTest, AllocAndFree);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800291 FRIEND_TEST(SpaceTest, AllocAndFreeList);
292 friend class SpaceTest;
Ian Rogers30fab402012-01-23 15:43:46 -0800293
Carl Shapiro69759ea2011-07-21 18:13:35 -0700294 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
295};
296
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700297class ScopedHeapLock {
298 public:
299 ScopedHeapLock() {
300 Heap::Lock();
301 }
302
303 ~ScopedHeapLock() {
304 Heap::Unlock();
305 }
306
307 private:
308 DISALLOW_COPY_AND_ASSIGN(ScopedHeapLock);
309};
310
Carl Shapiro1fb86202011-06-27 17:43:13 -0700311} // namespace art
312
313#endif // ART_SRC_HEAP_H_