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