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