1/* 2 * Copyright (C) 2014 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 "remembered_set.h" 18 19#include <memory> 20 21#include "base/stl_util.h" 22#include "card_table-inl.h" 23#include "heap_bitmap.h" 24#include "gc/collector/mark_sweep.h" 25#include "gc/collector/mark_sweep-inl.h" 26#include "gc/collector/semi_space.h" 27#include "gc/heap.h" 28#include "gc/space/space.h" 29#include "mirror/object-inl.h" 30#include "mirror/class-inl.h" 31#include "mirror/object_array-inl.h" 32#include "space_bitmap-inl.h" 33#include "thread.h" 34 35namespace art { 36namespace gc { 37namespace accounting { 38 39class RememberedSetCardVisitor { 40 public: 41 explicit RememberedSetCardVisitor(RememberedSet::CardSet* const dirty_cards) 42 : dirty_cards_(dirty_cards) {} 43 44 void operator()(uint8_t* card, uint8_t expected_value, uint8_t new_value) const { 45 UNUSED(new_value); 46 if (expected_value == CardTable::kCardDirty) { 47 dirty_cards_->insert(card); 48 } 49 } 50 51 private: 52 RememberedSet::CardSet* const dirty_cards_; 53}; 54 55void RememberedSet::ClearCards() { 56 CardTable* card_table = GetHeap()->GetCardTable(); 57 RememberedSetCardVisitor card_visitor(&dirty_cards_); 58 // Clear dirty cards in the space and insert them into the dirty card set. 59 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), card_visitor); 60} 61 62class RememberedSetReferenceVisitor { 63 public: 64 RememberedSetReferenceVisitor(MarkHeapReferenceCallback* callback, 65 DelayReferenceReferentCallback* ref_callback, 66 space::ContinuousSpace* target_space, 67 bool* const contains_reference_to_target_space, void* arg) 68 : callback_(callback), ref_callback_(ref_callback), target_space_(target_space), arg_(arg), 69 contains_reference_to_target_space_(contains_reference_to_target_space) {} 70 71 void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) const 72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 73 DCHECK(obj != nullptr); 74 mirror::HeapReference<mirror::Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset); 75 if (target_space_->HasAddress(ref_ptr->AsMirrorPtr())) { 76 *contains_reference_to_target_space_ = true; 77 callback_(ref_ptr, arg_); 78 DCHECK(!target_space_->HasAddress(ref_ptr->AsMirrorPtr())); 79 } 80 } 81 82 void operator()(mirror::Class* klass, mirror::Reference* ref) const 83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 84 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { 85 if (target_space_->HasAddress(ref->GetReferent())) { 86 *contains_reference_to_target_space_ = true; 87 ref_callback_(klass, ref, arg_); 88 } 89 } 90 91 private: 92 MarkHeapReferenceCallback* const callback_; 93 DelayReferenceReferentCallback* const ref_callback_; 94 space::ContinuousSpace* const target_space_; 95 void* const arg_; 96 bool* const contains_reference_to_target_space_; 97}; 98 99class RememberedSetObjectVisitor { 100 public: 101 RememberedSetObjectVisitor(MarkHeapReferenceCallback* callback, 102 DelayReferenceReferentCallback* ref_callback, 103 space::ContinuousSpace* target_space, 104 bool* const contains_reference_to_target_space, void* arg) 105 : callback_(callback), ref_callback_(ref_callback), target_space_(target_space), arg_(arg), 106 contains_reference_to_target_space_(contains_reference_to_target_space) {} 107 108 void operator()(mirror::Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) 109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 110 RememberedSetReferenceVisitor visitor(callback_, ref_callback_, target_space_, 111 contains_reference_to_target_space_, arg_); 112 obj->VisitReferences<kMovingClasses>(visitor, visitor); 113 } 114 115 private: 116 MarkHeapReferenceCallback* const callback_; 117 DelayReferenceReferentCallback* const ref_callback_; 118 space::ContinuousSpace* const target_space_; 119 void* const arg_; 120 bool* const contains_reference_to_target_space_; 121}; 122 123void RememberedSet::UpdateAndMarkReferences(MarkHeapReferenceCallback* callback, 124 DelayReferenceReferentCallback* ref_callback, 125 space::ContinuousSpace* target_space, void* arg) { 126 CardTable* card_table = heap_->GetCardTable(); 127 bool contains_reference_to_target_space = false; 128 RememberedSetObjectVisitor obj_visitor(callback, ref_callback, target_space, 129 &contains_reference_to_target_space, arg); 130 ContinuousSpaceBitmap* bitmap = space_->GetLiveBitmap(); 131 CardSet remove_card_set; 132 for (uint8_t* const card_addr : dirty_cards_) { 133 contains_reference_to_target_space = false; 134 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); 135 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start))); 136 bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, obj_visitor); 137 if (!contains_reference_to_target_space) { 138 // It was in the dirty card set, but it didn't actually contain 139 // a reference to the target space. So, remove it from the dirty 140 // card set so we won't have to scan it again (unless it gets 141 // dirty again.) 142 remove_card_set.insert(card_addr); 143 } 144 } 145 146 // Remove the cards that didn't contain a reference to the target 147 // space from the dirty card set. 148 for (uint8_t* const card_addr : remove_card_set) { 149 DCHECK(dirty_cards_.find(card_addr) != dirty_cards_.end()); 150 dirty_cards_.erase(card_addr); 151 } 152} 153 154void RememberedSet::Dump(std::ostream& os) { 155 CardTable* card_table = heap_->GetCardTable(); 156 os << "RememberedSet dirty cards: ["; 157 for (const uint8_t* card_addr : dirty_cards_) { 158 auto start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr)); 159 auto end = start + CardTable::kCardSize; 160 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "\n"; 161 } 162 os << "]"; 163} 164 165void RememberedSet::AssertAllDirtyCardsAreWithinSpace() const { 166 CardTable* card_table = heap_->GetCardTable(); 167 for (const uint8_t* card_addr : dirty_cards_) { 168 auto start = reinterpret_cast<uint8_t*>(card_table->AddrFromCard(card_addr)); 169 auto end = start + CardTable::kCardSize; 170 DCHECK_LE(space_->Begin(), start); 171 DCHECK_LE(end, space_->Limit()); 172 } 173} 174 175} // namespace accounting 176} // namespace gc 177} // namespace art 178