blob: aad214af3e8c2370b21b6d33e140a8ecc9319071 [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 Chartier83c8ee02014-01-28 14:50:23 -080073 ModUnionUpdateObjectReferencesVisitor(RootCallback* callback, void* arg)
74 : callback_(callback),
Mathieu Chartier11409ae2013-09-23 11:49:36 -070075 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) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080083 Object* new_ref = callback_(ref, arg_, 0, kRootVMInternal);
Mathieu Chartier11409ae2013-09-23 11:49:36 -070084 if (new_ref != ref) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 // Use SetFieldObjectWithoutWriteBarrier to avoid card mark as an optimization which
86 // reduces dirtied pages and improves performance.
87 obj->SetFieldObjectWithoutWriteBarrier(offset, new_ref, true);
Mathieu Chartier11409ae2013-09-23 11:49:36 -070088 }
89 }
Ian Rogers1d54e732013-05-02 21:10:01 -070090 }
91
92 private:
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080093 RootCallback* const callback_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -070094 void* arg_;
Ian Rogers1d54e732013-05-02 21:10:01 -070095};
96
Mathieu Chartier11409ae2013-09-23 11:49:36 -070097class ModUnionScanImageRootVisitor {
98 public:
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080099 ModUnionScanImageRootVisitor(RootCallback* callback, void* arg)
100 : callback_(callback), arg_(arg) {}
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700101
102 void operator()(Object* root) const
103 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
105 DCHECK(root != NULL);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800106 ModUnionUpdateObjectReferencesVisitor ref_visitor(callback_, arg_);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700107 collector::MarkSweep::VisitObjectReferences(root, ref_visitor, true);
108 }
109
110 private:
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800111 RootCallback* const callback_;
112 void* const arg_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700113};
114
115void ModUnionTableReferenceCache::ClearCards() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700116 CardTable* card_table = GetHeap()->GetCardTable();
117 ModUnionClearCardSetVisitor visitor(&cleared_cards_);
118 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700119 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700120}
121
122class AddToReferenceArrayVisitor {
123 public:
124 explicit AddToReferenceArrayVisitor(ModUnionTableReferenceCache* mod_union_table,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125 std::vector<mirror::HeapReference<Object>*>* references)
126 : mod_union_table_(mod_union_table), references_(references) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700127 }
128
129 // Extra parameters are required since we use this same visitor signature for checking objects.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700130 void operator()(Object* obj, Object* ref, const MemberOffset& offset,
Brian Carlstromdf629502013-07-17 22:39:56 -0700131 bool /* is_static */) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700132 // Only add the reference if it is non null and fits our criteria.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700133 if (ref != nullptr && mod_union_table_->AddReference(obj, ref)) {
134 // Push the adddress of the reference.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 references_->push_back(obj->GetFieldObjectReferenceAddr(offset));
Ian Rogers1d54e732013-05-02 21:10:01 -0700136 }
137 }
138
139 private:
140 ModUnionTableReferenceCache* const mod_union_table_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141 std::vector<mirror::HeapReference<Object>*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700142};
143
144class ModUnionReferenceVisitor {
145 public:
146 explicit ModUnionReferenceVisitor(ModUnionTableReferenceCache* const mod_union_table,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147 std::vector<mirror::HeapReference<Object>*>* references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700148 : mod_union_table_(mod_union_table),
149 references_(references) {
150 }
151
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700152 void operator()(Object* obj) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700153 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
154 DCHECK(obj != NULL);
155 // We don't have an early exit since we use the visitor pattern, an early
156 // exit should significantly speed this up.
157 AddToReferenceArrayVisitor visitor(mod_union_table_, references_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 collector::MarkSweep::VisitObjectReferences(obj, visitor, true);
Ian Rogers1d54e732013-05-02 21:10:01 -0700159 }
160 private:
161 ModUnionTableReferenceCache* const mod_union_table_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800162 std::vector<mirror::HeapReference<Object>*>* const references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700163};
164
165class CheckReferenceVisitor {
166 public:
167 explicit CheckReferenceVisitor(ModUnionTableReferenceCache* mod_union_table,
168 const std::set<const Object*>& references)
169 : mod_union_table_(mod_union_table),
170 references_(references) {
171 }
172
173 // Extra parameters are required since we use this same visitor signature for checking objects.
174 // TODO: Fixme when anotatalysis works with visitors.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800175 void operator()(Object* obj, Object* ref,
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700176 const MemberOffset& /* offset */, bool /* is_static */) const
Ian Rogers1d54e732013-05-02 21:10:01 -0700177 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
178 Heap* heap = mod_union_table_->GetHeap();
179 if (ref != NULL && mod_union_table_->AddReference(obj, ref) &&
180 references_.find(ref) == references_.end()) {
181 space::ContinuousSpace* from_space = heap->FindContinuousSpaceFromObject(obj, false);
182 space::ContinuousSpace* to_space = heap->FindContinuousSpaceFromObject(ref, false);
183 LOG(INFO) << "Object " << reinterpret_cast<const void*>(obj) << "(" << PrettyTypeOf(obj) << ")"
184 << "References " << reinterpret_cast<const void*>(ref)
185 << "(" << PrettyTypeOf(ref) << ") without being in mod-union table";
186 LOG(INFO) << "FromSpace " << from_space->GetName() << " type " << from_space->GetGcRetentionPolicy();
187 LOG(INFO) << "ToSpace " << to_space->GetName() << " type " << to_space->GetGcRetentionPolicy();
188 mod_union_table_->GetHeap()->DumpSpaces();
189 LOG(FATAL) << "FATAL ERROR";
190 }
191 }
192
193 private:
194 ModUnionTableReferenceCache* const mod_union_table_;
195 const std::set<const Object*>& references_;
196};
197
198class ModUnionCheckReferences {
199 public:
Brian Carlstromdf629502013-07-17 22:39:56 -0700200 explicit ModUnionCheckReferences(ModUnionTableReferenceCache* mod_union_table,
201 const std::set<const Object*>& references)
Ian Rogers1d54e732013-05-02 21:10:01 -0700202 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
203 : mod_union_table_(mod_union_table), references_(references) {
204 }
205
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700206 void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers1d54e732013-05-02 21:10:01 -0700207 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
208 DCHECK(obj != NULL);
209 CheckReferenceVisitor visitor(mod_union_table_, references_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700210 collector::MarkSweep::VisitObjectReferences(obj, visitor, true);
Ian Rogers1d54e732013-05-02 21:10:01 -0700211 }
212
213 private:
214 ModUnionTableReferenceCache* const mod_union_table_;
215 const std::set<const Object*>& references_;
216};
217
218void ModUnionTableReferenceCache::Verify() {
219 // Start by checking that everything in the mod union table is marked.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700220 for (const auto& ref_pair : references_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800221 for (mirror::HeapReference<Object>* ref : ref_pair.second) {
222 CHECK(heap_->IsLiveObjectLocked(ref->AsMirrorPtr()));
Ian Rogers1d54e732013-05-02 21:10:01 -0700223 }
224 }
225
226 // Check the references of each clean card which is also in the mod union table.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700227 CardTable* card_table = heap_->GetCardTable();
228 SpaceBitmap* live_bitmap = space_->GetLiveBitmap();
229 for (const auto& ref_pair : references_) {
230 const byte* card = ref_pair.first;
Ian Rogers1d54e732013-05-02 21:10:01 -0700231 if (*card == CardTable::kCardClean) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700232 std::set<const Object*> reference_set;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800233 for (mirror::HeapReference<Object>* obj_ptr : ref_pair.second) {
234 reference_set.insert(obj_ptr->AsMirrorPtr());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700235 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700236 ModUnionCheckReferences visitor(this, reference_set);
237 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700238 live_bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700239 }
240 }
241}
242
243void ModUnionTableReferenceCache::Dump(std::ostream& os) {
244 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700245 os << "ModUnionTable cleared cards: [";
Mathieu Chartier02e25112013-08-14 16:14:24 -0700246 for (byte* card_addr : cleared_cards_) {
247 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700248 uintptr_t end = start + CardTable::kCardSize;
249 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << ",";
250 }
251 os << "]\nModUnionTable references: [";
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700252 for (const auto& ref_pair : references_) {
253 const byte* card_addr = ref_pair.first;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700254 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
Ian Rogers1d54e732013-05-02 21:10:01 -0700255 uintptr_t end = start + CardTable::kCardSize;
256 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "->{";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800257 for (mirror::HeapReference<Object>* ref : ref_pair.second) {
258 os << reinterpret_cast<const void*>(ref->AsMirrorPtr()) << ",";
Ian Rogers1d54e732013-05-02 21:10:01 -0700259 }
260 os << "},";
261 }
262}
263
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800264void ModUnionTableReferenceCache::UpdateAndMarkReferences(RootCallback* callback, void* arg) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700265 Heap* heap = GetHeap();
266 CardTable* card_table = heap->GetCardTable();
267
Ian Rogersef7d42f2014-01-06 12:55:46 -0800268 std::vector<mirror::HeapReference<Object>*> cards_references;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700269 ModUnionReferenceVisitor add_visitor(this, &cards_references);
Ian Rogers1d54e732013-05-02 21:10:01 -0700270
Mathieu Chartier02e25112013-08-14 16:14:24 -0700271 for (const auto& card : cleared_cards_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700272 // Clear and re-compute alloc space references associated with this card.
273 cards_references.clear();
274 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card));
275 uintptr_t end = start + CardTable::kCardSize;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700276 auto* space = heap->FindContinuousSpaceFromObject(reinterpret_cast<Object*>(start), false);
277 DCHECK(space != nullptr);
278 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700279 live_bitmap->VisitMarkedRange(start, end, add_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700280
281 // Update the corresponding references for the card.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700282 auto found = references_.find(card);
Ian Rogers1d54e732013-05-02 21:10:01 -0700283 if (found == references_.end()) {
284 if (cards_references.empty()) {
285 // No reason to add empty array.
286 continue;
287 }
288 references_.Put(card, cards_references);
289 } else {
290 found->second = cards_references;
291 }
292 }
293 cleared_cards_.clear();
Ian Rogers1d54e732013-05-02 21:10:01 -0700294 size_t count = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700295 for (const auto& ref : references_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800296 for (mirror::HeapReference<Object>* obj_ptr : ref.second) {
297 Object* obj = obj_ptr->AsMirrorPtr();
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700298 if (obj != nullptr) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800299 Object* new_obj = callback(obj, arg, 0, kRootVMInternal);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700300 // Avoid dirtying pages in the image unless necessary.
301 if (new_obj != obj) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800302 obj_ptr->Assign(new_obj);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700303 }
304 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700305 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700306 count += ref.second.size();
Ian Rogers1d54e732013-05-02 21:10:01 -0700307 }
308 if (VLOG_IS_ON(heap)) {
309 VLOG(gc) << "Marked " << count << " references in mod union table";
310 }
311}
312
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700313void ModUnionTableCardCache::ClearCards() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700314 CardTable* card_table = GetHeap()->GetCardTable();
315 ModUnionClearCardSetVisitor visitor(&cleared_cards_);
316 // Clear dirty cards in the this space and update the corresponding mod-union bits.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700317 card_table->ModifyCardsAtomic(space_->Begin(), space_->End(), AgeCardVisitor(), visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700318}
319
320// Mark all references to the alloc space(s).
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800321void ModUnionTableCardCache::UpdateAndMarkReferences(RootCallback* callback, void* arg) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700322 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800323 ModUnionScanImageRootVisitor scan_visitor(callback, arg);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700324 SpaceBitmap* bitmap = space_->GetLiveBitmap();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700325 for (const byte* card_addr : cleared_cards_) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700326 uintptr_t start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
327 DCHECK(space_->HasAddress(reinterpret_cast<Object*>(start)));
328 bitmap->VisitMarkedRange(start, start + CardTable::kCardSize, scan_visitor);
Ian Rogers1d54e732013-05-02 21:10:01 -0700329 }
330}
331
332void ModUnionTableCardCache::Dump(std::ostream& os) {
333 CardTable* card_table = heap_->GetCardTable();
Ian Rogers1d54e732013-05-02 21:10:01 -0700334 os << "ModUnionTable dirty cards: [";
Mathieu Chartier02e25112013-08-14 16:14:24 -0700335 for (const byte* card_addr : cleared_cards_) {
336 auto start = reinterpret_cast<uintptr_t>(card_table->AddrFromCard(card_addr));
337 auto end = start + CardTable::kCardSize;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700338 os << reinterpret_cast<void*>(start) << "-" << reinterpret_cast<void*>(end) << "\n";
Ian Rogers1d54e732013-05-02 21:10:01 -0700339 }
340 os << "]";
341}
342
343} // namespace accounting
344} // namespace gc
345} // namespace art