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