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   class ReservedSpaceReleaser {
 197     ReservedSpace* const _rs;
 198    public:
 199     ReservedSpaceReleaser(ReservedSpace* rs) : _rs(rs) { }
 200     ~ReservedSpaceReleaser() {
 201       _rs->release();
 202     }
 203   };
 204 
 205   ReservedSpace reserve_memory(size_t reserve_size_aligned, TestLargePages mode) {
 206     switch(mode) {
 207       default:
 208       case Default:
 209       case Reserve:
 210         return ReservedSpace(reserve_size_aligned);
 211       case Disable:
 212       case Commit:
 213         return ReservedSpace(reserve_size_aligned,
 214                              os::vm_allocation_granularity(),
 215                              /* large */ false, /* exec */ false);
 216     }
 217   }
 218 
 219   bool initialize_virtual_space(VirtualSpace& vs, ReservedSpace rs, TestLargePages mode) {
 220     switch(mode) {
 221       default:
 222       case Default:
 223       case Reserve:
 224         return vs.initialize(rs, 0);
 225       case Disable:
 226         return vs.initialize_with_granularity(rs, 0, os::vm_page_size());
 227       case Commit:
 228         return vs.initialize_with_granularity(rs, 0, os::page_size_for_region_unaligned(rs.size(), 1));
 229     }
 230   }
 231 
 232  void test_virtual_space_actual_committed_space(size_t reserve_size, size_t commit_size,
 233                                                 TestLargePages mode = Default) {
 234     size_t granularity = os::vm_allocation_granularity();
 235     size_t reserve_size_aligned = align_up(reserve_size, granularity);
 236 
 237     ReservedSpace reserved = reserve_memory(reserve_size_aligned, mode);
 238     ReservedSpaceReleaser releaser(&reserved);
 239 
 240     ASSERT_TRUE(reserved.is_reserved());
 241 
 242     VirtualSpace vs;
 243     ASSERT_TRUE(initialize_virtual_space(vs, reserved, mode)) << "Failed to initialize VirtualSpace";
 244     vs.expand_by(commit_size, false);
 245 
 246     if (vs.special()) {
 247       EXPECT_EQ(reserve_size_aligned, vs.actual_committed_size());
 248     } else {
 249       EXPECT_GE(vs.actual_committed_size(), commit_size);
 250       // Approximate the commit granularity.
 251       // Make sure that we don't commit using large pages
 252       // if large pages has been disabled for this VirtualSpace.
 253       size_t commit_granularity = (mode == Disable || !UseLargePages) ?
 254                                    os::vm_page_size() : os::large_page_size();
 255       EXPECT_LT(vs.actual_committed_size(), commit_size + commit_granularity);
 256     }
 257   }
 258 };
 259 
 260 TEST_VM(VirtualSpace, actual_committed_space) {
 261   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(4 * K,  0));
 262   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(4 * K,  4 * K));
 263   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K,  0));
 264   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K,  4 * K));
 265   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(8 * K,  8 * K));
 266   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 0));
 267   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 4 * K));
 268   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 8 * K));
 269   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(12 * K, 12 * K));
 270   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 0));
 271   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 32 * K));
 272   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(64 * K, 64 * K));
 273   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  0));
 274   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  4 * K));
 275   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  64 * K));
 276   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  1 * M));
 277   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(2 * M,  2 * M));
 278   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0));
 279   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K));
 280   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K));
 281   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M));
 282   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M));
 283   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M));
 284   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M));
 285 }
 286 
 287 TEST_VM(VirtualSpace, actual_committed_space_one_large_page) {
 288   if (!UseLargePages) {
 289     return;
 290   }
 291 
 292   size_t large_page_size = os::large_page_size();
 293 
 294   ReservedSpace reserved(large_page_size, large_page_size, true, false);
 295   ReservedSpaceReleaser releaser(&reserved);
 296   ASSERT_TRUE(reserved.is_reserved());
 297 
 298   VirtualSpace vs;
 299   ASSERT_TRUE(vs.initialize(reserved, 0)) << "Failed to initialize VirtualSpace";
 300   vs.expand_by(large_page_size, false);
 301 
 302   EXPECT_EQ(large_page_size, vs.actual_committed_size());
 303 }
 304 
 305 TEST_VM(VirtualSpace, disable_large_pages) {
 306   if (!UseLargePages) {
 307     return;
 308   }
 309   // These test cases verify that if we force VirtualSpace to disable large pages
 310   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0,      Disable));
 311   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K,  Disable));
 312   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K,  Disable));
 313   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M,  Disable));
 314   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M,  Disable));
 315   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M,  Disable));
 316   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Disable));
 317 
 318   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0,      Reserve));
 319   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K,  Reserve));
 320   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K,  Reserve));
 321   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M,  Reserve));
 322   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M,  Reserve));
 323   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M,  Reserve));
 324   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Reserve));
 325 
 326   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 0,      Commit));
 327   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 4 * K,  Commit));
 328   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 8 * K,  Commit));
 329   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 1 * M,  Commit));
 330   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 2 * M,  Commit));
 331   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 5 * M,  Commit));
 332   EXPECT_NO_FATAL_FAILURE(test_virtual_space_actual_committed_space(10 * M, 10 * M, Commit));
 333 }