blob: a3fac58e8a58e384bacb8d0971465852a665733a [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"
Brian Carlstromea46f952013-07-30 01:26:50 -070031#include "mirror/art_field-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "mirror/object-inl.h"
33#include "mirror/class-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "mirror/object_array-inl.h"
35#include "space_bitmap-inl.h"
36#include "thread.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070038using ::art::mirror::Object;
Ian Rogers1d54e732013-05-02 21:10:01 -070039
40namespace art {
41namespace gc {
42namespace accounting {
43
Mathieu Chartier4858a932015-01-23 13:18:53 -080044class ModUnionAddToCardSetVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070045 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -080046 explicit ModUnionAddToCardSetVisitor(ModUnionTable::CardSet* const cleared_cards)
47 : cleared_cards_(cleared_cards) {
Ian Rogers1d54e732013-05-02 21:10:01 -070048 }
49
Mathieu Chartier4858a932015-01-23 13:18:53 -080050 inline void operator()(uint8_t* card, uint8_t expected_value,
51 uint8_t new_value ATTRIBUTE_UNUSED) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070052 if (expected_value == CardTable::kCardDirty) {
53 cleared_cards_->insert(card);
54 }
55 }
56
57 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070058 ModUnionTable::CardSet* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -070059};
60
Mathieu Chartier4858a932015-01-23 13:18:53 -080061class ModUnionAddToCardBitmapVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070062 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -080063 explicit ModUnionAddToCardBitmapVisitor(ModUnionTable::CardBitmap* bitmap,
64 CardTable* card_table)
65 : bitmap_(bitmap), card_table_(card_table) {
Ian Rogers1d54e732013-05-02 21:10:01 -070066 }
67
Mathieu Chartier4858a932015-01-23 13:18:53 -080068 inline void operator()(uint8_t* card, uint8_t expected_value,
69 uint8_t new_value ATTRIBUTE_UNUSED) const {
70 if (expected_value == CardTable::kCardDirty) {
71 // We want the address the card represents, not the address of the card.
72 bitmap_->Set(reinterpret_cast<uintptr_t>(card_table_->AddrFromCard(card)));
73 }
74 }
75
76 private:
77 ModUnionTable::CardBitmap* const bitmap_;
78 CardTable* const card_table_;
79};
80
81class ModUnionAddToCardVectorVisitor {
82 public:
83 explicit ModUnionAddToCardVectorVisitor(std::vector<uint8_t*>* cleared_cards)
84 : cleared_cards_(cleared_cards) {
85 }
86
87 void operator()(uint8_t* card, uint8_t expected_card, uint8_t new_card ATTRIBUTE_UNUSED) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070088 if (expected_card == CardTable::kCardDirty) {
89 cleared_cards_->push_back(card);
90 }
91 }
Mathieu Chartier4858a932015-01-23 13:18:53 -080092
Ian Rogers1d54e732013-05-02 21:10:01 -070093 private:
Ian Rogers13735952014-10-08 12:43:28 -070094 std::vector<uint8_t*>* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -070095};
96
Mathieu Chartier11409ae2013-09-23 11:49:36 -070097class ModUnionUpdateObjectReferencesVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070098 public:
Mathieu Chartiere4cab172014-08-19 18:24:04 -070099 ModUnionUpdateObjectReferencesVisitor(MarkHeapReferenceCallback* callback, void* arg,
100 space::ContinuousSpace* from_space,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800101 space::ContinuousSpace* immune_space,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700102 bool* contains_reference_to_other_space)
Mathieu Chartier4858a932015-01-23 13:18:53 -0800103 : callback_(callback), arg_(arg), from_space_(from_space), immune_space_(immune_space),
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700104 contains_reference_to_other_space_(contains_reference_to_other_space) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700105 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700106
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700107 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800108 void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
Mathieu Chartier407f7022014-02-18 14:37:05 -0800109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700110 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800111 mirror::HeapReference<Object>* const obj_ptr = obj->GetFieldObjectReferenceAddr(offset);
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700112 mirror::Object* ref = obj_ptr->AsMirrorPtr();
Mathieu Chartier4858a932015-01-23 13:18:53 -0800113 if (ref != nullptr && !from_space_->HasAddress(ref) && !immune_space_->HasAddress(ref)) {
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700114 *contains_reference_to_other_space_ = true;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800115 callback_(obj_ptr, arg_);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700116 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700117 }
118
119 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800120 MarkHeapReferenceCallback* const callback_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800121 void* const arg_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700122 // Space which we are scanning
123 space::ContinuousSpace* const from_space_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800124 space::ContinuousSpace* const immune_space_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700125 // Set if we have any references to another space.
126 bool* const contains_reference_to_other_space_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700127};
128
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700129class ModUnionScanImageRootVisitor {
130 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800131 // Immune space is any other space which we don't care about references to. Currently this is
132 // the image space in the case of the zygote mod union table.
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700133 ModUnionScanImageRootVisitor(MarkHeapReferenceCallback* callback, void* arg,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800134 space::ContinuousSpace* from_space,
135 space::ContinuousSpace* immune_space,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700136 bool* contains_reference_to_other_space)
Mathieu Chartier4858a932015-01-23 13:18:53 -0800137 : callback_(callback), arg_(arg), from_space_(from_space), immune_space_(immune_space),
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700138 contains_reference_to_other_space_(contains_reference_to_other_space) {}
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700139
140 void operator()(Object* root) const
141 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800143 DCHECK(root != nullptr);
144 ModUnionUpdateObjectReferencesVisitor ref_visitor(callback_, arg_, from_space_, immune_space_,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700145 contains_reference_to_other_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700146 root->VisitReferences<kMovingClasses>(ref_visitor, VoidFunctor());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700147 }
148
149 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800150 MarkHeapReferenceCallback* const callback_;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800151 void* const arg_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700152 // Space which we are scanning
153 space::ContinuousSpace* const from_space_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800154 space::ContinuousSpace* const immune_space_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700155 // Set if we have any references to another space.
156 bool* const contains_reference_to_other_space_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700157};
158
159void ModUnionTableReferenceCache::ClearCards() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700160 CardTable* card_table = GetHeap()->GetCardTable();
Mathieu Chartier4858a932015-01-23 13:18:53 -0800161 ModUnionAddToCardSetVisitor visitor(&cleared_cards_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700162 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700163 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700164}
165
166class AddToReferenceArrayVisitor {
167 public:
168 explicit AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169 std::vector<mirror::HeapReference<Object>*>* references)
170 : mod_union_table_(mod_union_table), references_(references) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700171 }
172
173 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700174 void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
Mathieu Chartier407f7022014-02-18 14:37:05 -0800175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
176 mirror::HeapReference<Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset);
177 mirror::Object* ref = ref_ptr->AsMirrorPtr();
Ian Rogers1d54e732013-05-02 21:10:01 -0700178 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700179 if (ref != nullptr && mod_union_table_->ShouldAddReference(ref)) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700180 // Push the adddress of the reference.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800181 references_->push_back(ref_ptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700182 }
183 }
184
185 private:
186 ModUnionTableReferenceCache* const mod_union_table_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800187 std::vector<mirror::HeapReference<Object>*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700188};
189
190class ModUnionReferenceVisitor {
191 public:
192 explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800193 std::vector<mirror::HeapReference<Object>*>* references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700194 : mod_union_table_(mod_union_table),
195 references_(references) {
196 }
197
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700198 void operator()(Object* obj) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700199 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700200 // We don't have an early exit since we use the visitor pattern, an early
201 // exit should significantly speed this up.
202 AddToReferenceArrayVisitor visitor(mod_union_table_, references_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700203 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Ian Rogers1d54e732013-05-02 21:10:01 -0700204 }
205 private:
206 ModUnionTableReferenceCache* const mod_union_table_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800207 std::vector<mirror::HeapReference<Object>*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700208};
209
210class CheckReferenceVisitor {
211 public:
212 explicit CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table,
213 const std::set<const Object*>& references)
214 : mod_union_table_(mod_union_table),
215 references_(references) {
216 }
217
218 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700219 void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700220 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700221 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700222 if (ref != nullptr && mod_union_table_->ShouldAddReference(ref) &&
Ian Rogers1d54e732013-05-02 21:10:01 -0700223 references_.find(ref) == references_.end()) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800224 Heap* heap = mod_union_table_->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -0700225 space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false);
226 space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800227 LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << PrettyTypeOf(obj)
228 << ")" << "References " << reinterpret_cast<const void*>(ref) << "(" << PrettyTypeOf(ref)
229 << ") without being in mod-union table";
230 LOG(INFO) << "FromSpace " << from_space->GetName() << " type "
231 << from_space->GetGcRetentionPolicy();
232 LOG(INFO) << "ToSpace " << to_space->GetName() << " type "
233 << to_space->GetGcRetentionPolicy();
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700234 heap->DumpSpaces(LOG(INFO));
Ian Rogers1d54e732013-05-02 21:10:01 -0700235 LOG(FATAL) << "FATAL ERROR";
236 }
237 }
238
239 private:
240 ModUnionTableReferenceCache* const mod_union_table_;
241 const std::set<const Object*>& references_;
242};
243
244class ModUnionCheckReferences {
245 public:
Brian Carlstromdf629502013-07-17 22:39:56 -0700246 explicit ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table,
247 const std::set<const Object*>& references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700248 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
249 : mod_union_table_(mod_union_table), references_(references) {
250 }
251
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700252 void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers1d54e732013-05-02 21:10:01 -0700253 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
Ian Rogers1d54e732013-05-02 21:10:01 -0700254 CheckReferenceVisitor visitor(mod_union_table_, references_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700255 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Ian Rogers1d54e732013-05-02 21:10:01 -0700256 }
257
258 private:
259 ModUnionTableReferenceCache* const mod_union_table_;
260 const std::set<const Object*>& references_;
261};
262
263void ModUnionTableReferenceCache::Verify() {
264 // Start by checking that everything in the mod union table is marked.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700265 for (const auto& ref_pair : references_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800266 for (mirror::HeapReference<Object>* ref : ref_pair.second) {
267 CHECK(heap_->IsLiveObjectLocked(ref->AsMirrorPtr()));
Ian Rogers1d54e732013-05-02 21:10:01 -0700268 }
269 }
270
271 // Check the references of each clean card which is also in the mod union table.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700272 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700273 ContinuousSpaceBitmap* live_bitmap = space_->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700274 for (const auto& ref_pair : references_) {
Ian Rogers13735952014-10-08 12:43:28 -0700275 const uint8_t* card = ref_pair.first;
Ian Rogers1d54e732013-05-02 21:10:01 -0700276 if (*card == CardTable::kCardClean) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700277 std::set<const Object*> reference_set;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800278 for (mirror::HeapReference<Object>* obj_ptr : ref_pair.second) {
279 reference_set.insert(obj_ptr->AsMirrorPtr());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700280 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700281 ModUnionCheckReferences visitor(this, reference_set);
282 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700283 live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700284 }
285 }
286}
287
288void ModUnionTableReferenceCache::Dump(std::ostream& os) {
289 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700290 os << "ModUnionTable cleared cards: [";
Ian Rogers13735952014-10-08 12:43:28 -0700291 for (uint8_t* card_addr : cleared_cards_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700292 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700293 uintptr_t end = start + CardTable::kCardSize;
294 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
295 }
296 os << "]\nModUnionTable references: [";
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700297 for (const auto& ref_pair : references_) {
Ian Rogers13735952014-10-08 12:43:28 -0700298 const uint8_t* card_addr = ref_pair.first;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700299 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700300 uintptr_t end = start + CardTable::kCardSize;
301 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800302 for (mirror::HeapReference<Object>* ref : ref_pair.second) {
303 os << reinterpret_cast<const void*>(ref->AsMirrorPtr()) << ",";
Ian Rogers1d54e732013-05-02 21:10:01 -0700304 }
305 os << "},";
306 }
307}
308
Mathieu Chartier407f7022014-02-18 14:37:05 -0800309void ModUnionTableReferenceCache::UpdateAndMarkReferences(MarkHeapReferenceCallback* callback,
Mathieu Chartier815873e2014-02-13 18:02:13 -0800310 void* arg) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700311 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700312
Ian Rogersef7d42f2014-01-06 12:55:46 -0800313 std::vector<mirror::HeapReference<Object>*> cards_references;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700314 ModUnionReferenceVisitor add_visitor(this, &cards_references);
Ian Rogers1d54e732013-05-02 21:10:01 -0700315
Mathieu Chartier02e25112013-08-14 16:14:24 -0700316 for (const auto& card : cleared_cards_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700317 // Clear and re-compute alloc space references associated with this card.
318 cards_references.clear();
319 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
320 uintptr_t end = start + CardTable::kCardSize;
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700321 auto* space = heap_->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700322 DCHECK(space != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700323 ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700324 live_bitmap->VisitMarkedRange(start, end, add_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700325
326 // Update the corresponding references for the card.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700327 auto found = references_.find(card);
Ian Rogers1d54e732013-05-02 21:10:01 -0700328 if (found == references_.end()) {
329 if (cards_references.empty()) {
330 // No reason to add empty array.
331 continue;
332 }
333 references_.Put(card, cards_references);
334 } else {
335 found->second = cards_references;
336 }
337 }
338 cleared_cards_.clear();
Ian Rogers1d54e732013-05-02 21:10:01 -0700339 size_t count = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700340 for (const auto& ref : references_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800341 for (mirror::HeapReference<Object>* obj_ptr : ref.second) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800342 callback(obj_ptr, arg);
Ian Rogers1d54e732013-05-02 21:10:01 -0700343 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700344 count += ref.second.size();
Ian Rogers1d54e732013-05-02 21:10:01 -0700345 }
346 if (VLOG_IS_ON(heap)) {
347 VLOG(gc) << "Marked " << count << " references in mod union table";
348 }
349}
350
Mathieu Chartier4858a932015-01-23 13:18:53 -0800351ModUnionTableCardCache::ModUnionTableCardCache(const std::string& name, Heap* heap,
352 space::ContinuousSpace* space)
353 : ModUnionTable(name, heap, space) {
354 // Normally here we could use End() instead of Limit(), but for testing we may want to have a
355 // mod-union table for a space which can still grow.
356 if (!space->IsImageSpace()) {
357 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize);
358 }
359 card_bitmap_.reset(CardBitmap::Create(
360 "mod union bitmap", reinterpret_cast<uintptr_t>(space->Begin()),
361 RoundUp(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize)));
362}
363
364class CardBitVisitor {
365 public:
366 CardBitVisitor(MarkHeapReferenceCallback* callback, void* arg, space::ContinuousSpace* space,
367 space::ContinuousSpace* immune_space, ModUnionTable::CardBitmap* card_bitmap)
368 : callback_(callback), arg_(arg), space_(space), immune_space_(immune_space),
369 bitmap_(space->GetLiveBitmap()), card_bitmap_(card_bitmap) {
370 DCHECK(immune_space_ != nullptr);
371 }
372
373 void operator()(size_t bit_index) const {
374 const uintptr_t start = card_bitmap_->AddrFromBitIndex(bit_index);
375 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start)))
376 << start << " " << *space_;
377 bool reference_to_other_space = false;
378 ModUnionScanImageRootVisitor scan_visitor(callback_, arg_, space_, immune_space_,
379 &reference_to_other_space);
380 bitmap_->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor);
381 if (!reference_to_other_space) {
382 // No non null reference to another space, clear the bit.
383 card_bitmap_->ClearBit(bit_index);
384 }
385 }
386
387 private:
388 MarkHeapReferenceCallback* const callback_;
389 void* const arg_;
390 space::ContinuousSpace* const space_;
391 space::ContinuousSpace* const immune_space_;
392 ContinuousSpaceBitmap* const bitmap_;
393 ModUnionTable::CardBitmap* const card_bitmap_;
394};
395
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700396void ModUnionTableCardCache::ClearCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800397 CardTable* const card_table = GetHeap()->GetCardTable();
398 ModUnionAddToCardBitmapVisitor visitor(card_bitmap_.get(), card_table);
Ian Rogers1d54e732013-05-02 21:10:01 -0700399 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700400 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700401}
402
403// Mark all references to the alloc space(s).
Mathieu Chartier407f7022014-02-18 14:37:05 -0800404void ModUnionTableCardCache::UpdateAndMarkReferences(MarkHeapReferenceCallback* callback,
405 void* arg) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800406 auto* image_space = heap_->GetImageSpace();
407 // If we don't have an image space, just pass in space_ as the immune space. Pass in the same
408 // space_ instead of image_space to avoid a null check in ModUnionUpdateObjectReferencesVisitor.
409 CardBitVisitor visitor(callback, arg, space_, image_space != nullptr ? image_space : space_,
410 card_bitmap_.get());
411 card_bitmap_->VisitSetBits(
412 0, RoundUp(space_->Size(), CardTable::kCardSize) / CardTable::kCardSize, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700413}
414
415void ModUnionTableCardCache::Dump(std::ostream& os) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700416 os << "ModUnionTable dirty cards: [";
Mathieu Chartier4858a932015-01-23 13:18:53 -0800417 // TODO: Find cleaner way of doing this.
418 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
419 addr += CardTable::kCardSize) {
420 if (card_bitmap_->Test(reinterpret_cast<uintptr_t>(addr))) {
421 os << reinterpret_cast<void*>(addr) << "-"
422 << reinterpret_cast<void*>(addr + CardTable::kCardSize) << "\n";
423 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700424 }
425 os << "]";
426}
427
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700428void ModUnionTableCardCache::SetCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800429 // 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 -0700430 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700431 addr += CardTable::kCardSize) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800432 card_bitmap_->Set(reinterpret_cast<uintptr_t>(addr));
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700433 }
434}
435
Mathieu Chartier4858a932015-01-23 13:18:53 -0800436bool ModUnionTableCardCache::ContainsCardFor(uintptr_t addr) {
437 return card_bitmap_->Test(addr);
438}
439
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700440void ModUnionTableReferenceCache::SetCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800441 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
442 addr += CardTable::kCardSize) {
443 cleared_cards_.insert(heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr)));
444 }
445}
446
447bool ModUnionTableReferenceCache::ContainsCardFor(uintptr_t addr) {
448 auto* card_ptr = heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr));
449 return cleared_cards_.find(card_ptr) != cleared_cards_.end() ||
450 references_.find(card_ptr) != references_.end();
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700451}
452
Ian Rogers1d54e732013-05-02 21:10:01 -0700453} // namespace accounting
454} // namespace gc
455} // namespace art