blob: 3bbc3810a0513a90c36a56595c1aca8579cdab82 [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"
22#include "gc/collector/mark_sweep-inl.h"
23#include "gc/heap.h"
24#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_field-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "mirror/object-inl.h"
27#include "mirror/class-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "mirror/object_array-inl.h"
29#include "space_bitmap-inl.h"
30#include "thread.h"
31#include "UniquePtr.h"
32
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070033using ::art::mirror::Object;
Ian Rogers1d54e732013-05-02 21:10:01 -070034
35namespace art {
36namespace gc {
37namespace accounting {
38
39class MarkIfReachesAllocspaceVisitor {
40 public:
41 explicit MarkIfReachesAllocspaceVisitor(Heap* const heap, accounting::SpaceBitmap* bitmap)
42 : heap_(heap),
43 bitmap_(bitmap) {
44 }
45
46 // Extra parameters are required since we use this same visitor signature for checking objects.
Brian Carlstromdf629502013-07-17 22:39:56 -070047 void operator()(const Object* obj, const Object* ref, const MemberOffset& /* offset */,
48 bool /* is_static */) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070049 // TODO: Optimize?
50 // TODO: C++0x auto
51 const std::vector<space::ContinuousSpace*>& spaces = heap_->GetContinuousSpaces();
52 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
53 for (It cur = spaces.begin(); cur != spaces.end(); ++cur) {
54 if ((*cur)->IsDlMallocSpace() && (*cur)->Contains(ref)) {
55 bitmap_->Set(obj);
56 break;
57 }
58 }
59 }
60
61 private:
62 Heap* const heap_;
63 accounting::SpaceBitmap* const bitmap_;
64};
65
66class ModUnionVisitor {
67 public:
68 explicit ModUnionVisitor(Heap* const heap, accounting::SpaceBitmap* bitmap)
69 : heap_(heap),
70 bitmap_(bitmap) {
71 }
72
Brian Carlstromdf629502013-07-17 22:39:56 -070073 void operator()(const Object* obj) const
Ian Rogers1d54e732013-05-02 21:10:01 -070074 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
75 Locks::mutator_lock_) {
76 DCHECK(obj != NULL);
77 // We don't have an early exit since we use the visitor pattern, an early exit should
78 // significantly speed this up.
79 MarkIfReachesAllocspaceVisitor visitor(heap_, bitmap_);
80 collector::MarkSweep::VisitObjectReferences(obj, visitor);
81 }
82 private:
83 Heap* const heap_;
84 accounting::SpaceBitmap* const bitmap_;
85};
86
87class ModUnionClearCardSetVisitor {
88 public:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070089 explicit ModUnionClearCardSetVisitor(ModUnionTable::CardSet* const cleared_cards)
Ian Rogers1d54e732013-05-02 21:10:01 -070090 : cleared_cards_(cleared_cards) {
91 }
92
Brian Carlstromdf629502013-07-17 22:39:56 -070093 inline void operator()(byte* card, byte expected_value, byte new_value) const {
Ian Rogers1d54e732013-05-02 21:10:01 -070094 if (expected_value == CardTable::kCardDirty) {
95 cleared_cards_->insert(card);
96 }
97 }
98
99 private:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700100 ModUnionTable::CardSet* const cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700101};
102
103class ModUnionClearCardVisitor {
104 public:
105 explicit ModUnionClearCardVisitor(std::vector<byte*>* cleared_cards)
106 : cleared_cards_(cleared_cards) {
107 }
108
Brian Carlstromdf629502013-07-17 22:39:56 -0700109 void operator()(byte* card, byte expected_card, byte new_card) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700110 if (expected_card == CardTable::kCardDirty) {
111 cleared_cards_->push_back(card);
112 }
113 }
114 private:
115 std::vector<byte*>* const cleared_cards_;
116};
117
118class ModUnionScanImageRootVisitor {
119 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700120 explicit ModUnionScanImageRootVisitor(collector::MarkSweep* const mark_sweep)
121 : mark_sweep_(mark_sweep) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700122
Brian Carlstromdf629502013-07-17 22:39:56 -0700123 void operator()(const Object* root) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700124 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
126 DCHECK(root != NULL);
127 mark_sweep_->ScanRoot(root);
128 }
129
130 private:
131 collector::MarkSweep* const mark_sweep_;
132};
133
134void ModUnionTableReferenceCache::ClearCards(space::ContinuousSpace* space) {
135 CardTable* card_table = GetHeap()->GetCardTable();
136 ModUnionClearCardSetVisitor visitor(&cleared_cards_);
137 // Clear dirty cards in the this space and update the corresponding mod-union bits.
138 card_table->ModifyCardsAtomic(space->Begin(), space->End(), AgeCardVisitor(), visitor);
139}
140
141class AddToReferenceArrayVisitor {
142 public:
143 explicit AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table,
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700144 std::vector<const Object*>* references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700145 : mod_union_table_(mod_union_table),
146 references_(references) {
147 }
148
149 // Extra parameters are required since we use this same visitor signature for checking objects.
Brian Carlstromdf629502013-07-17 22:39:56 -0700150 void operator()(const Object* obj, const Object* ref, const MemberOffset& /* offset */,
151 bool /* is_static */) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700152 // Only add the reference if it is non null and fits our criteria.
153 if (ref != NULL && mod_union_table_->AddReference(obj, ref)) {
154 references_->push_back(ref);
155 }
156 }
157
158 private:
159 ModUnionTableReferenceCache* const mod_union_table_;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700160 std::vector<const Object*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700161};
162
163class ModUnionReferenceVisitor {
164 public:
165 explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table,
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700166 std::vector<const Object*>* references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700167 : mod_union_table_(mod_union_table),
168 references_(references) {
169 }
170
Brian Carlstromdf629502013-07-17 22:39:56 -0700171 void operator()(const Object* obj) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700172 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
173 DCHECK(obj != NULL);
174 // We don't have an early exit since we use the visitor pattern, an early
175 // exit should significantly speed this up.
176 AddToReferenceArrayVisitor visitor(mod_union_table_, references_);
177 collector::MarkSweep::VisitObjectReferences(obj, visitor);
178 }
179 private:
180 ModUnionTableReferenceCache* const mod_union_table_;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700181 std::vector<const Object*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700182};
183
184class CheckReferenceVisitor {
185 public:
186 explicit CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table,
187 const std::set<const Object*>& references)
188 : mod_union_table_(mod_union_table),
189 references_(references) {
190 }
191
192 // Extra parameters are required since we use this same visitor signature for checking objects.
193 // TODO: Fixme when anotatalysis works with visitors.
Brian Carlstromdf629502013-07-17 22:39:56 -0700194 void operator()(const Object* obj, const Object* ref, const MemberOffset& /* offset */,
195 bool /* is_static */) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700196 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
197 Heap* heap = mod_union_table_->GetHeap();
198 if (ref != NULL && mod_union_table_->AddReference(obj, ref) &&
199 references_.find(ref) == references_.end()) {
200 space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false);
201 space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false);
202 LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << PrettyTypeOf(obj) << ")"
203 << "References " << reinterpret_cast<const void*>(ref)
204 << "(" << PrettyTypeOf(ref) << ") without being in mod-union table";
205 LOG(INFO) << "FromSpace " << from_space->GetName() << " type " << from_space->GetGcRetentionPolicy();
206 LOG(INFO) << "ToSpace " << to_space->GetName() << " type " << to_space->GetGcRetentionPolicy();
207 mod_union_table_->GetHeap()->DumpSpaces();
208 LOG(FATAL) << "FATAL ERROR";
209 }
210 }
211
212 private:
213 ModUnionTableReferenceCache* const mod_union_table_;
214 const std::set<const Object*>& references_;
215};
216
217class ModUnionCheckReferences {
218 public:
Brian Carlstromdf629502013-07-17 22:39:56 -0700219 explicit ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table,
220 const std::set<const Object*>& references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700221 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
222 : mod_union_table_(mod_union_table), references_(references) {
223 }
224
Brian Carlstromdf629502013-07-17 22:39:56 -0700225 void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers1d54e732013-05-02 21:10:01 -0700226 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
227 DCHECK(obj != NULL);
228 CheckReferenceVisitor visitor(mod_union_table_, references_);
229 collector::MarkSweep::VisitObjectReferences(obj, visitor);
230 }
231
232 private:
233 ModUnionTableReferenceCache* const mod_union_table_;
234 const std::set<const Object*>& references_;
235};
236
237void ModUnionTableReferenceCache::Verify() {
238 // Start by checking that everything in the mod union table is marked.
239 Heap* heap = GetHeap();
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700240 typedef SafeMap<const byte*, std::vector<const Object*> >::const_iterator It;
241 typedef std::vector<const Object*>::const_iterator It2;
Ian Rogers1d54e732013-05-02 21:10:01 -0700242 for (It it = references_.begin(), end = references_.end(); it != end; ++it) {
243 for (It2 it_ref = it->second.begin(), end_ref = it->second.end(); it_ref != end_ref;
244 ++it_ref ) {
245 CHECK(heap->IsLiveObjectLocked(*it_ref));
246 }
247 }
248
249 // Check the references of each clean card which is also in the mod union table.
250 CardTable* card_table = heap->GetCardTable();
251 for (It it = references_.begin(); it != references_.end(); ++it) {
252 const byte* card = &*it->first;
253 if (*card == CardTable::kCardClean) {
254 std::set<const Object*> reference_set;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700255 for (It2 itr = it->second.begin(); itr != it->second.end(); ++itr) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700256 reference_set.insert(*itr);
257 }
258 ModUnionCheckReferences visitor(this, reference_set);
259 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
260 uintptr_t end = start + CardTable::kCardSize;
261 space::ContinuousSpace* space =
262 heap->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
263 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartier184e3222013-08-03 14:02:57 -0700264 live_bitmap->VisitMarkedRange(start, end, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700265 }
266 }
267}
268
269void ModUnionTableReferenceCache::Dump(std::ostream& os) {
270 CardTable* card_table = heap_->GetCardTable();
271 typedef std::set<byte*>::const_iterator It;
272 os << "ModUnionTable cleared cards: [";
273 for (It it = cleared_cards_.begin(); it != cleared_cards_.end(); ++it) {
274 byte* card = *it;
275 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
276 uintptr_t end = start + CardTable::kCardSize;
277 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
278 }
279 os << "]\nModUnionTable references: [";
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700280 typedef SafeMap<const byte*, std::vector<const Object*> >::const_iterator It2;
Ian Rogers1d54e732013-05-02 21:10:01 -0700281 for (It2 it = references_.begin(); it != references_.end(); ++it) {
282 const byte* card = &*it->first;
283 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
284 uintptr_t end = start + CardTable::kCardSize;
285 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{";
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700286 typedef std::vector<const Object*>::const_iterator It3;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700287 for (It3 itr = it->second.begin(); itr != it->second.end(); ++itr) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700288 os << reinterpret_cast<const void*>(*itr) << ",";
289 }
290 os << "},";
291 }
292}
293
294void ModUnionTableReferenceCache::Update() {
295 Heap* heap = GetHeap();
296 CardTable* card_table = heap->GetCardTable();
297
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700298 std::vector<const Object*> cards_references;
Ian Rogers1d54e732013-05-02 21:10:01 -0700299 ModUnionReferenceVisitor visitor(this, &cards_references);
300
301 typedef std::set<byte*>::iterator It;
302 for (It it = cleared_cards_.begin(), cc_end = cleared_cards_.end(); it != cc_end; ++it) {
303 byte* card = *it;
304 // Clear and re-compute alloc space references associated with this card.
305 cards_references.clear();
306 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
307 uintptr_t end = start + CardTable::kCardSize;
308 SpaceBitmap* live_bitmap =
309 heap->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false)->GetLiveBitmap();
Mathieu Chartier184e3222013-08-03 14:02:57 -0700310 live_bitmap->VisitMarkedRange(start, end, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700311
312 // Update the corresponding references for the card.
313 // TODO: C++0x auto
Mathieu Chartier184e3222013-08-03 14:02:57 -0700314 SafeMap<const byte*, std::vector<const Object*> >::iterator found = references_.find(card);
Ian Rogers1d54e732013-05-02 21:10:01 -0700315 if (found == references_.end()) {
316 if (cards_references.empty()) {
317 // No reason to add empty array.
318 continue;
319 }
320 references_.Put(card, cards_references);
321 } else {
322 found->second = cards_references;
323 }
324 }
325 cleared_cards_.clear();
326}
327
328void ModUnionTableReferenceCache::MarkReferences(collector::MarkSweep* mark_sweep) {
329 // TODO: C++0x auto
330 size_t count = 0;
331
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700332 typedef SafeMap<const byte*, std::vector<const Object*> >::const_iterator It;
Ian Rogers1d54e732013-05-02 21:10:01 -0700333 for (It it = references_.begin(); it != references_.end(); ++it) {
Brian Carlstrom3e3d5912013-07-18 00:19:45 -0700334 typedef std::vector<const Object*>::const_iterator It2;
Brian Carlstromdf629502013-07-17 22:39:56 -0700335 for (It2 it_ref = it->second.begin(); it_ref != it->second.end(); ++it_ref) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700336 mark_sweep->MarkRoot(*it_ref);
337 ++count;
338 }
339 }
340 if (VLOG_IS_ON(heap)) {
341 VLOG(gc) << "Marked " << count << " references in mod union table";
342 }
343}
344
345void ModUnionTableCardCache::ClearCards(space::ContinuousSpace* space) {
346 CardTable* card_table = GetHeap()->GetCardTable();
347 ModUnionClearCardSetVisitor visitor(&cleared_cards_);
348 // Clear dirty cards in the this space and update the corresponding mod-union bits.
349 card_table->ModifyCardsAtomic(space->Begin(), space->End(), AgeCardVisitor(), visitor);
350}
351
352// Mark all references to the alloc space(s).
353void ModUnionTableCardCache::MarkReferences(collector::MarkSweep* mark_sweep) {
354 CardTable* card_table = heap_->GetCardTable();
355 ModUnionScanImageRootVisitor visitor(mark_sweep);
356 typedef std::set<byte*>::const_iterator It;
357 It it = cleared_cards_.begin();
358 It cc_end = cleared_cards_.end();
359 if (it != cc_end) {
360 byte* card = *it;
361 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
362 uintptr_t end = start + CardTable::kCardSize;
363 space::ContinuousSpace* cur_space =
364 heap_->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
365 accounting::SpaceBitmap* cur_live_bitmap = cur_space->GetLiveBitmap();
Mathieu Chartier184e3222013-08-03 14:02:57 -0700366 cur_live_bitmap->VisitMarkedRange(start, end, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700367 for (++it; it != cc_end; ++it) {
368 card = *it;
369 start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
370 end = start + CardTable::kCardSize;
371 if (UNLIKELY(!cur_space->Contains(reinterpret_cast<Object*>(start)))) {
372 cur_space = heap_->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
373 cur_live_bitmap = cur_space->GetLiveBitmap();
374 }
Mathieu Chartier184e3222013-08-03 14:02:57 -0700375 cur_live_bitmap->VisitMarkedRange(start, end, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700376 }
377 }
378}
379
380void ModUnionTableCardCache::Dump(std::ostream& os) {
381 CardTable* card_table = heap_->GetCardTable();
382 typedef std::set<byte*>::const_iterator It;
383 os << "ModUnionTable dirty cards: [";
384 for (It it = cleared_cards_.begin(); it != cleared_cards_.end(); ++it) {
385 byte* card = *it;
386 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
387 uintptr_t end = start + CardTable::kCardSize;
388 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
389 }
390 os << "]";
391}
392
393} // namespace accounting
394} // namespace gc
395} // namespace art