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