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