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