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