Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "heap.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 19 | #include <limits> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 22 | #include "card_table.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 23 | #include "debugger.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 24 | #include "image.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 25 | #include "mark_sweep.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 26 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 27 | #include "object_utils.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 28 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 29 | #include "stl_util.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 30 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 31 | #include "timing_logger.h" |
| 32 | #include "UniquePtr.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 36 | std::vector<Space*> Heap::spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 37 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 38 | AllocSpace* Heap::alloc_space_ = NULL; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 39 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 40 | size_t Heap::num_bytes_allocated_ = 0; |
| 41 | |
| 42 | size_t Heap::num_objects_allocated_ = 0; |
| 43 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 44 | bool Heap::is_gc_running_ = false; |
| 45 | |
| 46 | HeapBitmap* Heap::mark_bitmap_ = NULL; |
| 47 | |
| 48 | HeapBitmap* Heap::live_bitmap_ = NULL; |
| 49 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 50 | CardTable* Heap::card_table_ = NULL; |
| 51 | |
| 52 | bool Heap::card_marking_disabled_ = false; |
| 53 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 54 | Class* Heap::java_lang_ref_FinalizerReference_ = NULL; |
| 55 | Class* Heap::java_lang_ref_ReferenceQueue_ = NULL; |
| 56 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 57 | MemberOffset Heap::reference_referent_offset_ = MemberOffset(0); |
| 58 | MemberOffset Heap::reference_queue_offset_ = MemberOffset(0); |
| 59 | MemberOffset Heap::reference_queueNext_offset_ = MemberOffset(0); |
| 60 | MemberOffset Heap::reference_pendingNext_offset_ = MemberOffset(0); |
| 61 | MemberOffset Heap::finalizer_reference_zombie_offset_ = MemberOffset(0); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 62 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 63 | float Heap::target_utilization_ = 0.5; |
| 64 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 65 | Mutex* Heap::lock_ = NULL; |
| 66 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 67 | bool Heap::verify_objects_ = false; |
| 68 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 69 | static void UpdateFirstAndLastSpace(Space** first_space, Space** last_space, Space* space) { |
| 70 | if (*first_space == NULL) { |
| 71 | *first_space = space; |
| 72 | *last_space = space; |
| 73 | } else { |
| 74 | if ((*first_space)->Begin() > space->Begin()) { |
| 75 | *first_space = space; |
| 76 | } else if (space->Begin() > (*last_space)->Begin()) { |
| 77 | *last_space = space; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void Heap::Init(size_t initial_size, size_t growth_limit, size_t capacity, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 83 | const std::vector<std::string>& image_file_names) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 84 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 85 | LOG(INFO) << "Heap::Init entering"; |
| 86 | } |
| 87 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 88 | // Compute the bounds of all spaces for allocating live and mark bitmaps |
| 89 | // there will be at least one space (the alloc space) |
| 90 | Space* first_space = NULL; |
| 91 | Space* last_space = NULL; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 92 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 93 | // Requested begin for the alloc space, to follow the mapped image and oat files |
| 94 | byte* requested_begin = NULL; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 95 | std::vector<Space*> image_spaces; |
| 96 | for (size_t i = 0; i < image_file_names.size(); i++) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 97 | ImageSpace* space = Space::CreateImageSpace(image_file_names[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 98 | if (space == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 99 | LOG(FATAL) << "Failed to create space from " << image_file_names[i]; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 100 | } |
| 101 | image_spaces.push_back(space); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 102 | AddSpace(space); |
| 103 | UpdateFirstAndLastSpace(&first_space, &last_space, space); |
| 104 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 105 | // isn't going to get in the middle |
| 106 | byte* oat_end_addr = space->GetImageHeader().GetOatEnd(); |
| 107 | CHECK(oat_end_addr > space->End()); |
| 108 | if (oat_end_addr > requested_begin) { |
| 109 | requested_begin = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr), |
| 110 | kPageSize)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 111 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 114 | alloc_space_ = Space::CreateAllocSpace("alloc space", initial_size, growth_limit, capacity, |
| 115 | requested_begin); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 116 | if (alloc_space_ == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 117 | LOG(FATAL) << "Failed to create alloc space"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 118 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 119 | AddSpace(alloc_space_); |
| 120 | UpdateFirstAndLastSpace(&first_space, &last_space, alloc_space_); |
| 121 | byte* heap_begin = first_space->Begin(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 122 | size_t heap_capacity = (last_space->Begin() - first_space->Begin()) + last_space->NonGrowthLimitCapacity(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 123 | |
| 124 | // Allocate the initial live bitmap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 125 | UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create("dalvik-bitmap-1", heap_begin, heap_capacity)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 126 | if (live_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 127 | LOG(FATAL) << "Failed to create live bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 130 | // Mark image objects in the live bitmap |
| 131 | for (size_t i = 0; i < spaces_.size(); i++) { |
| 132 | Space* space = spaces_[i]; |
| 133 | if (space->IsImageSpace()) { |
| 134 | space->AsImageSpace()->RecordImageAllocations(live_bitmap.get()); |
| 135 | } |
| 136 | } |
| 137 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 138 | // Allocate the initial mark bitmap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 139 | UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create("dalvik-bitmap-2", heap_begin, heap_capacity)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 140 | if (mark_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 141 | LOG(FATAL) << "Failed to create mark bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 144 | // Allocate the card table. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 145 | UniquePtr<CardTable> card_table(CardTable::Create(heap_begin, heap_capacity)); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 146 | if (card_table.get() == NULL) { |
| 147 | LOG(FATAL) << "Failed to create card table"; |
| 148 | } |
| 149 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 150 | live_bitmap_ = live_bitmap.release(); |
| 151 | mark_bitmap_ = mark_bitmap.release(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 152 | card_table_ = card_table.release(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 153 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 154 | num_bytes_allocated_ = 0; |
| 155 | num_objects_allocated_ = 0; |
| 156 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 157 | // It's still to early to take a lock because there are no threads yet, |
| 158 | // but we can create the heap lock now. We don't create it earlier to |
| 159 | // make it clear that you can't use locks during heap initialization. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 160 | lock_ = new Mutex("Heap lock"); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 161 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 162 | Heap::EnableObjectValidation(); |
| 163 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 164 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 165 | LOG(INFO) << "Heap::Init exiting"; |
| 166 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void Heap::Destroy() { |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 170 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 171 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 172 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 173 | // those threads can't resume. We're the only running thread, and we can do whatever we like... |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 174 | STLDeleteElements(&spaces_); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 175 | delete mark_bitmap_; |
| 176 | delete live_bitmap_; |
| 177 | delete card_table_; |
| 178 | delete lock_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 181 | Object* Heap::AllocObject(Class* klass, size_t byte_count) { |
| 182 | { |
| 183 | ScopedHeapLock lock; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 184 | DCHECK(klass == NULL || (klass->IsClassClass() && byte_count >= sizeof(Class)) || |
| 185 | (klass->IsVariableSize() || klass->GetObjectSize() == byte_count) || |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 186 | strlen(ClassHelper(klass).GetDescriptor()) == 0); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 187 | DCHECK_GE(byte_count, sizeof(Object)); |
| 188 | Object* obj = AllocateLocked(byte_count); |
| 189 | if (obj != NULL) { |
| 190 | obj->SetClass(klass); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 191 | if (Dbg::IsAllocTrackingEnabled()) { |
| 192 | Dbg::RecordAllocation(klass, byte_count); |
| 193 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 194 | return obj; |
| 195 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 196 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 197 | |
| 198 | Thread::Current()->ThrowOutOfMemoryError(klass, byte_count); |
| 199 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 202 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 203 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 204 | // require taking the lock. |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 205 | if (obj == NULL || !IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 208 | for (size_t i = 0; i < spaces_.size(); i++) { |
| 209 | if (spaces_[i]->Contains(obj)) { |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 216 | bool Heap::IsLiveObjectLocked(const Object* obj) { |
| 217 | lock_->AssertHeld(); |
| 218 | return IsHeapAddress(obj) && live_bitmap_->Test(obj); |
| 219 | } |
| 220 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 221 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 222 | void Heap::VerifyObject(const Object* obj) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 223 | if (!verify_objects_) { |
| 224 | return; |
| 225 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 226 | ScopedHeapLock lock; |
| 227 | Heap::VerifyObjectLocked(obj); |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | void Heap::VerifyObjectLocked(const Object* obj) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 232 | lock_->AssertHeld(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 233 | if (obj != NULL) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 234 | if (!IsAligned<kObjectAlignment>(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 235 | LOG(FATAL) << "Object isn't aligned: " << obj; |
| 236 | } else if (!live_bitmap_->Test(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 237 | LOG(FATAL) << "Object is dead: " << obj; |
| 238 | } |
| 239 | // Ignore early dawn of the universe verifications |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 240 | if (num_objects_allocated_ > 10) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 241 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 242 | Object::ClassOffset().Int32Value(); |
| 243 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 244 | if (c == NULL) { |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 245 | LOG(FATAL) << "Null class in object: " << obj; |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 246 | } else if (!IsAligned<kObjectAlignment>(c)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 247 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 248 | } else if (!live_bitmap_->Test(c)) { |
| 249 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 250 | } |
| 251 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 252 | // 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] | 253 | // that we don't want to run |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 254 | raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 255 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 256 | raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 257 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 258 | CHECK_EQ(c_c, c_c_c); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 263 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 264 | DCHECK(obj != NULL); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 265 | Heap::VerifyObjectLocked(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void Heap::VerifyHeap() { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 269 | ScopedHeapLock lock; |
| 270 | live_bitmap_->Walk(Heap::VerificationCallback, NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 273 | void Heap::RecordAllocationLocked(AllocSpace* space, const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 274 | #ifndef NDEBUG |
| 275 | if (Runtime::Current()->IsStarted()) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 276 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 277 | } |
| 278 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 279 | size_t size = space->AllocationSize(obj); |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 280 | DCHECK_GT(size, 0u); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 281 | num_bytes_allocated_ += size; |
| 282 | num_objects_allocated_ += 1; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 283 | |
| 284 | if (Runtime::Current()->HasStatsEnabled()) { |
| 285 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 286 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 287 | ++global_stats->allocated_objects; |
| 288 | ++thread_stats->allocated_objects; |
| 289 | global_stats->allocated_bytes += size; |
| 290 | thread_stats->allocated_bytes += size; |
| 291 | } |
| 292 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 293 | live_bitmap_->Set(obj); |
| 294 | } |
| 295 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 296 | void Heap::RecordFreeLocked(size_t freed_objects, size_t freed_bytes) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 297 | lock_->AssertHeld(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 298 | |
| 299 | if (freed_objects < num_objects_allocated_) { |
| 300 | num_objects_allocated_ -= freed_objects; |
| 301 | } else { |
| 302 | num_objects_allocated_ = 0; |
| 303 | } |
| 304 | if (freed_bytes < num_bytes_allocated_) { |
| 305 | num_bytes_allocated_ -= freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 306 | } else { |
| 307 | num_bytes_allocated_ = 0; |
| 308 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 309 | |
| 310 | if (Runtime::Current()->HasStatsEnabled()) { |
| 311 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 312 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 313 | ++global_stats->freed_objects; |
| 314 | ++thread_stats->freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 315 | global_stats->freed_bytes += freed_bytes; |
| 316 | thread_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 317 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 320 | Object* Heap::AllocateLocked(size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 321 | lock_->AssertHeld(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 322 | DCHECK(alloc_space_ != NULL); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 323 | AllocSpace* space = alloc_space_; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 324 | Object* obj = AllocateLocked(space, size); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 325 | if (obj != NULL) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 326 | RecordAllocationLocked(space, obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 327 | } |
| 328 | return obj; |
| 329 | } |
| 330 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 331 | Object* Heap::AllocateLocked(AllocSpace* space, size_t alloc_size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 332 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 333 | |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 334 | // Since allocation can cause a GC which will need to SuspendAll, |
| 335 | // make sure all allocators are in the kRunnable state. |
Ian Rogers | f45b154 | 2012-02-03 18:03:48 -0800 | [diff] [blame^] | 336 | CHECK_EQ(Thread::Current()->GetState(), Thread::kRunnable); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 337 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 338 | // Fail impossible allocations |
| 339 | if (alloc_size > space->Capacity()) { |
| 340 | // On failure collect soft references |
| 341 | CollectGarbageInternal(true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 342 | return NULL; |
| 343 | } |
| 344 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 345 | Object* ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 346 | if (ptr != NULL) { |
| 347 | return ptr; |
| 348 | } |
| 349 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 350 | // The allocation failed. If the GC is running, block until it completes and retry. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 351 | if (is_gc_running_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 352 | // The GC is concurrently tracing the heap. Release the heap lock, wait for the GC to |
| 353 | // complete, and retrying allocating. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 354 | WaitForConcurrentGcToComplete(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 355 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 356 | if (ptr != NULL) { |
| 357 | return ptr; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // Another failure. Our thread was starved or there may be too many |
| 362 | // live objects. Try a foreground GC. This will have no effect if |
| 363 | // the concurrent GC is already running. |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 364 | if (Runtime::Current()->HasStatsEnabled()) { |
| 365 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 366 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 367 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 368 | CollectGarbageInternal(false); |
| 369 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 370 | if (ptr != NULL) { |
| 371 | return ptr; |
| 372 | } |
| 373 | |
| 374 | // Even that didn't work; this is an exceptional state. |
| 375 | // Try harder, growing the heap if necessary. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 376 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 377 | if (ptr != NULL) { |
| 378 | //size_t new_footprint = dvmHeapSourceGetIdealFootprint(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 379 | size_t new_footprint = space->GetFootprintLimit(); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 380 | // 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] | 381 | // free space is equal to the old free space + the |
| 382 | // utilization slop for the new allocation. |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 383 | VLOG(gc) << "Grow heap (frag case) to " << PrettySize(new_footprint) |
Ian Rogers | 162a31c | 2012-01-31 16:14:31 -0800 | [diff] [blame] | 384 | << " for a " << PrettySize(alloc_size) << " allocation"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 385 | return ptr; |
| 386 | } |
| 387 | |
| 388 | // Most allocations should have succeeded by now, so the heap is |
| 389 | // really full, really fragmented, or the requested size is really |
| 390 | // big. Do another GC, collecting SoftReferences this time. The VM |
| 391 | // spec requires that all SoftReferences have been collected and |
| 392 | // cleared before throwing an OOME. |
| 393 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 394 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 395 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) << " allocation"; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 396 | CollectGarbageInternal(true); |
| 397 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 398 | if (ptr != NULL) { |
| 399 | return ptr; |
| 400 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 401 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 402 | LOG(ERROR) << "Out of memory on a " << PrettySize(alloc_size) << " allocation"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 403 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 404 | // TODO: tell the HeapSource to dump its state |
| 405 | // TODO: dump stack traces for all threads |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 406 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 407 | return NULL; |
| 408 | } |
| 409 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 410 | int64_t Heap::GetMaxMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 411 | return alloc_space_->Capacity(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | int64_t Heap::GetTotalMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 415 | return alloc_space_->Capacity(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | int64_t Heap::GetFreeMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 419 | return alloc_space_->Capacity() - num_bytes_allocated_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 422 | class InstanceCounter { |
| 423 | public: |
| 424 | InstanceCounter(Class* c, bool count_assignable) |
| 425 | : class_(c), count_assignable_(count_assignable), count_(0) { |
| 426 | } |
| 427 | |
| 428 | size_t GetCount() { |
| 429 | return count_; |
| 430 | } |
| 431 | |
| 432 | static void Callback(Object* o, void* arg) { |
| 433 | reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o); |
| 434 | } |
| 435 | |
| 436 | private: |
| 437 | void VisitInstance(Object* o) { |
| 438 | Class* instance_class = o->GetClass(); |
| 439 | if (count_assignable_) { |
| 440 | if (instance_class == class_) { |
| 441 | ++count_; |
| 442 | } |
| 443 | } else { |
| 444 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
| 445 | ++count_; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | Class* class_; |
| 451 | bool count_assignable_; |
| 452 | size_t count_; |
| 453 | }; |
| 454 | |
| 455 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
| 456 | ScopedHeapLock lock; |
| 457 | InstanceCounter counter(c, count_assignable); |
| 458 | live_bitmap_->Walk(InstanceCounter::Callback, &counter); |
| 459 | return counter.GetCount(); |
| 460 | } |
| 461 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 462 | void Heap::CollectGarbage(bool clear_soft_references) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 463 | ScopedHeapLock lock; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 464 | CollectGarbageInternal(clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 467 | void Heap::CollectGarbageInternal(bool clear_soft_references) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 468 | lock_->AssertHeld(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 469 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 470 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 471 | thread_list->SuspendAll(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 472 | |
| 473 | size_t initial_size = num_bytes_allocated_; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 474 | TimingLogger timings("CollectGarbageInternal"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 475 | uint64_t t0 = NanoTime(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 476 | Object* cleared_references = NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 477 | { |
| 478 | MarkSweep mark_sweep; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 479 | timings.AddSplit("ctor"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 480 | |
| 481 | mark_sweep.Init(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 482 | timings.AddSplit("Init"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 483 | |
| 484 | mark_sweep.MarkRoots(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 485 | timings.AddSplit("MarkRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 486 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 487 | mark_sweep.ScanDirtyImageRoots(); |
| 488 | timings.AddSplit("DirtyImageRoots"); |
| 489 | |
| 490 | // Roots are marked on the bitmap and the mark_stack is empty |
| 491 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 492 | |
| 493 | // TODO: if concurrent |
| 494 | // unlock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 495 | // thread_list->ResumeAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 496 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 497 | // Recursively mark all bits set in the non-image mark bitmap |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 498 | mark_sweep.RecursiveMark(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 499 | timings.AddSplit("RecursiveMark"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 500 | |
| 501 | // TODO: if concurrent |
| 502 | // lock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 503 | // thread_list->SuspendAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 504 | // re-mark root set |
| 505 | // scan dirty objects |
| 506 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 507 | mark_sweep.ProcessReferences(clear_soft_references); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 508 | timings.AddSplit("ProcessReferences"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 509 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 510 | // TODO: if concurrent |
| 511 | // swap bitmaps |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 512 | |
| 513 | mark_sweep.Sweep(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 514 | timings.AddSplit("Sweep"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 515 | |
| 516 | cleared_references = mark_sweep.GetClearedReferences(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | GrowForUtilization(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 520 | timings.AddSplit("GrowForUtilization"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 521 | uint64_t t1 = NanoTime(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 522 | thread_list->ResumeAll(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 523 | |
| 524 | EnqueueClearedReferences(&cleared_references); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 525 | RequestHeapTrim(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 526 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 527 | uint64_t duration_ns = t1 - t0; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 528 | bool gc_was_particularly_slow = duration_ns > MsToNs(50); // TODO: crank this down for concurrent. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 529 | if (VLOG_IS_ON(gc) || gc_was_particularly_slow) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 530 | // TODO: somehow make the specific GC implementation (here MarkSweep) responsible for logging. |
| 531 | size_t bytes_freed = initial_size - num_bytes_allocated_; |
| 532 | if (bytes_freed > KB) { // ignore freed bytes in output if > 1KB |
| 533 | bytes_freed = RoundDown(bytes_freed, KB); |
| 534 | } |
| 535 | size_t bytes_allocated = RoundUp(num_bytes_allocated_, KB); |
| 536 | // lose low nanoseconds in duration. TODO: make this part of PrettyDuration |
| 537 | duration_ns = (duration_ns / 1000) * 1000; |
| 538 | size_t total = GetTotalMemory(); |
| 539 | size_t percentFree = 100 - static_cast<size_t>(100.0f * static_cast<float>(num_bytes_allocated_) / total); |
| 540 | LOG(INFO) << "GC freed " << PrettySize(bytes_freed) << ", " << percentFree << "% free, " |
| 541 | << PrettySize(bytes_allocated) << "/" << PrettySize(total) << ", " |
| 542 | << "paused " << PrettyDuration(duration_ns); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 543 | } |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 544 | Dbg::GcDidFinish(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 545 | if (VLOG_IS_ON(heap)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 546 | timings.Dump(); |
| 547 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void Heap::WaitForConcurrentGcToComplete() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 551 | lock_->AssertHeld(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 554 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 555 | size_t alloc_space_capacity = alloc_space_->Capacity(); |
| 556 | if (max_allowed_footprint > alloc_space_capacity) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 557 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) |
| 558 | << " to " << PrettySize(alloc_space_capacity); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 559 | max_allowed_footprint = alloc_space_capacity; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 560 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 561 | alloc_space_->SetFootprintLimit(max_allowed_footprint); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 564 | // kHeapIdealFree is the ideal maximum free size, when we grow the heap for utilization. |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 565 | static const size_t kHeapIdealFree = 2 * MB; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 566 | // kHeapMinFree guarantees that you always have at least 512 KB free, when you grow for utilization, |
| 567 | // regardless of target utilization ratio. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 568 | static const size_t kHeapMinFree = kHeapIdealFree / 4; |
| 569 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 570 | void Heap::GrowForUtilization() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 571 | lock_->AssertHeld(); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 572 | |
| 573 | // We know what our utilization is at this moment. |
| 574 | // This doesn't actually resize any memory. It just lets the heap grow more |
| 575 | // when necessary. |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 576 | size_t target_size(num_bytes_allocated_ / Heap::GetTargetHeapUtilization()); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 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 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 587 | void Heap::ClearGrowthLimit() { |
| 588 | ScopedHeapLock lock; |
| 589 | WaitForConcurrentGcToComplete(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 590 | alloc_space_->ClearGrowthLimit(); |
| 591 | } |
| 592 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 593 | pid_t Heap::GetLockOwner() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 594 | return lock_->GetOwner(); |
| 595 | } |
| 596 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 597 | void Heap::Lock() { |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 598 | // Grab the lock, but put ourselves into Thread::kVmWait if it looks |
| 599 | // like we're going to have to wait on the mutex. This prevents |
| 600 | // deadlock if another thread is calling CollectGarbageInternal, |
| 601 | // since they will have the heap lock and be waiting for mutators to |
| 602 | // suspend. |
| 603 | if (!lock_->TryLock()) { |
| 604 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 605 | lock_->Lock(); |
| 606 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | void Heap::Unlock() { |
| 610 | lock_->Unlock(); |
| 611 | } |
| 612 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 613 | void Heap::SetWellKnownClasses(Class* java_lang_ref_FinalizerReference, |
| 614 | Class* java_lang_ref_ReferenceQueue) { |
| 615 | java_lang_ref_FinalizerReference_ = java_lang_ref_FinalizerReference; |
| 616 | java_lang_ref_ReferenceQueue_ = java_lang_ref_ReferenceQueue; |
| 617 | CHECK(java_lang_ref_FinalizerReference_ != NULL); |
| 618 | CHECK(java_lang_ref_ReferenceQueue_ != NULL); |
| 619 | } |
| 620 | |
| 621 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 622 | MemberOffset reference_queue_offset, |
| 623 | MemberOffset reference_queueNext_offset, |
| 624 | MemberOffset reference_pendingNext_offset, |
| 625 | MemberOffset finalizer_reference_zombie_offset) { |
| 626 | reference_referent_offset_ = reference_referent_offset; |
| 627 | reference_queue_offset_ = reference_queue_offset; |
| 628 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 629 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 630 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 631 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 632 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 633 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 634 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 635 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 636 | } |
| 637 | |
| 638 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 639 | DCHECK(reference != NULL); |
| 640 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 641 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 642 | } |
| 643 | |
| 644 | void Heap::ClearReferenceReferent(Object* reference) { |
| 645 | DCHECK(reference != NULL); |
| 646 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 647 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 648 | } |
| 649 | |
| 650 | // Returns true if the reference object has not yet been enqueued. |
| 651 | bool Heap::IsEnqueuable(const Object* ref) { |
| 652 | DCHECK(ref != NULL); |
| 653 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 654 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 655 | return (queue != NULL) && (queue_next == NULL); |
| 656 | } |
| 657 | |
| 658 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 659 | DCHECK(ref != NULL); |
| 660 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 661 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 662 | EnqueuePendingReference(ref, cleared_reference_list); |
| 663 | } |
| 664 | |
| 665 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 666 | DCHECK(ref != NULL); |
| 667 | DCHECK(list != NULL); |
| 668 | |
| 669 | if (*list == NULL) { |
| 670 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 671 | *list = ref; |
| 672 | } else { |
| 673 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 674 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 675 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | Object* Heap::DequeuePendingReference(Object** list) { |
| 680 | DCHECK(list != NULL); |
| 681 | DCHECK(*list != NULL); |
| 682 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 683 | Object* ref; |
| 684 | if (*list == head) { |
| 685 | ref = *list; |
| 686 | *list = NULL; |
| 687 | } else { |
| 688 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 689 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 690 | ref = head; |
| 691 | } |
| 692 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 693 | return ref; |
| 694 | } |
| 695 | |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 696 | void Heap::AddFinalizerReference(Thread* self, Object* object) { |
| 697 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 698 | static Method* FinalizerReference_add = |
| 699 | java_lang_ref_FinalizerReference_->FindDirectMethod("add", "(Ljava/lang/Object;)V"); |
| 700 | DCHECK(FinalizerReference_add != NULL); |
| 701 | Object* args[] = { object }; |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 702 | FinalizerReference_add->Invoke(self, NULL, reinterpret_cast<byte*>(&args), NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 706 | DCHECK(cleared != NULL); |
| 707 | if (*cleared != NULL) { |
| 708 | static Method* ReferenceQueue_add = |
| 709 | java_lang_ref_ReferenceQueue_->FindDirectMethod("add", "(Ljava/lang/ref/Reference;)V"); |
| 710 | DCHECK(ReferenceQueue_add != NULL); |
| 711 | |
| 712 | Thread* self = Thread::Current(); |
| 713 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 714 | Object* args[] = { *cleared }; |
| 715 | ReferenceQueue_add->Invoke(self, NULL, reinterpret_cast<byte*>(&args), NULL); |
| 716 | *cleared = NULL; |
| 717 | } |
| 718 | } |
| 719 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 720 | void Heap::RequestHeapTrim() { |
| 721 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 722 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 723 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 724 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 725 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 726 | // not how much use we're making of those pages. |
| 727 | float utilization = static_cast<float>(num_bytes_allocated_) / alloc_space_->Size(); |
| 728 | if (utilization > 0.75f) { |
| 729 | // Don't bother trimming the heap if it's more than 75% utilized. |
| 730 | // (This percentage was picked arbitrarily.) |
| 731 | return; |
| 732 | } |
Ian Rogers | e1d490c | 2012-02-03 09:09:07 -0800 | [diff] [blame] | 733 | if (!Runtime::Current()->IsStarted()) { |
| 734 | // Heap trimming isn't supported without a Java runtime (such as at dex2oat time) |
| 735 | return; |
| 736 | } |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 737 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 738 | static jclass Daemons_class = CacheClass(env, "java/lang/Daemons"); |
| 739 | static jmethodID Daemons_requestHeapTrim = env->GetStaticMethodID(Daemons_class, "requestHeapTrim", "()V"); |
| 740 | env->CallStaticVoidMethod(Daemons_class, Daemons_requestHeapTrim); |
| 741 | CHECK(!env->ExceptionCheck()); |
| 742 | } |
| 743 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 744 | } // namespace art |