blob: 68e7fa03e348c73b36ddaa41fdd062473c50725b [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
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 Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
20
Ian Rogers1d54e732013-05-02 21:10:01 -070021#include "base/stl_util.h"
Mathieu Chartier4858a932015-01-23 13:18:53 -080022#include "bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "card_table-inl.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070024#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include "gc/heap.h"
Lei Li3befba42015-01-23 16:37:59 +080026#include "gc/space/image_space.h"
Mathieu Chartier97509952015-07-13 14:35:43 -070027#include "gc/space/space.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "mirror/object-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "space_bitmap-inl.h"
30#include "thread.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070032using ::art::mirror::Object;
Ian Rogers1d54e732013-05-02 21:10:01 -070033
34namespace art {
35namespace gc {
36namespace accounting {
37
Mathieu Chartier4858a932015-01-23 13:18:53 -080038class ModUnionAddToCardSetVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070039 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -080040 explicit ModUnionAddToCardSetVisitor(ModUnionTable::CardSet* const cleared_cards)
41 : cleared_cards_(cleared_cards) {
Ian Rogers1d54e732013-05-02 21:10:01 -070042 }
43
Mathieu Chartier4858a932015-01-23 13:18:53 -080044 inline void operator()(uint8_t* card, uint8_t expected_value,
45 uint8_t new_value ATTRIBUTE_UNUSED) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070046 if (expected_value == CardTable::kCardDirty) {
47 cleared_cards_->insert(card);
48 }
49 }
50
51 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070052 ModUnionTable::CardSet* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -070053};
54
Mathieu Chartier4858a932015-01-23 13:18:53 -080055class ModUnionAddToCardBitmapVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070056 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -080057 explicit ModUnionAddToCardBitmapVisitor(ModUnionTable::CardBitmap* bitmap,
58 CardTable* card_table)
59 : bitmap_(bitmap), card_table_(card_table) {
Ian Rogers1d54e732013-05-02 21:10:01 -070060 }
61
Mathieu Chartier4858a932015-01-23 13:18:53 -080062 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
75class 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 Rogers1d54e732013-05-02 21:10:01 -070082 if (expected_card == CardTable::kCardDirty) {
83 cleared_cards_->push_back(card);
84 }
85 }
Mathieu Chartier4858a932015-01-23 13:18:53 -080086
Ian Rogers1d54e732013-05-02 21:10:01 -070087 private:
Ian Rogers13735952014-10-08 12:43:28 -070088 std::vector<uint8_t*>* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -070089};
90
Mathieu Chartier11409ae2013-09-23 11:49:36 -070091class ModUnionUpdateObjectReferencesVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070092 public:
Mathieu Chartier97509952015-07-13 14:35:43 -070093 ModUnionUpdateObjectReferencesVisitor(MarkObjectVisitor* visitor,
Mathieu Chartiere4cab172014-08-19 18:24:04 -070094 space::ContinuousSpace* from_space,
Mathieu Chartier4858a932015-01-23 13:18:53 -080095 space::ContinuousSpace* immune_space,
Mathieu Chartiere4cab172014-08-19 18:24:04 -070096 bool* contains_reference_to_other_space)
Mathieu Chartier97509952015-07-13 14:35:43 -070097 : visitor_(visitor), from_space_(from_space), immune_space_(immune_space),
Mathieu Chartiere4cab172014-08-19 18:24:04 -070098 contains_reference_to_other_space_(contains_reference_to_other_space) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -070099 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700100
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700101 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800102 void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700104 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800105 mirror::HeapReference<Object>* const obj_ptr = obj->GetFieldObjectReferenceAddr(offset);
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700106 mirror::Object* ref = obj_ptr->AsMirrorPtr();
Mathieu Chartier4858a932015-01-23 13:18:53 -0800107 if (ref != nullptr && !from_space_->HasAddress(ref) && !immune_space_->HasAddress(ref)) {
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700108 *contains_reference_to_other_space_ = true;
Mathieu Chartier97509952015-07-13 14:35:43 -0700109 visitor_->MarkHeapReference(obj_ptr);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700110 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700111 }
112
113 private:
Mathieu Chartier97509952015-07-13 14:35:43 -0700114 MarkObjectVisitor* const visitor_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700115 // Space which we are scanning
116 space::ContinuousSpace* const from_space_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800117 space::ContinuousSpace* const immune_space_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700118 // Set if we have any references to another space.
119 bool* const contains_reference_to_other_space_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700120};
121
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700122class ModUnionScanImageRootVisitor {
123 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800124 // Immune space is any other space which we don't care about references to. Currently this is
125 // the image space in the case of the zygote mod union table.
Mathieu Chartier97509952015-07-13 14:35:43 -0700126 ModUnionScanImageRootVisitor(MarkObjectVisitor* visitor,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800127 space::ContinuousSpace* from_space,
128 space::ContinuousSpace* immune_space,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700129 bool* contains_reference_to_other_space)
Mathieu Chartier97509952015-07-13 14:35:43 -0700130 : visitor_(visitor), from_space_(from_space), immune_space_(immune_space),
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700131 contains_reference_to_other_space_(contains_reference_to_other_space) {}
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700132
133 void operator()(Object* root) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700134 REQUIRES(Locks::heap_bitmap_lock_)
135 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800136 DCHECK(root != nullptr);
Mathieu Chartier97509952015-07-13 14:35:43 -0700137 ModUnionUpdateObjectReferencesVisitor ref_visitor(visitor_, from_space_, immune_space_,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700138 contains_reference_to_other_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700139 root->VisitReferences<kMovingClasses>(ref_visitor, VoidFunctor());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700140 }
141
142 private:
Mathieu Chartier97509952015-07-13 14:35:43 -0700143 MarkObjectVisitor* const visitor_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700144 // Space which we are scanning
145 space::ContinuousSpace* const from_space_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800146 space::ContinuousSpace* const immune_space_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700147 // Set if we have any references to another space.
148 bool* const contains_reference_to_other_space_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700149};
150
151void ModUnionTableReferenceCache::ClearCards() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700152 CardTable* card_table = GetHeap()->GetCardTable();
Mathieu Chartier4858a932015-01-23 13:18:53 -0800153 ModUnionAddToCardSetVisitor visitor(&cleared_cards_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700154 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700155 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700156}
157
158class AddToReferenceArrayVisitor {
159 public:
160 explicit AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161 std::vector<mirror::HeapReference<Object>*>* references)
162 : mod_union_table_(mod_union_table), references_(references) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700163 }
164
165 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700166 void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800168 mirror::HeapReference<Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset);
169 mirror::Object* ref = ref_ptr->AsMirrorPtr();
Ian Rogers1d54e732013-05-02 21:10:01 -0700170 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700171 if (ref != nullptr && mod_union_table_->ShouldAddReference(ref)) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700172 // Push the adddress of the reference.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800173 references_->push_back(ref_ptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700174 }
175 }
176
177 private:
178 ModUnionTableReferenceCache* const mod_union_table_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800179 std::vector<mirror::HeapReference<Object>*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700180};
181
182class ModUnionReferenceVisitor {
183 public:
184 explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185 std::vector<mirror::HeapReference<Object>*>* references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700186 : mod_union_table_(mod_union_table),
187 references_(references) {
188 }
189
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700190 void operator()(Object* obj) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700192 // We don't have an early exit since we use the visitor pattern, an early
193 // exit should significantly speed this up.
194 AddToReferenceArrayVisitor visitor(mod_union_table_, references_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700195 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Ian Rogers1d54e732013-05-02 21:10:01 -0700196 }
197 private:
198 ModUnionTableReferenceCache* const mod_union_table_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800199 std::vector<mirror::HeapReference<Object>*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700200};
201
202class CheckReferenceVisitor {
203 public:
204 explicit CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table,
205 const std::set<const Object*>& references)
206 : mod_union_table_(mod_union_table),
207 references_(references) {
208 }
209
210 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700211 void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700212 SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700213 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700214 if (ref != nullptr && mod_union_table_->ShouldAddReference(ref) &&
Ian Rogers1d54e732013-05-02 21:10:01 -0700215 references_.find(ref) == references_.end()) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800216 Heap* heap = mod_union_table_->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -0700217 space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false);
218 space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800219 LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << PrettyTypeOf(obj)
220 << ")" << "References " << reinterpret_cast<const void*>(ref) << "(" << PrettyTypeOf(ref)
221 << ") without being in mod-union table";
222 LOG(INFO) << "FromSpace " << from_space->GetName() << " type "
223 << from_space->GetGcRetentionPolicy();
224 LOG(INFO) << "ToSpace " << to_space->GetName() << " type "
225 << to_space->GetGcRetentionPolicy();
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700226 heap->DumpSpaces(LOG(INFO));
Ian Rogers1d54e732013-05-02 21:10:01 -0700227 LOG(FATAL) << "FATAL ERROR";
228 }
229 }
230
231 private:
232 ModUnionTableReferenceCache* const mod_union_table_;
233 const std::set<const Object*>& references_;
234};
235
236class ModUnionCheckReferences {
237 public:
Brian Carlstromdf629502013-07-17 22:39:56 -0700238 explicit ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table,
239 const std::set<const Object*>& references)
Mathieu Chartier90443472015-07-16 20:32:27 -0700240 REQUIRES(Locks::heap_bitmap_lock_)
Ian Rogers1d54e732013-05-02 21:10:01 -0700241 : mod_union_table_(mod_union_table), references_(references) {
242 }
243
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700244 void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers1d54e732013-05-02 21:10:01 -0700245 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
Ian Rogers1d54e732013-05-02 21:10:01 -0700246 CheckReferenceVisitor visitor(mod_union_table_, references_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700247 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Ian Rogers1d54e732013-05-02 21:10:01 -0700248 }
249
250 private:
251 ModUnionTableReferenceCache* const mod_union_table_;
252 const std::set<const Object*>& references_;
253};
254
255void ModUnionTableReferenceCache::Verify() {
256 // Start by checking that everything in the mod union table is marked.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700257 for (const auto& ref_pair : references_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800258 for (mirror::HeapReference<Object>* ref : ref_pair.second) {
259 CHECK(heap_->IsLiveObjectLocked(ref->AsMirrorPtr()));
Ian Rogers1d54e732013-05-02 21:10:01 -0700260 }
261 }
262
263 // Check the references of each clean card which is also in the mod union table.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700264 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700265 ContinuousSpaceBitmap* live_bitmap = space_->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700266 for (const auto& ref_pair : references_) {
Ian Rogers13735952014-10-08 12:43:28 -0700267 const uint8_t* card = ref_pair.first;
Ian Rogers1d54e732013-05-02 21:10:01 -0700268 if (*card == CardTable::kCardClean) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700269 std::set<const Object*> reference_set;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800270 for (mirror::HeapReference<Object>* obj_ptr : ref_pair.second) {
271 reference_set.insert(obj_ptr->AsMirrorPtr());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700272 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700273 ModUnionCheckReferences visitor(this, reference_set);
274 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700275 live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700276 }
277 }
278}
279
280void ModUnionTableReferenceCache::Dump(std::ostream& os) {
281 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700282 os << "ModUnionTable cleared cards: [";
Ian Rogers13735952014-10-08 12:43:28 -0700283 for (uint8_t* card_addr : cleared_cards_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700284 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700285 uintptr_t end = start + CardTable::kCardSize;
286 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
287 }
288 os << "]\nModUnionTable references: [";
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700289 for (const auto& ref_pair : references_) {
Ian Rogers13735952014-10-08 12:43:28 -0700290 const uint8_t* card_addr = ref_pair.first;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700291 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700292 uintptr_t end = start + CardTable::kCardSize;
293 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800294 for (mirror::HeapReference<Object>* ref : ref_pair.second) {
295 os << reinterpret_cast<const void*>(ref->AsMirrorPtr()) << ",";
Ian Rogers1d54e732013-05-02 21:10:01 -0700296 }
297 os << "},";
298 }
299}
300
Mathieu Chartier97509952015-07-13 14:35:43 -0700301void ModUnionTableReferenceCache::UpdateAndMarkReferences(MarkObjectVisitor* visitor) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700302 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700303
Ian Rogersef7d42f2014-01-06 12:55:46 -0800304 std::vector<mirror::HeapReference<Object>*> cards_references;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700305 ModUnionReferenceVisitor add_visitor(this, &cards_references);
Ian Rogers1d54e732013-05-02 21:10:01 -0700306
Mathieu Chartier02e25112013-08-14 16:14:24 -0700307 for (const auto& card : cleared_cards_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700308 // Clear and re-compute alloc space references associated with this card.
309 cards_references.clear();
310 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
311 uintptr_t end = start + CardTable::kCardSize;
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700312 auto* space = heap_->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700313 DCHECK(space != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700314 ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700315 live_bitmap->VisitMarkedRange(start, end, add_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700316
317 // Update the corresponding references for the card.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700318 auto found = references_.find(card);
Ian Rogers1d54e732013-05-02 21:10:01 -0700319 if (found == references_.end()) {
320 if (cards_references.empty()) {
321 // No reason to add empty array.
322 continue;
323 }
324 references_.Put(card, cards_references);
325 } else {
326 found->second = cards_references;
327 }
328 }
329 cleared_cards_.clear();
Ian Rogers1d54e732013-05-02 21:10:01 -0700330 size_t count = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700331 for (const auto& ref : references_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332 for (mirror::HeapReference<Object>* obj_ptr : ref.second) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700333 visitor->MarkHeapReference(obj_ptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700334 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700335 count += ref.second.size();
Ian Rogers1d54e732013-05-02 21:10:01 -0700336 }
337 if (VLOG_IS_ON(heap)) {
338 VLOG(gc) << "Marked " << count << " references in mod union table";
339 }
340}
341
Mathieu Chartier4858a932015-01-23 13:18:53 -0800342ModUnionTableCardCache::ModUnionTableCardCache(const std::string& name, Heap* heap,
343 space::ContinuousSpace* space)
344 : ModUnionTable(name, heap, space) {
345 // Normally here we could use End() instead of Limit(), but for testing we may want to have a
346 // mod-union table for a space which can still grow.
347 if (!space->IsImageSpace()) {
348 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize);
349 }
350 card_bitmap_.reset(CardBitmap::Create(
351 "mod union bitmap", reinterpret_cast<uintptr_t>(space->Begin()),
352 RoundUp(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize)));
353}
354
355class CardBitVisitor {
356 public:
Mathieu Chartier97509952015-07-13 14:35:43 -0700357 CardBitVisitor(MarkObjectVisitor* visitor, space::ContinuousSpace* space,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800358 space::ContinuousSpace* immune_space, ModUnionTable::CardBitmap* card_bitmap)
Mathieu Chartier97509952015-07-13 14:35:43 -0700359 : visitor_(visitor), space_(space), immune_space_(immune_space),
Mathieu Chartier4858a932015-01-23 13:18:53 -0800360 bitmap_(space->GetLiveBitmap()), card_bitmap_(card_bitmap) {
361 DCHECK(immune_space_ != nullptr);
362 }
363
364 void operator()(size_t bit_index) const {
365 const uintptr_t start = card_bitmap_->AddrFromBitIndex(bit_index);
366 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start)))
367 << start << " " << *space_;
368 bool reference_to_other_space = false;
Mathieu Chartier97509952015-07-13 14:35:43 -0700369 ModUnionScanImageRootVisitor scan_visitor(visitor_, space_, immune_space_,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800370 &reference_to_other_space);
371 bitmap_->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor);
372 if (!reference_to_other_space) {
373 // No non null reference to another space, clear the bit.
374 card_bitmap_->ClearBit(bit_index);
375 }
376 }
377
378 private:
Mathieu Chartier97509952015-07-13 14:35:43 -0700379 MarkObjectVisitor* const visitor_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800380 space::ContinuousSpace* const space_;
381 space::ContinuousSpace* const immune_space_;
382 ContinuousSpaceBitmap* const bitmap_;
383 ModUnionTable::CardBitmap* const card_bitmap_;
384};
385
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700386void ModUnionTableCardCache::ClearCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800387 CardTable* const card_table = GetHeap()->GetCardTable();
388 ModUnionAddToCardBitmapVisitor visitor(card_bitmap_.get(), card_table);
Ian Rogers1d54e732013-05-02 21:10:01 -0700389 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700390 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700391}
392
393// Mark all references to the alloc space(s).
Mathieu Chartier97509952015-07-13 14:35:43 -0700394void ModUnionTableCardCache::UpdateAndMarkReferences(MarkObjectVisitor* visitor) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800395 auto* image_space = heap_->GetImageSpace();
396 // If we don't have an image space, just pass in space_ as the immune space. Pass in the same
397 // space_ instead of image_space to avoid a null check in ModUnionUpdateObjectReferencesVisitor.
Mathieu Chartier97509952015-07-13 14:35:43 -0700398 CardBitVisitor bit_visitor(visitor, space_, image_space != nullptr ? image_space : space_,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800399 card_bitmap_.get());
400 card_bitmap_->VisitSetBits(
Mathieu Chartier97509952015-07-13 14:35:43 -0700401 0, RoundUp(space_->Size(), CardTable::kCardSize) / CardTable::kCardSize, bit_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700402}
403
404void ModUnionTableCardCache::Dump(std::ostream& os) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700405 os << "ModUnionTable dirty cards: [";
Mathieu Chartier4858a932015-01-23 13:18:53 -0800406 // TODO: Find cleaner way of doing this.
407 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
408 addr += CardTable::kCardSize) {
409 if (card_bitmap_->Test(reinterpret_cast<uintptr_t>(addr))) {
410 os << reinterpret_cast<void*>(addr) << "-"
411 << reinterpret_cast<void*>(addr + CardTable::kCardSize) << "\n";
412 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700413 }
414 os << "]";
415}
416
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700417void ModUnionTableCardCache::SetCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800418 // Only clean up to the end since there cannot be any objects past the End() of the space.
Ian Rogers13735952014-10-08 12:43:28 -0700419 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700420 addr += CardTable::kCardSize) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800421 card_bitmap_->Set(reinterpret_cast<uintptr_t>(addr));
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700422 }
423}
424
Mathieu Chartier4858a932015-01-23 13:18:53 -0800425bool ModUnionTableCardCache::ContainsCardFor(uintptr_t addr) {
426 return card_bitmap_->Test(addr);
427}
428
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700429void ModUnionTableReferenceCache::SetCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800430 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
431 addr += CardTable::kCardSize) {
432 cleared_cards_.insert(heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr)));
433 }
434}
435
436bool ModUnionTableReferenceCache::ContainsCardFor(uintptr_t addr) {
437 auto* card_ptr = heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr));
438 return cleared_cards_.find(card_ptr) != cleared_cards_.end() ||
439 references_.find(card_ptr) != references_.end();
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700440}
441
Ian Rogers1d54e732013-05-02 21:10:01 -0700442} // namespace accounting
443} // namespace gc
444} // namespace art