blob: fc3fce05e8389a3f32acddf34bb32be08fd3b225 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "heap.h"
Carl Shapiro58551df2011-07-24 03:09:51 -07004
5#include <vector>
6
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include "UniquePtr.h"
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07008#include "image.h"
Carl Shapiro58551df2011-07-24 03:09:51 -07009#include "mark_sweep.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "object.h"
11#include "space.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070012#include "stl_util.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070013
14namespace art {
15
Carl Shapiro58551df2011-07-24 03:09:51 -070016std::vector<Space*> Heap::spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070017
Brian Carlstroma663ea52011-08-19 23:33:41 -070018Space* Heap::boot_space_ = NULL;
19
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070020Space* Heap::alloc_space_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -070021
22size_t Heap::maximum_size_ = 0;
23
Carl Shapiro58551df2011-07-24 03:09:51 -070024size_t Heap::num_bytes_allocated_ = 0;
25
26size_t Heap::num_objects_allocated_ = 0;
27
Carl Shapiro69759ea2011-07-21 18:13:35 -070028bool Heap::is_gc_running_ = false;
29
30HeapBitmap* Heap::mark_bitmap_ = NULL;
31
32HeapBitmap* Heap::live_bitmap_ = NULL;
33
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070034MemberOffset Heap::reference_referent_offset_ = MemberOffset(0);
35MemberOffset Heap::reference_queue_offset_ = MemberOffset(0);
36MemberOffset Heap::reference_queueNext_offset_ = MemberOffset(0);
37MemberOffset Heap::reference_pendingNext_offset_ = MemberOffset(0);
38MemberOffset Heap::finalizer_reference_zombie_offset_ = MemberOffset(0);
Brian Carlstrom1f870082011-08-23 16:02:11 -070039
Elliott Hughes92b3b562011-09-08 16:32:26 -070040Mutex* Heap::lock_ = NULL;
41
42class ScopedHeapLock {
43 public:
44 ScopedHeapLock() {
45 Heap::Lock();
46 }
47
48 ~ScopedHeapLock() {
49 Heap::Unlock();
50 }
51};
52
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070053bool Heap::Init(size_t initial_size, size_t maximum_size,
54 const char* boot_image_file_name,
55 std::vector<const char*>& image_file_names) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070056 Space* boot_space;
57 byte* requested_base;
58 if (boot_image_file_name == NULL) {
59 boot_space = NULL;
60 requested_base = NULL;
61 } else {
62 boot_space = Space::Create(boot_image_file_name);
63 if (boot_space == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070064 LOG(WARNING) << "Failed to create space from " << boot_image_file_name;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070065 return false;
66 }
67 spaces_.push_back(boot_space);
68 requested_base = boot_space->GetBase() + RoundUp(boot_space->Size(), kPageSize);
69 }
70
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070071 std::vector<Space*> image_spaces;
72 for (size_t i = 0; i < image_file_names.size(); i++) {
73 Space* space = Space::Create(image_file_names[i]);
74 if (space == NULL) {
75 LOG(WARNING) << "Failed to create space from " << image_file_names[i];
76 return false;
77 }
78 image_spaces.push_back(space);
79 spaces_.push_back(space);
80 requested_base = space->GetBase() + RoundUp(space->Size(), kPageSize);
81 }
82
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070083 Space* space = Space::Create(initial_size, maximum_size, requested_base);
Carl Shapiro58551df2011-07-24 03:09:51 -070084 if (space == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070085 LOG(WARNING) << "Failed to create alloc space";
Carl Shapiro69759ea2011-07-21 18:13:35 -070086 return false;
87 }
88
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070089 if (boot_space == NULL) {
90 boot_space = space;
91 }
92 byte* base = std::min(boot_space->GetBase(), space->GetBase());
93 byte* limit = std::max(boot_space->GetLimit(), space->GetLimit());
94 DCHECK_LT(base, limit);
95 size_t num_bytes = limit - base;
Carl Shapiro69759ea2011-07-21 18:13:35 -070096
97 // Allocate the initial live bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -070098 UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes));
99 if (live_bitmap.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700100 LOG(WARNING) << "Failed to create live bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700101 return false;
102 }
103
104 // Allocate the initial mark bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -0700105 UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes));
106 if (mark_bitmap.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700107 LOG(WARNING) << "Failed to create mark bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700108 return false;
109 }
110
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700111 alloc_space_ = space;
Carl Shapiro58551df2011-07-24 03:09:51 -0700112 spaces_.push_back(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700113 maximum_size_ = maximum_size;
114 live_bitmap_ = live_bitmap.release();
115 mark_bitmap_ = mark_bitmap.release();
116
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117 num_bytes_allocated_ = 0;
118 num_objects_allocated_ = 0;
119
Carl Shapiro69759ea2011-07-21 18:13:35 -0700120 // TODO: allocate the card table
121
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700122 // Make objects in boot_space live (after live_bitmap_ is set)
123 if (boot_image_file_name != NULL) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700124 boot_space_ = boot_space;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700125 RecordImageAllocations(boot_space);
126 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700127 for (size_t i = 0; i < image_spaces.size(); i++) {
128 RecordImageAllocations(image_spaces[i]);
129 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700130
Elliott Hughes92b3b562011-09-08 16:32:26 -0700131 // It's still to early to take a lock because there are no threads yet,
132 // but we can create the heap lock now. We don't create it earlier to
133 // make it clear that you can't use locks during heap initialization.
134 lock_ = Mutex::Create("Heap lock");
135
Carl Shapiro69759ea2011-07-21 18:13:35 -0700136 return true;
137}
138
139void Heap::Destroy() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700140 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700141 STLDeleteElements(&spaces_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700142 if (mark_bitmap_ != NULL) {
143 delete mark_bitmap_;
144 mark_bitmap_ = NULL;
145 }
146 if (live_bitmap_ != NULL) {
147 delete live_bitmap_;
148 }
149 live_bitmap_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150}
151
Carl Shapiro58551df2011-07-24 03:09:51 -0700152Object* Heap::AllocObject(Class* klass, size_t num_bytes) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700153 ScopedHeapLock lock;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700154 DCHECK(klass == NULL
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 || klass->GetDescriptor() == NULL
Brian Carlstrom4873d462011-08-21 15:23:39 -0700156 || (klass->IsClassClass() && num_bytes >= sizeof(Class))
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157 || (klass->IsVariableSize() || klass->GetObjectSize() == num_bytes));
158 DCHECK(num_bytes >= sizeof(Object));
Elliott Hughes92b3b562011-09-08 16:32:26 -0700159 Object* obj = AllocateLocked(num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700160 if (obj != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161 obj->SetClass(klass);
Carl Shapiro58551df2011-07-24 03:09:51 -0700162 }
163 return obj;
164}
165
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700166bool Heap::IsHeapAddress(const Object* obj) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700167 // Note: we deliberately don't take the lock here, and mustn't test anything that would
168 // require taking the lock.
Elliott Hughesa2501992011-08-26 19:39:54 -0700169 if (!IsAligned(obj, kObjectAlignment)) {
170 return false;
171 }
172 // TODO
173 return true;
174}
175
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700176bool Heap::verify_object_disabled_;
177
Elliott Hughes3e465b12011-09-02 18:26:12 -0700178#if VERIFY_OBJECT_ENABLED
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700179void Heap::VerifyObject(const Object* obj) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700180 ScopedHeapLock lock;
181 Heap::VerifyObjectLocked(obj);
182}
183#endif
184
185void Heap::VerifyObjectLocked(const Object* obj) {
186 DCHECK_LOCK_HELD(lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 if (obj != NULL && !verify_object_disabled_) {
188 if (!IsAligned(obj, kObjectAlignment)) {
189 LOG(FATAL) << "Object isn't aligned: " << obj;
190 } else if (!live_bitmap_->Test(obj)) {
191 // TODO: we don't hold a lock here as it is assumed the live bit map
192 // isn't changing if the mutator is running.
193 LOG(FATAL) << "Object is dead: " << obj;
194 }
195 // Ignore early dawn of the universe verifications
196 if(num_objects_allocated_ > 10) {
197 const byte* raw_addr = reinterpret_cast<const byte*>(obj) +
198 Object::ClassOffset().Int32Value();
199 const Class* c = *reinterpret_cast<Class* const *>(raw_addr);
200 if (c == NULL) {
201 LOG(FATAL) << "Null class" << " in object: " << obj;
202 } else if (!IsAligned(c, kObjectAlignment)) {
203 LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj;
204 } else if (!live_bitmap_->Test(c)) {
205 LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj;
206 }
207 // Check obj.getClass().getClass() == obj.getClass().getClass().getClass()
208 // NB we don't use the accessors here as they have internal sanity checks
209 // that we don't want to run
210 raw_addr = reinterpret_cast<const byte*>(c) +
211 Object::ClassOffset().Int32Value();
212 const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr);
213 raw_addr = reinterpret_cast<const byte*>(c_c) +
214 Object::ClassOffset().Int32Value();
215 const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr);
216 CHECK_EQ(c_c, c_c_c);
217 }
218 }
219}
220
Elliott Hughes92b3b562011-09-08 16:32:26 -0700221void Heap::VerificationCallback(Object* obj, void *arg) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700222 DCHECK(obj != NULL);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700223 Heap::VerifyObjectLocked(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224}
225
226void Heap::VerifyHeap() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700227 ScopedHeapLock lock;
228 live_bitmap_->Walk(Heap::VerificationCallback, NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700229}
230
Elliott Hughes92b3b562011-09-08 16:32:26 -0700231void Heap::RecordAllocationLocked(Space* space, const Object* obj) {
232#ifndef NDEBUG
233 if (Runtime::Current()->IsStarted()) {
234 DCHECK_LOCK_HELD(lock_);
235 }
236#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700237 size_t size = space->AllocationSize(obj);
238 DCHECK_NE(size, 0u);
239 num_bytes_allocated_ += size;
240 num_objects_allocated_ += 1;
241 live_bitmap_->Set(obj);
242}
243
Elliott Hughes92b3b562011-09-08 16:32:26 -0700244void Heap::RecordFreeLocked(Space* space, const Object* obj) {
245 DCHECK_LOCK_HELD(lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700246 size_t size = space->AllocationSize(obj);
247 DCHECK_NE(size, 0u);
248 if (size < num_bytes_allocated_) {
249 num_bytes_allocated_ -= size;
250 } else {
251 num_bytes_allocated_ = 0;
252 }
253 live_bitmap_->Clear(obj);
254 if (num_objects_allocated_ > 0) {
255 num_objects_allocated_ -= 1;
256 }
257}
258
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700259void Heap::RecordImageAllocations(Space* space) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700260 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700261 CHECK(space != NULL);
262 CHECK(live_bitmap_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700263 byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700264 while (current < space->GetLimit()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265 DCHECK(IsAligned(current, kObjectAlignment));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700266 const Object* obj = reinterpret_cast<const Object*>(current);
267 live_bitmap_->Set(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700268 current += RoundUp(obj->SizeOf(), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700269 }
270}
271
Elliott Hughes92b3b562011-09-08 16:32:26 -0700272Object* Heap::AllocateLocked(size_t size) {
273 DCHECK_LOCK_HELD(lock_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700274 DCHECK(alloc_space_ != NULL);
275 Space* space = alloc_space_;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700276 Object* obj = AllocateLocked(space, size);
Carl Shapiro58551df2011-07-24 03:09:51 -0700277 if (obj != NULL) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700278 RecordAllocationLocked(space, obj);
Carl Shapiro58551df2011-07-24 03:09:51 -0700279 }
280 return obj;
281}
282
Elliott Hughes92b3b562011-09-08 16:32:26 -0700283Object* Heap::AllocateLocked(Space* space, size_t size) {
284 DCHECK_LOCK_HELD(lock_);
285
Carl Shapiro69759ea2011-07-21 18:13:35 -0700286 // Fail impossible allocations. TODO: collect soft references.
287 if (size > maximum_size_) {
288 return NULL;
289 }
290
Carl Shapiro58551df2011-07-24 03:09:51 -0700291 Object* ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700292 if (ptr != NULL) {
293 return ptr;
294 }
295
296 // The allocation failed. If the GC is running, block until it
297 // completes and retry.
298 if (is_gc_running_) {
299 // The GC is concurrently tracing the heap. Release the heap
300 // lock, wait for the GC to complete, and retrying allocating.
301 WaitForConcurrentGcToComplete();
Carl Shapiro58551df2011-07-24 03:09:51 -0700302 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700303 if (ptr != NULL) {
304 return ptr;
305 }
306 }
307
308 // Another failure. Our thread was starved or there may be too many
309 // live objects. Try a foreground GC. This will have no effect if
310 // the concurrent GC is already running.
Carl Shapiro58551df2011-07-24 03:09:51 -0700311 CollectGarbageInternal();
312 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700313 if (ptr != NULL) {
314 return ptr;
315 }
316
317 // Even that didn't work; this is an exceptional state.
318 // Try harder, growing the heap if necessary.
Carl Shapiro58551df2011-07-24 03:09:51 -0700319 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700320 if (ptr != NULL) {
321 //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
Carl Shapiro58551df2011-07-24 03:09:51 -0700322 size_t new_footprint = space->MaxAllowedFootprint();
323 // TODO: may want to grow a little bit more so that the amount of
324 // free space is equal to the old free space + the
325 // utilization slop for the new allocation.
326 LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB
Carl Shapiro69759ea2011-07-21 18:13:35 -0700327 << "for " << size << "-byte allocation";
328 return ptr;
329 }
330
331 // Most allocations should have succeeded by now, so the heap is
332 // really full, really fragmented, or the requested size is really
333 // big. Do another GC, collecting SoftReferences this time. The VM
334 // spec requires that all SoftReferences have been collected and
335 // cleared before throwing an OOME.
336
Carl Shapiro58551df2011-07-24 03:09:51 -0700337 // TODO: wait for the finalizers from the previous GC to finish
Carl Shapiro69759ea2011-07-21 18:13:35 -0700338 LOG(INFO) << "Forcing collection of SoftReferences for "
339 << size << "-byte allocation";
Carl Shapiro58551df2011-07-24 03:09:51 -0700340 CollectGarbageInternal();
341 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700342 if (ptr != NULL) {
343 return ptr;
344 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700345
Carl Shapiro69759ea2011-07-21 18:13:35 -0700346 LOG(ERROR) << "Out of memory on a " << size << " byte allocation";
347
Carl Shapiro58551df2011-07-24 03:09:51 -0700348 // TODO: tell the HeapSource to dump its state
349 // TODO: dump stack traces for all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700350
Carl Shapiro69759ea2011-07-21 18:13:35 -0700351 return NULL;
352}
353
Elliott Hughesbf86d042011-08-31 17:53:14 -0700354int64_t Heap::GetMaxMemory() {
355 UNIMPLEMENTED(WARNING);
356 return 0;
357}
358
359int64_t Heap::GetTotalMemory() {
360 UNIMPLEMENTED(WARNING);
361 return 0;
362}
363
364int64_t Heap::GetFreeMemory() {
365 UNIMPLEMENTED(WARNING);
366 return 0;
367}
368
Carl Shapiro69759ea2011-07-21 18:13:35 -0700369void Heap::CollectGarbage() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700370 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700371 CollectGarbageInternal();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700372}
373
374void Heap::CollectGarbageInternal() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700375 DCHECK_LOCK_HELD(lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700376
377 // TODO: Suspend all threads
378 {
379 MarkSweep mark_sweep;
380
381 mark_sweep.Init();
382
383 mark_sweep.MarkRoots();
384
385 // Push marked roots onto the mark stack
386
387 // TODO: if concurrent
388 // unlock heap
389 // resume threads
390
391 mark_sweep.RecursiveMark();
392
393 // TODO: if concurrent
394 // lock heap
395 // suspend threads
396 // re-mark root set
397 // scan dirty objects
398
399 mark_sweep.ProcessReferences(false);
400
401 // TODO: swap bitmaps
402
403 mark_sweep.Sweep();
404 }
405
406 GrowForUtilization();
407
408 // TODO: Resume all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700409}
410
411void Heap::WaitForConcurrentGcToComplete() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700412 DCHECK_LOCK_HELD(lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700413}
414
415// Given the current contents of the active heap, increase the allowed
416// heap footprint to match the target utilization ratio. This should
417// only be called immediately after a full garbage collection.
418void Heap::GrowForUtilization() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700419 DCHECK_LOCK_HELD(lock_);
Elliott Hughes53b61312011-08-12 18:28:20 -0700420 UNIMPLEMENTED(ERROR);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700421}
422
Elliott Hughes92b3b562011-09-08 16:32:26 -0700423void Heap::Lock() {
424 // TODO: grab the lock, but put ourselves into THREAD_VMWAIT if it looks like
425 // we're going to have to wait on the mutex.
426 lock_->Lock();
427}
428
429void Heap::Unlock() {
430 lock_->Unlock();
431}
432
Carl Shapiro69759ea2011-07-21 18:13:35 -0700433} // namespace art