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