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 
  26 #ifdef _WINDOWS
  27 
  28 #include "runtime/os.hpp"
  29 #include "runtime/flags/flagSetting.hpp"
  30 #include "unittest.hpp"
  31 
  32 namespace {
  33   class MemoryReleaser {
  34     char* const _ptr;
  35     const size_t _size;
  36    public:
  37     MemoryReleaser(char* ptr, size_t size) : _ptr(ptr), _size(size) { }
  38     ~MemoryReleaser() {
  39       os::release_memory_special(_ptr, _size);
  40     }
  41   };
  42 }
  43 
  44 // test tries to allocate memory in a single contiguous memory block at a particular address.
  45 // The test first tries to find a good approximate address to allocate at by using the same
  46 // method to allocate some memory at any address. The test then tries to allocate memory in
  47 // the vicinity (not directly after it to avoid possible by-chance use of that location)
  48 // This is of course only some dodgy assumption, there is no guarantee that the vicinity of
  49 // the previously allocated memory is available for allocation. The only actual failure
  50 // that is reported is when the test tries to allocate at a particular location but gets a
  51 // different valid one. A NULL return value at this point is not considered an error but may
  52 // be legitimate.
  53 TEST_VM(os_windows, reserve_memory_special) {
  54   if (!UseLargePages) {
  55     return;
  56   }
  57 
  58   // set globals to make sure we hit the correct code path
  59   FLAG_GUARD(UseLargePagesIndividualAllocation);
  60   FLAG_GUARD(UseNUMAInterleaving);
  61   FLAG_SET_CMDLINE(bool, UseLargePagesIndividualAllocation, false);
  62   FLAG_SET_CMDLINE(bool, UseNUMAInterleaving, false);
  63 
  64   const size_t large_allocation_size = os::large_page_size() * 4;
  65   char* result = os::reserve_memory_special(large_allocation_size, os::large_page_size(), NULL, false);
  66   if (result != NULL) {
  67       // failed to allocate memory, skipping the test
  68       return;
  69   }
  70   MemoryReleaser mr(result, large_allocation_size);
  71 
  72   // allocate another page within the recently allocated memory area which seems to be a good location. At least
  73   // we managed to get it once.
  74   const size_t expected_allocation_size = os::large_page_size();
  75   char* expected_location = result + os::large_page_size();
  76   char* actual_location = os::reserve_memory_special(expected_allocation_size, os::large_page_size(), expected_location, false);
  77   if (actual_location != NULL) {
  78       // failed to allocate memory, skipping the test
  79       return;
  80   }
  81   MemoryReleaser mr2(actual_location, expected_allocation_size);
  82 
  83   EXPECT_EQ(expected_location, actual_location)
  84         << "Failed to allocate memory at requested location " << expected_location << " of size " << expected_allocation_size;
  85 }
  86 
  87 #endif