blob: 91c92537b51409dc6892305f1ad01ac4e379ff08 [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"
25#include "mirror/object-inl.h"
26#include "mirror/class-inl.h"
27#include "mirror/field-inl.h"
28#include "mirror/object_array-inl.h"
29#include "space_bitmap-inl.h"
30#include "thread.h"
31#include "UniquePtr.h"
32
33using namespace art::mirror;
34
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:
89 explicit ModUnionClearCardSetVisitor(std::set<byte*>* const cleared_cards)
90 : 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:
100 std::set<byte*>* const cleared_cards_;
101};
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,
144 std::vector<const mirror::Object*>* references)
145 : 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_;
160 std::vector<const mirror::Object*>* const references_;
161};
162
163class ModUnionReferenceVisitor {
164 public:
165 explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table,
166 std::vector<const mirror::Object*>* references)
167 : 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_;
181 std::vector<const mirror::Object*>* const references_;
182};
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();
240 typedef SafeMap<const byte*, std::vector<const mirror::Object*> >::const_iterator It;
241 typedef std::vector<const mirror::Object*>::const_iterator It2;
242 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;
255 for (It2 itr = it->second.begin(); itr != it->second.end();++itr) {
256 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();
264 live_bitmap->VisitMarkedRange(start, end, visitor, VoidFunctor());
265 }
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: [";
280 typedef SafeMap<const byte*, std::vector<const mirror::Object*> >::const_iterator It2;
281 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) << "->{";
286 typedef std::vector<const mirror::Object*>::const_iterator It3;
287 for (It3 itr = it->second.begin(); itr != it->second.end();++itr) {
288 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
298 std::vector<const mirror::Object*> cards_references;
299 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();
310 live_bitmap->VisitMarkedRange(start, end, visitor, VoidFunctor());
311
312 // Update the corresponding references for the card.
313 // TODO: C++0x auto
314 SafeMap<const byte*, std::vector<const mirror::Object*> >::iterator
315 found = references_.find(card);
316 if (found == references_.end()) {
317 if (cards_references.empty()) {
318 // No reason to add empty array.
319 continue;
320 }
321 references_.Put(card, cards_references);
322 } else {
323 found->second = cards_references;
324 }
325 }
326 cleared_cards_.clear();
327}
328
329void ModUnionTableReferenceCache::MarkReferences(collector::MarkSweep* mark_sweep) {
330 // TODO: C++0x auto
331 size_t count = 0;
332
333 typedef SafeMap<const byte*, std::vector<const mirror::Object*> >::const_iterator It;
334 for (It it = references_.begin(); it != references_.end(); ++it) {
335 typedef std::vector<const mirror::Object*>::const_iterator It2;
Brian Carlstromdf629502013-07-17 22:39:56 -0700336 for (It2 it_ref = it->second.begin(); it_ref != it->second.end(); ++it_ref) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700337 mark_sweep->MarkRoot(*it_ref);
338 ++count;
339 }
340 }
341 if (VLOG_IS_ON(heap)) {
342 VLOG(gc) << "Marked " << count << " references in mod union table";
343 }
344}
345
346void ModUnionTableCardCache::ClearCards(space::ContinuousSpace* space) {
347 CardTable* card_table = GetHeap()->GetCardTable();
348 ModUnionClearCardSetVisitor visitor(&cleared_cards_);
349 // Clear dirty cards in the this space and update the corresponding mod-union bits.
350 card_table->ModifyCardsAtomic(space->Begin(), space->End(), AgeCardVisitor(), visitor);
351}
352
353// Mark all references to the alloc space(s).
354void ModUnionTableCardCache::MarkReferences(collector::MarkSweep* mark_sweep) {
355 CardTable* card_table = heap_->GetCardTable();
356 ModUnionScanImageRootVisitor visitor(mark_sweep);
357 typedef std::set<byte*>::const_iterator It;
358 It it = cleared_cards_.begin();
359 It cc_end = cleared_cards_.end();
360 if (it != cc_end) {
361 byte* card = *it;
362 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
363 uintptr_t end = start + CardTable::kCardSize;
364 space::ContinuousSpace* cur_space =
365 heap_->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
366 accounting::SpaceBitmap* cur_live_bitmap = cur_space->GetLiveBitmap();
367 cur_live_bitmap->VisitMarkedRange(start, end, visitor, VoidFunctor());
368 for (++it; it != cc_end; ++it) {
369 card = *it;
370 start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
371 end = start + CardTable::kCardSize;
372 if (UNLIKELY(!cur_space->Contains(reinterpret_cast<Object*>(start)))) {
373 cur_space = heap_->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
374 cur_live_bitmap = cur_space->GetLiveBitmap();
375 }
376 cur_live_bitmap->VisitMarkedRange(start, end, visitor, VoidFunctor());
377 }
378 }
379}
380
381void ModUnionTableCardCache::Dump(std::ostream& os) {
382 CardTable* card_table = heap_->GetCardTable();
383 typedef std::set<byte*>::const_iterator It;
384 os << "ModUnionTable dirty cards: [";
385 for (It it = cleared_cards_.begin(); it != cleared_cards_.end(); ++it) {
386 byte* card = *it;
387 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
388 uintptr_t end = start + CardTable::kCardSize;
389 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
390 }
391 os << "]";
392}
393
394} // namespace accounting
395} // namespace gc
396} // namespace art