Path: blob/master/test/hotspot/gtest/gc/z/test_zForwarding.cpp
41149 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "gc/z/zAddress.inline.hpp"25#include "gc/z/zForwarding.inline.hpp"26#include "gc/z/zForwardingAllocator.inline.hpp"27#include "gc/z/zGlobals.hpp"28#include "gc/z/zPage.inline.hpp"29#include "unittest.hpp"3031using namespace testing;3233#define CAPTURE_DELIM "\n"34#define CAPTURE1(expression) #expression << " evaluates to " << expression35#define CAPTURE2(e0, e1) CAPTURE1(e0) << CAPTURE_DELIM << CAPTURE1(e1)3637#define CAPTURE(expression) CAPTURE1(expression)3839class ZForwardingTest : public Test {40public:41// Helper functions4243class SequenceToFromIndex : AllStatic {44public:45static uintptr_t even(size_t sequence_number) {46return sequence_number * 2;47}48static uintptr_t odd(size_t sequence_number) {49return even(sequence_number) + 1;50}51static uintptr_t one_to_one(size_t sequence_number) {52return sequence_number;53}54};5556// Test functions5758static void setup(ZForwarding* forwarding) {59EXPECT_PRED1(is_power_of_2<size_t>, forwarding->_entries.length()) << CAPTURE(forwarding->_entries.length());60}6162static void find_empty(ZForwarding* forwarding) {63size_t size = forwarding->_entries.length();64size_t entries_to_check = size * 2;6566for (size_t i = 0; i < entries_to_check; i++) {67uintptr_t from_index = SequenceToFromIndex::one_to_one(i);6869ZForwardingCursor cursor;70ZForwardingEntry entry = forwarding->find(from_index, &cursor);71EXPECT_FALSE(entry.populated()) << CAPTURE2(from_index, size);72}73}7475static void find_full(ZForwarding* forwarding) {76size_t size = forwarding->_entries.length();77size_t entries_to_populate = size;7879// Populate80for (size_t i = 0; i < entries_to_populate; i++) {81uintptr_t from_index = SequenceToFromIndex::one_to_one(i);8283ZForwardingCursor cursor;84ZForwardingEntry entry = forwarding->find(from_index, &cursor);85ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);8687forwarding->insert(from_index, from_index, &cursor);88}8990// Verify91for (size_t i = 0; i < entries_to_populate; i++) {92uintptr_t from_index = SequenceToFromIndex::one_to_one(i);9394ZForwardingCursor cursor;95ZForwardingEntry entry = forwarding->find(from_index, &cursor);96ASSERT_TRUE(entry.populated()) << CAPTURE2(from_index, size);9798ASSERT_EQ(entry.from_index(), from_index) << CAPTURE(size);99ASSERT_EQ(entry.to_offset(), from_index) << CAPTURE(size);100}101}102103static void find_every_other(ZForwarding* forwarding) {104size_t size = forwarding->_entries.length();105size_t entries_to_populate = size / 2;106107// Populate even from indices108for (size_t i = 0; i < entries_to_populate; i++) {109uintptr_t from_index = SequenceToFromIndex::even(i);110111ZForwardingCursor cursor;112ZForwardingEntry entry = forwarding->find(from_index, &cursor);113ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);114115forwarding->insert(from_index, from_index, &cursor);116}117118// Verify populated even indices119for (size_t i = 0; i < entries_to_populate; i++) {120uintptr_t from_index = SequenceToFromIndex::even(i);121122ZForwardingCursor cursor;123ZForwardingEntry entry = forwarding->find(from_index, &cursor);124ASSERT_TRUE(entry.populated()) << CAPTURE2(from_index, size);125126ASSERT_EQ(entry.from_index(), from_index) << CAPTURE(size);127ASSERT_EQ(entry.to_offset(), from_index) << CAPTURE(size);128}129130// Verify empty odd indices131//132// This check could be done on a larger range of sequence numbers,133// but currently entries_to_populate is used.134for (size_t i = 0; i < entries_to_populate; i++) {135uintptr_t from_index = SequenceToFromIndex::odd(i);136137ZForwardingCursor cursor;138ZForwardingEntry entry = forwarding->find(from_index, &cursor);139140ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);141}142}143144static void test(void (*function)(ZForwarding*), uint32_t size) {145// Create page146const ZVirtualMemory vmem(0, ZPageSizeSmall);147const ZPhysicalMemory pmem(ZPhysicalMemorySegment(0, ZPageSizeSmall, true));148ZPage page(ZPageTypeSmall, vmem, pmem);149150page.reset();151152const size_t object_size = 16;153const uintptr_t object = page.alloc_object(object_size);154155ZGlobalSeqNum++;156157bool dummy = false;158page.mark_object(ZAddress::marked(object), dummy, dummy);159160const uint32_t live_objects = size;161const size_t live_bytes = live_objects * object_size;162page.inc_live(live_objects, live_bytes);163164// Setup allocator165ZForwardingAllocator allocator;166const uint32_t nentries = ZForwarding::nentries(&page);167allocator.reset((sizeof(ZForwarding)) + (nentries * sizeof(ZForwardingEntry)));168169// Setup forwarding170ZForwarding* const forwarding = ZForwarding::alloc(&allocator, &page);171172// Actual test function173(*function)(forwarding);174}175176// Run the given function with a few different input values.177static void test(void (*function)(ZForwarding*)) {178test(function, 1);179test(function, 2);180test(function, 3);181test(function, 4);182test(function, 7);183test(function, 8);184test(function, 1023);185test(function, 1024);186test(function, 1025);187}188};189190TEST_F(ZForwardingTest, setup) {191test(&ZForwardingTest::setup);192}193194TEST_F(ZForwardingTest, find_empty) {195test(&ZForwardingTest::find_empty);196}197198TEST_F(ZForwardingTest, find_full) {199test(&ZForwardingTest::find_full);200}201202TEST_F(ZForwardingTest, find_every_other) {203test(&ZForwardingTest::find_every_other);204}205206207