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 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 5 | #include <limits> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 6 | #include <vector> |
| 7 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 8 | #include "UniquePtr.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 9 | #include "image.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 10 | #include "mark_sweep.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "object.h" |
| 12 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 13 | #include "stl_util.h" |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 14 | #include "timing_logger.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 15 | #include "thread_list.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
| 17 | namespace art { |
| 18 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 19 | std::vector<Space*> Heap::spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 35 | Class* Heap::java_lang_ref_FinalizerReference_ = NULL; |
| 36 | Class* Heap::java_lang_ref_ReferenceQueue_ = NULL; |
| 37 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 38 | MemberOffset Heap::reference_referent_offset_ = MemberOffset(0); |
| 39 | MemberOffset Heap::reference_queue_offset_ = MemberOffset(0); |
| 40 | MemberOffset Heap::reference_queueNext_offset_ = MemberOffset(0); |
| 41 | MemberOffset Heap::reference_pendingNext_offset_ = MemberOffset(0); |
| 42 | MemberOffset Heap::finalizer_reference_zombie_offset_ = MemberOffset(0); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 43 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 44 | float Heap::target_utilization_ = 0.5; |
| 45 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 46 | Mutex* Heap::lock_ = NULL; |
| 47 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 48 | bool Heap::verify_objects_ = false; |
| 49 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 50 | class ScopedHeapLock { |
| 51 | public: |
| 52 | ScopedHeapLock() { |
| 53 | Heap::Lock(); |
| 54 | } |
| 55 | |
| 56 | ~ScopedHeapLock() { |
| 57 | Heap::Unlock(); |
| 58 | } |
| 59 | }; |
| 60 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 61 | void Heap::Init(size_t initial_size, size_t maximum_size, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 62 | const std::vector<std::string>& image_file_names) { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 63 | const Runtime* runtime = Runtime::Current(); |
| 64 | if (runtime->IsVerboseStartup()) { |
| 65 | LOG(INFO) << "Heap::Init entering"; |
| 66 | } |
| 67 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 68 | // bounds of all spaces for allocating live and mark bitmaps |
| 69 | // there will be at least one space (the alloc space), |
| 70 | // so set to base to max and limit to min to start |
| 71 | byte* base = reinterpret_cast<byte*>(std::numeric_limits<uintptr_t>::max()); |
| 72 | byte* limit = reinterpret_cast<byte*>(std::numeric_limits<uintptr_t>::min()); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 73 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 74 | byte* requested_base = NULL; |
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++) { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 77 | Space* space = Space::CreateFromImage(image_file_names[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 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); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 83 | byte* oat_limit_addr = space->GetImageHeader().GetOatLimitAddr(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 84 | if (oat_limit_addr > requested_base) { |
| 85 | requested_base = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), |
| 86 | kPageSize)); |
| 87 | } |
| 88 | base = std::min(base, space->GetBase()); |
| 89 | limit = std::max(limit, space->GetLimit()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 92 | alloc_space_ = Space::Create("alloc space", initial_size, maximum_size, requested_base); |
| 93 | if (alloc_space_ == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 94 | LOG(FATAL) << "Failed to create alloc space"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 95 | } |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 96 | base = std::min(base, alloc_space_->GetBase()); |
| 97 | limit = std::max(limit, alloc_space_->GetLimit()); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 98 | DCHECK_LT(base, limit); |
| 99 | size_t num_bytes = limit - base; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 100 | |
| 101 | // Allocate the initial live bitmap. |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 102 | UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes)); |
| 103 | if (live_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 104 | LOG(FATAL) << "Failed to create live bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Allocate the initial mark bitmap. |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 108 | UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes)); |
| 109 | if (mark_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 110 | LOG(FATAL) << "Failed to create mark bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 113 | spaces_.push_back(alloc_space_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 114 | maximum_size_ = maximum_size; |
| 115 | live_bitmap_ = live_bitmap.release(); |
| 116 | mark_bitmap_ = mark_bitmap.release(); |
| 117 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 118 | num_bytes_allocated_ = 0; |
| 119 | num_objects_allocated_ = 0; |
| 120 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 121 | // TODO: allocate the card table |
| 122 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 123 | // Make image objects live (after live_bitmap_ is set) |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 124 | for (size_t i = 0; i < image_spaces.size(); i++) { |
| 125 | RecordImageAllocations(image_spaces[i]); |
| 126 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 127 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 128 | Heap::EnableObjectValidation(); |
| 129 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 130 | // It's still to early to take a lock because there are no threads yet, |
| 131 | // but we can create the heap lock now. We don't create it earlier to |
| 132 | // make it clear that you can't use locks during heap initialization. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 133 | lock_ = new Mutex("Heap lock"); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 134 | |
| 135 | if (runtime->IsVerboseStartup()) { |
| 136 | LOG(INFO) << "Heap::Init exiting"; |
| 137 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void Heap::Destroy() { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 141 | ScopedHeapLock lock; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 142 | STLDeleteElements(&spaces_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 143 | if (mark_bitmap_ != NULL) { |
| 144 | delete mark_bitmap_; |
| 145 | mark_bitmap_ = NULL; |
| 146 | } |
| 147 | if (live_bitmap_ != NULL) { |
| 148 | delete live_bitmap_; |
| 149 | } |
| 150 | live_bitmap_ = NULL; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 153 | Object* Heap::AllocObject(Class* klass, size_t byte_count) { |
| 154 | { |
| 155 | ScopedHeapLock lock; |
| 156 | DCHECK(klass == NULL || klass->GetDescriptor() == NULL || |
| 157 | (klass->IsClassClass() && byte_count >= sizeof(Class)) || |
| 158 | (klass->IsVariableSize() || klass->GetObjectSize() == byte_count)); |
| 159 | DCHECK_GE(byte_count, sizeof(Object)); |
| 160 | Object* obj = AllocateLocked(byte_count); |
| 161 | if (obj != NULL) { |
| 162 | obj->SetClass(klass); |
| 163 | return obj; |
| 164 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 165 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 166 | |
| 167 | Thread::Current()->ThrowOutOfMemoryError(klass, byte_count); |
| 168 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 171 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 172 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 173 | // require taking the lock. |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 174 | if (!IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | // TODO |
| 178 | return true; |
| 179 | } |
| 180 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 181 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 182 | void Heap::VerifyObject(const Object* obj) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 183 | if (!verify_objects_) { |
| 184 | return; |
| 185 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 186 | ScopedHeapLock lock; |
| 187 | Heap::VerifyObjectLocked(obj); |
| 188 | } |
| 189 | #endif |
| 190 | |
| 191 | void Heap::VerifyObjectLocked(const Object* obj) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 192 | lock_->AssertHeld(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 193 | if (obj != NULL) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 194 | if (!IsAligned<kObjectAlignment>(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 195 | LOG(FATAL) << "Object isn't aligned: " << obj; |
| 196 | } else if (!live_bitmap_->Test(obj)) { |
| 197 | // TODO: we don't hold a lock here as it is assumed the live bit map |
| 198 | // isn't changing if the mutator is running. |
| 199 | LOG(FATAL) << "Object is dead: " << obj; |
| 200 | } |
| 201 | // Ignore early dawn of the universe verifications |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 202 | if (num_objects_allocated_ > 10) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 203 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 204 | Object::ClassOffset().Int32Value(); |
| 205 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 206 | if (c == NULL) { |
| 207 | LOG(FATAL) << "Null class" << " in object: " << obj; |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 208 | } else if (!IsAligned<kObjectAlignment>(c)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 209 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 210 | } else if (!live_bitmap_->Test(c)) { |
| 211 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 212 | } |
| 213 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 214 | // Note: we don't use the accessors here as they have internal sanity checks |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 215 | // that we don't want to run |
| 216 | raw_addr = reinterpret_cast<const byte*>(c) + |
| 217 | Object::ClassOffset().Int32Value(); |
| 218 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 219 | raw_addr = reinterpret_cast<const byte*>(c_c) + |
| 220 | Object::ClassOffset().Int32Value(); |
| 221 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 222 | CHECK_EQ(c_c, c_c_c); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 227 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 228 | DCHECK(obj != NULL); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 229 | Heap::VerifyObjectLocked(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void Heap::VerifyHeap() { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 233 | ScopedHeapLock lock; |
| 234 | live_bitmap_->Walk(Heap::VerificationCallback, NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 237 | void Heap::RecordAllocationLocked(Space* space, const Object* obj) { |
| 238 | #ifndef NDEBUG |
| 239 | if (Runtime::Current()->IsStarted()) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 240 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 241 | } |
| 242 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 243 | size_t size = space->AllocationSize(obj); |
| 244 | DCHECK_NE(size, 0u); |
| 245 | num_bytes_allocated_ += size; |
| 246 | num_objects_allocated_ += 1; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 247 | |
| 248 | if (Runtime::Current()->HasStatsEnabled()) { |
| 249 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 250 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 251 | ++global_stats->allocated_objects; |
| 252 | ++thread_stats->allocated_objects; |
| 253 | global_stats->allocated_bytes += size; |
| 254 | thread_stats->allocated_bytes += size; |
| 255 | } |
| 256 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 257 | live_bitmap_->Set(obj); |
| 258 | } |
| 259 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 260 | void Heap::RecordFreeLocked(size_t freed_objects, size_t freed_bytes) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 261 | lock_->AssertHeld(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 262 | |
| 263 | if (freed_objects < num_objects_allocated_) { |
| 264 | num_objects_allocated_ -= freed_objects; |
| 265 | } else { |
| 266 | num_objects_allocated_ = 0; |
| 267 | } |
| 268 | if (freed_bytes < num_bytes_allocated_) { |
| 269 | num_bytes_allocated_ -= freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 270 | } else { |
| 271 | num_bytes_allocated_ = 0; |
| 272 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 273 | |
| 274 | if (Runtime::Current()->HasStatsEnabled()) { |
| 275 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 276 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 277 | ++global_stats->freed_objects; |
| 278 | ++thread_stats->freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 279 | global_stats->freed_bytes += freed_bytes; |
| 280 | thread_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 281 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 284 | void Heap::RecordImageAllocations(Space* space) { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 285 | const Runtime* runtime = Runtime::Current(); |
| 286 | if (runtime->IsVerboseStartup()) { |
| 287 | LOG(INFO) << "Heap::RecordImageAllocations entering"; |
| 288 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 289 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 290 | CHECK(space != NULL); |
| 291 | CHECK(live_bitmap_ != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 292 | byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 293 | while (current < space->GetLimit()) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 294 | DCHECK_ALIGNED(current, kObjectAlignment); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 295 | const Object* obj = reinterpret_cast<const Object*>(current); |
| 296 | live_bitmap_->Set(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 297 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 298 | } |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 299 | if (runtime->IsVerboseStartup()) { |
| 300 | LOG(INFO) << "Heap::RecordImageAllocations exiting"; |
| 301 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 304 | Object* Heap::AllocateLocked(size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 305 | lock_->AssertHeld(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 306 | DCHECK(alloc_space_ != NULL); |
| 307 | Space* space = alloc_space_; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 308 | Object* obj = AllocateLocked(space, size); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 309 | if (obj != NULL) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 310 | RecordAllocationLocked(space, obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 311 | } |
| 312 | return obj; |
| 313 | } |
| 314 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 315 | Object* Heap::AllocateLocked(Space* space, size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 316 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 317 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 318 | // Fail impossible allocations. TODO: collect soft references. |
| 319 | if (size > maximum_size_) { |
| 320 | return NULL; |
| 321 | } |
| 322 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 323 | Object* ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 324 | if (ptr != NULL) { |
| 325 | return ptr; |
| 326 | } |
| 327 | |
| 328 | // The allocation failed. If the GC is running, block until it |
| 329 | // completes and retry. |
| 330 | if (is_gc_running_) { |
| 331 | // The GC is concurrently tracing the heap. Release the heap |
| 332 | // lock, wait for the GC to complete, and retrying allocating. |
| 333 | WaitForConcurrentGcToComplete(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 334 | ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 335 | if (ptr != NULL) { |
| 336 | return ptr; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Another failure. Our thread was starved or there may be too many |
| 341 | // live objects. Try a foreground GC. This will have no effect if |
| 342 | // the concurrent GC is already running. |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 343 | if (Runtime::Current()->HasStatsEnabled()) { |
| 344 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 345 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 346 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 347 | LOG(INFO) << "GC_FOR_ALLOC: AllocWithoutGrowth: TODO: test"; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 348 | CollectGarbageInternal(); |
| 349 | ptr = space->AllocWithoutGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 350 | if (ptr != NULL) { |
| 351 | return ptr; |
| 352 | } |
| 353 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 354 | LOG(INFO) << "GC_FOR_ALLOC: AllocWithGrowth: TODO: test"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 355 | // Even that didn't work; this is an exceptional state. |
| 356 | // Try harder, growing the heap if necessary. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 357 | ptr = space->AllocWithGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 358 | if (ptr != NULL) { |
| 359 | //size_t new_footprint = dvmHeapSourceGetIdealFootprint(); |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 360 | size_t new_footprint = space->GetMaxAllowedFootprint(); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 361 | // OLD-TODO: may want to grow a little bit more so that the amount of |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 362 | // free space is equal to the old free space + the |
| 363 | // utilization slop for the new allocation. |
| 364 | LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 365 | << "for " << size << "-byte allocation"; |
| 366 | return ptr; |
| 367 | } |
| 368 | |
| 369 | // Most allocations should have succeeded by now, so the heap is |
| 370 | // really full, really fragmented, or the requested size is really |
| 371 | // big. Do another GC, collecting SoftReferences this time. The VM |
| 372 | // spec requires that all SoftReferences have been collected and |
| 373 | // cleared before throwing an OOME. |
| 374 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 375 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 376 | LOG(INFO) << "Forcing collection of SoftReferences for " |
| 377 | << size << "-byte allocation"; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 378 | CollectGarbageInternal(); |
| 379 | ptr = space->AllocWithGrowth(size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 380 | if (ptr != NULL) { |
| 381 | return ptr; |
| 382 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 383 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 384 | LOG(ERROR) << "Out of memory on a " << size << " byte allocation"; |
| 385 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 386 | // TODO: tell the HeapSource to dump its state |
| 387 | // TODO: dump stack traces for all threads |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 388 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 389 | return NULL; |
| 390 | } |
| 391 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 392 | int64_t Heap::GetMaxMemory() { |
| 393 | UNIMPLEMENTED(WARNING); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | int64_t Heap::GetTotalMemory() { |
| 398 | UNIMPLEMENTED(WARNING); |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | int64_t Heap::GetFreeMemory() { |
| 403 | UNIMPLEMENTED(WARNING); |
| 404 | return 0; |
| 405 | } |
| 406 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 407 | class InstanceCounter { |
| 408 | public: |
| 409 | InstanceCounter(Class* c, bool count_assignable) |
| 410 | : class_(c), count_assignable_(count_assignable), count_(0) { |
| 411 | } |
| 412 | |
| 413 | size_t GetCount() { |
| 414 | return count_; |
| 415 | } |
| 416 | |
| 417 | static void Callback(Object* o, void* arg) { |
| 418 | reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o); |
| 419 | } |
| 420 | |
| 421 | private: |
| 422 | void VisitInstance(Object* o) { |
| 423 | Class* instance_class = o->GetClass(); |
| 424 | if (count_assignable_) { |
| 425 | if (instance_class == class_) { |
| 426 | ++count_; |
| 427 | } |
| 428 | } else { |
| 429 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
| 430 | ++count_; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | Class* class_; |
| 436 | bool count_assignable_; |
| 437 | size_t count_; |
| 438 | }; |
| 439 | |
| 440 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
| 441 | ScopedHeapLock lock; |
| 442 | InstanceCounter counter(c, count_assignable); |
| 443 | live_bitmap_->Walk(InstanceCounter::Callback, &counter); |
| 444 | return counter.GetCount(); |
| 445 | } |
| 446 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 447 | void Heap::CollectGarbage() { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 448 | ScopedHeapLock lock; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 449 | CollectGarbageInternal(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void Heap::CollectGarbageInternal() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 453 | lock_->AssertHeld(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 454 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 455 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 456 | thread_list->SuspendAll(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 457 | |
| 458 | size_t initial_size = num_bytes_allocated_; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 459 | TimingLogger timings("CollectGarbageInternal"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 460 | uint64_t t0 = NanoTime(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 461 | Object* cleared_references = NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 462 | { |
| 463 | MarkSweep mark_sweep; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 464 | timings.AddSplit("ctor"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 465 | |
| 466 | mark_sweep.Init(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 467 | timings.AddSplit("Init"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 468 | |
| 469 | mark_sweep.MarkRoots(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 470 | timings.AddSplit("MarkRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 471 | |
| 472 | // Push marked roots onto the mark stack |
| 473 | |
| 474 | // TODO: if concurrent |
| 475 | // unlock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 476 | // thread_list->ResumeAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 477 | |
| 478 | mark_sweep.RecursiveMark(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 479 | timings.AddSplit("RecursiveMark"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 480 | |
| 481 | // TODO: if concurrent |
| 482 | // lock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 483 | // thread_list->SuspendAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 484 | // re-mark root set |
| 485 | // scan dirty objects |
| 486 | |
| 487 | mark_sweep.ProcessReferences(false); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 488 | timings.AddSplit("ProcessReferences"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 489 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 490 | // TODO: if concurrent |
| 491 | // swap bitmaps |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 492 | |
| 493 | mark_sweep.Sweep(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 494 | timings.AddSplit("Sweep"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 495 | |
| 496 | cleared_references = mark_sweep.GetClearedReferences(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | GrowForUtilization(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 500 | timings.AddSplit("GrowForUtilization"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 501 | uint64_t t1 = NanoTime(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 502 | thread_list->ResumeAll(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 503 | |
| 504 | EnqueueClearedReferences(&cleared_references); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 505 | |
| 506 | // TODO: somehow make the specific GC implementation (here MarkSweep) responsible for logging. |
| 507 | size_t bytes_freed = initial_size - num_bytes_allocated_; |
| 508 | bool is_small = (bytes_freed > 0 && bytes_freed < 1024); |
| 509 | size_t kib_freed = (bytes_freed > 0 ? std::max(bytes_freed/1024, 1U) : 0); |
| 510 | |
| 511 | size_t footprint = alloc_space_->Size(); |
| 512 | size_t percentFree = 100 - static_cast<size_t>(100.0f * float(num_bytes_allocated_) / footprint); |
| 513 | |
| 514 | uint32_t duration = (t1 - t0)/1000/1000; |
| 515 | LOG(INFO) << "GC freed " << (is_small ? "<" : "") << kib_freed << "KiB, " |
| 516 | << percentFree << "% free " |
| 517 | << (num_bytes_allocated_/1024) << "KiB/" << (footprint/1024) << "KiB, " |
| 518 | << "paused " << duration << "ms"; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 519 | timings.Dump(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void Heap::WaitForConcurrentGcToComplete() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 523 | lock_->AssertHeld(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 526 | /* Terminology: |
| 527 | * 1. Footprint: Capacity we allocate from system. |
| 528 | * 2. Active space: a.k.a. alloc_space_. |
| 529 | * 3. Soft footprint: external allocation + spaces footprint + active space footprint |
| 530 | * 4. Overhead: soft footprint excluding active. |
| 531 | * |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 532 | * Layout: (The spaces below might not be contiguous, but are lumped together to depict size.) |
| 533 | * |----------------------spaces footprint--------- --------------|----active space footprint----| |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 534 | * |--active space allocated--| |
| 535 | * |--------------------soft footprint (include active)--------------------------------------| |
| 536 | * |----------------soft footprint excluding active---------------| |
| 537 | * |------------soft limit-------...| |
| 538 | * |------------------------------------ideal footprint-----------------------------------------...| |
| 539 | * |
| 540 | */ |
| 541 | |
| 542 | // Sets the maximum number of bytes that the heap is allowed to |
| 543 | // allocate from the system. Clamps to the appropriate maximum |
| 544 | // value. |
| 545 | // Old spaces will count against the ideal size. |
| 546 | // |
| 547 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) |
| 548 | { |
| 549 | if (max_allowed_footprint > Heap::maximum_size_) { |
| 550 | LOG(INFO) << "Clamp target GC heap from " << max_allowed_footprint |
| 551 | << " to " << Heap::maximum_size_; |
| 552 | max_allowed_footprint = Heap::maximum_size_; |
| 553 | } |
| 554 | |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 555 | alloc_space_->SetMaxAllowedFootprint(max_allowed_footprint); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 558 | // kHeapIdealFree is the ideal maximum free size, when we grow the heap for |
| 559 | // utlization. |
| 560 | static const size_t kHeapIdealFree = 2 * MB; |
| 561 | // kHeapMinFree guarantees that you always have at least 512 KB free, when |
| 562 | // you grow for utilization, regardless of target utilization ratio. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 563 | static const size_t kHeapMinFree = kHeapIdealFree / 4; |
| 564 | |
| 565 | // Given the current contents of the active space, increase the allowed |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 566 | // heap footprint to match the target utilization ratio. This should |
| 567 | // only be called immediately after a full garbage collection. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 568 | // |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 569 | void Heap::GrowForUtilization() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 570 | lock_->AssertHeld(); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 571 | |
| 572 | // We know what our utilization is at this moment. |
| 573 | // This doesn't actually resize any memory. It just lets the heap grow more |
| 574 | // when necessary. |
| 575 | size_t target_size = size_t( num_bytes_allocated_ / |
| 576 | Heap::GetTargetHeapUtilization() ); |
| 577 | |
| 578 | if (target_size > num_bytes_allocated_ + kHeapIdealFree) { |
| 579 | target_size = num_bytes_allocated_ + kHeapIdealFree; |
| 580 | } else if (target_size < num_bytes_allocated_ + kHeapMinFree) { |
| 581 | target_size = num_bytes_allocated_ + kHeapMinFree; |
| 582 | } |
| 583 | |
| 584 | SetIdealFootprint(target_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame^] | 587 | uint32_t Heap::GetLockOwner() { |
| 588 | return lock_->GetOwner(); |
| 589 | } |
| 590 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 591 | void Heap::Lock() { |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 592 | // Grab the lock, but put ourselves into Thread::kVmWait if it looks |
| 593 | // like we're going to have to wait on the mutex. This prevents |
| 594 | // deadlock if another thread is calling CollectGarbageInternal, |
| 595 | // since they will have the heap lock and be waiting for mutators to |
| 596 | // suspend. |
| 597 | if (!lock_->TryLock()) { |
| 598 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 599 | lock_->Lock(); |
| 600 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void Heap::Unlock() { |
| 604 | lock_->Unlock(); |
| 605 | } |
| 606 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 607 | void Heap::SetWellKnownClasses(Class* java_lang_ref_FinalizerReference, |
| 608 | Class* java_lang_ref_ReferenceQueue) { |
| 609 | java_lang_ref_FinalizerReference_ = java_lang_ref_FinalizerReference; |
| 610 | java_lang_ref_ReferenceQueue_ = java_lang_ref_ReferenceQueue; |
| 611 | CHECK(java_lang_ref_FinalizerReference_ != NULL); |
| 612 | CHECK(java_lang_ref_ReferenceQueue_ != NULL); |
| 613 | } |
| 614 | |
| 615 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 616 | MemberOffset reference_queue_offset, |
| 617 | MemberOffset reference_queueNext_offset, |
| 618 | MemberOffset reference_pendingNext_offset, |
| 619 | MemberOffset finalizer_reference_zombie_offset) { |
| 620 | reference_referent_offset_ = reference_referent_offset; |
| 621 | reference_queue_offset_ = reference_queue_offset; |
| 622 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 623 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 624 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 625 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 626 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 627 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 628 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 629 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 630 | } |
| 631 | |
| 632 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 633 | DCHECK(reference != NULL); |
| 634 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 635 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 636 | } |
| 637 | |
| 638 | void Heap::ClearReferenceReferent(Object* reference) { |
| 639 | DCHECK(reference != NULL); |
| 640 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 641 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 642 | } |
| 643 | |
| 644 | // Returns true if the reference object has not yet been enqueued. |
| 645 | bool Heap::IsEnqueuable(const Object* ref) { |
| 646 | DCHECK(ref != NULL); |
| 647 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 648 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 649 | return (queue != NULL) && (queue_next == NULL); |
| 650 | } |
| 651 | |
| 652 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 653 | DCHECK(ref != NULL); |
| 654 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 655 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 656 | EnqueuePendingReference(ref, cleared_reference_list); |
| 657 | } |
| 658 | |
| 659 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 660 | DCHECK(ref != NULL); |
| 661 | DCHECK(list != NULL); |
| 662 | |
| 663 | if (*list == NULL) { |
| 664 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 665 | *list = ref; |
| 666 | } else { |
| 667 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 668 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 669 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | Object* Heap::DequeuePendingReference(Object** list) { |
| 674 | DCHECK(list != NULL); |
| 675 | DCHECK(*list != NULL); |
| 676 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 677 | Object* ref; |
| 678 | if (*list == head) { |
| 679 | ref = *list; |
| 680 | *list = NULL; |
| 681 | } else { |
| 682 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 683 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 684 | ref = head; |
| 685 | } |
| 686 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 687 | return ref; |
| 688 | } |
| 689 | |
| 690 | void Heap::AddFinalizerReference(Object* object) { |
| 691 | static Method* FinalizerReference_add = |
| 692 | java_lang_ref_FinalizerReference_->FindDirectMethod("add", "(Ljava/lang/Object;)V"); |
| 693 | DCHECK(FinalizerReference_add != NULL); |
| 694 | Object* args[] = { object }; |
| 695 | FinalizerReference_add->Invoke(Thread::Current(), NULL, reinterpret_cast<byte*>(&args), NULL); |
| 696 | } |
| 697 | |
| 698 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 699 | DCHECK(cleared != NULL); |
| 700 | if (*cleared != NULL) { |
| 701 | static Method* ReferenceQueue_add = |
| 702 | java_lang_ref_ReferenceQueue_->FindDirectMethod("add", "(Ljava/lang/ref/Reference;)V"); |
| 703 | DCHECK(ReferenceQueue_add != NULL); |
| 704 | |
| 705 | Thread* self = Thread::Current(); |
| 706 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 707 | Object* args[] = { *cleared }; |
| 708 | ReferenceQueue_add->Invoke(self, NULL, reinterpret_cast<byte*>(&args), NULL); |
| 709 | *cleared = NULL; |
| 710 | } |
| 711 | } |
| 712 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 713 | } // namespace art |