Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "heap.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 4 | |
| 5 | #include <vector> |
| 6 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 7 | #include "image.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 8 | #include "mark_sweep.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "object.h" |
| 10 | #include "space.h" |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 11 | #include "scoped_ptr.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 12 | #include "stl_util.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 13 | |
| 14 | namespace art { |
| 15 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 16 | std::vector<Space*> Heap::spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 17 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 18 | Space* Heap::alloc_space_ = NULL; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 19 | |
| 20 | size_t Heap::maximum_size_ = 0; |
| 21 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 22 | size_t Heap::num_bytes_allocated_ = 0; |
| 23 | |
| 24 | size_t Heap::num_objects_allocated_ = 0; |
| 25 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 26 | bool Heap::is_gc_running_ = false; |
| 27 | |
| 28 | HeapBitmap* Heap::mark_bitmap_ = NULL; |
| 29 | |
| 30 | HeapBitmap* Heap::live_bitmap_ = NULL; |
| 31 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 32 | bool Heap::Init(size_t initial_size, size_t maximum_size, const char* boot_image_file_name) { |
| 33 | Space* boot_space; |
| 34 | byte* requested_base; |
| 35 | if (boot_image_file_name == NULL) { |
| 36 | boot_space = NULL; |
| 37 | requested_base = NULL; |
| 38 | } else { |
| 39 | boot_space = Space::Create(boot_image_file_name); |
| 40 | if (boot_space == NULL) { |
| 41 | return false; |
| 42 | } |
| 43 | spaces_.push_back(boot_space); |
| 44 | requested_base = boot_space->GetBase() + RoundUp(boot_space->Size(), kPageSize); |
| 45 | } |
| 46 | |
| 47 | Space* space = Space::Create(initial_size, maximum_size, requested_base); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 48 | if (space == NULL) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 52 | if (boot_space == NULL) { |
| 53 | boot_space = space; |
| 54 | } |
| 55 | byte* base = std::min(boot_space->GetBase(), space->GetBase()); |
| 56 | byte* limit = std::max(boot_space->GetLimit(), space->GetLimit()); |
| 57 | DCHECK_LT(base, limit); |
| 58 | size_t num_bytes = limit - base; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 59 | |
| 60 | // Allocate the initial live bitmap. |
| 61 | scoped_ptr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes)); |
| 62 | if (live_bitmap == NULL) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // Allocate the initial mark bitmap. |
| 67 | scoped_ptr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes)); |
| 68 | if (mark_bitmap == NULL) { |
| 69 | return false; |
| 70 | } |
| 71 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 72 | alloc_space_ = space; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 73 | spaces_.push_back(space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 74 | maximum_size_ = maximum_size; |
| 75 | live_bitmap_ = live_bitmap.release(); |
| 76 | mark_bitmap_ = mark_bitmap.release(); |
| 77 | |
| 78 | // TODO: allocate the card table |
| 79 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 80 | // Make objects in boot_space live (after live_bitmap_ is set) |
| 81 | if (boot_image_file_name != NULL) { |
| 82 | RecordImageAllocations(boot_space); |
| 83 | } |
| 84 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
| 88 | void Heap::Destroy() { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 89 | STLDeleteElements(&spaces_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 90 | if (mark_bitmap_ != NULL) { |
| 91 | delete mark_bitmap_; |
| 92 | mark_bitmap_ = NULL; |
| 93 | } |
| 94 | if (live_bitmap_ != NULL) { |
| 95 | delete live_bitmap_; |
| 96 | } |
| 97 | live_bitmap_ = NULL; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 100 | Object* Heap::AllocObject(Class* klass, size_t num_bytes) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 101 | DCHECK(klass == NULL |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 102 | || klass->descriptor_ == NULL |
| 103 | || (klass->object_size_ == (klass->IsArray() ? 0 : num_bytes))); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 104 | Object* obj = Allocate(num_bytes); |
| 105 | if (obj != NULL) { |
| 106 | obj->klass_ = klass; |
| 107 | } |
| 108 | return obj; |
| 109 | } |
| 110 | |
| 111 | void Heap::RecordAllocation(Space* space, const Object* obj) { |
| 112 | size_t size = space->AllocationSize(obj); |
| 113 | DCHECK_NE(size, 0u); |
| 114 | num_bytes_allocated_ += size; |
| 115 | num_objects_allocated_ += 1; |
| 116 | live_bitmap_->Set(obj); |
| 117 | } |
| 118 | |
| 119 | void Heap::RecordFree(Space* space, const Object* obj) { |
| 120 | size_t size = space->AllocationSize(obj); |
| 121 | DCHECK_NE(size, 0u); |
| 122 | if (size < num_bytes_allocated_) { |
| 123 | num_bytes_allocated_ -= size; |
| 124 | } else { |
| 125 | num_bytes_allocated_ = 0; |
| 126 | } |
| 127 | live_bitmap_->Clear(obj); |
| 128 | if (num_objects_allocated_ > 0) { |
| 129 | num_objects_allocated_ -= 1; |
| 130 | } |
| 131 | } |
| 132 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 133 | void Heap::RecordImageAllocations(Space* space) { |
| 134 | CHECK(space != NULL); |
| 135 | CHECK(live_bitmap_ != NULL); |
| 136 | byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), 8); |
| 137 | while (current < space->GetLimit()) { |
| 138 | DCHECK(IsAligned(current, 8)); |
| 139 | const Object* obj = reinterpret_cast<const Object*>(current); |
| 140 | live_bitmap_->Set(obj); |
| 141 | current += RoundUp(obj->SizeOf(), 8); |
| 142 | } |
| 143 | } |
| 144 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 145 | Object* Heap::Allocate(size_t size) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 146 | DCHECK(alloc_space_ != NULL); |
| 147 | Space* space = alloc_space_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 148 | Object* obj = Allocate(space, size); |
| 149 | if (obj != NULL) { |
| 150 | RecordAllocation(space, obj); |
| 151 | } |
| 152 | return obj; |
| 153 | } |
| 154 | |
| 155 | Object* Heap::Allocate(Space* space, size_t size) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 156 | // Fail impossible allocations. TODO: collect soft references. |
| 157 | if (size > maximum_size_) { |
| 158 | return NULL; |
| 159 | } |
| 160 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 161 | Object* ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 162 | if (ptr != NULL) { |
| 163 | return ptr; |
| 164 | } |
| 165 | |
| 166 | // The allocation failed. If the GC is running, block until it |
| 167 | // completes and retry. |
| 168 | if (is_gc_running_) { |
| 169 | // The GC is concurrently tracing the heap. Release the heap |
| 170 | // lock, wait for the GC to complete, and retrying allocating. |
| 171 | WaitForConcurrentGcToComplete(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 172 | ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 173 | if (ptr != NULL) { |
| 174 | return ptr; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Another failure. Our thread was starved or there may be too many |
| 179 | // live objects. Try a foreground GC. This will have no effect if |
| 180 | // the concurrent GC is already running. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 181 | CollectGarbageInternal(); |
| 182 | ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 183 | if (ptr != NULL) { |
| 184 | return ptr; |
| 185 | } |
| 186 | |
| 187 | // Even that didn't work; this is an exceptional state. |
| 188 | // Try harder, growing the heap if necessary. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 189 | ptr = space->AllocWithGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 190 | if (ptr != NULL) { |
| 191 | //size_t new_footprint = dvmHeapSourceGetIdealFootprint(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 192 | size_t new_footprint = space->MaxAllowedFootprint(); |
| 193 | // TODO: may want to grow a little bit more so that the amount of |
| 194 | // free space is equal to the old free space + the |
| 195 | // utilization slop for the new allocation. |
| 196 | LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 197 | << "for " << size << "-byte allocation"; |
| 198 | return ptr; |
| 199 | } |
| 200 | |
| 201 | // Most allocations should have succeeded by now, so the heap is |
| 202 | // really full, really fragmented, or the requested size is really |
| 203 | // big. Do another GC, collecting SoftReferences this time. The VM |
| 204 | // spec requires that all SoftReferences have been collected and |
| 205 | // cleared before throwing an OOME. |
| 206 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 207 | // TODO: wait for the finalizers from the previous GC to finish |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 208 | LOG(INFO) << "Forcing collection of SoftReferences for " |
| 209 | << size << "-byte allocation"; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 210 | CollectGarbageInternal(); |
| 211 | ptr = space->AllocWithGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 212 | if (ptr != NULL) { |
| 213 | return ptr; |
| 214 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 215 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 216 | LOG(ERROR) << "Out of memory on a " << size << " byte allocation"; |
| 217 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 218 | // TODO: tell the HeapSource to dump its state |
| 219 | // TODO: dump stack traces for all threads |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 220 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 224 | void Heap::CollectGarbage() { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 225 | CollectGarbageInternal(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void Heap::CollectGarbageInternal() { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 229 | // TODO: check that heap lock is held |
| 230 | |
| 231 | // TODO: Suspend all threads |
| 232 | { |
| 233 | MarkSweep mark_sweep; |
| 234 | |
| 235 | mark_sweep.Init(); |
| 236 | |
| 237 | mark_sweep.MarkRoots(); |
| 238 | |
| 239 | // Push marked roots onto the mark stack |
| 240 | |
| 241 | // TODO: if concurrent |
| 242 | // unlock heap |
| 243 | // resume threads |
| 244 | |
| 245 | mark_sweep.RecursiveMark(); |
| 246 | |
| 247 | // TODO: if concurrent |
| 248 | // lock heap |
| 249 | // suspend threads |
| 250 | // re-mark root set |
| 251 | // scan dirty objects |
| 252 | |
| 253 | mark_sweep.ProcessReferences(false); |
| 254 | |
| 255 | // TODO: swap bitmaps |
| 256 | |
| 257 | mark_sweep.Sweep(); |
| 258 | } |
| 259 | |
| 260 | GrowForUtilization(); |
| 261 | |
| 262 | // TODO: Resume all threads |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void Heap::WaitForConcurrentGcToComplete() { |
| 266 | } |
| 267 | |
| 268 | // Given the current contents of the active heap, increase the allowed |
| 269 | // heap footprint to match the target utilization ratio. This should |
| 270 | // only be called immediately after a full garbage collection. |
| 271 | void Heap::GrowForUtilization() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 272 | UNIMPLEMENTED(ERROR); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | } // namespace art |