1 /*
   2  * Copyright (c) 2016, 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 "memory/virtualspace.hpp"
  26 #include "oops/oop.inline.hpp"
  27 #include "unittest.hpp"
  28 
  29 class ReservedSpaceTest {
  30  public:
  31 
  32   static void small_page_write(void* addr, size_t size) {
  33     size_t page_size = os::vm_page_size();
  34 
  35     char* end = (char*) addr + size;
  36     for (char* p = (char*) addr; p < end; p += page_size) {
  37       *p = 1;
  38     }
  39   }
  40 
  41   static void release_memory_for_test(ReservedSpace rs) {
  42     if (rs.special()) {
  43       EXPECT_TRUE(os::release_memory_special(rs.base(), rs.size()));
  44     } else {
  45       EXPECT_TRUE(os::release_memory(rs.base(), rs.size()));
  46     }
  47   }
  48 
  49   static void test_reserved_size_alignment(size_t size, size_t alignment) {
  50     ASSERT_TRUE(is_size_aligned(size, alignment))
  51             << "Incorrect input parameters: size " << size
  52             << ", alignment " << alignment;
  53 
  54     ReservedSpace rs(size, alignment, UseLargePages, (char *) NULL);
  55 
  56     ASSERT_TRUE(rs.base() != NULL) << "rs.special = " << rs.special();
  57     ASSERT_EQ(size, rs.size()) << "rs.special = " << rs.special();
  58 
  59     EXPECT_PRED2(is_ptr_aligned, rs.base(), alignment)
  60             << "aligned sizes should always give aligned addresses";
  61     EXPECT_PRED2(is_ptr_aligned, (void*) rs.size(), alignment)
  62             << "aligned sizes should always give aligned addresses";
  63 
  64     if (rs.special()) {
  65       small_page_write(rs.base(), size);
  66     }
  67 
  68     release_memory_for_test(rs);
  69   }
  70 
  71   static void test_reserved_size(size_t size) {
  72     ASSERT_PRED2(is_size_aligned, size, os::vm_allocation_granularity())
  73             << "Must be at least AG aligned";
  74 
  75     ReservedSpace rs(size);
  76 
  77     EXPECT_TRUE(rs.base() != NULL) << "rs.special: " << rs.special();
  78     EXPECT_EQ(size, rs.size()) << "rs.special: " << rs.special();
  79 
  80     if (rs.special()) {
  81       small_page_write(rs.base(), size);
  82     }
  83 
  84     release_memory_for_test(rs);
  85   }
  86 
  87   static void test_reserved_size_alignment_page_type(size_t size, size_t alignment, bool maybe_large) {
  88     if (size < alignment) {
  89       // Tests might set -XX:LargePageSizeInBytes=<small pages> and cause unexpected input arguments for this test.
  90       ASSERT_EQ((size_t) os::vm_page_size(), os::large_page_size()) << "Test needs further refinement";
  91     }
  92 
  93     ASSERT_PRED2(is_size_aligned, size, os::vm_allocation_granularity()) << "Must be at least AG aligned";
  94     ASSERT_PRED2(is_size_aligned, size, alignment) << "Must be at least AG aligned";
  95 
  96     bool large = maybe_large && UseLargePages && size >= os::large_page_size();
  97 
  98     ReservedSpace rs(size, alignment, large, false);
  99 
 100     EXPECT_TRUE(rs.base() != NULL) << "rs.special: " << rs.special();
 101     EXPECT_EQ(size, rs.size()) << "rs.special: " << rs.special();
 102 
 103     if (rs.special()) {
 104       small_page_write(rs.base(), size);
 105     }
 106 
 107     release_memory_for_test(rs);
 108   }
 109 };
 110 
 111 TEST(ReservedSpace, size_alignment) {
 112     size_t size = 2 * 1024 * 1024;
 113     size_t ag   = os::vm_allocation_granularity();
 114 
 115     ReservedSpaceTest::test_reserved_size_alignment(size,      ag);
 116     ReservedSpaceTest::test_reserved_size_alignment(size * 2,  ag);
 117     ReservedSpaceTest::test_reserved_size_alignment(size * 10, ag);
 118 }
 119 
 120 TEST(ReservedSpace, size) {
 121 
 122     size_t size = 2 * 1024 * 1024;
 123     size_t ag = os::vm_allocation_granularity();
 124 
 125     ReservedSpaceTest::test_reserved_size(size * 1);
 126     ReservedSpaceTest::test_reserved_size(size * 2);
 127     ReservedSpaceTest::test_reserved_size(size * 10);
 128     ReservedSpaceTest::test_reserved_size(ag);
 129     ReservedSpaceTest::test_reserved_size(size - ag);
 130     ReservedSpaceTest::test_reserved_size(size);
 131     ReservedSpaceTest::test_reserved_size(size + ag);
 132     ReservedSpaceTest::test_reserved_size(size * 2);
 133     ReservedSpaceTest::test_reserved_size(size * 2 - ag);
 134     ReservedSpaceTest::test_reserved_size(size * 2 + ag);
 135     ReservedSpaceTest::test_reserved_size(size * 3);
 136     ReservedSpaceTest::test_reserved_size(size * 3 - ag);
 137     ReservedSpaceTest::test_reserved_size(size * 3 + ag);
 138     ReservedSpaceTest::test_reserved_size(size * 10);
 139     ReservedSpaceTest::test_reserved_size(size * 10 + size / 2);
 140   }
 141 
 142 
 143 TEST(ReservedSpace, size_alignment_page_type) {
 144     size_t ag = os::vm_allocation_granularity();
 145 
 146     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag,      ag    , false);
 147     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag * 2,  ag    , false);
 148     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag * 3,  ag    , false);
 149     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag * 2,  ag * 2, false);
 150     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag * 4,  ag * 2, false);
 151     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag * 8,  ag * 2, false);
 152     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag * 4,  ag * 4, false);
 153     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag * 8,  ag * 4, false);
 154     ReservedSpaceTest::test_reserved_size_alignment_page_type(ag * 16, ag * 4, false);
 155 
 156     if (UseLargePages) {
 157       size_t lp = os::large_page_size();
 158 
 159       // Without large pages
 160       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp,     ag * 4, false);
 161       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 2, ag * 4, false);
 162       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 4, ag * 4, false);
 163       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp,     lp    , false);
 164       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 2, lp    , false);
 165       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 3, lp    , false);
 166       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 2, lp * 2, false);
 167       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 4, lp * 2, false);
 168       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 8, lp * 2, false);
 169 
 170       // With large pages
 171       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp, ag * 4    , true);
 172       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 2, ag * 4, true);
 173       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 4, ag * 4, true);
 174       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp, lp        , true);
 175       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 2, lp    , true);
 176       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 3, lp    , true);
 177       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 2, lp * 2, true);
 178       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 4, lp * 2, true);
 179       ReservedSpaceTest::test_reserved_size_alignment_page_type(lp * 8, lp * 2, true);
 180     }
 181 }