blob: b4026fc3f3051dd343945f4ad2a2ba6f9ce7dfb2 [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
Andreas Gampe170331f2017-12-07 18:41:03 -080021#include "base/logging.h" // For VLOG
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "base/stl_util.h"
Mathieu Chartier4858a932015-01-23 13:18:53 -080023#include "bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "card_table-inl.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070025#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/heap.h"
Lei Li3befba42015-01-23 16:37:59 +080027#include "gc/space/image_space.h"
Mathieu Chartier97509952015-07-13 14:35:43 -070028#include "gc/space/space.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "mirror/object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080030#include "mirror/object-refvisitor-inl.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070031#include "object_callbacks.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "space_bitmap-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070033#include "thread-current-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034
35namespace art {
36namespace gc {
37namespace accounting {
38
Mathieu Chartier4858a932015-01-23 13:18:53 -080039class ModUnionAddToCardSetVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070040 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -080041 explicit ModUnionAddToCardSetVisitor(ModUnionTable::CardSet* const cleared_cards)
Mathieu Chartierc8950822015-09-15 14:07:10 -070042 : cleared_cards_(cleared_cards) {}
Ian Rogers1d54e732013-05-02 21:10:01 -070043
Mathieu Chartierc8950822015-09-15 14:07:10 -070044 inline void operator()(uint8_t* card,
45 uint8_t expected_value,
Mathieu Chartier4858a932015-01-23 13:18:53 -080046 uint8_t new_value ATTRIBUTE_UNUSED) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070047 if (expected_value == CardTable::kCardDirty) {
48 cleared_cards_->insert(card);
49 }
50 }
51
52 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070053 ModUnionTable::CardSet* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -070054};
55
Mathieu Chartier4858a932015-01-23 13:18:53 -080056class ModUnionAddToCardBitmapVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070057 public:
Roland Levillain3887c462015-08-12 18:15:42 +010058 ModUnionAddToCardBitmapVisitor(ModUnionTable::CardBitmap* bitmap, CardTable* card_table)
Mathieu Chartierc8950822015-09-15 14:07:10 -070059 : bitmap_(bitmap), card_table_(card_table) {}
Ian Rogers1d54e732013-05-02 21:10:01 -070060
Mathieu Chartierc8950822015-09-15 14:07:10 -070061 inline void operator()(uint8_t* card,
62 uint8_t expected_value,
Mathieu Chartier4858a932015-01-23 13:18:53 -080063 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 Chartierc8950822015-09-15 14:07:10 -070097 : visitor_(visitor),
98 from_space_(from_space),
99 immune_space_(immune_space),
100 contains_reference_to_other_space_(contains_reference_to_other_space) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700101
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700102 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartierc8950822015-09-15 14:07:10 -0700103 void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierd82e89e2015-08-05 10:10:07 -0700105 MarkReference(obj->GetFieldObjectReferenceAddr(offset));
Ian Rogers1d54e732013-05-02 21:10:01 -0700106 }
107
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700108 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierd82e89e2015-08-05 10:10:07 -0700110 VisitRoot(root);
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700111 }
112
113 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierd82e89e2015-08-05 10:10:07 -0700115 MarkReference(root);
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700116 }
117
Ian Rogers1d54e732013-05-02 21:10:01 -0700118 private:
Hans Boehmcc55e1d2017-07-27 15:28:07 -0700119 template<typename CompressedReferenceType>
120 void MarkReference(CompressedReferenceType* obj_ptr) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierd82e89e2015-08-05 10:10:07 -0700122 // Only add the reference if it is non null and fits our criteria.
123 mirror::Object* ref = obj_ptr->AsMirrorPtr();
124 if (ref != nullptr && !from_space_->HasAddress(ref) && !immune_space_->HasAddress(ref)) {
125 *contains_reference_to_other_space_ = true;
126 mirror::Object* new_object = visitor_->MarkObject(ref);
127 if (ref != new_object) {
128 obj_ptr->Assign(new_object);
129 }
130 }
131 }
132
Mathieu Chartier97509952015-07-13 14:35:43 -0700133 MarkObjectVisitor* const visitor_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700134 // Space which we are scanning
135 space::ContinuousSpace* const from_space_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800136 space::ContinuousSpace* const immune_space_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700137 // Set if we have any references to another space.
138 bool* const contains_reference_to_other_space_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700139};
140
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700141class ModUnionScanImageRootVisitor {
142 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800143 // Immune space is any other space which we don't care about references to. Currently this is
144 // the image space in the case of the zygote mod union table.
Mathieu Chartier97509952015-07-13 14:35:43 -0700145 ModUnionScanImageRootVisitor(MarkObjectVisitor* visitor,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800146 space::ContinuousSpace* from_space,
147 space::ContinuousSpace* immune_space,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700148 bool* contains_reference_to_other_space)
Mathieu Chartierc8950822015-09-15 14:07:10 -0700149 : visitor_(visitor),
150 from_space_(from_space),
151 immune_space_(immune_space),
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700152 contains_reference_to_other_space_(contains_reference_to_other_space) {}
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700153
Mathieu Chartierc8950822015-09-15 14:07:10 -0700154 void operator()(mirror::Object* root) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700155 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700156 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800157 DCHECK(root != nullptr);
Mathieu Chartierc8950822015-09-15 14:07:10 -0700158 ModUnionUpdateObjectReferencesVisitor ref_visitor(visitor_,
159 from_space_,
160 immune_space_,
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700161 contains_reference_to_other_space_);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700162 root->VisitReferences(ref_visitor, VoidFunctor());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700163 }
164
165 private:
Mathieu Chartier97509952015-07-13 14:35:43 -0700166 MarkObjectVisitor* const visitor_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700167 // Space which we are scanning
168 space::ContinuousSpace* const from_space_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800169 space::ContinuousSpace* const immune_space_;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700170 // Set if we have any references to another space.
171 bool* const contains_reference_to_other_space_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700172};
173
Mathieu Chartier6e6078a2016-10-24 15:45:41 -0700174void ModUnionTableReferenceCache::ProcessCards() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700175 CardTable* card_table = GetHeap()->GetCardTable();
Mathieu Chartier4858a932015-01-23 13:18:53 -0800176 ModUnionAddToCardSetVisitor visitor(&cleared_cards_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700177 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700178 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700179}
180
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700181void ModUnionTableReferenceCache::ClearTable() {
182 cleared_cards_.clear();
183 references_.clear();
184}
185
Ian Rogers1d54e732013-05-02 21:10:01 -0700186class AddToReferenceArrayVisitor {
187 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100188 AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table,
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700189 MarkObjectVisitor* visitor,
Mathieu Chartierc8950822015-09-15 14:07:10 -0700190 std::vector<mirror::HeapReference<mirror::Object>*>* references,
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700191 bool* has_target_reference)
192 : mod_union_table_(mod_union_table),
193 visitor_(visitor),
194 references_(references),
195 has_target_reference_(has_target_reference) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700196
197 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartierc8950822015-09-15 14:07:10 -0700198 void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700199 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc8950822015-09-15 14:07:10 -0700200 mirror::HeapReference<mirror::Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800201 mirror::Object* ref = ref_ptr->AsMirrorPtr();
Ian Rogers1d54e732013-05-02 21:10:01 -0700202 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700203 if (ref != nullptr && mod_union_table_->ShouldAddReference(ref)) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700204 // Push the adddress of the reference.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800205 references_->push_back(ref_ptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700206 }
207 }
208
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700209 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700211 if (!root->IsNull()) {
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700212 VisitRoot(root);
213 }
214 }
215
216 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700218 if (mod_union_table_->ShouldAddReference(root->AsMirrorPtr())) {
219 *has_target_reference_ = true;
220 // TODO: Add MarkCompressedReference callback here.
Mathieu Chartier56f9a892016-04-06 15:27:19 -0700221 mirror::Object* old_ref = root->AsMirrorPtr();
222 mirror::Object* new_ref = visitor_->MarkObject(old_ref);
223 if (old_ref != new_ref) {
224 root->Assign(new_ref);
225 }
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700226 }
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700227 }
228
Ian Rogers1d54e732013-05-02 21:10:01 -0700229 private:
230 ModUnionTableReferenceCache* const mod_union_table_;
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700231 MarkObjectVisitor* const visitor_;
Mathieu Chartierc8950822015-09-15 14:07:10 -0700232 std::vector<mirror::HeapReference<mirror::Object>*>* const references_;
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700233 bool* const has_target_reference_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700234};
235
236class ModUnionReferenceVisitor {
237 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100238 ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table,
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700239 MarkObjectVisitor* visitor,
Mathieu Chartierc8950822015-09-15 14:07:10 -0700240 std::vector<mirror::HeapReference<mirror::Object>*>* references,
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700241 bool* has_target_reference)
Roland Levillain3887c462015-08-12 18:15:42 +0100242 : mod_union_table_(mod_union_table),
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700243 visitor_(visitor),
244 references_(references),
245 has_target_reference_(has_target_reference) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700246
Mathieu Chartierc8950822015-09-15 14:07:10 -0700247 void operator()(mirror::Object* obj) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700249 // We don't have an early exit since we use the visitor pattern, an early
250 // exit should significantly speed this up.
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700251 AddToReferenceArrayVisitor visitor(mod_union_table_,
252 visitor_,
253 references_,
254 has_target_reference_);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700255 obj->VisitReferences(visitor, VoidFunctor());
Ian Rogers1d54e732013-05-02 21:10:01 -0700256 }
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700257
Ian Rogers1d54e732013-05-02 21:10:01 -0700258 private:
259 ModUnionTableReferenceCache* const mod_union_table_;
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700260 MarkObjectVisitor* const visitor_;
Mathieu Chartierc8950822015-09-15 14:07:10 -0700261 std::vector<mirror::HeapReference<mirror::Object>*>* const references_;
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700262 bool* const has_target_reference_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700263};
264
265class CheckReferenceVisitor {
266 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100267 CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table,
Mathieu Chartierc8950822015-09-15 14:07:10 -0700268 const std::set<mirror::Object*>& references)
Roland Levillain3887c462015-08-12 18:15:42 +0100269 : mod_union_table_(mod_union_table),
Mathieu Chartierc8950822015-09-15 14:07:10 -0700270 references_(references) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700271
272 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartierc8950822015-09-15 14:07:10 -0700273 void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700275 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Mathieu Chartierc8950822015-09-15 14:07:10 -0700276 if (ref != nullptr &&
277 mod_union_table_->ShouldAddReference(ref) &&
Ian Rogers1d54e732013-05-02 21:10:01 -0700278 references_.find(ref) == references_.end()) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800279 Heap* heap = mod_union_table_->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -0700280 space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false);
281 space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false);
David Sehr709b0702016-10-13 09:12:37 -0700282 LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << obj->PrettyTypeOf()
283 << ")" << "References "
284 << reinterpret_cast<const void*>(ref) << "(" << mirror::Object::PrettyTypeOf(ref)
Mathieu Chartier407f7022014-02-18 14:37:05 -0800285 << ") without being in mod-union table";
286 LOG(INFO) << "FromSpace " << from_space->GetName() << " type "
287 << from_space->GetGcRetentionPolicy();
288 LOG(INFO) << "ToSpace " << to_space->GetName() << " type "
289 << to_space->GetGcRetentionPolicy();
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700290 heap->DumpSpaces(LOG_STREAM(INFO));
Ian Rogers1d54e732013-05-02 21:10:01 -0700291 LOG(FATAL) << "FATAL ERROR";
292 }
293 }
294
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700295 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700296 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700297 if (kIsDebugBuild && !root->IsNull()) {
298 VisitRoot(root);
299 }
300 }
301
302 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700303 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700304 DCHECK(!mod_union_table_->ShouldAddReference(root->AsMirrorPtr()));
305 }
306
Ian Rogers1d54e732013-05-02 21:10:01 -0700307 private:
308 ModUnionTableReferenceCache* const mod_union_table_;
Mathieu Chartierc8950822015-09-15 14:07:10 -0700309 const std::set<mirror::Object*>& references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700310};
311
312class ModUnionCheckReferences {
313 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100314 ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table,
Mathieu Chartierc8950822015-09-15 14:07:10 -0700315 const std::set<mirror::Object*>& references)
Mathieu Chartier90443472015-07-16 20:32:27 -0700316 REQUIRES(Locks::heap_bitmap_lock_)
Mathieu Chartierc8950822015-09-15 14:07:10 -0700317 : mod_union_table_(mod_union_table), references_(references) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700318
Mathieu Chartierc8950822015-09-15 14:07:10 -0700319 void operator()(mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers1d54e732013-05-02 21:10:01 -0700320 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
Ian Rogers1d54e732013-05-02 21:10:01 -0700321 CheckReferenceVisitor visitor(mod_union_table_, references_);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700322 obj->VisitReferences(visitor, VoidFunctor());
Ian Rogers1d54e732013-05-02 21:10:01 -0700323 }
324
325 private:
326 ModUnionTableReferenceCache* const mod_union_table_;
Mathieu Chartierc8950822015-09-15 14:07:10 -0700327 const std::set<mirror::Object*>& references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700328};
329
Mathieu Chartier21328a12016-07-22 10:47:45 -0700330class EmptyMarkObjectVisitor : public MarkObjectVisitor {
331 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100332 mirror::Object* MarkObject(mirror::Object* obj) override {return obj;}
333 void MarkHeapReference(mirror::HeapReference<mirror::Object>*, bool) override {}
Mathieu Chartier21328a12016-07-22 10:47:45 -0700334};
335
336void ModUnionTable::FilterCards() {
337 EmptyMarkObjectVisitor visitor;
338 // Use empty visitor since filtering is automatically done by UpdateAndMarkReferences.
339 UpdateAndMarkReferences(&visitor);
340}
341
Ian Rogers1d54e732013-05-02 21:10:01 -0700342void ModUnionTableReferenceCache::Verify() {
343 // Start by checking that everything in the mod union table is marked.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700344 for (const auto& ref_pair : references_) {
Mathieu Chartierc8950822015-09-15 14:07:10 -0700345 for (mirror::HeapReference<mirror::Object>* ref : ref_pair.second) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800346 CHECK(heap_->IsLiveObjectLocked(ref->AsMirrorPtr()));
Ian Rogers1d54e732013-05-02 21:10:01 -0700347 }
348 }
349
350 // Check the references of each clean card which is also in the mod union table.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700351 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700352 ContinuousSpaceBitmap* live_bitmap = space_->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700353 for (const auto& ref_pair : references_) {
Ian Rogers13735952014-10-08 12:43:28 -0700354 const uint8_t* card = ref_pair.first;
Ian Rogers1d54e732013-05-02 21:10:01 -0700355 if (*card == CardTable::kCardClean) {
Mathieu Chartierc8950822015-09-15 14:07:10 -0700356 std::set<mirror::Object*> reference_set;
357 for (mirror::HeapReference<mirror::Object>* obj_ptr : ref_pair.second) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800358 reference_set.insert(obj_ptr->AsMirrorPtr());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700359 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700360 ModUnionCheckReferences visitor(this, reference_set);
361 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700362 live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700363 }
364 }
365}
366
367void ModUnionTableReferenceCache::Dump(std::ostream& os) {
368 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700369 os << "ModUnionTable cleared cards: [";
Ian Rogers13735952014-10-08 12:43:28 -0700370 for (uint8_t* card_addr : cleared_cards_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700371 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700372 uintptr_t end = start + CardTable::kCardSize;
373 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
374 }
375 os << "]\nModUnionTable references: [";
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700376 for (const auto& ref_pair : references_) {
Ian Rogers13735952014-10-08 12:43:28 -0700377 const uint8_t* card_addr = ref_pair.first;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700378 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700379 uintptr_t end = start + CardTable::kCardSize;
380 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{";
Mathieu Chartierc8950822015-09-15 14:07:10 -0700381 for (mirror::HeapReference<mirror::Object>* ref : ref_pair.second) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800382 os << reinterpret_cast<const void*>(ref->AsMirrorPtr()) << ",";
Ian Rogers1d54e732013-05-02 21:10:01 -0700383 }
384 os << "},";
385 }
386}
387
Andreas Gampe0c183382017-07-13 22:26:24 -0700388void ModUnionTableReferenceCache::VisitObjects(ObjectCallback callback, void* arg) {
Mathieu Chartier21328a12016-07-22 10:47:45 -0700389 CardTable* const card_table = heap_->GetCardTable();
390 ContinuousSpaceBitmap* live_bitmap = space_->GetLiveBitmap();
391 for (uint8_t* card : cleared_cards_) {
392 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
393 uintptr_t end = start + CardTable::kCardSize;
394 live_bitmap->VisitMarkedRange(start,
395 end,
Yi Kong88307ed2017-04-25 22:33:06 -0700396 [callback, arg](mirror::Object* obj) {
Mathieu Chartier21328a12016-07-22 10:47:45 -0700397 callback(obj, arg);
398 });
399 }
400 // This may visit the same card twice, TODO avoid this.
401 for (const auto& pair : references_) {
402 const uint8_t* card = pair.first;
403 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
404 uintptr_t end = start + CardTable::kCardSize;
405 live_bitmap->VisitMarkedRange(start,
406 end,
Yi Kong88307ed2017-04-25 22:33:06 -0700407 [callback, arg](mirror::Object* obj) {
Mathieu Chartier21328a12016-07-22 10:47:45 -0700408 callback(obj, arg);
409 });
410 }
411}
412
Mathieu Chartier97509952015-07-13 14:35:43 -0700413void ModUnionTableReferenceCache::UpdateAndMarkReferences(MarkObjectVisitor* visitor) {
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700414 CardTable* const card_table = heap_->GetCardTable();
Mathieu Chartierc8950822015-09-15 14:07:10 -0700415 std::vector<mirror::HeapReference<mirror::Object>*> cards_references;
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700416 // If has_target_reference is true then there was a GcRoot compressed reference which wasn't
417 // added. In this case we need to keep the card dirty.
418 // We don't know if the GcRoot addresses will remain constant, for example, classloaders have a
419 // hash set of GcRoot which may be resized or modified.
420 bool has_target_reference;
421 ModUnionReferenceVisitor add_visitor(this, visitor, &cards_references, &has_target_reference);
422 CardSet new_cleared_cards;
423 for (uint8_t* card : cleared_cards_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700424 // Clear and re-compute alloc space references associated with this card.
425 cards_references.clear();
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700426 has_target_reference = false;
Ian Rogers1d54e732013-05-02 21:10:01 -0700427 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
428 uintptr_t end = start + CardTable::kCardSize;
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700429 space::ContinuousSpace* space =
Mathieu Chartierc8950822015-09-15 14:07:10 -0700430 heap_->FindContinuousSpaceFromObject(reinterpret_cast<mirror::Object*>(start), false);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700431 DCHECK(space != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700432 ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700433 live_bitmap->VisitMarkedRange(start, end, add_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700434 // Update the corresponding references for the card.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700435 auto found = references_.find(card);
Ian Rogers1d54e732013-05-02 21:10:01 -0700436 if (found == references_.end()) {
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700437 // Don't add card for an empty reference array.
438 if (!cards_references.empty()) {
439 references_.Put(card, cards_references);
Ian Rogers1d54e732013-05-02 21:10:01 -0700440 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700441 } else {
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700442 if (cards_references.empty()) {
443 references_.erase(found);
444 } else {
445 found->second = cards_references;
446 }
447 }
448 if (has_target_reference) {
449 // Keep this card for next time since it contains a GcRoot which matches the
450 // ShouldAddReference criteria. This usually occurs for class loaders.
451 new_cleared_cards.insert(card);
Ian Rogers1d54e732013-05-02 21:10:01 -0700452 }
453 }
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700454 cleared_cards_ = std::move(new_cleared_cards);
Ian Rogers1d54e732013-05-02 21:10:01 -0700455 size_t count = 0;
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700456 for (auto it = references_.begin(); it != references_.end();) {
Mathieu Chartierc8950822015-09-15 14:07:10 -0700457 std::vector<mirror::HeapReference<mirror::Object>*>& references = it->second;
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700458 // Since there is no card mark for setting a reference to null, we check each reference.
459 // If all of the references of a card are null then we can remove that card. This is racy
460 // with the mutators, but handled by rescanning dirty cards.
461 bool all_null = true;
Mathieu Chartierc8950822015-09-15 14:07:10 -0700462 for (mirror::HeapReference<mirror::Object>* obj_ptr : references) {
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700463 if (obj_ptr->AsMirrorPtr() != nullptr) {
464 all_null = false;
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700465 visitor->MarkHeapReference(obj_ptr, /*do_atomic_update=*/ false);
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700466 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700467 }
Mathieu Chartiera52f2f92015-08-16 14:37:04 -0700468 count += references.size();
469 if (!all_null) {
470 ++it;
471 } else {
472 // All null references, erase the array from the set.
473 it = references_.erase(it);
474 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700475 }
476 if (VLOG_IS_ON(heap)) {
477 VLOG(gc) << "Marked " << count << " references in mod union table";
478 }
479}
480
Mathieu Chartierc8950822015-09-15 14:07:10 -0700481ModUnionTableCardCache::ModUnionTableCardCache(const std::string& name,
482 Heap* heap,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800483 space::ContinuousSpace* space)
484 : ModUnionTable(name, heap, space) {
485 // Normally here we could use End() instead of Limit(), but for testing we may want to have a
486 // mod-union table for a space which can still grow.
487 if (!space->IsImageSpace()) {
488 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize);
489 }
490 card_bitmap_.reset(CardBitmap::Create(
491 "mod union bitmap", reinterpret_cast<uintptr_t>(space->Begin()),
492 RoundUp(reinterpret_cast<uintptr_t>(space->Limit()), CardTable::kCardSize)));
493}
494
495class CardBitVisitor {
496 public:
Mathieu Chartierc8950822015-09-15 14:07:10 -0700497 CardBitVisitor(MarkObjectVisitor* visitor,
498 space::ContinuousSpace* space,
499 space::ContinuousSpace* immune_space,
500 ModUnionTable::CardBitmap* card_bitmap)
501 : visitor_(visitor),
502 space_(space),
503 immune_space_(immune_space),
504 bitmap_(space->GetLiveBitmap()),
505 card_bitmap_(card_bitmap) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800506 DCHECK(immune_space_ != nullptr);
507 }
508
509 void operator()(size_t bit_index) const {
510 const uintptr_t start = card_bitmap_->AddrFromBitIndex(bit_index);
511 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start)))
512 << start << " " << *space_;
513 bool reference_to_other_space = false;
Mathieu Chartier97509952015-07-13 14:35:43 -0700514 ModUnionScanImageRootVisitor scan_visitor(visitor_, space_, immune_space_,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800515 &reference_to_other_space);
516 bitmap_->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor);
517 if (!reference_to_other_space) {
518 // No non null reference to another space, clear the bit.
519 card_bitmap_->ClearBit(bit_index);
520 }
521 }
522
523 private:
Mathieu Chartier97509952015-07-13 14:35:43 -0700524 MarkObjectVisitor* const visitor_;
Mathieu Chartier4858a932015-01-23 13:18:53 -0800525 space::ContinuousSpace* const space_;
526 space::ContinuousSpace* const immune_space_;
527 ContinuousSpaceBitmap* const bitmap_;
528 ModUnionTable::CardBitmap* const card_bitmap_;
529};
530
Mathieu Chartier6e6078a2016-10-24 15:45:41 -0700531void ModUnionTableCardCache::ProcessCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800532 CardTable* const card_table = GetHeap()->GetCardTable();
533 ModUnionAddToCardBitmapVisitor visitor(card_bitmap_.get(), card_table);
Ian Rogers1d54e732013-05-02 21:10:01 -0700534 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700535 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700536}
537
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700538void ModUnionTableCardCache::ClearTable() {
539 card_bitmap_->Bitmap::Clear();
540}
541
Ian Rogers1d54e732013-05-02 21:10:01 -0700542// Mark all references to the alloc space(s).
Mathieu Chartier97509952015-07-13 14:35:43 -0700543void ModUnionTableCardCache::UpdateAndMarkReferences(MarkObjectVisitor* visitor) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800544 // TODO: Needs better support for multi-images? b/26317072
545 space::ImageSpace* image_space =
546 heap_->GetBootImageSpaces().empty() ? nullptr : heap_->GetBootImageSpaces()[0];
Mathieu Chartier4858a932015-01-23 13:18:53 -0800547 // If we don't have an image space, just pass in space_ as the immune space. Pass in the same
548 // space_ instead of image_space to avoid a null check in ModUnionUpdateObjectReferencesVisitor.
Mathieu Chartier97509952015-07-13 14:35:43 -0700549 CardBitVisitor bit_visitor(visitor, space_, image_space != nullptr ? image_space : space_,
Mathieu Chartier4858a932015-01-23 13:18:53 -0800550 card_bitmap_.get());
551 card_bitmap_->VisitSetBits(
Mathieu Chartier97509952015-07-13 14:35:43 -0700552 0, RoundUp(space_->Size(), CardTable::kCardSize) / CardTable::kCardSize, bit_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700553}
554
Andreas Gampe0c183382017-07-13 22:26:24 -0700555void ModUnionTableCardCache::VisitObjects(ObjectCallback callback, void* arg) {
Mathieu Chartier21328a12016-07-22 10:47:45 -0700556 card_bitmap_->VisitSetBits(
557 0,
558 RoundUp(space_->Size(), CardTable::kCardSize) / CardTable::kCardSize,
559 [this, callback, arg](size_t bit_index) {
560 const uintptr_t start = card_bitmap_->AddrFromBitIndex(bit_index);
561 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start)))
562 << start << " " << *space_;
563 space_->GetLiveBitmap()->VisitMarkedRange(start,
564 start + CardTable::kCardSize,
Yi Kong88307ed2017-04-25 22:33:06 -0700565 [callback, arg](mirror::Object* obj) {
Mathieu Chartier21328a12016-07-22 10:47:45 -0700566 callback(obj, arg);
567 });
568 });
569}
570
Ian Rogers1d54e732013-05-02 21:10:01 -0700571void ModUnionTableCardCache::Dump(std::ostream& os) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700572 os << "ModUnionTable dirty cards: [";
Mathieu Chartier4858a932015-01-23 13:18:53 -0800573 // TODO: Find cleaner way of doing this.
574 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
575 addr += CardTable::kCardSize) {
576 if (card_bitmap_->Test(reinterpret_cast<uintptr_t>(addr))) {
577 os << reinterpret_cast<void*>(addr) << "-"
578 << reinterpret_cast<void*>(addr + CardTable::kCardSize) << "\n";
579 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700580 }
581 os << "]";
582}
583
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700584void ModUnionTableCardCache::SetCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800585 // 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 -0700586 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700587 addr += CardTable::kCardSize) {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800588 card_bitmap_->Set(reinterpret_cast<uintptr_t>(addr));
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700589 }
590}
591
Mathieu Chartier4858a932015-01-23 13:18:53 -0800592bool ModUnionTableCardCache::ContainsCardFor(uintptr_t addr) {
593 return card_bitmap_->Test(addr);
594}
595
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700596void ModUnionTableReferenceCache::SetCards() {
Mathieu Chartier4858a932015-01-23 13:18:53 -0800597 for (uint8_t* addr = space_->Begin(); addr < AlignUp(space_->End(), CardTable::kCardSize);
598 addr += CardTable::kCardSize) {
599 cleared_cards_.insert(heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr)));
600 }
601}
602
603bool ModUnionTableReferenceCache::ContainsCardFor(uintptr_t addr) {
604 auto* card_ptr = heap_->GetCardTable()->CardFromAddr(reinterpret_cast<void*>(addr));
605 return cleared_cards_.find(card_ptr) != cleared_cards_.end() ||
606 references_.find(card_ptr) != references_.end();
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700607}
608
Ian Rogers1d54e732013-05-02 21:10:01 -0700609} // namespace accounting
610} // namespace gc
611} // namespace art