blob: 7cbe94d3d2b6a607a8bb1d7e8a5fd000fbd39081 [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
2 * Copyright (C) 2012 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 "mod_union_table.h"
18
19#include "base/stl_util.h"
20#include "card_table-inl.h"
21#include "heap_bitmap.h"
Mathieu Chartier11409ae2013-09-23 11:49:36 -070022#include "gc/collector/mark_sweep.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "gc/collector/mark_sweep-inl.h"
24#include "gc/heap.h"
25#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_field-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "mirror/object-inl.h"
28#include "mirror/class-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "mirror/object_array-inl.h"
30#include "space_bitmap-inl.h"
31#include "thread.h"
32#include "UniquePtr.h"
33
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070034using ::art::mirror::Object;
Ian Rogers1d54e732013-05-02 21:10:01 -070035
36namespace art {
37namespace gc {
38namespace accounting {
39
Ian Rogers1d54e732013-05-02 21:10:01 -070040class ModUnionClearCardSetVisitor {
41 public:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070042 explicit ModUnionClearCardSetVisitor(ModUnionTable::CardSet* const cleared_cards)
Ian Rogers1d54e732013-05-02 21:10:01 -070043 : cleared_cards_(cleared_cards) {
44 }
45
Brian Carlstromdf629502013-07-17 22:39:56 -070046 inline void operator()(byte* card, byte expected_value, byte new_value) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070047 if (expected_value == CardTable::kCardDirty) {
48 cleared_cards_->insert(card);
49 }
50 }
51
52 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070053 ModUnionTable::CardSet* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -070054};
55
56class ModUnionClearCardVisitor {
57 public:
58 explicit ModUnionClearCardVisitor(std::vector<byte*>* cleared_cards)
59 : cleared_cards_(cleared_cards) {
60 }
61
Brian Carlstromdf629502013-07-17 22:39:56 -070062 void operator()(byte* card, byte expected_card, byte new_card) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070063 if (expected_card == CardTable::kCardDirty) {
64 cleared_cards_->push_back(card);
65 }
66 }
67 private:
68 std::vector<byte*>* const cleared_cards_;
69};
70
Mathieu Chartier11409ae2013-09-23 11:49:36 -070071class ModUnionUpdateObjectReferencesVisitor {
Ian Rogers1d54e732013-05-02 21:10:01 -070072 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -070073 ModUnionUpdateObjectReferencesVisitor(RootVisitor visitor, void* arg)
74 : visitor_(visitor),
75 arg_(arg) {
76 }
Ian Rogers1d54e732013-05-02 21:10:01 -070077
Mathieu Chartier11409ae2013-09-23 11:49:36 -070078 // Extra parameters are required since we use this same visitor signature for checking objects.
79 void operator()(Object* obj, Object* ref, const MemberOffset& offset,
80 bool /* is_static */) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81 // Only add the reference if it is non null and fits our criteria.
82 if (ref != nullptr) {
83 Object* new_ref = visitor_(ref, arg_);
84 if (new_ref != ref) {
85 obj->SetFieldObject(offset, ref, false, true);
86 }
87 }
Ian Rogers1d54e732013-05-02 21:10:01 -070088 }
89
90 private:
Mathieu Chartier11409ae2013-09-23 11:49:36 -070091 RootVisitor* visitor_;
92 void* arg_;
Ian Rogers1d54e732013-05-02 21:10:01 -070093};
94
Mathieu Chartier11409ae2013-09-23 11:49:36 -070095class ModUnionScanImageRootVisitor {
96 public:
97 ModUnionScanImageRootVisitor(RootVisitor visitor, void* arg)
98 : visitor_(visitor), arg_(arg) {}
99
100 void operator()(Object* root) const
101 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
103 DCHECK(root != NULL);
104 ModUnionUpdateObjectReferencesVisitor ref_visitor(visitor_, arg_);
105 collector::MarkSweep::VisitObjectReferences(root, ref_visitor, true);
106 }
107
108 private:
109 RootVisitor* visitor_;
110 void* arg_;
111};
112
113void ModUnionTableReferenceCache::ClearCards() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700114 CardTable* card_table = GetHeap()->GetCardTable();
115 ModUnionClearCardSetVisitor visitor(&cleared_cards_);
116 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700117 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700118}
119
120class AddToReferenceArrayVisitor {
121 public:
122 explicit AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table,
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700123 std::vector<Object**>* references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700124 : mod_union_table_(mod_union_table),
125 references_(references) {
126 }
127
128 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700129 void operator()(Object* obj, Object* ref, const MemberOffset& offset,
Brian Carlstromdf629502013-07-17 22:39:56 -0700130 bool /* is_static */) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700131 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700132 if (ref != nullptr && mod_union_table_->AddReference(obj, ref)) {
133 // Push the adddress of the reference.
134 references_->push_back(obj->GetFieldObjectAddr(offset));
Ian Rogers1d54e732013-05-02 21:10:01 -0700135 }
136 }
137
138 private:
139 ModUnionTableReferenceCache* const mod_union_table_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700140 std::vector<Object**>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700141};
142
143class ModUnionReferenceVisitor {
144 public:
145 explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table,
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700146 std::vector<Object**>* references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700147 : mod_union_table_(mod_union_table),
148 references_(references) {
149 }
150
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700151 void operator()(Object* obj) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700152 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
153 DCHECK(obj != NULL);
154 // We don't have an early exit since we use the visitor pattern, an early
155 // exit should significantly speed this up.
156 AddToReferenceArrayVisitor visitor(mod_union_table_, references_);
157 collector::MarkSweep::VisitObjectReferences(obj, visitor);
158 }
159 private:
160 ModUnionTableReferenceCache* const mod_union_table_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700161 std::vector<Object**>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700162};
163
164class CheckReferenceVisitor {
165 public:
166 explicit CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table,
167 const std::set<const Object*>& references)
168 : mod_union_table_(mod_union_table),
169 references_(references) {
170 }
171
172 // Extra parameters are required since we use this same visitor signature for checking objects.
173 // TODO: Fixme when anotatalysis works with visitors.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700174 void operator()(const Object* obj, const Object* ref,
175 const MemberOffset& /* offset */, bool /* is_static */) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700176 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
177 Heap* heap = mod_union_table_->GetHeap();
178 if (ref != NULL && mod_union_table_->AddReference(obj, ref) &&
179 references_.find(ref) == references_.end()) {
180 space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false);
181 space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false);
182 LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << PrettyTypeOf(obj) << ")"
183 << "References " << reinterpret_cast<const void*>(ref)
184 << "(" << PrettyTypeOf(ref) << ") without being in mod-union table";
185 LOG(INFO) << "FromSpace " << from_space->GetName() << " type " << from_space->GetGcRetentionPolicy();
186 LOG(INFO) << "ToSpace " << to_space->GetName() << " type " << to_space->GetGcRetentionPolicy();
187 mod_union_table_->GetHeap()->DumpSpaces();
188 LOG(FATAL) << "FATAL ERROR";
189 }
190 }
191
192 private:
193 ModUnionTableReferenceCache* const mod_union_table_;
194 const std::set<const Object*>& references_;
195};
196
197class ModUnionCheckReferences {
198 public:
Brian Carlstromdf629502013-07-17 22:39:56 -0700199 explicit ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table,
200 const std::set<const Object*>& references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700201 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
202 : mod_union_table_(mod_union_table), references_(references) {
203 }
204
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700205 void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers1d54e732013-05-02 21:10:01 -0700206 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
207 DCHECK(obj != NULL);
208 CheckReferenceVisitor visitor(mod_union_table_, references_);
209 collector::MarkSweep::VisitObjectReferences(obj, visitor);
210 }
211
212 private:
213 ModUnionTableReferenceCache* const mod_union_table_;
214 const std::set<const Object*>& references_;
215};
216
217void ModUnionTableReferenceCache::Verify() {
218 // Start by checking that everything in the mod union table is marked.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700219 for (const auto& ref_pair : references_) {
220 for (Object** ref : ref_pair.second) {
221 CHECK(heap_->IsLiveObjectLocked(*ref));
Ian Rogers1d54e732013-05-02 21:10:01 -0700222 }
223 }
224
225 // Check the references of each clean card which is also in the mod union table.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700226 CardTable* card_table = heap_->GetCardTable();
227 SpaceBitmap* live_bitmap = space_->GetLiveBitmap();
228 for (const auto& ref_pair : references_) {
229 const byte* card = ref_pair.first;
Ian Rogers1d54e732013-05-02 21:10:01 -0700230 if (*card == CardTable::kCardClean) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700231 std::set<const Object*> reference_set;
232 for (Object** obj_ptr : ref_pair.second) {
233 reference_set.insert(*obj_ptr);
234 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700235 ModUnionCheckReferences visitor(this, reference_set);
236 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700237 live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700238 }
239 }
240}
241
242void ModUnionTableReferenceCache::Dump(std::ostream& os) {
243 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700244 os << "ModUnionTable cleared cards: [";
Mathieu Chartier02e25112013-08-14 16:14:24 -0700245 for (byte* card_addr : cleared_cards_) {
246 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700247 uintptr_t end = start + CardTable::kCardSize;
248 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
249 }
250 os << "]\nModUnionTable references: [";
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700251 for (const auto& ref_pair : references_) {
252 const byte* card_addr = ref_pair.first;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700253 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700254 uintptr_t end = start + CardTable::kCardSize;
255 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{";
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700256 for (Object** ref : ref_pair.second) {
257 os << reinterpret_cast<const void*>(*ref) << ",";
Ian Rogers1d54e732013-05-02 21:10:01 -0700258 }
259 os << "},";
260 }
261}
262
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700263void ModUnionTableReferenceCache::UpdateAndMarkReferences(RootVisitor visitor, void* arg) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700264 Heap* heap = GetHeap();
265 CardTable* card_table = heap->GetCardTable();
266
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700267 std::vector<Object**> cards_references;
268 ModUnionReferenceVisitor add_visitor(this, &cards_references);
Ian Rogers1d54e732013-05-02 21:10:01 -0700269
Mathieu Chartier02e25112013-08-14 16:14:24 -0700270 for (const auto& card : cleared_cards_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700271 // Clear and re-compute alloc space references associated with this card.
272 cards_references.clear();
273 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
274 uintptr_t end = start + CardTable::kCardSize;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700275 auto* space = heap->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
276 DCHECK(space != nullptr);
277 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700278 live_bitmap->VisitMarkedRange(start, end, add_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700279
280 // Update the corresponding references for the card.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700281 auto found = references_.find(card);
Ian Rogers1d54e732013-05-02 21:10:01 -0700282 if (found == references_.end()) {
283 if (cards_references.empty()) {
284 // No reason to add empty array.
285 continue;
286 }
287 references_.Put(card, cards_references);
288 } else {
289 found->second = cards_references;
290 }
291 }
292 cleared_cards_.clear();
Ian Rogers1d54e732013-05-02 21:10:01 -0700293 size_t count = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700294 for (const auto& ref : references_) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700295 for (const auto& obj_ptr : ref.second) {
296 Object* obj = *obj_ptr;
297 if (obj != nullptr) {
298 Object* new_obj = visitor(obj, arg);
299 // Avoid dirtying pages in the image unless necessary.
300 if (new_obj != obj) {
301 *obj_ptr = new_obj;
302 }
303 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700304 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700305 count += ref.second.size();
Ian Rogers1d54e732013-05-02 21:10:01 -0700306 }
307 if (VLOG_IS_ON(heap)) {
308 VLOG(gc) << "Marked " << count << " references in mod union table";
309 }
310}
311
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700312void ModUnionTableCardCache::ClearCards() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700313 CardTable* card_table = GetHeap()->GetCardTable();
314 ModUnionClearCardSetVisitor visitor(&cleared_cards_);
315 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700316 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700317}
318
319// Mark all references to the alloc space(s).
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700320void ModUnionTableCardCache::UpdateAndMarkReferences(RootVisitor visitor, void* arg) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700321 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700322 ModUnionScanImageRootVisitor scan_visitor(visitor, arg);
323 SpaceBitmap* bitmap = space_->GetLiveBitmap();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700324 for (const byte* card_addr : cleared_cards_) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700325 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
326 DCHECK(space_->HasAddress(reinterpret_cast<Object*>(start)));
327 bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700328 }
329}
330
331void ModUnionTableCardCache::Dump(std::ostream& os) {
332 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700333 os << "ModUnionTable dirty cards: [";
Mathieu Chartier02e25112013-08-14 16:14:24 -0700334 for (const byte* card_addr : cleared_cards_) {
335 auto start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
336 auto end = start + CardTable::kCardSize;
Ian Rogers1d54e732013-05-02 21:10:01 -0700337 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
338 }
339 os << "]";
340}
341
342} // namespace accounting
343} // namespace gc
344} // namespace art