Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "mod_union_table.h" |
| 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 21 | #include "base/logging.h" // For VLOG |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 23 | #include "bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | #include "card_table-inl.h" |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 25 | #include "gc/accounting/space_bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 26 | #include "gc/heap.h" |
Lei Li | 3befba4 | 2015-01-23 16:37:59 +0800 | [diff] [blame] | 27 | #include "gc/space/image_space.h" |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 28 | #include "gc/space/space.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | #include "mirror/object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 30 | #include "mirror/object-refvisitor-inl.h" |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 31 | #include "object_callbacks.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 32 | #include "space_bitmap-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 33 | #include "thread-current-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | namespace gc { |
| 37 | namespace accounting { |
| 38 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 39 | class ModUnionAddToCardSetVisitor { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 40 | public: |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 41 | explicit ModUnionAddToCardSetVisitor(ModUnionTable::CardSet* const cleared_cards) |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 42 | : cleared_cards_(cleared_cards) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 43 | |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 44 | inline void operator()(uint8_t* card, |
| 45 | uint8_t expected_value, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 46 | uint8_t new_value ATTRIBUTE_UNUSED) const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 47 | if (expected_value == CardTable::kCardDirty) { |
| 48 | cleared_cards_->insert(card); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private: |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 53 | ModUnionTable::CardSet* const cleared_cards_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 56 | class ModUnionAddToCardBitmapVisitor { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 57 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 58 | ModUnionAddToCardBitmapVisitor(ModUnionTable::CardBitmap* bitmap, CardTable* card_table) |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 59 | : bitmap_(bitmap), card_table_(card_table) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 60 | |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 61 | inline void operator()(uint8_t* card, |
| 62 | uint8_t expected_value, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 63 | uint8_t new_value ATTRIBUTE_UNUSED) const { |
| 64 | if (expected_value == CardTable::kCardDirty) { |
| 65 | // We want the address the card represents, not the address of the card. |
| 66 | bitmap_->Set(reinterpret_cast<uintptr_t>(card_table_->AddrFromCard(card))); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | ModUnionTable::CardBitmap* const bitmap_; |
| 72 | CardTable* const card_table_; |
| 73 | }; |
| 74 | |
| 75 | class ModUnionAddToCardVectorVisitor { |
| 76 | public: |
| 77 | explicit ModUnionAddToCardVectorVisitor(std::vector<uint8_t*>* cleared_cards) |
| 78 | : cleared_cards_(cleared_cards) { |
| 79 | } |
| 80 | |
| 81 | void operator()(uint8_t* card, uint8_t expected_card, uint8_t new_card ATTRIBUTE_UNUSED) const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 82 | if (expected_card == CardTable::kCardDirty) { |
| 83 | cleared_cards_->push_back(card); |
| 84 | } |
| 85 | } |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 86 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 87 | private: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 88 | std::vector<uint8_t*>* const cleared_cards_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 91 | class ModUnionUpdateObjectReferencesVisitor { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 92 | public: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 93 | ModUnionUpdateObjectReferencesVisitor(MarkObjectVisitor* visitor, |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 94 | space::ContinuousSpace* from_space, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 95 | space::ContinuousSpace* immune_space, |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 96 | bool* contains_reference_to_other_space) |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 97 | : visitor_(visitor), |
| 98 | from_space_(from_space), |
| 99 | immune_space_(immune_space), |
| 100 | contains_reference_to_other_space_(contains_reference_to_other_space) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 101 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 102 | // Extra parameters are required since we use this same visitor signature for checking objects. |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 103 | void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 104 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d82e89e | 2015-08-05 10:10:07 -0700 | [diff] [blame] | 105 | MarkReference(obj->GetFieldObjectReferenceAddr(offset)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 108 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 109 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d82e89e | 2015-08-05 10:10:07 -0700 | [diff] [blame] | 110 | VisitRoot(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 114 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d82e89e | 2015-08-05 10:10:07 -0700 | [diff] [blame] | 115 | MarkReference(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 118 | private: |
Hans Boehm | cc55e1d | 2017-07-27 15:28:07 -0700 | [diff] [blame] | 119 | template<typename CompressedReferenceType> |
| 120 | void MarkReference(CompressedReferenceType* obj_ptr) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 121 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d82e89e | 2015-08-05 10:10:07 -0700 | [diff] [blame] | 122 | // Only add the reference if it is non null and fits our criteria. |
| 123 | mirror::Object* ref = obj_ptr->AsMirrorPtr(); |
| 124 | if (ref != nullptr && !from_space_->HasAddress(ref) && !immune_space_->HasAddress(ref)) { |
| 125 | *contains_reference_to_other_space_ = true; |
| 126 | mirror::Object* new_object = visitor_->MarkObject(ref); |
| 127 | if (ref != new_object) { |
| 128 | obj_ptr->Assign(new_object); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 133 | MarkObjectVisitor* const visitor_; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 134 | // Space which we are scanning |
| 135 | space::ContinuousSpace* const from_space_; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 136 | space::ContinuousSpace* const immune_space_; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 137 | // Set if we have any references to another space. |
| 138 | bool* const contains_reference_to_other_space_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 141 | class ModUnionScanImageRootVisitor { |
| 142 | public: |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 143 | // Immune space is any other space which we don't care about references to. Currently this is |
| 144 | // the image space in the case of the zygote mod union table. |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 145 | ModUnionScanImageRootVisitor(MarkObjectVisitor* visitor, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 146 | space::ContinuousSpace* from_space, |
| 147 | space::ContinuousSpace* immune_space, |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 148 | bool* contains_reference_to_other_space) |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 149 | : visitor_(visitor), |
| 150 | from_space_(from_space), |
| 151 | immune_space_(immune_space), |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 152 | contains_reference_to_other_space_(contains_reference_to_other_space) {} |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 153 | |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 154 | void operator()(mirror::Object* root) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 155 | REQUIRES(Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 156 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 157 | DCHECK(root != nullptr); |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 158 | ModUnionUpdateObjectReferencesVisitor ref_visitor(visitor_, |
| 159 | from_space_, |
| 160 | immune_space_, |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 161 | contains_reference_to_other_space_); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 162 | root->VisitReferences(ref_visitor, VoidFunctor()); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 166 | MarkObjectVisitor* const visitor_; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 167 | // Space which we are scanning |
| 168 | space::ContinuousSpace* const from_space_; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 169 | space::ContinuousSpace* const immune_space_; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 170 | // Set if we have any references to another space. |
| 171 | bool* const contains_reference_to_other_space_; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 174 | void ModUnionTableReferenceCache::ProcessCards() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 175 | CardTable* card_table = GetHeap()->GetCardTable(); |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 176 | ModUnionAddToCardSetVisitor visitor(&cleared_cards_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 177 | // Clear dirty cards in the this space and update the corresponding mod-union bits. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 178 | card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 181 | void ModUnionTableReferenceCache::ClearTable() { |
| 182 | cleared_cards_.clear(); |
| 183 | references_.clear(); |
| 184 | } |
| 185 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 186 | class AddToReferenceArrayVisitor { |
| 187 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 188 | AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table, |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 189 | MarkObjectVisitor* visitor, |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 190 | std::vector<mirror::HeapReference<mirror::Object>*>* references, |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 191 | bool* has_target_reference) |
| 192 | : mod_union_table_(mod_union_table), |
| 193 | visitor_(visitor), |
| 194 | references_(references), |
| 195 | has_target_reference_(has_target_reference) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 196 | |
| 197 | // Extra parameters are required since we use this same visitor signature for checking objects. |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 198 | void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 199 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 200 | mirror::HeapReference<mirror::Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 201 | mirror::Object* ref = ref_ptr->AsMirrorPtr(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 202 | // Only add the reference if it is non null and fits our criteria. |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 203 | if (ref != nullptr && mod_union_table_->ShouldAddReference(ref)) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 204 | // Push the adddress of the reference. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 205 | references_->push_back(ref_ptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 209 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 210 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 211 | if (!root->IsNull()) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 212 | VisitRoot(root); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 217 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 218 | if (mod_union_table_->ShouldAddReference(root->AsMirrorPtr())) { |
| 219 | *has_target_reference_ = true; |
| 220 | // TODO: Add MarkCompressedReference callback here. |
Mathieu Chartier | 56f9a89 | 2016-04-06 15:27:19 -0700 | [diff] [blame] | 221 | mirror::Object* old_ref = root->AsMirrorPtr(); |
| 222 | mirror::Object* new_ref = visitor_->MarkObject(old_ref); |
| 223 | if (old_ref != new_ref) { |
| 224 | root->Assign(new_ref); |
| 225 | } |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 226 | } |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 229 | private: |
| 230 | ModUnionTableReferenceCache* const mod_union_table_; |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 231 | MarkObjectVisitor* const visitor_; |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 232 | std::vector<mirror::HeapReference<mirror::Object>*>* const references_; |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 233 | bool* const has_target_reference_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | class ModUnionReferenceVisitor { |
| 237 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 238 | ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table, |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 239 | MarkObjectVisitor* visitor, |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 240 | std::vector<mirror::HeapReference<mirror::Object>*>* references, |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 241 | bool* has_target_reference) |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 242 | : mod_union_table_(mod_union_table), |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 243 | visitor_(visitor), |
| 244 | references_(references), |
| 245 | has_target_reference_(has_target_reference) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 246 | |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 247 | void operator()(mirror::Object* obj) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 248 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 249 | // We don't have an early exit since we use the visitor pattern, an early |
| 250 | // exit should significantly speed this up. |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 251 | AddToReferenceArrayVisitor visitor(mod_union_table_, |
| 252 | visitor_, |
| 253 | references_, |
| 254 | has_target_reference_); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 255 | obj->VisitReferences(visitor, VoidFunctor()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 256 | } |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 257 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 258 | private: |
| 259 | ModUnionTableReferenceCache* const mod_union_table_; |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 260 | MarkObjectVisitor* const visitor_; |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 261 | std::vector<mirror::HeapReference<mirror::Object>*>* const references_; |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 262 | bool* const has_target_reference_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | class CheckReferenceVisitor { |
| 266 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 267 | CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table, |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 268 | const std::set<mirror::Object*>& references) |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 269 | : mod_union_table_(mod_union_table), |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 270 | references_(references) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 271 | |
| 272 | // Extra parameters are required since we use this same visitor signature for checking objects. |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 273 | void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 274 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 275 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 276 | if (ref != nullptr && |
| 277 | mod_union_table_->ShouldAddReference(ref) && |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 278 | references_.find(ref) == references_.end()) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 279 | Heap* heap = mod_union_table_->GetHeap(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 280 | space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false); |
| 281 | space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 282 | LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << obj->PrettyTypeOf() |
| 283 | << ")" << "References " |
| 284 | << reinterpret_cast<const void*>(ref) << "(" << mirror::Object::PrettyTypeOf(ref) |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 285 | << ") without being in mod-union table"; |
| 286 | LOG(INFO) << "FromSpace " << from_space->GetName() << " type " |
| 287 | << from_space->GetGcRetentionPolicy(); |
| 288 | LOG(INFO) << "ToSpace " << to_space->GetName() << " type " |
| 289 | << to_space->GetGcRetentionPolicy(); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 290 | heap->DumpSpaces(LOG_STREAM(INFO)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 291 | LOG(FATAL) << "FATAL ERROR"; |
| 292 | } |
| 293 | } |
| 294 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 295 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 296 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 297 | if (kIsDebugBuild && !root->IsNull()) { |
| 298 | VisitRoot(root); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 303 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 304 | DCHECK(!mod_union_table_->ShouldAddReference(root->AsMirrorPtr())); |
| 305 | } |
| 306 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 307 | private: |
| 308 | ModUnionTableReferenceCache* const mod_union_table_; |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 309 | const std::set<mirror::Object*>& references_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | class ModUnionCheckReferences { |
| 313 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 314 | ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table, |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 315 | const std::set<mirror::Object*>& references) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 316 | REQUIRES(Locks::heap_bitmap_lock_) |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 317 | : mod_union_table_(mod_union_table), references_(references) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 318 | |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 319 | void operator()(mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 320 | Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 321 | CheckReferenceVisitor visitor(mod_union_table_, references_); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 322 | obj->VisitReferences(visitor, VoidFunctor()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | private: |
| 326 | ModUnionTableReferenceCache* const mod_union_table_; |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 327 | const std::set<mirror::Object*>& references_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 328 | }; |
| 329 | |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 330 | class EmptyMarkObjectVisitor : public MarkObjectVisitor { |
| 331 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 332 | mirror::Object* MarkObject(mirror::Object* obj) override {return obj;} |
| 333 | void MarkHeapReference(mirror::HeapReference<mirror::Object>*, bool) override {} |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | void ModUnionTable::FilterCards() { |
| 337 | EmptyMarkObjectVisitor visitor; |
| 338 | // Use empty visitor since filtering is automatically done by UpdateAndMarkReferences. |
| 339 | UpdateAndMarkReferences(&visitor); |
| 340 | } |
| 341 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 342 | void ModUnionTableReferenceCache::Verify() { |
| 343 | // Start by checking that everything in the mod union table is marked. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 344 | for (const auto& ref_pair : references_) { |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 345 | for (mirror::HeapReference<mirror::Object>* ref : ref_pair.second) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 346 | CHECK(heap_->IsLiveObjectLocked(ref->AsMirrorPtr())); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | // Check the references of each clean card which is also in the mod union table. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 351 | CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 352 | ContinuousSpaceBitmap* live_bitmap = space_->GetLiveBitmap(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 353 | for (const auto& ref_pair : references_) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 354 | const uint8_t* card = ref_pair.first; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 355 | if (*card == CardTable::kCardClean) { |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 356 | std::set<mirror::Object*> reference_set; |
| 357 | for (mirror::HeapReference<mirror::Object>* obj_ptr : ref_pair.second) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 358 | reference_set.insert(obj_ptr->AsMirrorPtr()); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 359 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 360 | ModUnionCheckReferences visitor(this, reference_set); |
| 361 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card)); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 362 | live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void ModUnionTableReferenceCache::Dump(std::ostream& os) { |
| 368 | CardTable* card_table = heap_->GetCardTable(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 369 | os << "ModUnionTable cleared cards: ["; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 370 | for (uint8_t* card_addr : cleared_cards_) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 371 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 372 | uintptr_t end = start + CardTable::kCardSize; |
| 373 | os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ","; |
| 374 | } |
| 375 | os << "]\nModUnionTable references: ["; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 376 | for (const auto& ref_pair : references_) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 377 | const uint8_t* card_addr = ref_pair.first; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 378 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 379 | uintptr_t end = start + CardTable::kCardSize; |
| 380 | os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{"; |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 381 | for (mirror::HeapReference<mirror::Object>* ref : ref_pair.second) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 382 | os << reinterpret_cast<const void*>(ref->AsMirrorPtr()) << ","; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 383 | } |
| 384 | os << "},"; |
| 385 | } |
| 386 | } |
| 387 | |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 388 | void ModUnionTableReferenceCache::VisitObjects(ObjectCallback callback, void* arg) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 389 | CardTable* const card_table = heap_->GetCardTable(); |
| 390 | ContinuousSpaceBitmap* live_bitmap = space_->GetLiveBitmap(); |
| 391 | for (uint8_t* card : cleared_cards_) { |
| 392 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card)); |
| 393 | uintptr_t end = start + CardTable::kCardSize; |
| 394 | live_bitmap->VisitMarkedRange(start, |
| 395 | end, |
Yi Kong | 88307ed | 2017-04-25 22:33:06 -0700 | [diff] [blame] | 396 | [callback, arg](mirror::Object* obj) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 397 | callback(obj, arg); |
| 398 | }); |
| 399 | } |
| 400 | // This may visit the same card twice, TODO avoid this. |
| 401 | for (const auto& pair : references_) { |
| 402 | const uint8_t* card = pair.first; |
| 403 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card)); |
| 404 | uintptr_t end = start + CardTable::kCardSize; |
| 405 | live_bitmap->VisitMarkedRange(start, |
| 406 | end, |
Yi Kong | 88307ed | 2017-04-25 22:33:06 -0700 | [diff] [blame] | 407 | [callback, arg](mirror::Object* obj) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 408 | callback(obj, arg); |
| 409 | }); |
| 410 | } |
| 411 | } |
| 412 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 413 | void ModUnionTableReferenceCache::UpdateAndMarkReferences(MarkObjectVisitor* visitor) { |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 414 | CardTable* const card_table = heap_->GetCardTable(); |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 415 | std::vector<mirror::HeapReference<mirror::Object>*> cards_references; |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 416 | // If has_target_reference is true then there was a GcRoot compressed reference which wasn't |
| 417 | // added. In this case we need to keep the card dirty. |
| 418 | // We don't know if the GcRoot addresses will remain constant, for example, classloaders have a |
| 419 | // hash set of GcRoot which may be resized or modified. |
| 420 | bool has_target_reference; |
| 421 | ModUnionReferenceVisitor add_visitor(this, visitor, &cards_references, &has_target_reference); |
| 422 | CardSet new_cleared_cards; |
| 423 | for (uint8_t* card : cleared_cards_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 424 | // Clear and re-compute alloc space references associated with this card. |
| 425 | cards_references.clear(); |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 426 | has_target_reference = false; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 427 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card)); |
| 428 | uintptr_t end = start + CardTable::kCardSize; |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 429 | space::ContinuousSpace* space = |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 430 | heap_->FindContinuousSpaceFromObject(reinterpret_cast<mirror::Object*>(start), false); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 431 | DCHECK(space != nullptr); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 432 | ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 433 | live_bitmap->VisitMarkedRange(start, end, add_visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 434 | // Update the corresponding references for the card. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 435 | auto found = references_.find(card); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 436 | if (found == references_.end()) { |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 437 | // Don't add card for an empty reference array. |
| 438 | if (!cards_references.empty()) { |
| 439 | references_.Put(card, cards_references); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 440 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 441 | } else { |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 442 | if (cards_references.empty()) { |
| 443 | references_.erase(found); |
| 444 | } else { |
| 445 | found->second = cards_references; |
| 446 | } |
| 447 | } |
| 448 | if (has_target_reference) { |
| 449 | // Keep this card for next time since it contains a GcRoot which matches the |
| 450 | // ShouldAddReference criteria. This usually occurs for class loaders. |
| 451 | new_cleared_cards.insert(card); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 454 | cleared_cards_ = std::move(new_cleared_cards); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 455 | size_t count = 0; |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 456 | for (auto it = references_.begin(); it != references_.end();) { |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 457 | std::vector<mirror::HeapReference<mirror::Object>*>& references = it->second; |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 458 | // Since there is no card mark for setting a reference to null, we check each reference. |
| 459 | // If all of the references of a card are null then we can remove that card. This is racy |
| 460 | // with the mutators, but handled by rescanning dirty cards. |
| 461 | bool all_null = true; |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 462 | for (mirror::HeapReference<mirror::Object>* obj_ptr : references) { |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 463 | if (obj_ptr->AsMirrorPtr() != nullptr) { |
| 464 | all_null = false; |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 465 | visitor->MarkHeapReference(obj_ptr, /*do_atomic_update=*/ false); |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 466 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 467 | } |
Mathieu Chartier | a52f2f9 | 2015-08-16 14:37:04 -0700 | [diff] [blame] | 468 | count += references.size(); |
| 469 | if (!all_null) { |
| 470 | ++it; |
| 471 | } else { |
| 472 | // All null references, erase the array from the set. |
| 473 | it = references_.erase(it); |
| 474 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 475 | } |
| 476 | if (VLOG_IS_ON(heap)) { |
| 477 | VLOG(gc) << "Marked " << count << " references in mod union table"; |
| 478 | } |
| 479 | } |
| 480 | |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 481 | ModUnionTableCardCache::ModUnionTableCardCache(const std::string& name, |
| 482 | Heap* heap, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 483 | space::ContinuousSpace* space) |
| 484 | : ModUnionTable(name, heap, space) { |
| 485 | // Normally here we could use End() instead of Limit(), but for testing we may want to have a |
| 486 | // mod-union table for a space which can still grow. |
| 487 | if (!space->IsImageSpace()) { |
| 488 | CHECK_ALIGNED(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize); |
| 489 | } |
| 490 | card_bitmap_.reset(CardBitmap::Create( |
| 491 | "mod union bitmap", reinterpret_cast<uintptr_t>(space->Begin()), |
| 492 | RoundUp(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize))); |
| 493 | } |
| 494 | |
| 495 | class CardBitVisitor { |
| 496 | public: |
Mathieu Chartier | c895082 | 2015-09-15 14:07:10 -0700 | [diff] [blame] | 497 | CardBitVisitor(MarkObjectVisitor* visitor, |
| 498 | space::ContinuousSpace* space, |
| 499 | space::ContinuousSpace* immune_space, |
| 500 | ModUnionTable::CardBitmap* card_bitmap) |
| 501 | : visitor_(visitor), |
| 502 | space_(space), |
| 503 | immune_space_(immune_space), |
| 504 | bitmap_(space->GetLiveBitmap()), |
| 505 | card_bitmap_(card_bitmap) { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 506 | DCHECK(immune_space_ != nullptr); |
| 507 | } |
| 508 | |
| 509 | void operator()(size_t bit_index) const { |
| 510 | const uintptr_t start = card_bitmap_->AddrFromBitIndex(bit_index); |
| 511 | DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start))) |
| 512 | << start << " " << *space_; |
| 513 | bool reference_to_other_space = false; |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 514 | ModUnionScanImageRootVisitor scan_visitor(visitor_, space_, immune_space_, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 515 | &reference_to_other_space); |
| 516 | bitmap_->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor); |
| 517 | if (!reference_to_other_space) { |
| 518 | // No non null reference to another space, clear the bit. |
| 519 | card_bitmap_->ClearBit(bit_index); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 524 | MarkObjectVisitor* const visitor_; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 525 | space::ContinuousSpace* const space_; |
| 526 | space::ContinuousSpace* const immune_space_; |
| 527 | ContinuousSpaceBitmap* const bitmap_; |
| 528 | ModUnionTable::CardBitmap* const card_bitmap_; |
| 529 | }; |
| 530 | |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 531 | void ModUnionTableCardCache::ProcessCards() { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 532 | CardTable* const card_table = GetHeap()->GetCardTable(); |
| 533 | ModUnionAddToCardBitmapVisitor visitor(card_bitmap_.get(), card_table); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 534 | // Clear dirty cards in the this space and update the corresponding mod-union bits. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 535 | card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 538 | void ModUnionTableCardCache::ClearTable() { |
| 539 | card_bitmap_->Bitmap::Clear(); |
| 540 | } |
| 541 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 542 | // Mark all references to the alloc space(s). |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 543 | void ModUnionTableCardCache::UpdateAndMarkReferences(MarkObjectVisitor* visitor) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 544 | // TODO: Needs better support for multi-images? b/26317072 |
| 545 | space::ImageSpace* image_space = |
| 546 | heap_->GetBootImageSpaces().empty() ? nullptr : heap_->GetBootImageSpaces()[0]; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 547 | // If we don't have an image space, just pass in space_ as the immune space. Pass in the same |
| 548 | // space_ instead of image_space to avoid a null check in ModUnionUpdateObjectReferencesVisitor. |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 549 | CardBitVisitor bit_visitor(visitor, space_, image_space != nullptr ? image_space : space_, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 550 | card_bitmap_.get()); |
| 551 | card_bitmap_->VisitSetBits( |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 552 | 0, RoundUp(space_->Size(), CardTable::kCardSize) / CardTable::kCardSize, bit_visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 555 | void ModUnionTableCardCache::VisitObjects(ObjectCallback callback, void* arg) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 556 | card_bitmap_->VisitSetBits( |
| 557 | 0, |
| 558 | RoundUp(space_->Size(), CardTable::kCardSize) / CardTable::kCardSize, |
| 559 | [this, callback, arg](size_t bit_index) { |
| 560 | const uintptr_t start = card_bitmap_->AddrFromBitIndex(bit_index); |
| 561 | DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start))) |
| 562 | << start << " " << *space_; |
| 563 | space_->GetLiveBitmap()->VisitMarkedRange(start, |
| 564 | start + CardTable::kCardSize, |
Yi Kong | 88307ed | 2017-04-25 22:33:06 -0700 | [diff] [blame] | 565 | [callback, arg](mirror::Object* obj) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 566 | callback(obj, arg); |
| 567 | }); |
| 568 | }); |
| 569 | } |
| 570 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 571 | void ModUnionTableCardCache::Dump(std::ostream& os) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 572 | os << "ModUnionTable dirty cards: ["; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 573 | // TODO: Find cleaner way of doing this. |
| 574 | for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize); |
| 575 | addr += CardTable::kCardSize) { |
| 576 | if (card_bitmap_->Test(reinterpret_cast<uintptr_t>(addr))) { |
| 577 | os << reinterpret_cast<void*>(addr) << "-" |
| 578 | << reinterpret_cast<void*>(addr + CardTable::kCardSize) << "\n"; |
| 579 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 580 | } |
| 581 | os << "]"; |
| 582 | } |
| 583 | |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 584 | void ModUnionTableCardCache::SetCards() { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 585 | // Only clean up to the end since there cannot be any objects past the End() of the space. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 586 | for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize); |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 587 | addr += CardTable::kCardSize) { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 588 | card_bitmap_->Set(reinterpret_cast<uintptr_t>(addr)); |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 592 | bool ModUnionTableCardCache::ContainsCardFor(uintptr_t addr) { |
| 593 | return card_bitmap_->Test(addr); |
| 594 | } |
| 595 | |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 596 | void ModUnionTableReferenceCache::SetCards() { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 597 | for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize); |
| 598 | addr += CardTable::kCardSize) { |
| 599 | cleared_cards_.insert(heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr))); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | bool ModUnionTableReferenceCache::ContainsCardFor(uintptr_t addr) { |
| 604 | auto* card_ptr = heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr)); |
| 605 | return cleared_cards_.find(card_ptr) != cleared_cards_.end() || |
| 606 | references_.find(card_ptr) != references_.end(); |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 609 | } // namespace accounting |
| 610 | } // namespace gc |
| 611 | } // namespace art |