blob: eb0852af6e72fc8fd3c654d59decb41a1ae11c17 [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"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080029#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"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080034
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
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010044 void operator()(uint8_t* card, uint8_t expected_value, uint8_t new_value ATTRIBUTE_UNUSED) const {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080045 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 Chartier97509952015-07-13 14:35:43 -070063 RememberedSetReferenceVisitor(space::ContinuousSpace* target_space,
64 bool* const contains_reference_to_target_space,
65 collector::GarbageCollector* collector)
66 : collector_(collector), target_space_(target_space),
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080067 contains_reference_to_target_space_(contains_reference_to_target_space) {}
68
Mathieu Chartierda7c6502015-07-23 16:01:26 -070069 void operator()(mirror::Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -070070 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070071 DCHECK(obj != nullptr);
Mathieu Chartier407f7022014-02-18 14:37:05 -080072 mirror::HeapReference<mirror::Object>* ref_ptr = obj->GetFieldObjectReferenceAddr(offset);
73 if (target_space_->HasAddress(ref_ptr->AsMirrorPtr())) {
74 *contains_reference_to_target_space_ = true;
Mathieu Chartier97509952015-07-13 14:35:43 -070075 collector_->MarkHeapReference(ref_ptr);
Mathieu Chartier407f7022014-02-18 14:37:05 -080076 DCHECK(!target_space_->HasAddress(ref_ptr->AsMirrorPtr()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080077 }
78 }
79
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070080 void operator()(mirror::Class* klass, mirror::Reference* ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -070081 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070082 if (target_space_->HasAddress(ref->GetReferent())) {
83 *contains_reference_to_target_space_ = true;
Mathieu Chartier97509952015-07-13 14:35:43 -070084 collector_->DelayReferenceReferent(klass, ref);
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -070085 }
86 }
87
Mathieu Chartierda7c6502015-07-23 16:01:26 -070088 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
89 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb837eb2015-07-29 17:25:41 -070090 if (!root->IsNull()) {
Mathieu Chartierda7c6502015-07-23 16:01:26 -070091 VisitRoot(root);
92 }
93 }
94
95 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
96 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier39089122015-07-27 16:08:02 -070097 if (target_space_->HasAddress(root->AsMirrorPtr())) {
98 *contains_reference_to_target_space_ = true;
99 root->Assign(collector_->MarkObject(root->AsMirrorPtr()));
100 DCHECK(!target_space_->HasAddress(root->AsMirrorPtr()));
101 }
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700102 }
103
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800104 private:
Mathieu Chartier97509952015-07-13 14:35:43 -0700105 collector::GarbageCollector* const collector_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800106 space::ContinuousSpace* const target_space_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800107 bool* const contains_reference_to_target_space_;
108};
109
110class RememberedSetObjectVisitor {
111 public:
Mathieu Chartier97509952015-07-13 14:35:43 -0700112 RememberedSetObjectVisitor(space::ContinuousSpace* target_space,
113 bool* const contains_reference_to_target_space,
114 collector::GarbageCollector* collector)
115 : collector_(collector), target_space_(target_space),
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800116 contains_reference_to_target_space_(contains_reference_to_target_space) {}
117
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 void operator()(mirror::Object* obj) const REQUIRES(Locks::heap_bitmap_lock_)
119 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700120 RememberedSetReferenceVisitor visitor(target_space_, contains_reference_to_target_space_,
121 collector_);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700122 obj->VisitReferences(visitor, visitor);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800123 }
124
125 private:
Mathieu Chartier97509952015-07-13 14:35:43 -0700126 collector::GarbageCollector* const collector_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800127 space::ContinuousSpace* const target_space_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800128 bool* const contains_reference_to_target_space_;
129};
130
Mathieu Chartier97509952015-07-13 14:35:43 -0700131void RememberedSet::UpdateAndMarkReferences(space::ContinuousSpace* target_space,
132 collector::GarbageCollector* collector) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800133 CardTable* card_table = heap_->GetCardTable();
134 bool contains_reference_to_target_space = false;
Mathieu Chartier97509952015-07-13 14:35:43 -0700135 RememberedSetObjectVisitor obj_visitor(target_space, &contains_reference_to_target_space,
136 collector);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700137 ContinuousSpaceBitmap* bitmap = space_->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800138 CardSet remove_card_set;
Ian Rogers13735952014-10-08 12:43:28 -0700139 for (uint8_t* const card_addr : dirty_cards_) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800140 contains_reference_to_target_space = false;
141 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
142 DCHECK(space_->HasAddress(reinterpret_cast<mirror::Object*>(start)));
143 bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, obj_visitor);
144 if (!contains_reference_to_target_space) {
145 // It was in the dirty card set, but it didn't actually contain
146 // a reference to the target space. So, remove it from the dirty
147 // card set so we won't have to scan it again (unless it gets
148 // dirty again.)
149 remove_card_set.insert(card_addr);
150 }
151 }
152
153 // Remove the cards that didn't contain a reference to the target
154 // space from the dirty card set.
Ian Rogers13735952014-10-08 12:43:28 -0700155 for (uint8_t* const card_addr : remove_card_set) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800156 DCHECK(dirty_cards_.find(card_addr) != dirty_cards_.end());
157 dirty_cards_.erase(card_addr);
158 }
159}
160
161void RememberedSet::Dump(std::ostream& os) {
162 CardTable* card_table = heap_->GetCardTable();
163 os << "RememberedSet dirty cards: [";
Ian Rogers13735952014-10-08 12:43:28 -0700164 for (const uint8_t* card_addr : dirty_cards_) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800165 auto start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
166 auto end = start + CardTable::kCardSize;
167 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "\n";
168 }
169 os << "]";
170}
171
172void RememberedSet::AssertAllDirtyCardsAreWithinSpace() const {
173 CardTable* card_table = heap_->GetCardTable();
Ian Rogers13735952014-10-08 12:43:28 -0700174 for (const uint8_t* card_addr : dirty_cards_) {
175 auto start = reinterpret_cast<uint8_t*>(card_table->AddrFromCard(card_addr));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800176 auto end = start + CardTable::kCardSize;
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700177 DCHECK_LE(space_->Begin(), start);
178 DCHECK_LE(end, space_->Limit());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800179 }
180}
181
182} // namespace accounting
183} // namespace gc
184} // namespace art