blob: b16a146ea9adaeab51e618d8fada2bd1e1624018 [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
Ian Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
20
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080021#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/art_field-inl.h"
30#include "mirror/object-inl.h"
31#include "mirror/class-inl.h"
32#include "mirror/object_array-inl.h"
33#include "space_bitmap-inl.h"
34#include "thread.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080035
36namespace art {
37namespace gc {
38namespace accounting {
39
40class RememberedSetCardVisitor {
41 public:
42 explicit RememberedSetCardVisitor(RememberedSet::CardSet* const dirty_cards)
43 : dirty_cards_(dirty_cards) {}
44
Ian Rogers13735952014-10-08 12:43:28 -070045 void operator()(uint8_t* card, uint8_t expected_value, uint8_t new_value) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070046 UNUSED(new_value);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080047 if (expected_value == CardTable::kCardDirty) {
48 dirty_cards_->insert(card);
49 }
50 }
51
52 private:
53 RememberedSet::CardSet* const dirty_cards_;
54};
55
56void RememberedSet::ClearCards() {
57 CardTable* card_table = GetHeap()->GetCardTable();
58 RememberedSetCardVisitor card_visitor(&dirty_cards_);
59 // Clear dirty cards in the space and insert them into the dirty card set.
60 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), card_visitor);
61}
62
63class RememberedSetReferenceVisitor {
64 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -080065 RememberedSetReferenceVisitor(MarkHeapReferenceCallback* callback,
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070066 DelayReferenceReferentCallback* ref_callback,
Mathieu Chartier407f7022014-02-18 14:37:05 -080067 space::ContinuousSpace* target_space,
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080068 bool* const contains_reference_to_target_space, void* arg)
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070069 : callback_(callback), ref_callback_(ref_callback), target_space_(target_space), arg_(arg),
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080070 contains_reference_to_target_space_(contains_reference_to_target_space) {}
71
Mathieu Chartier407f7022014-02-18 14:37:05 -080072 void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) const
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070074 DCHECK(obj != nullptr);
Mathieu Chartier407f7022014-02-18 14:37:05 -080075 mirror::HeapReference<mirror::Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset);
76 if (target_space_->HasAddress(ref_ptr->AsMirrorPtr())) {
77 *contains_reference_to_target_space_ = true;
78 callback_(ref_ptr, arg_);
79 DCHECK(!target_space_->HasAddress(ref_ptr->AsMirrorPtr()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080080 }
81 }
82
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070083 void operator()(mirror::Class* klass, mirror::Reference* ref) const
84 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
85 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
86 if (target_space_->HasAddress(ref->GetReferent())) {
87 *contains_reference_to_target_space_ = true;
88 ref_callback_(klass, ref, arg_);
89 }
90 }
91
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080092 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -080093 MarkHeapReferenceCallback* const callback_;
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070094 DelayReferenceReferentCallback* const ref_callback_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080095 space::ContinuousSpace* const target_space_;
96 void* const arg_;
97 bool* const contains_reference_to_target_space_;
98};
99
100class RememberedSetObjectVisitor {
101 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800102 RememberedSetObjectVisitor(MarkHeapReferenceCallback* callback,
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700103 DelayReferenceReferentCallback* ref_callback,
Mathieu Chartier407f7022014-02-18 14:37:05 -0800104 space::ContinuousSpace* target_space,
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800105 bool* const contains_reference_to_target_space, void* arg)
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700106 : callback_(callback), ref_callback_(ref_callback), target_space_(target_space), arg_(arg),
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800107 contains_reference_to_target_space_(contains_reference_to_target_space) {}
108
109 void operator()(mirror::Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700111 RememberedSetReferenceVisitor visitor(callback_, ref_callback_, target_space_,
112 contains_reference_to_target_space_, arg_);
113 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800114 }
115
116 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800117 MarkHeapReferenceCallback* const callback_;
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700118 DelayReferenceReferentCallback* const ref_callback_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800119 space::ContinuousSpace* const target_space_;
120 void* const arg_;
121 bool* const contains_reference_to_target_space_;
122};
123
Mathieu Chartier407f7022014-02-18 14:37:05 -0800124void RememberedSet::UpdateAndMarkReferences(MarkHeapReferenceCallback* callback,
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700125 DelayReferenceReferentCallback* ref_callback,
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800126 space::ContinuousSpace* target_space, void* arg) {
127 CardTable* card_table = heap_->GetCardTable();
128 bool contains_reference_to_target_space = false;
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700129 RememberedSetObjectVisitor obj_visitor(callback, ref_callback, target_space,
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800130 &contains_reference_to_target_space, arg);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700131 ContinuousSpaceBitmap* bitmap = space_->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800132 CardSet remove_card_set;
Ian Rogers13735952014-10-08 12:43:28 -0700133 for (uint8_t* const card_addr : dirty_cards_) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800134 contains_reference_to_target_space = false;
135 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
136 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start)));
137 bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, obj_visitor);
138 if (!contains_reference_to_target_space) {
139 // It was in the dirty card set, but it didn't actually contain
140 // a reference to the target space. So, remove it from the dirty
141 // card set so we won't have to scan it again (unless it gets
142 // dirty again.)
143 remove_card_set.insert(card_addr);
144 }
145 }
146
147 // Remove the cards that didn't contain a reference to the target
148 // space from the dirty card set.
Ian Rogers13735952014-10-08 12:43:28 -0700149 for (uint8_t* const card_addr : remove_card_set) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800150 DCHECK(dirty_cards_.find(card_addr) != dirty_cards_.end());
151 dirty_cards_.erase(card_addr);
152 }
153}
154
155void RememberedSet::Dump(std::ostream& os) {
156 CardTable* card_table = heap_->GetCardTable();
157 os << "RememberedSet dirty cards: [";
Ian Rogers13735952014-10-08 12:43:28 -0700158 for (const uint8_t* card_addr : dirty_cards_) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800159 auto start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
160 auto end = start + CardTable::kCardSize;
161 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "\n";
162 }
163 os << "]";
164}
165
166void RememberedSet::AssertAllDirtyCardsAreWithinSpace() const {
167 CardTable* card_table = heap_->GetCardTable();
Ian Rogers13735952014-10-08 12:43:28 -0700168 for (const uint8_t* card_addr : dirty_cards_) {
169 auto start = reinterpret_cast<uint8_t*>(card_table->AddrFromCard(card_addr));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800170 auto end = start + CardTable::kCardSize;
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700171 DCHECK_LE(space_->Begin(), start);
172 DCHECK_LE(end, space_->Limit());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800173 }
174}
175
176} // namespace accounting
177} // namespace gc
178} // namespace art