Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [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 "remembered_set.h" |
| 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 21 | #include "base/stl_util.h" |
| 22 | #include "card_table-inl.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 23 | #include "gc/collector/mark_sweep-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "gc/collector/mark_sweep.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 25 | #include "gc/collector/semi_space.h" |
| 26 | #include "gc/heap.h" |
| 27 | #include "gc/space/space.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "heap_bitmap.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 29 | #include "mirror/class-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 30 | #include "mirror/object-inl.h" |
| 31 | #include "mirror/object-refvisitor-inl.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 32 | #include "mirror/object_array-inl.h" |
| 33 | #include "space_bitmap-inl.h" |
| 34 | #include "thread.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | namespace gc { |
| 38 | namespace accounting { |
| 39 | |
| 40 | class RememberedSetCardVisitor { |
| 41 | public: |
| 42 | explicit RememberedSetCardVisitor(RememberedSet::CardSet* const dirty_cards) |
| 43 | : dirty_cards_(dirty_cards) {} |
| 44 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 45 | void operator()(uint8_t* card, uint8_t expected_value, uint8_t new_value ATTRIBUTE_UNUSED) const { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 46 | if (expected_value == CardTable::kCardDirty) { |
| 47 | dirty_cards_->insert(card); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | RememberedSet::CardSet* const dirty_cards_; |
| 53 | }; |
| 54 | |
| 55 | void RememberedSet::ClearCards() { |
| 56 | CardTable* card_table = GetHeap()->GetCardTable(); |
| 57 | RememberedSetCardVisitor card_visitor(&dirty_cards_); |
| 58 | // Clear dirty cards in the space and insert them into the dirty card set. |
| 59 | card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), card_visitor); |
| 60 | } |
| 61 | |
| 62 | class RememberedSetReferenceVisitor { |
| 63 | public: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 64 | RememberedSetReferenceVisitor(space::ContinuousSpace* target_space, |
| 65 | bool* const contains_reference_to_target_space, |
| 66 | collector::GarbageCollector* collector) |
| 67 | : collector_(collector), target_space_(target_space), |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 68 | contains_reference_to_target_space_(contains_reference_to_target_space) {} |
| 69 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 70 | void operator()(ObjPtr<mirror::Object> obj, |
| 71 | MemberOffset offset, |
| 72 | bool is_static ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 73 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 74 | DCHECK(obj != nullptr); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 75 | mirror::HeapReference<mirror::Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset); |
| 76 | if (target_space_->HasAddress(ref_ptr->AsMirrorPtr())) { |
| 77 | *contains_reference_to_target_space_ = true; |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 78 | collector_->MarkHeapReference(ref_ptr, /*do_atomic_update=*/ false); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 79 | DCHECK(!target_space_->HasAddress(ref_ptr->AsMirrorPtr())); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 83 | void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 84 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 4db7449 | 2014-04-22 17:10:48 -0700 | [diff] [blame] | 85 | if (target_space_->HasAddress(ref->GetReferent())) { |
| 86 | *contains_reference_to_target_space_ = true; |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 87 | collector_->DelayReferenceReferent(klass, ref); |
Hiroshi Yamauchi | 4db7449 | 2014-04-22 17:10:48 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 91 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 92 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 93 | if (!root->IsNull()) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 94 | VisitRoot(root); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 99 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 3908912 | 2015-07-27 16:08:02 -0700 | [diff] [blame] | 100 | if (target_space_->HasAddress(root->AsMirrorPtr())) { |
| 101 | *contains_reference_to_target_space_ = true; |
| 102 | root->Assign(collector_->MarkObject(root->AsMirrorPtr())); |
| 103 | DCHECK(!target_space_->HasAddress(root->AsMirrorPtr())); |
| 104 | } |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 107 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 108 | collector::GarbageCollector* const collector_; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 109 | space::ContinuousSpace* const target_space_; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 110 | bool* const contains_reference_to_target_space_; |
| 111 | }; |
| 112 | |
| 113 | class RememberedSetObjectVisitor { |
| 114 | public: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 115 | RememberedSetObjectVisitor(space::ContinuousSpace* target_space, |
| 116 | bool* const contains_reference_to_target_space, |
| 117 | collector::GarbageCollector* collector) |
| 118 | : collector_(collector), target_space_(target_space), |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 119 | contains_reference_to_target_space_(contains_reference_to_target_space) {} |
| 120 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 121 | void operator()(ObjPtr<mirror::Object> obj) const REQUIRES(Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 122 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 123 | RememberedSetReferenceVisitor visitor(target_space_, contains_reference_to_target_space_, |
| 124 | collector_); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 125 | obj->VisitReferences(visitor, visitor); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 129 | collector::GarbageCollector* const collector_; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 130 | space::ContinuousSpace* const target_space_; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 131 | bool* const contains_reference_to_target_space_; |
| 132 | }; |
| 133 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 134 | void RememberedSet::UpdateAndMarkReferences(space::ContinuousSpace* target_space, |
| 135 | collector::GarbageCollector* collector) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 136 | CardTable* card_table = heap_->GetCardTable(); |
| 137 | bool contains_reference_to_target_space = false; |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 138 | RememberedSetObjectVisitor obj_visitor(target_space, &contains_reference_to_target_space, |
| 139 | collector); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 140 | ContinuousSpaceBitmap* bitmap = space_->GetLiveBitmap(); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 141 | CardSet remove_card_set; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 142 | for (uint8_t* const card_addr : dirty_cards_) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 143 | contains_reference_to_target_space = false; |
| 144 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
| 145 | DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start))); |
| 146 | bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, obj_visitor); |
| 147 | if (!contains_reference_to_target_space) { |
| 148 | // It was in the dirty card set, but it didn't actually contain |
| 149 | // a reference to the target space. So, remove it from the dirty |
| 150 | // card set so we won't have to scan it again (unless it gets |
| 151 | // dirty again.) |
| 152 | remove_card_set.insert(card_addr); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Remove the cards that didn't contain a reference to the target |
| 157 | // space from the dirty card set. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 158 | for (uint8_t* const card_addr : remove_card_set) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 159 | DCHECK(dirty_cards_.find(card_addr) != dirty_cards_.end()); |
| 160 | dirty_cards_.erase(card_addr); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void RememberedSet::Dump(std::ostream& os) { |
| 165 | CardTable* card_table = heap_->GetCardTable(); |
| 166 | os << "RememberedSet dirty cards: ["; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 167 | for (const uint8_t* card_addr : dirty_cards_) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 168 | auto start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
| 169 | auto end = start + CardTable::kCardSize; |
| 170 | os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "\n"; |
| 171 | } |
| 172 | os << "]"; |
| 173 | } |
| 174 | |
| 175 | void RememberedSet::AssertAllDirtyCardsAreWithinSpace() const { |
| 176 | CardTable* card_table = heap_->GetCardTable(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 177 | for (const uint8_t* card_addr : dirty_cards_) { |
| 178 | auto start = reinterpret_cast<uint8_t*>(card_table->AddrFromCard(card_addr)); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 179 | auto end = start + CardTable::kCardSize; |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 180 | DCHECK_LE(space_->Begin(), start); |
| 181 | DCHECK_LE(end, space_->Limit()); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | } // namespace accounting |
| 186 | } // namespace gc |
| 187 | } // namespace art |