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