blob: f86f5562dff5c2a6d304a3e214d2b89a1fdca2b7 [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
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07007#include "image.h"
Carl Shapiro58551df2011-07-24 03:09:51 -07008#include "mark_sweep.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "object.h"
10#include "space.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070011#include "scoped_ptr.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
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070034bool Heap::Init(size_t initial_size, size_t maximum_size, const char* boot_image_file_name) {
35 Space* boot_space;
36 byte* requested_base;
37 if (boot_image_file_name == NULL) {
38 boot_space = NULL;
39 requested_base = NULL;
40 } else {
41 boot_space = Space::Create(boot_image_file_name);
42 if (boot_space == NULL) {
43 return false;
44 }
45 spaces_.push_back(boot_space);
46 requested_base = boot_space->GetBase() + RoundUp(boot_space->Size(), kPageSize);
47 }
48
49 Space* space = Space::Create(initial_size, maximum_size, requested_base);
Carl Shapiro58551df2011-07-24 03:09:51 -070050 if (space == NULL) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070051 return false;
52 }
53
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070054 if (boot_space == NULL) {
55 boot_space = space;
56 }
57 byte* base = std::min(boot_space->GetBase(), space->GetBase());
58 byte* limit = std::max(boot_space->GetLimit(), space->GetLimit());
59 DCHECK_LT(base, limit);
60 size_t num_bytes = limit - base;
Carl Shapiro69759ea2011-07-21 18:13:35 -070061
62 // Allocate the initial live bitmap.
63 scoped_ptr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes));
64 if (live_bitmap == NULL) {
65 return false;
66 }
67
68 // Allocate the initial mark bitmap.
69 scoped_ptr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes));
70 if (mark_bitmap == NULL) {
71 return false;
72 }
73
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070074 alloc_space_ = space;
Carl Shapiro58551df2011-07-24 03:09:51 -070075 spaces_.push_back(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -070076 maximum_size_ = maximum_size;
77 live_bitmap_ = live_bitmap.release();
78 mark_bitmap_ = mark_bitmap.release();
79
80 // TODO: allocate the card table
81
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070082 // Make objects in boot_space live (after live_bitmap_ is set)
83 if (boot_image_file_name != NULL) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070084 boot_space_ = boot_space;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070085 RecordImageAllocations(boot_space);
86 }
87
Carl Shapiro69759ea2011-07-21 18:13:35 -070088 return true;
89}
90
91void Heap::Destroy() {
Carl Shapiro58551df2011-07-24 03:09:51 -070092 STLDeleteElements(&spaces_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070093 if (mark_bitmap_ != NULL) {
94 delete mark_bitmap_;
95 mark_bitmap_ = NULL;
96 }
97 if (live_bitmap_ != NULL) {
98 delete live_bitmap_;
99 }
100 live_bitmap_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700101}
102
Carl Shapiro58551df2011-07-24 03:09:51 -0700103Object* Heap::AllocObject(Class* klass, size_t num_bytes) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700104 DCHECK(klass == NULL
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700105 || klass->descriptor_ == NULL
Brian Carlstrom4873d462011-08-21 15:23:39 -0700106 || (klass->IsClassClass() && num_bytes >= sizeof(Class))
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700107 || (klass->object_size_ == (klass->IsArray() ? 0 : num_bytes)));
Carl Shapiro58551df2011-07-24 03:09:51 -0700108 Object* obj = Allocate(num_bytes);
109 if (obj != NULL) {
110 obj->klass_ = klass;
111 }
112 return obj;
113}
114
115void Heap::RecordAllocation(Space* space, const Object* obj) {
116 size_t size = space->AllocationSize(obj);
117 DCHECK_NE(size, 0u);
118 num_bytes_allocated_ += size;
119 num_objects_allocated_ += 1;
120 live_bitmap_->Set(obj);
121}
122
123void Heap::RecordFree(Space* space, const Object* obj) {
124 size_t size = space->AllocationSize(obj);
125 DCHECK_NE(size, 0u);
126 if (size < num_bytes_allocated_) {
127 num_bytes_allocated_ -= size;
128 } else {
129 num_bytes_allocated_ = 0;
130 }
131 live_bitmap_->Clear(obj);
132 if (num_objects_allocated_ > 0) {
133 num_objects_allocated_ -= 1;
134 }
135}
136
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700137void Heap::RecordImageAllocations(Space* space) {
138 CHECK(space != NULL);
139 CHECK(live_bitmap_ != NULL);
140 byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), 8);
141 while (current < space->GetLimit()) {
142 DCHECK(IsAligned(current, 8));
143 const Object* obj = reinterpret_cast<const Object*>(current);
144 live_bitmap_->Set(obj);
145 current += RoundUp(obj->SizeOf(), 8);
146 }
147}
148
Carl Shapiro69759ea2011-07-21 18:13:35 -0700149Object* Heap::Allocate(size_t size) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700150 DCHECK(alloc_space_ != NULL);
151 Space* space = alloc_space_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700152 Object* obj = Allocate(space, size);
153 if (obj != NULL) {
154 RecordAllocation(space, obj);
155 }
156 return obj;
157}
158
159Object* Heap::Allocate(Space* space, size_t size) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700160 // Fail impossible allocations. TODO: collect soft references.
161 if (size > maximum_size_) {
162 return NULL;
163 }
164
Carl Shapiro58551df2011-07-24 03:09:51 -0700165 Object* ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166 if (ptr != NULL) {
167 return ptr;
168 }
169
170 // The allocation failed. If the GC is running, block until it
171 // completes and retry.
172 if (is_gc_running_) {
173 // The GC is concurrently tracing the heap. Release the heap
174 // lock, wait for the GC to complete, and retrying allocating.
175 WaitForConcurrentGcToComplete();
Carl Shapiro58551df2011-07-24 03:09:51 -0700176 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700177 if (ptr != NULL) {
178 return ptr;
179 }
180 }
181
182 // Another failure. Our thread was starved or there may be too many
183 // live objects. Try a foreground GC. This will have no effect if
184 // the concurrent GC is already running.
Carl Shapiro58551df2011-07-24 03:09:51 -0700185 CollectGarbageInternal();
186 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700187 if (ptr != NULL) {
188 return ptr;
189 }
190
191 // Even that didn't work; this is an exceptional state.
192 // Try harder, growing the heap if necessary.
Carl Shapiro58551df2011-07-24 03:09:51 -0700193 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700194 if (ptr != NULL) {
195 //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
Carl Shapiro58551df2011-07-24 03:09:51 -0700196 size_t new_footprint = space->MaxAllowedFootprint();
197 // TODO: may want to grow a little bit more so that the amount of
198 // free space is equal to the old free space + the
199 // utilization slop for the new allocation.
200 LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201 << "for " << size << "-byte allocation";
202 return ptr;
203 }
204
205 // Most allocations should have succeeded by now, so the heap is
206 // really full, really fragmented, or the requested size is really
207 // big. Do another GC, collecting SoftReferences this time. The VM
208 // spec requires that all SoftReferences have been collected and
209 // cleared before throwing an OOME.
210
Carl Shapiro58551df2011-07-24 03:09:51 -0700211 // TODO: wait for the finalizers from the previous GC to finish
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 LOG(INFO) << "Forcing collection of SoftReferences for "
213 << size << "-byte allocation";
Carl Shapiro58551df2011-07-24 03:09:51 -0700214 CollectGarbageInternal();
215 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216 if (ptr != NULL) {
217 return ptr;
218 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700219
Carl Shapiro69759ea2011-07-21 18:13:35 -0700220 LOG(ERROR) << "Out of memory on a " << size << " byte allocation";
221
Carl Shapiro58551df2011-07-24 03:09:51 -0700222 // TODO: tell the HeapSource to dump its state
223 // TODO: dump stack traces for all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700224
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225 return NULL;
226}
227
Carl Shapiro69759ea2011-07-21 18:13:35 -0700228void Heap::CollectGarbage() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700229 CollectGarbageInternal();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700230}
231
232void Heap::CollectGarbageInternal() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700233 // TODO: check that heap lock is held
234
235 // TODO: Suspend all threads
236 {
237 MarkSweep mark_sweep;
238
239 mark_sweep.Init();
240
241 mark_sweep.MarkRoots();
242
243 // Push marked roots onto the mark stack
244
245 // TODO: if concurrent
246 // unlock heap
247 // resume threads
248
249 mark_sweep.RecursiveMark();
250
251 // TODO: if concurrent
252 // lock heap
253 // suspend threads
254 // re-mark root set
255 // scan dirty objects
256
257 mark_sweep.ProcessReferences(false);
258
259 // TODO: swap bitmaps
260
261 mark_sweep.Sweep();
262 }
263
264 GrowForUtilization();
265
266 // TODO: Resume all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700267}
268
269void Heap::WaitForConcurrentGcToComplete() {
270}
271
272// Given the current contents of the active heap, increase the allowed
273// heap footprint to match the target utilization ratio. This should
274// only be called immediately after a full garbage collection.
275void Heap::GrowForUtilization() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700276 UNIMPLEMENTED(ERROR);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700277}
278
279} // namespace art