blob: bbbd1ed0555cac4f50b3e01178074134031fe5b6 [file] [log] [blame]
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -08001/*
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:
Mathieu Chartier407f7022014-02-18 14:37:05 -080063 RememberedSetReferenceVisitor(MarkHeapReferenceCallback* callback,
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070064 DelayReferenceReferentCallback* ref_callback,
Mathieu Chartier407f7022014-02-18 14:37:05 -080065 space::ContinuousSpace* target_space,
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080066 bool* const contains_reference_to_target_space, void* arg)
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070067 : callback_(callback), ref_callback_(ref_callback), target_space_(target_space), arg_(arg),
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080068 contains_reference_to_target_space_(contains_reference_to_target_space) {}
69
Mathieu Chartier407f7022014-02-18 14:37:05 -080070 void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) const
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070072 DCHECK(obj != nullptr);
Mathieu Chartier407f7022014-02-18 14:37:05 -080073 mirror::HeapReference<mirror::Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset);
74 if (target_space_->HasAddress(ref_ptr->AsMirrorPtr())) {
75 *contains_reference_to_target_space_ = true;
76 callback_(ref_ptr, arg_);
77 DCHECK(!target_space_->HasAddress(ref_ptr->AsMirrorPtr()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080078 }
79 }
80
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070081 void operator()(mirror::Class* klass, mirror::Reference* ref) const
82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
83 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
84 if (target_space_->HasAddress(ref->GetReferent())) {
85 *contains_reference_to_target_space_ = true;
86 ref_callback_(klass, ref, arg_);
87 }
88 }
89
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080090 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -080091 MarkHeapReferenceCallback* const callback_;
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070092 DelayReferenceReferentCallback* const ref_callback_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080093 space::ContinuousSpace* const target_space_;
94 void* const arg_;
95 bool* const contains_reference_to_target_space_;
96};
97
98class RememberedSetObjectVisitor {
99 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800100 RememberedSetObjectVisitor(MarkHeapReferenceCallback* callback,
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700101 DelayReferenceReferentCallback* ref_callback,
Mathieu Chartier407f7022014-02-18 14:37:05 -0800102 space::ContinuousSpace* target_space,
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800103 bool* const contains_reference_to_target_space, void* arg)
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700104 : callback_(callback), ref_callback_(ref_callback), target_space_(target_space), arg_(arg),
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800105 contains_reference_to_target_space_(contains_reference_to_target_space) {}
106
107 void operator()(mirror::Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700109 RememberedSetReferenceVisitor visitor(callback_, ref_callback_, target_space_,
110 contains_reference_to_target_space_, arg_);
111 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800112 }
113
114 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800115 MarkHeapReferenceCallback* const callback_;
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700116 DelayReferenceReferentCallback* const ref_callback_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800117 space::ContinuousSpace* const target_space_;
118 void* const arg_;
119 bool* const contains_reference_to_target_space_;
120};
121
Mathieu Chartier407f7022014-02-18 14:37:05 -0800122void RememberedSet::UpdateAndMarkReferences(MarkHeapReferenceCallback* callback,
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700123 DelayReferenceReferentCallback* ref_callback,
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800124 space::ContinuousSpace* target_space, void* arg) {
125 CardTable* card_table = heap_->GetCardTable();
126 bool contains_reference_to_target_space = false;
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700127 RememberedSetObjectVisitor obj_visitor(callback, ref_callback, target_space,
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800128 &contains_reference_to_target_space, arg);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700129 ContinuousSpaceBitmap* bitmap = space_->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800130 CardSet remove_card_set;
131 for (byte* const card_addr : dirty_cards_) {
132 contains_reference_to_target_space = false;
133 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
134 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start)));
135 bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, obj_visitor);
136 if (!contains_reference_to_target_space) {
137 // It was in the dirty card set, but it didn't actually contain
138 // a reference to the target space. So, remove it from the dirty
139 // card set so we won't have to scan it again (unless it gets
140 // dirty again.)
141 remove_card_set.insert(card_addr);
142 }
143 }
144
145 // Remove the cards that didn't contain a reference to the target
146 // space from the dirty card set.
147 for (byte* const card_addr : remove_card_set) {
148 DCHECK(dirty_cards_.find(card_addr) != dirty_cards_.end());
149 dirty_cards_.erase(card_addr);
150 }
151}
152
153void RememberedSet::Dump(std::ostream& os) {
154 CardTable* card_table = heap_->GetCardTable();
155 os << "RememberedSet dirty cards: [";
156 for (const byte* card_addr : dirty_cards_) {
157 auto start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
158 auto end = start + CardTable::kCardSize;
159 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "\n";
160 }
161 os << "]";
162}
163
164void RememberedSet::AssertAllDirtyCardsAreWithinSpace() const {
165 CardTable* card_table = heap_->GetCardTable();
166 for (const byte* card_addr : dirty_cards_) {
167 auto start = reinterpret_cast<byte*>(card_table->AddrFromCard(card_addr));
168 auto end = start + CardTable::kCardSize;
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700169 DCHECK_LE(space_->Begin(), start);
170 DCHECK_LE(end, space_->Limit());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800171 }
172}
173
174} // namespace accounting
175} // namespace gc
176} // namespace art