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