blob: cb5e0dc74879f9aea18fefe455791212de2754d9 [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
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070040bool Heap::Init(size_t initial_size, size_t maximum_size, const char* boot_image_file_name) {
41 Space* boot_space;
42 byte* requested_base;
43 if (boot_image_file_name == NULL) {
44 boot_space = NULL;
45 requested_base = NULL;
46 } else {
47 boot_space = Space::Create(boot_image_file_name);
48 if (boot_space == NULL) {
49 return false;
50 }
51 spaces_.push_back(boot_space);
52 requested_base = boot_space->GetBase() + RoundUp(boot_space->Size(), kPageSize);
53 }
54
55 Space* space = Space::Create(initial_size, maximum_size, requested_base);
Carl Shapiro58551df2011-07-24 03:09:51 -070056 if (space == NULL) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070057 return false;
58 }
59
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070060 if (boot_space == NULL) {
61 boot_space = space;
62 }
63 byte* base = std::min(boot_space->GetBase(), space->GetBase());
64 byte* limit = std::max(boot_space->GetLimit(), space->GetLimit());
65 DCHECK_LT(base, limit);
66 size_t num_bytes = limit - base;
Carl Shapiro69759ea2011-07-21 18:13:35 -070067
68 // Allocate the initial live bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -070069 UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes));
70 if (live_bitmap.get() == NULL) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070071 return false;
72 }
73
74 // Allocate the initial mark bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -070075 UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes));
76 if (mark_bitmap.get() == NULL) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070077 return false;
78 }
79
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070080 alloc_space_ = space;
Carl Shapiro58551df2011-07-24 03:09:51 -070081 spaces_.push_back(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -070082 maximum_size_ = maximum_size;
83 live_bitmap_ = live_bitmap.release();
84 mark_bitmap_ = mark_bitmap.release();
85
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070086 num_bytes_allocated_ = 0;
87 num_objects_allocated_ = 0;
88
Carl Shapiro69759ea2011-07-21 18:13:35 -070089 // TODO: allocate the card table
90
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070091 // Make objects in boot_space live (after live_bitmap_ is set)
92 if (boot_image_file_name != NULL) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070093 boot_space_ = boot_space;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070094 RecordImageAllocations(boot_space);
95 }
96
Carl Shapiro69759ea2011-07-21 18:13:35 -070097 return true;
98}
99
100void Heap::Destroy() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700101 STLDeleteElements(&spaces_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700102 if (mark_bitmap_ != NULL) {
103 delete mark_bitmap_;
104 mark_bitmap_ = NULL;
105 }
106 if (live_bitmap_ != NULL) {
107 delete live_bitmap_;
108 }
109 live_bitmap_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700110}
111
Carl Shapiro58551df2011-07-24 03:09:51 -0700112Object* Heap::AllocObject(Class* klass, size_t num_bytes) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700113 DCHECK(klass == NULL
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700114 || klass->GetDescriptor() == NULL
Brian Carlstrom4873d462011-08-21 15:23:39 -0700115 || (klass->IsClassClass() && num_bytes >= sizeof(Class))
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700116 || (klass->IsVariableSize() || klass->GetObjectSize() == num_bytes));
117 DCHECK(num_bytes >= sizeof(Object));
Carl Shapiro58551df2011-07-24 03:09:51 -0700118 Object* obj = Allocate(num_bytes);
119 if (obj != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700120 obj->SetClass(klass);
Carl Shapiro58551df2011-07-24 03:09:51 -0700121 }
122 return obj;
123}
124
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700125bool Heap::IsHeapAddress(const Object* obj) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700126 if (!IsAligned(obj, kObjectAlignment)) {
127 return false;
128 }
129 // TODO
130 return true;
131}
132
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700133bool Heap::verify_object_disabled_;
134
135void Heap::VerifyObject(const Object* obj) {
136 if (obj != NULL && !verify_object_disabled_) {
137 if (!IsAligned(obj, kObjectAlignment)) {
138 LOG(FATAL) << "Object isn't aligned: " << obj;
139 } else if (!live_bitmap_->Test(obj)) {
140 // TODO: we don't hold a lock here as it is assumed the live bit map
141 // isn't changing if the mutator is running.
142 LOG(FATAL) << "Object is dead: " << obj;
143 }
144 // Ignore early dawn of the universe verifications
145 if(num_objects_allocated_ > 10) {
146 const byte* raw_addr = reinterpret_cast<const byte*>(obj) +
147 Object::ClassOffset().Int32Value();
148 const Class* c = *reinterpret_cast<Class* const *>(raw_addr);
149 if (c == NULL) {
150 LOG(FATAL) << "Null class" << " in object: " << obj;
151 } else if (!IsAligned(c, kObjectAlignment)) {
152 LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj;
153 } else if (!live_bitmap_->Test(c)) {
154 LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj;
155 }
156 // Check obj.getClass().getClass() == obj.getClass().getClass().getClass()
157 // NB we don't use the accessors here as they have internal sanity checks
158 // that we don't want to run
159 raw_addr = reinterpret_cast<const byte*>(c) +
160 Object::ClassOffset().Int32Value();
161 const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr);
162 raw_addr = reinterpret_cast<const byte*>(c_c) +
163 Object::ClassOffset().Int32Value();
164 const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr);
165 CHECK_EQ(c_c, c_c_c);
166 }
167 }
168}
169
170static void HeapVerifyCallback(Object* obj, void *arg) {
171 DCHECK(obj != NULL);
172 Heap::VerifyObject(obj);
173}
174
175void Heap::VerifyHeap() {
176 live_bitmap_->Walk(HeapVerifyCallback, NULL);
177}
178
Carl Shapiro58551df2011-07-24 03:09:51 -0700179void Heap::RecordAllocation(Space* space, const Object* obj) {
180 size_t size = space->AllocationSize(obj);
181 DCHECK_NE(size, 0u);
182 num_bytes_allocated_ += size;
183 num_objects_allocated_ += 1;
184 live_bitmap_->Set(obj);
185}
186
187void Heap::RecordFree(Space* space, const Object* obj) {
188 size_t size = space->AllocationSize(obj);
189 DCHECK_NE(size, 0u);
190 if (size < num_bytes_allocated_) {
191 num_bytes_allocated_ -= size;
192 } else {
193 num_bytes_allocated_ = 0;
194 }
195 live_bitmap_->Clear(obj);
196 if (num_objects_allocated_ > 0) {
197 num_objects_allocated_ -= 1;
198 }
199}
200
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700201void Heap::RecordImageAllocations(Space* space) {
202 CHECK(space != NULL);
203 CHECK(live_bitmap_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700205 while (current < space->GetLimit()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 DCHECK(IsAligned(current, kObjectAlignment));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700207 const Object* obj = reinterpret_cast<const Object*>(current);
208 live_bitmap_->Set(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 current += RoundUp(obj->SizeOf(), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700210 }
211}
212
Carl Shapiro69759ea2011-07-21 18:13:35 -0700213Object* Heap::Allocate(size_t size) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700214 DCHECK(alloc_space_ != NULL);
215 Space* space = alloc_space_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700216 Object* obj = Allocate(space, size);
217 if (obj != NULL) {
218 RecordAllocation(space, obj);
219 }
220 return obj;
221}
222
223Object* Heap::Allocate(Space* space, size_t size) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700224 // Fail impossible allocations. TODO: collect soft references.
225 if (size > maximum_size_) {
226 return NULL;
227 }
228
Carl Shapiro58551df2011-07-24 03:09:51 -0700229 Object* ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700230 if (ptr != NULL) {
231 return ptr;
232 }
233
234 // The allocation failed. If the GC is running, block until it
235 // completes and retry.
236 if (is_gc_running_) {
237 // The GC is concurrently tracing the heap. Release the heap
238 // lock, wait for the GC to complete, and retrying allocating.
239 WaitForConcurrentGcToComplete();
Carl Shapiro58551df2011-07-24 03:09:51 -0700240 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700241 if (ptr != NULL) {
242 return ptr;
243 }
244 }
245
246 // Another failure. Our thread was starved or there may be too many
247 // live objects. Try a foreground GC. This will have no effect if
248 // the concurrent GC is already running.
Carl Shapiro58551df2011-07-24 03:09:51 -0700249 CollectGarbageInternal();
250 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251 if (ptr != NULL) {
252 return ptr;
253 }
254
255 // Even that didn't work; this is an exceptional state.
256 // Try harder, growing the heap if necessary.
Carl Shapiro58551df2011-07-24 03:09:51 -0700257 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700258 if (ptr != NULL) {
259 //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
Carl Shapiro58551df2011-07-24 03:09:51 -0700260 size_t new_footprint = space->MaxAllowedFootprint();
261 // TODO: may want to grow a little bit more so that the amount of
262 // free space is equal to the old free space + the
263 // utilization slop for the new allocation.
264 LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB
Carl Shapiro69759ea2011-07-21 18:13:35 -0700265 << "for " << size << "-byte allocation";
266 return ptr;
267 }
268
269 // Most allocations should have succeeded by now, so the heap is
270 // really full, really fragmented, or the requested size is really
271 // big. Do another GC, collecting SoftReferences this time. The VM
272 // spec requires that all SoftReferences have been collected and
273 // cleared before throwing an OOME.
274
Carl Shapiro58551df2011-07-24 03:09:51 -0700275 // TODO: wait for the finalizers from the previous GC to finish
Carl Shapiro69759ea2011-07-21 18:13:35 -0700276 LOG(INFO) << "Forcing collection of SoftReferences for "
277 << size << "-byte allocation";
Carl Shapiro58551df2011-07-24 03:09:51 -0700278 CollectGarbageInternal();
279 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700280 if (ptr != NULL) {
281 return ptr;
282 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700283
Carl Shapiro69759ea2011-07-21 18:13:35 -0700284 LOG(ERROR) << "Out of memory on a " << size << " byte allocation";
285
Carl Shapiro58551df2011-07-24 03:09:51 -0700286 // TODO: tell the HeapSource to dump its state
287 // TODO: dump stack traces for all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700288
Carl Shapiro69759ea2011-07-21 18:13:35 -0700289 return NULL;
290}
291
Elliott Hughesbf86d042011-08-31 17:53:14 -0700292int64_t Heap::GetMaxMemory() {
293 UNIMPLEMENTED(WARNING);
294 return 0;
295}
296
297int64_t Heap::GetTotalMemory() {
298 UNIMPLEMENTED(WARNING);
299 return 0;
300}
301
302int64_t Heap::GetFreeMemory() {
303 UNIMPLEMENTED(WARNING);
304 return 0;
305}
306
Carl Shapiro69759ea2011-07-21 18:13:35 -0700307void Heap::CollectGarbage() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700308 CollectGarbageInternal();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700309}
310
311void Heap::CollectGarbageInternal() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700312 // TODO: check that heap lock is held
313
314 // TODO: Suspend all threads
315 {
316 MarkSweep mark_sweep;
317
318 mark_sweep.Init();
319
320 mark_sweep.MarkRoots();
321
322 // Push marked roots onto the mark stack
323
324 // TODO: if concurrent
325 // unlock heap
326 // resume threads
327
328 mark_sweep.RecursiveMark();
329
330 // TODO: if concurrent
331 // lock heap
332 // suspend threads
333 // re-mark root set
334 // scan dirty objects
335
336 mark_sweep.ProcessReferences(false);
337
338 // TODO: swap bitmaps
339
340 mark_sweep.Sweep();
341 }
342
343 GrowForUtilization();
344
345 // TODO: Resume all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700346}
347
348void Heap::WaitForConcurrentGcToComplete() {
349}
350
351// Given the current contents of the active heap, increase the allowed
352// heap footprint to match the target utilization ratio. This should
353// only be called immediately after a full garbage collection.
354void Heap::GrowForUtilization() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700355 UNIMPLEMENTED(ERROR);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700356}
357
358} // namespace art