Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "concurrent_copying.h" |
| 18 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 20 | #include "base/enums.h" |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 21 | #include "base/histogram-inl.h" |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 23 | #include "base/systrace.h" |
Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 24 | #include "debugger.h" |
Andreas Gampe | 291ce17 | 2017-04-24 13:22:18 -0700 | [diff] [blame] | 25 | #include "gc/accounting/atomic_stack.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 26 | #include "gc/accounting/heap_bitmap-inl.h" |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 27 | #include "gc/accounting/mod_union_table-inl.h" |
Andreas Gampe | 291ce17 | 2017-04-24 13:22:18 -0700 | [diff] [blame] | 28 | #include "gc/accounting/read_barrier_table.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 29 | #include "gc/accounting/space_bitmap-inl.h" |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 30 | #include "gc/gc_pause_listener.h" |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 31 | #include "gc/reference_processor.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 32 | #include "gc/space/image_space.h" |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 33 | #include "gc/space/space-inl.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 34 | #include "gc/verification.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 35 | #include "image-inl.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 36 | #include "intern_table.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "mirror/class-inl.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 38 | #include "mirror/object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 39 | #include "mirror/object-refvisitor-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 40 | #include "scoped_thread_state_change-inl.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 41 | #include "thread-inl.h" |
| 42 | #include "thread_list.h" |
| 43 | #include "well_known_classes.h" |
| 44 | |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 45 | namespace art { |
| 46 | namespace gc { |
| 47 | namespace collector { |
| 48 | |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 49 | static constexpr size_t kDefaultGcMarkStackSize = 2 * MB; |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 50 | // If kFilterModUnionCards then we attempt to filter cards that don't need to be dirty in the mod |
| 51 | // union table. Disabled since it does not seem to help the pause much. |
| 52 | static constexpr bool kFilterModUnionCards = kIsDebugBuild; |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 53 | // If kDisallowReadBarrierDuringScan is true then the GC aborts if there are any that occur during |
| 54 | // ConcurrentCopying::Scan. May be used to diagnose possibly unnecessary read barriers. |
| 55 | // Only enabled for kIsDebugBuild to avoid performance hit. |
| 56 | static constexpr bool kDisallowReadBarrierDuringScan = kIsDebugBuild; |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 57 | // Slow path mark stack size, increase this if the stack is getting full and it is causing |
| 58 | // performance problems. |
| 59 | static constexpr size_t kReadBarrierMarkStackSize = 512 * KB; |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 60 | // Verify that there are no missing card marks. |
| 61 | static constexpr bool kVerifyNoMissingCardMarks = kIsDebugBuild; |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 62 | |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 63 | ConcurrentCopying::ConcurrentCopying(Heap* heap, |
| 64 | const std::string& name_prefix, |
| 65 | bool measure_read_barrier_slow_path) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 66 | : GarbageCollector(heap, |
| 67 | name_prefix + (name_prefix.empty() ? "" : " ") + |
Hiroshi Yamauchi | 88e0816 | 2017-01-06 15:03:26 -0800 | [diff] [blame] | 68 | "concurrent copying"), |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 69 | region_space_(nullptr), gc_barrier_(new Barrier(0)), |
| 70 | gc_mark_stack_(accounting::ObjectStack::Create("concurrent copying gc mark stack", |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 71 | kDefaultGcMarkStackSize, |
| 72 | kDefaultGcMarkStackSize)), |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 73 | rb_mark_bit_stack_(accounting::ObjectStack::Create("rb copying gc mark stack", |
| 74 | kReadBarrierMarkStackSize, |
| 75 | kReadBarrierMarkStackSize)), |
| 76 | rb_mark_bit_stack_full_(false), |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 77 | mark_stack_lock_("concurrent copying mark stack lock", kMarkSweepMarkStackLock), |
| 78 | thread_running_gc_(nullptr), |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 79 | is_marking_(false), |
| 80 | is_active_(false), |
| 81 | is_asserting_to_space_invariant_(false), |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 82 | region_space_bitmap_(nullptr), |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 83 | heap_mark_bitmap_(nullptr), |
| 84 | live_stack_freeze_size_(0), |
| 85 | from_space_num_objects_at_first_pause_(0), |
| 86 | from_space_num_bytes_at_first_pause_(0), |
| 87 | mark_stack_mode_(kMarkStackModeOff), |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 88 | weak_ref_access_enabled_(true), |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 89 | skipped_blocks_lock_("concurrent copying bytes blocks lock", kMarkSweepMarkStackLock), |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 90 | measure_read_barrier_slow_path_(measure_read_barrier_slow_path), |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 91 | mark_from_read_barrier_measurements_(false), |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 92 | rb_slow_path_ns_(0), |
| 93 | rb_slow_path_count_(0), |
| 94 | rb_slow_path_count_gc_(0), |
| 95 | rb_slow_path_histogram_lock_("Read barrier histogram lock"), |
| 96 | rb_slow_path_time_histogram_("Mutator time in read barrier slow path", 500, 32), |
| 97 | rb_slow_path_count_total_(0), |
| 98 | rb_slow_path_count_gc_total_(0), |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 99 | rb_table_(heap_->GetReadBarrierTable()), |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 100 | force_evacuate_all_(false), |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 101 | gc_grays_immune_objects_(false), |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 102 | immune_gray_stack_lock_("concurrent copying immune gray stack lock", |
| 103 | kMarkSweepMarkStackLock) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 104 | static_assert(space::RegionSpace::kRegionSize == accounting::ReadBarrierTable::kRegionSize, |
| 105 | "The region space size and the read barrier table region size must match"); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 106 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 107 | { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 108 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 109 | // Cache this so that we won't have to lock heap_bitmap_lock_ in |
| 110 | // Mark() which could cause a nested lock on heap_bitmap_lock_ |
| 111 | // when GC causes a RB while doing GC or a lock order violation |
| 112 | // (class_linker_lock_ and heap_bitmap_lock_). |
| 113 | heap_mark_bitmap_ = heap->GetMarkBitmap(); |
| 114 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 115 | { |
| 116 | MutexLock mu(self, mark_stack_lock_); |
| 117 | for (size_t i = 0; i < kMarkStackPoolSize; ++i) { |
| 118 | accounting::AtomicStack<mirror::Object>* mark_stack = |
| 119 | accounting::AtomicStack<mirror::Object>::Create( |
| 120 | "thread local mark stack", kMarkStackSize, kMarkStackSize); |
| 121 | pooled_mark_stacks_.push_back(mark_stack); |
| 122 | } |
| 123 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Hiroshi Yamauchi | 057d977 | 2017-02-17 15:33:23 -0800 | [diff] [blame] | 126 | void ConcurrentCopying::MarkHeapReference(mirror::HeapReference<mirror::Object>* field, |
| 127 | bool do_atomic_update) { |
| 128 | if (UNLIKELY(do_atomic_update)) { |
| 129 | // Used to mark the referent in DelayReferenceReferent in transaction mode. |
| 130 | mirror::Object* from_ref = field->AsMirrorPtr(); |
| 131 | if (from_ref == nullptr) { |
| 132 | return; |
| 133 | } |
| 134 | mirror::Object* to_ref = Mark(from_ref); |
| 135 | if (from_ref != to_ref) { |
| 136 | do { |
| 137 | if (field->AsMirrorPtr() != from_ref) { |
| 138 | // Concurrently overwritten by a mutator. |
| 139 | break; |
| 140 | } |
| 141 | } while (!field->CasWeakRelaxed(from_ref, to_ref)); |
| 142 | } |
| 143 | } else { |
| 144 | // Used for preserving soft references, should be OK to not have a CAS here since there should be |
| 145 | // no other threads which can trigger read barriers on the same referent during reference |
| 146 | // processing. |
| 147 | field->Assign(Mark(field->AsMirrorPtr())); |
| 148 | } |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 151 | ConcurrentCopying::~ConcurrentCopying() { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 152 | STLDeleteElements(&pooled_mark_stacks_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void ConcurrentCopying::RunPhases() { |
| 156 | CHECK(kUseBakerReadBarrier || kUseTableLookupReadBarrier); |
| 157 | CHECK(!is_active_); |
| 158 | is_active_ = true; |
| 159 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 160 | thread_running_gc_ = self; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 161 | Locks::mutator_lock_->AssertNotHeld(self); |
| 162 | { |
| 163 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 164 | InitializePhase(); |
| 165 | } |
| 166 | FlipThreadRoots(); |
| 167 | { |
| 168 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 169 | MarkingPhase(); |
| 170 | } |
| 171 | // Verify no from space refs. This causes a pause. |
Andreas Gampe | e3ce787 | 2017-02-22 13:36:21 -0800 | [diff] [blame] | 172 | if (kEnableNoFromSpaceRefsVerification) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 173 | TimingLogger::ScopedTiming split("(Paused)VerifyNoFromSpaceReferences", GetTimings()); |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 174 | ScopedPause pause(this, false); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 175 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 176 | if (kVerboseMode) { |
| 177 | LOG(INFO) << "Verifying no from-space refs"; |
| 178 | } |
| 179 | VerifyNoFromSpaceReferences(); |
Mathieu Chartier | 720e71a | 2015-04-06 17:10:58 -0700 | [diff] [blame] | 180 | if (kVerboseMode) { |
| 181 | LOG(INFO) << "Done verifying no from-space refs"; |
| 182 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 183 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 184 | } |
| 185 | { |
| 186 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 187 | ReclaimPhase(); |
| 188 | } |
| 189 | FinishPhase(); |
| 190 | CHECK(is_active_); |
| 191 | is_active_ = false; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 192 | thread_running_gc_ = nullptr; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void ConcurrentCopying::BindBitmaps() { |
| 196 | Thread* self = Thread::Current(); |
| 197 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 198 | // Mark all of the spaces we never collect as immune. |
| 199 | for (const auto& space : heap_->GetContinuousSpaces()) { |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 200 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect || |
| 201 | space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 202 | CHECK(space->IsZygoteSpace() || space->IsImageSpace()); |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 203 | immune_spaces_.AddSpace(space); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 204 | } else if (space == region_space_) { |
Mathieu Chartier | 7ec38dc | 2016-10-07 15:24:46 -0700 | [diff] [blame] | 205 | // It is OK to clear the bitmap with mutators running since the only place it is read is |
| 206 | // VisitObjects which has exclusion with CC. |
| 207 | region_space_bitmap_ = region_space_->GetMarkBitmap(); |
| 208 | region_space_bitmap_->Clear(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void ConcurrentCopying::InitializePhase() { |
| 214 | TimingLogger::ScopedTiming split("InitializePhase", GetTimings()); |
| 215 | if (kVerboseMode) { |
| 216 | LOG(INFO) << "GC InitializePhase"; |
| 217 | LOG(INFO) << "Region-space : " << reinterpret_cast<void*>(region_space_->Begin()) << "-" |
| 218 | << reinterpret_cast<void*>(region_space_->Limit()); |
| 219 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 220 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 221 | if (kIsDebugBuild) { |
| 222 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 223 | CHECK(false_gray_stack_.empty()); |
| 224 | } |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 225 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 226 | rb_mark_bit_stack_full_ = false; |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 227 | mark_from_read_barrier_measurements_ = measure_read_barrier_slow_path_; |
| 228 | if (measure_read_barrier_slow_path_) { |
| 229 | rb_slow_path_ns_.StoreRelaxed(0); |
| 230 | rb_slow_path_count_.StoreRelaxed(0); |
| 231 | rb_slow_path_count_gc_.StoreRelaxed(0); |
| 232 | } |
| 233 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 234 | immune_spaces_.Reset(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 235 | bytes_moved_.StoreRelaxed(0); |
| 236 | objects_moved_.StoreRelaxed(0); |
Hiroshi Yamauchi | 60985b7 | 2016-08-24 13:53:12 -0700 | [diff] [blame] | 237 | GcCause gc_cause = GetCurrentIteration()->GetGcCause(); |
| 238 | if (gc_cause == kGcCauseExplicit || |
| 239 | gc_cause == kGcCauseForNativeAlloc || |
| 240 | gc_cause == kGcCauseCollectorTransition || |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 241 | GetCurrentIteration()->GetClearSoftReferences()) { |
| 242 | force_evacuate_all_ = true; |
| 243 | } else { |
| 244 | force_evacuate_all_ = false; |
| 245 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 246 | if (kUseBakerReadBarrier) { |
| 247 | updated_all_immune_objects_.StoreRelaxed(false); |
| 248 | // GC may gray immune objects in the thread flip. |
| 249 | gc_grays_immune_objects_ = true; |
| 250 | if (kIsDebugBuild) { |
| 251 | MutexLock mu(Thread::Current(), immune_gray_stack_lock_); |
| 252 | DCHECK(immune_gray_stack_.empty()); |
| 253 | } |
| 254 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 255 | BindBitmaps(); |
| 256 | if (kVerboseMode) { |
| 257 | LOG(INFO) << "force_evacuate_all=" << force_evacuate_all_; |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 258 | LOG(INFO) << "Largest immune region: " << immune_spaces_.GetLargestImmuneRegion().Begin() |
| 259 | << "-" << immune_spaces_.GetLargestImmuneRegion().End(); |
| 260 | for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) { |
| 261 | LOG(INFO) << "Immune space: " << *space; |
| 262 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 263 | LOG(INFO) << "GC end of InitializePhase"; |
| 264 | } |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 265 | // Mark all of the zygote large objects without graying them. |
| 266 | MarkZygoteLargeObjects(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Used to switch the thread roots of a thread from from-space refs to to-space refs. |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 270 | class ConcurrentCopying::ThreadFlipVisitor : public Closure, public RootVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 271 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 272 | ThreadFlipVisitor(ConcurrentCopying* concurrent_copying, bool use_tlab) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 273 | : concurrent_copying_(concurrent_copying), use_tlab_(use_tlab) { |
| 274 | } |
| 275 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 276 | virtual void Run(Thread* thread) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 277 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 278 | Thread* self = Thread::Current(); |
| 279 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 280 | << thread->GetState() << " thread " << thread << " self " << self; |
Mathieu Chartier | fe814e8 | 2016-11-09 14:32:49 -0800 | [diff] [blame] | 281 | thread->SetIsGcMarkingAndUpdateEntrypoints(true); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 282 | if (use_tlab_ && thread->HasTlab()) { |
| 283 | if (ConcurrentCopying::kEnableFromSpaceAccountingCheck) { |
| 284 | // This must come before the revoke. |
| 285 | size_t thread_local_objects = thread->GetThreadLocalObjectsAllocated(); |
| 286 | concurrent_copying_->region_space_->RevokeThreadLocalBuffers(thread); |
| 287 | reinterpret_cast<Atomic<size_t>*>(&concurrent_copying_->from_space_num_objects_at_first_pause_)-> |
| 288 | FetchAndAddSequentiallyConsistent(thread_local_objects); |
| 289 | } else { |
| 290 | concurrent_copying_->region_space_->RevokeThreadLocalBuffers(thread); |
| 291 | } |
| 292 | } |
| 293 | if (kUseThreadLocalAllocationStack) { |
| 294 | thread->RevokeThreadLocalAllocationStack(); |
| 295 | } |
| 296 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 297 | // We can use the non-CAS VisitRoots functions below because we update thread-local GC roots |
| 298 | // only. |
| 299 | thread->VisitRoots(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 300 | concurrent_copying_->GetBarrier().Pass(self); |
| 301 | } |
| 302 | |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 303 | void VisitRoots(mirror::Object*** roots, |
| 304 | size_t count, |
| 305 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 306 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 307 | for (size_t i = 0; i < count; ++i) { |
| 308 | mirror::Object** root = roots[i]; |
| 309 | mirror::Object* ref = *root; |
| 310 | if (ref != nullptr) { |
| 311 | mirror::Object* to_ref = concurrent_copying_->Mark(ref); |
| 312 | if (to_ref != ref) { |
| 313 | *root = to_ref; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, |
| 320 | size_t count, |
| 321 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 322 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 323 | for (size_t i = 0; i < count; ++i) { |
| 324 | mirror::CompressedReference<mirror::Object>* const root = roots[i]; |
| 325 | if (!root->IsNull()) { |
| 326 | mirror::Object* ref = root->AsMirrorPtr(); |
| 327 | mirror::Object* to_ref = concurrent_copying_->Mark(ref); |
| 328 | if (to_ref != ref) { |
| 329 | root->Assign(to_ref); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 335 | private: |
| 336 | ConcurrentCopying* const concurrent_copying_; |
| 337 | const bool use_tlab_; |
| 338 | }; |
| 339 | |
| 340 | // Called back from Runtime::FlipThreadRoots() during a pause. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 341 | class ConcurrentCopying::FlipCallback : public Closure { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 342 | public: |
| 343 | explicit FlipCallback(ConcurrentCopying* concurrent_copying) |
| 344 | : concurrent_copying_(concurrent_copying) { |
| 345 | } |
| 346 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 347 | virtual void Run(Thread* thread) OVERRIDE REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 348 | ConcurrentCopying* cc = concurrent_copying_; |
| 349 | TimingLogger::ScopedTiming split("(Paused)FlipCallback", cc->GetTimings()); |
| 350 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 351 | Thread* self = Thread::Current(); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 352 | if (kVerifyNoMissingCardMarks) { |
| 353 | cc->VerifyNoMissingCardMarks(); |
| 354 | } |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 355 | CHECK(thread == self); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 356 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 357 | cc->region_space_->SetFromSpace(cc->rb_table_, cc->force_evacuate_all_); |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 358 | cc->SwapStacks(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 359 | if (ConcurrentCopying::kEnableFromSpaceAccountingCheck) { |
| 360 | cc->RecordLiveStackFreezeSize(self); |
| 361 | cc->from_space_num_objects_at_first_pause_ = cc->region_space_->GetObjectsAllocated(); |
| 362 | cc->from_space_num_bytes_at_first_pause_ = cc->region_space_->GetBytesAllocated(); |
| 363 | } |
| 364 | cc->is_marking_ = true; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 365 | cc->mark_stack_mode_.StoreRelaxed(ConcurrentCopying::kMarkStackModeThreadLocal); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 366 | if (kIsDebugBuild) { |
| 367 | cc->region_space_->AssertAllRegionLiveBytesZeroOrCleared(); |
| 368 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 369 | if (UNLIKELY(Runtime::Current()->IsActiveTransaction())) { |
Mathieu Chartier | 184c9dc | 2015-03-05 13:20:54 -0800 | [diff] [blame] | 370 | CHECK(Runtime::Current()->IsAotCompiler()); |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 371 | TimingLogger::ScopedTiming split2("(Paused)VisitTransactionRoots", cc->GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 372 | Runtime::Current()->VisitTransactionRoots(cc); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 373 | } |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 374 | if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects) { |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 375 | cc->GrayAllDirtyImmuneObjects(); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 376 | if (kIsDebugBuild) { |
| 377 | // Check that all non-gray immune objects only refernce immune objects. |
| 378 | cc->VerifyGrayImmuneObjects(); |
| 379 | } |
| 380 | } |
Mathieu Chartier | 9aef992 | 2017-04-23 13:53:50 -0700 | [diff] [blame] | 381 | // May be null during runtime creation, in this case leave java_lang_Object null. |
| 382 | // This is safe since single threaded behavior should mean FillDummyObject does not |
| 383 | // happen when java_lang_Object_ is null. |
| 384 | if (WellKnownClasses::java_lang_Object != nullptr) { |
| 385 | cc->java_lang_Object_ = down_cast<mirror::Class*>(cc->Mark( |
| 386 | WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object).Ptr())); |
| 387 | } else { |
| 388 | cc->java_lang_Object_ = nullptr; |
| 389 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private: |
| 393 | ConcurrentCopying* const concurrent_copying_; |
| 394 | }; |
| 395 | |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 396 | class ConcurrentCopying::VerifyGrayImmuneObjectsVisitor { |
| 397 | public: |
| 398 | explicit VerifyGrayImmuneObjectsVisitor(ConcurrentCopying* collector) |
| 399 | : collector_(collector) {} |
| 400 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 401 | void operator()(ObjPtr<mirror::Object> obj, MemberOffset offset, bool /* is_static */) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 402 | const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) |
| 403 | REQUIRES_SHARED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 404 | CheckReference(obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset), |
| 405 | obj, offset); |
| 406 | } |
| 407 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 408 | void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 409 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 410 | CHECK(klass->IsTypeOfReferenceClass()); |
| 411 | CheckReference(ref->GetReferent<kWithoutReadBarrier>(), |
| 412 | ref, |
| 413 | mirror::Reference::ReferentOffset()); |
| 414 | } |
| 415 | |
| 416 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 417 | ALWAYS_INLINE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 418 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 419 | if (!root->IsNull()) { |
| 420 | VisitRoot(root); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 425 | ALWAYS_INLINE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 426 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 427 | CheckReference(root->AsMirrorPtr(), nullptr, MemberOffset(0)); |
| 428 | } |
| 429 | |
| 430 | private: |
| 431 | ConcurrentCopying* const collector_; |
| 432 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 433 | void CheckReference(ObjPtr<mirror::Object> ref, |
| 434 | ObjPtr<mirror::Object> holder, |
| 435 | MemberOffset offset) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 436 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 437 | if (ref != nullptr) { |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 438 | if (!collector_->immune_spaces_.ContainsObject(ref.Ptr())) { |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 439 | // Not immune, must be a zygote large object. |
| 440 | CHECK(Runtime::Current()->GetHeap()->GetLargeObjectsSpace()->IsZygoteLargeObject( |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 441 | Thread::Current(), ref.Ptr())) |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 442 | << "Non gray object references non immune, non zygote large object "<< ref << " " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 443 | << mirror::Object::PrettyTypeOf(ref) << " in holder " << holder << " " |
| 444 | << mirror::Object::PrettyTypeOf(holder) << " offset=" << offset.Uint32Value(); |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 445 | } else { |
| 446 | // Make sure the large object class is immune since we will never scan the large object. |
| 447 | CHECK(collector_->immune_spaces_.ContainsObject( |
| 448 | ref->GetClass<kVerifyNone, kWithoutReadBarrier>())); |
| 449 | } |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | }; |
| 453 | |
| 454 | void ConcurrentCopying::VerifyGrayImmuneObjects() { |
| 455 | TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings()); |
| 456 | for (auto& space : immune_spaces_.GetSpaces()) { |
| 457 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
| 458 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 459 | VerifyGrayImmuneObjectsVisitor visitor(this); |
| 460 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 461 | reinterpret_cast<uintptr_t>(space->Limit()), |
| 462 | [&visitor](mirror::Object* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 463 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 464 | // If an object is not gray, it should only have references to things in the immune spaces. |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 465 | if (obj->GetReadBarrierState() != ReadBarrier::GrayState()) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 466 | obj->VisitReferences</*kVisitNativeRoots*/true, |
| 467 | kDefaultVerifyFlags, |
| 468 | kWithoutReadBarrier>(visitor, visitor); |
| 469 | } |
| 470 | }); |
| 471 | } |
| 472 | } |
| 473 | |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 474 | class ConcurrentCopying::VerifyNoMissingCardMarkVisitor { |
| 475 | public: |
| 476 | VerifyNoMissingCardMarkVisitor(ConcurrentCopying* cc, ObjPtr<mirror::Object> holder) |
| 477 | : cc_(cc), |
| 478 | holder_(holder) {} |
| 479 | |
| 480 | void operator()(ObjPtr<mirror::Object> obj, |
| 481 | MemberOffset offset, |
| 482 | bool is_static ATTRIBUTE_UNUSED) const |
| 483 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 484 | if (offset.Uint32Value() != mirror::Object::ClassOffset().Uint32Value()) { |
| 485 | CheckReference(obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>( |
| 486 | offset), offset.Uint32Value()); |
| 487 | } |
| 488 | } |
| 489 | void operator()(ObjPtr<mirror::Class> klass, |
| 490 | ObjPtr<mirror::Reference> ref) const |
| 491 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 492 | CHECK(klass->IsTypeOfReferenceClass()); |
| 493 | this->operator()(ref, mirror::Reference::ReferentOffset(), false); |
| 494 | } |
| 495 | |
| 496 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 497 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 498 | if (!root->IsNull()) { |
| 499 | VisitRoot(root); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 504 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 505 | CheckReference(root->AsMirrorPtr()); |
| 506 | } |
| 507 | |
| 508 | void CheckReference(mirror::Object* ref, int32_t offset = -1) const |
| 509 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 510 | CHECK(ref == nullptr || !cc_->region_space_->IsInNewlyAllocatedRegion(ref)) |
| 511 | << holder_->PrettyTypeOf() << "(" << holder_.Ptr() << ") references object " |
| 512 | << ref->PrettyTypeOf() << "(" << ref << ") in newly allocated region at offset=" << offset; |
| 513 | } |
| 514 | |
| 515 | private: |
| 516 | ConcurrentCopying* const cc_; |
| 517 | ObjPtr<mirror::Object> const holder_; |
| 518 | }; |
| 519 | |
| 520 | void ConcurrentCopying::VerifyNoMissingCardMarkCallback(mirror::Object* obj, void* arg) { |
| 521 | auto* collector = reinterpret_cast<ConcurrentCopying*>(arg); |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 522 | // Objects not on dirty cards should never have references to newly allocated regions. |
| 523 | if (!collector->heap_->GetCardTable()->IsDirty(obj)) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 524 | VerifyNoMissingCardMarkVisitor visitor(collector, /*holder*/ obj); |
| 525 | obj->VisitReferences</*kVisitNativeRoots*/true, kVerifyNone, kWithoutReadBarrier>( |
| 526 | visitor, |
| 527 | visitor); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | void ConcurrentCopying::VerifyNoMissingCardMarks() { |
| 532 | TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings()); |
| 533 | region_space_->Walk(&VerifyNoMissingCardMarkCallback, this); |
| 534 | { |
| 535 | ReaderMutexLock rmu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 536 | heap_->GetLiveBitmap()->Walk(&VerifyNoMissingCardMarkCallback, this); |
| 537 | } |
| 538 | } |
| 539 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 540 | // Switch threads that from from-space to to-space refs. Forward/mark the thread roots. |
| 541 | void ConcurrentCopying::FlipThreadRoots() { |
| 542 | TimingLogger::ScopedTiming split("FlipThreadRoots", GetTimings()); |
| 543 | if (kVerboseMode) { |
| 544 | LOG(INFO) << "time=" << region_space_->Time(); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 545 | region_space_->DumpNonFreeRegions(LOG_STREAM(INFO)); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 546 | } |
| 547 | Thread* self = Thread::Current(); |
| 548 | Locks::mutator_lock_->AssertNotHeld(self); |
| 549 | gc_barrier_->Init(self, 0); |
| 550 | ThreadFlipVisitor thread_flip_visitor(this, heap_->use_tlab_); |
| 551 | FlipCallback flip_callback(this); |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 552 | |
| 553 | // This is the point where Concurrent-Copying will pause all threads. We report a pause here, if |
| 554 | // necessary. This is slightly over-reporting, as this includes the time to actually suspend |
| 555 | // threads. |
| 556 | { |
| 557 | GcPauseListener* pause_listener = GetHeap()->GetGcPauseListener(); |
| 558 | if (pause_listener != nullptr) { |
| 559 | pause_listener->StartPause(); |
| 560 | } |
| 561 | } |
| 562 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 563 | size_t barrier_count = Runtime::Current()->FlipThreadRoots( |
| 564 | &thread_flip_visitor, &flip_callback, this); |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 565 | |
| 566 | { |
| 567 | GcPauseListener* pause_listener = GetHeap()->GetGcPauseListener(); |
| 568 | if (pause_listener != nullptr) { |
| 569 | pause_listener->EndPause(); |
| 570 | } |
| 571 | } |
| 572 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 573 | { |
| 574 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 575 | gc_barrier_->Increment(self, barrier_count); |
| 576 | } |
| 577 | is_asserting_to_space_invariant_ = true; |
| 578 | QuasiAtomic::ThreadFenceForConstructor(); |
| 579 | if (kVerboseMode) { |
| 580 | LOG(INFO) << "time=" << region_space_->Time(); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 581 | region_space_->DumpNonFreeRegions(LOG_STREAM(INFO)); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 582 | LOG(INFO) << "GC end of FlipThreadRoots"; |
| 583 | } |
| 584 | } |
| 585 | |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 586 | class ConcurrentCopying::GrayImmuneObjectVisitor { |
| 587 | public: |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 588 | explicit GrayImmuneObjectVisitor() {} |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 589 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 590 | ALWAYS_INLINE void operator()(mirror::Object* obj) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 591 | if (kUseBakerReadBarrier) { |
| 592 | if (kIsDebugBuild) { |
| 593 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 594 | } |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 595 | obj->SetReadBarrierState(ReadBarrier::GrayState()); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 599 | static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 600 | reinterpret_cast<GrayImmuneObjectVisitor*>(arg)->operator()(obj); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 601 | } |
| 602 | }; |
| 603 | |
| 604 | void ConcurrentCopying::GrayAllDirtyImmuneObjects() { |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 605 | TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings()); |
| 606 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 607 | accounting::CardTable* const card_table = heap->GetCardTable(); |
| 608 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 609 | for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) { |
| 610 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 611 | GrayImmuneObjectVisitor visitor; |
| 612 | accounting::ModUnionTable* table = heap->FindModUnionTableFromSpace(space); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 613 | // Mark all the objects on dirty cards since these may point to objects in other space. |
| 614 | // Once these are marked, the GC will eventually clear them later. |
| 615 | // Table is non null for boot image and zygote spaces. It is only null for application image |
| 616 | // spaces. |
| 617 | if (table != nullptr) { |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 618 | // TODO: Consider adding precleaning outside the pause. |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 619 | table->ProcessCards(); |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 620 | table->VisitObjects(GrayImmuneObjectVisitor::Callback, &visitor); |
| 621 | // Since the cards are recorded in the mod-union table and this is paused, we can clear |
| 622 | // the cards for the space (to madvise). |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 623 | TimingLogger::ScopedTiming split2("(Paused)ClearCards", GetTimings()); |
| 624 | card_table->ClearCardRange(space->Begin(), |
| 625 | AlignDown(space->End(), accounting::CardTable::kCardSize)); |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 626 | } else { |
| 627 | // TODO: Consider having a mark bitmap for app image spaces and avoid scanning during the |
| 628 | // pause because app image spaces are all dirty pages anyways. |
| 629 | card_table->Scan<false>(space->GetMarkBitmap(), space->Begin(), space->End(), visitor); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
Mathieu Chartier | c83dd7b | 2017-05-02 04:14:17 +0000 | [diff] [blame] | 632 | // Since all of the objects that may point to other spaces are marked, we can avoid all the read |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 633 | // barriers in the immune spaces. |
| 634 | updated_all_immune_objects_.StoreRelaxed(true); |
| 635 | } |
| 636 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 637 | void ConcurrentCopying::SwapStacks() { |
| 638 | heap_->SwapStacks(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void ConcurrentCopying::RecordLiveStackFreezeSize(Thread* self) { |
| 642 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 643 | live_stack_freeze_size_ = heap_->GetLiveStack()->Size(); |
| 644 | } |
| 645 | |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 646 | // Used to visit objects in the immune spaces. |
| 647 | inline void ConcurrentCopying::ScanImmuneObject(mirror::Object* obj) { |
| 648 | DCHECK(obj != nullptr); |
| 649 | DCHECK(immune_spaces_.ContainsObject(obj)); |
| 650 | // Update the fields without graying it or pushing it onto the mark stack. |
| 651 | Scan(obj); |
| 652 | } |
| 653 | |
| 654 | class ConcurrentCopying::ImmuneSpaceScanObjVisitor { |
| 655 | public: |
| 656 | explicit ImmuneSpaceScanObjVisitor(ConcurrentCopying* cc) |
| 657 | : collector_(cc) {} |
| 658 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 659 | ALWAYS_INLINE void operator()(mirror::Object* obj) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 660 | if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 661 | if (obj->GetReadBarrierState() == ReadBarrier::GrayState()) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 662 | collector_->ScanImmuneObject(obj); |
| 663 | // Done scanning the object, go back to white. |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 664 | bool success = obj->AtomicSetReadBarrierState(ReadBarrier::GrayState(), |
| 665 | ReadBarrier::WhiteState()); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 666 | CHECK(success); |
| 667 | } |
| 668 | } else { |
| 669 | collector_->ScanImmuneObject(obj); |
| 670 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 673 | static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 5408f23 | 2016-07-29 15:07:05 -0700 | [diff] [blame] | 674 | reinterpret_cast<ImmuneSpaceScanObjVisitor*>(arg)->operator()(obj); |
| 675 | } |
| 676 | |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 677 | private: |
| 678 | ConcurrentCopying* const collector_; |
| 679 | }; |
| 680 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 681 | // Concurrently mark roots that are guarded by read barriers and process the mark stack. |
| 682 | void ConcurrentCopying::MarkingPhase() { |
| 683 | TimingLogger::ScopedTiming split("MarkingPhase", GetTimings()); |
| 684 | if (kVerboseMode) { |
| 685 | LOG(INFO) << "GC MarkingPhase"; |
| 686 | } |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 687 | Thread* self = Thread::Current(); |
| 688 | if (kIsDebugBuild) { |
| 689 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 690 | CHECK(weak_ref_access_enabled_); |
| 691 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 692 | |
| 693 | // Scan immune spaces. |
| 694 | // Update all the fields in the immune spaces first without graying the objects so that we |
| 695 | // minimize dirty pages in the immune spaces. Note mutators can concurrently access and gray some |
| 696 | // of the objects. |
| 697 | if (kUseBakerReadBarrier) { |
| 698 | gc_grays_immune_objects_ = false; |
Hiroshi Yamauchi | 16292fc | 2016-06-20 20:23:34 -0700 | [diff] [blame] | 699 | } |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 700 | { |
| 701 | TimingLogger::ScopedTiming split2("ScanImmuneSpaces", GetTimings()); |
| 702 | for (auto& space : immune_spaces_.GetSpaces()) { |
| 703 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
| 704 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Hiroshi Yamauchi | 5408f23 | 2016-07-29 15:07:05 -0700 | [diff] [blame] | 705 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 706 | ImmuneSpaceScanObjVisitor visitor(this); |
Hiroshi Yamauchi | 5408f23 | 2016-07-29 15:07:05 -0700 | [diff] [blame] | 707 | if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects && table != nullptr) { |
| 708 | table->VisitObjects(ImmuneSpaceScanObjVisitor::Callback, &visitor); |
| 709 | } else { |
| 710 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 711 | reinterpret_cast<uintptr_t>(space->Limit()), |
| 712 | visitor); |
| 713 | } |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 714 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 715 | } |
| 716 | if (kUseBakerReadBarrier) { |
| 717 | // This release fence makes the field updates in the above loop visible before allowing mutator |
| 718 | // getting access to immune objects without graying it first. |
| 719 | updated_all_immune_objects_.StoreRelease(true); |
| 720 | // Now whiten immune objects concurrently accessed and grayed by mutators. We can't do this in |
| 721 | // the above loop because we would incorrectly disable the read barrier by whitening an object |
| 722 | // which may point to an unscanned, white object, breaking the to-space invariant. |
| 723 | // |
| 724 | // Make sure no mutators are in the middle of marking an immune object before whitening immune |
| 725 | // objects. |
| 726 | IssueEmptyCheckpoint(); |
| 727 | MutexLock mu(Thread::Current(), immune_gray_stack_lock_); |
| 728 | if (kVerboseMode) { |
| 729 | LOG(INFO) << "immune gray stack size=" << immune_gray_stack_.size(); |
| 730 | } |
| 731 | for (mirror::Object* obj : immune_gray_stack_) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 732 | DCHECK(obj->GetReadBarrierState() == ReadBarrier::GrayState()); |
| 733 | bool success = obj->AtomicSetReadBarrierState(ReadBarrier::GrayState(), |
| 734 | ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 735 | DCHECK(success); |
| 736 | } |
| 737 | immune_gray_stack_.clear(); |
| 738 | } |
| 739 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 740 | { |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 741 | TimingLogger::ScopedTiming split2("VisitConcurrentRoots", GetTimings()); |
| 742 | Runtime::Current()->VisitConcurrentRoots(this, kVisitRootFlagAllRoots); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 743 | } |
| 744 | { |
| 745 | // TODO: don't visit the transaction roots if it's not active. |
| 746 | TimingLogger::ScopedTiming split5("VisitNonThreadRoots", GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 747 | Runtime::Current()->VisitNonThreadRoots(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 750 | { |
Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 751 | TimingLogger::ScopedTiming split7("ProcessMarkStack", GetTimings()); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 752 | // We transition through three mark stack modes (thread-local, shared, GC-exclusive). The |
| 753 | // primary reasons are the fact that we need to use a checkpoint to process thread-local mark |
| 754 | // stacks, but after we disable weak refs accesses, we can't use a checkpoint due to a deadlock |
| 755 | // issue because running threads potentially blocking at WaitHoldingLocks, and that once we |
| 756 | // reach the point where we process weak references, we can avoid using a lock when accessing |
| 757 | // the GC mark stack, which makes mark stack processing more efficient. |
| 758 | |
| 759 | // Process the mark stack once in the thread local stack mode. This marks most of the live |
| 760 | // objects, aside from weak ref accesses with read barriers (Reference::GetReferent() and system |
| 761 | // weaks) that may happen concurrently while we processing the mark stack and newly mark/gray |
| 762 | // objects and push refs on the mark stack. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 763 | ProcessMarkStack(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 764 | // Switch to the shared mark stack mode. That is, revoke and process thread-local mark stacks |
| 765 | // for the last time before transitioning to the shared mark stack mode, which would process new |
| 766 | // refs that may have been concurrently pushed onto the mark stack during the ProcessMarkStack() |
| 767 | // call above. At the same time, disable weak ref accesses using a per-thread flag. It's |
| 768 | // important to do these together in a single checkpoint so that we can ensure that mutators |
| 769 | // won't newly gray objects and push new refs onto the mark stack due to weak ref accesses and |
| 770 | // mutators safely transition to the shared mark stack mode (without leaving unprocessed refs on |
| 771 | // the thread-local mark stacks), without a race. This is why we use a thread-local weak ref |
| 772 | // access flag Thread::tls32_.weak_ref_access_enabled_ instead of the global ones. |
| 773 | SwitchToSharedMarkStackMode(); |
| 774 | CHECK(!self->GetWeakRefAccessEnabled()); |
| 775 | // Now that weak refs accesses are disabled, once we exhaust the shared mark stack again here |
| 776 | // (which may be non-empty if there were refs found on thread-local mark stacks during the above |
| 777 | // SwitchToSharedMarkStackMode() call), we won't have new refs to process, that is, mutators |
| 778 | // (via read barriers) have no way to produce any more refs to process. Marking converges once |
| 779 | // before we process weak refs below. |
| 780 | ProcessMarkStack(); |
| 781 | CheckEmptyMarkStack(); |
| 782 | // Switch to the GC exclusive mark stack mode so that we can process the mark stack without a |
| 783 | // lock from this point on. |
| 784 | SwitchToGcExclusiveMarkStackMode(); |
| 785 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 786 | if (kVerboseMode) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 787 | LOG(INFO) << "ProcessReferences"; |
| 788 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 789 | // Process weak references. This may produce new refs to process and have them processed via |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 790 | // ProcessMarkStack (in the GC exclusive mark stack mode). |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 791 | ProcessReferences(self); |
| 792 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 793 | if (kVerboseMode) { |
| 794 | LOG(INFO) << "SweepSystemWeaks"; |
| 795 | } |
| 796 | SweepSystemWeaks(self); |
| 797 | if (kVerboseMode) { |
| 798 | LOG(INFO) << "SweepSystemWeaks done"; |
| 799 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 800 | // Process the mark stack here one last time because the above SweepSystemWeaks() call may have |
| 801 | // marked some objects (strings alive) as hash_set::Erase() can call the hash function for |
| 802 | // arbitrary elements in the weak intern table in InternTable::Table::SweepWeaks(). |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 803 | ProcessMarkStack(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 804 | CheckEmptyMarkStack(); |
| 805 | // Re-enable weak ref accesses. |
| 806 | ReenableWeakRefAccess(self); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 807 | // Free data for class loaders that we unloaded. |
| 808 | Runtime::Current()->GetClassLinker()->CleanupClassLoaders(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 809 | // Marking is done. Disable marking. |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 810 | DisableMarking(); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 811 | if (kUseBakerReadBarrier) { |
| 812 | ProcessFalseGrayStack(); |
| 813 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 814 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 817 | if (kIsDebugBuild) { |
| 818 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 819 | CHECK(weak_ref_access_enabled_); |
| 820 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 821 | if (kVerboseMode) { |
| 822 | LOG(INFO) << "GC end of MarkingPhase"; |
| 823 | } |
| 824 | } |
| 825 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 826 | void ConcurrentCopying::ReenableWeakRefAccess(Thread* self) { |
| 827 | if (kVerboseMode) { |
| 828 | LOG(INFO) << "ReenableWeakRefAccess"; |
| 829 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 830 | // Iterate all threads (don't need to or can't use a checkpoint) and re-enable weak ref access. |
| 831 | { |
| 832 | MutexLock mu(self, *Locks::thread_list_lock_); |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 833 | weak_ref_access_enabled_ = true; // This is for new threads. |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 834 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 835 | for (Thread* thread : thread_list) { |
| 836 | thread->SetWeakRefAccessEnabled(true); |
| 837 | } |
| 838 | } |
| 839 | // Unblock blocking threads. |
| 840 | GetHeap()->GetReferenceProcessor()->BroadcastForSlowPath(self); |
| 841 | Runtime::Current()->BroadcastForNewSystemWeaks(); |
| 842 | } |
| 843 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 844 | class ConcurrentCopying::DisableMarkingCheckpoint : public Closure { |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 845 | public: |
| 846 | explicit DisableMarkingCheckpoint(ConcurrentCopying* concurrent_copying) |
| 847 | : concurrent_copying_(concurrent_copying) { |
| 848 | } |
| 849 | |
| 850 | void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS { |
| 851 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 852 | Thread* self = Thread::Current(); |
| 853 | DCHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 854 | << thread->GetState() << " thread " << thread << " self " << self; |
| 855 | // Disable the thread-local is_gc_marking flag. |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 856 | // Note a thread that has just started right before this checkpoint may have already this flag |
| 857 | // set to false, which is ok. |
Mathieu Chartier | fe814e8 | 2016-11-09 14:32:49 -0800 | [diff] [blame] | 858 | thread->SetIsGcMarkingAndUpdateEntrypoints(false); |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 859 | // If thread is a running mutator, then act on behalf of the garbage collector. |
| 860 | // See the code in ThreadList::RunCheckpoint. |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 861 | concurrent_copying_->GetBarrier().Pass(self); |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | private: |
| 865 | ConcurrentCopying* const concurrent_copying_; |
| 866 | }; |
| 867 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 868 | class ConcurrentCopying::DisableMarkingCallback : public Closure { |
| 869 | public: |
| 870 | explicit DisableMarkingCallback(ConcurrentCopying* concurrent_copying) |
| 871 | : concurrent_copying_(concurrent_copying) { |
| 872 | } |
| 873 | |
| 874 | void Run(Thread* self ATTRIBUTE_UNUSED) OVERRIDE REQUIRES(Locks::thread_list_lock_) { |
| 875 | // This needs to run under the thread_list_lock_ critical section in ThreadList::RunCheckpoint() |
| 876 | // to avoid a race with ThreadList::Register(). |
| 877 | CHECK(concurrent_copying_->is_marking_); |
| 878 | concurrent_copying_->is_marking_ = false; |
| 879 | } |
| 880 | |
| 881 | private: |
| 882 | ConcurrentCopying* const concurrent_copying_; |
| 883 | }; |
| 884 | |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 885 | void ConcurrentCopying::IssueDisableMarkingCheckpoint() { |
| 886 | Thread* self = Thread::Current(); |
| 887 | DisableMarkingCheckpoint check_point(this); |
| 888 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 889 | gc_barrier_->Init(self, 0); |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 890 | DisableMarkingCallback dmc(this); |
| 891 | size_t barrier_count = thread_list->RunCheckpoint(&check_point, &dmc); |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 892 | // If there are no threads to wait which implies that all the checkpoint functions are finished, |
| 893 | // then no need to release the mutator lock. |
| 894 | if (barrier_count == 0) { |
| 895 | return; |
| 896 | } |
| 897 | // Release locks then wait for all mutator threads to pass the barrier. |
| 898 | Locks::mutator_lock_->SharedUnlock(self); |
| 899 | { |
| 900 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 901 | gc_barrier_->Increment(self, barrier_count); |
| 902 | } |
| 903 | Locks::mutator_lock_->SharedLock(self); |
| 904 | } |
| 905 | |
| 906 | void ConcurrentCopying::DisableMarking() { |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 907 | // Use a checkpoint to turn off the global is_marking and the thread-local is_gc_marking flags and |
| 908 | // to ensure no threads are still in the middle of a read barrier which may have a from-space ref |
| 909 | // cached in a local variable. |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 910 | IssueDisableMarkingCheckpoint(); |
| 911 | if (kUseTableLookupReadBarrier) { |
| 912 | heap_->rb_table_->ClearAll(); |
| 913 | DCHECK(heap_->rb_table_->IsAllCleared()); |
| 914 | } |
| 915 | is_mark_stack_push_disallowed_.StoreSequentiallyConsistent(1); |
| 916 | mark_stack_mode_.StoreSequentiallyConsistent(kMarkStackModeOff); |
| 917 | } |
| 918 | |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 919 | void ConcurrentCopying::PushOntoFalseGrayStack(mirror::Object* ref) { |
| 920 | CHECK(kUseBakerReadBarrier); |
| 921 | DCHECK(ref != nullptr); |
| 922 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 923 | false_gray_stack_.push_back(ref); |
| 924 | } |
| 925 | |
| 926 | void ConcurrentCopying::ProcessFalseGrayStack() { |
| 927 | CHECK(kUseBakerReadBarrier); |
| 928 | // Change the objects on the false gray stack from gray to white. |
| 929 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 930 | for (mirror::Object* obj : false_gray_stack_) { |
| 931 | DCHECK(IsMarked(obj)); |
| 932 | // The object could be white here if a thread got preempted after a success at the |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 933 | // AtomicSetReadBarrierState in Mark(), GC started marking through it (but not finished so |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 934 | // still gray), and the thread ran to register it onto the false gray stack. |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 935 | if (obj->GetReadBarrierState() == ReadBarrier::GrayState()) { |
| 936 | bool success = obj->AtomicSetReadBarrierState(ReadBarrier::GrayState(), |
| 937 | ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 938 | DCHECK(success); |
| 939 | } |
| 940 | } |
| 941 | false_gray_stack_.clear(); |
| 942 | } |
| 943 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 944 | void ConcurrentCopying::IssueEmptyCheckpoint() { |
| 945 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 946 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 947 | // Release locks then wait for all mutator threads to pass the barrier. |
| 948 | Locks::mutator_lock_->SharedUnlock(self); |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 949 | thread_list->RunEmptyCheckpoint(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 950 | Locks::mutator_lock_->SharedLock(self); |
| 951 | } |
| 952 | |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 953 | void ConcurrentCopying::ExpandGcMarkStack() { |
| 954 | DCHECK(gc_mark_stack_->IsFull()); |
| 955 | const size_t new_size = gc_mark_stack_->Capacity() * 2; |
| 956 | std::vector<StackReference<mirror::Object>> temp(gc_mark_stack_->Begin(), |
| 957 | gc_mark_stack_->End()); |
| 958 | gc_mark_stack_->Resize(new_size); |
| 959 | for (auto& ref : temp) { |
| 960 | gc_mark_stack_->PushBack(ref.AsMirrorPtr()); |
| 961 | } |
| 962 | DCHECK(!gc_mark_stack_->IsFull()); |
| 963 | } |
| 964 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 965 | void ConcurrentCopying::PushOntoMarkStack(mirror::Object* to_ref) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 966 | CHECK_EQ(is_mark_stack_push_disallowed_.LoadRelaxed(), 0) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 967 | << " " << to_ref << " " << mirror::Object::PrettyTypeOf(to_ref); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 968 | Thread* self = Thread::Current(); // TODO: pass self as an argument from call sites? |
| 969 | CHECK(thread_running_gc_ != nullptr); |
| 970 | MarkStackMode mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 971 | if (LIKELY(mark_stack_mode == kMarkStackModeThreadLocal)) { |
| 972 | if (LIKELY(self == thread_running_gc_)) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 973 | // If GC-running thread, use the GC mark stack instead of a thread-local mark stack. |
| 974 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 975 | if (UNLIKELY(gc_mark_stack_->IsFull())) { |
| 976 | ExpandGcMarkStack(); |
| 977 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 978 | gc_mark_stack_->PushBack(to_ref); |
| 979 | } else { |
| 980 | // Otherwise, use a thread-local mark stack. |
| 981 | accounting::AtomicStack<mirror::Object>* tl_mark_stack = self->GetThreadLocalMarkStack(); |
| 982 | if (UNLIKELY(tl_mark_stack == nullptr || tl_mark_stack->IsFull())) { |
| 983 | MutexLock mu(self, mark_stack_lock_); |
| 984 | // Get a new thread local mark stack. |
| 985 | accounting::AtomicStack<mirror::Object>* new_tl_mark_stack; |
| 986 | if (!pooled_mark_stacks_.empty()) { |
| 987 | // Use a pooled mark stack. |
| 988 | new_tl_mark_stack = pooled_mark_stacks_.back(); |
| 989 | pooled_mark_stacks_.pop_back(); |
| 990 | } else { |
| 991 | // None pooled. Create a new one. |
| 992 | new_tl_mark_stack = |
| 993 | accounting::AtomicStack<mirror::Object>::Create( |
| 994 | "thread local mark stack", 4 * KB, 4 * KB); |
| 995 | } |
| 996 | DCHECK(new_tl_mark_stack != nullptr); |
| 997 | DCHECK(new_tl_mark_stack->IsEmpty()); |
| 998 | new_tl_mark_stack->PushBack(to_ref); |
| 999 | self->SetThreadLocalMarkStack(new_tl_mark_stack); |
| 1000 | if (tl_mark_stack != nullptr) { |
| 1001 | // Store the old full stack into a vector. |
| 1002 | revoked_mark_stacks_.push_back(tl_mark_stack); |
| 1003 | } |
| 1004 | } else { |
| 1005 | tl_mark_stack->PushBack(to_ref); |
| 1006 | } |
| 1007 | } |
| 1008 | } else if (mark_stack_mode == kMarkStackModeShared) { |
| 1009 | // Access the shared GC mark stack with a lock. |
| 1010 | MutexLock mu(self, mark_stack_lock_); |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 1011 | if (UNLIKELY(gc_mark_stack_->IsFull())) { |
| 1012 | ExpandGcMarkStack(); |
| 1013 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1014 | gc_mark_stack_->PushBack(to_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1015 | } else { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1016 | CHECK_EQ(static_cast<uint32_t>(mark_stack_mode), |
Hiroshi Yamauchi | fa75518 | 2015-09-30 20:12:11 -0700 | [diff] [blame] | 1017 | static_cast<uint32_t>(kMarkStackModeGcExclusive)) |
| 1018 | << "ref=" << to_ref |
| 1019 | << " self->gc_marking=" << self->GetIsGcMarking() |
| 1020 | << " cc->is_marking=" << is_marking_; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1021 | CHECK(self == thread_running_gc_) |
| 1022 | << "Only GC-running thread should access the mark stack " |
| 1023 | << "in the GC exclusive mark stack mode"; |
| 1024 | // Access the GC mark stack without a lock. |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 1025 | if (UNLIKELY(gc_mark_stack_->IsFull())) { |
| 1026 | ExpandGcMarkStack(); |
| 1027 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1028 | gc_mark_stack_->PushBack(to_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | accounting::ObjectStack* ConcurrentCopying::GetAllocationStack() { |
| 1033 | return heap_->allocation_stack_.get(); |
| 1034 | } |
| 1035 | |
| 1036 | accounting::ObjectStack* ConcurrentCopying::GetLiveStack() { |
| 1037 | return heap_->live_stack_.get(); |
| 1038 | } |
| 1039 | |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1040 | // The following visitors are used to verify that there's no references to the from-space left after |
| 1041 | // marking. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1042 | class ConcurrentCopying::VerifyNoFromSpaceRefsVisitor : public SingleRootVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1043 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1044 | explicit VerifyNoFromSpaceRefsVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1045 | : collector_(collector) {} |
| 1046 | |
Mathieu Chartier | bc632f0 | 2017-04-20 13:31:39 -0700 | [diff] [blame] | 1047 | void operator()(mirror::Object* ref, |
| 1048 | MemberOffset offset = MemberOffset(0), |
| 1049 | mirror::Object* holder = nullptr) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1050 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1051 | if (ref == nullptr) { |
| 1052 | // OK. |
| 1053 | return; |
| 1054 | } |
Mathieu Chartier | bc632f0 | 2017-04-20 13:31:39 -0700 | [diff] [blame] | 1055 | collector_->AssertToSpaceInvariant(holder, offset, ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1056 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1057 | CHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::WhiteState()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1058 | << "Ref " << ref << " " << ref->PrettyTypeOf() |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1059 | << " has non-white rb_state "; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1063 | void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1064 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1065 | DCHECK(root != nullptr); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1066 | operator()(root); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | private: |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1070 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1071 | }; |
| 1072 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1073 | class ConcurrentCopying::VerifyNoFromSpaceRefsFieldVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1074 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1075 | explicit VerifyNoFromSpaceRefsFieldVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1076 | : collector_(collector) {} |
| 1077 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1078 | void operator()(ObjPtr<mirror::Object> obj, |
| 1079 | MemberOffset offset, |
| 1080 | bool is_static ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1081 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1082 | mirror::Object* ref = |
| 1083 | obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1084 | VerifyNoFromSpaceRefsVisitor visitor(collector_); |
Mathieu Chartier | bc632f0 | 2017-04-20 13:31:39 -0700 | [diff] [blame] | 1085 | visitor(ref, offset, obj.Ptr()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1086 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1087 | void operator()(ObjPtr<mirror::Class> klass, |
| 1088 | ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1089 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1090 | CHECK(klass->IsTypeOfReferenceClass()); |
| 1091 | this->operator()(ref, mirror::Reference::ReferentOffset(), false); |
| 1092 | } |
| 1093 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1094 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1095 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1096 | if (!root->IsNull()) { |
| 1097 | VisitRoot(root); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1102 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1103 | VerifyNoFromSpaceRefsVisitor visitor(collector_); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1104 | visitor(root->AsMirrorPtr()); |
| 1105 | } |
| 1106 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1107 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1108 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1109 | }; |
| 1110 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1111 | class ConcurrentCopying::VerifyNoFromSpaceRefsObjectVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1112 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1113 | explicit VerifyNoFromSpaceRefsObjectVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1114 | : collector_(collector) {} |
| 1115 | void operator()(mirror::Object* obj) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1116 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1117 | ObjectCallback(obj, collector_); |
| 1118 | } |
| 1119 | static void ObjectCallback(mirror::Object* obj, void *arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1120 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1121 | CHECK(obj != nullptr); |
| 1122 | ConcurrentCopying* collector = reinterpret_cast<ConcurrentCopying*>(arg); |
| 1123 | space::RegionSpace* region_space = collector->RegionSpace(); |
| 1124 | CHECK(!region_space->IsInFromSpace(obj)) << "Scanning object " << obj << " in from space"; |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1125 | VerifyNoFromSpaceRefsFieldVisitor visitor(collector); |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1126 | obj->VisitReferences</*kVisitNativeRoots*/true, kDefaultVerifyFlags, kWithoutReadBarrier>( |
| 1127 | visitor, |
| 1128 | visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1129 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1130 | CHECK_EQ(obj->GetReadBarrierState(), ReadBarrier::WhiteState()) |
| 1131 | << "obj=" << obj << " non-white rb_state " << obj->GetReadBarrierState(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | private: |
| 1136 | ConcurrentCopying* const collector_; |
| 1137 | }; |
| 1138 | |
| 1139 | // Verify there's no from-space references left after the marking phase. |
| 1140 | void ConcurrentCopying::VerifyNoFromSpaceReferences() { |
| 1141 | Thread* self = Thread::Current(); |
| 1142 | DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self)); |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 1143 | // Verify all threads have is_gc_marking to be false |
| 1144 | { |
| 1145 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 1146 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 1147 | for (Thread* thread : thread_list) { |
| 1148 | CHECK(!thread->GetIsGcMarking()); |
| 1149 | } |
| 1150 | } |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1151 | VerifyNoFromSpaceRefsObjectVisitor visitor(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1152 | // Roots. |
| 1153 | { |
| 1154 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1155 | VerifyNoFromSpaceRefsVisitor ref_visitor(this); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1156 | Runtime::Current()->VisitRoots(&ref_visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1157 | } |
| 1158 | // The to-space. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1159 | region_space_->WalkToSpace(VerifyNoFromSpaceRefsObjectVisitor::ObjectCallback, this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1160 | // Non-moving spaces. |
| 1161 | { |
| 1162 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1163 | heap_->GetMarkBitmap()->Visit(visitor); |
| 1164 | } |
| 1165 | // The alloc stack. |
| 1166 | { |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1167 | VerifyNoFromSpaceRefsVisitor ref_visitor(this); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 1168 | for (auto* it = heap_->allocation_stack_->Begin(), *end = heap_->allocation_stack_->End(); |
| 1169 | it < end; ++it) { |
| 1170 | mirror::Object* const obj = it->AsMirrorPtr(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1171 | if (obj != nullptr && obj->GetClass() != nullptr) { |
| 1172 | // TODO: need to call this only if obj is alive? |
| 1173 | ref_visitor(obj); |
| 1174 | visitor(obj); |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | // TODO: LOS. But only refs in LOS are classes. |
| 1179 | } |
| 1180 | |
| 1181 | // The following visitors are used to assert the to-space invariant. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1182 | class ConcurrentCopying::AssertToSpaceInvariantRefsVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1183 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1184 | explicit AssertToSpaceInvariantRefsVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1185 | : collector_(collector) {} |
| 1186 | |
| 1187 | void operator()(mirror::Object* ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1188 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1189 | if (ref == nullptr) { |
| 1190 | // OK. |
| 1191 | return; |
| 1192 | } |
| 1193 | collector_->AssertToSpaceInvariant(nullptr, MemberOffset(0), ref); |
| 1194 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1195 | |
| 1196 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1197 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1198 | }; |
| 1199 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1200 | class ConcurrentCopying::AssertToSpaceInvariantFieldVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1201 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1202 | explicit AssertToSpaceInvariantFieldVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1203 | : collector_(collector) {} |
| 1204 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1205 | void operator()(ObjPtr<mirror::Object> obj, |
| 1206 | MemberOffset offset, |
| 1207 | bool is_static ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1208 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1209 | mirror::Object* ref = |
| 1210 | obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1211 | AssertToSpaceInvariantRefsVisitor visitor(collector_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1212 | visitor(ref); |
| 1213 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1214 | void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1215 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1216 | CHECK(klass->IsTypeOfReferenceClass()); |
| 1217 | } |
| 1218 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1219 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1220 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1221 | if (!root->IsNull()) { |
| 1222 | VisitRoot(root); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1227 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1228 | AssertToSpaceInvariantRefsVisitor visitor(collector_); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1229 | visitor(root->AsMirrorPtr()); |
| 1230 | } |
| 1231 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1232 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1233 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1234 | }; |
| 1235 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1236 | class ConcurrentCopying::AssertToSpaceInvariantObjectVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1237 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1238 | explicit AssertToSpaceInvariantObjectVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1239 | : collector_(collector) {} |
| 1240 | void operator()(mirror::Object* obj) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1241 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1242 | ObjectCallback(obj, collector_); |
| 1243 | } |
| 1244 | static void ObjectCallback(mirror::Object* obj, void *arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1245 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1246 | CHECK(obj != nullptr); |
| 1247 | ConcurrentCopying* collector = reinterpret_cast<ConcurrentCopying*>(arg); |
| 1248 | space::RegionSpace* region_space = collector->RegionSpace(); |
| 1249 | CHECK(!region_space->IsInFromSpace(obj)) << "Scanning object " << obj << " in from space"; |
| 1250 | collector->AssertToSpaceInvariant(nullptr, MemberOffset(0), obj); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1251 | AssertToSpaceInvariantFieldVisitor visitor(collector); |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1252 | obj->VisitReferences</*kVisitNativeRoots*/true, kDefaultVerifyFlags, kWithoutReadBarrier>( |
| 1253 | visitor, |
| 1254 | visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1258 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1259 | }; |
| 1260 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1261 | class ConcurrentCopying::RevokeThreadLocalMarkStackCheckpoint : public Closure { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1262 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 1263 | RevokeThreadLocalMarkStackCheckpoint(ConcurrentCopying* concurrent_copying, |
| 1264 | bool disable_weak_ref_access) |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1265 | : concurrent_copying_(concurrent_copying), |
| 1266 | disable_weak_ref_access_(disable_weak_ref_access) { |
| 1267 | } |
| 1268 | |
| 1269 | virtual void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS { |
| 1270 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 1271 | Thread* self = Thread::Current(); |
| 1272 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 1273 | << thread->GetState() << " thread " << thread << " self " << self; |
| 1274 | // Revoke thread local mark stacks. |
| 1275 | accounting::AtomicStack<mirror::Object>* tl_mark_stack = thread->GetThreadLocalMarkStack(); |
| 1276 | if (tl_mark_stack != nullptr) { |
| 1277 | MutexLock mu(self, concurrent_copying_->mark_stack_lock_); |
| 1278 | concurrent_copying_->revoked_mark_stacks_.push_back(tl_mark_stack); |
| 1279 | thread->SetThreadLocalMarkStack(nullptr); |
| 1280 | } |
| 1281 | // Disable weak ref access. |
| 1282 | if (disable_weak_ref_access_) { |
| 1283 | thread->SetWeakRefAccessEnabled(false); |
| 1284 | } |
| 1285 | // If thread is a running mutator, then act on behalf of the garbage collector. |
| 1286 | // See the code in ThreadList::RunCheckpoint. |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 1287 | concurrent_copying_->GetBarrier().Pass(self); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | private: |
| 1291 | ConcurrentCopying* const concurrent_copying_; |
| 1292 | const bool disable_weak_ref_access_; |
| 1293 | }; |
| 1294 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1295 | void ConcurrentCopying::RevokeThreadLocalMarkStacks(bool disable_weak_ref_access, |
| 1296 | Closure* checkpoint_callback) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1297 | Thread* self = Thread::Current(); |
| 1298 | RevokeThreadLocalMarkStackCheckpoint check_point(this, disable_weak_ref_access); |
| 1299 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1300 | gc_barrier_->Init(self, 0); |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1301 | size_t barrier_count = thread_list->RunCheckpoint(&check_point, checkpoint_callback); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1302 | // If there are no threads to wait which implys that all the checkpoint functions are finished, |
| 1303 | // then no need to release the mutator lock. |
| 1304 | if (barrier_count == 0) { |
| 1305 | return; |
| 1306 | } |
| 1307 | Locks::mutator_lock_->SharedUnlock(self); |
| 1308 | { |
| 1309 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 1310 | gc_barrier_->Increment(self, barrier_count); |
| 1311 | } |
| 1312 | Locks::mutator_lock_->SharedLock(self); |
| 1313 | } |
| 1314 | |
| 1315 | void ConcurrentCopying::RevokeThreadLocalMarkStack(Thread* thread) { |
| 1316 | Thread* self = Thread::Current(); |
| 1317 | CHECK_EQ(self, thread); |
| 1318 | accounting::AtomicStack<mirror::Object>* tl_mark_stack = thread->GetThreadLocalMarkStack(); |
| 1319 | if (tl_mark_stack != nullptr) { |
| 1320 | CHECK(is_marking_); |
| 1321 | MutexLock mu(self, mark_stack_lock_); |
| 1322 | revoked_mark_stacks_.push_back(tl_mark_stack); |
| 1323 | thread->SetThreadLocalMarkStack(nullptr); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | void ConcurrentCopying::ProcessMarkStack() { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1328 | if (kVerboseMode) { |
| 1329 | LOG(INFO) << "ProcessMarkStack. "; |
| 1330 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1331 | bool empty_prev = false; |
| 1332 | while (true) { |
| 1333 | bool empty = ProcessMarkStackOnce(); |
| 1334 | if (empty_prev && empty) { |
| 1335 | // Saw empty mark stack for a second time, done. |
| 1336 | break; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1337 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1338 | empty_prev = empty; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1339 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | bool ConcurrentCopying::ProcessMarkStackOnce() { |
| 1343 | Thread* self = Thread::Current(); |
| 1344 | CHECK(thread_running_gc_ != nullptr); |
| 1345 | CHECK(self == thread_running_gc_); |
| 1346 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
| 1347 | size_t count = 0; |
| 1348 | MarkStackMode mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
| 1349 | if (mark_stack_mode == kMarkStackModeThreadLocal) { |
| 1350 | // Process the thread-local mark stacks and the GC mark stack. |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1351 | count += ProcessThreadLocalMarkStacks(false, nullptr); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1352 | while (!gc_mark_stack_->IsEmpty()) { |
| 1353 | mirror::Object* to_ref = gc_mark_stack_->PopBack(); |
| 1354 | ProcessMarkStackRef(to_ref); |
| 1355 | ++count; |
| 1356 | } |
| 1357 | gc_mark_stack_->Reset(); |
| 1358 | } else if (mark_stack_mode == kMarkStackModeShared) { |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 1359 | // Do an empty checkpoint to avoid a race with a mutator preempted in the middle of a read |
| 1360 | // barrier but before pushing onto the mark stack. b/32508093. Note the weak ref access is |
| 1361 | // disabled at this point. |
| 1362 | IssueEmptyCheckpoint(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1363 | // Process the shared GC mark stack with a lock. |
| 1364 | { |
| 1365 | MutexLock mu(self, mark_stack_lock_); |
| 1366 | CHECK(revoked_mark_stacks_.empty()); |
| 1367 | } |
| 1368 | while (true) { |
| 1369 | std::vector<mirror::Object*> refs; |
| 1370 | { |
| 1371 | // Copy refs with lock. Note the number of refs should be small. |
| 1372 | MutexLock mu(self, mark_stack_lock_); |
| 1373 | if (gc_mark_stack_->IsEmpty()) { |
| 1374 | break; |
| 1375 | } |
| 1376 | for (StackReference<mirror::Object>* p = gc_mark_stack_->Begin(); |
| 1377 | p != gc_mark_stack_->End(); ++p) { |
| 1378 | refs.push_back(p->AsMirrorPtr()); |
| 1379 | } |
| 1380 | gc_mark_stack_->Reset(); |
| 1381 | } |
| 1382 | for (mirror::Object* ref : refs) { |
| 1383 | ProcessMarkStackRef(ref); |
| 1384 | ++count; |
| 1385 | } |
| 1386 | } |
| 1387 | } else { |
| 1388 | CHECK_EQ(static_cast<uint32_t>(mark_stack_mode), |
| 1389 | static_cast<uint32_t>(kMarkStackModeGcExclusive)); |
| 1390 | { |
| 1391 | MutexLock mu(self, mark_stack_lock_); |
| 1392 | CHECK(revoked_mark_stacks_.empty()); |
| 1393 | } |
| 1394 | // Process the GC mark stack in the exclusive mode. No need to take the lock. |
| 1395 | while (!gc_mark_stack_->IsEmpty()) { |
| 1396 | mirror::Object* to_ref = gc_mark_stack_->PopBack(); |
| 1397 | ProcessMarkStackRef(to_ref); |
| 1398 | ++count; |
| 1399 | } |
| 1400 | gc_mark_stack_->Reset(); |
| 1401 | } |
| 1402 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1403 | // Return true if the stack was empty. |
| 1404 | return count == 0; |
| 1405 | } |
| 1406 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1407 | size_t ConcurrentCopying::ProcessThreadLocalMarkStacks(bool disable_weak_ref_access, |
| 1408 | Closure* checkpoint_callback) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1409 | // Run a checkpoint to collect all thread local mark stacks and iterate over them all. |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1410 | RevokeThreadLocalMarkStacks(disable_weak_ref_access, checkpoint_callback); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1411 | size_t count = 0; |
| 1412 | std::vector<accounting::AtomicStack<mirror::Object>*> mark_stacks; |
| 1413 | { |
| 1414 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 1415 | // Make a copy of the mark stack vector. |
| 1416 | mark_stacks = revoked_mark_stacks_; |
| 1417 | revoked_mark_stacks_.clear(); |
| 1418 | } |
| 1419 | for (accounting::AtomicStack<mirror::Object>* mark_stack : mark_stacks) { |
| 1420 | for (StackReference<mirror::Object>* p = mark_stack->Begin(); p != mark_stack->End(); ++p) { |
| 1421 | mirror::Object* to_ref = p->AsMirrorPtr(); |
| 1422 | ProcessMarkStackRef(to_ref); |
| 1423 | ++count; |
| 1424 | } |
| 1425 | { |
| 1426 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 1427 | if (pooled_mark_stacks_.size() >= kMarkStackPoolSize) { |
| 1428 | // The pool has enough. Delete it. |
| 1429 | delete mark_stack; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1430 | } else { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1431 | // Otherwise, put it into the pool for later reuse. |
| 1432 | mark_stack->Reset(); |
| 1433 | pooled_mark_stacks_.push_back(mark_stack); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1434 | } |
| 1435 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1436 | } |
| 1437 | return count; |
| 1438 | } |
| 1439 | |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1440 | inline void ConcurrentCopying::ProcessMarkStackRef(mirror::Object* to_ref) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1441 | DCHECK(!region_space_->IsInFromSpace(to_ref)); |
| 1442 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1443 | DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState()) |
| 1444 | << " " << to_ref << " " << to_ref->GetReadBarrierState() |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1445 | << " is_marked=" << IsMarked(to_ref); |
| 1446 | } |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1447 | bool add_to_live_bytes = false; |
| 1448 | if (region_space_->IsInUnevacFromSpace(to_ref)) { |
| 1449 | // Mark the bitmap only in the GC thread here so that we don't need a CAS. |
| 1450 | if (!kUseBakerReadBarrier || !region_space_bitmap_->Set(to_ref)) { |
| 1451 | // It may be already marked if we accidentally pushed the same object twice due to the racy |
| 1452 | // bitmap read in MarkUnevacFromSpaceRegion. |
| 1453 | Scan(to_ref); |
| 1454 | // Only add to the live bytes if the object was not already marked. |
| 1455 | add_to_live_bytes = true; |
| 1456 | } |
| 1457 | } else { |
| 1458 | Scan(to_ref); |
| 1459 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1460 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1461 | DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState()) |
| 1462 | << " " << to_ref << " " << to_ref->GetReadBarrierState() |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1463 | << " is_marked=" << IsMarked(to_ref); |
| 1464 | } |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1465 | #ifdef USE_BAKER_OR_BROOKS_READ_BARRIER |
Hiroshi Yamauchi | 39c12d4 | 2016-12-06 16:46:37 -0800 | [diff] [blame] | 1466 | mirror::Object* referent = nullptr; |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1467 | if (UNLIKELY((to_ref->GetClass<kVerifyNone, kWithoutReadBarrier>()->IsTypeOfReferenceClass() && |
Hiroshi Yamauchi | 39c12d4 | 2016-12-06 16:46:37 -0800 | [diff] [blame] | 1468 | (referent = to_ref->AsReference()->GetReferent<kWithoutReadBarrier>()) != nullptr && |
| 1469 | !IsInToSpace(referent)))) { |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1470 | // Leave this reference gray in the queue so that GetReferent() will trigger a read barrier. We |
| 1471 | // will change it to white later in ReferenceQueue::DequeuePendingReference(). |
Richard Uhler | e362740 | 2016-02-02 13:36:55 -0800 | [diff] [blame] | 1472 | DCHECK(to_ref->AsReference()->GetPendingNext() != nullptr) << "Left unenqueued ref gray " << to_ref; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1473 | } else { |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1474 | // We may occasionally leave a reference white in the queue if its referent happens to be |
| 1475 | // concurrently marked after the Scan() call above has enqueued the Reference, in which case the |
| 1476 | // above IsInToSpace() evaluates to true and we change the color from gray to white here in this |
| 1477 | // else block. |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1478 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1479 | bool success = to_ref->AtomicSetReadBarrierState</*kCasRelease*/true>( |
| 1480 | ReadBarrier::GrayState(), |
| 1481 | ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1482 | DCHECK(success) << "Must succeed as we won the race."; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1483 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1484 | } |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1485 | #else |
| 1486 | DCHECK(!kUseBakerReadBarrier); |
| 1487 | #endif |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1488 | |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1489 | if (add_to_live_bytes) { |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1490 | // Add to the live bytes per unevacuated from space. Note this code is always run by the |
| 1491 | // GC-running thread (no synchronization required). |
| 1492 | DCHECK(region_space_bitmap_->Test(to_ref)); |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1493 | size_t obj_size = to_ref->SizeOf<kDefaultVerifyFlags>(); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1494 | size_t alloc_size = RoundUp(obj_size, space::RegionSpace::kAlignment); |
| 1495 | region_space_->AddLiveBytes(to_ref, alloc_size); |
| 1496 | } |
Andreas Gampe | e3ce787 | 2017-02-22 13:36:21 -0800 | [diff] [blame] | 1497 | if (ReadBarrier::kEnableToSpaceInvariantChecks) { |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1498 | AssertToSpaceInvariantObjectVisitor visitor(this); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1499 | visitor(to_ref); |
| 1500 | } |
| 1501 | } |
| 1502 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1503 | class ConcurrentCopying::DisableWeakRefAccessCallback : public Closure { |
| 1504 | public: |
| 1505 | explicit DisableWeakRefAccessCallback(ConcurrentCopying* concurrent_copying) |
| 1506 | : concurrent_copying_(concurrent_copying) { |
| 1507 | } |
| 1508 | |
| 1509 | void Run(Thread* self ATTRIBUTE_UNUSED) OVERRIDE REQUIRES(Locks::thread_list_lock_) { |
| 1510 | // This needs to run under the thread_list_lock_ critical section in ThreadList::RunCheckpoint() |
| 1511 | // to avoid a deadlock b/31500969. |
| 1512 | CHECK(concurrent_copying_->weak_ref_access_enabled_); |
| 1513 | concurrent_copying_->weak_ref_access_enabled_ = false; |
| 1514 | } |
| 1515 | |
| 1516 | private: |
| 1517 | ConcurrentCopying* const concurrent_copying_; |
| 1518 | }; |
| 1519 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1520 | void ConcurrentCopying::SwitchToSharedMarkStackMode() { |
| 1521 | Thread* self = Thread::Current(); |
| 1522 | CHECK(thread_running_gc_ != nullptr); |
| 1523 | CHECK_EQ(self, thread_running_gc_); |
| 1524 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
| 1525 | MarkStackMode before_mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
| 1526 | CHECK_EQ(static_cast<uint32_t>(before_mark_stack_mode), |
| 1527 | static_cast<uint32_t>(kMarkStackModeThreadLocal)); |
| 1528 | mark_stack_mode_.StoreRelaxed(kMarkStackModeShared); |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1529 | DisableWeakRefAccessCallback dwrac(this); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1530 | // Process the thread local mark stacks one last time after switching to the shared mark stack |
| 1531 | // mode and disable weak ref accesses. |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1532 | ProcessThreadLocalMarkStacks(true, &dwrac); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1533 | if (kVerboseMode) { |
| 1534 | LOG(INFO) << "Switched to shared mark stack mode and disabled weak ref access"; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | void ConcurrentCopying::SwitchToGcExclusiveMarkStackMode() { |
| 1539 | Thread* self = Thread::Current(); |
| 1540 | CHECK(thread_running_gc_ != nullptr); |
| 1541 | CHECK_EQ(self, thread_running_gc_); |
| 1542 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
| 1543 | MarkStackMode before_mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
| 1544 | CHECK_EQ(static_cast<uint32_t>(before_mark_stack_mode), |
| 1545 | static_cast<uint32_t>(kMarkStackModeShared)); |
| 1546 | mark_stack_mode_.StoreRelaxed(kMarkStackModeGcExclusive); |
| 1547 | QuasiAtomic::ThreadFenceForConstructor(); |
| 1548 | if (kVerboseMode) { |
| 1549 | LOG(INFO) << "Switched to GC exclusive mark stack mode"; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | void ConcurrentCopying::CheckEmptyMarkStack() { |
| 1554 | Thread* self = Thread::Current(); |
| 1555 | CHECK(thread_running_gc_ != nullptr); |
| 1556 | CHECK_EQ(self, thread_running_gc_); |
| 1557 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
| 1558 | MarkStackMode mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
| 1559 | if (mark_stack_mode == kMarkStackModeThreadLocal) { |
| 1560 | // Thread-local mark stack mode. |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1561 | RevokeThreadLocalMarkStacks(false, nullptr); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1562 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 1563 | if (!revoked_mark_stacks_.empty()) { |
| 1564 | for (accounting::AtomicStack<mirror::Object>* mark_stack : revoked_mark_stacks_) { |
| 1565 | while (!mark_stack->IsEmpty()) { |
| 1566 | mirror::Object* obj = mark_stack->PopBack(); |
| 1567 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1568 | uint32_t rb_state = obj->GetReadBarrierState(); |
| 1569 | LOG(INFO) << "On mark queue : " << obj << " " << obj->PrettyTypeOf() << " rb_state=" |
| 1570 | << rb_state << " is_marked=" << IsMarked(obj); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1571 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1572 | LOG(INFO) << "On mark queue : " << obj << " " << obj->PrettyTypeOf() |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1573 | << " is_marked=" << IsMarked(obj); |
| 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | LOG(FATAL) << "mark stack is not empty"; |
| 1578 | } |
| 1579 | } else { |
| 1580 | // Shared, GC-exclusive, or off. |
| 1581 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 1582 | CHECK(gc_mark_stack_->IsEmpty()); |
| 1583 | CHECK(revoked_mark_stacks_.empty()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | void ConcurrentCopying::SweepSystemWeaks(Thread* self) { |
| 1588 | TimingLogger::ScopedTiming split("SweepSystemWeaks", GetTimings()); |
| 1589 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1590 | Runtime::Current()->SweepSystemWeaks(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | void ConcurrentCopying::Sweep(bool swap_bitmaps) { |
| 1594 | { |
| 1595 | TimingLogger::ScopedTiming t("MarkStackAsLive", GetTimings()); |
| 1596 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 1597 | if (kEnableFromSpaceAccountingCheck) { |
| 1598 | CHECK_GE(live_stack_freeze_size_, live_stack->Size()); |
| 1599 | } |
| 1600 | heap_->MarkAllocStackAsLive(live_stack); |
| 1601 | live_stack->Reset(); |
| 1602 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1603 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1604 | TimingLogger::ScopedTiming split("Sweep", GetTimings()); |
| 1605 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
| 1606 | if (space->IsContinuousMemMapAllocSpace()) { |
| 1607 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 1608 | if (space == region_space_ || immune_spaces_.ContainsSpace(space)) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1609 | continue; |
| 1610 | } |
| 1611 | TimingLogger::ScopedTiming split2( |
| 1612 | alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings()); |
| 1613 | RecordFree(alloc_space->Sweep(swap_bitmaps)); |
| 1614 | } |
| 1615 | } |
| 1616 | SweepLargeObjects(swap_bitmaps); |
| 1617 | } |
| 1618 | |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 1619 | void ConcurrentCopying::MarkZygoteLargeObjects() { |
| 1620 | TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings()); |
| 1621 | Thread* const self = Thread::Current(); |
| 1622 | WriterMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
| 1623 | space::LargeObjectSpace* const los = heap_->GetLargeObjectsSpace(); |
Nicolas Geoffray | 7acddd8 | 2017-05-03 15:04:55 +0100 | [diff] [blame^] | 1624 | if (los != nullptr) { |
| 1625 | // Pick the current live bitmap (mark bitmap if swapped). |
| 1626 | accounting::LargeObjectBitmap* const live_bitmap = los->GetLiveBitmap(); |
| 1627 | accounting::LargeObjectBitmap* const mark_bitmap = los->GetMarkBitmap(); |
| 1628 | // Walk through all of the objects and explicitly mark the zygote ones so they don't get swept. |
| 1629 | std::pair<uint8_t*, uint8_t*> range = los->GetBeginEndAtomic(); |
| 1630 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(range.first), |
| 1631 | reinterpret_cast<uintptr_t>(range.second), |
| 1632 | [mark_bitmap, los, self](mirror::Object* obj) |
| 1633 | REQUIRES(Locks::heap_bitmap_lock_) |
| 1634 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1635 | if (los->IsZygoteLargeObject(self, obj)) { |
| 1636 | mark_bitmap->Set(obj); |
| 1637 | } |
| 1638 | }); |
| 1639 | } |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1642 | void ConcurrentCopying::SweepLargeObjects(bool swap_bitmaps) { |
| 1643 | TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings()); |
Nicolas Geoffray | 7acddd8 | 2017-05-03 15:04:55 +0100 | [diff] [blame^] | 1644 | if (heap_->GetLargeObjectsSpace() != nullptr) { |
| 1645 | RecordFreeLOS(heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps)); |
| 1646 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1649 | void ConcurrentCopying::ReclaimPhase() { |
| 1650 | TimingLogger::ScopedTiming split("ReclaimPhase", GetTimings()); |
| 1651 | if (kVerboseMode) { |
| 1652 | LOG(INFO) << "GC ReclaimPhase"; |
| 1653 | } |
| 1654 | Thread* self = Thread::Current(); |
| 1655 | |
| 1656 | { |
| 1657 | // Double-check that the mark stack is empty. |
| 1658 | // Note: need to set this after VerifyNoFromSpaceRef(). |
| 1659 | is_asserting_to_space_invariant_ = false; |
| 1660 | QuasiAtomic::ThreadFenceForConstructor(); |
| 1661 | if (kVerboseMode) { |
| 1662 | LOG(INFO) << "Issue an empty check point. "; |
| 1663 | } |
| 1664 | IssueEmptyCheckpoint(); |
| 1665 | // Disable the check. |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1666 | is_mark_stack_push_disallowed_.StoreSequentiallyConsistent(0); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1667 | if (kUseBakerReadBarrier) { |
| 1668 | updated_all_immune_objects_.StoreSequentiallyConsistent(false); |
| 1669 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1670 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | { |
| 1674 | // Record freed objects. |
| 1675 | TimingLogger::ScopedTiming split2("RecordFree", GetTimings()); |
| 1676 | // Don't include thread-locals that are in the to-space. |
Mathieu Chartier | 371b047 | 2017-02-27 16:37:21 -0800 | [diff] [blame] | 1677 | const uint64_t from_bytes = region_space_->GetBytesAllocatedInFromSpace(); |
| 1678 | const uint64_t from_objects = region_space_->GetObjectsAllocatedInFromSpace(); |
| 1679 | const uint64_t unevac_from_bytes = region_space_->GetBytesAllocatedInUnevacFromSpace(); |
| 1680 | const uint64_t unevac_from_objects = region_space_->GetObjectsAllocatedInUnevacFromSpace(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1681 | uint64_t to_bytes = bytes_moved_.LoadSequentiallyConsistent(); |
Mathieu Chartier | cca44a0 | 2016-08-17 10:07:29 -0700 | [diff] [blame] | 1682 | cumulative_bytes_moved_.FetchAndAddRelaxed(to_bytes); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1683 | uint64_t to_objects = objects_moved_.LoadSequentiallyConsistent(); |
Mathieu Chartier | cca44a0 | 2016-08-17 10:07:29 -0700 | [diff] [blame] | 1684 | cumulative_objects_moved_.FetchAndAddRelaxed(to_objects); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1685 | if (kEnableFromSpaceAccountingCheck) { |
| 1686 | CHECK_EQ(from_space_num_objects_at_first_pause_, from_objects + unevac_from_objects); |
| 1687 | CHECK_EQ(from_space_num_bytes_at_first_pause_, from_bytes + unevac_from_bytes); |
| 1688 | } |
| 1689 | CHECK_LE(to_objects, from_objects); |
| 1690 | CHECK_LE(to_bytes, from_bytes); |
Mathieu Chartier | 371b047 | 2017-02-27 16:37:21 -0800 | [diff] [blame] | 1691 | // cleared_bytes and cleared_objects may be greater than the from space equivalents since |
| 1692 | // ClearFromSpace may clear empty unevac regions. |
| 1693 | uint64_t cleared_bytes; |
| 1694 | uint64_t cleared_objects; |
| 1695 | { |
| 1696 | TimingLogger::ScopedTiming split4("ClearFromSpace", GetTimings()); |
| 1697 | region_space_->ClearFromSpace(&cleared_bytes, &cleared_objects); |
| 1698 | CHECK_GE(cleared_bytes, from_bytes); |
| 1699 | CHECK_GE(cleared_objects, from_objects); |
| 1700 | } |
| 1701 | int64_t freed_bytes = cleared_bytes - to_bytes; |
| 1702 | int64_t freed_objects = cleared_objects - to_objects; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1703 | if (kVerboseMode) { |
| 1704 | LOG(INFO) << "RecordFree:" |
| 1705 | << " from_bytes=" << from_bytes << " from_objects=" << from_objects |
| 1706 | << " unevac_from_bytes=" << unevac_from_bytes << " unevac_from_objects=" << unevac_from_objects |
| 1707 | << " to_bytes=" << to_bytes << " to_objects=" << to_objects |
| 1708 | << " freed_bytes=" << freed_bytes << " freed_objects=" << freed_objects |
| 1709 | << " from_space size=" << region_space_->FromSpaceSize() |
| 1710 | << " unevac_from_space size=" << region_space_->UnevacFromSpaceSize() |
| 1711 | << " to_space size=" << region_space_->ToSpaceSize(); |
| 1712 | LOG(INFO) << "(before) num_bytes_allocated=" << heap_->num_bytes_allocated_.LoadSequentiallyConsistent(); |
| 1713 | } |
| 1714 | RecordFree(ObjectBytePair(freed_objects, freed_bytes)); |
| 1715 | if (kVerboseMode) { |
| 1716 | LOG(INFO) << "(after) num_bytes_allocated=" << heap_->num_bytes_allocated_.LoadSequentiallyConsistent(); |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1721 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1722 | Sweep(false); |
| 1723 | SwapBitmaps(); |
| 1724 | heap_->UnBindBitmaps(); |
| 1725 | |
Mathieu Chartier | 7ec38dc | 2016-10-07 15:24:46 -0700 | [diff] [blame] | 1726 | // The bitmap was cleared at the start of the GC, there is nothing we need to do here. |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1727 | DCHECK(region_space_bitmap_ != nullptr); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1728 | region_space_bitmap_ = nullptr; |
| 1729 | } |
| 1730 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1731 | CheckEmptyMarkStack(); |
| 1732 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1733 | if (kVerboseMode) { |
| 1734 | LOG(INFO) << "GC end of ReclaimPhase"; |
| 1735 | } |
| 1736 | } |
| 1737 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1738 | // Assert the to-space invariant. |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1739 | void ConcurrentCopying::AssertToSpaceInvariant(mirror::Object* obj, |
| 1740 | MemberOffset offset, |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1741 | mirror::Object* ref) { |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1742 | CHECK_EQ(heap_->collector_type_, kCollectorTypeCC); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1743 | if (is_asserting_to_space_invariant_) { |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1744 | using RegionType = space::RegionSpace::RegionType; |
| 1745 | space::RegionSpace::RegionType type = region_space_->GetRegionType(ref); |
| 1746 | if (type == RegionType::kRegionTypeToSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1747 | // OK. |
| 1748 | return; |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1749 | } else if (type == RegionType::kRegionTypeUnevacFromSpace) { |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1750 | CHECK(IsMarkedInUnevacFromSpace(ref)) << ref; |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1751 | } else if (UNLIKELY(type == RegionType::kRegionTypeFromSpace)) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1752 | // Not OK. Do extra logging. |
| 1753 | if (obj != nullptr) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1754 | LogFromSpaceRefHolder(obj, offset); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1755 | } |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1756 | ref->GetLockWord(false).Dump(LOG_STREAM(FATAL_WITHOUT_ABORT)); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1757 | CHECK(false) << "Found from-space ref " << ref << " " << ref->PrettyTypeOf(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1758 | } else { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1759 | AssertToSpaceInvariantInNonMovingSpace(obj, ref); |
| 1760 | } |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | class RootPrinter { |
| 1765 | public: |
| 1766 | RootPrinter() { } |
| 1767 | |
| 1768 | template <class MirrorType> |
| 1769 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<MirrorType>* root) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1770 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1771 | if (!root->IsNull()) { |
| 1772 | VisitRoot(root); |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | template <class MirrorType> |
| 1777 | void VisitRoot(mirror::Object** root) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1778 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1779 | LOG(FATAL_WITHOUT_ABORT) << "root=" << root << " ref=" << *root; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | template <class MirrorType> |
| 1783 | void VisitRoot(mirror::CompressedReference<MirrorType>* root) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1784 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1785 | LOG(FATAL_WITHOUT_ABORT) << "root=" << root << " ref=" << root->AsMirrorPtr(); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1786 | } |
| 1787 | }; |
| 1788 | |
| 1789 | void ConcurrentCopying::AssertToSpaceInvariant(GcRootSource* gc_root_source, |
| 1790 | mirror::Object* ref) { |
| 1791 | CHECK(heap_->collector_type_ == kCollectorTypeCC) << static_cast<size_t>(heap_->collector_type_); |
| 1792 | if (is_asserting_to_space_invariant_) { |
| 1793 | if (region_space_->IsInToSpace(ref)) { |
| 1794 | // OK. |
| 1795 | return; |
| 1796 | } else if (region_space_->IsInUnevacFromSpace(ref)) { |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1797 | CHECK(IsMarkedInUnevacFromSpace(ref)) << ref; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1798 | } else if (region_space_->IsInFromSpace(ref)) { |
| 1799 | // Not OK. Do extra logging. |
| 1800 | if (gc_root_source == nullptr) { |
| 1801 | // No info. |
| 1802 | } else if (gc_root_source->HasArtField()) { |
| 1803 | ArtField* field = gc_root_source->GetArtField(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1804 | LOG(FATAL_WITHOUT_ABORT) << "gc root in field " << field << " " |
| 1805 | << ArtField::PrettyField(field); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1806 | RootPrinter root_printer; |
| 1807 | field->VisitRoots(root_printer); |
| 1808 | } else if (gc_root_source->HasArtMethod()) { |
| 1809 | ArtMethod* method = gc_root_source->GetArtMethod(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1810 | LOG(FATAL_WITHOUT_ABORT) << "gc root in method " << method << " " |
| 1811 | << ArtMethod::PrettyMethod(method); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1812 | RootPrinter root_printer; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1813 | method->VisitRoots(root_printer, kRuntimePointerSize); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1814 | } |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1815 | ref->GetLockWord(false).Dump(LOG_STREAM(FATAL_WITHOUT_ABORT)); |
| 1816 | region_space_->DumpNonFreeRegions(LOG_STREAM(FATAL_WITHOUT_ABORT)); |
| 1817 | PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT); |
| 1818 | MemMap::DumpMaps(LOG_STREAM(FATAL_WITHOUT_ABORT), true); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1819 | CHECK(false) << "Found from-space ref " << ref << " " << ref->PrettyTypeOf(); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1820 | } else { |
| 1821 | AssertToSpaceInvariantInNonMovingSpace(nullptr, ref); |
| 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | void ConcurrentCopying::LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset) { |
| 1827 | if (kUseBakerReadBarrier) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1828 | LOG(INFO) << "holder=" << obj << " " << obj->PrettyTypeOf() |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1829 | << " holder rb_state=" << obj->GetReadBarrierState(); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1830 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1831 | LOG(INFO) << "holder=" << obj << " " << obj->PrettyTypeOf(); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1832 | } |
| 1833 | if (region_space_->IsInFromSpace(obj)) { |
| 1834 | LOG(INFO) << "holder is in the from-space."; |
| 1835 | } else if (region_space_->IsInToSpace(obj)) { |
| 1836 | LOG(INFO) << "holder is in the to-space."; |
| 1837 | } else if (region_space_->IsInUnevacFromSpace(obj)) { |
| 1838 | LOG(INFO) << "holder is in the unevac from-space."; |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1839 | if (IsMarkedInUnevacFromSpace(obj)) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1840 | LOG(INFO) << "holder is marked in the region space bitmap."; |
| 1841 | } else { |
| 1842 | LOG(INFO) << "holder is not marked in the region space bitmap."; |
| 1843 | } |
| 1844 | } else { |
| 1845 | // In a non-moving space. |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 1846 | if (immune_spaces_.ContainsObject(obj)) { |
| 1847 | LOG(INFO) << "holder is in an immune image or the zygote space."; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1848 | } else { |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 1849 | LOG(INFO) << "holder is in a non-immune, non-moving (or main) space."; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1850 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1851 | heap_mark_bitmap_->GetContinuousSpaceBitmap(obj); |
| 1852 | accounting::LargeObjectBitmap* los_bitmap = |
| 1853 | heap_mark_bitmap_->GetLargeObjectBitmap(obj); |
| 1854 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 1855 | bool is_los = mark_bitmap == nullptr; |
| 1856 | if (!is_los && mark_bitmap->Test(obj)) { |
| 1857 | LOG(INFO) << "holder is marked in the mark bit map."; |
| 1858 | } else if (is_los && los_bitmap->Test(obj)) { |
| 1859 | LOG(INFO) << "holder is marked in the los bit map."; |
| 1860 | } else { |
| 1861 | // If ref is on the allocation stack, then it is considered |
| 1862 | // mark/alive (but not necessarily on the live stack.) |
| 1863 | if (IsOnAllocStack(obj)) { |
| 1864 | LOG(INFO) << "holder is on the alloc stack."; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1865 | } else { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1866 | LOG(INFO) << "holder is not marked or on the alloc stack."; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | } |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1871 | LOG(INFO) << "offset=" << offset.SizeValue(); |
| 1872 | } |
| 1873 | |
| 1874 | void ConcurrentCopying::AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj, |
| 1875 | mirror::Object* ref) { |
| 1876 | // In a non-moving spaces. Check that the ref is marked. |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 1877 | if (immune_spaces_.ContainsObject(ref)) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1878 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1879 | // Immune object may not be gray if called from the GC. |
| 1880 | if (Thread::Current() == thread_running_gc_ && !gc_grays_immune_objects_) { |
| 1881 | return; |
| 1882 | } |
| 1883 | bool updated_all_immune_objects = updated_all_immune_objects_.LoadSequentiallyConsistent(); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1884 | CHECK(updated_all_immune_objects || ref->GetReadBarrierState() == ReadBarrier::GrayState()) |
| 1885 | << "Unmarked immune space ref. obj=" << obj << " rb_state=" |
| 1886 | << (obj != nullptr ? obj->GetReadBarrierState() : 0U) |
| 1887 | << " ref=" << ref << " ref rb_state=" << ref->GetReadBarrierState() |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1888 | << " updated_all_immune_objects=" << updated_all_immune_objects; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1889 | } |
| 1890 | } else { |
| 1891 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1892 | heap_mark_bitmap_->GetContinuousSpaceBitmap(ref); |
| 1893 | accounting::LargeObjectBitmap* los_bitmap = |
| 1894 | heap_mark_bitmap_->GetLargeObjectBitmap(ref); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1895 | bool is_los = mark_bitmap == nullptr; |
| 1896 | if ((!is_los && mark_bitmap->Test(ref)) || |
| 1897 | (is_los && los_bitmap->Test(ref))) { |
| 1898 | // OK. |
| 1899 | } else { |
| 1900 | // If ref is on the allocation stack, then it may not be |
| 1901 | // marked live, but considered marked/alive (but not |
| 1902 | // necessarily on the live stack). |
| 1903 | CHECK(IsOnAllocStack(ref)) << "Unmarked ref that's not on the allocation stack. " |
| 1904 | << "obj=" << obj << " ref=" << ref; |
| 1905 | } |
| 1906 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1909 | // Used to scan ref fields of an object. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1910 | class ConcurrentCopying::RefFieldsVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1911 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1912 | explicit RefFieldsVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1913 | : collector_(collector) {} |
| 1914 | |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1915 | void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1916 | const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) |
| 1917 | REQUIRES_SHARED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 1918 | collector_->Process(obj, offset); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1919 | } |
| 1920 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1921 | void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1922 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1923 | CHECK(klass->IsTypeOfReferenceClass()); |
| 1924 | collector_->DelayReferenceReferent(klass, ref); |
| 1925 | } |
| 1926 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1927 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1928 | ALWAYS_INLINE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1929 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1930 | if (!root->IsNull()) { |
| 1931 | VisitRoot(root); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1936 | ALWAYS_INLINE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1937 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1938 | collector_->MarkRoot</*kGrayImmuneObject*/false>(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1939 | } |
| 1940 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1941 | private: |
| 1942 | ConcurrentCopying* const collector_; |
| 1943 | }; |
| 1944 | |
| 1945 | // Scan ref fields of an object. |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1946 | inline void ConcurrentCopying::Scan(mirror::Object* to_ref) { |
Hiroshi Yamauchi | 9b60d50 | 2017-02-03 15:09:26 -0800 | [diff] [blame] | 1947 | if (kDisallowReadBarrierDuringScan && !Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1948 | // Avoid all read barriers during visit references to help performance. |
Hiroshi Yamauchi | 9b60d50 | 2017-02-03 15:09:26 -0800 | [diff] [blame] | 1949 | // Don't do this in transaction mode because we may read the old value of an field which may |
| 1950 | // trigger read barriers. |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1951 | Thread::Current()->ModifyDebugDisallowReadBarrier(1); |
| 1952 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1953 | DCHECK(!region_space_->IsInFromSpace(to_ref)); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1954 | DCHECK_EQ(Thread::Current(), thread_running_gc_); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1955 | RefFieldsVisitor visitor(this); |
Hiroshi Yamauchi | 5496f69 | 2016-02-17 13:29:59 -0800 | [diff] [blame] | 1956 | // Disable the read barrier for a performance reason. |
| 1957 | to_ref->VisitReferences</*kVisitNativeRoots*/true, kDefaultVerifyFlags, kWithoutReadBarrier>( |
| 1958 | visitor, visitor); |
Hiroshi Yamauchi | 9b60d50 | 2017-02-03 15:09:26 -0800 | [diff] [blame] | 1959 | if (kDisallowReadBarrierDuringScan && !Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1960 | Thread::Current()->ModifyDebugDisallowReadBarrier(-1); |
| 1961 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | // Process a field. |
| 1965 | inline void ConcurrentCopying::Process(mirror::Object* obj, MemberOffset offset) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1966 | DCHECK_EQ(Thread::Current(), thread_running_gc_); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1967 | mirror::Object* ref = obj->GetFieldObject< |
| 1968 | mirror::Object, kVerifyNone, kWithoutReadBarrier, false>(offset); |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1969 | mirror::Object* to_ref = Mark</*kGrayImmuneObject*/false, /*kFromGCThread*/true>(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1970 | if (to_ref == ref) { |
| 1971 | return; |
| 1972 | } |
| 1973 | // This may fail if the mutator writes to the field at the same time. But it's ok. |
| 1974 | mirror::Object* expected_ref = ref; |
| 1975 | mirror::Object* new_ref = to_ref; |
| 1976 | do { |
| 1977 | if (expected_ref != |
| 1978 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier, false>(offset)) { |
| 1979 | // It was updated by the mutator. |
| 1980 | break; |
| 1981 | } |
Hiroshi Yamauchi | fed3e2f | 2015-10-20 11:11:56 -0700 | [diff] [blame] | 1982 | } while (!obj->CasFieldWeakRelaxedObjectWithoutWriteBarrier< |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1983 | false, false, kVerifyNone>(offset, expected_ref, new_ref)); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1984 | } |
| 1985 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1986 | // Process some roots. |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1987 | inline void ConcurrentCopying::VisitRoots( |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1988 | mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) { |
| 1989 | for (size_t i = 0; i < count; ++i) { |
| 1990 | mirror::Object** root = roots[i]; |
| 1991 | mirror::Object* ref = *root; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1992 | mirror::Object* to_ref = Mark(ref); |
| 1993 | if (to_ref == ref) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 1994 | continue; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1995 | } |
| 1996 | Atomic<mirror::Object*>* addr = reinterpret_cast<Atomic<mirror::Object*>*>(root); |
| 1997 | mirror::Object* expected_ref = ref; |
| 1998 | mirror::Object* new_ref = to_ref; |
| 1999 | do { |
| 2000 | if (expected_ref != addr->LoadRelaxed()) { |
| 2001 | // It was updated by the mutator. |
| 2002 | break; |
| 2003 | } |
Hiroshi Yamauchi | fed3e2f | 2015-10-20 11:11:56 -0700 | [diff] [blame] | 2004 | } while (!addr->CompareExchangeWeakRelaxed(expected_ref, new_ref)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2005 | } |
| 2006 | } |
| 2007 | |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2008 | template<bool kGrayImmuneObject> |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 2009 | inline void ConcurrentCopying::MarkRoot(mirror::CompressedReference<mirror::Object>* root) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2010 | DCHECK(!root->IsNull()); |
| 2011 | mirror::Object* const ref = root->AsMirrorPtr(); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2012 | mirror::Object* to_ref = Mark<kGrayImmuneObject>(ref); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2013 | if (to_ref != ref) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2014 | auto* addr = reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root); |
| 2015 | auto expected_ref = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(ref); |
| 2016 | auto new_ref = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(to_ref); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2017 | // If the cas fails, then it was updated by the mutator. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2018 | do { |
| 2019 | if (ref != addr->LoadRelaxed().AsMirrorPtr()) { |
| 2020 | // It was updated by the mutator. |
| 2021 | break; |
| 2022 | } |
Hiroshi Yamauchi | fed3e2f | 2015-10-20 11:11:56 -0700 | [diff] [blame] | 2023 | } while (!addr->CompareExchangeWeakRelaxed(expected_ref, new_ref)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2024 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2025 | } |
| 2026 | |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 2027 | inline void ConcurrentCopying::VisitRoots( |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2028 | mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 2029 | const RootInfo& info ATTRIBUTE_UNUSED) { |
| 2030 | for (size_t i = 0; i < count; ++i) { |
| 2031 | mirror::CompressedReference<mirror::Object>* const root = roots[i]; |
| 2032 | if (!root->IsNull()) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2033 | // kGrayImmuneObject is true because this is used for the thread flip. |
| 2034 | MarkRoot</*kGrayImmuneObject*/true>(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | } |
| 2038 | |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2039 | // Temporary set gc_grays_immune_objects_ to true in a scope if the current thread is GC. |
| 2040 | class ConcurrentCopying::ScopedGcGraysImmuneObjects { |
| 2041 | public: |
| 2042 | explicit ScopedGcGraysImmuneObjects(ConcurrentCopying* collector) |
| 2043 | : collector_(collector), enabled_(false) { |
| 2044 | if (kUseBakerReadBarrier && |
| 2045 | collector_->thread_running_gc_ == Thread::Current() && |
| 2046 | !collector_->gc_grays_immune_objects_) { |
| 2047 | collector_->gc_grays_immune_objects_ = true; |
| 2048 | enabled_ = true; |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | ~ScopedGcGraysImmuneObjects() { |
| 2053 | if (kUseBakerReadBarrier && |
| 2054 | collector_->thread_running_gc_ == Thread::Current() && |
| 2055 | enabled_) { |
| 2056 | DCHECK(collector_->gc_grays_immune_objects_); |
| 2057 | collector_->gc_grays_immune_objects_ = false; |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | private: |
| 2062 | ConcurrentCopying* const collector_; |
| 2063 | bool enabled_; |
| 2064 | }; |
| 2065 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2066 | // Fill the given memory block with a dummy object. Used to fill in a |
| 2067 | // copy of objects that was lost in race. |
| 2068 | void ConcurrentCopying::FillWithDummyObject(mirror::Object* dummy_obj, size_t byte_size) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2069 | // GC doesn't gray immune objects while scanning immune objects. But we need to trigger the read |
| 2070 | // barriers here because we need the updated reference to the int array class, etc. Temporary set |
| 2071 | // gc_grays_immune_objects_ to true so that we won't cause a DCHECK failure in MarkImmuneSpace(). |
| 2072 | ScopedGcGraysImmuneObjects scoped_gc_gray_immune_objects(this); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2073 | CHECK_ALIGNED(byte_size, kObjectAlignment); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2074 | memset(dummy_obj, 0, byte_size); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2075 | // Avoid going through read barrier for since kDisallowReadBarrierDuringScan may be enabled. |
| 2076 | // Explicitly mark to make sure to get an object in the to-space. |
| 2077 | mirror::Class* int_array_class = down_cast<mirror::Class*>( |
| 2078 | Mark(mirror::IntArray::GetArrayClass<kWithoutReadBarrier>())); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2079 | CHECK(int_array_class != nullptr); |
| 2080 | AssertToSpaceInvariant(nullptr, MemberOffset(0), int_array_class); |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 2081 | size_t component_size = int_array_class->GetComponentSize<kWithoutReadBarrier>(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2082 | CHECK_EQ(component_size, sizeof(int32_t)); |
| 2083 | size_t data_offset = mirror::Array::DataOffset(component_size).SizeValue(); |
| 2084 | if (data_offset > byte_size) { |
| 2085 | // An int array is too big. Use java.lang.Object. |
Mathieu Chartier | 9aef992 | 2017-04-23 13:53:50 -0700 | [diff] [blame] | 2086 | CHECK(java_lang_Object_ != nullptr); |
Mathieu Chartier | 3ed8ec1 | 2017-04-20 19:28:54 -0700 | [diff] [blame] | 2087 | AssertToSpaceInvariant(nullptr, MemberOffset(0), java_lang_Object_); |
| 2088 | CHECK_EQ(byte_size, (java_lang_Object_->GetObjectSize<kVerifyNone, kWithoutReadBarrier>())); |
| 2089 | dummy_obj->SetClass(java_lang_Object_); |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 2090 | CHECK_EQ(byte_size, (dummy_obj->SizeOf<kVerifyNone>())); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2091 | } else { |
| 2092 | // Use an int array. |
| 2093 | dummy_obj->SetClass(int_array_class); |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 2094 | CHECK((dummy_obj->IsArrayInstance<kVerifyNone, kWithoutReadBarrier>())); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2095 | int32_t length = (byte_size - data_offset) / component_size; |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 2096 | mirror::Array* dummy_arr = dummy_obj->AsArray<kVerifyNone, kWithoutReadBarrier>(); |
| 2097 | dummy_arr->SetLength(length); |
| 2098 | CHECK_EQ(dummy_arr->GetLength(), length) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2099 | << "byte_size=" << byte_size << " length=" << length |
| 2100 | << " component_size=" << component_size << " data_offset=" << data_offset; |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 2101 | CHECK_EQ(byte_size, (dummy_obj->SizeOf<kVerifyNone>())) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2102 | << "byte_size=" << byte_size << " length=" << length |
| 2103 | << " component_size=" << component_size << " data_offset=" << data_offset; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | // Reuse the memory blocks that were copy of objects that were lost in race. |
| 2108 | mirror::Object* ConcurrentCopying::AllocateInSkippedBlock(size_t alloc_size) { |
| 2109 | // Try to reuse the blocks that were unused due to CAS failures. |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2110 | CHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2111 | Thread* self = Thread::Current(); |
| 2112 | size_t min_object_size = RoundUp(sizeof(mirror::Object), space::RegionSpace::kAlignment); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2113 | size_t byte_size; |
| 2114 | uint8_t* addr; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2115 | { |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2116 | MutexLock mu(self, skipped_blocks_lock_); |
| 2117 | auto it = skipped_blocks_map_.lower_bound(alloc_size); |
| 2118 | if (it == skipped_blocks_map_.end()) { |
| 2119 | // Not found. |
| 2120 | return nullptr; |
| 2121 | } |
| 2122 | byte_size = it->first; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2123 | CHECK_GE(byte_size, alloc_size); |
| 2124 | if (byte_size > alloc_size && byte_size - alloc_size < min_object_size) { |
| 2125 | // If remainder would be too small for a dummy object, retry with a larger request size. |
| 2126 | it = skipped_blocks_map_.lower_bound(alloc_size + min_object_size); |
| 2127 | if (it == skipped_blocks_map_.end()) { |
| 2128 | // Not found. |
| 2129 | return nullptr; |
| 2130 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2131 | CHECK_ALIGNED(it->first - alloc_size, space::RegionSpace::kAlignment); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2132 | CHECK_GE(it->first - alloc_size, min_object_size) |
| 2133 | << "byte_size=" << byte_size << " it->first=" << it->first << " alloc_size=" << alloc_size; |
| 2134 | } |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2135 | // Found a block. |
| 2136 | CHECK(it != skipped_blocks_map_.end()); |
| 2137 | byte_size = it->first; |
| 2138 | addr = it->second; |
| 2139 | CHECK_GE(byte_size, alloc_size); |
| 2140 | CHECK(region_space_->IsInToSpace(reinterpret_cast<mirror::Object*>(addr))); |
| 2141 | CHECK_ALIGNED(byte_size, space::RegionSpace::kAlignment); |
| 2142 | if (kVerboseMode) { |
| 2143 | LOG(INFO) << "Reusing skipped bytes : " << reinterpret_cast<void*>(addr) << ", " << byte_size; |
| 2144 | } |
| 2145 | skipped_blocks_map_.erase(it); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2146 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2147 | memset(addr, 0, byte_size); |
| 2148 | if (byte_size > alloc_size) { |
| 2149 | // Return the remainder to the map. |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2150 | CHECK_ALIGNED(byte_size - alloc_size, space::RegionSpace::kAlignment); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2151 | CHECK_GE(byte_size - alloc_size, min_object_size); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2152 | // FillWithDummyObject may mark an object, avoid holding skipped_blocks_lock_ to prevent lock |
| 2153 | // violation and possible deadlock. The deadlock case is a recursive case: |
| 2154 | // FillWithDummyObject -> IntArray::GetArrayClass -> Mark -> Copy -> AllocateInSkippedBlock. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2155 | FillWithDummyObject(reinterpret_cast<mirror::Object*>(addr + alloc_size), |
| 2156 | byte_size - alloc_size); |
| 2157 | CHECK(region_space_->IsInToSpace(reinterpret_cast<mirror::Object*>(addr + alloc_size))); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2158 | { |
| 2159 | MutexLock mu(self, skipped_blocks_lock_); |
| 2160 | skipped_blocks_map_.insert(std::make_pair(byte_size - alloc_size, addr + alloc_size)); |
| 2161 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2162 | } |
| 2163 | return reinterpret_cast<mirror::Object*>(addr); |
| 2164 | } |
| 2165 | |
Mathieu Chartier | ef496d9 | 2017-04-28 18:58:59 -0700 | [diff] [blame] | 2166 | mirror::Object* ConcurrentCopying::Copy(mirror::Object* from_ref, |
| 2167 | mirror::Object* holder, |
| 2168 | MemberOffset offset) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2169 | DCHECK(region_space_->IsInFromSpace(from_ref)); |
Mathieu Chartier | ef496d9 | 2017-04-28 18:58:59 -0700 | [diff] [blame] | 2170 | // If the class pointer is null, the object is invalid. This could occur for a dangling pointer |
| 2171 | // from a previous GC that is either inside or outside the allocated region. |
| 2172 | mirror::Class* klass = from_ref->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
| 2173 | if (UNLIKELY(klass == nullptr)) { |
| 2174 | heap_->GetVerification()->LogHeapCorruption(holder, offset, from_ref, /* fatal */ true); |
| 2175 | } |
Mathieu Chartier | d08f66f | 2017-04-13 11:47:53 -0700 | [diff] [blame] | 2176 | // There must not be a read barrier to avoid nested RB that might violate the to-space invariant. |
| 2177 | // Note that from_ref is a from space ref so the SizeOf() call will access the from-space meta |
| 2178 | // objects, but it's ok and necessary. |
| 2179 | size_t obj_size = from_ref->SizeOf<kDefaultVerifyFlags>(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2180 | size_t region_space_alloc_size = RoundUp(obj_size, space::RegionSpace::kAlignment); |
| 2181 | size_t region_space_bytes_allocated = 0U; |
| 2182 | size_t non_moving_space_bytes_allocated = 0U; |
| 2183 | size_t bytes_allocated = 0U; |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 2184 | size_t dummy; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2185 | mirror::Object* to_ref = region_space_->AllocNonvirtual<true>( |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 2186 | region_space_alloc_size, ®ion_space_bytes_allocated, nullptr, &dummy); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2187 | bytes_allocated = region_space_bytes_allocated; |
| 2188 | if (to_ref != nullptr) { |
| 2189 | DCHECK_EQ(region_space_alloc_size, region_space_bytes_allocated); |
| 2190 | } |
| 2191 | bool fall_back_to_non_moving = false; |
| 2192 | if (UNLIKELY(to_ref == nullptr)) { |
| 2193 | // Failed to allocate in the region space. Try the skipped blocks. |
| 2194 | to_ref = AllocateInSkippedBlock(region_space_alloc_size); |
| 2195 | if (to_ref != nullptr) { |
| 2196 | // Succeeded to allocate in a skipped block. |
| 2197 | if (heap_->use_tlab_) { |
| 2198 | // This is necessary for the tlab case as it's not accounted in the space. |
| 2199 | region_space_->RecordAlloc(to_ref); |
| 2200 | } |
| 2201 | bytes_allocated = region_space_alloc_size; |
| 2202 | } else { |
| 2203 | // Fall back to the non-moving space. |
| 2204 | fall_back_to_non_moving = true; |
| 2205 | if (kVerboseMode) { |
| 2206 | LOG(INFO) << "Out of memory in the to-space. Fall back to non-moving. skipped_bytes=" |
| 2207 | << to_space_bytes_skipped_.LoadSequentiallyConsistent() |
| 2208 | << " skipped_objects=" << to_space_objects_skipped_.LoadSequentiallyConsistent(); |
| 2209 | } |
| 2210 | fall_back_to_non_moving = true; |
| 2211 | to_ref = heap_->non_moving_space_->Alloc(Thread::Current(), obj_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 2212 | &non_moving_space_bytes_allocated, nullptr, &dummy); |
Mathieu Chartier | b01335c | 2017-03-22 13:15:01 -0700 | [diff] [blame] | 2213 | if (UNLIKELY(to_ref == nullptr)) { |
| 2214 | LOG(FATAL_WITHOUT_ABORT) << "Fall-back non-moving space allocation failed for a " |
| 2215 | << obj_size << " byte object in region type " |
| 2216 | << region_space_->GetRegionType(from_ref); |
| 2217 | LOG(FATAL) << "Object address=" << from_ref << " type=" << from_ref->PrettyTypeOf(); |
| 2218 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2219 | bytes_allocated = non_moving_space_bytes_allocated; |
| 2220 | // Mark it in the mark bitmap. |
| 2221 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 2222 | heap_mark_bitmap_->GetContinuousSpaceBitmap(to_ref); |
| 2223 | CHECK(mark_bitmap != nullptr); |
| 2224 | CHECK(!mark_bitmap->AtomicTestAndSet(to_ref)); |
| 2225 | } |
| 2226 | } |
| 2227 | DCHECK(to_ref != nullptr); |
| 2228 | |
Mathieu Chartier | d818adb | 2016-09-15 13:12:47 -0700 | [diff] [blame] | 2229 | // Copy the object excluding the lock word since that is handled in the loop. |
Mathieu Chartier | ef496d9 | 2017-04-28 18:58:59 -0700 | [diff] [blame] | 2230 | to_ref->SetClass(klass); |
Mathieu Chartier | d818adb | 2016-09-15 13:12:47 -0700 | [diff] [blame] | 2231 | const size_t kObjectHeaderSize = sizeof(mirror::Object); |
| 2232 | DCHECK_GE(obj_size, kObjectHeaderSize); |
| 2233 | static_assert(kObjectHeaderSize == sizeof(mirror::HeapReference<mirror::Class>) + |
| 2234 | sizeof(LockWord), |
| 2235 | "Object header size does not match"); |
| 2236 | // Memcpy can tear for words since it may do byte copy. It is only safe to do this since the |
| 2237 | // object in the from space is immutable other than the lock word. b/31423258 |
| 2238 | memcpy(reinterpret_cast<uint8_t*>(to_ref) + kObjectHeaderSize, |
| 2239 | reinterpret_cast<const uint8_t*>(from_ref) + kObjectHeaderSize, |
| 2240 | obj_size - kObjectHeaderSize); |
| 2241 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2242 | // Attempt to install the forward pointer. This is in a loop as the |
| 2243 | // lock word atomic write can fail. |
| 2244 | while (true) { |
Mathieu Chartier | d818adb | 2016-09-15 13:12:47 -0700 | [diff] [blame] | 2245 | LockWord old_lock_word = from_ref->GetLockWord(false); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2246 | |
| 2247 | if (old_lock_word.GetState() == LockWord::kForwardingAddress) { |
| 2248 | // Lost the race. Another thread (either GC or mutator) stored |
| 2249 | // the forwarding pointer first. Make the lost copy (to_ref) |
| 2250 | // look like a valid but dead (dummy) object and keep it for |
| 2251 | // future reuse. |
| 2252 | FillWithDummyObject(to_ref, bytes_allocated); |
| 2253 | if (!fall_back_to_non_moving) { |
| 2254 | DCHECK(region_space_->IsInToSpace(to_ref)); |
| 2255 | if (bytes_allocated > space::RegionSpace::kRegionSize) { |
| 2256 | // Free the large alloc. |
| 2257 | region_space_->FreeLarge(to_ref, bytes_allocated); |
| 2258 | } else { |
| 2259 | // Record the lost copy for later reuse. |
| 2260 | heap_->num_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes_allocated); |
| 2261 | to_space_bytes_skipped_.FetchAndAddSequentiallyConsistent(bytes_allocated); |
| 2262 | to_space_objects_skipped_.FetchAndAddSequentiallyConsistent(1); |
| 2263 | MutexLock mu(Thread::Current(), skipped_blocks_lock_); |
| 2264 | skipped_blocks_map_.insert(std::make_pair(bytes_allocated, |
| 2265 | reinterpret_cast<uint8_t*>(to_ref))); |
| 2266 | } |
| 2267 | } else { |
| 2268 | DCHECK(heap_->non_moving_space_->HasAddress(to_ref)); |
| 2269 | DCHECK_EQ(bytes_allocated, non_moving_space_bytes_allocated); |
| 2270 | // Free the non-moving-space chunk. |
| 2271 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 2272 | heap_mark_bitmap_->GetContinuousSpaceBitmap(to_ref); |
| 2273 | CHECK(mark_bitmap != nullptr); |
| 2274 | CHECK(mark_bitmap->Clear(to_ref)); |
| 2275 | heap_->non_moving_space_->Free(Thread::Current(), to_ref); |
| 2276 | } |
| 2277 | |
| 2278 | // Get the winner's forward ptr. |
| 2279 | mirror::Object* lost_fwd_ptr = to_ref; |
| 2280 | to_ref = reinterpret_cast<mirror::Object*>(old_lock_word.ForwardingAddress()); |
| 2281 | CHECK(to_ref != nullptr); |
| 2282 | CHECK_NE(to_ref, lost_fwd_ptr); |
Mathieu Chartier | dfcd6f4 | 2016-09-13 10:02:48 -0700 | [diff] [blame] | 2283 | CHECK(region_space_->IsInToSpace(to_ref) || heap_->non_moving_space_->HasAddress(to_ref)) |
| 2284 | << "to_ref=" << to_ref << " " << heap_->DumpSpaces(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2285 | CHECK_NE(to_ref->GetLockWord(false).GetState(), LockWord::kForwardingAddress); |
| 2286 | return to_ref; |
| 2287 | } |
| 2288 | |
Mathieu Chartier | d818adb | 2016-09-15 13:12:47 -0700 | [diff] [blame] | 2289 | // Copy the old lock word over since we did not copy it yet. |
| 2290 | to_ref->SetLockWord(old_lock_word, false); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 2291 | // Set the gray ptr. |
| 2292 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2293 | to_ref->SetReadBarrierState(ReadBarrier::GrayState()); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
Mathieu Chartier | a813126 | 2016-11-29 17:55:19 -0800 | [diff] [blame] | 2296 | // Do a fence to prevent the field CAS in ConcurrentCopying::Process from possibly reordering |
| 2297 | // before the object copy. |
| 2298 | QuasiAtomic::ThreadFenceRelease(); |
| 2299 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2300 | LockWord new_lock_word = LockWord::FromForwardingAddress(reinterpret_cast<size_t>(to_ref)); |
| 2301 | |
| 2302 | // Try to atomically write the fwd ptr. |
Mathieu Chartier | a813126 | 2016-11-29 17:55:19 -0800 | [diff] [blame] | 2303 | bool success = from_ref->CasLockWordWeakRelaxed(old_lock_word, new_lock_word); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2304 | if (LIKELY(success)) { |
| 2305 | // The CAS succeeded. |
Mathieu Chartier | a813126 | 2016-11-29 17:55:19 -0800 | [diff] [blame] | 2306 | objects_moved_.FetchAndAddRelaxed(1); |
| 2307 | bytes_moved_.FetchAndAddRelaxed(region_space_alloc_size); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2308 | if (LIKELY(!fall_back_to_non_moving)) { |
| 2309 | DCHECK(region_space_->IsInToSpace(to_ref)); |
| 2310 | } else { |
| 2311 | DCHECK(heap_->non_moving_space_->HasAddress(to_ref)); |
| 2312 | DCHECK_EQ(bytes_allocated, non_moving_space_bytes_allocated); |
| 2313 | } |
| 2314 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2315 | DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2316 | } |
| 2317 | DCHECK(GetFwdPtr(from_ref) == to_ref); |
| 2318 | CHECK_NE(to_ref->GetLockWord(false).GetState(), LockWord::kForwardingAddress); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2319 | PushOntoMarkStack(to_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2320 | return to_ref; |
| 2321 | } else { |
| 2322 | // The CAS failed. It may have lost the race or may have failed |
| 2323 | // due to monitor/hashcode ops. Either way, retry. |
| 2324 | } |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | mirror::Object* ConcurrentCopying::IsMarked(mirror::Object* from_ref) { |
| 2329 | DCHECK(from_ref != nullptr); |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 2330 | space::RegionSpace::RegionType rtype = region_space_->GetRegionType(from_ref); |
| 2331 | if (rtype == space::RegionSpace::RegionType::kRegionTypeToSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2332 | // It's already marked. |
| 2333 | return from_ref; |
| 2334 | } |
| 2335 | mirror::Object* to_ref; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 2336 | if (rtype == space::RegionSpace::RegionType::kRegionTypeFromSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2337 | to_ref = GetFwdPtr(from_ref); |
| 2338 | DCHECK(to_ref == nullptr || region_space_->IsInToSpace(to_ref) || |
| 2339 | heap_->non_moving_space_->HasAddress(to_ref)) |
| 2340 | << "from_ref=" << from_ref << " to_ref=" << to_ref; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 2341 | } else if (rtype == space::RegionSpace::RegionType::kRegionTypeUnevacFromSpace) { |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 2342 | if (IsMarkedInUnevacFromSpace(from_ref)) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2343 | to_ref = from_ref; |
| 2344 | } else { |
| 2345 | to_ref = nullptr; |
| 2346 | } |
| 2347 | } else { |
| 2348 | // from_ref is in a non-moving space. |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 2349 | if (immune_spaces_.ContainsObject(from_ref)) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2350 | // An immune object is alive. |
| 2351 | to_ref = from_ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2352 | } else { |
| 2353 | // Non-immune non-moving space. Use the mark bitmap. |
| 2354 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 2355 | heap_mark_bitmap_->GetContinuousSpaceBitmap(from_ref); |
| 2356 | accounting::LargeObjectBitmap* los_bitmap = |
| 2357 | heap_mark_bitmap_->GetLargeObjectBitmap(from_ref); |
| 2358 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 2359 | bool is_los = mark_bitmap == nullptr; |
| 2360 | if (!is_los && mark_bitmap->Test(from_ref)) { |
| 2361 | // Already marked. |
| 2362 | to_ref = from_ref; |
| 2363 | } else if (is_los && los_bitmap->Test(from_ref)) { |
| 2364 | // Already marked in LOS. |
| 2365 | to_ref = from_ref; |
| 2366 | } else { |
| 2367 | // Not marked. |
| 2368 | if (IsOnAllocStack(from_ref)) { |
| 2369 | // If on the allocation stack, it's considered marked. |
| 2370 | to_ref = from_ref; |
| 2371 | } else { |
| 2372 | // Not marked. |
| 2373 | to_ref = nullptr; |
| 2374 | } |
| 2375 | } |
| 2376 | } |
| 2377 | } |
| 2378 | return to_ref; |
| 2379 | } |
| 2380 | |
| 2381 | bool ConcurrentCopying::IsOnAllocStack(mirror::Object* ref) { |
| 2382 | QuasiAtomic::ThreadFenceAcquire(); |
| 2383 | accounting::ObjectStack* alloc_stack = GetAllocationStack(); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 2384 | return alloc_stack->Contains(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2385 | } |
| 2386 | |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 2387 | mirror::Object* ConcurrentCopying::MarkNonMoving(mirror::Object* ref, |
| 2388 | mirror::Object* holder, |
| 2389 | MemberOffset offset) { |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 2390 | // ref is in a non-moving space (from_ref == to_ref). |
| 2391 | DCHECK(!region_space_->HasAddress(ref)) << ref; |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2392 | DCHECK(!immune_spaces_.ContainsObject(ref)); |
| 2393 | // Use the mark bitmap. |
| 2394 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 2395 | heap_mark_bitmap_->GetContinuousSpaceBitmap(ref); |
| 2396 | accounting::LargeObjectBitmap* los_bitmap = |
| 2397 | heap_mark_bitmap_->GetLargeObjectBitmap(ref); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2398 | bool is_los = mark_bitmap == nullptr; |
| 2399 | if (!is_los && mark_bitmap->Test(ref)) { |
| 2400 | // Already marked. |
| 2401 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2402 | DCHECK(ref->GetReadBarrierState() == ReadBarrier::GrayState() || |
| 2403 | ref->GetReadBarrierState() == ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2404 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2405 | } else if (is_los && los_bitmap->Test(ref)) { |
| 2406 | // Already marked in LOS. |
| 2407 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2408 | DCHECK(ref->GetReadBarrierState() == ReadBarrier::GrayState() || |
| 2409 | ref->GetReadBarrierState() == ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2410 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2411 | } else { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2412 | // Not marked. |
| 2413 | if (IsOnAllocStack(ref)) { |
| 2414 | // If it's on the allocation stack, it's considered marked. Keep it white. |
| 2415 | // Objects on the allocation stack need not be marked. |
| 2416 | if (!is_los) { |
| 2417 | DCHECK(!mark_bitmap->Test(ref)); |
| 2418 | } else { |
| 2419 | DCHECK(!los_bitmap->Test(ref)); |
Nicolas Geoffray | ddeb172 | 2016-06-28 08:25:59 +0000 | [diff] [blame] | 2420 | } |
Nicolas Geoffray | ddeb172 | 2016-06-28 08:25:59 +0000 | [diff] [blame] | 2421 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2422 | DCHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2423 | } |
| 2424 | } else { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2425 | // For the baker-style RB, we need to handle 'false-gray' cases. See the |
| 2426 | // kRegionTypeUnevacFromSpace-case comment in Mark(). |
| 2427 | if (kUseBakerReadBarrier) { |
| 2428 | // Test the bitmap first to reduce the chance of false gray cases. |
| 2429 | if ((!is_los && mark_bitmap->Test(ref)) || |
| 2430 | (is_los && los_bitmap->Test(ref))) { |
| 2431 | return ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2432 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2433 | } |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 2434 | if (is_los && !IsAligned<kPageSize>(ref)) { |
| 2435 | // Ref is a large object that is not aligned, it must be heap corruption. Dump data before |
| 2436 | // AtomicSetReadBarrierState since it will fault if the address is not valid. |
Mathieu Chartier | ef496d9 | 2017-04-28 18:58:59 -0700 | [diff] [blame] | 2437 | heap_->GetVerification()->LogHeapCorruption(holder, offset, ref, /* fatal */ true); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 2438 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2439 | // Not marked or on the allocation stack. Try to mark it. |
| 2440 | // This may or may not succeed, which is ok. |
| 2441 | bool cas_success = false; |
| 2442 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2443 | cas_success = ref->AtomicSetReadBarrierState(ReadBarrier::WhiteState(), |
| 2444 | ReadBarrier::GrayState()); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2445 | } |
| 2446 | if (!is_los && mark_bitmap->AtomicTestAndSet(ref)) { |
| 2447 | // Already marked. |
| 2448 | if (kUseBakerReadBarrier && cas_success && |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2449 | ref->GetReadBarrierState() == ReadBarrier::GrayState()) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2450 | PushOntoFalseGrayStack(ref); |
| 2451 | } |
| 2452 | } else if (is_los && los_bitmap->AtomicTestAndSet(ref)) { |
| 2453 | // Already marked in LOS. |
| 2454 | if (kUseBakerReadBarrier && cas_success && |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2455 | ref->GetReadBarrierState() == ReadBarrier::GrayState()) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2456 | PushOntoFalseGrayStack(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2457 | } |
| 2458 | } else { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2459 | // Newly marked. |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 2460 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2461 | DCHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::GrayState()); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 2462 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2463 | PushOntoMarkStack(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | } |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 2467 | return ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | void ConcurrentCopying::FinishPhase() { |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 2471 | Thread* const self = Thread::Current(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2472 | { |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 2473 | MutexLock mu(self, mark_stack_lock_); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2474 | CHECK_EQ(pooled_mark_stacks_.size(), kMarkStackPoolSize); |
| 2475 | } |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 2476 | // kVerifyNoMissingCardMarks relies on the region space cards not being cleared to avoid false |
| 2477 | // positives. |
| 2478 | if (!kVerifyNoMissingCardMarks) { |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 2479 | TimingLogger::ScopedTiming split("ClearRegionSpaceCards", GetTimings()); |
| 2480 | // We do not currently use the region space cards at all, madvise them away to save ram. |
| 2481 | heap_->GetCardTable()->ClearCardRange(region_space_->Begin(), region_space_->Limit()); |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 2482 | } |
| 2483 | { |
| 2484 | MutexLock mu(self, skipped_blocks_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2485 | skipped_blocks_map_.clear(); |
| 2486 | } |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 2487 | { |
| 2488 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 2489 | { |
| 2490 | WriterMutexLock mu2(self, *Locks::heap_bitmap_lock_); |
| 2491 | heap_->ClearMarkedObjects(); |
| 2492 | } |
| 2493 | if (kUseBakerReadBarrier && kFilterModUnionCards) { |
| 2494 | TimingLogger::ScopedTiming split("FilterModUnionCards", GetTimings()); |
| 2495 | ReaderMutexLock mu2(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 2496 | for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) { |
| 2497 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 2498 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 2499 | // Filter out cards that don't need to be set. |
| 2500 | if (table != nullptr) { |
| 2501 | table->FilterCards(); |
| 2502 | } |
| 2503 | } |
| 2504 | } |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 2505 | if (kUseBakerReadBarrier) { |
| 2506 | TimingLogger::ScopedTiming split("EmptyRBMarkBitStack", GetTimings()); |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 2507 | DCHECK(rb_mark_bit_stack_ != nullptr); |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 2508 | const auto* limit = rb_mark_bit_stack_->End(); |
| 2509 | for (StackReference<mirror::Object>* it = rb_mark_bit_stack_->Begin(); it != limit; ++it) { |
| 2510 | CHECK(it->AsMirrorPtr()->AtomicSetMarkBit(1, 0)); |
| 2511 | } |
| 2512 | rb_mark_bit_stack_->Reset(); |
| 2513 | } |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 2514 | } |
| 2515 | if (measure_read_barrier_slow_path_) { |
| 2516 | MutexLock mu(self, rb_slow_path_histogram_lock_); |
| 2517 | rb_slow_path_time_histogram_.AdjustAndAddValue(rb_slow_path_ns_.LoadRelaxed()); |
| 2518 | rb_slow_path_count_total_ += rb_slow_path_count_.LoadRelaxed(); |
| 2519 | rb_slow_path_count_gc_total_ += rb_slow_path_count_gc_.LoadRelaxed(); |
| 2520 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2521 | } |
| 2522 | |
Hiroshi Yamauchi | 65f5f24 | 2016-12-19 11:44:47 -0800 | [diff] [blame] | 2523 | bool ConcurrentCopying::IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* field, |
| 2524 | bool do_atomic_update) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2525 | mirror::Object* from_ref = field->AsMirrorPtr(); |
Hiroshi Yamauchi | 65f5f24 | 2016-12-19 11:44:47 -0800 | [diff] [blame] | 2526 | if (from_ref == nullptr) { |
| 2527 | return true; |
| 2528 | } |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2529 | mirror::Object* to_ref = IsMarked(from_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2530 | if (to_ref == nullptr) { |
| 2531 | return false; |
| 2532 | } |
| 2533 | if (from_ref != to_ref) { |
Hiroshi Yamauchi | 65f5f24 | 2016-12-19 11:44:47 -0800 | [diff] [blame] | 2534 | if (do_atomic_update) { |
| 2535 | do { |
| 2536 | if (field->AsMirrorPtr() != from_ref) { |
| 2537 | // Concurrently overwritten by a mutator. |
| 2538 | break; |
| 2539 | } |
| 2540 | } while (!field->CasWeakRelaxed(from_ref, to_ref)); |
| 2541 | } else { |
| 2542 | QuasiAtomic::ThreadFenceRelease(); |
| 2543 | field->Assign(to_ref); |
| 2544 | QuasiAtomic::ThreadFenceSequentiallyConsistent(); |
| 2545 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2546 | } |
| 2547 | return true; |
| 2548 | } |
| 2549 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2550 | mirror::Object* ConcurrentCopying::MarkObject(mirror::Object* from_ref) { |
| 2551 | return Mark(from_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2552 | } |
| 2553 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 2554 | void ConcurrentCopying::DelayReferenceReferent(ObjPtr<mirror::Class> klass, |
| 2555 | ObjPtr<mirror::Reference> reference) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2556 | heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2557 | } |
| 2558 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2559 | void ConcurrentCopying::ProcessReferences(Thread* self) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2560 | TimingLogger::ScopedTiming split("ProcessReferences", GetTimings()); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2561 | // We don't really need to lock the heap bitmap lock as we use CAS to mark in bitmaps. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2562 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 2563 | GetHeap()->GetReferenceProcessor()->ProcessReferences( |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2564 | true /*concurrent*/, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | void ConcurrentCopying::RevokeAllThreadLocalBuffers() { |
| 2568 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
| 2569 | region_space_->RevokeAllThreadLocalBuffers(); |
| 2570 | } |
| 2571 | |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 2572 | mirror::Object* ConcurrentCopying::MarkFromReadBarrierWithMeasurements(mirror::Object* from_ref) { |
| 2573 | if (Thread::Current() != thread_running_gc_) { |
| 2574 | rb_slow_path_count_.FetchAndAddRelaxed(1u); |
| 2575 | } else { |
| 2576 | rb_slow_path_count_gc_.FetchAndAddRelaxed(1u); |
| 2577 | } |
| 2578 | ScopedTrace tr(__FUNCTION__); |
| 2579 | const uint64_t start_time = measure_read_barrier_slow_path_ ? NanoTime() : 0u; |
| 2580 | mirror::Object* ret = Mark(from_ref); |
| 2581 | if (measure_read_barrier_slow_path_) { |
| 2582 | rb_slow_path_ns_.FetchAndAddRelaxed(NanoTime() - start_time); |
| 2583 | } |
| 2584 | return ret; |
| 2585 | } |
| 2586 | |
| 2587 | void ConcurrentCopying::DumpPerformanceInfo(std::ostream& os) { |
| 2588 | GarbageCollector::DumpPerformanceInfo(os); |
| 2589 | MutexLock mu(Thread::Current(), rb_slow_path_histogram_lock_); |
| 2590 | if (rb_slow_path_time_histogram_.SampleSize() > 0) { |
| 2591 | Histogram<uint64_t>::CumulativeData cumulative_data; |
| 2592 | rb_slow_path_time_histogram_.CreateHistogram(&cumulative_data); |
| 2593 | rb_slow_path_time_histogram_.PrintConfidenceIntervals(os, 0.99, cumulative_data); |
| 2594 | } |
| 2595 | if (rb_slow_path_count_total_ > 0) { |
| 2596 | os << "Slow path count " << rb_slow_path_count_total_ << "\n"; |
| 2597 | } |
| 2598 | if (rb_slow_path_count_gc_total_ > 0) { |
| 2599 | os << "GC slow path count " << rb_slow_path_count_gc_total_ << "\n"; |
| 2600 | } |
Mathieu Chartier | cca44a0 | 2016-08-17 10:07:29 -0700 | [diff] [blame] | 2601 | os << "Cumulative bytes moved " << cumulative_bytes_moved_.LoadRelaxed() << "\n"; |
| 2602 | os << "Cumulative objects moved " << cumulative_objects_moved_.LoadRelaxed() << "\n"; |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 2603 | } |
| 2604 | |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 2605 | } // namespace collector |
| 2606 | } // namespace gc |
| 2607 | } // namespace art |