1 /*
   2  * Copyright (c) 2018, 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 "runtime/os.hpp"
  27 #include "oops/oop.hpp"
  28 #include "utilities/align.hpp"
  29 #include "unittest.hpp"
  30 
  31 namespace {
  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       ASSERT_TRUE(os::release_memory_special(rs.base(), rs.size()));
  44     } else {
  45       ASSERT_TRUE(os::release_memory(rs.base(), rs.size()));
  46     }
  47   }
  48 
  49   // have to use these functions, as gtest's _PRED macros don't like is_aligned
  50   // nor (is_aligned<size_t, size_t>)
  51   static bool is_size_alignment(size_t size, size_t alignment) {
  52     return is_aligned(size, alignment);
  53   }
  54   static bool is_ptr_alignment(void* ptr, size_t alignment) {
  55     return is_aligned(ptr, alignment);
  56   }
  57 
  58   static void test_reserved_size(size_t size) {
  59     ASSERT_PRED2(is_size_alignment, size, os::vm_allocation_granularity());
  60 
  61     ReservedSpace rs(size);
  62 
  63     EXPECT_TRUE(rs.base() != NULL) << "rs.special: " << rs.special();
  64     EXPECT_EQ(size, rs.size()) << "rs.special: " << rs.special();
  65 
  66     if (rs.special()) {
  67       small_page_write(rs.base(), size);
  68     }
  69 
  70     release_memory_for_test(rs);
  71   }
  72 
  73   static void test_reserved_size_alignment(size_t size, size_t alignment) {
  74     ASSERT_PRED2(is_size_alignment, size, alignment) << "Incorrect input parameters";
  75 
  76     ReservedSpace rs(size, alignment, UseLargePages, (char *) NULL);
  77 
  78     ASSERT_TRUE(rs.base() != NULL) << "rs.special = " << rs.special();
  79     ASSERT_EQ(size, rs.size()) << "rs.special = " << rs.special();
  80 
  81     EXPECT_PRED2(is_ptr_alignment, rs.base(), alignment)
  82             << "aligned sizes should always give aligned addresses";
  83     EXPECT_PRED2(is_ptr_alignment, (void*) rs.size(), alignment)
  84             << "aligned sizes should always give aligned addresses";
  85 
  86     if (rs.special()) {
  87       small_page_write(rs.base(), size);
  88     }
  89 
  90     release_memory_for_test(rs);
  91   }
  92 
  93   static void test_reserved_size_alignment_page_type(size_t size, size_t alignment, bool maybe_large) {
  94     if (size < alignment) {
  95       // Tests might set -XX:LargePageSizeInBytes=<small pages> and cause unexpected input arguments for this test.
  96       ASSERT_EQ((size_t) os::vm_page_size(), os::large_page_size()) << "Test needs further refinement";
  97       return;
  98     }
  99 
 100     ASSERT_PRED2(is_size_alignment, size, os::vm_allocation_granularity()) << "Must be at least AG aligned";
 101     ASSERT_PRED2(is_size_alignment, size, alignment) << "Must be at least AG aligned";
 102 
 103     bool large = maybe_large && UseLargePages && size >= os::large_page_size();
 104 
 105     ReservedSpace rs(size, alignment, large, false);
 106 
 107     EXPECT_TRUE(rs.base() != NULL) << "rs.special: " << rs.special();
 108     EXPECT_EQ(size, rs.size()) << "rs.special: " << rs.special();
 109 
 110     if (rs.special()) {
 111       small_page_write(rs.base(), size);
 112     }
 113 
 114     release_memory_for_test(rs);
 115   }
 116 }
 117 
 118 TEST_VM(ReservedSpace, size_alignment) {
 119   size_t size = 2 * 1024 * 1024;
 120   size_t ag   = os::vm_allocation_granularity();
 121 
 122   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment(size,      ag));
 123   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment(size * 2,  ag));
 124   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment(size * 10, ag));
 125 }
 126 
 127 TEST_VM(ReservedSpace, size) {
 128   size_t size = 2 * 1024 * 1024;
 129   size_t ag = os::vm_allocation_granularity();
 130 
 131   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 1));
 132   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 2));
 133   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 10));
 134   EXPECT_NO_FATAL_FAILURE(test_reserved_size(ag));
 135   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size - ag));
 136   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size));
 137   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size + ag));
 138   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 2));
 139   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 2 - ag));
 140   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 2 + ag));
 141   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 3));
 142   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 3 - ag));
 143   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 3 + ag));
 144   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 10));
 145   EXPECT_NO_FATAL_FAILURE(test_reserved_size(size * 10 + size / 2));
 146 }
 147 
 148 TEST_VM(ReservedSpace, size_alignment_page_type) {
 149   size_t ag = os::vm_allocation_granularity();
 150 
 151   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag,      ag    , false));
 152   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 2,  ag    , false));
 153   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 3,  ag    , false));
 154   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 2,  ag * 2, false));
 155   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 4,  ag * 2, false));
 156   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 8,  ag * 2, false));
 157   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 4,  ag * 4, false));
 158   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 8,  ag * 4, false));
 159   EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(ag * 16, ag * 4, false));
 160 
 161   if (UseLargePages) {
 162     size_t lp = os::large_page_size();
 163 
 164     // Without large pages
 165     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp,     ag * 4, false));
 166     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, ag * 4, false));
 167     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 4, ag * 4, false));
 168     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp,     lp    , false));
 169     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, lp    , false));
 170     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 3, lp    , false));
 171     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, lp * 2, false));
 172     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 4, lp * 2, false));
 173     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 8, lp * 2, false));
 174 
 175     // With large pages
 176     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp, ag * 4    , true));
 177     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, ag * 4, true));
 178     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 4, ag * 4, true));
 179     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp, lp        , true));
 180     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, lp    , true));
 181     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 3, lp    , true));
 182     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 2, lp * 2, true));
 183     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 4, lp * 2, true));
 184     EXPECT_NO_FATAL_FAILURE(test_reserved_size_alignment_page_type(lp * 8, lp * 2, true));
 185   }
 186 }
 187 
 188 namespace {
 189   enum TestLargePages {
 190     Default,
 191     Disable,
 192     Reserve,
 193     Commit
 194   };
 195 
 196   ReservedSpace reserve_memory(size_t reserve_size_aligned, TestLargePages mode) {
 197     switch(mode) {
 198       default:
 199       case Default:
 200       case Reserve:
 201         return ReservedSpace(reserve_size_aligned);
 202       case Disable:
 203       case Commit:
 204         return ReservedSpace(reserve_size_aligned,
 205                              os::vm_allocation_granularity(),
 206                              /* large */ false, /* exec */ false);
 207     }
 208   }
 209 
 210   bool initialize_virtual_space(VirtualSpace& vs, ReservedSpace rs, TestLargePages mode) {
 211     switch(mode) {
 212       default:
 213       case Default:
 214       case Reserve:
 215         return vs.initialize(rs, 0);
 216       case Disable:
 217         return vs.initialize_with_granularity(rs, 0, os::vm_page_size());
 218       case Commit:
 219         return vs.initialize_with_granularity(rs, 0, os::page_size_for_region_unaligned(rs.size(), 1));
 220     }
 221   }
 222 
 223  void test_virtual_space_actual_committed_space(size_t reserve_size, size_t commit_size,
 224                                                 TestLargePages mode = Default) {
 225     size_t granularity = os::vm_allocation_granularity();
 226     size_t reserve_size_aligned = align_up(reserve_size, granularity);
 227 
 228     ReservedSpace reserved = reserve_memory(reserve_size_aligned, mode);
 229 
 230     ASSERT_TRUE(reserved.is_reserved());
 231 
 232     VirtualSpace vs;
 233     ASSERT_TRUE(initialize_virtual_space(vs, reserved, mode)) << "Failed to initialize VirtualSpace";
 234     vs.expand_by(commit_size, false);
 235 
 236     if (vs.special()) {
 237       EXPECT_EQ(reserve_size_aligned, vs.actual_committed_size());
 238     } else {
 239       EXPECT_GE(vs.actual_committed_size(), commit_size);
 240       // Approximate the commit granularity.
 241       // Make sure that we don't commit using large pages
 242       // if large pages has been disabled for this VirtualSpace.
 243       size_t commit_granularity = (mode == Disable || !UseLargePages) ?
 244                                    os::vm_page_size() : os::large_page_size();
 245       EXPECT_LT(vs.actual_committed_size(), commit_size + commit_granularity);
 246     }
 247 
 248     reserved.release();
 249   }
 250 };
 251 
 252 TEST_VM(VirtualSpace, actual_committed_space) {
 253   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(4 * K,  0));
 254   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(4 * K,  4 * K));
 255   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K,  0));
 256   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K,  4 * K));
 257   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K,  8 * K));
 258   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 0));
 259   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 4 * K));
 260   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 8 * K));
 261   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 12 * K));
 262   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 0));
 263   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 32 * K));
 264   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 64 * K));
 265   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  0));
 266   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  4 * K));
 267   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  64 * K));
 268   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  1 * M));
 269   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  2 * M));
 270   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0));
 271   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K));
 272   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K));
 273   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M));
 274   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M));
 275   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M));
 276   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M));
 277 }
 278 
 279 TEST_VM(VirtualSpace, actual_committed_space_one_large_page) {
 280   if (!UseLargePages) {
 281     return;
 282   }
 283 
 284   size_t large_page_size = os::large_page_size();
 285 
 286   ReservedSpace reserved(large_page_size, large_page_size, true, false);
 287   ASSERT_TRUE(reserved.is_reserved());
 288 
 289   VirtualSpace vs;
 290   ASSERT_TRUE(vs.initialize(reserved, 0)) << "Failed to initialize VirtualSpace";
 291   vs.expand_by(large_page_size, false);
 292 
 293   EXPECT_EQ(large_page_size, vs.actual_committed_size());
 294 
 295   reserved.release();
 296 }
 297 
 298 TEST_VM(VirtualSpace, disable_large_pages) {
 299   if (!UseLargePages) {
 300     return;
 301   }
 302   // These test cases verify that if we force VirtualSpace to disable large pages
 303   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0,      Disable));
 304   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K,  Disable));
 305   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K,  Disable));
 306   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M,  Disable));
 307   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M,  Disable));
 308   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M,  Disable));
 309   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Disable));
 310 
 311   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0,      Reserve));
 312   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K,  Reserve));
 313   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K,  Reserve));
 314   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M,  Reserve));
 315   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M,  Reserve));
 316   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M,  Reserve));
 317   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Reserve));
 318 
 319   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0,      Commit));
 320   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K,  Commit));
 321   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K,  Commit));
 322   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M,  Commit));
 323   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M,  Commit));
 324   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M,  Commit));
 325   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Commit));
 326 }