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 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 21 | #include "base/stl_util.h" |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 22 | #include "bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "card_table-inl.h" |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 24 | #include "gc/accounting/space_bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 25 | #include "gc/heap.h" |
Lei Li | 3befba4 | 2015-01-23 16:37:59 +0800 | [diff] [blame] | 26 | #include "gc/space/image_space.h" |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 27 | #include "gc/space/space.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | #include "mirror/object-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | #include "space_bitmap-inl.h" |
| 30 | #include "thread.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 32 | using ::art::mirror::Object; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | namespace gc { |
| 36 | namespace accounting { |
| 37 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 38 | class ModUnionAddToCardSetVisitor { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | public: |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 40 | explicit ModUnionAddToCardSetVisitor(ModUnionTable::CardSet* const cleared_cards) |
| 41 | : cleared_cards_(cleared_cards) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 44 | inline void operator()(uint8_t* card, uint8_t expected_value, |
| 45 | uint8_t new_value ATTRIBUTE_UNUSED) const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | if (expected_value == CardTable::kCardDirty) { |
| 47 | cleared_cards_->insert(card); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private: |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 52 | ModUnionTable::CardSet* const cleared_cards_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 55 | class ModUnionAddToCardBitmapVisitor { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 56 | public: |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 57 | explicit ModUnionAddToCardBitmapVisitor(ModUnionTable::CardBitmap* bitmap, |
| 58 | CardTable* card_table) |
| 59 | : bitmap_(bitmap), card_table_(card_table) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 62 | inline void operator()(uint8_t* card, uint8_t expected_value, |
| 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 | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 97 | : visitor_(visitor), from_space_(from_space), immune_space_(immune_space), |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 98 | contains_reference_to_other_space_(contains_reference_to_other_space) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 99 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 100 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 101 | // Extra parameters are required since we use this same visitor signature for checking objects. |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 102 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 103 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d82e89e | 2015-08-05 10:10:07 -0700 | [diff] [blame^] | 104 | MarkReference(obj->GetFieldObjectReferenceAddr(offset)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 107 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 108 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d82e89e | 2015-08-05 10:10:07 -0700 | [diff] [blame^] | 109 | VisitRoot(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 113 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d82e89e | 2015-08-05 10:10:07 -0700 | [diff] [blame^] | 114 | MarkReference(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 117 | private: |
Mathieu Chartier | d82e89e | 2015-08-05 10:10:07 -0700 | [diff] [blame^] | 118 | template<bool kPoisonReferences> |
| 119 | void MarkReference(mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr) const |
| 120 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 121 | // Only add the reference if it is non null and fits our criteria. |
| 122 | mirror::Object* ref = obj_ptr->AsMirrorPtr(); |
| 123 | if (ref != nullptr && !from_space_->HasAddress(ref) && !immune_space_->HasAddress(ref)) { |
| 124 | *contains_reference_to_other_space_ = true; |
| 125 | mirror::Object* new_object = visitor_->MarkObject(ref); |
| 126 | if (ref != new_object) { |
| 127 | obj_ptr->Assign(new_object); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 132 | MarkObjectVisitor* const visitor_; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 133 | // Space which we are scanning |
| 134 | space::ContinuousSpace* const from_space_; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 135 | space::ContinuousSpace* const immune_space_; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 136 | // Set if we have any references to another space. |
| 137 | bool* const contains_reference_to_other_space_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 140 | class ModUnionScanImageRootVisitor { |
| 141 | public: |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 142 | // Immune space is any other space which we don't care about references to. Currently this is |
| 143 | // the image space in the case of the zygote mod union table. |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 144 | ModUnionScanImageRootVisitor(MarkObjectVisitor* visitor, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 145 | space::ContinuousSpace* from_space, |
| 146 | space::ContinuousSpace* immune_space, |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 147 | bool* contains_reference_to_other_space) |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 148 | : visitor_(visitor), from_space_(from_space), immune_space_(immune_space), |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 149 | contains_reference_to_other_space_(contains_reference_to_other_space) {} |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 150 | |
| 151 | void operator()(Object* root) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 152 | REQUIRES(Locks::heap_bitmap_lock_) |
| 153 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 154 | DCHECK(root != nullptr); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 155 | ModUnionUpdateObjectReferencesVisitor ref_visitor(visitor_, from_space_, immune_space_, |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 156 | contains_reference_to_other_space_); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 157 | root->VisitReferences<kMovingClasses>(ref_visitor, VoidFunctor()); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 161 | MarkObjectVisitor* const visitor_; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 162 | // Space which we are scanning |
| 163 | space::ContinuousSpace* const from_space_; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 164 | space::ContinuousSpace* const immune_space_; |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 165 | // Set if we have any references to another space. |
| 166 | bool* const contains_reference_to_other_space_; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | void ModUnionTableReferenceCache::ClearCards() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 170 | CardTable* card_table = GetHeap()->GetCardTable(); |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 171 | ModUnionAddToCardSetVisitor visitor(&cleared_cards_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 172 | // 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] | 173 | card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | class AddToReferenceArrayVisitor { |
| 177 | public: |
| 178 | explicit AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 179 | std::vector<mirror::HeapReference<Object>*>* references) |
| 180 | : mod_union_table_(mod_union_table), references_(references) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Extra parameters are required since we use this same visitor signature for checking objects. |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 184 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 185 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 186 | mirror::HeapReference<Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset); |
| 187 | mirror::Object* ref = ref_ptr->AsMirrorPtr(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 188 | // 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] | 189 | if (ref != nullptr && mod_union_table_->ShouldAddReference(ref)) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 190 | // Push the adddress of the reference. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 191 | references_->push_back(ref_ptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 195 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 196 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 197 | if (kIsDebugBuild && !root->IsNull()) { |
| 198 | VisitRoot(root); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 203 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 204 | DCHECK(!mod_union_table_->ShouldAddReference(root->AsMirrorPtr())); |
| 205 | } |
| 206 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 207 | private: |
| 208 | ModUnionTableReferenceCache* const mod_union_table_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 209 | std::vector<mirror::HeapReference<Object>*>* const references_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | class ModUnionReferenceVisitor { |
| 213 | public: |
| 214 | explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 215 | std::vector<mirror::HeapReference<Object>*>* references) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 216 | : mod_union_table_(mod_union_table), |
| 217 | references_(references) { |
| 218 | } |
| 219 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 220 | void operator()(Object* obj) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 221 | SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 222 | // We don't have an early exit since we use the visitor pattern, an early |
| 223 | // exit should significantly speed this up. |
| 224 | AddToReferenceArrayVisitor visitor(mod_union_table_, references_); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 225 | obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 226 | } |
| 227 | private: |
| 228 | ModUnionTableReferenceCache* const mod_union_table_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 229 | std::vector<mirror::HeapReference<Object>*>* const references_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | class CheckReferenceVisitor { |
| 233 | public: |
| 234 | explicit CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table, |
| 235 | const std::set<const Object*>& references) |
| 236 | : mod_union_table_(mod_union_table), |
| 237 | references_(references) { |
| 238 | } |
| 239 | |
| 240 | // Extra parameters are required since we use this same visitor signature for checking objects. |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 241 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 242 | SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 243 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 244 | if (ref != nullptr && mod_union_table_->ShouldAddReference(ref) && |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 245 | references_.find(ref) == references_.end()) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 246 | Heap* heap = mod_union_table_->GetHeap(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 247 | space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false); |
| 248 | space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 249 | LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << PrettyTypeOf(obj) |
| 250 | << ")" << "References " << reinterpret_cast<const void*>(ref) << "(" << PrettyTypeOf(ref) |
| 251 | << ") without being in mod-union table"; |
| 252 | LOG(INFO) << "FromSpace " << from_space->GetName() << " type " |
| 253 | << from_space->GetGcRetentionPolicy(); |
| 254 | LOG(INFO) << "ToSpace " << to_space->GetName() << " type " |
| 255 | << to_space->GetGcRetentionPolicy(); |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 256 | heap->DumpSpaces(LOG(INFO)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 257 | LOG(FATAL) << "FATAL ERROR"; |
| 258 | } |
| 259 | } |
| 260 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 261 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 262 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 263 | if (kIsDebugBuild && !root->IsNull()) { |
| 264 | VisitRoot(root); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 269 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 270 | DCHECK(!mod_union_table_->ShouldAddReference(root->AsMirrorPtr())); |
| 271 | } |
| 272 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 273 | private: |
| 274 | ModUnionTableReferenceCache* const mod_union_table_; |
| 275 | const std::set<const Object*>& references_; |
| 276 | }; |
| 277 | |
| 278 | class ModUnionCheckReferences { |
| 279 | public: |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 280 | explicit ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table, |
| 281 | const std::set<const Object*>& references) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 282 | REQUIRES(Locks::heap_bitmap_lock_) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 283 | : mod_union_table_(mod_union_table), references_(references) { |
| 284 | } |
| 285 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 286 | void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 287 | Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 288 | CheckReferenceVisitor visitor(mod_union_table_, references_); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 289 | obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | private: |
| 293 | ModUnionTableReferenceCache* const mod_union_table_; |
| 294 | const std::set<const Object*>& references_; |
| 295 | }; |
| 296 | |
| 297 | void ModUnionTableReferenceCache::Verify() { |
| 298 | // Start by checking that everything in the mod union table is marked. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 299 | for (const auto& ref_pair : references_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 300 | for (mirror::HeapReference<Object>* ref : ref_pair.second) { |
| 301 | CHECK(heap_->IsLiveObjectLocked(ref->AsMirrorPtr())); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | // 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] | 306 | CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 307 | ContinuousSpaceBitmap* live_bitmap = space_->GetLiveBitmap(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 308 | for (const auto& ref_pair : references_) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 309 | const uint8_t* card = ref_pair.first; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 310 | if (*card == CardTable::kCardClean) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 311 | std::set<const Object*> reference_set; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 312 | for (mirror::HeapReference<Object>* obj_ptr : ref_pair.second) { |
| 313 | reference_set.insert(obj_ptr->AsMirrorPtr()); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 314 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 315 | ModUnionCheckReferences visitor(this, reference_set); |
| 316 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card)); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 317 | live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void ModUnionTableReferenceCache::Dump(std::ostream& os) { |
| 323 | CardTable* card_table = heap_->GetCardTable(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 324 | os << "ModUnionTable cleared cards: ["; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 325 | for (uint8_t* card_addr : cleared_cards_) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 326 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 327 | uintptr_t end = start + CardTable::kCardSize; |
| 328 | os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ","; |
| 329 | } |
| 330 | os << "]\nModUnionTable references: ["; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 331 | for (const auto& ref_pair : references_) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 332 | const uint8_t* card_addr = ref_pair.first; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 333 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 334 | uintptr_t end = start + CardTable::kCardSize; |
| 335 | os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{"; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 336 | for (mirror::HeapReference<Object>* ref : ref_pair.second) { |
| 337 | os << reinterpret_cast<const void*>(ref->AsMirrorPtr()) << ","; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 338 | } |
| 339 | os << "},"; |
| 340 | } |
| 341 | } |
| 342 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 343 | void ModUnionTableReferenceCache::UpdateAndMarkReferences(MarkObjectVisitor* visitor) { |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 344 | CardTable* card_table = heap_->GetCardTable(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 345 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 346 | std::vector<mirror::HeapReference<Object>*> cards_references; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 347 | ModUnionReferenceVisitor add_visitor(this, &cards_references); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 348 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 349 | for (const auto& card : cleared_cards_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 350 | // Clear and re-compute alloc space references associated with this card. |
| 351 | cards_references.clear(); |
| 352 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card)); |
| 353 | uintptr_t end = start + CardTable::kCardSize; |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 354 | auto* space = heap_->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 355 | DCHECK(space != nullptr); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 356 | ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 357 | live_bitmap->VisitMarkedRange(start, end, add_visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 358 | |
| 359 | // Update the corresponding references for the card. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 360 | auto found = references_.find(card); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 361 | if (found == references_.end()) { |
| 362 | if (cards_references.empty()) { |
| 363 | // No reason to add empty array. |
| 364 | continue; |
| 365 | } |
| 366 | references_.Put(card, cards_references); |
| 367 | } else { |
| 368 | found->second = cards_references; |
| 369 | } |
| 370 | } |
| 371 | cleared_cards_.clear(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 372 | size_t count = 0; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 373 | for (const auto& ref : references_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 374 | for (mirror::HeapReference<Object>* obj_ptr : ref.second) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 375 | visitor->MarkHeapReference(obj_ptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 376 | } |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 377 | count += ref.second.size(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 378 | } |
| 379 | if (VLOG_IS_ON(heap)) { |
| 380 | VLOG(gc) << "Marked " << count << " references in mod union table"; |
| 381 | } |
| 382 | } |
| 383 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 384 | ModUnionTableCardCache::ModUnionTableCardCache(const std::string& name, Heap* heap, |
| 385 | space::ContinuousSpace* space) |
| 386 | : ModUnionTable(name, heap, space) { |
| 387 | // Normally here we could use End() instead of Limit(), but for testing we may want to have a |
| 388 | // mod-union table for a space which can still grow. |
| 389 | if (!space->IsImageSpace()) { |
| 390 | CHECK_ALIGNED(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize); |
| 391 | } |
| 392 | card_bitmap_.reset(CardBitmap::Create( |
| 393 | "mod union bitmap", reinterpret_cast<uintptr_t>(space->Begin()), |
| 394 | RoundUp(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize))); |
| 395 | } |
| 396 | |
| 397 | class CardBitVisitor { |
| 398 | public: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 399 | CardBitVisitor(MarkObjectVisitor* visitor, space::ContinuousSpace* space, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 400 | space::ContinuousSpace* immune_space, ModUnionTable::CardBitmap* card_bitmap) |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 401 | : visitor_(visitor), space_(space), immune_space_(immune_space), |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 402 | bitmap_(space->GetLiveBitmap()), card_bitmap_(card_bitmap) { |
| 403 | DCHECK(immune_space_ != nullptr); |
| 404 | } |
| 405 | |
| 406 | void operator()(size_t bit_index) const { |
| 407 | const uintptr_t start = card_bitmap_->AddrFromBitIndex(bit_index); |
| 408 | DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start))) |
| 409 | << start << " " << *space_; |
| 410 | bool reference_to_other_space = false; |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 411 | ModUnionScanImageRootVisitor scan_visitor(visitor_, space_, immune_space_, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 412 | &reference_to_other_space); |
| 413 | bitmap_->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor); |
| 414 | if (!reference_to_other_space) { |
| 415 | // No non null reference to another space, clear the bit. |
| 416 | card_bitmap_->ClearBit(bit_index); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 421 | MarkObjectVisitor* const visitor_; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 422 | space::ContinuousSpace* const space_; |
| 423 | space::ContinuousSpace* const immune_space_; |
| 424 | ContinuousSpaceBitmap* const bitmap_; |
| 425 | ModUnionTable::CardBitmap* const card_bitmap_; |
| 426 | }; |
| 427 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 428 | void ModUnionTableCardCache::ClearCards() { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 429 | CardTable* const card_table = GetHeap()->GetCardTable(); |
| 430 | ModUnionAddToCardBitmapVisitor visitor(card_bitmap_.get(), card_table); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 431 | // 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] | 432 | card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // Mark all references to the alloc space(s). |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 436 | void ModUnionTableCardCache::UpdateAndMarkReferences(MarkObjectVisitor* visitor) { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 437 | auto* image_space = heap_->GetImageSpace(); |
| 438 | // If we don't have an image space, just pass in space_ as the immune space. Pass in the same |
| 439 | // space_ instead of image_space to avoid a null check in ModUnionUpdateObjectReferencesVisitor. |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 440 | CardBitVisitor bit_visitor(visitor, space_, image_space != nullptr ? image_space : space_, |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 441 | card_bitmap_.get()); |
| 442 | card_bitmap_->VisitSetBits( |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 443 | 0, RoundUp(space_->Size(), CardTable::kCardSize) / CardTable::kCardSize, bit_visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void ModUnionTableCardCache::Dump(std::ostream& os) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 447 | os << "ModUnionTable dirty cards: ["; |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 448 | // TODO: Find cleaner way of doing this. |
| 449 | for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize); |
| 450 | addr += CardTable::kCardSize) { |
| 451 | if (card_bitmap_->Test(reinterpret_cast<uintptr_t>(addr))) { |
| 452 | os << reinterpret_cast<void*>(addr) << "-" |
| 453 | << reinterpret_cast<void*>(addr + CardTable::kCardSize) << "\n"; |
| 454 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 455 | } |
| 456 | os << "]"; |
| 457 | } |
| 458 | |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 459 | void ModUnionTableCardCache::SetCards() { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 460 | // 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] | 461 | for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize); |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 462 | addr += CardTable::kCardSize) { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 463 | card_bitmap_->Set(reinterpret_cast<uintptr_t>(addr)); |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 467 | bool ModUnionTableCardCache::ContainsCardFor(uintptr_t addr) { |
| 468 | return card_bitmap_->Test(addr); |
| 469 | } |
| 470 | |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 471 | void ModUnionTableReferenceCache::SetCards() { |
Mathieu Chartier | 4858a93 | 2015-01-23 13:18:53 -0800 | [diff] [blame] | 472 | for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize); |
| 473 | addr += CardTable::kCardSize) { |
| 474 | cleared_cards_.insert(heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr))); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | bool ModUnionTableReferenceCache::ContainsCardFor(uintptr_t addr) { |
| 479 | auto* card_ptr = heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr)); |
| 480 | return cleared_cards_.find(card_ptr) != cleared_cards_.end() || |
| 481 | references_.find(card_ptr) != references_.end(); |
Mathieu Chartier | e4cab17 | 2014-08-19 18:24:04 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 484 | } // namespace accounting |
| 485 | } // namespace gc |
| 486 | } // namespace art |