xref: /aosp_15_r20/art/runtime/gc/accounting/mod_union_table_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2015 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-inl.h"
18 
19 #include "class_linker-inl.h"
20 #include "class_root-inl.h"
21 #include "common_runtime_test.h"
22 #include "gc/space/space-inl.h"
23 #include "mirror/array-alloc-inl.h"
24 #include "mirror/array-inl.h"
25 #include "space_bitmap-inl.h"
26 #include "thread-current-inl.h"
27 #include "thread_list.h"
28 
29 namespace art HIDDEN {
30 namespace gc {
31 namespace accounting {
32 
33 class ModUnionTableFactory {
34  public:
35   enum TableType {
36     kTableTypeCardCache,
37     kTableTypeReferenceCache,
38     kTableTypeCount,  // Number of values in the enum.
39   };
40 
41   // Target space is ignored for the card cache implementation.
42   static ModUnionTable* Create(
43       TableType type, space::ContinuousSpace* space, space::ContinuousSpace* target_space);
44 };
45 
46 class ModUnionTableTest : public CommonRuntimeTest {
47  public:
ModUnionTableTest()48   ModUnionTableTest() : java_lang_object_array_(nullptr) {
49     use_boot_image_ = true;  // Make the Runtime creation cheaper.
50   }
AllocObjectArray(Thread * self,space::ContinuousMemMapAllocSpace * space,size_t component_count)51   mirror::ObjectArray<mirror::Object>* AllocObjectArray(
52       Thread* self, space::ContinuousMemMapAllocSpace* space, size_t component_count)
53       REQUIRES_SHARED(Locks::mutator_lock_) {
54     auto* klass = GetObjectArrayClass(self, space);
55     const size_t size = mirror::ComputeArraySize(component_count, 2);
56     size_t bytes_allocated = 0, bytes_tl_bulk_allocated;
57     auto* obj = down_cast<mirror::ObjectArray<mirror::Object>*>(
58         space->Alloc(self, size, &bytes_allocated, nullptr, &bytes_tl_bulk_allocated));
59     if (obj != nullptr) {
60       obj->SetClass(klass);
61       obj->SetLength(static_cast<int32_t>(component_count));
62       space->GetLiveBitmap()->Set(obj);
63       EXPECT_GE(bytes_allocated, size);
64     }
65     return obj;
66   }
ResetClass()67   void ResetClass() {
68     java_lang_object_array_ = nullptr;
69   }
70   void RunTest(ModUnionTableFactory::TableType type);
71 
72  private:
GetObjectArrayClass(Thread * self,space::ContinuousMemMapAllocSpace * space)73   mirror::Class* GetObjectArrayClass(Thread* self, space::ContinuousMemMapAllocSpace* space)
74       REQUIRES_SHARED(Locks::mutator_lock_) {
75     if (java_lang_object_array_ == nullptr) {
76       java_lang_object_array_ = GetClassRoot<mirror::ObjectArray<mirror::Object>>().Ptr();
77       // Since the test doesn't have an image, the class of the object array keeps cards live
78       // inside the card cache mod-union table and causes the check
79       // ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj3)));
80       // to fail since the class ends up keeping the card dirty. To get around this, we make a fake
81       // copy of the class in the same space that we are allocating in.
82       DCHECK(java_lang_object_array_ != nullptr);
83       const size_t class_size = java_lang_object_array_->GetClassSize();
84       size_t bytes_allocated = 0, bytes_tl_bulk_allocated;
85       auto* klass = down_cast<mirror::Class*>(space->Alloc(self, class_size, &bytes_allocated,
86                                                            nullptr,
87                                                            &bytes_tl_bulk_allocated));
88       DCHECK(klass != nullptr);
89       memcpy(klass, java_lang_object_array_, class_size);
90       Runtime::Current()->GetHeap()->GetCardTable()->MarkCard(klass);
91       java_lang_object_array_ = klass;
92     }
93     return java_lang_object_array_;
94   }
95   mirror::Class* java_lang_object_array_;
96 };
97 
98 // Collect visited objects into container.
99 class CollectVisitedVisitor : public MarkObjectVisitor {
100  public:
CollectVisitedVisitor(std::set<mirror::Object * > * out)101   explicit CollectVisitedVisitor(std::set<mirror::Object*>* out) : out_(out) {}
MarkHeapReference(mirror::HeapReference<mirror::Object> * ref,bool do_atomic_update)102   void MarkHeapReference(mirror::HeapReference<mirror::Object>* ref,
103                          [[maybe_unused]] bool do_atomic_update) override
104       REQUIRES_SHARED(Locks::mutator_lock_) {
105     DCHECK(ref != nullptr);
106     MarkObject(ref->AsMirrorPtr());
107   }
MarkObject(mirror::Object * obj)108   mirror::Object* MarkObject(mirror::Object* obj) override
109       REQUIRES_SHARED(Locks::mutator_lock_) {
110     DCHECK(obj != nullptr);
111     out_->insert(obj);
112     return obj;
113   }
114 
115  private:
116   std::set<mirror::Object*>* const out_;
117 };
118 
119 // A mod union table that only holds references to a specified target space.
120 class ModUnionTableRefCacheToSpace : public ModUnionTableReferenceCache {
121  public:
ModUnionTableRefCacheToSpace(const std::string & name,Heap * heap,space::ContinuousSpace * space,space::ContinuousSpace * target_space)122   explicit ModUnionTableRefCacheToSpace(
123       const std::string& name, Heap* heap, space::ContinuousSpace* space,
124       space::ContinuousSpace* target_space)
125       : ModUnionTableReferenceCache(name, heap, space), target_space_(target_space) {}
126 
ShouldAddReference(const mirror::Object * ref) const127   bool ShouldAddReference(const mirror::Object* ref) const override {
128     return target_space_->HasAddress(ref);
129   }
130 
131  private:
132   space::ContinuousSpace* const target_space_;
133 };
134 
operator <<(std::ostream & oss,ModUnionTableFactory::TableType type)135 std::ostream& operator<<(std::ostream& oss, ModUnionTableFactory::TableType type) {
136   switch (type) {
137     case ModUnionTableFactory::kTableTypeCardCache: {
138       oss << "CardCache";
139       break;
140     }
141     case ModUnionTableFactory::kTableTypeReferenceCache: {
142       oss << "ReferenceCache";
143       break;
144     }
145     default: {
146       UNIMPLEMENTED(FATAL) << static_cast<size_t>(type);
147     }
148   }
149   return oss;
150 }
151 
Create(TableType type,space::ContinuousSpace * space,space::ContinuousSpace * target_space)152 ModUnionTable* ModUnionTableFactory::Create(
153     TableType type, space::ContinuousSpace* space, space::ContinuousSpace* target_space) {
154   std::ostringstream name;
155   name << "Mod union table: " << type;
156   switch (type) {
157     case kTableTypeCardCache: {
158       return new ModUnionTableCardCache(name.str(), Runtime::Current()->GetHeap(), space);
159     }
160     case kTableTypeReferenceCache: {
161       return new ModUnionTableRefCacheToSpace(name.str(), Runtime::Current()->GetHeap(), space,
162                                               target_space);
163     }
164     default: {
165       UNIMPLEMENTED(FATAL) << "Invalid type " << type;
166       UNREACHABLE();
167     }
168   }
169 }
170 
TEST_F(ModUnionTableTest,TestCardCache)171 TEST_F(ModUnionTableTest, TestCardCache) {
172   RunTest(ModUnionTableFactory::kTableTypeCardCache);
173 }
174 
TEST_F(ModUnionTableTest,TestReferenceCache)175 TEST_F(ModUnionTableTest, TestReferenceCache) {
176   RunTest(ModUnionTableFactory::kTableTypeReferenceCache);
177 }
178 
RunTest(ModUnionTableFactory::TableType type)179 void ModUnionTableTest::RunTest(ModUnionTableFactory::TableType type) {
180   Thread* const self = Thread::Current();
181   ScopedObjectAccess soa(self);
182   Runtime* const runtime = Runtime::Current();
183   gc::Heap* const heap = runtime->GetHeap();
184   // Use non moving space since moving GC don't necessarily have a primary free list space.
185   auto* space = heap->GetNonMovingSpace();
186   ResetClass();
187   // Create another space that we can put references in.
188   std::unique_ptr<space::DlMallocSpace> other_space(space::DlMallocSpace::Create(
189       "other space", 128 * KB, 4 * MB, 4 * MB, /*can_move_objects=*/ false));
190   ASSERT_TRUE(other_space.get() != nullptr);
191   {
192     ScopedThreadSuspension sts(self, ThreadState::kSuspended);
193     ScopedSuspendAll ssa("Add image space");
194     heap->AddSpace(other_space.get());
195   }
196   std::unique_ptr<ModUnionTable> table(ModUnionTableFactory::Create(
197       type, space, other_space.get()));
198   ASSERT_TRUE(table.get() != nullptr);
199   // Create some fake objects and put the main space and dirty cards in the non moving space.
200   auto* obj1 = AllocObjectArray(self, space, CardTable::kCardSize);
201   ASSERT_TRUE(obj1 != nullptr);
202   auto* obj2 = AllocObjectArray(self, space, CardTable::kCardSize);
203   ASSERT_TRUE(obj2 != nullptr);
204   auto* obj3 = AllocObjectArray(self, space, CardTable::kCardSize);
205   ASSERT_TRUE(obj3 != nullptr);
206   auto* obj4 = AllocObjectArray(self, space, CardTable::kCardSize);
207   ASSERT_TRUE(obj4 != nullptr);
208   // Dirty some cards.
209   obj1->Set(0, obj2);
210   obj2->Set(0, obj3);
211   obj3->Set(0, obj4);
212   obj4->Set(0, obj1);
213   // Dirty some more cards to objects in another space.
214   auto* other_space_ref1 = AllocObjectArray(self, other_space.get(), CardTable::kCardSize);
215   ASSERT_TRUE(other_space_ref1 != nullptr);
216   auto* other_space_ref2 = AllocObjectArray(self, other_space.get(), CardTable::kCardSize);
217   ASSERT_TRUE(other_space_ref2 != nullptr);
218   obj1->Set(1, other_space_ref1);
219   obj2->Set(3, other_space_ref2);
220   table->ProcessCards();
221   std::set<mirror::Object*> visited_before;
222   CollectVisitedVisitor collector_before(&visited_before);
223   table->UpdateAndMarkReferences(&collector_before);
224   // Check that we visited all the references in other spaces only.
225   ASSERT_GE(visited_before.size(), 2u);
226   ASSERT_TRUE(visited_before.find(other_space_ref1) != visited_before.end());
227   ASSERT_TRUE(visited_before.find(other_space_ref2) != visited_before.end());
228   // Verify that all the other references were visited.
229   // obj1, obj2 cards should still be in mod union table since they have references to other
230   // spaces.
231   ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj1)));
232   ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj2)));
233   // obj3, obj4 don't have a reference to any object in the other space, their cards should have
234   // been removed from the mod union table during UpdateAndMarkReferences.
235   ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj3)));
236   ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj4)));
237   {
238     // Currently no-op, make sure it still works however.
239     ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
240     table->Verify();
241   }
242   // Verify that dump doesn't crash.
243   std::ostringstream oss;
244   table->Dump(oss);
245   // Set all the cards, then verify.
246   table->SetCards();
247   // TODO: Check that the cards are actually set.
248   for (auto* ptr = space->Begin(); ptr < AlignUp(space->End(), CardTable::kCardSize);
249       ptr += CardTable::kCardSize) {
250     ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(ptr)));
251   }
252   // Visit again and make sure the cards got cleared back to their expected state.
253   std::set<mirror::Object*> visited_after;
254   CollectVisitedVisitor collector_after(&visited_after);
255   table->UpdateAndMarkReferences(&collector_after);
256   // Check that we visited a superset after.
257   for (auto* obj : visited_before) {
258     ASSERT_TRUE(visited_after.find(obj) != visited_after.end()) << obj;
259   }
260   // Verify that the dump still works.
261   std::ostringstream oss2;
262   table->Dump(oss2);
263   // Remove the space we added so it doesn't persist to the next test.
264   ScopedThreadSuspension sts(self, ThreadState::kSuspended);
265   ScopedSuspendAll ssa("Add image space");
266   heap->RemoveSpace(other_space.get());
267 }
268 
269 }  // namespace accounting
270 }  // namespace gc
271 }  // namespace art
272