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