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 | |
| 19 | #include "base/stl_util.h" |
| 20 | #include "card_table-inl.h" |
| 21 | #include "heap_bitmap.h" |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 22 | #include "gc/collector/mark_sweep.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "gc/collector/mark_sweep-inl.h" |
| 24 | #include "gc/heap.h" |
| 25 | #include "gc/space/space.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 26 | #include "mirror/art_field-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
| 28 | #include "mirror/class-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | #include "mirror/object_array-inl.h" |
| 30 | #include "space_bitmap-inl.h" |
| 31 | #include "thread.h" |
| 32 | #include "UniquePtr.h" |
| 33 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 34 | using ::art::mirror::Object; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | namespace gc { |
| 38 | namespace accounting { |
| 39 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 40 | class ModUnionClearCardSetVisitor { |
| 41 | public: |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 42 | explicit ModUnionClearCardSetVisitor(ModUnionTable::CardSet* const cleared_cards) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 43 | : cleared_cards_(cleared_cards) { |
| 44 | } |
| 45 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 46 | inline void operator()(byte* card, byte expected_value, byte new_value) 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 | |
| 56 | class ModUnionClearCardVisitor { |
| 57 | public: |
| 58 | explicit ModUnionClearCardVisitor(std::vector<byte*>* cleared_cards) |
| 59 | : cleared_cards_(cleared_cards) { |
| 60 | } |
| 61 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 62 | void operator()(byte* card, byte expected_card, byte new_card) const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 63 | if (expected_card == CardTable::kCardDirty) { |
| 64 | cleared_cards_->push_back(card); |
| 65 | } |
| 66 | } |
| 67 | private: |
| 68 | std::vector<byte*>* const cleared_cards_; |
| 69 | }; |
| 70 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 71 | class ModUnionUpdateObjectReferencesVisitor { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 72 | public: |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 73 | ModUnionUpdateObjectReferencesVisitor(MarkObjectCallback* callback, void* arg) |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 74 | : callback_(callback), |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 75 | arg_(arg) { |
| 76 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 77 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 78 | // Extra parameters are required since we use this same visitor signature for checking objects. |
| 79 | void operator()(Object* obj, Object* ref, const MemberOffset& offset, |
| 80 | bool /* is_static */) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 81 | // Only add the reference if it is non null and fits our criteria. |
| 82 | if (ref != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 83 | Object* new_ref = callback_(ref, arg_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 84 | if (new_ref != ref) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 85 | // Use SetFieldObjectWithoutWriteBarrier to avoid card mark as an optimization which |
| 86 | // reduces dirtied pages and improves performance. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 87 | if (Runtime::Current()->IsActiveTransaction()) { |
| 88 | obj->SetFieldObjectWithoutWriteBarrier<true>(offset, new_ref, true); |
| 89 | } else { |
| 90 | obj->SetFieldObjectWithoutWriteBarrier<false>(offset, new_ref, true); |
| 91 | } |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private: |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 97 | MarkObjectCallback* const callback_; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 98 | void* arg_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 101 | class ModUnionScanImageRootVisitor { |
| 102 | public: |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 103 | ModUnionScanImageRootVisitor(MarkObjectCallback* callback, void* arg) |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 104 | : callback_(callback), arg_(arg) {} |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 105 | |
| 106 | void operator()(Object* root) const |
| 107 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 108 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 109 | DCHECK(root != NULL); |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 110 | ModUnionUpdateObjectReferencesVisitor ref_visitor(callback_, arg_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 111 | collector::MarkSweep::VisitObjectReferences(root, ref_visitor, true); |
| 112 | } |
| 113 | |
| 114 | private: |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 115 | MarkObjectCallback* const callback_; |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 116 | void* const arg_; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | void ModUnionTableReferenceCache::ClearCards() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 120 | CardTable* card_table = GetHeap()->GetCardTable(); |
| 121 | ModUnionClearCardSetVisitor visitor(&cleared_cards_); |
| 122 | // 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] | 123 | card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | class AddToReferenceArrayVisitor { |
| 127 | public: |
| 128 | explicit AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 129 | std::vector<mirror::HeapReference<Object>*>* references) |
| 130 | : mod_union_table_(mod_union_table), references_(references) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Extra parameters are required since we use this same visitor signature for checking objects. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 134 | void operator()(Object* obj, Object* ref, const MemberOffset& offset, |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 135 | bool /* is_static */) const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 136 | // Only add the reference if it is non null and fits our criteria. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 137 | if (ref != nullptr && mod_union_table_->AddReference(obj, ref)) { |
| 138 | // Push the adddress of the reference. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 139 | references_->push_back(obj->GetFieldObjectReferenceAddr(offset)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | ModUnionTableReferenceCache* const mod_union_table_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 145 | std::vector<mirror::HeapReference<Object>*>* const references_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | class ModUnionReferenceVisitor { |
| 149 | public: |
| 150 | explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 151 | std::vector<mirror::HeapReference<Object>*>* references) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 152 | : mod_union_table_(mod_union_table), |
| 153 | references_(references) { |
| 154 | } |
| 155 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 156 | void operator()(Object* obj) const |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 157 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 158 | DCHECK(obj != NULL); |
| 159 | // We don't have an early exit since we use the visitor pattern, an early |
| 160 | // exit should significantly speed this up. |
| 161 | AddToReferenceArrayVisitor visitor(mod_union_table_, references_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 162 | collector::MarkSweep::VisitObjectReferences(obj, visitor, true); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 163 | } |
| 164 | private: |
| 165 | ModUnionTableReferenceCache* const mod_union_table_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 166 | std::vector<mirror::HeapReference<Object>*>* const references_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | class CheckReferenceVisitor { |
| 170 | public: |
| 171 | explicit CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table, |
| 172 | const std::set<const Object*>& references) |
| 173 | : mod_union_table_(mod_union_table), |
| 174 | references_(references) { |
| 175 | } |
| 176 | |
| 177 | // Extra parameters are required since we use this same visitor signature for checking objects. |
| 178 | // TODO: Fixme when anotatalysis works with visitors. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 179 | void operator()(Object* obj, Object* ref, |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 180 | const MemberOffset& /* offset */, bool /* is_static */) const |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 181 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 182 | Heap* heap = mod_union_table_->GetHeap(); |
| 183 | if (ref != NULL && mod_union_table_->AddReference(obj, ref) && |
| 184 | references_.find(ref) == references_.end()) { |
| 185 | space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false); |
| 186 | space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false); |
| 187 | LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << PrettyTypeOf(obj) << ")" |
| 188 | << "References " << reinterpret_cast<const void*>(ref) |
| 189 | << "(" << PrettyTypeOf(ref) << ") without being in mod-union table"; |
| 190 | LOG(INFO) << "FromSpace " << from_space->GetName() << " type " << from_space->GetGcRetentionPolicy(); |
| 191 | LOG(INFO) << "ToSpace " << to_space->GetName() << " type " << to_space->GetGcRetentionPolicy(); |
| 192 | mod_union_table_->GetHeap()->DumpSpaces(); |
| 193 | LOG(FATAL) << "FATAL ERROR"; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | ModUnionTableReferenceCache* const mod_union_table_; |
| 199 | const std::set<const Object*>& references_; |
| 200 | }; |
| 201 | |
| 202 | class ModUnionCheckReferences { |
| 203 | public: |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 204 | explicit ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table, |
| 205 | const std::set<const Object*>& references) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 206 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 207 | : mod_union_table_(mod_union_table), references_(references) { |
| 208 | } |
| 209 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 210 | void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 211 | Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current()); |
| 212 | DCHECK(obj != NULL); |
| 213 | CheckReferenceVisitor visitor(mod_union_table_, references_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 214 | collector::MarkSweep::VisitObjectReferences(obj, visitor, true); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | private: |
| 218 | ModUnionTableReferenceCache* const mod_union_table_; |
| 219 | const std::set<const Object*>& references_; |
| 220 | }; |
| 221 | |
| 222 | void ModUnionTableReferenceCache::Verify() { |
| 223 | // Start by checking that everything in the mod union table is marked. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 224 | for (const auto& ref_pair : references_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 225 | for (mirror::HeapReference<Object>* ref : ref_pair.second) { |
| 226 | CHECK(heap_->IsLiveObjectLocked(ref->AsMirrorPtr())); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | // 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] | 231 | CardTable* card_table = heap_->GetCardTable(); |
| 232 | SpaceBitmap* live_bitmap = space_->GetLiveBitmap(); |
| 233 | for (const auto& ref_pair : references_) { |
| 234 | const byte* card = ref_pair.first; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 235 | if (*card == CardTable::kCardClean) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 236 | std::set<const Object*> reference_set; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 237 | for (mirror::HeapReference<Object>* obj_ptr : ref_pair.second) { |
| 238 | reference_set.insert(obj_ptr->AsMirrorPtr()); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 239 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 240 | ModUnionCheckReferences visitor(this, reference_set); |
| 241 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card)); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 242 | live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void ModUnionTableReferenceCache::Dump(std::ostream& os) { |
| 248 | CardTable* card_table = heap_->GetCardTable(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 249 | os << "ModUnionTable cleared cards: ["; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 250 | for (byte* card_addr : cleared_cards_) { |
| 251 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 252 | uintptr_t end = start + CardTable::kCardSize; |
| 253 | os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ","; |
| 254 | } |
| 255 | os << "]\nModUnionTable references: ["; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 256 | for (const auto& ref_pair : references_) { |
| 257 | const byte* card_addr = ref_pair.first; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 258 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 259 | uintptr_t end = start + CardTable::kCardSize; |
| 260 | os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{"; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 261 | for (mirror::HeapReference<Object>* ref : ref_pair.second) { |
| 262 | os << reinterpret_cast<const void*>(ref->AsMirrorPtr()) << ","; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 263 | } |
| 264 | os << "},"; |
| 265 | } |
| 266 | } |
| 267 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 268 | void ModUnionTableReferenceCache::UpdateAndMarkReferences(MarkObjectCallback* callback, |
| 269 | void* arg) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 270 | Heap* heap = GetHeap(); |
| 271 | CardTable* card_table = heap->GetCardTable(); |
| 272 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 273 | std::vector<mirror::HeapReference<Object>*> cards_references; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 274 | ModUnionReferenceVisitor add_visitor(this, &cards_references); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 275 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 276 | for (const auto& card : cleared_cards_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 277 | // Clear and re-compute alloc space references associated with this card. |
| 278 | cards_references.clear(); |
| 279 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card)); |
| 280 | uintptr_t end = start + CardTable::kCardSize; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 281 | auto* space = heap->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false); |
| 282 | DCHECK(space != nullptr); |
| 283 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 284 | live_bitmap->VisitMarkedRange(start, end, add_visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 285 | |
| 286 | // Update the corresponding references for the card. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 287 | auto found = references_.find(card); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 288 | if (found == references_.end()) { |
| 289 | if (cards_references.empty()) { |
| 290 | // No reason to add empty array. |
| 291 | continue; |
| 292 | } |
| 293 | references_.Put(card, cards_references); |
| 294 | } else { |
| 295 | found->second = cards_references; |
| 296 | } |
| 297 | } |
| 298 | cleared_cards_.clear(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 299 | size_t count = 0; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 300 | for (const auto& ref : references_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 301 | for (mirror::HeapReference<Object>* obj_ptr : ref.second) { |
| 302 | Object* obj = obj_ptr->AsMirrorPtr(); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 303 | if (obj != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 304 | Object* new_obj = callback(obj, arg); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 305 | // Avoid dirtying pages in the image unless necessary. |
| 306 | if (new_obj != obj) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 307 | obj_ptr->Assign(new_obj); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 310 | } |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 311 | count += ref.second.size(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 312 | } |
| 313 | if (VLOG_IS_ON(heap)) { |
| 314 | VLOG(gc) << "Marked " << count << " references in mod union table"; |
| 315 | } |
| 316 | } |
| 317 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 318 | void ModUnionTableCardCache::ClearCards() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 319 | CardTable* card_table = GetHeap()->GetCardTable(); |
| 320 | ModUnionClearCardSetVisitor visitor(&cleared_cards_); |
| 321 | // 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] | 322 | card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Mark all references to the alloc space(s). |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 326 | void ModUnionTableCardCache::UpdateAndMarkReferences(MarkObjectCallback* callback, void* arg) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 327 | CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 328 | ModUnionScanImageRootVisitor scan_visitor(callback, arg); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 329 | SpaceBitmap* bitmap = space_->GetLiveBitmap(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 330 | for (const byte* card_addr : cleared_cards_) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 331 | uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
| 332 | DCHECK(space_->HasAddress(reinterpret_cast<Object*>(start))); |
| 333 | bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | void ModUnionTableCardCache::Dump(std::ostream& os) { |
| 338 | CardTable* card_table = heap_->GetCardTable(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 339 | os << "ModUnionTable dirty cards: ["; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 340 | for (const byte* card_addr : cleared_cards_) { |
| 341 | auto start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); |
| 342 | auto end = start + CardTable::kCardSize; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 343 | os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "\n"; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 344 | } |
| 345 | os << "]"; |
| 346 | } |
| 347 | |
| 348 | } // namespace accounting |
| 349 | } // namespace gc |
| 350 | } // namespace art |