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