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