blob: cd3f910d952c994318c458b6a97a3489e9bd7dbb [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"
24#include "heap_bitmap.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070025#include "gc/accounting/space_bitmap-inl.h"
Mathieu Chartier11409ae2013-09-23 11:49:36 -070026#include "gc/collector/mark_sweep.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/collector/mark_sweep-inl.h"
28#include "gc/heap.h"
29#include "gc/space/space.h"
Lei Li3befba42015-01-23 16:37:59 +080030#include "gc/space/image_space.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "mirror/object-inl.h"
32#include "mirror/class-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "mirror/object_array-inl.h"
34#include "space_bitmap-inl.h"
35#include "thread.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070037using ::art::mirror::Object;
Ian Rogers1d54e732013-05-02 21:10:01 -070038
39namespace art {
40namespace gc {
41namespace accounting {
42
Mathieu Chartier4858a932015-01-23 13:18:53 -080043class ModUnionAddToCardSetVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070044 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -080045 explicit ModUnionAddToCardSetVisitor(ModUnionTable::CardSet* const cleared_cards)
46 : cleared_cards_(cleared_cards) {
Ian Rogers1d54e732013-05-02 21:10:01 -070047 }
48
Mathieu Chartier4858a932015-01-23 13:18:53 -080049 inline void operator()(uint8_t* card, uint8_t expected_value,
50 uint8_t new_value ATTRIBUTE_UNUSED) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070051 if (expected_value == CardTable::kCardDirty) {
52 cleared_cards_->insert(card);
53 }
54 }
55
56 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070057 ModUnionTable::CardSet* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -070058};
59
Mathieu Chartier4858a932015-01-23 13:18:53 -080060class ModUnionAddToCardBitmapVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070061 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -080062 explicit ModUnionAddToCardBitmapVisitor(ModUnionTable::CardBitmap* bitmap,
63 CardTable* card_table)
64 : bitmap_(bitmap), card_table_(card_table) {
Ian Rogers1d54e732013-05-02 21:10:01 -070065 }
66
Mathieu Chartier4858a932015-01-23 13:18:53 -080067 inline void operator()(uint8_t* card, uint8_t expected_value,
68 uint8_t new_value ATTRIBUTE_UNUSED) const {
69 if (expected_value == CardTable::kCardDirty) {
70 // We want the address the card represents, not the address of the card.
71 bitmap_->Set(reinterpret_cast<uintptr_t>(card_table_->AddrFromCard(card)));
72 }
73 }
74
75 private:
76 ModUnionTable::CardBitmap* const bitmap_;
77 CardTable* const card_table_;
78};
79
80class ModUnionAddToCardVectorVisitor {
81 public:
82 explicit ModUnionAddToCardVectorVisitor(std::vector<uint8_t*>* cleared_cards)
83 : cleared_cards_(cleared_cards) {
84 }
85
86 void operator()(uint8_t* card, uint8_t expected_card, uint8_t new_card ATTRIBUTE_UNUSED) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070087 if (expected_card == CardTable::kCardDirty) {
88 cleared_cards_->push_back(card);
89 }
90 }
Mathieu Chartier4858a932015-01-23 13:18:53 -080091
Ian Rogers1d54e732013-05-02 21:10:01 -070092 private:
Ian Rogers13735952014-10-08 12:43:28 -070093 std::vector<uint8_t*>* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -070094};
95
Mathieu Chartier11409ae2013-09-23 11:49:36 -070096class ModUnionUpdateObjectReferencesVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070097 public:
Mathieu Chartiere4cab172014-08-19 18:24:04 -070098 ModUnionUpdateObjectReferencesVisitor(MarkHeapReferenceCallback* callback, void* arg,
99 space::ContinuousSpace* from_space,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800100 space::ContinuousSpace* immune_space,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700101 bool* contains_reference_to_other_space)
Mathieu Chartier4858a932015-01-23 13:18:53 -0800102 : callback_(callback), arg_(arg), from_space_(from_space), immune_space_(immune_space),
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700103 contains_reference_to_other_space_(contains_reference_to_other_space) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700104 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700105
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700106 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800107 void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
Mathieu Chartier407f7022014-02-18 14:37:05 -0800108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700109 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800110 mirror::HeapReference<Object>* const obj_ptr = obj->GetFieldObjectReferenceAddr(offset);
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700111 mirror::Object* ref = obj_ptr->AsMirrorPtr();
Mathieu Chartier4858a932015-01-23 13:18:53 -0800112 if (ref != nullptr && !from_space_->HasAddress(ref) && !immune_space_->HasAddress(ref)) {
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700113 *contains_reference_to_other_space_ = true;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800114 callback_(obj_ptr, arg_);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700115 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700116 }
117
118 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800119 MarkHeapReferenceCallback* const callback_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800120 void* const arg_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700121 // Space which we are scanning
122 space::ContinuousSpace* const from_space_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800123 space::ContinuousSpace* const immune_space_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700124 // Set if we have any references to another space.
125 bool* const contains_reference_to_other_space_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700126};
127
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700128class ModUnionScanImageRootVisitor {
129 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800130 // Immune space is any other space which we don't care about references to. Currently this is
131 // the image space in the case of the zygote mod union table.
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700132 ModUnionScanImageRootVisitor(MarkHeapReferenceCallback* callback, void* arg,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800133 space::ContinuousSpace* from_space,
134 space::ContinuousSpace* immune_space,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700135 bool* contains_reference_to_other_space)
Mathieu Chartier4858a932015-01-23 13:18:53 -0800136 : callback_(callback), arg_(arg), from_space_(from_space), immune_space_(immune_space),
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700137 contains_reference_to_other_space_(contains_reference_to_other_space) {}
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700138
139 void operator()(Object* root) const
140 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800142 DCHECK(root != nullptr);
143 ModUnionUpdateObjectReferencesVisitor ref_visitor(callback_, arg_, from_space_, immune_space_,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700144 contains_reference_to_other_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700145 root->VisitReferences<kMovingClasses>(ref_visitor, VoidFunctor());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700146 }
147
148 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800149 MarkHeapReferenceCallback* const callback_;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800150 void* const arg_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700151 // Space which we are scanning
152 space::ContinuousSpace* const from_space_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800153 space::ContinuousSpace* const immune_space_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700154 // Set if we have any references to another space.
155 bool* const contains_reference_to_other_space_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700156};
157
158void ModUnionTableReferenceCache::ClearCards() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700159 CardTable* card_table = GetHeap()->GetCardTable();
Mathieu Chartier4858a932015-01-23 13:18:53 -0800160 ModUnionAddToCardSetVisitor visitor(&cleared_cards_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700161 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700162 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700163}
164
165class AddToReferenceArrayVisitor {
166 public:
167 explicit AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168 std::vector<mirror::HeapReference<Object>*>* references)
169 : mod_union_table_(mod_union_table), references_(references) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700170 }
171
172 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700173 void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
Mathieu Chartier407f7022014-02-18 14:37:05 -0800174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
175 mirror::HeapReference<Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset);
176 mirror::Object* ref = ref_ptr->AsMirrorPtr();
Ian Rogers1d54e732013-05-02 21:10:01 -0700177 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700178 if (ref != nullptr && mod_union_table_->ShouldAddReference(ref)) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700179 // Push the adddress of the reference.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800180 references_->push_back(ref_ptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700181 }
182 }
183
184 private:
185 ModUnionTableReferenceCache* const mod_union_table_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800186 std::vector<mirror::HeapReference<Object>*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700187};
188
189class ModUnionReferenceVisitor {
190 public:
191 explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800192 std::vector<mirror::HeapReference<Object>*>* references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700193 : mod_union_table_(mod_union_table),
194 references_(references) {
195 }
196
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700197 void operator()(Object* obj) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700198 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700199 // We don't have an early exit since we use the visitor pattern, an early
200 // exit should significantly speed this up.
201 AddToReferenceArrayVisitor visitor(mod_union_table_, references_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700202 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Ian Rogers1d54e732013-05-02 21:10:01 -0700203 }
204 private:
205 ModUnionTableReferenceCache* const mod_union_table_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800206 std::vector<mirror::HeapReference<Object>*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700207};
208
209class CheckReferenceVisitor {
210 public:
211 explicit CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table,
212 const std::set<const Object*>& references)
213 : mod_union_table_(mod_union_table),
214 references_(references) {
215 }
216
217 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700218 void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700219 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700220 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700221 if (ref != nullptr && mod_union_table_->ShouldAddReference(ref) &&
Ian Rogers1d54e732013-05-02 21:10:01 -0700222 references_.find(ref) == references_.end()) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800223 Heap* heap = mod_union_table_->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -0700224 space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false);
225 space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800226 LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << PrettyTypeOf(obj)
227 << ")" << "References " << reinterpret_cast<const void*>(ref) << "(" << PrettyTypeOf(ref)
228 << ") without being in mod-union table";
229 LOG(INFO) << "FromSpace " << from_space->GetName() << " type "
230 << from_space->GetGcRetentionPolicy();
231 LOG(INFO) << "ToSpace " << to_space->GetName() << " type "
232 << to_space->GetGcRetentionPolicy();
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700233 heap->DumpSpaces(LOG(INFO));
Ian Rogers1d54e732013-05-02 21:10:01 -0700234 LOG(FATAL) << "FATAL ERROR";
235 }
236 }
237
238 private:
239 ModUnionTableReferenceCache* const mod_union_table_;
240 const std::set<const Object*>& references_;
241};
242
243class ModUnionCheckReferences {
244 public:
Brian Carlstromdf629502013-07-17 22:39:56 -0700245 explicit ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table,
246 const std::set<const Object*>& references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700247 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
248 : mod_union_table_(mod_union_table), references_(references) {
249 }
250
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700251 void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers1d54e732013-05-02 21:10:01 -0700252 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
Ian Rogers1d54e732013-05-02 21:10:01 -0700253 CheckReferenceVisitor visitor(mod_union_table_, references_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700254 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Ian Rogers1d54e732013-05-02 21:10:01 -0700255 }
256
257 private:
258 ModUnionTableReferenceCache* const mod_union_table_;
259 const std::set<const Object*>& references_;
260};
261
262void ModUnionTableReferenceCache::Verify() {
263 // Start by checking that everything in the mod union table is marked.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700264 for (const auto& ref_pair : references_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265 for (mirror::HeapReference<Object>* ref : ref_pair.second) {
266 CHECK(heap_->IsLiveObjectLocked(ref->AsMirrorPtr()));
Ian Rogers1d54e732013-05-02 21:10:01 -0700267 }
268 }
269
270 // Check the references of each clean card which is also in the mod union table.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700271 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700272 ContinuousSpaceBitmap* live_bitmap = space_->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700273 for (const auto& ref_pair : references_) {
Ian Rogers13735952014-10-08 12:43:28 -0700274 const uint8_t* card = ref_pair.first;
Ian Rogers1d54e732013-05-02 21:10:01 -0700275 if (*card == CardTable::kCardClean) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700276 std::set<const Object*> reference_set;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800277 for (mirror::HeapReference<Object>* obj_ptr : ref_pair.second) {
278 reference_set.insert(obj_ptr->AsMirrorPtr());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700279 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700280 ModUnionCheckReferences visitor(this, reference_set);
281 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700282 live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700283 }
284 }
285}
286
287void ModUnionTableReferenceCache::Dump(std::ostream& os) {
288 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700289 os << "ModUnionTable cleared cards: [";
Ian Rogers13735952014-10-08 12:43:28 -0700290 for (uint8_t* card_addr : cleared_cards_) {
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) << ",";
294 }
295 os << "]\nModUnionTable references: [";
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700296 for (const auto& ref_pair : references_) {
Ian Rogers13735952014-10-08 12:43:28 -0700297 const uint8_t* card_addr = ref_pair.first;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700298 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700299 uintptr_t end = start + CardTable::kCardSize;
300 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800301 for (mirror::HeapReference<Object>* ref : ref_pair.second) {
302 os << reinterpret_cast<const void*>(ref->AsMirrorPtr()) << ",";
Ian Rogers1d54e732013-05-02 21:10:01 -0700303 }
304 os << "},";
305 }
306}
307
Mathieu Chartier407f7022014-02-18 14:37:05 -0800308void ModUnionTableReferenceCache::UpdateAndMarkReferences(MarkHeapReferenceCallback* callback,
Mathieu Chartier815873e2014-02-13 18:02:13 -0800309 void* arg) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700310 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700311
Ian Rogersef7d42f2014-01-06 12:55:46 -0800312 std::vector<mirror::HeapReference<Object>*> cards_references;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700313 ModUnionReferenceVisitor add_visitor(this, &cards_references);
Ian Rogers1d54e732013-05-02 21:10:01 -0700314
Mathieu Chartier02e25112013-08-14 16:14:24 -0700315 for (const auto& card : cleared_cards_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700316 // Clear and re-compute alloc space references associated with this card.
317 cards_references.clear();
318 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
319 uintptr_t end = start + CardTable::kCardSize;
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700320 auto* space = heap_->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700321 DCHECK(space != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700322 ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700323 live_bitmap->VisitMarkedRange(start, end, add_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700324
325 // Update the corresponding references for the card.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700326 auto found = references_.find(card);
Ian Rogers1d54e732013-05-02 21:10:01 -0700327 if (found == references_.end()) {
328 if (cards_references.empty()) {
329 // No reason to add empty array.
330 continue;
331 }
332 references_.Put(card, cards_references);
333 } else {
334 found->second = cards_references;
335 }
336 }
337 cleared_cards_.clear();
Ian Rogers1d54e732013-05-02 21:10:01 -0700338 size_t count = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700339 for (const auto& ref : references_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800340 for (mirror::HeapReference<Object>* obj_ptr : ref.second) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800341 callback(obj_ptr, arg);
Ian Rogers1d54e732013-05-02 21:10:01 -0700342 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700343 count += ref.second.size();
Ian Rogers1d54e732013-05-02 21:10:01 -0700344 }
345 if (VLOG_IS_ON(heap)) {
346 VLOG(gc) << "Marked " << count << " references in mod union table";
347 }
348}
349
Mathieu Chartier4858a932015-01-23 13:18:53 -0800350ModUnionTableCardCache::ModUnionTableCardCache(const std::string& name, Heap* heap,
351 space::ContinuousSpace* space)
352 : ModUnionTable(name, heap, space) {
353 // Normally here we could use End() instead of Limit(), but for testing we may want to have a
354 // mod-union table for a space which can still grow.
355 if (!space->IsImageSpace()) {
356 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize);
357 }
358 card_bitmap_.reset(CardBitmap::Create(
359 "mod union bitmap", reinterpret_cast<uintptr_t>(space->Begin()),
360 RoundUp(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize)));
361}
362
363class CardBitVisitor {
364 public:
365 CardBitVisitor(MarkHeapReferenceCallback* callback, void* arg, space::ContinuousSpace* space,
366 space::ContinuousSpace* immune_space, ModUnionTable::CardBitmap* card_bitmap)
367 : callback_(callback), arg_(arg), space_(space), immune_space_(immune_space),
368 bitmap_(space->GetLiveBitmap()), card_bitmap_(card_bitmap) {
369 DCHECK(immune_space_ != nullptr);
370 }
371
372 void operator()(size_t bit_index) const {
373 const uintptr_t start = card_bitmap_->AddrFromBitIndex(bit_index);
374 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start)))
375 << start << " " << *space_;
376 bool reference_to_other_space = false;
377 ModUnionScanImageRootVisitor scan_visitor(callback_, arg_, space_, immune_space_,
378 &reference_to_other_space);
379 bitmap_->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor);
380 if (!reference_to_other_space) {
381 // No non null reference to another space, clear the bit.
382 card_bitmap_->ClearBit(bit_index);
383 }
384 }
385
386 private:
387 MarkHeapReferenceCallback* const callback_;
388 void* const arg_;
389 space::ContinuousSpace* const space_;
390 space::ContinuousSpace* const immune_space_;
391 ContinuousSpaceBitmap* const bitmap_;
392 ModUnionTable::CardBitmap* const card_bitmap_;
393};
394
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700395void ModUnionTableCardCache::ClearCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800396 CardTable* const card_table = GetHeap()->GetCardTable();
397 ModUnionAddToCardBitmapVisitor visitor(card_bitmap_.get(), card_table);
Ian Rogers1d54e732013-05-02 21:10:01 -0700398 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700399 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700400}
401
402// Mark all references to the alloc space(s).
Mathieu Chartier407f7022014-02-18 14:37:05 -0800403void ModUnionTableCardCache::UpdateAndMarkReferences(MarkHeapReferenceCallback* callback,
404 void* arg) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800405 auto* image_space = heap_->GetImageSpace();
406 // If we don't have an image space, just pass in space_ as the immune space. Pass in the same
407 // space_ instead of image_space to avoid a null check in ModUnionUpdateObjectReferencesVisitor.
408 CardBitVisitor visitor(callback, arg, space_, image_space != nullptr ? image_space : space_,
409 card_bitmap_.get());
410 card_bitmap_->VisitSetBits(
411 0, RoundUp(space_->Size(), CardTable::kCardSize) / CardTable::kCardSize, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700412}
413
414void ModUnionTableCardCache::Dump(std::ostream& os) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700415 os << "ModUnionTable dirty cards: [";
Mathieu Chartier4858a932015-01-23 13:18:53 -0800416 // TODO: Find cleaner way of doing this.
417 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
418 addr += CardTable::kCardSize) {
419 if (card_bitmap_->Test(reinterpret_cast<uintptr_t>(addr))) {
420 os << reinterpret_cast<void*>(addr) << "-"
421 << reinterpret_cast<void*>(addr + CardTable::kCardSize) << "\n";
422 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700423 }
424 os << "]";
425}
426
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700427void ModUnionTableCardCache::SetCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800428 // 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 -0700429 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700430 addr += CardTable::kCardSize) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800431 card_bitmap_->Set(reinterpret_cast<uintptr_t>(addr));
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700432 }
433}
434
Mathieu Chartier4858a932015-01-23 13:18:53 -0800435bool ModUnionTableCardCache::ContainsCardFor(uintptr_t addr) {
436 return card_bitmap_->Test(addr);
437}
438
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700439void ModUnionTableReferenceCache::SetCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800440 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
441 addr += CardTable::kCardSize) {
442 cleared_cards_.insert(heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr)));
443 }
444}
445
446bool ModUnionTableReferenceCache::ContainsCardFor(uintptr_t addr) {
447 auto* card_ptr = heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr));
448 return cleared_cards_.find(card_ptr) != cleared_cards_.end() ||
449 references_.find(card_ptr) != references_.end();
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700450}
451
Ian Rogers1d54e732013-05-02 21:10:01 -0700452} // namespace accounting
453} // namespace gc
454} // namespace art