namespace art HIDDEN { namespace gc { namespace accounting {
class ModUnionTableFactory { public: enum TableType {
kTableTypeCardCache,
kTableTypeReferenceCache,
kTableTypeCount, // Number of values in the enum.
};
// Target space is ignored for the card cache implementation. static ModUnionTable* Create(
TableType type, space::ContinuousSpace* space, space::ContinuousSpace* target_space);
};
private:
mirror::Class* GetObjectArrayClass(Thread* self, space::ContinuousMemMapAllocSpace* space)
REQUIRES_SHARED(Locks::mutator_lock_) { if (java_lang_object_array_ == nullptr) {
java_lang_object_array_ = GetClassRoot<mirror::ObjectArray<mirror::Object>>().Ptr(); // Since the test doesn't have an image, the class of the object array keeps cards live // inside the card cache mod-union table and causes the check // ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj3))); // to fail since the class ends up keeping the card dirty. To get around this, we make a fake // copy of the class in the same space that we are allocating in.
DCHECK(java_lang_object_array_ != nullptr); const size_t class_size = java_lang_object_array_->GetClassSize();
size_t bytes_allocated = 0, bytes_tl_bulk_allocated; auto* klass = down_cast<mirror::Class*>(space->Alloc(self, class_size, &bytes_allocated,
nullptr,
&bytes_tl_bulk_allocated));
DCHECK(klass != nullptr);
memcpy(klass, java_lang_object_array_, class_size);
Runtime::Current()->GetHeap()->GetCardTable()->MarkCard(klass);
java_lang_object_array_ = klass;
} return java_lang_object_array_;
}
mirror::Class* java_lang_object_array_;
};
// A mod union table that only holds references to a specified target space. class ModUnionTableRefCacheToSpace : public ModUnionTableReferenceCache { public: explicit ModUnionTableRefCacheToSpace( const std::string& name, Heap* heap, space::ContinuousSpace* space,
space::ContinuousSpace* target_space)
: ModUnionTableReferenceCache(name, heap, space), target_space_(target_space) {}
void ModUnionTableTest::RunTest(ModUnionTableFactory::TableType type) {
Thread* const self = Thread::Current();
ScopedObjectAccess soa(self);
Runtime* const runtime = Runtime::Current();
gc::Heap* const heap = runtime->GetHeap(); // Use non moving space since moving GC don't necessarily have a primary free list space. auto* space = heap->GetNonMovingSpace();
ResetClass(); // Create another space that we can put references in.
std::unique_ptr<space::DlMallocSpace> other_space(space::DlMallocSpace::Create( "other space", 128 * KB, 4 * MB, 4 * MB, /*can_move_objects=*/ false));
ASSERT_TRUE(other_space.get() != nullptr);
{
ScopedThreadSuspension sts(self, ThreadState::kSuspended);
ScopedSuspendAll ssa("Add image space");
heap->AddSpace(other_space.get());
}
std::unique_ptr<ModUnionTable> table(ModUnionTableFactory::Create(
type, space, other_space.get()));
ASSERT_TRUE(table.get() != nullptr); // Create some fake objects and put the main space and dirty cards in the non moving space. auto* obj1 = AllocObjectArray(self, space, CardTable::kCardSize);
ASSERT_TRUE(obj1 != nullptr); auto* obj2 = AllocObjectArray(self, space, CardTable::kCardSize);
ASSERT_TRUE(obj2 != nullptr); auto* obj3 = AllocObjectArray(self, space, CardTable::kCardSize);
ASSERT_TRUE(obj3 != nullptr); auto* obj4 = AllocObjectArray(self, space, CardTable::kCardSize);
ASSERT_TRUE(obj4 != nullptr); // Dirty some cards.
obj1->Set(0, obj2);
obj2->Set(0, obj3);
obj3->Set(0, obj4);
obj4->Set(0, obj1); // Dirty some more cards to objects in another space. auto* other_space_ref1 = AllocObjectArray(self, other_space.get(), CardTable::kCardSize);
ASSERT_TRUE(other_space_ref1 != nullptr); auto* other_space_ref2 = AllocObjectArray(self, other_space.get(), CardTable::kCardSize);
ASSERT_TRUE(other_space_ref2 != nullptr);
obj1->Set(1, other_space_ref1);
obj2->Set(3, other_space_ref2);
table->ProcessCards();
std::set<mirror::Object*> visited_before;
CollectVisitedVisitor collector_before(&visited_before);
table->UpdateAndMarkReferences(&collector_before); // Check that we visited all the references in other spaces only.
ASSERT_GE(visited_before.size(), 2u);
ASSERT_TRUE(visited_before.find(other_space_ref1) != visited_before.end());
ASSERT_TRUE(visited_before.find(other_space_ref2) != visited_before.end()); // Verify that all the other references were visited. // obj1, obj2 cards should still be in mod union table since they have references to other // spaces.
ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj1)));
ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj2))); // obj3, obj4 don't have a reference to any object in the other space, their cards should have // been removed from the mod union table during UpdateAndMarkReferences.
ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj3)));
ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj4)));
{ // Currently no-op, make sure it still works however.
ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
table->Verify();
} // Verify that dump doesn't crash.
std::ostringstream oss;
table->Dump(oss); // Set all the cards, then verify.
table->SetCards(); // TODO: Check that the cards are actually set. for (auto* ptr = space->Begin(); ptr < AlignUp(space->End(), CardTable::kCardSize);
ptr += CardTable::kCardSize) {
ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(ptr)));
} // Visit again and make sure the cards got cleared back to their expected state.
std::set<mirror::Object*> visited_after;
CollectVisitedVisitor collector_after(&visited_after);
table->UpdateAndMarkReferences(&collector_after); // Check that we visited a superset after. for (auto* obj : visited_before) {
ASSERT_TRUE(visited_after.find(obj) != visited_after.end()) << obj;
} // Verify that the dump still works.
std::ostringstream oss2;
table->Dump(oss2); // Remove the space we added so it doesn't persist to the next test.
ScopedThreadSuspension sts(self, ThreadState::kSuspended);
ScopedSuspendAll ssa("Add image space");
heap->RemoveSpace(other_space.get());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.