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 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 7 | #include "UniquePtr.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 8 | #include "image.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 9 | #include "mark_sweep.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "object.h" |
| 11 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 12 | #include "stl_util.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 13 | #include "thread_list.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 14 | |
| 15 | namespace art { |
| 16 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 17 | std::vector<Space*> Heap::spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 19 | Space* Heap::boot_space_ = NULL; |
| 20 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 21 | Space* Heap::alloc_space_ = NULL; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 22 | |
| 23 | size_t Heap::maximum_size_ = 0; |
| 24 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 25 | size_t Heap::num_bytes_allocated_ = 0; |
| 26 | |
| 27 | size_t Heap::num_objects_allocated_ = 0; |
| 28 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 29 | bool Heap::is_gc_running_ = false; |
| 30 | |
| 31 | HeapBitmap* Heap::mark_bitmap_ = NULL; |
| 32 | |
| 33 | HeapBitmap* Heap::live_bitmap_ = NULL; |
| 34 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 35 | MemberOffset Heap::reference_referent_offset_ = MemberOffset(0); |
| 36 | MemberOffset Heap::reference_queue_offset_ = MemberOffset(0); |
| 37 | MemberOffset Heap::reference_queueNext_offset_ = MemberOffset(0); |
| 38 | MemberOffset Heap::reference_pendingNext_offset_ = MemberOffset(0); |
| 39 | MemberOffset Heap::finalizer_reference_zombie_offset_ = MemberOffset(0); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 41 | Mutex* Heap::lock_ = NULL; |
| 42 | |
| 43 | class ScopedHeapLock { |
| 44 | public: |
| 45 | ScopedHeapLock() { |
| 46 | Heap::Lock(); |
| 47 | } |
| 48 | |
| 49 | ~ScopedHeapLock() { |
| 50 | Heap::Unlock(); |
| 51 | } |
| 52 | }; |
| 53 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 54 | void Heap::Init(size_t initial_size, size_t maximum_size, |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 55 | const char* boot_image_file_name, |
| 56 | std::vector<const char*>& image_file_names) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 57 | Space* boot_space; |
| 58 | byte* requested_base; |
| 59 | if (boot_image_file_name == NULL) { |
| 60 | boot_space = NULL; |
| 61 | requested_base = NULL; |
| 62 | } else { |
| 63 | boot_space = Space::Create(boot_image_file_name); |
| 64 | if (boot_space == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 65 | LOG(FATAL) << "Failed to create space from " << boot_image_file_name; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 66 | } |
| 67 | spaces_.push_back(boot_space); |
| 68 | requested_base = boot_space->GetBase() + RoundUp(boot_space->Size(), kPageSize); |
| 69 | } |
| 70 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 71 | 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) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 75 | LOG(FATAL) << "Failed to create space from " << image_file_names[i]; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 76 | } |
| 77 | image_spaces.push_back(space); |
| 78 | spaces_.push_back(space); |
| 79 | requested_base = space->GetBase() + RoundUp(space->Size(), kPageSize); |
| 80 | } |
| 81 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 82 | Space* space = Space::Create(initial_size, maximum_size, requested_base); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 83 | if (space == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 84 | LOG(FATAL) << "Failed to create alloc space"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 87 | if (boot_space == NULL) { |
| 88 | boot_space = space; |
| 89 | } |
| 90 | byte* base = std::min(boot_space->GetBase(), space->GetBase()); |
| 91 | byte* limit = std::max(boot_space->GetLimit(), space->GetLimit()); |
| 92 | DCHECK_LT(base, limit); |
| 93 | size_t num_bytes = limit - base; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 94 | |
| 95 | // Allocate the initial live bitmap. |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 96 | UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes)); |
| 97 | if (live_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 98 | LOG(FATAL) << "Failed to create live bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Allocate the initial mark bitmap. |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 102 | UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes)); |
| 103 | if (mark_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 104 | LOG(FATAL) << "Failed to create mark bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 107 | alloc_space_ = space; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 108 | spaces_.push_back(space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 109 | maximum_size_ = maximum_size; |
| 110 | live_bitmap_ = live_bitmap.release(); |
| 111 | mark_bitmap_ = mark_bitmap.release(); |
| 112 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 113 | num_bytes_allocated_ = 0; |
| 114 | num_objects_allocated_ = 0; |
| 115 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 116 | // TODO: allocate the card table |
| 117 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 118 | // Make objects in boot_space live (after live_bitmap_ is set) |
| 119 | if (boot_image_file_name != NULL) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 120 | boot_space_ = boot_space; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 121 | RecordImageAllocations(boot_space); |
| 122 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 123 | for (size_t i = 0; i < image_spaces.size(); i++) { |
| 124 | RecordImageAllocations(image_spaces[i]); |
| 125 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 126 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 127 | Heap::EnableObjectValidation(); |
| 128 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 129 | // It's still to early to take a lock because there are no threads yet, |
| 130 | // but we can create the heap lock now. We don't create it earlier to |
| 131 | // make it clear that you can't use locks during heap initialization. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 132 | lock_ = new Mutex("Heap lock"); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void Heap::Destroy() { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 136 | ScopedHeapLock lock; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 137 | STLDeleteElements(&spaces_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 138 | if (mark_bitmap_ != NULL) { |
| 139 | delete mark_bitmap_; |
| 140 | mark_bitmap_ = NULL; |
| 141 | } |
| 142 | if (live_bitmap_ != NULL) { |
| 143 | delete live_bitmap_; |
| 144 | } |
| 145 | live_bitmap_ = NULL; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 148 | Object* Heap::AllocObject(Class* klass, size_t num_bytes) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 149 | ScopedHeapLock lock; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 150 | DCHECK(klass == NULL |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 151 | || klass->GetDescriptor() == NULL |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 152 | || (klass->IsClassClass() && num_bytes >= sizeof(Class)) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 153 | || (klass->IsVariableSize() || klass->GetObjectSize() == num_bytes)); |
| 154 | DCHECK(num_bytes >= sizeof(Object)); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 155 | Object* obj = AllocateLocked(num_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 156 | if (obj != NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 157 | obj->SetClass(klass); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 158 | } |
| 159 | return obj; |
| 160 | } |
| 161 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 162 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 163 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 164 | // require taking the lock. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 165 | if (!IsAligned(obj, kObjectAlignment)) { |
| 166 | return false; |
| 167 | } |
| 168 | // TODO |
| 169 | return true; |
| 170 | } |
| 171 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 172 | bool Heap::verify_objects_ = false; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 173 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 174 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 175 | void Heap::VerifyObject(const Object* obj) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 176 | if (!verify_objects_) { |
| 177 | return; |
| 178 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 179 | ScopedHeapLock lock; |
| 180 | Heap::VerifyObjectLocked(obj); |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | void Heap::VerifyObjectLocked(const Object* obj) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 185 | lock_->AssertHeld(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 186 | if (obj != NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 187 | if (!IsAligned(obj, kObjectAlignment)) { |
| 188 | LOG(FATAL) << "Object isn't aligned: " << obj; |
| 189 | } else if (!live_bitmap_->Test(obj)) { |
| 190 | // TODO: we don't hold a lock here as it is assumed the live bit map |
| 191 | // isn't changing if the mutator is running. |
| 192 | LOG(FATAL) << "Object is dead: " << obj; |
| 193 | } |
| 194 | // Ignore early dawn of the universe verifications |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 195 | if (num_objects_allocated_ > 10) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 196 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 197 | Object::ClassOffset().Int32Value(); |
| 198 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 199 | if (c == NULL) { |
| 200 | LOG(FATAL) << "Null class" << " in object: " << obj; |
| 201 | } else if (!IsAligned(c, kObjectAlignment)) { |
| 202 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 203 | } else if (!live_bitmap_->Test(c)) { |
| 204 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 205 | } |
| 206 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
| 207 | // NB we don't use the accessors here as they have internal sanity checks |
| 208 | // that we don't want to run |
| 209 | raw_addr = reinterpret_cast<const byte*>(c) + |
| 210 | Object::ClassOffset().Int32Value(); |
| 211 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 212 | raw_addr = reinterpret_cast<const byte*>(c_c) + |
| 213 | Object::ClassOffset().Int32Value(); |
| 214 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 215 | CHECK_EQ(c_c, c_c_c); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 220 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 221 | DCHECK(obj != NULL); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 222 | Heap::VerifyObjectLocked(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void Heap::VerifyHeap() { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 226 | ScopedHeapLock lock; |
| 227 | live_bitmap_->Walk(Heap::VerificationCallback, NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 230 | void Heap::RecordAllocationLocked(Space* space, const Object* obj) { |
| 231 | #ifndef NDEBUG |
| 232 | if (Runtime::Current()->IsStarted()) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 233 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 234 | } |
| 235 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 236 | size_t size = space->AllocationSize(obj); |
| 237 | DCHECK_NE(size, 0u); |
| 238 | num_bytes_allocated_ += size; |
| 239 | num_objects_allocated_ += 1; |
| 240 | live_bitmap_->Set(obj); |
| 241 | } |
| 242 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 243 | void Heap::RecordFreeLocked(Space* space, const Object* obj) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 244 | lock_->AssertHeld(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 245 | size_t size = space->AllocationSize(obj); |
| 246 | DCHECK_NE(size, 0u); |
| 247 | if (size < num_bytes_allocated_) { |
| 248 | num_bytes_allocated_ -= size; |
| 249 | } else { |
| 250 | num_bytes_allocated_ = 0; |
| 251 | } |
| 252 | live_bitmap_->Clear(obj); |
| 253 | if (num_objects_allocated_ > 0) { |
| 254 | num_objects_allocated_ -= 1; |
| 255 | } |
| 256 | } |
| 257 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 258 | void Heap::RecordImageAllocations(Space* space) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 259 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 260 | CHECK(space != NULL); |
| 261 | CHECK(live_bitmap_ != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 262 | byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 263 | while (current < space->GetLimit()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 264 | DCHECK(IsAligned(current, kObjectAlignment)); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 265 | const Object* obj = reinterpret_cast<const Object*>(current); |
| 266 | live_bitmap_->Set(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 267 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 271 | Object* Heap::AllocateLocked(size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 272 | lock_->AssertHeld(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 273 | DCHECK(alloc_space_ != NULL); |
| 274 | Space* space = alloc_space_; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 275 | Object* obj = AllocateLocked(space, size); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 276 | if (obj != NULL) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 277 | RecordAllocationLocked(space, obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 278 | } |
| 279 | return obj; |
| 280 | } |
| 281 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 282 | Object* Heap::AllocateLocked(Space* space, size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 283 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 284 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 285 | // Fail impossible allocations. TODO: collect soft references. |
| 286 | if (size > maximum_size_) { |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 290 | Object* ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 291 | if (ptr != NULL) { |
| 292 | return ptr; |
| 293 | } |
| 294 | |
| 295 | // The allocation failed. If the GC is running, block until it |
| 296 | // completes and retry. |
| 297 | if (is_gc_running_) { |
| 298 | // The GC is concurrently tracing the heap. Release the heap |
| 299 | // lock, wait for the GC to complete, and retrying allocating. |
| 300 | WaitForConcurrentGcToComplete(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 301 | ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 302 | if (ptr != NULL) { |
| 303 | return ptr; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Another failure. Our thread was starved or there may be too many |
| 308 | // live objects. Try a foreground GC. This will have no effect if |
| 309 | // the concurrent GC is already running. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 310 | CollectGarbageInternal(); |
| 311 | ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 312 | if (ptr != NULL) { |
| 313 | return ptr; |
| 314 | } |
| 315 | |
| 316 | // Even that didn't work; this is an exceptional state. |
| 317 | // Try harder, growing the heap if necessary. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 318 | ptr = space->AllocWithGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 319 | if (ptr != NULL) { |
| 320 | //size_t new_footprint = dvmHeapSourceGetIdealFootprint(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 321 | size_t new_footprint = space->MaxAllowedFootprint(); |
| 322 | // TODO: may want to grow a little bit more so that the amount of |
| 323 | // free space is equal to the old free space + the |
| 324 | // utilization slop for the new allocation. |
| 325 | LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 326 | << "for " << size << "-byte allocation"; |
| 327 | return ptr; |
| 328 | } |
| 329 | |
| 330 | // Most allocations should have succeeded by now, so the heap is |
| 331 | // really full, really fragmented, or the requested size is really |
| 332 | // big. Do another GC, collecting SoftReferences this time. The VM |
| 333 | // spec requires that all SoftReferences have been collected and |
| 334 | // cleared before throwing an OOME. |
| 335 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 336 | // TODO: wait for the finalizers from the previous GC to finish |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 337 | LOG(INFO) << "Forcing collection of SoftReferences for " |
| 338 | << size << "-byte allocation"; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 339 | CollectGarbageInternal(); |
| 340 | ptr = space->AllocWithGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 341 | if (ptr != NULL) { |
| 342 | return ptr; |
| 343 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 344 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 345 | LOG(ERROR) << "Out of memory on a " << size << " byte allocation"; |
| 346 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 347 | // TODO: tell the HeapSource to dump its state |
| 348 | // TODO: dump stack traces for all threads |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 349 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
| 352 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 353 | int64_t Heap::GetMaxMemory() { |
| 354 | UNIMPLEMENTED(WARNING); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | int64_t Heap::GetTotalMemory() { |
| 359 | UNIMPLEMENTED(WARNING); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | int64_t Heap::GetFreeMemory() { |
| 364 | UNIMPLEMENTED(WARNING); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 368 | void Heap::CollectGarbage() { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 369 | ScopedHeapLock lock; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 370 | CollectGarbageInternal(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void Heap::CollectGarbageInternal() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 374 | lock_->AssertHeld(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 375 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 376 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 377 | thread_list->SuspendAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 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 |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 389 | // thread_list->ResumeAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 390 | |
| 391 | mark_sweep.RecursiveMark(); |
| 392 | |
| 393 | // TODO: if concurrent |
| 394 | // lock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 395 | // thread_list->SuspendAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 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(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 407 | thread_list->ResumeAll(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void Heap::WaitForConcurrentGcToComplete() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 411 | lock_->AssertHeld(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // Given the current contents of the active heap, increase the allowed |
| 415 | // heap footprint to match the target utilization ratio. This should |
| 416 | // only be called immediately after a full garbage collection. |
| 417 | void Heap::GrowForUtilization() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 418 | lock_->AssertHeld(); |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 419 | UNIMPLEMENTED(ERROR); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 422 | void Heap::Lock() { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 423 | // TODO: grab the lock, but put ourselves into Thread::kVmWait if it looks like |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 424 | // we're going to have to wait on the mutex. |
| 425 | lock_->Lock(); |
| 426 | } |
| 427 | |
| 428 | void Heap::Unlock() { |
| 429 | lock_->Unlock(); |
| 430 | } |
| 431 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 432 | } // namespace art |