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