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