1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/z/zAddress.inline.hpp"
  26 #include "gc/z/zForwarding.inline.hpp"
  27 #include "gc/z/zGlobals.hpp"
  28 #include "gc/z/zPage.inline.hpp"
  29 #include "unittest.hpp"
  30 
  31 using namespace testing;
  32 
  33 #define CAPTURE_DELIM "\n"
  34 #define CAPTURE1(expression) #expression << " evaluates to " << expression
  35 #define CAPTURE2(e0, e1)                 CAPTURE1(e0) << CAPTURE_DELIM << CAPTURE1(e1)
  36 
  37 #define CAPTURE(expression) CAPTURE1(expression)
  38 
  39 class ZForwardingTest : public Test {
  40 public:
  41   // Helper functions
  42 
  43   static bool is_power_of_2(uint32_t value) {
  44     return ::is_power_of_2((intptr_t)value);
  45   }
  46 
  47   class SequenceToFromIndex : AllStatic {
  48   public:
  49     static uintptr_t even(uint32_t sequence_number) {
  50       return sequence_number * 2;
  51     }
  52     static uintptr_t odd(uint32_t sequence_number) {
  53       return even(sequence_number) + 1;
  54     }
  55     static uintptr_t one_to_one(uint32_t sequence_number) {
  56       return sequence_number;
  57     }
  58   };
  59 
  60   // Test functions
  61 
  62   static void setup(ZForwarding* forwarding) {
  63     EXPECT_PRED1(is_power_of_2, forwarding->_entries.length()) << CAPTURE(forwarding->_entries.length());
  64   }
  65 
  66   static void find_empty(ZForwarding* forwarding) {
  67     uint32_t size = forwarding->_entries.length();
  68     uint32_t entries_to_check = size * 2;
  69 
  70     for (uint32_t i = 0; i < entries_to_check; i++) {
  71       uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
  72 
  73       EXPECT_TRUE(forwarding->find(from_index).is_empty()) << CAPTURE2(from_index, size);
  74     }
  75 
  76     EXPECT_TRUE(forwarding->find(uintptr_t(-1)).is_empty()) << CAPTURE(size);
  77   }
  78 
  79   static void find_full(ZForwarding* forwarding) {
  80     uint32_t size = forwarding->_entries.length();
  81     uint32_t entries_to_populate = size;
  82 
  83     // Populate
  84     for (uint32_t i = 0; i < entries_to_populate; i++) {
  85       uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
  86 
  87       ZForwardingCursor cursor;
  88       ZForwardingEntry entry = forwarding->find(from_index, &cursor);
  89       ASSERT_TRUE(entry.is_empty()) << CAPTURE2(from_index, size);
  90 
  91       forwarding->insert(from_index, from_index, &cursor);
  92     }
  93 
  94     // Verify
  95     for (uint32_t i = 0; i < entries_to_populate; i++) {
  96       uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
  97 
  98       ZForwardingEntry entry = forwarding->find(from_index);
  99       ASSERT_FALSE(entry.is_empty()) << CAPTURE2(from_index, size);
 100 
 101       ASSERT_EQ(entry.from_index(), from_index) << CAPTURE(size);
 102       ASSERT_EQ(entry.to_offset(), from_index) << CAPTURE(size);
 103     }
 104   }
 105 
 106   static void find_every_other(ZForwarding* forwarding) {
 107     uint32_t size = forwarding->_entries.length();
 108     uint32_t entries_to_populate = size / 2;
 109 
 110     // Populate even from indices
 111     for (uint32_t i = 0; i < entries_to_populate; i++) {
 112       uintptr_t from_index = SequenceToFromIndex::even(i);
 113 
 114       ZForwardingCursor cursor;
 115       ZForwardingEntry entry = forwarding->find(from_index, &cursor);
 116       ASSERT_TRUE(entry.is_empty()) << CAPTURE2(from_index, size);
 117 
 118       forwarding->insert(from_index, from_index, &cursor);
 119     }
 120 
 121     // Verify populated even indices
 122     for (uint32_t i = 0; i < entries_to_populate; i++) {
 123       uintptr_t from_index = SequenceToFromIndex::even(i);
 124 
 125       ZForwardingCursor cursor;
 126       ZForwardingEntry entry = forwarding->find(from_index, &cursor);
 127       ASSERT_FALSE(entry.is_empty()) << CAPTURE2(from_index, size);
 128 
 129       ASSERT_EQ(entry.from_index(), from_index) << CAPTURE(size);
 130       ASSERT_EQ(entry.to_offset(), from_index) << CAPTURE(size);
 131     }
 132 
 133     // Verify empty odd indices
 134     //
 135     // This check could be done on a larger range of sequence numbers,
 136     // but currently entries_to_populate is used.
 137     for (uint32_t i = 0; i < entries_to_populate; i++) {
 138       uintptr_t from_index = SequenceToFromIndex::odd(i);
 139 
 140       ZForwardingEntry entry = forwarding->find(from_index);
 141 
 142       ASSERT_TRUE(entry.is_empty()) << CAPTURE2(from_index, size);
 143     }
 144   }
 145 
 146   static void test(void (*function)(ZForwarding*), uint32_t size) {
 147     // Create page
 148     const ZVirtualMemory vmem(0, ZPageSizeSmall);
 149     const ZPhysicalMemory pmem(ZPhysicalMemorySegment(0, ZPageSizeSmall));
 150     ZPage page(ZPageTypeSmall, vmem, pmem);
 151 
 152     page.reset();
 153 
 154     const size_t object_size = 16;
 155     const uintptr_t object = page.alloc_object(object_size);
 156 
 157     ZGlobalSeqNum++;
 158 
 159     bool dummy = false;
 160     page.mark_object(ZAddress::marked(object), dummy, dummy);
 161 
 162     const uint32_t live_objects = size;
 163     const uint32_t live_bytes = live_objects * object_size;
 164     page.inc_live_atomic(live_objects, live_bytes);
 165 
 166     // Setup forwarding
 167     ZForwarding* const forwarding = ZForwarding::create(&page);
 168 
 169     // Actual test function
 170     (*function)(forwarding);
 171 
 172     // Teardown forwarding
 173     ZForwarding::destroy(forwarding);
 174 
 175     // Teardown page
 176     page.physical_memory().clear();
 177   }
 178 
 179   // Run the given function with a few different input values.
 180   static void test(void (*function)(ZForwarding*)) {
 181     test(function, 1);
 182     test(function, 2);
 183     test(function, 3);
 184     test(function, 4);
 185     test(function, 7);
 186     test(function, 8);
 187     test(function, 1023);
 188     test(function, 1024);
 189     test(function, 1025);
 190   }
 191 };
 192 
 193 TEST_F(ZForwardingTest, setup) {
 194   test(&ZForwardingTest::setup);
 195 }
 196 
 197 TEST_F(ZForwardingTest, find_empty) {
 198   test(&ZForwardingTest::find_empty);
 199 }
 200 
 201 TEST_F(ZForwardingTest, find_full) {
 202   test(&ZForwardingTest::find_full);
 203 }
 204 
 205 TEST_F(ZForwardingTest, find_every_other) {
 206   test(&ZForwardingTest::find_every_other);
 207 }