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